1 //===-- ARMSubtarget.cpp - ARM Subtarget Information ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ARM specific subclass of TargetSubtargetInfo.
12 //===----------------------------------------------------------------------===//
14 #include "ARMSubtarget.h"
15 #include "ARMFrameLowering.h"
16 #include "ARMISelLowering.h"
17 #include "ARMInstrInfo.h"
18 #include "ARMMachineFunctionInfo.h"
19 #include "ARMSelectionDAGInfo.h"
20 #include "ARMSubtarget.h"
21 #include "ARMTargetMachine.h"
22 #include "Thumb1FrameLowering.h"
23 #include "Thumb1InstrInfo.h"
24 #include "Thumb2InstrInfo.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/IR/Attributes.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/GlobalValue.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Target/TargetInstrInfo.h"
32 #include "llvm/Target/TargetOptions.h"
33 #include "llvm/Target/TargetRegisterInfo.h"
37 #define DEBUG_TYPE "arm-subtarget"
39 #define GET_SUBTARGETINFO_TARGET_DESC
40 #define GET_SUBTARGETINFO_CTOR
41 #include "ARMGenSubtargetInfo.inc"
44 UseFusedMulOps("arm-use-mulops",
45 cl::init(true), cl::Hidden);
53 static cl::opt<ITMode>
54 IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT),
56 cl::values(clEnumValN(DefaultIT, "arm-default-it",
57 "Generate IT block based on arch"),
58 clEnumValN(RestrictedIT, "arm-restrict-it",
59 "Disallow deprecated IT based on ARMv8"),
60 clEnumValN(NoRestrictedIT, "arm-no-restrict-it",
61 "Allow IT blocks based on ARMv7"),
64 /// ForceFastISel - Use the fast-isel, even for subtargets where it is not
65 /// currently supported (for testing only).
67 ForceFastISel("arm-force-fast-isel",
68 cl::init(false), cl::Hidden);
70 /// initializeSubtargetDependencies - Initializes using a CPU and feature string
71 /// so that we can use initializer lists for subtarget initialization.
72 ARMSubtarget &ARMSubtarget::initializeSubtargetDependencies(StringRef CPU,
74 initializeEnvironment();
75 initSubtargetFeatures(CPU, FS);
79 ARMFrameLowering *ARMSubtarget::initializeFrameLowering(StringRef CPU,
81 ARMSubtarget &STI = initializeSubtargetDependencies(CPU, FS);
82 if (STI.isThumb1Only())
83 return (ARMFrameLowering *)new Thumb1FrameLowering(STI);
85 return new ARMFrameLowering(STI);
88 ARMSubtarget::ARMSubtarget(const Triple &TT, const std::string &CPU,
89 const std::string &FS,
90 const ARMBaseTargetMachine &TM, bool IsLittle)
91 : ARMGenSubtargetInfo(TT, CPU, FS), ARMProcFamily(Others),
92 ARMProcClass(None), ARMArch(ARMv4t), stackAlignment(4), CPUString(CPU),
93 IsLittle(IsLittle), TargetTriple(TT), Options(TM.Options), TM(TM),
94 FrameLowering(initializeFrameLowering(CPU, FS)),
95 // At this point initializeSubtargetDependencies has been called so
96 // we can query directly.
97 InstrInfo(isThumb1Only()
98 ? (ARMBaseInstrInfo *)new Thumb1InstrInfo(*this)
100 ? (ARMBaseInstrInfo *)new ARMInstrInfo(*this)
101 : (ARMBaseInstrInfo *)new Thumb2InstrInfo(*this)),
104 void ARMSubtarget::initializeEnvironment() {
121 UseNEONForSinglePrecisionFP = false;
122 UseMulOps = UseFusedMulOps;
124 HasVMLxForwarding = false;
127 UseSoftFloat = false;
132 SupportsTailCall = false;
136 HasHardwareDivide = false;
137 HasHardwareDivideInARM = false;
138 HasT2ExtractPack = false;
139 HasDataBarrier = false;
140 Pref32BitThumb = false;
141 AvoidCPSRPartialUpdate = false;
142 AvoidMOVsShifterOperand = false;
144 HasMPExtension = false;
145 HasVirtualization = false;
148 HasTrustZone = false;
151 HasZeroCycleZeroing = false;
155 GenLongCalls = false;
156 UnsafeFPMath = false;
158 // MCAsmInfo isn't always present (e.g. in opt) so we can't initialize this
159 // directly from it, but we can try to make sure they're consistent when both
161 UseSjLjEH = isTargetDarwin() && !isTargetWatchOS();
162 assert((!TM.getMCAsmInfo() ||
163 (TM.getMCAsmInfo()->getExceptionHandlingType() ==
164 ExceptionHandling::SjLj) == UseSjLjEH) &&
165 "inconsistent sjlj choice between CodeGen and MC");
168 void ARMSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
169 if (CPUString.empty()) {
170 CPUString = "generic";
172 if (isTargetDarwin()) {
173 StringRef ArchName = TargetTriple.getArchName();
174 if (ArchName.endswith("v7s"))
175 // Default to the Swift CPU when targeting armv7s/thumbv7s.
177 else if (ArchName.endswith("v7k"))
178 // Default to the Cortex-a7 CPU when targeting armv7k/thumbv7k.
179 // ARMv7k does not use SjLj exception handling.
180 CPUString = "cortex-a7";
184 // Insert the architecture feature derived from the target triple into the
185 // feature string. This is important for setting features that are implied
186 // based on the architecture version.
187 std::string ArchFS = ARM_MC::ParseARMTriple(TargetTriple, CPUString);
190 ArchFS = (Twine(ArchFS) + "," + FS).str();
194 ParseSubtargetFeatures(CPUString, ArchFS);
196 // FIXME: This used enable V6T2 support implicitly for Thumb2 mode.
197 // Assert this for now to make the change obvious.
198 assert(hasV6T2Ops() || !hasThumb2());
200 // Keep a pointer to static instruction cost data for the specified CPU.
201 SchedModel = getSchedModelForCPU(CPUString);
203 // Initialize scheduling itinerary for the specified CPU.
204 InstrItins = getInstrItineraryForCPU(CPUString);
206 // FIXME: this is invalid for WindowsCE
207 if (isTargetWindows())
212 if (isTargetNaCl() || isAAPCS16_ABI())
215 // FIXME: Completely disable sibcall for Thumb1 since ThumbRegisterInfo::
216 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
217 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
218 // support in the assembler and linker to be used. This would need to be
219 // fixed to fully support tail calls in Thumb1.
221 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
222 // LR. This means if we need to reload LR, it takes an extra instructions,
223 // which outweighs the value of the tail call; but here we don't know yet
224 // whether LR is going to be used. Probably the right approach is to
225 // generate the tail call here and turn it back into CALL/RET in
226 // emitEpilogue if LR is used.
228 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
229 // but we need to make sure there are enough registers; the only valid
230 // registers are the 4 used for parameters. We don't currently do this
233 SupportsTailCall = !isThumb1Only();
235 if (isTargetMachO() && isTargetIOS() && getTargetTriple().isOSVersionLT(5, 0))
236 SupportsTailCall = false;
240 RestrictIT = hasV8Ops();
250 // NEON f32 ops are non-IEEE 754 compliant. Darwin is ok with it by default.
251 const FeatureBitset &Bits = getFeatureBits();
252 if ((Bits[ARM::ProcA5] || Bits[ARM::ProcA8]) && // Where this matters
253 (Options.UnsafeFPMath || isTargetDarwin()))
254 UseNEONForSinglePrecisionFP = true;
257 bool ARMSubtarget::isAPCS_ABI() const {
258 assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
259 return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_APCS;
261 bool ARMSubtarget::isAAPCS_ABI() const {
262 assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
263 return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS ||
264 TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16;
266 bool ARMSubtarget::isAAPCS16_ABI() const {
267 assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
268 return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16;
272 /// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol.
274 ARMSubtarget::GVIsIndirectSymbol(const GlobalValue *GV,
275 Reloc::Model RelocM) const {
276 if (RelocM == Reloc::Static)
279 bool isDef = GV->isStrongDefinitionForLinker();
281 if (!isTargetMachO()) {
282 // Extra load is needed for all externally visible.
283 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
287 // If this is a strong reference to a definition, it is definitely not
292 // Unless we have a symbol with hidden visibility, we have to go through a
293 // normal $non_lazy_ptr stub because this symbol might be resolved late.
294 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
297 if (RelocM == Reloc::PIC_) {
298 // If symbol visibility is hidden, we have a stub for common symbol
299 // references and external declarations.
300 if (GV->isDeclarationForLinker() || GV->hasCommonLinkage())
301 // Hidden $non_lazy_ptr reference.
309 unsigned ARMSubtarget::getMispredictionPenalty() const {
310 return SchedModel.MispredictPenalty;
313 bool ARMSubtarget::hasSinCos() const {
314 return isTargetWatchOS() ||
315 (isTargetIOS() && !getTargetTriple().isOSVersionLT(7, 0));
318 bool ARMSubtarget::enableMachineScheduler() const {
319 // Enable the MachineScheduler before register allocation for out-of-order
320 // architectures where we do not use the PostRA scheduler anymore (for now
321 // restricted to swift).
322 return getSchedModel().isOutOfOrder() && isSwift();
325 // This overrides the PostRAScheduler bit in the SchedModel for any CPU.
326 bool ARMSubtarget::enablePostRAScheduler() const {
327 // No need for PostRA scheduling on out of order CPUs (for now restricted to
329 if (getSchedModel().isOutOfOrder() && isSwift())
331 return (!isThumb() || hasThumb2());
334 bool ARMSubtarget::enableAtomicExpand() const {
335 return hasAnyDataBarrier() && !isThumb1Only();
338 bool ARMSubtarget::useStride4VFPs(const MachineFunction &MF) const {
339 // For general targets, the prologue can grow when VFPs are allocated with
340 // stride 4 (more vpush instructions). But WatchOS uses a compact unwind
341 // format which it's more important to get right.
342 return isTargetWatchOS() || (isSwift() && !MF.getFunction()->optForMinSize());
345 bool ARMSubtarget::useMovt(const MachineFunction &MF) const {
346 // NOTE Windows on ARM needs to use mov.w/mov.t pairs to materialise 32-bit
347 // immediates as it is inherently position independent, and may be out of
349 return !NoMovt && hasV6T2Ops() &&
350 (isTargetWindows() || !MF.getFunction()->optForMinSize());
353 bool ARMSubtarget::useFastISel() const {
354 // Enable fast-isel for any target, for testing only.
358 // Limit fast-isel to the targets that are or have been tested.
362 // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
363 return TM.Options.EnableFastISel &&
364 ((isTargetMachO() && !isThumb1Only()) ||
365 (isTargetLinux() && !isThumb()) || (isTargetNaCl() && !isThumb()));