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 "ARMJITInfo.h"
19 #include "ARMSelectionDAGInfo.h"
20 #include "ARMSubtarget.h"
21 #include "Thumb1FrameLowering.h"
22 #include "Thumb1InstrInfo.h"
23 #include "Thumb2InstrInfo.h"
24 #include "llvm/IR/Attributes.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/GlobalValue.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Target/TargetInstrInfo.h"
29 #include "llvm/Target/TargetOptions.h"
33 #define DEBUG_TYPE "arm-subtarget"
35 #define GET_SUBTARGETINFO_TARGET_DESC
36 #define GET_SUBTARGETINFO_CTOR
37 #include "ARMGenSubtargetInfo.inc"
40 ReserveR9("arm-reserve-r9", cl::Hidden,
41 cl::desc("Reserve R9, making it unavailable as GPR"));
44 ArmUseMOVT("arm-use-movt", cl::init(true), cl::Hidden);
47 UseFusedMulOps("arm-use-mulops",
48 cl::init(true), cl::Hidden);
56 static cl::opt<AlignMode>
57 Align(cl::desc("Load/store alignment support"),
58 cl::Hidden, cl::init(DefaultAlign),
60 clEnumValN(DefaultAlign, "arm-default-align",
61 "Generate unaligned accesses only on hardware/OS "
62 "combinations that are known to support them"),
63 clEnumValN(StrictAlign, "arm-strict-align",
64 "Disallow all unaligned memory accesses"),
65 clEnumValN(NoStrictAlign, "arm-no-strict-align",
66 "Allow unaligned memory accesses"),
75 static cl::opt<ITMode>
76 IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT),
78 cl::values(clEnumValN(DefaultIT, "arm-default-it",
79 "Generate IT block based on arch"),
80 clEnumValN(RestrictedIT, "arm-restrict-it",
81 "Disallow deprecated IT based on ARMv8"),
82 clEnumValN(NoRestrictedIT, "arm-no-restrict-it",
83 "Allow IT blocks based on ARMv7"),
86 static std::string computeDataLayout(ARMSubtarget &ST) {
96 Ret += DataLayout::getManglingComponent(ST.getTargetTriple());
98 // Pointers are 32 bits and aligned to 32 bits.
101 // On thumb, i16,i18 and i1 have natural aligment requirements, but we try to
104 Ret += "-i1:8:32-i8:8:32-i16:16:32";
106 // ABIs other than APCS have 64 bit integers with natural alignment.
107 if (!ST.isAPCS_ABI())
110 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
111 // bits, others to 64 bits. We always try to align to 64 bits.
115 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
116 // to 64. We always ty to give them natural alignment.
118 Ret += "-v64:32:64-v128:32:128";
120 Ret += "-v128:64:128";
122 // On thumb and APCS, only try to align aggregates to 32 bits (the default is
124 if (ST.isThumb() || ST.isAPCS_ABI())
127 // Integer registers are 32 bits.
130 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
131 // aligned everywhere else.
132 if (ST.isTargetNaCl())
134 else if (ST.isAAPCS_ABI())
142 /// initializeSubtargetDependencies - Initializes using a CPU and feature string
143 /// so that we can use initializer lists for subtarget initialization.
144 ARMSubtarget &ARMSubtarget::initializeSubtargetDependencies(StringRef CPU,
146 initializeEnvironment();
147 resetSubtargetFeatures(CPU, FS);
151 ARMSubtarget::ARMSubtarget(const std::string &TT, const std::string &CPU,
152 const std::string &FS, TargetMachine &TM,
153 bool IsLittle, const TargetOptions &Options)
154 : ARMGenSubtargetInfo(TT, CPU, FS), ARMProcFamily(Others),
155 ARMProcClass(None), stackAlignment(4), CPUString(CPU), IsLittle(IsLittle),
156 TargetTriple(TT), Options(Options), TargetABI(ARM_ABI_UNKNOWN),
157 DL(computeDataLayout(initializeSubtargetDependencies(CPU, FS))),
158 TSInfo(DL), JITInfo(),
159 InstrInfo(isThumb1Only()
160 ? (ARMBaseInstrInfo *)new Thumb1InstrInfo(*this)
162 ? (ARMBaseInstrInfo *)new ARMInstrInfo(*this)
163 : (ARMBaseInstrInfo *)new Thumb2InstrInfo(*this)),
165 FrameLowering(!isThumb1Only()
166 ? new ARMFrameLowering(*this)
167 : (ARMFrameLowering *)new Thumb1FrameLowering(*this)) {}
169 void ARMSubtarget::initializeEnvironment() {
183 UseNEONForSinglePrecisionFP = false;
184 UseMulOps = UseFusedMulOps;
186 HasVMLxForwarding = false;
191 PostRAScheduler = false;
192 IsR9Reserved = ReserveR9;
194 SupportsTailCall = false;
197 HasHardwareDivide = false;
198 HasHardwareDivideInARM = false;
199 HasT2ExtractPack = false;
200 HasDataBarrier = false;
201 Pref32BitThumb = false;
202 AvoidCPSRPartialUpdate = false;
203 AvoidMOVsShifterOperand = false;
205 HasMPExtension = false;
206 HasVirtualization = false;
209 HasTrustZone = false;
212 HasZeroCycleZeroing = false;
213 AllowsUnalignedMem = false;
216 UnsafeFPMath = false;
219 void ARMSubtarget::resetSubtargetFeatures(const MachineFunction *MF) {
220 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
221 Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
223 Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
226 !CPUAttr.hasAttribute(Attribute::None) ?CPUAttr.getValueAsString() : "";
228 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
230 initializeEnvironment();
231 resetSubtargetFeatures(CPU, FS);
235 void ARMSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
236 if (CPUString.empty()) {
237 if (isTargetIOS() && TargetTriple.getArchName().endswith("v7s"))
238 // Default to the Swift CPU when targeting armv7s/thumbv7s.
241 CPUString = "generic";
244 // Insert the architecture feature derived from the target triple into the
245 // feature string. This is important for setting features that are implied
246 // based on the architecture version.
247 std::string ArchFS = ARM_MC::ParseARMTriple(TargetTriple.getTriple(),
251 ArchFS = ArchFS + "," + FS.str();
255 ParseSubtargetFeatures(CPUString, ArchFS);
257 // FIXME: This used enable V6T2 support implicitly for Thumb2 mode.
258 // Assert this for now to make the change obvious.
259 assert(hasV6T2Ops() || !hasThumb2());
261 // Keep a pointer to static instruction cost data for the specified CPU.
262 SchedModel = getSchedModelForCPU(CPUString);
264 // Initialize scheduling itinerary for the specified CPU.
265 InstrItins = getInstrItineraryForCPU(CPUString);
267 if (TargetABI == ARM_ABI_UNKNOWN) {
268 switch (TargetTriple.getEnvironment()) {
269 case Triple::Android:
272 case Triple::GNUEABI:
273 case Triple::GNUEABIHF:
274 TargetABI = ARM_ABI_AAPCS;
277 if ((isTargetIOS() && isMClass()) ||
278 (TargetTriple.isOSBinFormatMachO() &&
279 TargetTriple.getOS() == Triple::UnknownOS))
280 TargetABI = ARM_ABI_AAPCS;
282 TargetABI = ARM_ABI_APCS;
287 // FIXME: this is invalid for WindowsCE
288 if (isTargetWindows()) {
289 TargetABI = ARM_ABI_AAPCS;
298 UseMovt = hasV6T2Ops() && ArmUseMOVT;
300 if (isTargetMachO()) {
301 IsR9Reserved = ReserveR9 | !HasV6Ops;
302 SupportsTailCall = !isTargetIOS() || !getTargetTriple().isOSVersionLT(5, 0);
304 IsR9Reserved = ReserveR9;
305 SupportsTailCall = !isThumb1Only();
308 if (!isThumb() || hasThumb2())
309 PostRAScheduler = true;
313 // Assume pre-ARMv6 doesn't support unaligned accesses.
315 // ARMv6 may or may not support unaligned accesses depending on the
316 // SCTLR.U bit, which is architecture-specific. We assume ARMv6
317 // Darwin and NetBSD targets support unaligned accesses, and others don't.
319 // ARMv7 always has SCTLR.U set to 1, but it has a new SCTLR.A bit
320 // which raises an alignment fault on unaligned accesses. Linux
321 // defaults this bit to 0 and handles it as a system-wide (not
322 // per-process) setting. It is therefore safe to assume that ARMv7+
323 // Linux targets support unaligned accesses. The same goes for NaCl.
325 // The above behavior is consistent with GCC.
327 (hasV7Ops() && (isTargetLinux() || isTargetNaCl() ||
328 isTargetNetBSD())) ||
329 (hasV6Ops() && (isTargetMachO() || isTargetNetBSD()));
330 // The one exception is cortex-m0, which despite being v6, does not
331 // support unaligned accesses. Rather than make the above boolean
332 // expression even more obtuse, just override the value here.
333 if (isThumb1Only() && isMClass())
334 AllowsUnalignedMem = false;
337 AllowsUnalignedMem = false;
340 AllowsUnalignedMem = true;
346 RestrictIT = hasV8Ops() ? true : false;
356 // NEON f32 ops are non-IEEE 754 compliant. Darwin is ok with it by default.
357 uint64_t Bits = getFeatureBits();
358 if ((Bits & ARM::ProcA5 || Bits & ARM::ProcA8) && // Where this matters
359 (Options.UnsafeFPMath || isTargetDarwin()))
360 UseNEONForSinglePrecisionFP = true;
363 /// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol.
365 ARMSubtarget::GVIsIndirectSymbol(const GlobalValue *GV,
366 Reloc::Model RelocM) const {
367 if (RelocM == Reloc::Static)
370 // Materializable GVs (in JIT lazy compilation mode) do not require an extra
372 bool isDecl = GV->hasAvailableExternallyLinkage();
373 if (GV->isDeclaration() && !GV->isMaterializable())
376 if (!isTargetMachO()) {
377 // Extra load is needed for all externally visible.
378 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
382 if (RelocM == Reloc::PIC_) {
383 // If this is a strong reference to a definition, it is definitely not
385 if (!isDecl && !GV->isWeakForLinker())
388 // Unless we have a symbol with hidden visibility, we have to go through a
389 // normal $non_lazy_ptr stub because this symbol might be resolved late.
390 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
393 // If symbol visibility is hidden, we have a stub for common symbol
394 // references and external declarations.
395 if (isDecl || GV->hasCommonLinkage())
396 // Hidden $non_lazy_ptr reference.
401 // If this is a strong reference to a definition, it is definitely not
403 if (!isDecl && !GV->isWeakForLinker())
406 // Unless we have a symbol with hidden visibility, we have to go through a
407 // normal $non_lazy_ptr stub because this symbol might be resolved late.
408 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
416 unsigned ARMSubtarget::getMispredictionPenalty() const {
417 return SchedModel->MispredictPenalty;
420 bool ARMSubtarget::hasSinCos() const {
421 return getTargetTriple().getOS() == Triple::IOS &&
422 !getTargetTriple().isOSVersionLT(7, 0);
425 // Enable the PostMachineScheduler if the target selects it instead of
426 // PostRAScheduler. Currently only available on the command line via
428 bool ARMSubtarget::enablePostMachineScheduler() const {
429 return PostRAScheduler;
432 bool ARMSubtarget::enableAtomicExpandLoadLinked() const {
433 return hasAnyDataBarrier() && !isThumb1Only();
436 bool ARMSubtarget::enablePostRAScheduler(
437 CodeGenOpt::Level OptLevel,
438 TargetSubtargetInfo::AntiDepBreakMode& Mode,
439 RegClassVector& CriticalPathRCs) const {
440 Mode = TargetSubtargetInfo::ANTIDEP_NONE;
441 return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
444 bool ARMSubtarget::useMovt(const MachineFunction &MF) const {
445 // NOTE Windows on ARM needs to use mov.w/mov.t pairs to materialise 32-bit
446 // immediates as it is inherently position independent, and may be out of
448 return UseMovt && (isTargetWindows() ||
449 !MF.getFunction()->getAttributes().hasAttribute(
450 AttributeSet::FunctionIndex, Attribute::MinSize));