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/Support/CommandLine.h"
30 #include "llvm/Target/TargetInstrInfo.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include "llvm/Target/TargetRegisterInfo.h"
36 #define DEBUG_TYPE "arm-subtarget"
38 #define GET_SUBTARGETINFO_TARGET_DESC
39 #define GET_SUBTARGETINFO_CTOR
40 #include "ARMGenSubtargetInfo.inc"
43 UseFusedMulOps("arm-use-mulops",
44 cl::init(true), cl::Hidden);
52 static cl::opt<ITMode>
53 IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT),
55 cl::values(clEnumValN(DefaultIT, "arm-default-it",
56 "Generate IT block based on arch"),
57 clEnumValN(RestrictedIT, "arm-restrict-it",
58 "Disallow deprecated IT based on ARMv8"),
59 clEnumValN(NoRestrictedIT, "arm-no-restrict-it",
60 "Allow IT blocks based on ARMv7"),
63 /// ForceFastISel - Use the fast-isel, even for subtargets where it is not
64 /// currently supported (for testing only).
66 ForceFastISel("arm-force-fast-isel",
67 cl::init(false), cl::Hidden);
69 /// initializeSubtargetDependencies - Initializes using a CPU and feature string
70 /// so that we can use initializer lists for subtarget initialization.
71 ARMSubtarget &ARMSubtarget::initializeSubtargetDependencies(StringRef CPU,
73 initializeEnvironment();
74 initSubtargetFeatures(CPU, FS);
78 ARMFrameLowering *ARMSubtarget::initializeFrameLowering(StringRef CPU,
80 ARMSubtarget &STI = initializeSubtargetDependencies(CPU, FS);
81 if (STI.isThumb1Only())
82 return (ARMFrameLowering *)new Thumb1FrameLowering(STI);
84 return new ARMFrameLowering(STI);
87 ARMSubtarget::ARMSubtarget(const Triple &TT, const std::string &CPU,
88 const std::string &FS,
89 const ARMBaseTargetMachine &TM, bool IsLittle)
90 : ARMGenSubtargetInfo(TT, CPU, FS), ARMProcFamily(Others),
91 ARMProcClass(None), stackAlignment(4), CPUString(CPU), IsLittle(IsLittle),
92 TargetTriple(TT), Options(TM.Options), TM(TM),
93 FrameLowering(initializeFrameLowering(CPU, FS)),
94 // At this point initializeSubtargetDependencies has been called so
95 // we can query directly.
96 InstrInfo(isThumb1Only()
97 ? (ARMBaseInstrInfo *)new Thumb1InstrInfo(*this)
99 ? (ARMBaseInstrInfo *)new ARMInstrInfo(*this)
100 : (ARMBaseInstrInfo *)new Thumb2InstrInfo(*this)),
103 void ARMSubtarget::initializeEnvironment() {
119 UseNEONForSinglePrecisionFP = false;
120 UseMulOps = UseFusedMulOps;
122 HasVMLxForwarding = false;
125 UseSoftFloat = false;
130 SupportsTailCall = false;
133 HasHardwareDivide = false;
134 HasHardwareDivideInARM = false;
135 HasT2ExtractPack = false;
136 HasDataBarrier = false;
137 Pref32BitThumb = false;
138 AvoidCPSRPartialUpdate = false;
139 AvoidMOVsShifterOperand = false;
141 HasMPExtension = false;
142 HasVirtualization = false;
145 HasTrustZone = false;
148 HasZeroCycleZeroing = false;
152 GenLongCalls = false;
153 UnsafeFPMath = false;
154 UseSjLjEH = (isTargetDarwin() &&
155 TargetTriple.getSubArch() != Triple::ARMSubArch_v7k);
158 void ARMSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
159 if (CPUString.empty()) {
160 CPUString = "generic";
162 if (isTargetDarwin()) {
163 StringRef ArchName = TargetTriple.getArchName();
164 if (ArchName.endswith("v7s"))
165 // Default to the Swift CPU when targeting armv7s/thumbv7s.
167 else if (ArchName.endswith("v7k"))
168 // Default to the Cortex-a7 CPU when targeting armv7k/thumbv7k.
169 // ARMv7k does not use SjLj exception handling.
170 CPUString = "cortex-a7";
174 // Insert the architecture feature derived from the target triple into the
175 // feature string. This is important for setting features that are implied
176 // based on the architecture version.
177 std::string ArchFS = ARM_MC::ParseARMTriple(TargetTriple, CPUString);
180 ArchFS = (Twine(ArchFS) + "," + FS).str();
184 ParseSubtargetFeatures(CPUString, ArchFS);
186 // FIXME: This used enable V6T2 support implicitly for Thumb2 mode.
187 // Assert this for now to make the change obvious.
188 assert(hasV6T2Ops() || !hasThumb2());
190 // Keep a pointer to static instruction cost data for the specified CPU.
191 SchedModel = getSchedModelForCPU(CPUString);
193 // Initialize scheduling itinerary for the specified CPU.
194 InstrItins = getInstrItineraryForCPU(CPUString);
196 // FIXME: this is invalid for WindowsCE
197 if (isTargetWindows())
202 if (isTargetNaCl() || isAAPCS16_ABI())
205 // FIXME: Completely disable sibcall for Thumb1 since ThumbRegisterInfo::
206 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
207 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
208 // support in the assembler and linker to be used. This would need to be
209 // fixed to fully support tail calls in Thumb1.
211 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
212 // LR. This means if we need to reload LR, it takes an extra instructions,
213 // which outweighs the value of the tail call; but here we don't know yet
214 // whether LR is going to be used. Probably the right approach is to
215 // generate the tail call here and turn it back into CALL/RET in
216 // emitEpilogue if LR is used.
218 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
219 // but we need to make sure there are enough registers; the only valid
220 // registers are the 4 used for parameters. We don't currently do this
223 SupportsTailCall = !isThumb1Only();
225 if (isTargetMachO() && isTargetIOS() && getTargetTriple().isOSVersionLT(5, 0))
226 SupportsTailCall = false;
230 RestrictIT = hasV8Ops();
240 // NEON f32 ops are non-IEEE 754 compliant. Darwin is ok with it by default.
241 const FeatureBitset &Bits = getFeatureBits();
242 if ((Bits[ARM::ProcA5] || Bits[ARM::ProcA8]) && // Where this matters
243 (Options.UnsafeFPMath || isTargetDarwin()))
244 UseNEONForSinglePrecisionFP = true;
247 bool ARMSubtarget::isAPCS_ABI() const {
248 assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
249 return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_APCS;
251 bool ARMSubtarget::isAAPCS_ABI() const {
252 assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
253 return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS ||
254 TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16;
256 bool ARMSubtarget::isAAPCS16_ABI() const {
257 assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
258 return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16;
262 /// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol.
264 ARMSubtarget::GVIsIndirectSymbol(const GlobalValue *GV,
265 Reloc::Model RelocM) const {
266 if (RelocM == Reloc::Static)
269 bool isDef = GV->isStrongDefinitionForLinker();
271 if (!isTargetMachO()) {
272 // Extra load is needed for all externally visible.
273 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
277 // If this is a strong reference to a definition, it is definitely not
282 // Unless we have a symbol with hidden visibility, we have to go through a
283 // normal $non_lazy_ptr stub because this symbol might be resolved late.
284 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
287 if (RelocM == Reloc::PIC_) {
288 // If symbol visibility is hidden, we have a stub for common symbol
289 // references and external declarations.
290 if (GV->isDeclarationForLinker() || GV->hasCommonLinkage())
291 // Hidden $non_lazy_ptr reference.
299 unsigned ARMSubtarget::getMispredictionPenalty() const {
300 return SchedModel.MispredictPenalty;
303 bool ARMSubtarget::hasSinCos() const {
304 return isTargetWatchOS() ||
305 (isTargetIOS() && !getTargetTriple().isOSVersionLT(7, 0));
308 bool ARMSubtarget::enableMachineScheduler() const {
309 // Enable the MachineScheduler before register allocation for out-of-order
310 // architectures where we do not use the PostRA scheduler anymore (for now
311 // restricted to swift).
312 return getSchedModel().isOutOfOrder() && isSwift();
315 // This overrides the PostRAScheduler bit in the SchedModel for any CPU.
316 bool ARMSubtarget::enablePostRAScheduler() const {
317 // No need for PostRA scheduling on out of order CPUs (for now restricted to
319 if (getSchedModel().isOutOfOrder() && isSwift())
321 return (!isThumb() || hasThumb2());
324 bool ARMSubtarget::enableAtomicExpand() const {
325 return hasAnyDataBarrier() && !isThumb1Only();
328 bool ARMSubtarget::useStride4VFPs(const MachineFunction &MF) const {
329 // For general targets, the prologue can grow when VFPs are allocated with
330 // stride 4 (more vpush instructions). But WatchOS uses a compact unwind
331 // format which it's more important to get right.
332 return isTargetWatchOS() || (isSwift() && !MF.getFunction()->optForMinSize());
335 bool ARMSubtarget::useMovt(const MachineFunction &MF) const {
336 // NOTE Windows on ARM needs to use mov.w/mov.t pairs to materialise 32-bit
337 // immediates as it is inherently position independent, and may be out of
339 return !NoMovt && hasV6T2Ops() &&
340 (isTargetWindows() || !MF.getFunction()->optForMinSize());
343 bool ARMSubtarget::useFastISel() const {
344 // Enable fast-isel for any target, for testing only.
348 // Limit fast-isel to the targets that are or have been tested.
352 // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
353 return TM.Options.EnableFastISel &&
354 ((isTargetMachO() && !isThumb1Only()) ||
355 (isTargetLinux() && !isThumb()) || (isTargetNaCl() && !isThumb()));