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 "ARMBaseInstrInfo.h"
16 #include "ARMBaseRegisterInfo.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Target/TargetInstrInfo.h"
22 #include "llvm/Target/TargetOptions.h"
26 #define DEBUG_TYPE "arm-subtarget"
28 #define GET_SUBTARGETINFO_TARGET_DESC
29 #define GET_SUBTARGETINFO_CTOR
30 #include "ARMGenSubtargetInfo.inc"
33 ReserveR9("arm-reserve-r9", cl::Hidden,
34 cl::desc("Reserve R9, making it unavailable as GPR"));
37 ArmUseMOVT("arm-use-movt", cl::init(true), cl::Hidden);
40 UseFusedMulOps("arm-use-mulops",
41 cl::init(true), cl::Hidden);
49 static cl::opt<AlignMode>
50 Align(cl::desc("Load/store alignment support"),
51 cl::Hidden, cl::init(DefaultAlign),
53 clEnumValN(DefaultAlign, "arm-default-align",
54 "Generate unaligned accesses only on hardware/OS "
55 "combinations that are known to support them"),
56 clEnumValN(StrictAlign, "arm-strict-align",
57 "Disallow all unaligned memory accesses"),
58 clEnumValN(NoStrictAlign, "arm-no-strict-align",
59 "Allow unaligned memory accesses"),
68 static cl::opt<ITMode>
69 IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT),
71 cl::values(clEnumValN(DefaultIT, "arm-default-it",
72 "Generate IT block based on arch"),
73 clEnumValN(RestrictedIT, "arm-restrict-it",
74 "Disallow deprecated IT based on ARMv8"),
75 clEnumValN(NoRestrictedIT, "arm-no-restrict-it",
76 "Allow IT blocks based on ARMv7"),
79 static std::string computeDataLayout(ARMSubtarget &ST) {
89 Ret += DataLayout::getManglingComponent(ST.getTargetTriple());
91 // Pointers are 32 bits and aligned to 32 bits.
94 // On thumb, i16,i18 and i1 have natural aligment requirements, but we try to
97 Ret += "-i1:8:32-i8:8:32-i16:16:32";
99 // ABIs other than APCS have 64 bit integers with natural alignment.
100 if (!ST.isAPCS_ABI())
103 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
104 // bits, others to 64 bits. We always try to align to 64 bits.
108 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
109 // to 64. We always ty to give them natural alignment.
111 Ret += "-v64:32:64-v128:32:128";
113 Ret += "-v128:64:128";
115 // On thumb and APCS, only try to align aggregates to 32 bits (the default is
117 if (ST.isThumb() || ST.isAPCS_ABI())
120 // Integer registers are 32 bits.
123 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
124 // aligned everywhere else.
125 if (ST.isTargetNaCl())
127 else if (ST.isAAPCS_ABI())
135 /// initializeSubtargetDependencies - Initializes using a CPU and feature string
136 /// so that we can use initializer lists for subtarget initialization.
137 ARMSubtarget &ARMSubtarget::initializeSubtargetDependencies(StringRef CPU,
139 initializeEnvironment();
140 resetSubtargetFeatures(CPU, FS);
144 ARMSubtarget::ARMSubtarget(const std::string &TT, const std::string &CPU,
145 const std::string &FS, bool IsLittle,
146 const TargetOptions &Options)
147 : ARMGenSubtargetInfo(TT, CPU, FS), ARMProcFamily(Others),
148 ARMProcClass(None), stackAlignment(4), CPUString(CPU), IsLittle(IsLittle),
149 TargetTriple(TT), Options(Options), TargetABI(ARM_ABI_UNKNOWN),
150 DL(computeDataLayout(initializeSubtargetDependencies(CPU, FS))),
151 TSInfo(DL), JITInfo() {}
153 void ARMSubtarget::initializeEnvironment() {
168 UseNEONForSinglePrecisionFP = false;
169 UseMulOps = UseFusedMulOps;
171 HasVMLxForwarding = false;
176 PostRAScheduler = false;
177 IsR9Reserved = ReserveR9;
179 SupportsTailCall = false;
182 HasHardwareDivide = false;
183 HasHardwareDivideInARM = false;
184 HasT2ExtractPack = false;
185 HasDataBarrier = false;
186 Pref32BitThumb = false;
187 AvoidCPSRPartialUpdate = false;
188 AvoidMOVsShifterOperand = false;
190 HasMPExtension = false;
191 HasVirtualization = false;
194 HasTrustZone = false;
197 HasZeroCycleZeroing = false;
198 AllowsUnalignedMem = false;
201 UnsafeFPMath = false;
204 void ARMSubtarget::resetSubtargetFeatures(const MachineFunction *MF) {
205 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
206 Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
208 Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
211 !CPUAttr.hasAttribute(Attribute::None) ?CPUAttr.getValueAsString() : "";
213 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
215 initializeEnvironment();
216 resetSubtargetFeatures(CPU, FS);
220 FnAttrs.hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
223 void ARMSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
224 if (CPUString.empty()) {
225 if (isTargetIOS() && TargetTriple.getArchName().endswith("v7s"))
226 // Default to the Swift CPU when targeting armv7s/thumbv7s.
229 CPUString = "generic";
232 // Insert the architecture feature derived from the target triple into the
233 // feature string. This is important for setting features that are implied
234 // based on the architecture version.
235 std::string ArchFS = ARM_MC::ParseARMTriple(TargetTriple.getTriple(),
239 ArchFS = ArchFS + "," + FS.str();
243 ParseSubtargetFeatures(CPUString, ArchFS);
245 // FIXME: This used enable V6T2 support implicitly for Thumb2 mode.
246 // Assert this for now to make the change obvious.
247 assert(hasV6T2Ops() || !hasThumb2());
249 // Keep a pointer to static instruction cost data for the specified CPU.
250 SchedModel = getSchedModelForCPU(CPUString);
252 // Initialize scheduling itinerary for the specified CPU.
253 InstrItins = getInstrItineraryForCPU(CPUString);
255 if (TargetABI == ARM_ABI_UNKNOWN) {
256 switch (TargetTriple.getEnvironment()) {
257 case Triple::Android:
260 case Triple::GNUEABI:
261 case Triple::GNUEABIHF:
262 TargetABI = ARM_ABI_AAPCS;
265 if ((isTargetIOS() && isMClass()) ||
266 (TargetTriple.isOSBinFormatMachO() &&
267 TargetTriple.getOS() == Triple::UnknownOS))
268 TargetABI = ARM_ABI_AAPCS;
270 TargetABI = ARM_ABI_APCS;
275 // FIXME: this is invalid for WindowsCE
276 if (isTargetWindows()) {
277 TargetABI = ARM_ABI_AAPCS;
286 UseMovt = hasV6T2Ops() && ArmUseMOVT;
288 if (isTargetMachO()) {
289 IsR9Reserved = ReserveR9 | !HasV6Ops;
290 SupportsTailCall = !isTargetIOS() || !getTargetTriple().isOSVersionLT(5, 0);
292 IsR9Reserved = ReserveR9;
293 SupportsTailCall = !isThumb1Only();
296 if (!isThumb() || hasThumb2())
297 PostRAScheduler = true;
301 // Assume pre-ARMv6 doesn't support unaligned accesses.
303 // ARMv6 may or may not support unaligned accesses depending on the
304 // SCTLR.U bit, which is architecture-specific. We assume ARMv6
305 // Darwin and NetBSD targets support unaligned accesses, and others don't.
307 // ARMv7 always has SCTLR.U set to 1, but it has a new SCTLR.A bit
308 // which raises an alignment fault on unaligned accesses. Linux
309 // defaults this bit to 0 and handles it as a system-wide (not
310 // per-process) setting. It is therefore safe to assume that ARMv7+
311 // Linux targets support unaligned accesses. The same goes for NaCl.
313 // The above behavior is consistent with GCC.
315 (hasV7Ops() && (isTargetLinux() || isTargetNaCl() ||
316 isTargetNetBSD())) ||
317 (hasV6Ops() && (isTargetMachO() || isTargetNetBSD()));
318 // The one exception is cortex-m0, which despite being v6, does not
319 // support unaligned accesses. Rather than make the above boolean
320 // expression even more obtuse, just override the value here.
321 if (isThumb1Only() && isMClass())
322 AllowsUnalignedMem = false;
325 AllowsUnalignedMem = false;
328 AllowsUnalignedMem = true;
334 RestrictIT = hasV8Ops() ? true : false;
344 // NEON f32 ops are non-IEEE 754 compliant. Darwin is ok with it by default.
345 uint64_t Bits = getFeatureBits();
346 if ((Bits & ARM::ProcA5 || Bits & ARM::ProcA8) && // Where this matters
347 (Options.UnsafeFPMath || isTargetDarwin()))
348 UseNEONForSinglePrecisionFP = true;
351 /// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol.
353 ARMSubtarget::GVIsIndirectSymbol(const GlobalValue *GV,
354 Reloc::Model RelocM) const {
355 if (RelocM == Reloc::Static)
358 // Materializable GVs (in JIT lazy compilation mode) do not require an extra
360 bool isDecl = GV->hasAvailableExternallyLinkage();
361 if (GV->isDeclaration() && !GV->isMaterializable())
364 if (!isTargetMachO()) {
365 // Extra load is needed for all externally visible.
366 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
370 if (RelocM == Reloc::PIC_) {
371 // If this is a strong reference to a definition, it is definitely not
373 if (!isDecl && !GV->isWeakForLinker())
376 // Unless we have a symbol with hidden visibility, we have to go through a
377 // normal $non_lazy_ptr stub because this symbol might be resolved late.
378 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
381 // If symbol visibility is hidden, we have a stub for common symbol
382 // references and external declarations.
383 if (isDecl || GV->hasCommonLinkage())
384 // Hidden $non_lazy_ptr reference.
389 // If this is a strong reference to a definition, it is definitely not
391 if (!isDecl && !GV->isWeakForLinker())
394 // Unless we have a symbol with hidden visibility, we have to go through a
395 // normal $non_lazy_ptr stub because this symbol might be resolved late.
396 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
404 unsigned ARMSubtarget::getMispredictionPenalty() const {
405 return SchedModel->MispredictPenalty;
408 bool ARMSubtarget::hasSinCos() const {
409 return getTargetTriple().getOS() == Triple::IOS &&
410 !getTargetTriple().isOSVersionLT(7, 0);
413 // Enable the PostMachineScheduler if the target selects it instead of
414 // PostRAScheduler. Currently only available on the command line via
416 bool ARMSubtarget::enablePostMachineScheduler() const {
417 return PostRAScheduler;
420 bool ARMSubtarget::enableAtomicExpandLoadLinked() const {
421 return hasAnyDataBarrier() && !isThumb1Only();
424 bool ARMSubtarget::enablePostRAScheduler(
425 CodeGenOpt::Level OptLevel,
426 TargetSubtargetInfo::AntiDepBreakMode& Mode,
427 RegClassVector& CriticalPathRCs) const {
428 Mode = TargetSubtargetInfo::ANTIDEP_NONE;
429 return PostRAScheduler && OptLevel >= CodeGenOpt::Default;