blob: a2896fcf589e53256df9e8e0313f7cee9cdc5db5 [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
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000021#include "art_method-inl.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/bit_utils.h"
23#include "base/bit_utils_iterator.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010024#include "class_table.h"
Zheng Xuc6667102015-05-15 16:08:45 +080025#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000026#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010027#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080028#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010029#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010030#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070031#include "heap_poisoning.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080032#include "intrinsics.h"
33#include "intrinsics_arm64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010034#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "lock_word.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010036#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000038#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010039#include "thread.h"
40#include "utils/arm64/assembler_arm64.h"
41#include "utils/assembler.h"
42#include "utils/stack_checks.h"
43
Scott Wakeling97c72b72016-06-24 16:19:36 +010044using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000045using vixl::ExactAssemblyScope;
46using vixl::CodeBufferCheckScope;
47using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010048
49#ifdef __
50#error "ARM64 Codegen VIXL macro-assembler macro already defined."
51#endif
52
Vladimir Marko0a516052019-10-14 13:00:44 +000053namespace art {
Alexandre Rames5319def2014-10-23 10:03:10 +010054
Roland Levillain22ccc3a2015-11-24 13:10:05 +000055template<class MirrorType>
56class GcRoot;
57
Alexandre Rames5319def2014-10-23 10:03:10 +010058namespace arm64 {
59
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::ARM64EncodableConstantOrRegister;
61using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080062using helpers::CPURegisterFrom;
63using helpers::DRegisterFrom;
64using helpers::FPRegisterFrom;
65using helpers::HeapOperand;
66using helpers::HeapOperandFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080068using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010070using helpers::InputRegisterAt;
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +010071using helpers::Int64FromLocation;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010072using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080073using helpers::LocationFrom;
74using helpers::OperandFromMemOperand;
75using helpers::OutputCPURegister;
76using helpers::OutputFPRegister;
77using helpers::OutputRegister;
78using helpers::RegisterFrom;
79using helpers::StackOperandFrom;
80using helpers::VIXLRegCodeFromART;
81using helpers::WRegisterFrom;
82using helpers::XRegisterFrom;
83
Vladimir Markof3e0ee22015-12-17 15:23:13 +000084// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080085// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
86// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000087static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010088
Vladimir Markof4f2daa2017-03-20 18:26:59 +000089// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
90// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
Vladimir Marko008e09f32018-08-06 15:42:43 +010091// For the Baker read barrier implementation using link-time generated thunks we need to split
Vladimir Markof4f2daa2017-03-20 18:26:59 +000092// the offset explicitly.
93constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
94
Alexandre Rames5319def2014-10-23 10:03:10 +010095inline Condition ARM64Condition(IfCondition cond) {
96 switch (cond) {
97 case kCondEQ: return eq;
98 case kCondNE: return ne;
99 case kCondLT: return lt;
100 case kCondLE: return le;
101 case kCondGT: return gt;
102 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700103 case kCondB: return lo;
104 case kCondBE: return ls;
105 case kCondA: return hi;
106 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100107 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000108 LOG(FATAL) << "Unreachable";
109 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100110}
111
Vladimir Markod6e069b2016-01-18 11:11:01 +0000112inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
113 // The ARM64 condition codes can express all the necessary branches, see the
114 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
115 // There is no dex instruction or HIR that would need the missing conditions
116 // "equal or unordered" or "not equal".
117 switch (cond) {
118 case kCondEQ: return eq;
119 case kCondNE: return ne /* unordered */;
120 case kCondLT: return gt_bias ? cc : lt /* unordered */;
121 case kCondLE: return gt_bias ? ls : le /* unordered */;
122 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
123 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
124 default:
125 LOG(FATAL) << "UNREACHABLE";
126 UNREACHABLE();
127 }
128}
129
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100130Location ARM64ReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000131 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
132 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
133 // but we use the exact registers for clarity.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100134 if (return_type == DataType::Type::kFloat32) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000135 return LocationFrom(s0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 } else if (return_type == DataType::Type::kFloat64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000137 return LocationFrom(d0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100138 } else if (return_type == DataType::Type::kInt64) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000139 return LocationFrom(x0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100140 } else if (return_type == DataType::Type::kVoid) {
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100141 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000142 } else {
143 return LocationFrom(w0);
144 }
145}
146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100147Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000148 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100149}
150
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100151static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
152 InvokeRuntimeCallingConvention calling_convention;
153 RegisterSet caller_saves = RegisterSet::Empty();
154 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
155 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
156 RegisterFrom(calling_convention.GetReturnLocation(DataType::Type::kReference),
157 DataType::Type::kReference).GetCode());
158 return caller_saves;
159}
160
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100161// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
162#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700163#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100164
Zheng Xuda403092015-04-24 17:35:39 +0800165// Calculate memory accessing operand for save/restore live registers.
166static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100167 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800168 int64_t spill_offset,
169 bool is_save) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800170 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
171 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100172 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800173 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100174 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800175 codegen->GetNumberOfFloatingPointRegisters()));
176
Vladimir Marko804b03f2016-09-14 16:26:36 +0100177 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serovc8150b52019-07-31 18:28:00 +0100178 const unsigned v_reg_size_in_bits = codegen->GetSlowPathFPWidth() * 8;
Artem Serov1a719e42019-07-18 14:24:55 +0100179 DCHECK_LE(codegen->GetSIMDRegisterWidth(), kQRegSizeInBytes);
Artem Serovc8150b52019-07-31 18:28:00 +0100180 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size_in_bits, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800181
182 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
183 UseScratchRegisterScope temps(masm);
184
185 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100186 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
187 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800188 int64_t reg_size = kXRegSizeInBytes;
189 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
190 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100191 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800192 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
193 // If the offset does not fit in the instruction's immediate field, use an alternate register
194 // to compute the base address(float point registers spill base address).
195 Register new_base = temps.AcquireSameSizeAs(base);
196 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
197 base = new_base;
198 spill_offset = -core_spill_size;
199 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
200 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
201 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
202 }
203
204 if (is_save) {
205 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
206 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
207 } else {
208 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
209 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
210 }
211}
212
213void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800214 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Andreas Gampe3db70682018-12-26 15:12:03 -0800215 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ true);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100216 for (uint32_t i : LowToHighBits(core_spills)) {
217 // If the register holds an object, update the stack mask.
218 if (locations->RegisterContainsObject(i)) {
219 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800220 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100221 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
222 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
223 saved_core_stack_offsets_[i] = stack_offset;
224 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800225 }
226
Artem Serovc8150b52019-07-31 18:28:00 +0100227 const size_t fp_reg_size = codegen->GetSlowPathFPWidth();
Andreas Gampe3db70682018-12-26 15:12:03 -0800228 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers= */ false);
Vladimir Marko804b03f2016-09-14 16:26:36 +0100229 for (uint32_t i : LowToHighBits(fp_spills)) {
230 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
231 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
232 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serov9df37b92019-07-23 16:41:54 +0100233 stack_offset += fp_reg_size;
Zheng Xuda403092015-04-24 17:35:39 +0800234 }
235
Vladimir Marko804b03f2016-09-14 16:26:36 +0100236 SaveRestoreLiveRegistersHelper(codegen,
237 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800238 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ true);
Zheng Xuda403092015-04-24 17:35:39 +0800239}
240
241void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100242 SaveRestoreLiveRegistersHelper(codegen,
243 locations,
Andreas Gampe3db70682018-12-26 15:12:03 -0800244 codegen->GetFirstRegisterSlotInSlowPath(), /* is_save= */ false);
Zheng Xuda403092015-04-24 17:35:39 +0800245}
246
Alexandre Rames5319def2014-10-23 10:03:10 +0100247class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
248 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000249 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100250
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100251 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100252 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000253 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100254
Alexandre Rames5319def2014-10-23 10:03:10 +0100255 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000256 if (instruction_->CanThrowIntoCatchBlock()) {
257 // Live registers will be restored in the catch block if caught.
258 SaveLiveRegisters(codegen, instruction_->GetLocations());
259 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000260 // We're moving two locations to locations that could overlap, so we need a parallel
261 // move resolver.
262 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100263 codegen->EmitParallelMoves(locations->InAt(0),
264 LocationFrom(calling_convention.GetRegisterAt(0)),
265 DataType::Type::kInt32,
266 locations->InAt(1),
267 LocationFrom(calling_convention.GetRegisterAt(1)),
268 DataType::Type::kInt32);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000269 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
270 ? kQuickThrowStringBounds
271 : kQuickThrowArrayBounds;
272 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100273 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800274 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100275 }
276
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100277 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100278
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100279 const char* GetDescription() const override { return "BoundsCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100280
Alexandre Rames5319def2014-10-23 10:03:10 +0100281 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100282 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
283};
284
Alexandre Rames67555f72014-11-18 10:55:16 +0000285class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
286 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000287 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000288
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100289 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000290 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
291 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000292 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800293 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000294 }
295
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100296 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100297
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100298 const char* GetDescription() const override { return "DivZeroCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100299
Alexandre Rames67555f72014-11-18 10:55:16 +0000300 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000301 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
302};
303
304class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
305 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100306 LoadClassSlowPathARM64(HLoadClass* cls, HInstruction* at)
307 : SlowPathCodeARM64(at), cls_(cls) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000308 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100309 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexandre Rames67555f72014-11-18 10:55:16 +0000310 }
311
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100312 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000313 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000314 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100315 const uint32_t dex_pc = instruction_->GetDexPc();
316 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
317 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
Alexandre Rames67555f72014-11-18 10:55:16 +0000318
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100319 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames67555f72014-11-18 10:55:16 +0000320 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000321 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000322
Vladimir Markof3c52b42017-11-17 17:32:12 +0000323 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100324 if (must_resolve_type) {
325 DCHECK(IsSameDexFile(cls_->GetDexFile(), arm64_codegen->GetGraph()->GetDexFile()));
326 dex::TypeIndex type_index = cls_->GetTypeIndex();
327 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Vladimir Marko9d479252018-07-24 11:35:20 +0100328 arm64_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
329 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100330 // If we also must_do_clinit, the resolved type is now in the correct register.
331 } else {
332 DCHECK(must_do_clinit);
333 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
334 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)),
335 source,
336 cls_->GetType());
337 }
338 if (must_do_clinit) {
339 arm64_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
340 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800341 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000342
343 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000344 if (out.IsValid()) {
345 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100346 DataType::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000347 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000348 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000349 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000350 __ B(GetExitLabel());
351 }
352
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100353 const char* GetDescription() const override { return "LoadClassSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100354
Alexandre Rames67555f72014-11-18 10:55:16 +0000355 private:
356 // The class this slow path will load.
357 HLoadClass* const cls_;
358
Alexandre Rames67555f72014-11-18 10:55:16 +0000359 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
360};
361
Vladimir Markoaad75c62016-10-03 08:46:48 +0000362class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
363 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000364 explicit LoadStringSlowPathARM64(HLoadString* instruction)
365 : SlowPathCodeARM64(instruction) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000366
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100367 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000368 LocationSummary* locations = instruction_->GetLocations();
369 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
370 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
371
372 __ Bind(GetEntryLabel());
373 SaveLiveRegisters(codegen, locations);
374
Vladimir Markof3c52b42017-11-17 17:32:12 +0000375 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000376 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
377 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000378 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
379 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100380 DataType::Type type = instruction_->GetType();
Vladimir Markoaad75c62016-10-03 08:46:48 +0000381 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
382
383 RestoreLiveRegisters(codegen, locations);
384
Vladimir Markoaad75c62016-10-03 08:46:48 +0000385 __ B(GetExitLabel());
386 }
387
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100388 const char* GetDescription() const override { return "LoadStringSlowPathARM64"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000389
390 private:
391 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
392};
393
Alexandre Rames5319def2014-10-23 10:03:10 +0100394class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
395 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000396 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100397
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100398 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames67555f72014-11-18 10:55:16 +0000399 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100400 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000401 if (instruction_->CanThrowIntoCatchBlock()) {
402 // Live registers will be restored in the catch block if caught.
403 SaveLiveRegisters(codegen, instruction_->GetLocations());
404 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000405 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
406 instruction_,
407 instruction_->GetDexPc(),
408 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800409 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100410 }
411
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100412 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100413
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100414 const char* GetDescription() const override { return "NullCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100415
Alexandre Rames5319def2014-10-23 10:03:10 +0100416 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100417 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
418};
419
420class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
421 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100422 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000423 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100424
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100425 void EmitNativeCode(CodeGenerator* codegen) override {
Artem Serov7957d952017-04-04 15:44:09 +0100426 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000427 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100428 __ Bind(GetEntryLabel());
Artem Serov1a719e42019-07-18 14:24:55 +0100429 SaveLiveRegisters(codegen, locations); // Only saves live vector regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000430 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800431 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov1a719e42019-07-18 14:24:55 +0100432 RestoreLiveRegisters(codegen, locations); // Only restores live vector regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000433 if (successor_ == nullptr) {
434 __ B(GetReturnLabel());
435 } else {
436 __ B(arm64_codegen->GetLabelOf(successor_));
437 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100438 }
439
Scott Wakeling97c72b72016-06-24 16:19:36 +0100440 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100441 DCHECK(successor_ == nullptr);
442 return &return_label_;
443 }
444
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100445 HBasicBlock* GetSuccessor() const {
446 return successor_;
447 }
448
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100449 const char* GetDescription() const override { return "SuspendCheckSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100450
Alexandre Rames5319def2014-10-23 10:03:10 +0100451 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100452 // If not null, the block to branch to after the suspend check.
453 HBasicBlock* const successor_;
454
455 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100456 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100457
458 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
459};
460
Alexandre Rames67555f72014-11-18 10:55:16 +0000461class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
462 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000463 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000464 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000465
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100466 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000467 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800468
Alexandre Rames3e69f162014-12-10 10:36:50 +0000469 DCHECK(instruction_->IsCheckCast()
470 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
471 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100472 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000473
Alexandre Rames67555f72014-11-18 10:55:16 +0000474 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000475
Vladimir Marko87584542017-12-12 17:47:52 +0000476 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000477 SaveLiveRegisters(codegen, locations);
478 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000479
480 // We're moving two locations to locations that could overlap, so we need a parallel
481 // move resolver.
482 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800483 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800484 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100485 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800486 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800487 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100488 DataType::Type::kReference);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000489 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000490 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800491 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100492 DataType::Type ret_type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000493 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
494 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
495 } else {
496 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800497 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
498 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000499 }
500
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000501 if (!is_fatal_) {
502 RestoreLiveRegisters(codegen, locations);
503 __ B(GetExitLabel());
504 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000505 }
506
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100507 const char* GetDescription() const override { return "TypeCheckSlowPathARM64"; }
508 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100509
Alexandre Rames67555f72014-11-18 10:55:16 +0000510 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000511 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000512
Alexandre Rames67555f72014-11-18 10:55:16 +0000513 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
514};
515
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700516class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
517 public:
Aart Bik42249c32016-01-07 15:33:50 -0800518 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000519 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700520
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100521 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bik42249c32016-01-07 15:33:50 -0800522 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700523 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100524 LocationSummary* locations = instruction_->GetLocations();
525 SaveLiveRegisters(codegen, locations);
526 InvokeRuntimeCallingConvention calling_convention;
527 __ Mov(calling_convention.GetRegisterAt(0),
528 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000529 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100530 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700531 }
532
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100533 const char* GetDescription() const override { return "DeoptimizationSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100534
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700535 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700536 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
537};
538
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100539class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
540 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000541 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100542
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100543 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100544 LocationSummary* locations = instruction_->GetLocations();
545 __ Bind(GetEntryLabel());
546 SaveLiveRegisters(codegen, locations);
547
548 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100549 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100550 parallel_move.AddMove(
551 locations->InAt(0),
552 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100553 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100554 nullptr);
555 parallel_move.AddMove(
556 locations->InAt(1),
557 LocationFrom(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100558 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100559 nullptr);
560 parallel_move.AddMove(
561 locations->InAt(2),
562 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100563 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100564 nullptr);
565 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
566
567 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000568 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100569 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
570 RestoreLiveRegisters(codegen, locations);
571 __ B(GetExitLabel());
572 }
573
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100574 const char* GetDescription() const override { return "ArraySetSlowPathARM64"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100575
576 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100577 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
578};
579
Zheng Xu3927c8b2015-11-18 17:46:25 +0800580void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
581 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000582 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800583
584 // We are about to use the assembler to place literals directly. Make sure we have enough
585 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000586 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
587 num_entries * sizeof(int32_t),
588 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800589
590 __ Bind(&table_start_);
591 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
592 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100593 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800594 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100595 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800596 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
597 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
598 Literal<int32_t> literal(jump_offset);
599 __ place(&literal);
600 }
601}
602
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000603// Slow path generating a read barrier for a heap reference.
604class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
605 public:
606 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
607 Location out,
608 Location ref,
609 Location obj,
610 uint32_t offset,
611 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000612 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000613 out_(out),
614 ref_(ref),
615 obj_(obj),
616 offset_(offset),
617 index_(index) {
618 DCHECK(kEmitCompilerReadBarrier);
619 // If `obj` is equal to `out` or `ref`, it means the initial object
620 // has been overwritten by (or after) the heap object reference load
621 // to be instrumented, e.g.:
622 //
623 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000624 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000625 //
626 // In that case, we have lost the information about the original
627 // object, and the emitted read barrier cannot work properly.
628 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
629 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
630 }
631
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100632 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000633 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
634 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100635 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000636 DCHECK(locations->CanCall());
637 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100638 DCHECK(instruction_->IsInstanceFieldGet() ||
639 instruction_->IsStaticFieldGet() ||
640 instruction_->IsArrayGet() ||
641 instruction_->IsInstanceOf() ||
642 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700643 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000644 << "Unexpected instruction in read barrier for heap reference slow path: "
645 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000646 // The read barrier instrumentation of object ArrayGet
647 // instructions does not support the HIntermediateAddress
648 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000649 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100650 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000651
652 __ Bind(GetEntryLabel());
653
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000654 SaveLiveRegisters(codegen, locations);
655
656 // We may have to change the index's value, but as `index_` is a
657 // constant member (like other "inputs" of this slow path),
658 // introduce a copy of it, `index`.
659 Location index = index_;
660 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100661 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000662 if (instruction_->IsArrayGet()) {
663 // Compute the actual memory offset and store it in `index`.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100664 Register index_reg = RegisterFrom(index_, DataType::Type::kInt32);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000665 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
666 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
667 // We are about to change the value of `index_reg` (see the
668 // calls to vixl::MacroAssembler::Lsl and
669 // vixl::MacroAssembler::Mov below), but it has
670 // not been saved by the previous call to
671 // art::SlowPathCode::SaveLiveRegisters, as it is a
672 // callee-save register --
673 // art::SlowPathCode::SaveLiveRegisters does not consider
674 // callee-save registers, as it has been designed with the
675 // assumption that callee-save registers are supposed to be
676 // handled by the called function. So, as a callee-save
677 // register, `index_reg` _would_ eventually be saved onto
678 // the stack, but it would be too late: we would have
679 // changed its value earlier. Therefore, we manually save
680 // it here into another freely available register,
681 // `free_reg`, chosen of course among the caller-save
682 // registers (as a callee-save `free_reg` register would
683 // exhibit the same problem).
684 //
685 // Note we could have requested a temporary register from
686 // the register allocator instead; but we prefer not to, as
687 // this is a slow path, and we know we can find a
688 // caller-save register that is available.
689 Register free_reg = FindAvailableCallerSaveRegister(codegen);
690 __ Mov(free_reg.W(), index_reg);
691 index_reg = free_reg;
692 index = LocationFrom(index_reg);
693 } else {
694 // The initial register stored in `index_` has already been
695 // saved in the call to art::SlowPathCode::SaveLiveRegisters
696 // (as it is not a callee-save register), so we can freely
697 // use it.
698 }
699 // Shifting the index value contained in `index_reg` by the scale
700 // factor (2) cannot overflow in practice, as the runtime is
701 // unable to allocate object arrays with a size larger than
702 // 2^26 - 1 (that is, 2^28 - 4 bytes).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100703 __ Lsl(index_reg, index_reg, DataType::SizeShift(type));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000704 static_assert(
705 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
706 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
707 __ Add(index_reg, index_reg, Operand(offset_));
708 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100709 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
710 // intrinsics, `index_` is not shifted by a scale factor of 2
711 // (as in the case of ArrayGet), as it is actually an offset
712 // to an object field within an object.
713 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000714 DCHECK(instruction_->GetLocations()->Intrinsified());
715 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
716 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
717 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100718 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +0100719 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000720 }
721 }
722
723 // We're moving two or three locations to locations that could
724 // overlap, so we need a parallel move resolver.
725 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100726 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000727 parallel_move.AddMove(ref_,
728 LocationFrom(calling_convention.GetRegisterAt(0)),
729 type,
730 nullptr);
731 parallel_move.AddMove(obj_,
732 LocationFrom(calling_convention.GetRegisterAt(1)),
733 type,
734 nullptr);
735 if (index.IsValid()) {
736 parallel_move.AddMove(index,
737 LocationFrom(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100738 DataType::Type::kInt32,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000739 nullptr);
740 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
741 } else {
742 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
743 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
744 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000745 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000746 instruction_,
747 instruction_->GetDexPc(),
748 this);
749 CheckEntrypointTypes<
750 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
751 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
752
753 RestoreLiveRegisters(codegen, locations);
754
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000755 __ B(GetExitLabel());
756 }
757
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100758 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000759
760 private:
761 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100762 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
763 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000764 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
765 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
766 return Register(VIXLRegCodeFromART(i), kXRegSize);
767 }
768 }
769 // We shall never fail to find a free caller-save register, as
770 // there are more than two core caller-save registers on ARM64
771 // (meaning it is possible to find one which is different from
772 // `ref` and `obj`).
773 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
774 LOG(FATAL) << "Could not find a free register";
775 UNREACHABLE();
776 }
777
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000778 const Location out_;
779 const Location ref_;
780 const Location obj_;
781 const uint32_t offset_;
782 // An additional location containing an index to an array.
783 // Only used for HArrayGet and the UnsafeGetObject &
784 // UnsafeGetObjectVolatile intrinsics.
785 const Location index_;
786
787 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
788};
789
790// Slow path generating a read barrier for a GC root.
791class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
792 public:
793 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000794 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000795 DCHECK(kEmitCompilerReadBarrier);
796 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000797
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100798 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000799 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100800 DataType::Type type = DataType::Type::kReference;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000801 DCHECK(locations->CanCall());
802 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000803 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
804 << "Unexpected instruction in read barrier for GC root slow path: "
805 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000806
807 __ Bind(GetEntryLabel());
808 SaveLiveRegisters(codegen, locations);
809
810 InvokeRuntimeCallingConvention calling_convention;
811 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
812 // The argument of the ReadBarrierForRootSlow is not a managed
813 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
814 // thus we need a 64-bit move here, and we cannot use
815 //
816 // arm64_codegen->MoveLocation(
817 // LocationFrom(calling_convention.GetRegisterAt(0)),
818 // root_,
819 // type);
820 //
821 // which would emit a 32-bit move, as `type` is a (32-bit wide)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100822 // reference type (`DataType::Type::kReference`).
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000823 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000824 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000825 instruction_,
826 instruction_->GetDexPc(),
827 this);
828 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
829 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
830
831 RestoreLiveRegisters(codegen, locations);
832 __ B(GetExitLabel());
833 }
834
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100835 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathARM64"; }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000836
837 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000838 const Location out_;
839 const Location root_;
840
841 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
842};
843
Alexandre Rames5319def2014-10-23 10:03:10 +0100844#undef __
845
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100846Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(DataType::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100847 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100848 if (type == DataType::Type::kVoid) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100849 LOG(FATAL) << "Unreachable type " << type;
850 }
851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100852 if (DataType::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100853 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
854 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100855 } else if (!DataType::IsFloatingPointType(type) &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000856 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000857 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
858 } else {
859 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100860 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
861 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100862 }
863
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000864 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100865 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100866 return next_location;
867}
868
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100869Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100870 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100871}
872
Serban Constantinescu579885a2015-02-22 20:51:33 +0000873CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100874 const CompilerOptions& compiler_options,
875 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100876 : CodeGenerator(graph,
877 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000878 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000879 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100880 callee_saved_core_registers.GetList(),
881 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100882 compiler_options,
883 stats),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100884 block_labels_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
885 jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Artem Serov1a719e42019-07-18 14:24:55 +0100886 location_builder_neon_(graph, this),
887 instruction_visitor_neon_(graph, this),
888 location_builder_sve_(graph, this),
889 instruction_visitor_sve_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100890 move_resolver_(graph->GetAllocator(), this),
Artem Serovaa6f4832018-11-21 18:57:54 +0000891 assembler_(graph->GetAllocator(),
892 compiler_options.GetInstructionSetFeatures()->AsArm64InstructionSetFeatures()),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000893 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100894 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000895 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100896 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +0000897 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100898 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +0100899 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100900 call_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100901 baker_read_barrier_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof6675082019-05-17 12:05:28 +0100902 uint32_literals_(std::less<uint32_t>(),
903 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
904 uint64_literals_(std::less<uint64_t>(),
905 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000906 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100907 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000908 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Marko966b46f2018-08-03 10:20:19 +0000909 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
910 jit_baker_read_barrier_slow_paths_(std::less<uint32_t>(),
911 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000912 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000913 AddAllocatedRegister(LocationFrom(lr));
Artem Serov1a719e42019-07-18 14:24:55 +0100914
915 bool use_sve = ShouldUseSVE();
916 if (use_sve) {
917 location_builder_ = &location_builder_sve_;
918 instruction_visitor_ = &instruction_visitor_sve_;
919 } else {
920 location_builder_ = &location_builder_neon_;
921 instruction_visitor_ = &instruction_visitor_neon_;
922 }
923}
924
925bool CodeGeneratorARM64::ShouldUseSVE() const {
926 return kArm64AllowSVE && GetInstructionSetFeatures().HasSVE();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000927}
Alexandre Rames5319def2014-10-23 10:03:10 +0100928
Alexandre Rames67555f72014-11-18 10:55:16 +0000929#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100930
Zheng Xu3927c8b2015-11-18 17:46:25 +0800931void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100932 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800933 jump_table->EmitTable(this);
934 }
935}
936
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000937void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800938 EmitJumpTables();
Vladimir Marko966b46f2018-08-03 10:20:19 +0000939
940 // Emit JIT baker read barrier slow paths.
941 DCHECK(Runtime::Current()->UseJitCompilation() || jit_baker_read_barrier_slow_paths_.empty());
942 for (auto& entry : jit_baker_read_barrier_slow_paths_) {
943 uint32_t encoded_data = entry.first;
944 vixl::aarch64::Label* slow_path_entry = &entry.second.label;
945 __ Bind(slow_path_entry);
Andreas Gampe3db70682018-12-26 15:12:03 -0800946 CompileBakerReadBarrierThunk(*GetAssembler(), encoded_data, /* debug_name= */ nullptr);
Vladimir Marko966b46f2018-08-03 10:20:19 +0000947 }
948
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000949 // Ensure we emit the literal pool.
950 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000951
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000952 CodeGenerator::Finalize(allocator);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000953
954 // Verify Baker read barrier linker patches.
955 if (kIsDebugBuild) {
956 ArrayRef<const uint8_t> code = allocator->GetMemory();
957 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
958 DCHECK(info.label.IsBound());
959 uint32_t literal_offset = info.label.GetLocation();
960 DCHECK_ALIGNED(literal_offset, 4u);
961
962 auto GetInsn = [&code](uint32_t offset) {
963 DCHECK_ALIGNED(offset, 4u);
964 return
965 (static_cast<uint32_t>(code[offset + 0]) << 0) +
966 (static_cast<uint32_t>(code[offset + 1]) << 8) +
967 (static_cast<uint32_t>(code[offset + 2]) << 16)+
968 (static_cast<uint32_t>(code[offset + 3]) << 24);
969 };
970
971 const uint32_t encoded_data = info.custom_data;
972 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
973 // Check that the next instruction matches the expected LDR.
974 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +0100975 case BakerReadBarrierKind::kField:
976 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +0000977 DCHECK_GE(code.size() - literal_offset, 8u);
978 uint32_t next_insn = GetInsn(literal_offset + 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +0000979 CheckValidReg(next_insn & 0x1fu); // Check destination register.
980 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko0ecac682018-08-07 10:40:38 +0100981 if (kind == BakerReadBarrierKind::kField) {
982 // LDR (immediate) with correct base_reg.
983 CHECK_EQ(next_insn & 0xffc003e0u, 0xb9400000u | (base_reg << 5));
984 } else {
985 DCHECK(kind == BakerReadBarrierKind::kAcquire);
986 // LDAR with correct base_reg.
987 CHECK_EQ(next_insn & 0xffffffe0u, 0x88dffc00u | (base_reg << 5));
988 }
Vladimir Markoca1e0382018-04-11 09:58:41 +0000989 break;
990 }
991 case BakerReadBarrierKind::kArray: {
992 DCHECK_GE(code.size() - literal_offset, 8u);
993 uint32_t next_insn = GetInsn(literal_offset + 4u);
994 // LDR (register) with the correct base_reg, size=10 (32-bit), option=011 (extend = LSL),
995 // and S=1 (shift amount = 2 for 32-bit version), i.e. LDR Wt, [Xn, Xm, LSL #2].
996 CheckValidReg(next_insn & 0x1fu); // Check destination register.
997 const uint32_t base_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
998 CHECK_EQ(next_insn & 0xffe0ffe0u, 0xb8607800u | (base_reg << 5));
999 CheckValidReg((next_insn >> 16) & 0x1f); // Check index register
1000 break;
1001 }
1002 case BakerReadBarrierKind::kGcRoot: {
1003 DCHECK_GE(literal_offset, 4u);
1004 uint32_t prev_insn = GetInsn(literal_offset - 4u);
Vladimir Markoca1e0382018-04-11 09:58:41 +00001005 const uint32_t root_reg = BakerReadBarrierFirstRegField::Decode(encoded_data);
Vladimir Marko94796f82018-08-08 15:15:33 +01001006 // Usually LDR (immediate) with correct root_reg but
1007 // we may have a "MOV marked, old_value" for UnsafeCASObject.
1008 if ((prev_insn & 0xffe0ffff) != (0x2a0003e0 | root_reg)) { // MOV?
1009 CHECK_EQ(prev_insn & 0xffc0001fu, 0xb9400000u | root_reg); // LDR?
1010 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00001011 break;
1012 }
1013 default:
1014 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
1015 UNREACHABLE();
1016 }
1017 }
1018 }
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001019}
1020
Zheng Xuad4450e2015-04-17 18:48:56 +08001021void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1022 // Note: There are 6 kinds of moves:
1023 // 1. constant -> GPR/FPR (non-cycle)
1024 // 2. constant -> stack (non-cycle)
1025 // 3. GPR/FPR -> GPR/FPR
1026 // 4. GPR/FPR -> stack
1027 // 5. stack -> GPR/FPR
1028 // 6. stack -> stack (non-cycle)
1029 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1030 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1031 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1032 // dependency.
1033 vixl_temps_.Open(GetVIXLAssembler());
1034}
1035
1036void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1037 vixl_temps_.Close();
1038}
1039
1040Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001041 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1042 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1043 || kind == Location::kSIMDStackSlot);
1044 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1045 ? Location::kFpuRegister
1046 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001047 Location scratch = GetScratchLocation(kind);
1048 if (!scratch.Equals(Location::NoLocation())) {
1049 return scratch;
1050 }
1051 // Allocate from VIXL temp registers.
1052 if (kind == Location::kRegister) {
1053 scratch = LocationFrom(vixl_temps_.AcquireX());
1054 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001055 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serov1a719e42019-07-18 14:24:55 +01001056 scratch = codegen_->GetGraph()->HasSIMD()
1057 ? codegen_->GetInstructionCodeGeneratorArm64()->AllocateSIMDScratchLocation(&vixl_temps_)
1058 : LocationFrom(vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001059 }
1060 AddScratchLocation(scratch);
1061 return scratch;
1062}
1063
1064void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1065 if (loc.IsRegister()) {
1066 vixl_temps_.Release(XRegisterFrom(loc));
1067 } else {
1068 DCHECK(loc.IsFpuRegister());
Artem Serov1a719e42019-07-18 14:24:55 +01001069 if (codegen_->GetGraph()->HasSIMD()) {
1070 codegen_->GetInstructionCodeGeneratorArm64()->FreeSIMDScratchLocation(loc, &vixl_temps_);
1071 } else {
1072 vixl_temps_.Release(DRegisterFrom(loc));
1073 }
Zheng Xuad4450e2015-04-17 18:48:56 +08001074 }
1075 RemoveScratchLocation(loc);
1076}
1077
Alexandre Rames3e69f162014-12-10 10:36:50 +00001078void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001079 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001080 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001081}
1082
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001083void CodeGeneratorARM64::MaybeIncrementHotness(bool is_frame_entry) {
1084 MacroAssembler* masm = GetVIXLAssembler();
1085 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1086 UseScratchRegisterScope temps(masm);
1087 Register counter = temps.AcquireX();
1088 Register method = is_frame_entry ? kArtMethodRegister : temps.AcquireX();
1089 if (!is_frame_entry) {
1090 __ Ldr(method, MemOperand(sp, 0));
1091 }
1092 __ Ldrh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1093 __ Add(counter, counter, 1);
1094 // Subtract one if the counter would overflow.
1095 __ Sub(counter, counter, Operand(counter, LSR, 16));
1096 __ Strh(counter, MemOperand(method, ArtMethod::HotnessCountOffset().Int32Value()));
1097 }
1098
1099 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
1100 ScopedObjectAccess soa(Thread::Current());
1101 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001102 if (info != nullptr) {
Nicolas Geoffrayc1cd1332020-01-25 13:08:24 +00001103 uint64_t address = reinterpret_cast64<uint64_t>(info);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001104 vixl::aarch64::Label done;
1105 UseScratchRegisterScope temps(masm);
1106 Register temp = temps.AcquireX();
1107 Register counter = temps.AcquireW();
1108 __ Mov(temp, address);
1109 __ Ldrh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1110 __ Add(counter, counter, 1);
1111 __ Strh(counter, MemOperand(temp, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()));
1112 __ Tst(counter, 0xffff);
1113 __ B(ne, &done);
1114 if (is_frame_entry) {
1115 if (HasEmptyFrame()) {
1116 // The entyrpoint expects the method at the bottom of the stack. We
1117 // claim stack space necessary for alignment.
1118 __ Claim(kStackAlignment);
1119 __ Stp(kArtMethodRegister, lr, MemOperand(sp, 0));
1120 } else if (!RequiresCurrentMethod()) {
1121 __ Str(kArtMethodRegister, MemOperand(sp, 0));
1122 }
1123 } else {
1124 CHECK(RequiresCurrentMethod());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001125 }
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001126 uint32_t entrypoint_offset =
1127 GetThreadOffset<kArm64PointerSize>(kQuickCompileOptimized).Int32Value();
1128 __ Ldr(lr, MemOperand(tr, entrypoint_offset));
1129 // Note: we don't record the call here (and therefore don't generate a stack
1130 // map), as the entrypoint should never be suspended.
1131 __ Blr(lr);
1132 if (HasEmptyFrame()) {
1133 CHECK(is_frame_entry);
1134 __ Ldr(lr, MemOperand(sp, 8));
1135 __ Drop(kStackAlignment);
1136 }
1137 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001138 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001139 }
1140}
1141
Alexandre Rames5319def2014-10-23 10:03:10 +01001142void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001143 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001144 __ Bind(&frame_entry_label_);
1145
Vladimir Marko33bff252017-11-01 14:35:42 +00001146 bool do_overflow_check =
1147 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm64) || !IsLeafMethod();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001148 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001149 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001150 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001151 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Vladimir Marko33bff252017-11-01 14:35:42 +00001152 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001153 {
1154 // Ensure that between load and RecordPcInfo there are no pools emitted.
1155 ExactAssemblyScope eas(GetVIXLAssembler(),
1156 kInstructionSize,
1157 CodeBufferCheckScope::kExactSize);
1158 __ ldr(wzr, MemOperand(temp, 0));
1159 RecordPcInfo(nullptr, 0);
1160 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001161 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001162
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001163 if (!HasEmptyFrame()) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001164 // Stack layout:
1165 // sp[frame_size - 8] : lr.
1166 // ... : other preserved core registers.
1167 // ... : other preserved fp registers.
1168 // ... : reserved frame space.
1169 // sp[0] : current method.
Vladimir Marko1a225a72019-07-05 13:37:42 +01001170 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1171 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1172 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1173 DCHECK(!preserved_core_registers.IsEmpty());
1174 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1175 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001176
Vladimir Marko1a225a72019-07-05 13:37:42 +01001177 // Save the current method if we need it, or if using STP reduces code
1178 // size. Note that we do not do this in HCurrentMethod, as the
1179 // instruction might have been removed in the SSA graph.
1180 CPURegister lowest_spill;
1181 if (core_spills_offset == kXRegSizeInBytes) {
1182 // If there is no gap between the method and the lowest core spill, use
1183 // aligned STP pre-index to store both. Max difference is 512. We do
1184 // that to reduce code size even if we do not have to save the method.
1185 DCHECK_LE(frame_size, 512); // 32 core registers are only 256 bytes.
1186 lowest_spill = preserved_core_registers.PopLowestIndex();
1187 __ Stp(kArtMethodRegister, lowest_spill, MemOperand(sp, -frame_size, PreIndex));
1188 } else if (RequiresCurrentMethod()) {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001189 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001190 } else {
1191 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001192 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001193 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Vladimir Marko1a225a72019-07-05 13:37:42 +01001194 if (lowest_spill.IsValid()) {
1195 GetAssembler()->cfi().RelOffset(DWARFReg(lowest_spill), core_spills_offset);
1196 core_spills_offset += kXRegSizeInBytes;
1197 }
1198 GetAssembler()->SpillRegisters(preserved_core_registers, core_spills_offset);
1199 GetAssembler()->SpillRegisters(preserved_fp_registers, fp_spills_offset);
Mingyao Yang063fc772016-08-02 11:02:54 -07001200
1201 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1202 // Initialize should_deoptimize flag to 0.
1203 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1204 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1205 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001206 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001207 MaybeIncrementHotness(/* is_frame_entry= */ true);
Andreas Gampe3db70682018-12-26 15:12:03 -08001208 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01001209}
1210
1211void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001212 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001213 if (!HasEmptyFrame()) {
Vladimir Marko1a225a72019-07-05 13:37:42 +01001214 int32_t frame_size = dchecked_integral_cast<int32_t>(GetFrameSize());
1215 uint32_t core_spills_offset = frame_size - GetCoreSpillSize();
1216 CPURegList preserved_core_registers = GetFramePreservedCoreRegisters();
1217 DCHECK(!preserved_core_registers.IsEmpty());
1218 uint32_t fp_spills_offset = frame_size - FrameEntrySpillSize();
1219 CPURegList preserved_fp_registers = GetFramePreservedFPRegisters();
1220
1221 CPURegister lowest_spill;
1222 if (core_spills_offset == kXRegSizeInBytes) {
1223 // If there is no gap between the method and the lowest core spill, use
1224 // aligned LDP pre-index to pop both. Max difference is 504. We do
1225 // that to reduce code size even though the loaded method is unused.
1226 DCHECK_LE(frame_size, 504); // 32 core registers are only 256 bytes.
1227 lowest_spill = preserved_core_registers.PopLowestIndex();
1228 core_spills_offset += kXRegSizeInBytes;
1229 }
1230 GetAssembler()->UnspillRegisters(preserved_fp_registers, fp_spills_offset);
1231 GetAssembler()->UnspillRegisters(preserved_core_registers, core_spills_offset);
1232 if (lowest_spill.IsValid()) {
1233 __ Ldp(xzr, lowest_spill, MemOperand(sp, frame_size, PostIndex));
1234 GetAssembler()->cfi().Restore(DWARFReg(lowest_spill));
1235 } else {
1236 __ Drop(frame_size);
1237 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001238 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001239 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001240 __ Ret();
1241 GetAssembler()->cfi().RestoreState();
1242 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001243}
1244
Scott Wakeling97c72b72016-06-24 16:19:36 +01001245CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001246 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001247 return CPURegList(CPURegister::kRegister, kXRegSize,
1248 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001249}
1250
Scott Wakeling97c72b72016-06-24 16:19:36 +01001251CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001252 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1253 GetNumberOfFloatingPointRegisters()));
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001254 return CPURegList(CPURegister::kVRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001255 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001256}
1257
Alexandre Rames5319def2014-10-23 10:03:10 +01001258void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1259 __ Bind(GetLabelOf(block));
1260}
1261
Calin Juravle175dc732015-08-25 15:42:32 +01001262void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1263 DCHECK(location.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001264 __ Mov(RegisterFrom(location, DataType::Type::kInt32), value);
Calin Juravle175dc732015-08-25 15:42:32 +01001265}
1266
Calin Juravlee460d1d2015-09-29 04:52:17 +01001267void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1268 if (location.IsRegister()) {
1269 locations->AddTemp(location);
1270 } else {
1271 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1272 }
1273}
1274
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001275void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001276 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001277 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001278 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001279 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001280 if (value_can_be_null) {
1281 __ Cbz(value, &done);
1282 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001283 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07001284 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01001285 // Calculate the offset (in the card table) of the card corresponding to
1286 // `object`.
Alexandre Rames5319def2014-10-23 10:03:10 +01001287 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Roland Levillainc73f0522018-08-14 15:16:50 +01001288 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1289 // `object`'s card.
1290 //
1291 // Register `card` contains the address of the card table. Note that the card
1292 // table's base is biased during its creation so that it always starts at an
1293 // address whose least-significant byte is equal to `kCardDirty` (see
1294 // art::gc::accounting::CardTable::Create). Therefore the STRB instruction
1295 // below writes the `kCardDirty` (byte) value into the `object`'s card
1296 // (located at `card + object >> kCardShift`).
1297 //
1298 // This dual use of the value in register `card` (1. to calculate the location
1299 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1300 // (no need to explicitly load `kCardDirty` as an immediate value).
Serban Constantinescu02164b32014-11-13 14:05:07 +00001301 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001302 if (value_can_be_null) {
1303 __ Bind(&done);
1304 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001305}
1306
David Brazdil58282f42016-01-14 12:45:10 +00001307void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001308 // Blocked core registers:
1309 // lr : Runtime reserved.
1310 // tr : Runtime reserved.
Roland Levillain97c46462017-05-11 14:04:03 +01001311 // mr : Runtime reserved.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001312 // ip1 : VIXL core temp.
1313 // ip0 : VIXL core temp.
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001314 // x18 : Platform register.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001315 //
1316 // Blocked fp registers:
1317 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001318 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1319 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001320 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001321 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001322 }
Peter Collingbournebd8e10c2018-04-12 16:39:55 -07001323 blocked_core_registers_[X18] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001324
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001325 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001326 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001327 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001328 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001329
David Brazdil58282f42016-01-14 12:45:10 +00001330 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001331 // Stubs do not save callee-save floating point registers. If the graph
1332 // is debuggable, we need to deal with these registers differently. For
1333 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001334 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1335 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001336 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001337 }
1338 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001339}
1340
Alexandre Rames3e69f162014-12-10 10:36:50 +00001341size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1342 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1343 __ Str(reg, MemOperand(sp, stack_index));
1344 return kArm64WordSize;
1345}
1346
1347size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1348 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1349 __ Ldr(reg, MemOperand(sp, stack_index));
1350 return kArm64WordSize;
1351}
1352
Artem Serov9df37b92019-07-23 16:41:54 +01001353size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1354 uint32_t reg_id ATTRIBUTE_UNUSED) {
1355 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1356 << "use SaveRestoreLiveRegistersHelper";
1357 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001358}
1359
Artem Serov9df37b92019-07-23 16:41:54 +01001360size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index ATTRIBUTE_UNUSED,
1361 uint32_t reg_id ATTRIBUTE_UNUSED) {
1362 LOG(FATAL) << "FP registers shouldn't be saved/restored individually, "
1363 << "use SaveRestoreLiveRegistersHelper";
1364 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00001365}
1366
Alexandre Rames5319def2014-10-23 10:03:10 +01001367void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001368 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001369}
1370
1371void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001372 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001373}
1374
Vladimir Markoa0431112018-06-25 09:32:54 +01001375const Arm64InstructionSetFeatures& CodeGeneratorARM64::GetInstructionSetFeatures() const {
1376 return *GetCompilerOptions().GetInstructionSetFeatures()->AsArm64InstructionSetFeatures();
1377}
1378
Alexandre Rames67555f72014-11-18 10:55:16 +00001379void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001380 if (constant->IsIntConstant()) {
1381 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1382 } else if (constant->IsLongConstant()) {
1383 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1384 } else if (constant->IsNullConstant()) {
1385 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001386 } else if (constant->IsFloatConstant()) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001387 __ Fmov(VRegister(destination), constant->AsFloatConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001388 } else {
1389 DCHECK(constant->IsDoubleConstant());
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001390 __ Fmov(VRegister(destination), constant->AsDoubleConstant()->GetValue());
Alexandre Rames67555f72014-11-18 10:55:16 +00001391 }
1392}
1393
Alexandre Rames3e69f162014-12-10 10:36:50 +00001394
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001395static bool CoherentConstantAndType(Location constant, DataType::Type type) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001396 DCHECK(constant.IsConstant());
1397 HConstant* cst = constant.GetConstant();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001398 return (cst->IsIntConstant() && type == DataType::Type::kInt32) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001399 // Null is mapped to a core W register, which we associate with kPrimInt.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001400 (cst->IsNullConstant() && type == DataType::Type::kInt32) ||
1401 (cst->IsLongConstant() && type == DataType::Type::kInt64) ||
1402 (cst->IsFloatConstant() && type == DataType::Type::kFloat32) ||
1403 (cst->IsDoubleConstant() && type == DataType::Type::kFloat64);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001404}
1405
Roland Levillain952b2352017-05-03 19:49:14 +01001406// Allocate a scratch register from the VIXL pool, querying first
1407// the floating-point register pool, and then the core register
1408// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001409// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1410// using a different allocation strategy.
1411static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1412 vixl::aarch64::UseScratchRegisterScope* temps,
1413 int size_in_bits) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001414 return masm->GetScratchVRegisterList()->IsEmpty()
Roland Levillain558dea12017-01-27 19:40:44 +00001415 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1416 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1417}
1418
Calin Juravlee460d1d2015-09-29 04:52:17 +01001419void CodeGeneratorARM64::MoveLocation(Location destination,
1420 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001421 DataType::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001422 if (source.Equals(destination)) {
1423 return;
1424 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001425
1426 // A valid move can always be inferred from the destination and source
1427 // locations. When moving from and to a register, the argument type can be
1428 // used to generate 32bit instead of 64bit moves. In debug mode we also
1429 // checks the coherency of the locations and the type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001430 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001431
1432 if (destination.IsRegister() || destination.IsFpuRegister()) {
1433 if (unspecified_type) {
1434 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1435 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001436 (src_cst != nullptr && (src_cst->IsIntConstant()
1437 || src_cst->IsFloatConstant()
1438 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001439 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001440 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexandre Rames67555f72014-11-18 10:55:16 +00001441 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001442 // If the source is a double stack slot or a 64bit constant, a 64bit
1443 // type is appropriate. Else the source is a register, and since the
1444 // type has not been specified, we chose a 64bit type to force a 64bit
1445 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001446 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexandre Rames67555f72014-11-18 10:55:16 +00001447 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001448 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001449 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1450 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001451 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001452 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1453 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1454 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001455 } else if (source.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001456 GetInstructionCodeGeneratorArm64()->LoadSIMDRegFromStack(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001457 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001458 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001459 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001460 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001461 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001462 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001463 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001464 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001465 DataType::Type source_type = DataType::Is64BitType(dst_type)
1466 ? DataType::Type::kInt64
1467 : DataType::Type::kInt32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001468 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1469 }
1470 } else {
1471 DCHECK(source.IsFpuRegister());
1472 if (destination.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001473 DataType::Type source_type = DataType::Is64BitType(dst_type)
1474 ? DataType::Type::kFloat64
1475 : DataType::Type::kFloat32;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001476 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1477 } else {
1478 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001479 if (GetGraph()->HasSIMD()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001480 GetInstructionCodeGeneratorArm64()->MoveSIMDRegToSIMDReg(destination, source);
Artem Serovd4bccf12017-04-03 18:47:32 +01001481 } else {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001482 __ Fmov(VRegister(dst), FPRegisterFrom(source, dst_type));
Artem Serovd4bccf12017-04-03 18:47:32 +01001483 }
1484 }
1485 }
1486 } else if (destination.IsSIMDStackSlot()) {
Artem Serov1a719e42019-07-18 14:24:55 +01001487 GetInstructionCodeGeneratorArm64()->MoveToSIMDStackSlot(destination, source);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001488 } else { // The destination is not a register. It must be a stack slot.
1489 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1490 if (source.IsRegister() || source.IsFpuRegister()) {
1491 if (unspecified_type) {
1492 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001493 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001494 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001495 dst_type =
1496 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001497 }
1498 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001499 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1500 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001501 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001502 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001503 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1504 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001505 UseScratchRegisterScope temps(GetVIXLAssembler());
1506 HConstant* src_cst = source.GetConstant();
1507 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001508 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001509 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1510 ? Register(xzr)
1511 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001512 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001513 if (src_cst->IsIntConstant()) {
1514 temp = temps.AcquireW();
1515 } else if (src_cst->IsLongConstant()) {
1516 temp = temps.AcquireX();
1517 } else if (src_cst->IsFloatConstant()) {
1518 temp = temps.AcquireS();
1519 } else {
1520 DCHECK(src_cst->IsDoubleConstant());
1521 temp = temps.AcquireD();
1522 }
1523 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001524 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001525 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001526 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001527 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001528 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001529 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001530 // Use any scratch register (a core or a floating-point one)
1531 // from VIXL scratch register pools as a temporary.
1532 //
1533 // We used to only use the FP scratch register pool, but in some
1534 // rare cases the only register from this pool (D31) would
1535 // already be used (e.g. within a ParallelMove instruction, when
1536 // a move is blocked by a another move requiring a scratch FP
1537 // register, which would reserve D31). To prevent this issue, we
1538 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001539 //
1540 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001541 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001542 // use AcquireFPOrCoreCPURegisterOfSize instead of
1543 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1544 // allocates core scratch registers first.
1545 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1546 GetVIXLAssembler(),
1547 &temps,
1548 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001549 __ Ldr(temp, StackOperandFrom(source));
1550 __ Str(temp, StackOperandFrom(destination));
1551 }
1552 }
1553}
1554
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001555void CodeGeneratorARM64::Load(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001556 CPURegister dst,
1557 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001558 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001559 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001560 case DataType::Type::kUint8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001561 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001562 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001563 case DataType::Type::kInt8:
Alexandre Rames67555f72014-11-18 10:55:16 +00001564 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001565 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001566 case DataType::Type::kUint16:
Alexandre Rames67555f72014-11-18 10:55:16 +00001567 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001568 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001569 case DataType::Type::kInt16:
1570 __ Ldrsh(Register(dst), src);
1571 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001572 case DataType::Type::kInt32:
1573 case DataType::Type::kReference:
1574 case DataType::Type::kInt64:
1575 case DataType::Type::kFloat32:
1576 case DataType::Type::kFloat64:
1577 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001578 __ Ldr(dst, src);
1579 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001580 case DataType::Type::kUint32:
1581 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001582 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001583 LOG(FATAL) << "Unreachable type " << type;
1584 }
1585}
1586
Calin Juravle77520bc2015-01-12 18:45:46 +00001587void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001588 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001589 const MemOperand& src,
1590 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001591 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001592 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001593 Register temp_base = temps.AcquireX();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001594 DataType::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001595
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001596 DCHECK(!src.IsPreIndex());
1597 DCHECK(!src.IsPostIndex());
1598
1599 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001600 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001601 {
1602 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1603 MemOperand base = MemOperand(temp_base);
1604 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001605 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001606 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001607 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001608 {
1609 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1610 __ ldarb(Register(dst), base);
1611 if (needs_null_check) {
1612 MaybeRecordImplicitNullCheck(instruction);
1613 }
1614 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001615 if (type == DataType::Type::kInt8) {
1616 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
Artem Serov914d7a82017-02-07 14:33:49 +00001617 }
1618 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001619 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001620 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001621 {
1622 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1623 __ ldarh(Register(dst), base);
1624 if (needs_null_check) {
1625 MaybeRecordImplicitNullCheck(instruction);
1626 }
1627 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001628 if (type == DataType::Type::kInt16) {
1629 __ Sbfx(Register(dst), Register(dst), 0, DataType::Size(type) * kBitsPerByte);
1630 }
Artem Serov914d7a82017-02-07 14:33:49 +00001631 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001632 case DataType::Type::kInt32:
1633 case DataType::Type::kReference:
1634 case DataType::Type::kInt64:
1635 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001636 {
1637 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1638 __ ldar(Register(dst), base);
1639 if (needs_null_check) {
1640 MaybeRecordImplicitNullCheck(instruction);
1641 }
1642 }
1643 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001644 case DataType::Type::kFloat32:
1645 case DataType::Type::kFloat64: {
Artem Serov914d7a82017-02-07 14:33:49 +00001646 DCHECK(dst.IsFPRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001647 DCHECK_EQ(dst.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001648
Artem Serov914d7a82017-02-07 14:33:49 +00001649 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1650 {
1651 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1652 __ ldar(temp, base);
1653 if (needs_null_check) {
1654 MaybeRecordImplicitNullCheck(instruction);
1655 }
1656 }
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001657 __ Fmov(VRegister(dst), temp);
Artem Serov914d7a82017-02-07 14:33:49 +00001658 break;
Roland Levillain44015862016-01-22 11:47:17 +00001659 }
Aart Bik66c158e2018-01-31 12:55:04 -08001660 case DataType::Type::kUint32:
1661 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001662 case DataType::Type::kVoid:
Artem Serov914d7a82017-02-07 14:33:49 +00001663 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001664 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001665 }
1666}
1667
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001668void CodeGeneratorARM64::Store(DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001669 CPURegister src,
1670 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001671 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001672 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001673 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001674 case DataType::Type::kInt8:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001675 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001676 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001677 case DataType::Type::kUint16:
1678 case DataType::Type::kInt16:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001679 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001680 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001681 case DataType::Type::kInt32:
1682 case DataType::Type::kReference:
1683 case DataType::Type::kInt64:
1684 case DataType::Type::kFloat32:
1685 case DataType::Type::kFloat64:
1686 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001687 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001688 break;
Aart Bik66c158e2018-01-31 12:55:04 -08001689 case DataType::Type::kUint32:
1690 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001691 case DataType::Type::kVoid:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001692 LOG(FATAL) << "Unreachable type " << type;
1693 }
1694}
1695
Artem Serov914d7a82017-02-07 14:33:49 +00001696void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001697 DataType::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001698 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001699 const MemOperand& dst,
1700 bool needs_null_check) {
1701 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001702 UseScratchRegisterScope temps(GetVIXLAssembler());
1703 Register temp_base = temps.AcquireX();
1704
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001705 DCHECK(!dst.IsPreIndex());
1706 DCHECK(!dst.IsPostIndex());
1707
1708 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001709 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001710 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001711 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001712 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001713 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001714 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001715 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001716 case DataType::Type::kInt8:
Artem Serov914d7a82017-02-07 14:33:49 +00001717 {
1718 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1719 __ stlrb(Register(src), base);
1720 if (needs_null_check) {
1721 MaybeRecordImplicitNullCheck(instruction);
1722 }
1723 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001724 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001725 case DataType::Type::kUint16:
1726 case DataType::Type::kInt16:
Artem Serov914d7a82017-02-07 14:33:49 +00001727 {
1728 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1729 __ stlrh(Register(src), base);
1730 if (needs_null_check) {
1731 MaybeRecordImplicitNullCheck(instruction);
1732 }
1733 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001734 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001735 case DataType::Type::kInt32:
1736 case DataType::Type::kReference:
1737 case DataType::Type::kInt64:
1738 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001739 {
1740 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1741 __ stlr(Register(src), base);
1742 if (needs_null_check) {
1743 MaybeRecordImplicitNullCheck(instruction);
1744 }
1745 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001746 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001747 case DataType::Type::kFloat32:
1748 case DataType::Type::kFloat64: {
1749 DCHECK_EQ(src.Is64Bits(), DataType::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001750 Register temp_src;
1751 if (src.IsZero()) {
1752 // The zero register is used to avoid synthesizing zero constants.
1753 temp_src = Register(src);
1754 } else {
1755 DCHECK(src.IsFPRegister());
1756 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01001757 __ Fmov(temp_src, VRegister(src));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001758 }
Artem Serov914d7a82017-02-07 14:33:49 +00001759 {
1760 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1761 __ stlr(temp_src, base);
1762 if (needs_null_check) {
1763 MaybeRecordImplicitNullCheck(instruction);
1764 }
1765 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001766 break;
1767 }
Aart Bik66c158e2018-01-31 12:55:04 -08001768 case DataType::Type::kUint32:
1769 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001770 case DataType::Type::kVoid:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001771 LOG(FATAL) << "Unreachable type " << type;
1772 }
1773}
1774
Calin Juravle175dc732015-08-25 15:42:32 +01001775void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1776 HInstruction* instruction,
1777 uint32_t dex_pc,
1778 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001779 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001780
Vladimir Markof6675082019-05-17 12:05:28 +01001781 ThreadOffset64 entrypoint_offset = GetThreadOffset<kArm64PointerSize>(entrypoint);
1782 // Reduce code size for AOT by using shared trampolines for slow path runtime calls across the
1783 // entire oat file. This adds an extra branch and we do not want to slow down the main path.
1784 // For JIT, thunk sharing is per-method, so the gains would be smaller or even negative.
1785 if (slow_path == nullptr || Runtime::Current()->UseJitCompilation()) {
1786 __ Ldr(lr, MemOperand(tr, entrypoint_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00001787 // Ensure the pc position is recorded immediately after the `blr` instruction.
1788 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1789 __ blr(lr);
1790 if (EntrypointRequiresStackMap(entrypoint)) {
1791 RecordPcInfo(instruction, dex_pc, slow_path);
1792 }
Vladimir Markof6675082019-05-17 12:05:28 +01001793 } else {
1794 // Ensure the pc position is recorded immediately after the `bl` instruction.
1795 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1796 EmitEntrypointThunkCall(entrypoint_offset);
1797 if (EntrypointRequiresStackMap(entrypoint)) {
1798 RecordPcInfo(instruction, dex_pc, slow_path);
1799 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001800 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001801}
1802
Roland Levillaindec8f632016-07-22 17:10:06 +01001803void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1804 HInstruction* instruction,
1805 SlowPathCode* slow_path) {
1806 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001807 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1808 __ Blr(lr);
1809}
1810
Alexandre Rames67555f72014-11-18 10:55:16 +00001811void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001812 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001813 UseScratchRegisterScope temps(GetVIXLAssembler());
1814 Register temp = temps.AcquireW();
Vladimir Markodc682aa2018-01-04 18:42:57 +00001815 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001816 const size_t status_byte_offset =
1817 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1818 constexpr uint32_t shifted_visibly_initialized_value =
1819 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001820
Vladimir Marko2bb44fe2019-10-04 12:28:14 +01001821 // CMP (immediate) is limited to imm12 or imm12<<12, so we would need to materialize
1822 // the constant 0xf0000000 for comparison with the full 32-bit field. To reduce the code
1823 // size, load only the high byte of the field and compare with 0xf0.
1824 // Note: The same code size could be achieved with LDR+MNV(asr #24)+CBNZ but benchmarks
1825 // show that this pattern is slower (tested on little cores).
1826 __ Ldrb(temp, HeapOperand(class_reg, status_byte_offset));
1827 __ Cmp(temp, shifted_visibly_initialized_value);
1828 __ B(lo, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001829 __ Bind(slow_path->GetExitLabel());
1830}
Alexandre Rames5319def2014-10-23 10:03:10 +01001831
Vladimir Marko175e7862018-03-27 09:03:13 +00001832void InstructionCodeGeneratorARM64::GenerateBitstringTypeCheckCompare(
1833 HTypeCheckInstruction* check, vixl::aarch64::Register temp) {
1834 uint32_t path_to_root = check->GetBitstringPathToRoot();
1835 uint32_t mask = check->GetBitstringMask();
1836 DCHECK(IsPowerOfTwo(mask + 1));
1837 size_t mask_bits = WhichPowerOf2(mask + 1);
1838
1839 if (mask_bits == 16u) {
1840 // Load only the bitstring part of the status word.
1841 __ Ldrh(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1842 } else {
1843 // /* uint32_t */ temp = temp->status_
1844 __ Ldr(temp, HeapOperand(temp, mirror::Class::StatusOffset()));
1845 // Extract the bitstring bits.
1846 __ Ubfx(temp, temp, 0, mask_bits);
1847 }
1848 // Compare the bitstring bits to `path_to_root`.
1849 __ Cmp(temp, path_to_root);
1850}
1851
Roland Levillain44015862016-01-22 11:47:17 +00001852void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001853 BarrierType type = BarrierAll;
1854
1855 switch (kind) {
1856 case MemBarrierKind::kAnyAny:
1857 case MemBarrierKind::kAnyStore: {
1858 type = BarrierAll;
1859 break;
1860 }
1861 case MemBarrierKind::kLoadAny: {
1862 type = BarrierReads;
1863 break;
1864 }
1865 case MemBarrierKind::kStoreStore: {
1866 type = BarrierWrites;
1867 break;
1868 }
1869 default:
1870 LOG(FATAL) << "Unexpected memory barrier " << kind;
1871 }
1872 __ Dmb(InnerShareable, type);
1873}
1874
Serban Constantinescu02164b32014-11-13 14:05:07 +00001875void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1876 HBasicBlock* successor) {
1877 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001878 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1879 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001880 slow_path =
1881 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathARM64(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001882 instruction->SetSlowPath(slow_path);
1883 codegen_->AddSlowPath(slow_path);
1884 if (successor != nullptr) {
1885 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001886 }
1887 } else {
1888 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1889 }
1890
Serban Constantinescu02164b32014-11-13 14:05:07 +00001891 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1892 Register temp = temps.AcquireW();
1893
Andreas Gampe542451c2016-07-26 09:02:02 -07001894 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001895 if (successor == nullptr) {
1896 __ Cbnz(temp, slow_path->GetEntryLabel());
1897 __ Bind(slow_path->GetReturnLabel());
1898 } else {
1899 __ Cbz(temp, codegen_->GetLabelOf(successor));
1900 __ B(slow_path->GetEntryLabel());
1901 // slow_path will return to GetLabelOf(successor).
1902 }
1903}
1904
Alexandre Rames5319def2014-10-23 10:03:10 +01001905InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1906 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001907 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001908 assembler_(codegen->GetAssembler()),
1909 codegen_(codegen) {}
1910
Alexandre Rames67555f72014-11-18 10:55:16 +00001911void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001912 DCHECK_EQ(instr->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001913 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001914 DataType::Type type = instr->GetResultType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001915 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001916 case DataType::Type::kInt32:
1917 case DataType::Type::kInt64:
Alexandre Rames5319def2014-10-23 10:03:10 +01001918 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001919 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001920 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001921 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001922
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001923 case DataType::Type::kFloat32:
1924 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001925 locations->SetInAt(0, Location::RequiresFpuRegister());
1926 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001927 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001928 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001929
Alexandre Rames5319def2014-10-23 10:03:10 +01001930 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001931 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001932 }
1933}
1934
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001935void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
1936 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001937 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1938
1939 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001940 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Rames09a99962015-04-15 11:47:56 +01001941 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001942 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
1943 object_field_get_with_read_barrier
1944 ? LocationSummary::kCallOnSlowPath
1945 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01001946 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001947 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko0ecac682018-08-07 10:40:38 +01001948 // We need a temporary register for the read barrier load in
1949 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
1950 // only if the field is volatile or the offset is too big.
1951 if (field_info.IsVolatile() ||
1952 field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
1953 locations->AddTemp(FixedTempLocation());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001954 }
Vladimir Marko70e97462016-08-09 11:04:26 +01001955 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001956 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001957 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001958 locations->SetOut(Location::RequiresFpuRegister());
1959 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001960 // The output overlaps for an object field get when read barriers
1961 // are enabled: we do not want the load to overwrite the object's
1962 // location, as we need it to emit the read barrier.
1963 locations->SetOut(
1964 Location::RequiresRegister(),
1965 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001966 }
1967}
1968
1969void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1970 const FieldInfo& field_info) {
1971 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001972 LocationSummary* locations = instruction->GetLocations();
1973 Location base_loc = locations->InAt(0);
1974 Location out = locations->Out();
1975 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Vladimir Marko61b92282017-10-11 13:23:17 +01001976 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
1977 DataType::Type load_type = instruction->GetType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001978 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001979
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001980 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier &&
Vladimir Marko61b92282017-10-11 13:23:17 +01001981 load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00001982 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00001983 // /* HeapReference<Object> */ out = *(base + offset)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001984 Register base = RegisterFrom(base_loc, DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001985 Location maybe_temp =
1986 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00001987 // Note that potential implicit null checks are handled in this
1988 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1989 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1990 instruction,
1991 out,
1992 base,
1993 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001994 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08001995 /* needs_null_check= */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001996 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001997 } else {
1998 // General case.
1999 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002000 // Note that a potential implicit null check is handled in this
2001 // CodeGeneratorARM64::LoadAcquire call.
2002 // NB: LoadAcquire will record the pc info if needed.
2003 codegen_->LoadAcquire(
Andreas Gampe3db70682018-12-26 15:12:03 -08002004 instruction, OutputCPURegister(instruction), field, /* needs_null_check= */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002005 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002006 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2007 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Vladimir Marko61b92282017-10-11 13:23:17 +01002008 codegen_->Load(load_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002009 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002010 }
Vladimir Marko61b92282017-10-11 13:23:17 +01002011 if (load_type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002012 // If read barriers are enabled, emit read barriers other than
2013 // Baker's using a slow path (and also unpoison the loaded
2014 // reference, if heap poisoning is enabled).
2015 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2016 }
Roland Levillain4d027112015-07-01 15:41:14 +01002017 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002018}
2019
2020void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2021 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002022 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01002023 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002024 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2025 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002026 } else if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002027 locations->SetInAt(1, Location::RequiresFpuRegister());
2028 } else {
2029 locations->SetInAt(1, Location::RequiresRegister());
2030 }
2031}
2032
2033void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002034 const FieldInfo& field_info,
2035 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002036 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2037
2038 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002039 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002040 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002041 Offset offset = field_info.GetFieldOffset();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002042 DataType::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002043
Roland Levillain4d027112015-07-01 15:41:14 +01002044 {
2045 // We use a block to end the scratch scope before the write barrier, thus
2046 // freeing the temporary registers so they can be used in `MarkGCCard`.
2047 UseScratchRegisterScope temps(GetVIXLAssembler());
2048
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002049 if (kPoisonHeapReferences && field_type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002050 DCHECK(value.IsW());
2051 Register temp = temps.AcquireW();
2052 __ Mov(temp, value.W());
2053 GetAssembler()->PoisonHeapReference(temp.W());
2054 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002055 }
Roland Levillain4d027112015-07-01 15:41:14 +01002056
2057 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002058 codegen_->StoreRelease(
Andreas Gampe3db70682018-12-26 15:12:03 -08002059 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check= */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002060 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002061 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2062 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002063 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2064 codegen_->MaybeRecordImplicitNullCheck(instruction);
2065 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002066 }
2067
2068 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002069 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002070 }
2071}
2072
Alexandre Rames67555f72014-11-18 10:55:16 +00002073void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002074 DataType::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002075
2076 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002077 case DataType::Type::kInt32:
2078 case DataType::Type::kInt64: {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002079 Register dst = OutputRegister(instr);
2080 Register lhs = InputRegisterAt(instr, 0);
2081 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002082 if (instr->IsAdd()) {
2083 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002084 } else if (instr->IsAnd()) {
2085 __ And(dst, lhs, rhs);
2086 } else if (instr->IsOr()) {
2087 __ Orr(dst, lhs, rhs);
2088 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002089 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002090 } else if (instr->IsRor()) {
2091 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002092 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002093 __ Ror(dst, lhs, shift);
2094 } else {
2095 // Ensure shift distance is in the same size register as the result. If
2096 // we are rotating a long and the shift comes in a w register originally,
2097 // we don't need to sxtw for use as an x since the shift distances are
2098 // all & reg_bits - 1.
2099 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2100 }
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002101 } else if (instr->IsMin() || instr->IsMax()) {
2102 __ Cmp(lhs, rhs);
2103 __ Csel(dst, lhs, rhs, instr->IsMin() ? lt : gt);
Alexandre Rames67555f72014-11-18 10:55:16 +00002104 } else {
2105 DCHECK(instr->IsXor());
2106 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002107 }
2108 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002109 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002110 case DataType::Type::kFloat32:
2111 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002112 VRegister dst = OutputFPRegister(instr);
2113 VRegister lhs = InputFPRegisterAt(instr, 0);
2114 VRegister rhs = InputFPRegisterAt(instr, 1);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002115 if (instr->IsAdd()) {
2116 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002117 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002118 __ Fsub(dst, lhs, rhs);
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01002119 } else if (instr->IsMin()) {
2120 __ Fmin(dst, lhs, rhs);
2121 } else if (instr->IsMax()) {
2122 __ Fmax(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002123 } else {
2124 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002125 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002126 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002127 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002128 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002129 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002130 }
2131}
2132
Serban Constantinescu02164b32014-11-13 14:05:07 +00002133void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2134 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2135
Vladimir Markoca6fff82017-10-03 14:49:14 +01002136 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002137 DataType::Type type = instr->GetResultType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002138 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002139 case DataType::Type::kInt32:
2140 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002141 locations->SetInAt(0, Location::RequiresRegister());
2142 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002143 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002144 break;
2145 }
2146 default:
2147 LOG(FATAL) << "Unexpected shift type " << type;
2148 }
2149}
2150
2151void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2152 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002154 DataType::Type type = instr->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002155 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002156 case DataType::Type::kInt32:
2157 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002158 Register dst = OutputRegister(instr);
2159 Register lhs = InputRegisterAt(instr, 0);
2160 Operand rhs = InputOperandAt(instr, 1);
2161 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002162 uint32_t shift_value = rhs.GetImmediate() &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002163 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002164 if (instr->IsShl()) {
2165 __ Lsl(dst, lhs, shift_value);
2166 } else if (instr->IsShr()) {
2167 __ Asr(dst, lhs, shift_value);
2168 } else {
2169 __ Lsr(dst, lhs, shift_value);
2170 }
2171 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002172 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002173
2174 if (instr->IsShl()) {
2175 __ Lsl(dst, lhs, rhs_reg);
2176 } else if (instr->IsShr()) {
2177 __ Asr(dst, lhs, rhs_reg);
2178 } else {
2179 __ Lsr(dst, lhs, rhs_reg);
2180 }
2181 }
2182 break;
2183 }
2184 default:
2185 LOG(FATAL) << "Unexpected shift operation type " << type;
2186 }
2187}
2188
Alexandre Rames5319def2014-10-23 10:03:10 +01002189void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002190 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002191}
2192
2193void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002194 HandleBinaryOp(instruction);
2195}
2196
2197void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2198 HandleBinaryOp(instruction);
2199}
2200
2201void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2202 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002203}
2204
Artem Serov7fc63502016-02-09 17:15:29 +00002205void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002206 DCHECK(DataType::IsIntegralType(instr->GetType())) << instr->GetType();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002207 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002208 locations->SetInAt(0, Location::RequiresRegister());
2209 // There is no immediate variant of negated bitwise instructions in AArch64.
2210 locations->SetInAt(1, Location::RequiresRegister());
2211 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2212}
2213
Artem Serov7fc63502016-02-09 17:15:29 +00002214void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002215 Register dst = OutputRegister(instr);
2216 Register lhs = InputRegisterAt(instr, 0);
2217 Register rhs = InputRegisterAt(instr, 1);
2218
2219 switch (instr->GetOpKind()) {
2220 case HInstruction::kAnd:
2221 __ Bic(dst, lhs, rhs);
2222 break;
2223 case HInstruction::kOr:
2224 __ Orn(dst, lhs, rhs);
2225 break;
2226 case HInstruction::kXor:
2227 __ Eon(dst, lhs, rhs);
2228 break;
2229 default:
2230 LOG(FATAL) << "Unreachable";
2231 }
2232}
2233
Anton Kirilov74234da2017-01-13 14:42:47 +00002234void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2235 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002236 DCHECK(instruction->GetType() == DataType::Type::kInt32 ||
2237 instruction->GetType() == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002238 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002239 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Rames8626b742015-11-25 16:28:08 +00002240 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2241 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2242 } else {
2243 locations->SetInAt(0, Location::RequiresRegister());
2244 }
2245 locations->SetInAt(1, Location::RequiresRegister());
2246 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2247}
2248
Anton Kirilov74234da2017-01-13 14:42:47 +00002249void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2250 HDataProcWithShifterOp* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002251 DataType::Type type = instruction->GetType();
Alexandre Rames8626b742015-11-25 16:28:08 +00002252 HInstruction::InstructionKind kind = instruction->GetInstrKind();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002253 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Alexandre Rames8626b742015-11-25 16:28:08 +00002254 Register out = OutputRegister(instruction);
2255 Register left;
2256 if (kind != HInstruction::kNeg) {
2257 left = InputRegisterAt(instruction, 0);
2258 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002259 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002260 // shifter operand operation, the IR generating `right_reg` (input to the type
2261 // conversion) can have a different type from the current instruction's type,
2262 // so we manually indicate the type.
2263 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002264 Operand right_operand(0);
2265
Anton Kirilov74234da2017-01-13 14:42:47 +00002266 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2267 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002268 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2269 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002270 right_operand = Operand(right_reg,
2271 helpers::ShiftFromOpKind(op_kind),
2272 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002273 }
2274
2275 // Logical binary operations do not support extension operations in the
2276 // operand. Note that VIXL would still manage if it was passed by generating
2277 // the extension as a separate instruction.
2278 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2279 DCHECK(!right_operand.IsExtendedRegister() ||
2280 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2281 kind != HInstruction::kNeg));
2282 switch (kind) {
2283 case HInstruction::kAdd:
2284 __ Add(out, left, right_operand);
2285 break;
2286 case HInstruction::kAnd:
2287 __ And(out, left, right_operand);
2288 break;
2289 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002290 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002291 __ Neg(out, right_operand);
2292 break;
2293 case HInstruction::kOr:
2294 __ Orr(out, left, right_operand);
2295 break;
2296 case HInstruction::kSub:
2297 __ Sub(out, left, right_operand);
2298 break;
2299 case HInstruction::kXor:
2300 __ Eor(out, left, right_operand);
2301 break;
2302 default:
2303 LOG(FATAL) << "Unexpected operation kind: " << kind;
2304 UNREACHABLE();
2305 }
2306}
2307
Artem Serov328429f2016-07-06 16:23:04 +01002308void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002309 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002310 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002311 locations->SetInAt(0, Location::RequiresRegister());
2312 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002313 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002314}
2315
Roland Levillain19c54192016-11-04 13:44:09 +00002316void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002317 __ Add(OutputRegister(instruction),
2318 InputRegisterAt(instruction, 0),
2319 Operand(InputOperandAt(instruction, 1)));
2320}
2321
Artem Serove1811ed2017-04-27 16:50:47 +01002322void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2323 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002324 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Artem Serove1811ed2017-04-27 16:50:47 +01002325
2326 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2327
2328 locations->SetInAt(0, Location::RequiresRegister());
2329 // For byte case we don't need to shift the index variable so we can encode the data offset into
2330 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2331 // data offset constant generation out of the loop and reduce the critical path length in the
2332 // loop.
2333 locations->SetInAt(1, shift->GetValue() == 0
2334 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2335 : Location::RequiresRegister());
2336 locations->SetInAt(2, Location::ConstantLocation(shift));
2337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2338}
2339
2340void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2341 HIntermediateAddressIndex* instruction) {
2342 Register index_reg = InputRegisterAt(instruction, 0);
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002343 uint32_t shift = Int64FromLocation(instruction->GetLocations()->InAt(2));
Artem Serove1811ed2017-04-27 16:50:47 +01002344 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2345
2346 if (shift == 0) {
2347 __ Add(OutputRegister(instruction), index_reg, offset);
2348 } else {
2349 Register offset_reg = InputRegisterAt(instruction, 1);
2350 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2351 }
2352}
2353
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002354void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002355 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002356 new (GetGraph()->GetAllocator()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002357 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2358 if (instr->GetOpKind() == HInstruction::kSub &&
2359 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002360 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002361 // Don't allocate register for Mneg instruction.
2362 } else {
2363 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2364 Location::RequiresRegister());
2365 }
2366 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2367 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002368 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2369}
2370
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002371void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002372 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002373 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2374 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002375
2376 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2377 // This fixup should be carried out for all multiply-accumulate instructions:
2378 // madd, msub, smaddl, smsubl, umaddl and umsubl.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002379 if (instr->GetType() == DataType::Type::kInt64 &&
Alexandre Rames418318f2015-11-20 15:55:47 +00002380 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2381 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002382 vixl::aarch64::Instruction* prev =
2383 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002384 if (prev->IsLoadOrStore()) {
2385 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002386 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002387 __ nop();
2388 }
2389 }
2390
2391 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002392 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002393 __ Madd(res, mul_left, mul_right, accumulator);
2394 } else {
2395 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002396 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002397 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002398 __ Mneg(res, mul_left, mul_right);
2399 } else {
2400 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2401 __ Msub(res, mul_left, mul_right, accumulator);
2402 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002403 }
2404}
2405
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002406void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002407 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002408 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002409 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002410 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2411 object_array_get_with_read_barrier
2412 ? LocationSummary::kCallOnSlowPath
2413 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002414 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002415 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002416 if (instruction->GetIndex()->IsConstant()) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002417 // Array loads with constant index are treated as field loads.
Vladimir Marko008e09f32018-08-06 15:42:43 +01002418 // We need a temporary register for the read barrier load in
2419 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier()
2420 // only if the offset is too big.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002421 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2422 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002423 offset += index << DataType::SizeShift(DataType::Type::kReference);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002424 if (offset >= kReferenceLoadMinFarOffset) {
2425 locations->AddTemp(FixedTempLocation());
2426 }
Artem Serov0806f582018-10-11 20:14:20 +01002427 } else if (!instruction->GetArray()->IsIntermediateAddress()) {
Vladimir Marko008e09f32018-08-06 15:42:43 +01002428 // We need a non-scratch temporary for the array data pointer in
Artem Serov0806f582018-10-11 20:14:20 +01002429 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier() for the case with no
2430 // intermediate address.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002431 locations->AddTemp(Location::RequiresRegister());
2432 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002433 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002434 locations->SetInAt(0, Location::RequiresRegister());
2435 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002436 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002437 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2438 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002439 // The output overlaps in the case of an object array get with
2440 // read barriers enabled: we do not want the move to overwrite the
2441 // array's location, as we need it to emit the read barrier.
2442 locations->SetOut(
2443 Location::RequiresRegister(),
2444 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002445 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002446}
2447
2448void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002449 DataType::Type type = instruction->GetType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002450 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002451 LocationSummary* locations = instruction->GetLocations();
2452 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002453 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002454 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002455 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2456 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002457 MacroAssembler* masm = GetVIXLAssembler();
2458 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002459
Artem Serov0806f582018-10-11 20:14:20 +01002460 // The non-Baker read barrier instrumentation of object ArrayGet instructions
Roland Levillain19c54192016-11-04 13:44:09 +00002461 // does not support the HIntermediateAddress instruction.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002462 DCHECK(!((type == DataType::Type::kReference) &&
Roland Levillain19c54192016-11-04 13:44:09 +00002463 instruction->GetArray()->IsIntermediateAddress() &&
Artem Serov0806f582018-10-11 20:14:20 +01002464 kEmitCompilerReadBarrier &&
2465 !kUseBakerReadBarrier));
Roland Levillain19c54192016-11-04 13:44:09 +00002466
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002467 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00002468 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002469 // Note that a potential implicit null check is handled in the
2470 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002471 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002472 if (index.IsConstant()) {
Artem Serov0806f582018-10-11 20:14:20 +01002473 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002474 // Array load with a constant index can be treated as a field load.
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002475 offset += Int64FromLocation(index) << DataType::SizeShift(type);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002476 Location maybe_temp =
2477 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2478 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2479 out,
2480 obj.W(),
2481 offset,
2482 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002483 /* needs_null_check= */ false,
2484 /* use_load_acquire= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002485 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002486 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08002487 instruction, out, obj.W(), offset, index, /* needs_null_check= */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002488 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002489 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002490 // General case.
2491 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002492 Register length;
2493 if (maybe_compressed_char_at) {
2494 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2495 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002496 {
2497 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2498 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2499
2500 if (instruction->GetArray()->IsIntermediateAddress()) {
2501 DCHECK_LT(count_offset, offset);
2502 int64_t adjusted_offset =
2503 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2504 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2505 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2506 } else {
2507 __ Ldr(length, HeapOperand(obj, count_offset));
2508 }
2509 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002510 }
jessicahandojo05765752016-09-09 19:01:32 -07002511 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002512 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002513 if (maybe_compressed_char_at) {
2514 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002515 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2516 "Expecting 0=compressed, 1=uncompressed");
2517 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002518 __ Ldrb(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002519 HeapOperand(obj, offset + Int64FromLocation(index)));
jessicahandojo05765752016-09-09 19:01:32 -07002520 __ B(&done);
2521 __ Bind(&uncompressed_load);
2522 __ Ldrh(Register(OutputCPURegister(instruction)),
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002523 HeapOperand(obj, offset + (Int64FromLocation(index) << 1)));
jessicahandojo05765752016-09-09 19:01:32 -07002524 __ Bind(&done);
2525 } else {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002526 offset += Int64FromLocation(index) << DataType::SizeShift(type);
jessicahandojo05765752016-09-09 19:01:32 -07002527 source = HeapOperand(obj, offset);
2528 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002529 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002530 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002531 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002532 // We do not need to compute the intermediate address from the array: the
2533 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002534 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002535 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002536 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2537 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
Roland Levillain44015862016-01-22 11:47:17 +00002538 }
2539 temp = obj;
2540 } else {
2541 __ Add(temp, obj, offset);
2542 }
jessicahandojo05765752016-09-09 19:01:32 -07002543 if (maybe_compressed_char_at) {
2544 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002545 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2546 "Expecting 0=compressed, 1=uncompressed");
2547 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002548 __ Ldrb(Register(OutputCPURegister(instruction)),
2549 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2550 __ B(&done);
2551 __ Bind(&uncompressed_load);
2552 __ Ldrh(Register(OutputCPURegister(instruction)),
2553 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2554 __ Bind(&done);
2555 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002556 source = HeapOperand(temp, XRegisterFrom(index), LSL, DataType::SizeShift(type));
jessicahandojo05765752016-09-09 19:01:32 -07002557 }
Roland Levillain44015862016-01-22 11:47:17 +00002558 }
jessicahandojo05765752016-09-09 19:01:32 -07002559 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002560 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2561 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002562 codegen_->Load(type, OutputCPURegister(instruction), source);
2563 codegen_->MaybeRecordImplicitNullCheck(instruction);
2564 }
Roland Levillain44015862016-01-22 11:47:17 +00002565
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002566 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +00002567 static_assert(
2568 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2569 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2570 Location obj_loc = locations->InAt(0);
2571 if (index.IsConstant()) {
2572 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2573 } else {
2574 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2575 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002576 }
Roland Levillain4d027112015-07-01 15:41:14 +01002577 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002578}
2579
Alexandre Rames5319def2014-10-23 10:03:10 +01002580void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002581 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002582 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002583 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002584}
2585
2586void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002587 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002588 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002589 {
2590 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2591 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2592 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2593 codegen_->MaybeRecordImplicitNullCheck(instruction);
2594 }
jessicahandojo05765752016-09-09 19:01:32 -07002595 // Mask out compression flag from String's array length.
2596 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002597 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002598 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002599}
2600
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002601void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002602 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002603
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002604 bool needs_type_check = instruction->NeedsTypeCheck();
Vladimir Markoca6fff82017-10-03 14:49:14 +01002605 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002606 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002607 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002608 locations->SetInAt(0, Location::RequiresRegister());
2609 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002610 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2611 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002612 } else if (DataType::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002613 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002614 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002615 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002616 }
2617}
2618
2619void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002620 DataType::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002621 LocationSummary* locations = instruction->GetLocations();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002622 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002623 bool needs_write_barrier =
2624 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002625
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002626 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002627 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002628 CPURegister source = value;
2629 Location index = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002630 size_t offset = mirror::Array::DataOffset(DataType::Size(value_type)).Uint32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002631 MemOperand destination = HeapOperand(array);
2632 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002633
2634 if (!needs_write_barrier) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002635 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002636 if (index.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01002637 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002638 destination = HeapOperand(array, offset);
2639 } else {
2640 UseScratchRegisterScope temps(masm);
2641 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002642 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002643 // We do not need to compute the intermediate address from the array: the
2644 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002645 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002646 if (kIsDebugBuild) {
Artem Serov0806f582018-10-11 20:14:20 +01002647 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
2648 DCHECK(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002649 }
2650 temp = array;
2651 } else {
2652 __ Add(temp, array, offset);
2653 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002654 destination = HeapOperand(temp,
2655 XRegisterFrom(index),
2656 LSL,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002657 DataType::SizeShift(value_type));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002658 }
Artem Serov914d7a82017-02-07 14:33:49 +00002659 {
2660 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2661 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2662 codegen_->Store(value_type, value, destination);
2663 codegen_->MaybeRecordImplicitNullCheck(instruction);
2664 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002665 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002666 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002667
2668 bool can_value_be_null = instruction->GetValueCanBeNull();
2669 vixl::aarch64::Label do_store;
2670 if (can_value_be_null) {
2671 __ Cbz(Register(value), &do_store);
2672 }
2673
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002674 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002675 if (needs_type_check) {
2676 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathARM64(instruction);
2677 codegen_->AddSlowPath(slow_path);
2678
2679 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2680 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2681 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2682
Alexandre Rames97833a02015-04-16 15:07:12 +01002683 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002684 Register temp = temps.AcquireSameSizeAs(array);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002685 Register temp2 = temps.AcquireSameSizeAs(array);
2686
2687 // Note that when Baker read barriers are enabled, the type
2688 // checks are performed without read barriers. This is fine,
2689 // even in the case where a class object is in the from-space
2690 // after the flip, as a comparison involving such a type would
2691 // not produce a false positive; it may of course produce a
2692 // false negative, in which case we would take the ArraySet
2693 // slow path.
2694
2695 // /* HeapReference<Class> */ temp = array->klass_
2696 {
2697 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2698 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2699 __ Ldr(temp, HeapOperand(array, class_offset));
2700 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames97833a02015-04-16 15:07:12 +01002701 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002702 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Alexandre Rames97833a02015-04-16 15:07:12 +01002703
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002704 // /* HeapReference<Class> */ temp = temp->component_type_
2705 __ Ldr(temp, HeapOperand(temp, component_offset));
2706 // /* HeapReference<Class> */ temp2 = value->klass_
2707 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2708 // If heap poisoning is enabled, no need to unpoison `temp`
2709 // nor `temp2`, as we are comparing two poisoned references.
2710 __ Cmp(temp, temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002711
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002712 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2713 vixl::aarch64::Label do_put;
2714 __ B(eq, &do_put);
2715 // If heap poisoning is enabled, the `temp` reference has
2716 // not been unpoisoned yet; unpoison it now.
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002717 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002718
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002719 // /* HeapReference<Class> */ temp = temp->super_class_
2720 __ Ldr(temp, HeapOperand(temp, super_offset));
2721 // If heap poisoning is enabled, no need to unpoison
2722 // `temp`, as we are comparing against null below.
2723 __ Cbnz(temp, slow_path->GetEntryLabel());
2724 __ Bind(&do_put);
Vladimir Markod1ef8732017-04-18 13:55:13 +01002725 } else {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002726 __ B(ne, slow_path->GetEntryLabel());
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002727 }
2728 }
2729
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002730 codegen_->MarkGCCard(array, value.W(), /* value_can_be_null= */ false);
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002731
Vladimir Marko8fa839c2019-05-16 12:50:47 +00002732 if (can_value_be_null) {
2733 DCHECK(do_store.IsLinked());
2734 __ Bind(&do_store);
2735 }
2736
2737 UseScratchRegisterScope temps(masm);
2738 if (kPoisonHeapReferences) {
2739 Register temp_source = temps.AcquireSameSizeAs(array);
2740 DCHECK(value.IsW());
2741 __ Mov(temp_source, value.W());
2742 GetAssembler()->PoisonHeapReference(temp_source);
2743 source = temp_source;
2744 }
2745
2746 if (index.IsConstant()) {
2747 offset += Int64FromLocation(index) << DataType::SizeShift(value_type);
2748 destination = HeapOperand(array, offset);
2749 } else {
2750 Register temp_base = temps.AcquireSameSizeAs(array);
2751 __ Add(temp_base, array, offset);
2752 destination = HeapOperand(temp_base,
2753 XRegisterFrom(index),
2754 LSL,
2755 DataType::SizeShift(value_type));
2756 }
2757
2758 {
2759 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2760 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2761 __ Str(source, destination);
2762
2763 if (can_value_be_null || !needs_type_check) {
2764 codegen_->MaybeRecordImplicitNullCheck(instruction);
2765 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00002766 }
2767
2768 if (slow_path != nullptr) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002769 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002770 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002771 }
2772}
2773
Alexandre Rames67555f72014-11-18 10:55:16 +00002774void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002775 RegisterSet caller_saves = RegisterSet::Empty();
2776 InvokeRuntimeCallingConvention calling_convention;
2777 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2778 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2779 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002780
2781 // If both index and length are constant, we can check the bounds statically and
2782 // generate code accordingly. We want to make sure we generate constant locations
2783 // in that case, regardless of whether they are encodable in the comparison or not.
2784 HInstruction* index = instruction->InputAt(0);
2785 HInstruction* length = instruction->InputAt(1);
2786 bool both_const = index->IsConstant() && length->IsConstant();
2787 locations->SetInAt(0, both_const
2788 ? Location::ConstantLocation(index->AsConstant())
2789 : ARM64EncodableConstantOrRegister(index, instruction));
2790 locations->SetInAt(1, both_const
2791 ? Location::ConstantLocation(length->AsConstant())
2792 : ARM64EncodableConstantOrRegister(length, instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002793}
2794
2795void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Georgia Kouvelibe530852019-01-17 10:46:41 +00002796 LocationSummary* locations = instruction->GetLocations();
2797 Location index_loc = locations->InAt(0);
2798 Location length_loc = locations->InAt(1);
2799
2800 int cmp_first_input = 0;
2801 int cmp_second_input = 1;
2802 Condition cond = hs;
2803
2804 if (index_loc.IsConstant()) {
2805 int64_t index = Int64FromLocation(index_loc);
2806 if (length_loc.IsConstant()) {
2807 int64_t length = Int64FromLocation(length_loc);
2808 if (index < 0 || index >= length) {
2809 BoundsCheckSlowPathARM64* slow_path =
2810 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
2811 codegen_->AddSlowPath(slow_path);
2812 __ B(slow_path->GetEntryLabel());
2813 } else {
2814 // BCE will remove the bounds check if we are guaranteed to pass.
2815 // However, some optimization after BCE may have generated this, and we should not
2816 // generate a bounds check if it is a valid range.
2817 }
2818 return;
2819 }
2820 // Only the index is constant: change the order of the operands and commute the condition
2821 // so we can use an immediate constant for the index (only the second input to a cmp
2822 // instruction can be an immediate).
2823 cmp_first_input = 1;
2824 cmp_second_input = 0;
2825 cond = ls;
2826 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002827 BoundsCheckSlowPathARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002828 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathARM64(instruction);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002829 __ Cmp(InputRegisterAt(instruction, cmp_first_input),
2830 InputOperandAt(instruction, cmp_second_input));
Alexandre Rames67555f72014-11-18 10:55:16 +00002831 codegen_->AddSlowPath(slow_path);
Georgia Kouvelibe530852019-01-17 10:46:41 +00002832 __ B(slow_path->GetEntryLabel(), cond);
Alexandre Rames67555f72014-11-18 10:55:16 +00002833}
2834
Alexandre Rames67555f72014-11-18 10:55:16 +00002835void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2836 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002837 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexandre Rames67555f72014-11-18 10:55:16 +00002838 locations->SetInAt(0, Location::RequiresRegister());
2839 if (check->HasUses()) {
2840 locations->SetOut(Location::SameAsFirstInput());
2841 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01002842 // Rely on the type initialization to save everything we need.
2843 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexandre Rames67555f72014-11-18 10:55:16 +00002844}
2845
2846void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2847 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01002848 SlowPathCodeARM64* slow_path =
2849 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(check->GetLoadClass(), check);
Alexandre Rames67555f72014-11-18 10:55:16 +00002850 codegen_->AddSlowPath(slow_path);
2851 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2852}
2853
Roland Levillain1a653882016-03-18 18:05:57 +00002854static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2855 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2856 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2857}
2858
2859void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01002860 VRegister lhs_reg = InputFPRegisterAt(instruction, 0);
Roland Levillain1a653882016-03-18 18:05:57 +00002861 Location rhs_loc = instruction->GetLocations()->InAt(1);
2862 if (rhs_loc.IsConstant()) {
2863 // 0.0 is the only immediate that can be encoded directly in
2864 // an FCMP instruction.
2865 //
2866 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2867 // specify that in a floating-point comparison, positive zero
2868 // and negative zero are considered equal, so we can use the
2869 // literal 0.0 for both cases here.
2870 //
2871 // Note however that some methods (Float.equal, Float.compare,
2872 // Float.compareTo, Double.equal, Double.compare,
2873 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2874 // StrictMath.min) consider 0.0 to be (strictly) greater than
2875 // -0.0. So if we ever translate calls to these methods into a
2876 // HCompare instruction, we must handle the -0.0 case with
2877 // care here.
2878 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2879 __ Fcmp(lhs_reg, 0.0);
2880 } else {
2881 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2882 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002883}
2884
Serban Constantinescu02164b32014-11-13 14:05:07 +00002885void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002886 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002887 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002888 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002889 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002890 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002891 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002892 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002893 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002894 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002895 case DataType::Type::kInt32:
2896 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002897 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002898 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002899 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2900 break;
2901 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002902 case DataType::Type::kFloat32:
2903 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002904 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002905 locations->SetInAt(1,
2906 IsFloatingPointZeroConstant(compare->InputAt(1))
2907 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2908 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002909 locations->SetOut(Location::RequiresRegister());
2910 break;
2911 }
2912 default:
2913 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2914 }
2915}
2916
2917void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002918 DataType::Type in_type = compare->InputAt(0)->GetType();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002919
2920 // 0 if: left == right
2921 // 1 if: left > right
2922 // -1 if: left < right
2923 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002924 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002925 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002926 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002927 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002928 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002929 case DataType::Type::kInt32:
2930 case DataType::Type::kInt64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002931 Register result = OutputRegister(compare);
2932 Register left = InputRegisterAt(compare, 0);
2933 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002934 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002935 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2936 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002937 break;
2938 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002939 case DataType::Type::kFloat32:
2940 case DataType::Type::kFloat64: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002941 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002942 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002943 __ Cset(result, ne);
2944 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002945 break;
2946 }
2947 default:
2948 LOG(FATAL) << "Unimplemented compare type " << in_type;
2949 }
2950}
2951
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002952void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002953 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002954
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002955 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002956 locations->SetInAt(0, Location::RequiresFpuRegister());
2957 locations->SetInAt(1,
2958 IsFloatingPointZeroConstant(instruction->InputAt(1))
2959 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2960 : Location::RequiresFpuRegister());
2961 } else {
2962 // Integer cases.
2963 locations->SetInAt(0, Location::RequiresRegister());
2964 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2965 }
2966
David Brazdilb3e773e2016-01-26 11:28:37 +00002967 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002969 }
2970}
2971
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002972void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002973 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002974 return;
2975 }
2976
2977 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002978 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002979 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002980
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002981 if (DataType::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002982 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002983 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002984 } else {
2985 // Integer cases.
2986 Register lhs = InputRegisterAt(instruction, 0);
2987 Operand rhs = InputOperandAt(instruction, 1);
2988 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002989 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002990 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002991}
2992
2993#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2994 M(Equal) \
2995 M(NotEqual) \
2996 M(LessThan) \
2997 M(LessThanOrEqual) \
2998 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002999 M(GreaterThanOrEqual) \
3000 M(Below) \
3001 M(BelowOrEqual) \
3002 M(Above) \
3003 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003004#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003005void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3006void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003007FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003008#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003009#undef FOR_EACH_CONDITION_INSTRUCTION
3010
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003011void InstructionCodeGeneratorARM64::GenerateIntDivForPower2Denom(HDiv* instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003012 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003013 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003014 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
3015
3016 Register out = OutputRegister(instruction);
3017 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01003018
3019 if (abs_imm == 2) {
3020 int bits = DataType::Size(instruction->GetResultType()) * kBitsPerByte;
3021 __ Add(out, dividend, Operand(dividend, LSR, bits - 1));
3022 } else {
3023 UseScratchRegisterScope temps(GetVIXLAssembler());
3024 Register temp = temps.AcquireSameSizeAs(out);
3025 __ Add(temp, dividend, abs_imm - 1);
3026 __ Cmp(dividend, 0);
3027 __ Csel(out, temp, dividend, lt);
3028 }
3029
Zheng Xuc6667102015-05-15 16:08:45 +08003030 int ctz_imm = CTZ(abs_imm);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003031 if (imm > 0) {
3032 __ Asr(out, out, ctz_imm);
Zheng Xuc6667102015-05-15 16:08:45 +08003033 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003034 __ Neg(out, Operand(out, ASR, ctz_imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003035 }
3036}
3037
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003038// Return true if the magic number was modified by subtracting 2^32. So dividend needs to be added.
3039static inline bool NeedToAddDividend(int64_t magic_number, int64_t divisor) {
3040 return divisor > 0 && magic_number < 0;
3041}
3042
3043// Return true if the magic number was modified by adding 2^32. So dividend needs to be subtracted.
3044static inline bool NeedToSubDividend(int64_t magic_number, int64_t divisor) {
3045 return divisor < 0 && magic_number > 0;
3046}
3047
3048void InstructionCodeGeneratorARM64::GenerateResultDivRemWithAnyConstant(
3049 bool is_rem,
3050 int final_right_shift,
3051 int64_t magic_number,
3052 int64_t divisor,
3053 Register dividend,
3054 Register temp_result,
3055 Register out,
3056 UseScratchRegisterScope* temps_scope) {
3057 // As magic_number can be modified to fit into 32 bits, check whether the correction is needed.
3058 if (NeedToAddDividend(magic_number, divisor)) {
3059 __ Add(temp_result, temp_result, dividend);
3060 } else if (NeedToSubDividend(magic_number, divisor)) {
3061 __ Sub(temp_result, temp_result, dividend);
3062 }
3063
3064 if (final_right_shift != 0) {
3065 __ Asr(temp_result, temp_result, final_right_shift);
3066 }
3067
3068 Register& result = (is_rem) ? temp_result : out;
3069 __ Add(result, temp_result, Operand(temp_result, LSR, temp_result.GetSizeInBits() - 1));
3070 if (is_rem) {
3071 // TODO: Strength reduction for msub.
3072 Register temp_imm = temps_scope->AcquireSameSizeAs(out);
3073 __ Mov(temp_imm, divisor);
3074 __ Msub(out, temp_result, temp_imm, dividend);
3075 }
3076}
3077
3078void InstructionCodeGeneratorARM64::GenerateInt64DivRemWithAnyConstant(
3079 HBinaryOperation* instruction) {
Zheng Xuc6667102015-05-15 16:08:45 +08003080 DCHECK(instruction->IsDiv() || instruction->IsRem());
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003081 DCHECK(instruction->GetResultType() == DataType::Type::kInt64);
Zheng Xuc6667102015-05-15 16:08:45 +08003082
3083 LocationSummary* locations = instruction->GetLocations();
3084 Location second = locations->InAt(1);
3085 DCHECK(second.IsConstant());
3086
3087 Register out = OutputRegister(instruction);
3088 Register dividend = InputRegisterAt(instruction, 0);
3089 int64_t imm = Int64FromConstant(second.GetConstant());
3090
Zheng Xuc6667102015-05-15 16:08:45 +08003091 int64_t magic;
3092 int shift;
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003093 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ true, &magic, &shift);
Zheng Xuc6667102015-05-15 16:08:45 +08003094
3095 UseScratchRegisterScope temps(GetVIXLAssembler());
3096 Register temp = temps.AcquireSameSizeAs(out);
3097
3098 // temp = get_high(dividend * magic)
3099 __ Mov(temp, magic);
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003100 __ Smulh(temp, dividend, temp);
3101
3102 GenerateResultDivRemWithAnyConstant(/* is_rem= */ instruction->IsRem(),
3103 /* final_right_shift= */ shift,
3104 magic,
3105 imm,
3106 dividend,
3107 temp,
3108 out,
3109 &temps);
3110}
3111
3112void InstructionCodeGeneratorARM64::GenerateInt32DivRemWithAnyConstant(
3113 HBinaryOperation* instruction) {
3114 DCHECK(instruction->IsDiv() || instruction->IsRem());
3115 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
3116
3117 LocationSummary* locations = instruction->GetLocations();
3118 Location second = locations->InAt(1);
3119 DCHECK(second.IsConstant());
3120
3121 Register out = OutputRegister(instruction);
3122 Register dividend = InputRegisterAt(instruction, 0);
3123 int64_t imm = Int64FromConstant(second.GetConstant());
3124
3125 int64_t magic;
3126 int shift;
3127 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
3128 UseScratchRegisterScope temps(GetVIXLAssembler());
3129 Register temp = temps.AcquireSameSizeAs(out);
3130
3131 // temp = get_high(dividend * magic)
3132 __ Mov(temp, magic);
3133 __ Smull(temp.X(), dividend, temp);
3134 __ Lsr(temp.X(), temp.X(), 32);
3135
3136 GenerateResultDivRemWithAnyConstant(/* is_rem= */ instruction->IsRem(),
3137 /* final_right_shift= */ shift,
3138 magic,
3139 imm,
3140 dividend,
3141 temp,
3142 out,
3143 &temps);
3144}
3145
3146void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3147 DCHECK(instruction->IsDiv() || instruction->IsRem());
3148 if (instruction->GetResultType() == DataType::Type::kInt64) {
3149 GenerateInt64DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003150 } else {
Evgeny Astigeevicha6653d32020-05-05 16:30:24 +01003151 GenerateInt32DivRemWithAnyConstant(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003152 }
3153}
3154
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003155void InstructionCodeGeneratorARM64::GenerateIntDivForConstDenom(HDiv *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003156 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Zheng Xuc6667102015-05-15 16:08:45 +08003157
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003158 if (imm == 0) {
3159 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3160 return;
3161 }
Zheng Xuc6667102015-05-15 16:08:45 +08003162
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003163 if (IsPowerOfTwo(AbsOrMin(imm))) {
3164 GenerateIntDivForPower2Denom(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003165 } else {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003166 // Cases imm == -1 or imm == 1 are handled by InstructionSimplifier.
3167 DCHECK(imm < -2 || imm > 2) << imm;
3168 GenerateDivRemWithAnyConstant(instruction);
3169 }
3170}
3171
3172void InstructionCodeGeneratorARM64::GenerateIntDiv(HDiv *instruction) {
3173 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
3174 << instruction->GetResultType();
3175
3176 if (instruction->GetLocations()->InAt(1).IsConstant()) {
3177 GenerateIntDivForConstDenom(instruction);
3178 } else {
3179 Register out = OutputRegister(instruction);
Zheng Xuc6667102015-05-15 16:08:45 +08003180 Register dividend = InputRegisterAt(instruction, 0);
3181 Register divisor = InputRegisterAt(instruction, 1);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003182 __ Sdiv(out, dividend, divisor);
Zheng Xuc6667102015-05-15 16:08:45 +08003183 }
3184}
3185
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003186void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3187 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003188 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003189 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003190 case DataType::Type::kInt32:
3191 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003192 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003193 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003194 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3195 break;
3196
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003197 case DataType::Type::kFloat32:
3198 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003199 locations->SetInAt(0, Location::RequiresFpuRegister());
3200 locations->SetInAt(1, Location::RequiresFpuRegister());
3201 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3202 break;
3203
3204 default:
3205 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3206 }
3207}
3208
3209void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003210 DataType::Type type = div->GetResultType();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003211 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003212 case DataType::Type::kInt32:
3213 case DataType::Type::kInt64:
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01003214 GenerateIntDiv(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003215 break;
3216
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003217 case DataType::Type::kFloat32:
3218 case DataType::Type::kFloat64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003219 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3220 break;
3221
3222 default:
3223 LOG(FATAL) << "Unexpected div type " << type;
3224 }
3225}
3226
Alexandre Rames67555f72014-11-18 10:55:16 +00003227void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003228 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003229 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003230}
3231
3232void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3233 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003234 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003235 codegen_->AddSlowPath(slow_path);
3236 Location value = instruction->GetLocations()->InAt(0);
3237
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003238 DataType::Type type = instruction->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003239
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003240 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003241 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Elliott Hughesc1896c92018-11-29 11:33:18 -08003242 UNREACHABLE();
Alexandre Rames3e69f162014-12-10 10:36:50 +00003243 }
3244
Alexandre Rames67555f72014-11-18 10:55:16 +00003245 if (value.IsConstant()) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01003246 int64_t divisor = Int64FromLocation(value);
Alexandre Rames67555f72014-11-18 10:55:16 +00003247 if (divisor == 0) {
3248 __ B(slow_path->GetEntryLabel());
3249 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003250 // A division by a non-null constant is valid. We don't need to perform
3251 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003252 }
3253 } else {
3254 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3255 }
3256}
3257
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003258void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3259 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003260 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003261 locations->SetOut(Location::ConstantLocation(constant));
3262}
3263
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003264void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3265 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003266 // Will be generated at use site.
3267}
3268
Alexandre Rames5319def2014-10-23 10:03:10 +01003269void LocationsBuilderARM64::VisitExit(HExit* exit) {
3270 exit->SetLocations(nullptr);
3271}
3272
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003273void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003274}
3275
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003276void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3277 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003278 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003279 locations->SetOut(Location::ConstantLocation(constant));
3280}
3281
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003282void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003283 // Will be generated at use site.
3284}
3285
David Brazdilfc6a86a2015-06-26 10:33:45 +00003286void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08003287 if (successor->IsExitBlock()) {
3288 DCHECK(got->GetPrevious()->AlwaysThrows());
3289 return; // no code needed
3290 }
3291
Serban Constantinescu02164b32014-11-13 14:05:07 +00003292 HBasicBlock* block = got->GetBlock();
3293 HInstruction* previous = got->GetPrevious();
3294 HLoopInformation* info = block->GetLoopInformation();
3295
David Brazdil46e2a392015-03-16 17:31:52 +00003296 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00003297 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003298 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3299 return;
3300 }
3301 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3302 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08003303 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003304 }
3305 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003306 __ B(codegen_->GetLabelOf(successor));
3307 }
3308}
3309
David Brazdilfc6a86a2015-06-26 10:33:45 +00003310void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3311 got->SetLocations(nullptr);
3312}
3313
3314void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3315 HandleGoto(got, got->GetSuccessor());
3316}
3317
3318void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3319 try_boundary->SetLocations(nullptr);
3320}
3321
3322void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3323 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3324 if (!successor->IsExitBlock()) {
3325 HandleGoto(try_boundary, successor);
3326 }
3327}
3328
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003329void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003330 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003331 vixl::aarch64::Label* true_target,
3332 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003333 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003334
David Brazdil0debae72015-11-12 18:37:00 +00003335 if (true_target == nullptr && false_target == nullptr) {
3336 // Nothing to do. The code always falls through.
3337 return;
3338 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003339 // Constant condition, statically compared against "true" (integer value 1).
3340 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003341 if (true_target != nullptr) {
3342 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003343 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003344 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003345 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003346 if (false_target != nullptr) {
3347 __ B(false_target);
3348 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003349 }
David Brazdil0debae72015-11-12 18:37:00 +00003350 return;
3351 }
3352
3353 // The following code generates these patterns:
3354 // (1) true_target == nullptr && false_target != nullptr
3355 // - opposite condition true => branch to false_target
3356 // (2) true_target != nullptr && false_target == nullptr
3357 // - condition true => branch to true_target
3358 // (3) true_target != nullptr && false_target != nullptr
3359 // - condition true => branch to true_target
3360 // - branch to false_target
3361 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003362 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003363 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003364 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003365 if (true_target == nullptr) {
3366 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3367 } else {
3368 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3369 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003370 } else {
3371 // The condition instruction has not been materialized, use its inputs as
3372 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003373 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003374
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003375 DataType::Type type = condition->InputAt(0)->GetType();
3376 if (DataType::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003377 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003378 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003379 IfCondition opposite_condition = condition->GetOppositeCondition();
3380 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003381 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003382 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003383 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003384 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003385 // Integer cases.
3386 Register lhs = InputRegisterAt(condition, 0);
3387 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003388
3389 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003390 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003391 if (true_target == nullptr) {
3392 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3393 non_fallthrough_target = false_target;
3394 } else {
3395 arm64_cond = ARM64Condition(condition->GetCondition());
3396 non_fallthrough_target = true_target;
3397 }
3398
Aart Bik086d27e2016-01-20 17:02:00 -08003399 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003400 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003401 switch (arm64_cond) {
3402 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003403 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003404 break;
3405 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003406 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003407 break;
3408 case lt:
3409 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003410 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003411 break;
3412 case ge:
3413 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003414 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003415 break;
3416 default:
3417 // Without the `static_cast` the compiler throws an error for
3418 // `-Werror=sign-promo`.
3419 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3420 }
3421 } else {
3422 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003423 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003424 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003425 }
3426 }
David Brazdil0debae72015-11-12 18:37:00 +00003427
3428 // If neither branch falls through (case 3), the conditional branch to `true_target`
3429 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3430 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003431 __ B(false_target);
3432 }
3433}
3434
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003435void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003436 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003437 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003438 locations->SetInAt(0, Location::RequiresRegister());
3439 }
3440}
3441
3442void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003443 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3444 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003445 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3446 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3447 true_target = nullptr;
3448 }
3449 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3450 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3451 false_target = nullptr;
3452 }
Andreas Gampe3db70682018-12-26 15:12:03 -08003453 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003454}
3455
3456void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003457 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003458 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003459 InvokeRuntimeCallingConvention calling_convention;
3460 RegisterSet caller_saves = RegisterSet::Empty();
3461 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3462 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003463 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003464 locations->SetInAt(0, Location::RequiresRegister());
3465 }
3466}
3467
3468void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003469 SlowPathCodeARM64* slow_path =
3470 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003471 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08003472 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00003473 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08003474 /* false_target= */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003475}
3476
Mingyao Yang063fc772016-08-02 11:02:54 -07003477void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003478 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07003479 LocationSummary(flag, LocationSummary::kNoCall);
3480 locations->SetOut(Location::RequiresRegister());
3481}
3482
3483void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3484 __ Ldr(OutputRegister(flag),
3485 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3486}
3487
David Brazdilc0b601b2016-02-08 14:20:45 +00003488static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3489 return condition->IsCondition() &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003490 DataType::IsFloatingPointType(condition->InputAt(0)->GetType());
David Brazdilc0b601b2016-02-08 14:20:45 +00003491}
3492
Alexandre Rames880f1192016-06-13 16:04:50 +01003493static inline Condition GetConditionForSelect(HCondition* condition) {
3494 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003495 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3496 : ARM64Condition(cond);
3497}
3498
David Brazdil74eb1b22015-12-14 11:44:01 +00003499void LocationsBuilderARM64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003500 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003501 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003502 locations->SetInAt(0, Location::RequiresFpuRegister());
3503 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003504 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003505 } else {
3506 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3507 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3508 bool is_true_value_constant = cst_true_value != nullptr;
3509 bool is_false_value_constant = cst_false_value != nullptr;
3510 // Ask VIXL whether we should synthesize constants in registers.
3511 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3512 Operand true_op = is_true_value_constant ?
3513 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3514 Operand false_op = is_false_value_constant ?
3515 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3516 bool true_value_in_register = false;
3517 bool false_value_in_register = false;
3518 MacroAssembler::GetCselSynthesisInformation(
3519 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3520 true_value_in_register |= !is_true_value_constant;
3521 false_value_in_register |= !is_false_value_constant;
3522
3523 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3524 : Location::ConstantLocation(cst_true_value));
3525 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3526 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003527 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003528 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003529
David Brazdil74eb1b22015-12-14 11:44:01 +00003530 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3531 locations->SetInAt(2, Location::RequiresRegister());
3532 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003533}
3534
3535void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003536 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003537 Condition csel_cond;
3538
3539 if (IsBooleanValueOrMaterializedCondition(cond)) {
3540 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003541 // Use the condition flags set by the previous instruction.
3542 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003543 } else {
3544 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003545 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003546 }
3547 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003548 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003549 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003550 } else {
3551 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003552 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003553 }
3554
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003555 if (DataType::IsFloatingPointType(select->GetType())) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003556 __ Fcsel(OutputFPRegister(select),
3557 InputFPRegisterAt(select, 1),
3558 InputFPRegisterAt(select, 0),
3559 csel_cond);
3560 } else {
3561 __ Csel(OutputRegister(select),
3562 InputOperandAt(select, 1),
3563 InputOperandAt(select, 0),
3564 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003565 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003566}
3567
David Srbecky0cf44932015-12-09 14:09:59 +00003568void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003569 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00003570}
3571
David Srbeckyd28f4a02016-03-14 17:14:24 +00003572void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3573 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003574}
3575
3576void CodeGeneratorARM64::GenerateNop() {
3577 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003578}
3579
Alexandre Rames5319def2014-10-23 10:03:10 +01003580void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003581 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003582}
3583
3584void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003585 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003586}
3587
3588void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003589 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003590}
3591
3592void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003593 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003594}
3595
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003596// Temp is used for read barrier.
3597static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3598 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003599 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003600 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3601 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3602 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3603 return 1;
3604 }
3605 return 0;
3606}
3607
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003608// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003609// interface pointer, one for loading the current interface.
3610// The other checks have one temp for loading the object's class.
3611static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3612 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3613 return 3;
3614 }
3615 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003616}
3617
Alexandre Rames67555f72014-11-18 10:55:16 +00003618void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003619 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003620 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003621 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003622 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003623 case TypeCheckKind::kExactCheck:
3624 case TypeCheckKind::kAbstractClassCheck:
3625 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00003626 case TypeCheckKind::kArrayObjectCheck: {
3627 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
3628 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
3629 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003630 break;
Vladimir Marko87584542017-12-12 17:47:52 +00003631 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003632 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003633 case TypeCheckKind::kUnresolvedCheck:
3634 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003635 call_kind = LocationSummary::kCallOnSlowPath;
3636 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00003637 case TypeCheckKind::kBitstringCheck:
3638 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003639 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003640
Vladimir Markoca6fff82017-10-03 14:49:14 +01003641 LocationSummary* locations =
3642 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003643 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003644 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003645 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003646 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003647 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3648 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3649 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3650 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3651 } else {
3652 locations->SetInAt(1, Location::RequiresRegister());
3653 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003654 // The "out" register is used as a temporary, so it overlaps with the inputs.
3655 // Note that TypeCheckSlowPathARM64 uses this register too.
3656 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003657 // Add temps if necessary for read barriers.
3658 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003659}
3660
3661void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003662 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003663 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003664 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003665 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003666 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3667 ? Register()
3668 : InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003669 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003670 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003671 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3672 DCHECK_LE(num_temps, 1u);
3673 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003674 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3675 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3676 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3677 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003678
Scott Wakeling97c72b72016-06-24 16:19:36 +01003679 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003680 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003681
3682 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003683 // Avoid null check if we know `obj` is not null.
3684 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003685 __ Cbz(obj, &zero);
3686 }
3687
Roland Levillain44015862016-01-22 11:47:17 +00003688 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003689 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003690 ReadBarrierOption read_barrier_option =
3691 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003692 // /* HeapReference<Class> */ out = obj->klass_
3693 GenerateReferenceLoadTwoRegisters(instruction,
3694 out_loc,
3695 obj_loc,
3696 class_offset,
3697 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003698 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003699 __ Cmp(out, cls);
3700 __ Cset(out, eq);
3701 if (zero.IsLinked()) {
3702 __ B(&done);
3703 }
3704 break;
3705 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003706
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003707 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003708 ReadBarrierOption read_barrier_option =
3709 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003710 // /* HeapReference<Class> */ out = obj->klass_
3711 GenerateReferenceLoadTwoRegisters(instruction,
3712 out_loc,
3713 obj_loc,
3714 class_offset,
3715 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003716 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003717 // If the class is abstract, we eagerly fetch the super class of the
3718 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003719 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003720 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003721 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003722 GenerateReferenceLoadOneRegister(instruction,
3723 out_loc,
3724 super_offset,
3725 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003726 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003727 // If `out` is null, we use it for the result, and jump to `done`.
3728 __ Cbz(out, &done);
3729 __ Cmp(out, cls);
3730 __ B(ne, &loop);
3731 __ Mov(out, 1);
3732 if (zero.IsLinked()) {
3733 __ B(&done);
3734 }
3735 break;
3736 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003737
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003738 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003739 ReadBarrierOption read_barrier_option =
3740 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003741 // /* HeapReference<Class> */ out = obj->klass_
3742 GenerateReferenceLoadTwoRegisters(instruction,
3743 out_loc,
3744 obj_loc,
3745 class_offset,
3746 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003747 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003748 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003749 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003750 __ Bind(&loop);
3751 __ Cmp(out, cls);
3752 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003753 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003754 GenerateReferenceLoadOneRegister(instruction,
3755 out_loc,
3756 super_offset,
3757 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003758 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003759 __ Cbnz(out, &loop);
3760 // If `out` is null, we use it for the result, and jump to `done`.
3761 __ B(&done);
3762 __ Bind(&success);
3763 __ Mov(out, 1);
3764 if (zero.IsLinked()) {
3765 __ B(&done);
3766 }
3767 break;
3768 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003769
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003770 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00003771 ReadBarrierOption read_barrier_option =
3772 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003773 // /* HeapReference<Class> */ out = obj->klass_
3774 GenerateReferenceLoadTwoRegisters(instruction,
3775 out_loc,
3776 obj_loc,
3777 class_offset,
3778 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003779 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003780 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003781 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003782 __ Cmp(out, cls);
3783 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003784 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003785 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003786 GenerateReferenceLoadOneRegister(instruction,
3787 out_loc,
3788 component_offset,
3789 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00003790 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003791 // If `out` is null, we use it for the result, and jump to `done`.
3792 __ Cbz(out, &done);
3793 __ Ldrh(out, HeapOperand(out, primitive_offset));
3794 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3795 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003796 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003797 __ Mov(out, 1);
3798 __ B(&done);
3799 break;
3800 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003801
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003802 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003803 // No read barrier since the slow path will retry upon failure.
3804 // /* HeapReference<Class> */ out = obj->klass_
3805 GenerateReferenceLoadTwoRegisters(instruction,
3806 out_loc,
3807 obj_loc,
3808 class_offset,
3809 maybe_temp_loc,
3810 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003811 __ Cmp(out, cls);
3812 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003813 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08003814 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003815 codegen_->AddSlowPath(slow_path);
3816 __ B(ne, slow_path->GetEntryLabel());
3817 __ Mov(out, 1);
3818 if (zero.IsLinked()) {
3819 __ B(&done);
3820 }
3821 break;
3822 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003823
Calin Juravle98893e12015-10-02 21:05:03 +01003824 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003825 case TypeCheckKind::kInterfaceCheck: {
3826 // Note that we indeed only call on slow path, but we always go
3827 // into the slow path for the unresolved and interface check
3828 // cases.
3829 //
3830 // We cannot directly call the InstanceofNonTrivial runtime
3831 // entry point without resorting to a type checking slow path
3832 // here (i.e. by calling InvokeRuntime directly), as it would
3833 // require to assign fixed registers for the inputs of this
3834 // HInstanceOf instruction (following the runtime calling
3835 // convention), which might be cluttered by the potential first
3836 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003837 //
3838 // TODO: Introduce a new runtime entry point taking the object
3839 // to test (instead of its class) as argument, and let it deal
3840 // with the read barrier issues. This will let us refactor this
3841 // case of the `switch` code as it was previously (with a direct
3842 // call to the runtime not using a type checking slow path).
3843 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003844 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01003845 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
Andreas Gampe3db70682018-12-26 15:12:03 -08003846 instruction, /* is_fatal= */ false);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003847 codegen_->AddSlowPath(slow_path);
3848 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003849 if (zero.IsLinked()) {
3850 __ B(&done);
3851 }
3852 break;
3853 }
Vladimir Marko175e7862018-03-27 09:03:13 +00003854
3855 case TypeCheckKind::kBitstringCheck: {
3856 // /* HeapReference<Class> */ temp = obj->klass_
3857 GenerateReferenceLoadTwoRegisters(instruction,
3858 out_loc,
3859 obj_loc,
3860 class_offset,
3861 maybe_temp_loc,
3862 kWithoutReadBarrier);
3863
3864 GenerateBitstringTypeCheckCompare(instruction, out);
3865 __ Cset(out, eq);
3866 if (zero.IsLinked()) {
3867 __ B(&done);
3868 }
3869 break;
3870 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003871 }
3872
3873 if (zero.IsLinked()) {
3874 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003875 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003876 }
3877
3878 if (done.IsLinked()) {
3879 __ Bind(&done);
3880 }
3881
3882 if (slow_path != nullptr) {
3883 __ Bind(slow_path->GetExitLabel());
3884 }
3885}
3886
3887void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003888 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00003889 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003890 LocationSummary* locations =
3891 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003892 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003893 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3894 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3895 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3896 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3897 } else {
3898 locations->SetInAt(1, Location::RequiresRegister());
3899 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003900 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3901 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003902}
3903
3904void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003905 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003906 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003907 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003908 Register obj = InputRegisterAt(instruction, 0);
Vladimir Marko175e7862018-03-27 09:03:13 +00003909 Register cls = (type_check_kind == TypeCheckKind::kBitstringCheck)
3910 ? Register()
3911 : InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003912 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3913 DCHECK_GE(num_temps, 1u);
3914 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003915 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003916 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3917 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003918 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003919 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3920 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3921 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3922 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3923 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3924 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3925 const uint32_t object_array_data_offset =
3926 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003927
Vladimir Marko87584542017-12-12 17:47:52 +00003928 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003929 SlowPathCodeARM64* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003930 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathARM64(
3931 instruction, is_type_check_slow_path_fatal);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003932 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003933
Scott Wakeling97c72b72016-06-24 16:19:36 +01003934 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003935 // Avoid null check if we know obj is not null.
3936 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003937 __ Cbz(obj, &done);
3938 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003939
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003940 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003941 case TypeCheckKind::kExactCheck:
3942 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003943 // /* HeapReference<Class> */ temp = obj->klass_
3944 GenerateReferenceLoadTwoRegisters(instruction,
3945 temp_loc,
3946 obj_loc,
3947 class_offset,
3948 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003949 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003950
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003951 __ Cmp(temp, cls);
3952 // Jump to slow path for throwing the exception or doing a
3953 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003954 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003955 break;
3956 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003957
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003958 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003959 // /* HeapReference<Class> */ temp = obj->klass_
3960 GenerateReferenceLoadTwoRegisters(instruction,
3961 temp_loc,
3962 obj_loc,
3963 class_offset,
3964 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003965 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003966
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003967 // If the class is abstract, we eagerly fetch the super class of the
3968 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003969 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003970 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003971 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003972 GenerateReferenceLoadOneRegister(instruction,
3973 temp_loc,
3974 super_offset,
3975 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003976 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003977
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003978 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3979 // exception.
3980 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3981 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003982 __ Cmp(temp, cls);
3983 __ B(ne, &loop);
3984 break;
3985 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003986
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003987 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003988 // /* HeapReference<Class> */ temp = obj->klass_
3989 GenerateReferenceLoadTwoRegisters(instruction,
3990 temp_loc,
3991 obj_loc,
3992 class_offset,
3993 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003994 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003995
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003996 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003997 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003998 __ Bind(&loop);
3999 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004000 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004001
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004002 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004003 GenerateReferenceLoadOneRegister(instruction,
4004 temp_loc,
4005 super_offset,
4006 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004007 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004008
4009 // If the class reference currently in `temp` is not null, jump
4010 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004011 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004012 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004013 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004014 break;
4015 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004016
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004017 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004018 // /* HeapReference<Class> */ temp = obj->klass_
4019 GenerateReferenceLoadTwoRegisters(instruction,
4020 temp_loc,
4021 obj_loc,
4022 class_offset,
4023 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004024 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004025
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004026 // Do an exact check.
4027 __ Cmp(temp, cls);
4028 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004029
4030 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004031 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004032 GenerateReferenceLoadOneRegister(instruction,
4033 temp_loc,
4034 component_offset,
4035 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004036 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004037
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004038 // If the component type is null, jump to the slow path to throw the exception.
4039 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4040 // Otherwise, the object is indeed an array. Further check that this component type is not a
4041 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004042 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4043 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004044 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004045 break;
4046 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004047
Calin Juravle98893e12015-10-02 21:05:03 +01004048 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004049 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004050 //
4051 // We cannot directly call the CheckCast runtime entry point
4052 // without resorting to a type checking slow path here (i.e. by
4053 // calling InvokeRuntime directly), as it would require to
4054 // assign fixed registers for the inputs of this HInstanceOf
4055 // instruction (following the runtime calling convention), which
4056 // might be cluttered by the potential first read barrier
4057 // emission at the beginning of this method.
4058 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004059 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004060 case TypeCheckKind::kInterfaceCheck: {
4061 // /* HeapReference<Class> */ temp = obj->klass_
4062 GenerateReferenceLoadTwoRegisters(instruction,
4063 temp_loc,
4064 obj_loc,
4065 class_offset,
4066 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004067 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004068
4069 // /* HeapReference<Class> */ temp = temp->iftable_
4070 GenerateReferenceLoadTwoRegisters(instruction,
4071 temp_loc,
4072 temp_loc,
4073 iftable_offset,
4074 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004075 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004076 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004077 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004078 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004079 vixl::aarch64::Label start_loop;
4080 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004081 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004082 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4083 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004084 // Go to next interface.
4085 __ Add(temp, temp, 2 * kHeapReferenceSize);
4086 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004087 // Compare the classes and continue the loop if they do not match.
4088 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4089 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004090 break;
4091 }
Vladimir Marko175e7862018-03-27 09:03:13 +00004092
4093 case TypeCheckKind::kBitstringCheck: {
4094 // /* HeapReference<Class> */ temp = obj->klass_
4095 GenerateReferenceLoadTwoRegisters(instruction,
4096 temp_loc,
4097 obj_loc,
4098 class_offset,
4099 maybe_temp2_loc,
4100 kWithoutReadBarrier);
4101
4102 GenerateBitstringTypeCheckCompare(instruction, temp);
4103 __ B(ne, type_check_slow_path->GetEntryLabel());
4104 break;
4105 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004106 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004107 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004108
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004109 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004110}
4111
Alexandre Rames5319def2014-10-23 10:03:10 +01004112void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004113 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01004114 locations->SetOut(Location::ConstantLocation(constant));
4115}
4116
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004117void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004118 // Will be generated at use site.
4119}
4120
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004121void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004122 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004123 locations->SetOut(Location::ConstantLocation(constant));
4124}
4125
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004126void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004127 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004128}
4129
Calin Juravle175dc732015-08-25 15:42:32 +01004130void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4131 // The trampoline uses the same calling convention as dex calling conventions,
4132 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4133 // the method_idx.
4134 HandleInvoke(invoke);
4135}
4136
4137void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4138 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004139 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle175dc732015-08-25 15:42:32 +01004140}
4141
Alexandre Rames5319def2014-10-23 10:03:10 +01004142void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004143 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004144 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004145}
4146
Alexandre Rames67555f72014-11-18 10:55:16 +00004147void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4148 HandleInvoke(invoke);
4149}
4150
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004151void CodeGeneratorARM64::MaybeGenerateInlineCacheCheck(HInstruction* instruction,
4152 Register klass) {
4153 DCHECK_EQ(klass.GetCode(), 0u);
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004154 // We know the destination of an intrinsic, so no need to record inline
4155 // caches.
4156 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00004157 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray20036d82019-11-28 16:15:00 +00004158 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004159 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
4160 ScopedObjectAccess soa(Thread::Current());
4161 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00004162 if (info != nullptr) {
4163 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
4164 uint64_t address = reinterpret_cast64<uint64_t>(cache);
4165 vixl::aarch64::Label done;
4166 __ Mov(x8, address);
4167 __ Ldr(x9, MemOperand(x8, InlineCache::ClassesOffset().Int32Value()));
4168 // Fast path for a monomorphic cache.
4169 __ Cmp(klass, x9);
4170 __ B(eq, &done);
4171 InvokeRuntime(kQuickUpdateInlineCache, instruction, instruction->GetDexPc());
4172 __ Bind(&done);
4173 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004174 }
4175}
4176
Alexandre Rames67555f72014-11-18 10:55:16 +00004177void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4178 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004179 LocationSummary* locations = invoke->GetLocations();
4180 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004181 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004182 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004183 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004184
Artem Serov914d7a82017-02-07 14:33:49 +00004185 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004186 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004187 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004188 {
4189 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4190 // /* HeapReference<Class> */ temp = temp->klass_
4191 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4192 codegen_->MaybeRecordImplicitNullCheck(invoke);
4193 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004194 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004195 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004196 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004197 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004198 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004199 }
Artem Serov914d7a82017-02-07 14:33:49 +00004200
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004201 // Instead of simply (possibly) unpoisoning `temp` here, we should
4202 // emit a read barrier for the previous class reference load.
4203 // However this is not required in practice, as this is an
4204 // intermediate/temporary reference and because the current
4205 // concurrent copying collector keeps the from-space memory
4206 // intact/accessible until the end of the marking phase (the
4207 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004208 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004209
4210 // If we're compiling baseline, update the inline cache.
4211 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
4212
4213 // The register ip1 is required to be used for the hidden argument in
4214 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
4215 MacroAssembler* masm = GetVIXLAssembler();
4216 UseScratchRegisterScope scratch_scope(masm);
4217 scratch_scope.Exclude(ip1);
4218 __ Mov(ip1, invoke->GetDexMethodIndex());
4219
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004220 __ Ldr(temp,
4221 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4222 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004223 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004224 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004225 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004226 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004227 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004228
4229 {
4230 // Ensure the pc position is recorded immediately after the `blr` instruction.
4231 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4232
4233 // lr();
4234 __ blr(lr);
4235 DCHECK(!codegen_->IsLeafMethod());
4236 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4237 }
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004238
Andreas Gampe3db70682018-12-26 15:12:03 -08004239 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004240}
4241
4242void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004243 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004244 if (intrinsic.TryDispatch(invoke)) {
4245 return;
4246 }
4247
Alexandre Rames67555f72014-11-18 10:55:16 +00004248 HandleInvoke(invoke);
4249}
4250
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004251void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004252 // Explicit clinit checks triggered by static invokes must have been pruned by
4253 // art::PrepareForRegisterAllocation.
4254 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004255
Vladimir Markoca6fff82017-10-03 14:49:14 +01004256 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetAllocator(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004257 if (intrinsic.TryDispatch(invoke)) {
4258 return;
4259 }
4260
Alexandre Rames67555f72014-11-18 10:55:16 +00004261 HandleInvoke(invoke);
4262}
4263
Andreas Gampe878d58c2015-01-15 23:24:00 -08004264static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4265 if (invoke->GetLocations()->Intrinsified()) {
4266 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4267 intrinsic.Dispatch(invoke);
4268 return true;
4269 }
4270 return false;
4271}
4272
Vladimir Markodc151b22015-10-15 18:02:30 +01004273HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4274 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004275 ArtMethod* method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004276 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004277 return desired_dispatch_info;
4278}
4279
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004280void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4281 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004282 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004283 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4284 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004285 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4286 uint32_t offset =
4287 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004288 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004289 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004290 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004291 }
Vladimir Marko58155012015-08-19 12:49:41 +00004292 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004293 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004294 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004295 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004296 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004297 // Add ADRP with its PC-relative method patch.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004298 vixl::aarch64::Label* adrp_label = NewBootImageMethodPatch(invoke->GetTargetMethod());
Vladimir Marko65979462017-05-19 17:25:12 +01004299 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4300 // Add ADD with its PC-relative method patch.
4301 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004302 NewBootImageMethodPatch(invoke->GetTargetMethod(), adrp_label);
Vladimir Marko65979462017-05-19 17:25:12 +01004303 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4304 break;
4305 }
Vladimir Markob066d432018-01-03 13:14:37 +00004306 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4307 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004308 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00004309 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_offset);
4310 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4311 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
4312 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_offset, adrp_label);
4313 // Note: Boot image is in the low 4GiB and the entry is 32-bit, so emit a 32-bit load.
4314 EmitLdrOffsetPlaceholder(ldr_label, WRegisterFrom(temp), XRegisterFrom(temp));
4315 break;
4316 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004317 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Markob066d432018-01-03 13:14:37 +00004318 // Add ADRP with its PC-relative .bss entry patch.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004319 MethodReference target_method(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4320 vixl::aarch64::Label* adrp_label = NewMethodBssEntryPatch(target_method);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004321 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Markob066d432018-01-03 13:14:37 +00004322 // Add LDR with its PC-relative .bss entry patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004323 vixl::aarch64::Label* ldr_label =
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004324 NewMethodBssEntryPatch(target_method, adrp_label);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004325 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoaad75c62016-10-03 08:46:48 +00004326 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004327 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004328 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004329 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
4330 // Load method address from literal pool.
4331 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
4332 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004333 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4334 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4335 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004336 }
4337 }
4338
4339 switch (invoke->GetCodePtrLocation()) {
4340 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004341 {
4342 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4343 ExactAssemblyScope eas(GetVIXLAssembler(),
4344 kInstructionSize,
4345 CodeBufferCheckScope::kExactSize);
4346 __ bl(&frame_entry_label_);
4347 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4348 }
Vladimir Marko58155012015-08-19 12:49:41 +00004349 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004350 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4351 // LR = callee_method->entry_point_from_quick_compiled_code_;
4352 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004353 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004354 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004355 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004356 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004357 ExactAssemblyScope eas(GetVIXLAssembler(),
4358 kInstructionSize,
4359 CodeBufferCheckScope::kExactSize);
4360 // lr()
4361 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004362 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004363 }
Vladimir Marko58155012015-08-19 12:49:41 +00004364 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004365 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004366
Andreas Gampe878d58c2015-01-15 23:24:00 -08004367 DCHECK(!IsLeafMethod());
4368}
4369
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004370void CodeGeneratorARM64::GenerateVirtualCall(
4371 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004372 // Use the calling convention instead of the location of the receiver, as
4373 // intrinsics may have put the receiver in a different register. In the intrinsics
4374 // slow path, the arguments have been moved to the right place, so here we are
4375 // guaranteed that the receiver is the first register of the calling convention.
4376 InvokeDexCallingConvention calling_convention;
4377 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004378 Register temp = XRegisterFrom(temp_in);
4379 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4380 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4381 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004382 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004383
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004384 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004385
4386 {
4387 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4388 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4389 // /* HeapReference<Class> */ temp = receiver->klass_
4390 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4391 MaybeRecordImplicitNullCheck(invoke);
4392 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004393 // Instead of simply (possibly) unpoisoning `temp` here, we should
4394 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004395 // intermediate/temporary reference and because the current
4396 // concurrent copying collector keeps the from-space memory
4397 // intact/accessible until the end of the marking phase (the
4398 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004399 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00004400
4401 // If we're compiling baseline, update the inline cache.
4402 MaybeGenerateInlineCacheCheck(invoke, temp);
4403
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004404 // temp = temp->GetMethodAt(method_offset);
4405 __ Ldr(temp, MemOperand(temp, method_offset));
4406 // lr = temp->GetEntryPoint();
4407 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004408 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004409 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004410 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4411 // lr();
4412 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004413 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004414 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004415}
4416
Orion Hodsonac141392017-01-13 11:53:47 +00004417void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4418 HandleInvoke(invoke);
4419}
4420
4421void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4422 codegen_->GenerateInvokePolymorphicCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004423 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodsonac141392017-01-13 11:53:47 +00004424}
4425
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004426void LocationsBuilderARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4427 HandleInvoke(invoke);
4428}
4429
4430void InstructionCodeGeneratorARM64::VisitInvokeCustom(HInvokeCustom* invoke) {
4431 codegen_->GenerateInvokeCustomCall(invoke);
Andreas Gampe3db70682018-12-26 15:12:03 -08004432 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004433}
4434
Vladimir Marko6fd16062018-06-26 11:02:04 +01004435vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageIntrinsicPatch(
4436 uint32_t intrinsic_data,
4437 vixl::aarch64::Label* adrp_label) {
4438 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004439 /* dex_file= */ nullptr, intrinsic_data, adrp_label, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01004440}
4441
Vladimir Markob066d432018-01-03 13:14:37 +00004442vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageRelRoPatch(
4443 uint32_t boot_image_offset,
4444 vixl::aarch64::Label* adrp_label) {
4445 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01004446 /* dex_file= */ nullptr, boot_image_offset, adrp_label, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00004447}
4448
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004449vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageMethodPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004450 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004451 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004452 return NewPcRelativePatch(
4453 target_method.dex_file, target_method.index, adrp_label, &boot_image_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004454}
4455
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004456vixl::aarch64::Label* CodeGeneratorARM64::NewMethodBssEntryPatch(
4457 MethodReference target_method,
4458 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004459 return NewPcRelativePatch(
4460 target_method.dex_file, target_method.index, adrp_label, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004461}
4462
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004463vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageTypePatch(
Scott Wakeling97c72b72016-06-24 16:19:36 +01004464 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004465 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004466 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004467 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &boot_image_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004468}
4469
Vladimir Marko1998cd02017-01-13 13:02:58 +00004470vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4471 const DexFile& dex_file,
4472 dex::TypeIndex type_index,
4473 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004474 return NewPcRelativePatch(&dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004475}
4476
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004477vixl::aarch64::Label* CodeGeneratorARM64::NewBootImageStringPatch(
Vladimir Marko65979462017-05-19 17:25:12 +01004478 const DexFile& dex_file,
4479 dex::StringIndex string_index,
4480 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004481 return NewPcRelativePatch(
4482 &dex_file, string_index.index_, adrp_label, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01004483}
4484
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004485vixl::aarch64::Label* CodeGeneratorARM64::NewStringBssEntryPatch(
4486 const DexFile& dex_file,
4487 dex::StringIndex string_index,
4488 vixl::aarch64::Label* adrp_label) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004489 return NewPcRelativePatch(&dex_file, string_index.index_, adrp_label, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004490}
4491
Vladimir Markof6675082019-05-17 12:05:28 +01004492void CodeGeneratorARM64::EmitEntrypointThunkCall(ThreadOffset64 entrypoint_offset) {
4493 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
4494 DCHECK(!Runtime::Current()->UseJitCompilation());
4495 call_entrypoint_patches_.emplace_back(/*dex_file*/ nullptr, entrypoint_offset.Uint32Value());
4496 vixl::aarch64::Label* bl_label = &call_entrypoint_patches_.back().label;
4497 __ bind(bl_label);
4498 __ bl(static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4499}
4500
Vladimir Marko966b46f2018-08-03 10:20:19 +00004501void CodeGeneratorARM64::EmitBakerReadBarrierCbnz(uint32_t custom_data) {
Vladimir Marko94796f82018-08-08 15:15:33 +01004502 DCHECK(!__ AllowMacroInstructions()); // In ExactAssemblyScope.
Vladimir Marko966b46f2018-08-03 10:20:19 +00004503 if (Runtime::Current()->UseJitCompilation()) {
4504 auto it = jit_baker_read_barrier_slow_paths_.FindOrAdd(custom_data);
4505 vixl::aarch64::Label* slow_path_entry = &it->second.label;
4506 __ cbnz(mr, slow_path_entry);
4507 } else {
4508 baker_read_barrier_patches_.emplace_back(custom_data);
4509 vixl::aarch64::Label* cbnz_label = &baker_read_barrier_patches_.back().label;
4510 __ bind(cbnz_label);
4511 __ cbnz(mr, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
4512 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004513}
4514
Scott Wakeling97c72b72016-06-24 16:19:36 +01004515vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004516 const DexFile* dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004517 uint32_t offset_or_index,
4518 vixl::aarch64::Label* adrp_label,
4519 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004520 // Add a patch entry and return the label.
4521 patches->emplace_back(dex_file, offset_or_index);
4522 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004523 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004524 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4525 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4526 return label;
4527}
4528
Scott Wakeling97c72b72016-06-24 16:19:36 +01004529vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4530 uint64_t address) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004531 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004532}
4533
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004534vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004535 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004536 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004537 return jit_string_patches_.GetOrCreate(
4538 StringReference(&dex_file, string_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004539 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004540}
4541
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004542vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004543 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004544 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004545 return jit_class_patches_.GetOrCreate(
4546 TypeReference(&dex_file, type_index),
Andreas Gampe3db70682018-12-26 15:12:03 -08004547 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u); });
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004548}
4549
Vladimir Markoaad75c62016-10-03 08:46:48 +00004550void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4551 vixl::aarch64::Register reg) {
4552 DCHECK(reg.IsX());
4553 SingleEmissionCheckScope guard(GetVIXLAssembler());
4554 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004555 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004556}
4557
4558void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4559 vixl::aarch64::Register out,
4560 vixl::aarch64::Register base) {
4561 DCHECK(out.IsX());
4562 DCHECK(base.IsX());
4563 SingleEmissionCheckScope guard(GetVIXLAssembler());
4564 __ Bind(fixup_label);
4565 __ add(out, base, Operand(/* offset placeholder */ 0));
4566}
4567
4568void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4569 vixl::aarch64::Register out,
4570 vixl::aarch64::Register base) {
4571 DCHECK(base.IsX());
4572 SingleEmissionCheckScope guard(GetVIXLAssembler());
4573 __ Bind(fixup_label);
4574 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4575}
4576
Vladimir Markoeebb8212018-06-05 14:57:24 +01004577void CodeGeneratorARM64::LoadBootImageAddress(vixl::aarch64::Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01004578 uint32_t boot_image_reference) {
4579 if (GetCompilerOptions().IsBootImage()) {
4580 // Add ADRP with its PC-relative type patch.
4581 vixl::aarch64::Label* adrp_label = NewBootImageIntrinsicPatch(boot_image_reference);
4582 EmitAdrpPlaceholder(adrp_label, reg.X());
4583 // Add ADD with its PC-relative type patch.
4584 vixl::aarch64::Label* add_label = NewBootImageIntrinsicPatch(boot_image_reference, adrp_label);
4585 EmitAddPlaceholder(add_label, reg.X(), reg.X());
Vladimir Markoa2da9b92018-10-10 14:21:55 +01004586 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01004587 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004588 vixl::aarch64::Label* adrp_label = NewBootImageRelRoPatch(boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004589 EmitAdrpPlaceholder(adrp_label, reg.X());
4590 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6fd16062018-06-26 11:02:04 +01004591 vixl::aarch64::Label* ldr_label = NewBootImageRelRoPatch(boot_image_reference, adrp_label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01004592 EmitLdrOffsetPlaceholder(ldr_label, reg.W(), reg.X());
4593 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004594 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01004595 gc::Heap* heap = Runtime::Current()->GetHeap();
4596 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01004597 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01004598 __ Ldr(reg.W(), DeduplicateBootImageAddressLiteral(reinterpret_cast<uintptr_t>(address)));
4599 }
4600}
4601
Vladimir Marko6fd16062018-06-26 11:02:04 +01004602void CodeGeneratorARM64::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
4603 uint32_t boot_image_offset) {
4604 DCHECK(invoke->IsStatic());
4605 InvokeRuntimeCallingConvention calling_convention;
4606 Register argument = calling_convention.GetRegisterAt(0);
4607 if (GetCompilerOptions().IsBootImage()) {
4608 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
4609 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
4610 MethodReference target_method = invoke->GetTargetMethod();
4611 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
4612 // Add ADRP with its PC-relative type patch.
4613 vixl::aarch64::Label* adrp_label = NewBootImageTypePatch(*target_method.dex_file, type_idx);
4614 EmitAdrpPlaceholder(adrp_label, argument.X());
4615 // Add ADD with its PC-relative type patch.
4616 vixl::aarch64::Label* add_label =
4617 NewBootImageTypePatch(*target_method.dex_file, type_idx, adrp_label);
4618 EmitAddPlaceholder(add_label, argument.X(), argument.X());
4619 } else {
4620 LoadBootImageAddress(argument, boot_image_offset);
4621 }
4622 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
4623 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4624}
4625
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004626template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004627inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4628 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004629 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004630 for (const PcRelativePatchInfo& info : infos) {
4631 linker_patches->push_back(Factory(info.label.GetLocation(),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004632 info.target_dex_file,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004633 info.pc_insn_label->GetLocation(),
4634 info.offset_or_index));
4635 }
4636}
4637
Vladimir Marko6fd16062018-06-26 11:02:04 +01004638template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
4639linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
4640 const DexFile* target_dex_file,
4641 uint32_t pc_insn_offset,
4642 uint32_t boot_image_offset) {
4643 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
4644 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00004645}
4646
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004647void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004648 DCHECK(linker_patches->empty());
4649 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004650 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004651 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004652 boot_image_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004653 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004654 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004655 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01004656 boot_image_other_patches_.size() +
Vladimir Markof6675082019-05-17 12:05:28 +01004657 call_entrypoint_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004658 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004659 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01004660 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004661 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004662 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004663 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004664 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004665 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004666 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004667 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01004668 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004669 DCHECK(boot_image_type_patches_.empty());
4670 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01004671 }
4672 if (GetCompilerOptions().IsBootImage()) {
4673 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
4674 boot_image_other_patches_, linker_patches);
4675 } else {
4676 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
4677 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004678 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004679 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4680 method_bss_entry_patches_, linker_patches);
4681 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4682 type_bss_entry_patches_, linker_patches);
4683 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4684 string_bss_entry_patches_, linker_patches);
Vladimir Markof6675082019-05-17 12:05:28 +01004685 for (const PatchInfo<vixl::aarch64::Label>& info : call_entrypoint_patches_) {
4686 DCHECK(info.target_dex_file == nullptr);
4687 linker_patches->push_back(linker::LinkerPatch::CallEntrypointPatch(
4688 info.label.GetLocation(), info.offset_or_index));
4689 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004690 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004691 linker_patches->push_back(linker::LinkerPatch::BakerReadBarrierBranchPatch(
4692 info.label.GetLocation(), info.custom_data));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004693 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004694 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004695}
4696
Vladimir Markoca1e0382018-04-11 09:58:41 +00004697bool CodeGeneratorARM64::NeedsThunkCode(const linker::LinkerPatch& patch) const {
Vladimir Markof6675082019-05-17 12:05:28 +01004698 return patch.GetType() == linker::LinkerPatch::Type::kCallEntrypoint ||
4699 patch.GetType() == linker::LinkerPatch::Type::kBakerReadBarrierBranch ||
Vladimir Markoca1e0382018-04-11 09:58:41 +00004700 patch.GetType() == linker::LinkerPatch::Type::kCallRelative;
4701}
4702
4703void CodeGeneratorARM64::EmitThunkCode(const linker::LinkerPatch& patch,
4704 /*out*/ ArenaVector<uint8_t>* code,
4705 /*out*/ std::string* debug_name) {
4706 Arm64Assembler assembler(GetGraph()->GetAllocator());
4707 switch (patch.GetType()) {
4708 case linker::LinkerPatch::Type::kCallRelative: {
4709 // The thunk just uses the entry point in the ArtMethod. This works even for calls
4710 // to the generic JNI and interpreter trampolines.
4711 Offset offset(ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4712 kArm64PointerSize).Int32Value());
4713 assembler.JumpTo(ManagedRegister(arm64::X0), offset, ManagedRegister(arm64::IP0));
4714 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4715 *debug_name = "MethodCallThunk";
4716 }
4717 break;
4718 }
Vladimir Markof6675082019-05-17 12:05:28 +01004719 case linker::LinkerPatch::Type::kCallEntrypoint: {
4720 Offset offset(patch.EntrypointOffset());
4721 assembler.JumpTo(ManagedRegister(arm64::TR), offset, ManagedRegister(arm64::IP0));
4722 if (GetCompilerOptions().GenerateAnyDebugInfo()) {
4723 *debug_name = "EntrypointCallThunk_" + std::to_string(offset.Uint32Value());
4724 }
4725 break;
4726 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00004727 case linker::LinkerPatch::Type::kBakerReadBarrierBranch: {
4728 DCHECK_EQ(patch.GetBakerCustomValue2(), 0u);
4729 CompileBakerReadBarrierThunk(assembler, patch.GetBakerCustomValue1(), debug_name);
4730 break;
4731 }
4732 default:
4733 LOG(FATAL) << "Unexpected patch type " << patch.GetType();
4734 UNREACHABLE();
4735 }
4736
4737 // Ensure we emit the literal pool if any.
4738 assembler.FinalizeCode();
4739 code->resize(assembler.CodeSize());
4740 MemoryRegion code_region(code->data(), code->size());
4741 assembler.FinalizeInstructions(code_region);
4742}
4743
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004744vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value) {
4745 return uint32_literals_.GetOrCreate(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004746 value,
4747 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4748}
4749
Scott Wakeling97c72b72016-06-24 16:19:36 +01004750vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004751 return uint64_literals_.GetOrCreate(
4752 value,
4753 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004754}
4755
Andreas Gampe878d58c2015-01-15 23:24:00 -08004756void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004757 // Explicit clinit checks triggered by static invokes must have been pruned by
4758 // art::PrepareForRegisterAllocation.
4759 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004760
Andreas Gampe878d58c2015-01-15 23:24:00 -08004761 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08004762 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004763 return;
4764 }
4765
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004766 {
4767 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4768 // are no pools emitted.
4769 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4770 LocationSummary* locations = invoke->GetLocations();
4771 codegen_->GenerateStaticOrDirectCall(
4772 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
4773 }
4774
Andreas Gampe3db70682018-12-26 15:12:03 -08004775 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004776}
4777
4778void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004779 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08004780 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004781 return;
4782 }
4783
Roland Levillain2b03a1f2017-06-06 16:09:59 +01004784 {
4785 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4786 // are no pools emitted.
4787 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
4788 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
4789 DCHECK(!codegen_->IsLeafMethod());
4790 }
4791
Andreas Gampe3db70682018-12-26 15:12:03 -08004792 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01004793}
4794
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004795HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4796 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004797 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004798 case HLoadClass::LoadKind::kInvalid:
4799 LOG(FATAL) << "UNREACHABLE";
4800 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004801 case HLoadClass::LoadKind::kReferrersClass:
4802 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004803 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004804 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004805 case HLoadClass::LoadKind::kBssEntry:
4806 DCHECK(!Runtime::Current()->UseJitCompilation());
4807 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004808 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004809 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004810 DCHECK(Runtime::Current()->UseJitCompilation());
4811 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004812 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004813 break;
4814 }
4815 return desired_class_load_kind;
4816}
4817
Alexandre Rames67555f72014-11-18 10:55:16 +00004818void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004819 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004820 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004821 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004822 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004823 cls,
4824 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004825 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004826 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004827 return;
4828 }
Vladimir Marko41559982017-01-06 14:04:23 +00004829 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004830
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004831 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4832 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004833 ? LocationSummary::kCallOnSlowPath
4834 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004835 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004836 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004837 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004838 }
4839
Vladimir Marko41559982017-01-06 14:04:23 +00004840 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004841 locations->SetInAt(0, Location::RequiresRegister());
4842 }
4843 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004844 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4845 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4846 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01004847 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004848 } else {
4849 // For non-Baker read barrier we have a temp-clobbering call.
4850 }
4851 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004852}
4853
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004854// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4855// move.
4856void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004857 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004858 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00004859 codegen_->GenerateLoadClassRuntimeCall(cls);
Andreas Gampe3db70682018-12-26 15:12:03 -08004860 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Calin Juravle580b6092015-10-06 17:35:58 +01004861 return;
4862 }
Vladimir Marko41559982017-01-06 14:04:23 +00004863 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004864
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004865 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004866 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004867
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004868 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4869 ? kWithoutReadBarrier
4870 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004871 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004872 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004873 case HLoadClass::LoadKind::kReferrersClass: {
4874 DCHECK(!cls->CanCallRuntime());
4875 DCHECK(!cls->MustGenerateClinitCheck());
4876 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4877 Register current_method = InputRegisterAt(cls, 0);
Vladimir Markoca1e0382018-04-11 09:58:41 +00004878 codegen_->GenerateGcRootFieldLoad(cls,
4879 out_loc,
4880 current_method,
4881 ArtMethod::DeclaringClassOffset().Int32Value(),
Andreas Gampe3db70682018-12-26 15:12:03 -08004882 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00004883 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004884 break;
4885 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004886 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004887 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
4888 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004889 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004890 // Add ADRP with its PC-relative type patch.
4891 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004892 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004893 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004894 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004895 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004896 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004897 codegen_->NewBootImageTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004898 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004899 break;
4900 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004901 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004902 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004903 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
4904 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
4905 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004906 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004907 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004908 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004909 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004910 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004911 break;
4912 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004913 case HLoadClass::LoadKind::kBssEntry: {
4914 // Add ADRP with its PC-relative Class .bss entry patch.
4915 const DexFile& dex_file = cls->GetDexFile();
4916 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00004917 vixl::aarch64::Register temp = XRegisterFrom(out_loc);
4918 vixl::aarch64::Label* adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4919 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004920 // Add LDR with its PC-relative Class .bss entry patch.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004921 vixl::aarch64::Label* ldr_label =
Vladimir Markof3c52b42017-11-17 17:32:12 +00004922 codegen_->NewBssEntryTypePatch(dex_file, type_index, adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004923 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004924 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00004925 codegen_->GenerateGcRootFieldLoad(cls,
4926 out_loc,
4927 temp,
4928 /* offset placeholder */ 0u,
4929 ldr_label,
4930 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004931 generate_null_check = true;
4932 break;
4933 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004934 case HLoadClass::LoadKind::kJitBootImageAddress: {
4935 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
4936 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
4937 DCHECK_NE(address, 0u);
4938 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
4939 break;
4940 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004941 case HLoadClass::LoadKind::kJitTableAddress: {
4942 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4943 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004944 cls->GetClass()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00004945 codegen_->GenerateGcRootFieldLoad(cls,
4946 out_loc,
4947 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08004948 /* offset= */ 0,
4949 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00004950 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004951 break;
4952 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004953 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004954 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004955 LOG(FATAL) << "UNREACHABLE";
4956 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004957 }
4958
Vladimir Markoea4c1262017-02-06 19:59:33 +00004959 bool do_clinit = cls->MustGenerateClinitCheck();
4960 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004961 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01004962 SlowPathCodeARM64* slow_path =
4963 new (codegen_->GetScopedAllocator()) LoadClassSlowPathARM64(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004964 codegen_->AddSlowPath(slow_path);
4965 if (generate_null_check) {
4966 __ Cbz(out, slow_path->GetEntryLabel());
4967 }
4968 if (cls->MustGenerateClinitCheck()) {
4969 GenerateClassInitializationCheck(slow_path, out);
4970 } else {
4971 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004972 }
Andreas Gampe3db70682018-12-26 15:12:03 -08004973 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00004974 }
4975}
4976
Orion Hodsondbaa5c72018-05-10 08:22:46 +01004977void LocationsBuilderARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
4978 InvokeRuntimeCallingConvention calling_convention;
4979 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
4980 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
4981}
4982
4983void InstructionCodeGeneratorARM64::VisitLoadMethodHandle(HLoadMethodHandle* load) {
4984 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
4985}
4986
Orion Hodson18259d72018-04-12 11:18:23 +01004987void LocationsBuilderARM64::VisitLoadMethodType(HLoadMethodType* load) {
4988 InvokeRuntimeCallingConvention calling_convention;
4989 Location location = LocationFrom(calling_convention.GetRegisterAt(0));
4990 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
4991}
4992
4993void InstructionCodeGeneratorARM64::VisitLoadMethodType(HLoadMethodType* load) {
4994 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
4995}
4996
David Brazdilcb1c0552015-08-04 16:22:25 +01004997static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004998 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004999}
5000
Alexandre Rames67555f72014-11-18 10:55:16 +00005001void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5002 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005003 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexandre Rames67555f72014-11-18 10:55:16 +00005004 locations->SetOut(Location::RequiresRegister());
5005}
5006
5007void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005008 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5009}
5010
5011void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005012 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01005013}
5014
5015void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5016 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005017}
5018
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005019HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5020 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005021 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005022 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005023 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005024 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005025 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005026 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005027 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005028 case HLoadString::LoadKind::kJitTableAddress:
5029 DCHECK(Runtime::Current()->UseJitCompilation());
5030 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005031 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005032 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005033 }
5034 return desired_string_load_kind;
5035}
5036
Alexandre Rames67555f72014-11-18 10:55:16 +00005037void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005038 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01005039 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005040 if (load->GetLoadKind() == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005041 InvokeRuntimeCallingConvention calling_convention;
5042 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5043 } else {
5044 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005045 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5046 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005047 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01005048 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005049 } else {
5050 // For non-Baker read barrier we have a temp-clobbering call.
5051 }
5052 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005053 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005054}
5055
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005056// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5057// move.
5058void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005059 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005060 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005061
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005062 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005063 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005064 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
5065 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005066 // Add ADRP with its PC-relative String patch.
5067 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005068 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005069 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005070 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005071 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005072 vixl::aarch64::Label* add_label =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005073 codegen_->NewBootImageStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005074 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005075 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005076 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005077 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005078 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005079 // Add ADRP with its PC-relative .data.bimg.rel.ro patch.
5080 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
5081 vixl::aarch64::Label* adrp_label = codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005082 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005083 // Add LDR with its PC-relative .data.bimg.rel.ro patch.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005084 vixl::aarch64::Label* ldr_label =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005085 codegen_->NewBootImageRelRoPatch(boot_image_offset, adrp_label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005086 codegen_->EmitLdrOffsetPlaceholder(ldr_label, out.W(), out.X());
5087 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005088 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005089 case HLoadString::LoadKind::kBssEntry: {
5090 // Add ADRP with its PC-relative String .bss entry patch.
5091 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005092 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markof3c52b42017-11-17 17:32:12 +00005093 Register temp = XRegisterFrom(out_loc);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005094 vixl::aarch64::Label* adrp_label = codegen_->NewStringBssEntryPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005095 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005096 // Add LDR with its PC-relative String .bss entry patch.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005097 vixl::aarch64::Label* ldr_label =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005098 codegen_->NewStringBssEntryPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005099 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005100 // All aligned loads are implicitly atomic consume operations on ARM64.
Vladimir Markoca1e0382018-04-11 09:58:41 +00005101 codegen_->GenerateGcRootFieldLoad(load,
5102 out_loc,
5103 temp,
5104 /* offset placeholder */ 0u,
5105 ldr_label,
5106 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005107 SlowPathCodeARM64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00005108 new (codegen_->GetScopedAllocator()) LoadStringSlowPathARM64(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005109 codegen_->AddSlowPath(slow_path);
5110 __ Cbz(out.X(), slow_path->GetEntryLabel());
5111 __ Bind(slow_path->GetExitLabel());
Andreas Gampe3db70682018-12-26 15:12:03 -08005112 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005113 return;
5114 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005115 case HLoadString::LoadKind::kJitBootImageAddress: {
5116 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
5117 DCHECK_NE(address, 0u);
5118 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
5119 return;
5120 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005121 case HLoadString::LoadKind::kJitTableAddress: {
5122 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005123 load->GetStringIndex(),
5124 load->GetString()));
Vladimir Markoca1e0382018-04-11 09:58:41 +00005125 codegen_->GenerateGcRootFieldLoad(load,
5126 out_loc,
5127 out.X(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005128 /* offset= */ 0,
5129 /* fixup_label= */ nullptr,
Vladimir Markoca1e0382018-04-11 09:58:41 +00005130 kCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005131 return;
5132 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005133 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005134 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005135 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005136
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005137 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005138 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005139 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005140 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005141 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5142 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005143 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005144}
5145
Alexandre Rames5319def2014-10-23 10:03:10 +01005146void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005147 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexandre Rames5319def2014-10-23 10:03:10 +01005148 locations->SetOut(Location::ConstantLocation(constant));
5149}
5150
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005151void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005152 // Will be generated at use site.
5153}
5154
Alexandre Rames67555f72014-11-18 10:55:16 +00005155void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005156 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5157 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005158 InvokeRuntimeCallingConvention calling_convention;
5159 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5160}
5161
5162void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005163 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005164 instruction,
5165 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005166 if (instruction->IsEnter()) {
5167 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5168 } else {
5169 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5170 }
Andreas Gampe3db70682018-12-26 15:12:03 -08005171 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames67555f72014-11-18 10:55:16 +00005172}
5173
Alexandre Rames42d641b2014-10-27 14:00:51 +00005174void LocationsBuilderARM64::VisitMul(HMul* mul) {
5175 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005176 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005177 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005178 case DataType::Type::kInt32:
5179 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005180 locations->SetInAt(0, Location::RequiresRegister());
5181 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005182 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005183 break;
5184
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005185 case DataType::Type::kFloat32:
5186 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005187 locations->SetInAt(0, Location::RequiresFpuRegister());
5188 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005189 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005190 break;
5191
5192 default:
5193 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5194 }
5195}
5196
5197void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5198 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005199 case DataType::Type::kInt32:
5200 case DataType::Type::kInt64:
Alexandre Rames42d641b2014-10-27 14:00:51 +00005201 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5202 break;
5203
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005204 case DataType::Type::kFloat32:
5205 case DataType::Type::kFloat64:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005206 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005207 break;
5208
5209 default:
5210 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5211 }
5212}
5213
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005214void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5215 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005216 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005217 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005218 case DataType::Type::kInt32:
5219 case DataType::Type::kInt64:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005220 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005221 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005222 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005223
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005224 case DataType::Type::kFloat32:
5225 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005226 locations->SetInAt(0, Location::RequiresFpuRegister());
5227 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005228 break;
5229
5230 default:
5231 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5232 }
5233}
5234
5235void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5236 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005237 case DataType::Type::kInt32:
5238 case DataType::Type::kInt64:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005239 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5240 break;
5241
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005242 case DataType::Type::kFloat32:
5243 case DataType::Type::kFloat64:
Alexandre Rames67555f72014-11-18 10:55:16 +00005244 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005245 break;
5246
5247 default:
5248 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5249 }
5250}
5251
5252void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005253 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5254 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005255 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005256 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005257 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5258 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005259}
5260
5261void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005262 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5263 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005264 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005265 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005266 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005267}
5268
Alexandre Rames5319def2014-10-23 10:03:10 +01005269void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005270 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5271 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005272 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07005273 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005274 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexandre Rames5319def2014-10-23 10:03:10 +01005275}
5276
5277void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005278 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5279 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Andreas Gampe3db70682018-12-26 15:12:03 -08005280 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005281}
5282
5283void LocationsBuilderARM64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005284 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005285 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005286 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005287}
5288
5289void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005290 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005291 case DataType::Type::kInt32:
5292 case DataType::Type::kInt64:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005293 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005294 break;
5295
5296 default:
5297 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5298 }
5299}
5300
David Brazdil66d126e2015-04-03 16:02:44 +01005301void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005302 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
David Brazdil66d126e2015-04-03 16:02:44 +01005303 locations->SetInAt(0, Location::RequiresRegister());
5304 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5305}
5306
5307void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005308 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005309}
5310
Alexandre Rames5319def2014-10-23 10:03:10 +01005311void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005312 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5313 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005314}
5315
Calin Juravle2ae48182016-03-16 14:05:09 +00005316void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5317 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005318 return;
5319 }
Artem Serov914d7a82017-02-07 14:33:49 +00005320 {
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005321 // Ensure that between load and RecordPcInfo there are no pools emitted.
Artem Serov914d7a82017-02-07 14:33:49 +00005322 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5323 Location obj = instruction->GetLocations()->InAt(0);
5324 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5325 RecordPcInfo(instruction, instruction->GetDexPc());
5326 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005327}
5328
Calin Juravle2ae48182016-03-16 14:05:09 +00005329void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005330 SlowPathCodeARM64* slow_path = new (GetScopedAllocator()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005331 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005332
5333 LocationSummary* locations = instruction->GetLocations();
5334 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005335
5336 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005337}
5338
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005339void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005340 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005341}
5342
Alexandre Rames67555f72014-11-18 10:55:16 +00005343void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5344 HandleBinaryOp(instruction);
5345}
5346
5347void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5348 HandleBinaryOp(instruction);
5349}
5350
Alexandre Rames3e69f162014-12-10 10:36:50 +00005351void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5352 LOG(FATAL) << "Unreachable";
5353}
5354
5355void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005356 if (instruction->GetNext()->IsSuspendCheck() &&
5357 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5358 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5359 // The back edge will generate the suspend check.
5360 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5361 }
5362
Alexandre Rames3e69f162014-12-10 10:36:50 +00005363 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5364}
5365
Alexandre Rames5319def2014-10-23 10:03:10 +01005366void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005367 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005368 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5369 if (location.IsStackSlot()) {
5370 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5371 } else if (location.IsDoubleStackSlot()) {
5372 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5373 }
5374 locations->SetOut(location);
5375}
5376
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005377void InstructionCodeGeneratorARM64::VisitParameterValue(
5378 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005379 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005380}
5381
5382void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5383 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005384 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005385 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005386}
5387
5388void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5389 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5390 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005391}
5392
5393void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005394 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005395 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005396 locations->SetInAt(i, Location::Any());
5397 }
5398 locations->SetOut(Location::Any());
5399}
5400
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005401void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005402 LOG(FATAL) << "Unreachable";
5403}
5404
Serban Constantinescu02164b32014-11-13 14:05:07 +00005405void LocationsBuilderARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005406 DataType::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005407 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005408 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005409 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005410 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005411
5412 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005413 case DataType::Type::kInt32:
5414 case DataType::Type::kInt64:
Serban Constantinescu02164b32014-11-13 14:05:07 +00005415 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005416 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005417 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5418 break;
5419
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005420 case DataType::Type::kFloat32:
5421 case DataType::Type::kFloat64: {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005422 InvokeRuntimeCallingConvention calling_convention;
5423 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5424 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5425 locations->SetOut(calling_convention.GetReturnLocation(type));
5426
5427 break;
5428 }
5429
Serban Constantinescu02164b32014-11-13 14:05:07 +00005430 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005431 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005432 }
5433}
5434
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005435void InstructionCodeGeneratorARM64::GenerateIntRemForPower2Denom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005436 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005437 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
5438 DCHECK(IsPowerOfTwo(abs_imm)) << abs_imm;
5439
5440 Register out = OutputRegister(instruction);
5441 Register dividend = InputRegisterAt(instruction, 0);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005442
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005443 if (abs_imm == 2) {
5444 __ Cmp(dividend, 0);
5445 __ And(out, dividend, 1);
5446 __ Csneg(out, out, out, ge);
5447 } else {
5448 UseScratchRegisterScope temps(GetVIXLAssembler());
5449 Register temp = temps.AcquireSameSizeAs(out);
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005450
Evgeny Astigeevicha3234e92018-06-19 23:26:15 +01005451 __ Negs(temp, dividend);
5452 __ And(out, dividend, abs_imm - 1);
5453 __ And(temp, temp, abs_imm - 1);
5454 __ Csneg(out, out, temp, mi);
5455 }
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005456}
5457
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005458void InstructionCodeGeneratorARM64::GenerateIntRemForConstDenom(HRem *instruction) {
Evgeny Astigeevichf9e90542018-06-25 13:43:53 +01005459 int64_t imm = Int64FromLocation(instruction->GetLocations()->InAt(1));
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005460
5461 if (imm == 0) {
5462 // Do not generate anything.
5463 // DivZeroCheck would prevent any code to be executed.
5464 return;
5465 }
5466
Evgeny Astigeevichf58dc652018-06-25 17:54:07 +01005467 if (IsPowerOfTwo(AbsOrMin(imm))) {
5468 // Cases imm == -1 or imm == 1 are handled in constant folding by
5469 // InstructionWithAbsorbingInputSimplifier.
5470 // If the cases have survided till code generation they are handled in
5471 // GenerateIntRemForPower2Denom becauses -1 and 1 are the power of 2 (2^0).
5472 // The correct code is generated for them, just more instructions.
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005473 GenerateIntRemForPower2Denom(instruction);
5474 } else {
5475 DCHECK(imm < -2 || imm > 2) << imm;
5476 GenerateDivRemWithAnyConstant(instruction);
5477 }
5478}
5479
5480void InstructionCodeGeneratorARM64::GenerateIntRem(HRem* instruction) {
5481 DCHECK(DataType::IsIntOrLongType(instruction->GetResultType()))
5482 << instruction->GetResultType();
5483
5484 if (instruction->GetLocations()->InAt(1).IsConstant()) {
5485 GenerateIntRemForConstDenom(instruction);
5486 } else {
5487 Register out = OutputRegister(instruction);
5488 Register dividend = InputRegisterAt(instruction, 0);
5489 Register divisor = InputRegisterAt(instruction, 1);
5490 UseScratchRegisterScope temps(GetVIXLAssembler());
5491 Register temp = temps.AcquireSameSizeAs(out);
5492 __ Sdiv(temp, dividend, divisor);
5493 __ Msub(out, temp, divisor, dividend);
5494 }
5495}
5496
Serban Constantinescu02164b32014-11-13 14:05:07 +00005497void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005498 DataType::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005499
Serban Constantinescu02164b32014-11-13 14:05:07 +00005500 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005501 case DataType::Type::kInt32:
5502 case DataType::Type::kInt64: {
Evgeny Astigeevich878f17d2018-06-01 16:53:58 +01005503 GenerateIntRem(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005504 break;
5505 }
5506
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005507 case DataType::Type::kFloat32:
5508 case DataType::Type::kFloat64: {
5509 QuickEntrypointEnum entrypoint =
5510 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005511 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005512 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00005513 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5514 } else {
5515 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5516 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005517 break;
5518 }
5519
Serban Constantinescu02164b32014-11-13 14:05:07 +00005520 default:
5521 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005522 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005523 }
5524}
5525
Aart Bik1f8d51b2018-02-15 10:42:37 -08005526void LocationsBuilderARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005527 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005528}
5529
Aart Bik1f8d51b2018-02-15 10:42:37 -08005530void InstructionCodeGeneratorARM64::VisitMin(HMin* min) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005531 HandleBinaryOp(min);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005532}
5533
5534void LocationsBuilderARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005535 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005536}
5537
5538void InstructionCodeGeneratorARM64::VisitMax(HMax* max) {
Petre-Ionut Tudor2227fe42018-04-20 17:12:05 +01005539 HandleBinaryOp(max);
Aart Bik1f8d51b2018-02-15 10:42:37 -08005540}
5541
Aart Bik3dad3412018-02-28 12:01:46 -08005542void LocationsBuilderARM64::VisitAbs(HAbs* abs) {
5543 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
5544 switch (abs->GetResultType()) {
5545 case DataType::Type::kInt32:
5546 case DataType::Type::kInt64:
5547 locations->SetInAt(0, Location::RequiresRegister());
5548 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5549 break;
5550 case DataType::Type::kFloat32:
5551 case DataType::Type::kFloat64:
5552 locations->SetInAt(0, Location::RequiresFpuRegister());
5553 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5554 break;
5555 default:
5556 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5557 }
5558}
5559
5560void InstructionCodeGeneratorARM64::VisitAbs(HAbs* abs) {
5561 switch (abs->GetResultType()) {
5562 case DataType::Type::kInt32:
5563 case DataType::Type::kInt64: {
5564 Register in_reg = InputRegisterAt(abs, 0);
5565 Register out_reg = OutputRegister(abs);
5566 __ Cmp(in_reg, Operand(0));
5567 __ Cneg(out_reg, in_reg, lt);
5568 break;
5569 }
5570 case DataType::Type::kFloat32:
5571 case DataType::Type::kFloat64: {
Evgeny Astigeevich7d48dcd2019-10-16 12:46:28 +01005572 VRegister in_reg = InputFPRegisterAt(abs, 0);
5573 VRegister out_reg = OutputFPRegister(abs);
Aart Bik3dad3412018-02-28 12:01:46 -08005574 __ Fabs(out_reg, in_reg);
5575 break;
5576 }
5577 default:
5578 LOG(FATAL) << "Unexpected type for abs operation " << abs->GetResultType();
5579 }
5580}
5581
Igor Murashkind01745e2017-04-05 16:40:31 -07005582void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5583 constructor_fence->SetLocations(nullptr);
5584}
5585
5586void InstructionCodeGeneratorARM64::VisitConstructorFence(
5587 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5588 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5589}
5590
Calin Juravle27df7582015-04-17 19:12:31 +01005591void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5592 memory_barrier->SetLocations(nullptr);
5593}
5594
5595void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005596 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005597}
5598
Alexandre Rames5319def2014-10-23 10:03:10 +01005599void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005600 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005601 DataType::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005602 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005603}
5604
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00005605void InstructionCodeGeneratorARM64::VisitReturn(HReturn* ret) {
5606 if (GetGraph()->IsCompilingOsr()) {
5607 // To simplify callers of an OSR method, we put the return value in both
5608 // floating point and core register.
5609 switch (ret->InputAt(0)->GetType()) {
5610 case DataType::Type::kFloat32:
5611 __ Fmov(w0, s0);
5612 break;
5613 case DataType::Type::kFloat64:
5614 __ Fmov(x0, d0);
5615 break;
5616 default:
5617 break;
5618 }
5619 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005620 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005621}
5622
5623void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5624 instruction->SetLocations(nullptr);
5625}
5626
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005627void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005628 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005629}
5630
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005631void LocationsBuilderARM64::VisitRor(HRor* ror) {
5632 HandleBinaryOp(ror);
5633}
5634
5635void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5636 HandleBinaryOp(ror);
5637}
5638
Serban Constantinescu02164b32014-11-13 14:05:07 +00005639void LocationsBuilderARM64::VisitShl(HShl* shl) {
5640 HandleShift(shl);
5641}
5642
5643void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5644 HandleShift(shl);
5645}
5646
5647void LocationsBuilderARM64::VisitShr(HShr* shr) {
5648 HandleShift(shr);
5649}
5650
5651void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5652 HandleShift(shr);
5653}
5654
Alexandre Rames5319def2014-10-23 10:03:10 +01005655void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005656 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005657}
5658
5659void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005660 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005661}
5662
Alexandre Rames67555f72014-11-18 10:55:16 +00005663void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005664 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005665}
5666
5667void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005668 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005669}
5670
5671void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005672 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005673}
5674
Alexandre Rames67555f72014-11-18 10:55:16 +00005675void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005676 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005677}
5678
Vladimir Marko552a1342017-10-31 10:56:47 +00005679void LocationsBuilderARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5680 codegen_->CreateStringBuilderAppendLocations(instruction, LocationFrom(x0));
5681}
5682
5683void InstructionCodeGeneratorARM64::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5684 __ Mov(w0, instruction->GetFormat()->GetValue());
5685 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5686}
5687
Calin Juravlee460d1d2015-09-29 04:52:17 +01005688void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5689 HUnresolvedInstanceFieldGet* instruction) {
5690 FieldAccessCallingConventionARM64 calling_convention;
5691 codegen_->CreateUnresolvedFieldLocationSummary(
5692 instruction, instruction->GetFieldType(), calling_convention);
5693}
5694
5695void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5696 HUnresolvedInstanceFieldGet* instruction) {
5697 FieldAccessCallingConventionARM64 calling_convention;
5698 codegen_->GenerateUnresolvedFieldAccess(instruction,
5699 instruction->GetFieldType(),
5700 instruction->GetFieldIndex(),
5701 instruction->GetDexPc(),
5702 calling_convention);
5703}
5704
5705void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5706 HUnresolvedInstanceFieldSet* instruction) {
5707 FieldAccessCallingConventionARM64 calling_convention;
5708 codegen_->CreateUnresolvedFieldLocationSummary(
5709 instruction, instruction->GetFieldType(), calling_convention);
5710}
5711
5712void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5713 HUnresolvedInstanceFieldSet* instruction) {
5714 FieldAccessCallingConventionARM64 calling_convention;
5715 codegen_->GenerateUnresolvedFieldAccess(instruction,
5716 instruction->GetFieldType(),
5717 instruction->GetFieldIndex(),
5718 instruction->GetDexPc(),
5719 calling_convention);
5720}
5721
5722void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5723 HUnresolvedStaticFieldGet* instruction) {
5724 FieldAccessCallingConventionARM64 calling_convention;
5725 codegen_->CreateUnresolvedFieldLocationSummary(
5726 instruction, instruction->GetFieldType(), calling_convention);
5727}
5728
5729void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5730 HUnresolvedStaticFieldGet* instruction) {
5731 FieldAccessCallingConventionARM64 calling_convention;
5732 codegen_->GenerateUnresolvedFieldAccess(instruction,
5733 instruction->GetFieldType(),
5734 instruction->GetFieldIndex(),
5735 instruction->GetDexPc(),
5736 calling_convention);
5737}
5738
5739void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5740 HUnresolvedStaticFieldSet* instruction) {
5741 FieldAccessCallingConventionARM64 calling_convention;
5742 codegen_->CreateUnresolvedFieldLocationSummary(
5743 instruction, instruction->GetFieldType(), calling_convention);
5744}
5745
5746void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5747 HUnresolvedStaticFieldSet* instruction) {
5748 FieldAccessCallingConventionARM64 calling_convention;
5749 codegen_->GenerateUnresolvedFieldAccess(instruction,
5750 instruction->GetFieldType(),
5751 instruction->GetFieldIndex(),
5752 instruction->GetDexPc(),
5753 calling_convention);
5754}
5755
Alexandre Rames5319def2014-10-23 10:03:10 +01005756void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005757 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5758 instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005759 // In suspend check slow path, usually there are no caller-save registers at all.
5760 // If SIMD instructions are present, however, we force spilling all live SIMD
5761 // registers in full width (since the runtime only saves/restores lower part).
5762 locations->SetCustomSlowPathCallerSaves(
5763 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005764}
5765
5766void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005767 HBasicBlock* block = instruction->GetBlock();
5768 if (block->GetLoopInformation() != nullptr) {
5769 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5770 // The back edge will generate the suspend check.
5771 return;
5772 }
5773 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5774 // The goto will generate the suspend check.
5775 return;
5776 }
5777 GenerateSuspendCheck(instruction, nullptr);
Andreas Gampe3db70682018-12-26 15:12:03 -08005778 codegen_->MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Alexandre Rames5319def2014-10-23 10:03:10 +01005779}
5780
Alexandre Rames67555f72014-11-18 10:55:16 +00005781void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005782 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5783 instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005784 InvokeRuntimeCallingConvention calling_convention;
5785 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5786}
5787
5788void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005789 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005790 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005791}
5792
5793void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5794 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005795 new (GetGraph()->GetAllocator()) LocationSummary(conversion, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005796 DataType::Type input_type = conversion->GetInputType();
5797 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005798 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5799 << input_type << " -> " << result_type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005800 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
5801 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005802 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5803 }
5804
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005805 if (DataType::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005806 locations->SetInAt(0, Location::RequiresFpuRegister());
5807 } else {
5808 locations->SetInAt(0, Location::RequiresRegister());
5809 }
5810
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005811 if (DataType::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005812 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5813 } else {
5814 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5815 }
5816}
5817
5818void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005819 DataType::Type result_type = conversion->GetResultType();
5820 DataType::Type input_type = conversion->GetInputType();
Alexandre Rames67555f72014-11-18 10:55:16 +00005821
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005822 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
5823 << input_type << " -> " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005824
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005825 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
5826 int result_size = DataType::Size(result_type);
5827 int input_size = DataType::Size(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005828 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005829 Register output = OutputRegister(conversion);
5830 Register source = InputRegisterAt(conversion, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005831 if (result_type == DataType::Type::kInt32 && input_type == DataType::Type::kInt64) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005832 // 'int' values are used directly as W registers, discarding the top
5833 // bits, so we don't need to sign-extend and can just perform a move.
5834 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5835 // top 32 bits of the target register. We theoretically could leave those
5836 // bits unchanged, but we would have to make sure that no code uses a
5837 // 32bit input value as a 64bit value assuming that the top 32 bits are
5838 // zero.
5839 __ Mov(output.W(), source.W());
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005840 } else if (DataType::IsUnsignedType(result_type) ||
5841 (DataType::IsUnsignedType(input_type) && input_size < result_size)) {
5842 __ Ubfx(output, output.IsX() ? source.X() : source.W(), 0, result_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005843 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005844 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005845 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005846 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005847 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005848 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
5849 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005850 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005851 } else if (DataType::IsFloatingPointType(result_type) &&
5852 DataType::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005853 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5854 } else {
5855 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5856 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005857 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005858}
Alexandre Rames67555f72014-11-18 10:55:16 +00005859
Serban Constantinescu02164b32014-11-13 14:05:07 +00005860void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5861 HandleShift(ushr);
5862}
5863
5864void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5865 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005866}
5867
5868void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5869 HandleBinaryOp(instruction);
5870}
5871
5872void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5873 HandleBinaryOp(instruction);
5874}
5875
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005876void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005877 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005878 LOG(FATAL) << "Unreachable";
5879}
5880
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005881void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005882 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005883 LOG(FATAL) << "Unreachable";
5884}
5885
Mark Mendellfe57faa2015-09-18 09:26:15 -04005886// Simple implementation of packed switch - generate cascaded compare/jumps.
5887void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5888 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005889 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005890 locations->SetInAt(0, Location::RequiresRegister());
5891}
5892
5893void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5894 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005895 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005896 Register value_reg = InputRegisterAt(switch_instr, 0);
5897 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5898
Zheng Xu3927c8b2015-11-18 17:46:25 +08005899 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005900 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005901 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5902 // make sure we don't emit it if the target may run out of range.
5903 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5904 // ranges and emit the tables only as required.
5905 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005906
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005907 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005908 // Current instruction id is an upper bound of the number of HIRs in the graph.
5909 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5910 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005911 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5912 Register temp = temps.AcquireW();
5913 __ Subs(temp, value_reg, Operand(lower_bound));
5914
Zheng Xu3927c8b2015-11-18 17:46:25 +08005915 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005916 // Jump to successors[0] if value == lower_bound.
5917 __ B(eq, codegen_->GetLabelOf(successors[0]));
5918 int32_t last_index = 0;
5919 for (; num_entries - last_index > 2; last_index += 2) {
5920 __ Subs(temp, temp, Operand(2));
5921 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5922 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5923 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5924 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5925 }
5926 if (num_entries - last_index == 2) {
5927 // The last missing case_value.
5928 __ Cmp(temp, Operand(1));
5929 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005930 }
5931
5932 // And the default for any other value.
5933 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5934 __ B(codegen_->GetLabelOf(default_block));
5935 }
5936 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005937 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005938
5939 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5940
5941 // Below instructions should use at most one blocked register. Since there are two blocked
5942 // registers, we are free to block one.
5943 Register temp_w = temps.AcquireW();
5944 Register index;
5945 // Remove the bias.
5946 if (lower_bound != 0) {
5947 index = temp_w;
5948 __ Sub(index, value_reg, Operand(lower_bound));
5949 } else {
5950 index = value_reg;
5951 }
5952
5953 // Jump to default block if index is out of the range.
5954 __ Cmp(index, Operand(num_entries));
5955 __ B(hs, codegen_->GetLabelOf(default_block));
5956
5957 // In current VIXL implementation, it won't require any blocked registers to encode the
5958 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5959 // register pressure.
5960 Register table_base = temps.AcquireX();
5961 // Load jump offset from the table.
5962 __ Adr(table_base, jump_table->GetTableStartLabel());
5963 Register jump_offset = temp_w;
5964 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5965
5966 // Jump to target block by branching to table_base(pc related) + offset.
5967 Register target_address = table_base;
5968 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5969 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005970 }
5971}
5972
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005973void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5974 HInstruction* instruction,
5975 Location out,
5976 uint32_t offset,
5977 Location maybe_temp,
5978 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005979 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00005980 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005981 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005982 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005983 if (kUseBakerReadBarrier) {
5984 // Load with fast path based Baker's read barrier.
5985 // /* HeapReference<Object> */ out = *(out + offset)
5986 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5987 out,
5988 out_reg,
5989 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005990 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08005991 /* needs_null_check= */ false,
5992 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005993 } else {
5994 // Load with slow path based read barrier.
5995 // Save the value of `out` into `maybe_temp` before overwriting it
5996 // in the following move operation, as we will need it for the
5997 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005998 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005999 __ Mov(temp_reg, out_reg);
6000 // /* HeapReference<Object> */ out = *(out + offset)
6001 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6002 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6003 }
6004 } else {
6005 // Plain load with no read barrier.
6006 // /* HeapReference<Object> */ out = *(out + offset)
6007 __ Ldr(out_reg, HeapOperand(out_reg, offset));
6008 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6009 }
6010}
6011
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006012void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
6013 HInstruction* instruction,
6014 Location out,
6015 Location obj,
6016 uint32_t offset,
6017 Location maybe_temp,
6018 ReadBarrierOption read_barrier_option) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006019 DataType::Type type = DataType::Type::kReference;
Roland Levillain44015862016-01-22 11:47:17 +00006020 Register out_reg = RegisterFrom(out, type);
6021 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006022 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006023 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006024 if (kUseBakerReadBarrier) {
6025 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00006026 // /* HeapReference<Object> */ out = *(obj + offset)
6027 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6028 out,
6029 obj_reg,
6030 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006031 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006032 /* needs_null_check= */ false,
6033 /* use_load_acquire= */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006034 } else {
6035 // Load with slow path based read barrier.
6036 // /* HeapReference<Object> */ out = *(obj + offset)
6037 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6038 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6039 }
6040 } else {
6041 // Plain load with no read barrier.
6042 // /* HeapReference<Object> */ out = *(obj + offset)
6043 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
6044 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
6045 }
6046}
6047
Vladimir Markoca1e0382018-04-11 09:58:41 +00006048void CodeGeneratorARM64::GenerateGcRootFieldLoad(
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006049 HInstruction* instruction,
6050 Location root,
6051 Register obj,
6052 uint32_t offset,
6053 vixl::aarch64::Label* fixup_label,
6054 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006055 DCHECK(fixup_label == nullptr || offset == 0u);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006056 Register root_reg = RegisterFrom(root, DataType::Type::kReference);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006057 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006058 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00006059 if (kUseBakerReadBarrier) {
6060 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00006061 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00006062
Vladimir Marko008e09f32018-08-06 15:42:43 +01006063 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in
6064 // the Marking Register) to decide whether we need to enter
6065 // the slow path to mark the GC root.
6066 //
6067 // We use shared thunks for the slow path; shared within the method
6068 // for JIT, across methods for AOT. That thunk checks the reference
6069 // and jumps to the entrypoint if needed.
6070 //
6071 // lr = &return_address;
6072 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6073 // if (mr) { // Thread::Current()->GetIsGcMarking()
6074 // goto gc_root_thunk<root_reg>(lr)
6075 // }
6076 // return_address:
Roland Levillainba650a42017-03-06 13:52:32 +00006077
Vladimir Marko008e09f32018-08-06 15:42:43 +01006078 UseScratchRegisterScope temps(GetVIXLAssembler());
6079 DCHECK(temps.IsAvailable(ip0));
6080 DCHECK(temps.IsAvailable(ip1));
6081 temps.Exclude(ip0, ip1);
6082 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
Roland Levillain44015862016-01-22 11:47:17 +00006083
Vladimir Marko008e09f32018-08-06 15:42:43 +01006084 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6085 vixl::aarch64::Label return_address;
6086 __ adr(lr, &return_address);
6087 if (fixup_label != nullptr) {
6088 __ bind(fixup_label);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006089 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006090 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
Vladimir Marko94796f82018-08-08 15:15:33 +01006091 "GC root LDR must be 2 instructions (8B) before the return address label.");
Vladimir Marko008e09f32018-08-06 15:42:43 +01006092 __ ldr(root_reg, MemOperand(obj.X(), offset));
6093 EmitBakerReadBarrierCbnz(custom_data);
6094 __ bind(&return_address);
Roland Levillain44015862016-01-22 11:47:17 +00006095 } else {
6096 // GC root loaded through a slow path for read barriers other
6097 // than Baker's.
6098 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006099 if (fixup_label == nullptr) {
6100 __ Add(root_reg.X(), obj.X(), offset);
6101 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006102 EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006103 }
Roland Levillain44015862016-01-22 11:47:17 +00006104 // /* mirror::Object* */ root = root->Read()
Vladimir Markoca1e0382018-04-11 09:58:41 +00006105 GenerateReadBarrierForRootSlow(instruction, root, root);
Roland Levillain44015862016-01-22 11:47:17 +00006106 }
6107 } else {
6108 // Plain GC root load with no read barrier.
6109 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006110 if (fixup_label == nullptr) {
6111 __ Ldr(root_reg, MemOperand(obj, offset));
6112 } else {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006113 EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006114 }
Roland Levillain44015862016-01-22 11:47:17 +00006115 // Note that GC roots are not affected by heap poisoning, thus we
6116 // do not have to unpoison `root_reg` here.
6117 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006118 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__);
Roland Levillain44015862016-01-22 11:47:17 +00006119}
6120
Vladimir Marko94796f82018-08-08 15:15:33 +01006121void CodeGeneratorARM64::GenerateUnsafeCasOldValueMovWithBakerReadBarrier(
6122 vixl::aarch64::Register marked,
6123 vixl::aarch64::Register old_value) {
6124 DCHECK(kEmitCompilerReadBarrier);
6125 DCHECK(kUseBakerReadBarrier);
6126
6127 // Similar to the Baker RB path in GenerateGcRootFieldLoad(), with a MOV instead of LDR.
6128 uint32_t custom_data = EncodeBakerReadBarrierGcRootData(marked.GetCode());
6129
6130 ExactAssemblyScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6131 vixl::aarch64::Label return_address;
6132 __ adr(lr, &return_address);
6133 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6134 "GC root LDR must be 2 instructions (8B) before the return address label.");
6135 __ mov(marked, old_value);
6136 EmitBakerReadBarrierCbnz(custom_data);
6137 __ bind(&return_address);
6138}
6139
Roland Levillain44015862016-01-22 11:47:17 +00006140void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6141 Location ref,
Vladimir Marko248141f2018-08-10 10:40:07 +01006142 vixl::aarch64::Register obj,
6143 const vixl::aarch64::MemOperand& src,
Roland Levillain44015862016-01-22 11:47:17 +00006144 bool needs_null_check,
6145 bool use_load_acquire) {
6146 DCHECK(kEmitCompilerReadBarrier);
6147 DCHECK(kUseBakerReadBarrier);
6148
Vladimir Marko0ecac682018-08-07 10:40:38 +01006149 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6150 // Marking Register) to decide whether we need to enter the slow
6151 // path to mark the reference. Then, in the slow path, check the
6152 // gray bit in the lock word of the reference's holder (`obj`) to
6153 // decide whether to mark `ref` or not.
6154 //
6155 // We use shared thunks for the slow path; shared within the method
6156 // for JIT, across methods for AOT. That thunk checks the holder
6157 // and jumps to the entrypoint if needed. If the holder is not gray,
6158 // it creates a fake dependency and returns to the LDR instruction.
6159 //
6160 // lr = &gray_return_address;
6161 // if (mr) { // Thread::Current()->GetIsGcMarking()
6162 // goto field_thunk<holder_reg, base_reg, use_load_acquire>(lr)
6163 // }
6164 // not_gray_return_address:
6165 // // Original reference load. If the offset is too large to fit
6166 // // into LDR, we use an adjusted base register here.
6167 // HeapReference<mirror::Object> reference = *(obj+offset);
6168 // gray_return_address:
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006169
Vladimir Marko248141f2018-08-10 10:40:07 +01006170 DCHECK(src.GetAddrMode() == vixl::aarch64::Offset);
6171 DCHECK_ALIGNED(src.GetOffset(), sizeof(mirror::HeapReference<mirror::Object>));
6172
6173 UseScratchRegisterScope temps(GetVIXLAssembler());
6174 DCHECK(temps.IsAvailable(ip0));
6175 DCHECK(temps.IsAvailable(ip1));
6176 temps.Exclude(ip0, ip1);
6177 uint32_t custom_data = use_load_acquire
6178 ? EncodeBakerReadBarrierAcquireData(src.GetBaseRegister().GetCode(), obj.GetCode())
6179 : EncodeBakerReadBarrierFieldData(src.GetBaseRegister().GetCode(), obj.GetCode());
6180
6181 {
6182 ExactAssemblyScope guard(GetVIXLAssembler(),
6183 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6184 vixl::aarch64::Label return_address;
6185 __ adr(lr, &return_address);
6186 EmitBakerReadBarrierCbnz(custom_data);
6187 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6188 "Field LDR must be 1 instruction (4B) before the return address label; "
6189 " 2 instructions (8B) for heap poisoning.");
6190 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
6191 if (use_load_acquire) {
6192 DCHECK_EQ(src.GetOffset(), 0);
6193 __ ldar(ref_reg, src);
6194 } else {
6195 __ ldr(ref_reg, src);
6196 }
6197 if (needs_null_check) {
6198 MaybeRecordImplicitNullCheck(instruction);
6199 }
6200 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6201 // macro instructions disallowed in ExactAssemblyScope.
6202 if (kPoisonHeapReferences) {
6203 __ neg(ref_reg, Operand(ref_reg));
6204 }
6205 __ bind(&return_address);
6206 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006207 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Vladimir Marko248141f2018-08-10 10:40:07 +01006208}
6209
6210void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6211 Location ref,
6212 Register obj,
6213 uint32_t offset,
6214 Location maybe_temp,
6215 bool needs_null_check,
6216 bool use_load_acquire) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006217 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6218 Register base = obj;
6219 if (use_load_acquire) {
6220 DCHECK(maybe_temp.IsRegister());
6221 base = WRegisterFrom(maybe_temp);
6222 __ Add(base, obj, offset);
6223 offset = 0u;
6224 } else if (offset >= kReferenceLoadMinFarOffset) {
6225 DCHECK(maybe_temp.IsRegister());
6226 base = WRegisterFrom(maybe_temp);
6227 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6228 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6229 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006230 }
Vladimir Marko248141f2018-08-10 10:40:07 +01006231 MemOperand src(base.X(), offset);
6232 GenerateFieldLoadWithBakerReadBarrier(
6233 instruction, ref, obj, src, needs_null_check, use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006234}
6235
Artem Serov0806f582018-10-11 20:14:20 +01006236void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HArrayGet* instruction,
6237 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006238 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006239 uint32_t data_offset,
6240 Location index,
Roland Levillain44015862016-01-22 11:47:17 +00006241 bool needs_null_check) {
6242 DCHECK(kEmitCompilerReadBarrier);
6243 DCHECK(kUseBakerReadBarrier);
6244
Vladimir Marko66d691d2017-04-07 17:53:39 +01006245 static_assert(
6246 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6247 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006248 size_t scale_factor = DataType::SizeShift(DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006249
Vladimir Marko008e09f32018-08-06 15:42:43 +01006250 // Query `art::Thread::Current()->GetIsGcMarking()` (stored in the
6251 // Marking Register) to decide whether we need to enter the slow
6252 // path to mark the reference. Then, in the slow path, check the
6253 // gray bit in the lock word of the reference's holder (`obj`) to
6254 // decide whether to mark `ref` or not.
6255 //
6256 // We use shared thunks for the slow path; shared within the method
6257 // for JIT, across methods for AOT. That thunk checks the holder
6258 // and jumps to the entrypoint if needed. If the holder is not gray,
6259 // it creates a fake dependency and returns to the LDR instruction.
6260 //
6261 // lr = &gray_return_address;
6262 // if (mr) { // Thread::Current()->GetIsGcMarking()
6263 // goto array_thunk<base_reg>(lr)
6264 // }
6265 // not_gray_return_address:
6266 // // Original reference load. If the offset is too large to fit
6267 // // into LDR, we use an adjusted base register here.
6268 // HeapReference<mirror::Object> reference = data[index];
6269 // gray_return_address:
Vladimir Marko66d691d2017-04-07 17:53:39 +01006270
Vladimir Marko008e09f32018-08-06 15:42:43 +01006271 DCHECK(index.IsValid());
6272 Register index_reg = RegisterFrom(index, DataType::Type::kInt32);
6273 Register ref_reg = RegisterFrom(ref, DataType::Type::kReference);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006274
Vladimir Marko008e09f32018-08-06 15:42:43 +01006275 UseScratchRegisterScope temps(GetVIXLAssembler());
6276 DCHECK(temps.IsAvailable(ip0));
6277 DCHECK(temps.IsAvailable(ip1));
6278 temps.Exclude(ip0, ip1);
Artem Serov0806f582018-10-11 20:14:20 +01006279
6280 Register temp;
6281 if (instruction->GetArray()->IsIntermediateAddress()) {
6282 // We do not need to compute the intermediate address from the array: the
6283 // input instruction has done it already. See the comment in
6284 // `TryExtractArrayAccessAddress()`.
6285 if (kIsDebugBuild) {
6286 HIntermediateAddress* interm_addr = instruction->GetArray()->AsIntermediateAddress();
6287 DCHECK_EQ(interm_addr->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6288 }
6289 temp = obj;
6290 } else {
6291 temp = WRegisterFrom(instruction->GetLocations()->GetTemp(0));
6292 __ Add(temp.X(), obj.X(), Operand(data_offset));
6293 }
6294
Vladimir Marko008e09f32018-08-06 15:42:43 +01006295 uint32_t custom_data = EncodeBakerReadBarrierArrayData(temp.GetCode());
Vladimir Marko66d691d2017-04-07 17:53:39 +01006296
Vladimir Marko008e09f32018-08-06 15:42:43 +01006297 {
6298 ExactAssemblyScope guard(GetVIXLAssembler(),
6299 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6300 vixl::aarch64::Label return_address;
6301 __ adr(lr, &return_address);
6302 EmitBakerReadBarrierCbnz(custom_data);
6303 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6304 "Array LDR must be 1 instruction (4B) before the return address label; "
6305 " 2 instructions (8B) for heap poisoning.");
6306 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6307 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6308 // Unpoison the reference explicitly if needed. MaybeUnpoisonHeapReference() uses
6309 // macro instructions disallowed in ExactAssemblyScope.
6310 if (kPoisonHeapReferences) {
6311 __ neg(ref_reg, Operand(ref_reg));
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006312 }
Vladimir Marko008e09f32018-08-06 15:42:43 +01006313 __ bind(&return_address);
Vladimir Marko66d691d2017-04-07 17:53:39 +01006314 }
Andreas Gampe3db70682018-12-26 15:12:03 -08006315 MaybeGenerateMarkingRegisterCheck(/* code= */ __LINE__, /* temp_loc= */ LocationFrom(ip1));
Roland Levillain44015862016-01-22 11:47:17 +00006316}
6317
Roland Levillain2b03a1f2017-06-06 16:09:59 +01006318void CodeGeneratorARM64::MaybeGenerateMarkingRegisterCheck(int code, Location temp_loc) {
6319 // The following condition is a compile-time one, so it does not have a run-time cost.
6320 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier && kIsDebugBuild) {
6321 // The following condition is a run-time one; it is executed after the
6322 // previous compile-time test, to avoid penalizing non-debug builds.
6323 if (GetCompilerOptions().EmitRunTimeChecksInDebugMode()) {
6324 UseScratchRegisterScope temps(GetVIXLAssembler());
6325 Register temp = temp_loc.IsValid() ? WRegisterFrom(temp_loc) : temps.AcquireW();
6326 GetAssembler()->GenerateMarkingRegisterCheck(temp, code);
6327 }
6328 }
6329}
6330
Roland Levillain44015862016-01-22 11:47:17 +00006331void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6332 Location out,
6333 Location ref,
6334 Location obj,
6335 uint32_t offset,
6336 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006337 DCHECK(kEmitCompilerReadBarrier);
6338
Roland Levillain44015862016-01-22 11:47:17 +00006339 // Insert a slow path based read barrier *after* the reference load.
6340 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006341 // If heap poisoning is enabled, the unpoisoning of the loaded
6342 // reference will be carried out by the runtime within the slow
6343 // path.
6344 //
6345 // Note that `ref` currently does not get unpoisoned (when heap
6346 // poisoning is enabled), which is alright as the `ref` argument is
6347 // not used by the artReadBarrierSlow entry point.
6348 //
6349 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006350 SlowPathCodeARM64* slow_path = new (GetScopedAllocator())
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006351 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6352 AddSlowPath(slow_path);
6353
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006354 __ B(slow_path->GetEntryLabel());
6355 __ Bind(slow_path->GetExitLabel());
6356}
6357
Roland Levillain44015862016-01-22 11:47:17 +00006358void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6359 Location out,
6360 Location ref,
6361 Location obj,
6362 uint32_t offset,
6363 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006364 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006365 // Baker's read barriers shall be handled by the fast path
6366 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6367 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006368 // If heap poisoning is enabled, unpoisoning will be taken care of
6369 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006370 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006371 } else if (kPoisonHeapReferences) {
6372 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6373 }
6374}
6375
Roland Levillain44015862016-01-22 11:47:17 +00006376void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6377 Location out,
6378 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006379 DCHECK(kEmitCompilerReadBarrier);
6380
Roland Levillain44015862016-01-22 11:47:17 +00006381 // Insert a slow path based read barrier *after* the GC root load.
6382 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006383 // Note that GC roots are not affected by heap poisoning, so we do
6384 // not need to do anything special for this here.
6385 SlowPathCodeARM64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006386 new (GetScopedAllocator()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006387 AddSlowPath(slow_path);
6388
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006389 __ B(slow_path->GetEntryLabel());
6390 __ Bind(slow_path->GetExitLabel());
6391}
6392
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006393void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6394 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006395 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006396 locations->SetInAt(0, Location::RequiresRegister());
6397 locations->SetOut(Location::RequiresRegister());
6398}
6399
6400void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6401 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006402 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006403 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006404 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006405 __ Ldr(XRegisterFrom(locations->Out()),
6406 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006407 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006408 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006409 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006410 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6411 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006412 __ Ldr(XRegisterFrom(locations->Out()),
6413 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006414 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006415}
6416
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006417static void PatchJitRootUse(uint8_t* code,
6418 const uint8_t* roots_data,
6419 vixl::aarch64::Literal<uint32_t>* literal,
6420 uint64_t index_in_table) {
6421 uint32_t literal_offset = literal->GetOffset();
6422 uintptr_t address =
6423 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6424 uint8_t* data = code + literal_offset;
6425 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6426}
6427
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006428void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6429 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006430 const StringReference& string_reference = entry.first;
6431 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006432 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006433 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006434 }
6435 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006436 const TypeReference& type_reference = entry.first;
6437 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01006438 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006439 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006440 }
6441}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006442
Artem Serov1a719e42019-07-18 14:24:55 +01006443MemOperand InstructionCodeGeneratorARM64::VecNeonAddress(
6444 HVecMemoryOperation* instruction,
6445 UseScratchRegisterScope* temps_scope,
6446 size_t size,
6447 bool is_string_char_at,
6448 /*out*/ Register* scratch) {
6449 LocationSummary* locations = instruction->GetLocations();
6450 Register base = InputRegisterAt(instruction, 0);
6451
6452 if (instruction->InputAt(1)->IsIntermediateAddressIndex()) {
6453 DCHECK(!is_string_char_at);
6454 return MemOperand(base.X(), InputRegisterAt(instruction, 1).X());
6455 }
6456
6457 Location index = locations->InAt(1);
6458 uint32_t offset = is_string_char_at
6459 ? mirror::String::ValueOffset().Uint32Value()
6460 : mirror::Array::DataOffset(size).Uint32Value();
6461 size_t shift = ComponentSizeShiftWidth(size);
6462
6463 // HIntermediateAddress optimization is only applied for scalar ArrayGet and ArraySet.
6464 DCHECK(!instruction->InputAt(0)->IsIntermediateAddress());
6465
6466 if (index.IsConstant()) {
6467 offset += Int64FromLocation(index) << shift;
6468 return HeapOperand(base, offset);
6469 } else {
6470 *scratch = temps_scope->AcquireSameSizeAs(base);
6471 __ Add(*scratch, base, Operand(WRegisterFrom(index), LSL, shift));
6472 return HeapOperand(*scratch, offset);
6473 }
6474}
6475
Alexandre Rames67555f72014-11-18 10:55:16 +00006476#undef __
6477#undef QUICK_ENTRY_POINT
6478
Vladimir Markoca1e0382018-04-11 09:58:41 +00006479#define __ assembler.GetVIXLAssembler()->
6480
6481static void EmitGrayCheckAndFastPath(arm64::Arm64Assembler& assembler,
6482 vixl::aarch64::Register base_reg,
6483 vixl::aarch64::MemOperand& lock_word,
Vladimir Marko7a695052018-04-12 10:26:50 +01006484 vixl::aarch64::Label* slow_path,
6485 vixl::aarch64::Label* throw_npe = nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006486 // Load the lock word containing the rb_state.
6487 __ Ldr(ip0.W(), lock_word);
6488 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01006489 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Vladimir Markoca1e0382018-04-11 09:58:41 +00006490 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6491 __ Tbnz(ip0.W(), LockWord::kReadBarrierStateShift, slow_path);
6492 static_assert(
6493 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET,
6494 "Field and array LDR offsets must be the same to reuse the same code.");
Vladimir Marko7a695052018-04-12 10:26:50 +01006495 // To throw NPE, we return to the fast path; the artificial dependence below does not matter.
6496 if (throw_npe != nullptr) {
6497 __ Bind(throw_npe);
6498 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006499 // Adjust the return address back to the LDR (1 instruction; 2 for heap poisoning).
6500 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6501 "Field LDR must be 1 instruction (4B) before the return address label; "
6502 " 2 instructions (8B) for heap poisoning.");
6503 __ Add(lr, lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6504 // Introduce a dependency on the lock_word including rb_state,
6505 // to prevent load-load reordering, and without using
6506 // a memory barrier (which would be more expensive).
6507 __ Add(base_reg, base_reg, Operand(ip0, LSR, 32));
6508 __ Br(lr); // And return back to the function.
6509 // Note: The fake dependency is unnecessary for the slow path.
6510}
6511
6512// Load the read barrier introspection entrypoint in register `entrypoint`.
6513static void LoadReadBarrierMarkIntrospectionEntrypoint(arm64::Arm64Assembler& assembler,
6514 vixl::aarch64::Register entrypoint) {
6515 // entrypoint = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6516 DCHECK_EQ(ip0.GetCode(), 16u);
6517 const int32_t entry_point_offset =
6518 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6519 __ Ldr(entrypoint, MemOperand(tr, entry_point_offset));
6520}
6521
6522void CodeGeneratorARM64::CompileBakerReadBarrierThunk(Arm64Assembler& assembler,
6523 uint32_t encoded_data,
6524 /*out*/ std::string* debug_name) {
6525 BakerReadBarrierKind kind = BakerReadBarrierKindField::Decode(encoded_data);
6526 switch (kind) {
Vladimir Marko0ecac682018-08-07 10:40:38 +01006527 case BakerReadBarrierKind::kField:
6528 case BakerReadBarrierKind::kAcquire: {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006529 auto base_reg =
6530 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6531 CheckValidReg(base_reg.GetCode());
6532 auto holder_reg =
6533 Register::GetXRegFromCode(BakerReadBarrierSecondRegField::Decode(encoded_data));
6534 CheckValidReg(holder_reg.GetCode());
6535 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6536 temps.Exclude(ip0, ip1);
Roland Levillain988c3912019-09-25 19:33:35 +01006537 // In the case of a field load (with relaxed semantic), if `base_reg` differs from
6538 // `holder_reg`, the offset was too large and we must have emitted (during the construction
6539 // of the HIR graph, see `art::HInstructionBuilder::BuildInstanceFieldAccess`) and preserved
6540 // (see `art::PrepareForRegisterAllocation::VisitNullCheck`) an explicit null check before
6541 // the load. Otherwise, for implicit null checks, we need to null-check the holder as we do
6542 // not necessarily do that check before going to the thunk.
6543 //
6544 // In the case of a field load with load-acquire semantics (where `base_reg` always differs
6545 // from `holder_reg`), we also need an explicit null check when implicit null checks are
6546 // allowed, as we do not emit one before going to the thunk.
Vladimir Marko7a695052018-04-12 10:26:50 +01006547 vixl::aarch64::Label throw_npe_label;
6548 vixl::aarch64::Label* throw_npe = nullptr;
Roland Levillain988c3912019-09-25 19:33:35 +01006549 if (GetCompilerOptions().GetImplicitNullChecks() &&
6550 (holder_reg.Is(base_reg) || (kind == BakerReadBarrierKind::kAcquire))) {
Vladimir Marko7a695052018-04-12 10:26:50 +01006551 throw_npe = &throw_npe_label;
6552 __ Cbz(holder_reg.W(), throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006553 }
Vladimir Marko7a695052018-04-12 10:26:50 +01006554 // Check if the holder is gray and, if not, add fake dependency to the base register
6555 // and return to the LDR instruction to load the reference. Otherwise, use introspection
6556 // to load the reference and call the entrypoint that performs further checks on the
6557 // reference and marks it if needed.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006558 vixl::aarch64::Label slow_path;
6559 MemOperand lock_word(holder_reg, mirror::Object::MonitorOffset().Int32Value());
Vladimir Marko7a695052018-04-12 10:26:50 +01006560 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path, throw_npe);
Vladimir Markoca1e0382018-04-11 09:58:41 +00006561 __ Bind(&slow_path);
Vladimir Marko0ecac682018-08-07 10:40:38 +01006562 if (kind == BakerReadBarrierKind::kField) {
6563 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET);
6564 __ Ldr(ip0.W(), ldr_address); // Load the LDR (immediate) unsigned offset.
6565 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6566 __ Ubfx(ip0.W(), ip0.W(), 10, 12); // Extract the offset.
6567 __ Ldr(ip0.W(), MemOperand(base_reg, ip0, LSL, 2)); // Load the reference.
6568 } else {
6569 DCHECK(kind == BakerReadBarrierKind::kAcquire);
6570 DCHECK(!base_reg.Is(holder_reg));
6571 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6572 __ Ldar(ip0.W(), MemOperand(base_reg));
6573 }
Vladimir Markoca1e0382018-04-11 09:58:41 +00006574 // Do not unpoison. With heap poisoning enabled, the entrypoint expects a poisoned reference.
6575 __ Br(ip1); // Jump to the entrypoint.
Vladimir Markoca1e0382018-04-11 09:58:41 +00006576 break;
6577 }
6578 case BakerReadBarrierKind::kArray: {
6579 auto base_reg =
6580 Register::GetXRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6581 CheckValidReg(base_reg.GetCode());
6582 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6583 BakerReadBarrierSecondRegField::Decode(encoded_data));
6584 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6585 temps.Exclude(ip0, ip1);
6586 vixl::aarch64::Label slow_path;
6587 int32_t data_offset =
6588 mirror::Array::DataOffset(Primitive::ComponentSize(Primitive::kPrimNot)).Int32Value();
6589 MemOperand lock_word(base_reg, mirror::Object::MonitorOffset().Int32Value() - data_offset);
6590 DCHECK_LT(lock_word.GetOffset(), 0);
6591 EmitGrayCheckAndFastPath(assembler, base_reg, lock_word, &slow_path);
6592 __ Bind(&slow_path);
6593 MemOperand ldr_address(lr, BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
6594 __ Ldr(ip0.W(), ldr_address); // Load the LDR (register) unsigned offset.
6595 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6596 __ Ubfx(ip0, ip0, 16, 6); // Extract the index register, plus 32 (bit 21 is set).
6597 __ Bfi(ip1, ip0, 3, 6); // Insert ip0 to the entrypoint address to create
6598 // a switch case target based on the index register.
6599 __ Mov(ip0, base_reg); // Move the base register to ip0.
6600 __ Br(ip1); // Jump to the entrypoint's array switch case.
6601 break;
6602 }
6603 case BakerReadBarrierKind::kGcRoot: {
6604 // Check if the reference needs to be marked and if so (i.e. not null, not marked yet
6605 // and it does not have a forwarding address), call the correct introspection entrypoint;
6606 // otherwise return the reference (or the extracted forwarding address).
6607 // There is no gray bit check for GC roots.
6608 auto root_reg =
6609 Register::GetWRegFromCode(BakerReadBarrierFirstRegField::Decode(encoded_data));
6610 CheckValidReg(root_reg.GetCode());
6611 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6612 BakerReadBarrierSecondRegField::Decode(encoded_data));
6613 UseScratchRegisterScope temps(assembler.GetVIXLAssembler());
6614 temps.Exclude(ip0, ip1);
6615 vixl::aarch64::Label return_label, not_marked, forwarding_address;
6616 __ Cbz(root_reg, &return_label);
6617 MemOperand lock_word(root_reg.X(), mirror::Object::MonitorOffset().Int32Value());
6618 __ Ldr(ip0.W(), lock_word);
6619 __ Tbz(ip0.W(), LockWord::kMarkBitStateShift, &not_marked);
6620 __ Bind(&return_label);
6621 __ Br(lr);
6622 __ Bind(&not_marked);
6623 __ Tst(ip0.W(), Operand(ip0.W(), LSL, 1));
6624 __ B(&forwarding_address, mi);
6625 LoadReadBarrierMarkIntrospectionEntrypoint(assembler, ip1);
6626 // Adjust the art_quick_read_barrier_mark_introspection address in IP1 to
6627 // art_quick_read_barrier_mark_introspection_gc_roots.
6628 __ Add(ip1, ip1, Operand(BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRYPOINT_OFFSET));
6629 __ Mov(ip0.W(), root_reg);
6630 __ Br(ip1);
6631 __ Bind(&forwarding_address);
6632 __ Lsl(root_reg, ip0.W(), LockWord::kForwardingAddressShift);
6633 __ Br(lr);
6634 break;
6635 }
6636 default:
6637 LOG(FATAL) << "Unexpected kind: " << static_cast<uint32_t>(kind);
6638 UNREACHABLE();
6639 }
6640
Vladimir Marko966b46f2018-08-03 10:20:19 +00006641 // For JIT, the slow path is considered part of the compiled method,
6642 // so JIT should pass null as `debug_name`. Tests may not have a runtime.
6643 DCHECK(Runtime::Current() == nullptr ||
6644 !Runtime::Current()->UseJitCompilation() ||
6645 debug_name == nullptr);
6646 if (debug_name != nullptr && GetCompilerOptions().GenerateAnyDebugInfo()) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00006647 std::ostringstream oss;
6648 oss << "BakerReadBarrierThunk";
6649 switch (kind) {
6650 case BakerReadBarrierKind::kField:
6651 oss << "Field_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6652 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6653 break;
Vladimir Marko0ecac682018-08-07 10:40:38 +01006654 case BakerReadBarrierKind::kAcquire:
6655 oss << "Acquire_r" << BakerReadBarrierFirstRegField::Decode(encoded_data)
6656 << "_r" << BakerReadBarrierSecondRegField::Decode(encoded_data);
6657 break;
Vladimir Markoca1e0382018-04-11 09:58:41 +00006658 case BakerReadBarrierKind::kArray:
6659 oss << "Array_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6660 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6661 BakerReadBarrierSecondRegField::Decode(encoded_data));
6662 break;
6663 case BakerReadBarrierKind::kGcRoot:
6664 oss << "GcRoot_r" << BakerReadBarrierFirstRegField::Decode(encoded_data);
6665 DCHECK_EQ(kBakerReadBarrierInvalidEncodedReg,
6666 BakerReadBarrierSecondRegField::Decode(encoded_data));
6667 break;
6668 }
6669 *debug_name = oss.str();
6670 }
6671}
6672
6673#undef __
6674
Alexandre Rames5319def2014-10-23 10:03:10 +01006675} // namespace arm64
6676} // namespace art