blob: 3d93553059fcb09b2c30fe0f09da98355cf5979f [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080022#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080025#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027#include "intrinsics.h"
28#include "intrinsics_arm64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000029#include "linker/arm64/relative_patcher_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000032#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010033#include "thread.h"
34#include "utils/arm64/assembler_arm64.h"
35#include "utils/assembler.h"
36#include "utils/stack_checks.h"
37
Scott Wakeling97c72b72016-06-24 16:19:36 +010038using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000039using vixl::ExactAssemblyScope;
40using vixl::CodeBufferCheckScope;
41using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010042
43#ifdef __
44#error "ARM64 Codegen VIXL macro-assembler macro already defined."
45#endif
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace art {
48
Roland Levillain22ccc3a2015-11-24 13:10:05 +000049template<class MirrorType>
50class GcRoot;
51
Alexandre Rames5319def2014-10-23 10:03:10 +010052namespace arm64 {
53
Alexandre Ramesbe919d92016-08-23 18:33:36 +010054using helpers::ARM64EncodableConstantOrRegister;
55using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080056using helpers::CPURegisterFrom;
57using helpers::DRegisterFrom;
58using helpers::FPRegisterFrom;
59using helpers::HeapOperand;
60using helpers::HeapOperandFrom;
61using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080064using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010065using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080066using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080068using helpers::LocationFrom;
69using helpers::OperandFromMemOperand;
70using helpers::OutputCPURegister;
71using helpers::OutputFPRegister;
72using helpers::OutputRegister;
73using helpers::RegisterFrom;
74using helpers::StackOperandFrom;
75using helpers::VIXLRegCodeFromART;
76using helpers::WRegisterFrom;
77using helpers::XRegisterFrom;
78
Alexandre Rames5319def2014-10-23 10:03:10 +010079static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000080// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080081// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
82// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000083static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010084
Vladimir Markof4f2daa2017-03-20 18:26:59 +000085// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
86// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
87// For the Baker read barrier implementation using link-generated thunks we need to split
88// the offset explicitly.
89constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
90
91// Flags controlling the use of link-time generated thunks for Baker read barriers.
92constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
93constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
94
95// Some instructions have special requirements for a temporary, for example
96// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
97// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
98// loads with large offsets need a fixed register to limit the number of link-time
99// thunks we generate. For these and similar cases, we want to reserve a specific
100// register that's neither callee-save nor an argument register. We choose x15.
101inline Location FixedTempLocation() {
102 return Location::RegisterLocation(x15.GetCode());
103}
104
Alexandre Rames5319def2014-10-23 10:03:10 +0100105inline Condition ARM64Condition(IfCondition cond) {
106 switch (cond) {
107 case kCondEQ: return eq;
108 case kCondNE: return ne;
109 case kCondLT: return lt;
110 case kCondLE: return le;
111 case kCondGT: return gt;
112 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700113 case kCondB: return lo;
114 case kCondBE: return ls;
115 case kCondA: return hi;
116 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100117 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000118 LOG(FATAL) << "Unreachable";
119 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100120}
121
Vladimir Markod6e069b2016-01-18 11:11:01 +0000122inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
123 // The ARM64 condition codes can express all the necessary branches, see the
124 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
125 // There is no dex instruction or HIR that would need the missing conditions
126 // "equal or unordered" or "not equal".
127 switch (cond) {
128 case kCondEQ: return eq;
129 case kCondNE: return ne /* unordered */;
130 case kCondLT: return gt_bias ? cc : lt /* unordered */;
131 case kCondLE: return gt_bias ? ls : le /* unordered */;
132 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
133 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
134 default:
135 LOG(FATAL) << "UNREACHABLE";
136 UNREACHABLE();
137 }
138}
139
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000140Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000141 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
142 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
143 // but we use the exact registers for clarity.
144 if (return_type == Primitive::kPrimFloat) {
145 return LocationFrom(s0);
146 } else if (return_type == Primitive::kPrimDouble) {
147 return LocationFrom(d0);
148 } else if (return_type == Primitive::kPrimLong) {
149 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100150 } else if (return_type == Primitive::kPrimVoid) {
151 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000152 } else {
153 return LocationFrom(w0);
154 }
155}
156
Alexandre Rames5319def2014-10-23 10:03:10 +0100157Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000158 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100159}
160
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100161// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
162#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700163#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100164
Zheng Xuda403092015-04-24 17:35:39 +0800165// Calculate memory accessing operand for save/restore live registers.
166static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100167 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800168 int64_t spill_offset,
169 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100170 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
171 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
172 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800173 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100174 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800175 codegen->GetNumberOfFloatingPointRegisters()));
176
Vladimir Marko804b03f2016-09-14 16:26:36 +0100177 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100178 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
179 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800180
181 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
182 UseScratchRegisterScope temps(masm);
183
184 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100185 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
186 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800187 int64_t reg_size = kXRegSizeInBytes;
188 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
189 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100190 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800191 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
192 // If the offset does not fit in the instruction's immediate field, use an alternate register
193 // to compute the base address(float point registers spill base address).
194 Register new_base = temps.AcquireSameSizeAs(base);
195 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
196 base = new_base;
197 spill_offset = -core_spill_size;
198 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
199 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
200 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
201 }
202
203 if (is_save) {
204 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
205 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
206 } else {
207 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
208 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
209 }
210}
211
212void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800213 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100214 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
215 for (uint32_t i : LowToHighBits(core_spills)) {
216 // If the register holds an object, update the stack mask.
217 if (locations->RegisterContainsObject(i)) {
218 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800219 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100220 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
221 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
222 saved_core_stack_offsets_[i] = stack_offset;
223 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800224 }
225
Vladimir Marko804b03f2016-09-14 16:26:36 +0100226 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
227 for (uint32_t i : LowToHighBits(fp_spills)) {
228 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
229 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
230 saved_fpu_stack_offsets_[i] = stack_offset;
231 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800232 }
233
Vladimir Marko804b03f2016-09-14 16:26:36 +0100234 SaveRestoreLiveRegistersHelper(codegen,
235 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800236 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
237}
238
239void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100240 SaveRestoreLiveRegistersHelper(codegen,
241 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800242 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
243}
244
Alexandre Rames5319def2014-10-23 10:03:10 +0100245class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
246 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000247 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100248
Alexandre Rames67555f72014-11-18 10:55:16 +0000249 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100250 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000251 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100252
Alexandre Rames5319def2014-10-23 10:03:10 +0100253 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000254 if (instruction_->CanThrowIntoCatchBlock()) {
255 // Live registers will be restored in the catch block if caught.
256 SaveLiveRegisters(codegen, instruction_->GetLocations());
257 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000258 // We're moving two locations to locations that could overlap, so we need a parallel
259 // move resolver.
260 InvokeRuntimeCallingConvention calling_convention;
261 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100262 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
263 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000264 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
265 ? kQuickThrowStringBounds
266 : kQuickThrowArrayBounds;
267 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100268 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800269 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100270 }
271
Alexandre Rames8158f282015-08-07 10:26:17 +0100272 bool IsFatal() const OVERRIDE { return true; }
273
Alexandre Rames9931f312015-06-19 14:47:01 +0100274 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
275
Alexandre Rames5319def2014-10-23 10:03:10 +0100276 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100277 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
278};
279
Alexandre Rames67555f72014-11-18 10:55:16 +0000280class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
281 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000282 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000283
284 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
285 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
286 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000287 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800288 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000289 }
290
Alexandre Rames8158f282015-08-07 10:26:17 +0100291 bool IsFatal() const OVERRIDE { return true; }
292
Alexandre Rames9931f312015-06-19 14:47:01 +0100293 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
294
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000296 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
297};
298
299class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
300 public:
301 LoadClassSlowPathARM64(HLoadClass* cls,
302 HInstruction* at,
303 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000304 bool do_clinit,
305 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
306 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
307 : SlowPathCodeARM64(at),
308 cls_(cls),
309 dex_pc_(dex_pc),
310 do_clinit_(do_clinit),
311 bss_entry_temp_(bss_entry_temp),
312 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000313 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
314 }
315
316 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000317 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000318 Location out = locations->Out();
319 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000320 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
321
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000322 InvokeRuntimeCallingConvention calling_convention;
323 // For HLoadClass/kBssEntry/kSaveEverything, the page address of the entry is in a temp
324 // register, make sure it's not clobbered by the call or by saving/restoring registers.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000325 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
326 bool is_load_class_bss_entry =
327 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000328 if (is_load_class_bss_entry) {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000329 DCHECK(bss_entry_temp_.IsValid());
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000330 DCHECK(!bss_entry_temp_.Is(calling_convention.GetRegisterAt(0)));
331 DCHECK(
332 !UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(bss_entry_temp_));
Vladimir Markoea4c1262017-02-06 19:59:33 +0000333 }
334
Alexandre Rames67555f72014-11-18 10:55:16 +0000335 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000336 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000337
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000338 dex::TypeIndex type_index = cls_->GetTypeIndex();
339 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000340 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
341 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000342 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800343 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100344 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800345 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100346 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800347 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000348
349 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000350 if (out.IsValid()) {
351 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000352 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000353 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000355 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000356 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000357 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000358 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000359 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000360 if (call_saves_everything_except_r0_ip0) {
361 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
362 } else {
363 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
364 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
365 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
366 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000367 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000368 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000369 {
370 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
371 __ Bind(strp_label);
372 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000373 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000374 }
375 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000376 __ B(GetExitLabel());
377 }
378
Alexandre Rames9931f312015-06-19 14:47:01 +0100379 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
380
Alexandre Rames67555f72014-11-18 10:55:16 +0000381 private:
382 // The class this slow path will load.
383 HLoadClass* const cls_;
384
Alexandre Rames67555f72014-11-18 10:55:16 +0000385 // The dex PC of `at_`.
386 const uint32_t dex_pc_;
387
388 // Whether to initialize the class.
389 const bool do_clinit_;
390
Vladimir Markoea4c1262017-02-06 19:59:33 +0000391 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
392 vixl::aarch64::Register bss_entry_temp_;
393 vixl::aarch64::Label* bss_entry_adrp_label_;
394
Alexandre Rames67555f72014-11-18 10:55:16 +0000395 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
396};
397
Vladimir Markoaad75c62016-10-03 08:46:48 +0000398class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
399 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100400 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
401 : SlowPathCodeARM64(instruction),
402 temp_(temp),
403 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000404
405 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
406 LocationSummary* locations = instruction_->GetLocations();
407 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
408 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
409
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000410 InvokeRuntimeCallingConvention calling_convention;
411 // Make sure `temp_` is not clobbered by the call or by saving/restoring registers.
412 DCHECK(temp_.IsValid());
413 DCHECK(!temp_.Is(calling_convention.GetRegisterAt(0)));
414 DCHECK(!UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(temp_));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100415
Vladimir Markoaad75c62016-10-03 08:46:48 +0000416 __ Bind(GetEntryLabel());
417 SaveLiveRegisters(codegen, locations);
418
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000419 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
420 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000421 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
422 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
423 Primitive::Type type = instruction_->GetType();
424 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
425
426 RestoreLiveRegisters(codegen, locations);
427
428 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000429 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100430 if (!kUseReadBarrier || kUseBakerReadBarrier) {
431 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
432 } else {
433 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
434 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
435 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
436 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000437 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100438 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000439 {
440 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
441 __ Bind(strp_label);
442 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100443 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000444 }
445
446 __ B(GetExitLabel());
447 }
448
449 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
450
451 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100452 const Register temp_;
453 vixl::aarch64::Label* adrp_label_;
454
Vladimir Markoaad75c62016-10-03 08:46:48 +0000455 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
456};
457
Alexandre Rames5319def2014-10-23 10:03:10 +0100458class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
459 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000460 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100461
Alexandre Rames67555f72014-11-18 10:55:16 +0000462 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
463 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100464 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000465 if (instruction_->CanThrowIntoCatchBlock()) {
466 // Live registers will be restored in the catch block if caught.
467 SaveLiveRegisters(codegen, instruction_->GetLocations());
468 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000469 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
470 instruction_,
471 instruction_->GetDexPc(),
472 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800473 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100474 }
475
Alexandre Rames8158f282015-08-07 10:26:17 +0100476 bool IsFatal() const OVERRIDE { return true; }
477
Alexandre Rames9931f312015-06-19 14:47:01 +0100478 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
479
Alexandre Rames5319def2014-10-23 10:03:10 +0100480 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100481 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
482};
483
484class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
485 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100486 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000487 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100488
Alexandre Rames67555f72014-11-18 10:55:16 +0000489 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100490 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000491 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100492 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100493 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000494 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800495 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100496 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000497 if (successor_ == nullptr) {
498 __ B(GetReturnLabel());
499 } else {
500 __ B(arm64_codegen->GetLabelOf(successor_));
501 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100502 }
503
Scott Wakeling97c72b72016-06-24 16:19:36 +0100504 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100505 DCHECK(successor_ == nullptr);
506 return &return_label_;
507 }
508
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100509 HBasicBlock* GetSuccessor() const {
510 return successor_;
511 }
512
Alexandre Rames9931f312015-06-19 14:47:01 +0100513 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
514
Alexandre Rames5319def2014-10-23 10:03:10 +0100515 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100516 // If not null, the block to branch to after the suspend check.
517 HBasicBlock* const successor_;
518
519 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100520 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100521
522 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
523};
524
Alexandre Rames67555f72014-11-18 10:55:16 +0000525class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
526 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000527 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000528 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000529
530 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000531 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800532
Alexandre Rames3e69f162014-12-10 10:36:50 +0000533 DCHECK(instruction_->IsCheckCast()
534 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
535 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100536 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000537
Alexandre Rames67555f72014-11-18 10:55:16 +0000538 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000539
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000540 if (!is_fatal_) {
541 SaveLiveRegisters(codegen, locations);
542 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000543
544 // We're moving two locations to locations that could overlap, so we need a parallel
545 // move resolver.
546 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800547 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800548 LocationFrom(calling_convention.GetRegisterAt(0)),
549 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800550 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800551 LocationFrom(calling_convention.GetRegisterAt(1)),
552 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000553 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000554 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800555 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000556 Primitive::Type ret_type = instruction_->GetType();
557 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
558 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
559 } else {
560 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800561 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
562 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000563 }
564
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000565 if (!is_fatal_) {
566 RestoreLiveRegisters(codegen, locations);
567 __ B(GetExitLabel());
568 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000569 }
570
Alexandre Rames9931f312015-06-19 14:47:01 +0100571 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100572 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100573
Alexandre Rames67555f72014-11-18 10:55:16 +0000574 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000575 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000576
Alexandre Rames67555f72014-11-18 10:55:16 +0000577 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
578};
579
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700580class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
581 public:
Aart Bik42249c32016-01-07 15:33:50 -0800582 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000583 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700584
585 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800586 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700587 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000588 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000589 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700590 }
591
Alexandre Rames9931f312015-06-19 14:47:01 +0100592 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
593
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700594 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700595 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
596};
597
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100598class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
599 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000600 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100601
602 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
603 LocationSummary* locations = instruction_->GetLocations();
604 __ Bind(GetEntryLabel());
605 SaveLiveRegisters(codegen, locations);
606
607 InvokeRuntimeCallingConvention calling_convention;
608 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
609 parallel_move.AddMove(
610 locations->InAt(0),
611 LocationFrom(calling_convention.GetRegisterAt(0)),
612 Primitive::kPrimNot,
613 nullptr);
614 parallel_move.AddMove(
615 locations->InAt(1),
616 LocationFrom(calling_convention.GetRegisterAt(1)),
617 Primitive::kPrimInt,
618 nullptr);
619 parallel_move.AddMove(
620 locations->InAt(2),
621 LocationFrom(calling_convention.GetRegisterAt(2)),
622 Primitive::kPrimNot,
623 nullptr);
624 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
625
626 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000627 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100628 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
629 RestoreLiveRegisters(codegen, locations);
630 __ B(GetExitLabel());
631 }
632
633 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
634
635 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100636 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
637};
638
Zheng Xu3927c8b2015-11-18 17:46:25 +0800639void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
640 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000641 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800642
643 // We are about to use the assembler to place literals directly. Make sure we have enough
644 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000645 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
646 num_entries * sizeof(int32_t),
647 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800648
649 __ Bind(&table_start_);
650 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
651 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100652 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800653 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100654 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800655 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
656 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
657 Literal<int32_t> literal(jump_offset);
658 __ place(&literal);
659 }
660}
661
Roland Levillain54f869e2017-03-06 13:54:11 +0000662// Abstract base class for read barrier slow paths marking a reference
663// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000664//
Roland Levillain54f869e2017-03-06 13:54:11 +0000665// Argument `entrypoint` must be a register location holding the read
666// barrier marking runtime entry point to be invoked.
667class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
668 protected:
669 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
670 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000671 DCHECK(kEmitCompilerReadBarrier);
672 }
673
Roland Levillain54f869e2017-03-06 13:54:11 +0000674 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000675
Roland Levillain54f869e2017-03-06 13:54:11 +0000676 // Generate assembly code calling the read barrier marking runtime
677 // entry point (ReadBarrierMarkRegX).
678 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000679 // No need to save live registers; it's taken care of by the
680 // entrypoint. Also, there is no need to update the stack mask,
681 // as this runtime call will not trigger a garbage collection.
682 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
683 DCHECK_NE(ref_.reg(), LR);
684 DCHECK_NE(ref_.reg(), WSP);
685 DCHECK_NE(ref_.reg(), WZR);
686 // IP0 is used internally by the ReadBarrierMarkRegX entry point
687 // as a temporary, it cannot be the entry point's input/output.
688 DCHECK_NE(ref_.reg(), IP0);
689 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
690 // "Compact" slow path, saving two moves.
691 //
692 // Instead of using the standard runtime calling convention (input
693 // and output in W0):
694 //
695 // W0 <- ref
696 // W0 <- ReadBarrierMark(W0)
697 // ref <- W0
698 //
699 // we just use rX (the register containing `ref`) as input and output
700 // of a dedicated entrypoint:
701 //
702 // rX <- ReadBarrierMarkRegX(rX)
703 //
704 if (entrypoint_.IsValid()) {
705 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
706 __ Blr(XRegisterFrom(entrypoint_));
707 } else {
708 // Entrypoint is not already loaded, load from the thread.
709 int32_t entry_point_offset =
710 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
711 // This runtime call does not require a stack map.
712 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
713 }
714 }
715
716 // The location (register) of the marked object reference.
717 const Location ref_;
718
719 // The location of the entrypoint if it is already loaded.
720 const Location entrypoint_;
721
Roland Levillain54f869e2017-03-06 13:54:11 +0000722 private:
723 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
724};
725
Alexandre Rames5319def2014-10-23 10:03:10 +0100726// Slow path marking an object reference `ref` during a read
727// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000728// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100729//
730// This means that after the execution of this slow path, `ref` will
731// always be up-to-date, but `obj.field` may not; i.e., after the
732// flip, `ref` will be a to-space reference, but `obj.field` will
733// probably still be a from-space reference (unless it gets updated by
734// another thread, or if another thread installed another object
735// reference (different from `ref`) in `obj.field`).
736//
737// If `entrypoint` is a valid location it is assumed to already be
738// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000739// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000740class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100741 public:
742 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
743 Location ref,
744 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000745 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100746 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100747 }
748
749 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
750
751 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000752 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100753 DCHECK(locations->CanCall());
754 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000755 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000756 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
757 << "Unexpected instruction in read barrier marking slow path: "
758 << instruction_->DebugName();
759
760 __ Bind(GetEntryLabel());
761 GenerateReadBarrierMarkRuntimeCall(codegen);
762 __ B(GetExitLabel());
763 }
764
765 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000766 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
767};
768
Roland Levillain54f869e2017-03-06 13:54:11 +0000769// Slow path loading `obj`'s lock word, loading a reference from
770// object `*(obj + offset + (index << scale_factor))` into `ref`, and
771// marking `ref` if `obj` is gray according to the lock word (Baker
772// read barrier). The field `obj.field` in the object `obj` holding
773// this reference does not get updated by this slow path after marking
774// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
775// below for that).
776//
777// This means that after the execution of this slow path, `ref` will
778// always be up-to-date, but `obj.field` may not; i.e., after the
779// flip, `ref` will be a to-space reference, but `obj.field` will
780// probably still be a from-space reference (unless it gets updated by
781// another thread, or if another thread installed another object
782// reference (different from `ref`) in `obj.field`).
783//
784// Argument `entrypoint` must be a register location holding the read
785// barrier marking runtime entry point to be invoked.
786class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
787 public:
788 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
789 Location ref,
790 Register obj,
791 uint32_t offset,
792 Location index,
793 size_t scale_factor,
794 bool needs_null_check,
795 bool use_load_acquire,
796 Register temp,
797 Location entrypoint)
798 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
799 obj_(obj),
800 offset_(offset),
801 index_(index),
802 scale_factor_(scale_factor),
803 needs_null_check_(needs_null_check),
804 use_load_acquire_(use_load_acquire),
805 temp_(temp) {
806 DCHECK(kEmitCompilerReadBarrier);
807 DCHECK(kUseBakerReadBarrier);
808 }
809
810 const char* GetDescription() const OVERRIDE {
811 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
812 }
813
814 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
815 LocationSummary* locations = instruction_->GetLocations();
816 DCHECK(locations->CanCall());
817 DCHECK(ref_.IsRegister()) << ref_;
818 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
819 DCHECK(obj_.IsW());
820 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100821 DCHECK(instruction_->IsInstanceFieldGet() ||
822 instruction_->IsStaticFieldGet() ||
823 instruction_->IsArrayGet() ||
824 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100825 instruction_->IsInstanceOf() ||
826 instruction_->IsCheckCast() ||
827 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
828 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
829 << "Unexpected instruction in read barrier marking slow path: "
830 << instruction_->DebugName();
831 // The read barrier instrumentation of object ArrayGet
832 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000833 // instruction.
834 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000835 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
836
Roland Levillain54f869e2017-03-06 13:54:11 +0000837 // Temporary register `temp_`, used to store the lock word, must
838 // not be IP0 nor IP1, as we may use them to emit the reference
839 // load (in the call to GenerateRawReferenceLoad below), and we
840 // need the lock word to still be in `temp_` after the reference
841 // load.
842 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
843 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
844
Alexandre Rames5319def2014-10-23 10:03:10 +0100845 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000846
847 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
848 // inserted after the original load. However, in fast path based
849 // Baker's read barriers, we need to perform the load of
850 // mirror::Object::monitor_ *before* the original reference load.
851 // This load-load ordering is required by the read barrier.
852 // The fast path/slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100853 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000854 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
855 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
856 // HeapReference<mirror::Object> ref = *src; // Original reference load.
857 // bool is_gray = (rb_state == ReadBarrier::GrayState());
858 // if (is_gray) {
859 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
860 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000861 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000862 // Note: the original implementation in ReadBarrier::Barrier is
863 // slightly more complex as it performs additional checks that we do
864 // not do here for performance reasons.
865
866 // /* int32_t */ monitor = obj->monitor_
867 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
868 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
869 if (needs_null_check_) {
870 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100871 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000872 // /* LockWord */ lock_word = LockWord(monitor)
873 static_assert(sizeof(LockWord) == sizeof(int32_t),
874 "art::LockWord and int32_t have different sizes.");
875
876 // Introduce a dependency on the lock_word including rb_state,
877 // to prevent load-load reordering, and without using
878 // a memory barrier (which would be more expensive).
879 // `obj` is unchanged by this operation, but its value now depends
880 // on `temp`.
881 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
882
883 // The actual reference load.
884 // A possible implicit null check has already been handled above.
885 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
886 arm64_codegen->GenerateRawReferenceLoad(instruction_,
887 ref_,
888 obj_,
889 offset_,
890 index_,
891 scale_factor_,
892 /* needs_null_check */ false,
893 use_load_acquire_);
894
895 // Mark the object `ref` when `obj` is gray.
896 //
897 // if (rb_state == ReadBarrier::GrayState())
898 // ref = ReadBarrier::Mark(ref);
899 //
900 // Given the numeric representation, it's enough to check the low bit of the rb_state.
901 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
902 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
903 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
904 GenerateReadBarrierMarkRuntimeCall(codegen);
905
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000906 __ B(GetExitLabel());
907 }
908
909 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000910 // The register containing the object holding the marked object reference field.
911 Register obj_;
912 // The offset, index and scale factor to access the reference in `obj_`.
913 uint32_t offset_;
914 Location index_;
915 size_t scale_factor_;
916 // Is a null check required?
917 bool needs_null_check_;
918 // Should this reference load use Load-Acquire semantics?
919 bool use_load_acquire_;
920 // A temporary register used to hold the lock word of `obj_`.
921 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000922
Roland Levillain54f869e2017-03-06 13:54:11 +0000923 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000924};
925
Roland Levillain54f869e2017-03-06 13:54:11 +0000926// Slow path loading `obj`'s lock word, loading a reference from
927// object `*(obj + offset + (index << scale_factor))` into `ref`, and
928// marking `ref` if `obj` is gray according to the lock word (Baker
929// read barrier). If needed, this slow path also atomically updates
930// the field `obj.field` in the object `obj` holding this reference
931// after marking (contrary to
932// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
933// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100934//
935// This means that after the execution of this slow path, both `ref`
936// and `obj.field` will be up-to-date; i.e., after the flip, both will
937// hold the same to-space reference (unless another thread installed
938// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000939//
Roland Levillain54f869e2017-03-06 13:54:11 +0000940// Argument `entrypoint` must be a register location holding the read
941// barrier marking runtime entry point to be invoked.
942class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
943 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100944 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000945 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(HInstruction* instruction,
946 Location ref,
947 Register obj,
948 uint32_t offset,
949 Location index,
950 size_t scale_factor,
951 bool needs_null_check,
952 bool use_load_acquire,
953 Register temp,
954 Location entrypoint)
955 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100956 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000957 offset_(offset),
958 index_(index),
959 scale_factor_(scale_factor),
960 needs_null_check_(needs_null_check),
961 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000962 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100963 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000964 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100965 }
966
967 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000968 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100969 }
970
971 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
972 LocationSummary* locations = instruction_->GetLocations();
973 Register ref_reg = WRegisterFrom(ref_);
974 DCHECK(locations->CanCall());
975 DCHECK(ref_.IsRegister()) << ref_;
976 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000977 DCHECK(obj_.IsW());
978 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
979
980 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100981 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
982 << "Unexpected instruction in read barrier marking and field updating slow path: "
983 << instruction_->DebugName();
984 DCHECK(instruction_->GetLocations()->Intrinsified());
985 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000986 DCHECK_EQ(offset_, 0u);
987 DCHECK_EQ(scale_factor_, 0u);
988 DCHECK_EQ(use_load_acquire_, false);
989 // The location of the offset of the marked reference field within `obj_`.
990 Location field_offset = index_;
991 DCHECK(field_offset.IsRegister()) << field_offset;
992
993 // Temporary register `temp_`, used to store the lock word, must
994 // not be IP0 nor IP1, as we may use them to emit the reference
995 // load (in the call to GenerateRawReferenceLoad below), and we
996 // need the lock word to still be in `temp_` after the reference
997 // load.
998 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
999 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001000
1001 __ Bind(GetEntryLabel());
1002
Roland Levillain54f869e2017-03-06 13:54:11 +00001003 // /* int32_t */ monitor = obj->monitor_
1004 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1005 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1006 if (needs_null_check_) {
1007 codegen->MaybeRecordImplicitNullCheck(instruction_);
1008 }
1009 // /* LockWord */ lock_word = LockWord(monitor)
1010 static_assert(sizeof(LockWord) == sizeof(int32_t),
1011 "art::LockWord and int32_t have different sizes.");
1012
1013 // Introduce a dependency on the lock_word including rb_state,
1014 // to prevent load-load reordering, and without using
1015 // a memory barrier (which would be more expensive).
1016 // `obj` is unchanged by this operation, but its value now depends
1017 // on `temp`.
1018 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1019
1020 // The actual reference load.
1021 // A possible implicit null check has already been handled above.
1022 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1023 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1024 ref_,
1025 obj_,
1026 offset_,
1027 index_,
1028 scale_factor_,
1029 /* needs_null_check */ false,
1030 use_load_acquire_);
1031
1032 // Mark the object `ref` when `obj` is gray.
1033 //
1034 // if (rb_state == ReadBarrier::GrayState())
1035 // ref = ReadBarrier::Mark(ref);
1036 //
1037 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1038 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1039 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1040 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1041
1042 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001043 // Note that we cannot use IP to save the old reference, as IP is
1044 // used internally by the ReadBarrierMarkRegX entry point, and we
1045 // need the old reference after the call to that entry point.
1046 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1047 __ Mov(temp_.W(), ref_reg);
1048
Roland Levillain54f869e2017-03-06 13:54:11 +00001049 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001050
1051 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001052 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001053 //
1054 // Note that this field could also hold a different object, if
1055 // another thread had concurrently changed it. In that case, the
1056 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1057 // (CAS) operation below would abort the CAS, leaving the field
1058 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001059 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001060 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001061
1062 // Update the the holder's field atomically. This may fail if
1063 // mutator updates before us, but it's OK. This is achieved
1064 // using a strong compare-and-set (CAS) operation with relaxed
1065 // memory synchronization ordering, where the expected value is
1066 // the old reference and the desired value is the new reference.
1067
1068 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1069 UseScratchRegisterScope temps(masm);
1070
1071 // Convenience aliases.
1072 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001073 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001074 Register expected = temp_.W();
1075 Register value = ref_reg;
1076 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1077 Register tmp_value = temps.AcquireW(); // Value in memory.
1078
1079 __ Add(tmp_ptr, base.X(), Operand(offset));
1080
1081 if (kPoisonHeapReferences) {
1082 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1083 if (value.Is(expected)) {
1084 // Do not poison `value`, as it is the same register as
1085 // `expected`, which has just been poisoned.
1086 } else {
1087 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1088 }
1089 }
1090
1091 // do {
1092 // tmp_value = [tmp_ptr] - expected;
1093 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1094
Roland Levillain24a4d112016-10-26 13:10:46 +01001095 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001096 __ Bind(&loop_head);
1097 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1098 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001099 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001100 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1101 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001102 __ B(&exit_loop);
1103 __ Bind(&comparison_failed);
1104 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001105 __ Bind(&exit_loop);
1106
1107 if (kPoisonHeapReferences) {
1108 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1109 if (value.Is(expected)) {
1110 // Do not unpoison `value`, as it is the same register as
1111 // `expected`, which has just been unpoisoned.
1112 } else {
1113 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1114 }
1115 }
1116
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001117 __ B(GetExitLabel());
1118 }
1119
1120 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001121 // The register containing the object holding the marked object reference field.
1122 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001123 // The offset, index and scale factor to access the reference in `obj_`.
1124 uint32_t offset_;
1125 Location index_;
1126 size_t scale_factor_;
1127 // Is a null check required?
1128 bool needs_null_check_;
1129 // Should this reference load use Load-Acquire semantics?
1130 bool use_load_acquire_;
1131 // A temporary register used to hold the lock word of `obj_`; and
1132 // also to hold the original reference value, when the reference is
1133 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001134 const Register temp_;
1135
Roland Levillain54f869e2017-03-06 13:54:11 +00001136 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001137};
1138
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001139// Slow path generating a read barrier for a heap reference.
1140class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1141 public:
1142 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1143 Location out,
1144 Location ref,
1145 Location obj,
1146 uint32_t offset,
1147 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001148 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001149 out_(out),
1150 ref_(ref),
1151 obj_(obj),
1152 offset_(offset),
1153 index_(index) {
1154 DCHECK(kEmitCompilerReadBarrier);
1155 // If `obj` is equal to `out` or `ref`, it means the initial object
1156 // has been overwritten by (or after) the heap object reference load
1157 // to be instrumented, e.g.:
1158 //
1159 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001160 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001161 //
1162 // In that case, we have lost the information about the original
1163 // object, and the emitted read barrier cannot work properly.
1164 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1165 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1166 }
1167
1168 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1169 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1170 LocationSummary* locations = instruction_->GetLocations();
1171 Primitive::Type type = Primitive::kPrimNot;
1172 DCHECK(locations->CanCall());
1173 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001174 DCHECK(instruction_->IsInstanceFieldGet() ||
1175 instruction_->IsStaticFieldGet() ||
1176 instruction_->IsArrayGet() ||
1177 instruction_->IsInstanceOf() ||
1178 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001179 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001180 << "Unexpected instruction in read barrier for heap reference slow path: "
1181 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001182 // The read barrier instrumentation of object ArrayGet
1183 // instructions does not support the HIntermediateAddress
1184 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001185 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001186 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001187
1188 __ Bind(GetEntryLabel());
1189
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001190 SaveLiveRegisters(codegen, locations);
1191
1192 // We may have to change the index's value, but as `index_` is a
1193 // constant member (like other "inputs" of this slow path),
1194 // introduce a copy of it, `index`.
1195 Location index = index_;
1196 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001197 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001198 if (instruction_->IsArrayGet()) {
1199 // Compute the actual memory offset and store it in `index`.
1200 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1201 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1202 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1203 // We are about to change the value of `index_reg` (see the
1204 // calls to vixl::MacroAssembler::Lsl and
1205 // vixl::MacroAssembler::Mov below), but it has
1206 // not been saved by the previous call to
1207 // art::SlowPathCode::SaveLiveRegisters, as it is a
1208 // callee-save register --
1209 // art::SlowPathCode::SaveLiveRegisters does not consider
1210 // callee-save registers, as it has been designed with the
1211 // assumption that callee-save registers are supposed to be
1212 // handled by the called function. So, as a callee-save
1213 // register, `index_reg` _would_ eventually be saved onto
1214 // the stack, but it would be too late: we would have
1215 // changed its value earlier. Therefore, we manually save
1216 // it here into another freely available register,
1217 // `free_reg`, chosen of course among the caller-save
1218 // registers (as a callee-save `free_reg` register would
1219 // exhibit the same problem).
1220 //
1221 // Note we could have requested a temporary register from
1222 // the register allocator instead; but we prefer not to, as
1223 // this is a slow path, and we know we can find a
1224 // caller-save register that is available.
1225 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1226 __ Mov(free_reg.W(), index_reg);
1227 index_reg = free_reg;
1228 index = LocationFrom(index_reg);
1229 } else {
1230 // The initial register stored in `index_` has already been
1231 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1232 // (as it is not a callee-save register), so we can freely
1233 // use it.
1234 }
1235 // Shifting the index value contained in `index_reg` by the scale
1236 // factor (2) cannot overflow in practice, as the runtime is
1237 // unable to allocate object arrays with a size larger than
1238 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1239 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1240 static_assert(
1241 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1242 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1243 __ Add(index_reg, index_reg, Operand(offset_));
1244 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001245 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1246 // intrinsics, `index_` is not shifted by a scale factor of 2
1247 // (as in the case of ArrayGet), as it is actually an offset
1248 // to an object field within an object.
1249 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001250 DCHECK(instruction_->GetLocations()->Intrinsified());
1251 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1252 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1253 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001254 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001255 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001256 }
1257 }
1258
1259 // We're moving two or three locations to locations that could
1260 // overlap, so we need a parallel move resolver.
1261 InvokeRuntimeCallingConvention calling_convention;
1262 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1263 parallel_move.AddMove(ref_,
1264 LocationFrom(calling_convention.GetRegisterAt(0)),
1265 type,
1266 nullptr);
1267 parallel_move.AddMove(obj_,
1268 LocationFrom(calling_convention.GetRegisterAt(1)),
1269 type,
1270 nullptr);
1271 if (index.IsValid()) {
1272 parallel_move.AddMove(index,
1273 LocationFrom(calling_convention.GetRegisterAt(2)),
1274 Primitive::kPrimInt,
1275 nullptr);
1276 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1277 } else {
1278 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1279 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1280 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001281 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001282 instruction_,
1283 instruction_->GetDexPc(),
1284 this);
1285 CheckEntrypointTypes<
1286 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1287 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1288
1289 RestoreLiveRegisters(codegen, locations);
1290
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001291 __ B(GetExitLabel());
1292 }
1293
1294 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1295
1296 private:
1297 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001298 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1299 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001300 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1301 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1302 return Register(VIXLRegCodeFromART(i), kXRegSize);
1303 }
1304 }
1305 // We shall never fail to find a free caller-save register, as
1306 // there are more than two core caller-save registers on ARM64
1307 // (meaning it is possible to find one which is different from
1308 // `ref` and `obj`).
1309 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1310 LOG(FATAL) << "Could not find a free register";
1311 UNREACHABLE();
1312 }
1313
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001314 const Location out_;
1315 const Location ref_;
1316 const Location obj_;
1317 const uint32_t offset_;
1318 // An additional location containing an index to an array.
1319 // Only used for HArrayGet and the UnsafeGetObject &
1320 // UnsafeGetObjectVolatile intrinsics.
1321 const Location index_;
1322
1323 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1324};
1325
1326// Slow path generating a read barrier for a GC root.
1327class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1328 public:
1329 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001330 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001331 DCHECK(kEmitCompilerReadBarrier);
1332 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001333
1334 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1335 LocationSummary* locations = instruction_->GetLocations();
1336 Primitive::Type type = Primitive::kPrimNot;
1337 DCHECK(locations->CanCall());
1338 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001339 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1340 << "Unexpected instruction in read barrier for GC root slow path: "
1341 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001342
1343 __ Bind(GetEntryLabel());
1344 SaveLiveRegisters(codegen, locations);
1345
1346 InvokeRuntimeCallingConvention calling_convention;
1347 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1348 // The argument of the ReadBarrierForRootSlow is not a managed
1349 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1350 // thus we need a 64-bit move here, and we cannot use
1351 //
1352 // arm64_codegen->MoveLocation(
1353 // LocationFrom(calling_convention.GetRegisterAt(0)),
1354 // root_,
1355 // type);
1356 //
1357 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1358 // reference type (`Primitive::kPrimNot`).
1359 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001360 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001361 instruction_,
1362 instruction_->GetDexPc(),
1363 this);
1364 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1365 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1366
1367 RestoreLiveRegisters(codegen, locations);
1368 __ B(GetExitLabel());
1369 }
1370
1371 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1372
1373 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001374 const Location out_;
1375 const Location root_;
1376
1377 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1378};
1379
Alexandre Rames5319def2014-10-23 10:03:10 +01001380#undef __
1381
1382Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1383 Location next_location;
1384 if (type == Primitive::kPrimVoid) {
1385 LOG(FATAL) << "Unreachable type " << type;
1386 }
1387
1388 if (Primitive::IsFloatingPointType(type) &&
1389 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001390 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1391 } else if (!Primitive::IsFloatingPointType(type) &&
1392 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1393 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1394 } else {
1395 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001396 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1397 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001398 }
1399
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001400 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001401 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001402 return next_location;
1403}
1404
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001405Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001406 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001407}
1408
Serban Constantinescu579885a2015-02-22 20:51:33 +00001409CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1410 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001411 const CompilerOptions& compiler_options,
1412 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001413 : CodeGenerator(graph,
1414 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001415 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001416 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001417 callee_saved_core_registers.GetList(),
1418 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001419 compiler_options,
1420 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001421 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001422 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001423 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001424 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001425 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001426 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001427 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001428 uint32_literals_(std::less<uint32_t>(),
1429 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001430 uint64_literals_(std::less<uint64_t>(),
1431 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001432 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1433 boot_image_string_patches_(StringReferenceValueComparator(),
1434 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1435 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001436 boot_image_type_patches_(TypeReferenceValueComparator(),
1437 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1438 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001439 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001440 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001441 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001442 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1443 jit_class_patches_(TypeReferenceValueComparator(),
1444 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001445 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001446 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001447}
Alexandre Rames5319def2014-10-23 10:03:10 +01001448
Alexandre Rames67555f72014-11-18 10:55:16 +00001449#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001450
Zheng Xu3927c8b2015-11-18 17:46:25 +08001451void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001452 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001453 jump_table->EmitTable(this);
1454 }
1455}
1456
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001457void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001458 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001459 // Ensure we emit the literal pool.
1460 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001461
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001462 CodeGenerator::Finalize(allocator);
1463}
1464
Zheng Xuad4450e2015-04-17 18:48:56 +08001465void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1466 // Note: There are 6 kinds of moves:
1467 // 1. constant -> GPR/FPR (non-cycle)
1468 // 2. constant -> stack (non-cycle)
1469 // 3. GPR/FPR -> GPR/FPR
1470 // 4. GPR/FPR -> stack
1471 // 5. stack -> GPR/FPR
1472 // 6. stack -> stack (non-cycle)
1473 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1474 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1475 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1476 // dependency.
1477 vixl_temps_.Open(GetVIXLAssembler());
1478}
1479
1480void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1481 vixl_temps_.Close();
1482}
1483
1484Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
1485 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1486 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1487 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1488 Location scratch = GetScratchLocation(kind);
1489 if (!scratch.Equals(Location::NoLocation())) {
1490 return scratch;
1491 }
1492 // Allocate from VIXL temp registers.
1493 if (kind == Location::kRegister) {
1494 scratch = LocationFrom(vixl_temps_.AcquireX());
1495 } else {
1496 DCHECK(kind == Location::kFpuRegister);
1497 scratch = LocationFrom(vixl_temps_.AcquireD());
1498 }
1499 AddScratchLocation(scratch);
1500 return scratch;
1501}
1502
1503void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1504 if (loc.IsRegister()) {
1505 vixl_temps_.Release(XRegisterFrom(loc));
1506 } else {
1507 DCHECK(loc.IsFpuRegister());
1508 vixl_temps_.Release(DRegisterFrom(loc));
1509 }
1510 RemoveScratchLocation(loc);
1511}
1512
Alexandre Rames3e69f162014-12-10 10:36:50 +00001513void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001514 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001515 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001516}
1517
Alexandre Rames5319def2014-10-23 10:03:10 +01001518void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001519 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001520 __ Bind(&frame_entry_label_);
1521
Serban Constantinescu02164b32014-11-13 14:05:07 +00001522 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1523 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001524 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001525 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001526 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001527 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001528 {
1529 // Ensure that between load and RecordPcInfo there are no pools emitted.
1530 ExactAssemblyScope eas(GetVIXLAssembler(),
1531 kInstructionSize,
1532 CodeBufferCheckScope::kExactSize);
1533 __ ldr(wzr, MemOperand(temp, 0));
1534 RecordPcInfo(nullptr, 0);
1535 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001536 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001537
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001538 if (!HasEmptyFrame()) {
1539 int frame_size = GetFrameSize();
1540 // Stack layout:
1541 // sp[frame_size - 8] : lr.
1542 // ... : other preserved core registers.
1543 // ... : other preserved fp registers.
1544 // ... : reserved frame space.
1545 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001546
1547 // Save the current method if we need it. Note that we do not
1548 // do this in HCurrentMethod, as the instruction might have been removed
1549 // in the SSA graph.
1550 if (RequiresCurrentMethod()) {
1551 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001552 } else {
1553 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001554 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001555 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001556 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1557 frame_size - GetCoreSpillSize());
1558 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1559 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001560
1561 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1562 // Initialize should_deoptimize flag to 0.
1563 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1564 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1565 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001566 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001567}
1568
1569void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001570 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001571 if (!HasEmptyFrame()) {
1572 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001573 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1574 frame_size - FrameEntrySpillSize());
1575 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1576 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001577 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001578 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001579 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001580 __ Ret();
1581 GetAssembler()->cfi().RestoreState();
1582 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001583}
1584
Scott Wakeling97c72b72016-06-24 16:19:36 +01001585CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001586 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001587 return CPURegList(CPURegister::kRegister, kXRegSize,
1588 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001589}
1590
Scott Wakeling97c72b72016-06-24 16:19:36 +01001591CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001592 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1593 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001594 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1595 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001596}
1597
Alexandre Rames5319def2014-10-23 10:03:10 +01001598void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1599 __ Bind(GetLabelOf(block));
1600}
1601
Calin Juravle175dc732015-08-25 15:42:32 +01001602void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1603 DCHECK(location.IsRegister());
1604 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1605}
1606
Calin Juravlee460d1d2015-09-29 04:52:17 +01001607void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1608 if (location.IsRegister()) {
1609 locations->AddTemp(location);
1610 } else {
1611 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1612 }
1613}
1614
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001615void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001616 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001617 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001618 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001619 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001620 if (value_can_be_null) {
1621 __ Cbz(value, &done);
1622 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001623 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001624 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001625 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001626 if (value_can_be_null) {
1627 __ Bind(&done);
1628 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001629}
1630
David Brazdil58282f42016-01-14 12:45:10 +00001631void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001632 // Blocked core registers:
1633 // lr : Runtime reserved.
1634 // tr : Runtime reserved.
1635 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1636 // ip1 : VIXL core temp.
1637 // ip0 : VIXL core temp.
1638 //
1639 // Blocked fp registers:
1640 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001641 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1642 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001643 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001644 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001645 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001646
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001647 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001648 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001649 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001650 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001651
David Brazdil58282f42016-01-14 12:45:10 +00001652 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001653 // Stubs do not save callee-save floating point registers. If the graph
1654 // is debuggable, we need to deal with these registers differently. For
1655 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001656 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1657 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001658 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001659 }
1660 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001661}
1662
Alexandre Rames3e69f162014-12-10 10:36:50 +00001663size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1664 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1665 __ Str(reg, MemOperand(sp, stack_index));
1666 return kArm64WordSize;
1667}
1668
1669size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1670 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1671 __ Ldr(reg, MemOperand(sp, stack_index));
1672 return kArm64WordSize;
1673}
1674
1675size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1676 FPRegister reg = FPRegister(reg_id, kDRegSize);
1677 __ Str(reg, MemOperand(sp, stack_index));
1678 return kArm64WordSize;
1679}
1680
1681size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1682 FPRegister reg = FPRegister(reg_id, kDRegSize);
1683 __ Ldr(reg, MemOperand(sp, stack_index));
1684 return kArm64WordSize;
1685}
1686
Alexandre Rames5319def2014-10-23 10:03:10 +01001687void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001688 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001689}
1690
1691void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001692 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001693}
1694
Alexandre Rames67555f72014-11-18 10:55:16 +00001695void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001696 if (constant->IsIntConstant()) {
1697 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1698 } else if (constant->IsLongConstant()) {
1699 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1700 } else if (constant->IsNullConstant()) {
1701 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001702 } else if (constant->IsFloatConstant()) {
1703 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1704 } else {
1705 DCHECK(constant->IsDoubleConstant());
1706 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1707 }
1708}
1709
Alexandre Rames3e69f162014-12-10 10:36:50 +00001710
1711static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1712 DCHECK(constant.IsConstant());
1713 HConstant* cst = constant.GetConstant();
1714 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001715 // Null is mapped to a core W register, which we associate with kPrimInt.
1716 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001717 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1718 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1719 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1720}
1721
Roland Levillain558dea12017-01-27 19:40:44 +00001722// Allocate a scratch register from the VIXL pool, querying first into
1723// the floating-point register pool, and then the the core register
1724// pool. This is essentially a reimplementation of
1725// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1726// using a different allocation strategy.
1727static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1728 vixl::aarch64::UseScratchRegisterScope* temps,
1729 int size_in_bits) {
1730 return masm->GetScratchFPRegisterList()->IsEmpty()
1731 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1732 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1733}
1734
Calin Juravlee460d1d2015-09-29 04:52:17 +01001735void CodeGeneratorARM64::MoveLocation(Location destination,
1736 Location source,
1737 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001738 if (source.Equals(destination)) {
1739 return;
1740 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001741
1742 // A valid move can always be inferred from the destination and source
1743 // locations. When moving from and to a register, the argument type can be
1744 // used to generate 32bit instead of 64bit moves. In debug mode we also
1745 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001746 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001747
1748 if (destination.IsRegister() || destination.IsFpuRegister()) {
1749 if (unspecified_type) {
1750 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1751 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001752 (src_cst != nullptr && (src_cst->IsIntConstant()
1753 || src_cst->IsFloatConstant()
1754 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001755 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001756 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001757 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001758 // If the source is a double stack slot or a 64bit constant, a 64bit
1759 // type is appropriate. Else the source is a register, and since the
1760 // type has not been specified, we chose a 64bit type to force a 64bit
1761 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001762 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001763 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001764 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001765 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1766 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1767 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001768 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1769 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1770 __ Ldr(dst, StackOperandFrom(source));
1771 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001772 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001773 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001774 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001775 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001776 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001777 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001778 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001779 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1780 ? Primitive::kPrimLong
1781 : Primitive::kPrimInt;
1782 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1783 }
1784 } else {
1785 DCHECK(source.IsFpuRegister());
1786 if (destination.IsRegister()) {
1787 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1788 ? Primitive::kPrimDouble
1789 : Primitive::kPrimFloat;
1790 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1791 } else {
1792 DCHECK(destination.IsFpuRegister());
1793 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001794 }
1795 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001796 } else { // The destination is not a register. It must be a stack slot.
1797 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1798 if (source.IsRegister() || source.IsFpuRegister()) {
1799 if (unspecified_type) {
1800 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001801 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001802 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001803 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001804 }
1805 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001806 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1807 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1808 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001809 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001810 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1811 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001812 UseScratchRegisterScope temps(GetVIXLAssembler());
1813 HConstant* src_cst = source.GetConstant();
1814 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001815 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001816 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1817 ? Register(xzr)
1818 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001819 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001820 if (src_cst->IsIntConstant()) {
1821 temp = temps.AcquireW();
1822 } else if (src_cst->IsLongConstant()) {
1823 temp = temps.AcquireX();
1824 } else if (src_cst->IsFloatConstant()) {
1825 temp = temps.AcquireS();
1826 } else {
1827 DCHECK(src_cst->IsDoubleConstant());
1828 temp = temps.AcquireD();
1829 }
1830 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001831 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001832 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001833 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001834 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001835 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001836 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001837 // Use any scratch register (a core or a floating-point one)
1838 // from VIXL scratch register pools as a temporary.
1839 //
1840 // We used to only use the FP scratch register pool, but in some
1841 // rare cases the only register from this pool (D31) would
1842 // already be used (e.g. within a ParallelMove instruction, when
1843 // a move is blocked by a another move requiring a scratch FP
1844 // register, which would reserve D31). To prevent this issue, we
1845 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001846 //
1847 // Also, we start by asking for a FP scratch register first, as the
1848 // demand of scratch core registers is higher. This is why we
1849 // use AcquireFPOrCoreCPURegisterOfSize instead of
1850 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1851 // allocates core scratch registers first.
1852 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1853 GetVIXLAssembler(),
1854 &temps,
1855 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001856 __ Ldr(temp, StackOperandFrom(source));
1857 __ Str(temp, StackOperandFrom(destination));
1858 }
1859 }
1860}
1861
1862void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001863 CPURegister dst,
1864 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001865 switch (type) {
1866 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001867 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001868 break;
1869 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001870 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001871 break;
1872 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001873 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001874 break;
1875 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001876 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001877 break;
1878 case Primitive::kPrimInt:
1879 case Primitive::kPrimNot:
1880 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001881 case Primitive::kPrimFloat:
1882 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001883 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001884 __ Ldr(dst, src);
1885 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001886 case Primitive::kPrimVoid:
1887 LOG(FATAL) << "Unreachable type " << type;
1888 }
1889}
1890
Calin Juravle77520bc2015-01-12 18:45:46 +00001891void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001892 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001893 const MemOperand& src,
1894 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001895 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001896 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001897 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001898 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001899
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001900 DCHECK(!src.IsPreIndex());
1901 DCHECK(!src.IsPostIndex());
1902
1903 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001904 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001905 {
1906 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1907 MemOperand base = MemOperand(temp_base);
1908 switch (type) {
1909 case Primitive::kPrimBoolean:
1910 {
1911 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1912 __ ldarb(Register(dst), base);
1913 if (needs_null_check) {
1914 MaybeRecordImplicitNullCheck(instruction);
1915 }
1916 }
1917 break;
1918 case Primitive::kPrimByte:
1919 {
1920 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1921 __ ldarb(Register(dst), base);
1922 if (needs_null_check) {
1923 MaybeRecordImplicitNullCheck(instruction);
1924 }
1925 }
1926 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1927 break;
1928 case Primitive::kPrimChar:
1929 {
1930 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1931 __ ldarh(Register(dst), base);
1932 if (needs_null_check) {
1933 MaybeRecordImplicitNullCheck(instruction);
1934 }
1935 }
1936 break;
1937 case Primitive::kPrimShort:
1938 {
1939 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1940 __ ldarh(Register(dst), base);
1941 if (needs_null_check) {
1942 MaybeRecordImplicitNullCheck(instruction);
1943 }
1944 }
1945 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1946 break;
1947 case Primitive::kPrimInt:
1948 case Primitive::kPrimNot:
1949 case Primitive::kPrimLong:
1950 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1951 {
1952 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1953 __ ldar(Register(dst), base);
1954 if (needs_null_check) {
1955 MaybeRecordImplicitNullCheck(instruction);
1956 }
1957 }
1958 break;
1959 case Primitive::kPrimFloat:
1960 case Primitive::kPrimDouble: {
1961 DCHECK(dst.IsFPRegister());
1962 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001963
Artem Serov914d7a82017-02-07 14:33:49 +00001964 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1965 {
1966 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1967 __ ldar(temp, base);
1968 if (needs_null_check) {
1969 MaybeRecordImplicitNullCheck(instruction);
1970 }
1971 }
1972 __ Fmov(FPRegister(dst), temp);
1973 break;
Roland Levillain44015862016-01-22 11:47:17 +00001974 }
Artem Serov914d7a82017-02-07 14:33:49 +00001975 case Primitive::kPrimVoid:
1976 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001977 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001978 }
1979}
1980
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001981void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001982 CPURegister src,
1983 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001984 switch (type) {
1985 case Primitive::kPrimBoolean:
1986 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001987 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001988 break;
1989 case Primitive::kPrimChar:
1990 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001991 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001992 break;
1993 case Primitive::kPrimInt:
1994 case Primitive::kPrimNot:
1995 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001996 case Primitive::kPrimFloat:
1997 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001998 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001999 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002000 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002001 case Primitive::kPrimVoid:
2002 LOG(FATAL) << "Unreachable type " << type;
2003 }
2004}
2005
Artem Serov914d7a82017-02-07 14:33:49 +00002006void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2007 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002008 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002009 const MemOperand& dst,
2010 bool needs_null_check) {
2011 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002012 UseScratchRegisterScope temps(GetVIXLAssembler());
2013 Register temp_base = temps.AcquireX();
2014
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002015 DCHECK(!dst.IsPreIndex());
2016 DCHECK(!dst.IsPostIndex());
2017
2018 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002019 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002020 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002021 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002022 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002023 switch (type) {
2024 case Primitive::kPrimBoolean:
2025 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002026 {
2027 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2028 __ stlrb(Register(src), base);
2029 if (needs_null_check) {
2030 MaybeRecordImplicitNullCheck(instruction);
2031 }
2032 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002033 break;
2034 case Primitive::kPrimChar:
2035 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002036 {
2037 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2038 __ stlrh(Register(src), base);
2039 if (needs_null_check) {
2040 MaybeRecordImplicitNullCheck(instruction);
2041 }
2042 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002043 break;
2044 case Primitive::kPrimInt:
2045 case Primitive::kPrimNot:
2046 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002047 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002048 {
2049 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2050 __ stlr(Register(src), base);
2051 if (needs_null_check) {
2052 MaybeRecordImplicitNullCheck(instruction);
2053 }
2054 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002055 break;
2056 case Primitive::kPrimFloat:
2057 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002058 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002059 Register temp_src;
2060 if (src.IsZero()) {
2061 // The zero register is used to avoid synthesizing zero constants.
2062 temp_src = Register(src);
2063 } else {
2064 DCHECK(src.IsFPRegister());
2065 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2066 __ Fmov(temp_src, FPRegister(src));
2067 }
Artem Serov914d7a82017-02-07 14:33:49 +00002068 {
2069 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2070 __ stlr(temp_src, base);
2071 if (needs_null_check) {
2072 MaybeRecordImplicitNullCheck(instruction);
2073 }
2074 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002075 break;
2076 }
2077 case Primitive::kPrimVoid:
2078 LOG(FATAL) << "Unreachable type " << type;
2079 }
2080}
2081
Calin Juravle175dc732015-08-25 15:42:32 +01002082void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2083 HInstruction* instruction,
2084 uint32_t dex_pc,
2085 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002086 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002087
2088 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2089 {
2090 // Ensure the pc position is recorded immediately after the `blr` instruction.
2091 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2092 __ blr(lr);
2093 if (EntrypointRequiresStackMap(entrypoint)) {
2094 RecordPcInfo(instruction, dex_pc, slow_path);
2095 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002096 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002097}
2098
Roland Levillaindec8f632016-07-22 17:10:06 +01002099void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2100 HInstruction* instruction,
2101 SlowPathCode* slow_path) {
2102 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002103 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2104 __ Blr(lr);
2105}
2106
Alexandre Rames67555f72014-11-18 10:55:16 +00002107void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002108 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002109 UseScratchRegisterScope temps(GetVIXLAssembler());
2110 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002111 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2112
Serban Constantinescu02164b32014-11-13 14:05:07 +00002113 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002114 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2115 __ Add(temp, class_reg, status_offset);
2116 __ Ldar(temp, HeapOperand(temp));
2117 __ Cmp(temp, mirror::Class::kStatusInitialized);
2118 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002119 __ Bind(slow_path->GetExitLabel());
2120}
Alexandre Rames5319def2014-10-23 10:03:10 +01002121
Roland Levillain44015862016-01-22 11:47:17 +00002122void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002123 BarrierType type = BarrierAll;
2124
2125 switch (kind) {
2126 case MemBarrierKind::kAnyAny:
2127 case MemBarrierKind::kAnyStore: {
2128 type = BarrierAll;
2129 break;
2130 }
2131 case MemBarrierKind::kLoadAny: {
2132 type = BarrierReads;
2133 break;
2134 }
2135 case MemBarrierKind::kStoreStore: {
2136 type = BarrierWrites;
2137 break;
2138 }
2139 default:
2140 LOG(FATAL) << "Unexpected memory barrier " << kind;
2141 }
2142 __ Dmb(InnerShareable, type);
2143}
2144
Serban Constantinescu02164b32014-11-13 14:05:07 +00002145void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2146 HBasicBlock* successor) {
2147 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002148 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2149 if (slow_path == nullptr) {
2150 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2151 instruction->SetSlowPath(slow_path);
2152 codegen_->AddSlowPath(slow_path);
2153 if (successor != nullptr) {
2154 DCHECK(successor->IsLoopHeader());
2155 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2156 }
2157 } else {
2158 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2159 }
2160
Serban Constantinescu02164b32014-11-13 14:05:07 +00002161 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2162 Register temp = temps.AcquireW();
2163
Andreas Gampe542451c2016-07-26 09:02:02 -07002164 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002165 if (successor == nullptr) {
2166 __ Cbnz(temp, slow_path->GetEntryLabel());
2167 __ Bind(slow_path->GetReturnLabel());
2168 } else {
2169 __ Cbz(temp, codegen_->GetLabelOf(successor));
2170 __ B(slow_path->GetEntryLabel());
2171 // slow_path will return to GetLabelOf(successor).
2172 }
2173}
2174
Alexandre Rames5319def2014-10-23 10:03:10 +01002175InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2176 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002177 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002178 assembler_(codegen->GetAssembler()),
2179 codegen_(codegen) {}
2180
2181#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002182 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002183
2184#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2185
2186enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002187 // Using a base helps identify when we hit such breakpoints.
2188 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002189#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2190 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2191#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2192};
2193
2194#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002195 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002196 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2197 } \
2198 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2199 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2200 locations->SetOut(Location::Any()); \
2201 }
2202 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2203#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2204
2205#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002206#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002207
Alexandre Rames67555f72014-11-18 10:55:16 +00002208void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002209 DCHECK_EQ(instr->InputCount(), 2U);
2210 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2211 Primitive::Type type = instr->GetResultType();
2212 switch (type) {
2213 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002214 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002215 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002216 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002217 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002218 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002219
2220 case Primitive::kPrimFloat:
2221 case Primitive::kPrimDouble:
2222 locations->SetInAt(0, Location::RequiresFpuRegister());
2223 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002224 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002225 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002226
Alexandre Rames5319def2014-10-23 10:03:10 +01002227 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002228 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002229 }
2230}
2231
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002232void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2233 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002234 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2235
2236 bool object_field_get_with_read_barrier =
2237 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002238 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002239 new (GetGraph()->GetArena()) LocationSummary(instruction,
2240 object_field_get_with_read_barrier ?
2241 LocationSummary::kCallOnSlowPath :
2242 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002243 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002244 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002245 // We need a temporary register for the read barrier marking slow
2246 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002247 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2248 !Runtime::Current()->UseJitCompilation() &&
2249 !field_info.IsVolatile()) {
2250 // If link-time thunks for the Baker read barrier are enabled, for AOT
2251 // non-volatile loads we need a temporary only if the offset is too big.
2252 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2253 locations->AddTemp(FixedTempLocation());
2254 }
2255 } else {
2256 locations->AddTemp(Location::RequiresRegister());
2257 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002258 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002259 locations->SetInAt(0, Location::RequiresRegister());
2260 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2261 locations->SetOut(Location::RequiresFpuRegister());
2262 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002263 // The output overlaps for an object field get when read barriers
2264 // are enabled: we do not want the load to overwrite the object's
2265 // location, as we need it to emit the read barrier.
2266 locations->SetOut(
2267 Location::RequiresRegister(),
2268 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002269 }
2270}
2271
2272void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2273 const FieldInfo& field_info) {
2274 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002275 LocationSummary* locations = instruction->GetLocations();
2276 Location base_loc = locations->InAt(0);
2277 Location out = locations->Out();
2278 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002279 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002280 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002281
Roland Levillain44015862016-01-22 11:47:17 +00002282 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2283 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002284 // /* HeapReference<Object> */ out = *(base + offset)
2285 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002286 Location maybe_temp =
2287 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002288 // Note that potential implicit null checks are handled in this
2289 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2290 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2291 instruction,
2292 out,
2293 base,
2294 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002295 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002296 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002297 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002298 } else {
2299 // General case.
2300 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002301 // Note that a potential implicit null check is handled in this
2302 // CodeGeneratorARM64::LoadAcquire call.
2303 // NB: LoadAcquire will record the pc info if needed.
2304 codegen_->LoadAcquire(
2305 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002306 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002307 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2308 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002309 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002310 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002311 }
Roland Levillain44015862016-01-22 11:47:17 +00002312 if (field_type == Primitive::kPrimNot) {
2313 // If read barriers are enabled, emit read barriers other than
2314 // Baker's using a slow path (and also unpoison the loaded
2315 // reference, if heap poisoning is enabled).
2316 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2317 }
Roland Levillain4d027112015-07-01 15:41:14 +01002318 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002319}
2320
2321void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2322 LocationSummary* locations =
2323 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2324 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002325 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2326 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2327 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002328 locations->SetInAt(1, Location::RequiresFpuRegister());
2329 } else {
2330 locations->SetInAt(1, Location::RequiresRegister());
2331 }
2332}
2333
2334void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002335 const FieldInfo& field_info,
2336 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002337 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2338
2339 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002340 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002341 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002342 Offset offset = field_info.GetFieldOffset();
2343 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002344
Roland Levillain4d027112015-07-01 15:41:14 +01002345 {
2346 // We use a block to end the scratch scope before the write barrier, thus
2347 // freeing the temporary registers so they can be used in `MarkGCCard`.
2348 UseScratchRegisterScope temps(GetVIXLAssembler());
2349
2350 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2351 DCHECK(value.IsW());
2352 Register temp = temps.AcquireW();
2353 __ Mov(temp, value.W());
2354 GetAssembler()->PoisonHeapReference(temp.W());
2355 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002356 }
Roland Levillain4d027112015-07-01 15:41:14 +01002357
2358 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002359 codegen_->StoreRelease(
2360 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002361 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002362 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2363 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002364 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2365 codegen_->MaybeRecordImplicitNullCheck(instruction);
2366 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002367 }
2368
2369 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002370 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002371 }
2372}
2373
Alexandre Rames67555f72014-11-18 10:55:16 +00002374void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002375 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002376
2377 switch (type) {
2378 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002379 case Primitive::kPrimLong: {
2380 Register dst = OutputRegister(instr);
2381 Register lhs = InputRegisterAt(instr, 0);
2382 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002383 if (instr->IsAdd()) {
2384 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002385 } else if (instr->IsAnd()) {
2386 __ And(dst, lhs, rhs);
2387 } else if (instr->IsOr()) {
2388 __ Orr(dst, lhs, rhs);
2389 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002390 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002391 } else if (instr->IsRor()) {
2392 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002393 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002394 __ Ror(dst, lhs, shift);
2395 } else {
2396 // Ensure shift distance is in the same size register as the result. If
2397 // we are rotating a long and the shift comes in a w register originally,
2398 // we don't need to sxtw for use as an x since the shift distances are
2399 // all & reg_bits - 1.
2400 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2401 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002402 } else {
2403 DCHECK(instr->IsXor());
2404 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002405 }
2406 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002407 }
2408 case Primitive::kPrimFloat:
2409 case Primitive::kPrimDouble: {
2410 FPRegister dst = OutputFPRegister(instr);
2411 FPRegister lhs = InputFPRegisterAt(instr, 0);
2412 FPRegister rhs = InputFPRegisterAt(instr, 1);
2413 if (instr->IsAdd()) {
2414 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002415 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002416 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002417 } else {
2418 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002419 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002420 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002421 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002422 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002423 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002424 }
2425}
2426
Serban Constantinescu02164b32014-11-13 14:05:07 +00002427void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2428 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2429
2430 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2431 Primitive::Type type = instr->GetResultType();
2432 switch (type) {
2433 case Primitive::kPrimInt:
2434 case Primitive::kPrimLong: {
2435 locations->SetInAt(0, Location::RequiresRegister());
2436 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002437 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002438 break;
2439 }
2440 default:
2441 LOG(FATAL) << "Unexpected shift type " << type;
2442 }
2443}
2444
2445void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2446 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2447
2448 Primitive::Type type = instr->GetType();
2449 switch (type) {
2450 case Primitive::kPrimInt:
2451 case Primitive::kPrimLong: {
2452 Register dst = OutputRegister(instr);
2453 Register lhs = InputRegisterAt(instr, 0);
2454 Operand rhs = InputOperandAt(instr, 1);
2455 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002456 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002457 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002458 if (instr->IsShl()) {
2459 __ Lsl(dst, lhs, shift_value);
2460 } else if (instr->IsShr()) {
2461 __ Asr(dst, lhs, shift_value);
2462 } else {
2463 __ Lsr(dst, lhs, shift_value);
2464 }
2465 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002466 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002467
2468 if (instr->IsShl()) {
2469 __ Lsl(dst, lhs, rhs_reg);
2470 } else if (instr->IsShr()) {
2471 __ Asr(dst, lhs, rhs_reg);
2472 } else {
2473 __ Lsr(dst, lhs, rhs_reg);
2474 }
2475 }
2476 break;
2477 }
2478 default:
2479 LOG(FATAL) << "Unexpected shift operation type " << type;
2480 }
2481}
2482
Alexandre Rames5319def2014-10-23 10:03:10 +01002483void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002484 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002485}
2486
2487void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002488 HandleBinaryOp(instruction);
2489}
2490
2491void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2492 HandleBinaryOp(instruction);
2493}
2494
2495void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2496 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002497}
2498
Artem Serov7fc63502016-02-09 17:15:29 +00002499void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002500 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2501 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2502 locations->SetInAt(0, Location::RequiresRegister());
2503 // There is no immediate variant of negated bitwise instructions in AArch64.
2504 locations->SetInAt(1, Location::RequiresRegister());
2505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2506}
2507
Artem Serov7fc63502016-02-09 17:15:29 +00002508void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002509 Register dst = OutputRegister(instr);
2510 Register lhs = InputRegisterAt(instr, 0);
2511 Register rhs = InputRegisterAt(instr, 1);
2512
2513 switch (instr->GetOpKind()) {
2514 case HInstruction::kAnd:
2515 __ Bic(dst, lhs, rhs);
2516 break;
2517 case HInstruction::kOr:
2518 __ Orn(dst, lhs, rhs);
2519 break;
2520 case HInstruction::kXor:
2521 __ Eon(dst, lhs, rhs);
2522 break;
2523 default:
2524 LOG(FATAL) << "Unreachable";
2525 }
2526}
2527
Anton Kirilov74234da2017-01-13 14:42:47 +00002528void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2529 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002530 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2531 instruction->GetType() == Primitive::kPrimLong);
2532 LocationSummary* locations =
2533 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2534 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2535 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2536 } else {
2537 locations->SetInAt(0, Location::RequiresRegister());
2538 }
2539 locations->SetInAt(1, Location::RequiresRegister());
2540 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2541}
2542
Anton Kirilov74234da2017-01-13 14:42:47 +00002543void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2544 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002545 Primitive::Type type = instruction->GetType();
2546 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2547 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2548 Register out = OutputRegister(instruction);
2549 Register left;
2550 if (kind != HInstruction::kNeg) {
2551 left = InputRegisterAt(instruction, 0);
2552 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002553 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002554 // shifter operand operation, the IR generating `right_reg` (input to the type
2555 // conversion) can have a different type from the current instruction's type,
2556 // so we manually indicate the type.
2557 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002558 Operand right_operand(0);
2559
Anton Kirilov74234da2017-01-13 14:42:47 +00002560 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2561 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002562 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2563 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002564 right_operand = Operand(right_reg,
2565 helpers::ShiftFromOpKind(op_kind),
2566 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002567 }
2568
2569 // Logical binary operations do not support extension operations in the
2570 // operand. Note that VIXL would still manage if it was passed by generating
2571 // the extension as a separate instruction.
2572 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2573 DCHECK(!right_operand.IsExtendedRegister() ||
2574 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2575 kind != HInstruction::kNeg));
2576 switch (kind) {
2577 case HInstruction::kAdd:
2578 __ Add(out, left, right_operand);
2579 break;
2580 case HInstruction::kAnd:
2581 __ And(out, left, right_operand);
2582 break;
2583 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002584 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002585 __ Neg(out, right_operand);
2586 break;
2587 case HInstruction::kOr:
2588 __ Orr(out, left, right_operand);
2589 break;
2590 case HInstruction::kSub:
2591 __ Sub(out, left, right_operand);
2592 break;
2593 case HInstruction::kXor:
2594 __ Eor(out, left, right_operand);
2595 break;
2596 default:
2597 LOG(FATAL) << "Unexpected operation kind: " << kind;
2598 UNREACHABLE();
2599 }
2600}
2601
Artem Serov328429f2016-07-06 16:23:04 +01002602void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002603 LocationSummary* locations =
2604 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2605 locations->SetInAt(0, Location::RequiresRegister());
2606 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002607 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002608}
2609
Roland Levillain19c54192016-11-04 13:44:09 +00002610void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002611 __ Add(OutputRegister(instruction),
2612 InputRegisterAt(instruction, 0),
2613 Operand(InputOperandAt(instruction, 1)));
2614}
2615
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002616void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002617 LocationSummary* locations =
2618 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002619 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2620 if (instr->GetOpKind() == HInstruction::kSub &&
2621 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002622 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002623 // Don't allocate register for Mneg instruction.
2624 } else {
2625 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2626 Location::RequiresRegister());
2627 }
2628 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2629 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002630 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2631}
2632
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002633void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002634 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002635 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2636 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002637
2638 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2639 // This fixup should be carried out for all multiply-accumulate instructions:
2640 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2641 if (instr->GetType() == Primitive::kPrimLong &&
2642 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2643 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002644 vixl::aarch64::Instruction* prev =
2645 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002646 if (prev->IsLoadOrStore()) {
2647 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002648 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002649 __ nop();
2650 }
2651 }
2652
2653 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002654 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002655 __ Madd(res, mul_left, mul_right, accumulator);
2656 } else {
2657 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002658 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002659 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002660 __ Mneg(res, mul_left, mul_right);
2661 } else {
2662 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2663 __ Msub(res, mul_left, mul_right, accumulator);
2664 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002665 }
2666}
2667
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002668void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002669 bool object_array_get_with_read_barrier =
2670 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002671 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002672 new (GetGraph()->GetArena()) LocationSummary(instruction,
2673 object_array_get_with_read_barrier ?
2674 LocationSummary::kCallOnSlowPath :
2675 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002676 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002677 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002678 // We need a temporary register for the read barrier marking slow
2679 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002680 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2681 !Runtime::Current()->UseJitCompilation() &&
2682 instruction->GetIndex()->IsConstant()) {
2683 // Array loads with constant index are treated as field loads.
2684 // If link-time thunks for the Baker read barrier are enabled, for AOT
2685 // constant index loads we need a temporary only if the offset is too big.
2686 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2687 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2688 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2689 if (offset >= kReferenceLoadMinFarOffset) {
2690 locations->AddTemp(FixedTempLocation());
2691 }
2692 } else {
2693 locations->AddTemp(Location::RequiresRegister());
2694 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002695 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002696 locations->SetInAt(0, Location::RequiresRegister());
2697 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002698 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2699 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2700 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002701 // The output overlaps in the case of an object array get with
2702 // read barriers enabled: we do not want the move to overwrite the
2703 // array's location, as we need it to emit the read barrier.
2704 locations->SetOut(
2705 Location::RequiresRegister(),
2706 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002707 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002708}
2709
2710void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002711 Primitive::Type type = instruction->GetType();
2712 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002713 LocationSummary* locations = instruction->GetLocations();
2714 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002715 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002716 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002717 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2718 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002719 MacroAssembler* masm = GetVIXLAssembler();
2720 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002721
Roland Levillain19c54192016-11-04 13:44:09 +00002722 // The read barrier instrumentation of object ArrayGet instructions
2723 // does not support the HIntermediateAddress instruction.
2724 DCHECK(!((type == Primitive::kPrimNot) &&
2725 instruction->GetArray()->IsIntermediateAddress() &&
2726 kEmitCompilerReadBarrier));
2727
Roland Levillain44015862016-01-22 11:47:17 +00002728 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2729 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002730 // Note that a potential implicit null check is handled in the
2731 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002732 if (index.IsConstant()) {
2733 // Array load with a constant index can be treated as a field load.
2734 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2735 Location maybe_temp =
2736 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2737 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2738 out,
2739 obj.W(),
2740 offset,
2741 maybe_temp,
2742 /* needs_null_check */ true,
2743 /* use_load_acquire */ false);
2744 } else {
2745 Register temp = WRegisterFrom(locations->GetTemp(0));
2746 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2747 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
2748 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002749 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002750 // General case.
2751 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002752 Register length;
2753 if (maybe_compressed_char_at) {
2754 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2755 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002756 {
2757 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2758 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2759
2760 if (instruction->GetArray()->IsIntermediateAddress()) {
2761 DCHECK_LT(count_offset, offset);
2762 int64_t adjusted_offset =
2763 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2764 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2765 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2766 } else {
2767 __ Ldr(length, HeapOperand(obj, count_offset));
2768 }
2769 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002770 }
jessicahandojo05765752016-09-09 19:01:32 -07002771 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002772 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002773 if (maybe_compressed_char_at) {
2774 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002775 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2776 "Expecting 0=compressed, 1=uncompressed");
2777 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002778 __ Ldrb(Register(OutputCPURegister(instruction)),
2779 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2780 __ B(&done);
2781 __ Bind(&uncompressed_load);
2782 __ Ldrh(Register(OutputCPURegister(instruction)),
2783 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2784 __ Bind(&done);
2785 } else {
2786 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2787 source = HeapOperand(obj, offset);
2788 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002789 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002790 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002791 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002792 // We do not need to compute the intermediate address from the array: the
2793 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002794 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002795 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002796 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002797 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2798 }
2799 temp = obj;
2800 } else {
2801 __ Add(temp, obj, offset);
2802 }
jessicahandojo05765752016-09-09 19:01:32 -07002803 if (maybe_compressed_char_at) {
2804 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002805 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2806 "Expecting 0=compressed, 1=uncompressed");
2807 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002808 __ Ldrb(Register(OutputCPURegister(instruction)),
2809 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2810 __ B(&done);
2811 __ Bind(&uncompressed_load);
2812 __ Ldrh(Register(OutputCPURegister(instruction)),
2813 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2814 __ Bind(&done);
2815 } else {
2816 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2817 }
Roland Levillain44015862016-01-22 11:47:17 +00002818 }
jessicahandojo05765752016-09-09 19:01:32 -07002819 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002820 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2821 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002822 codegen_->Load(type, OutputCPURegister(instruction), source);
2823 codegen_->MaybeRecordImplicitNullCheck(instruction);
2824 }
Roland Levillain44015862016-01-22 11:47:17 +00002825
2826 if (type == Primitive::kPrimNot) {
2827 static_assert(
2828 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2829 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2830 Location obj_loc = locations->InAt(0);
2831 if (index.IsConstant()) {
2832 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2833 } else {
2834 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2835 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002836 }
Roland Levillain4d027112015-07-01 15:41:14 +01002837 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002838}
2839
Alexandre Rames5319def2014-10-23 10:03:10 +01002840void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2841 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2842 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002843 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002844}
2845
2846void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002847 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002848 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002849 {
2850 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2851 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2852 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2853 codegen_->MaybeRecordImplicitNullCheck(instruction);
2854 }
jessicahandojo05765752016-09-09 19:01:32 -07002855 // Mask out compression flag from String's array length.
2856 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002857 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002858 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002859}
2860
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002861void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002862 Primitive::Type value_type = instruction->GetComponentType();
2863
2864 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002865 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2866 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002867 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002868 LocationSummary::kCallOnSlowPath :
2869 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002870 locations->SetInAt(0, Location::RequiresRegister());
2871 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002872 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2873 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2874 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002875 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002876 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002877 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002878 }
2879}
2880
2881void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2882 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002883 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002884 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002885 bool needs_write_barrier =
2886 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002887
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002888 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002889 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002890 CPURegister source = value;
2891 Location index = locations->InAt(1);
2892 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2893 MemOperand destination = HeapOperand(array);
2894 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002895
2896 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002897 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002898 if (index.IsConstant()) {
2899 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2900 destination = HeapOperand(array, offset);
2901 } else {
2902 UseScratchRegisterScope temps(masm);
2903 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002904 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002905 // We do not need to compute the intermediate address from the array: the
2906 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002907 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002908 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002909 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002910 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2911 }
2912 temp = array;
2913 } else {
2914 __ Add(temp, array, offset);
2915 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002916 destination = HeapOperand(temp,
2917 XRegisterFrom(index),
2918 LSL,
2919 Primitive::ComponentSizeShift(value_type));
2920 }
Artem Serov914d7a82017-02-07 14:33:49 +00002921 {
2922 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2923 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2924 codegen_->Store(value_type, value, destination);
2925 codegen_->MaybeRecordImplicitNullCheck(instruction);
2926 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002927 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002928 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002929 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002930 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002931 {
2932 // We use a block to end the scratch scope before the write barrier, thus
2933 // freeing the temporary registers so they can be used in `MarkGCCard`.
2934 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002935 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002936 if (index.IsConstant()) {
2937 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002938 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002939 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002940 destination = HeapOperand(temp,
2941 XRegisterFrom(index),
2942 LSL,
2943 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002944 }
2945
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002946 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2947 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2948 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2949
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002950 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002951 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2952 codegen_->AddSlowPath(slow_path);
2953 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002954 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002955 __ Cbnz(Register(value), &non_zero);
2956 if (!index.IsConstant()) {
2957 __ Add(temp, array, offset);
2958 }
Artem Serov914d7a82017-02-07 14:33:49 +00002959 {
2960 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
2961 // emitted.
2962 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2963 __ Str(wzr, destination);
2964 codegen_->MaybeRecordImplicitNullCheck(instruction);
2965 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002966 __ B(&done);
2967 __ Bind(&non_zero);
2968 }
2969
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002970 // Note that when Baker read barriers are enabled, the type
2971 // checks are performed without read barriers. This is fine,
2972 // even in the case where a class object is in the from-space
2973 // after the flip, as a comparison involving such a type would
2974 // not produce a false positive; it may of course produce a
2975 // false negative, in which case we would take the ArraySet
2976 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01002977
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002978 Register temp2 = temps.AcquireSameSizeAs(array);
2979 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00002980 {
2981 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2982 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2983 __ Ldr(temp, HeapOperand(array, class_offset));
2984 codegen_->MaybeRecordImplicitNullCheck(instruction);
2985 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002986 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01002987
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002988 // /* HeapReference<Class> */ temp = temp->component_type_
2989 __ Ldr(temp, HeapOperand(temp, component_offset));
2990 // /* HeapReference<Class> */ temp2 = value->klass_
2991 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2992 // If heap poisoning is enabled, no need to unpoison `temp`
2993 // nor `temp2`, as we are comparing two poisoned references.
2994 __ Cmp(temp, temp2);
2995 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01002996
Roland Levillain9d6e1f82016-09-05 15:57:33 +01002997 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2998 vixl::aarch64::Label do_put;
2999 __ B(eq, &do_put);
3000 // If heap poisoning is enabled, the `temp` reference has
3001 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003002 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3003
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003004 // /* HeapReference<Class> */ temp = temp->super_class_
3005 __ Ldr(temp, HeapOperand(temp, super_offset));
3006 // If heap poisoning is enabled, no need to unpoison
3007 // `temp`, as we are comparing against null below.
3008 __ Cbnz(temp, slow_path->GetEntryLabel());
3009 __ Bind(&do_put);
3010 } else {
3011 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003012 }
3013 }
3014
3015 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003016 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003017 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003018 __ Mov(temp2, value.W());
3019 GetAssembler()->PoisonHeapReference(temp2);
3020 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003021 }
3022
3023 if (!index.IsConstant()) {
3024 __ Add(temp, array, offset);
3025 }
Artem Serov914d7a82017-02-07 14:33:49 +00003026 {
3027 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3028 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3029 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003030
Artem Serov914d7a82017-02-07 14:33:49 +00003031 if (!may_need_runtime_call_for_type_check) {
3032 codegen_->MaybeRecordImplicitNullCheck(instruction);
3033 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003034 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003035 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003036
3037 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3038
3039 if (done.IsLinked()) {
3040 __ Bind(&done);
3041 }
3042
3043 if (slow_path != nullptr) {
3044 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003045 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003046 }
3047}
3048
Alexandre Rames67555f72014-11-18 10:55:16 +00003049void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003050 RegisterSet caller_saves = RegisterSet::Empty();
3051 InvokeRuntimeCallingConvention calling_convention;
3052 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3053 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3054 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003055 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003056 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003057}
3058
3059void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003060 BoundsCheckSlowPathARM64* slow_path =
3061 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003062 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003063 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3064 __ B(slow_path->GetEntryLabel(), hs);
3065}
3066
Alexandre Rames67555f72014-11-18 10:55:16 +00003067void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3068 LocationSummary* locations =
3069 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3070 locations->SetInAt(0, Location::RequiresRegister());
3071 if (check->HasUses()) {
3072 locations->SetOut(Location::SameAsFirstInput());
3073 }
3074}
3075
3076void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3077 // We assume the class is not null.
3078 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3079 check->GetLoadClass(), check, check->GetDexPc(), true);
3080 codegen_->AddSlowPath(slow_path);
3081 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3082}
3083
Roland Levillain1a653882016-03-18 18:05:57 +00003084static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3085 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3086 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3087}
3088
3089void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3090 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3091 Location rhs_loc = instruction->GetLocations()->InAt(1);
3092 if (rhs_loc.IsConstant()) {
3093 // 0.0 is the only immediate that can be encoded directly in
3094 // an FCMP instruction.
3095 //
3096 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3097 // specify that in a floating-point comparison, positive zero
3098 // and negative zero are considered equal, so we can use the
3099 // literal 0.0 for both cases here.
3100 //
3101 // Note however that some methods (Float.equal, Float.compare,
3102 // Float.compareTo, Double.equal, Double.compare,
3103 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3104 // StrictMath.min) consider 0.0 to be (strictly) greater than
3105 // -0.0. So if we ever translate calls to these methods into a
3106 // HCompare instruction, we must handle the -0.0 case with
3107 // care here.
3108 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3109 __ Fcmp(lhs_reg, 0.0);
3110 } else {
3111 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3112 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003113}
3114
Serban Constantinescu02164b32014-11-13 14:05:07 +00003115void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003116 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003117 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3118 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003119 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003120 case Primitive::kPrimBoolean:
3121 case Primitive::kPrimByte:
3122 case Primitive::kPrimShort:
3123 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003124 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003125 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003126 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003127 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003128 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3129 break;
3130 }
3131 case Primitive::kPrimFloat:
3132 case Primitive::kPrimDouble: {
3133 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003134 locations->SetInAt(1,
3135 IsFloatingPointZeroConstant(compare->InputAt(1))
3136 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3137 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003138 locations->SetOut(Location::RequiresRegister());
3139 break;
3140 }
3141 default:
3142 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3143 }
3144}
3145
3146void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3147 Primitive::Type in_type = compare->InputAt(0)->GetType();
3148
3149 // 0 if: left == right
3150 // 1 if: left > right
3151 // -1 if: left < right
3152 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003153 case Primitive::kPrimBoolean:
3154 case Primitive::kPrimByte:
3155 case Primitive::kPrimShort:
3156 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003157 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003158 case Primitive::kPrimLong: {
3159 Register result = OutputRegister(compare);
3160 Register left = InputRegisterAt(compare, 0);
3161 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003162 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003163 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3164 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003165 break;
3166 }
3167 case Primitive::kPrimFloat:
3168 case Primitive::kPrimDouble: {
3169 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003170 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003171 __ Cset(result, ne);
3172 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003173 break;
3174 }
3175 default:
3176 LOG(FATAL) << "Unimplemented compare type " << in_type;
3177 }
3178}
3179
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003180void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003181 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003182
3183 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3184 locations->SetInAt(0, Location::RequiresFpuRegister());
3185 locations->SetInAt(1,
3186 IsFloatingPointZeroConstant(instruction->InputAt(1))
3187 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3188 : Location::RequiresFpuRegister());
3189 } else {
3190 // Integer cases.
3191 locations->SetInAt(0, Location::RequiresRegister());
3192 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3193 }
3194
David Brazdilb3e773e2016-01-26 11:28:37 +00003195 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003196 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003197 }
3198}
3199
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003200void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003201 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003202 return;
3203 }
3204
3205 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003206 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003207 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003208
Roland Levillain7f63c522015-07-13 15:54:55 +00003209 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003210 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003211 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003212 } else {
3213 // Integer cases.
3214 Register lhs = InputRegisterAt(instruction, 0);
3215 Operand rhs = InputOperandAt(instruction, 1);
3216 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003217 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003218 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003219}
3220
3221#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3222 M(Equal) \
3223 M(NotEqual) \
3224 M(LessThan) \
3225 M(LessThanOrEqual) \
3226 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003227 M(GreaterThanOrEqual) \
3228 M(Below) \
3229 M(BelowOrEqual) \
3230 M(Above) \
3231 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003232#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003233void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3234void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003235FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003236#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003237#undef FOR_EACH_CONDITION_INSTRUCTION
3238
Zheng Xuc6667102015-05-15 16:08:45 +08003239void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3240 DCHECK(instruction->IsDiv() || instruction->IsRem());
3241
3242 LocationSummary* locations = instruction->GetLocations();
3243 Location second = locations->InAt(1);
3244 DCHECK(second.IsConstant());
3245
3246 Register out = OutputRegister(instruction);
3247 Register dividend = InputRegisterAt(instruction, 0);
3248 int64_t imm = Int64FromConstant(second.GetConstant());
3249 DCHECK(imm == 1 || imm == -1);
3250
3251 if (instruction->IsRem()) {
3252 __ Mov(out, 0);
3253 } else {
3254 if (imm == 1) {
3255 __ Mov(out, dividend);
3256 } else {
3257 __ Neg(out, dividend);
3258 }
3259 }
3260}
3261
3262void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3263 DCHECK(instruction->IsDiv() || instruction->IsRem());
3264
3265 LocationSummary* locations = instruction->GetLocations();
3266 Location second = locations->InAt(1);
3267 DCHECK(second.IsConstant());
3268
3269 Register out = OutputRegister(instruction);
3270 Register dividend = InputRegisterAt(instruction, 0);
3271 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003272 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003273 int ctz_imm = CTZ(abs_imm);
3274
3275 UseScratchRegisterScope temps(GetVIXLAssembler());
3276 Register temp = temps.AcquireSameSizeAs(out);
3277
3278 if (instruction->IsDiv()) {
3279 __ Add(temp, dividend, abs_imm - 1);
3280 __ Cmp(dividend, 0);
3281 __ Csel(out, temp, dividend, lt);
3282 if (imm > 0) {
3283 __ Asr(out, out, ctz_imm);
3284 } else {
3285 __ Neg(out, Operand(out, ASR, ctz_imm));
3286 }
3287 } else {
3288 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3289 __ Asr(temp, dividend, bits - 1);
3290 __ Lsr(temp, temp, bits - ctz_imm);
3291 __ Add(out, dividend, temp);
3292 __ And(out, out, abs_imm - 1);
3293 __ Sub(out, out, temp);
3294 }
3295}
3296
3297void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3298 DCHECK(instruction->IsDiv() || instruction->IsRem());
3299
3300 LocationSummary* locations = instruction->GetLocations();
3301 Location second = locations->InAt(1);
3302 DCHECK(second.IsConstant());
3303
3304 Register out = OutputRegister(instruction);
3305 Register dividend = InputRegisterAt(instruction, 0);
3306 int64_t imm = Int64FromConstant(second.GetConstant());
3307
3308 Primitive::Type type = instruction->GetResultType();
3309 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3310
3311 int64_t magic;
3312 int shift;
3313 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3314
3315 UseScratchRegisterScope temps(GetVIXLAssembler());
3316 Register temp = temps.AcquireSameSizeAs(out);
3317
3318 // temp = get_high(dividend * magic)
3319 __ Mov(temp, magic);
3320 if (type == Primitive::kPrimLong) {
3321 __ Smulh(temp, dividend, temp);
3322 } else {
3323 __ Smull(temp.X(), dividend, temp);
3324 __ Lsr(temp.X(), temp.X(), 32);
3325 }
3326
3327 if (imm > 0 && magic < 0) {
3328 __ Add(temp, temp, dividend);
3329 } else if (imm < 0 && magic > 0) {
3330 __ Sub(temp, temp, dividend);
3331 }
3332
3333 if (shift != 0) {
3334 __ Asr(temp, temp, shift);
3335 }
3336
3337 if (instruction->IsDiv()) {
3338 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3339 } else {
3340 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3341 // TODO: Strength reduction for msub.
3342 Register temp_imm = temps.AcquireSameSizeAs(out);
3343 __ Mov(temp_imm, imm);
3344 __ Msub(out, temp, temp_imm, dividend);
3345 }
3346}
3347
3348void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3349 DCHECK(instruction->IsDiv() || instruction->IsRem());
3350 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003351 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003352
3353 LocationSummary* locations = instruction->GetLocations();
3354 Register out = OutputRegister(instruction);
3355 Location second = locations->InAt(1);
3356
3357 if (second.IsConstant()) {
3358 int64_t imm = Int64FromConstant(second.GetConstant());
3359
3360 if (imm == 0) {
3361 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3362 } else if (imm == 1 || imm == -1) {
3363 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003364 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003365 DivRemByPowerOfTwo(instruction);
3366 } else {
3367 DCHECK(imm <= -2 || imm >= 2);
3368 GenerateDivRemWithAnyConstant(instruction);
3369 }
3370 } else {
3371 Register dividend = InputRegisterAt(instruction, 0);
3372 Register divisor = InputRegisterAt(instruction, 1);
3373 if (instruction->IsDiv()) {
3374 __ Sdiv(out, dividend, divisor);
3375 } else {
3376 UseScratchRegisterScope temps(GetVIXLAssembler());
3377 Register temp = temps.AcquireSameSizeAs(out);
3378 __ Sdiv(temp, dividend, divisor);
3379 __ Msub(out, temp, divisor, dividend);
3380 }
3381 }
3382}
3383
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003384void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3385 LocationSummary* locations =
3386 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3387 switch (div->GetResultType()) {
3388 case Primitive::kPrimInt:
3389 case Primitive::kPrimLong:
3390 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003391 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003392 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3393 break;
3394
3395 case Primitive::kPrimFloat:
3396 case Primitive::kPrimDouble:
3397 locations->SetInAt(0, Location::RequiresFpuRegister());
3398 locations->SetInAt(1, Location::RequiresFpuRegister());
3399 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3400 break;
3401
3402 default:
3403 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3404 }
3405}
3406
3407void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3408 Primitive::Type type = div->GetResultType();
3409 switch (type) {
3410 case Primitive::kPrimInt:
3411 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003412 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003413 break;
3414
3415 case Primitive::kPrimFloat:
3416 case Primitive::kPrimDouble:
3417 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3418 break;
3419
3420 default:
3421 LOG(FATAL) << "Unexpected div type " << type;
3422 }
3423}
3424
Alexandre Rames67555f72014-11-18 10:55:16 +00003425void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003426 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003427 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003428}
3429
3430void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3431 SlowPathCodeARM64* slow_path =
3432 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3433 codegen_->AddSlowPath(slow_path);
3434 Location value = instruction->GetLocations()->InAt(0);
3435
Alexandre Rames3e69f162014-12-10 10:36:50 +00003436 Primitive::Type type = instruction->GetType();
3437
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003438 if (!Primitive::IsIntegralType(type)) {
3439 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003440 return;
3441 }
3442
Alexandre Rames67555f72014-11-18 10:55:16 +00003443 if (value.IsConstant()) {
3444 int64_t divisor = Int64ConstantFrom(value);
3445 if (divisor == 0) {
3446 __ B(slow_path->GetEntryLabel());
3447 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003448 // A division by a non-null constant is valid. We don't need to perform
3449 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003450 }
3451 } else {
3452 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3453 }
3454}
3455
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003456void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3457 LocationSummary* locations =
3458 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3459 locations->SetOut(Location::ConstantLocation(constant));
3460}
3461
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003462void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3463 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003464 // Will be generated at use site.
3465}
3466
Alexandre Rames5319def2014-10-23 10:03:10 +01003467void LocationsBuilderARM64::VisitExit(HExit* exit) {
3468 exit->SetLocations(nullptr);
3469}
3470
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003471void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003472}
3473
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003474void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3475 LocationSummary* locations =
3476 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3477 locations->SetOut(Location::ConstantLocation(constant));
3478}
3479
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003480void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003481 // Will be generated at use site.
3482}
3483
David Brazdilfc6a86a2015-06-26 10:33:45 +00003484void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003485 DCHECK(!successor->IsExitBlock());
3486 HBasicBlock* block = got->GetBlock();
3487 HInstruction* previous = got->GetPrevious();
3488 HLoopInformation* info = block->GetLoopInformation();
3489
David Brazdil46e2a392015-03-16 17:31:52 +00003490 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003491 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3492 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3493 return;
3494 }
3495 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3496 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3497 }
3498 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003499 __ B(codegen_->GetLabelOf(successor));
3500 }
3501}
3502
David Brazdilfc6a86a2015-06-26 10:33:45 +00003503void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3504 got->SetLocations(nullptr);
3505}
3506
3507void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3508 HandleGoto(got, got->GetSuccessor());
3509}
3510
3511void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3512 try_boundary->SetLocations(nullptr);
3513}
3514
3515void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3516 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3517 if (!successor->IsExitBlock()) {
3518 HandleGoto(try_boundary, successor);
3519 }
3520}
3521
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003522void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003523 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003524 vixl::aarch64::Label* true_target,
3525 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003526 // FP branching requires both targets to be explicit. If either of the targets
3527 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003528 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003529 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003530
David Brazdil0debae72015-11-12 18:37:00 +00003531 if (true_target == nullptr && false_target == nullptr) {
3532 // Nothing to do. The code always falls through.
3533 return;
3534 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003535 // Constant condition, statically compared against "true" (integer value 1).
3536 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003537 if (true_target != nullptr) {
3538 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003539 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003540 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003541 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003542 if (false_target != nullptr) {
3543 __ B(false_target);
3544 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003545 }
David Brazdil0debae72015-11-12 18:37:00 +00003546 return;
3547 }
3548
3549 // The following code generates these patterns:
3550 // (1) true_target == nullptr && false_target != nullptr
3551 // - opposite condition true => branch to false_target
3552 // (2) true_target != nullptr && false_target == nullptr
3553 // - condition true => branch to true_target
3554 // (3) true_target != nullptr && false_target != nullptr
3555 // - condition true => branch to true_target
3556 // - branch to false_target
3557 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003558 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003559 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003560 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003561 if (true_target == nullptr) {
3562 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3563 } else {
3564 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3565 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003566 } else {
3567 // The condition instruction has not been materialized, use its inputs as
3568 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003569 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003570
David Brazdil0debae72015-11-12 18:37:00 +00003571 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003572 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003573 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003574 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003575 IfCondition opposite_condition = condition->GetOppositeCondition();
3576 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003577 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003578 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003579 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003580 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003581 // Integer cases.
3582 Register lhs = InputRegisterAt(condition, 0);
3583 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003584
3585 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003586 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003587 if (true_target == nullptr) {
3588 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3589 non_fallthrough_target = false_target;
3590 } else {
3591 arm64_cond = ARM64Condition(condition->GetCondition());
3592 non_fallthrough_target = true_target;
3593 }
3594
Aart Bik086d27e2016-01-20 17:02:00 -08003595 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003596 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003597 switch (arm64_cond) {
3598 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003599 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003600 break;
3601 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003602 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003603 break;
3604 case lt:
3605 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003606 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003607 break;
3608 case ge:
3609 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003610 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003611 break;
3612 default:
3613 // Without the `static_cast` the compiler throws an error for
3614 // `-Werror=sign-promo`.
3615 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3616 }
3617 } else {
3618 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003619 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003620 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003621 }
3622 }
David Brazdil0debae72015-11-12 18:37:00 +00003623
3624 // If neither branch falls through (case 3), the conditional branch to `true_target`
3625 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3626 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003627 __ B(false_target);
3628 }
David Brazdil0debae72015-11-12 18:37:00 +00003629
3630 if (fallthrough_target.IsLinked()) {
3631 __ Bind(&fallthrough_target);
3632 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003633}
3634
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003635void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3636 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003637 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003638 locations->SetInAt(0, Location::RequiresRegister());
3639 }
3640}
3641
3642void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003643 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3644 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003645 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3646 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3647 true_target = nullptr;
3648 }
3649 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3650 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3651 false_target = nullptr;
3652 }
David Brazdil0debae72015-11-12 18:37:00 +00003653 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003654}
3655
3656void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3657 LocationSummary* locations = new (GetGraph()->GetArena())
3658 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003659 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003660 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003661 locations->SetInAt(0, Location::RequiresRegister());
3662 }
3663}
3664
3665void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003666 SlowPathCodeARM64* slow_path =
3667 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003668 GenerateTestAndBranch(deoptimize,
3669 /* condition_input_index */ 0,
3670 slow_path->GetEntryLabel(),
3671 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003672}
3673
Mingyao Yang063fc772016-08-02 11:02:54 -07003674void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3675 LocationSummary* locations = new (GetGraph()->GetArena())
3676 LocationSummary(flag, LocationSummary::kNoCall);
3677 locations->SetOut(Location::RequiresRegister());
3678}
3679
3680void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3681 __ Ldr(OutputRegister(flag),
3682 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3683}
3684
David Brazdilc0b601b2016-02-08 14:20:45 +00003685static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3686 return condition->IsCondition() &&
3687 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3688}
3689
Alexandre Rames880f1192016-06-13 16:04:50 +01003690static inline Condition GetConditionForSelect(HCondition* condition) {
3691 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003692 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3693 : ARM64Condition(cond);
3694}
3695
David Brazdil74eb1b22015-12-14 11:44:01 +00003696void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3697 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003698 if (Primitive::IsFloatingPointType(select->GetType())) {
3699 locations->SetInAt(0, Location::RequiresFpuRegister());
3700 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003701 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003702 } else {
3703 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3704 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3705 bool is_true_value_constant = cst_true_value != nullptr;
3706 bool is_false_value_constant = cst_false_value != nullptr;
3707 // Ask VIXL whether we should synthesize constants in registers.
3708 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3709 Operand true_op = is_true_value_constant ?
3710 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3711 Operand false_op = is_false_value_constant ?
3712 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3713 bool true_value_in_register = false;
3714 bool false_value_in_register = false;
3715 MacroAssembler::GetCselSynthesisInformation(
3716 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3717 true_value_in_register |= !is_true_value_constant;
3718 false_value_in_register |= !is_false_value_constant;
3719
3720 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3721 : Location::ConstantLocation(cst_true_value));
3722 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3723 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003724 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003725 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003726
David Brazdil74eb1b22015-12-14 11:44:01 +00003727 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3728 locations->SetInAt(2, Location::RequiresRegister());
3729 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003730}
3731
3732void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003733 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003734 Condition csel_cond;
3735
3736 if (IsBooleanValueOrMaterializedCondition(cond)) {
3737 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003738 // Use the condition flags set by the previous instruction.
3739 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003740 } else {
3741 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003742 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003743 }
3744 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003745 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003746 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003747 } else {
3748 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003749 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003750 }
3751
Alexandre Rames880f1192016-06-13 16:04:50 +01003752 if (Primitive::IsFloatingPointType(select->GetType())) {
3753 __ Fcsel(OutputFPRegister(select),
3754 InputFPRegisterAt(select, 1),
3755 InputFPRegisterAt(select, 0),
3756 csel_cond);
3757 } else {
3758 __ Csel(OutputRegister(select),
3759 InputOperandAt(select, 1),
3760 InputOperandAt(select, 0),
3761 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003762 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003763}
3764
David Srbecky0cf44932015-12-09 14:09:59 +00003765void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3766 new (GetGraph()->GetArena()) LocationSummary(info);
3767}
3768
David Srbeckyd28f4a02016-03-14 17:14:24 +00003769void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3770 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003771}
3772
3773void CodeGeneratorARM64::GenerateNop() {
3774 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003775}
3776
Alexandre Rames5319def2014-10-23 10:03:10 +01003777void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003778 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003779}
3780
3781void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003782 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003783}
3784
3785void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003786 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003787}
3788
3789void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003790 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003791}
3792
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003793// Temp is used for read barrier.
3794static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3795 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003796 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003797 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3798 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3799 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3800 return 1;
3801 }
3802 return 0;
3803}
3804
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003805// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003806// interface pointer, one for loading the current interface.
3807// The other checks have one temp for loading the object's class.
3808static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3809 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3810 return 3;
3811 }
3812 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003813}
3814
Alexandre Rames67555f72014-11-18 10:55:16 +00003815void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003816 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003817 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003818 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003819 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003820 case TypeCheckKind::kExactCheck:
3821 case TypeCheckKind::kAbstractClassCheck:
3822 case TypeCheckKind::kClassHierarchyCheck:
3823 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003824 call_kind =
3825 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003826 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003827 break;
3828 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003829 case TypeCheckKind::kUnresolvedCheck:
3830 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003831 call_kind = LocationSummary::kCallOnSlowPath;
3832 break;
3833 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003834
Alexandre Rames67555f72014-11-18 10:55:16 +00003835 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003836 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003837 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003838 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003839 locations->SetInAt(0, Location::RequiresRegister());
3840 locations->SetInAt(1, Location::RequiresRegister());
3841 // The "out" register is used as a temporary, so it overlaps with the inputs.
3842 // Note that TypeCheckSlowPathARM64 uses this register too.
3843 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003844 // Add temps if necessary for read barriers.
3845 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003846}
3847
3848void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003849 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003850 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003851 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003852 Register obj = InputRegisterAt(instruction, 0);
3853 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003854 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003855 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003856 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3857 DCHECK_LE(num_temps, 1u);
3858 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003859 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3860 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3861 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3862 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003863
Scott Wakeling97c72b72016-06-24 16:19:36 +01003864 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003865 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003866
3867 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003868 // Avoid null check if we know `obj` is not null.
3869 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003870 __ Cbz(obj, &zero);
3871 }
3872
Roland Levillain44015862016-01-22 11:47:17 +00003873 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003874 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003875 // /* HeapReference<Class> */ out = obj->klass_
3876 GenerateReferenceLoadTwoRegisters(instruction,
3877 out_loc,
3878 obj_loc,
3879 class_offset,
3880 maybe_temp_loc,
3881 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003882 __ Cmp(out, cls);
3883 __ Cset(out, eq);
3884 if (zero.IsLinked()) {
3885 __ B(&done);
3886 }
3887 break;
3888 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003889
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003890 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003891 // /* HeapReference<Class> */ out = obj->klass_
3892 GenerateReferenceLoadTwoRegisters(instruction,
3893 out_loc,
3894 obj_loc,
3895 class_offset,
3896 maybe_temp_loc,
3897 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003898 // If the class is abstract, we eagerly fetch the super class of the
3899 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003900 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003901 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003902 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003903 GenerateReferenceLoadOneRegister(instruction,
3904 out_loc,
3905 super_offset,
3906 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003907 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003908 // If `out` is null, we use it for the result, and jump to `done`.
3909 __ Cbz(out, &done);
3910 __ Cmp(out, cls);
3911 __ B(ne, &loop);
3912 __ Mov(out, 1);
3913 if (zero.IsLinked()) {
3914 __ B(&done);
3915 }
3916 break;
3917 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003918
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003919 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003920 // /* HeapReference<Class> */ out = obj->klass_
3921 GenerateReferenceLoadTwoRegisters(instruction,
3922 out_loc,
3923 obj_loc,
3924 class_offset,
3925 maybe_temp_loc,
3926 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003927 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003928 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003929 __ Bind(&loop);
3930 __ Cmp(out, cls);
3931 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003932 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003933 GenerateReferenceLoadOneRegister(instruction,
3934 out_loc,
3935 super_offset,
3936 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003937 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003938 __ Cbnz(out, &loop);
3939 // If `out` is null, we use it for the result, and jump to `done`.
3940 __ B(&done);
3941 __ Bind(&success);
3942 __ Mov(out, 1);
3943 if (zero.IsLinked()) {
3944 __ B(&done);
3945 }
3946 break;
3947 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003948
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003949 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003950 // /* HeapReference<Class> */ out = obj->klass_
3951 GenerateReferenceLoadTwoRegisters(instruction,
3952 out_loc,
3953 obj_loc,
3954 class_offset,
3955 maybe_temp_loc,
3956 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003957 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003958 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003959 __ Cmp(out, cls);
3960 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003961 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003962 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003963 GenerateReferenceLoadOneRegister(instruction,
3964 out_loc,
3965 component_offset,
3966 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003967 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003968 // If `out` is null, we use it for the result, and jump to `done`.
3969 __ Cbz(out, &done);
3970 __ Ldrh(out, HeapOperand(out, primitive_offset));
3971 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3972 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003973 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003974 __ Mov(out, 1);
3975 __ B(&done);
3976 break;
3977 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003978
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003979 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003980 // No read barrier since the slow path will retry upon failure.
3981 // /* HeapReference<Class> */ out = obj->klass_
3982 GenerateReferenceLoadTwoRegisters(instruction,
3983 out_loc,
3984 obj_loc,
3985 class_offset,
3986 maybe_temp_loc,
3987 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003988 __ Cmp(out, cls);
3989 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003990 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3991 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003992 codegen_->AddSlowPath(slow_path);
3993 __ B(ne, slow_path->GetEntryLabel());
3994 __ Mov(out, 1);
3995 if (zero.IsLinked()) {
3996 __ B(&done);
3997 }
3998 break;
3999 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004000
Calin Juravle98893e12015-10-02 21:05:03 +01004001 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004002 case TypeCheckKind::kInterfaceCheck: {
4003 // Note that we indeed only call on slow path, but we always go
4004 // into the slow path for the unresolved and interface check
4005 // cases.
4006 //
4007 // We cannot directly call the InstanceofNonTrivial runtime
4008 // entry point without resorting to a type checking slow path
4009 // here (i.e. by calling InvokeRuntime directly), as it would
4010 // require to assign fixed registers for the inputs of this
4011 // HInstanceOf instruction (following the runtime calling
4012 // convention), which might be cluttered by the potential first
4013 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004014 //
4015 // TODO: Introduce a new runtime entry point taking the object
4016 // to test (instead of its class) as argument, and let it deal
4017 // with the read barrier issues. This will let us refactor this
4018 // case of the `switch` code as it was previously (with a direct
4019 // call to the runtime not using a type checking slow path).
4020 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004021 DCHECK(locations->OnlyCallsOnSlowPath());
4022 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4023 /* is_fatal */ false);
4024 codegen_->AddSlowPath(slow_path);
4025 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004026 if (zero.IsLinked()) {
4027 __ B(&done);
4028 }
4029 break;
4030 }
4031 }
4032
4033 if (zero.IsLinked()) {
4034 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004035 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004036 }
4037
4038 if (done.IsLinked()) {
4039 __ Bind(&done);
4040 }
4041
4042 if (slow_path != nullptr) {
4043 __ Bind(slow_path->GetExitLabel());
4044 }
4045}
4046
4047void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4048 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4049 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4050
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004051 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4052 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004053 case TypeCheckKind::kExactCheck:
4054 case TypeCheckKind::kAbstractClassCheck:
4055 case TypeCheckKind::kClassHierarchyCheck:
4056 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004057 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4058 LocationSummary::kCallOnSlowPath :
4059 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004060 break;
4061 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004062 case TypeCheckKind::kUnresolvedCheck:
4063 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004064 call_kind = LocationSummary::kCallOnSlowPath;
4065 break;
4066 }
4067
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004068 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4069 locations->SetInAt(0, Location::RequiresRegister());
4070 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004071 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4072 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004073}
4074
4075void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004076 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004077 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004078 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004079 Register obj = InputRegisterAt(instruction, 0);
4080 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004081 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4082 DCHECK_GE(num_temps, 1u);
4083 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004084 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004085 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4086 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004087 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004088 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4089 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4090 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4091 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4092 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4093 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4094 const uint32_t object_array_data_offset =
4095 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004096
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004097 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004098 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4099 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4100 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004101 if (!kEmitCompilerReadBarrier) {
4102 is_type_check_slow_path_fatal =
4103 (type_check_kind == TypeCheckKind::kExactCheck ||
4104 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4105 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4106 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4107 !instruction->CanThrowIntoCatchBlock();
4108 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004109 SlowPathCodeARM64* type_check_slow_path =
4110 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4111 is_type_check_slow_path_fatal);
4112 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004113
Scott Wakeling97c72b72016-06-24 16:19:36 +01004114 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004115 // Avoid null check if we know obj is not null.
4116 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004117 __ Cbz(obj, &done);
4118 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004119
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004120 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004121 case TypeCheckKind::kExactCheck:
4122 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004123 // /* HeapReference<Class> */ temp = obj->klass_
4124 GenerateReferenceLoadTwoRegisters(instruction,
4125 temp_loc,
4126 obj_loc,
4127 class_offset,
4128 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004129 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004130
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004131 __ Cmp(temp, cls);
4132 // Jump to slow path for throwing the exception or doing a
4133 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004134 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004135 break;
4136 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004137
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004138 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004139 // /* HeapReference<Class> */ temp = obj->klass_
4140 GenerateReferenceLoadTwoRegisters(instruction,
4141 temp_loc,
4142 obj_loc,
4143 class_offset,
4144 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004145 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004146
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004147 // If the class is abstract, we eagerly fetch the super class of the
4148 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004149 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004150 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004151 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004152 GenerateReferenceLoadOneRegister(instruction,
4153 temp_loc,
4154 super_offset,
4155 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004156 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004157
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004158 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4159 // exception.
4160 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4161 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004162 __ Cmp(temp, cls);
4163 __ B(ne, &loop);
4164 break;
4165 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004166
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004167 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004168 // /* HeapReference<Class> */ temp = obj->klass_
4169 GenerateReferenceLoadTwoRegisters(instruction,
4170 temp_loc,
4171 obj_loc,
4172 class_offset,
4173 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004174 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004175
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004176 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004177 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004178 __ Bind(&loop);
4179 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004180 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004181
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004182 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004183 GenerateReferenceLoadOneRegister(instruction,
4184 temp_loc,
4185 super_offset,
4186 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004187 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004188
4189 // If the class reference currently in `temp` is not null, jump
4190 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004191 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004192 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004193 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004194 break;
4195 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004196
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004197 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004198 // /* HeapReference<Class> */ temp = obj->klass_
4199 GenerateReferenceLoadTwoRegisters(instruction,
4200 temp_loc,
4201 obj_loc,
4202 class_offset,
4203 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004204 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004205
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004206 // Do an exact check.
4207 __ Cmp(temp, cls);
4208 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004209
4210 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004211 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004212 GenerateReferenceLoadOneRegister(instruction,
4213 temp_loc,
4214 component_offset,
4215 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004216 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004217
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004218 // If the component type is null, jump to the slow path to throw the exception.
4219 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4220 // Otherwise, the object is indeed an array. Further check that this component type is not a
4221 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004222 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4223 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004224 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004225 break;
4226 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004227
Calin Juravle98893e12015-10-02 21:05:03 +01004228 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004229 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004230 //
4231 // We cannot directly call the CheckCast runtime entry point
4232 // without resorting to a type checking slow path here (i.e. by
4233 // calling InvokeRuntime directly), as it would require to
4234 // assign fixed registers for the inputs of this HInstanceOf
4235 // instruction (following the runtime calling convention), which
4236 // might be cluttered by the potential first read barrier
4237 // emission at the beginning of this method.
4238 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004239 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004240 case TypeCheckKind::kInterfaceCheck: {
4241 // /* HeapReference<Class> */ temp = obj->klass_
4242 GenerateReferenceLoadTwoRegisters(instruction,
4243 temp_loc,
4244 obj_loc,
4245 class_offset,
4246 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004247 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004248
4249 // /* HeapReference<Class> */ temp = temp->iftable_
4250 GenerateReferenceLoadTwoRegisters(instruction,
4251 temp_loc,
4252 temp_loc,
4253 iftable_offset,
4254 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004255 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004256 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004257 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004258 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004259 vixl::aarch64::Label start_loop;
4260 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004261 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004262 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4263 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004264 // Go to next interface.
4265 __ Add(temp, temp, 2 * kHeapReferenceSize);
4266 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004267 // Compare the classes and continue the loop if they do not match.
4268 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4269 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004270 break;
4271 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004272 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004273 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004274
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004275 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004276}
4277
Alexandre Rames5319def2014-10-23 10:03:10 +01004278void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4279 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4280 locations->SetOut(Location::ConstantLocation(constant));
4281}
4282
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004283void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004284 // Will be generated at use site.
4285}
4286
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004287void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4288 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4289 locations->SetOut(Location::ConstantLocation(constant));
4290}
4291
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004292void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004293 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004294}
4295
Calin Juravle175dc732015-08-25 15:42:32 +01004296void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4297 // The trampoline uses the same calling convention as dex calling conventions,
4298 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4299 // the method_idx.
4300 HandleInvoke(invoke);
4301}
4302
4303void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4304 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4305}
4306
Alexandre Rames5319def2014-10-23 10:03:10 +01004307void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004308 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004309 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004310}
4311
Alexandre Rames67555f72014-11-18 10:55:16 +00004312void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4313 HandleInvoke(invoke);
4314}
4315
4316void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4317 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004318 LocationSummary* locations = invoke->GetLocations();
4319 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004320 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004321 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004322 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004323
4324 // The register ip1 is required to be used for the hidden argument in
4325 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004326 MacroAssembler* masm = GetVIXLAssembler();
4327 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004328 scratch_scope.Exclude(ip1);
4329 __ Mov(ip1, invoke->GetDexMethodIndex());
4330
Artem Serov914d7a82017-02-07 14:33:49 +00004331 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004332 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004333 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004334 {
4335 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4336 // /* HeapReference<Class> */ temp = temp->klass_
4337 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4338 codegen_->MaybeRecordImplicitNullCheck(invoke);
4339 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004340 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004341 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004342 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004343 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004344 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004345 }
Artem Serov914d7a82017-02-07 14:33:49 +00004346
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004347 // Instead of simply (possibly) unpoisoning `temp` here, we should
4348 // emit a read barrier for the previous class reference load.
4349 // However this is not required in practice, as this is an
4350 // intermediate/temporary reference and because the current
4351 // concurrent copying collector keeps the from-space memory
4352 // intact/accessible until the end of the marking phase (the
4353 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004354 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004355 __ Ldr(temp,
4356 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4357 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004358 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004359 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004360 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004361 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004362 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004363
4364 {
4365 // Ensure the pc position is recorded immediately after the `blr` instruction.
4366 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4367
4368 // lr();
4369 __ blr(lr);
4370 DCHECK(!codegen_->IsLeafMethod());
4371 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4372 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004373}
4374
4375void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004376 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004377 if (intrinsic.TryDispatch(invoke)) {
4378 return;
4379 }
4380
Alexandre Rames67555f72014-11-18 10:55:16 +00004381 HandleInvoke(invoke);
4382}
4383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004384void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004385 // Explicit clinit checks triggered by static invokes must have been pruned by
4386 // art::PrepareForRegisterAllocation.
4387 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004388
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004389 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004390 if (intrinsic.TryDispatch(invoke)) {
4391 return;
4392 }
4393
Alexandre Rames67555f72014-11-18 10:55:16 +00004394 HandleInvoke(invoke);
4395}
4396
Andreas Gampe878d58c2015-01-15 23:24:00 -08004397static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4398 if (invoke->GetLocations()->Intrinsified()) {
4399 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4400 intrinsic.Dispatch(invoke);
4401 return true;
4402 }
4403 return false;
4404}
4405
Vladimir Markodc151b22015-10-15 18:02:30 +01004406HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4407 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004408 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004409 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004410 return desired_dispatch_info;
4411}
4412
TatWai Chongd8c052a2016-11-02 16:12:48 +08004413Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4414 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004415 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004416 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4417 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004418 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4419 uint32_t offset =
4420 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004421 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004422 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004423 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004424 }
Vladimir Marko58155012015-08-19 12:49:41 +00004425 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004426 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004427 break;
4428 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4429 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004430 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004431 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004432 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4433 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004434 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004435 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004436 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004437 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004438 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004439 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004440 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004441 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004442 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004443 }
Vladimir Marko58155012015-08-19 12:49:41 +00004444 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004445 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004446 Register reg = XRegisterFrom(temp);
4447 Register method_reg;
4448 if (current_method.IsRegister()) {
4449 method_reg = XRegisterFrom(current_method);
4450 } else {
4451 DCHECK(invoke->GetLocations()->Intrinsified());
4452 DCHECK(!current_method.IsValid());
4453 method_reg = reg;
4454 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4455 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004456
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004457 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004458 __ Ldr(reg.X(),
4459 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004460 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004461 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004462 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4463 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004464 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4465 break;
4466 }
4467 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004468 return callee_method;
4469}
4470
4471void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4472 // All registers are assumed to be correctly set up.
4473 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004474
4475 switch (invoke->GetCodePtrLocation()) {
4476 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4477 __ Bl(&frame_entry_label_);
4478 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004479 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4480 // LR = callee_method->entry_point_from_quick_compiled_code_;
4481 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004482 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004483 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004484 {
4485 // To ensure that the pc position is recorded immediately after the `blr` instruction
4486 // BLR must be the last instruction emitted in this function.
4487 // Recording the pc will occur right after returning from this function.
4488 ExactAssemblyScope eas(GetVIXLAssembler(),
4489 kInstructionSize,
4490 CodeBufferCheckScope::kExactSize);
4491 // lr()
4492 __ blr(lr);
4493 }
Vladimir Marko58155012015-08-19 12:49:41 +00004494 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004495 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004496
Andreas Gampe878d58c2015-01-15 23:24:00 -08004497 DCHECK(!IsLeafMethod());
4498}
4499
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004500void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004501 // Use the calling convention instead of the location of the receiver, as
4502 // intrinsics may have put the receiver in a different register. In the intrinsics
4503 // slow path, the arguments have been moved to the right place, so here we are
4504 // guaranteed that the receiver is the first register of the calling convention.
4505 InvokeDexCallingConvention calling_convention;
4506 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004507 Register temp = XRegisterFrom(temp_in);
4508 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4509 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4510 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004511 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004512
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004513 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004514
4515 {
4516 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4517 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4518 // /* HeapReference<Class> */ temp = receiver->klass_
4519 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4520 MaybeRecordImplicitNullCheck(invoke);
4521 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004522 // Instead of simply (possibly) unpoisoning `temp` here, we should
4523 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004524 // intermediate/temporary reference and because the current
4525 // concurrent copying collector keeps the from-space memory
4526 // intact/accessible until the end of the marking phase (the
4527 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004528 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4529 // temp = temp->GetMethodAt(method_offset);
4530 __ Ldr(temp, MemOperand(temp, method_offset));
4531 // lr = temp->GetEntryPoint();
4532 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004533 {
4534 // To ensure that the pc position is recorded immediately after the `blr` instruction
4535 // BLR should be the last instruction emitted in this function.
4536 // Recording the pc will occur right after returning from this function.
4537 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4538 // lr();
4539 __ blr(lr);
4540 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004541}
4542
Orion Hodsonac141392017-01-13 11:53:47 +00004543void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4544 HandleInvoke(invoke);
4545}
4546
4547void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4548 codegen_->GenerateInvokePolymorphicCall(invoke);
4549}
4550
Scott Wakeling97c72b72016-06-24 16:19:36 +01004551vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4552 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004553 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004554 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004555 return
4556 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004557}
4558
Scott Wakeling97c72b72016-06-24 16:19:36 +01004559vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4560 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004561 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004562 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004563 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004564}
4565
Vladimir Marko1998cd02017-01-13 13:02:58 +00004566vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4567 const DexFile& dex_file,
4568 dex::TypeIndex type_index,
4569 vixl::aarch64::Label* adrp_label) {
4570 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4571}
4572
Scott Wakeling97c72b72016-06-24 16:19:36 +01004573vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4574 const DexFile& dex_file,
4575 uint32_t element_offset,
4576 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004577 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4578}
4579
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004580vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4581 baker_read_barrier_patches_.emplace_back(custom_data);
4582 return &baker_read_barrier_patches_.back().label;
4583}
4584
Scott Wakeling97c72b72016-06-24 16:19:36 +01004585vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4586 const DexFile& dex_file,
4587 uint32_t offset_or_index,
4588 vixl::aarch64::Label* adrp_label,
4589 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004590 // Add a patch entry and return the label.
4591 patches->emplace_back(dex_file, offset_or_index);
4592 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004593 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004594 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4595 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4596 return label;
4597}
4598
Scott Wakeling97c72b72016-06-24 16:19:36 +01004599vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004600 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004601 return boot_image_string_patches_.GetOrCreate(
4602 StringReference(&dex_file, string_index),
4603 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4604}
4605
Scott Wakeling97c72b72016-06-24 16:19:36 +01004606vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004607 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004608 return boot_image_type_patches_.GetOrCreate(
4609 TypeReference(&dex_file, type_index),
4610 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4611}
4612
Scott Wakeling97c72b72016-06-24 16:19:36 +01004613vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4614 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004615 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004616}
4617
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004618vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004619 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4620 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4621 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004622 return jit_string_patches_.GetOrCreate(
4623 StringReference(&dex_file, string_index),
4624 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4625}
4626
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004627vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004628 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4629 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4630 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004631 return jit_class_patches_.GetOrCreate(
4632 TypeReference(&dex_file, type_index),
4633 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4634}
4635
Vladimir Markoaad75c62016-10-03 08:46:48 +00004636void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4637 vixl::aarch64::Register reg) {
4638 DCHECK(reg.IsX());
4639 SingleEmissionCheckScope guard(GetVIXLAssembler());
4640 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004641 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004642}
4643
4644void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4645 vixl::aarch64::Register out,
4646 vixl::aarch64::Register base) {
4647 DCHECK(out.IsX());
4648 DCHECK(base.IsX());
4649 SingleEmissionCheckScope guard(GetVIXLAssembler());
4650 __ Bind(fixup_label);
4651 __ add(out, base, Operand(/* offset placeholder */ 0));
4652}
4653
4654void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4655 vixl::aarch64::Register out,
4656 vixl::aarch64::Register base) {
4657 DCHECK(base.IsX());
4658 SingleEmissionCheckScope guard(GetVIXLAssembler());
4659 __ Bind(fixup_label);
4660 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4661}
4662
4663template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4664inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4665 const ArenaDeque<PcRelativePatchInfo>& infos,
4666 ArenaVector<LinkerPatch>* linker_patches) {
4667 for (const PcRelativePatchInfo& info : infos) {
4668 linker_patches->push_back(Factory(info.label.GetLocation(),
4669 &info.target_dex_file,
4670 info.pc_insn_label->GetLocation(),
4671 info.offset_or_index));
4672 }
4673}
4674
Vladimir Marko58155012015-08-19 12:49:41 +00004675void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4676 DCHECK(linker_patches->empty());
4677 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004678 pc_relative_dex_cache_patches_.size() +
4679 boot_image_string_patches_.size() +
4680 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004681 boot_image_type_patches_.size() +
4682 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004683 type_bss_entry_patches_.size() +
4684 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004685 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004686 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004687 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004688 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004689 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004690 info.offset_or_index));
4691 }
4692 for (const auto& entry : boot_image_string_patches_) {
4693 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004694 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4695 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004696 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004697 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004698 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004699 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004700 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004701 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4702 linker_patches);
4703 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004704 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4705 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004706 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4707 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004708 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004709 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4710 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004711 for (const auto& entry : boot_image_type_patches_) {
4712 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004713 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4714 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004715 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004716 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004717 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004718 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4719 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4720 info.custom_data));
4721 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004722 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004723}
4724
Scott Wakeling97c72b72016-06-24 16:19:36 +01004725vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004726 Uint32ToLiteralMap* map) {
4727 return map->GetOrCreate(
4728 value,
4729 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4730}
4731
Scott Wakeling97c72b72016-06-24 16:19:36 +01004732vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004733 return uint64_literals_.GetOrCreate(
4734 value,
4735 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004736}
4737
Scott Wakeling97c72b72016-06-24 16:19:36 +01004738vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004739 MethodReference target_method,
4740 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004741 return map->GetOrCreate(
4742 target_method,
4743 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004744}
4745
Andreas Gampe878d58c2015-01-15 23:24:00 -08004746void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004747 // Explicit clinit checks triggered by static invokes must have been pruned by
4748 // art::PrepareForRegisterAllocation.
4749 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004750
Andreas Gampe878d58c2015-01-15 23:24:00 -08004751 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4752 return;
4753 }
4754
Artem Serov914d7a82017-02-07 14:33:49 +00004755 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4756 // are no pools emitted.
4757 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004758 LocationSummary* locations = invoke->GetLocations();
4759 codegen_->GenerateStaticOrDirectCall(
4760 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004761 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004762}
4763
4764void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004765 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4766 return;
4767 }
4768
Artem Serov914d7a82017-02-07 14:33:49 +00004769 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4770 // are no pools emitted.
4771 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004772 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004773 DCHECK(!codegen_->IsLeafMethod());
4774 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4775}
4776
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004777HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4778 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004779 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004780 case HLoadClass::LoadKind::kInvalid:
4781 LOG(FATAL) << "UNREACHABLE";
4782 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004783 case HLoadClass::LoadKind::kReferrersClass:
4784 break;
4785 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4786 DCHECK(!GetCompilerOptions().GetCompilePic());
4787 break;
4788 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4789 DCHECK(GetCompilerOptions().GetCompilePic());
4790 break;
4791 case HLoadClass::LoadKind::kBootImageAddress:
4792 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004793 case HLoadClass::LoadKind::kBssEntry:
4794 DCHECK(!Runtime::Current()->UseJitCompilation());
4795 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004796 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004797 DCHECK(Runtime::Current()->UseJitCompilation());
4798 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004799 case HLoadClass::LoadKind::kDexCacheViaMethod:
4800 break;
4801 }
4802 return desired_class_load_kind;
4803}
4804
Alexandre Rames67555f72014-11-18 10:55:16 +00004805void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004806 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4807 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004808 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004809 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004810 cls,
4811 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004812 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004813 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004814 return;
4815 }
Vladimir Marko41559982017-01-06 14:04:23 +00004816 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004817
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004818 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4819 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004820 ? LocationSummary::kCallOnSlowPath
4821 : LocationSummary::kNoCall;
4822 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004823 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004824 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004825 }
4826
Vladimir Marko41559982017-01-06 14:04:23 +00004827 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004828 locations->SetInAt(0, Location::RequiresRegister());
4829 }
4830 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004831 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4832 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4833 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004834 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004835 RegisterSet caller_saves = RegisterSet::Empty();
4836 InvokeRuntimeCallingConvention calling_convention;
4837 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4838 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4839 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4840 Primitive::kPrimNot).GetCode());
4841 locations->SetCustomSlowPathCallerSaves(caller_saves);
4842 } else {
4843 // For non-Baker read barrier we have a temp-clobbering call.
4844 }
4845 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004846}
4847
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004848// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4849// move.
4850void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004851 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4852 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4853 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004854 return;
4855 }
Vladimir Marko41559982017-01-06 14:04:23 +00004856 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004857
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004858 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004859 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004860 Register bss_entry_temp;
4861 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004862
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004863 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4864 ? kWithoutReadBarrier
4865 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004866 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004867 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004868 case HLoadClass::LoadKind::kReferrersClass: {
4869 DCHECK(!cls->CanCallRuntime());
4870 DCHECK(!cls->MustGenerateClinitCheck());
4871 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4872 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004873 GenerateGcRootFieldLoad(cls,
4874 out_loc,
4875 current_method,
4876 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004877 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004878 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004879 break;
4880 }
4881 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004882 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004883 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4884 cls->GetTypeIndex()));
4885 break;
4886 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004887 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004888 // Add ADRP with its PC-relative type patch.
4889 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004890 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004891 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004892 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004893 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004894 vixl::aarch64::Label* add_label =
4895 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004896 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004897 break;
4898 }
4899 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004900 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004901 uint32_t address = dchecked_integral_cast<uint32_t>(
4902 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4903 DCHECK_NE(address, 0u);
4904 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004905 break;
4906 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004907 case HLoadClass::LoadKind::kBssEntry: {
4908 // Add ADRP with its PC-relative Class .bss entry patch.
4909 const DexFile& dex_file = cls->GetDexFile();
4910 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004911 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004912 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4913 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004914 // Add LDR with its PC-relative Class patch.
4915 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004916 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004917 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4918 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004919 out_loc,
4920 bss_entry_temp,
4921 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004922 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004923 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004924 generate_null_check = true;
4925 break;
4926 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004927 case HLoadClass::LoadKind::kJitTableAddress: {
4928 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4929 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004930 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004931 GenerateGcRootFieldLoad(cls,
4932 out_loc,
4933 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004934 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004935 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004936 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004937 break;
4938 }
Vladimir Marko41559982017-01-06 14:04:23 +00004939 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004940 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004941 LOG(FATAL) << "UNREACHABLE";
4942 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004943 }
4944
Vladimir Markoea4c1262017-02-06 19:59:33 +00004945 bool do_clinit = cls->MustGenerateClinitCheck();
4946 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004947 DCHECK(cls->CanCallRuntime());
4948 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004949 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004950 codegen_->AddSlowPath(slow_path);
4951 if (generate_null_check) {
4952 __ Cbz(out, slow_path->GetEntryLabel());
4953 }
4954 if (cls->MustGenerateClinitCheck()) {
4955 GenerateClassInitializationCheck(slow_path, out);
4956 } else {
4957 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004958 }
4959 }
4960}
4961
David Brazdilcb1c0552015-08-04 16:22:25 +01004962static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004963 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004964}
4965
Alexandre Rames67555f72014-11-18 10:55:16 +00004966void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4967 LocationSummary* locations =
4968 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4969 locations->SetOut(Location::RequiresRegister());
4970}
4971
4972void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004973 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4974}
4975
4976void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4977 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4978}
4979
4980void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4981 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004982}
4983
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004984HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4985 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004986 switch (desired_string_load_kind) {
4987 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4988 DCHECK(!GetCompilerOptions().GetCompilePic());
4989 break;
4990 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4991 DCHECK(GetCompilerOptions().GetCompilePic());
4992 break;
4993 case HLoadString::LoadKind::kBootImageAddress:
4994 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004995 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01004996 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004997 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004998 case HLoadString::LoadKind::kJitTableAddress:
4999 DCHECK(Runtime::Current()->UseJitCompilation());
5000 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005001 case HLoadString::LoadKind::kDexCacheViaMethod:
5002 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005003 }
5004 return desired_string_load_kind;
5005}
5006
Alexandre Rames67555f72014-11-18 10:55:16 +00005007void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005008 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005009 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005010 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005011 InvokeRuntimeCallingConvention calling_convention;
5012 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5013 } else {
5014 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005015 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5016 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005017 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005018 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005019 RegisterSet caller_saves = RegisterSet::Empty();
5020 InvokeRuntimeCallingConvention calling_convention;
5021 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5022 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5023 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5024 Primitive::kPrimNot).GetCode());
5025 locations->SetCustomSlowPathCallerSaves(caller_saves);
5026 } else {
5027 // For non-Baker read barrier we have a temp-clobbering call.
5028 }
5029 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005030 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005031}
5032
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005033// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5034// move.
5035void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005036 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005037 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005038
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005039 switch (load->GetLoadKind()) {
5040 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005041 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5042 load->GetStringIndex()));
5043 return; // No dex cache slow path.
5044 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005045 // Add ADRP with its PC-relative String patch.
5046 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005047 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005048 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005049 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005050 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005051 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005052 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005053 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005054 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005055 return; // No dex cache slow path.
5056 }
5057 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005058 uint32_t address = dchecked_integral_cast<uint32_t>(
5059 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5060 DCHECK_NE(address, 0u);
5061 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005062 return; // No dex cache slow path.
5063 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005064 case HLoadString::LoadKind::kBssEntry: {
5065 // Add ADRP with its PC-relative String .bss entry patch.
5066 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005067 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005068 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005069 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005070 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005071 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005072 // Add LDR with its PC-relative String patch.
5073 vixl::aarch64::Label* ldr_label =
5074 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005075 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005076 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005077 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005078 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005079 /* offset placeholder */ 0u,
5080 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005081 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005082 SlowPathCodeARM64* slow_path =
5083 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005084 codegen_->AddSlowPath(slow_path);
5085 __ Cbz(out.X(), slow_path->GetEntryLabel());
5086 __ Bind(slow_path->GetExitLabel());
5087 return;
5088 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005089 case HLoadString::LoadKind::kJitTableAddress: {
5090 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005091 load->GetStringIndex(),
5092 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005093 GenerateGcRootFieldLoad(load,
5094 out_loc,
5095 out.X(),
5096 /* offset */ 0,
5097 /* fixup_label */ nullptr,
5098 kCompilerReadBarrierOption);
5099 return;
5100 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005101 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005102 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005103 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005104
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005105 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005106 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005107 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005108 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005109 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5110 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005111}
5112
Alexandre Rames5319def2014-10-23 10:03:10 +01005113void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5114 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5115 locations->SetOut(Location::ConstantLocation(constant));
5116}
5117
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005118void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005119 // Will be generated at use site.
5120}
5121
Alexandre Rames67555f72014-11-18 10:55:16 +00005122void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5123 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005124 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005125 InvokeRuntimeCallingConvention calling_convention;
5126 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5127}
5128
5129void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005130 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005131 instruction,
5132 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005133 if (instruction->IsEnter()) {
5134 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5135 } else {
5136 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5137 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005138}
5139
Alexandre Rames42d641b2014-10-27 14:00:51 +00005140void LocationsBuilderARM64::VisitMul(HMul* mul) {
5141 LocationSummary* locations =
5142 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5143 switch (mul->GetResultType()) {
5144 case Primitive::kPrimInt:
5145 case Primitive::kPrimLong:
5146 locations->SetInAt(0, Location::RequiresRegister());
5147 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005148 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005149 break;
5150
5151 case Primitive::kPrimFloat:
5152 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005153 locations->SetInAt(0, Location::RequiresFpuRegister());
5154 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005155 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005156 break;
5157
5158 default:
5159 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5160 }
5161}
5162
5163void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5164 switch (mul->GetResultType()) {
5165 case Primitive::kPrimInt:
5166 case Primitive::kPrimLong:
5167 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5168 break;
5169
5170 case Primitive::kPrimFloat:
5171 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005172 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005173 break;
5174
5175 default:
5176 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5177 }
5178}
5179
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005180void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5181 LocationSummary* locations =
5182 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5183 switch (neg->GetResultType()) {
5184 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005185 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005186 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005187 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005188 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005189
5190 case Primitive::kPrimFloat:
5191 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005192 locations->SetInAt(0, Location::RequiresFpuRegister());
5193 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005194 break;
5195
5196 default:
5197 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5198 }
5199}
5200
5201void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5202 switch (neg->GetResultType()) {
5203 case Primitive::kPrimInt:
5204 case Primitive::kPrimLong:
5205 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5206 break;
5207
5208 case Primitive::kPrimFloat:
5209 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005210 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005211 break;
5212
5213 default:
5214 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5215 }
5216}
5217
5218void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5219 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005220 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005221 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005222 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005223 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5224 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005225}
5226
5227void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005228 // Note: if heap poisoning is enabled, the entry point takes cares
5229 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005230 QuickEntrypointEnum entrypoint =
5231 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5232 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005233 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005234}
5235
Alexandre Rames5319def2014-10-23 10:03:10 +01005236void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5237 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005238 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005239 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005240 if (instruction->IsStringAlloc()) {
5241 locations->AddTemp(LocationFrom(kArtMethodRegister));
5242 } else {
5243 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005244 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005245 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5246}
5247
5248void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005249 // Note: if heap poisoning is enabled, the entry point takes cares
5250 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005251 if (instruction->IsStringAlloc()) {
5252 // String is allocated through StringFactory. Call NewEmptyString entry point.
5253 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005254 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005255 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5256 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005257
5258 {
5259 // Ensure the pc position is recorded immediately after the `blr` instruction.
5260 ExactAssemblyScope eas(GetVIXLAssembler(),
5261 kInstructionSize,
5262 CodeBufferCheckScope::kExactSize);
5263 __ blr(lr);
5264 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5265 }
David Brazdil6de19382016-01-08 17:37:10 +00005266 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005267 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005268 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005269 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005270}
5271
5272void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5273 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005274 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005276}
5277
5278void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005279 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005280 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005281 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005282 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005283 break;
5284
5285 default:
5286 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5287 }
5288}
5289
David Brazdil66d126e2015-04-03 16:02:44 +01005290void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5291 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5292 locations->SetInAt(0, Location::RequiresRegister());
5293 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5294}
5295
5296void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005297 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005298}
5299
Alexandre Rames5319def2014-10-23 10:03:10 +01005300void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005301 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5302 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005303}
5304
Calin Juravle2ae48182016-03-16 14:05:09 +00005305void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5306 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005307 return;
5308 }
Artem Serov914d7a82017-02-07 14:33:49 +00005309 {
5310 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5311 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5312 Location obj = instruction->GetLocations()->InAt(0);
5313 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5314 RecordPcInfo(instruction, instruction->GetDexPc());
5315 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005316}
5317
Calin Juravle2ae48182016-03-16 14:05:09 +00005318void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005319 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005320 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005321
5322 LocationSummary* locations = instruction->GetLocations();
5323 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005324
5325 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005326}
5327
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005328void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005329 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005330}
5331
Alexandre Rames67555f72014-11-18 10:55:16 +00005332void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5333 HandleBinaryOp(instruction);
5334}
5335
5336void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5337 HandleBinaryOp(instruction);
5338}
5339
Alexandre Rames3e69f162014-12-10 10:36:50 +00005340void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5341 LOG(FATAL) << "Unreachable";
5342}
5343
5344void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5345 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5346}
5347
Alexandre Rames5319def2014-10-23 10:03:10 +01005348void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5349 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5350 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5351 if (location.IsStackSlot()) {
5352 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5353 } else if (location.IsDoubleStackSlot()) {
5354 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5355 }
5356 locations->SetOut(location);
5357}
5358
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005359void InstructionCodeGeneratorARM64::VisitParameterValue(
5360 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005361 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005362}
5363
5364void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5365 LocationSummary* locations =
5366 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005367 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005368}
5369
5370void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5371 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5372 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005373}
5374
5375void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5376 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005377 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005378 locations->SetInAt(i, Location::Any());
5379 }
5380 locations->SetOut(Location::Any());
5381}
5382
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005383void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005384 LOG(FATAL) << "Unreachable";
5385}
5386
Serban Constantinescu02164b32014-11-13 14:05:07 +00005387void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005388 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005389 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005390 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5391 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005392 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5393
5394 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005395 case Primitive::kPrimInt:
5396 case Primitive::kPrimLong:
5397 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005398 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005399 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5400 break;
5401
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005402 case Primitive::kPrimFloat:
5403 case Primitive::kPrimDouble: {
5404 InvokeRuntimeCallingConvention calling_convention;
5405 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5406 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5407 locations->SetOut(calling_convention.GetReturnLocation(type));
5408
5409 break;
5410 }
5411
Serban Constantinescu02164b32014-11-13 14:05:07 +00005412 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005413 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005414 }
5415}
5416
5417void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5418 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005419
Serban Constantinescu02164b32014-11-13 14:05:07 +00005420 switch (type) {
5421 case Primitive::kPrimInt:
5422 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005423 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005424 break;
5425 }
5426
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005427 case Primitive::kPrimFloat:
5428 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005429 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5430 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005431 if (type == Primitive::kPrimFloat) {
5432 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5433 } else {
5434 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5435 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005436 break;
5437 }
5438
Serban Constantinescu02164b32014-11-13 14:05:07 +00005439 default:
5440 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005441 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005442 }
5443}
5444
Calin Juravle27df7582015-04-17 19:12:31 +01005445void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5446 memory_barrier->SetLocations(nullptr);
5447}
5448
5449void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005450 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005451}
5452
Alexandre Rames5319def2014-10-23 10:03:10 +01005453void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5454 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5455 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005456 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005457}
5458
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005459void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005460 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005461}
5462
5463void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5464 instruction->SetLocations(nullptr);
5465}
5466
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005467void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005468 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005469}
5470
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005471void LocationsBuilderARM64::VisitRor(HRor* ror) {
5472 HandleBinaryOp(ror);
5473}
5474
5475void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5476 HandleBinaryOp(ror);
5477}
5478
Serban Constantinescu02164b32014-11-13 14:05:07 +00005479void LocationsBuilderARM64::VisitShl(HShl* shl) {
5480 HandleShift(shl);
5481}
5482
5483void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5484 HandleShift(shl);
5485}
5486
5487void LocationsBuilderARM64::VisitShr(HShr* shr) {
5488 HandleShift(shr);
5489}
5490
5491void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5492 HandleShift(shr);
5493}
5494
Alexandre Rames5319def2014-10-23 10:03:10 +01005495void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005496 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005497}
5498
5499void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005500 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005501}
5502
Alexandre Rames67555f72014-11-18 10:55:16 +00005503void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005504 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005505}
5506
5507void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005508 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005509}
5510
5511void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005512 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005513}
5514
Alexandre Rames67555f72014-11-18 10:55:16 +00005515void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005516 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005517}
5518
Calin Juravlee460d1d2015-09-29 04:52:17 +01005519void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5520 HUnresolvedInstanceFieldGet* instruction) {
5521 FieldAccessCallingConventionARM64 calling_convention;
5522 codegen_->CreateUnresolvedFieldLocationSummary(
5523 instruction, instruction->GetFieldType(), calling_convention);
5524}
5525
5526void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5527 HUnresolvedInstanceFieldGet* instruction) {
5528 FieldAccessCallingConventionARM64 calling_convention;
5529 codegen_->GenerateUnresolvedFieldAccess(instruction,
5530 instruction->GetFieldType(),
5531 instruction->GetFieldIndex(),
5532 instruction->GetDexPc(),
5533 calling_convention);
5534}
5535
5536void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5537 HUnresolvedInstanceFieldSet* instruction) {
5538 FieldAccessCallingConventionARM64 calling_convention;
5539 codegen_->CreateUnresolvedFieldLocationSummary(
5540 instruction, instruction->GetFieldType(), calling_convention);
5541}
5542
5543void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5544 HUnresolvedInstanceFieldSet* instruction) {
5545 FieldAccessCallingConventionARM64 calling_convention;
5546 codegen_->GenerateUnresolvedFieldAccess(instruction,
5547 instruction->GetFieldType(),
5548 instruction->GetFieldIndex(),
5549 instruction->GetDexPc(),
5550 calling_convention);
5551}
5552
5553void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5554 HUnresolvedStaticFieldGet* instruction) {
5555 FieldAccessCallingConventionARM64 calling_convention;
5556 codegen_->CreateUnresolvedFieldLocationSummary(
5557 instruction, instruction->GetFieldType(), calling_convention);
5558}
5559
5560void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5561 HUnresolvedStaticFieldGet* instruction) {
5562 FieldAccessCallingConventionARM64 calling_convention;
5563 codegen_->GenerateUnresolvedFieldAccess(instruction,
5564 instruction->GetFieldType(),
5565 instruction->GetFieldIndex(),
5566 instruction->GetDexPc(),
5567 calling_convention);
5568}
5569
5570void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5571 HUnresolvedStaticFieldSet* instruction) {
5572 FieldAccessCallingConventionARM64 calling_convention;
5573 codegen_->CreateUnresolvedFieldLocationSummary(
5574 instruction, instruction->GetFieldType(), calling_convention);
5575}
5576
5577void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5578 HUnresolvedStaticFieldSet* instruction) {
5579 FieldAccessCallingConventionARM64 calling_convention;
5580 codegen_->GenerateUnresolvedFieldAccess(instruction,
5581 instruction->GetFieldType(),
5582 instruction->GetFieldIndex(),
5583 instruction->GetDexPc(),
5584 calling_convention);
5585}
5586
Alexandre Rames5319def2014-10-23 10:03:10 +01005587void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005588 LocationSummary* locations =
5589 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005590 // In suspend check slow path, usually there are no caller-save registers at all.
5591 // If SIMD instructions are present, however, we force spilling all live SIMD
5592 // registers in full width (since the runtime only saves/restores lower part).
5593 locations->SetCustomSlowPathCallerSaves(
5594 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005595}
5596
5597void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005598 HBasicBlock* block = instruction->GetBlock();
5599 if (block->GetLoopInformation() != nullptr) {
5600 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5601 // The back edge will generate the suspend check.
5602 return;
5603 }
5604 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5605 // The goto will generate the suspend check.
5606 return;
5607 }
5608 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005609}
5610
Alexandre Rames67555f72014-11-18 10:55:16 +00005611void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5612 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005613 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005614 InvokeRuntimeCallingConvention calling_convention;
5615 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5616}
5617
5618void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005619 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005620 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005621}
5622
5623void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5624 LocationSummary* locations =
5625 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5626 Primitive::Type input_type = conversion->GetInputType();
5627 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005628 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005629 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5630 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5631 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5632 }
5633
Alexandre Rames542361f2015-01-29 16:57:31 +00005634 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005635 locations->SetInAt(0, Location::RequiresFpuRegister());
5636 } else {
5637 locations->SetInAt(0, Location::RequiresRegister());
5638 }
5639
Alexandre Rames542361f2015-01-29 16:57:31 +00005640 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005641 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5642 } else {
5643 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5644 }
5645}
5646
5647void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5648 Primitive::Type result_type = conversion->GetResultType();
5649 Primitive::Type input_type = conversion->GetInputType();
5650
5651 DCHECK_NE(input_type, result_type);
5652
Alexandre Rames542361f2015-01-29 16:57:31 +00005653 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005654 int result_size = Primitive::ComponentSize(result_type);
5655 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005656 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005657 Register output = OutputRegister(conversion);
5658 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005659 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005660 // 'int' values are used directly as W registers, discarding the top
5661 // bits, so we don't need to sign-extend and can just perform a move.
5662 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5663 // top 32 bits of the target register. We theoretically could leave those
5664 // bits unchanged, but we would have to make sure that no code uses a
5665 // 32bit input value as a 64bit value assuming that the top 32 bits are
5666 // zero.
5667 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005668 } else if (result_type == Primitive::kPrimChar ||
5669 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5670 __ Ubfx(output,
5671 output.IsX() ? source.X() : source.W(),
5672 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005673 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005674 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005675 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005676 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005677 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005678 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005679 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5680 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005681 } else if (Primitive::IsFloatingPointType(result_type) &&
5682 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005683 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5684 } else {
5685 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5686 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005687 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005688}
Alexandre Rames67555f72014-11-18 10:55:16 +00005689
Serban Constantinescu02164b32014-11-13 14:05:07 +00005690void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5691 HandleShift(ushr);
5692}
5693
5694void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5695 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005696}
5697
5698void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5699 HandleBinaryOp(instruction);
5700}
5701
5702void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5703 HandleBinaryOp(instruction);
5704}
5705
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005706void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005707 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005708 LOG(FATAL) << "Unreachable";
5709}
5710
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005711void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005712 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005713 LOG(FATAL) << "Unreachable";
5714}
5715
Mark Mendellfe57faa2015-09-18 09:26:15 -04005716// Simple implementation of packed switch - generate cascaded compare/jumps.
5717void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5718 LocationSummary* locations =
5719 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5720 locations->SetInAt(0, Location::RequiresRegister());
5721}
5722
5723void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5724 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005725 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005726 Register value_reg = InputRegisterAt(switch_instr, 0);
5727 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5728
Zheng Xu3927c8b2015-11-18 17:46:25 +08005729 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005730 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005731 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5732 // make sure we don't emit it if the target may run out of range.
5733 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5734 // ranges and emit the tables only as required.
5735 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005736
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005737 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005738 // Current instruction id is an upper bound of the number of HIRs in the graph.
5739 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5740 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005741 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5742 Register temp = temps.AcquireW();
5743 __ Subs(temp, value_reg, Operand(lower_bound));
5744
Zheng Xu3927c8b2015-11-18 17:46:25 +08005745 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005746 // Jump to successors[0] if value == lower_bound.
5747 __ B(eq, codegen_->GetLabelOf(successors[0]));
5748 int32_t last_index = 0;
5749 for (; num_entries - last_index > 2; last_index += 2) {
5750 __ Subs(temp, temp, Operand(2));
5751 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5752 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5753 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5754 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5755 }
5756 if (num_entries - last_index == 2) {
5757 // The last missing case_value.
5758 __ Cmp(temp, Operand(1));
5759 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005760 }
5761
5762 // And the default for any other value.
5763 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5764 __ B(codegen_->GetLabelOf(default_block));
5765 }
5766 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005767 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005768
5769 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5770
5771 // Below instructions should use at most one blocked register. Since there are two blocked
5772 // registers, we are free to block one.
5773 Register temp_w = temps.AcquireW();
5774 Register index;
5775 // Remove the bias.
5776 if (lower_bound != 0) {
5777 index = temp_w;
5778 __ Sub(index, value_reg, Operand(lower_bound));
5779 } else {
5780 index = value_reg;
5781 }
5782
5783 // Jump to default block if index is out of the range.
5784 __ Cmp(index, Operand(num_entries));
5785 __ B(hs, codegen_->GetLabelOf(default_block));
5786
5787 // In current VIXL implementation, it won't require any blocked registers to encode the
5788 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5789 // register pressure.
5790 Register table_base = temps.AcquireX();
5791 // Load jump offset from the table.
5792 __ Adr(table_base, jump_table->GetTableStartLabel());
5793 Register jump_offset = temp_w;
5794 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5795
5796 // Jump to target block by branching to table_base(pc related) + offset.
5797 Register target_address = table_base;
5798 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5799 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005800 }
5801}
5802
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005803void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5804 HInstruction* instruction,
5805 Location out,
5806 uint32_t offset,
5807 Location maybe_temp,
5808 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005809 Primitive::Type type = Primitive::kPrimNot;
5810 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005811 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005812 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005813 if (kUseBakerReadBarrier) {
5814 // Load with fast path based Baker's read barrier.
5815 // /* HeapReference<Object> */ out = *(out + offset)
5816 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5817 out,
5818 out_reg,
5819 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005820 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005821 /* needs_null_check */ false,
5822 /* use_load_acquire */ false);
5823 } else {
5824 // Load with slow path based read barrier.
5825 // Save the value of `out` into `maybe_temp` before overwriting it
5826 // in the following move operation, as we will need it for the
5827 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005828 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005829 __ Mov(temp_reg, out_reg);
5830 // /* HeapReference<Object> */ out = *(out + offset)
5831 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5832 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5833 }
5834 } else {
5835 // Plain load with no read barrier.
5836 // /* HeapReference<Object> */ out = *(out + offset)
5837 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5838 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5839 }
5840}
5841
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005842void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5843 HInstruction* instruction,
5844 Location out,
5845 Location obj,
5846 uint32_t offset,
5847 Location maybe_temp,
5848 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005849 Primitive::Type type = Primitive::kPrimNot;
5850 Register out_reg = RegisterFrom(out, type);
5851 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005852 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005853 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005854 if (kUseBakerReadBarrier) {
5855 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005856 // /* HeapReference<Object> */ out = *(obj + offset)
5857 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5858 out,
5859 obj_reg,
5860 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005861 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005862 /* needs_null_check */ false,
5863 /* use_load_acquire */ false);
5864 } else {
5865 // Load with slow path based read barrier.
5866 // /* HeapReference<Object> */ out = *(obj + offset)
5867 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5868 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5869 }
5870 } else {
5871 // Plain load with no read barrier.
5872 // /* HeapReference<Object> */ out = *(obj + offset)
5873 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5874 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5875 }
5876}
5877
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005878void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5879 HInstruction* instruction,
5880 Location root,
5881 Register obj,
5882 uint32_t offset,
5883 vixl::aarch64::Label* fixup_label,
5884 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005885 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005886 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005887 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005888 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005889 if (kUseBakerReadBarrier) {
5890 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005891 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005892 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5893 !Runtime::Current()->UseJitCompilation()) {
5894 // Note that we do not actually check the value of `GetIsGcMarking()`
5895 // to decide whether to mark the loaded GC root or not. Instead, we
5896 // load into `temp` the read barrier mark introspection entrypoint.
5897 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5898 // vice versa.
5899 //
5900 // We use link-time generated thunks for the slow path. That thunk
5901 // checks the reference and jumps to the entrypoint if needed.
5902 //
5903 // temp = Thread::Current()->pReadBarrierMarkIntrospection
5904 // lr = &return_address;
5905 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5906 // if (temp != nullptr) {
5907 // goto gc_root_thunk<root_reg>(lr)
5908 // }
5909 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005910
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005911 UseScratchRegisterScope temps(GetVIXLAssembler());
5912 DCHECK(temps.IsAvailable(ip0));
5913 DCHECK(temps.IsAvailable(ip1));
5914 temps.Exclude(ip0, ip1);
5915 uint32_t custom_data =
5916 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5917 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005918
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005919 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
5920 DCHECK_EQ(ip0.GetCode(), 16u);
5921 const int32_t entry_point_offset =
5922 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
5923 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
5924 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5925 vixl::aarch64::Label return_address;
5926 __ adr(lr, &return_address);
5927 if (fixup_label != nullptr) {
5928 __ Bind(fixup_label);
5929 }
5930 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5931 "GC root LDR must be 2 instruction (8B) before the return address label.");
5932 __ ldr(root_reg, MemOperand(obj.X(), offset));
5933 __ Bind(cbnz_label);
5934 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5935 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005936 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005937 // Note that we do not actually check the value of
5938 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5939 // root or not. Instead, we load into `temp` the read barrier
5940 // mark entry point corresponding to register `root`. If `temp`
5941 // is null, it means that `GetIsGcMarking()` is false, and vice
5942 // versa.
5943 //
5944 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5945 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5946 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5947 // // Slow path.
5948 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
5949 // }
Roland Levillain44015862016-01-22 11:47:17 +00005950
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005951 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
5952 Register temp = lr;
5953 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
5954 instruction, root, /* entrypoint */ LocationFrom(temp));
5955 codegen_->AddSlowPath(slow_path);
5956
5957 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5958 const int32_t entry_point_offset =
5959 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5960 // Loading the entrypoint does not require a load acquire since it is only changed when
5961 // threads are suspended or running a checkpoint.
5962 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5963
5964 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5965 if (fixup_label == nullptr) {
5966 __ Ldr(root_reg, MemOperand(obj, offset));
5967 } else {
5968 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
5969 }
5970 static_assert(
5971 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5972 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5973 "have different sizes.");
5974 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5975 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5976 "have different sizes.");
5977
5978 // The entrypoint is null when the GC is not marking, this prevents one load compared to
5979 // checking GetIsGcMarking.
5980 __ Cbnz(temp, slow_path->GetEntryLabel());
5981 __ Bind(slow_path->GetExitLabel());
5982 }
Roland Levillain44015862016-01-22 11:47:17 +00005983 } else {
5984 // GC root loaded through a slow path for read barriers other
5985 // than Baker's.
5986 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005987 if (fixup_label == nullptr) {
5988 __ Add(root_reg.X(), obj.X(), offset);
5989 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005990 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005991 }
Roland Levillain44015862016-01-22 11:47:17 +00005992 // /* mirror::Object* */ root = root->Read()
5993 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5994 }
5995 } else {
5996 // Plain GC root load with no read barrier.
5997 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005998 if (fixup_label == nullptr) {
5999 __ Ldr(root_reg, MemOperand(obj, offset));
6000 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006001 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006002 }
Roland Levillain44015862016-01-22 11:47:17 +00006003 // Note that GC roots are not affected by heap poisoning, thus we
6004 // do not have to unpoison `root_reg` here.
6005 }
6006}
6007
6008void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6009 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006010 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006011 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006012 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006013 bool needs_null_check,
6014 bool use_load_acquire) {
6015 DCHECK(kEmitCompilerReadBarrier);
6016 DCHECK(kUseBakerReadBarrier);
6017
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006018 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6019 !use_load_acquire &&
6020 !Runtime::Current()->UseJitCompilation()) {
6021 // Note that we do not actually check the value of `GetIsGcMarking()`
6022 // to decide whether to mark the loaded GC root or not. Instead, we
6023 // load into `temp` the read barrier mark introspection entrypoint.
6024 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6025 // vice versa.
6026 //
6027 // We use link-time generated thunks for the slow path. That thunk checks
6028 // the holder and jumps to the entrypoint if needed. If the holder is not
6029 // gray, it creates a fake dependency and returns to the LDR instruction.
6030 //
6031 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6032 // lr = &return_address;
6033 // if (temp != nullptr) {
6034 // goto field_thunk<holder_reg, base_reg>(lr)
6035 // }
6036 // not_gray_return_address:
6037 // // Original reference load. If the offset is too large to fit
6038 // // into LDR, we use an adjusted base register here.
6039 // GcRoot<mirror::Object> root = *(obj+offset);
6040 // gray_return_address:
6041
6042 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6043 Register base = obj;
6044 if (offset >= kReferenceLoadMinFarOffset) {
6045 DCHECK(maybe_temp.IsRegister());
6046 base = WRegisterFrom(maybe_temp);
6047 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6048 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6049 offset &= (kReferenceLoadMinFarOffset - 1u);
6050 }
6051 UseScratchRegisterScope temps(GetVIXLAssembler());
6052 DCHECK(temps.IsAvailable(ip0));
6053 DCHECK(temps.IsAvailable(ip1));
6054 temps.Exclude(ip0, ip1);
6055 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6056 base.GetCode(),
6057 obj.GetCode());
6058 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6059
6060 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6061 DCHECK_EQ(ip0.GetCode(), 16u);
6062 const int32_t entry_point_offset =
6063 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6064 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
6065 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6066 vixl::aarch64::Label return_address;
6067 __ adr(lr, &return_address);
6068 __ Bind(cbnz_label);
6069 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6070 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == -4,
6071 "Field LDR must be 1 instruction (4B) before the return address label.");
6072 __ ldr(RegisterFrom(ref, Primitive::kPrimNot), MemOperand(base.X(), offset));
6073 if (needs_null_check) {
6074 MaybeRecordImplicitNullCheck(instruction);
6075 }
6076 __ Bind(&return_address);
6077 return;
6078 }
6079
Roland Levillain44015862016-01-22 11:47:17 +00006080 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006081 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006082 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006083 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006084 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6085 ref,
6086 obj,
6087 offset,
6088 no_index,
6089 no_scale_factor,
6090 temp,
6091 needs_null_check,
6092 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006093}
6094
6095void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6096 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006097 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006098 uint32_t data_offset,
6099 Location index,
6100 Register temp,
6101 bool needs_null_check) {
6102 DCHECK(kEmitCompilerReadBarrier);
6103 DCHECK(kUseBakerReadBarrier);
6104
6105 // Array cells are never volatile variables, therefore array loads
6106 // never use Load-Acquire instructions on ARM64.
6107 const bool use_load_acquire = false;
6108
Roland Levillainbfea3352016-06-23 13:48:47 +01006109 static_assert(
6110 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6111 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00006112 // /* HeapReference<Object> */ ref =
6113 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006114 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6115 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6116 ref,
6117 obj,
6118 data_offset,
6119 index,
6120 scale_factor,
6121 temp,
6122 needs_null_check,
6123 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006124}
6125
6126void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6127 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006128 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006129 uint32_t offset,
6130 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006131 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006132 Register temp,
6133 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006134 bool use_load_acquire,
6135 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00006136 DCHECK(kEmitCompilerReadBarrier);
6137 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006138 // If we are emitting an array load, we should not be using a
6139 // Load Acquire instruction. In other words:
6140 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6141 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006142
Roland Levillain54f869e2017-03-06 13:54:11 +00006143 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6144 // whether we need to enter the slow path to mark the reference.
6145 // Then, in the slow path, check the gray bit in the lock word of
6146 // the reference's holder (`obj`) to decide whether to mark `ref` or
6147 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006148 //
Roland Levillainba650a42017-03-06 13:52:32 +00006149 // Note that we do not actually check the value of `GetIsGcMarking()`;
6150 // instead, we load into `temp2` the read barrier mark entry point
6151 // corresponding to register `ref`. If `temp2` is null, it means
6152 // that `GetIsGcMarking()` is false, and vice versa.
6153 //
6154 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006155 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6156 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006157 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6158 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6159 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6160 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6161 // if (is_gray) {
6162 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6163 // }
6164 // } else {
6165 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006166 // }
Roland Levillain44015862016-01-22 11:47:17 +00006167
Roland Levillainba650a42017-03-06 13:52:32 +00006168 // Slow path marking the object `ref` when the GC is marking. The
6169 // entrypoint will already be loaded in `temp2`.
6170 Register temp2 = lr;
6171 Location temp2_loc = LocationFrom(temp2);
6172 SlowPathCodeARM64* slow_path;
6173 if (always_update_field) {
Roland Levillain54f869e2017-03-06 13:54:11 +00006174 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
6175 // only supports address of the form `obj + field_offset`, where
6176 // `obj` is a register and `field_offset` is a register. Thus
6177 // `offset` and `scale_factor` above are expected to be null in
6178 // this code path.
Roland Levillainba650a42017-03-06 13:52:32 +00006179 DCHECK_EQ(offset, 0u);
6180 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
Roland Levillain54f869e2017-03-06 13:54:11 +00006181 Location field_offset = index;
6182 slow_path =
6183 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6184 instruction,
6185 ref,
6186 obj,
6187 offset,
6188 /* index */ field_offset,
6189 scale_factor,
6190 needs_null_check,
6191 use_load_acquire,
6192 temp,
6193 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006194 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00006195 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6196 instruction,
6197 ref,
6198 obj,
6199 offset,
6200 index,
6201 scale_factor,
6202 needs_null_check,
6203 use_load_acquire,
6204 temp,
6205 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006206 }
6207 AddSlowPath(slow_path);
6208
6209 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6210 const int32_t entry_point_offset =
6211 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6212 // Loading the entrypoint does not require a load acquire since it is only changed when
6213 // threads are suspended or running a checkpoint.
6214 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006215 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6216 // checking GetIsGcMarking.
6217 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00006218 // Fast path: just load the reference.
6219 GenerateRawReferenceLoad(
6220 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006221 __ Bind(slow_path->GetExitLabel());
6222}
6223
6224void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6225 Location ref,
6226 Register obj,
6227 uint32_t offset,
6228 Location index,
6229 size_t scale_factor,
6230 bool needs_null_check,
6231 bool use_load_acquire) {
6232 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006233 Primitive::Type type = Primitive::kPrimNot;
6234 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006235
Roland Levillainba650a42017-03-06 13:52:32 +00006236 // If needed, vixl::EmissionCheckScope guards are used to ensure
6237 // that no pools are emitted between the load (macro) instruction
6238 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006239
Roland Levillain44015862016-01-22 11:47:17 +00006240 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006241 // Load types involving an "index": ArrayGet,
6242 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6243 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006244 if (use_load_acquire) {
6245 // UnsafeGetObjectVolatile intrinsic case.
6246 // Register `index` is not an index in an object array, but an
6247 // offset to an object reference field within object `obj`.
6248 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6249 DCHECK(instruction->GetLocations()->Intrinsified());
6250 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6251 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006252 DCHECK_EQ(offset, 0u);
6253 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006254 DCHECK_EQ(needs_null_check, false);
6255 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006256 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6257 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006258 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006259 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6260 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006261 if (index.IsConstant()) {
6262 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006263 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006264 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006265 if (needs_null_check) {
6266 MaybeRecordImplicitNullCheck(instruction);
6267 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006268 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006269 UseScratchRegisterScope temps(GetVIXLAssembler());
6270 Register temp = temps.AcquireW();
6271 __ Add(temp, obj, offset);
6272 {
6273 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6274 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6275 if (needs_null_check) {
6276 MaybeRecordImplicitNullCheck(instruction);
6277 }
6278 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006279 }
Roland Levillain44015862016-01-22 11:47:17 +00006280 }
Roland Levillain44015862016-01-22 11:47:17 +00006281 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006282 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006283 MemOperand field = HeapOperand(obj, offset);
6284 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006285 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6286 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006287 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006288 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006289 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006290 if (needs_null_check) {
6291 MaybeRecordImplicitNullCheck(instruction);
6292 }
Roland Levillain44015862016-01-22 11:47:17 +00006293 }
6294 }
6295
6296 // Object* ref = ref_addr->AsMirrorPtr()
6297 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006298}
6299
6300void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6301 Location out,
6302 Location ref,
6303 Location obj,
6304 uint32_t offset,
6305 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006306 DCHECK(kEmitCompilerReadBarrier);
6307
Roland Levillain44015862016-01-22 11:47:17 +00006308 // Insert a slow path based read barrier *after* the reference load.
6309 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006310 // If heap poisoning is enabled, the unpoisoning of the loaded
6311 // reference will be carried out by the runtime within the slow
6312 // path.
6313 //
6314 // Note that `ref` currently does not get unpoisoned (when heap
6315 // poisoning is enabled), which is alright as the `ref` argument is
6316 // not used by the artReadBarrierSlow entry point.
6317 //
6318 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6319 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6320 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6321 AddSlowPath(slow_path);
6322
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006323 __ B(slow_path->GetEntryLabel());
6324 __ Bind(slow_path->GetExitLabel());
6325}
6326
Roland Levillain44015862016-01-22 11:47:17 +00006327void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6328 Location out,
6329 Location ref,
6330 Location obj,
6331 uint32_t offset,
6332 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006333 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006334 // Baker's read barriers shall be handled by the fast path
6335 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6336 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006337 // If heap poisoning is enabled, unpoisoning will be taken care of
6338 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006339 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006340 } else if (kPoisonHeapReferences) {
6341 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6342 }
6343}
6344
Roland Levillain44015862016-01-22 11:47:17 +00006345void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6346 Location out,
6347 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006348 DCHECK(kEmitCompilerReadBarrier);
6349
Roland Levillain44015862016-01-22 11:47:17 +00006350 // Insert a slow path based read barrier *after* the GC root load.
6351 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006352 // Note that GC roots are not affected by heap poisoning, so we do
6353 // not need to do anything special for this here.
6354 SlowPathCodeARM64* slow_path =
6355 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6356 AddSlowPath(slow_path);
6357
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006358 __ B(slow_path->GetEntryLabel());
6359 __ Bind(slow_path->GetExitLabel());
6360}
6361
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006362void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6363 LocationSummary* locations =
6364 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6365 locations->SetInAt(0, Location::RequiresRegister());
6366 locations->SetOut(Location::RequiresRegister());
6367}
6368
6369void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6370 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006371 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006372 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006373 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006374 __ Ldr(XRegisterFrom(locations->Out()),
6375 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006376 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006377 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006378 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006379 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6380 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006381 __ Ldr(XRegisterFrom(locations->Out()),
6382 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006383 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006384}
6385
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006386static void PatchJitRootUse(uint8_t* code,
6387 const uint8_t* roots_data,
6388 vixl::aarch64::Literal<uint32_t>* literal,
6389 uint64_t index_in_table) {
6390 uint32_t literal_offset = literal->GetOffset();
6391 uintptr_t address =
6392 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6393 uint8_t* data = code + literal_offset;
6394 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6395}
6396
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006397void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6398 for (const auto& entry : jit_string_patches_) {
6399 const auto& it = jit_string_roots_.find(entry.first);
6400 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006401 PatchJitRootUse(code, roots_data, entry.second, it->second);
6402 }
6403 for (const auto& entry : jit_class_patches_) {
6404 const auto& it = jit_class_roots_.find(entry.first);
6405 DCHECK(it != jit_class_roots_.end());
6406 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006407 }
6408}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006409
Alexandre Rames67555f72014-11-18 10:55:16 +00006410#undef __
6411#undef QUICK_ENTRY_POINT
6412
Alexandre Rames5319def2014-10-23 10:03:10 +01006413} // namespace arm64
6414} // namespace art