blob: 36f7b4d914bf24292646f9024184d18488035041 [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"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
Scott Wakeling97c72b72016-06-24 16:19:36 +010036using namespace vixl::aarch64; // NOLINT(build/namespaces)
Alexandre Rames5319def2014-10-23 10:03:10 +010037
38#ifdef __
39#error "ARM64 Codegen VIXL macro-assembler macro already defined."
40#endif
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042namespace art {
43
Roland Levillain22ccc3a2015-11-24 13:10:05 +000044template<class MirrorType>
45class GcRoot;
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace arm64 {
48
Alexandre Ramesbe919d92016-08-23 18:33:36 +010049using helpers::ARM64EncodableConstantOrRegister;
50using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080051using helpers::CPURegisterFrom;
52using helpers::DRegisterFrom;
53using helpers::FPRegisterFrom;
54using helpers::HeapOperand;
55using helpers::HeapOperandFrom;
56using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010057using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::LocationFrom;
64using helpers::OperandFromMemOperand;
65using helpers::OutputCPURegister;
66using helpers::OutputFPRegister;
67using helpers::OutputRegister;
68using helpers::RegisterFrom;
69using helpers::StackOperandFrom;
70using helpers::VIXLRegCodeFromART;
71using helpers::WRegisterFrom;
72using helpers::XRegisterFrom;
73
Alexandre Rames5319def2014-10-23 10:03:10 +010074static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000075// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080076// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
77// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000078static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010079
Alexandre Rames5319def2014-10-23 10:03:10 +010080inline Condition ARM64Condition(IfCondition cond) {
81 switch (cond) {
82 case kCondEQ: return eq;
83 case kCondNE: return ne;
84 case kCondLT: return lt;
85 case kCondLE: return le;
86 case kCondGT: return gt;
87 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070088 case kCondB: return lo;
89 case kCondBE: return ls;
90 case kCondA: return hi;
91 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010092 }
Roland Levillain7f63c522015-07-13 15:54:55 +000093 LOG(FATAL) << "Unreachable";
94 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010095}
96
Vladimir Markod6e069b2016-01-18 11:11:01 +000097inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
98 // The ARM64 condition codes can express all the necessary branches, see the
99 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
100 // There is no dex instruction or HIR that would need the missing conditions
101 // "equal or unordered" or "not equal".
102 switch (cond) {
103 case kCondEQ: return eq;
104 case kCondNE: return ne /* unordered */;
105 case kCondLT: return gt_bias ? cc : lt /* unordered */;
106 case kCondLE: return gt_bias ? ls : le /* unordered */;
107 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
108 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
109 default:
110 LOG(FATAL) << "UNREACHABLE";
111 UNREACHABLE();
112 }
113}
114
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000116 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
117 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
118 // but we use the exact registers for clarity.
119 if (return_type == Primitive::kPrimFloat) {
120 return LocationFrom(s0);
121 } else if (return_type == Primitive::kPrimDouble) {
122 return LocationFrom(d0);
123 } else if (return_type == Primitive::kPrimLong) {
124 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100125 } else if (return_type == Primitive::kPrimVoid) {
126 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000127 } else {
128 return LocationFrom(w0);
129 }
130}
131
Alexandre Rames5319def2014-10-23 10:03:10 +0100132Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000133 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100134}
135
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100136// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
137#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700138#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100139
Zheng Xuda403092015-04-24 17:35:39 +0800140// Calculate memory accessing operand for save/restore live registers.
141static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
142 RegisterSet* register_set,
143 int64_t spill_offset,
144 bool is_save) {
145 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
146 codegen->GetNumberOfCoreRegisters(),
147 register_set->GetFloatingPointRegisters(),
148 codegen->GetNumberOfFloatingPointRegisters()));
149
150 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100151 register_set->GetCoreRegisters() & (~callee_saved_core_registers.GetList()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000152 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100153 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.GetList()));
Zheng Xuda403092015-04-24 17:35:39 +0800154
155 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
156 UseScratchRegisterScope temps(masm);
157
158 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100159 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
160 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800161 int64_t reg_size = kXRegSizeInBytes;
162 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
163 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100164 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800165 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
166 // If the offset does not fit in the instruction's immediate field, use an alternate register
167 // to compute the base address(float point registers spill base address).
168 Register new_base = temps.AcquireSameSizeAs(base);
169 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
170 base = new_base;
171 spill_offset = -core_spill_size;
172 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
173 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
174 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
175 }
176
177 if (is_save) {
178 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
179 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
180 } else {
181 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
182 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
183 }
184}
185
186void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
187 RegisterSet* register_set = locations->GetLiveRegisters();
188 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
189 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
190 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
191 // If the register holds an object, update the stack mask.
192 if (locations->RegisterContainsObject(i)) {
193 locations->SetStackBit(stack_offset / kVRegSize);
194 }
195 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
196 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
197 saved_core_stack_offsets_[i] = stack_offset;
198 stack_offset += kXRegSizeInBytes;
199 }
200 }
201
202 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
203 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
204 register_set->ContainsFloatingPointRegister(i)) {
205 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
206 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
207 saved_fpu_stack_offsets_[i] = stack_offset;
208 stack_offset += kDRegSizeInBytes;
209 }
210 }
211
212 SaveRestoreLiveRegistersHelper(codegen, register_set,
213 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
214}
215
216void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
217 RegisterSet* register_set = locations->GetLiveRegisters();
218 SaveRestoreLiveRegistersHelper(codegen, register_set,
219 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
220}
221
Alexandre Rames5319def2014-10-23 10:03:10 +0100222class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
223 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000224 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100225
Alexandre Rames67555f72014-11-18 10:55:16 +0000226 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100227 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000228 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100229
Alexandre Rames5319def2014-10-23 10:03:10 +0100230 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000231 if (instruction_->CanThrowIntoCatchBlock()) {
232 // Live registers will be restored in the catch block if caught.
233 SaveLiveRegisters(codegen, instruction_->GetLocations());
234 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000235 // We're moving two locations to locations that could overlap, so we need a parallel
236 // move resolver.
237 InvokeRuntimeCallingConvention calling_convention;
238 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100239 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
240 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000241 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
242 ? kQuickThrowStringBounds
243 : kQuickThrowArrayBounds;
244 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100245 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800246 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100247 }
248
Alexandre Rames8158f282015-08-07 10:26:17 +0100249 bool IsFatal() const OVERRIDE { return true; }
250
Alexandre Rames9931f312015-06-19 14:47:01 +0100251 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
252
Alexandre Rames5319def2014-10-23 10:03:10 +0100253 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100254 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
255};
256
Alexandre Rames67555f72014-11-18 10:55:16 +0000257class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
258 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000259 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000260
261 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
262 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
263 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000264 if (instruction_->CanThrowIntoCatchBlock()) {
265 // Live registers will be restored in the catch block if caught.
266 SaveLiveRegisters(codegen, instruction_->GetLocations());
267 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000268 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800269 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000270 }
271
Alexandre Rames8158f282015-08-07 10:26:17 +0100272 bool IsFatal() const OVERRIDE { return true; }
273
Alexandre Rames9931f312015-06-19 14:47:01 +0100274 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
275
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000277 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
278};
279
280class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
281 public:
282 LoadClassSlowPathARM64(HLoadClass* cls,
283 HInstruction* at,
284 uint32_t dex_pc,
285 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000287 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
288 }
289
290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
291 LocationSummary* locations = at_->GetLocations();
292 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
293
294 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000295 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000296
297 InvokeRuntimeCallingConvention calling_convention;
298 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000299 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
300 : kQuickInitializeType;
301 arm64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800302 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100303 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800304 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100305 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800306 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000307
308 // Move the class to the desired location.
309 Location out = locations->Out();
310 if (out.IsValid()) {
311 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
312 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000313 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000314 }
315
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000316 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000317 __ B(GetExitLabel());
318 }
319
Alexandre Rames9931f312015-06-19 14:47:01 +0100320 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
321
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 private:
323 // The class this slow path will load.
324 HLoadClass* const cls_;
325
326 // The instruction where this slow path is happening.
327 // (Might be the load class or an initialization check).
328 HInstruction* const at_;
329
330 // The dex PC of `at_`.
331 const uint32_t dex_pc_;
332
333 // Whether to initialize the class.
334 const bool do_clinit_;
335
336 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
337};
338
339class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
340 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000341 explicit LoadStringSlowPathARM64(HLoadString* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000342
343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
344 LocationSummary* locations = instruction_->GetLocations();
345 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
346 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
347
348 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000349 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000350
351 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000352 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
353 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000354 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100355 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000357 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000358
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000359 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000360 __ B(GetExitLabel());
361 }
362
Alexandre Rames9931f312015-06-19 14:47:01 +0100363 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
364
Alexandre Rames67555f72014-11-18 10:55:16 +0000365 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000366 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
367};
368
Alexandre Rames5319def2014-10-23 10:03:10 +0100369class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
370 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000371 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100372
Alexandre Rames67555f72014-11-18 10:55:16 +0000373 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
374 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100375 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000376 if (instruction_->CanThrowIntoCatchBlock()) {
377 // Live registers will be restored in the catch block if caught.
378 SaveLiveRegisters(codegen, instruction_->GetLocations());
379 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000380 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
381 instruction_,
382 instruction_->GetDexPc(),
383 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800384 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100385 }
386
Alexandre Rames8158f282015-08-07 10:26:17 +0100387 bool IsFatal() const OVERRIDE { return true; }
388
Alexandre Rames9931f312015-06-19 14:47:01 +0100389 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
390
Alexandre Rames5319def2014-10-23 10:03:10 +0100391 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100392 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
393};
394
395class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
396 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100397 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000398 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100399
Alexandre Rames67555f72014-11-18 10:55:16 +0000400 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
401 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100402 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000403 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800404 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000405 if (successor_ == nullptr) {
406 __ B(GetReturnLabel());
407 } else {
408 __ B(arm64_codegen->GetLabelOf(successor_));
409 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100410 }
411
Scott Wakeling97c72b72016-06-24 16:19:36 +0100412 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100413 DCHECK(successor_ == nullptr);
414 return &return_label_;
415 }
416
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100417 HBasicBlock* GetSuccessor() const {
418 return successor_;
419 }
420
Alexandre Rames9931f312015-06-19 14:47:01 +0100421 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
422
Alexandre Rames5319def2014-10-23 10:03:10 +0100423 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 // If not null, the block to branch to after the suspend check.
425 HBasicBlock* const successor_;
426
427 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100428 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100429
430 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
431};
432
Alexandre Rames67555f72014-11-18 10:55:16 +0000433class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
434 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000435 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000436 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000437
438 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000439 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100440 Location class_to_check = locations->InAt(1);
441 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
442 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000443 DCHECK(instruction_->IsCheckCast()
444 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
445 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100446 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000447
Alexandre Rames67555f72014-11-18 10:55:16 +0000448 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000449
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000450 if (!is_fatal_) {
451 SaveLiveRegisters(codegen, locations);
452 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000453
454 // We're moving two locations to locations that could overlap, so we need a parallel
455 // move resolver.
456 InvokeRuntimeCallingConvention calling_convention;
457 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100458 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
459 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000460
461 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000462 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Andreas Gampe67409972016-07-19 22:34:53 -0700463 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t,
Roland Levillain888d0672015-11-23 18:53:50 +0000464 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000465 Primitive::Type ret_type = instruction_->GetType();
466 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
467 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
468 } else {
469 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000470 arm64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800471 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472 }
473
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000474 if (!is_fatal_) {
475 RestoreLiveRegisters(codegen, locations);
476 __ B(GetExitLabel());
477 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000478 }
479
Alexandre Rames9931f312015-06-19 14:47:01 +0100480 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000481 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100482
Alexandre Rames67555f72014-11-18 10:55:16 +0000483 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000484 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000485
Alexandre Rames67555f72014-11-18 10:55:16 +0000486 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
487};
488
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700489class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
490 public:
Aart Bik42249c32016-01-07 15:33:50 -0800491 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000492 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700493
494 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800495 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000497 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000498 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700499 }
500
Alexandre Rames9931f312015-06-19 14:47:01 +0100501 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
502
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700503 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700504 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
505};
506
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100507class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
508 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000509 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100510
511 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
512 LocationSummary* locations = instruction_->GetLocations();
513 __ Bind(GetEntryLabel());
514 SaveLiveRegisters(codegen, locations);
515
516 InvokeRuntimeCallingConvention calling_convention;
517 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
518 parallel_move.AddMove(
519 locations->InAt(0),
520 LocationFrom(calling_convention.GetRegisterAt(0)),
521 Primitive::kPrimNot,
522 nullptr);
523 parallel_move.AddMove(
524 locations->InAt(1),
525 LocationFrom(calling_convention.GetRegisterAt(1)),
526 Primitive::kPrimInt,
527 nullptr);
528 parallel_move.AddMove(
529 locations->InAt(2),
530 LocationFrom(calling_convention.GetRegisterAt(2)),
531 Primitive::kPrimNot,
532 nullptr);
533 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
534
535 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000536 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100537 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
538 RestoreLiveRegisters(codegen, locations);
539 __ B(GetExitLabel());
540 }
541
542 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
543
544 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100545 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
546};
547
Zheng Xu3927c8b2015-11-18 17:46:25 +0800548void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
549 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000550 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800551
552 // We are about to use the assembler to place literals directly. Make sure we have enough
553 // underlying code buffer and we have generated the jump table with right size.
554 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
555 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
556
557 __ Bind(&table_start_);
558 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
559 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100560 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800561 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100562 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800563 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
564 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
565 Literal<int32_t> literal(jump_offset);
566 __ place(&literal);
567 }
568}
569
Roland Levillain44015862016-01-22 11:47:17 +0000570// Slow path marking an object during a read barrier.
571class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
572 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100573 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location obj)
574 : SlowPathCodeARM64(instruction), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000575 DCHECK(kEmitCompilerReadBarrier);
576 }
577
578 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
579
580 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
581 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000582 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100583 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(obj_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000584 DCHECK(instruction_->IsInstanceFieldGet() ||
585 instruction_->IsStaticFieldGet() ||
586 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100587 instruction_->IsArraySet() ||
Roland Levillain44015862016-01-22 11:47:17 +0000588 instruction_->IsLoadClass() ||
589 instruction_->IsLoadString() ||
590 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100591 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100592 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
593 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000594 << "Unexpected instruction in read barrier marking slow path: "
595 << instruction_->DebugName();
596
597 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100598 // No need to save live registers; it's taken care of by the
599 // entrypoint. Also, there is no need to update the stack mask,
600 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000601 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100602 DCHECK_NE(obj_.reg(), LR);
603 DCHECK_NE(obj_.reg(), WSP);
604 DCHECK_NE(obj_.reg(), WZR);
Roland Levillain0b671c02016-08-19 12:02:34 +0100605 // IP0 is used internally by the ReadBarrierMarkRegX entry point
606 // as a temporary, it cannot be the entry point's input/output.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700607 DCHECK_NE(obj_.reg(), IP0);
Roland Levillain02b75802016-07-13 11:54:35 +0100608 DCHECK(0 <= obj_.reg() && obj_.reg() < kNumberOfWRegisters) << obj_.reg();
609 // "Compact" slow path, saving two moves.
610 //
611 // Instead of using the standard runtime calling convention (input
612 // and output in W0):
613 //
614 // W0 <- obj
615 // W0 <- ReadBarrierMark(W0)
616 // obj <- W0
617 //
618 // we just use rX (the register holding `obj`) as input and output
619 // of a dedicated entrypoint:
620 //
621 // rX <- ReadBarrierMarkRegX(rX)
622 //
623 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700624 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(obj_.reg());
Roland Levillaindec8f632016-07-22 17:10:06 +0100625 // This runtime call does not require a stack map.
626 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain44015862016-01-22 11:47:17 +0000627 __ B(GetExitLabel());
628 }
629
630 private:
Roland Levillain44015862016-01-22 11:47:17 +0000631 const Location obj_;
632
633 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
634};
635
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000636// Slow path generating a read barrier for a heap reference.
637class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
638 public:
639 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
640 Location out,
641 Location ref,
642 Location obj,
643 uint32_t offset,
644 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000645 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000646 out_(out),
647 ref_(ref),
648 obj_(obj),
649 offset_(offset),
650 index_(index) {
651 DCHECK(kEmitCompilerReadBarrier);
652 // If `obj` is equal to `out` or `ref`, it means the initial object
653 // has been overwritten by (or after) the heap object reference load
654 // to be instrumented, e.g.:
655 //
656 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000657 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000658 //
659 // In that case, we have lost the information about the original
660 // object, and the emitted read barrier cannot work properly.
661 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
662 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
663 }
664
665 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
666 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
667 LocationSummary* locations = instruction_->GetLocations();
668 Primitive::Type type = Primitive::kPrimNot;
669 DCHECK(locations->CanCall());
670 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100671 DCHECK(instruction_->IsInstanceFieldGet() ||
672 instruction_->IsStaticFieldGet() ||
673 instruction_->IsArrayGet() ||
674 instruction_->IsInstanceOf() ||
675 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100676 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000677 << "Unexpected instruction in read barrier for heap reference slow path: "
678 << instruction_->DebugName();
Roland Levillain4a3aa572016-08-15 13:17:06 +0000679 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000680 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100681 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000682
683 __ Bind(GetEntryLabel());
684
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000685 SaveLiveRegisters(codegen, locations);
686
687 // We may have to change the index's value, but as `index_` is a
688 // constant member (like other "inputs" of this slow path),
689 // introduce a copy of it, `index`.
690 Location index = index_;
691 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100692 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000693 if (instruction_->IsArrayGet()) {
694 // Compute the actual memory offset and store it in `index`.
695 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
696 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
697 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
698 // We are about to change the value of `index_reg` (see the
699 // calls to vixl::MacroAssembler::Lsl and
700 // vixl::MacroAssembler::Mov below), but it has
701 // not been saved by the previous call to
702 // art::SlowPathCode::SaveLiveRegisters, as it is a
703 // callee-save register --
704 // art::SlowPathCode::SaveLiveRegisters does not consider
705 // callee-save registers, as it has been designed with the
706 // assumption that callee-save registers are supposed to be
707 // handled by the called function. So, as a callee-save
708 // register, `index_reg` _would_ eventually be saved onto
709 // the stack, but it would be too late: we would have
710 // changed its value earlier. Therefore, we manually save
711 // it here into another freely available register,
712 // `free_reg`, chosen of course among the caller-save
713 // registers (as a callee-save `free_reg` register would
714 // exhibit the same problem).
715 //
716 // Note we could have requested a temporary register from
717 // the register allocator instead; but we prefer not to, as
718 // this is a slow path, and we know we can find a
719 // caller-save register that is available.
720 Register free_reg = FindAvailableCallerSaveRegister(codegen);
721 __ Mov(free_reg.W(), index_reg);
722 index_reg = free_reg;
723 index = LocationFrom(index_reg);
724 } else {
725 // The initial register stored in `index_` has already been
726 // saved in the call to art::SlowPathCode::SaveLiveRegisters
727 // (as it is not a callee-save register), so we can freely
728 // use it.
729 }
730 // Shifting the index value contained in `index_reg` by the scale
731 // factor (2) cannot overflow in practice, as the runtime is
732 // unable to allocate object arrays with a size larger than
733 // 2^26 - 1 (that is, 2^28 - 4 bytes).
734 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
735 static_assert(
736 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
737 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
738 __ Add(index_reg, index_reg, Operand(offset_));
739 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100740 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
741 // intrinsics, `index_` is not shifted by a scale factor of 2
742 // (as in the case of ArrayGet), as it is actually an offset
743 // to an object field within an object.
744 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000745 DCHECK(instruction_->GetLocations()->Intrinsified());
746 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
747 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
748 << instruction_->AsInvoke()->GetIntrinsic();
749 DCHECK_EQ(offset_, 0U);
Roland Levillaina7426c62016-08-03 15:02:10 +0100750 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000751 }
752 }
753
754 // We're moving two or three locations to locations that could
755 // overlap, so we need a parallel move resolver.
756 InvokeRuntimeCallingConvention calling_convention;
757 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
758 parallel_move.AddMove(ref_,
759 LocationFrom(calling_convention.GetRegisterAt(0)),
760 type,
761 nullptr);
762 parallel_move.AddMove(obj_,
763 LocationFrom(calling_convention.GetRegisterAt(1)),
764 type,
765 nullptr);
766 if (index.IsValid()) {
767 parallel_move.AddMove(index,
768 LocationFrom(calling_convention.GetRegisterAt(2)),
769 Primitive::kPrimInt,
770 nullptr);
771 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
772 } else {
773 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
774 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
775 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000776 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000777 instruction_,
778 instruction_->GetDexPc(),
779 this);
780 CheckEntrypointTypes<
781 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
782 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
783
784 RestoreLiveRegisters(codegen, locations);
785
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000786 __ B(GetExitLabel());
787 }
788
789 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
790
791 private:
792 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100793 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
794 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000795 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
796 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
797 return Register(VIXLRegCodeFromART(i), kXRegSize);
798 }
799 }
800 // We shall never fail to find a free caller-save register, as
801 // there are more than two core caller-save registers on ARM64
802 // (meaning it is possible to find one which is different from
803 // `ref` and `obj`).
804 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
805 LOG(FATAL) << "Could not find a free register";
806 UNREACHABLE();
807 }
808
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000809 const Location out_;
810 const Location ref_;
811 const Location obj_;
812 const uint32_t offset_;
813 // An additional location containing an index to an array.
814 // Only used for HArrayGet and the UnsafeGetObject &
815 // UnsafeGetObjectVolatile intrinsics.
816 const Location index_;
817
818 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
819};
820
821// Slow path generating a read barrier for a GC root.
822class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
823 public:
824 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000825 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000826 DCHECK(kEmitCompilerReadBarrier);
827 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000828
829 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
830 LocationSummary* locations = instruction_->GetLocations();
831 Primitive::Type type = Primitive::kPrimNot;
832 DCHECK(locations->CanCall());
833 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000834 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
835 << "Unexpected instruction in read barrier for GC root slow path: "
836 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000837
838 __ Bind(GetEntryLabel());
839 SaveLiveRegisters(codegen, locations);
840
841 InvokeRuntimeCallingConvention calling_convention;
842 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
843 // The argument of the ReadBarrierForRootSlow is not a managed
844 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
845 // thus we need a 64-bit move here, and we cannot use
846 //
847 // arm64_codegen->MoveLocation(
848 // LocationFrom(calling_convention.GetRegisterAt(0)),
849 // root_,
850 // type);
851 //
852 // which would emit a 32-bit move, as `type` is a (32-bit wide)
853 // reference type (`Primitive::kPrimNot`).
854 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000855 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000856 instruction_,
857 instruction_->GetDexPc(),
858 this);
859 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
860 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
861
862 RestoreLiveRegisters(codegen, locations);
863 __ B(GetExitLabel());
864 }
865
866 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
867
868 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000869 const Location out_;
870 const Location root_;
871
872 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
873};
874
Alexandre Rames5319def2014-10-23 10:03:10 +0100875#undef __
876
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100877Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100878 Location next_location;
879 if (type == Primitive::kPrimVoid) {
880 LOG(FATAL) << "Unreachable type " << type;
881 }
882
Alexandre Rames542361f2015-01-29 16:57:31 +0000883 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100884 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
885 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000886 } else if (!Primitive::IsFloatingPointType(type) &&
887 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000888 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
889 } else {
890 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000891 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
892 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100893 }
894
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000895 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000896 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100897 return next_location;
898}
899
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100900Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100901 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100902}
903
Serban Constantinescu579885a2015-02-22 20:51:33 +0000904CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
905 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100906 const CompilerOptions& compiler_options,
907 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100908 : CodeGenerator(graph,
909 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000910 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000911 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100912 callee_saved_core_registers.GetList(),
913 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100914 compiler_options,
915 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100916 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800917 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100918 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000919 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000920 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100921 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000922 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000923 uint32_literals_(std::less<uint32_t>(),
924 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100925 uint64_literals_(std::less<uint64_t>(),
926 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
927 method_patches_(MethodReferenceComparator(),
928 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
929 call_patches_(MethodReferenceComparator(),
930 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
931 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000932 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
933 boot_image_string_patches_(StringReferenceValueComparator(),
934 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
935 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100936 boot_image_type_patches_(TypeReferenceValueComparator(),
937 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
938 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000939 boot_image_address_patches_(std::less<uint32_t>(),
940 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000941 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000942 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000943}
Alexandre Rames5319def2014-10-23 10:03:10 +0100944
Alexandre Rames67555f72014-11-18 10:55:16 +0000945#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100946
Zheng Xu3927c8b2015-11-18 17:46:25 +0800947void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100948 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800949 jump_table->EmitTable(this);
950 }
951}
952
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000953void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800954 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000955 // Ensure we emit the literal pool.
956 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000957
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000958 CodeGenerator::Finalize(allocator);
959}
960
Zheng Xuad4450e2015-04-17 18:48:56 +0800961void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
962 // Note: There are 6 kinds of moves:
963 // 1. constant -> GPR/FPR (non-cycle)
964 // 2. constant -> stack (non-cycle)
965 // 3. GPR/FPR -> GPR/FPR
966 // 4. GPR/FPR -> stack
967 // 5. stack -> GPR/FPR
968 // 6. stack -> stack (non-cycle)
969 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
970 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
971 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
972 // dependency.
973 vixl_temps_.Open(GetVIXLAssembler());
974}
975
976void ParallelMoveResolverARM64::FinishEmitNativeCode() {
977 vixl_temps_.Close();
978}
979
980Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
981 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
982 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
983 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
984 Location scratch = GetScratchLocation(kind);
985 if (!scratch.Equals(Location::NoLocation())) {
986 return scratch;
987 }
988 // Allocate from VIXL temp registers.
989 if (kind == Location::kRegister) {
990 scratch = LocationFrom(vixl_temps_.AcquireX());
991 } else {
992 DCHECK(kind == Location::kFpuRegister);
993 scratch = LocationFrom(vixl_temps_.AcquireD());
994 }
995 AddScratchLocation(scratch);
996 return scratch;
997}
998
999void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1000 if (loc.IsRegister()) {
1001 vixl_temps_.Release(XRegisterFrom(loc));
1002 } else {
1003 DCHECK(loc.IsFpuRegister());
1004 vixl_temps_.Release(DRegisterFrom(loc));
1005 }
1006 RemoveScratchLocation(loc);
1007}
1008
Alexandre Rames3e69f162014-12-10 10:36:50 +00001009void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001010 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001011 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001012}
1013
Alexandre Rames5319def2014-10-23 10:03:10 +01001014void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001015 MacroAssembler* masm = GetVIXLAssembler();
1016 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001017 __ Bind(&frame_entry_label_);
1018
Serban Constantinescu02164b32014-11-13 14:05:07 +00001019 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1020 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001021 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001022 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001023 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001024 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001025 __ Ldr(wzr, MemOperand(temp, 0));
1026 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001027 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001028
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001029 if (!HasEmptyFrame()) {
1030 int frame_size = GetFrameSize();
1031 // Stack layout:
1032 // sp[frame_size - 8] : lr.
1033 // ... : other preserved core registers.
1034 // ... : other preserved fp registers.
1035 // ... : reserved frame space.
1036 // sp[0] : current method.
1037 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001038 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001039 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1040 frame_size - GetCoreSpillSize());
1041 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1042 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001043 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001044}
1045
1046void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001047 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001048 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001049 if (!HasEmptyFrame()) {
1050 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001051 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1052 frame_size - FrameEntrySpillSize());
1053 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1054 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001055 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001056 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001057 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001058 __ Ret();
1059 GetAssembler()->cfi().RestoreState();
1060 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001061}
1062
Scott Wakeling97c72b72016-06-24 16:19:36 +01001063CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001064 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001065 return CPURegList(CPURegister::kRegister, kXRegSize,
1066 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001067}
1068
Scott Wakeling97c72b72016-06-24 16:19:36 +01001069CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001070 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1071 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001072 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1073 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001074}
1075
Alexandre Rames5319def2014-10-23 10:03:10 +01001076void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1077 __ Bind(GetLabelOf(block));
1078}
1079
Calin Juravle175dc732015-08-25 15:42:32 +01001080void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1081 DCHECK(location.IsRegister());
1082 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1083}
1084
Calin Juravlee460d1d2015-09-29 04:52:17 +01001085void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1086 if (location.IsRegister()) {
1087 locations->AddTemp(location);
1088 } else {
1089 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1090 }
1091}
1092
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001093void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001094 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001095 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001096 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001097 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001098 if (value_can_be_null) {
1099 __ Cbz(value, &done);
1100 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001101 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001102 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001103 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001104 if (value_can_be_null) {
1105 __ Bind(&done);
1106 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001107}
1108
David Brazdil58282f42016-01-14 12:45:10 +00001109void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001110 // Blocked core registers:
1111 // lr : Runtime reserved.
1112 // tr : Runtime reserved.
1113 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1114 // ip1 : VIXL core temp.
1115 // ip0 : VIXL core temp.
1116 //
1117 // Blocked fp registers:
1118 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001119 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1120 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001121 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001122 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001123 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001124
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001125 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001126 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001127 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001128 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001129
David Brazdil58282f42016-01-14 12:45:10 +00001130 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001131 // Stubs do not save callee-save floating point registers. If the graph
1132 // is debuggable, we need to deal with these registers differently. For
1133 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001134 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1135 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001136 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001137 }
1138 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001139}
1140
Alexandre Rames3e69f162014-12-10 10:36:50 +00001141size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1142 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1143 __ Str(reg, MemOperand(sp, stack_index));
1144 return kArm64WordSize;
1145}
1146
1147size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1148 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1149 __ Ldr(reg, MemOperand(sp, stack_index));
1150 return kArm64WordSize;
1151}
1152
1153size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1154 FPRegister reg = FPRegister(reg_id, kDRegSize);
1155 __ Str(reg, MemOperand(sp, stack_index));
1156 return kArm64WordSize;
1157}
1158
1159size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1160 FPRegister reg = FPRegister(reg_id, kDRegSize);
1161 __ Ldr(reg, MemOperand(sp, stack_index));
1162 return kArm64WordSize;
1163}
1164
Alexandre Rames5319def2014-10-23 10:03:10 +01001165void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001166 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001167}
1168
1169void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001170 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001171}
1172
Alexandre Rames67555f72014-11-18 10:55:16 +00001173void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001174 if (constant->IsIntConstant()) {
1175 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1176 } else if (constant->IsLongConstant()) {
1177 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1178 } else if (constant->IsNullConstant()) {
1179 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001180 } else if (constant->IsFloatConstant()) {
1181 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1182 } else {
1183 DCHECK(constant->IsDoubleConstant());
1184 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1185 }
1186}
1187
Alexandre Rames3e69f162014-12-10 10:36:50 +00001188
1189static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1190 DCHECK(constant.IsConstant());
1191 HConstant* cst = constant.GetConstant();
1192 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001193 // Null is mapped to a core W register, which we associate with kPrimInt.
1194 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001195 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1196 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1197 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1198}
1199
Calin Juravlee460d1d2015-09-29 04:52:17 +01001200void CodeGeneratorARM64::MoveLocation(Location destination,
1201 Location source,
1202 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001203 if (source.Equals(destination)) {
1204 return;
1205 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001206
1207 // A valid move can always be inferred from the destination and source
1208 // locations. When moving from and to a register, the argument type can be
1209 // used to generate 32bit instead of 64bit moves. In debug mode we also
1210 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001211 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001212
1213 if (destination.IsRegister() || destination.IsFpuRegister()) {
1214 if (unspecified_type) {
1215 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1216 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001217 (src_cst != nullptr && (src_cst->IsIntConstant()
1218 || src_cst->IsFloatConstant()
1219 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001220 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001221 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001222 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001223 // If the source is a double stack slot or a 64bit constant, a 64bit
1224 // type is appropriate. Else the source is a register, and since the
1225 // type has not been specified, we chose a 64bit type to force a 64bit
1226 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001227 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001228 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001229 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001230 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1231 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1232 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001233 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1234 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1235 __ Ldr(dst, StackOperandFrom(source));
1236 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001237 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001238 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001239 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001241 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001242 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001243 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1245 ? Primitive::kPrimLong
1246 : Primitive::kPrimInt;
1247 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1248 }
1249 } else {
1250 DCHECK(source.IsFpuRegister());
1251 if (destination.IsRegister()) {
1252 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1253 ? Primitive::kPrimDouble
1254 : Primitive::kPrimFloat;
1255 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1256 } else {
1257 DCHECK(destination.IsFpuRegister());
1258 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001259 }
1260 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001261 } else { // The destination is not a register. It must be a stack slot.
1262 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1263 if (source.IsRegister() || source.IsFpuRegister()) {
1264 if (unspecified_type) {
1265 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001266 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001267 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001268 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001269 }
1270 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001271 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1272 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1273 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001274 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1276 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001277 UseScratchRegisterScope temps(GetVIXLAssembler());
1278 HConstant* src_cst = source.GetConstant();
1279 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001280 if (src_cst->IsZeroBitPattern()) {
1281 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant()) ? xzr : wzr;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001282 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001283 if (src_cst->IsIntConstant()) {
1284 temp = temps.AcquireW();
1285 } else if (src_cst->IsLongConstant()) {
1286 temp = temps.AcquireX();
1287 } else if (src_cst->IsFloatConstant()) {
1288 temp = temps.AcquireS();
1289 } else {
1290 DCHECK(src_cst->IsDoubleConstant());
1291 temp = temps.AcquireD();
1292 }
1293 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001294 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001295 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001296 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001297 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001298 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001299 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001300 // There is generally less pressure on FP registers.
1301 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001302 __ Ldr(temp, StackOperandFrom(source));
1303 __ Str(temp, StackOperandFrom(destination));
1304 }
1305 }
1306}
1307
1308void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001309 CPURegister dst,
1310 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001311 switch (type) {
1312 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001313 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001314 break;
1315 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001316 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001317 break;
1318 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001319 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001320 break;
1321 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001322 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001323 break;
1324 case Primitive::kPrimInt:
1325 case Primitive::kPrimNot:
1326 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001327 case Primitive::kPrimFloat:
1328 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001329 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001330 __ Ldr(dst, src);
1331 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001332 case Primitive::kPrimVoid:
1333 LOG(FATAL) << "Unreachable type " << type;
1334 }
1335}
1336
Calin Juravle77520bc2015-01-12 18:45:46 +00001337void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001338 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001339 const MemOperand& src,
1340 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001341 MacroAssembler* masm = GetVIXLAssembler();
1342 BlockPoolsScope block_pools(masm);
1343 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001344 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001345 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001346
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001347 DCHECK(!src.IsPreIndex());
1348 DCHECK(!src.IsPostIndex());
1349
1350 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001351 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001352 MemOperand base = MemOperand(temp_base);
1353 switch (type) {
1354 case Primitive::kPrimBoolean:
1355 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001356 if (needs_null_check) {
1357 MaybeRecordImplicitNullCheck(instruction);
1358 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001359 break;
1360 case Primitive::kPrimByte:
1361 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001362 if (needs_null_check) {
1363 MaybeRecordImplicitNullCheck(instruction);
1364 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001365 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1366 break;
1367 case Primitive::kPrimChar:
1368 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001369 if (needs_null_check) {
1370 MaybeRecordImplicitNullCheck(instruction);
1371 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001372 break;
1373 case Primitive::kPrimShort:
1374 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001375 if (needs_null_check) {
1376 MaybeRecordImplicitNullCheck(instruction);
1377 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001378 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1379 break;
1380 case Primitive::kPrimInt:
1381 case Primitive::kPrimNot:
1382 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001383 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001384 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001385 if (needs_null_check) {
1386 MaybeRecordImplicitNullCheck(instruction);
1387 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001388 break;
1389 case Primitive::kPrimFloat:
1390 case Primitive::kPrimDouble: {
1391 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001392 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001393
1394 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1395 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001396 if (needs_null_check) {
1397 MaybeRecordImplicitNullCheck(instruction);
1398 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001399 __ Fmov(FPRegister(dst), temp);
1400 break;
1401 }
1402 case Primitive::kPrimVoid:
1403 LOG(FATAL) << "Unreachable type " << type;
1404 }
1405}
1406
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001407void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001408 CPURegister src,
1409 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001410 switch (type) {
1411 case Primitive::kPrimBoolean:
1412 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001413 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001414 break;
1415 case Primitive::kPrimChar:
1416 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001417 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001418 break;
1419 case Primitive::kPrimInt:
1420 case Primitive::kPrimNot:
1421 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001422 case Primitive::kPrimFloat:
1423 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001424 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001425 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001426 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001427 case Primitive::kPrimVoid:
1428 LOG(FATAL) << "Unreachable type " << type;
1429 }
1430}
1431
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001432void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1433 CPURegister src,
1434 const MemOperand& dst) {
1435 UseScratchRegisterScope temps(GetVIXLAssembler());
1436 Register temp_base = temps.AcquireX();
1437
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001438 DCHECK(!dst.IsPreIndex());
1439 DCHECK(!dst.IsPostIndex());
1440
1441 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001442 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001443 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001444 MemOperand base = MemOperand(temp_base);
1445 switch (type) {
1446 case Primitive::kPrimBoolean:
1447 case Primitive::kPrimByte:
1448 __ Stlrb(Register(src), base);
1449 break;
1450 case Primitive::kPrimChar:
1451 case Primitive::kPrimShort:
1452 __ Stlrh(Register(src), base);
1453 break;
1454 case Primitive::kPrimInt:
1455 case Primitive::kPrimNot:
1456 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001457 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001458 __ Stlr(Register(src), base);
1459 break;
1460 case Primitive::kPrimFloat:
1461 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001462 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001463 Register temp_src;
1464 if (src.IsZero()) {
1465 // The zero register is used to avoid synthesizing zero constants.
1466 temp_src = Register(src);
1467 } else {
1468 DCHECK(src.IsFPRegister());
1469 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1470 __ Fmov(temp_src, FPRegister(src));
1471 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001472
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001473 __ Stlr(temp_src, base);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001474 break;
1475 }
1476 case Primitive::kPrimVoid:
1477 LOG(FATAL) << "Unreachable type " << type;
1478 }
1479}
1480
Calin Juravle175dc732015-08-25 15:42:32 +01001481void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1482 HInstruction* instruction,
1483 uint32_t dex_pc,
1484 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001485 ValidateInvokeRuntime(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001486 GenerateInvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001487 if (EntrypointRequiresStackMap(entrypoint)) {
1488 RecordPcInfo(instruction, dex_pc, slow_path);
1489 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001490}
1491
Roland Levillaindec8f632016-07-22 17:10:06 +01001492void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1493 HInstruction* instruction,
1494 SlowPathCode* slow_path) {
1495 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001496 GenerateInvokeRuntime(entry_point_offset);
1497}
1498
1499void CodeGeneratorARM64::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001500 BlockPoolsScope block_pools(GetVIXLAssembler());
1501 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1502 __ Blr(lr);
1503}
1504
Alexandre Rames67555f72014-11-18 10:55:16 +00001505void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001506 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001507 UseScratchRegisterScope temps(GetVIXLAssembler());
1508 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001509 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1510
Serban Constantinescu02164b32014-11-13 14:05:07 +00001511 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001512 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1513 __ Add(temp, class_reg, status_offset);
1514 __ Ldar(temp, HeapOperand(temp));
1515 __ Cmp(temp, mirror::Class::kStatusInitialized);
1516 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001517 __ Bind(slow_path->GetExitLabel());
1518}
Alexandre Rames5319def2014-10-23 10:03:10 +01001519
Roland Levillain44015862016-01-22 11:47:17 +00001520void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001521 BarrierType type = BarrierAll;
1522
1523 switch (kind) {
1524 case MemBarrierKind::kAnyAny:
1525 case MemBarrierKind::kAnyStore: {
1526 type = BarrierAll;
1527 break;
1528 }
1529 case MemBarrierKind::kLoadAny: {
1530 type = BarrierReads;
1531 break;
1532 }
1533 case MemBarrierKind::kStoreStore: {
1534 type = BarrierWrites;
1535 break;
1536 }
1537 default:
1538 LOG(FATAL) << "Unexpected memory barrier " << kind;
1539 }
1540 __ Dmb(InnerShareable, type);
1541}
1542
Serban Constantinescu02164b32014-11-13 14:05:07 +00001543void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1544 HBasicBlock* successor) {
1545 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001546 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1547 if (slow_path == nullptr) {
1548 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1549 instruction->SetSlowPath(slow_path);
1550 codegen_->AddSlowPath(slow_path);
1551 if (successor != nullptr) {
1552 DCHECK(successor->IsLoopHeader());
1553 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1554 }
1555 } else {
1556 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1557 }
1558
Serban Constantinescu02164b32014-11-13 14:05:07 +00001559 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1560 Register temp = temps.AcquireW();
1561
Andreas Gampe542451c2016-07-26 09:02:02 -07001562 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001563 if (successor == nullptr) {
1564 __ Cbnz(temp, slow_path->GetEntryLabel());
1565 __ Bind(slow_path->GetReturnLabel());
1566 } else {
1567 __ Cbz(temp, codegen_->GetLabelOf(successor));
1568 __ B(slow_path->GetEntryLabel());
1569 // slow_path will return to GetLabelOf(successor).
1570 }
1571}
1572
Alexandre Rames5319def2014-10-23 10:03:10 +01001573InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1574 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001575 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001576 assembler_(codegen->GetAssembler()),
1577 codegen_(codegen) {}
1578
1579#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001580 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001581
1582#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1583
1584enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001585 // Using a base helps identify when we hit such breakpoints.
1586 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001587#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1588 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1589#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1590};
1591
1592#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001593 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001594 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1595 } \
1596 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1597 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1598 locations->SetOut(Location::Any()); \
1599 }
1600 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1601#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1602
1603#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001604#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001605
Alexandre Rames67555f72014-11-18 10:55:16 +00001606void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001607 DCHECK_EQ(instr->InputCount(), 2U);
1608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1609 Primitive::Type type = instr->GetResultType();
1610 switch (type) {
1611 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001612 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001613 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001614 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001615 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001616 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001617
1618 case Primitive::kPrimFloat:
1619 case Primitive::kPrimDouble:
1620 locations->SetInAt(0, Location::RequiresFpuRegister());
1621 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001622 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001623 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001624
Alexandre Rames5319def2014-10-23 10:03:10 +01001625 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001626 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001627 }
1628}
1629
Alexandre Rames09a99962015-04-15 11:47:56 +01001630void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001631 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1632
1633 bool object_field_get_with_read_barrier =
1634 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001635 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001636 new (GetGraph()->GetArena()) LocationSummary(instruction,
1637 object_field_get_with_read_barrier ?
1638 LocationSummary::kCallOnSlowPath :
1639 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001640 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
1641 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
1642 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001643 locations->SetInAt(0, Location::RequiresRegister());
1644 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1645 locations->SetOut(Location::RequiresFpuRegister());
1646 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001647 // The output overlaps for an object field get when read barriers
1648 // are enabled: we do not want the load to overwrite the object's
1649 // location, as we need it to emit the read barrier.
1650 locations->SetOut(
1651 Location::RequiresRegister(),
1652 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001653 }
1654}
1655
1656void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1657 const FieldInfo& field_info) {
1658 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001659 LocationSummary* locations = instruction->GetLocations();
1660 Location base_loc = locations->InAt(0);
1661 Location out = locations->Out();
1662 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001663 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001664 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001665 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001666
Roland Levillain44015862016-01-22 11:47:17 +00001667 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1668 // Object FieldGet with Baker's read barrier case.
1669 MacroAssembler* masm = GetVIXLAssembler();
1670 UseScratchRegisterScope temps(masm);
1671 // /* HeapReference<Object> */ out = *(base + offset)
1672 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1673 Register temp = temps.AcquireW();
1674 // Note that potential implicit null checks are handled in this
1675 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1676 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1677 instruction,
1678 out,
1679 base,
1680 offset,
1681 temp,
1682 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001683 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001684 } else {
1685 // General case.
1686 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001687 // Note that a potential implicit null check is handled in this
1688 // CodeGeneratorARM64::LoadAcquire call.
1689 // NB: LoadAcquire will record the pc info if needed.
1690 codegen_->LoadAcquire(
1691 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001692 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001693 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001694 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001695 }
Roland Levillain44015862016-01-22 11:47:17 +00001696 if (field_type == Primitive::kPrimNot) {
1697 // If read barriers are enabled, emit read barriers other than
1698 // Baker's using a slow path (and also unpoison the loaded
1699 // reference, if heap poisoning is enabled).
1700 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1701 }
Roland Levillain4d027112015-07-01 15:41:14 +01001702 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001703}
1704
1705void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1706 LocationSummary* locations =
1707 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1708 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001709 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
1710 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
1711 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001712 locations->SetInAt(1, Location::RequiresFpuRegister());
1713 } else {
1714 locations->SetInAt(1, Location::RequiresRegister());
1715 }
1716}
1717
1718void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001719 const FieldInfo& field_info,
1720 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001721 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001722 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001723
1724 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001725 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001726 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001727 Offset offset = field_info.GetFieldOffset();
1728 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001729
Roland Levillain4d027112015-07-01 15:41:14 +01001730 {
1731 // We use a block to end the scratch scope before the write barrier, thus
1732 // freeing the temporary registers so they can be used in `MarkGCCard`.
1733 UseScratchRegisterScope temps(GetVIXLAssembler());
1734
1735 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1736 DCHECK(value.IsW());
1737 Register temp = temps.AcquireW();
1738 __ Mov(temp, value.W());
1739 GetAssembler()->PoisonHeapReference(temp.W());
1740 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001741 }
Roland Levillain4d027112015-07-01 15:41:14 +01001742
1743 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001744 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1745 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001746 } else {
1747 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1748 codegen_->MaybeRecordImplicitNullCheck(instruction);
1749 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001750 }
1751
1752 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001753 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001754 }
1755}
1756
Alexandre Rames67555f72014-11-18 10:55:16 +00001757void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001758 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001759
1760 switch (type) {
1761 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001762 case Primitive::kPrimLong: {
1763 Register dst = OutputRegister(instr);
1764 Register lhs = InputRegisterAt(instr, 0);
1765 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001766 if (instr->IsAdd()) {
1767 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001768 } else if (instr->IsAnd()) {
1769 __ And(dst, lhs, rhs);
1770 } else if (instr->IsOr()) {
1771 __ Orr(dst, lhs, rhs);
1772 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001773 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001774 } else if (instr->IsRor()) {
1775 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001776 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001777 __ Ror(dst, lhs, shift);
1778 } else {
1779 // Ensure shift distance is in the same size register as the result. If
1780 // we are rotating a long and the shift comes in a w register originally,
1781 // we don't need to sxtw for use as an x since the shift distances are
1782 // all & reg_bits - 1.
1783 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1784 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001785 } else {
1786 DCHECK(instr->IsXor());
1787 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001788 }
1789 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001790 }
1791 case Primitive::kPrimFloat:
1792 case Primitive::kPrimDouble: {
1793 FPRegister dst = OutputFPRegister(instr);
1794 FPRegister lhs = InputFPRegisterAt(instr, 0);
1795 FPRegister rhs = InputFPRegisterAt(instr, 1);
1796 if (instr->IsAdd()) {
1797 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001798 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001799 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001800 } else {
1801 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001802 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001803 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001804 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001805 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001806 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001807 }
1808}
1809
Serban Constantinescu02164b32014-11-13 14:05:07 +00001810void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1811 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1812
1813 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1814 Primitive::Type type = instr->GetResultType();
1815 switch (type) {
1816 case Primitive::kPrimInt:
1817 case Primitive::kPrimLong: {
1818 locations->SetInAt(0, Location::RequiresRegister());
1819 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1820 locations->SetOut(Location::RequiresRegister());
1821 break;
1822 }
1823 default:
1824 LOG(FATAL) << "Unexpected shift type " << type;
1825 }
1826}
1827
1828void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1829 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1830
1831 Primitive::Type type = instr->GetType();
1832 switch (type) {
1833 case Primitive::kPrimInt:
1834 case Primitive::kPrimLong: {
1835 Register dst = OutputRegister(instr);
1836 Register lhs = InputRegisterAt(instr, 0);
1837 Operand rhs = InputOperandAt(instr, 1);
1838 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001839 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001840 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001841 if (instr->IsShl()) {
1842 __ Lsl(dst, lhs, shift_value);
1843 } else if (instr->IsShr()) {
1844 __ Asr(dst, lhs, shift_value);
1845 } else {
1846 __ Lsr(dst, lhs, shift_value);
1847 }
1848 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001849 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001850
1851 if (instr->IsShl()) {
1852 __ Lsl(dst, lhs, rhs_reg);
1853 } else if (instr->IsShr()) {
1854 __ Asr(dst, lhs, rhs_reg);
1855 } else {
1856 __ Lsr(dst, lhs, rhs_reg);
1857 }
1858 }
1859 break;
1860 }
1861 default:
1862 LOG(FATAL) << "Unexpected shift operation type " << type;
1863 }
1864}
1865
Alexandre Rames5319def2014-10-23 10:03:10 +01001866void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001867 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001868}
1869
1870void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001871 HandleBinaryOp(instruction);
1872}
1873
1874void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1875 HandleBinaryOp(instruction);
1876}
1877
1878void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1879 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001880}
1881
Artem Serov7fc63502016-02-09 17:15:29 +00001882void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001883 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1884 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1885 locations->SetInAt(0, Location::RequiresRegister());
1886 // There is no immediate variant of negated bitwise instructions in AArch64.
1887 locations->SetInAt(1, Location::RequiresRegister());
1888 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1889}
1890
Artem Serov7fc63502016-02-09 17:15:29 +00001891void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001892 Register dst = OutputRegister(instr);
1893 Register lhs = InputRegisterAt(instr, 0);
1894 Register rhs = InputRegisterAt(instr, 1);
1895
1896 switch (instr->GetOpKind()) {
1897 case HInstruction::kAnd:
1898 __ Bic(dst, lhs, rhs);
1899 break;
1900 case HInstruction::kOr:
1901 __ Orn(dst, lhs, rhs);
1902 break;
1903 case HInstruction::kXor:
1904 __ Eon(dst, lhs, rhs);
1905 break;
1906 default:
1907 LOG(FATAL) << "Unreachable";
1908 }
1909}
1910
Alexandre Rames8626b742015-11-25 16:28:08 +00001911void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1912 HArm64DataProcWithShifterOp* instruction) {
1913 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1914 instruction->GetType() == Primitive::kPrimLong);
1915 LocationSummary* locations =
1916 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1917 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1918 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1919 } else {
1920 locations->SetInAt(0, Location::RequiresRegister());
1921 }
1922 locations->SetInAt(1, Location::RequiresRegister());
1923 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1924}
1925
1926void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1927 HArm64DataProcWithShifterOp* instruction) {
1928 Primitive::Type type = instruction->GetType();
1929 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1930 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1931 Register out = OutputRegister(instruction);
1932 Register left;
1933 if (kind != HInstruction::kNeg) {
1934 left = InputRegisterAt(instruction, 0);
1935 }
1936 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1937 // shifter operand operation, the IR generating `right_reg` (input to the type
1938 // conversion) can have a different type from the current instruction's type,
1939 // so we manually indicate the type.
1940 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001941 int64_t shift_amount = instruction->GetShiftAmount() &
1942 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001943
1944 Operand right_operand(0);
1945
1946 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1947 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1948 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1949 } else {
1950 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1951 }
1952
1953 // Logical binary operations do not support extension operations in the
1954 // operand. Note that VIXL would still manage if it was passed by generating
1955 // the extension as a separate instruction.
1956 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1957 DCHECK(!right_operand.IsExtendedRegister() ||
1958 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1959 kind != HInstruction::kNeg));
1960 switch (kind) {
1961 case HInstruction::kAdd:
1962 __ Add(out, left, right_operand);
1963 break;
1964 case HInstruction::kAnd:
1965 __ And(out, left, right_operand);
1966 break;
1967 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001968 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001969 __ Neg(out, right_operand);
1970 break;
1971 case HInstruction::kOr:
1972 __ Orr(out, left, right_operand);
1973 break;
1974 case HInstruction::kSub:
1975 __ Sub(out, left, right_operand);
1976 break;
1977 case HInstruction::kXor:
1978 __ Eor(out, left, right_operand);
1979 break;
1980 default:
1981 LOG(FATAL) << "Unexpected operation kind: " << kind;
1982 UNREACHABLE();
1983 }
1984}
1985
Artem Serov328429f2016-07-06 16:23:04 +01001986void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00001987 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
1988 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001989 LocationSummary* locations =
1990 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1991 locations->SetInAt(0, Location::RequiresRegister());
1992 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1993 locations->SetOut(Location::RequiresRegister());
1994}
1995
Roland Levillain4a3aa572016-08-15 13:17:06 +00001996void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
1997 HIntermediateAddress* instruction) {
1998 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
1999 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002000 __ Add(OutputRegister(instruction),
2001 InputRegisterAt(instruction, 0),
2002 Operand(InputOperandAt(instruction, 1)));
2003}
2004
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002005void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002006 LocationSummary* locations =
2007 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002008 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2009 if (instr->GetOpKind() == HInstruction::kSub &&
2010 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002011 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002012 // Don't allocate register for Mneg instruction.
2013 } else {
2014 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2015 Location::RequiresRegister());
2016 }
2017 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2018 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002019 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2020}
2021
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002022void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002023 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002024 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2025 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002026
2027 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2028 // This fixup should be carried out for all multiply-accumulate instructions:
2029 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2030 if (instr->GetType() == Primitive::kPrimLong &&
2031 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2032 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002033 vixl::aarch64::Instruction* prev =
2034 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002035 if (prev->IsLoadOrStore()) {
2036 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002037 vixl::aarch64::CodeBufferCheckScope scope(masm,
2038 kInstructionSize,
2039 vixl::aarch64::CodeBufferCheckScope::kCheck,
2040 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002041 __ nop();
2042 }
2043 }
2044
2045 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002046 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002047 __ Madd(res, mul_left, mul_right, accumulator);
2048 } else {
2049 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002050 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002051 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002052 __ Mneg(res, mul_left, mul_right);
2053 } else {
2054 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2055 __ Msub(res, mul_left, mul_right, accumulator);
2056 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002057 }
2058}
2059
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002060void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002061 bool object_array_get_with_read_barrier =
2062 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002063 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002064 new (GetGraph()->GetArena()) LocationSummary(instruction,
2065 object_array_get_with_read_barrier ?
2066 LocationSummary::kCallOnSlowPath :
2067 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002068 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2069 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
2070 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002071 locations->SetInAt(0, Location::RequiresRegister());
2072 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002073 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2074 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2075 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002076 // The output overlaps in the case of an object array get with
2077 // read barriers enabled: we do not want the move to overwrite the
2078 // array's location, as we need it to emit the read barrier.
2079 locations->SetOut(
2080 Location::RequiresRegister(),
2081 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002082 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002083}
2084
2085void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002086 Primitive::Type type = instruction->GetType();
2087 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002088 LocationSummary* locations = instruction->GetLocations();
2089 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002090 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002091 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002092
Alexandre Ramesd921d642015-04-16 15:07:16 +01002093 MacroAssembler* masm = GetVIXLAssembler();
2094 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002095 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002096 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002097
Roland Levillain44015862016-01-22 11:47:17 +00002098 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2099 // Object ArrayGet with Baker's read barrier case.
2100 Register temp = temps.AcquireW();
Roland Levillain4a3aa572016-08-15 13:17:06 +00002101 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2102 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002103 // Note that a potential implicit null check is handled in the
2104 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2105 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2106 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002107 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002108 // General case.
2109 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002110 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002111 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2112 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002113 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002114 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002115 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002116 // The read barrier instrumentation does not support the
2117 // HIntermediateAddress instruction yet.
2118 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00002119 // We do not need to compute the intermediate address from the array: the
2120 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002121 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002122 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002123 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002124 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2125 }
2126 temp = obj;
2127 } else {
2128 __ Add(temp, obj, offset);
2129 }
2130 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2131 }
2132
2133 codegen_->Load(type, OutputCPURegister(instruction), source);
2134 codegen_->MaybeRecordImplicitNullCheck(instruction);
2135
2136 if (type == Primitive::kPrimNot) {
2137 static_assert(
2138 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2139 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2140 Location obj_loc = locations->InAt(0);
2141 if (index.IsConstant()) {
2142 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2143 } else {
2144 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2145 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002146 }
Roland Levillain4d027112015-07-01 15:41:14 +01002147 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002148}
2149
Alexandre Rames5319def2014-10-23 10:03:10 +01002150void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2151 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2152 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002153 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002154}
2155
2156void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002157 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002158 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002159 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002160 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002161}
2162
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002163void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002164 Primitive::Type value_type = instruction->GetComponentType();
2165
2166 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002167 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2168 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002169 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002170 LocationSummary::kCallOnSlowPath :
2171 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002172 locations->SetInAt(0, Location::RequiresRegister());
2173 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002174 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2175 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2176 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002177 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002178 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002179 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002180 }
Roland Levillaina8c6d702016-08-26 11:17:44 +01002181 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && (value_type == Primitive::kPrimNot)) {
Roland Levillain16d9f942016-08-25 17:27:56 +01002182 // Additional temporary registers for a Baker read barrier.
2183 locations->AddTemp(Location::RequiresRegister());
2184 locations->AddTemp(Location::RequiresRegister());
2185 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002186}
2187
2188void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2189 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002190 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002191 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002192 bool needs_write_barrier =
2193 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002194
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002195 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002196 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002197 CPURegister source = value;
2198 Location index = locations->InAt(1);
2199 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2200 MemOperand destination = HeapOperand(array);
2201 MacroAssembler* masm = GetVIXLAssembler();
2202 BlockPoolsScope block_pools(masm);
2203
2204 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002205 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002206 if (index.IsConstant()) {
2207 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2208 destination = HeapOperand(array, offset);
2209 } else {
2210 UseScratchRegisterScope temps(masm);
2211 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002212 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00002213 // The read barrier instrumentation does not support the
2214 // HIntermediateAddress instruction yet.
2215 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002216 // We do not need to compute the intermediate address from the array: the
2217 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002218 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002219 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002220 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002221 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2222 }
2223 temp = array;
2224 } else {
2225 __ Add(temp, array, offset);
2226 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002227 destination = HeapOperand(temp,
2228 XRegisterFrom(index),
2229 LSL,
2230 Primitive::ComponentSizeShift(value_type));
2231 }
2232 codegen_->Store(value_type, value, destination);
2233 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002234 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002235 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002236 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002237 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002238 {
2239 // We use a block to end the scratch scope before the write barrier, thus
2240 // freeing the temporary registers so they can be used in `MarkGCCard`.
2241 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002242 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002243 if (index.IsConstant()) {
2244 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002245 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002246 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002247 destination = HeapOperand(temp,
2248 XRegisterFrom(index),
2249 LSL,
2250 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002251 }
2252
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002253 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2254 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2255 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2256
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002257 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002258 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2259 codegen_->AddSlowPath(slow_path);
2260 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002261 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002262 __ Cbnz(Register(value), &non_zero);
2263 if (!index.IsConstant()) {
2264 __ Add(temp, array, offset);
2265 }
2266 __ Str(wzr, destination);
2267 codegen_->MaybeRecordImplicitNullCheck(instruction);
2268 __ B(&done);
2269 __ Bind(&non_zero);
2270 }
2271
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002272 if (kEmitCompilerReadBarrier) {
Roland Levillain16d9f942016-08-25 17:27:56 +01002273 if (!kUseBakerReadBarrier) {
2274 // When (non-Baker) read barriers are enabled, the type
2275 // checking instrumentation requires two read barriers
2276 // generated by CodeGeneratorARM64::GenerateReadBarrierSlow:
2277 //
2278 // __ Mov(temp2, temp);
2279 // // /* HeapReference<Class> */ temp = temp->component_type_
2280 // __ Ldr(temp, HeapOperand(temp, component_offset));
2281 // codegen_->GenerateReadBarrierSlow(
2282 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2283 //
2284 // // /* HeapReference<Class> */ temp2 = value->klass_
2285 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2286 // codegen_->GenerateReadBarrierSlow(
2287 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2288 //
2289 // __ Cmp(temp, temp2);
2290 //
2291 // However, the second read barrier may trash `temp`, as it
2292 // is a temporary register, and as such would not be saved
2293 // along with live registers before calling the runtime (nor
2294 // restored afterwards). So in this case, we bail out and
2295 // delegate the work to the array set slow path.
2296 //
2297 // TODO: Extend the register allocator to support a new
2298 // "(locally) live temp" location so as to avoid always
2299 // going into the slow path when read barriers are enabled?
2300 //
2301 // There is no such problem with Baker read barriers (see below).
2302 __ B(slow_path->GetEntryLabel());
2303 } else {
2304 // Note that we cannot use `temps` (instance of VIXL's
2305 // UseScratchRegisterScope) to allocate `temp2` because
2306 // the Baker read barriers generated by
2307 // GenerateFieldLoadWithBakerReadBarrier below also use
2308 // that facility to allocate a temporary register, thus
2309 // making VIXL's scratch register pool empty.
2310 Location temp2_loc = locations->GetTemp(0);
2311 Register temp2 = WRegisterFrom(temp2_loc);
2312
2313 // Note: Because it is acquired from VIXL's scratch register
2314 // pool, `temp` might be IP0, and thus cannot be used as
2315 // `ref` argument of GenerateFieldLoadWithBakerReadBarrier
2316 // calls below (see ReadBarrierMarkSlowPathARM64 for more
2317 // details).
2318
2319 // /* HeapReference<Class> */ temp2 = array->klass_
2320 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2321 temp2_loc,
2322 array,
2323 class_offset,
2324 temp,
2325 /* needs_null_check */ true,
2326 /* use_load_acquire */ false);
2327
2328 // /* HeapReference<Class> */ temp2 = temp2->component_type_
2329 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2330 temp2_loc,
2331 temp2,
2332 component_offset,
2333 temp,
2334 /* needs_null_check */ false,
2335 /* use_load_acquire */ false);
2336 // For the same reason that we request `temp2` from the
2337 // register allocator above, we cannot get `temp3` from
2338 // VIXL's scratch register pool.
2339 Location temp3_loc = locations->GetTemp(1);
2340 Register temp3 = WRegisterFrom(temp3_loc);
2341 // Register `temp2` is not trashed by the read barrier
2342 // emitted by GenerateFieldLoadWithBakerReadBarrier below,
2343 // as that method produces a call to a ReadBarrierMarkRegX
2344 // entry point, which saves all potentially live registers,
2345 // including temporaries such a `temp2`.
2346 // /* HeapReference<Class> */ temp3 = register_value->klass_
2347 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2348 temp3_loc,
2349 value.W(),
2350 class_offset,
2351 temp,
2352 /* needs_null_check */ false,
2353 /* use_load_acquire */ false);
2354 // If heap poisoning is enabled, `temp2` and `temp3` have
2355 // been unpoisoned by the the previous calls to
2356 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
2357 __ Cmp(temp2, temp3);
2358
2359 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2360 vixl::aarch64::Label do_put;
2361 __ B(eq, &do_put);
2362 // We do not need to emit a read barrier for the
2363 // following heap reference load, as `temp2` is only used
2364 // in a comparison with null below, and this reference
2365 // is not kept afterwards.
2366 // /* HeapReference<Class> */ temp = temp2->super_class_
2367 __ Ldr(temp, HeapOperand(temp2, super_offset));
2368 // If heap poisoning is enabled, no need to unpoison
2369 // `temp`, as we are comparing against null below.
2370 __ Cbnz(temp, slow_path->GetEntryLabel());
2371 __ Bind(&do_put);
2372 } else {
2373 __ B(ne, slow_path->GetEntryLabel());
2374 }
2375 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002376 } else {
Roland Levillain16d9f942016-08-25 17:27:56 +01002377 // Non read barrier code.
2378
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002379 Register temp2 = temps.AcquireSameSizeAs(array);
2380 // /* HeapReference<Class> */ temp = array->klass_
2381 __ Ldr(temp, HeapOperand(array, class_offset));
2382 codegen_->MaybeRecordImplicitNullCheck(instruction);
2383 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2384
2385 // /* HeapReference<Class> */ temp = temp->component_type_
2386 __ Ldr(temp, HeapOperand(temp, component_offset));
2387 // /* HeapReference<Class> */ temp2 = value->klass_
2388 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2389 // If heap poisoning is enabled, no need to unpoison `temp`
2390 // nor `temp2`, as we are comparing two poisoned references.
2391 __ Cmp(temp, temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002392 temps.Release(temp2);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002393
2394 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002395 vixl::aarch64::Label do_put;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002396 __ B(eq, &do_put);
2397 // If heap poisoning is enabled, the `temp` reference has
2398 // not been unpoisoned yet; unpoison it now.
2399 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2400
2401 // /* HeapReference<Class> */ temp = temp->super_class_
2402 __ Ldr(temp, HeapOperand(temp, super_offset));
2403 // If heap poisoning is enabled, no need to unpoison
2404 // `temp`, as we are comparing against null below.
2405 __ Cbnz(temp, slow_path->GetEntryLabel());
2406 __ Bind(&do_put);
2407 } else {
2408 __ B(ne, slow_path->GetEntryLabel());
2409 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002410 }
2411 }
2412
2413 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002414 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002415 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002416 __ Mov(temp2, value.W());
2417 GetAssembler()->PoisonHeapReference(temp2);
2418 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002419 }
2420
2421 if (!index.IsConstant()) {
2422 __ Add(temp, array, offset);
2423 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002424 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002425
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002426 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002427 codegen_->MaybeRecordImplicitNullCheck(instruction);
2428 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002429 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002430
2431 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2432
2433 if (done.IsLinked()) {
2434 __ Bind(&done);
2435 }
2436
2437 if (slow_path != nullptr) {
2438 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002439 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002440 }
2441}
2442
Alexandre Rames67555f72014-11-18 10:55:16 +00002443void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002444 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2445 ? LocationSummary::kCallOnSlowPath
2446 : LocationSummary::kNoCall;
2447 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002448 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002449 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002450 if (instruction->HasUses()) {
2451 locations->SetOut(Location::SameAsFirstInput());
2452 }
2453}
2454
2455void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002456 BoundsCheckSlowPathARM64* slow_path =
2457 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002458 codegen_->AddSlowPath(slow_path);
2459
2460 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2461 __ B(slow_path->GetEntryLabel(), hs);
2462}
2463
Alexandre Rames67555f72014-11-18 10:55:16 +00002464void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2465 LocationSummary* locations =
2466 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2467 locations->SetInAt(0, Location::RequiresRegister());
2468 if (check->HasUses()) {
2469 locations->SetOut(Location::SameAsFirstInput());
2470 }
2471}
2472
2473void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2474 // We assume the class is not null.
2475 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2476 check->GetLoadClass(), check, check->GetDexPc(), true);
2477 codegen_->AddSlowPath(slow_path);
2478 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2479}
2480
Roland Levillain1a653882016-03-18 18:05:57 +00002481static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2482 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2483 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2484}
2485
2486void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2487 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2488 Location rhs_loc = instruction->GetLocations()->InAt(1);
2489 if (rhs_loc.IsConstant()) {
2490 // 0.0 is the only immediate that can be encoded directly in
2491 // an FCMP instruction.
2492 //
2493 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2494 // specify that in a floating-point comparison, positive zero
2495 // and negative zero are considered equal, so we can use the
2496 // literal 0.0 for both cases here.
2497 //
2498 // Note however that some methods (Float.equal, Float.compare,
2499 // Float.compareTo, Double.equal, Double.compare,
2500 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2501 // StrictMath.min) consider 0.0 to be (strictly) greater than
2502 // -0.0. So if we ever translate calls to these methods into a
2503 // HCompare instruction, we must handle the -0.0 case with
2504 // care here.
2505 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2506 __ Fcmp(lhs_reg, 0.0);
2507 } else {
2508 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2509 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002510}
2511
Serban Constantinescu02164b32014-11-13 14:05:07 +00002512void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002513 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002514 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2515 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002516 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002517 case Primitive::kPrimBoolean:
2518 case Primitive::kPrimByte:
2519 case Primitive::kPrimShort:
2520 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002521 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002522 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002523 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002524 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002525 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2526 break;
2527 }
2528 case Primitive::kPrimFloat:
2529 case Primitive::kPrimDouble: {
2530 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002531 locations->SetInAt(1,
2532 IsFloatingPointZeroConstant(compare->InputAt(1))
2533 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2534 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002535 locations->SetOut(Location::RequiresRegister());
2536 break;
2537 }
2538 default:
2539 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2540 }
2541}
2542
2543void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2544 Primitive::Type in_type = compare->InputAt(0)->GetType();
2545
2546 // 0 if: left == right
2547 // 1 if: left > right
2548 // -1 if: left < right
2549 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002550 case Primitive::kPrimBoolean:
2551 case Primitive::kPrimByte:
2552 case Primitive::kPrimShort:
2553 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002554 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002555 case Primitive::kPrimLong: {
2556 Register result = OutputRegister(compare);
2557 Register left = InputRegisterAt(compare, 0);
2558 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002559 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002560 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2561 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002562 break;
2563 }
2564 case Primitive::kPrimFloat:
2565 case Primitive::kPrimDouble: {
2566 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002567 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002568 __ Cset(result, ne);
2569 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002570 break;
2571 }
2572 default:
2573 LOG(FATAL) << "Unimplemented compare type " << in_type;
2574 }
2575}
2576
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002577void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002578 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002579
2580 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2581 locations->SetInAt(0, Location::RequiresFpuRegister());
2582 locations->SetInAt(1,
2583 IsFloatingPointZeroConstant(instruction->InputAt(1))
2584 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2585 : Location::RequiresFpuRegister());
2586 } else {
2587 // Integer cases.
2588 locations->SetInAt(0, Location::RequiresRegister());
2589 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2590 }
2591
David Brazdilb3e773e2016-01-26 11:28:37 +00002592 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002593 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002594 }
2595}
2596
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002597void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002598 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002599 return;
2600 }
2601
2602 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002603 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002604 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002605
Roland Levillain7f63c522015-07-13 15:54:55 +00002606 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002607 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002608 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002609 } else {
2610 // Integer cases.
2611 Register lhs = InputRegisterAt(instruction, 0);
2612 Operand rhs = InputOperandAt(instruction, 1);
2613 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002614 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002615 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002616}
2617
2618#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2619 M(Equal) \
2620 M(NotEqual) \
2621 M(LessThan) \
2622 M(LessThanOrEqual) \
2623 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002624 M(GreaterThanOrEqual) \
2625 M(Below) \
2626 M(BelowOrEqual) \
2627 M(Above) \
2628 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002629#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002630void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2631void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002632FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002633#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002634#undef FOR_EACH_CONDITION_INSTRUCTION
2635
Zheng Xuc6667102015-05-15 16:08:45 +08002636void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2637 DCHECK(instruction->IsDiv() || instruction->IsRem());
2638
2639 LocationSummary* locations = instruction->GetLocations();
2640 Location second = locations->InAt(1);
2641 DCHECK(second.IsConstant());
2642
2643 Register out = OutputRegister(instruction);
2644 Register dividend = InputRegisterAt(instruction, 0);
2645 int64_t imm = Int64FromConstant(second.GetConstant());
2646 DCHECK(imm == 1 || imm == -1);
2647
2648 if (instruction->IsRem()) {
2649 __ Mov(out, 0);
2650 } else {
2651 if (imm == 1) {
2652 __ Mov(out, dividend);
2653 } else {
2654 __ Neg(out, dividend);
2655 }
2656 }
2657}
2658
2659void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2660 DCHECK(instruction->IsDiv() || instruction->IsRem());
2661
2662 LocationSummary* locations = instruction->GetLocations();
2663 Location second = locations->InAt(1);
2664 DCHECK(second.IsConstant());
2665
2666 Register out = OutputRegister(instruction);
2667 Register dividend = InputRegisterAt(instruction, 0);
2668 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002669 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002670 int ctz_imm = CTZ(abs_imm);
2671
2672 UseScratchRegisterScope temps(GetVIXLAssembler());
2673 Register temp = temps.AcquireSameSizeAs(out);
2674
2675 if (instruction->IsDiv()) {
2676 __ Add(temp, dividend, abs_imm - 1);
2677 __ Cmp(dividend, 0);
2678 __ Csel(out, temp, dividend, lt);
2679 if (imm > 0) {
2680 __ Asr(out, out, ctz_imm);
2681 } else {
2682 __ Neg(out, Operand(out, ASR, ctz_imm));
2683 }
2684 } else {
2685 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2686 __ Asr(temp, dividend, bits - 1);
2687 __ Lsr(temp, temp, bits - ctz_imm);
2688 __ Add(out, dividend, temp);
2689 __ And(out, out, abs_imm - 1);
2690 __ Sub(out, out, temp);
2691 }
2692}
2693
2694void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2695 DCHECK(instruction->IsDiv() || instruction->IsRem());
2696
2697 LocationSummary* locations = instruction->GetLocations();
2698 Location second = locations->InAt(1);
2699 DCHECK(second.IsConstant());
2700
2701 Register out = OutputRegister(instruction);
2702 Register dividend = InputRegisterAt(instruction, 0);
2703 int64_t imm = Int64FromConstant(second.GetConstant());
2704
2705 Primitive::Type type = instruction->GetResultType();
2706 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2707
2708 int64_t magic;
2709 int shift;
2710 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2711
2712 UseScratchRegisterScope temps(GetVIXLAssembler());
2713 Register temp = temps.AcquireSameSizeAs(out);
2714
2715 // temp = get_high(dividend * magic)
2716 __ Mov(temp, magic);
2717 if (type == Primitive::kPrimLong) {
2718 __ Smulh(temp, dividend, temp);
2719 } else {
2720 __ Smull(temp.X(), dividend, temp);
2721 __ Lsr(temp.X(), temp.X(), 32);
2722 }
2723
2724 if (imm > 0 && magic < 0) {
2725 __ Add(temp, temp, dividend);
2726 } else if (imm < 0 && magic > 0) {
2727 __ Sub(temp, temp, dividend);
2728 }
2729
2730 if (shift != 0) {
2731 __ Asr(temp, temp, shift);
2732 }
2733
2734 if (instruction->IsDiv()) {
2735 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2736 } else {
2737 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2738 // TODO: Strength reduction for msub.
2739 Register temp_imm = temps.AcquireSameSizeAs(out);
2740 __ Mov(temp_imm, imm);
2741 __ Msub(out, temp, temp_imm, dividend);
2742 }
2743}
2744
2745void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2746 DCHECK(instruction->IsDiv() || instruction->IsRem());
2747 Primitive::Type type = instruction->GetResultType();
2748 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2749
2750 LocationSummary* locations = instruction->GetLocations();
2751 Register out = OutputRegister(instruction);
2752 Location second = locations->InAt(1);
2753
2754 if (second.IsConstant()) {
2755 int64_t imm = Int64FromConstant(second.GetConstant());
2756
2757 if (imm == 0) {
2758 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2759 } else if (imm == 1 || imm == -1) {
2760 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002761 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002762 DivRemByPowerOfTwo(instruction);
2763 } else {
2764 DCHECK(imm <= -2 || imm >= 2);
2765 GenerateDivRemWithAnyConstant(instruction);
2766 }
2767 } else {
2768 Register dividend = InputRegisterAt(instruction, 0);
2769 Register divisor = InputRegisterAt(instruction, 1);
2770 if (instruction->IsDiv()) {
2771 __ Sdiv(out, dividend, divisor);
2772 } else {
2773 UseScratchRegisterScope temps(GetVIXLAssembler());
2774 Register temp = temps.AcquireSameSizeAs(out);
2775 __ Sdiv(temp, dividend, divisor);
2776 __ Msub(out, temp, divisor, dividend);
2777 }
2778 }
2779}
2780
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002781void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2782 LocationSummary* locations =
2783 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2784 switch (div->GetResultType()) {
2785 case Primitive::kPrimInt:
2786 case Primitive::kPrimLong:
2787 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002788 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002789 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2790 break;
2791
2792 case Primitive::kPrimFloat:
2793 case Primitive::kPrimDouble:
2794 locations->SetInAt(0, Location::RequiresFpuRegister());
2795 locations->SetInAt(1, Location::RequiresFpuRegister());
2796 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2797 break;
2798
2799 default:
2800 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2801 }
2802}
2803
2804void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2805 Primitive::Type type = div->GetResultType();
2806 switch (type) {
2807 case Primitive::kPrimInt:
2808 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002809 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002810 break;
2811
2812 case Primitive::kPrimFloat:
2813 case Primitive::kPrimDouble:
2814 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2815 break;
2816
2817 default:
2818 LOG(FATAL) << "Unexpected div type " << type;
2819 }
2820}
2821
Alexandre Rames67555f72014-11-18 10:55:16 +00002822void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002823 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2824 ? LocationSummary::kCallOnSlowPath
2825 : LocationSummary::kNoCall;
2826 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002827 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2828 if (instruction->HasUses()) {
2829 locations->SetOut(Location::SameAsFirstInput());
2830 }
2831}
2832
2833void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2834 SlowPathCodeARM64* slow_path =
2835 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2836 codegen_->AddSlowPath(slow_path);
2837 Location value = instruction->GetLocations()->InAt(0);
2838
Alexandre Rames3e69f162014-12-10 10:36:50 +00002839 Primitive::Type type = instruction->GetType();
2840
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002841 if (!Primitive::IsIntegralType(type)) {
2842 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002843 return;
2844 }
2845
Alexandre Rames67555f72014-11-18 10:55:16 +00002846 if (value.IsConstant()) {
2847 int64_t divisor = Int64ConstantFrom(value);
2848 if (divisor == 0) {
2849 __ B(slow_path->GetEntryLabel());
2850 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002851 // A division by a non-null constant is valid. We don't need to perform
2852 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002853 }
2854 } else {
2855 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2856 }
2857}
2858
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002859void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2860 LocationSummary* locations =
2861 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2862 locations->SetOut(Location::ConstantLocation(constant));
2863}
2864
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002865void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2866 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002867 // Will be generated at use site.
2868}
2869
Alexandre Rames5319def2014-10-23 10:03:10 +01002870void LocationsBuilderARM64::VisitExit(HExit* exit) {
2871 exit->SetLocations(nullptr);
2872}
2873
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002874void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002875}
2876
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002877void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2878 LocationSummary* locations =
2879 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2880 locations->SetOut(Location::ConstantLocation(constant));
2881}
2882
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002883void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002884 // Will be generated at use site.
2885}
2886
David Brazdilfc6a86a2015-06-26 10:33:45 +00002887void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002888 DCHECK(!successor->IsExitBlock());
2889 HBasicBlock* block = got->GetBlock();
2890 HInstruction* previous = got->GetPrevious();
2891 HLoopInformation* info = block->GetLoopInformation();
2892
David Brazdil46e2a392015-03-16 17:31:52 +00002893 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002894 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2895 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2896 return;
2897 }
2898 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2899 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2900 }
2901 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002902 __ B(codegen_->GetLabelOf(successor));
2903 }
2904}
2905
David Brazdilfc6a86a2015-06-26 10:33:45 +00002906void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2907 got->SetLocations(nullptr);
2908}
2909
2910void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2911 HandleGoto(got, got->GetSuccessor());
2912}
2913
2914void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2915 try_boundary->SetLocations(nullptr);
2916}
2917
2918void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2919 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2920 if (!successor->IsExitBlock()) {
2921 HandleGoto(try_boundary, successor);
2922 }
2923}
2924
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002925void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002926 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002927 vixl::aarch64::Label* true_target,
2928 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002929 // FP branching requires both targets to be explicit. If either of the targets
2930 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002931 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002932 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002933
David Brazdil0debae72015-11-12 18:37:00 +00002934 if (true_target == nullptr && false_target == nullptr) {
2935 // Nothing to do. The code always falls through.
2936 return;
2937 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002938 // Constant condition, statically compared against "true" (integer value 1).
2939 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002940 if (true_target != nullptr) {
2941 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002942 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002943 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002944 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002945 if (false_target != nullptr) {
2946 __ B(false_target);
2947 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002948 }
David Brazdil0debae72015-11-12 18:37:00 +00002949 return;
2950 }
2951
2952 // The following code generates these patterns:
2953 // (1) true_target == nullptr && false_target != nullptr
2954 // - opposite condition true => branch to false_target
2955 // (2) true_target != nullptr && false_target == nullptr
2956 // - condition true => branch to true_target
2957 // (3) true_target != nullptr && false_target != nullptr
2958 // - condition true => branch to true_target
2959 // - branch to false_target
2960 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002961 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002962 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002963 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002964 if (true_target == nullptr) {
2965 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2966 } else {
2967 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2968 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002969 } else {
2970 // The condition instruction has not been materialized, use its inputs as
2971 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002972 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002973
David Brazdil0debae72015-11-12 18:37:00 +00002974 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002975 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002976 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002977 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002978 IfCondition opposite_condition = condition->GetOppositeCondition();
2979 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002980 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002981 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002982 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002983 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002984 // Integer cases.
2985 Register lhs = InputRegisterAt(condition, 0);
2986 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002987
2988 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002989 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002990 if (true_target == nullptr) {
2991 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2992 non_fallthrough_target = false_target;
2993 } else {
2994 arm64_cond = ARM64Condition(condition->GetCondition());
2995 non_fallthrough_target = true_target;
2996 }
2997
Aart Bik086d27e2016-01-20 17:02:00 -08002998 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002999 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003000 switch (arm64_cond) {
3001 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003002 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003003 break;
3004 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003005 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003006 break;
3007 case lt:
3008 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003009 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003010 break;
3011 case ge:
3012 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003013 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003014 break;
3015 default:
3016 // Without the `static_cast` the compiler throws an error for
3017 // `-Werror=sign-promo`.
3018 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3019 }
3020 } else {
3021 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003022 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003023 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003024 }
3025 }
David Brazdil0debae72015-11-12 18:37:00 +00003026
3027 // If neither branch falls through (case 3), the conditional branch to `true_target`
3028 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3029 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003030 __ B(false_target);
3031 }
David Brazdil0debae72015-11-12 18:37:00 +00003032
3033 if (fallthrough_target.IsLinked()) {
3034 __ Bind(&fallthrough_target);
3035 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003036}
3037
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003038void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3039 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003040 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003041 locations->SetInAt(0, Location::RequiresRegister());
3042 }
3043}
3044
3045void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003046 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3047 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003048 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3049 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3050 true_target = nullptr;
3051 }
3052 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3053 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3054 false_target = nullptr;
3055 }
David Brazdil0debae72015-11-12 18:37:00 +00003056 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003057}
3058
3059void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3060 LocationSummary* locations = new (GetGraph()->GetArena())
3061 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko239d6ea2016-09-05 10:44:04 +01003062 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003063 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003064 locations->SetInAt(0, Location::RequiresRegister());
3065 }
3066}
3067
3068void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003069 SlowPathCodeARM64* slow_path =
3070 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003071 GenerateTestAndBranch(deoptimize,
3072 /* condition_input_index */ 0,
3073 slow_path->GetEntryLabel(),
3074 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003075}
3076
David Brazdilc0b601b2016-02-08 14:20:45 +00003077static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3078 return condition->IsCondition() &&
3079 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3080}
3081
Alexandre Rames880f1192016-06-13 16:04:50 +01003082static inline Condition GetConditionForSelect(HCondition* condition) {
3083 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003084 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3085 : ARM64Condition(cond);
3086}
3087
David Brazdil74eb1b22015-12-14 11:44:01 +00003088void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3089 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003090 if (Primitive::IsFloatingPointType(select->GetType())) {
3091 locations->SetInAt(0, Location::RequiresFpuRegister());
3092 locations->SetInAt(1, Location::RequiresFpuRegister());
3093 locations->SetOut(Location::RequiresFpuRegister());
3094 } else {
3095 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3096 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3097 bool is_true_value_constant = cst_true_value != nullptr;
3098 bool is_false_value_constant = cst_false_value != nullptr;
3099 // Ask VIXL whether we should synthesize constants in registers.
3100 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3101 Operand true_op = is_true_value_constant ?
3102 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3103 Operand false_op = is_false_value_constant ?
3104 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3105 bool true_value_in_register = false;
3106 bool false_value_in_register = false;
3107 MacroAssembler::GetCselSynthesisInformation(
3108 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3109 true_value_in_register |= !is_true_value_constant;
3110 false_value_in_register |= !is_false_value_constant;
3111
3112 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3113 : Location::ConstantLocation(cst_true_value));
3114 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3115 : Location::ConstantLocation(cst_false_value));
3116 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003117 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003118
David Brazdil74eb1b22015-12-14 11:44:01 +00003119 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3120 locations->SetInAt(2, Location::RequiresRegister());
3121 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003122}
3123
3124void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003125 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003126 Condition csel_cond;
3127
3128 if (IsBooleanValueOrMaterializedCondition(cond)) {
3129 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003130 // Use the condition flags set by the previous instruction.
3131 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003132 } else {
3133 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003134 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003135 }
3136 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003137 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003138 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003139 } else {
3140 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003141 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003142 }
3143
Alexandre Rames880f1192016-06-13 16:04:50 +01003144 if (Primitive::IsFloatingPointType(select->GetType())) {
3145 __ Fcsel(OutputFPRegister(select),
3146 InputFPRegisterAt(select, 1),
3147 InputFPRegisterAt(select, 0),
3148 csel_cond);
3149 } else {
3150 __ Csel(OutputRegister(select),
3151 InputOperandAt(select, 1),
3152 InputOperandAt(select, 0),
3153 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003154 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003155}
3156
David Srbecky0cf44932015-12-09 14:09:59 +00003157void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3158 new (GetGraph()->GetArena()) LocationSummary(info);
3159}
3160
David Srbeckyd28f4a02016-03-14 17:14:24 +00003161void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3162 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003163}
3164
3165void CodeGeneratorARM64::GenerateNop() {
3166 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003167}
3168
Alexandre Rames5319def2014-10-23 10:03:10 +01003169void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003170 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003171}
3172
3173void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003174 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003175}
3176
3177void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003178 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003179}
3180
3181void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003182 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003183}
3184
Roland Levillain44015862016-01-22 11:47:17 +00003185static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3186 return kEmitCompilerReadBarrier &&
3187 (kUseBakerReadBarrier ||
3188 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3189 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3190 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3191}
3192
Alexandre Rames67555f72014-11-18 10:55:16 +00003193void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003194 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003195 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003196 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003197 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003198 case TypeCheckKind::kExactCheck:
3199 case TypeCheckKind::kAbstractClassCheck:
3200 case TypeCheckKind::kClassHierarchyCheck:
3201 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003202 call_kind =
3203 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003204 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003205 break;
3206 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003207 case TypeCheckKind::kUnresolvedCheck:
3208 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003209 call_kind = LocationSummary::kCallOnSlowPath;
3210 break;
3211 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003212
Alexandre Rames67555f72014-11-18 10:55:16 +00003213 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003214 if (baker_read_barrier_slow_path) {
3215 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
3216 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003217 locations->SetInAt(0, Location::RequiresRegister());
3218 locations->SetInAt(1, Location::RequiresRegister());
3219 // The "out" register is used as a temporary, so it overlaps with the inputs.
3220 // Note that TypeCheckSlowPathARM64 uses this register too.
3221 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3222 // When read barriers are enabled, we need a temporary register for
3223 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003224 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003225 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003226 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003227}
3228
3229void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003230 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003231 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003232 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003233 Register obj = InputRegisterAt(instruction, 0);
3234 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003235 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003236 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003237 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3238 locations->GetTemp(0) :
3239 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003240 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3241 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3242 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3243 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003244
Scott Wakeling97c72b72016-06-24 16:19:36 +01003245 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003246 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003247
3248 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003249 // Avoid null check if we know `obj` is not null.
3250 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003251 __ Cbz(obj, &zero);
3252 }
3253
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003254 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003255 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003256
Roland Levillain44015862016-01-22 11:47:17 +00003257 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003258 case TypeCheckKind::kExactCheck: {
3259 __ Cmp(out, cls);
3260 __ Cset(out, eq);
3261 if (zero.IsLinked()) {
3262 __ B(&done);
3263 }
3264 break;
3265 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003266
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003267 case TypeCheckKind::kAbstractClassCheck: {
3268 // If the class is abstract, we eagerly fetch the super class of the
3269 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003270 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003271 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003272 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003273 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003274 // If `out` is null, we use it for the result, and jump to `done`.
3275 __ Cbz(out, &done);
3276 __ Cmp(out, cls);
3277 __ B(ne, &loop);
3278 __ Mov(out, 1);
3279 if (zero.IsLinked()) {
3280 __ B(&done);
3281 }
3282 break;
3283 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003284
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003285 case TypeCheckKind::kClassHierarchyCheck: {
3286 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003287 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003288 __ Bind(&loop);
3289 __ Cmp(out, cls);
3290 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003291 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003292 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003293 __ Cbnz(out, &loop);
3294 // If `out` is null, we use it for the result, and jump to `done`.
3295 __ B(&done);
3296 __ Bind(&success);
3297 __ Mov(out, 1);
3298 if (zero.IsLinked()) {
3299 __ B(&done);
3300 }
3301 break;
3302 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003303
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003304 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003305 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003306 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003307 __ Cmp(out, cls);
3308 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003309 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003310 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003311 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003312 // If `out` is null, we use it for the result, and jump to `done`.
3313 __ Cbz(out, &done);
3314 __ Ldrh(out, HeapOperand(out, primitive_offset));
3315 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3316 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003317 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003318 __ Mov(out, 1);
3319 __ B(&done);
3320 break;
3321 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003322
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003323 case TypeCheckKind::kArrayCheck: {
3324 __ Cmp(out, cls);
3325 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003326 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3327 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003328 codegen_->AddSlowPath(slow_path);
3329 __ B(ne, slow_path->GetEntryLabel());
3330 __ Mov(out, 1);
3331 if (zero.IsLinked()) {
3332 __ B(&done);
3333 }
3334 break;
3335 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003336
Calin Juravle98893e12015-10-02 21:05:03 +01003337 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003338 case TypeCheckKind::kInterfaceCheck: {
3339 // Note that we indeed only call on slow path, but we always go
3340 // into the slow path for the unresolved and interface check
3341 // cases.
3342 //
3343 // We cannot directly call the InstanceofNonTrivial runtime
3344 // entry point without resorting to a type checking slow path
3345 // here (i.e. by calling InvokeRuntime directly), as it would
3346 // require to assign fixed registers for the inputs of this
3347 // HInstanceOf instruction (following the runtime calling
3348 // convention), which might be cluttered by the potential first
3349 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003350 //
3351 // TODO: Introduce a new runtime entry point taking the object
3352 // to test (instead of its class) as argument, and let it deal
3353 // with the read barrier issues. This will let us refactor this
3354 // case of the `switch` code as it was previously (with a direct
3355 // call to the runtime not using a type checking slow path).
3356 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003357 DCHECK(locations->OnlyCallsOnSlowPath());
3358 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3359 /* is_fatal */ false);
3360 codegen_->AddSlowPath(slow_path);
3361 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003362 if (zero.IsLinked()) {
3363 __ B(&done);
3364 }
3365 break;
3366 }
3367 }
3368
3369 if (zero.IsLinked()) {
3370 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003371 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003372 }
3373
3374 if (done.IsLinked()) {
3375 __ Bind(&done);
3376 }
3377
3378 if (slow_path != nullptr) {
3379 __ Bind(slow_path->GetExitLabel());
3380 }
3381}
3382
3383void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3384 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3385 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3386
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003387 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003388 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003389 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003390 case TypeCheckKind::kExactCheck:
3391 case TypeCheckKind::kAbstractClassCheck:
3392 case TypeCheckKind::kClassHierarchyCheck:
3393 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003394 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3395 LocationSummary::kCallOnSlowPath :
3396 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Vladimir Marko70e97462016-08-09 11:04:26 +01003397 baker_read_barrier_slow_path = kUseBakerReadBarrier && !throws_into_catch;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003398 break;
3399 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003400 case TypeCheckKind::kUnresolvedCheck:
3401 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003402 call_kind = LocationSummary::kCallOnSlowPath;
3403 break;
3404 }
3405
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003407 if (baker_read_barrier_slow_path) {
3408 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
3409 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003410 locations->SetInAt(0, Location::RequiresRegister());
3411 locations->SetInAt(1, Location::RequiresRegister());
3412 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3413 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003414 // When read barriers are enabled, we need an additional temporary
3415 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003416 if (TypeCheckNeedsATemporary(type_check_kind)) {
3417 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003418 }
3419}
3420
3421void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003422 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003423 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003424 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003425 Register obj = InputRegisterAt(instruction, 0);
3426 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003427 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003428 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3429 locations->GetTemp(1) :
3430 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003431 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003432 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3433 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3434 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3435 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003436
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003437 bool is_type_check_slow_path_fatal =
3438 (type_check_kind == TypeCheckKind::kExactCheck ||
3439 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3440 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3441 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3442 !instruction->CanThrowIntoCatchBlock();
3443 SlowPathCodeARM64* type_check_slow_path =
3444 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3445 is_type_check_slow_path_fatal);
3446 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003447
Scott Wakeling97c72b72016-06-24 16:19:36 +01003448 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003449 // Avoid null check if we know obj is not null.
3450 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003451 __ Cbz(obj, &done);
3452 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003453
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003454 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003455 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003456
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003457 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003458 case TypeCheckKind::kExactCheck:
3459 case TypeCheckKind::kArrayCheck: {
3460 __ Cmp(temp, cls);
3461 // Jump to slow path for throwing the exception or doing a
3462 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003463 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003464 break;
3465 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003466
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003467 case TypeCheckKind::kAbstractClassCheck: {
3468 // If the class is abstract, we eagerly fetch the super class of the
3469 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003470 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003471 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003472 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003473 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003474
3475 // If the class reference currently in `temp` is not null, jump
3476 // to the `compare_classes` label to compare it with the checked
3477 // class.
3478 __ Cbnz(temp, &compare_classes);
3479 // Otherwise, jump to the slow path to throw the exception.
3480 //
3481 // But before, move back the object's class into `temp` before
3482 // going into the slow path, as it has been overwritten in the
3483 // meantime.
3484 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003485 GenerateReferenceLoadTwoRegisters(
3486 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003487 __ B(type_check_slow_path->GetEntryLabel());
3488
3489 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003490 __ Cmp(temp, cls);
3491 __ B(ne, &loop);
3492 break;
3493 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003494
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003495 case TypeCheckKind::kClassHierarchyCheck: {
3496 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003497 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003498 __ Bind(&loop);
3499 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003500 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003501
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003502 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003503 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003504
3505 // If the class reference currently in `temp` is not null, jump
3506 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003507 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003508 // Otherwise, jump to the slow path to throw the exception.
3509 //
3510 // But before, move back the object's class into `temp` before
3511 // going into the slow path, as it has been overwritten in the
3512 // meantime.
3513 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003514 GenerateReferenceLoadTwoRegisters(
3515 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003516 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003517 break;
3518 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003519
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003520 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003521 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003522 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003523 __ Cmp(temp, cls);
3524 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003525
3526 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003527 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003528 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003529
3530 // If the component type is not null (i.e. the object is indeed
3531 // an array), jump to label `check_non_primitive_component_type`
3532 // to further check that this component type is not a primitive
3533 // type.
3534 __ Cbnz(temp, &check_non_primitive_component_type);
3535 // Otherwise, jump to the slow path to throw the exception.
3536 //
3537 // But before, move back the object's class into `temp` before
3538 // going into the slow path, as it has been overwritten in the
3539 // meantime.
3540 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003541 GenerateReferenceLoadTwoRegisters(
3542 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003543 __ B(type_check_slow_path->GetEntryLabel());
3544
3545 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003546 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3547 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003548 __ Cbz(temp, &done);
3549 // Same comment as above regarding `temp` and the slow path.
3550 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003551 GenerateReferenceLoadTwoRegisters(
3552 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003553 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003554 break;
3555 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003556
Calin Juravle98893e12015-10-02 21:05:03 +01003557 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003558 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003559 // We always go into the type check slow path for the unresolved
3560 // and interface check cases.
3561 //
3562 // We cannot directly call the CheckCast runtime entry point
3563 // without resorting to a type checking slow path here (i.e. by
3564 // calling InvokeRuntime directly), as it would require to
3565 // assign fixed registers for the inputs of this HInstanceOf
3566 // instruction (following the runtime calling convention), which
3567 // might be cluttered by the potential first read barrier
3568 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003569 //
3570 // TODO: Introduce a new runtime entry point taking the object
3571 // to test (instead of its class) as argument, and let it deal
3572 // with the read barrier issues. This will let us refactor this
3573 // case of the `switch` code as it was previously (with a direct
3574 // call to the runtime not using a type checking slow path).
3575 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003576 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003577 break;
3578 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003579 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003580
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003581 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003582}
3583
Alexandre Rames5319def2014-10-23 10:03:10 +01003584void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3585 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3586 locations->SetOut(Location::ConstantLocation(constant));
3587}
3588
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003589void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003590 // Will be generated at use site.
3591}
3592
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003593void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3594 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3595 locations->SetOut(Location::ConstantLocation(constant));
3596}
3597
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003598void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003599 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003600}
3601
Calin Juravle175dc732015-08-25 15:42:32 +01003602void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3603 // The trampoline uses the same calling convention as dex calling conventions,
3604 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3605 // the method_idx.
3606 HandleInvoke(invoke);
3607}
3608
3609void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3610 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3611}
3612
Alexandre Rames5319def2014-10-23 10:03:10 +01003613void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003614 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003615 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003616}
3617
Alexandre Rames67555f72014-11-18 10:55:16 +00003618void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3619 HandleInvoke(invoke);
3620}
3621
3622void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3623 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003624 LocationSummary* locations = invoke->GetLocations();
3625 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003626 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003627 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003628 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003629
3630 // The register ip1 is required to be used for the hidden argument in
3631 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003632 MacroAssembler* masm = GetVIXLAssembler();
3633 UseScratchRegisterScope scratch_scope(masm);
3634 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003635 scratch_scope.Exclude(ip1);
3636 __ Mov(ip1, invoke->GetDexMethodIndex());
3637
Alexandre Rames67555f72014-11-18 10:55:16 +00003638 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003639 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003640 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003641 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003642 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003643 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003644 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003645 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003646 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003647 // Instead of simply (possibly) unpoisoning `temp` here, we should
3648 // emit a read barrier for the previous class reference load.
3649 // However this is not required in practice, as this is an
3650 // intermediate/temporary reference and because the current
3651 // concurrent copying collector keeps the from-space memory
3652 // intact/accessible until the end of the marking phase (the
3653 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003654 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003655 __ Ldr(temp,
3656 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3657 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003658 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003659 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003660 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003661 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003662 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003663 // lr();
3664 __ Blr(lr);
3665 DCHECK(!codegen_->IsLeafMethod());
3666 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3667}
3668
3669void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003670 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3671 if (intrinsic.TryDispatch(invoke)) {
3672 return;
3673 }
3674
Alexandre Rames67555f72014-11-18 10:55:16 +00003675 HandleInvoke(invoke);
3676}
3677
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003678void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003679 // Explicit clinit checks triggered by static invokes must have been pruned by
3680 // art::PrepareForRegisterAllocation.
3681 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003682
Andreas Gampe878d58c2015-01-15 23:24:00 -08003683 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3684 if (intrinsic.TryDispatch(invoke)) {
3685 return;
3686 }
3687
Alexandre Rames67555f72014-11-18 10:55:16 +00003688 HandleInvoke(invoke);
3689}
3690
Andreas Gampe878d58c2015-01-15 23:24:00 -08003691static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3692 if (invoke->GetLocations()->Intrinsified()) {
3693 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3694 intrinsic.Dispatch(invoke);
3695 return true;
3696 }
3697 return false;
3698}
3699
Vladimir Markodc151b22015-10-15 18:02:30 +01003700HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3701 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3702 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003703 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003704 return desired_dispatch_info;
3705}
3706
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003707void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003708 // For better instruction scheduling we load the direct code pointer before the method pointer.
3709 bool direct_code_loaded = false;
3710 switch (invoke->GetCodePtrLocation()) {
3711 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3712 // LR = code address from literal pool with link-time patch.
3713 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3714 direct_code_loaded = true;
3715 break;
3716 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3717 // LR = invoke->GetDirectCodePtr();
3718 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3719 direct_code_loaded = true;
3720 break;
3721 default:
3722 break;
3723 }
3724
Andreas Gampe878d58c2015-01-15 23:24:00 -08003725 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003726 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3727 switch (invoke->GetMethodLoadKind()) {
3728 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3729 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003730 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003731 break;
3732 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003733 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003734 break;
3735 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3736 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003737 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003738 break;
3739 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3740 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003741 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003742 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3743 break;
3744 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3745 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003746 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3747 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003748 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003749 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003750 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003751 __ Bind(adrp_label);
3752 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003753 }
Vladimir Marko58155012015-08-19 12:49:41 +00003754 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003755 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003756 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003757 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003758 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003759 __ Bind(ldr_label);
3760 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003761 }
Vladimir Marko58155012015-08-19 12:49:41 +00003762 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003763 }
Vladimir Marko58155012015-08-19 12:49:41 +00003764 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003765 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003766 Register reg = XRegisterFrom(temp);
3767 Register method_reg;
3768 if (current_method.IsRegister()) {
3769 method_reg = XRegisterFrom(current_method);
3770 } else {
3771 DCHECK(invoke->GetLocations()->Intrinsified());
3772 DCHECK(!current_method.IsValid());
3773 method_reg = reg;
3774 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3775 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003776
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003777 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003778 __ Ldr(reg.X(),
3779 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003780 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003781 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003782 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3783 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003784 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3785 break;
3786 }
3787 }
3788
3789 switch (invoke->GetCodePtrLocation()) {
3790 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3791 __ Bl(&frame_entry_label_);
3792 break;
3793 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3794 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003795 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3796 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003797 __ Bind(label);
3798 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003799 break;
3800 }
3801 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3802 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3803 // LR prepared above for better instruction scheduling.
3804 DCHECK(direct_code_loaded);
3805 // lr()
3806 __ Blr(lr);
3807 break;
3808 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3809 // LR = callee_method->entry_point_from_quick_compiled_code_;
3810 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003811 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003812 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003813 // lr()
3814 __ Blr(lr);
3815 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003816 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003817
Andreas Gampe878d58c2015-01-15 23:24:00 -08003818 DCHECK(!IsLeafMethod());
3819}
3820
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003821void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003822 // Use the calling convention instead of the location of the receiver, as
3823 // intrinsics may have put the receiver in a different register. In the intrinsics
3824 // slow path, the arguments have been moved to the right place, so here we are
3825 // guaranteed that the receiver is the first register of the calling convention.
3826 InvokeDexCallingConvention calling_convention;
3827 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003828 Register temp = XRegisterFrom(temp_in);
3829 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3830 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3831 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003832 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003833
3834 BlockPoolsScope block_pools(GetVIXLAssembler());
3835
3836 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003837 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003838 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003839 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003840 // Instead of simply (possibly) unpoisoning `temp` here, we should
3841 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003842 // intermediate/temporary reference and because the current
3843 // concurrent copying collector keeps the from-space memory
3844 // intact/accessible until the end of the marking phase (the
3845 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003846 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3847 // temp = temp->GetMethodAt(method_offset);
3848 __ Ldr(temp, MemOperand(temp, method_offset));
3849 // lr = temp->GetEntryPoint();
3850 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3851 // lr();
3852 __ Blr(lr);
3853}
3854
Scott Wakeling97c72b72016-06-24 16:19:36 +01003855vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3856 const DexFile& dex_file,
3857 uint32_t string_index,
3858 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003859 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3860}
3861
Scott Wakeling97c72b72016-06-24 16:19:36 +01003862vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3863 const DexFile& dex_file,
3864 uint32_t type_index,
3865 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003866 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3867}
3868
Scott Wakeling97c72b72016-06-24 16:19:36 +01003869vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3870 const DexFile& dex_file,
3871 uint32_t element_offset,
3872 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003873 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3874}
3875
Scott Wakeling97c72b72016-06-24 16:19:36 +01003876vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3877 const DexFile& dex_file,
3878 uint32_t offset_or_index,
3879 vixl::aarch64::Label* adrp_label,
3880 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003881 // Add a patch entry and return the label.
3882 patches->emplace_back(dex_file, offset_or_index);
3883 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003884 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003885 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3886 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3887 return label;
3888}
3889
Scott Wakeling97c72b72016-06-24 16:19:36 +01003890vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003891 const DexFile& dex_file, uint32_t string_index) {
3892 return boot_image_string_patches_.GetOrCreate(
3893 StringReference(&dex_file, string_index),
3894 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3895}
3896
Scott Wakeling97c72b72016-06-24 16:19:36 +01003897vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003898 const DexFile& dex_file, uint32_t type_index) {
3899 return boot_image_type_patches_.GetOrCreate(
3900 TypeReference(&dex_file, type_index),
3901 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3902}
3903
Scott Wakeling97c72b72016-06-24 16:19:36 +01003904vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3905 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003906 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3907 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3908 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3909}
3910
Scott Wakeling97c72b72016-06-24 16:19:36 +01003911vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3912 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003913 return DeduplicateUint64Literal(address);
3914}
3915
Vladimir Marko58155012015-08-19 12:49:41 +00003916void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3917 DCHECK(linker_patches->empty());
3918 size_t size =
3919 method_patches_.size() +
3920 call_patches_.size() +
3921 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003922 pc_relative_dex_cache_patches_.size() +
3923 boot_image_string_patches_.size() +
3924 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003925 boot_image_type_patches_.size() +
3926 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003927 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003928 linker_patches->reserve(size);
3929 for (const auto& entry : method_patches_) {
3930 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003931 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3932 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003933 target_method.dex_file,
3934 target_method.dex_method_index));
3935 }
3936 for (const auto& entry : call_patches_) {
3937 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003938 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3939 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003940 target_method.dex_file,
3941 target_method.dex_method_index));
3942 }
Scott Wakeling97c72b72016-06-24 16:19:36 +01003943 for (const MethodPatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3944 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003945 info.target_method.dex_file,
3946 info.target_method.dex_method_index));
3947 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003948 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003949 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003950 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003951 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003952 info.offset_or_index));
3953 }
3954 for (const auto& entry : boot_image_string_patches_) {
3955 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003956 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3957 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003958 target_string.dex_file,
3959 target_string.string_index));
3960 }
3961 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003962 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003963 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003964 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003965 info.offset_or_index));
3966 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003967 for (const auto& entry : boot_image_type_patches_) {
3968 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003969 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3970 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003971 target_type.dex_file,
3972 target_type.type_index));
3973 }
3974 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003975 linker_patches->push_back(LinkerPatch::RelativeTypePatch(info.label.GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003976 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003977 info.pc_insn_label->GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003978 info.offset_or_index));
3979 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003980 for (const auto& entry : boot_image_address_patches_) {
3981 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003982 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3983 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003984 }
3985}
3986
Scott Wakeling97c72b72016-06-24 16:19:36 +01003987vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003988 Uint32ToLiteralMap* map) {
3989 return map->GetOrCreate(
3990 value,
3991 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3992}
3993
Scott Wakeling97c72b72016-06-24 16:19:36 +01003994vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003995 return uint64_literals_.GetOrCreate(
3996 value,
3997 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003998}
3999
Scott Wakeling97c72b72016-06-24 16:19:36 +01004000vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004001 MethodReference target_method,
4002 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004003 return map->GetOrCreate(
4004 target_method,
4005 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004006}
4007
Scott Wakeling97c72b72016-06-24 16:19:36 +01004008vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004009 MethodReference target_method) {
4010 return DeduplicateMethodLiteral(target_method, &method_patches_);
4011}
4012
Scott Wakeling97c72b72016-06-24 16:19:36 +01004013vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004014 MethodReference target_method) {
4015 return DeduplicateMethodLiteral(target_method, &call_patches_);
4016}
4017
4018
Andreas Gampe878d58c2015-01-15 23:24:00 -08004019void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004020 // Explicit clinit checks triggered by static invokes must have been pruned by
4021 // art::PrepareForRegisterAllocation.
4022 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004023
Andreas Gampe878d58c2015-01-15 23:24:00 -08004024 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4025 return;
4026 }
4027
Alexandre Ramesd921d642015-04-16 15:07:16 +01004028 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004029 LocationSummary* locations = invoke->GetLocations();
4030 codegen_->GenerateStaticOrDirectCall(
4031 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004032 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004033}
4034
4035void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004036 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4037 return;
4038 }
4039
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004040 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004041 DCHECK(!codegen_->IsLeafMethod());
4042 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4043}
4044
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004045HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4046 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004047 switch (desired_class_load_kind) {
4048 case HLoadClass::LoadKind::kReferrersClass:
4049 break;
4050 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4051 DCHECK(!GetCompilerOptions().GetCompilePic());
4052 break;
4053 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4054 DCHECK(GetCompilerOptions().GetCompilePic());
4055 break;
4056 case HLoadClass::LoadKind::kBootImageAddress:
4057 break;
4058 case HLoadClass::LoadKind::kDexCacheAddress:
4059 DCHECK(Runtime::Current()->UseJitCompilation());
4060 break;
4061 case HLoadClass::LoadKind::kDexCachePcRelative:
4062 DCHECK(!Runtime::Current()->UseJitCompilation());
4063 break;
4064 case HLoadClass::LoadKind::kDexCacheViaMethod:
4065 break;
4066 }
4067 return desired_class_load_kind;
4068}
4069
Alexandre Rames67555f72014-11-18 10:55:16 +00004070void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004071 if (cls->NeedsAccessCheck()) {
4072 InvokeRuntimeCallingConvention calling_convention;
4073 CodeGenerator::CreateLoadClassLocationSummary(
4074 cls,
4075 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01004076 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004077 /* code_generator_supports_read_barrier */ true);
4078 return;
4079 }
4080
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004081 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4082 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004083 ? LocationSummary::kCallOnSlowPath
4084 : LocationSummary::kNoCall;
4085 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004086 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004087 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4088 }
4089
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004090 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4091 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4092 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4093 locations->SetInAt(0, Location::RequiresRegister());
4094 }
4095 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004096}
4097
4098void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004099 if (cls->NeedsAccessCheck()) {
4100 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004101 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004102 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004103 return;
4104 }
4105
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004106 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004107 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004108
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004109 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004110 bool generate_null_check = false;
4111 switch (cls->GetLoadKind()) {
4112 case HLoadClass::LoadKind::kReferrersClass: {
4113 DCHECK(!cls->CanCallRuntime());
4114 DCHECK(!cls->MustGenerateClinitCheck());
4115 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4116 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004117 GenerateGcRootFieldLoad(cls,
4118 out_loc,
4119 current_method,
4120 ArtMethod::DeclaringClassOffset().Int32Value(),
4121 /*fixup_label*/ nullptr,
4122 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004123 break;
4124 }
4125 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004126 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004127 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4128 cls->GetTypeIndex()));
4129 break;
4130 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004131 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004132 // Add ADRP with its PC-relative type patch.
4133 const DexFile& dex_file = cls->GetDexFile();
4134 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004135 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004136 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004137 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004138 __ Bind(adrp_label);
4139 __ adrp(out.X(), /* offset placeholder */ 0);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004140 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004141 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004142 vixl::aarch64::Label* add_label =
4143 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004144 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004145 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004146 __ Bind(add_label);
4147 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004148 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004149 break;
4150 }
4151 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004152 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004153 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4154 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4155 break;
4156 }
4157 case HLoadClass::LoadKind::kDexCacheAddress: {
4158 DCHECK_NE(cls->GetAddress(), 0u);
4159 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4160 // that gives a 16KiB range. To try and reduce the number of literals if we load
4161 // multiple types, simply split the dex cache address to a 16KiB aligned base
4162 // loaded from a literal and the remaining offset embedded in the load.
4163 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4164 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4165 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4166 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4167 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4168 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4169 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004170 GenerateGcRootFieldLoad(cls,
4171 out_loc,
4172 out.X(),
4173 offset,
4174 /*fixup_label*/ nullptr,
4175 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004176 generate_null_check = !cls->IsInDexCache();
4177 break;
4178 }
4179 case HLoadClass::LoadKind::kDexCachePcRelative: {
4180 // Add ADRP with its PC-relative DexCache access patch.
4181 const DexFile& dex_file = cls->GetDexFile();
4182 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004183 vixl::aarch64::Label* adrp_label =
4184 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004185 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004186 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004187 __ Bind(adrp_label);
4188 __ adrp(out.X(), /* offset placeholder */ 0);
4189 }
4190 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004191 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004192 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4193 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004194 GenerateGcRootFieldLoad(cls,
4195 out_loc,
4196 out.X(),
4197 /* offset placeholder */ 0,
4198 ldr_label,
4199 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004200 generate_null_check = !cls->IsInDexCache();
4201 break;
4202 }
4203 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4204 MemberOffset resolved_types_offset =
4205 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4206 // /* GcRoot<mirror::Class>[] */ out =
4207 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4208 Register current_method = InputRegisterAt(cls, 0);
4209 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4210 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004211 GenerateGcRootFieldLoad(cls,
4212 out_loc,
4213 out.X(),
4214 CodeGenerator::GetCacheOffset(cls->GetTypeIndex()),
4215 /*fixup_label*/ nullptr,
4216 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004217 generate_null_check = !cls->IsInDexCache();
4218 break;
4219 }
4220 }
4221
4222 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4223 DCHECK(cls->CanCallRuntime());
4224 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4225 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4226 codegen_->AddSlowPath(slow_path);
4227 if (generate_null_check) {
4228 __ Cbz(out, slow_path->GetEntryLabel());
4229 }
4230 if (cls->MustGenerateClinitCheck()) {
4231 GenerateClassInitializationCheck(slow_path, out);
4232 } else {
4233 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004234 }
4235 }
4236}
4237
David Brazdilcb1c0552015-08-04 16:22:25 +01004238static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004239 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004240}
4241
Alexandre Rames67555f72014-11-18 10:55:16 +00004242void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4243 LocationSummary* locations =
4244 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4245 locations->SetOut(Location::RequiresRegister());
4246}
4247
4248void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004249 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4250}
4251
4252void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4253 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4254}
4255
4256void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4257 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004258}
4259
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004260HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4261 HLoadString::LoadKind desired_string_load_kind) {
4262 if (kEmitCompilerReadBarrier) {
4263 switch (desired_string_load_kind) {
4264 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4265 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4266 case HLoadString::LoadKind::kBootImageAddress:
4267 // TODO: Implement for read barrier.
4268 return HLoadString::LoadKind::kDexCacheViaMethod;
4269 default:
4270 break;
4271 }
4272 }
4273 switch (desired_string_load_kind) {
4274 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4275 DCHECK(!GetCompilerOptions().GetCompilePic());
4276 break;
4277 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4278 DCHECK(GetCompilerOptions().GetCompilePic());
4279 break;
4280 case HLoadString::LoadKind::kBootImageAddress:
4281 break;
4282 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004283 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004284 break;
4285 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01004286 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004287 break;
4288 case HLoadString::LoadKind::kDexCacheViaMethod:
4289 break;
4290 }
4291 return desired_string_load_kind;
4292}
4293
Alexandre Rames67555f72014-11-18 10:55:16 +00004294void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004295 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004296 ? LocationSummary::kCallOnSlowPath
4297 : LocationSummary::kNoCall;
4298 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01004299 if (kUseBakerReadBarrier && !load->NeedsEnvironment()) {
4300 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4301 }
4302
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004303 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4304 locations->SetInAt(0, Location::RequiresRegister());
4305 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004306 locations->SetOut(Location::RequiresRegister());
4307}
4308
4309void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004310 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004311
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004312 switch (load->GetLoadKind()) {
4313 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4314 DCHECK(!kEmitCompilerReadBarrier);
4315 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4316 load->GetStringIndex()));
4317 return; // No dex cache slow path.
4318 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4319 DCHECK(!kEmitCompilerReadBarrier);
4320 // Add ADRP with its PC-relative String patch.
4321 const DexFile& dex_file = load->GetDexFile();
4322 uint32_t string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004323 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004324 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004325 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004326 __ Bind(adrp_label);
4327 __ adrp(out.X(), /* offset placeholder */ 0);
4328 }
4329 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004330 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004331 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4332 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004333 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004334 __ Bind(add_label);
4335 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4336 }
4337 return; // No dex cache slow path.
4338 }
4339 case HLoadString::LoadKind::kBootImageAddress: {
4340 DCHECK(!kEmitCompilerReadBarrier);
4341 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4342 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4343 return; // No dex cache slow path.
4344 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004345 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004346 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004347 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004348
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004349 // TODO: Re-add the compiler code to do string dex cache lookup again.
4350 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4351 codegen_->AddSlowPath(slow_path);
4352 __ B(slow_path->GetEntryLabel());
4353 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004354}
4355
Alexandre Rames5319def2014-10-23 10:03:10 +01004356void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4357 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4358 locations->SetOut(Location::ConstantLocation(constant));
4359}
4360
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004361void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004362 // Will be generated at use site.
4363}
4364
Alexandre Rames67555f72014-11-18 10:55:16 +00004365void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4366 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004367 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004368 InvokeRuntimeCallingConvention calling_convention;
4369 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4370}
4371
4372void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004373 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject: kQuickUnlockObject,
4374 instruction,
4375 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004376 if (instruction->IsEnter()) {
4377 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4378 } else {
4379 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4380 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004381}
4382
Alexandre Rames42d641b2014-10-27 14:00:51 +00004383void LocationsBuilderARM64::VisitMul(HMul* mul) {
4384 LocationSummary* locations =
4385 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4386 switch (mul->GetResultType()) {
4387 case Primitive::kPrimInt:
4388 case Primitive::kPrimLong:
4389 locations->SetInAt(0, Location::RequiresRegister());
4390 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004391 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004392 break;
4393
4394 case Primitive::kPrimFloat:
4395 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004396 locations->SetInAt(0, Location::RequiresFpuRegister());
4397 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004398 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004399 break;
4400
4401 default:
4402 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4403 }
4404}
4405
4406void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4407 switch (mul->GetResultType()) {
4408 case Primitive::kPrimInt:
4409 case Primitive::kPrimLong:
4410 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4411 break;
4412
4413 case Primitive::kPrimFloat:
4414 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004415 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004416 break;
4417
4418 default:
4419 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4420 }
4421}
4422
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004423void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4424 LocationSummary* locations =
4425 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4426 switch (neg->GetResultType()) {
4427 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004428 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004429 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004431 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004432
4433 case Primitive::kPrimFloat:
4434 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004435 locations->SetInAt(0, Location::RequiresFpuRegister());
4436 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004437 break;
4438
4439 default:
4440 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4441 }
4442}
4443
4444void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4445 switch (neg->GetResultType()) {
4446 case Primitive::kPrimInt:
4447 case Primitive::kPrimLong:
4448 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4449 break;
4450
4451 case Primitive::kPrimFloat:
4452 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004453 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004454 break;
4455
4456 default:
4457 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4458 }
4459}
4460
4461void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4462 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004463 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004464 InvokeRuntimeCallingConvention calling_convention;
4465 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004466 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004467 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004468 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004469}
4470
4471void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4472 LocationSummary* locations = instruction->GetLocations();
4473 InvokeRuntimeCallingConvention calling_convention;
4474 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4475 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004476 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004477 // Note: if heap poisoning is enabled, the entry point takes cares
4478 // of poisoning the reference.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004479 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004480 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004481}
4482
Alexandre Rames5319def2014-10-23 10:03:10 +01004483void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4484 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004485 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004486 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004487 if (instruction->IsStringAlloc()) {
4488 locations->AddTemp(LocationFrom(kArtMethodRegister));
4489 } else {
4490 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4491 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4492 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004493 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4494}
4495
4496void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004497 // Note: if heap poisoning is enabled, the entry point takes cares
4498 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004499 if (instruction->IsStringAlloc()) {
4500 // String is allocated through StringFactory. Call NewEmptyString entry point.
4501 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004502 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004503 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4504 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4505 __ Blr(lr);
4506 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4507 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004508 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004509 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4510 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004511}
4512
4513void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4514 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004515 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004516 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004517}
4518
4519void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004520 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004521 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004522 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004523 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004524 break;
4525
4526 default:
4527 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4528 }
4529}
4530
David Brazdil66d126e2015-04-03 16:02:44 +01004531void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4532 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4533 locations->SetInAt(0, Location::RequiresRegister());
4534 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4535}
4536
4537void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004538 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004539}
4540
Alexandre Rames5319def2014-10-23 10:03:10 +01004541void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004542 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4543 ? LocationSummary::kCallOnSlowPath
4544 : LocationSummary::kNoCall;
4545 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004546 locations->SetInAt(0, Location::RequiresRegister());
4547 if (instruction->HasUses()) {
4548 locations->SetOut(Location::SameAsFirstInput());
4549 }
4550}
4551
Calin Juravle2ae48182016-03-16 14:05:09 +00004552void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4553 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004554 return;
4555 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004556
Alexandre Ramesd921d642015-04-16 15:07:16 +01004557 BlockPoolsScope block_pools(GetVIXLAssembler());
4558 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004559 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004560 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004561}
4562
Calin Juravle2ae48182016-03-16 14:05:09 +00004563void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004564 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004565 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004566
4567 LocationSummary* locations = instruction->GetLocations();
4568 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004569
4570 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004571}
4572
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004573void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004574 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004575}
4576
Alexandre Rames67555f72014-11-18 10:55:16 +00004577void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4578 HandleBinaryOp(instruction);
4579}
4580
4581void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4582 HandleBinaryOp(instruction);
4583}
4584
Alexandre Rames3e69f162014-12-10 10:36:50 +00004585void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4586 LOG(FATAL) << "Unreachable";
4587}
4588
4589void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4590 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4591}
4592
Alexandre Rames5319def2014-10-23 10:03:10 +01004593void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4594 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4595 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4596 if (location.IsStackSlot()) {
4597 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4598 } else if (location.IsDoubleStackSlot()) {
4599 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4600 }
4601 locations->SetOut(location);
4602}
4603
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004604void InstructionCodeGeneratorARM64::VisitParameterValue(
4605 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004606 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004607}
4608
4609void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4610 LocationSummary* locations =
4611 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004612 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004613}
4614
4615void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4616 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4617 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004618}
4619
4620void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4621 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004622 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004623 locations->SetInAt(i, Location::Any());
4624 }
4625 locations->SetOut(Location::Any());
4626}
4627
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004628void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004629 LOG(FATAL) << "Unreachable";
4630}
4631
Serban Constantinescu02164b32014-11-13 14:05:07 +00004632void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004633 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004634 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004635 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4636 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004637 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4638
4639 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004640 case Primitive::kPrimInt:
4641 case Primitive::kPrimLong:
4642 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004643 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004644 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4645 break;
4646
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004647 case Primitive::kPrimFloat:
4648 case Primitive::kPrimDouble: {
4649 InvokeRuntimeCallingConvention calling_convention;
4650 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4651 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4652 locations->SetOut(calling_convention.GetReturnLocation(type));
4653
4654 break;
4655 }
4656
Serban Constantinescu02164b32014-11-13 14:05:07 +00004657 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004658 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004659 }
4660}
4661
4662void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4663 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004664
Serban Constantinescu02164b32014-11-13 14:05:07 +00004665 switch (type) {
4666 case Primitive::kPrimInt:
4667 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004668 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004669 break;
4670 }
4671
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004672 case Primitive::kPrimFloat:
4673 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004674 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4675 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004676 if (type == Primitive::kPrimFloat) {
4677 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4678 } else {
4679 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4680 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004681 break;
4682 }
4683
Serban Constantinescu02164b32014-11-13 14:05:07 +00004684 default:
4685 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004686 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004687 }
4688}
4689
Calin Juravle27df7582015-04-17 19:12:31 +01004690void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4691 memory_barrier->SetLocations(nullptr);
4692}
4693
4694void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004695 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004696}
4697
Alexandre Rames5319def2014-10-23 10:03:10 +01004698void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4699 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4700 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004701 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004702}
4703
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004704void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004705 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004706}
4707
4708void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4709 instruction->SetLocations(nullptr);
4710}
4711
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004712void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004713 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004714}
4715
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004716void LocationsBuilderARM64::VisitRor(HRor* ror) {
4717 HandleBinaryOp(ror);
4718}
4719
4720void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4721 HandleBinaryOp(ror);
4722}
4723
Serban Constantinescu02164b32014-11-13 14:05:07 +00004724void LocationsBuilderARM64::VisitShl(HShl* shl) {
4725 HandleShift(shl);
4726}
4727
4728void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4729 HandleShift(shl);
4730}
4731
4732void LocationsBuilderARM64::VisitShr(HShr* shr) {
4733 HandleShift(shr);
4734}
4735
4736void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4737 HandleShift(shr);
4738}
4739
Alexandre Rames5319def2014-10-23 10:03:10 +01004740void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004741 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004742}
4743
4744void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004745 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004746}
4747
Alexandre Rames67555f72014-11-18 10:55:16 +00004748void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004749 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004750}
4751
4752void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004753 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004754}
4755
4756void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004757 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004758}
4759
Alexandre Rames67555f72014-11-18 10:55:16 +00004760void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004761 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004762}
4763
Calin Juravlee460d1d2015-09-29 04:52:17 +01004764void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4765 HUnresolvedInstanceFieldGet* instruction) {
4766 FieldAccessCallingConventionARM64 calling_convention;
4767 codegen_->CreateUnresolvedFieldLocationSummary(
4768 instruction, instruction->GetFieldType(), calling_convention);
4769}
4770
4771void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4772 HUnresolvedInstanceFieldGet* instruction) {
4773 FieldAccessCallingConventionARM64 calling_convention;
4774 codegen_->GenerateUnresolvedFieldAccess(instruction,
4775 instruction->GetFieldType(),
4776 instruction->GetFieldIndex(),
4777 instruction->GetDexPc(),
4778 calling_convention);
4779}
4780
4781void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4782 HUnresolvedInstanceFieldSet* instruction) {
4783 FieldAccessCallingConventionARM64 calling_convention;
4784 codegen_->CreateUnresolvedFieldLocationSummary(
4785 instruction, instruction->GetFieldType(), calling_convention);
4786}
4787
4788void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4789 HUnresolvedInstanceFieldSet* instruction) {
4790 FieldAccessCallingConventionARM64 calling_convention;
4791 codegen_->GenerateUnresolvedFieldAccess(instruction,
4792 instruction->GetFieldType(),
4793 instruction->GetFieldIndex(),
4794 instruction->GetDexPc(),
4795 calling_convention);
4796}
4797
4798void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4799 HUnresolvedStaticFieldGet* instruction) {
4800 FieldAccessCallingConventionARM64 calling_convention;
4801 codegen_->CreateUnresolvedFieldLocationSummary(
4802 instruction, instruction->GetFieldType(), calling_convention);
4803}
4804
4805void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4806 HUnresolvedStaticFieldGet* instruction) {
4807 FieldAccessCallingConventionARM64 calling_convention;
4808 codegen_->GenerateUnresolvedFieldAccess(instruction,
4809 instruction->GetFieldType(),
4810 instruction->GetFieldIndex(),
4811 instruction->GetDexPc(),
4812 calling_convention);
4813}
4814
4815void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4816 HUnresolvedStaticFieldSet* instruction) {
4817 FieldAccessCallingConventionARM64 calling_convention;
4818 codegen_->CreateUnresolvedFieldLocationSummary(
4819 instruction, instruction->GetFieldType(), calling_convention);
4820}
4821
4822void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4823 HUnresolvedStaticFieldSet* instruction) {
4824 FieldAccessCallingConventionARM64 calling_convention;
4825 codegen_->GenerateUnresolvedFieldAccess(instruction,
4826 instruction->GetFieldType(),
4827 instruction->GetFieldIndex(),
4828 instruction->GetDexPc(),
4829 calling_convention);
4830}
4831
Alexandre Rames5319def2014-10-23 10:03:10 +01004832void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004833 LocationSummary* locations =
4834 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4835 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01004836}
4837
4838void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004839 HBasicBlock* block = instruction->GetBlock();
4840 if (block->GetLoopInformation() != nullptr) {
4841 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4842 // The back edge will generate the suspend check.
4843 return;
4844 }
4845 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4846 // The goto will generate the suspend check.
4847 return;
4848 }
4849 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004850}
4851
Alexandre Rames67555f72014-11-18 10:55:16 +00004852void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4853 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004854 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004855 InvokeRuntimeCallingConvention calling_convention;
4856 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4857}
4858
4859void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004860 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004861 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004862}
4863
4864void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4865 LocationSummary* locations =
4866 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4867 Primitive::Type input_type = conversion->GetInputType();
4868 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004869 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004870 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4871 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4872 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4873 }
4874
Alexandre Rames542361f2015-01-29 16:57:31 +00004875 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004876 locations->SetInAt(0, Location::RequiresFpuRegister());
4877 } else {
4878 locations->SetInAt(0, Location::RequiresRegister());
4879 }
4880
Alexandre Rames542361f2015-01-29 16:57:31 +00004881 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004882 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4883 } else {
4884 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4885 }
4886}
4887
4888void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4889 Primitive::Type result_type = conversion->GetResultType();
4890 Primitive::Type input_type = conversion->GetInputType();
4891
4892 DCHECK_NE(input_type, result_type);
4893
Alexandre Rames542361f2015-01-29 16:57:31 +00004894 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004895 int result_size = Primitive::ComponentSize(result_type);
4896 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004897 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004898 Register output = OutputRegister(conversion);
4899 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004900 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004901 // 'int' values are used directly as W registers, discarding the top
4902 // bits, so we don't need to sign-extend and can just perform a move.
4903 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4904 // top 32 bits of the target register. We theoretically could leave those
4905 // bits unchanged, but we would have to make sure that no code uses a
4906 // 32bit input value as a 64bit value assuming that the top 32 bits are
4907 // zero.
4908 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004909 } else if (result_type == Primitive::kPrimChar ||
4910 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4911 __ Ubfx(output,
4912 output.IsX() ? source.X() : source.W(),
4913 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004914 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004915 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004916 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004917 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004918 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004919 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004920 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4921 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004922 } else if (Primitive::IsFloatingPointType(result_type) &&
4923 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004924 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4925 } else {
4926 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4927 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004928 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004929}
Alexandre Rames67555f72014-11-18 10:55:16 +00004930
Serban Constantinescu02164b32014-11-13 14:05:07 +00004931void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4932 HandleShift(ushr);
4933}
4934
4935void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4936 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004937}
4938
4939void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4940 HandleBinaryOp(instruction);
4941}
4942
4943void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4944 HandleBinaryOp(instruction);
4945}
4946
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004947void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004948 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004949 LOG(FATAL) << "Unreachable";
4950}
4951
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004952void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004953 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004954 LOG(FATAL) << "Unreachable";
4955}
4956
Mark Mendellfe57faa2015-09-18 09:26:15 -04004957// Simple implementation of packed switch - generate cascaded compare/jumps.
4958void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4959 LocationSummary* locations =
4960 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4961 locations->SetInAt(0, Location::RequiresRegister());
4962}
4963
4964void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4965 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004966 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004967 Register value_reg = InputRegisterAt(switch_instr, 0);
4968 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4969
Zheng Xu3927c8b2015-11-18 17:46:25 +08004970 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004971 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004972 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4973 // make sure we don't emit it if the target may run out of range.
4974 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4975 // ranges and emit the tables only as required.
4976 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004977
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004978 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004979 // Current instruction id is an upper bound of the number of HIRs in the graph.
4980 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4981 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004982 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4983 Register temp = temps.AcquireW();
4984 __ Subs(temp, value_reg, Operand(lower_bound));
4985
Zheng Xu3927c8b2015-11-18 17:46:25 +08004986 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004987 // Jump to successors[0] if value == lower_bound.
4988 __ B(eq, codegen_->GetLabelOf(successors[0]));
4989 int32_t last_index = 0;
4990 for (; num_entries - last_index > 2; last_index += 2) {
4991 __ Subs(temp, temp, Operand(2));
4992 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4993 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4994 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4995 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4996 }
4997 if (num_entries - last_index == 2) {
4998 // The last missing case_value.
4999 __ Cmp(temp, Operand(1));
5000 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005001 }
5002
5003 // And the default for any other value.
5004 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5005 __ B(codegen_->GetLabelOf(default_block));
5006 }
5007 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005008 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005009
5010 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5011
5012 // Below instructions should use at most one blocked register. Since there are two blocked
5013 // registers, we are free to block one.
5014 Register temp_w = temps.AcquireW();
5015 Register index;
5016 // Remove the bias.
5017 if (lower_bound != 0) {
5018 index = temp_w;
5019 __ Sub(index, value_reg, Operand(lower_bound));
5020 } else {
5021 index = value_reg;
5022 }
5023
5024 // Jump to default block if index is out of the range.
5025 __ Cmp(index, Operand(num_entries));
5026 __ B(hs, codegen_->GetLabelOf(default_block));
5027
5028 // In current VIXL implementation, it won't require any blocked registers to encode the
5029 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5030 // register pressure.
5031 Register table_base = temps.AcquireX();
5032 // Load jump offset from the table.
5033 __ Adr(table_base, jump_table->GetTableStartLabel());
5034 Register jump_offset = temp_w;
5035 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5036
5037 // Jump to target block by branching to table_base(pc related) + offset.
5038 Register target_address = table_base;
5039 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5040 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005041 }
5042}
5043
Roland Levillain44015862016-01-22 11:47:17 +00005044void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5045 Location out,
5046 uint32_t offset,
5047 Location maybe_temp) {
5048 Primitive::Type type = Primitive::kPrimNot;
5049 Register out_reg = RegisterFrom(out, type);
5050 if (kEmitCompilerReadBarrier) {
5051 Register temp_reg = RegisterFrom(maybe_temp, type);
5052 if (kUseBakerReadBarrier) {
5053 // Load with fast path based Baker's read barrier.
5054 // /* HeapReference<Object> */ out = *(out + offset)
5055 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5056 out,
5057 out_reg,
5058 offset,
5059 temp_reg,
5060 /* needs_null_check */ false,
5061 /* use_load_acquire */ false);
5062 } else {
5063 // Load with slow path based read barrier.
5064 // Save the value of `out` into `maybe_temp` before overwriting it
5065 // in the following move operation, as we will need it for the
5066 // read barrier below.
5067 __ Mov(temp_reg, out_reg);
5068 // /* HeapReference<Object> */ out = *(out + offset)
5069 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5070 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5071 }
5072 } else {
5073 // Plain load with no read barrier.
5074 // /* HeapReference<Object> */ out = *(out + offset)
5075 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5076 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5077 }
5078}
5079
5080void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5081 Location out,
5082 Location obj,
5083 uint32_t offset,
5084 Location maybe_temp) {
5085 Primitive::Type type = Primitive::kPrimNot;
5086 Register out_reg = RegisterFrom(out, type);
5087 Register obj_reg = RegisterFrom(obj, type);
5088 if (kEmitCompilerReadBarrier) {
5089 if (kUseBakerReadBarrier) {
5090 // Load with fast path based Baker's read barrier.
5091 Register temp_reg = RegisterFrom(maybe_temp, type);
5092 // /* HeapReference<Object> */ out = *(obj + offset)
5093 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5094 out,
5095 obj_reg,
5096 offset,
5097 temp_reg,
5098 /* needs_null_check */ false,
5099 /* use_load_acquire */ false);
5100 } else {
5101 // Load with slow path based read barrier.
5102 // /* HeapReference<Object> */ out = *(obj + offset)
5103 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5104 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5105 }
5106 } else {
5107 // Plain load with no read barrier.
5108 // /* HeapReference<Object> */ out = *(obj + offset)
5109 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5110 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5111 }
5112}
5113
5114void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5115 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005116 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005117 uint32_t offset,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005118 vixl::aarch64::Label* fixup_label,
5119 bool requires_read_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005120 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005121 if (requires_read_barrier) {
5122 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005123 if (kUseBakerReadBarrier) {
5124 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5125 // Baker's read barrier are used:
5126 //
5127 // root = obj.field;
5128 // if (Thread::Current()->GetIsGcMarking()) {
5129 // root = ReadBarrier::Mark(root)
5130 // }
5131
5132 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005133 if (fixup_label == nullptr) {
5134 __ Ldr(root_reg, MemOperand(obj, offset));
5135 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005136 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005137 __ Bind(fixup_label);
5138 __ ldr(root_reg, MemOperand(obj, offset));
5139 }
Roland Levillain44015862016-01-22 11:47:17 +00005140 static_assert(
5141 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5142 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5143 "have different sizes.");
5144 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5145 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5146 "have different sizes.");
5147
Vladimir Marko953437b2016-08-24 08:30:46 +00005148 // Slow path marking the GC root `root`.
Roland Levillain44015862016-01-22 11:47:17 +00005149 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005150 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005151 codegen_->AddSlowPath(slow_path);
5152
5153 MacroAssembler* masm = GetVIXLAssembler();
5154 UseScratchRegisterScope temps(masm);
5155 Register temp = temps.AcquireW();
5156 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07005157 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00005158 __ Cbnz(temp, slow_path->GetEntryLabel());
5159 __ Bind(slow_path->GetExitLabel());
5160 } else {
5161 // GC root loaded through a slow path for read barriers other
5162 // than Baker's.
5163 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005164 if (fixup_label == nullptr) {
5165 __ Add(root_reg.X(), obj.X(), offset);
5166 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005167 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005168 __ Bind(fixup_label);
5169 __ add(root_reg.X(), obj.X(), offset);
5170 }
Roland Levillain44015862016-01-22 11:47:17 +00005171 // /* mirror::Object* */ root = root->Read()
5172 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5173 }
5174 } else {
5175 // Plain GC root load with no read barrier.
5176 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005177 if (fixup_label == nullptr) {
5178 __ Ldr(root_reg, MemOperand(obj, offset));
5179 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005180 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005181 __ Bind(fixup_label);
5182 __ ldr(root_reg, MemOperand(obj, offset));
5183 }
Roland Levillain44015862016-01-22 11:47:17 +00005184 // Note that GC roots are not affected by heap poisoning, thus we
5185 // do not have to unpoison `root_reg` here.
5186 }
5187}
5188
5189void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5190 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005191 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005192 uint32_t offset,
5193 Register temp,
5194 bool needs_null_check,
5195 bool use_load_acquire) {
5196 DCHECK(kEmitCompilerReadBarrier);
5197 DCHECK(kUseBakerReadBarrier);
5198
5199 // /* HeapReference<Object> */ ref = *(obj + offset)
5200 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005201 size_t no_scale_factor = 0U;
5202 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5203 ref,
5204 obj,
5205 offset,
5206 no_index,
5207 no_scale_factor,
5208 temp,
5209 needs_null_check,
5210 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005211}
5212
5213void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5214 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005215 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005216 uint32_t data_offset,
5217 Location index,
5218 Register temp,
5219 bool needs_null_check) {
5220 DCHECK(kEmitCompilerReadBarrier);
5221 DCHECK(kUseBakerReadBarrier);
5222
5223 // Array cells are never volatile variables, therefore array loads
5224 // never use Load-Acquire instructions on ARM64.
5225 const bool use_load_acquire = false;
5226
Roland Levillainbfea3352016-06-23 13:48:47 +01005227 static_assert(
5228 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5229 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005230 // /* HeapReference<Object> */ ref =
5231 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005232 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5233 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5234 ref,
5235 obj,
5236 data_offset,
5237 index,
5238 scale_factor,
5239 temp,
5240 needs_null_check,
5241 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005242}
5243
5244void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5245 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005246 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005247 uint32_t offset,
5248 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005249 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005250 Register temp,
5251 bool needs_null_check,
5252 bool use_load_acquire) {
5253 DCHECK(kEmitCompilerReadBarrier);
5254 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005255 // If we are emitting an array load, we should not be using a
5256 // Load Acquire instruction. In other words:
5257 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5258 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005259
5260 MacroAssembler* masm = GetVIXLAssembler();
5261 UseScratchRegisterScope temps(masm);
5262
5263 // In slow path based read barriers, the read barrier call is
5264 // inserted after the original load. However, in fast path based
5265 // Baker's read barriers, we need to perform the load of
5266 // mirror::Object::monitor_ *before* the original reference load.
5267 // This load-load ordering is required by the read barrier.
5268 // The fast path/slow path (for Baker's algorithm) should look like:
5269 //
5270 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5271 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5272 // HeapReference<Object> ref = *src; // Original reference load.
5273 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5274 // if (is_gray) {
5275 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5276 // }
5277 //
5278 // Note: the original implementation in ReadBarrier::Barrier is
5279 // slightly more complex as it performs additional checks that we do
5280 // not do here for performance reasons.
5281
5282 Primitive::Type type = Primitive::kPrimNot;
5283 Register ref_reg = RegisterFrom(ref, type);
5284 DCHECK(obj.IsW());
5285 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5286
5287 // /* int32_t */ monitor = obj->monitor_
5288 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5289 if (needs_null_check) {
5290 MaybeRecordImplicitNullCheck(instruction);
5291 }
5292 // /* LockWord */ lock_word = LockWord(monitor)
5293 static_assert(sizeof(LockWord) == sizeof(int32_t),
5294 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005295
Vladimir Marko877a0332016-07-11 19:30:56 +01005296 // Introduce a dependency on the lock_word including rb_state,
5297 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005298 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01005299 // `obj` is unchanged by this operation, but its value now depends
5300 // on `temp`.
Vladimir Marko877a0332016-07-11 19:30:56 +01005301 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005302
5303 // The actual reference load.
5304 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005305 // Load types involving an "index".
5306 if (use_load_acquire) {
5307 // UnsafeGetObjectVolatile intrinsic case.
5308 // Register `index` is not an index in an object array, but an
5309 // offset to an object reference field within object `obj`.
5310 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5311 DCHECK(instruction->GetLocations()->Intrinsified());
5312 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5313 << instruction->AsInvoke()->GetIntrinsic();
5314 DCHECK_EQ(offset, 0U);
5315 DCHECK_EQ(scale_factor, 0U);
5316 DCHECK_EQ(needs_null_check, 0U);
5317 // /* HeapReference<Object> */ ref = *(obj + index)
5318 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5319 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005320 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005321 // ArrayGet and UnsafeGetObject intrinsics cases.
5322 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5323 if (index.IsConstant()) {
5324 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5325 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5326 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005327 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005328 __ Add(temp2, obj, offset);
5329 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5330 temps.Release(temp2);
5331 }
Roland Levillain44015862016-01-22 11:47:17 +00005332 }
Roland Levillain44015862016-01-22 11:47:17 +00005333 } else {
5334 // /* HeapReference<Object> */ ref = *(obj + offset)
5335 MemOperand field = HeapOperand(obj, offset);
5336 if (use_load_acquire) {
5337 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5338 } else {
5339 Load(type, ref_reg, field);
5340 }
5341 }
5342
5343 // Object* ref = ref_addr->AsMirrorPtr()
5344 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5345
Vladimir Marko953437b2016-08-24 08:30:46 +00005346 // Slow path marking the object `ref` when it is gray.
Roland Levillain44015862016-01-22 11:47:17 +00005347 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005348 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005349 AddSlowPath(slow_path);
5350
5351 // if (rb_state == ReadBarrier::gray_ptr_)
5352 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005353 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5354 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5355 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5356 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5357 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005358 __ Bind(slow_path->GetExitLabel());
5359}
5360
5361void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5362 Location out,
5363 Location ref,
5364 Location obj,
5365 uint32_t offset,
5366 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005367 DCHECK(kEmitCompilerReadBarrier);
5368
Roland Levillain44015862016-01-22 11:47:17 +00005369 // Insert a slow path based read barrier *after* the reference load.
5370 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005371 // If heap poisoning is enabled, the unpoisoning of the loaded
5372 // reference will be carried out by the runtime within the slow
5373 // path.
5374 //
5375 // Note that `ref` currently does not get unpoisoned (when heap
5376 // poisoning is enabled), which is alright as the `ref` argument is
5377 // not used by the artReadBarrierSlow entry point.
5378 //
5379 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5380 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5381 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5382 AddSlowPath(slow_path);
5383
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005384 __ B(slow_path->GetEntryLabel());
5385 __ Bind(slow_path->GetExitLabel());
5386}
5387
Roland Levillain44015862016-01-22 11:47:17 +00005388void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5389 Location out,
5390 Location ref,
5391 Location obj,
5392 uint32_t offset,
5393 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005394 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005395 // Baker's read barriers shall be handled by the fast path
5396 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5397 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005398 // If heap poisoning is enabled, unpoisoning will be taken care of
5399 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005400 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005401 } else if (kPoisonHeapReferences) {
5402 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5403 }
5404}
5405
Roland Levillain44015862016-01-22 11:47:17 +00005406void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5407 Location out,
5408 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005409 DCHECK(kEmitCompilerReadBarrier);
5410
Roland Levillain44015862016-01-22 11:47:17 +00005411 // Insert a slow path based read barrier *after* the GC root load.
5412 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005413 // Note that GC roots are not affected by heap poisoning, so we do
5414 // not need to do anything special for this here.
5415 SlowPathCodeARM64* slow_path =
5416 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5417 AddSlowPath(slow_path);
5418
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005419 __ B(slow_path->GetEntryLabel());
5420 __ Bind(slow_path->GetExitLabel());
5421}
5422
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005423void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5424 LocationSummary* locations =
5425 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5426 locations->SetInAt(0, Location::RequiresRegister());
5427 locations->SetOut(Location::RequiresRegister());
5428}
5429
5430void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5431 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005432 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005433 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005434 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005435 __ Ldr(XRegisterFrom(locations->Out()),
5436 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005437 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005438 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005439 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005440 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5441 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005442 __ Ldr(XRegisterFrom(locations->Out()),
5443 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005444 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005445}
5446
5447
5448
Alexandre Rames67555f72014-11-18 10:55:16 +00005449#undef __
5450#undef QUICK_ENTRY_POINT
5451
Alexandre Rames5319def2014-10-23 10:03:10 +01005452} // namespace arm64
5453} // namespace art