blob: c2cf0de13980419a58834d52d7cc7a01b1cea541 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
Scott Wakeling97c72b72016-06-24 16:19:36 +010036using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000037using vixl::ExactAssemblyScope;
38using vixl::CodeBufferCheckScope;
39using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010040
41#ifdef __
42#error "ARM64 Codegen VIXL macro-assembler macro already defined."
43#endif
44
Alexandre Rames5319def2014-10-23 10:03:10 +010045namespace art {
46
Roland Levillain22ccc3a2015-11-24 13:10:05 +000047template<class MirrorType>
48class GcRoot;
49
Alexandre Rames5319def2014-10-23 10:03:10 +010050namespace arm64 {
51
Alexandre Ramesbe919d92016-08-23 18:33:36 +010052using helpers::ARM64EncodableConstantOrRegister;
53using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080054using helpers::CPURegisterFrom;
55using helpers::DRegisterFrom;
56using helpers::FPRegisterFrom;
57using helpers::HeapOperand;
58using helpers::HeapOperandFrom;
59using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010060using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080061using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080062using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010063using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080064using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010065using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080066using helpers::LocationFrom;
67using helpers::OperandFromMemOperand;
68using helpers::OutputCPURegister;
69using helpers::OutputFPRegister;
70using helpers::OutputRegister;
71using helpers::RegisterFrom;
72using helpers::StackOperandFrom;
73using helpers::VIXLRegCodeFromART;
74using helpers::WRegisterFrom;
75using helpers::XRegisterFrom;
76
Alexandre Rames5319def2014-10-23 10:03:10 +010077static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000078// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080079// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
80// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000081static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010082
Alexandre Rames5319def2014-10-23 10:03:10 +010083inline Condition ARM64Condition(IfCondition cond) {
84 switch (cond) {
85 case kCondEQ: return eq;
86 case kCondNE: return ne;
87 case kCondLT: return lt;
88 case kCondLE: return le;
89 case kCondGT: return gt;
90 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070091 case kCondB: return lo;
92 case kCondBE: return ls;
93 case kCondA: return hi;
94 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010095 }
Roland Levillain7f63c522015-07-13 15:54:55 +000096 LOG(FATAL) << "Unreachable";
97 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010098}
99
Vladimir Markod6e069b2016-01-18 11:11:01 +0000100inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
101 // The ARM64 condition codes can express all the necessary branches, see the
102 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
103 // There is no dex instruction or HIR that would need the missing conditions
104 // "equal or unordered" or "not equal".
105 switch (cond) {
106 case kCondEQ: return eq;
107 case kCondNE: return ne /* unordered */;
108 case kCondLT: return gt_bias ? cc : lt /* unordered */;
109 case kCondLE: return gt_bias ? ls : le /* unordered */;
110 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
111 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
112 default:
113 LOG(FATAL) << "UNREACHABLE";
114 UNREACHABLE();
115 }
116}
117
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000118Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000119 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
120 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
121 // but we use the exact registers for clarity.
122 if (return_type == Primitive::kPrimFloat) {
123 return LocationFrom(s0);
124 } else if (return_type == Primitive::kPrimDouble) {
125 return LocationFrom(d0);
126 } else if (return_type == Primitive::kPrimLong) {
127 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100128 } else if (return_type == Primitive::kPrimVoid) {
129 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000130 } else {
131 return LocationFrom(w0);
132 }
133}
134
Alexandre Rames5319def2014-10-23 10:03:10 +0100135Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000136 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100137}
138
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100139// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
140#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700141#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100142
Zheng Xuda403092015-04-24 17:35:39 +0800143// Calculate memory accessing operand for save/restore live registers.
144static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100145 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800146 int64_t spill_offset,
147 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100148 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
149 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
150 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800151 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100152 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800153 codegen->GetNumberOfFloatingPointRegisters()));
154
Vladimir Marko804b03f2016-09-14 16:26:36 +0100155 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
156 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800157
158 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
159 UseScratchRegisterScope temps(masm);
160
161 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100162 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
163 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800164 int64_t reg_size = kXRegSizeInBytes;
165 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
166 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100167 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800168 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
169 // If the offset does not fit in the instruction's immediate field, use an alternate register
170 // to compute the base address(float point registers spill base address).
171 Register new_base = temps.AcquireSameSizeAs(base);
172 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
173 base = new_base;
174 spill_offset = -core_spill_size;
175 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
176 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
177 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
178 }
179
180 if (is_save) {
181 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
182 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
183 } else {
184 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
185 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
186 }
187}
188
189void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800190 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100191 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
192 for (uint32_t i : LowToHighBits(core_spills)) {
193 // If the register holds an object, update the stack mask.
194 if (locations->RegisterContainsObject(i)) {
195 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800196 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100197 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
198 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
199 saved_core_stack_offsets_[i] = stack_offset;
200 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800201 }
202
Vladimir Marko804b03f2016-09-14 16:26:36 +0100203 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
204 for (uint32_t i : LowToHighBits(fp_spills)) {
205 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
206 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
207 saved_fpu_stack_offsets_[i] = stack_offset;
208 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800209 }
210
Vladimir Marko804b03f2016-09-14 16:26:36 +0100211 SaveRestoreLiveRegistersHelper(codegen,
212 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800213 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
214}
215
216void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100217 SaveRestoreLiveRegistersHelper(codegen,
218 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800219 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
220}
221
Alexandre Rames5319def2014-10-23 10:03:10 +0100222class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
223 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000224 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100225
Alexandre Rames67555f72014-11-18 10:55:16 +0000226 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100227 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000228 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100229
Alexandre Rames5319def2014-10-23 10:03:10 +0100230 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000231 if (instruction_->CanThrowIntoCatchBlock()) {
232 // Live registers will be restored in the catch block if caught.
233 SaveLiveRegisters(codegen, instruction_->GetLocations());
234 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000235 // We're moving two locations to locations that could overlap, so we need a parallel
236 // move resolver.
237 InvokeRuntimeCallingConvention calling_convention;
238 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100239 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
240 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000241 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
242 ? kQuickThrowStringBounds
243 : kQuickThrowArrayBounds;
244 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100245 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800246 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100247 }
248
Alexandre Rames8158f282015-08-07 10:26:17 +0100249 bool IsFatal() const OVERRIDE { return true; }
250
Alexandre Rames9931f312015-06-19 14:47:01 +0100251 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
252
Alexandre Rames5319def2014-10-23 10:03:10 +0100253 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100254 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
255};
256
Alexandre Rames67555f72014-11-18 10:55:16 +0000257class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
258 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000259 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000260
261 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
262 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
263 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000264 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800265 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000266 }
267
Alexandre Rames8158f282015-08-07 10:26:17 +0100268 bool IsFatal() const OVERRIDE { return true; }
269
Alexandre Rames9931f312015-06-19 14:47:01 +0100270 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
271
Alexandre Rames67555f72014-11-18 10:55:16 +0000272 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000273 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
274};
275
276class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
277 public:
278 LoadClassSlowPathARM64(HLoadClass* cls,
279 HInstruction* at,
280 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000281 bool do_clinit,
282 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
283 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
284 : SlowPathCodeARM64(at),
285 cls_(cls),
286 dex_pc_(dex_pc),
287 do_clinit_(do_clinit),
288 bss_entry_temp_(bss_entry_temp),
289 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000290 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
291 }
292
293 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000294 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000295 Location out = locations->Out();
296 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000297 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
298
Vladimir Markoea4c1262017-02-06 19:59:33 +0000299 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the page address of
300 // the entry which is in a scratch register. Make sure it's not used for saving/restoring
301 // registers. Exclude the scratch register also for non-Baker read barrier for simplicity.
302 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
303 bool is_load_class_bss_entry =
304 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
305 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
306 if (is_load_class_bss_entry) {
307 // This temp is a scratch register.
308 DCHECK(bss_entry_temp_.IsValid());
309 temps.Exclude(bss_entry_temp_);
310 }
311
Alexandre Rames67555f72014-11-18 10:55:16 +0000312 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000313 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000314
315 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000316 dex::TypeIndex type_index = cls_->GetTypeIndex();
317 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000318 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
319 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000320 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800321 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100322 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800323 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100324 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800325 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000326
327 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000328 if (out.IsValid()) {
329 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000330 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000331 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000332 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000333 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000334 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000335 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000336 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000337 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000338 if (call_saves_everything_except_r0_ip0) {
339 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
340 } else {
341 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
342 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
343 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
344 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000345 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000346 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000347 {
348 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
349 __ Bind(strp_label);
350 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000351 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000352 }
353 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 __ B(GetExitLabel());
355 }
356
Alexandre Rames9931f312015-06-19 14:47:01 +0100357 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
358
Alexandre Rames67555f72014-11-18 10:55:16 +0000359 private:
360 // The class this slow path will load.
361 HLoadClass* const cls_;
362
Alexandre Rames67555f72014-11-18 10:55:16 +0000363 // The dex PC of `at_`.
364 const uint32_t dex_pc_;
365
366 // Whether to initialize the class.
367 const bool do_clinit_;
368
Vladimir Markoea4c1262017-02-06 19:59:33 +0000369 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
370 vixl::aarch64::Register bss_entry_temp_;
371 vixl::aarch64::Label* bss_entry_adrp_label_;
372
Alexandre Rames67555f72014-11-18 10:55:16 +0000373 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
374};
375
Vladimir Markoaad75c62016-10-03 08:46:48 +0000376class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
377 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100378 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
379 : SlowPathCodeARM64(instruction),
380 temp_(temp),
381 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000382
383 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
384 LocationSummary* locations = instruction_->GetLocations();
385 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
386 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
387
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100388 // temp_ is a scratch register. Make sure it's not used for saving/restoring registers.
389 UseScratchRegisterScope temps(arm64_codegen->GetVIXLAssembler());
390 temps.Exclude(temp_);
391
Vladimir Markoaad75c62016-10-03 08:46:48 +0000392 __ Bind(GetEntryLabel());
393 SaveLiveRegisters(codegen, locations);
394
395 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000396 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
397 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000398 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
399 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
400 Primitive::Type type = instruction_->GetType();
401 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
402
403 RestoreLiveRegisters(codegen, locations);
404
405 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000406 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100407 if (!kUseReadBarrier || kUseBakerReadBarrier) {
408 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
409 } else {
410 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
411 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
412 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
413 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000414 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100415 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000416 {
417 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
418 __ Bind(strp_label);
419 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100420 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000421 }
422
423 __ B(GetExitLabel());
424 }
425
426 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
427
428 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100429 const Register temp_;
430 vixl::aarch64::Label* adrp_label_;
431
Vladimir Markoaad75c62016-10-03 08:46:48 +0000432 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
433};
434
Alexandre Rames5319def2014-10-23 10:03:10 +0100435class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
436 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000437 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100438
Alexandre Rames67555f72014-11-18 10:55:16 +0000439 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
440 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100441 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000442 if (instruction_->CanThrowIntoCatchBlock()) {
443 // Live registers will be restored in the catch block if caught.
444 SaveLiveRegisters(codegen, instruction_->GetLocations());
445 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000446 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
447 instruction_,
448 instruction_->GetDexPc(),
449 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800450 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100451 }
452
Alexandre Rames8158f282015-08-07 10:26:17 +0100453 bool IsFatal() const OVERRIDE { return true; }
454
Alexandre Rames9931f312015-06-19 14:47:01 +0100455 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
456
Alexandre Rames5319def2014-10-23 10:03:10 +0100457 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100458 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
459};
460
461class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
462 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100463 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000464 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100465
Alexandre Rames67555f72014-11-18 10:55:16 +0000466 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
467 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100468 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000469 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800470 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000471 if (successor_ == nullptr) {
472 __ B(GetReturnLabel());
473 } else {
474 __ B(arm64_codegen->GetLabelOf(successor_));
475 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100476 }
477
Scott Wakeling97c72b72016-06-24 16:19:36 +0100478 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100479 DCHECK(successor_ == nullptr);
480 return &return_label_;
481 }
482
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100483 HBasicBlock* GetSuccessor() const {
484 return successor_;
485 }
486
Alexandre Rames9931f312015-06-19 14:47:01 +0100487 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
488
Alexandre Rames5319def2014-10-23 10:03:10 +0100489 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100490 // If not null, the block to branch to after the suspend check.
491 HBasicBlock* const successor_;
492
493 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100494 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100495
496 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
497};
498
Alexandre Rames67555f72014-11-18 10:55:16 +0000499class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
500 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000501 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000502 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000503
504 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000505 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800506
Alexandre Rames3e69f162014-12-10 10:36:50 +0000507 DCHECK(instruction_->IsCheckCast()
508 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
509 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100510 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000511
Alexandre Rames67555f72014-11-18 10:55:16 +0000512 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000513
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000514 if (!is_fatal_) {
515 SaveLiveRegisters(codegen, locations);
516 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000517
518 // We're moving two locations to locations that could overlap, so we need a parallel
519 // move resolver.
520 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800521 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800522 LocationFrom(calling_convention.GetRegisterAt(0)),
523 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800524 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800525 LocationFrom(calling_convention.GetRegisterAt(1)),
526 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000527 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000528 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800529 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000530 Primitive::Type ret_type = instruction_->GetType();
531 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
532 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
533 } else {
534 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800535 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
536 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000537 }
538
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000539 if (!is_fatal_) {
540 RestoreLiveRegisters(codegen, locations);
541 __ B(GetExitLabel());
542 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000543 }
544
Alexandre Rames9931f312015-06-19 14:47:01 +0100545 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100546 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100547
Alexandre Rames67555f72014-11-18 10:55:16 +0000548 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000549 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000550
Alexandre Rames67555f72014-11-18 10:55:16 +0000551 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
552};
553
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700554class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
555 public:
Aart Bik42249c32016-01-07 15:33:50 -0800556 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000557 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700558
559 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800560 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700561 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000562 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000563 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700564 }
565
Alexandre Rames9931f312015-06-19 14:47:01 +0100566 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
567
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700568 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700569 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
570};
571
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100572class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
573 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000574 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100575
576 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
577 LocationSummary* locations = instruction_->GetLocations();
578 __ Bind(GetEntryLabel());
579 SaveLiveRegisters(codegen, locations);
580
581 InvokeRuntimeCallingConvention calling_convention;
582 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
583 parallel_move.AddMove(
584 locations->InAt(0),
585 LocationFrom(calling_convention.GetRegisterAt(0)),
586 Primitive::kPrimNot,
587 nullptr);
588 parallel_move.AddMove(
589 locations->InAt(1),
590 LocationFrom(calling_convention.GetRegisterAt(1)),
591 Primitive::kPrimInt,
592 nullptr);
593 parallel_move.AddMove(
594 locations->InAt(2),
595 LocationFrom(calling_convention.GetRegisterAt(2)),
596 Primitive::kPrimNot,
597 nullptr);
598 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
599
600 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000601 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100602 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
603 RestoreLiveRegisters(codegen, locations);
604 __ B(GetExitLabel());
605 }
606
607 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
608
609 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100610 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
611};
612
Zheng Xu3927c8b2015-11-18 17:46:25 +0800613void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
614 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000615 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800616
617 // We are about to use the assembler to place literals directly. Make sure we have enough
618 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000619 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
620 num_entries * sizeof(int32_t),
621 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800622
623 __ Bind(&table_start_);
624 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
625 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100626 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800627 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100628 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800629 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
630 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
631 Literal<int32_t> literal(jump_offset);
632 __ place(&literal);
633 }
634}
635
Roland Levillain47b3ab22017-02-27 14:31:35 +0000636// Slow path marking an object reference `ref` during a read
637// barrier. The field `obj.field` in the object `obj` holding this
638// reference does not get updated by this slow path after marking (see
639// ReadBarrierMarkAndUpdateFieldSlowPathARM64 below for that).
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000640//
Roland Levillain47b3ab22017-02-27 14:31:35 +0000641// This means that after the execution of this slow path, `ref` will
642// always be up-to-date, but `obj.field` may not; i.e., after the
643// flip, `ref` will be a to-space reference, but `obj.field` will
644// probably still be a from-space reference (unless it gets updated by
645// another thread, or if another thread installed another object
646// reference (different from `ref`) in `obj.field`).
647//
648// If `entrypoint` is a valid location it is assumed to already be
649// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000650// is when the decision to mark is based on whether the GC is marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000651class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
652 public:
653 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
654 Location ref,
655 Location entrypoint = Location::NoLocation())
656 : SlowPathCodeARM64(instruction),
657 ref_(ref),
658 entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000659 DCHECK(kEmitCompilerReadBarrier);
660 }
661
Roland Levillain47b3ab22017-02-27 14:31:35 +0000662 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000663
Roland Levillain47b3ab22017-02-27 14:31:35 +0000664 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
665 LocationSummary* locations = instruction_->GetLocations();
666 DCHECK(locations->CanCall());
667 DCHECK(ref_.IsRegister()) << ref_;
668 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
669 DCHECK(instruction_->IsInstanceFieldGet() ||
670 instruction_->IsStaticFieldGet() ||
671 instruction_->IsArrayGet() ||
672 instruction_->IsArraySet() ||
673 instruction_->IsLoadClass() ||
674 instruction_->IsLoadString() ||
675 instruction_->IsInstanceOf() ||
676 instruction_->IsCheckCast() ||
677 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
678 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
679 << "Unexpected instruction in read barrier marking slow path: "
680 << instruction_->DebugName();
681 // The read barrier instrumentation of object ArrayGet
682 // instructions does not support the HIntermediateAddress
683 // instruction.
684 DCHECK(!(instruction_->IsArrayGet() &&
685 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
686
687 __ Bind(GetEntryLabel());
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000688 // No need to save live registers; it's taken care of by the
689 // entrypoint. Also, there is no need to update the stack mask,
690 // as this runtime call will not trigger a garbage collection.
691 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
692 DCHECK_NE(ref_.reg(), LR);
693 DCHECK_NE(ref_.reg(), WSP);
694 DCHECK_NE(ref_.reg(), WZR);
695 // IP0 is used internally by the ReadBarrierMarkRegX entry point
696 // as a temporary, it cannot be the entry point's input/output.
697 DCHECK_NE(ref_.reg(), IP0);
698 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
699 // "Compact" slow path, saving two moves.
700 //
701 // Instead of using the standard runtime calling convention (input
702 // and output in W0):
703 //
704 // W0 <- ref
705 // W0 <- ReadBarrierMark(W0)
706 // ref <- W0
707 //
708 // we just use rX (the register containing `ref`) as input and output
709 // of a dedicated entrypoint:
710 //
711 // rX <- ReadBarrierMarkRegX(rX)
712 //
713 if (entrypoint_.IsValid()) {
714 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
715 __ Blr(XRegisterFrom(entrypoint_));
716 } else {
717 // Entrypoint is not already loaded, load from the thread.
718 int32_t entry_point_offset =
719 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
720 // This runtime call does not require a stack map.
721 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
722 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000723 __ B(GetExitLabel());
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000724 }
725
Roland Levillain47b3ab22017-02-27 14:31:35 +0000726 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000727 // The location (register) of the marked object reference.
728 const Location ref_;
729
730 // The location of the entrypoint if it is already loaded.
731 const Location entrypoint_;
732
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000733 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
734};
735
Roland Levillain47b3ab22017-02-27 14:31:35 +0000736// Slow path marking an object reference `ref` during a read barrier,
737// and if needed, atomically updating the field `obj.field` in the
738// object `obj` holding this reference after marking (contrary to
739// ReadBarrierMarkSlowPathARM64 above, which never tries to update
740// `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100741//
742// This means that after the execution of this slow path, both `ref`
743// and `obj.field` will be up-to-date; i.e., after the flip, both will
744// hold the same to-space reference (unless another thread installed
745// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000746//
747// If `entrypoint` is a valid location it is assumed to already be
748// holding the entrypoint. The case where the entrypoint is passed in
749// is when the decision to mark is based on whether the GC is marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000750class ReadBarrierMarkAndUpdateFieldSlowPathARM64 : public SlowPathCodeARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100751 public:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000752 ReadBarrierMarkAndUpdateFieldSlowPathARM64(HInstruction* instruction,
753 Location ref,
754 Register obj,
755 Location field_offset,
Roland Levillainba650a42017-03-06 13:52:32 +0000756 Register temp,
757 Location entrypoint = Location::NoLocation())
Roland Levillain47b3ab22017-02-27 14:31:35 +0000758 : SlowPathCodeARM64(instruction),
759 ref_(ref),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100760 obj_(obj),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000761 field_offset_(field_offset),
Roland Levillainba650a42017-03-06 13:52:32 +0000762 temp_(temp),
763 entrypoint_(entrypoint) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100764 DCHECK(kEmitCompilerReadBarrier);
765 }
766
767 const char* GetDescription() const OVERRIDE {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000768 return "ReadBarrierMarkAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100769 }
770
771 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
772 LocationSummary* locations = instruction_->GetLocations();
773 Register ref_reg = WRegisterFrom(ref_);
774 DCHECK(locations->CanCall());
775 DCHECK(ref_.IsRegister()) << ref_;
776 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain47b3ab22017-02-27 14:31:35 +0000777 // This slow path is only used by the UnsafeCASObject intrinsic.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100778 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
779 << "Unexpected instruction in read barrier marking and field updating slow path: "
780 << instruction_->DebugName();
781 DCHECK(instruction_->GetLocations()->Intrinsified());
782 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000783 DCHECK(field_offset_.IsRegister()) << field_offset_;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100784
785 __ Bind(GetEntryLabel());
786
Roland Levillain47b3ab22017-02-27 14:31:35 +0000787 // Save the old reference.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100788 // Note that we cannot use IP to save the old reference, as IP is
789 // used internally by the ReadBarrierMarkRegX entry point, and we
790 // need the old reference after the call to that entry point.
791 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
792 __ Mov(temp_.W(), ref_reg);
793
Roland Levillain47b3ab22017-02-27 14:31:35 +0000794 // No need to save live registers; it's taken care of by the
795 // entrypoint. Also, there is no need to update the stack mask,
796 // as this runtime call will not trigger a garbage collection.
797 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
798 DCHECK_NE(ref_.reg(), LR);
799 DCHECK_NE(ref_.reg(), WSP);
800 DCHECK_NE(ref_.reg(), WZR);
801 // IP0 is used internally by the ReadBarrierMarkRegX entry point
802 // as a temporary, it cannot be the entry point's input/output.
803 DCHECK_NE(ref_.reg(), IP0);
804 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
805 // "Compact" slow path, saving two moves.
806 //
807 // Instead of using the standard runtime calling convention (input
808 // and output in W0):
809 //
810 // W0 <- ref
811 // W0 <- ReadBarrierMark(W0)
812 // ref <- W0
813 //
814 // we just use rX (the register containing `ref`) as input and output
815 // of a dedicated entrypoint:
816 //
817 // rX <- ReadBarrierMarkRegX(rX)
818 //
Roland Levillainba650a42017-03-06 13:52:32 +0000819 if (entrypoint_.IsValid()) {
820 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
821 __ Blr(XRegisterFrom(entrypoint_));
822 } else {
823 // Entrypoint is not already loaded, load from the thread.
824 int32_t entry_point_offset =
825 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
826 // This runtime call does not require a stack map.
827 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
828 }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100829
830 // If the new reference is different from the old reference,
Roland Levillain47b3ab22017-02-27 14:31:35 +0000831 // update the field in the holder (`*(obj_ + field_offset_)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100832 //
833 // Note that this field could also hold a different object, if
834 // another thread had concurrently changed it. In that case, the
835 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
836 // (CAS) operation below would abort the CAS, leaving the field
837 // as-is.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000838 vixl::aarch64::Label done;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100839 __ Cmp(temp_.W(), ref_reg);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000840 __ B(eq, &done);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100841
842 // Update the the holder's field atomically. This may fail if
843 // mutator updates before us, but it's OK. This is achieved
844 // using a strong compare-and-set (CAS) operation with relaxed
845 // memory synchronization ordering, where the expected value is
846 // the old reference and the desired value is the new reference.
847
848 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
849 UseScratchRegisterScope temps(masm);
850
851 // Convenience aliases.
852 Register base = obj_.W();
Roland Levillain47b3ab22017-02-27 14:31:35 +0000853 Register offset = XRegisterFrom(field_offset_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100854 Register expected = temp_.W();
855 Register value = ref_reg;
856 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
857 Register tmp_value = temps.AcquireW(); // Value in memory.
858
859 __ Add(tmp_ptr, base.X(), Operand(offset));
860
861 if (kPoisonHeapReferences) {
862 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
863 if (value.Is(expected)) {
864 // Do not poison `value`, as it is the same register as
865 // `expected`, which has just been poisoned.
866 } else {
867 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
868 }
869 }
870
871 // do {
872 // tmp_value = [tmp_ptr] - expected;
873 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
874
Roland Levillain24a4d112016-10-26 13:10:46 +0100875 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100876 __ Bind(&loop_head);
877 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
878 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +0100879 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100880 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
881 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +0100882 __ B(&exit_loop);
883 __ Bind(&comparison_failed);
884 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100885 __ Bind(&exit_loop);
886
887 if (kPoisonHeapReferences) {
888 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
889 if (value.Is(expected)) {
890 // Do not unpoison `value`, as it is the same register as
891 // `expected`, which has just been unpoisoned.
892 } else {
893 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
894 }
895 }
896
Roland Levillain47b3ab22017-02-27 14:31:35 +0000897 __ Bind(&done);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100898 __ B(GetExitLabel());
899 }
900
901 private:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000902 // The location (register) of the marked object reference.
903 const Location ref_;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100904 // The register containing the object holding the marked object reference field.
905 const Register obj_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000906 // The location of the offset of the marked reference field within `obj_`.
907 Location field_offset_;
908
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100909 const Register temp_;
910
Roland Levillainba650a42017-03-06 13:52:32 +0000911 // The location of the entrypoint if it is already loaded.
912 const Location entrypoint_;
913
Roland Levillain47b3ab22017-02-27 14:31:35 +0000914 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100915};
916
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000917// Slow path generating a read barrier for a heap reference.
918class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
919 public:
920 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
921 Location out,
922 Location ref,
923 Location obj,
924 uint32_t offset,
925 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000926 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000927 out_(out),
928 ref_(ref),
929 obj_(obj),
930 offset_(offset),
931 index_(index) {
932 DCHECK(kEmitCompilerReadBarrier);
933 // If `obj` is equal to `out` or `ref`, it means the initial object
934 // has been overwritten by (or after) the heap object reference load
935 // to be instrumented, e.g.:
936 //
937 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000938 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000939 //
940 // In that case, we have lost the information about the original
941 // object, and the emitted read barrier cannot work properly.
942 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
943 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
944 }
945
946 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
947 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
948 LocationSummary* locations = instruction_->GetLocations();
949 Primitive::Type type = Primitive::kPrimNot;
950 DCHECK(locations->CanCall());
951 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100952 DCHECK(instruction_->IsInstanceFieldGet() ||
953 instruction_->IsStaticFieldGet() ||
954 instruction_->IsArrayGet() ||
955 instruction_->IsInstanceOf() ||
956 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100957 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000958 << "Unexpected instruction in read barrier for heap reference slow path: "
959 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000960 // The read barrier instrumentation of object ArrayGet
961 // instructions does not support the HIntermediateAddress
962 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000963 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100964 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000965
966 __ Bind(GetEntryLabel());
967
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000968 SaveLiveRegisters(codegen, locations);
969
970 // We may have to change the index's value, but as `index_` is a
971 // constant member (like other "inputs" of this slow path),
972 // introduce a copy of it, `index`.
973 Location index = index_;
974 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100975 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000976 if (instruction_->IsArrayGet()) {
977 // Compute the actual memory offset and store it in `index`.
978 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
979 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
980 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
981 // We are about to change the value of `index_reg` (see the
982 // calls to vixl::MacroAssembler::Lsl and
983 // vixl::MacroAssembler::Mov below), but it has
984 // not been saved by the previous call to
985 // art::SlowPathCode::SaveLiveRegisters, as it is a
986 // callee-save register --
987 // art::SlowPathCode::SaveLiveRegisters does not consider
988 // callee-save registers, as it has been designed with the
989 // assumption that callee-save registers are supposed to be
990 // handled by the called function. So, as a callee-save
991 // register, `index_reg` _would_ eventually be saved onto
992 // the stack, but it would be too late: we would have
993 // changed its value earlier. Therefore, we manually save
994 // it here into another freely available register,
995 // `free_reg`, chosen of course among the caller-save
996 // registers (as a callee-save `free_reg` register would
997 // exhibit the same problem).
998 //
999 // Note we could have requested a temporary register from
1000 // the register allocator instead; but we prefer not to, as
1001 // this is a slow path, and we know we can find a
1002 // caller-save register that is available.
1003 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1004 __ Mov(free_reg.W(), index_reg);
1005 index_reg = free_reg;
1006 index = LocationFrom(index_reg);
1007 } else {
1008 // The initial register stored in `index_` has already been
1009 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1010 // (as it is not a callee-save register), so we can freely
1011 // use it.
1012 }
1013 // Shifting the index value contained in `index_reg` by the scale
1014 // factor (2) cannot overflow in practice, as the runtime is
1015 // unable to allocate object arrays with a size larger than
1016 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1017 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1018 static_assert(
1019 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1020 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1021 __ Add(index_reg, index_reg, Operand(offset_));
1022 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001023 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1024 // intrinsics, `index_` is not shifted by a scale factor of 2
1025 // (as in the case of ArrayGet), as it is actually an offset
1026 // to an object field within an object.
1027 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001028 DCHECK(instruction_->GetLocations()->Intrinsified());
1029 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1030 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1031 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001032 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001033 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001034 }
1035 }
1036
1037 // We're moving two or three locations to locations that could
1038 // overlap, so we need a parallel move resolver.
1039 InvokeRuntimeCallingConvention calling_convention;
1040 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1041 parallel_move.AddMove(ref_,
1042 LocationFrom(calling_convention.GetRegisterAt(0)),
1043 type,
1044 nullptr);
1045 parallel_move.AddMove(obj_,
1046 LocationFrom(calling_convention.GetRegisterAt(1)),
1047 type,
1048 nullptr);
1049 if (index.IsValid()) {
1050 parallel_move.AddMove(index,
1051 LocationFrom(calling_convention.GetRegisterAt(2)),
1052 Primitive::kPrimInt,
1053 nullptr);
1054 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1055 } else {
1056 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1057 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1058 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001059 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001060 instruction_,
1061 instruction_->GetDexPc(),
1062 this);
1063 CheckEntrypointTypes<
1064 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1065 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1066
1067 RestoreLiveRegisters(codegen, locations);
1068
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001069 __ B(GetExitLabel());
1070 }
1071
1072 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1073
1074 private:
1075 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001076 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1077 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001078 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1079 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1080 return Register(VIXLRegCodeFromART(i), kXRegSize);
1081 }
1082 }
1083 // We shall never fail to find a free caller-save register, as
1084 // there are more than two core caller-save registers on ARM64
1085 // (meaning it is possible to find one which is different from
1086 // `ref` and `obj`).
1087 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1088 LOG(FATAL) << "Could not find a free register";
1089 UNREACHABLE();
1090 }
1091
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001092 const Location out_;
1093 const Location ref_;
1094 const Location obj_;
1095 const uint32_t offset_;
1096 // An additional location containing an index to an array.
1097 // Only used for HArrayGet and the UnsafeGetObject &
1098 // UnsafeGetObjectVolatile intrinsics.
1099 const Location index_;
1100
1101 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1102};
1103
1104// Slow path generating a read barrier for a GC root.
1105class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1106 public:
1107 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001108 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001109 DCHECK(kEmitCompilerReadBarrier);
1110 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001111
1112 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1113 LocationSummary* locations = instruction_->GetLocations();
1114 Primitive::Type type = Primitive::kPrimNot;
1115 DCHECK(locations->CanCall());
1116 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001117 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1118 << "Unexpected instruction in read barrier for GC root slow path: "
1119 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001120
1121 __ Bind(GetEntryLabel());
1122 SaveLiveRegisters(codegen, locations);
1123
1124 InvokeRuntimeCallingConvention calling_convention;
1125 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1126 // The argument of the ReadBarrierForRootSlow is not a managed
1127 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1128 // thus we need a 64-bit move here, and we cannot use
1129 //
1130 // arm64_codegen->MoveLocation(
1131 // LocationFrom(calling_convention.GetRegisterAt(0)),
1132 // root_,
1133 // type);
1134 //
1135 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1136 // reference type (`Primitive::kPrimNot`).
1137 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001138 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001139 instruction_,
1140 instruction_->GetDexPc(),
1141 this);
1142 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1143 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1144
1145 RestoreLiveRegisters(codegen, locations);
1146 __ B(GetExitLabel());
1147 }
1148
1149 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1150
1151 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001152 const Location out_;
1153 const Location root_;
1154
1155 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1156};
1157
Alexandre Rames5319def2014-10-23 10:03:10 +01001158#undef __
1159
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001160Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001161 Location next_location;
1162 if (type == Primitive::kPrimVoid) {
1163 LOG(FATAL) << "Unreachable type " << type;
1164 }
1165
Alexandre Rames542361f2015-01-29 16:57:31 +00001166 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001167 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
1168 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +00001169 } else if (!Primitive::IsFloatingPointType(type) &&
1170 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001171 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1172 } else {
1173 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001174 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1175 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001176 }
1177
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001178 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001179 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001180 return next_location;
1181}
1182
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001183Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001184 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001185}
1186
Serban Constantinescu579885a2015-02-22 20:51:33 +00001187CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1188 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001189 const CompilerOptions& compiler_options,
1190 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001191 : CodeGenerator(graph,
1192 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001193 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001194 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001195 callee_saved_core_registers.GetList(),
1196 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001197 compiler_options,
1198 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001199 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001200 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001201 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001202 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001203 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001204 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001205 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001206 uint32_literals_(std::less<uint32_t>(),
1207 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001208 uint64_literals_(std::less<uint64_t>(),
1209 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001210 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1211 boot_image_string_patches_(StringReferenceValueComparator(),
1212 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1213 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001214 boot_image_type_patches_(TypeReferenceValueComparator(),
1215 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1216 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001217 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001218 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001219 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1220 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001221 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1222 jit_class_patches_(TypeReferenceValueComparator(),
1223 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001224 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001225 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001226}
Alexandre Rames5319def2014-10-23 10:03:10 +01001227
Alexandre Rames67555f72014-11-18 10:55:16 +00001228#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001229
Zheng Xu3927c8b2015-11-18 17:46:25 +08001230void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001231 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001232 jump_table->EmitTable(this);
1233 }
1234}
1235
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001236void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001237 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001238 // Ensure we emit the literal pool.
1239 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001240
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001241 CodeGenerator::Finalize(allocator);
1242}
1243
Zheng Xuad4450e2015-04-17 18:48:56 +08001244void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1245 // Note: There are 6 kinds of moves:
1246 // 1. constant -> GPR/FPR (non-cycle)
1247 // 2. constant -> stack (non-cycle)
1248 // 3. GPR/FPR -> GPR/FPR
1249 // 4. GPR/FPR -> stack
1250 // 5. stack -> GPR/FPR
1251 // 6. stack -> stack (non-cycle)
1252 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1253 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1254 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1255 // dependency.
1256 vixl_temps_.Open(GetVIXLAssembler());
1257}
1258
1259void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1260 vixl_temps_.Close();
1261}
1262
1263Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1264 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1265 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1266 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1267 Location scratch = GetScratchLocation(kind);
1268 if (!scratch.Equals(Location::NoLocation())) {
1269 return scratch;
1270 }
1271 // Allocate from VIXL temp registers.
1272 if (kind == Location::kRegister) {
1273 scratch = LocationFrom(vixl_temps_.AcquireX());
1274 } else {
1275 DCHECK(kind == Location::kFpuRegister);
1276 scratch = LocationFrom(vixl_temps_.AcquireD());
1277 }
1278 AddScratchLocation(scratch);
1279 return scratch;
1280}
1281
1282void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1283 if (loc.IsRegister()) {
1284 vixl_temps_.Release(XRegisterFrom(loc));
1285 } else {
1286 DCHECK(loc.IsFpuRegister());
1287 vixl_temps_.Release(DRegisterFrom(loc));
1288 }
1289 RemoveScratchLocation(loc);
1290}
1291
Alexandre Rames3e69f162014-12-10 10:36:50 +00001292void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001293 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001294 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001295}
1296
Alexandre Rames5319def2014-10-23 10:03:10 +01001297void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001298 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001299 __ Bind(&frame_entry_label_);
1300
Serban Constantinescu02164b32014-11-13 14:05:07 +00001301 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1302 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001303 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001304 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001305 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001306 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001307 {
1308 // Ensure that between load and RecordPcInfo there are no pools emitted.
1309 ExactAssemblyScope eas(GetVIXLAssembler(),
1310 kInstructionSize,
1311 CodeBufferCheckScope::kExactSize);
1312 __ ldr(wzr, MemOperand(temp, 0));
1313 RecordPcInfo(nullptr, 0);
1314 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001315 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001316
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001317 if (!HasEmptyFrame()) {
1318 int frame_size = GetFrameSize();
1319 // Stack layout:
1320 // sp[frame_size - 8] : lr.
1321 // ... : other preserved core registers.
1322 // ... : other preserved fp registers.
1323 // ... : reserved frame space.
1324 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001325
1326 // Save the current method if we need it. Note that we do not
1327 // do this in HCurrentMethod, as the instruction might have been removed
1328 // in the SSA graph.
1329 if (RequiresCurrentMethod()) {
1330 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001331 } else {
1332 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001333 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001334 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001335 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1336 frame_size - GetCoreSpillSize());
1337 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1338 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001339
1340 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1341 // Initialize should_deoptimize flag to 0.
1342 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1343 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1344 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001345 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001346}
1347
1348void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001349 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001350 if (!HasEmptyFrame()) {
1351 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001352 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1353 frame_size - FrameEntrySpillSize());
1354 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1355 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001356 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001357 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001358 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001359 __ Ret();
1360 GetAssembler()->cfi().RestoreState();
1361 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001362}
1363
Scott Wakeling97c72b72016-06-24 16:19:36 +01001364CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001365 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001366 return CPURegList(CPURegister::kRegister, kXRegSize,
1367 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001368}
1369
Scott Wakeling97c72b72016-06-24 16:19:36 +01001370CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001371 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1372 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001373 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1374 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001375}
1376
Alexandre Rames5319def2014-10-23 10:03:10 +01001377void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1378 __ Bind(GetLabelOf(block));
1379}
1380
Calin Juravle175dc732015-08-25 15:42:32 +01001381void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1382 DCHECK(location.IsRegister());
1383 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1384}
1385
Calin Juravlee460d1d2015-09-29 04:52:17 +01001386void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1387 if (location.IsRegister()) {
1388 locations->AddTemp(location);
1389 } else {
1390 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1391 }
1392}
1393
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001394void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001395 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001396 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001397 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001398 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001399 if (value_can_be_null) {
1400 __ Cbz(value, &done);
1401 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001402 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001403 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001404 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001405 if (value_can_be_null) {
1406 __ Bind(&done);
1407 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001408}
1409
David Brazdil58282f42016-01-14 12:45:10 +00001410void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001411 // Blocked core registers:
1412 // lr : Runtime reserved.
1413 // tr : Runtime reserved.
1414 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1415 // ip1 : VIXL core temp.
1416 // ip0 : VIXL core temp.
1417 //
1418 // Blocked fp registers:
1419 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001420 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1421 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001422 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001423 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001424 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001425
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001426 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001427 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001428 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001429 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001430
David Brazdil58282f42016-01-14 12:45:10 +00001431 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001432 // Stubs do not save callee-save floating point registers. If the graph
1433 // is debuggable, we need to deal with these registers differently. For
1434 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001435 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1436 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001437 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001438 }
1439 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001440}
1441
Alexandre Rames3e69f162014-12-10 10:36:50 +00001442size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1443 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1444 __ Str(reg, MemOperand(sp, stack_index));
1445 return kArm64WordSize;
1446}
1447
1448size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1449 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1450 __ Ldr(reg, MemOperand(sp, stack_index));
1451 return kArm64WordSize;
1452}
1453
1454size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1455 FPRegister reg = FPRegister(reg_id, kDRegSize);
1456 __ Str(reg, MemOperand(sp, stack_index));
1457 return kArm64WordSize;
1458}
1459
1460size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1461 FPRegister reg = FPRegister(reg_id, kDRegSize);
1462 __ Ldr(reg, MemOperand(sp, stack_index));
1463 return kArm64WordSize;
1464}
1465
Alexandre Rames5319def2014-10-23 10:03:10 +01001466void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001467 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001468}
1469
1470void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001471 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001472}
1473
Alexandre Rames67555f72014-11-18 10:55:16 +00001474void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001475 if (constant->IsIntConstant()) {
1476 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1477 } else if (constant->IsLongConstant()) {
1478 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1479 } else if (constant->IsNullConstant()) {
1480 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001481 } else if (constant->IsFloatConstant()) {
1482 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1483 } else {
1484 DCHECK(constant->IsDoubleConstant());
1485 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1486 }
1487}
1488
Alexandre Rames3e69f162014-12-10 10:36:50 +00001489
1490static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1491 DCHECK(constant.IsConstant());
1492 HConstant* cst = constant.GetConstant();
1493 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001494 // Null is mapped to a core W register, which we associate with kPrimInt.
1495 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001496 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1497 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1498 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1499}
1500
Roland Levillain558dea12017-01-27 19:40:44 +00001501// Allocate a scratch register from the VIXL pool, querying first into
1502// the floating-point register pool, and then the the core register
1503// pool. This is essentially a reimplementation of
1504// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1505// using a different allocation strategy.
1506static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1507 vixl::aarch64::UseScratchRegisterScope* temps,
1508 int size_in_bits) {
1509 return masm->GetScratchFPRegisterList()->IsEmpty()
1510 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1511 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1512}
1513
Calin Juravlee460d1d2015-09-29 04:52:17 +01001514void CodeGeneratorARM64::MoveLocation(Location destination,
1515 Location source,
1516 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001517 if (source.Equals(destination)) {
1518 return;
1519 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001520
1521 // A valid move can always be inferred from the destination and source
1522 // locations. When moving from and to a register, the argument type can be
1523 // used to generate 32bit instead of 64bit moves. In debug mode we also
1524 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001525 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001526
1527 if (destination.IsRegister() || destination.IsFpuRegister()) {
1528 if (unspecified_type) {
1529 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1530 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001531 (src_cst != nullptr && (src_cst->IsIntConstant()
1532 || src_cst->IsFloatConstant()
1533 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001534 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001535 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001536 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001537 // If the source is a double stack slot or a 64bit constant, a 64bit
1538 // type is appropriate. Else the source is a register, and since the
1539 // type has not been specified, we chose a 64bit type to force a 64bit
1540 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001541 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001542 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001543 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001544 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1545 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1546 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001547 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1548 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1549 __ Ldr(dst, StackOperandFrom(source));
1550 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001551 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001552 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001553 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001554 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001555 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001556 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001557 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001558 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1559 ? Primitive::kPrimLong
1560 : Primitive::kPrimInt;
1561 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1562 }
1563 } else {
1564 DCHECK(source.IsFpuRegister());
1565 if (destination.IsRegister()) {
1566 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1567 ? Primitive::kPrimDouble
1568 : Primitive::kPrimFloat;
1569 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1570 } else {
1571 DCHECK(destination.IsFpuRegister());
1572 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001573 }
1574 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001575 } else { // The destination is not a register. It must be a stack slot.
1576 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1577 if (source.IsRegister() || source.IsFpuRegister()) {
1578 if (unspecified_type) {
1579 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001580 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001581 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001582 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001583 }
1584 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001585 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1586 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1587 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001588 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001589 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1590 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001591 UseScratchRegisterScope temps(GetVIXLAssembler());
1592 HConstant* src_cst = source.GetConstant();
1593 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001594 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001595 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1596 ? Register(xzr)
1597 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001598 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001599 if (src_cst->IsIntConstant()) {
1600 temp = temps.AcquireW();
1601 } else if (src_cst->IsLongConstant()) {
1602 temp = temps.AcquireX();
1603 } else if (src_cst->IsFloatConstant()) {
1604 temp = temps.AcquireS();
1605 } else {
1606 DCHECK(src_cst->IsDoubleConstant());
1607 temp = temps.AcquireD();
1608 }
1609 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001610 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001611 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001612 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001613 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001614 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001615 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001616 // Use any scratch register (a core or a floating-point one)
1617 // from VIXL scratch register pools as a temporary.
1618 //
1619 // We used to only use the FP scratch register pool, but in some
1620 // rare cases the only register from this pool (D31) would
1621 // already be used (e.g. within a ParallelMove instruction, when
1622 // a move is blocked by a another move requiring a scratch FP
1623 // register, which would reserve D31). To prevent this issue, we
1624 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001625 //
1626 // Also, we start by asking for a FP scratch register first, as the
1627 // demand of scratch core registers is higher. This is why we
1628 // use AcquireFPOrCoreCPURegisterOfSize instead of
1629 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1630 // allocates core scratch registers first.
1631 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1632 GetVIXLAssembler(),
1633 &temps,
1634 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001635 __ Ldr(temp, StackOperandFrom(source));
1636 __ Str(temp, StackOperandFrom(destination));
1637 }
1638 }
1639}
1640
1641void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001642 CPURegister dst,
1643 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001644 switch (type) {
1645 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001646 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001647 break;
1648 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001649 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001650 break;
1651 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001652 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001653 break;
1654 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001655 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001656 break;
1657 case Primitive::kPrimInt:
1658 case Primitive::kPrimNot:
1659 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001660 case Primitive::kPrimFloat:
1661 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001662 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001663 __ Ldr(dst, src);
1664 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001665 case Primitive::kPrimVoid:
1666 LOG(FATAL) << "Unreachable type " << type;
1667 }
1668}
1669
Calin Juravle77520bc2015-01-12 18:45:46 +00001670void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001671 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001672 const MemOperand& src,
1673 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001674 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001675 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001676 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001677 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001678
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001679 DCHECK(!src.IsPreIndex());
1680 DCHECK(!src.IsPostIndex());
1681
1682 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001683 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001684 {
1685 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1686 MemOperand base = MemOperand(temp_base);
1687 switch (type) {
1688 case Primitive::kPrimBoolean:
1689 {
1690 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1691 __ ldarb(Register(dst), base);
1692 if (needs_null_check) {
1693 MaybeRecordImplicitNullCheck(instruction);
1694 }
1695 }
1696 break;
1697 case Primitive::kPrimByte:
1698 {
1699 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1700 __ ldarb(Register(dst), base);
1701 if (needs_null_check) {
1702 MaybeRecordImplicitNullCheck(instruction);
1703 }
1704 }
1705 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1706 break;
1707 case Primitive::kPrimChar:
1708 {
1709 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1710 __ ldarh(Register(dst), base);
1711 if (needs_null_check) {
1712 MaybeRecordImplicitNullCheck(instruction);
1713 }
1714 }
1715 break;
1716 case Primitive::kPrimShort:
1717 {
1718 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1719 __ ldarh(Register(dst), base);
1720 if (needs_null_check) {
1721 MaybeRecordImplicitNullCheck(instruction);
1722 }
1723 }
1724 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1725 break;
1726 case Primitive::kPrimInt:
1727 case Primitive::kPrimNot:
1728 case Primitive::kPrimLong:
1729 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1730 {
1731 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1732 __ ldar(Register(dst), base);
1733 if (needs_null_check) {
1734 MaybeRecordImplicitNullCheck(instruction);
1735 }
1736 }
1737 break;
1738 case Primitive::kPrimFloat:
1739 case Primitive::kPrimDouble: {
1740 DCHECK(dst.IsFPRegister());
1741 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001742
Artem Serov914d7a82017-02-07 14:33:49 +00001743 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1744 {
1745 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1746 __ ldar(temp, base);
1747 if (needs_null_check) {
1748 MaybeRecordImplicitNullCheck(instruction);
1749 }
1750 }
1751 __ Fmov(FPRegister(dst), temp);
1752 break;
Roland Levillain44015862016-01-22 11:47:17 +00001753 }
Artem Serov914d7a82017-02-07 14:33:49 +00001754 case Primitive::kPrimVoid:
1755 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001756 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001757 }
1758}
1759
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001760void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001761 CPURegister src,
1762 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001763 switch (type) {
1764 case Primitive::kPrimBoolean:
1765 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001766 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001767 break;
1768 case Primitive::kPrimChar:
1769 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001770 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001771 break;
1772 case Primitive::kPrimInt:
1773 case Primitive::kPrimNot:
1774 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001775 case Primitive::kPrimFloat:
1776 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001777 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001778 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001779 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001780 case Primitive::kPrimVoid:
1781 LOG(FATAL) << "Unreachable type " << type;
1782 }
1783}
1784
Artem Serov914d7a82017-02-07 14:33:49 +00001785void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
1786 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001787 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00001788 const MemOperand& dst,
1789 bool needs_null_check) {
1790 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001791 UseScratchRegisterScope temps(GetVIXLAssembler());
1792 Register temp_base = temps.AcquireX();
1793
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001794 DCHECK(!dst.IsPreIndex());
1795 DCHECK(!dst.IsPostIndex());
1796
1797 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001798 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001799 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001800 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00001801 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001802 switch (type) {
1803 case Primitive::kPrimBoolean:
1804 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00001805 {
1806 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1807 __ stlrb(Register(src), base);
1808 if (needs_null_check) {
1809 MaybeRecordImplicitNullCheck(instruction);
1810 }
1811 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001812 break;
1813 case Primitive::kPrimChar:
1814 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00001815 {
1816 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1817 __ stlrh(Register(src), base);
1818 if (needs_null_check) {
1819 MaybeRecordImplicitNullCheck(instruction);
1820 }
1821 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001822 break;
1823 case Primitive::kPrimInt:
1824 case Primitive::kPrimNot:
1825 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001826 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00001827 {
1828 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1829 __ stlr(Register(src), base);
1830 if (needs_null_check) {
1831 MaybeRecordImplicitNullCheck(instruction);
1832 }
1833 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001834 break;
1835 case Primitive::kPrimFloat:
1836 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00001837 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01001838 Register temp_src;
1839 if (src.IsZero()) {
1840 // The zero register is used to avoid synthesizing zero constants.
1841 temp_src = Register(src);
1842 } else {
1843 DCHECK(src.IsFPRegister());
1844 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1845 __ Fmov(temp_src, FPRegister(src));
1846 }
Artem Serov914d7a82017-02-07 14:33:49 +00001847 {
1848 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1849 __ stlr(temp_src, base);
1850 if (needs_null_check) {
1851 MaybeRecordImplicitNullCheck(instruction);
1852 }
1853 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001854 break;
1855 }
1856 case Primitive::kPrimVoid:
1857 LOG(FATAL) << "Unreachable type " << type;
1858 }
1859}
1860
Calin Juravle175dc732015-08-25 15:42:32 +01001861void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1862 HInstruction* instruction,
1863 uint32_t dex_pc,
1864 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001865 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00001866
1867 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
1868 {
1869 // Ensure the pc position is recorded immediately after the `blr` instruction.
1870 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
1871 __ blr(lr);
1872 if (EntrypointRequiresStackMap(entrypoint)) {
1873 RecordPcInfo(instruction, dex_pc, slow_path);
1874 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001875 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001876}
1877
Roland Levillaindec8f632016-07-22 17:10:06 +01001878void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1879 HInstruction* instruction,
1880 SlowPathCode* slow_path) {
1881 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01001882 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1883 __ Blr(lr);
1884}
1885
Alexandre Rames67555f72014-11-18 10:55:16 +00001886void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001887 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001888 UseScratchRegisterScope temps(GetVIXLAssembler());
1889 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001890 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1891
Serban Constantinescu02164b32014-11-13 14:05:07 +00001892 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001893 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1894 __ Add(temp, class_reg, status_offset);
1895 __ Ldar(temp, HeapOperand(temp));
1896 __ Cmp(temp, mirror::Class::kStatusInitialized);
1897 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001898 __ Bind(slow_path->GetExitLabel());
1899}
Alexandre Rames5319def2014-10-23 10:03:10 +01001900
Roland Levillain44015862016-01-22 11:47:17 +00001901void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001902 BarrierType type = BarrierAll;
1903
1904 switch (kind) {
1905 case MemBarrierKind::kAnyAny:
1906 case MemBarrierKind::kAnyStore: {
1907 type = BarrierAll;
1908 break;
1909 }
1910 case MemBarrierKind::kLoadAny: {
1911 type = BarrierReads;
1912 break;
1913 }
1914 case MemBarrierKind::kStoreStore: {
1915 type = BarrierWrites;
1916 break;
1917 }
1918 default:
1919 LOG(FATAL) << "Unexpected memory barrier " << kind;
1920 }
1921 __ Dmb(InnerShareable, type);
1922}
1923
Serban Constantinescu02164b32014-11-13 14:05:07 +00001924void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1925 HBasicBlock* successor) {
1926 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001927 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1928 if (slow_path == nullptr) {
1929 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1930 instruction->SetSlowPath(slow_path);
1931 codegen_->AddSlowPath(slow_path);
1932 if (successor != nullptr) {
1933 DCHECK(successor->IsLoopHeader());
1934 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1935 }
1936 } else {
1937 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1938 }
1939
Serban Constantinescu02164b32014-11-13 14:05:07 +00001940 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1941 Register temp = temps.AcquireW();
1942
Andreas Gampe542451c2016-07-26 09:02:02 -07001943 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001944 if (successor == nullptr) {
1945 __ Cbnz(temp, slow_path->GetEntryLabel());
1946 __ Bind(slow_path->GetReturnLabel());
1947 } else {
1948 __ Cbz(temp, codegen_->GetLabelOf(successor));
1949 __ B(slow_path->GetEntryLabel());
1950 // slow_path will return to GetLabelOf(successor).
1951 }
1952}
1953
Alexandre Rames5319def2014-10-23 10:03:10 +01001954InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1955 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001956 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001957 assembler_(codegen->GetAssembler()),
1958 codegen_(codegen) {}
1959
1960#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001961 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001962
1963#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1964
1965enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001966 // Using a base helps identify when we hit such breakpoints.
1967 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001968#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1969 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1970#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1971};
1972
1973#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001974 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001975 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1976 } \
1977 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1978 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1979 locations->SetOut(Location::Any()); \
1980 }
1981 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1982#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1983
1984#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001985#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001986
Alexandre Rames67555f72014-11-18 10:55:16 +00001987void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001988 DCHECK_EQ(instr->InputCount(), 2U);
1989 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1990 Primitive::Type type = instr->GetResultType();
1991 switch (type) {
1992 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001993 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001994 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001995 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001996 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001997 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001998
1999 case Primitive::kPrimFloat:
2000 case Primitive::kPrimDouble:
2001 locations->SetInAt(0, Location::RequiresFpuRegister());
2002 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002003 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002004 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002005
Alexandre Rames5319def2014-10-23 10:03:10 +01002006 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002007 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002008 }
2009}
2010
Alexandre Rames09a99962015-04-15 11:47:56 +01002011void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002012 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2013
2014 bool object_field_get_with_read_barrier =
2015 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002016 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002017 new (GetGraph()->GetArena()) LocationSummary(instruction,
2018 object_field_get_with_read_barrier ?
2019 LocationSummary::kCallOnSlowPath :
2020 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002021 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002022 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002023 // We need a temporary register for the read barrier marking slow
2024 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
2025 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +01002026 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002027 locations->SetInAt(0, Location::RequiresRegister());
2028 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2029 locations->SetOut(Location::RequiresFpuRegister());
2030 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002031 // The output overlaps for an object field get when read barriers
2032 // are enabled: we do not want the load to overwrite the object's
2033 // location, as we need it to emit the read barrier.
2034 locations->SetOut(
2035 Location::RequiresRegister(),
2036 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002037 }
2038}
2039
2040void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2041 const FieldInfo& field_info) {
2042 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002043 LocationSummary* locations = instruction->GetLocations();
2044 Location base_loc = locations->InAt(0);
2045 Location out = locations->Out();
2046 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002047 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002048 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002049
Roland Levillain44015862016-01-22 11:47:17 +00002050 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2051 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002052 // /* HeapReference<Object> */ out = *(base + offset)
2053 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Roland Levillaind0b51832017-01-26 19:04:23 +00002054 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillain44015862016-01-22 11:47:17 +00002055 // Note that potential implicit null checks are handled in this
2056 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2057 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2058 instruction,
2059 out,
2060 base,
2061 offset,
2062 temp,
2063 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002064 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002065 } else {
2066 // General case.
2067 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002068 // Note that a potential implicit null check is handled in this
2069 // CodeGeneratorARM64::LoadAcquire call.
2070 // NB: LoadAcquire will record the pc info if needed.
2071 codegen_->LoadAcquire(
2072 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002073 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002074 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2075 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002076 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002077 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002078 }
Roland Levillain44015862016-01-22 11:47:17 +00002079 if (field_type == Primitive::kPrimNot) {
2080 // If read barriers are enabled, emit read barriers other than
2081 // Baker's using a slow path (and also unpoison the loaded
2082 // reference, if heap poisoning is enabled).
2083 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2084 }
Roland Levillain4d027112015-07-01 15:41:14 +01002085 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002086}
2087
2088void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2089 LocationSummary* locations =
2090 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2091 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002092 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2093 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2094 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002095 locations->SetInAt(1, Location::RequiresFpuRegister());
2096 } else {
2097 locations->SetInAt(1, Location::RequiresRegister());
2098 }
2099}
2100
2101void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002102 const FieldInfo& field_info,
2103 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002104 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2105
2106 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002107 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002108 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002109 Offset offset = field_info.GetFieldOffset();
2110 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002111
Roland Levillain4d027112015-07-01 15:41:14 +01002112 {
2113 // We use a block to end the scratch scope before the write barrier, thus
2114 // freeing the temporary registers so they can be used in `MarkGCCard`.
2115 UseScratchRegisterScope temps(GetVIXLAssembler());
2116
2117 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2118 DCHECK(value.IsW());
2119 Register temp = temps.AcquireW();
2120 __ Mov(temp, value.W());
2121 GetAssembler()->PoisonHeapReference(temp.W());
2122 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002123 }
Roland Levillain4d027112015-07-01 15:41:14 +01002124
2125 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002126 codegen_->StoreRelease(
2127 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002128 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002129 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2130 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002131 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2132 codegen_->MaybeRecordImplicitNullCheck(instruction);
2133 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002134 }
2135
2136 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002137 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002138 }
2139}
2140
Alexandre Rames67555f72014-11-18 10:55:16 +00002141void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002142 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002143
2144 switch (type) {
2145 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002146 case Primitive::kPrimLong: {
2147 Register dst = OutputRegister(instr);
2148 Register lhs = InputRegisterAt(instr, 0);
2149 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002150 if (instr->IsAdd()) {
2151 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002152 } else if (instr->IsAnd()) {
2153 __ And(dst, lhs, rhs);
2154 } else if (instr->IsOr()) {
2155 __ Orr(dst, lhs, rhs);
2156 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002157 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002158 } else if (instr->IsRor()) {
2159 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002160 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002161 __ Ror(dst, lhs, shift);
2162 } else {
2163 // Ensure shift distance is in the same size register as the result. If
2164 // we are rotating a long and the shift comes in a w register originally,
2165 // we don't need to sxtw for use as an x since the shift distances are
2166 // all & reg_bits - 1.
2167 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2168 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002169 } else {
2170 DCHECK(instr->IsXor());
2171 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002172 }
2173 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002174 }
2175 case Primitive::kPrimFloat:
2176 case Primitive::kPrimDouble: {
2177 FPRegister dst = OutputFPRegister(instr);
2178 FPRegister lhs = InputFPRegisterAt(instr, 0);
2179 FPRegister rhs = InputFPRegisterAt(instr, 1);
2180 if (instr->IsAdd()) {
2181 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002182 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002183 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002184 } else {
2185 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002186 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002187 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002188 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002189 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002190 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002191 }
2192}
2193
Serban Constantinescu02164b32014-11-13 14:05:07 +00002194void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2195 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2196
2197 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2198 Primitive::Type type = instr->GetResultType();
2199 switch (type) {
2200 case Primitive::kPrimInt:
2201 case Primitive::kPrimLong: {
2202 locations->SetInAt(0, Location::RequiresRegister());
2203 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2204 locations->SetOut(Location::RequiresRegister());
2205 break;
2206 }
2207 default:
2208 LOG(FATAL) << "Unexpected shift type " << type;
2209 }
2210}
2211
2212void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2213 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2214
2215 Primitive::Type type = instr->GetType();
2216 switch (type) {
2217 case Primitive::kPrimInt:
2218 case Primitive::kPrimLong: {
2219 Register dst = OutputRegister(instr);
2220 Register lhs = InputRegisterAt(instr, 0);
2221 Operand rhs = InputOperandAt(instr, 1);
2222 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002223 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002224 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002225 if (instr->IsShl()) {
2226 __ Lsl(dst, lhs, shift_value);
2227 } else if (instr->IsShr()) {
2228 __ Asr(dst, lhs, shift_value);
2229 } else {
2230 __ Lsr(dst, lhs, shift_value);
2231 }
2232 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002233 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002234
2235 if (instr->IsShl()) {
2236 __ Lsl(dst, lhs, rhs_reg);
2237 } else if (instr->IsShr()) {
2238 __ Asr(dst, lhs, rhs_reg);
2239 } else {
2240 __ Lsr(dst, lhs, rhs_reg);
2241 }
2242 }
2243 break;
2244 }
2245 default:
2246 LOG(FATAL) << "Unexpected shift operation type " << type;
2247 }
2248}
2249
Alexandre Rames5319def2014-10-23 10:03:10 +01002250void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002251 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002252}
2253
2254void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002255 HandleBinaryOp(instruction);
2256}
2257
2258void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2259 HandleBinaryOp(instruction);
2260}
2261
2262void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2263 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002264}
2265
Artem Serov7fc63502016-02-09 17:15:29 +00002266void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002267 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2268 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2269 locations->SetInAt(0, Location::RequiresRegister());
2270 // There is no immediate variant of negated bitwise instructions in AArch64.
2271 locations->SetInAt(1, Location::RequiresRegister());
2272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2273}
2274
Artem Serov7fc63502016-02-09 17:15:29 +00002275void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002276 Register dst = OutputRegister(instr);
2277 Register lhs = InputRegisterAt(instr, 0);
2278 Register rhs = InputRegisterAt(instr, 1);
2279
2280 switch (instr->GetOpKind()) {
2281 case HInstruction::kAnd:
2282 __ Bic(dst, lhs, rhs);
2283 break;
2284 case HInstruction::kOr:
2285 __ Orn(dst, lhs, rhs);
2286 break;
2287 case HInstruction::kXor:
2288 __ Eon(dst, lhs, rhs);
2289 break;
2290 default:
2291 LOG(FATAL) << "Unreachable";
2292 }
2293}
2294
Anton Kirilov74234da2017-01-13 14:42:47 +00002295void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2296 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002297 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2298 instruction->GetType() == Primitive::kPrimLong);
2299 LocationSummary* locations =
2300 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2301 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2302 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2303 } else {
2304 locations->SetInAt(0, Location::RequiresRegister());
2305 }
2306 locations->SetInAt(1, Location::RequiresRegister());
2307 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2308}
2309
Anton Kirilov74234da2017-01-13 14:42:47 +00002310void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2311 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002312 Primitive::Type type = instruction->GetType();
2313 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2314 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2315 Register out = OutputRegister(instruction);
2316 Register left;
2317 if (kind != HInstruction::kNeg) {
2318 left = InputRegisterAt(instruction, 0);
2319 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002320 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002321 // shifter operand operation, the IR generating `right_reg` (input to the type
2322 // conversion) can have a different type from the current instruction's type,
2323 // so we manually indicate the type.
2324 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002325 Operand right_operand(0);
2326
Anton Kirilov74234da2017-01-13 14:42:47 +00002327 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2328 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002329 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2330 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002331 right_operand = Operand(right_reg,
2332 helpers::ShiftFromOpKind(op_kind),
2333 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002334 }
2335
2336 // Logical binary operations do not support extension operations in the
2337 // operand. Note that VIXL would still manage if it was passed by generating
2338 // the extension as a separate instruction.
2339 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2340 DCHECK(!right_operand.IsExtendedRegister() ||
2341 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2342 kind != HInstruction::kNeg));
2343 switch (kind) {
2344 case HInstruction::kAdd:
2345 __ Add(out, left, right_operand);
2346 break;
2347 case HInstruction::kAnd:
2348 __ And(out, left, right_operand);
2349 break;
2350 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002351 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002352 __ Neg(out, right_operand);
2353 break;
2354 case HInstruction::kOr:
2355 __ Orr(out, left, right_operand);
2356 break;
2357 case HInstruction::kSub:
2358 __ Sub(out, left, right_operand);
2359 break;
2360 case HInstruction::kXor:
2361 __ Eor(out, left, right_operand);
2362 break;
2363 default:
2364 LOG(FATAL) << "Unexpected operation kind: " << kind;
2365 UNREACHABLE();
2366 }
2367}
2368
Artem Serov328429f2016-07-06 16:23:04 +01002369void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002370 LocationSummary* locations =
2371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2372 locations->SetInAt(0, Location::RequiresRegister());
2373 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
2374 locations->SetOut(Location::RequiresRegister());
2375}
2376
Roland Levillain19c54192016-11-04 13:44:09 +00002377void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002378 __ Add(OutputRegister(instruction),
2379 InputRegisterAt(instruction, 0),
2380 Operand(InputOperandAt(instruction, 1)));
2381}
2382
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002383void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002384 LocationSummary* locations =
2385 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002386 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2387 if (instr->GetOpKind() == HInstruction::kSub &&
2388 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002389 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002390 // Don't allocate register for Mneg instruction.
2391 } else {
2392 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2393 Location::RequiresRegister());
2394 }
2395 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2396 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002397 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2398}
2399
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002400void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002401 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002402 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2403 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002404
2405 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2406 // This fixup should be carried out for all multiply-accumulate instructions:
2407 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2408 if (instr->GetType() == Primitive::kPrimLong &&
2409 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2410 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002411 vixl::aarch64::Instruction* prev =
2412 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002413 if (prev->IsLoadOrStore()) {
2414 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002415 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002416 __ nop();
2417 }
2418 }
2419
2420 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002421 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002422 __ Madd(res, mul_left, mul_right, accumulator);
2423 } else {
2424 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002425 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002426 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002427 __ Mneg(res, mul_left, mul_right);
2428 } else {
2429 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2430 __ Msub(res, mul_left, mul_right, accumulator);
2431 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002432 }
2433}
2434
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002435void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002436 bool object_array_get_with_read_barrier =
2437 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002438 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002439 new (GetGraph()->GetArena()) LocationSummary(instruction,
2440 object_array_get_with_read_barrier ?
2441 LocationSummary::kCallOnSlowPath :
2442 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002443 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002444 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002445 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002446 locations->SetInAt(0, Location::RequiresRegister());
2447 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002448 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2449 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2450 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002451 // The output overlaps in the case of an object array get with
2452 // read barriers enabled: we do not want the move to overwrite the
2453 // array's location, as we need it to emit the read barrier.
2454 locations->SetOut(
2455 Location::RequiresRegister(),
2456 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002457 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002458}
2459
2460void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002461 Primitive::Type type = instruction->GetType();
2462 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002463 LocationSummary* locations = instruction->GetLocations();
2464 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002465 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002466 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002467 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2468 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002469 MacroAssembler* masm = GetVIXLAssembler();
2470 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002471
Roland Levillain19c54192016-11-04 13:44:09 +00002472 // The read barrier instrumentation of object ArrayGet instructions
2473 // does not support the HIntermediateAddress instruction.
2474 DCHECK(!((type == Primitive::kPrimNot) &&
2475 instruction->GetArray()->IsIntermediateAddress() &&
2476 kEmitCompilerReadBarrier));
2477
Roland Levillain44015862016-01-22 11:47:17 +00002478 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2479 // Object ArrayGet with Baker's read barrier case.
2480 Register temp = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00002481 // Note that a potential implicit null check is handled in the
2482 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2483 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2484 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002485 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002486 // General case.
2487 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002488 Register length;
2489 if (maybe_compressed_char_at) {
2490 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2491 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002492 {
2493 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2494 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2495
2496 if (instruction->GetArray()->IsIntermediateAddress()) {
2497 DCHECK_LT(count_offset, offset);
2498 int64_t adjusted_offset =
2499 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2500 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2501 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2502 } else {
2503 __ Ldr(length, HeapOperand(obj, count_offset));
2504 }
2505 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002506 }
jessicahandojo05765752016-09-09 19:01:32 -07002507 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002508 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002509 if (maybe_compressed_char_at) {
2510 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002511 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2512 "Expecting 0=compressed, 1=uncompressed");
2513 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002514 __ Ldrb(Register(OutputCPURegister(instruction)),
2515 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2516 __ B(&done);
2517 __ Bind(&uncompressed_load);
2518 __ Ldrh(Register(OutputCPURegister(instruction)),
2519 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2520 __ Bind(&done);
2521 } else {
2522 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2523 source = HeapOperand(obj, offset);
2524 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002525 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002526 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002527 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002528 // We do not need to compute the intermediate address from the array: the
2529 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002530 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002531 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002532 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002533 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2534 }
2535 temp = obj;
2536 } else {
2537 __ Add(temp, obj, offset);
2538 }
jessicahandojo05765752016-09-09 19:01:32 -07002539 if (maybe_compressed_char_at) {
2540 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002541 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2542 "Expecting 0=compressed, 1=uncompressed");
2543 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002544 __ Ldrb(Register(OutputCPURegister(instruction)),
2545 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2546 __ B(&done);
2547 __ Bind(&uncompressed_load);
2548 __ Ldrh(Register(OutputCPURegister(instruction)),
2549 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2550 __ Bind(&done);
2551 } else {
2552 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2553 }
Roland Levillain44015862016-01-22 11:47:17 +00002554 }
jessicahandojo05765752016-09-09 19:01:32 -07002555 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002556 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2557 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002558 codegen_->Load(type, OutputCPURegister(instruction), source);
2559 codegen_->MaybeRecordImplicitNullCheck(instruction);
2560 }
Roland Levillain44015862016-01-22 11:47:17 +00002561
2562 if (type == Primitive::kPrimNot) {
2563 static_assert(
2564 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2565 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2566 Location obj_loc = locations->InAt(0);
2567 if (index.IsConstant()) {
2568 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2569 } else {
2570 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2571 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002572 }
Roland Levillain4d027112015-07-01 15:41:14 +01002573 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002574}
2575
Alexandre Rames5319def2014-10-23 10:03:10 +01002576void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2577 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2578 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002579 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002580}
2581
2582void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002583 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002584 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002585 {
2586 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2587 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2588 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2589 codegen_->MaybeRecordImplicitNullCheck(instruction);
2590 }
jessicahandojo05765752016-09-09 19:01:32 -07002591 // Mask out compression flag from String's array length.
2592 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002593 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002594 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002595}
2596
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002597void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002598 Primitive::Type value_type = instruction->GetComponentType();
2599
2600 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002601 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2602 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002603 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002604 LocationSummary::kCallOnSlowPath :
2605 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002606 locations->SetInAt(0, Location::RequiresRegister());
2607 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002608 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2609 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2610 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002611 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002612 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002613 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002614 }
2615}
2616
2617void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2618 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002619 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002620 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002621 bool needs_write_barrier =
2622 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002623
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002624 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002625 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002626 CPURegister source = value;
2627 Location index = locations->InAt(1);
2628 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2629 MemOperand destination = HeapOperand(array);
2630 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002631
2632 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002633 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002634 if (index.IsConstant()) {
2635 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2636 destination = HeapOperand(array, offset);
2637 } else {
2638 UseScratchRegisterScope temps(masm);
2639 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002640 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002641 // We do not need to compute the intermediate address from the array: the
2642 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002643 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002644 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002645 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002646 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2647 }
2648 temp = array;
2649 } else {
2650 __ Add(temp, array, offset);
2651 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002652 destination = HeapOperand(temp,
2653 XRegisterFrom(index),
2654 LSL,
2655 Primitive::ComponentSizeShift(value_type));
2656 }
Artem Serov914d7a82017-02-07 14:33:49 +00002657 {
2658 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2659 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2660 codegen_->Store(value_type, value, destination);
2661 codegen_->MaybeRecordImplicitNullCheck(instruction);
2662 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002663 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002664 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002665 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002666 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002667 {
2668 // We use a block to end the scratch scope before the write barrier, thus
2669 // freeing the temporary registers so they can be used in `MarkGCCard`.
2670 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002671 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002672 if (index.IsConstant()) {
2673 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002674 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002675 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002676 destination = HeapOperand(temp,
2677 XRegisterFrom(index),
2678 LSL,
2679 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002680 }
2681
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002682 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2683 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2684 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2685
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002686 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002687 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2688 codegen_->AddSlowPath(slow_path);
2689 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002690 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002691 __ Cbnz(Register(value), &non_zero);
2692 if (!index.IsConstant()) {
2693 __ Add(temp, array, offset);
2694 }
Artem Serov914d7a82017-02-07 14:33:49 +00002695 {
2696 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
2697 // emitted.
2698 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2699 __ Str(wzr, destination);
2700 codegen_->MaybeRecordImplicitNullCheck(instruction);
2701 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002702 __ B(&done);
2703 __ Bind(&non_zero);
2704 }
2705
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002706 // Note that when Baker read barriers are enabled, the type
2707 // checks are performed without read barriers. This is fine,
2708 // even in the case where a class object is in the from-space
2709 // after the flip, as a comparison involving such a type would
2710 // not produce a false positive; it may of course produce a
2711 // false negative, in which case we would take the ArraySet
2712 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002713
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002714 Register temp2 = temps.AcquireSameSizeAs(array);
2715 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00002716 {
2717 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2718 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2719 __ Ldr(temp, HeapOperand(array, class_offset));
2720 codegen_->MaybeRecordImplicitNullCheck(instruction);
2721 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002722 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002723
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002724 // /* HeapReference<Class> */ temp = temp->component_type_
2725 __ Ldr(temp, HeapOperand(temp, component_offset));
2726 // /* HeapReference<Class> */ temp2 = value->klass_
2727 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2728 // If heap poisoning is enabled, no need to unpoison `temp`
2729 // nor `temp2`, as we are comparing two poisoned references.
2730 __ Cmp(temp, temp2);
2731 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002732
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002733 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2734 vixl::aarch64::Label do_put;
2735 __ B(eq, &do_put);
2736 // If heap poisoning is enabled, the `temp` reference has
2737 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002738 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2739
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002740 // /* HeapReference<Class> */ temp = temp->super_class_
2741 __ Ldr(temp, HeapOperand(temp, super_offset));
2742 // If heap poisoning is enabled, no need to unpoison
2743 // `temp`, as we are comparing against null below.
2744 __ Cbnz(temp, slow_path->GetEntryLabel());
2745 __ Bind(&do_put);
2746 } else {
2747 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002748 }
2749 }
2750
2751 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002752 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002753 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002754 __ Mov(temp2, value.W());
2755 GetAssembler()->PoisonHeapReference(temp2);
2756 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002757 }
2758
2759 if (!index.IsConstant()) {
2760 __ Add(temp, array, offset);
2761 }
Artem Serov914d7a82017-02-07 14:33:49 +00002762 {
2763 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2764 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2765 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002766
Artem Serov914d7a82017-02-07 14:33:49 +00002767 if (!may_need_runtime_call_for_type_check) {
2768 codegen_->MaybeRecordImplicitNullCheck(instruction);
2769 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002770 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002771 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002772
2773 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2774
2775 if (done.IsLinked()) {
2776 __ Bind(&done);
2777 }
2778
2779 if (slow_path != nullptr) {
2780 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002781 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002782 }
2783}
2784
Alexandre Rames67555f72014-11-18 10:55:16 +00002785void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002786 RegisterSet caller_saves = RegisterSet::Empty();
2787 InvokeRuntimeCallingConvention calling_convention;
2788 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2789 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
2790 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00002791 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002792 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002793}
2794
2795void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002796 BoundsCheckSlowPathARM64* slow_path =
2797 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002798 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00002799 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2800 __ B(slow_path->GetEntryLabel(), hs);
2801}
2802
Alexandre Rames67555f72014-11-18 10:55:16 +00002803void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2804 LocationSummary* locations =
2805 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2806 locations->SetInAt(0, Location::RequiresRegister());
2807 if (check->HasUses()) {
2808 locations->SetOut(Location::SameAsFirstInput());
2809 }
2810}
2811
2812void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2813 // We assume the class is not null.
2814 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2815 check->GetLoadClass(), check, check->GetDexPc(), true);
2816 codegen_->AddSlowPath(slow_path);
2817 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2818}
2819
Roland Levillain1a653882016-03-18 18:05:57 +00002820static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2821 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2822 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2823}
2824
2825void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2826 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2827 Location rhs_loc = instruction->GetLocations()->InAt(1);
2828 if (rhs_loc.IsConstant()) {
2829 // 0.0 is the only immediate that can be encoded directly in
2830 // an FCMP instruction.
2831 //
2832 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2833 // specify that in a floating-point comparison, positive zero
2834 // and negative zero are considered equal, so we can use the
2835 // literal 0.0 for both cases here.
2836 //
2837 // Note however that some methods (Float.equal, Float.compare,
2838 // Float.compareTo, Double.equal, Double.compare,
2839 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2840 // StrictMath.min) consider 0.0 to be (strictly) greater than
2841 // -0.0. So if we ever translate calls to these methods into a
2842 // HCompare instruction, we must handle the -0.0 case with
2843 // care here.
2844 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2845 __ Fcmp(lhs_reg, 0.0);
2846 } else {
2847 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2848 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002849}
2850
Serban Constantinescu02164b32014-11-13 14:05:07 +00002851void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002852 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002853 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2854 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002855 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002856 case Primitive::kPrimBoolean:
2857 case Primitive::kPrimByte:
2858 case Primitive::kPrimShort:
2859 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002860 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002861 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002862 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002863 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002864 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2865 break;
2866 }
2867 case Primitive::kPrimFloat:
2868 case Primitive::kPrimDouble: {
2869 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002870 locations->SetInAt(1,
2871 IsFloatingPointZeroConstant(compare->InputAt(1))
2872 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2873 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002874 locations->SetOut(Location::RequiresRegister());
2875 break;
2876 }
2877 default:
2878 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2879 }
2880}
2881
2882void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2883 Primitive::Type in_type = compare->InputAt(0)->GetType();
2884
2885 // 0 if: left == right
2886 // 1 if: left > right
2887 // -1 if: left < right
2888 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002889 case Primitive::kPrimBoolean:
2890 case Primitive::kPrimByte:
2891 case Primitive::kPrimShort:
2892 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002893 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002894 case Primitive::kPrimLong: {
2895 Register result = OutputRegister(compare);
2896 Register left = InputRegisterAt(compare, 0);
2897 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002898 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002899 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2900 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002901 break;
2902 }
2903 case Primitive::kPrimFloat:
2904 case Primitive::kPrimDouble: {
2905 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002906 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002907 __ Cset(result, ne);
2908 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002909 break;
2910 }
2911 default:
2912 LOG(FATAL) << "Unimplemented compare type " << in_type;
2913 }
2914}
2915
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002916void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002917 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002918
2919 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2920 locations->SetInAt(0, Location::RequiresFpuRegister());
2921 locations->SetInAt(1,
2922 IsFloatingPointZeroConstant(instruction->InputAt(1))
2923 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2924 : Location::RequiresFpuRegister());
2925 } else {
2926 // Integer cases.
2927 locations->SetInAt(0, Location::RequiresRegister());
2928 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2929 }
2930
David Brazdilb3e773e2016-01-26 11:28:37 +00002931 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002932 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002933 }
2934}
2935
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002936void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002937 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002938 return;
2939 }
2940
2941 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002942 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002943 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002944
Roland Levillain7f63c522015-07-13 15:54:55 +00002945 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002946 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002947 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002948 } else {
2949 // Integer cases.
2950 Register lhs = InputRegisterAt(instruction, 0);
2951 Operand rhs = InputOperandAt(instruction, 1);
2952 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002953 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002954 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002955}
2956
2957#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2958 M(Equal) \
2959 M(NotEqual) \
2960 M(LessThan) \
2961 M(LessThanOrEqual) \
2962 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002963 M(GreaterThanOrEqual) \
2964 M(Below) \
2965 M(BelowOrEqual) \
2966 M(Above) \
2967 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002968#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002969void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2970void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002971FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002972#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002973#undef FOR_EACH_CONDITION_INSTRUCTION
2974
Zheng Xuc6667102015-05-15 16:08:45 +08002975void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2976 DCHECK(instruction->IsDiv() || instruction->IsRem());
2977
2978 LocationSummary* locations = instruction->GetLocations();
2979 Location second = locations->InAt(1);
2980 DCHECK(second.IsConstant());
2981
2982 Register out = OutputRegister(instruction);
2983 Register dividend = InputRegisterAt(instruction, 0);
2984 int64_t imm = Int64FromConstant(second.GetConstant());
2985 DCHECK(imm == 1 || imm == -1);
2986
2987 if (instruction->IsRem()) {
2988 __ Mov(out, 0);
2989 } else {
2990 if (imm == 1) {
2991 __ Mov(out, dividend);
2992 } else {
2993 __ Neg(out, dividend);
2994 }
2995 }
2996}
2997
2998void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2999 DCHECK(instruction->IsDiv() || instruction->IsRem());
3000
3001 LocationSummary* locations = instruction->GetLocations();
3002 Location second = locations->InAt(1);
3003 DCHECK(second.IsConstant());
3004
3005 Register out = OutputRegister(instruction);
3006 Register dividend = InputRegisterAt(instruction, 0);
3007 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003008 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003009 int ctz_imm = CTZ(abs_imm);
3010
3011 UseScratchRegisterScope temps(GetVIXLAssembler());
3012 Register temp = temps.AcquireSameSizeAs(out);
3013
3014 if (instruction->IsDiv()) {
3015 __ Add(temp, dividend, abs_imm - 1);
3016 __ Cmp(dividend, 0);
3017 __ Csel(out, temp, dividend, lt);
3018 if (imm > 0) {
3019 __ Asr(out, out, ctz_imm);
3020 } else {
3021 __ Neg(out, Operand(out, ASR, ctz_imm));
3022 }
3023 } else {
3024 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3025 __ Asr(temp, dividend, bits - 1);
3026 __ Lsr(temp, temp, bits - ctz_imm);
3027 __ Add(out, dividend, temp);
3028 __ And(out, out, abs_imm - 1);
3029 __ Sub(out, out, temp);
3030 }
3031}
3032
3033void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3034 DCHECK(instruction->IsDiv() || instruction->IsRem());
3035
3036 LocationSummary* locations = instruction->GetLocations();
3037 Location second = locations->InAt(1);
3038 DCHECK(second.IsConstant());
3039
3040 Register out = OutputRegister(instruction);
3041 Register dividend = InputRegisterAt(instruction, 0);
3042 int64_t imm = Int64FromConstant(second.GetConstant());
3043
3044 Primitive::Type type = instruction->GetResultType();
3045 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3046
3047 int64_t magic;
3048 int shift;
3049 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3050
3051 UseScratchRegisterScope temps(GetVIXLAssembler());
3052 Register temp = temps.AcquireSameSizeAs(out);
3053
3054 // temp = get_high(dividend * magic)
3055 __ Mov(temp, magic);
3056 if (type == Primitive::kPrimLong) {
3057 __ Smulh(temp, dividend, temp);
3058 } else {
3059 __ Smull(temp.X(), dividend, temp);
3060 __ Lsr(temp.X(), temp.X(), 32);
3061 }
3062
3063 if (imm > 0 && magic < 0) {
3064 __ Add(temp, temp, dividend);
3065 } else if (imm < 0 && magic > 0) {
3066 __ Sub(temp, temp, dividend);
3067 }
3068
3069 if (shift != 0) {
3070 __ Asr(temp, temp, shift);
3071 }
3072
3073 if (instruction->IsDiv()) {
3074 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3075 } else {
3076 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3077 // TODO: Strength reduction for msub.
3078 Register temp_imm = temps.AcquireSameSizeAs(out);
3079 __ Mov(temp_imm, imm);
3080 __ Msub(out, temp, temp_imm, dividend);
3081 }
3082}
3083
3084void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3085 DCHECK(instruction->IsDiv() || instruction->IsRem());
3086 Primitive::Type type = instruction->GetResultType();
3087 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3088
3089 LocationSummary* locations = instruction->GetLocations();
3090 Register out = OutputRegister(instruction);
3091 Location second = locations->InAt(1);
3092
3093 if (second.IsConstant()) {
3094 int64_t imm = Int64FromConstant(second.GetConstant());
3095
3096 if (imm == 0) {
3097 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3098 } else if (imm == 1 || imm == -1) {
3099 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003100 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003101 DivRemByPowerOfTwo(instruction);
3102 } else {
3103 DCHECK(imm <= -2 || imm >= 2);
3104 GenerateDivRemWithAnyConstant(instruction);
3105 }
3106 } else {
3107 Register dividend = InputRegisterAt(instruction, 0);
3108 Register divisor = InputRegisterAt(instruction, 1);
3109 if (instruction->IsDiv()) {
3110 __ Sdiv(out, dividend, divisor);
3111 } else {
3112 UseScratchRegisterScope temps(GetVIXLAssembler());
3113 Register temp = temps.AcquireSameSizeAs(out);
3114 __ Sdiv(temp, dividend, divisor);
3115 __ Msub(out, temp, divisor, dividend);
3116 }
3117 }
3118}
3119
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003120void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3121 LocationSummary* locations =
3122 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3123 switch (div->GetResultType()) {
3124 case Primitive::kPrimInt:
3125 case Primitive::kPrimLong:
3126 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003127 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003128 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3129 break;
3130
3131 case Primitive::kPrimFloat:
3132 case Primitive::kPrimDouble:
3133 locations->SetInAt(0, Location::RequiresFpuRegister());
3134 locations->SetInAt(1, Location::RequiresFpuRegister());
3135 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3136 break;
3137
3138 default:
3139 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3140 }
3141}
3142
3143void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3144 Primitive::Type type = div->GetResultType();
3145 switch (type) {
3146 case Primitive::kPrimInt:
3147 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003148 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003149 break;
3150
3151 case Primitive::kPrimFloat:
3152 case Primitive::kPrimDouble:
3153 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3154 break;
3155
3156 default:
3157 LOG(FATAL) << "Unexpected div type " << type;
3158 }
3159}
3160
Alexandre Rames67555f72014-11-18 10:55:16 +00003161void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003162 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003163 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003164}
3165
3166void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3167 SlowPathCodeARM64* slow_path =
3168 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3169 codegen_->AddSlowPath(slow_path);
3170 Location value = instruction->GetLocations()->InAt(0);
3171
Alexandre Rames3e69f162014-12-10 10:36:50 +00003172 Primitive::Type type = instruction->GetType();
3173
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003174 if (!Primitive::IsIntegralType(type)) {
3175 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003176 return;
3177 }
3178
Alexandre Rames67555f72014-11-18 10:55:16 +00003179 if (value.IsConstant()) {
3180 int64_t divisor = Int64ConstantFrom(value);
3181 if (divisor == 0) {
3182 __ B(slow_path->GetEntryLabel());
3183 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003184 // A division by a non-null constant is valid. We don't need to perform
3185 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003186 }
3187 } else {
3188 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3189 }
3190}
3191
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003192void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3193 LocationSummary* locations =
3194 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3195 locations->SetOut(Location::ConstantLocation(constant));
3196}
3197
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003198void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3199 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003200 // Will be generated at use site.
3201}
3202
Alexandre Rames5319def2014-10-23 10:03:10 +01003203void LocationsBuilderARM64::VisitExit(HExit* exit) {
3204 exit->SetLocations(nullptr);
3205}
3206
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003207void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003208}
3209
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003210void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3211 LocationSummary* locations =
3212 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3213 locations->SetOut(Location::ConstantLocation(constant));
3214}
3215
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003216void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003217 // Will be generated at use site.
3218}
3219
David Brazdilfc6a86a2015-06-26 10:33:45 +00003220void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003221 DCHECK(!successor->IsExitBlock());
3222 HBasicBlock* block = got->GetBlock();
3223 HInstruction* previous = got->GetPrevious();
3224 HLoopInformation* info = block->GetLoopInformation();
3225
David Brazdil46e2a392015-03-16 17:31:52 +00003226 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003227 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3228 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3229 return;
3230 }
3231 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3232 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3233 }
3234 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003235 __ B(codegen_->GetLabelOf(successor));
3236 }
3237}
3238
David Brazdilfc6a86a2015-06-26 10:33:45 +00003239void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3240 got->SetLocations(nullptr);
3241}
3242
3243void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3244 HandleGoto(got, got->GetSuccessor());
3245}
3246
3247void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3248 try_boundary->SetLocations(nullptr);
3249}
3250
3251void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3252 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3253 if (!successor->IsExitBlock()) {
3254 HandleGoto(try_boundary, successor);
3255 }
3256}
3257
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003258void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003259 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003260 vixl::aarch64::Label* true_target,
3261 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003262 // FP branching requires both targets to be explicit. If either of the targets
3263 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003264 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003265 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003266
David Brazdil0debae72015-11-12 18:37:00 +00003267 if (true_target == nullptr && false_target == nullptr) {
3268 // Nothing to do. The code always falls through.
3269 return;
3270 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003271 // Constant condition, statically compared against "true" (integer value 1).
3272 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003273 if (true_target != nullptr) {
3274 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003275 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003276 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003277 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003278 if (false_target != nullptr) {
3279 __ B(false_target);
3280 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003281 }
David Brazdil0debae72015-11-12 18:37:00 +00003282 return;
3283 }
3284
3285 // The following code generates these patterns:
3286 // (1) true_target == nullptr && false_target != nullptr
3287 // - opposite condition true => branch to false_target
3288 // (2) true_target != nullptr && false_target == nullptr
3289 // - condition true => branch to true_target
3290 // (3) true_target != nullptr && false_target != nullptr
3291 // - condition true => branch to true_target
3292 // - branch to false_target
3293 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003294 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003295 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003296 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003297 if (true_target == nullptr) {
3298 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3299 } else {
3300 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3301 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003302 } else {
3303 // The condition instruction has not been materialized, use its inputs as
3304 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003305 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003306
David Brazdil0debae72015-11-12 18:37:00 +00003307 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003308 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003309 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003310 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003311 IfCondition opposite_condition = condition->GetOppositeCondition();
3312 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003313 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003314 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003315 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003316 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003317 // Integer cases.
3318 Register lhs = InputRegisterAt(condition, 0);
3319 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003320
3321 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003322 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003323 if (true_target == nullptr) {
3324 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3325 non_fallthrough_target = false_target;
3326 } else {
3327 arm64_cond = ARM64Condition(condition->GetCondition());
3328 non_fallthrough_target = true_target;
3329 }
3330
Aart Bik086d27e2016-01-20 17:02:00 -08003331 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003332 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003333 switch (arm64_cond) {
3334 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003335 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003336 break;
3337 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003338 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003339 break;
3340 case lt:
3341 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003342 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003343 break;
3344 case ge:
3345 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003346 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003347 break;
3348 default:
3349 // Without the `static_cast` the compiler throws an error for
3350 // `-Werror=sign-promo`.
3351 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3352 }
3353 } else {
3354 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003355 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003356 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003357 }
3358 }
David Brazdil0debae72015-11-12 18:37:00 +00003359
3360 // If neither branch falls through (case 3), the conditional branch to `true_target`
3361 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3362 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003363 __ B(false_target);
3364 }
David Brazdil0debae72015-11-12 18:37:00 +00003365
3366 if (fallthrough_target.IsLinked()) {
3367 __ Bind(&fallthrough_target);
3368 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003369}
3370
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003371void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3372 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003373 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003374 locations->SetInAt(0, Location::RequiresRegister());
3375 }
3376}
3377
3378void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003379 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3380 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003381 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3382 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3383 true_target = nullptr;
3384 }
3385 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3386 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3387 false_target = nullptr;
3388 }
David Brazdil0debae72015-11-12 18:37:00 +00003389 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003390}
3391
3392void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3393 LocationSummary* locations = new (GetGraph()->GetArena())
3394 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003395 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003396 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003397 locations->SetInAt(0, Location::RequiresRegister());
3398 }
3399}
3400
3401void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003402 SlowPathCodeARM64* slow_path =
3403 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003404 GenerateTestAndBranch(deoptimize,
3405 /* condition_input_index */ 0,
3406 slow_path->GetEntryLabel(),
3407 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003408}
3409
Mingyao Yang063fc772016-08-02 11:02:54 -07003410void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3411 LocationSummary* locations = new (GetGraph()->GetArena())
3412 LocationSummary(flag, LocationSummary::kNoCall);
3413 locations->SetOut(Location::RequiresRegister());
3414}
3415
3416void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3417 __ Ldr(OutputRegister(flag),
3418 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3419}
3420
David Brazdilc0b601b2016-02-08 14:20:45 +00003421static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3422 return condition->IsCondition() &&
3423 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3424}
3425
Alexandre Rames880f1192016-06-13 16:04:50 +01003426static inline Condition GetConditionForSelect(HCondition* condition) {
3427 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003428 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3429 : ARM64Condition(cond);
3430}
3431
David Brazdil74eb1b22015-12-14 11:44:01 +00003432void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3433 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003434 if (Primitive::IsFloatingPointType(select->GetType())) {
3435 locations->SetInAt(0, Location::RequiresFpuRegister());
3436 locations->SetInAt(1, Location::RequiresFpuRegister());
3437 locations->SetOut(Location::RequiresFpuRegister());
3438 } else {
3439 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3440 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3441 bool is_true_value_constant = cst_true_value != nullptr;
3442 bool is_false_value_constant = cst_false_value != nullptr;
3443 // Ask VIXL whether we should synthesize constants in registers.
3444 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3445 Operand true_op = is_true_value_constant ?
3446 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3447 Operand false_op = is_false_value_constant ?
3448 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3449 bool true_value_in_register = false;
3450 bool false_value_in_register = false;
3451 MacroAssembler::GetCselSynthesisInformation(
3452 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3453 true_value_in_register |= !is_true_value_constant;
3454 false_value_in_register |= !is_false_value_constant;
3455
3456 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3457 : Location::ConstantLocation(cst_true_value));
3458 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3459 : Location::ConstantLocation(cst_false_value));
3460 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003461 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003462
David Brazdil74eb1b22015-12-14 11:44:01 +00003463 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3464 locations->SetInAt(2, Location::RequiresRegister());
3465 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003466}
3467
3468void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003469 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003470 Condition csel_cond;
3471
3472 if (IsBooleanValueOrMaterializedCondition(cond)) {
3473 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003474 // Use the condition flags set by the previous instruction.
3475 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003476 } else {
3477 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003478 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003479 }
3480 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003481 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003482 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003483 } else {
3484 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003485 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003486 }
3487
Alexandre Rames880f1192016-06-13 16:04:50 +01003488 if (Primitive::IsFloatingPointType(select->GetType())) {
3489 __ Fcsel(OutputFPRegister(select),
3490 InputFPRegisterAt(select, 1),
3491 InputFPRegisterAt(select, 0),
3492 csel_cond);
3493 } else {
3494 __ Csel(OutputRegister(select),
3495 InputOperandAt(select, 1),
3496 InputOperandAt(select, 0),
3497 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003498 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003499}
3500
David Srbecky0cf44932015-12-09 14:09:59 +00003501void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3502 new (GetGraph()->GetArena()) LocationSummary(info);
3503}
3504
David Srbeckyd28f4a02016-03-14 17:14:24 +00003505void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3506 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003507}
3508
3509void CodeGeneratorARM64::GenerateNop() {
3510 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003511}
3512
Alexandre Rames5319def2014-10-23 10:03:10 +01003513void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003514 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003515}
3516
3517void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003518 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003519}
3520
3521void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003522 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003523}
3524
3525void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003526 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003527}
3528
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003529// Temp is used for read barrier.
3530static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3531 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003532 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003533 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3534 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3535 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3536 return 1;
3537 }
3538 return 0;
3539}
3540
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003541// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003542// interface pointer, one for loading the current interface.
3543// The other checks have one temp for loading the object's class.
3544static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3545 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3546 return 3;
3547 }
3548 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003549}
3550
Alexandre Rames67555f72014-11-18 10:55:16 +00003551void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003552 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003553 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003554 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003555 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003556 case TypeCheckKind::kExactCheck:
3557 case TypeCheckKind::kAbstractClassCheck:
3558 case TypeCheckKind::kClassHierarchyCheck:
3559 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003560 call_kind =
3561 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003562 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003563 break;
3564 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003565 case TypeCheckKind::kUnresolvedCheck:
3566 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003567 call_kind = LocationSummary::kCallOnSlowPath;
3568 break;
3569 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003570
Alexandre Rames67555f72014-11-18 10:55:16 +00003571 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003572 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003573 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003574 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003575 locations->SetInAt(0, Location::RequiresRegister());
3576 locations->SetInAt(1, Location::RequiresRegister());
3577 // The "out" register is used as a temporary, so it overlaps with the inputs.
3578 // Note that TypeCheckSlowPathARM64 uses this register too.
3579 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003580 // Add temps if necessary for read barriers.
3581 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003582}
3583
3584void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003585 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003586 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003587 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003588 Register obj = InputRegisterAt(instruction, 0);
3589 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003590 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003591 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003592 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3593 DCHECK_LE(num_temps, 1u);
3594 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003595 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3596 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3597 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3598 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003599
Scott Wakeling97c72b72016-06-24 16:19:36 +01003600 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003601 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003602
3603 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003604 // Avoid null check if we know `obj` is not null.
3605 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003606 __ Cbz(obj, &zero);
3607 }
3608
Roland Levillain44015862016-01-22 11:47:17 +00003609 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003610 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003611 // /* HeapReference<Class> */ out = obj->klass_
3612 GenerateReferenceLoadTwoRegisters(instruction,
3613 out_loc,
3614 obj_loc,
3615 class_offset,
3616 maybe_temp_loc,
3617 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003618 __ Cmp(out, cls);
3619 __ Cset(out, eq);
3620 if (zero.IsLinked()) {
3621 __ B(&done);
3622 }
3623 break;
3624 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003625
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003626 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003627 // /* HeapReference<Class> */ out = obj->klass_
3628 GenerateReferenceLoadTwoRegisters(instruction,
3629 out_loc,
3630 obj_loc,
3631 class_offset,
3632 maybe_temp_loc,
3633 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003634 // If the class is abstract, we eagerly fetch the super class of the
3635 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003636 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003637 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003638 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003639 GenerateReferenceLoadOneRegister(instruction,
3640 out_loc,
3641 super_offset,
3642 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003643 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003644 // If `out` is null, we use it for the result, and jump to `done`.
3645 __ Cbz(out, &done);
3646 __ Cmp(out, cls);
3647 __ B(ne, &loop);
3648 __ Mov(out, 1);
3649 if (zero.IsLinked()) {
3650 __ B(&done);
3651 }
3652 break;
3653 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003654
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003655 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003656 // /* HeapReference<Class> */ out = obj->klass_
3657 GenerateReferenceLoadTwoRegisters(instruction,
3658 out_loc,
3659 obj_loc,
3660 class_offset,
3661 maybe_temp_loc,
3662 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003663 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003664 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003665 __ Bind(&loop);
3666 __ Cmp(out, cls);
3667 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003668 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003669 GenerateReferenceLoadOneRegister(instruction,
3670 out_loc,
3671 super_offset,
3672 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003673 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003674 __ Cbnz(out, &loop);
3675 // If `out` is null, we use it for the result, and jump to `done`.
3676 __ B(&done);
3677 __ Bind(&success);
3678 __ Mov(out, 1);
3679 if (zero.IsLinked()) {
3680 __ B(&done);
3681 }
3682 break;
3683 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003684
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003685 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003686 // /* HeapReference<Class> */ out = obj->klass_
3687 GenerateReferenceLoadTwoRegisters(instruction,
3688 out_loc,
3689 obj_loc,
3690 class_offset,
3691 maybe_temp_loc,
3692 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003693 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003694 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003695 __ Cmp(out, cls);
3696 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003697 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003698 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003699 GenerateReferenceLoadOneRegister(instruction,
3700 out_loc,
3701 component_offset,
3702 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003703 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003704 // If `out` is null, we use it for the result, and jump to `done`.
3705 __ Cbz(out, &done);
3706 __ Ldrh(out, HeapOperand(out, primitive_offset));
3707 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3708 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003709 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003710 __ Mov(out, 1);
3711 __ B(&done);
3712 break;
3713 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003714
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003715 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003716 // No read barrier since the slow path will retry upon failure.
3717 // /* HeapReference<Class> */ out = obj->klass_
3718 GenerateReferenceLoadTwoRegisters(instruction,
3719 out_loc,
3720 obj_loc,
3721 class_offset,
3722 maybe_temp_loc,
3723 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003724 __ Cmp(out, cls);
3725 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003726 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3727 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003728 codegen_->AddSlowPath(slow_path);
3729 __ B(ne, slow_path->GetEntryLabel());
3730 __ Mov(out, 1);
3731 if (zero.IsLinked()) {
3732 __ B(&done);
3733 }
3734 break;
3735 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003736
Calin Juravle98893e12015-10-02 21:05:03 +01003737 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003738 case TypeCheckKind::kInterfaceCheck: {
3739 // Note that we indeed only call on slow path, but we always go
3740 // into the slow path for the unresolved and interface check
3741 // cases.
3742 //
3743 // We cannot directly call the InstanceofNonTrivial runtime
3744 // entry point without resorting to a type checking slow path
3745 // here (i.e. by calling InvokeRuntime directly), as it would
3746 // require to assign fixed registers for the inputs of this
3747 // HInstanceOf instruction (following the runtime calling
3748 // convention), which might be cluttered by the potential first
3749 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003750 //
3751 // TODO: Introduce a new runtime entry point taking the object
3752 // to test (instead of its class) as argument, and let it deal
3753 // with the read barrier issues. This will let us refactor this
3754 // case of the `switch` code as it was previously (with a direct
3755 // call to the runtime not using a type checking slow path).
3756 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003757 DCHECK(locations->OnlyCallsOnSlowPath());
3758 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3759 /* is_fatal */ false);
3760 codegen_->AddSlowPath(slow_path);
3761 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003762 if (zero.IsLinked()) {
3763 __ B(&done);
3764 }
3765 break;
3766 }
3767 }
3768
3769 if (zero.IsLinked()) {
3770 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003771 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003772 }
3773
3774 if (done.IsLinked()) {
3775 __ Bind(&done);
3776 }
3777
3778 if (slow_path != nullptr) {
3779 __ Bind(slow_path->GetExitLabel());
3780 }
3781}
3782
3783void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3784 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3785 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3786
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003787 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3788 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003789 case TypeCheckKind::kExactCheck:
3790 case TypeCheckKind::kAbstractClassCheck:
3791 case TypeCheckKind::kClassHierarchyCheck:
3792 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003793 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3794 LocationSummary::kCallOnSlowPath :
3795 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003796 break;
3797 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003798 case TypeCheckKind::kUnresolvedCheck:
3799 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003800 call_kind = LocationSummary::kCallOnSlowPath;
3801 break;
3802 }
3803
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003804 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3805 locations->SetInAt(0, Location::RequiresRegister());
3806 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003807 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
3808 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003809}
3810
3811void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003812 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003813 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003814 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003815 Register obj = InputRegisterAt(instruction, 0);
3816 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003817 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3818 DCHECK_GE(num_temps, 1u);
3819 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003820 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003821 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
3822 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003823 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003824 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3825 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3826 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3827 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3828 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3829 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3830 const uint32_t object_array_data_offset =
3831 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003832
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003833 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003834 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3835 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3836 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003837 if (!kEmitCompilerReadBarrier) {
3838 is_type_check_slow_path_fatal =
3839 (type_check_kind == TypeCheckKind::kExactCheck ||
3840 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3841 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3842 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3843 !instruction->CanThrowIntoCatchBlock();
3844 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003845 SlowPathCodeARM64* type_check_slow_path =
3846 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3847 is_type_check_slow_path_fatal);
3848 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003849
Scott Wakeling97c72b72016-06-24 16:19:36 +01003850 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003851 // Avoid null check if we know obj is not null.
3852 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003853 __ Cbz(obj, &done);
3854 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003855
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003856 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003857 case TypeCheckKind::kExactCheck:
3858 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003859 // /* HeapReference<Class> */ temp = obj->klass_
3860 GenerateReferenceLoadTwoRegisters(instruction,
3861 temp_loc,
3862 obj_loc,
3863 class_offset,
3864 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003865 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003866
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003867 __ Cmp(temp, cls);
3868 // Jump to slow path for throwing the exception or doing a
3869 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003870 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003871 break;
3872 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003873
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003874 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003875 // /* HeapReference<Class> */ temp = obj->klass_
3876 GenerateReferenceLoadTwoRegisters(instruction,
3877 temp_loc,
3878 obj_loc,
3879 class_offset,
3880 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003881 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003882
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003883 // If the class is abstract, we eagerly fetch the super class of the
3884 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003885 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003886 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003887 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003888 GenerateReferenceLoadOneRegister(instruction,
3889 temp_loc,
3890 super_offset,
3891 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003892 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003893
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003894 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3895 // exception.
3896 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3897 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003898 __ Cmp(temp, cls);
3899 __ B(ne, &loop);
3900 break;
3901 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003902
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003903 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003904 // /* HeapReference<Class> */ temp = obj->klass_
3905 GenerateReferenceLoadTwoRegisters(instruction,
3906 temp_loc,
3907 obj_loc,
3908 class_offset,
3909 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003910 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003911
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003912 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003913 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003914 __ Bind(&loop);
3915 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003916 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003917
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003918 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003919 GenerateReferenceLoadOneRegister(instruction,
3920 temp_loc,
3921 super_offset,
3922 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003923 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003924
3925 // If the class reference currently in `temp` is not null, jump
3926 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003927 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003928 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003929 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003930 break;
3931 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003932
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003933 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003934 // /* HeapReference<Class> */ temp = obj->klass_
3935 GenerateReferenceLoadTwoRegisters(instruction,
3936 temp_loc,
3937 obj_loc,
3938 class_offset,
3939 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003940 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003941
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003942 // Do an exact check.
3943 __ Cmp(temp, cls);
3944 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003945
3946 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003947 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003948 GenerateReferenceLoadOneRegister(instruction,
3949 temp_loc,
3950 component_offset,
3951 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003952 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003953
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003954 // If the component type is null, jump to the slow path to throw the exception.
3955 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
3956 // Otherwise, the object is indeed an array. Further check that this component type is not a
3957 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003958 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3959 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08003960 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003961 break;
3962 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003963
Calin Juravle98893e12015-10-02 21:05:03 +01003964 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003965 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003966 //
3967 // We cannot directly call the CheckCast runtime entry point
3968 // without resorting to a type checking slow path here (i.e. by
3969 // calling InvokeRuntime directly), as it would require to
3970 // assign fixed registers for the inputs of this HInstanceOf
3971 // instruction (following the runtime calling convention), which
3972 // might be cluttered by the potential first read barrier
3973 // emission at the beginning of this method.
3974 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003975 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003976 case TypeCheckKind::kInterfaceCheck: {
3977 // /* HeapReference<Class> */ temp = obj->klass_
3978 GenerateReferenceLoadTwoRegisters(instruction,
3979 temp_loc,
3980 obj_loc,
3981 class_offset,
3982 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003983 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003984
3985 // /* HeapReference<Class> */ temp = temp->iftable_
3986 GenerateReferenceLoadTwoRegisters(instruction,
3987 temp_loc,
3988 temp_loc,
3989 iftable_offset,
3990 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003991 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003992 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003993 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08003994 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003995 vixl::aarch64::Label start_loop;
3996 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08003997 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003998 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
3999 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004000 // Go to next interface.
4001 __ Add(temp, temp, 2 * kHeapReferenceSize);
4002 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004003 // Compare the classes and continue the loop if they do not match.
4004 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4005 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004006 break;
4007 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004008 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004009 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004010
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004011 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004012}
4013
Alexandre Rames5319def2014-10-23 10:03:10 +01004014void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4015 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4016 locations->SetOut(Location::ConstantLocation(constant));
4017}
4018
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004019void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004020 // Will be generated at use site.
4021}
4022
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004023void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4024 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4025 locations->SetOut(Location::ConstantLocation(constant));
4026}
4027
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004028void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004029 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004030}
4031
Calin Juravle175dc732015-08-25 15:42:32 +01004032void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4033 // The trampoline uses the same calling convention as dex calling conventions,
4034 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4035 // the method_idx.
4036 HandleInvoke(invoke);
4037}
4038
4039void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4040 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4041}
4042
Alexandre Rames5319def2014-10-23 10:03:10 +01004043void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004044 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004045 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004046}
4047
Alexandre Rames67555f72014-11-18 10:55:16 +00004048void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4049 HandleInvoke(invoke);
4050}
4051
4052void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4053 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004054 LocationSummary* locations = invoke->GetLocations();
4055 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004056 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004057 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004058 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004059
4060 // The register ip1 is required to be used for the hidden argument in
4061 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004062 MacroAssembler* masm = GetVIXLAssembler();
4063 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004064 scratch_scope.Exclude(ip1);
4065 __ Mov(ip1, invoke->GetDexMethodIndex());
4066
Artem Serov914d7a82017-02-07 14:33:49 +00004067 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004068 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004069 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004070 {
4071 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4072 // /* HeapReference<Class> */ temp = temp->klass_
4073 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4074 codegen_->MaybeRecordImplicitNullCheck(invoke);
4075 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004076 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004077 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004078 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004079 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004080 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004081 }
Artem Serov914d7a82017-02-07 14:33:49 +00004082
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004083 // Instead of simply (possibly) unpoisoning `temp` here, we should
4084 // emit a read barrier for the previous class reference load.
4085 // However this is not required in practice, as this is an
4086 // intermediate/temporary reference and because the current
4087 // concurrent copying collector keeps the from-space memory
4088 // intact/accessible until the end of the marking phase (the
4089 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004090 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004091 __ Ldr(temp,
4092 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4093 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004094 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004095 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004096 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004097 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004098 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004099
4100 {
4101 // Ensure the pc position is recorded immediately after the `blr` instruction.
4102 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4103
4104 // lr();
4105 __ blr(lr);
4106 DCHECK(!codegen_->IsLeafMethod());
4107 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4108 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004109}
4110
4111void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004112 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004113 if (intrinsic.TryDispatch(invoke)) {
4114 return;
4115 }
4116
Alexandre Rames67555f72014-11-18 10:55:16 +00004117 HandleInvoke(invoke);
4118}
4119
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004120void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004121 // Explicit clinit checks triggered by static invokes must have been pruned by
4122 // art::PrepareForRegisterAllocation.
4123 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004124
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004125 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004126 if (intrinsic.TryDispatch(invoke)) {
4127 return;
4128 }
4129
Alexandre Rames67555f72014-11-18 10:55:16 +00004130 HandleInvoke(invoke);
4131}
4132
Andreas Gampe878d58c2015-01-15 23:24:00 -08004133static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4134 if (invoke->GetLocations()->Intrinsified()) {
4135 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4136 intrinsic.Dispatch(invoke);
4137 return true;
4138 }
4139 return false;
4140}
4141
Vladimir Markodc151b22015-10-15 18:02:30 +01004142HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4143 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004144 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004145 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004146 return desired_dispatch_info;
4147}
4148
TatWai Chongd8c052a2016-11-02 16:12:48 +08004149Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4150 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004151 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004152 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4153 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004154 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4155 uint32_t offset =
4156 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004157 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004158 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004159 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004160 }
Vladimir Marko58155012015-08-19 12:49:41 +00004161 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004162 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004163 break;
4164 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4165 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004166 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004167 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004168 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4169 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004170 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004171 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004172 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004173 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004174 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004175 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004176 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004177 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004178 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004179 }
Vladimir Marko58155012015-08-19 12:49:41 +00004180 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004181 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004182 Register reg = XRegisterFrom(temp);
4183 Register method_reg;
4184 if (current_method.IsRegister()) {
4185 method_reg = XRegisterFrom(current_method);
4186 } else {
4187 DCHECK(invoke->GetLocations()->Intrinsified());
4188 DCHECK(!current_method.IsValid());
4189 method_reg = reg;
4190 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4191 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004192
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004193 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004194 __ Ldr(reg.X(),
4195 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004196 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004197 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004198 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4199 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004200 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4201 break;
4202 }
4203 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004204 return callee_method;
4205}
4206
4207void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4208 // All registers are assumed to be correctly set up.
4209 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004210
4211 switch (invoke->GetCodePtrLocation()) {
4212 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4213 __ Bl(&frame_entry_label_);
4214 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004215 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4216 // LR = callee_method->entry_point_from_quick_compiled_code_;
4217 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004218 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004219 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004220 {
4221 // To ensure that the pc position is recorded immediately after the `blr` instruction
4222 // BLR must be the last instruction emitted in this function.
4223 // Recording the pc will occur right after returning from this function.
4224 ExactAssemblyScope eas(GetVIXLAssembler(),
4225 kInstructionSize,
4226 CodeBufferCheckScope::kExactSize);
4227 // lr()
4228 __ blr(lr);
4229 }
Vladimir Marko58155012015-08-19 12:49:41 +00004230 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004231 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004232
Andreas Gampe878d58c2015-01-15 23:24:00 -08004233 DCHECK(!IsLeafMethod());
4234}
4235
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004236void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004237 // Use the calling convention instead of the location of the receiver, as
4238 // intrinsics may have put the receiver in a different register. In the intrinsics
4239 // slow path, the arguments have been moved to the right place, so here we are
4240 // guaranteed that the receiver is the first register of the calling convention.
4241 InvokeDexCallingConvention calling_convention;
4242 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004243 Register temp = XRegisterFrom(temp_in);
4244 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4245 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4246 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004247 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004248
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004249 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004250
4251 {
4252 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4253 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4254 // /* HeapReference<Class> */ temp = receiver->klass_
4255 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4256 MaybeRecordImplicitNullCheck(invoke);
4257 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004258 // Instead of simply (possibly) unpoisoning `temp` here, we should
4259 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004260 // intermediate/temporary reference and because the current
4261 // concurrent copying collector keeps the from-space memory
4262 // intact/accessible until the end of the marking phase (the
4263 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004264 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4265 // temp = temp->GetMethodAt(method_offset);
4266 __ Ldr(temp, MemOperand(temp, method_offset));
4267 // lr = temp->GetEntryPoint();
4268 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004269 {
4270 // To ensure that the pc position is recorded immediately after the `blr` instruction
4271 // BLR should be the last instruction emitted in this function.
4272 // Recording the pc will occur right after returning from this function.
4273 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4274 // lr();
4275 __ blr(lr);
4276 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004277}
4278
Orion Hodsonac141392017-01-13 11:53:47 +00004279void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4280 HandleInvoke(invoke);
4281}
4282
4283void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4284 codegen_->GenerateInvokePolymorphicCall(invoke);
4285}
4286
Scott Wakeling97c72b72016-06-24 16:19:36 +01004287vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4288 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004289 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004290 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004291 return
4292 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004293}
4294
Scott Wakeling97c72b72016-06-24 16:19:36 +01004295vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4296 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004297 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004298 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004299 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004300}
4301
Vladimir Marko1998cd02017-01-13 13:02:58 +00004302vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4303 const DexFile& dex_file,
4304 dex::TypeIndex type_index,
4305 vixl::aarch64::Label* adrp_label) {
4306 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4307}
4308
Scott Wakeling97c72b72016-06-24 16:19:36 +01004309vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4310 const DexFile& dex_file,
4311 uint32_t element_offset,
4312 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004313 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4314}
4315
Scott Wakeling97c72b72016-06-24 16:19:36 +01004316vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4317 const DexFile& dex_file,
4318 uint32_t offset_or_index,
4319 vixl::aarch64::Label* adrp_label,
4320 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004321 // Add a patch entry and return the label.
4322 patches->emplace_back(dex_file, offset_or_index);
4323 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004324 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004325 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4326 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4327 return label;
4328}
4329
Scott Wakeling97c72b72016-06-24 16:19:36 +01004330vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004331 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004332 return boot_image_string_patches_.GetOrCreate(
4333 StringReference(&dex_file, string_index),
4334 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4335}
4336
Scott Wakeling97c72b72016-06-24 16:19:36 +01004337vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004338 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004339 return boot_image_type_patches_.GetOrCreate(
4340 TypeReference(&dex_file, type_index),
4341 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4342}
4343
Scott Wakeling97c72b72016-06-24 16:19:36 +01004344vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4345 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004346 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
4347 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
4348 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
4349}
4350
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004351vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004352 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4353 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4354 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004355 return jit_string_patches_.GetOrCreate(
4356 StringReference(&dex_file, string_index),
4357 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4358}
4359
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004360vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004361 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4362 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4363 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004364 return jit_class_patches_.GetOrCreate(
4365 TypeReference(&dex_file, type_index),
4366 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4367}
4368
Vladimir Markoaad75c62016-10-03 08:46:48 +00004369void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4370 vixl::aarch64::Register reg) {
4371 DCHECK(reg.IsX());
4372 SingleEmissionCheckScope guard(GetVIXLAssembler());
4373 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004374 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004375}
4376
4377void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4378 vixl::aarch64::Register out,
4379 vixl::aarch64::Register base) {
4380 DCHECK(out.IsX());
4381 DCHECK(base.IsX());
4382 SingleEmissionCheckScope guard(GetVIXLAssembler());
4383 __ Bind(fixup_label);
4384 __ add(out, base, Operand(/* offset placeholder */ 0));
4385}
4386
4387void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4388 vixl::aarch64::Register out,
4389 vixl::aarch64::Register base) {
4390 DCHECK(base.IsX());
4391 SingleEmissionCheckScope guard(GetVIXLAssembler());
4392 __ Bind(fixup_label);
4393 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4394}
4395
4396template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4397inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4398 const ArenaDeque<PcRelativePatchInfo>& infos,
4399 ArenaVector<LinkerPatch>* linker_patches) {
4400 for (const PcRelativePatchInfo& info : infos) {
4401 linker_patches->push_back(Factory(info.label.GetLocation(),
4402 &info.target_dex_file,
4403 info.pc_insn_label->GetLocation(),
4404 info.offset_or_index));
4405 }
4406}
4407
Vladimir Marko58155012015-08-19 12:49:41 +00004408void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4409 DCHECK(linker_patches->empty());
4410 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004411 pc_relative_dex_cache_patches_.size() +
4412 boot_image_string_patches_.size() +
4413 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004414 boot_image_type_patches_.size() +
4415 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004416 type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004417 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004418 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004419 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004420 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004421 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004422 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004423 info.offset_or_index));
4424 }
4425 for (const auto& entry : boot_image_string_patches_) {
4426 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004427 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4428 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004429 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004430 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004431 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004432 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004433 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004434 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4435 linker_patches);
4436 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004437 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4438 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004439 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4440 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004441 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004442 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4443 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004444 for (const auto& entry : boot_image_type_patches_) {
4445 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004446 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4447 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004448 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004449 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004450 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004451 for (const auto& entry : boot_image_address_patches_) {
4452 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004453 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4454 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00004455 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004456 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004457}
4458
Scott Wakeling97c72b72016-06-24 16:19:36 +01004459vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004460 Uint32ToLiteralMap* map) {
4461 return map->GetOrCreate(
4462 value,
4463 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4464}
4465
Scott Wakeling97c72b72016-06-24 16:19:36 +01004466vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004467 return uint64_literals_.GetOrCreate(
4468 value,
4469 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004470}
4471
Scott Wakeling97c72b72016-06-24 16:19:36 +01004472vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004473 MethodReference target_method,
4474 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004475 return map->GetOrCreate(
4476 target_method,
4477 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004478}
4479
Andreas Gampe878d58c2015-01-15 23:24:00 -08004480void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004481 // Explicit clinit checks triggered by static invokes must have been pruned by
4482 // art::PrepareForRegisterAllocation.
4483 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004484
Andreas Gampe878d58c2015-01-15 23:24:00 -08004485 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4486 return;
4487 }
4488
Artem Serov914d7a82017-02-07 14:33:49 +00004489 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4490 // are no pools emitted.
4491 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004492 LocationSummary* locations = invoke->GetLocations();
4493 codegen_->GenerateStaticOrDirectCall(
4494 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004495 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004496}
4497
4498void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004499 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4500 return;
4501 }
4502
Artem Serov914d7a82017-02-07 14:33:49 +00004503 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4504 // are no pools emitted.
4505 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004506 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004507 DCHECK(!codegen_->IsLeafMethod());
4508 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4509}
4510
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004511HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4512 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004513 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004514 case HLoadClass::LoadKind::kInvalid:
4515 LOG(FATAL) << "UNREACHABLE";
4516 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004517 case HLoadClass::LoadKind::kReferrersClass:
4518 break;
4519 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4520 DCHECK(!GetCompilerOptions().GetCompilePic());
4521 break;
4522 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4523 DCHECK(GetCompilerOptions().GetCompilePic());
4524 break;
4525 case HLoadClass::LoadKind::kBootImageAddress:
4526 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004527 case HLoadClass::LoadKind::kBssEntry:
4528 DCHECK(!Runtime::Current()->UseJitCompilation());
4529 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004530 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004531 DCHECK(Runtime::Current()->UseJitCompilation());
4532 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004533 case HLoadClass::LoadKind::kDexCacheViaMethod:
4534 break;
4535 }
4536 return desired_class_load_kind;
4537}
4538
Alexandre Rames67555f72014-11-18 10:55:16 +00004539void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004540 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4541 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004542 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004543 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004544 cls,
4545 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004546 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004547 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004548 return;
4549 }
Vladimir Marko41559982017-01-06 14:04:23 +00004550 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004551
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004552 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4553 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004554 ? LocationSummary::kCallOnSlowPath
4555 : LocationSummary::kNoCall;
4556 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004557 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004558 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004559 }
4560
Vladimir Marko41559982017-01-06 14:04:23 +00004561 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004562 locations->SetInAt(0, Location::RequiresRegister());
4563 }
4564 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004565 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4566 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4567 // Rely on the type resolution or initialization and marking to save everything we need.
4568 // Note that IP0 may be clobbered by saving/restoring the live register (only one thanks
4569 // to the custom calling convention) or by marking, so we shall use IP1.
4570 RegisterSet caller_saves = RegisterSet::Empty();
4571 InvokeRuntimeCallingConvention calling_convention;
4572 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4573 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4574 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4575 Primitive::kPrimNot).GetCode());
4576 locations->SetCustomSlowPathCallerSaves(caller_saves);
4577 } else {
4578 // For non-Baker read barrier we have a temp-clobbering call.
4579 }
4580 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004581}
4582
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004583// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4584// move.
4585void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004586 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4587 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4588 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004589 return;
4590 }
Vladimir Marko41559982017-01-06 14:04:23 +00004591 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004592
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004593 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004594 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004595 Register bss_entry_temp;
4596 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004597
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004598 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4599 ? kWithoutReadBarrier
4600 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004601 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004602 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004603 case HLoadClass::LoadKind::kReferrersClass: {
4604 DCHECK(!cls->CanCallRuntime());
4605 DCHECK(!cls->MustGenerateClinitCheck());
4606 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4607 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004608 GenerateGcRootFieldLoad(cls,
4609 out_loc,
4610 current_method,
4611 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004612 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004613 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004614 break;
4615 }
4616 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004617 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004618 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4619 cls->GetTypeIndex()));
4620 break;
4621 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004622 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004623 // Add ADRP with its PC-relative type patch.
4624 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004625 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004626 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004627 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004628 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004629 vixl::aarch64::Label* add_label =
4630 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004631 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004632 break;
4633 }
4634 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004635 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004636 uint32_t address = dchecked_integral_cast<uint32_t>(
4637 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4638 DCHECK_NE(address, 0u);
4639 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004640 break;
4641 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004642 case HLoadClass::LoadKind::kBssEntry: {
4643 // Add ADRP with its PC-relative Class .bss entry patch.
4644 const DexFile& dex_file = cls->GetDexFile();
4645 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markoea4c1262017-02-06 19:59:33 +00004646 // We can go to slow path even with non-zero reference and in that case marking
4647 // can clobber IP0, so we need to use IP1 which shall be preserved.
4648 bss_entry_temp = ip1;
4649 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4650 temps.Exclude(bss_entry_temp);
4651 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4652 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004653 // Add LDR with its PC-relative Class patch.
4654 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004655 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004656 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4657 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004658 out_loc,
4659 bss_entry_temp,
4660 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004661 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004662 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004663 generate_null_check = true;
4664 break;
4665 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004666 case HLoadClass::LoadKind::kJitTableAddress: {
4667 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4668 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004669 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004670 GenerateGcRootFieldLoad(cls,
4671 out_loc,
4672 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004673 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004674 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004675 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004676 break;
4677 }
Vladimir Marko41559982017-01-06 14:04:23 +00004678 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004679 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004680 LOG(FATAL) << "UNREACHABLE";
4681 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004682 }
4683
Vladimir Markoea4c1262017-02-06 19:59:33 +00004684 bool do_clinit = cls->MustGenerateClinitCheck();
4685 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004686 DCHECK(cls->CanCallRuntime());
4687 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004688 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004689 codegen_->AddSlowPath(slow_path);
4690 if (generate_null_check) {
4691 __ Cbz(out, slow_path->GetEntryLabel());
4692 }
4693 if (cls->MustGenerateClinitCheck()) {
4694 GenerateClassInitializationCheck(slow_path, out);
4695 } else {
4696 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004697 }
4698 }
4699}
4700
David Brazdilcb1c0552015-08-04 16:22:25 +01004701static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004702 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004703}
4704
Alexandre Rames67555f72014-11-18 10:55:16 +00004705void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4706 LocationSummary* locations =
4707 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4708 locations->SetOut(Location::RequiresRegister());
4709}
4710
4711void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004712 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4713}
4714
4715void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4716 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4717}
4718
4719void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4720 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004721}
4722
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004723HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4724 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004725 switch (desired_string_load_kind) {
4726 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4727 DCHECK(!GetCompilerOptions().GetCompilePic());
4728 break;
4729 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4730 DCHECK(GetCompilerOptions().GetCompilePic());
4731 break;
4732 case HLoadString::LoadKind::kBootImageAddress:
4733 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004734 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004735 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004736 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004737 case HLoadString::LoadKind::kJitTableAddress:
4738 DCHECK(Runtime::Current()->UseJitCompilation());
4739 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004740 case HLoadString::LoadKind::kDexCacheViaMethod:
4741 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004742 }
4743 return desired_string_load_kind;
4744}
4745
Alexandre Rames67555f72014-11-18 10:55:16 +00004746void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004747 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004748 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004749 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004750 InvokeRuntimeCallingConvention calling_convention;
4751 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4752 } else {
4753 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004754 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
4755 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00004756 // Rely on the pResolveString and marking to save everything we need.
4757 // Note that IP0 may be clobbered by saving/restoring the live register (only one thanks
4758 // to the custom calling convention) or by marking, so we shall use IP1.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004759 RegisterSet caller_saves = RegisterSet::Empty();
4760 InvokeRuntimeCallingConvention calling_convention;
4761 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4762 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4763 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4764 Primitive::kPrimNot).GetCode());
4765 locations->SetCustomSlowPathCallerSaves(caller_saves);
4766 } else {
4767 // For non-Baker read barrier we have a temp-clobbering call.
4768 }
4769 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004770 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004771}
4772
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004773// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4774// move.
4775void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00004776 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004777 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004778
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004779 switch (load->GetLoadKind()) {
4780 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004781 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4782 load->GetStringIndex()));
4783 return; // No dex cache slow path.
4784 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004785 // Add ADRP with its PC-relative String patch.
4786 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004787 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004788 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01004789 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004790 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004791 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004792 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004793 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004794 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004795 return; // No dex cache slow path.
4796 }
4797 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004798 uint32_t address = dchecked_integral_cast<uint32_t>(
4799 reinterpret_cast<uintptr_t>(load->GetString().Get()));
4800 DCHECK_NE(address, 0u);
4801 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004802 return; // No dex cache slow path.
4803 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004804 case HLoadString::LoadKind::kBssEntry: {
4805 // Add ADRP with its PC-relative String .bss entry patch.
4806 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004807 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00004808 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004809 // We could use IP0 as the marking shall not clobber IP0 if the reference is null and
4810 // that's when we need the slow path. But let's not rely on such details and use IP1.
4811 Register temp = ip1;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004812 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004813 temps.Exclude(temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004814 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004815 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004816 // Add LDR with its PC-relative String patch.
4817 vixl::aarch64::Label* ldr_label =
4818 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004819 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00004820 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004821 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004822 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01004823 /* offset placeholder */ 0u,
4824 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004825 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004826 SlowPathCodeARM64* slow_path =
4827 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004828 codegen_->AddSlowPath(slow_path);
4829 __ Cbz(out.X(), slow_path->GetEntryLabel());
4830 __ Bind(slow_path->GetExitLabel());
4831 return;
4832 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004833 case HLoadString::LoadKind::kJitTableAddress: {
4834 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004835 load->GetStringIndex(),
4836 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004837 GenerateGcRootFieldLoad(load,
4838 out_loc,
4839 out.X(),
4840 /* offset */ 0,
4841 /* fixup_label */ nullptr,
4842 kCompilerReadBarrierOption);
4843 return;
4844 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004845 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004846 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004847 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004848
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004849 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004850 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01004851 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004852 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07004853 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4854 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004855}
4856
Alexandre Rames5319def2014-10-23 10:03:10 +01004857void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4858 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4859 locations->SetOut(Location::ConstantLocation(constant));
4860}
4861
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004862void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004863 // Will be generated at use site.
4864}
4865
Alexandre Rames67555f72014-11-18 10:55:16 +00004866void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4867 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004868 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004869 InvokeRuntimeCallingConvention calling_convention;
4870 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4871}
4872
4873void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004874 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00004875 instruction,
4876 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004877 if (instruction->IsEnter()) {
4878 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4879 } else {
4880 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4881 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004882}
4883
Alexandre Rames42d641b2014-10-27 14:00:51 +00004884void LocationsBuilderARM64::VisitMul(HMul* mul) {
4885 LocationSummary* locations =
4886 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4887 switch (mul->GetResultType()) {
4888 case Primitive::kPrimInt:
4889 case Primitive::kPrimLong:
4890 locations->SetInAt(0, Location::RequiresRegister());
4891 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004892 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004893 break;
4894
4895 case Primitive::kPrimFloat:
4896 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004897 locations->SetInAt(0, Location::RequiresFpuRegister());
4898 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004899 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004900 break;
4901
4902 default:
4903 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4904 }
4905}
4906
4907void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4908 switch (mul->GetResultType()) {
4909 case Primitive::kPrimInt:
4910 case Primitive::kPrimLong:
4911 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4912 break;
4913
4914 case Primitive::kPrimFloat:
4915 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004916 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004917 break;
4918
4919 default:
4920 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4921 }
4922}
4923
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004924void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4925 LocationSummary* locations =
4926 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4927 switch (neg->GetResultType()) {
4928 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004929 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004930 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004931 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004932 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004933
4934 case Primitive::kPrimFloat:
4935 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004936 locations->SetInAt(0, Location::RequiresFpuRegister());
4937 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004938 break;
4939
4940 default:
4941 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4942 }
4943}
4944
4945void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4946 switch (neg->GetResultType()) {
4947 case Primitive::kPrimInt:
4948 case Primitive::kPrimLong:
4949 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4950 break;
4951
4952 case Primitive::kPrimFloat:
4953 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004954 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004955 break;
4956
4957 default:
4958 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4959 }
4960}
4961
4962void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4963 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004964 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004965 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004966 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004967 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4968 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004969}
4970
4971void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004972 // Note: if heap poisoning is enabled, the entry point takes cares
4973 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00004974 QuickEntrypointEnum entrypoint =
4975 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4976 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004977 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004978}
4979
Alexandre Rames5319def2014-10-23 10:03:10 +01004980void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4981 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004982 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004983 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004984 if (instruction->IsStringAlloc()) {
4985 locations->AddTemp(LocationFrom(kArtMethodRegister));
4986 } else {
4987 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004988 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004989 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4990}
4991
4992void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004993 // Note: if heap poisoning is enabled, the entry point takes cares
4994 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004995 if (instruction->IsStringAlloc()) {
4996 // String is allocated through StringFactory. Call NewEmptyString entry point.
4997 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004998 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004999 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5000 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005001
5002 {
5003 // Ensure the pc position is recorded immediately after the `blr` instruction.
5004 ExactAssemblyScope eas(GetVIXLAssembler(),
5005 kInstructionSize,
5006 CodeBufferCheckScope::kExactSize);
5007 __ blr(lr);
5008 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5009 }
David Brazdil6de19382016-01-08 17:37:10 +00005010 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005011 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005012 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005013 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005014}
5015
5016void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5017 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005018 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005019 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005020}
5021
5022void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005023 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005024 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005025 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005026 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005027 break;
5028
5029 default:
5030 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5031 }
5032}
5033
David Brazdil66d126e2015-04-03 16:02:44 +01005034void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5035 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5036 locations->SetInAt(0, Location::RequiresRegister());
5037 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5038}
5039
5040void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005041 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005042}
5043
Alexandre Rames5319def2014-10-23 10:03:10 +01005044void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005045 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5046 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005047}
5048
Calin Juravle2ae48182016-03-16 14:05:09 +00005049void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5050 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005051 return;
5052 }
Artem Serov914d7a82017-02-07 14:33:49 +00005053 {
5054 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5055 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5056 Location obj = instruction->GetLocations()->InAt(0);
5057 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5058 RecordPcInfo(instruction, instruction->GetDexPc());
5059 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005060}
5061
Calin Juravle2ae48182016-03-16 14:05:09 +00005062void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005063 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005064 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005065
5066 LocationSummary* locations = instruction->GetLocations();
5067 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005068
5069 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005070}
5071
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005072void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005073 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005074}
5075
Alexandre Rames67555f72014-11-18 10:55:16 +00005076void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5077 HandleBinaryOp(instruction);
5078}
5079
5080void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5081 HandleBinaryOp(instruction);
5082}
5083
Alexandre Rames3e69f162014-12-10 10:36:50 +00005084void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5085 LOG(FATAL) << "Unreachable";
5086}
5087
5088void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5089 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5090}
5091
Alexandre Rames5319def2014-10-23 10:03:10 +01005092void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5093 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5094 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5095 if (location.IsStackSlot()) {
5096 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5097 } else if (location.IsDoubleStackSlot()) {
5098 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5099 }
5100 locations->SetOut(location);
5101}
5102
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005103void InstructionCodeGeneratorARM64::VisitParameterValue(
5104 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005105 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005106}
5107
5108void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5109 LocationSummary* locations =
5110 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005111 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005112}
5113
5114void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5115 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5116 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005117}
5118
5119void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5120 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005121 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005122 locations->SetInAt(i, Location::Any());
5123 }
5124 locations->SetOut(Location::Any());
5125}
5126
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005127void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005128 LOG(FATAL) << "Unreachable";
5129}
5130
Serban Constantinescu02164b32014-11-13 14:05:07 +00005131void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005132 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005133 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005134 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5135 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005136 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5137
5138 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005139 case Primitive::kPrimInt:
5140 case Primitive::kPrimLong:
5141 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005142 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005143 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5144 break;
5145
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005146 case Primitive::kPrimFloat:
5147 case Primitive::kPrimDouble: {
5148 InvokeRuntimeCallingConvention calling_convention;
5149 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5150 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5151 locations->SetOut(calling_convention.GetReturnLocation(type));
5152
5153 break;
5154 }
5155
Serban Constantinescu02164b32014-11-13 14:05:07 +00005156 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005157 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005158 }
5159}
5160
5161void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5162 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005163
Serban Constantinescu02164b32014-11-13 14:05:07 +00005164 switch (type) {
5165 case Primitive::kPrimInt:
5166 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005167 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005168 break;
5169 }
5170
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005171 case Primitive::kPrimFloat:
5172 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005173 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5174 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005175 if (type == Primitive::kPrimFloat) {
5176 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5177 } else {
5178 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5179 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005180 break;
5181 }
5182
Serban Constantinescu02164b32014-11-13 14:05:07 +00005183 default:
5184 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005185 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005186 }
5187}
5188
Calin Juravle27df7582015-04-17 19:12:31 +01005189void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5190 memory_barrier->SetLocations(nullptr);
5191}
5192
5193void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005194 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005195}
5196
Alexandre Rames5319def2014-10-23 10:03:10 +01005197void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5198 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5199 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005200 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005201}
5202
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005203void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005204 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005205}
5206
5207void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5208 instruction->SetLocations(nullptr);
5209}
5210
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005211void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005212 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005213}
5214
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005215void LocationsBuilderARM64::VisitRor(HRor* ror) {
5216 HandleBinaryOp(ror);
5217}
5218
5219void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5220 HandleBinaryOp(ror);
5221}
5222
Serban Constantinescu02164b32014-11-13 14:05:07 +00005223void LocationsBuilderARM64::VisitShl(HShl* shl) {
5224 HandleShift(shl);
5225}
5226
5227void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5228 HandleShift(shl);
5229}
5230
5231void LocationsBuilderARM64::VisitShr(HShr* shr) {
5232 HandleShift(shr);
5233}
5234
5235void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5236 HandleShift(shr);
5237}
5238
Alexandre Rames5319def2014-10-23 10:03:10 +01005239void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005240 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005241}
5242
5243void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005244 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005245}
5246
Alexandre Rames67555f72014-11-18 10:55:16 +00005247void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005248 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00005249}
5250
5251void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005252 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005253}
5254
5255void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005256 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005257}
5258
Alexandre Rames67555f72014-11-18 10:55:16 +00005259void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005260 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005261}
5262
Calin Juravlee460d1d2015-09-29 04:52:17 +01005263void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5264 HUnresolvedInstanceFieldGet* instruction) {
5265 FieldAccessCallingConventionARM64 calling_convention;
5266 codegen_->CreateUnresolvedFieldLocationSummary(
5267 instruction, instruction->GetFieldType(), calling_convention);
5268}
5269
5270void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5271 HUnresolvedInstanceFieldGet* instruction) {
5272 FieldAccessCallingConventionARM64 calling_convention;
5273 codegen_->GenerateUnresolvedFieldAccess(instruction,
5274 instruction->GetFieldType(),
5275 instruction->GetFieldIndex(),
5276 instruction->GetDexPc(),
5277 calling_convention);
5278}
5279
5280void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5281 HUnresolvedInstanceFieldSet* instruction) {
5282 FieldAccessCallingConventionARM64 calling_convention;
5283 codegen_->CreateUnresolvedFieldLocationSummary(
5284 instruction, instruction->GetFieldType(), calling_convention);
5285}
5286
5287void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5288 HUnresolvedInstanceFieldSet* instruction) {
5289 FieldAccessCallingConventionARM64 calling_convention;
5290 codegen_->GenerateUnresolvedFieldAccess(instruction,
5291 instruction->GetFieldType(),
5292 instruction->GetFieldIndex(),
5293 instruction->GetDexPc(),
5294 calling_convention);
5295}
5296
5297void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5298 HUnresolvedStaticFieldGet* instruction) {
5299 FieldAccessCallingConventionARM64 calling_convention;
5300 codegen_->CreateUnresolvedFieldLocationSummary(
5301 instruction, instruction->GetFieldType(), calling_convention);
5302}
5303
5304void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5305 HUnresolvedStaticFieldGet* instruction) {
5306 FieldAccessCallingConventionARM64 calling_convention;
5307 codegen_->GenerateUnresolvedFieldAccess(instruction,
5308 instruction->GetFieldType(),
5309 instruction->GetFieldIndex(),
5310 instruction->GetDexPc(),
5311 calling_convention);
5312}
5313
5314void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5315 HUnresolvedStaticFieldSet* instruction) {
5316 FieldAccessCallingConventionARM64 calling_convention;
5317 codegen_->CreateUnresolvedFieldLocationSummary(
5318 instruction, instruction->GetFieldType(), calling_convention);
5319}
5320
5321void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5322 HUnresolvedStaticFieldSet* instruction) {
5323 FieldAccessCallingConventionARM64 calling_convention;
5324 codegen_->GenerateUnresolvedFieldAccess(instruction,
5325 instruction->GetFieldType(),
5326 instruction->GetFieldIndex(),
5327 instruction->GetDexPc(),
5328 calling_convention);
5329}
5330
Alexandre Rames5319def2014-10-23 10:03:10 +01005331void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005332 LocationSummary* locations =
5333 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005334 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexandre Rames5319def2014-10-23 10:03:10 +01005335}
5336
5337void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005338 HBasicBlock* block = instruction->GetBlock();
5339 if (block->GetLoopInformation() != nullptr) {
5340 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5341 // The back edge will generate the suspend check.
5342 return;
5343 }
5344 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5345 // The goto will generate the suspend check.
5346 return;
5347 }
5348 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005349}
5350
Alexandre Rames67555f72014-11-18 10:55:16 +00005351void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5352 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005353 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005354 InvokeRuntimeCallingConvention calling_convention;
5355 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5356}
5357
5358void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005359 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005360 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005361}
5362
5363void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5364 LocationSummary* locations =
5365 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5366 Primitive::Type input_type = conversion->GetInputType();
5367 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005368 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005369 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5370 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5371 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5372 }
5373
Alexandre Rames542361f2015-01-29 16:57:31 +00005374 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005375 locations->SetInAt(0, Location::RequiresFpuRegister());
5376 } else {
5377 locations->SetInAt(0, Location::RequiresRegister());
5378 }
5379
Alexandre Rames542361f2015-01-29 16:57:31 +00005380 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005381 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5382 } else {
5383 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5384 }
5385}
5386
5387void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5388 Primitive::Type result_type = conversion->GetResultType();
5389 Primitive::Type input_type = conversion->GetInputType();
5390
5391 DCHECK_NE(input_type, result_type);
5392
Alexandre Rames542361f2015-01-29 16:57:31 +00005393 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005394 int result_size = Primitive::ComponentSize(result_type);
5395 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005396 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005397 Register output = OutputRegister(conversion);
5398 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005399 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005400 // 'int' values are used directly as W registers, discarding the top
5401 // bits, so we don't need to sign-extend and can just perform a move.
5402 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5403 // top 32 bits of the target register. We theoretically could leave those
5404 // bits unchanged, but we would have to make sure that no code uses a
5405 // 32bit input value as a 64bit value assuming that the top 32 bits are
5406 // zero.
5407 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005408 } else if (result_type == Primitive::kPrimChar ||
5409 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5410 __ Ubfx(output,
5411 output.IsX() ? source.X() : source.W(),
5412 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005413 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005414 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005415 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005416 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005417 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005418 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005419 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5420 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005421 } else if (Primitive::IsFloatingPointType(result_type) &&
5422 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005423 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5424 } else {
5425 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5426 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005427 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005428}
Alexandre Rames67555f72014-11-18 10:55:16 +00005429
Serban Constantinescu02164b32014-11-13 14:05:07 +00005430void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5431 HandleShift(ushr);
5432}
5433
5434void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5435 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005436}
5437
5438void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5439 HandleBinaryOp(instruction);
5440}
5441
5442void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5443 HandleBinaryOp(instruction);
5444}
5445
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005446void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005447 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005448 LOG(FATAL) << "Unreachable";
5449}
5450
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005451void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005452 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005453 LOG(FATAL) << "Unreachable";
5454}
5455
Mark Mendellfe57faa2015-09-18 09:26:15 -04005456// Simple implementation of packed switch - generate cascaded compare/jumps.
5457void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5458 LocationSummary* locations =
5459 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5460 locations->SetInAt(0, Location::RequiresRegister());
5461}
5462
5463void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5464 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005465 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005466 Register value_reg = InputRegisterAt(switch_instr, 0);
5467 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5468
Zheng Xu3927c8b2015-11-18 17:46:25 +08005469 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005470 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005471 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5472 // make sure we don't emit it if the target may run out of range.
5473 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5474 // ranges and emit the tables only as required.
5475 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005476
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005477 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005478 // Current instruction id is an upper bound of the number of HIRs in the graph.
5479 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5480 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005481 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5482 Register temp = temps.AcquireW();
5483 __ Subs(temp, value_reg, Operand(lower_bound));
5484
Zheng Xu3927c8b2015-11-18 17:46:25 +08005485 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005486 // Jump to successors[0] if value == lower_bound.
5487 __ B(eq, codegen_->GetLabelOf(successors[0]));
5488 int32_t last_index = 0;
5489 for (; num_entries - last_index > 2; last_index += 2) {
5490 __ Subs(temp, temp, Operand(2));
5491 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5492 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5493 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5494 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5495 }
5496 if (num_entries - last_index == 2) {
5497 // The last missing case_value.
5498 __ Cmp(temp, Operand(1));
5499 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005500 }
5501
5502 // And the default for any other value.
5503 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5504 __ B(codegen_->GetLabelOf(default_block));
5505 }
5506 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005507 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005508
5509 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5510
5511 // Below instructions should use at most one blocked register. Since there are two blocked
5512 // registers, we are free to block one.
5513 Register temp_w = temps.AcquireW();
5514 Register index;
5515 // Remove the bias.
5516 if (lower_bound != 0) {
5517 index = temp_w;
5518 __ Sub(index, value_reg, Operand(lower_bound));
5519 } else {
5520 index = value_reg;
5521 }
5522
5523 // Jump to default block if index is out of the range.
5524 __ Cmp(index, Operand(num_entries));
5525 __ B(hs, codegen_->GetLabelOf(default_block));
5526
5527 // In current VIXL implementation, it won't require any blocked registers to encode the
5528 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5529 // register pressure.
5530 Register table_base = temps.AcquireX();
5531 // Load jump offset from the table.
5532 __ Adr(table_base, jump_table->GetTableStartLabel());
5533 Register jump_offset = temp_w;
5534 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5535
5536 // Jump to target block by branching to table_base(pc related) + offset.
5537 Register target_address = table_base;
5538 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5539 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005540 }
5541}
5542
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005543void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5544 HInstruction* instruction,
5545 Location out,
5546 uint32_t offset,
5547 Location maybe_temp,
5548 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005549 Primitive::Type type = Primitive::kPrimNot;
5550 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005551 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005552 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005553 Register temp_reg = RegisterFrom(maybe_temp, type);
5554 if (kUseBakerReadBarrier) {
5555 // Load with fast path based Baker's read barrier.
5556 // /* HeapReference<Object> */ out = *(out + offset)
5557 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5558 out,
5559 out_reg,
5560 offset,
5561 temp_reg,
5562 /* needs_null_check */ false,
5563 /* use_load_acquire */ false);
5564 } else {
5565 // Load with slow path based read barrier.
5566 // Save the value of `out` into `maybe_temp` before overwriting it
5567 // in the following move operation, as we will need it for the
5568 // read barrier below.
5569 __ Mov(temp_reg, out_reg);
5570 // /* HeapReference<Object> */ out = *(out + offset)
5571 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5572 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5573 }
5574 } else {
5575 // Plain load with no read barrier.
5576 // /* HeapReference<Object> */ out = *(out + offset)
5577 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5578 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5579 }
5580}
5581
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005582void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5583 HInstruction* instruction,
5584 Location out,
5585 Location obj,
5586 uint32_t offset,
5587 Location maybe_temp,
5588 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005589 Primitive::Type type = Primitive::kPrimNot;
5590 Register out_reg = RegisterFrom(out, type);
5591 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005592 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005593 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005594 if (kUseBakerReadBarrier) {
5595 // Load with fast path based Baker's read barrier.
5596 Register temp_reg = RegisterFrom(maybe_temp, type);
5597 // /* HeapReference<Object> */ out = *(obj + offset)
5598 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5599 out,
5600 obj_reg,
5601 offset,
5602 temp_reg,
5603 /* needs_null_check */ false,
5604 /* use_load_acquire */ false);
5605 } else {
5606 // Load with slow path based read barrier.
5607 // /* HeapReference<Object> */ out = *(obj + offset)
5608 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5609 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5610 }
5611 } else {
5612 // Plain load with no read barrier.
5613 // /* HeapReference<Object> */ out = *(obj + offset)
5614 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5615 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5616 }
5617}
5618
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005619void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5620 HInstruction* instruction,
5621 Location root,
5622 Register obj,
5623 uint32_t offset,
5624 vixl::aarch64::Label* fixup_label,
5625 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005626 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005627 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005628 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005629 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005630 if (kUseBakerReadBarrier) {
5631 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005632 // Baker's read barrier are used.
Roland Levillain44015862016-01-22 11:47:17 +00005633 //
Roland Levillainba650a42017-03-06 13:52:32 +00005634 // Note that we do not actually check the value of
5635 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5636 // root or not. Instead, we load into `temp` the read barrier
5637 // mark entry point corresponding to register `root`. If `temp`
5638 // is null, it means that `GetIsGcMarking()` is false, and vice
5639 // versa.
5640 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005641 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00005642 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5643 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5644 // // Slow path.
5645 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
Roland Levillain44015862016-01-22 11:47:17 +00005646 // }
5647
Roland Levillainba650a42017-03-06 13:52:32 +00005648 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
5649 Register temp = lr;
5650 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
5651 instruction, root, /* entrypoint */ LocationFrom(temp));
5652 codegen_->AddSlowPath(slow_path);
5653
5654 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5655 const int32_t entry_point_offset =
5656 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5657 // Loading the entrypoint does not require a load acquire since it is only changed when
5658 // threads are suspended or running a checkpoint.
5659 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5660
Roland Levillain44015862016-01-22 11:47:17 +00005661 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005662 if (fixup_label == nullptr) {
5663 __ Ldr(root_reg, MemOperand(obj, offset));
5664 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005665 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005666 }
Roland Levillain44015862016-01-22 11:47:17 +00005667 static_assert(
5668 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5669 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5670 "have different sizes.");
5671 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5672 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5673 "have different sizes.");
5674
Mathieu Chartierfe814e82016-11-09 14:32:49 -08005675 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5676 // checking GetIsGcMarking.
Roland Levillain44015862016-01-22 11:47:17 +00005677 __ Cbnz(temp, slow_path->GetEntryLabel());
5678 __ Bind(slow_path->GetExitLabel());
5679 } else {
5680 // GC root loaded through a slow path for read barriers other
5681 // than Baker's.
5682 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005683 if (fixup_label == nullptr) {
5684 __ Add(root_reg.X(), obj.X(), offset);
5685 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005686 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005687 }
Roland Levillain44015862016-01-22 11:47:17 +00005688 // /* mirror::Object* */ root = root->Read()
5689 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5690 }
5691 } else {
5692 // Plain GC root load with no read barrier.
5693 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005694 if (fixup_label == nullptr) {
5695 __ Ldr(root_reg, MemOperand(obj, offset));
5696 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005697 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005698 }
Roland Levillain44015862016-01-22 11:47:17 +00005699 // Note that GC roots are not affected by heap poisoning, thus we
5700 // do not have to unpoison `root_reg` here.
5701 }
5702}
5703
5704void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5705 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005706 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005707 uint32_t offset,
5708 Register temp,
5709 bool needs_null_check,
5710 bool use_load_acquire) {
5711 DCHECK(kEmitCompilerReadBarrier);
5712 DCHECK(kUseBakerReadBarrier);
5713
5714 // /* HeapReference<Object> */ ref = *(obj + offset)
5715 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005716 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01005717 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5718 ref,
5719 obj,
5720 offset,
5721 no_index,
5722 no_scale_factor,
5723 temp,
5724 needs_null_check,
5725 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005726}
5727
5728void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5729 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005730 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005731 uint32_t data_offset,
5732 Location index,
5733 Register temp,
5734 bool needs_null_check) {
5735 DCHECK(kEmitCompilerReadBarrier);
5736 DCHECK(kUseBakerReadBarrier);
5737
5738 // Array cells are never volatile variables, therefore array loads
5739 // never use Load-Acquire instructions on ARM64.
5740 const bool use_load_acquire = false;
5741
Roland Levillainbfea3352016-06-23 13:48:47 +01005742 static_assert(
5743 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5744 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005745 // /* HeapReference<Object> */ ref =
5746 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005747 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5748 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5749 ref,
5750 obj,
5751 data_offset,
5752 index,
5753 scale_factor,
5754 temp,
5755 needs_null_check,
5756 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005757}
5758
5759void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5760 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005761 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005762 uint32_t offset,
5763 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005764 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005765 Register temp,
5766 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005767 bool use_load_acquire,
5768 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00005769 DCHECK(kEmitCompilerReadBarrier);
5770 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005771 // If we are emitting an array load, we should not be using a
5772 // Load Acquire instruction. In other words:
5773 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5774 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005775
Roland Levillainba650a42017-03-06 13:52:32 +00005776 // After loading the reference from `obj.field` into `ref`, query
5777 // `art::Thread::Current()->GetIsGcMarking()` to decide whether we
5778 // need to enter the slow path to mark the reference. This
5779 // optimistic strategy (we expect the GC to not be marking most of
5780 // the time) does not check `obj`'s lock word (to see if it is a
5781 // gray object or not), so may sometimes mark an already marked
5782 // object.
Roland Levillain44015862016-01-22 11:47:17 +00005783 //
Roland Levillainba650a42017-03-06 13:52:32 +00005784 // Note that we do not actually check the value of `GetIsGcMarking()`;
5785 // instead, we load into `temp2` the read barrier mark entry point
5786 // corresponding to register `ref`. If `temp2` is null, it means
5787 // that `GetIsGcMarking()` is false, and vice versa.
5788 //
5789 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5790 // HeapReference<mirror::Object> ref = *src; // Original reference load.
5791 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5792 // // Slow path.
5793 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillain44015862016-01-22 11:47:17 +00005794 // }
Roland Levillain44015862016-01-22 11:47:17 +00005795
Roland Levillainba650a42017-03-06 13:52:32 +00005796 // Slow path marking the object `ref` when the GC is marking. The
5797 // entrypoint will already be loaded in `temp2`.
5798 Register temp2 = lr;
5799 Location temp2_loc = LocationFrom(temp2);
5800 SlowPathCodeARM64* slow_path;
5801 if (always_update_field) {
5802 // ReadBarrierMarkAndUpdateFieldSlowPathARM64 only supports
5803 // address of the form `obj + field_offset`, where `obj` is a
5804 // register and `field_offset` is a register. Thus `offset` and
5805 // `scale_factor` above are expected to be null in this code path.
5806 DCHECK_EQ(offset, 0u);
5807 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
5808 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM64(
5809 instruction, ref, obj, /* field_offset */ index, temp, /* entrypoint */ temp2_loc);
5810 } else {
5811 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
5812 instruction, ref, /* entrypoint */ temp2_loc);
5813 }
5814 AddSlowPath(slow_path);
5815
5816 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
5817 const int32_t entry_point_offset =
5818 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
5819 // Loading the entrypoint does not require a load acquire since it is only changed when
5820 // threads are suspended or running a checkpoint.
5821 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
5822 // The reference load.
5823 GenerateRawReferenceLoad(
5824 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
5825 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5826 // checking GetIsGcMarking.
5827 __ Cbnz(temp2, slow_path->GetEntryLabel());
5828 __ Bind(slow_path->GetExitLabel());
5829}
5830
5831void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
5832 Location ref,
5833 Register obj,
5834 uint32_t offset,
5835 Location index,
5836 size_t scale_factor,
5837 bool needs_null_check,
5838 bool use_load_acquire) {
5839 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00005840 Primitive::Type type = Primitive::kPrimNot;
5841 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00005842
Roland Levillainba650a42017-03-06 13:52:32 +00005843 // If needed, vixl::EmissionCheckScope guards are used to ensure
5844 // that no pools are emitted between the load (macro) instruction
5845 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00005846
Roland Levillain44015862016-01-22 11:47:17 +00005847 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005848 // Load types involving an "index": ArrayGet,
5849 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5850 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01005851 if (use_load_acquire) {
5852 // UnsafeGetObjectVolatile intrinsic case.
5853 // Register `index` is not an index in an object array, but an
5854 // offset to an object reference field within object `obj`.
5855 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5856 DCHECK(instruction->GetLocations()->Intrinsified());
5857 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5858 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01005859 DCHECK_EQ(offset, 0u);
5860 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00005861 DCHECK_EQ(needs_null_check, false);
5862 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01005863 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5864 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005865 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00005866 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
5867 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01005868 if (index.IsConstant()) {
5869 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00005870 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01005871 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00005872 if (needs_null_check) {
5873 MaybeRecordImplicitNullCheck(instruction);
5874 }
Roland Levillainbfea3352016-06-23 13:48:47 +01005875 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00005876 UseScratchRegisterScope temps(GetVIXLAssembler());
5877 Register temp = temps.AcquireW();
5878 __ Add(temp, obj, offset);
5879 {
5880 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5881 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
5882 if (needs_null_check) {
5883 MaybeRecordImplicitNullCheck(instruction);
5884 }
5885 }
Roland Levillainbfea3352016-06-23 13:48:47 +01005886 }
Roland Levillain44015862016-01-22 11:47:17 +00005887 }
Roland Levillain44015862016-01-22 11:47:17 +00005888 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00005889 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00005890 MemOperand field = HeapOperand(obj, offset);
5891 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00005892 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
5893 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00005894 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00005895 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00005896 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00005897 if (needs_null_check) {
5898 MaybeRecordImplicitNullCheck(instruction);
5899 }
Roland Levillain44015862016-01-22 11:47:17 +00005900 }
5901 }
5902
5903 // Object* ref = ref_addr->AsMirrorPtr()
5904 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00005905}
5906
5907void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5908 Location out,
5909 Location ref,
5910 Location obj,
5911 uint32_t offset,
5912 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005913 DCHECK(kEmitCompilerReadBarrier);
5914
Roland Levillain44015862016-01-22 11:47:17 +00005915 // Insert a slow path based read barrier *after* the reference load.
5916 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005917 // If heap poisoning is enabled, the unpoisoning of the loaded
5918 // reference will be carried out by the runtime within the slow
5919 // path.
5920 //
5921 // Note that `ref` currently does not get unpoisoned (when heap
5922 // poisoning is enabled), which is alright as the `ref` argument is
5923 // not used by the artReadBarrierSlow entry point.
5924 //
5925 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5926 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5927 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5928 AddSlowPath(slow_path);
5929
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005930 __ B(slow_path->GetEntryLabel());
5931 __ Bind(slow_path->GetExitLabel());
5932}
5933
Roland Levillain44015862016-01-22 11:47:17 +00005934void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5935 Location out,
5936 Location ref,
5937 Location obj,
5938 uint32_t offset,
5939 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005940 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005941 // Baker's read barriers shall be handled by the fast path
5942 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5943 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005944 // If heap poisoning is enabled, unpoisoning will be taken care of
5945 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005946 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005947 } else if (kPoisonHeapReferences) {
5948 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5949 }
5950}
5951
Roland Levillain44015862016-01-22 11:47:17 +00005952void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5953 Location out,
5954 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005955 DCHECK(kEmitCompilerReadBarrier);
5956
Roland Levillain44015862016-01-22 11:47:17 +00005957 // Insert a slow path based read barrier *after* the GC root load.
5958 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005959 // Note that GC roots are not affected by heap poisoning, so we do
5960 // not need to do anything special for this here.
5961 SlowPathCodeARM64* slow_path =
5962 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5963 AddSlowPath(slow_path);
5964
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005965 __ B(slow_path->GetEntryLabel());
5966 __ Bind(slow_path->GetExitLabel());
5967}
5968
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005969void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5970 LocationSummary* locations =
5971 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5972 locations->SetInAt(0, Location::RequiresRegister());
5973 locations->SetOut(Location::RequiresRegister());
5974}
5975
5976void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5977 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005978 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005979 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005980 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005981 __ Ldr(XRegisterFrom(locations->Out()),
5982 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005983 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005984 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005985 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005986 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5987 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005988 __ Ldr(XRegisterFrom(locations->Out()),
5989 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005990 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005991}
5992
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005993static void PatchJitRootUse(uint8_t* code,
5994 const uint8_t* roots_data,
5995 vixl::aarch64::Literal<uint32_t>* literal,
5996 uint64_t index_in_table) {
5997 uint32_t literal_offset = literal->GetOffset();
5998 uintptr_t address =
5999 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6000 uint8_t* data = code + literal_offset;
6001 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6002}
6003
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006004void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6005 for (const auto& entry : jit_string_patches_) {
6006 const auto& it = jit_string_roots_.find(entry.first);
6007 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006008 PatchJitRootUse(code, roots_data, entry.second, it->second);
6009 }
6010 for (const auto& entry : jit_class_patches_) {
6011 const auto& it = jit_class_roots_.find(entry.first);
6012 DCHECK(it != jit_class_roots_.end());
6013 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006014 }
6015}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006016
Alexandre Rames67555f72014-11-18 10:55:16 +00006017#undef __
6018#undef QUICK_ENTRY_POINT
6019
Alexandre Rames5319def2014-10-23 10:03:10 +01006020} // namespace arm64
6021} // namespace art