1 //===-- AArch64Subtarget.cpp - AArch64 Subtarget Information ----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the AArch64 specific subclass of TargetSubtarget.
11 //===----------------------------------------------------------------------===//
13 #include "AArch64Subtarget.h"
16 #include "AArch64InstrInfo.h"
17 #include "AArch64PBQPRegAlloc.h"
18 #include "AArch64RegisterBankInfo.h"
19 #include "AArch64TargetMachine.h"
20 #include "GISel/AArch64CallLowering.h"
21 #include "GISel/AArch64LegalizerInfo.h"
22 #include "MCTargetDesc/AArch64AddressingModes.h"
23 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
24 #include "llvm/CodeGen/MachineScheduler.h"
25 #include "llvm/IR/GlobalValue.h"
26 #include "llvm/Support/TargetParser.h"
30 #define DEBUG_TYPE "aarch64-subtarget"
32 #define GET_SUBTARGETINFO_CTOR
33 #define GET_SUBTARGETINFO_TARGET_DESC
34 #include "AArch64GenSubtargetInfo.inc"
37 EnableEarlyIfConvert("aarch64-early-ifcvt", cl::desc("Enable the early if "
38 "converter pass"), cl::init(true), cl::Hidden);
40 // If OS supports TBI, use this flag to enable it.
42 UseAddressTopByteIgnored("aarch64-use-tbi", cl::desc("Assume that top byte of "
43 "an address is ignored"), cl::init(false), cl::Hidden);
46 UseNonLazyBind("aarch64-enable-nonlazybind",
47 cl::desc("Call nonlazybind functions via direct GOT load"),
48 cl::init(false), cl::Hidden);
51 AArch64Subtarget::initializeSubtargetDependencies(StringRef FS,
52 StringRef CPUString) {
53 // Determine default and user-specified characteristics
55 if (CPUString.empty())
56 CPUString = "generic";
58 ParseSubtargetFeatures(CPUString, FS);
59 initializeProperties();
64 void AArch64Subtarget::initializeProperties() {
65 // Initialize CPU specific properties. We should add a tablegen feature for
66 // this in the future so we can specify it together with the subtarget
68 switch (ARMProcFamily) {
77 PrefFunctionLogAlignment = 3;
82 MaxInterleaveFactor = 4;
83 PrefFunctionLogAlignment = 4;
86 PrefFunctionLogAlignment = 3;
92 PrefFunctionLogAlignment = 4;
96 PrefFunctionLogAlignment = 5;
97 PrefLoopLogAlignment = 5;
105 PrefetchDistance = 280;
106 MinPrefetchStride = 2048;
107 MaxPrefetchIterationsAhead = 3;
110 MaxInterleaveFactor = 4;
111 MaxJumpTableSize = 20;
112 PrefFunctionLogAlignment = 5;
113 PrefLoopLogAlignment = 4;
116 MaxInterleaveFactor = 4;
117 // FIXME: remove this to enable 64-bit SLP if performance looks good.
118 MinVectorRegisterBitWidth = 128;
120 PrefetchDistance = 820;
121 MinPrefetchStride = 2048;
122 MaxPrefetchIterationsAhead = 8;
125 MaxInterleaveFactor = 4;
126 VectorInsertExtractBaseCost = 2;
128 PrefetchDistance = 740;
129 MinPrefetchStride = 1024;
130 MaxPrefetchIterationsAhead = 11;
131 // FIXME: remove this to enable 64-bit SLP if performance looks good.
132 MinVectorRegisterBitWidth = 128;
135 PrefFunctionLogAlignment = 3;
138 PrefFunctionLogAlignment = 4;
141 MaxInterleaveFactor = 4;
142 // FIXME: remove this to enable 64-bit SLP if performance looks good.
143 MinVectorRegisterBitWidth = 128;
147 PrefFunctionLogAlignment = 3;
148 PrefLoopLogAlignment = 2;
149 MaxInterleaveFactor = 4;
150 PrefetchDistance = 128;
151 MinPrefetchStride = 1024;
152 MaxPrefetchIterationsAhead = 4;
153 // FIXME: remove this to enable 64-bit SLP if performance looks good.
154 MinVectorRegisterBitWidth = 128;
161 PrefFunctionLogAlignment = 3;
162 PrefLoopLogAlignment = 2;
163 // FIXME: remove this to enable 64-bit SLP if performance looks good.
164 MinVectorRegisterBitWidth = 128;
168 PrefFunctionLogAlignment = 4;
169 PrefLoopLogAlignment = 2;
173 PrefFunctionLogAlignment = 4;
174 PrefLoopLogAlignment = 2;
175 MaxInterleaveFactor = 4;
176 PrefetchDistance = 128;
177 MinPrefetchStride = 1024;
178 MaxPrefetchIterationsAhead = 4;
179 // FIXME: remove this to enable 64-bit SLP if performance looks good.
180 MinVectorRegisterBitWidth = 128;
185 AArch64Subtarget::AArch64Subtarget(const Triple &TT, const std::string &CPU,
186 const std::string &FS,
187 const TargetMachine &TM, bool LittleEndian)
188 : AArch64GenSubtargetInfo(TT, CPU, FS),
189 ReserveXRegister(AArch64::GPR64commonRegClass.getNumRegs()),
190 CustomCallSavedXRegs(AArch64::GPR64commonRegClass.getNumRegs()),
191 IsLittle(LittleEndian),
192 TargetTriple(TT), FrameLowering(),
193 InstrInfo(initializeSubtargetDependencies(FS, CPU)), TSInfo(),
195 if (AArch64::isX18ReservedByDefault(TT))
196 ReserveXRegister.set(18);
198 CallLoweringInfo.reset(new AArch64CallLowering(*getTargetLowering()));
199 InlineAsmLoweringInfo.reset(new InlineAsmLowering(getTargetLowering()));
200 Legalizer.reset(new AArch64LegalizerInfo(*this));
202 auto *RBI = new AArch64RegisterBankInfo(*getRegisterInfo());
204 // FIXME: At this point, we can't rely on Subtarget having RBI.
205 // It's awkward to mix passing RBI and the Subtarget; should we pass
207 InstSelector.reset(createAArch64InstructionSelector(
208 *static_cast<const AArch64TargetMachine *>(&TM), *this, *RBI));
210 RegBankInfo.reset(RBI);
213 const CallLowering *AArch64Subtarget::getCallLowering() const {
214 return CallLoweringInfo.get();
217 const InlineAsmLowering *AArch64Subtarget::getInlineAsmLowering() const {
218 return InlineAsmLoweringInfo.get();
221 InstructionSelector *AArch64Subtarget::getInstructionSelector() const {
222 return InstSelector.get();
225 const LegalizerInfo *AArch64Subtarget::getLegalizerInfo() const {
226 return Legalizer.get();
229 const RegisterBankInfo *AArch64Subtarget::getRegBankInfo() const {
230 return RegBankInfo.get();
233 /// Find the target operand flags that describe how a global value should be
234 /// referenced for the current subtarget.
236 AArch64Subtarget::ClassifyGlobalReference(const GlobalValue *GV,
237 const TargetMachine &TM) const {
238 // MachO large model always goes via a GOT, simply to get a single 8-byte
239 // absolute relocation on all global addresses.
240 if (TM.getCodeModel() == CodeModel::Large && isTargetMachO())
241 return AArch64II::MO_GOT;
243 if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV)) {
244 if (GV->hasDLLImportStorageClass())
245 return AArch64II::MO_GOT | AArch64II::MO_DLLIMPORT;
246 if (getTargetTriple().isOSWindows())
247 return AArch64II::MO_GOT | AArch64II::MO_COFFSTUB;
248 return AArch64II::MO_GOT;
251 // The small code model's direct accesses use ADRP, which cannot
252 // necessarily produce the value 0 (if the code is above 4GB).
253 // Same for the tiny code model, where we have a pc relative LDR.
254 if ((useSmallAddressing() || TM.getCodeModel() == CodeModel::Tiny) &&
255 GV->hasExternalWeakLinkage())
256 return AArch64II::MO_GOT;
258 // References to tagged globals are marked with MO_NC | MO_TAGGED to indicate
259 // that their nominal addresses are tagged and outside of the code model. In
260 // AArch64ExpandPseudo::expandMI we emit an additional instruction to set the
261 // tag if necessary based on MO_TAGGED.
262 if (AllowTaggedGlobals && !isa<FunctionType>(GV->getValueType()))
263 return AArch64II::MO_NC | AArch64II::MO_TAGGED;
265 return AArch64II::MO_NO_FLAG;
268 unsigned AArch64Subtarget::classifyGlobalFunctionReference(
269 const GlobalValue *GV, const TargetMachine &TM) const {
270 // MachO large model always goes via a GOT, because we don't have the
271 // relocations available to do anything else..
272 if (TM.getCodeModel() == CodeModel::Large && isTargetMachO() &&
273 !GV->hasInternalLinkage())
274 return AArch64II::MO_GOT;
276 // NonLazyBind goes via GOT unless we know it's available locally.
277 auto *F = dyn_cast<Function>(GV);
278 if (UseNonLazyBind && F && F->hasFnAttribute(Attribute::NonLazyBind) &&
279 !TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
280 return AArch64II::MO_GOT;
282 // Use ClassifyGlobalReference for setting MO_DLLIMPORT/MO_COFFSTUB.
283 if (getTargetTriple().isOSWindows())
284 return ClassifyGlobalReference(GV, TM);
286 return AArch64II::MO_NO_FLAG;
289 void AArch64Subtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
290 unsigned NumRegionInstrs) const {
291 // LNT run (at least on Cyclone) showed reasonably significant gains for
292 // bi-directional scheduling. 253.perlbmk.
293 Policy.OnlyTopDown = false;
294 Policy.OnlyBottomUp = false;
295 // Enabling or Disabling the latency heuristic is a close call: It seems to
296 // help nearly no benchmark on out-of-order architectures, on the other hand
297 // it regresses register pressure on a few benchmarking.
298 Policy.DisableLatencyHeuristic = DisableLatencySchedHeuristic;
301 bool AArch64Subtarget::enableEarlyIfConversion() const {
302 return EnableEarlyIfConvert;
305 bool AArch64Subtarget::supportsAddressTopByteIgnored() const {
306 if (!UseAddressTopByteIgnored)
309 if (TargetTriple.isiOS()) {
310 unsigned Major, Minor, Micro;
311 TargetTriple.getiOSVersion(Major, Minor, Micro);
318 std::unique_ptr<PBQPRAConstraint>
319 AArch64Subtarget::getCustomPBQPConstraints() const {
320 return balanceFPOps() ? std::make_unique<A57ChainingConstraint>() : nullptr;
323 void AArch64Subtarget::mirFileLoaded(MachineFunction &MF) const {
324 // We usually compute max call frame size after ISel. Do the computation now
325 // if the .mir file didn't specify it. Note that this will probably give you
326 // bogus values after PEI has eliminated the callframe setup/destroy pseudo
327 // instructions, specify explicitly if you need it to be correct.
328 MachineFrameInfo &MFI = MF.getFrameInfo();
329 if (!MFI.isMaxCallFrameSizeComputed())
330 MFI.computeMaxCallFrameSize(MF);