blob: b39a0e43fa2b7e7c5a66da0b4446658e91713295 [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)
Artem Serov914d7a82017-02-07 14:33:49 +000037using vixl::ExactAssemblyScope;
38using vixl::CodeBufferCheckScope;
39using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010040
41#ifdef __
42#error "ARM64 Codegen VIXL macro-assembler macro already defined."
43#endif
44
Alexandre Rames5319def2014-10-23 10:03:10 +010045namespace art {
46
Roland Levillain22ccc3a2015-11-24 13:10:05 +000047template<class MirrorType>
48class GcRoot;
49
Alexandre Rames5319def2014-10-23 10:03:10 +010050namespace arm64 {
51
Alexandre Ramesbe919d92016-08-23 18:33:36 +010052using helpers::ARM64EncodableConstantOrRegister;
53using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080054using helpers::CPURegisterFrom;
55using helpers::DRegisterFrom;
56using helpers::FPRegisterFrom;
57using helpers::HeapOperand;
58using helpers::HeapOperandFrom;
59using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080062using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010063using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080064using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010065using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080066using helpers::LocationFrom;
67using helpers::OperandFromMemOperand;
68using helpers::OutputCPURegister;
69using helpers::OutputFPRegister;
70using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010071using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072using helpers::RegisterFrom;
73using helpers::StackOperandFrom;
74using helpers::VIXLRegCodeFromART;
75using helpers::WRegisterFrom;
76using helpers::XRegisterFrom;
77
Alexandre Rames5319def2014-10-23 10:03:10 +010078static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000079// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080080// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
81// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000082static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010083
Alexandre Rames5319def2014-10-23 10:03:10 +010084inline Condition ARM64Condition(IfCondition cond) {
85 switch (cond) {
86 case kCondEQ: return eq;
87 case kCondNE: return ne;
88 case kCondLT: return lt;
89 case kCondLE: return le;
90 case kCondGT: return gt;
91 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070092 case kCondB: return lo;
93 case kCondBE: return ls;
94 case kCondA: return hi;
95 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010096 }
Roland Levillain7f63c522015-07-13 15:54:55 +000097 LOG(FATAL) << "Unreachable";
98 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010099}
100
Vladimir Markod6e069b2016-01-18 11:11:01 +0000101inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
102 // The ARM64 condition codes can express all the necessary branches, see the
103 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
104 // There is no dex instruction or HIR that would need the missing conditions
105 // "equal or unordered" or "not equal".
106 switch (cond) {
107 case kCondEQ: return eq;
108 case kCondNE: return ne /* unordered */;
109 case kCondLT: return gt_bias ? cc : lt /* unordered */;
110 case kCondLE: return gt_bias ? ls : le /* unordered */;
111 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
112 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
113 default:
114 LOG(FATAL) << "UNREACHABLE";
115 UNREACHABLE();
116 }
117}
118
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000119Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000120 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
121 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
122 // but we use the exact registers for clarity.
123 if (return_type == Primitive::kPrimFloat) {
124 return LocationFrom(s0);
125 } else if (return_type == Primitive::kPrimDouble) {
126 return LocationFrom(d0);
127 } else if (return_type == Primitive::kPrimLong) {
128 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100129 } else if (return_type == Primitive::kPrimVoid) {
130 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000131 } else {
132 return LocationFrom(w0);
133 }
134}
135
Alexandre Rames5319def2014-10-23 10:03:10 +0100136Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000137 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100138}
139
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100140// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
141#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700142#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100143
Zheng Xuda403092015-04-24 17:35:39 +0800144// Calculate memory accessing operand for save/restore live registers.
145static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100146 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800147 int64_t spill_offset,
148 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100149 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
150 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
151 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800152 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100153 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800154 codegen->GetNumberOfFloatingPointRegisters()));
155
Vladimir Marko804b03f2016-09-14 16:26:36 +0100156 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100157 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
158 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800159
160 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
161 UseScratchRegisterScope temps(masm);
162
163 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100164 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
165 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800166 int64_t reg_size = kXRegSizeInBytes;
167 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
168 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100169 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800170 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
171 // If the offset does not fit in the instruction's immediate field, use an alternate register
172 // to compute the base address(float point registers spill base address).
173 Register new_base = temps.AcquireSameSizeAs(base);
174 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
175 base = new_base;
176 spill_offset = -core_spill_size;
177 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
178 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
179 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
180 }
181
182 if (is_save) {
183 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
184 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
185 } else {
186 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
187 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
188 }
189}
190
191void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800192 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100193 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
194 for (uint32_t i : LowToHighBits(core_spills)) {
195 // If the register holds an object, update the stack mask.
196 if (locations->RegisterContainsObject(i)) {
197 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800198 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100199 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
200 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
201 saved_core_stack_offsets_[i] = stack_offset;
202 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800203 }
204
Vladimir Marko804b03f2016-09-14 16:26:36 +0100205 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
206 for (uint32_t i : LowToHighBits(fp_spills)) {
207 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
208 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
209 saved_fpu_stack_offsets_[i] = stack_offset;
210 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800211 }
212
Vladimir Marko804b03f2016-09-14 16:26:36 +0100213 SaveRestoreLiveRegistersHelper(codegen,
214 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800215 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
216}
217
218void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100219 SaveRestoreLiveRegistersHelper(codegen,
220 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800221 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
222}
223
Alexandre Rames5319def2014-10-23 10:03:10 +0100224class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
225 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000226 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100227
Alexandre Rames67555f72014-11-18 10:55:16 +0000228 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100229 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000230 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100231
Alexandre Rames5319def2014-10-23 10:03:10 +0100232 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000233 if (instruction_->CanThrowIntoCatchBlock()) {
234 // Live registers will be restored in the catch block if caught.
235 SaveLiveRegisters(codegen, instruction_->GetLocations());
236 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000237 // We're moving two locations to locations that could overlap, so we need a parallel
238 // move resolver.
239 InvokeRuntimeCallingConvention calling_convention;
240 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100241 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
242 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000243 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
244 ? kQuickThrowStringBounds
245 : kQuickThrowArrayBounds;
246 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100247 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800248 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100249 }
250
Alexandre Rames8158f282015-08-07 10:26:17 +0100251 bool IsFatal() const OVERRIDE { return true; }
252
Alexandre Rames9931f312015-06-19 14:47:01 +0100253 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
254
Alexandre Rames5319def2014-10-23 10:03:10 +0100255 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100256 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
257};
258
Alexandre Rames67555f72014-11-18 10:55:16 +0000259class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
260 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000261 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000262
263 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
264 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
265 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000266 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800267 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000268 }
269
Alexandre Rames8158f282015-08-07 10:26:17 +0100270 bool IsFatal() const OVERRIDE { return true; }
271
Alexandre Rames9931f312015-06-19 14:47:01 +0100272 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
273
Alexandre Rames67555f72014-11-18 10:55:16 +0000274 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000275 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
276};
277
278class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
279 public:
280 LoadClassSlowPathARM64(HLoadClass* cls,
281 HInstruction* at,
282 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000283 bool do_clinit,
284 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
285 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
286 : SlowPathCodeARM64(at),
287 cls_(cls),
288 dex_pc_(dex_pc),
289 do_clinit_(do_clinit),
290 bss_entry_temp_(bss_entry_temp),
291 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
293 }
294
295 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000296 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000297 Location out = locations->Out();
298 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000299 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
300
Vladimir Markoea4c1262017-02-06 19:59:33 +0000301 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the page address of
302 // the entry which is in a scratch register. Make sure it's not used for saving/restoring
303 // registers. Exclude the scratch register also for non-Baker read barrier for simplicity.
304 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
305 bool is_load_class_bss_entry =
306 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
307 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
308 if (is_load_class_bss_entry) {
309 // This temp is a scratch register.
310 DCHECK(bss_entry_temp_.IsValid());
311 temps.Exclude(bss_entry_temp_);
312 }
313
Alexandre Rames67555f72014-11-18 10:55:16 +0000314 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000315 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000316
317 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000318 dex::TypeIndex type_index = cls_->GetTypeIndex();
319 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000320 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
321 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000322 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800323 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100324 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800325 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100326 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800327 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000328
329 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000330 if (out.IsValid()) {
331 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000332 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000333 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000335 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000336 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000337 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000338 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000339 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000340 if (call_saves_everything_except_r0_ip0) {
341 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
342 } else {
343 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
344 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
345 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
346 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000347 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000348 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000349 {
350 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
351 __ Bind(strp_label);
352 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000353 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000354 }
355 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 __ B(GetExitLabel());
357 }
358
Alexandre Rames9931f312015-06-19 14:47:01 +0100359 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
360
Alexandre Rames67555f72014-11-18 10:55:16 +0000361 private:
362 // The class this slow path will load.
363 HLoadClass* const cls_;
364
Alexandre Rames67555f72014-11-18 10:55:16 +0000365 // The dex PC of `at_`.
366 const uint32_t dex_pc_;
367
368 // Whether to initialize the class.
369 const bool do_clinit_;
370
Vladimir Markoea4c1262017-02-06 19:59:33 +0000371 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
372 vixl::aarch64::Register bss_entry_temp_;
373 vixl::aarch64::Label* bss_entry_adrp_label_;
374
Alexandre Rames67555f72014-11-18 10:55:16 +0000375 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
376};
377
Vladimir Markoaad75c62016-10-03 08:46:48 +0000378class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
379 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100380 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
381 : SlowPathCodeARM64(instruction),
382 temp_(temp),
383 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000384
385 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
386 LocationSummary* locations = instruction_->GetLocations();
387 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
388 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
389
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100390 // temp_ is a scratch register. Make sure it's not used for saving/restoring registers.
391 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
392 temps.Exclude(temp_);
393
Vladimir Markoaad75c62016-10-03 08:46:48 +0000394 __ Bind(GetEntryLabel());
395 SaveLiveRegisters(codegen, locations);
396
397 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000398 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
399 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000400 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
401 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
402 Primitive::Type type = instruction_->GetType();
403 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
404
405 RestoreLiveRegisters(codegen, locations);
406
407 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000408 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100409 if (!kUseReadBarrier || kUseBakerReadBarrier) {
410 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
411 } else {
412 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
413 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
414 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
415 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000416 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100417 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000418 {
419 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
420 __ Bind(strp_label);
421 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100422 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000423 }
424
425 __ B(GetExitLabel());
426 }
427
428 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
429
430 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100431 const Register temp_;
432 vixl::aarch64::Label* adrp_label_;
433
Vladimir Markoaad75c62016-10-03 08:46:48 +0000434 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
435};
436
Alexandre Rames5319def2014-10-23 10:03:10 +0100437class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
438 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000439 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100440
Alexandre Rames67555f72014-11-18 10:55:16 +0000441 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
442 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100443 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000444 if (instruction_->CanThrowIntoCatchBlock()) {
445 // Live registers will be restored in the catch block if caught.
446 SaveLiveRegisters(codegen, instruction_->GetLocations());
447 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000448 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
449 instruction_,
450 instruction_->GetDexPc(),
451 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800452 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100453 }
454
Alexandre Rames8158f282015-08-07 10:26:17 +0100455 bool IsFatal() const OVERRIDE { return true; }
456
Alexandre Rames9931f312015-06-19 14:47:01 +0100457 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
458
Alexandre Rames5319def2014-10-23 10:03:10 +0100459 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100460 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
461};
462
463class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
464 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100465 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000466 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100467
Alexandre Rames67555f72014-11-18 10:55:16 +0000468 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100469 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000470 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100471 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100472 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000473 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800474 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100475 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000476 if (successor_ == nullptr) {
477 __ B(GetReturnLabel());
478 } else {
479 __ B(arm64_codegen->GetLabelOf(successor_));
480 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100481 }
482
Scott Wakeling97c72b72016-06-24 16:19:36 +0100483 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100484 DCHECK(successor_ == nullptr);
485 return &return_label_;
486 }
487
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100488 HBasicBlock* GetSuccessor() const {
489 return successor_;
490 }
491
Alexandre Rames9931f312015-06-19 14:47:01 +0100492 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
493
Alexandre Rames5319def2014-10-23 10:03:10 +0100494 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100495 // If not null, the block to branch to after the suspend check.
496 HBasicBlock* const successor_;
497
498 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100499 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100500
501 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
502};
503
Alexandre Rames67555f72014-11-18 10:55:16 +0000504class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
505 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000506 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000507 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000508
509 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000510 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800511
Alexandre Rames3e69f162014-12-10 10:36:50 +0000512 DCHECK(instruction_->IsCheckCast()
513 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
514 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100515 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000516
Alexandre Rames67555f72014-11-18 10:55:16 +0000517 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000518
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000519 if (!is_fatal_) {
520 SaveLiveRegisters(codegen, locations);
521 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000522
523 // We're moving two locations to locations that could overlap, so we need a parallel
524 // move resolver.
525 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800526 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800527 LocationFrom(calling_convention.GetRegisterAt(0)),
528 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800529 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800530 LocationFrom(calling_convention.GetRegisterAt(1)),
531 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000532 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000533 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800534 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000535 Primitive::Type ret_type = instruction_->GetType();
536 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
537 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
538 } else {
539 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800540 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
541 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000542 }
543
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000544 if (!is_fatal_) {
545 RestoreLiveRegisters(codegen, locations);
546 __ B(GetExitLabel());
547 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000548 }
549
Alexandre Rames9931f312015-06-19 14:47:01 +0100550 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100551 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100552
Alexandre Rames67555f72014-11-18 10:55:16 +0000553 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000554 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000555
Alexandre Rames67555f72014-11-18 10:55:16 +0000556 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
557};
558
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700559class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
560 public:
Aart Bik42249c32016-01-07 15:33:50 -0800561 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000562 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700563
564 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800565 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700566 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000567 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000568 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700569 }
570
Alexandre Rames9931f312015-06-19 14:47:01 +0100571 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
572
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700573 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700574 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
575};
576
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100577class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
578 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000579 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100580
581 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
582 LocationSummary* locations = instruction_->GetLocations();
583 __ Bind(GetEntryLabel());
584 SaveLiveRegisters(codegen, locations);
585
586 InvokeRuntimeCallingConvention calling_convention;
587 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
588 parallel_move.AddMove(
589 locations->InAt(0),
590 LocationFrom(calling_convention.GetRegisterAt(0)),
591 Primitive::kPrimNot,
592 nullptr);
593 parallel_move.AddMove(
594 locations->InAt(1),
595 LocationFrom(calling_convention.GetRegisterAt(1)),
596 Primitive::kPrimInt,
597 nullptr);
598 parallel_move.AddMove(
599 locations->InAt(2),
600 LocationFrom(calling_convention.GetRegisterAt(2)),
601 Primitive::kPrimNot,
602 nullptr);
603 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
604
605 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000606 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100607 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
608 RestoreLiveRegisters(codegen, locations);
609 __ B(GetExitLabel());
610 }
611
612 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
613
614 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100615 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
616};
617
Zheng Xu3927c8b2015-11-18 17:46:25 +0800618void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
619 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000620 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800621
622 // We are about to use the assembler to place literals directly. Make sure we have enough
623 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000624 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
625 num_entries * sizeof(int32_t),
626 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800627
628 __ Bind(&table_start_);
629 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
630 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100631 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800632 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100633 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800634 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
635 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
636 Literal<int32_t> literal(jump_offset);
637 __ place(&literal);
638 }
639}
640
Roland Levillain54f869e2017-03-06 13:54:11 +0000641// Abstract base class for read barrier slow paths marking a reference
642// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000643//
Roland Levillain54f869e2017-03-06 13:54:11 +0000644// Argument `entrypoint` must be a register location holding the read
645// barrier marking runtime entry point to be invoked.
646class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
647 protected:
648 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
649 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000650 DCHECK(kEmitCompilerReadBarrier);
651 }
652
Roland Levillain54f869e2017-03-06 13:54:11 +0000653 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000654
Roland Levillain54f869e2017-03-06 13:54:11 +0000655 // Generate assembly code calling the read barrier marking runtime
656 // entry point (ReadBarrierMarkRegX).
657 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000658 // No need to save live registers; it's taken care of by the
659 // entrypoint. Also, there is no need to update the stack mask,
660 // as this runtime call will not trigger a garbage collection.
661 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
662 DCHECK_NE(ref_.reg(), LR);
663 DCHECK_NE(ref_.reg(), WSP);
664 DCHECK_NE(ref_.reg(), WZR);
665 // IP0 is used internally by the ReadBarrierMarkRegX entry point
666 // as a temporary, it cannot be the entry point's input/output.
667 DCHECK_NE(ref_.reg(), IP0);
668 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
669 // "Compact" slow path, saving two moves.
670 //
671 // Instead of using the standard runtime calling convention (input
672 // and output in W0):
673 //
674 // W0 <- ref
675 // W0 <- ReadBarrierMark(W0)
676 // ref <- W0
677 //
678 // we just use rX (the register containing `ref`) as input and output
679 // of a dedicated entrypoint:
680 //
681 // rX <- ReadBarrierMarkRegX(rX)
682 //
683 if (entrypoint_.IsValid()) {
684 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
685 __ Blr(XRegisterFrom(entrypoint_));
686 } else {
687 // Entrypoint is not already loaded, load from the thread.
688 int32_t entry_point_offset =
689 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
690 // This runtime call does not require a stack map.
691 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
692 }
693 }
694
695 // The location (register) of the marked object reference.
696 const Location ref_;
697
698 // The location of the entrypoint if it is already loaded.
699 const Location entrypoint_;
700
Roland Levillain54f869e2017-03-06 13:54:11 +0000701 private:
702 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
703};
704
Alexandre Rames5319def2014-10-23 10:03:10 +0100705// Slow path marking an object reference `ref` during a read
706// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000707// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100708//
709// This means that after the execution of this slow path, `ref` will
710// always be up-to-date, but `obj.field` may not; i.e., after the
711// flip, `ref` will be a to-space reference, but `obj.field` will
712// probably still be a from-space reference (unless it gets updated by
713// another thread, or if another thread installed another object
714// reference (different from `ref`) in `obj.field`).
715//
716// If `entrypoint` is a valid location it is assumed to already be
717// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000718// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000719class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100720 public:
721 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
722 Location ref,
723 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000724 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100725 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100726 }
727
728 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
729
730 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000731 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100732 DCHECK(locations->CanCall());
733 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000734 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000735 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
736 << "Unexpected instruction in read barrier marking slow path: "
737 << instruction_->DebugName();
738
739 __ Bind(GetEntryLabel());
740 GenerateReadBarrierMarkRuntimeCall(codegen);
741 __ B(GetExitLabel());
742 }
743
744 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000745 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
746};
747
Roland Levillain54f869e2017-03-06 13:54:11 +0000748// Slow path loading `obj`'s lock word, loading a reference from
749// object `*(obj + offset + (index << scale_factor))` into `ref`, and
750// marking `ref` if `obj` is gray according to the lock word (Baker
751// read barrier). The field `obj.field` in the object `obj` holding
752// this reference does not get updated by this slow path after marking
753// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
754// below for that).
755//
756// This means that after the execution of this slow path, `ref` will
757// always be up-to-date, but `obj.field` may not; i.e., after the
758// flip, `ref` will be a to-space reference, but `obj.field` will
759// probably still be a from-space reference (unless it gets updated by
760// another thread, or if another thread installed another object
761// reference (different from `ref`) in `obj.field`).
762//
763// Argument `entrypoint` must be a register location holding the read
764// barrier marking runtime entry point to be invoked.
765class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
766 public:
767 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
768 Location ref,
769 Register obj,
770 uint32_t offset,
771 Location index,
772 size_t scale_factor,
773 bool needs_null_check,
774 bool use_load_acquire,
775 Register temp,
776 Location entrypoint)
777 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
778 obj_(obj),
779 offset_(offset),
780 index_(index),
781 scale_factor_(scale_factor),
782 needs_null_check_(needs_null_check),
783 use_load_acquire_(use_load_acquire),
784 temp_(temp) {
785 DCHECK(kEmitCompilerReadBarrier);
786 DCHECK(kUseBakerReadBarrier);
787 }
788
789 const char* GetDescription() const OVERRIDE {
790 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
791 }
792
793 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
794 LocationSummary* locations = instruction_->GetLocations();
795 DCHECK(locations->CanCall());
796 DCHECK(ref_.IsRegister()) << ref_;
797 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
798 DCHECK(obj_.IsW());
799 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100800 DCHECK(instruction_->IsInstanceFieldGet() ||
801 instruction_->IsStaticFieldGet() ||
802 instruction_->IsArrayGet() ||
803 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100804 instruction_->IsInstanceOf() ||
805 instruction_->IsCheckCast() ||
806 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
807 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
808 << "Unexpected instruction in read barrier marking slow path: "
809 << instruction_->DebugName();
810 // The read barrier instrumentation of object ArrayGet
811 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000812 // instruction.
813 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000814 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
815
Roland Levillain54f869e2017-03-06 13:54:11 +0000816 // Temporary register `temp_`, used to store the lock word, must
817 // not be IP0 nor IP1, as we may use them to emit the reference
818 // load (in the call to GenerateRawReferenceLoad below), and we
819 // need the lock word to still be in `temp_` after the reference
820 // load.
821 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
822 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
823
Alexandre Rames5319def2014-10-23 10:03:10 +0100824 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000825
826 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
827 // inserted after the original load. However, in fast path based
828 // Baker's read barriers, we need to perform the load of
829 // mirror::Object::monitor_ *before* the original reference load.
830 // This load-load ordering is required by the read barrier.
831 // The fast path/slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100832 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000833 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
834 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
835 // HeapReference<mirror::Object> ref = *src; // Original reference load.
836 // bool is_gray = (rb_state == ReadBarrier::GrayState());
837 // if (is_gray) {
838 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
839 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000840 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000841 // Note: the original implementation in ReadBarrier::Barrier is
842 // slightly more complex as it performs additional checks that we do
843 // not do here for performance reasons.
844
845 // /* int32_t */ monitor = obj->monitor_
846 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
847 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
848 if (needs_null_check_) {
849 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100850 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000851 // /* LockWord */ lock_word = LockWord(monitor)
852 static_assert(sizeof(LockWord) == sizeof(int32_t),
853 "art::LockWord and int32_t have different sizes.");
854
855 // Introduce a dependency on the lock_word including rb_state,
856 // to prevent load-load reordering, and without using
857 // a memory barrier (which would be more expensive).
858 // `obj` is unchanged by this operation, but its value now depends
859 // on `temp`.
860 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
861
862 // The actual reference load.
863 // A possible implicit null check has already been handled above.
864 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
865 arm64_codegen->GenerateRawReferenceLoad(instruction_,
866 ref_,
867 obj_,
868 offset_,
869 index_,
870 scale_factor_,
871 /* needs_null_check */ false,
872 use_load_acquire_);
873
874 // Mark the object `ref` when `obj` is gray.
875 //
876 // if (rb_state == ReadBarrier::GrayState())
877 // ref = ReadBarrier::Mark(ref);
878 //
879 // Given the numeric representation, it's enough to check the low bit of the rb_state.
880 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
881 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
882 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
883 GenerateReadBarrierMarkRuntimeCall(codegen);
884
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000885 __ B(GetExitLabel());
886 }
887
888 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000889 // The register containing the object holding the marked object reference field.
890 Register obj_;
891 // The offset, index and scale factor to access the reference in `obj_`.
892 uint32_t offset_;
893 Location index_;
894 size_t scale_factor_;
895 // Is a null check required?
896 bool needs_null_check_;
897 // Should this reference load use Load-Acquire semantics?
898 bool use_load_acquire_;
899 // A temporary register used to hold the lock word of `obj_`.
900 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000901
Roland Levillain54f869e2017-03-06 13:54:11 +0000902 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000903};
904
Roland Levillain54f869e2017-03-06 13:54:11 +0000905// Slow path loading `obj`'s lock word, loading a reference from
906// object `*(obj + offset + (index << scale_factor))` into `ref`, and
907// marking `ref` if `obj` is gray according to the lock word (Baker
908// read barrier). If needed, this slow path also atomically updates
909// the field `obj.field` in the object `obj` holding this reference
910// after marking (contrary to
911// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
912// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100913//
914// This means that after the execution of this slow path, both `ref`
915// and `obj.field` will be up-to-date; i.e., after the flip, both will
916// hold the same to-space reference (unless another thread installed
917// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000918//
Roland Levillain54f869e2017-03-06 13:54:11 +0000919// Argument `entrypoint` must be a register location holding the read
920// barrier marking runtime entry point to be invoked.
921class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
922 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100923 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000924 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(HInstruction* instruction,
925 Location ref,
926 Register obj,
927 uint32_t offset,
928 Location index,
929 size_t scale_factor,
930 bool needs_null_check,
931 bool use_load_acquire,
932 Register temp,
933 Location entrypoint)
934 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100935 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000936 offset_(offset),
937 index_(index),
938 scale_factor_(scale_factor),
939 needs_null_check_(needs_null_check),
940 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000941 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100942 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000943 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100944 }
945
946 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000947 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100948 }
949
950 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
951 LocationSummary* locations = instruction_->GetLocations();
952 Register ref_reg = WRegisterFrom(ref_);
953 DCHECK(locations->CanCall());
954 DCHECK(ref_.IsRegister()) << ref_;
955 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000956 DCHECK(obj_.IsW());
957 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
958
959 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100960 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
961 << "Unexpected instruction in read barrier marking and field updating slow path: "
962 << instruction_->DebugName();
963 DCHECK(instruction_->GetLocations()->Intrinsified());
964 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000965 DCHECK_EQ(offset_, 0u);
966 DCHECK_EQ(scale_factor_, 0u);
967 DCHECK_EQ(use_load_acquire_, false);
968 // The location of the offset of the marked reference field within `obj_`.
969 Location field_offset = index_;
970 DCHECK(field_offset.IsRegister()) << field_offset;
971
972 // Temporary register `temp_`, used to store the lock word, must
973 // not be IP0 nor IP1, as we may use them to emit the reference
974 // load (in the call to GenerateRawReferenceLoad below), and we
975 // need the lock word to still be in `temp_` after the reference
976 // load.
977 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
978 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100979
980 __ Bind(GetEntryLabel());
981
Roland Levillain54f869e2017-03-06 13:54:11 +0000982 // /* int32_t */ monitor = obj->monitor_
983 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
984 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
985 if (needs_null_check_) {
986 codegen->MaybeRecordImplicitNullCheck(instruction_);
987 }
988 // /* LockWord */ lock_word = LockWord(monitor)
989 static_assert(sizeof(LockWord) == sizeof(int32_t),
990 "art::LockWord and int32_t have different sizes.");
991
992 // Introduce a dependency on the lock_word including rb_state,
993 // to prevent load-load reordering, and without using
994 // a memory barrier (which would be more expensive).
995 // `obj` is unchanged by this operation, but its value now depends
996 // on `temp`.
997 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
998
999 // The actual reference load.
1000 // A possible implicit null check has already been handled above.
1001 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1002 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1003 ref_,
1004 obj_,
1005 offset_,
1006 index_,
1007 scale_factor_,
1008 /* needs_null_check */ false,
1009 use_load_acquire_);
1010
1011 // Mark the object `ref` when `obj` is gray.
1012 //
1013 // if (rb_state == ReadBarrier::GrayState())
1014 // ref = ReadBarrier::Mark(ref);
1015 //
1016 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1017 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1018 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1019 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1020
1021 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001022 // Note that we cannot use IP to save the old reference, as IP is
1023 // used internally by the ReadBarrierMarkRegX entry point, and we
1024 // need the old reference after the call to that entry point.
1025 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1026 __ Mov(temp_.W(), ref_reg);
1027
Roland Levillain54f869e2017-03-06 13:54:11 +00001028 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001029
1030 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001031 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001032 //
1033 // Note that this field could also hold a different object, if
1034 // another thread had concurrently changed it. In that case, the
1035 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1036 // (CAS) operation below would abort the CAS, leaving the field
1037 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001038 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001039 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001040
1041 // Update the the holder's field atomically. This may fail if
1042 // mutator updates before us, but it's OK. This is achieved
1043 // using a strong compare-and-set (CAS) operation with relaxed
1044 // memory synchronization ordering, where the expected value is
1045 // the old reference and the desired value is the new reference.
1046
1047 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1048 UseScratchRegisterScope temps(masm);
1049
1050 // Convenience aliases.
1051 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001052 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001053 Register expected = temp_.W();
1054 Register value = ref_reg;
1055 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1056 Register tmp_value = temps.AcquireW(); // Value in memory.
1057
1058 __ Add(tmp_ptr, base.X(), Operand(offset));
1059
1060 if (kPoisonHeapReferences) {
1061 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1062 if (value.Is(expected)) {
1063 // Do not poison `value`, as it is the same register as
1064 // `expected`, which has just been poisoned.
1065 } else {
1066 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1067 }
1068 }
1069
1070 // do {
1071 // tmp_value = [tmp_ptr] - expected;
1072 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1073
Roland Levillain24a4d112016-10-26 13:10:46 +01001074 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001075 __ Bind(&loop_head);
1076 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1077 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001078 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001079 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1080 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001081 __ B(&exit_loop);
1082 __ Bind(&comparison_failed);
1083 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001084 __ Bind(&exit_loop);
1085
1086 if (kPoisonHeapReferences) {
1087 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1088 if (value.Is(expected)) {
1089 // Do not unpoison `value`, as it is the same register as
1090 // `expected`, which has just been unpoisoned.
1091 } else {
1092 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1093 }
1094 }
1095
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001096 __ B(GetExitLabel());
1097 }
1098
1099 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001100 // The register containing the object holding the marked object reference field.
1101 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001102 // The offset, index and scale factor to access the reference in `obj_`.
1103 uint32_t offset_;
1104 Location index_;
1105 size_t scale_factor_;
1106 // Is a null check required?
1107 bool needs_null_check_;
1108 // Should this reference load use Load-Acquire semantics?
1109 bool use_load_acquire_;
1110 // A temporary register used to hold the lock word of `obj_`; and
1111 // also to hold the original reference value, when the reference is
1112 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001113 const Register temp_;
1114
Roland Levillain54f869e2017-03-06 13:54:11 +00001115 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001116};
1117
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001118// Slow path generating a read barrier for a heap reference.
1119class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1120 public:
1121 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1122 Location out,
1123 Location ref,
1124 Location obj,
1125 uint32_t offset,
1126 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001127 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001128 out_(out),
1129 ref_(ref),
1130 obj_(obj),
1131 offset_(offset),
1132 index_(index) {
1133 DCHECK(kEmitCompilerReadBarrier);
1134 // If `obj` is equal to `out` or `ref`, it means the initial object
1135 // has been overwritten by (or after) the heap object reference load
1136 // to be instrumented, e.g.:
1137 //
1138 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001139 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001140 //
1141 // In that case, we have lost the information about the original
1142 // object, and the emitted read barrier cannot work properly.
1143 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1144 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1145 }
1146
1147 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1148 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1149 LocationSummary* locations = instruction_->GetLocations();
1150 Primitive::Type type = Primitive::kPrimNot;
1151 DCHECK(locations->CanCall());
1152 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001153 DCHECK(instruction_->IsInstanceFieldGet() ||
1154 instruction_->IsStaticFieldGet() ||
1155 instruction_->IsArrayGet() ||
1156 instruction_->IsInstanceOf() ||
1157 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001158 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001159 << "Unexpected instruction in read barrier for heap reference slow path: "
1160 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001161 // The read barrier instrumentation of object ArrayGet
1162 // instructions does not support the HIntermediateAddress
1163 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001164 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001165 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001166
1167 __ Bind(GetEntryLabel());
1168
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001169 SaveLiveRegisters(codegen, locations);
1170
1171 // We may have to change the index's value, but as `index_` is a
1172 // constant member (like other "inputs" of this slow path),
1173 // introduce a copy of it, `index`.
1174 Location index = index_;
1175 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001176 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001177 if (instruction_->IsArrayGet()) {
1178 // Compute the actual memory offset and store it in `index`.
1179 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1180 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1181 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1182 // We are about to change the value of `index_reg` (see the
1183 // calls to vixl::MacroAssembler::Lsl and
1184 // vixl::MacroAssembler::Mov below), but it has
1185 // not been saved by the previous call to
1186 // art::SlowPathCode::SaveLiveRegisters, as it is a
1187 // callee-save register --
1188 // art::SlowPathCode::SaveLiveRegisters does not consider
1189 // callee-save registers, as it has been designed with the
1190 // assumption that callee-save registers are supposed to be
1191 // handled by the called function. So, as a callee-save
1192 // register, `index_reg` _would_ eventually be saved onto
1193 // the stack, but it would be too late: we would have
1194 // changed its value earlier. Therefore, we manually save
1195 // it here into another freely available register,
1196 // `free_reg`, chosen of course among the caller-save
1197 // registers (as a callee-save `free_reg` register would
1198 // exhibit the same problem).
1199 //
1200 // Note we could have requested a temporary register from
1201 // the register allocator instead; but we prefer not to, as
1202 // this is a slow path, and we know we can find a
1203 // caller-save register that is available.
1204 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1205 __ Mov(free_reg.W(), index_reg);
1206 index_reg = free_reg;
1207 index = LocationFrom(index_reg);
1208 } else {
1209 // The initial register stored in `index_` has already been
1210 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1211 // (as it is not a callee-save register), so we can freely
1212 // use it.
1213 }
1214 // Shifting the index value contained in `index_reg` by the scale
1215 // factor (2) cannot overflow in practice, as the runtime is
1216 // unable to allocate object arrays with a size larger than
1217 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1218 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1219 static_assert(
1220 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1221 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1222 __ Add(index_reg, index_reg, Operand(offset_));
1223 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001224 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1225 // intrinsics, `index_` is not shifted by a scale factor of 2
1226 // (as in the case of ArrayGet), as it is actually an offset
1227 // to an object field within an object.
1228 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001229 DCHECK(instruction_->GetLocations()->Intrinsified());
1230 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1231 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1232 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001233 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001234 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001235 }
1236 }
1237
1238 // We're moving two or three locations to locations that could
1239 // overlap, so we need a parallel move resolver.
1240 InvokeRuntimeCallingConvention calling_convention;
1241 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1242 parallel_move.AddMove(ref_,
1243 LocationFrom(calling_convention.GetRegisterAt(0)),
1244 type,
1245 nullptr);
1246 parallel_move.AddMove(obj_,
1247 LocationFrom(calling_convention.GetRegisterAt(1)),
1248 type,
1249 nullptr);
1250 if (index.IsValid()) {
1251 parallel_move.AddMove(index,
1252 LocationFrom(calling_convention.GetRegisterAt(2)),
1253 Primitive::kPrimInt,
1254 nullptr);
1255 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1256 } else {
1257 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1258 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1259 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001260 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001261 instruction_,
1262 instruction_->GetDexPc(),
1263 this);
1264 CheckEntrypointTypes<
1265 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1266 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1267
1268 RestoreLiveRegisters(codegen, locations);
1269
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001270 __ B(GetExitLabel());
1271 }
1272
1273 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1274
1275 private:
1276 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001277 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1278 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001279 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1280 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1281 return Register(VIXLRegCodeFromART(i), kXRegSize);
1282 }
1283 }
1284 // We shall never fail to find a free caller-save register, as
1285 // there are more than two core caller-save registers on ARM64
1286 // (meaning it is possible to find one which is different from
1287 // `ref` and `obj`).
1288 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1289 LOG(FATAL) << "Could not find a free register";
1290 UNREACHABLE();
1291 }
1292
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001293 const Location out_;
1294 const Location ref_;
1295 const Location obj_;
1296 const uint32_t offset_;
1297 // An additional location containing an index to an array.
1298 // Only used for HArrayGet and the UnsafeGetObject &
1299 // UnsafeGetObjectVolatile intrinsics.
1300 const Location index_;
1301
1302 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1303};
1304
1305// Slow path generating a read barrier for a GC root.
1306class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1307 public:
1308 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001309 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001310 DCHECK(kEmitCompilerReadBarrier);
1311 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001312
1313 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1314 LocationSummary* locations = instruction_->GetLocations();
1315 Primitive::Type type = Primitive::kPrimNot;
1316 DCHECK(locations->CanCall());
1317 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001318 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1319 << "Unexpected instruction in read barrier for GC root slow path: "
1320 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001321
1322 __ Bind(GetEntryLabel());
1323 SaveLiveRegisters(codegen, locations);
1324
1325 InvokeRuntimeCallingConvention calling_convention;
1326 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1327 // The argument of the ReadBarrierForRootSlow is not a managed
1328 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1329 // thus we need a 64-bit move here, and we cannot use
1330 //
1331 // arm64_codegen->MoveLocation(
1332 // LocationFrom(calling_convention.GetRegisterAt(0)),
1333 // root_,
1334 // type);
1335 //
1336 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1337 // reference type (`Primitive::kPrimNot`).
1338 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001339 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001340 instruction_,
1341 instruction_->GetDexPc(),
1342 this);
1343 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1344 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1345
1346 RestoreLiveRegisters(codegen, locations);
1347 __ B(GetExitLabel());
1348 }
1349
1350 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1351
1352 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001353 const Location out_;
1354 const Location root_;
1355
1356 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1357};
1358
Alexandre Rames5319def2014-10-23 10:03:10 +01001359#undef __
1360
1361Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1362 Location next_location;
1363 if (type == Primitive::kPrimVoid) {
1364 LOG(FATAL) << "Unreachable type " << type;
1365 }
1366
1367 if (Primitive::IsFloatingPointType(type) &&
1368 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001369 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1370 } else if (!Primitive::IsFloatingPointType(type) &&
1371 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1372 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1373 } else {
1374 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001375 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1376 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001377 }
1378
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001379 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001380 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001381 return next_location;
1382}
1383
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001384Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001385 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001386}
1387
Serban Constantinescu579885a2015-02-22 20:51:33 +00001388CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1389 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001390 const CompilerOptions& compiler_options,
1391 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001392 : CodeGenerator(graph,
1393 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001394 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001395 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001396 callee_saved_core_registers.GetList(),
1397 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001398 compiler_options,
1399 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001400 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001401 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001402 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001403 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001404 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001405 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001406 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001407 uint32_literals_(std::less<uint32_t>(),
1408 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001409 uint64_literals_(std::less<uint64_t>(),
1410 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001411 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1412 boot_image_string_patches_(StringReferenceValueComparator(),
1413 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1414 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001415 boot_image_type_patches_(TypeReferenceValueComparator(),
1416 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1417 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001418 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001419 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001420 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1421 jit_class_patches_(TypeReferenceValueComparator(),
1422 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001423 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001424 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001425}
Alexandre Rames5319def2014-10-23 10:03:10 +01001426
Alexandre Rames67555f72014-11-18 10:55:16 +00001427#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001428
Zheng Xu3927c8b2015-11-18 17:46:25 +08001429void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001430 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001431 jump_table->EmitTable(this);
1432 }
1433}
1434
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001435void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001436 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001437 // Ensure we emit the literal pool.
1438 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001439
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001440 CodeGenerator::Finalize(allocator);
1441}
1442
Zheng Xuad4450e2015-04-17 18:48:56 +08001443void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1444 // Note: There are 6 kinds of moves:
1445 // 1. constant -> GPR/FPR (non-cycle)
1446 // 2. constant -> stack (non-cycle)
1447 // 3. GPR/FPR -> GPR/FPR
1448 // 4. GPR/FPR -> stack
1449 // 5. stack -> GPR/FPR
1450 // 6. stack -> stack (non-cycle)
1451 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1452 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1453 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1454 // dependency.
1455 vixl_temps_.Open(GetVIXLAssembler());
1456}
1457
1458void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1459 vixl_temps_.Close();
1460}
1461
1462Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001463 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1464 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1465 || kind == Location::kSIMDStackSlot);
1466 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1467 ? Location::kFpuRegister
1468 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001469 Location scratch = GetScratchLocation(kind);
1470 if (!scratch.Equals(Location::NoLocation())) {
1471 return scratch;
1472 }
1473 // Allocate from VIXL temp registers.
1474 if (kind == Location::kRegister) {
1475 scratch = LocationFrom(vixl_temps_.AcquireX());
1476 } else {
1477 DCHECK(kind == Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001478 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1479 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1480 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001481 }
1482 AddScratchLocation(scratch);
1483 return scratch;
1484}
1485
1486void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1487 if (loc.IsRegister()) {
1488 vixl_temps_.Release(XRegisterFrom(loc));
1489 } else {
1490 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001491 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001492 }
1493 RemoveScratchLocation(loc);
1494}
1495
Alexandre Rames3e69f162014-12-10 10:36:50 +00001496void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001497 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001498 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001499}
1500
Alexandre Rames5319def2014-10-23 10:03:10 +01001501void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001502 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001503 __ Bind(&frame_entry_label_);
1504
Serban Constantinescu02164b32014-11-13 14:05:07 +00001505 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1506 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001507 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001508 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001509 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001510 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001511 {
1512 // Ensure that between load and RecordPcInfo there are no pools emitted.
1513 ExactAssemblyScope eas(GetVIXLAssembler(),
1514 kInstructionSize,
1515 CodeBufferCheckScope::kExactSize);
1516 __ ldr(wzr, MemOperand(temp, 0));
1517 RecordPcInfo(nullptr, 0);
1518 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001519 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001520
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001521 if (!HasEmptyFrame()) {
1522 int frame_size = GetFrameSize();
1523 // Stack layout:
1524 // sp[frame_size - 8] : lr.
1525 // ... : other preserved core registers.
1526 // ... : other preserved fp registers.
1527 // ... : reserved frame space.
1528 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001529
1530 // Save the current method if we need it. Note that we do not
1531 // do this in HCurrentMethod, as the instruction might have been removed
1532 // in the SSA graph.
1533 if (RequiresCurrentMethod()) {
1534 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001535 } else {
1536 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001537 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001538 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001539 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1540 frame_size - GetCoreSpillSize());
1541 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1542 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001543
1544 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1545 // Initialize should_deoptimize flag to 0.
1546 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1547 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1548 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001549 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001550}
1551
1552void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001553 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001554 if (!HasEmptyFrame()) {
1555 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001556 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1557 frame_size - FrameEntrySpillSize());
1558 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1559 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001560 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001561 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001562 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001563 __ Ret();
1564 GetAssembler()->cfi().RestoreState();
1565 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001566}
1567
Scott Wakeling97c72b72016-06-24 16:19:36 +01001568CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001569 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001570 return CPURegList(CPURegister::kRegister, kXRegSize,
1571 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001572}
1573
Scott Wakeling97c72b72016-06-24 16:19:36 +01001574CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001575 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1576 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001577 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1578 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001579}
1580
Alexandre Rames5319def2014-10-23 10:03:10 +01001581void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1582 __ Bind(GetLabelOf(block));
1583}
1584
Calin Juravle175dc732015-08-25 15:42:32 +01001585void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1586 DCHECK(location.IsRegister());
1587 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1588}
1589
Calin Juravlee460d1d2015-09-29 04:52:17 +01001590void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1591 if (location.IsRegister()) {
1592 locations->AddTemp(location);
1593 } else {
1594 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1595 }
1596}
1597
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001598void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001599 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001600 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001601 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001602 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001603 if (value_can_be_null) {
1604 __ Cbz(value, &done);
1605 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001606 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001607 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001608 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001609 if (value_can_be_null) {
1610 __ Bind(&done);
1611 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001612}
1613
David Brazdil58282f42016-01-14 12:45:10 +00001614void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001615 // Blocked core registers:
1616 // lr : Runtime reserved.
1617 // tr : Runtime reserved.
1618 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1619 // ip1 : VIXL core temp.
1620 // ip0 : VIXL core temp.
1621 //
1622 // Blocked fp registers:
1623 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001624 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1625 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001626 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001627 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001628 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001629
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001630 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001631 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001632 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001633 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001634
David Brazdil58282f42016-01-14 12:45:10 +00001635 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001636 // Stubs do not save callee-save floating point registers. If the graph
1637 // is debuggable, we need to deal with these registers differently. For
1638 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001639 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1640 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001641 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001642 }
1643 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001644}
1645
Alexandre Rames3e69f162014-12-10 10:36:50 +00001646size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1647 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1648 __ Str(reg, MemOperand(sp, stack_index));
1649 return kArm64WordSize;
1650}
1651
1652size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1653 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1654 __ Ldr(reg, MemOperand(sp, stack_index));
1655 return kArm64WordSize;
1656}
1657
1658size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1659 FPRegister reg = FPRegister(reg_id, kDRegSize);
1660 __ Str(reg, MemOperand(sp, stack_index));
1661 return kArm64WordSize;
1662}
1663
1664size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1665 FPRegister reg = FPRegister(reg_id, kDRegSize);
1666 __ Ldr(reg, MemOperand(sp, stack_index));
1667 return kArm64WordSize;
1668}
1669
Alexandre Rames5319def2014-10-23 10:03:10 +01001670void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001671 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001672}
1673
1674void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001675 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001676}
1677
Alexandre Rames67555f72014-11-18 10:55:16 +00001678void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001679 if (constant->IsIntConstant()) {
1680 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1681 } else if (constant->IsLongConstant()) {
1682 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1683 } else if (constant->IsNullConstant()) {
1684 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001685 } else if (constant->IsFloatConstant()) {
1686 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1687 } else {
1688 DCHECK(constant->IsDoubleConstant());
1689 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1690 }
1691}
1692
Alexandre Rames3e69f162014-12-10 10:36:50 +00001693
1694static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1695 DCHECK(constant.IsConstant());
1696 HConstant* cst = constant.GetConstant();
1697 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001698 // Null is mapped to a core W register, which we associate with kPrimInt.
1699 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001700 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1701 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1702 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1703}
1704
Roland Levillain558dea12017-01-27 19:40:44 +00001705// Allocate a scratch register from the VIXL pool, querying first into
1706// the floating-point register pool, and then the the core register
1707// pool. This is essentially a reimplementation of
1708// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1709// using a different allocation strategy.
1710static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1711 vixl::aarch64::UseScratchRegisterScope* temps,
1712 int size_in_bits) {
1713 return masm->GetScratchFPRegisterList()->IsEmpty()
1714 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1715 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1716}
1717
Calin Juravlee460d1d2015-09-29 04:52:17 +01001718void CodeGeneratorARM64::MoveLocation(Location destination,
1719 Location source,
1720 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001721 if (source.Equals(destination)) {
1722 return;
1723 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001724
1725 // A valid move can always be inferred from the destination and source
1726 // locations. When moving from and to a register, the argument type can be
1727 // used to generate 32bit instead of 64bit moves. In debug mode we also
1728 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001729 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001730
1731 if (destination.IsRegister() || destination.IsFpuRegister()) {
1732 if (unspecified_type) {
1733 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1734 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001735 (src_cst != nullptr && (src_cst->IsIntConstant()
1736 || src_cst->IsFloatConstant()
1737 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001738 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001739 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001740 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001741 // If the source is a double stack slot or a 64bit constant, a 64bit
1742 // type is appropriate. Else the source is a register, and since the
1743 // type has not been specified, we chose a 64bit type to force a 64bit
1744 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001745 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001746 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001747 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001748 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1749 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1750 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001751 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1752 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1753 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001754 } else if (source.IsSIMDStackSlot()) {
1755 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001756 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001757 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001758 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001759 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001760 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001761 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001762 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001763 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001764 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1765 ? Primitive::kPrimLong
1766 : Primitive::kPrimInt;
1767 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1768 }
1769 } else {
1770 DCHECK(source.IsFpuRegister());
1771 if (destination.IsRegister()) {
1772 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1773 ? Primitive::kPrimDouble
1774 : Primitive::kPrimFloat;
1775 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1776 } else {
1777 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001778 if (GetGraph()->HasSIMD()) {
1779 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1780 } else {
1781 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1782 }
1783 }
1784 }
1785 } else if (destination.IsSIMDStackSlot()) {
1786 if (source.IsFpuRegister()) {
1787 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1788 } else {
1789 DCHECK(source.IsSIMDStackSlot());
1790 UseScratchRegisterScope temps(GetVIXLAssembler());
1791 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1792 Register temp = temps.AcquireX();
1793 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1794 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1795 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1796 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1797 } else {
1798 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1799 __ Ldr(temp, StackOperandFrom(source));
1800 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001801 }
1802 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001803 } else { // The destination is not a register. It must be a stack slot.
1804 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1805 if (source.IsRegister() || source.IsFpuRegister()) {
1806 if (unspecified_type) {
1807 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001808 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001809 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001810 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001811 }
1812 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001813 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1814 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1815 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001816 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001817 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1818 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001819 UseScratchRegisterScope temps(GetVIXLAssembler());
1820 HConstant* src_cst = source.GetConstant();
1821 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001822 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001823 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1824 ? Register(xzr)
1825 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001826 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001827 if (src_cst->IsIntConstant()) {
1828 temp = temps.AcquireW();
1829 } else if (src_cst->IsLongConstant()) {
1830 temp = temps.AcquireX();
1831 } else if (src_cst->IsFloatConstant()) {
1832 temp = temps.AcquireS();
1833 } else {
1834 DCHECK(src_cst->IsDoubleConstant());
1835 temp = temps.AcquireD();
1836 }
1837 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001838 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001839 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001840 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001841 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001842 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001843 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001844 // Use any scratch register (a core or a floating-point one)
1845 // from VIXL scratch register pools as a temporary.
1846 //
1847 // We used to only use the FP scratch register pool, but in some
1848 // rare cases the only register from this pool (D31) would
1849 // already be used (e.g. within a ParallelMove instruction, when
1850 // a move is blocked by a another move requiring a scratch FP
1851 // register, which would reserve D31). To prevent this issue, we
1852 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001853 //
1854 // Also, we start by asking for a FP scratch register first, as the
1855 // demand of scratch core registers is higher. This is why we
1856 // use AcquireFPOrCoreCPURegisterOfSize instead of
1857 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1858 // allocates core scratch registers first.
1859 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1860 GetVIXLAssembler(),
1861 &temps,
1862 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001863 __ Ldr(temp, StackOperandFrom(source));
1864 __ Str(temp, StackOperandFrom(destination));
1865 }
1866 }
1867}
1868
1869void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001870 CPURegister dst,
1871 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001872 switch (type) {
1873 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001874 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001875 break;
1876 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001877 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001878 break;
1879 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001880 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001881 break;
1882 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001883 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001884 break;
1885 case Primitive::kPrimInt:
1886 case Primitive::kPrimNot:
1887 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001888 case Primitive::kPrimFloat:
1889 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001890 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001891 __ Ldr(dst, src);
1892 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001893 case Primitive::kPrimVoid:
1894 LOG(FATAL) << "Unreachable type " << type;
1895 }
1896}
1897
Calin Juravle77520bc2015-01-12 18:45:46 +00001898void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001899 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001900 const MemOperand& src,
1901 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001902 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001903 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001904 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001905 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001906
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001907 DCHECK(!src.IsPreIndex());
1908 DCHECK(!src.IsPostIndex());
1909
1910 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001911 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001912 {
1913 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1914 MemOperand base = MemOperand(temp_base);
1915 switch (type) {
1916 case Primitive::kPrimBoolean:
1917 {
1918 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1919 __ ldarb(Register(dst), base);
1920 if (needs_null_check) {
1921 MaybeRecordImplicitNullCheck(instruction);
1922 }
1923 }
1924 break;
1925 case Primitive::kPrimByte:
1926 {
1927 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1928 __ ldarb(Register(dst), base);
1929 if (needs_null_check) {
1930 MaybeRecordImplicitNullCheck(instruction);
1931 }
1932 }
1933 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1934 break;
1935 case Primitive::kPrimChar:
1936 {
1937 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1938 __ ldarh(Register(dst), base);
1939 if (needs_null_check) {
1940 MaybeRecordImplicitNullCheck(instruction);
1941 }
1942 }
1943 break;
1944 case Primitive::kPrimShort:
1945 {
1946 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1947 __ ldarh(Register(dst), base);
1948 if (needs_null_check) {
1949 MaybeRecordImplicitNullCheck(instruction);
1950 }
1951 }
1952 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1953 break;
1954 case Primitive::kPrimInt:
1955 case Primitive::kPrimNot:
1956 case Primitive::kPrimLong:
1957 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1958 {
1959 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1960 __ ldar(Register(dst), base);
1961 if (needs_null_check) {
1962 MaybeRecordImplicitNullCheck(instruction);
1963 }
1964 }
1965 break;
1966 case Primitive::kPrimFloat:
1967 case Primitive::kPrimDouble: {
1968 DCHECK(dst.IsFPRegister());
1969 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001970
Artem Serov914d7a82017-02-07 14:33:49 +00001971 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1972 {
1973 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1974 __ ldar(temp, base);
1975 if (needs_null_check) {
1976 MaybeRecordImplicitNullCheck(instruction);
1977 }
1978 }
1979 __ Fmov(FPRegister(dst), temp);
1980 break;
Roland Levillain44015862016-01-22 11:47:17 +00001981 }
Artem Serov914d7a82017-02-07 14:33:49 +00001982 case Primitive::kPrimVoid:
1983 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001984 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001985 }
1986}
1987
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001988void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001989 CPURegister src,
1990 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001991 switch (type) {
1992 case Primitive::kPrimBoolean:
1993 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001994 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001995 break;
1996 case Primitive::kPrimChar:
1997 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001998 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001999 break;
2000 case Primitive::kPrimInt:
2001 case Primitive::kPrimNot:
2002 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002003 case Primitive::kPrimFloat:
2004 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002005 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002006 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002007 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002008 case Primitive::kPrimVoid:
2009 LOG(FATAL) << "Unreachable type " << type;
2010 }
2011}
2012
Artem Serov914d7a82017-02-07 14:33:49 +00002013void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2014 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002015 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002016 const MemOperand& dst,
2017 bool needs_null_check) {
2018 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002019 UseScratchRegisterScope temps(GetVIXLAssembler());
2020 Register temp_base = temps.AcquireX();
2021
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002022 DCHECK(!dst.IsPreIndex());
2023 DCHECK(!dst.IsPostIndex());
2024
2025 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002026 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002027 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002028 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002029 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002030 switch (type) {
2031 case Primitive::kPrimBoolean:
2032 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002033 {
2034 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2035 __ stlrb(Register(src), base);
2036 if (needs_null_check) {
2037 MaybeRecordImplicitNullCheck(instruction);
2038 }
2039 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002040 break;
2041 case Primitive::kPrimChar:
2042 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002043 {
2044 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2045 __ stlrh(Register(src), base);
2046 if (needs_null_check) {
2047 MaybeRecordImplicitNullCheck(instruction);
2048 }
2049 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002050 break;
2051 case Primitive::kPrimInt:
2052 case Primitive::kPrimNot:
2053 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002054 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002055 {
2056 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2057 __ stlr(Register(src), base);
2058 if (needs_null_check) {
2059 MaybeRecordImplicitNullCheck(instruction);
2060 }
2061 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002062 break;
2063 case Primitive::kPrimFloat:
2064 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002065 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002066 Register temp_src;
2067 if (src.IsZero()) {
2068 // The zero register is used to avoid synthesizing zero constants.
2069 temp_src = Register(src);
2070 } else {
2071 DCHECK(src.IsFPRegister());
2072 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2073 __ Fmov(temp_src, FPRegister(src));
2074 }
Artem Serov914d7a82017-02-07 14:33:49 +00002075 {
2076 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2077 __ stlr(temp_src, base);
2078 if (needs_null_check) {
2079 MaybeRecordImplicitNullCheck(instruction);
2080 }
2081 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002082 break;
2083 }
2084 case Primitive::kPrimVoid:
2085 LOG(FATAL) << "Unreachable type " << type;
2086 }
2087}
2088
Calin Juravle175dc732015-08-25 15:42:32 +01002089void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2090 HInstruction* instruction,
2091 uint32_t dex_pc,
2092 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002093 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002094
2095 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2096 {
2097 // Ensure the pc position is recorded immediately after the `blr` instruction.
2098 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2099 __ blr(lr);
2100 if (EntrypointRequiresStackMap(entrypoint)) {
2101 RecordPcInfo(instruction, dex_pc, slow_path);
2102 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002103 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002104}
2105
Roland Levillaindec8f632016-07-22 17:10:06 +01002106void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2107 HInstruction* instruction,
2108 SlowPathCode* slow_path) {
2109 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002110 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2111 __ Blr(lr);
2112}
2113
Alexandre Rames67555f72014-11-18 10:55:16 +00002114void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002115 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002116 UseScratchRegisterScope temps(GetVIXLAssembler());
2117 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002118 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2119
Serban Constantinescu02164b32014-11-13 14:05:07 +00002120 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002121 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2122 __ Add(temp, class_reg, status_offset);
2123 __ Ldar(temp, HeapOperand(temp));
2124 __ Cmp(temp, mirror::Class::kStatusInitialized);
2125 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002126 __ Bind(slow_path->GetExitLabel());
2127}
Alexandre Rames5319def2014-10-23 10:03:10 +01002128
Roland Levillain44015862016-01-22 11:47:17 +00002129void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002130 BarrierType type = BarrierAll;
2131
2132 switch (kind) {
2133 case MemBarrierKind::kAnyAny:
2134 case MemBarrierKind::kAnyStore: {
2135 type = BarrierAll;
2136 break;
2137 }
2138 case MemBarrierKind::kLoadAny: {
2139 type = BarrierReads;
2140 break;
2141 }
2142 case MemBarrierKind::kStoreStore: {
2143 type = BarrierWrites;
2144 break;
2145 }
2146 default:
2147 LOG(FATAL) << "Unexpected memory barrier " << kind;
2148 }
2149 __ Dmb(InnerShareable, type);
2150}
2151
Serban Constantinescu02164b32014-11-13 14:05:07 +00002152void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2153 HBasicBlock* successor) {
2154 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002155 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2156 if (slow_path == nullptr) {
2157 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2158 instruction->SetSlowPath(slow_path);
2159 codegen_->AddSlowPath(slow_path);
2160 if (successor != nullptr) {
2161 DCHECK(successor->IsLoopHeader());
2162 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2163 }
2164 } else {
2165 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2166 }
2167
Serban Constantinescu02164b32014-11-13 14:05:07 +00002168 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2169 Register temp = temps.AcquireW();
2170
Andreas Gampe542451c2016-07-26 09:02:02 -07002171 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002172 if (successor == nullptr) {
2173 __ Cbnz(temp, slow_path->GetEntryLabel());
2174 __ Bind(slow_path->GetReturnLabel());
2175 } else {
2176 __ Cbz(temp, codegen_->GetLabelOf(successor));
2177 __ B(slow_path->GetEntryLabel());
2178 // slow_path will return to GetLabelOf(successor).
2179 }
2180}
2181
Alexandre Rames5319def2014-10-23 10:03:10 +01002182InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2183 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002184 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002185 assembler_(codegen->GetAssembler()),
2186 codegen_(codegen) {}
2187
2188#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002189 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002190
2191#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2192
2193enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002194 // Using a base helps identify when we hit such breakpoints.
2195 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002196#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2197 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2198#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2199};
2200
2201#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002202 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002203 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2204 } \
2205 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2206 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2207 locations->SetOut(Location::Any()); \
2208 }
2209 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2210#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2211
2212#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002213#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002214
Alexandre Rames67555f72014-11-18 10:55:16 +00002215void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002216 DCHECK_EQ(instr->InputCount(), 2U);
2217 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2218 Primitive::Type type = instr->GetResultType();
2219 switch (type) {
2220 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002221 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002222 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002223 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002224 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002225 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002226
2227 case Primitive::kPrimFloat:
2228 case Primitive::kPrimDouble:
2229 locations->SetInAt(0, Location::RequiresFpuRegister());
2230 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002231 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002232 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002233
Alexandre Rames5319def2014-10-23 10:03:10 +01002234 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002235 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002236 }
2237}
2238
Alexandre Rames09a99962015-04-15 11:47:56 +01002239void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002240 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2241
2242 bool object_field_get_with_read_barrier =
2243 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002244 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002245 new (GetGraph()->GetArena()) LocationSummary(instruction,
2246 object_field_get_with_read_barrier ?
2247 LocationSummary::kCallOnSlowPath :
2248 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002249 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002250 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002251 // We need a temporary register for the read barrier marking slow
2252 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
2253 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +01002254 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002255 locations->SetInAt(0, Location::RequiresRegister());
2256 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2257 locations->SetOut(Location::RequiresFpuRegister());
2258 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002259 // The output overlaps for an object field get when read barriers
2260 // are enabled: we do not want the load to overwrite the object's
2261 // location, as we need it to emit the read barrier.
2262 locations->SetOut(
2263 Location::RequiresRegister(),
2264 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002265 }
2266}
2267
2268void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2269 const FieldInfo& field_info) {
2270 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002271 LocationSummary* locations = instruction->GetLocations();
2272 Location base_loc = locations->InAt(0);
2273 Location out = locations->Out();
2274 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002275 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002276 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002277
Roland Levillain44015862016-01-22 11:47:17 +00002278 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2279 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002280 // /* HeapReference<Object> */ out = *(base + offset)
2281 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Roland Levillaind0b51832017-01-26 19:04:23 +00002282 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillain44015862016-01-22 11:47:17 +00002283 // Note that potential implicit null checks are handled in this
2284 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2285 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2286 instruction,
2287 out,
2288 base,
2289 offset,
2290 temp,
2291 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002292 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002293 } else {
2294 // General case.
2295 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002296 // Note that a potential implicit null check is handled in this
2297 // CodeGeneratorARM64::LoadAcquire call.
2298 // NB: LoadAcquire will record the pc info if needed.
2299 codegen_->LoadAcquire(
2300 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002301 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002302 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2303 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002304 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002305 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002306 }
Roland Levillain44015862016-01-22 11:47:17 +00002307 if (field_type == Primitive::kPrimNot) {
2308 // If read barriers are enabled, emit read barriers other than
2309 // Baker's using a slow path (and also unpoison the loaded
2310 // reference, if heap poisoning is enabled).
2311 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2312 }
Roland Levillain4d027112015-07-01 15:41:14 +01002313 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002314}
2315
2316void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2317 LocationSummary* locations =
2318 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2319 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002320 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2321 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2322 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002323 locations->SetInAt(1, Location::RequiresFpuRegister());
2324 } else {
2325 locations->SetInAt(1, Location::RequiresRegister());
2326 }
2327}
2328
2329void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002330 const FieldInfo& field_info,
2331 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002332 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2333
2334 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002335 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002336 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002337 Offset offset = field_info.GetFieldOffset();
2338 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002339
Roland Levillain4d027112015-07-01 15:41:14 +01002340 {
2341 // We use a block to end the scratch scope before the write barrier, thus
2342 // freeing the temporary registers so they can be used in `MarkGCCard`.
2343 UseScratchRegisterScope temps(GetVIXLAssembler());
2344
2345 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2346 DCHECK(value.IsW());
2347 Register temp = temps.AcquireW();
2348 __ Mov(temp, value.W());
2349 GetAssembler()->PoisonHeapReference(temp.W());
2350 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002351 }
Roland Levillain4d027112015-07-01 15:41:14 +01002352
2353 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002354 codegen_->StoreRelease(
2355 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002356 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002357 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2358 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002359 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2360 codegen_->MaybeRecordImplicitNullCheck(instruction);
2361 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002362 }
2363
2364 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002365 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002366 }
2367}
2368
Alexandre Rames67555f72014-11-18 10:55:16 +00002369void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002370 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002371
2372 switch (type) {
2373 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002374 case Primitive::kPrimLong: {
2375 Register dst = OutputRegister(instr);
2376 Register lhs = InputRegisterAt(instr, 0);
2377 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002378 if (instr->IsAdd()) {
2379 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002380 } else if (instr->IsAnd()) {
2381 __ And(dst, lhs, rhs);
2382 } else if (instr->IsOr()) {
2383 __ Orr(dst, lhs, rhs);
2384 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002385 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002386 } else if (instr->IsRor()) {
2387 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002388 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002389 __ Ror(dst, lhs, shift);
2390 } else {
2391 // Ensure shift distance is in the same size register as the result. If
2392 // we are rotating a long and the shift comes in a w register originally,
2393 // we don't need to sxtw for use as an x since the shift distances are
2394 // all & reg_bits - 1.
2395 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2396 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002397 } else {
2398 DCHECK(instr->IsXor());
2399 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002400 }
2401 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002402 }
2403 case Primitive::kPrimFloat:
2404 case Primitive::kPrimDouble: {
2405 FPRegister dst = OutputFPRegister(instr);
2406 FPRegister lhs = InputFPRegisterAt(instr, 0);
2407 FPRegister rhs = InputFPRegisterAt(instr, 1);
2408 if (instr->IsAdd()) {
2409 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002410 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002411 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002412 } else {
2413 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002414 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002415 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002416 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002417 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002418 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002419 }
2420}
2421
Serban Constantinescu02164b32014-11-13 14:05:07 +00002422void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2423 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2424
2425 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2426 Primitive::Type type = instr->GetResultType();
2427 switch (type) {
2428 case Primitive::kPrimInt:
2429 case Primitive::kPrimLong: {
2430 locations->SetInAt(0, Location::RequiresRegister());
2431 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002432 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002433 break;
2434 }
2435 default:
2436 LOG(FATAL) << "Unexpected shift type " << type;
2437 }
2438}
2439
2440void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2441 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2442
2443 Primitive::Type type = instr->GetType();
2444 switch (type) {
2445 case Primitive::kPrimInt:
2446 case Primitive::kPrimLong: {
2447 Register dst = OutputRegister(instr);
2448 Register lhs = InputRegisterAt(instr, 0);
2449 Operand rhs = InputOperandAt(instr, 1);
2450 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002451 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002452 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002453 if (instr->IsShl()) {
2454 __ Lsl(dst, lhs, shift_value);
2455 } else if (instr->IsShr()) {
2456 __ Asr(dst, lhs, shift_value);
2457 } else {
2458 __ Lsr(dst, lhs, shift_value);
2459 }
2460 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002461 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002462
2463 if (instr->IsShl()) {
2464 __ Lsl(dst, lhs, rhs_reg);
2465 } else if (instr->IsShr()) {
2466 __ Asr(dst, lhs, rhs_reg);
2467 } else {
2468 __ Lsr(dst, lhs, rhs_reg);
2469 }
2470 }
2471 break;
2472 }
2473 default:
2474 LOG(FATAL) << "Unexpected shift operation type " << type;
2475 }
2476}
2477
Alexandre Rames5319def2014-10-23 10:03:10 +01002478void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002479 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002480}
2481
2482void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002483 HandleBinaryOp(instruction);
2484}
2485
2486void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2487 HandleBinaryOp(instruction);
2488}
2489
2490void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2491 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002492}
2493
Artem Serov7fc63502016-02-09 17:15:29 +00002494void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002495 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2496 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2497 locations->SetInAt(0, Location::RequiresRegister());
2498 // There is no immediate variant of negated bitwise instructions in AArch64.
2499 locations->SetInAt(1, Location::RequiresRegister());
2500 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2501}
2502
Artem Serov7fc63502016-02-09 17:15:29 +00002503void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002504 Register dst = OutputRegister(instr);
2505 Register lhs = InputRegisterAt(instr, 0);
2506 Register rhs = InputRegisterAt(instr, 1);
2507
2508 switch (instr->GetOpKind()) {
2509 case HInstruction::kAnd:
2510 __ Bic(dst, lhs, rhs);
2511 break;
2512 case HInstruction::kOr:
2513 __ Orn(dst, lhs, rhs);
2514 break;
2515 case HInstruction::kXor:
2516 __ Eon(dst, lhs, rhs);
2517 break;
2518 default:
2519 LOG(FATAL) << "Unreachable";
2520 }
2521}
2522
Anton Kirilov74234da2017-01-13 14:42:47 +00002523void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2524 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002525 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2526 instruction->GetType() == Primitive::kPrimLong);
2527 LocationSummary* locations =
2528 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2529 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2530 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2531 } else {
2532 locations->SetInAt(0, Location::RequiresRegister());
2533 }
2534 locations->SetInAt(1, Location::RequiresRegister());
2535 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2536}
2537
Anton Kirilov74234da2017-01-13 14:42:47 +00002538void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2539 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002540 Primitive::Type type = instruction->GetType();
2541 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2542 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2543 Register out = OutputRegister(instruction);
2544 Register left;
2545 if (kind != HInstruction::kNeg) {
2546 left = InputRegisterAt(instruction, 0);
2547 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002548 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002549 // shifter operand operation, the IR generating `right_reg` (input to the type
2550 // conversion) can have a different type from the current instruction's type,
2551 // so we manually indicate the type.
2552 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002553 Operand right_operand(0);
2554
Anton Kirilov74234da2017-01-13 14:42:47 +00002555 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2556 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002557 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2558 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002559 right_operand = Operand(right_reg,
2560 helpers::ShiftFromOpKind(op_kind),
2561 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002562 }
2563
2564 // Logical binary operations do not support extension operations in the
2565 // operand. Note that VIXL would still manage if it was passed by generating
2566 // the extension as a separate instruction.
2567 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2568 DCHECK(!right_operand.IsExtendedRegister() ||
2569 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2570 kind != HInstruction::kNeg));
2571 switch (kind) {
2572 case HInstruction::kAdd:
2573 __ Add(out, left, right_operand);
2574 break;
2575 case HInstruction::kAnd:
2576 __ And(out, left, right_operand);
2577 break;
2578 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002579 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002580 __ Neg(out, right_operand);
2581 break;
2582 case HInstruction::kOr:
2583 __ Orr(out, left, right_operand);
2584 break;
2585 case HInstruction::kSub:
2586 __ Sub(out, left, right_operand);
2587 break;
2588 case HInstruction::kXor:
2589 __ Eor(out, left, right_operand);
2590 break;
2591 default:
2592 LOG(FATAL) << "Unexpected operation kind: " << kind;
2593 UNREACHABLE();
2594 }
2595}
2596
Artem Serov328429f2016-07-06 16:23:04 +01002597void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002598 LocationSummary* locations =
2599 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2600 locations->SetInAt(0, Location::RequiresRegister());
2601 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002602 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002603}
2604
Roland Levillain19c54192016-11-04 13:44:09 +00002605void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002606 __ Add(OutputRegister(instruction),
2607 InputRegisterAt(instruction, 0),
2608 Operand(InputOperandAt(instruction, 1)));
2609}
2610
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002611void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002612 LocationSummary* locations =
2613 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002614 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2615 if (instr->GetOpKind() == HInstruction::kSub &&
2616 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002617 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002618 // Don't allocate register for Mneg instruction.
2619 } else {
2620 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2621 Location::RequiresRegister());
2622 }
2623 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2624 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002625 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2626}
2627
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002628void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002629 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002630 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2631 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002632
2633 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2634 // This fixup should be carried out for all multiply-accumulate instructions:
2635 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2636 if (instr->GetType() == Primitive::kPrimLong &&
2637 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2638 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002639 vixl::aarch64::Instruction* prev =
2640 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002641 if (prev->IsLoadOrStore()) {
2642 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002643 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002644 __ nop();
2645 }
2646 }
2647
2648 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002649 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002650 __ Madd(res, mul_left, mul_right, accumulator);
2651 } else {
2652 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002653 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002654 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002655 __ Mneg(res, mul_left, mul_right);
2656 } else {
2657 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2658 __ Msub(res, mul_left, mul_right, accumulator);
2659 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002660 }
2661}
2662
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002663void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002664 bool object_array_get_with_read_barrier =
2665 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002666 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002667 new (GetGraph()->GetArena()) LocationSummary(instruction,
2668 object_array_get_with_read_barrier ?
2669 LocationSummary::kCallOnSlowPath :
2670 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002671 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002672 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002673 // We need a temporary register for the read barrier marking slow
2674 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
2675 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +01002676 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002677 locations->SetInAt(0, Location::RequiresRegister());
2678 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002679 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2680 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2681 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002682 // The output overlaps in the case of an object array get with
2683 // read barriers enabled: we do not want the move to overwrite the
2684 // array's location, as we need it to emit the read barrier.
2685 locations->SetOut(
2686 Location::RequiresRegister(),
2687 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002688 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002689}
2690
2691void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002692 Primitive::Type type = instruction->GetType();
2693 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002694 LocationSummary* locations = instruction->GetLocations();
2695 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002696 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002697 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002698 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2699 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002700 MacroAssembler* masm = GetVIXLAssembler();
2701 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002702
Roland Levillain19c54192016-11-04 13:44:09 +00002703 // The read barrier instrumentation of object ArrayGet instructions
2704 // does not support the HIntermediateAddress instruction.
2705 DCHECK(!((type == Primitive::kPrimNot) &&
2706 instruction->GetArray()->IsIntermediateAddress() &&
2707 kEmitCompilerReadBarrier));
2708
Roland Levillain44015862016-01-22 11:47:17 +00002709 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2710 // Object ArrayGet with Baker's read barrier case.
Roland Levillain54f869e2017-03-06 13:54:11 +00002711 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillain44015862016-01-22 11:47:17 +00002712 // Note that a potential implicit null check is handled in the
2713 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2714 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2715 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002716 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002717 // General case.
2718 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002719 Register length;
2720 if (maybe_compressed_char_at) {
2721 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2722 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002723 {
2724 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2725 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2726
2727 if (instruction->GetArray()->IsIntermediateAddress()) {
2728 DCHECK_LT(count_offset, offset);
2729 int64_t adjusted_offset =
2730 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2731 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2732 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2733 } else {
2734 __ Ldr(length, HeapOperand(obj, count_offset));
2735 }
2736 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002737 }
jessicahandojo05765752016-09-09 19:01:32 -07002738 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002739 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002740 if (maybe_compressed_char_at) {
2741 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002742 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2743 "Expecting 0=compressed, 1=uncompressed");
2744 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002745 __ Ldrb(Register(OutputCPURegister(instruction)),
2746 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2747 __ B(&done);
2748 __ Bind(&uncompressed_load);
2749 __ Ldrh(Register(OutputCPURegister(instruction)),
2750 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2751 __ Bind(&done);
2752 } else {
2753 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2754 source = HeapOperand(obj, offset);
2755 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002756 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002757 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002758 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002759 // We do not need to compute the intermediate address from the array: the
2760 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002761 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002762 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002763 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002764 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2765 }
2766 temp = obj;
2767 } else {
2768 __ Add(temp, obj, offset);
2769 }
jessicahandojo05765752016-09-09 19:01:32 -07002770 if (maybe_compressed_char_at) {
2771 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002772 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2773 "Expecting 0=compressed, 1=uncompressed");
2774 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002775 __ Ldrb(Register(OutputCPURegister(instruction)),
2776 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2777 __ B(&done);
2778 __ Bind(&uncompressed_load);
2779 __ Ldrh(Register(OutputCPURegister(instruction)),
2780 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2781 __ Bind(&done);
2782 } else {
2783 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2784 }
Roland Levillain44015862016-01-22 11:47:17 +00002785 }
jessicahandojo05765752016-09-09 19:01:32 -07002786 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002787 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2788 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002789 codegen_->Load(type, OutputCPURegister(instruction), source);
2790 codegen_->MaybeRecordImplicitNullCheck(instruction);
2791 }
Roland Levillain44015862016-01-22 11:47:17 +00002792
2793 if (type == Primitive::kPrimNot) {
2794 static_assert(
2795 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2796 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2797 Location obj_loc = locations->InAt(0);
2798 if (index.IsConstant()) {
2799 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2800 } else {
2801 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2802 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002803 }
Roland Levillain4d027112015-07-01 15:41:14 +01002804 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002805}
2806
Alexandre Rames5319def2014-10-23 10:03:10 +01002807void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2808 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2809 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002810 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002811}
2812
2813void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002814 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002815 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002816 {
2817 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2818 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2819 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2820 codegen_->MaybeRecordImplicitNullCheck(instruction);
2821 }
jessicahandojo05765752016-09-09 19:01:32 -07002822 // Mask out compression flag from String's array length.
2823 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002824 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002825 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002826}
2827
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002828void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002829 Primitive::Type value_type = instruction->GetComponentType();
2830
2831 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002832 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2833 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002834 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002835 LocationSummary::kCallOnSlowPath :
2836 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002837 locations->SetInAt(0, Location::RequiresRegister());
2838 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002839 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2840 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2841 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002842 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002843 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002844 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002845 }
2846}
2847
2848void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2849 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002850 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002851 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002852 bool needs_write_barrier =
2853 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002854
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002855 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002856 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002857 CPURegister source = value;
2858 Location index = locations->InAt(1);
2859 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2860 MemOperand destination = HeapOperand(array);
2861 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002862
2863 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002864 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002865 if (index.IsConstant()) {
2866 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2867 destination = HeapOperand(array, offset);
2868 } else {
2869 UseScratchRegisterScope temps(masm);
2870 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002871 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002872 // We do not need to compute the intermediate address from the array: the
2873 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002874 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002875 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002876 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002877 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2878 }
2879 temp = array;
2880 } else {
2881 __ Add(temp, array, offset);
2882 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002883 destination = HeapOperand(temp,
2884 XRegisterFrom(index),
2885 LSL,
2886 Primitive::ComponentSizeShift(value_type));
2887 }
Artem Serov914d7a82017-02-07 14:33:49 +00002888 {
2889 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2890 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2891 codegen_->Store(value_type, value, destination);
2892 codegen_->MaybeRecordImplicitNullCheck(instruction);
2893 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002894 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002895 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002896 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002897 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002898 {
2899 // We use a block to end the scratch scope before the write barrier, thus
2900 // freeing the temporary registers so they can be used in `MarkGCCard`.
2901 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002902 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002903 if (index.IsConstant()) {
2904 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002905 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002906 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002907 destination = HeapOperand(temp,
2908 XRegisterFrom(index),
2909 LSL,
2910 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002911 }
2912
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002913 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2914 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2915 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2916
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002917 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002918 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2919 codegen_->AddSlowPath(slow_path);
2920 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002921 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002922 __ Cbnz(Register(value), &non_zero);
2923 if (!index.IsConstant()) {
2924 __ Add(temp, array, offset);
2925 }
Artem Serov914d7a82017-02-07 14:33:49 +00002926 {
2927 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
2928 // emitted.
2929 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2930 __ Str(wzr, destination);
2931 codegen_->MaybeRecordImplicitNullCheck(instruction);
2932 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002933 __ B(&done);
2934 __ Bind(&non_zero);
2935 }
2936
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002937 // Note that when Baker read barriers are enabled, the type
2938 // checks are performed without read barriers. This is fine,
2939 // even in the case where a class object is in the from-space
2940 // after the flip, as a comparison involving such a type would
2941 // not produce a false positive; it may of course produce a
2942 // false negative, in which case we would take the ArraySet
2943 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002944
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002945 Register temp2 = temps.AcquireSameSizeAs(array);
2946 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00002947 {
2948 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2949 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2950 __ Ldr(temp, HeapOperand(array, class_offset));
2951 codegen_->MaybeRecordImplicitNullCheck(instruction);
2952 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002953 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002954
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002955 // /* HeapReference<Class> */ temp = temp->component_type_
2956 __ Ldr(temp, HeapOperand(temp, component_offset));
2957 // /* HeapReference<Class> */ temp2 = value->klass_
2958 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2959 // If heap poisoning is enabled, no need to unpoison `temp`
2960 // nor `temp2`, as we are comparing two poisoned references.
2961 __ Cmp(temp, temp2);
2962 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002963
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002964 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2965 vixl::aarch64::Label do_put;
2966 __ B(eq, &do_put);
2967 // If heap poisoning is enabled, the `temp` reference has
2968 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002969 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2970
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002971 // /* HeapReference<Class> */ temp = temp->super_class_
2972 __ Ldr(temp, HeapOperand(temp, super_offset));
2973 // If heap poisoning is enabled, no need to unpoison
2974 // `temp`, as we are comparing against null below.
2975 __ Cbnz(temp, slow_path->GetEntryLabel());
2976 __ Bind(&do_put);
2977 } else {
2978 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002979 }
2980 }
2981
2982 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002983 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002984 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002985 __ Mov(temp2, value.W());
2986 GetAssembler()->PoisonHeapReference(temp2);
2987 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002988 }
2989
2990 if (!index.IsConstant()) {
2991 __ Add(temp, array, offset);
2992 }
Artem Serov914d7a82017-02-07 14:33:49 +00002993 {
2994 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2995 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2996 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002997
Artem Serov914d7a82017-02-07 14:33:49 +00002998 if (!may_need_runtime_call_for_type_check) {
2999 codegen_->MaybeRecordImplicitNullCheck(instruction);
3000 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003001 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003002 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003003
3004 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3005
3006 if (done.IsLinked()) {
3007 __ Bind(&done);
3008 }
3009
3010 if (slow_path != nullptr) {
3011 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003012 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003013 }
3014}
3015
Alexandre Rames67555f72014-11-18 10:55:16 +00003016void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003017 RegisterSet caller_saves = RegisterSet::Empty();
3018 InvokeRuntimeCallingConvention calling_convention;
3019 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3020 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3021 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003022 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003023 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003024}
3025
3026void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003027 BoundsCheckSlowPathARM64* slow_path =
3028 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003029 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003030 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3031 __ B(slow_path->GetEntryLabel(), hs);
3032}
3033
Alexandre Rames67555f72014-11-18 10:55:16 +00003034void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3035 LocationSummary* locations =
3036 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3037 locations->SetInAt(0, Location::RequiresRegister());
3038 if (check->HasUses()) {
3039 locations->SetOut(Location::SameAsFirstInput());
3040 }
3041}
3042
3043void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3044 // We assume the class is not null.
3045 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3046 check->GetLoadClass(), check, check->GetDexPc(), true);
3047 codegen_->AddSlowPath(slow_path);
3048 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3049}
3050
Roland Levillain1a653882016-03-18 18:05:57 +00003051static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3052 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3053 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3054}
3055
3056void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3057 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3058 Location rhs_loc = instruction->GetLocations()->InAt(1);
3059 if (rhs_loc.IsConstant()) {
3060 // 0.0 is the only immediate that can be encoded directly in
3061 // an FCMP instruction.
3062 //
3063 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3064 // specify that in a floating-point comparison, positive zero
3065 // and negative zero are considered equal, so we can use the
3066 // literal 0.0 for both cases here.
3067 //
3068 // Note however that some methods (Float.equal, Float.compare,
3069 // Float.compareTo, Double.equal, Double.compare,
3070 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3071 // StrictMath.min) consider 0.0 to be (strictly) greater than
3072 // -0.0. So if we ever translate calls to these methods into a
3073 // HCompare instruction, we must handle the -0.0 case with
3074 // care here.
3075 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3076 __ Fcmp(lhs_reg, 0.0);
3077 } else {
3078 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3079 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003080}
3081
Serban Constantinescu02164b32014-11-13 14:05:07 +00003082void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003083 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003084 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3085 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003086 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003087 case Primitive::kPrimBoolean:
3088 case Primitive::kPrimByte:
3089 case Primitive::kPrimShort:
3090 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003091 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003092 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003093 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003094 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003095 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3096 break;
3097 }
3098 case Primitive::kPrimFloat:
3099 case Primitive::kPrimDouble: {
3100 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003101 locations->SetInAt(1,
3102 IsFloatingPointZeroConstant(compare->InputAt(1))
3103 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3104 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003105 locations->SetOut(Location::RequiresRegister());
3106 break;
3107 }
3108 default:
3109 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3110 }
3111}
3112
3113void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3114 Primitive::Type in_type = compare->InputAt(0)->GetType();
3115
3116 // 0 if: left == right
3117 // 1 if: left > right
3118 // -1 if: left < right
3119 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003120 case Primitive::kPrimBoolean:
3121 case Primitive::kPrimByte:
3122 case Primitive::kPrimShort:
3123 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003124 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003125 case Primitive::kPrimLong: {
3126 Register result = OutputRegister(compare);
3127 Register left = InputRegisterAt(compare, 0);
3128 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003129 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003130 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3131 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003132 break;
3133 }
3134 case Primitive::kPrimFloat:
3135 case Primitive::kPrimDouble: {
3136 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003137 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003138 __ Cset(result, ne);
3139 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003140 break;
3141 }
3142 default:
3143 LOG(FATAL) << "Unimplemented compare type " << in_type;
3144 }
3145}
3146
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003147void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003148 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003149
3150 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3151 locations->SetInAt(0, Location::RequiresFpuRegister());
3152 locations->SetInAt(1,
3153 IsFloatingPointZeroConstant(instruction->InputAt(1))
3154 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3155 : Location::RequiresFpuRegister());
3156 } else {
3157 // Integer cases.
3158 locations->SetInAt(0, Location::RequiresRegister());
3159 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3160 }
3161
David Brazdilb3e773e2016-01-26 11:28:37 +00003162 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003163 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003164 }
3165}
3166
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003167void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003168 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003169 return;
3170 }
3171
3172 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003173 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003174 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003175
Roland Levillain7f63c522015-07-13 15:54:55 +00003176 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003177 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003178 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003179 } else {
3180 // Integer cases.
3181 Register lhs = InputRegisterAt(instruction, 0);
3182 Operand rhs = InputOperandAt(instruction, 1);
3183 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003184 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003185 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003186}
3187
3188#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3189 M(Equal) \
3190 M(NotEqual) \
3191 M(LessThan) \
3192 M(LessThanOrEqual) \
3193 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003194 M(GreaterThanOrEqual) \
3195 M(Below) \
3196 M(BelowOrEqual) \
3197 M(Above) \
3198 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003199#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003200void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3201void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003202FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003203#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003204#undef FOR_EACH_CONDITION_INSTRUCTION
3205
Zheng Xuc6667102015-05-15 16:08:45 +08003206void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3207 DCHECK(instruction->IsDiv() || instruction->IsRem());
3208
3209 LocationSummary* locations = instruction->GetLocations();
3210 Location second = locations->InAt(1);
3211 DCHECK(second.IsConstant());
3212
3213 Register out = OutputRegister(instruction);
3214 Register dividend = InputRegisterAt(instruction, 0);
3215 int64_t imm = Int64FromConstant(second.GetConstant());
3216 DCHECK(imm == 1 || imm == -1);
3217
3218 if (instruction->IsRem()) {
3219 __ Mov(out, 0);
3220 } else {
3221 if (imm == 1) {
3222 __ Mov(out, dividend);
3223 } else {
3224 __ Neg(out, dividend);
3225 }
3226 }
3227}
3228
3229void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3230 DCHECK(instruction->IsDiv() || instruction->IsRem());
3231
3232 LocationSummary* locations = instruction->GetLocations();
3233 Location second = locations->InAt(1);
3234 DCHECK(second.IsConstant());
3235
3236 Register out = OutputRegister(instruction);
3237 Register dividend = InputRegisterAt(instruction, 0);
3238 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003239 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003240 int ctz_imm = CTZ(abs_imm);
3241
3242 UseScratchRegisterScope temps(GetVIXLAssembler());
3243 Register temp = temps.AcquireSameSizeAs(out);
3244
3245 if (instruction->IsDiv()) {
3246 __ Add(temp, dividend, abs_imm - 1);
3247 __ Cmp(dividend, 0);
3248 __ Csel(out, temp, dividend, lt);
3249 if (imm > 0) {
3250 __ Asr(out, out, ctz_imm);
3251 } else {
3252 __ Neg(out, Operand(out, ASR, ctz_imm));
3253 }
3254 } else {
3255 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3256 __ Asr(temp, dividend, bits - 1);
3257 __ Lsr(temp, temp, bits - ctz_imm);
3258 __ Add(out, dividend, temp);
3259 __ And(out, out, abs_imm - 1);
3260 __ Sub(out, out, temp);
3261 }
3262}
3263
3264void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3265 DCHECK(instruction->IsDiv() || instruction->IsRem());
3266
3267 LocationSummary* locations = instruction->GetLocations();
3268 Location second = locations->InAt(1);
3269 DCHECK(second.IsConstant());
3270
3271 Register out = OutputRegister(instruction);
3272 Register dividend = InputRegisterAt(instruction, 0);
3273 int64_t imm = Int64FromConstant(second.GetConstant());
3274
3275 Primitive::Type type = instruction->GetResultType();
3276 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3277
3278 int64_t magic;
3279 int shift;
3280 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3281
3282 UseScratchRegisterScope temps(GetVIXLAssembler());
3283 Register temp = temps.AcquireSameSizeAs(out);
3284
3285 // temp = get_high(dividend * magic)
3286 __ Mov(temp, magic);
3287 if (type == Primitive::kPrimLong) {
3288 __ Smulh(temp, dividend, temp);
3289 } else {
3290 __ Smull(temp.X(), dividend, temp);
3291 __ Lsr(temp.X(), temp.X(), 32);
3292 }
3293
3294 if (imm > 0 && magic < 0) {
3295 __ Add(temp, temp, dividend);
3296 } else if (imm < 0 && magic > 0) {
3297 __ Sub(temp, temp, dividend);
3298 }
3299
3300 if (shift != 0) {
3301 __ Asr(temp, temp, shift);
3302 }
3303
3304 if (instruction->IsDiv()) {
3305 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3306 } else {
3307 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3308 // TODO: Strength reduction for msub.
3309 Register temp_imm = temps.AcquireSameSizeAs(out);
3310 __ Mov(temp_imm, imm);
3311 __ Msub(out, temp, temp_imm, dividend);
3312 }
3313}
3314
3315void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3316 DCHECK(instruction->IsDiv() || instruction->IsRem());
3317 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003318 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003319
3320 LocationSummary* locations = instruction->GetLocations();
3321 Register out = OutputRegister(instruction);
3322 Location second = locations->InAt(1);
3323
3324 if (second.IsConstant()) {
3325 int64_t imm = Int64FromConstant(second.GetConstant());
3326
3327 if (imm == 0) {
3328 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3329 } else if (imm == 1 || imm == -1) {
3330 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003331 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003332 DivRemByPowerOfTwo(instruction);
3333 } else {
3334 DCHECK(imm <= -2 || imm >= 2);
3335 GenerateDivRemWithAnyConstant(instruction);
3336 }
3337 } else {
3338 Register dividend = InputRegisterAt(instruction, 0);
3339 Register divisor = InputRegisterAt(instruction, 1);
3340 if (instruction->IsDiv()) {
3341 __ Sdiv(out, dividend, divisor);
3342 } else {
3343 UseScratchRegisterScope temps(GetVIXLAssembler());
3344 Register temp = temps.AcquireSameSizeAs(out);
3345 __ Sdiv(temp, dividend, divisor);
3346 __ Msub(out, temp, divisor, dividend);
3347 }
3348 }
3349}
3350
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003351void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3352 LocationSummary* locations =
3353 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3354 switch (div->GetResultType()) {
3355 case Primitive::kPrimInt:
3356 case Primitive::kPrimLong:
3357 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003358 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003359 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3360 break;
3361
3362 case Primitive::kPrimFloat:
3363 case Primitive::kPrimDouble:
3364 locations->SetInAt(0, Location::RequiresFpuRegister());
3365 locations->SetInAt(1, Location::RequiresFpuRegister());
3366 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3367 break;
3368
3369 default:
3370 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3371 }
3372}
3373
3374void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3375 Primitive::Type type = div->GetResultType();
3376 switch (type) {
3377 case Primitive::kPrimInt:
3378 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003379 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003380 break;
3381
3382 case Primitive::kPrimFloat:
3383 case Primitive::kPrimDouble:
3384 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3385 break;
3386
3387 default:
3388 LOG(FATAL) << "Unexpected div type " << type;
3389 }
3390}
3391
Alexandre Rames67555f72014-11-18 10:55:16 +00003392void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003393 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003394 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003395}
3396
3397void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3398 SlowPathCodeARM64* slow_path =
3399 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3400 codegen_->AddSlowPath(slow_path);
3401 Location value = instruction->GetLocations()->InAt(0);
3402
Alexandre Rames3e69f162014-12-10 10:36:50 +00003403 Primitive::Type type = instruction->GetType();
3404
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003405 if (!Primitive::IsIntegralType(type)) {
3406 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003407 return;
3408 }
3409
Alexandre Rames67555f72014-11-18 10:55:16 +00003410 if (value.IsConstant()) {
3411 int64_t divisor = Int64ConstantFrom(value);
3412 if (divisor == 0) {
3413 __ B(slow_path->GetEntryLabel());
3414 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003415 // A division by a non-null constant is valid. We don't need to perform
3416 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003417 }
3418 } else {
3419 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3420 }
3421}
3422
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003423void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3424 LocationSummary* locations =
3425 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3426 locations->SetOut(Location::ConstantLocation(constant));
3427}
3428
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003429void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3430 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003431 // Will be generated at use site.
3432}
3433
Alexandre Rames5319def2014-10-23 10:03:10 +01003434void LocationsBuilderARM64::VisitExit(HExit* exit) {
3435 exit->SetLocations(nullptr);
3436}
3437
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003438void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003439}
3440
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003441void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3442 LocationSummary* locations =
3443 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3444 locations->SetOut(Location::ConstantLocation(constant));
3445}
3446
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003447void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003448 // Will be generated at use site.
3449}
3450
David Brazdilfc6a86a2015-06-26 10:33:45 +00003451void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003452 DCHECK(!successor->IsExitBlock());
3453 HBasicBlock* block = got->GetBlock();
3454 HInstruction* previous = got->GetPrevious();
3455 HLoopInformation* info = block->GetLoopInformation();
3456
David Brazdil46e2a392015-03-16 17:31:52 +00003457 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003458 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3459 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3460 return;
3461 }
3462 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3463 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3464 }
3465 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003466 __ B(codegen_->GetLabelOf(successor));
3467 }
3468}
3469
David Brazdilfc6a86a2015-06-26 10:33:45 +00003470void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3471 got->SetLocations(nullptr);
3472}
3473
3474void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3475 HandleGoto(got, got->GetSuccessor());
3476}
3477
3478void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3479 try_boundary->SetLocations(nullptr);
3480}
3481
3482void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3483 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3484 if (!successor->IsExitBlock()) {
3485 HandleGoto(try_boundary, successor);
3486 }
3487}
3488
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003489void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003490 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003491 vixl::aarch64::Label* true_target,
3492 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003493 // FP branching requires both targets to be explicit. If either of the targets
3494 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003495 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003496 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003497
David Brazdil0debae72015-11-12 18:37:00 +00003498 if (true_target == nullptr && false_target == nullptr) {
3499 // Nothing to do. The code always falls through.
3500 return;
3501 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003502 // Constant condition, statically compared against "true" (integer value 1).
3503 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003504 if (true_target != nullptr) {
3505 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003506 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003507 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003508 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003509 if (false_target != nullptr) {
3510 __ B(false_target);
3511 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003512 }
David Brazdil0debae72015-11-12 18:37:00 +00003513 return;
3514 }
3515
3516 // The following code generates these patterns:
3517 // (1) true_target == nullptr && false_target != nullptr
3518 // - opposite condition true => branch to false_target
3519 // (2) true_target != nullptr && false_target == nullptr
3520 // - condition true => branch to true_target
3521 // (3) true_target != nullptr && false_target != nullptr
3522 // - condition true => branch to true_target
3523 // - branch to false_target
3524 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003525 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003526 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003527 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003528 if (true_target == nullptr) {
3529 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3530 } else {
3531 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3532 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003533 } else {
3534 // The condition instruction has not been materialized, use its inputs as
3535 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003536 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003537
David Brazdil0debae72015-11-12 18:37:00 +00003538 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003539 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003540 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003541 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003542 IfCondition opposite_condition = condition->GetOppositeCondition();
3543 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003544 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003545 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003546 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003547 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003548 // Integer cases.
3549 Register lhs = InputRegisterAt(condition, 0);
3550 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003551
3552 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003553 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003554 if (true_target == nullptr) {
3555 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3556 non_fallthrough_target = false_target;
3557 } else {
3558 arm64_cond = ARM64Condition(condition->GetCondition());
3559 non_fallthrough_target = true_target;
3560 }
3561
Aart Bik086d27e2016-01-20 17:02:00 -08003562 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003563 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003564 switch (arm64_cond) {
3565 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003566 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003567 break;
3568 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003569 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003570 break;
3571 case lt:
3572 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003573 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003574 break;
3575 case ge:
3576 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003577 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003578 break;
3579 default:
3580 // Without the `static_cast` the compiler throws an error for
3581 // `-Werror=sign-promo`.
3582 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3583 }
3584 } else {
3585 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003586 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003587 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003588 }
3589 }
David Brazdil0debae72015-11-12 18:37:00 +00003590
3591 // If neither branch falls through (case 3), the conditional branch to `true_target`
3592 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3593 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003594 __ B(false_target);
3595 }
David Brazdil0debae72015-11-12 18:37:00 +00003596
3597 if (fallthrough_target.IsLinked()) {
3598 __ Bind(&fallthrough_target);
3599 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003600}
3601
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003602void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3603 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003604 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003605 locations->SetInAt(0, Location::RequiresRegister());
3606 }
3607}
3608
3609void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003610 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3611 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003612 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3613 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3614 true_target = nullptr;
3615 }
3616 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3617 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3618 false_target = nullptr;
3619 }
David Brazdil0debae72015-11-12 18:37:00 +00003620 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003621}
3622
3623void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3624 LocationSummary* locations = new (GetGraph()->GetArena())
3625 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003626 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003627 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003628 locations->SetInAt(0, Location::RequiresRegister());
3629 }
3630}
3631
3632void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003633 SlowPathCodeARM64* slow_path =
3634 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003635 GenerateTestAndBranch(deoptimize,
3636 /* condition_input_index */ 0,
3637 slow_path->GetEntryLabel(),
3638 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003639}
3640
Mingyao Yang063fc772016-08-02 11:02:54 -07003641void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3642 LocationSummary* locations = new (GetGraph()->GetArena())
3643 LocationSummary(flag, LocationSummary::kNoCall);
3644 locations->SetOut(Location::RequiresRegister());
3645}
3646
3647void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3648 __ Ldr(OutputRegister(flag),
3649 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3650}
3651
David Brazdilc0b601b2016-02-08 14:20:45 +00003652static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3653 return condition->IsCondition() &&
3654 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3655}
3656
Alexandre Rames880f1192016-06-13 16:04:50 +01003657static inline Condition GetConditionForSelect(HCondition* condition) {
3658 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003659 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3660 : ARM64Condition(cond);
3661}
3662
David Brazdil74eb1b22015-12-14 11:44:01 +00003663void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3664 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003665 if (Primitive::IsFloatingPointType(select->GetType())) {
3666 locations->SetInAt(0, Location::RequiresFpuRegister());
3667 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003668 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003669 } else {
3670 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3671 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3672 bool is_true_value_constant = cst_true_value != nullptr;
3673 bool is_false_value_constant = cst_false_value != nullptr;
3674 // Ask VIXL whether we should synthesize constants in registers.
3675 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3676 Operand true_op = is_true_value_constant ?
3677 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3678 Operand false_op = is_false_value_constant ?
3679 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3680 bool true_value_in_register = false;
3681 bool false_value_in_register = false;
3682 MacroAssembler::GetCselSynthesisInformation(
3683 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3684 true_value_in_register |= !is_true_value_constant;
3685 false_value_in_register |= !is_false_value_constant;
3686
3687 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3688 : Location::ConstantLocation(cst_true_value));
3689 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3690 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003691 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003692 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003693
David Brazdil74eb1b22015-12-14 11:44:01 +00003694 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3695 locations->SetInAt(2, Location::RequiresRegister());
3696 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003697}
3698
3699void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003700 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003701 Condition csel_cond;
3702
3703 if (IsBooleanValueOrMaterializedCondition(cond)) {
3704 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003705 // Use the condition flags set by the previous instruction.
3706 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003707 } else {
3708 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003709 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003710 }
3711 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003712 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003713 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003714 } else {
3715 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003716 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003717 }
3718
Alexandre Rames880f1192016-06-13 16:04:50 +01003719 if (Primitive::IsFloatingPointType(select->GetType())) {
3720 __ Fcsel(OutputFPRegister(select),
3721 InputFPRegisterAt(select, 1),
3722 InputFPRegisterAt(select, 0),
3723 csel_cond);
3724 } else {
3725 __ Csel(OutputRegister(select),
3726 InputOperandAt(select, 1),
3727 InputOperandAt(select, 0),
3728 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003729 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003730}
3731
David Srbecky0cf44932015-12-09 14:09:59 +00003732void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3733 new (GetGraph()->GetArena()) LocationSummary(info);
3734}
3735
David Srbeckyd28f4a02016-03-14 17:14:24 +00003736void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3737 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003738}
3739
3740void CodeGeneratorARM64::GenerateNop() {
3741 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003742}
3743
Alexandre Rames5319def2014-10-23 10:03:10 +01003744void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003745 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003746}
3747
3748void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003749 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003750}
3751
3752void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003753 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003754}
3755
3756void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003757 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003758}
3759
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003760// Temp is used for read barrier.
3761static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3762 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003763 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003764 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3765 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3766 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3767 return 1;
3768 }
3769 return 0;
3770}
3771
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003772// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003773// interface pointer, one for loading the current interface.
3774// The other checks have one temp for loading the object's class.
3775static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3776 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3777 return 3;
3778 }
3779 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003780}
3781
Alexandre Rames67555f72014-11-18 10:55:16 +00003782void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003783 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003784 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003785 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003786 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003787 case TypeCheckKind::kExactCheck:
3788 case TypeCheckKind::kAbstractClassCheck:
3789 case TypeCheckKind::kClassHierarchyCheck:
3790 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003791 call_kind =
3792 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003793 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003794 break;
3795 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003796 case TypeCheckKind::kUnresolvedCheck:
3797 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003798 call_kind = LocationSummary::kCallOnSlowPath;
3799 break;
3800 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003801
Alexandre Rames67555f72014-11-18 10:55:16 +00003802 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003803 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003804 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003805 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003806 locations->SetInAt(0, Location::RequiresRegister());
3807 locations->SetInAt(1, Location::RequiresRegister());
3808 // The "out" register is used as a temporary, so it overlaps with the inputs.
3809 // Note that TypeCheckSlowPathARM64 uses this register too.
3810 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003811 // Add temps if necessary for read barriers.
3812 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003813}
3814
3815void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003816 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003817 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003818 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003819 Register obj = InputRegisterAt(instruction, 0);
3820 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003821 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003822 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003823 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3824 DCHECK_LE(num_temps, 1u);
3825 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003826 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3827 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3828 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3829 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003830
Scott Wakeling97c72b72016-06-24 16:19:36 +01003831 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003832 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003833
3834 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003835 // Avoid null check if we know `obj` is not null.
3836 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003837 __ Cbz(obj, &zero);
3838 }
3839
Roland Levillain44015862016-01-22 11:47:17 +00003840 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003841 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003842 // /* HeapReference<Class> */ out = obj->klass_
3843 GenerateReferenceLoadTwoRegisters(instruction,
3844 out_loc,
3845 obj_loc,
3846 class_offset,
3847 maybe_temp_loc,
3848 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003849 __ Cmp(out, cls);
3850 __ Cset(out, eq);
3851 if (zero.IsLinked()) {
3852 __ B(&done);
3853 }
3854 break;
3855 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003856
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003857 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003858 // /* HeapReference<Class> */ out = obj->klass_
3859 GenerateReferenceLoadTwoRegisters(instruction,
3860 out_loc,
3861 obj_loc,
3862 class_offset,
3863 maybe_temp_loc,
3864 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003865 // If the class is abstract, we eagerly fetch the super class of the
3866 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003867 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003868 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003869 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003870 GenerateReferenceLoadOneRegister(instruction,
3871 out_loc,
3872 super_offset,
3873 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003874 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003875 // If `out` is null, we use it for the result, and jump to `done`.
3876 __ Cbz(out, &done);
3877 __ Cmp(out, cls);
3878 __ B(ne, &loop);
3879 __ Mov(out, 1);
3880 if (zero.IsLinked()) {
3881 __ B(&done);
3882 }
3883 break;
3884 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003885
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003886 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003887 // /* HeapReference<Class> */ out = obj->klass_
3888 GenerateReferenceLoadTwoRegisters(instruction,
3889 out_loc,
3890 obj_loc,
3891 class_offset,
3892 maybe_temp_loc,
3893 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003894 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003895 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003896 __ Bind(&loop);
3897 __ Cmp(out, cls);
3898 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003899 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003900 GenerateReferenceLoadOneRegister(instruction,
3901 out_loc,
3902 super_offset,
3903 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003904 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003905 __ Cbnz(out, &loop);
3906 // If `out` is null, we use it for the result, and jump to `done`.
3907 __ B(&done);
3908 __ Bind(&success);
3909 __ Mov(out, 1);
3910 if (zero.IsLinked()) {
3911 __ B(&done);
3912 }
3913 break;
3914 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003915
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003916 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003917 // /* HeapReference<Class> */ out = obj->klass_
3918 GenerateReferenceLoadTwoRegisters(instruction,
3919 out_loc,
3920 obj_loc,
3921 class_offset,
3922 maybe_temp_loc,
3923 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003924 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003925 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003926 __ Cmp(out, cls);
3927 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003928 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003929 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003930 GenerateReferenceLoadOneRegister(instruction,
3931 out_loc,
3932 component_offset,
3933 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003934 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003935 // If `out` is null, we use it for the result, and jump to `done`.
3936 __ Cbz(out, &done);
3937 __ Ldrh(out, HeapOperand(out, primitive_offset));
3938 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3939 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003940 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003941 __ Mov(out, 1);
3942 __ B(&done);
3943 break;
3944 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003945
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003946 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003947 // No read barrier since the slow path will retry upon failure.
3948 // /* HeapReference<Class> */ out = obj->klass_
3949 GenerateReferenceLoadTwoRegisters(instruction,
3950 out_loc,
3951 obj_loc,
3952 class_offset,
3953 maybe_temp_loc,
3954 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003955 __ Cmp(out, cls);
3956 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003957 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3958 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003959 codegen_->AddSlowPath(slow_path);
3960 __ B(ne, slow_path->GetEntryLabel());
3961 __ Mov(out, 1);
3962 if (zero.IsLinked()) {
3963 __ B(&done);
3964 }
3965 break;
3966 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003967
Calin Juravle98893e12015-10-02 21:05:03 +01003968 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003969 case TypeCheckKind::kInterfaceCheck: {
3970 // Note that we indeed only call on slow path, but we always go
3971 // into the slow path for the unresolved and interface check
3972 // cases.
3973 //
3974 // We cannot directly call the InstanceofNonTrivial runtime
3975 // entry point without resorting to a type checking slow path
3976 // here (i.e. by calling InvokeRuntime directly), as it would
3977 // require to assign fixed registers for the inputs of this
3978 // HInstanceOf instruction (following the runtime calling
3979 // convention), which might be cluttered by the potential first
3980 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003981 //
3982 // TODO: Introduce a new runtime entry point taking the object
3983 // to test (instead of its class) as argument, and let it deal
3984 // with the read barrier issues. This will let us refactor this
3985 // case of the `switch` code as it was previously (with a direct
3986 // call to the runtime not using a type checking slow path).
3987 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003988 DCHECK(locations->OnlyCallsOnSlowPath());
3989 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3990 /* is_fatal */ false);
3991 codegen_->AddSlowPath(slow_path);
3992 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003993 if (zero.IsLinked()) {
3994 __ B(&done);
3995 }
3996 break;
3997 }
3998 }
3999
4000 if (zero.IsLinked()) {
4001 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004002 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004003 }
4004
4005 if (done.IsLinked()) {
4006 __ Bind(&done);
4007 }
4008
4009 if (slow_path != nullptr) {
4010 __ Bind(slow_path->GetExitLabel());
4011 }
4012}
4013
4014void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4015 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4016 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4017
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004018 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4019 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004020 case TypeCheckKind::kExactCheck:
4021 case TypeCheckKind::kAbstractClassCheck:
4022 case TypeCheckKind::kClassHierarchyCheck:
4023 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004024 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4025 LocationSummary::kCallOnSlowPath :
4026 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004027 break;
4028 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004029 case TypeCheckKind::kUnresolvedCheck:
4030 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004031 call_kind = LocationSummary::kCallOnSlowPath;
4032 break;
4033 }
4034
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004035 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4036 locations->SetInAt(0, Location::RequiresRegister());
4037 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004038 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4039 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004040}
4041
4042void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004043 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004044 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004045 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004046 Register obj = InputRegisterAt(instruction, 0);
4047 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004048 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4049 DCHECK_GE(num_temps, 1u);
4050 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004051 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004052 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4053 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004054 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004055 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4056 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4057 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4058 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4059 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4060 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4061 const uint32_t object_array_data_offset =
4062 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004063
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004064 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004065 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4066 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4067 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004068 if (!kEmitCompilerReadBarrier) {
4069 is_type_check_slow_path_fatal =
4070 (type_check_kind == TypeCheckKind::kExactCheck ||
4071 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4072 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4073 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4074 !instruction->CanThrowIntoCatchBlock();
4075 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004076 SlowPathCodeARM64* type_check_slow_path =
4077 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4078 is_type_check_slow_path_fatal);
4079 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004080
Scott Wakeling97c72b72016-06-24 16:19:36 +01004081 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004082 // Avoid null check if we know obj is not null.
4083 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004084 __ Cbz(obj, &done);
4085 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004086
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004087 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004088 case TypeCheckKind::kExactCheck:
4089 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004090 // /* HeapReference<Class> */ temp = obj->klass_
4091 GenerateReferenceLoadTwoRegisters(instruction,
4092 temp_loc,
4093 obj_loc,
4094 class_offset,
4095 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004096 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004097
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004098 __ Cmp(temp, cls);
4099 // Jump to slow path for throwing the exception or doing a
4100 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004101 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004102 break;
4103 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004104
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004105 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004106 // /* HeapReference<Class> */ temp = obj->klass_
4107 GenerateReferenceLoadTwoRegisters(instruction,
4108 temp_loc,
4109 obj_loc,
4110 class_offset,
4111 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004112 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004113
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004114 // If the class is abstract, we eagerly fetch the super class of the
4115 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004116 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004117 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004118 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004119 GenerateReferenceLoadOneRegister(instruction,
4120 temp_loc,
4121 super_offset,
4122 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004123 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004124
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004125 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4126 // exception.
4127 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4128 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004129 __ Cmp(temp, cls);
4130 __ B(ne, &loop);
4131 break;
4132 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004133
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004134 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004135 // /* HeapReference<Class> */ temp = obj->klass_
4136 GenerateReferenceLoadTwoRegisters(instruction,
4137 temp_loc,
4138 obj_loc,
4139 class_offset,
4140 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004141 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004142
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004143 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004144 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004145 __ Bind(&loop);
4146 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004147 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004148
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004149 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004150 GenerateReferenceLoadOneRegister(instruction,
4151 temp_loc,
4152 super_offset,
4153 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004154 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004155
4156 // If the class reference currently in `temp` is not null, jump
4157 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004158 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004159 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004160 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004161 break;
4162 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004163
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004164 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004165 // /* HeapReference<Class> */ temp = obj->klass_
4166 GenerateReferenceLoadTwoRegisters(instruction,
4167 temp_loc,
4168 obj_loc,
4169 class_offset,
4170 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004171 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004172
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004173 // Do an exact check.
4174 __ Cmp(temp, cls);
4175 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004176
4177 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004178 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004179 GenerateReferenceLoadOneRegister(instruction,
4180 temp_loc,
4181 component_offset,
4182 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004183 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004184
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004185 // If the component type is null, jump to the slow path to throw the exception.
4186 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4187 // Otherwise, the object is indeed an array. Further check that this component type is not a
4188 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004189 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4190 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004191 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004192 break;
4193 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004194
Calin Juravle98893e12015-10-02 21:05:03 +01004195 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004196 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004197 //
4198 // We cannot directly call the CheckCast runtime entry point
4199 // without resorting to a type checking slow path here (i.e. by
4200 // calling InvokeRuntime directly), as it would require to
4201 // assign fixed registers for the inputs of this HInstanceOf
4202 // instruction (following the runtime calling convention), which
4203 // might be cluttered by the potential first read barrier
4204 // emission at the beginning of this method.
4205 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004206 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004207 case TypeCheckKind::kInterfaceCheck: {
4208 // /* HeapReference<Class> */ temp = obj->klass_
4209 GenerateReferenceLoadTwoRegisters(instruction,
4210 temp_loc,
4211 obj_loc,
4212 class_offset,
4213 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004214 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004215
4216 // /* HeapReference<Class> */ temp = temp->iftable_
4217 GenerateReferenceLoadTwoRegisters(instruction,
4218 temp_loc,
4219 temp_loc,
4220 iftable_offset,
4221 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004222 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004223 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004224 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004225 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004226 vixl::aarch64::Label start_loop;
4227 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004228 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004229 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4230 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004231 // Go to next interface.
4232 __ Add(temp, temp, 2 * kHeapReferenceSize);
4233 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004234 // Compare the classes and continue the loop if they do not match.
4235 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4236 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004237 break;
4238 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004239 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004240 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004241
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004242 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004243}
4244
Alexandre Rames5319def2014-10-23 10:03:10 +01004245void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4246 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4247 locations->SetOut(Location::ConstantLocation(constant));
4248}
4249
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004250void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004251 // Will be generated at use site.
4252}
4253
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004254void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4255 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4256 locations->SetOut(Location::ConstantLocation(constant));
4257}
4258
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004259void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004260 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004261}
4262
Calin Juravle175dc732015-08-25 15:42:32 +01004263void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4264 // The trampoline uses the same calling convention as dex calling conventions,
4265 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4266 // the method_idx.
4267 HandleInvoke(invoke);
4268}
4269
4270void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4271 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4272}
4273
Alexandre Rames5319def2014-10-23 10:03:10 +01004274void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004275 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004276 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004277}
4278
Alexandre Rames67555f72014-11-18 10:55:16 +00004279void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4280 HandleInvoke(invoke);
4281}
4282
4283void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4284 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004285 LocationSummary* locations = invoke->GetLocations();
4286 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004287 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004288 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004289 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004290
4291 // The register ip1 is required to be used for the hidden argument in
4292 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004293 MacroAssembler* masm = GetVIXLAssembler();
4294 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004295 scratch_scope.Exclude(ip1);
4296 __ Mov(ip1, invoke->GetDexMethodIndex());
4297
Artem Serov914d7a82017-02-07 14:33:49 +00004298 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004299 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004300 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004301 {
4302 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4303 // /* HeapReference<Class> */ temp = temp->klass_
4304 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4305 codegen_->MaybeRecordImplicitNullCheck(invoke);
4306 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004307 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004308 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004309 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004310 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004311 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004312 }
Artem Serov914d7a82017-02-07 14:33:49 +00004313
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004314 // Instead of simply (possibly) unpoisoning `temp` here, we should
4315 // emit a read barrier for the previous class reference load.
4316 // However this is not required in practice, as this is an
4317 // intermediate/temporary reference and because the current
4318 // concurrent copying collector keeps the from-space memory
4319 // intact/accessible until the end of the marking phase (the
4320 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004321 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004322 __ Ldr(temp,
4323 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4324 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004325 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004326 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004327 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004328 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004329 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004330
4331 {
4332 // Ensure the pc position is recorded immediately after the `blr` instruction.
4333 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4334
4335 // lr();
4336 __ blr(lr);
4337 DCHECK(!codegen_->IsLeafMethod());
4338 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4339 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004340}
4341
4342void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004343 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004344 if (intrinsic.TryDispatch(invoke)) {
4345 return;
4346 }
4347
Alexandre Rames67555f72014-11-18 10:55:16 +00004348 HandleInvoke(invoke);
4349}
4350
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004351void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004352 // Explicit clinit checks triggered by static invokes must have been pruned by
4353 // art::PrepareForRegisterAllocation.
4354 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004355
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004356 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004357 if (intrinsic.TryDispatch(invoke)) {
4358 return;
4359 }
4360
Alexandre Rames67555f72014-11-18 10:55:16 +00004361 HandleInvoke(invoke);
4362}
4363
Andreas Gampe878d58c2015-01-15 23:24:00 -08004364static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4365 if (invoke->GetLocations()->Intrinsified()) {
4366 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4367 intrinsic.Dispatch(invoke);
4368 return true;
4369 }
4370 return false;
4371}
4372
Vladimir Markodc151b22015-10-15 18:02:30 +01004373HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4374 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004375 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004376 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004377 return desired_dispatch_info;
4378}
4379
TatWai Chongd8c052a2016-11-02 16:12:48 +08004380Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4381 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004382 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004383 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4384 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004385 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4386 uint32_t offset =
4387 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004388 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004389 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004390 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004391 }
Vladimir Marko58155012015-08-19 12:49:41 +00004392 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004393 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004394 break;
4395 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4396 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004397 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004398 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004399 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4400 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004401 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004402 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004403 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004404 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004405 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004406 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004407 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004408 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004409 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004410 }
Vladimir Marko58155012015-08-19 12:49:41 +00004411 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004412 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004413 Register reg = XRegisterFrom(temp);
4414 Register method_reg;
4415 if (current_method.IsRegister()) {
4416 method_reg = XRegisterFrom(current_method);
4417 } else {
4418 DCHECK(invoke->GetLocations()->Intrinsified());
4419 DCHECK(!current_method.IsValid());
4420 method_reg = reg;
4421 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4422 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004423
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004424 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004425 __ Ldr(reg.X(),
4426 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004427 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004428 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004429 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4430 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004431 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4432 break;
4433 }
4434 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004435 return callee_method;
4436}
4437
4438void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4439 // All registers are assumed to be correctly set up.
4440 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004441
4442 switch (invoke->GetCodePtrLocation()) {
4443 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4444 __ Bl(&frame_entry_label_);
4445 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004446 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4447 // LR = callee_method->entry_point_from_quick_compiled_code_;
4448 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004449 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004450 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004451 {
4452 // To ensure that the pc position is recorded immediately after the `blr` instruction
4453 // BLR must be the last instruction emitted in this function.
4454 // Recording the pc will occur right after returning from this function.
4455 ExactAssemblyScope eas(GetVIXLAssembler(),
4456 kInstructionSize,
4457 CodeBufferCheckScope::kExactSize);
4458 // lr()
4459 __ blr(lr);
4460 }
Vladimir Marko58155012015-08-19 12:49:41 +00004461 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004462 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004463
Andreas Gampe878d58c2015-01-15 23:24:00 -08004464 DCHECK(!IsLeafMethod());
4465}
4466
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004467void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004468 // Use the calling convention instead of the location of the receiver, as
4469 // intrinsics may have put the receiver in a different register. In the intrinsics
4470 // slow path, the arguments have been moved to the right place, so here we are
4471 // guaranteed that the receiver is the first register of the calling convention.
4472 InvokeDexCallingConvention calling_convention;
4473 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004474 Register temp = XRegisterFrom(temp_in);
4475 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4476 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4477 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004478 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004479
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004480 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004481
4482 {
4483 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4484 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4485 // /* HeapReference<Class> */ temp = receiver->klass_
4486 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4487 MaybeRecordImplicitNullCheck(invoke);
4488 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004489 // Instead of simply (possibly) unpoisoning `temp` here, we should
4490 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004491 // intermediate/temporary reference and because the current
4492 // concurrent copying collector keeps the from-space memory
4493 // intact/accessible until the end of the marking phase (the
4494 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004495 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4496 // temp = temp->GetMethodAt(method_offset);
4497 __ Ldr(temp, MemOperand(temp, method_offset));
4498 // lr = temp->GetEntryPoint();
4499 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004500 {
4501 // To ensure that the pc position is recorded immediately after the `blr` instruction
4502 // BLR should be the last instruction emitted in this function.
4503 // Recording the pc will occur right after returning from this function.
4504 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4505 // lr();
4506 __ blr(lr);
4507 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004508}
4509
Orion Hodsonac141392017-01-13 11:53:47 +00004510void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4511 HandleInvoke(invoke);
4512}
4513
4514void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4515 codegen_->GenerateInvokePolymorphicCall(invoke);
4516}
4517
Scott Wakeling97c72b72016-06-24 16:19:36 +01004518vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4519 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004520 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004521 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004522 return
4523 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004524}
4525
Scott Wakeling97c72b72016-06-24 16:19:36 +01004526vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4527 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004528 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004529 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004530 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004531}
4532
Vladimir Marko1998cd02017-01-13 13:02:58 +00004533vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4534 const DexFile& dex_file,
4535 dex::TypeIndex type_index,
4536 vixl::aarch64::Label* adrp_label) {
4537 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4538}
4539
Scott Wakeling97c72b72016-06-24 16:19:36 +01004540vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4541 const DexFile& dex_file,
4542 uint32_t element_offset,
4543 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004544 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4545}
4546
Scott Wakeling97c72b72016-06-24 16:19:36 +01004547vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4548 const DexFile& dex_file,
4549 uint32_t offset_or_index,
4550 vixl::aarch64::Label* adrp_label,
4551 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004552 // Add a patch entry and return the label.
4553 patches->emplace_back(dex_file, offset_or_index);
4554 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004555 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004556 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4557 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4558 return label;
4559}
4560
Scott Wakeling97c72b72016-06-24 16:19:36 +01004561vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004562 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004563 return boot_image_string_patches_.GetOrCreate(
4564 StringReference(&dex_file, string_index),
4565 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4566}
4567
Scott Wakeling97c72b72016-06-24 16:19:36 +01004568vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004569 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004570 return boot_image_type_patches_.GetOrCreate(
4571 TypeReference(&dex_file, type_index),
4572 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4573}
4574
Scott Wakeling97c72b72016-06-24 16:19:36 +01004575vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4576 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004577 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004578}
4579
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004580vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004581 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4582 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4583 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004584 return jit_string_patches_.GetOrCreate(
4585 StringReference(&dex_file, string_index),
4586 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4587}
4588
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004589vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004590 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4591 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4592 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004593 return jit_class_patches_.GetOrCreate(
4594 TypeReference(&dex_file, type_index),
4595 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4596}
4597
Vladimir Markoaad75c62016-10-03 08:46:48 +00004598void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4599 vixl::aarch64::Register reg) {
4600 DCHECK(reg.IsX());
4601 SingleEmissionCheckScope guard(GetVIXLAssembler());
4602 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004603 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004604}
4605
4606void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4607 vixl::aarch64::Register out,
4608 vixl::aarch64::Register base) {
4609 DCHECK(out.IsX());
4610 DCHECK(base.IsX());
4611 SingleEmissionCheckScope guard(GetVIXLAssembler());
4612 __ Bind(fixup_label);
4613 __ add(out, base, Operand(/* offset placeholder */ 0));
4614}
4615
4616void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4617 vixl::aarch64::Register out,
4618 vixl::aarch64::Register base) {
4619 DCHECK(base.IsX());
4620 SingleEmissionCheckScope guard(GetVIXLAssembler());
4621 __ Bind(fixup_label);
4622 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4623}
4624
4625template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4626inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4627 const ArenaDeque<PcRelativePatchInfo>& infos,
4628 ArenaVector<LinkerPatch>* linker_patches) {
4629 for (const PcRelativePatchInfo& info : infos) {
4630 linker_patches->push_back(Factory(info.label.GetLocation(),
4631 &info.target_dex_file,
4632 info.pc_insn_label->GetLocation(),
4633 info.offset_or_index));
4634 }
4635}
4636
Vladimir Marko58155012015-08-19 12:49:41 +00004637void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4638 DCHECK(linker_patches->empty());
4639 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004640 pc_relative_dex_cache_patches_.size() +
4641 boot_image_string_patches_.size() +
4642 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004643 boot_image_type_patches_.size() +
4644 pc_relative_type_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00004645 type_bss_entry_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004646 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004647 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004648 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004649 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004650 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004651 info.offset_or_index));
4652 }
4653 for (const auto& entry : boot_image_string_patches_) {
4654 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004655 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4656 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004657 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004658 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004659 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004660 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004661 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004662 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4663 linker_patches);
4664 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004665 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4666 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004667 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4668 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004669 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004670 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4671 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004672 for (const auto& entry : boot_image_type_patches_) {
4673 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004674 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4675 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004676 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004677 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004678 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004679 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004680}
4681
Scott Wakeling97c72b72016-06-24 16:19:36 +01004682vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004683 Uint32ToLiteralMap* map) {
4684 return map->GetOrCreate(
4685 value,
4686 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4687}
4688
Scott Wakeling97c72b72016-06-24 16:19:36 +01004689vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004690 return uint64_literals_.GetOrCreate(
4691 value,
4692 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004693}
4694
Scott Wakeling97c72b72016-06-24 16:19:36 +01004695vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004696 MethodReference target_method,
4697 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004698 return map->GetOrCreate(
4699 target_method,
4700 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004701}
4702
Andreas Gampe878d58c2015-01-15 23:24:00 -08004703void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004704 // Explicit clinit checks triggered by static invokes must have been pruned by
4705 // art::PrepareForRegisterAllocation.
4706 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004707
Andreas Gampe878d58c2015-01-15 23:24:00 -08004708 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4709 return;
4710 }
4711
Artem Serov914d7a82017-02-07 14:33:49 +00004712 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4713 // are no pools emitted.
4714 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004715 LocationSummary* locations = invoke->GetLocations();
4716 codegen_->GenerateStaticOrDirectCall(
4717 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004718 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004719}
4720
4721void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004722 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4723 return;
4724 }
4725
Artem Serov914d7a82017-02-07 14:33:49 +00004726 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4727 // are no pools emitted.
4728 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004729 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004730 DCHECK(!codegen_->IsLeafMethod());
4731 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4732}
4733
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004734HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4735 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004736 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004737 case HLoadClass::LoadKind::kInvalid:
4738 LOG(FATAL) << "UNREACHABLE";
4739 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004740 case HLoadClass::LoadKind::kReferrersClass:
4741 break;
4742 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4743 DCHECK(!GetCompilerOptions().GetCompilePic());
4744 break;
4745 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4746 DCHECK(GetCompilerOptions().GetCompilePic());
4747 break;
4748 case HLoadClass::LoadKind::kBootImageAddress:
4749 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004750 case HLoadClass::LoadKind::kBssEntry:
4751 DCHECK(!Runtime::Current()->UseJitCompilation());
4752 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004753 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004754 DCHECK(Runtime::Current()->UseJitCompilation());
4755 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004756 case HLoadClass::LoadKind::kDexCacheViaMethod:
4757 break;
4758 }
4759 return desired_class_load_kind;
4760}
4761
Alexandre Rames67555f72014-11-18 10:55:16 +00004762void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004763 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4764 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004765 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004766 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004767 cls,
4768 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004769 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004770 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004771 return;
4772 }
Vladimir Marko41559982017-01-06 14:04:23 +00004773 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004774
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004775 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4776 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004777 ? LocationSummary::kCallOnSlowPath
4778 : LocationSummary::kNoCall;
4779 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004780 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004781 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004782 }
4783
Vladimir Marko41559982017-01-06 14:04:23 +00004784 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004785 locations->SetInAt(0, Location::RequiresRegister());
4786 }
4787 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004788 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4789 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4790 // Rely on the type resolution or initialization and marking to save everything we need.
4791 // Note that IP0 may be clobbered by saving/restoring the live register (only one thanks
4792 // to the custom calling convention) or by marking, so we shall use IP1.
4793 RegisterSet caller_saves = RegisterSet::Empty();
4794 InvokeRuntimeCallingConvention calling_convention;
4795 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4796 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4797 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4798 Primitive::kPrimNot).GetCode());
4799 locations->SetCustomSlowPathCallerSaves(caller_saves);
4800 } else {
4801 // For non-Baker read barrier we have a temp-clobbering call.
4802 }
4803 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004804}
4805
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004806// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4807// move.
4808void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004809 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4810 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4811 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004812 return;
4813 }
Vladimir Marko41559982017-01-06 14:04:23 +00004814 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004815
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004816 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004817 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004818 Register bss_entry_temp;
4819 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004820
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004821 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4822 ? kWithoutReadBarrier
4823 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004824 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004825 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004826 case HLoadClass::LoadKind::kReferrersClass: {
4827 DCHECK(!cls->CanCallRuntime());
4828 DCHECK(!cls->MustGenerateClinitCheck());
4829 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4830 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004831 GenerateGcRootFieldLoad(cls,
4832 out_loc,
4833 current_method,
4834 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004835 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004836 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004837 break;
4838 }
4839 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004840 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004841 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4842 cls->GetTypeIndex()));
4843 break;
4844 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004845 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004846 // Add ADRP with its PC-relative type patch.
4847 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004848 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004849 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004850 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004851 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004852 vixl::aarch64::Label* add_label =
4853 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004854 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004855 break;
4856 }
4857 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004858 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004859 uint32_t address = dchecked_integral_cast<uint32_t>(
4860 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4861 DCHECK_NE(address, 0u);
4862 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004863 break;
4864 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004865 case HLoadClass::LoadKind::kBssEntry: {
4866 // Add ADRP with its PC-relative Class .bss entry patch.
4867 const DexFile& dex_file = cls->GetDexFile();
4868 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markoea4c1262017-02-06 19:59:33 +00004869 // We can go to slow path even with non-zero reference and in that case marking
4870 // can clobber IP0, so we need to use IP1 which shall be preserved.
4871 bss_entry_temp = ip1;
4872 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4873 temps.Exclude(bss_entry_temp);
4874 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4875 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004876 // Add LDR with its PC-relative Class patch.
4877 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004878 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004879 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4880 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004881 out_loc,
4882 bss_entry_temp,
4883 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004884 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004885 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004886 generate_null_check = true;
4887 break;
4888 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004889 case HLoadClass::LoadKind::kJitTableAddress: {
4890 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4891 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004892 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004893 GenerateGcRootFieldLoad(cls,
4894 out_loc,
4895 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004896 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004897 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004898 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004899 break;
4900 }
Vladimir Marko41559982017-01-06 14:04:23 +00004901 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004902 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004903 LOG(FATAL) << "UNREACHABLE";
4904 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004905 }
4906
Vladimir Markoea4c1262017-02-06 19:59:33 +00004907 bool do_clinit = cls->MustGenerateClinitCheck();
4908 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004909 DCHECK(cls->CanCallRuntime());
4910 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004911 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004912 codegen_->AddSlowPath(slow_path);
4913 if (generate_null_check) {
4914 __ Cbz(out, slow_path->GetEntryLabel());
4915 }
4916 if (cls->MustGenerateClinitCheck()) {
4917 GenerateClassInitializationCheck(slow_path, out);
4918 } else {
4919 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004920 }
4921 }
4922}
4923
David Brazdilcb1c0552015-08-04 16:22:25 +01004924static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004925 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004926}
4927
Alexandre Rames67555f72014-11-18 10:55:16 +00004928void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4929 LocationSummary* locations =
4930 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4931 locations->SetOut(Location::RequiresRegister());
4932}
4933
4934void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004935 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4936}
4937
4938void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4939 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4940}
4941
4942void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4943 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004944}
4945
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004946HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4947 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004948 switch (desired_string_load_kind) {
4949 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4950 DCHECK(!GetCompilerOptions().GetCompilePic());
4951 break;
4952 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4953 DCHECK(GetCompilerOptions().GetCompilePic());
4954 break;
4955 case HLoadString::LoadKind::kBootImageAddress:
4956 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004957 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004958 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004959 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004960 case HLoadString::LoadKind::kJitTableAddress:
4961 DCHECK(Runtime::Current()->UseJitCompilation());
4962 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004963 case HLoadString::LoadKind::kDexCacheViaMethod:
4964 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004965 }
4966 return desired_string_load_kind;
4967}
4968
Alexandre Rames67555f72014-11-18 10:55:16 +00004969void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004970 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004971 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004972 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004973 InvokeRuntimeCallingConvention calling_convention;
4974 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4975 } else {
4976 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004977 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4978 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00004979 // Rely on the pResolveString and marking to save everything we need.
4980 // Note that IP0 may be clobbered by saving/restoring the live register (only one thanks
4981 // to the custom calling convention) or by marking, so we shall use IP1.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004982 RegisterSet caller_saves = RegisterSet::Empty();
4983 InvokeRuntimeCallingConvention calling_convention;
4984 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4985 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4986 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4987 Primitive::kPrimNot).GetCode());
4988 locations->SetCustomSlowPathCallerSaves(caller_saves);
4989 } else {
4990 // For non-Baker read barrier we have a temp-clobbering call.
4991 }
4992 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004993 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004994}
4995
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004996// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4997// move.
4998void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004999 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005000 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005001
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005002 switch (load->GetLoadKind()) {
5003 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005004 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5005 load->GetStringIndex()));
5006 return; // No dex cache slow path.
5007 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005008 // Add ADRP with its PC-relative String patch.
5009 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005010 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005011 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005012 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005013 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005014 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005015 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005016 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005017 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005018 return; // No dex cache slow path.
5019 }
5020 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005021 uint32_t address = dchecked_integral_cast<uint32_t>(
5022 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5023 DCHECK_NE(address, 0u);
5024 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005025 return; // No dex cache slow path.
5026 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005027 case HLoadString::LoadKind::kBssEntry: {
5028 // Add ADRP with its PC-relative String .bss entry patch.
5029 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005030 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005031 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005032 // We could use IP0 as the marking shall not clobber IP0 if the reference is null and
5033 // that's when we need the slow path. But let's not rely on such details and use IP1.
5034 Register temp = ip1;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005035 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005036 temps.Exclude(temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005037 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005038 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005039 // Add LDR with its PC-relative String patch.
5040 vixl::aarch64::Label* ldr_label =
5041 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005042 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005043 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005044 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005045 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005046 /* offset placeholder */ 0u,
5047 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005048 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005049 SlowPathCodeARM64* slow_path =
5050 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005051 codegen_->AddSlowPath(slow_path);
5052 __ Cbz(out.X(), slow_path->GetEntryLabel());
5053 __ Bind(slow_path->GetExitLabel());
5054 return;
5055 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005056 case HLoadString::LoadKind::kJitTableAddress: {
5057 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005058 load->GetStringIndex(),
5059 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005060 GenerateGcRootFieldLoad(load,
5061 out_loc,
5062 out.X(),
5063 /* offset */ 0,
5064 /* fixup_label */ nullptr,
5065 kCompilerReadBarrierOption);
5066 return;
5067 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005068 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005069 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005070 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005071
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005072 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005073 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005074 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005075 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005076 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5077 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005078}
5079
Alexandre Rames5319def2014-10-23 10:03:10 +01005080void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5081 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5082 locations->SetOut(Location::ConstantLocation(constant));
5083}
5084
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005085void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005086 // Will be generated at use site.
5087}
5088
Alexandre Rames67555f72014-11-18 10:55:16 +00005089void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5090 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005091 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005092 InvokeRuntimeCallingConvention calling_convention;
5093 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5094}
5095
5096void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005097 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005098 instruction,
5099 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005100 if (instruction->IsEnter()) {
5101 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5102 } else {
5103 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5104 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005105}
5106
Alexandre Rames42d641b2014-10-27 14:00:51 +00005107void LocationsBuilderARM64::VisitMul(HMul* mul) {
5108 LocationSummary* locations =
5109 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5110 switch (mul->GetResultType()) {
5111 case Primitive::kPrimInt:
5112 case Primitive::kPrimLong:
5113 locations->SetInAt(0, Location::RequiresRegister());
5114 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005115 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005116 break;
5117
5118 case Primitive::kPrimFloat:
5119 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005120 locations->SetInAt(0, Location::RequiresFpuRegister());
5121 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005122 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005123 break;
5124
5125 default:
5126 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5127 }
5128}
5129
5130void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5131 switch (mul->GetResultType()) {
5132 case Primitive::kPrimInt:
5133 case Primitive::kPrimLong:
5134 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5135 break;
5136
5137 case Primitive::kPrimFloat:
5138 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005139 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005140 break;
5141
5142 default:
5143 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5144 }
5145}
5146
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005147void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5148 LocationSummary* locations =
5149 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5150 switch (neg->GetResultType()) {
5151 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005152 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005153 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005154 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005155 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005156
5157 case Primitive::kPrimFloat:
5158 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005159 locations->SetInAt(0, Location::RequiresFpuRegister());
5160 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005161 break;
5162
5163 default:
5164 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5165 }
5166}
5167
5168void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5169 switch (neg->GetResultType()) {
5170 case Primitive::kPrimInt:
5171 case Primitive::kPrimLong:
5172 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5173 break;
5174
5175 case Primitive::kPrimFloat:
5176 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005177 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005178 break;
5179
5180 default:
5181 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5182 }
5183}
5184
5185void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5186 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005187 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005188 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005189 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005190 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5191 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005192}
5193
5194void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005195 // Note: if heap poisoning is enabled, the entry point takes cares
5196 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005197 QuickEntrypointEnum entrypoint =
5198 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5199 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005200 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005201}
5202
Alexandre Rames5319def2014-10-23 10:03:10 +01005203void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5204 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005205 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005206 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005207 if (instruction->IsStringAlloc()) {
5208 locations->AddTemp(LocationFrom(kArtMethodRegister));
5209 } else {
5210 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005211 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005212 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5213}
5214
5215void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005216 // Note: if heap poisoning is enabled, the entry point takes cares
5217 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005218 if (instruction->IsStringAlloc()) {
5219 // String is allocated through StringFactory. Call NewEmptyString entry point.
5220 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005221 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005222 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5223 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005224
5225 {
5226 // Ensure the pc position is recorded immediately after the `blr` instruction.
5227 ExactAssemblyScope eas(GetVIXLAssembler(),
5228 kInstructionSize,
5229 CodeBufferCheckScope::kExactSize);
5230 __ blr(lr);
5231 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5232 }
David Brazdil6de19382016-01-08 17:37:10 +00005233 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005234 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005235 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005236 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005237}
5238
5239void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5240 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005241 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005242 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005243}
5244
5245void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005246 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005247 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005248 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005249 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005250 break;
5251
5252 default:
5253 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5254 }
5255}
5256
David Brazdil66d126e2015-04-03 16:02:44 +01005257void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5258 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5259 locations->SetInAt(0, Location::RequiresRegister());
5260 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5261}
5262
5263void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005264 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005265}
5266
Alexandre Rames5319def2014-10-23 10:03:10 +01005267void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005268 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5269 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005270}
5271
Calin Juravle2ae48182016-03-16 14:05:09 +00005272void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5273 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005274 return;
5275 }
Artem Serov914d7a82017-02-07 14:33:49 +00005276 {
5277 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5278 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5279 Location obj = instruction->GetLocations()->InAt(0);
5280 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5281 RecordPcInfo(instruction, instruction->GetDexPc());
5282 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005283}
5284
Calin Juravle2ae48182016-03-16 14:05:09 +00005285void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005286 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005287 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005288
5289 LocationSummary* locations = instruction->GetLocations();
5290 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005291
5292 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005293}
5294
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005295void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005296 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005297}
5298
Alexandre Rames67555f72014-11-18 10:55:16 +00005299void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5300 HandleBinaryOp(instruction);
5301}
5302
5303void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5304 HandleBinaryOp(instruction);
5305}
5306
Alexandre Rames3e69f162014-12-10 10:36:50 +00005307void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5308 LOG(FATAL) << "Unreachable";
5309}
5310
5311void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5312 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5313}
5314
Alexandre Rames5319def2014-10-23 10:03:10 +01005315void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5317 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5318 if (location.IsStackSlot()) {
5319 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5320 } else if (location.IsDoubleStackSlot()) {
5321 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5322 }
5323 locations->SetOut(location);
5324}
5325
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005326void InstructionCodeGeneratorARM64::VisitParameterValue(
5327 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005328 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005329}
5330
5331void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5332 LocationSummary* locations =
5333 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005334 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005335}
5336
5337void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5338 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5339 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005340}
5341
5342void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5343 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005344 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005345 locations->SetInAt(i, Location::Any());
5346 }
5347 locations->SetOut(Location::Any());
5348}
5349
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005350void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005351 LOG(FATAL) << "Unreachable";
5352}
5353
Serban Constantinescu02164b32014-11-13 14:05:07 +00005354void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005355 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005356 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005357 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5358 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5360
5361 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005362 case Primitive::kPrimInt:
5363 case Primitive::kPrimLong:
5364 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005365 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005366 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5367 break;
5368
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005369 case Primitive::kPrimFloat:
5370 case Primitive::kPrimDouble: {
5371 InvokeRuntimeCallingConvention calling_convention;
5372 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5373 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5374 locations->SetOut(calling_convention.GetReturnLocation(type));
5375
5376 break;
5377 }
5378
Serban Constantinescu02164b32014-11-13 14:05:07 +00005379 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005380 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005381 }
5382}
5383
5384void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5385 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005386
Serban Constantinescu02164b32014-11-13 14:05:07 +00005387 switch (type) {
5388 case Primitive::kPrimInt:
5389 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005390 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005391 break;
5392 }
5393
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005394 case Primitive::kPrimFloat:
5395 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005396 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5397 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005398 if (type == Primitive::kPrimFloat) {
5399 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5400 } else {
5401 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5402 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005403 break;
5404 }
5405
Serban Constantinescu02164b32014-11-13 14:05:07 +00005406 default:
5407 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005408 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005409 }
5410}
5411
Calin Juravle27df7582015-04-17 19:12:31 +01005412void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5413 memory_barrier->SetLocations(nullptr);
5414}
5415
5416void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005417 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005418}
5419
Alexandre Rames5319def2014-10-23 10:03:10 +01005420void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5421 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5422 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005423 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005424}
5425
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005426void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005427 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005428}
5429
5430void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5431 instruction->SetLocations(nullptr);
5432}
5433
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005434void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005435 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005436}
5437
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005438void LocationsBuilderARM64::VisitRor(HRor* ror) {
5439 HandleBinaryOp(ror);
5440}
5441
5442void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5443 HandleBinaryOp(ror);
5444}
5445
Serban Constantinescu02164b32014-11-13 14:05:07 +00005446void LocationsBuilderARM64::VisitShl(HShl* shl) {
5447 HandleShift(shl);
5448}
5449
5450void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5451 HandleShift(shl);
5452}
5453
5454void LocationsBuilderARM64::VisitShr(HShr* shr) {
5455 HandleShift(shr);
5456}
5457
5458void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5459 HandleShift(shr);
5460}
5461
Alexandre Rames5319def2014-10-23 10:03:10 +01005462void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005463 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005464}
5465
5466void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005467 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005468}
5469
Alexandre Rames67555f72014-11-18 10:55:16 +00005470void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005471 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005472}
5473
5474void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005475 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005476}
5477
5478void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005479 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005480}
5481
Alexandre Rames67555f72014-11-18 10:55:16 +00005482void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005483 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005484}
5485
Calin Juravlee460d1d2015-09-29 04:52:17 +01005486void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5487 HUnresolvedInstanceFieldGet* instruction) {
5488 FieldAccessCallingConventionARM64 calling_convention;
5489 codegen_->CreateUnresolvedFieldLocationSummary(
5490 instruction, instruction->GetFieldType(), calling_convention);
5491}
5492
5493void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5494 HUnresolvedInstanceFieldGet* instruction) {
5495 FieldAccessCallingConventionARM64 calling_convention;
5496 codegen_->GenerateUnresolvedFieldAccess(instruction,
5497 instruction->GetFieldType(),
5498 instruction->GetFieldIndex(),
5499 instruction->GetDexPc(),
5500 calling_convention);
5501}
5502
5503void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5504 HUnresolvedInstanceFieldSet* instruction) {
5505 FieldAccessCallingConventionARM64 calling_convention;
5506 codegen_->CreateUnresolvedFieldLocationSummary(
5507 instruction, instruction->GetFieldType(), calling_convention);
5508}
5509
5510void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5511 HUnresolvedInstanceFieldSet* instruction) {
5512 FieldAccessCallingConventionARM64 calling_convention;
5513 codegen_->GenerateUnresolvedFieldAccess(instruction,
5514 instruction->GetFieldType(),
5515 instruction->GetFieldIndex(),
5516 instruction->GetDexPc(),
5517 calling_convention);
5518}
5519
5520void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5521 HUnresolvedStaticFieldGet* instruction) {
5522 FieldAccessCallingConventionARM64 calling_convention;
5523 codegen_->CreateUnresolvedFieldLocationSummary(
5524 instruction, instruction->GetFieldType(), calling_convention);
5525}
5526
5527void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5528 HUnresolvedStaticFieldGet* instruction) {
5529 FieldAccessCallingConventionARM64 calling_convention;
5530 codegen_->GenerateUnresolvedFieldAccess(instruction,
5531 instruction->GetFieldType(),
5532 instruction->GetFieldIndex(),
5533 instruction->GetDexPc(),
5534 calling_convention);
5535}
5536
5537void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5538 HUnresolvedStaticFieldSet* instruction) {
5539 FieldAccessCallingConventionARM64 calling_convention;
5540 codegen_->CreateUnresolvedFieldLocationSummary(
5541 instruction, instruction->GetFieldType(), calling_convention);
5542}
5543
5544void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5545 HUnresolvedStaticFieldSet* instruction) {
5546 FieldAccessCallingConventionARM64 calling_convention;
5547 codegen_->GenerateUnresolvedFieldAccess(instruction,
5548 instruction->GetFieldType(),
5549 instruction->GetFieldIndex(),
5550 instruction->GetDexPc(),
5551 calling_convention);
5552}
5553
Alexandre Rames5319def2014-10-23 10:03:10 +01005554void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005555 LocationSummary* locations =
5556 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005557 // In suspend check slow path, usually there are no caller-save registers at all.
5558 // If SIMD instructions are present, however, we force spilling all live SIMD
5559 // registers in full width (since the runtime only saves/restores lower part).
5560 locations->SetCustomSlowPathCallerSaves(
5561 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005562}
5563
5564void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005565 HBasicBlock* block = instruction->GetBlock();
5566 if (block->GetLoopInformation() != nullptr) {
5567 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5568 // The back edge will generate the suspend check.
5569 return;
5570 }
5571 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5572 // The goto will generate the suspend check.
5573 return;
5574 }
5575 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005576}
5577
Alexandre Rames67555f72014-11-18 10:55:16 +00005578void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5579 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005580 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005581 InvokeRuntimeCallingConvention calling_convention;
5582 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5583}
5584
5585void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005586 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005587 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005588}
5589
5590void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5591 LocationSummary* locations =
5592 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5593 Primitive::Type input_type = conversion->GetInputType();
5594 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005595 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005596 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5597 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5598 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5599 }
5600
Alexandre Rames542361f2015-01-29 16:57:31 +00005601 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005602 locations->SetInAt(0, Location::RequiresFpuRegister());
5603 } else {
5604 locations->SetInAt(0, Location::RequiresRegister());
5605 }
5606
Alexandre Rames542361f2015-01-29 16:57:31 +00005607 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005608 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5609 } else {
5610 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5611 }
5612}
5613
5614void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5615 Primitive::Type result_type = conversion->GetResultType();
5616 Primitive::Type input_type = conversion->GetInputType();
5617
5618 DCHECK_NE(input_type, result_type);
5619
Alexandre Rames542361f2015-01-29 16:57:31 +00005620 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005621 int result_size = Primitive::ComponentSize(result_type);
5622 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005623 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005624 Register output = OutputRegister(conversion);
5625 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005626 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005627 // 'int' values are used directly as W registers, discarding the top
5628 // bits, so we don't need to sign-extend and can just perform a move.
5629 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5630 // top 32 bits of the target register. We theoretically could leave those
5631 // bits unchanged, but we would have to make sure that no code uses a
5632 // 32bit input value as a 64bit value assuming that the top 32 bits are
5633 // zero.
5634 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005635 } else if (result_type == Primitive::kPrimChar ||
5636 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5637 __ Ubfx(output,
5638 output.IsX() ? source.X() : source.W(),
5639 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005640 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005641 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005642 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005643 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005644 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005645 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005646 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5647 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005648 } else if (Primitive::IsFloatingPointType(result_type) &&
5649 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005650 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5651 } else {
5652 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5653 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005654 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005655}
Alexandre Rames67555f72014-11-18 10:55:16 +00005656
Serban Constantinescu02164b32014-11-13 14:05:07 +00005657void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5658 HandleShift(ushr);
5659}
5660
5661void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5662 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005663}
5664
5665void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5666 HandleBinaryOp(instruction);
5667}
5668
5669void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5670 HandleBinaryOp(instruction);
5671}
5672
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005673void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005674 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005675 LOG(FATAL) << "Unreachable";
5676}
5677
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005678void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005679 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005680 LOG(FATAL) << "Unreachable";
5681}
5682
Mark Mendellfe57faa2015-09-18 09:26:15 -04005683// Simple implementation of packed switch - generate cascaded compare/jumps.
5684void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5685 LocationSummary* locations =
5686 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5687 locations->SetInAt(0, Location::RequiresRegister());
5688}
5689
5690void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5691 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005692 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005693 Register value_reg = InputRegisterAt(switch_instr, 0);
5694 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5695
Zheng Xu3927c8b2015-11-18 17:46:25 +08005696 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005697 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005698 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5699 // make sure we don't emit it if the target may run out of range.
5700 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5701 // ranges and emit the tables only as required.
5702 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005703
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005704 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005705 // Current instruction id is an upper bound of the number of HIRs in the graph.
5706 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5707 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005708 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5709 Register temp = temps.AcquireW();
5710 __ Subs(temp, value_reg, Operand(lower_bound));
5711
Zheng Xu3927c8b2015-11-18 17:46:25 +08005712 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005713 // Jump to successors[0] if value == lower_bound.
5714 __ B(eq, codegen_->GetLabelOf(successors[0]));
5715 int32_t last_index = 0;
5716 for (; num_entries - last_index > 2; last_index += 2) {
5717 __ Subs(temp, temp, Operand(2));
5718 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5719 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5720 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5721 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5722 }
5723 if (num_entries - last_index == 2) {
5724 // The last missing case_value.
5725 __ Cmp(temp, Operand(1));
5726 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005727 }
5728
5729 // And the default for any other value.
5730 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5731 __ B(codegen_->GetLabelOf(default_block));
5732 }
5733 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005734 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005735
5736 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5737
5738 // Below instructions should use at most one blocked register. Since there are two blocked
5739 // registers, we are free to block one.
5740 Register temp_w = temps.AcquireW();
5741 Register index;
5742 // Remove the bias.
5743 if (lower_bound != 0) {
5744 index = temp_w;
5745 __ Sub(index, value_reg, Operand(lower_bound));
5746 } else {
5747 index = value_reg;
5748 }
5749
5750 // Jump to default block if index is out of the range.
5751 __ Cmp(index, Operand(num_entries));
5752 __ B(hs, codegen_->GetLabelOf(default_block));
5753
5754 // In current VIXL implementation, it won't require any blocked registers to encode the
5755 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5756 // register pressure.
5757 Register table_base = temps.AcquireX();
5758 // Load jump offset from the table.
5759 __ Adr(table_base, jump_table->GetTableStartLabel());
5760 Register jump_offset = temp_w;
5761 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5762
5763 // Jump to target block by branching to table_base(pc related) + offset.
5764 Register target_address = table_base;
5765 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5766 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005767 }
5768}
5769
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005770void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5771 HInstruction* instruction,
5772 Location out,
5773 uint32_t offset,
5774 Location maybe_temp,
5775 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005776 Primitive::Type type = Primitive::kPrimNot;
5777 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005778 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005779 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005780 Register temp_reg = RegisterFrom(maybe_temp, type);
5781 if (kUseBakerReadBarrier) {
5782 // Load with fast path based Baker's read barrier.
5783 // /* HeapReference<Object> */ out = *(out + offset)
5784 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5785 out,
5786 out_reg,
5787 offset,
5788 temp_reg,
5789 /* needs_null_check */ false,
5790 /* use_load_acquire */ false);
5791 } else {
5792 // Load with slow path based read barrier.
5793 // Save the value of `out` into `maybe_temp` before overwriting it
5794 // in the following move operation, as we will need it for the
5795 // read barrier below.
5796 __ Mov(temp_reg, out_reg);
5797 // /* HeapReference<Object> */ out = *(out + offset)
5798 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5799 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5800 }
5801 } else {
5802 // Plain load with no read barrier.
5803 // /* HeapReference<Object> */ out = *(out + offset)
5804 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5805 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5806 }
5807}
5808
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005809void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5810 HInstruction* instruction,
5811 Location out,
5812 Location obj,
5813 uint32_t offset,
5814 Location maybe_temp,
5815 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005816 Primitive::Type type = Primitive::kPrimNot;
5817 Register out_reg = RegisterFrom(out, type);
5818 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005819 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005820 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005821 if (kUseBakerReadBarrier) {
5822 // Load with fast path based Baker's read barrier.
5823 Register temp_reg = RegisterFrom(maybe_temp, type);
5824 // /* HeapReference<Object> */ out = *(obj + offset)
5825 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5826 out,
5827 obj_reg,
5828 offset,
5829 temp_reg,
5830 /* needs_null_check */ false,
5831 /* use_load_acquire */ false);
5832 } else {
5833 // Load with slow path based read barrier.
5834 // /* HeapReference<Object> */ out = *(obj + offset)
5835 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5836 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5837 }
5838 } else {
5839 // Plain load with no read barrier.
5840 // /* HeapReference<Object> */ out = *(obj + offset)
5841 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5842 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5843 }
5844}
5845
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005846void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5847 HInstruction* instruction,
5848 Location root,
5849 Register obj,
5850 uint32_t offset,
5851 vixl::aarch64::Label* fixup_label,
5852 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005853 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005854 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005855 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005856 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005857 if (kUseBakerReadBarrier) {
5858 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005859 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00005860 //
Roland Levillainba650a42017-03-06 13:52:32 +00005861 // Note that we do not actually check the value of
5862 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5863 // root or not. Instead, we load into `temp` the read barrier
5864 // mark entry point corresponding to register `root`. If `temp`
5865 // is null, it means that `GetIsGcMarking()` is false, and vice
5866 // versa.
5867 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005868 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00005869 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5870 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5871 // // Slow path.
5872 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
Roland Levillain44015862016-01-22 11:47:17 +00005873 // }
5874
Roland Levillainba650a42017-03-06 13:52:32 +00005875 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
5876 Register temp = lr;
5877 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
5878 instruction, root, /* entrypoint */ LocationFrom(temp));
5879 codegen_->AddSlowPath(slow_path);
5880
5881 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5882 const int32_t entry_point_offset =
5883 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5884 // Loading the entrypoint does not require a load acquire since it is only changed when
5885 // threads are suspended or running a checkpoint.
5886 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5887
Roland Levillain44015862016-01-22 11:47:17 +00005888 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005889 if (fixup_label == nullptr) {
5890 __ Ldr(root_reg, MemOperand(obj, offset));
5891 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005892 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005893 }
Roland Levillain44015862016-01-22 11:47:17 +00005894 static_assert(
5895 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5896 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5897 "have different sizes.");
5898 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5899 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5900 "have different sizes.");
5901
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005902 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5903 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005904 __ Cbnz(temp, slow_path->GetEntryLabel());
5905 __ Bind(slow_path->GetExitLabel());
5906 } else {
5907 // GC root loaded through a slow path for read barriers other
5908 // than Baker's.
5909 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005910 if (fixup_label == nullptr) {
5911 __ Add(root_reg.X(), obj.X(), offset);
5912 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005913 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005914 }
Roland Levillain44015862016-01-22 11:47:17 +00005915 // /* mirror::Object* */ root = root->Read()
5916 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5917 }
5918 } else {
5919 // Plain GC root load with no read barrier.
5920 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005921 if (fixup_label == nullptr) {
5922 __ Ldr(root_reg, MemOperand(obj, offset));
5923 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005924 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005925 }
Roland Levillain44015862016-01-22 11:47:17 +00005926 // Note that GC roots are not affected by heap poisoning, thus we
5927 // do not have to unpoison `root_reg` here.
5928 }
5929}
5930
5931void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5932 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005933 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005934 uint32_t offset,
5935 Register temp,
5936 bool needs_null_check,
5937 bool use_load_acquire) {
5938 DCHECK(kEmitCompilerReadBarrier);
5939 DCHECK(kUseBakerReadBarrier);
5940
5941 // /* HeapReference<Object> */ ref = *(obj + offset)
5942 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005943 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005944 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5945 ref,
5946 obj,
5947 offset,
5948 no_index,
5949 no_scale_factor,
5950 temp,
5951 needs_null_check,
5952 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005953}
5954
5955void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5956 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005957 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005958 uint32_t data_offset,
5959 Location index,
5960 Register temp,
5961 bool needs_null_check) {
5962 DCHECK(kEmitCompilerReadBarrier);
5963 DCHECK(kUseBakerReadBarrier);
5964
5965 // Array cells are never volatile variables, therefore array loads
5966 // never use Load-Acquire instructions on ARM64.
5967 const bool use_load_acquire = false;
5968
Roland Levillainbfea3352016-06-23 13:48:47 +01005969 static_assert(
5970 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5971 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005972 // /* HeapReference<Object> */ ref =
5973 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005974 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5975 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5976 ref,
5977 obj,
5978 data_offset,
5979 index,
5980 scale_factor,
5981 temp,
5982 needs_null_check,
5983 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005984}
5985
5986void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5987 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005988 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005989 uint32_t offset,
5990 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005991 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005992 Register temp,
5993 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005994 bool use_load_acquire,
5995 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005996 DCHECK(kEmitCompilerReadBarrier);
5997 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005998 // If we are emitting an array load, we should not be using a
5999 // Load Acquire instruction. In other words:
6000 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6001 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006002
Roland Levillain54f869e2017-03-06 13:54:11 +00006003 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6004 // whether we need to enter the slow path to mark the reference.
6005 // Then, in the slow path, check the gray bit in the lock word of
6006 // the reference's holder (`obj`) to decide whether to mark `ref` or
6007 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006008 //
Roland Levillainba650a42017-03-06 13:52:32 +00006009 // Note that we do not actually check the value of `GetIsGcMarking()`;
6010 // instead, we load into `temp2` the read barrier mark entry point
6011 // corresponding to register `ref`. If `temp2` is null, it means
6012 // that `GetIsGcMarking()` is false, and vice versa.
6013 //
6014 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006015 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6016 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006017 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6018 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6019 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6020 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6021 // if (is_gray) {
6022 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6023 // }
6024 // } else {
6025 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006026 // }
Roland Levillain44015862016-01-22 11:47:17 +00006027
Roland Levillainba650a42017-03-06 13:52:32 +00006028 // Slow path marking the object `ref` when the GC is marking. The
6029 // entrypoint will already be loaded in `temp2`.
6030 Register temp2 = lr;
6031 Location temp2_loc = LocationFrom(temp2);
6032 SlowPathCodeARM64* slow_path;
6033 if (always_update_field) {
Roland Levillain54f869e2017-03-06 13:54:11 +00006034 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
6035 // only supports address of the form `obj + field_offset`, where
6036 // `obj` is a register and `field_offset` is a register. Thus
6037 // `offset` and `scale_factor` above are expected to be null in
6038 // this code path.
Roland Levillainba650a42017-03-06 13:52:32 +00006039 DCHECK_EQ(offset, 0u);
6040 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
Roland Levillain54f869e2017-03-06 13:54:11 +00006041 Location field_offset = index;
6042 slow_path =
6043 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6044 instruction,
6045 ref,
6046 obj,
6047 offset,
6048 /* index */ field_offset,
6049 scale_factor,
6050 needs_null_check,
6051 use_load_acquire,
6052 temp,
6053 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006054 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00006055 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6056 instruction,
6057 ref,
6058 obj,
6059 offset,
6060 index,
6061 scale_factor,
6062 needs_null_check,
6063 use_load_acquire,
6064 temp,
6065 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006066 }
6067 AddSlowPath(slow_path);
6068
6069 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6070 const int32_t entry_point_offset =
6071 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6072 // Loading the entrypoint does not require a load acquire since it is only changed when
6073 // threads are suspended or running a checkpoint.
6074 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006075 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6076 // checking GetIsGcMarking.
6077 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00006078 // Fast path: just load the reference.
6079 GenerateRawReferenceLoad(
6080 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006081 __ Bind(slow_path->GetExitLabel());
6082}
6083
6084void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6085 Location ref,
6086 Register obj,
6087 uint32_t offset,
6088 Location index,
6089 size_t scale_factor,
6090 bool needs_null_check,
6091 bool use_load_acquire) {
6092 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006093 Primitive::Type type = Primitive::kPrimNot;
6094 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006095
Roland Levillainba650a42017-03-06 13:52:32 +00006096 // If needed, vixl::EmissionCheckScope guards are used to ensure
6097 // that no pools are emitted between the load (macro) instruction
6098 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006099
Roland Levillain44015862016-01-22 11:47:17 +00006100 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006101 // Load types involving an "index": ArrayGet,
6102 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6103 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006104 if (use_load_acquire) {
6105 // UnsafeGetObjectVolatile intrinsic case.
6106 // Register `index` is not an index in an object array, but an
6107 // offset to an object reference field within object `obj`.
6108 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6109 DCHECK(instruction->GetLocations()->Intrinsified());
6110 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6111 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006112 DCHECK_EQ(offset, 0u);
6113 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006114 DCHECK_EQ(needs_null_check, false);
6115 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006116 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6117 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006118 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006119 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6120 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006121 if (index.IsConstant()) {
6122 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006123 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006124 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006125 if (needs_null_check) {
6126 MaybeRecordImplicitNullCheck(instruction);
6127 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006128 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006129 UseScratchRegisterScope temps(GetVIXLAssembler());
6130 Register temp = temps.AcquireW();
6131 __ Add(temp, obj, offset);
6132 {
6133 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6134 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6135 if (needs_null_check) {
6136 MaybeRecordImplicitNullCheck(instruction);
6137 }
6138 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006139 }
Roland Levillain44015862016-01-22 11:47:17 +00006140 }
Roland Levillain44015862016-01-22 11:47:17 +00006141 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006142 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006143 MemOperand field = HeapOperand(obj, offset);
6144 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006145 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6146 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006147 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006148 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006149 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006150 if (needs_null_check) {
6151 MaybeRecordImplicitNullCheck(instruction);
6152 }
Roland Levillain44015862016-01-22 11:47:17 +00006153 }
6154 }
6155
6156 // Object* ref = ref_addr->AsMirrorPtr()
6157 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006158}
6159
6160void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6161 Location out,
6162 Location ref,
6163 Location obj,
6164 uint32_t offset,
6165 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006166 DCHECK(kEmitCompilerReadBarrier);
6167
Roland Levillain44015862016-01-22 11:47:17 +00006168 // Insert a slow path based read barrier *after* the reference load.
6169 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006170 // If heap poisoning is enabled, the unpoisoning of the loaded
6171 // reference will be carried out by the runtime within the slow
6172 // path.
6173 //
6174 // Note that `ref` currently does not get unpoisoned (when heap
6175 // poisoning is enabled), which is alright as the `ref` argument is
6176 // not used by the artReadBarrierSlow entry point.
6177 //
6178 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6179 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6180 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6181 AddSlowPath(slow_path);
6182
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006183 __ B(slow_path->GetEntryLabel());
6184 __ Bind(slow_path->GetExitLabel());
6185}
6186
Roland Levillain44015862016-01-22 11:47:17 +00006187void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6188 Location out,
6189 Location ref,
6190 Location obj,
6191 uint32_t offset,
6192 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006193 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006194 // Baker's read barriers shall be handled by the fast path
6195 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6196 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006197 // If heap poisoning is enabled, unpoisoning will be taken care of
6198 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006199 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006200 } else if (kPoisonHeapReferences) {
6201 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6202 }
6203}
6204
Roland Levillain44015862016-01-22 11:47:17 +00006205void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6206 Location out,
6207 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006208 DCHECK(kEmitCompilerReadBarrier);
6209
Roland Levillain44015862016-01-22 11:47:17 +00006210 // Insert a slow path based read barrier *after* the GC root load.
6211 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006212 // Note that GC roots are not affected by heap poisoning, so we do
6213 // not need to do anything special for this here.
6214 SlowPathCodeARM64* slow_path =
6215 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6216 AddSlowPath(slow_path);
6217
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006218 __ B(slow_path->GetEntryLabel());
6219 __ Bind(slow_path->GetExitLabel());
6220}
6221
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006222void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6223 LocationSummary* locations =
6224 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6225 locations->SetInAt(0, Location::RequiresRegister());
6226 locations->SetOut(Location::RequiresRegister());
6227}
6228
6229void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6230 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006231 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006232 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006233 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006234 __ Ldr(XRegisterFrom(locations->Out()),
6235 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006236 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006237 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006238 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006239 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6240 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006241 __ Ldr(XRegisterFrom(locations->Out()),
6242 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006243 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006244}
6245
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006246static void PatchJitRootUse(uint8_t* code,
6247 const uint8_t* roots_data,
6248 vixl::aarch64::Literal<uint32_t>* literal,
6249 uint64_t index_in_table) {
6250 uint32_t literal_offset = literal->GetOffset();
6251 uintptr_t address =
6252 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6253 uint8_t* data = code + literal_offset;
6254 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6255}
6256
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006257void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6258 for (const auto& entry : jit_string_patches_) {
6259 const auto& it = jit_string_roots_.find(entry.first);
6260 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006261 PatchJitRootUse(code, roots_data, entry.second, it->second);
6262 }
6263 for (const auto& entry : jit_class_patches_) {
6264 const auto& it = jit_class_roots_.find(entry.first);
6265 DCHECK(it != jit_class_roots_.end());
6266 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006267 }
6268}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006269
Alexandre Rames67555f72014-11-18 10:55:16 +00006270#undef __
6271#undef QUICK_ENTRY_POINT
6272
Alexandre Rames5319def2014-10-23 10:03:10 +01006273} // namespace arm64
6274} // namespace art