5a09ecced196bbda55ea17cbca24903f99a2543a
[oota-llvm.git] / lib / Target / ARM / ARMSubtarget.cpp
1 //===-- ARMSubtarget.cpp - ARM Subtarget Information ----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ARM specific subclass of TargetSubtargetInfo.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ARMSubtarget.h"
15 #include "ARMFrameLowering.h"
16 #include "ARMISelLowering.h"
17 #include "ARMInstrInfo.h"
18 #include "ARMSelectionDAGInfo.h"
19 #include "ARMSubtarget.h"
20 #include "ARMMachineFunctionInfo.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"
30 #include "llvm/Target/TargetRegisterInfo.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32
33 using namespace llvm;
34
35 #define DEBUG_TYPE "arm-subtarget"
36
37 #define GET_SUBTARGETINFO_TARGET_DESC
38 #define GET_SUBTARGETINFO_CTOR
39 #include "ARMGenSubtargetInfo.inc"
40
41 static cl::opt<bool>
42 ReserveR9("arm-reserve-r9", cl::Hidden,
43           cl::desc("Reserve R9, making it unavailable as GPR"));
44
45 static cl::opt<bool>
46 ArmUseMOVT("arm-use-movt", cl::init(true), cl::Hidden);
47
48 static cl::opt<bool>
49 UseFusedMulOps("arm-use-mulops",
50                cl::init(true), cl::Hidden);
51
52 namespace {
53 enum AlignMode {
54   DefaultAlign,
55   StrictAlign,
56   NoStrictAlign
57 };
58 }
59
60 static cl::opt<AlignMode>
61 Align(cl::desc("Load/store alignment support"),
62       cl::Hidden, cl::init(DefaultAlign),
63       cl::values(
64           clEnumValN(DefaultAlign,  "arm-default-align",
65                      "Generate unaligned accesses only on hardware/OS "
66                      "combinations that are known to support them"),
67           clEnumValN(StrictAlign,   "arm-strict-align",
68                      "Disallow all unaligned memory accesses"),
69           clEnumValN(NoStrictAlign, "arm-no-strict-align",
70                      "Allow unaligned memory accesses"),
71           clEnumValEnd));
72
73 enum ITMode {
74   DefaultIT,
75   RestrictedIT,
76   NoRestrictedIT
77 };
78
79 static cl::opt<ITMode>
80 IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT),
81    cl::ZeroOrMore,
82    cl::values(clEnumValN(DefaultIT, "arm-default-it",
83                          "Generate IT block based on arch"),
84               clEnumValN(RestrictedIT, "arm-restrict-it",
85                          "Disallow deprecated IT based on ARMv8"),
86               clEnumValN(NoRestrictedIT, "arm-no-restrict-it",
87                          "Allow IT blocks based on ARMv7"),
88               clEnumValEnd));
89
90 static std::string computeDataLayout(ARMSubtarget &ST) {
91   std::string Ret = "";
92
93   if (ST.isLittle())
94     // Little endian.
95     Ret += "e";
96   else
97     // Big endian.
98     Ret += "E";
99
100   Ret += DataLayout::getManglingComponent(ST.getTargetTriple());
101
102   // Pointers are 32 bits and aligned to 32 bits.
103   Ret += "-p:32:32";
104
105   // On thumb, i16,i18 and i1 have natural aligment requirements, but we try to
106   // align to 32.
107   if (ST.isThumb())
108     Ret += "-i1:8:32-i8:8:32-i16:16:32";
109
110   // ABIs other than APCS have 64 bit integers with natural alignment.
111   if (!ST.isAPCS_ABI())
112     Ret += "-i64:64";
113
114   // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
115   // bits, others to 64 bits. We always try to align to 64 bits.
116   if (ST.isAPCS_ABI())
117     Ret += "-f64:32:64";
118
119   // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
120   // to 64. We always ty to give them natural alignment.
121   if (ST.isAPCS_ABI())
122     Ret += "-v64:32:64-v128:32:128";
123   else
124     Ret += "-v128:64:128";
125
126   // On thumb and APCS, only try to align aggregates to 32 bits (the default is
127   // 64 bits).
128   if (ST.isThumb() || ST.isAPCS_ABI())
129     Ret += "-a:0:32";
130
131   // Integer registers are 32 bits.
132   Ret += "-n32";
133
134   // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
135   // aligned everywhere else.
136   if (ST.isTargetNaCl())
137     Ret += "-S128";
138   else if (ST.isAAPCS_ABI())
139     Ret += "-S64";
140   else
141     Ret += "-S32";
142
143   return Ret;
144 }
145
146 /// initializeSubtargetDependencies - Initializes using a CPU and feature string
147 /// so that we can use initializer lists for subtarget initialization.
148 ARMSubtarget &ARMSubtarget::initializeSubtargetDependencies(StringRef CPU,
149                                                             StringRef FS) {
150   initializeEnvironment();
151   resetSubtargetFeatures(CPU, FS);
152   return *this;
153 }
154
155 ARMSubtarget::ARMSubtarget(const std::string &TT, const std::string &CPU,
156                            const std::string &FS, TargetMachine &TM,
157                            bool IsLittle, const TargetOptions &Options)
158     : ARMGenSubtargetInfo(TT, CPU, FS), ARMProcFamily(Others),
159       ARMProcClass(None), stackAlignment(4), CPUString(CPU), IsLittle(IsLittle),
160       TargetTriple(TT), Options(Options), TargetABI(ARM_ABI_UNKNOWN),
161       DL(computeDataLayout(initializeSubtargetDependencies(CPU, FS))),
162       TSInfo(DL),
163       InstrInfo(isThumb1Only()
164                     ? (ARMBaseInstrInfo *)new Thumb1InstrInfo(*this)
165                     : !isThumb()
166                           ? (ARMBaseInstrInfo *)new ARMInstrInfo(*this)
167                           : (ARMBaseInstrInfo *)new Thumb2InstrInfo(*this)),
168       TLInfo(TM),
169       FrameLowering(!isThumb1Only()
170                         ? new ARMFrameLowering(*this)
171                         : (ARMFrameLowering *)new Thumb1FrameLowering(*this)) {}
172
173 void ARMSubtarget::initializeEnvironment() {
174   HasV4TOps = false;
175   HasV5TOps = false;
176   HasV5TEOps = false;
177   HasV6Ops = false;
178   HasV6MOps = false;
179   HasV6T2Ops = false;
180   HasV7Ops = false;
181   HasV8Ops = false;
182   HasVFPv2 = false;
183   HasVFPv3 = false;
184   HasVFPv4 = false;
185   HasFPARMv8 = false;
186   HasNEON = false;
187   UseNEONForSinglePrecisionFP = false;
188   UseMulOps = UseFusedMulOps;
189   SlowFPVMLx = false;
190   HasVMLxForwarding = false;
191   SlowFPBrcc = false;
192   InThumbMode = false;
193   HasThumb2 = false;
194   NoARM = false;
195   IsR9Reserved = ReserveR9;
196   UseMovt = false;
197   SupportsTailCall = false;
198   HasFP16 = false;
199   HasD16 = false;
200   HasHardwareDivide = false;
201   HasHardwareDivideInARM = false;
202   HasT2ExtractPack = false;
203   HasDataBarrier = false;
204   Pref32BitThumb = false;
205   AvoidCPSRPartialUpdate = false;
206   AvoidMOVsShifterOperand = false;
207   HasRAS = false;
208   HasMPExtension = false;
209   HasVirtualization = false;
210   FPOnlySP = false;
211   HasPerfMon = false;
212   HasTrustZone = false;
213   HasCrypto = false;
214   HasCRC = false;
215   HasZeroCycleZeroing = false;
216   AllowsUnalignedMem = false;
217   Thumb2DSP = false;
218   UseNaClTrap = false;
219   UnsafeFPMath = false;
220 }
221
222 void ARMSubtarget::resetSubtargetFeatures(const MachineFunction *MF) {
223   AttributeSet FnAttrs = MF->getFunction()->getAttributes();
224   Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
225                                            "target-cpu");
226   Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
227                                           "target-features");
228   std::string CPU =
229     !CPUAttr.hasAttribute(Attribute::None) ?CPUAttr.getValueAsString() : "";
230   std::string FS =
231     !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
232   if (!FS.empty()) {
233     initializeEnvironment();
234     resetSubtargetFeatures(CPU, FS);
235   }
236 }
237
238 void ARMSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
239   if (CPUString.empty()) {
240     if (isTargetIOS() && TargetTriple.getArchName().endswith("v7s"))
241       // Default to the Swift CPU when targeting armv7s/thumbv7s.
242       CPUString = "swift";
243     else
244       CPUString = "generic";
245   }
246
247   // Insert the architecture feature derived from the target triple into the
248   // feature string. This is important for setting features that are implied
249   // based on the architecture version.
250   std::string ArchFS = ARM_MC::ParseARMTriple(TargetTriple.getTriple(),
251                                               CPUString);
252   if (!FS.empty()) {
253     if (!ArchFS.empty())
254       ArchFS = ArchFS + "," + FS.str();
255     else
256       ArchFS = FS;
257   }
258   ParseSubtargetFeatures(CPUString, ArchFS);
259
260   // FIXME: This used enable V6T2 support implicitly for Thumb2 mode.
261   // Assert this for now to make the change obvious.
262   assert(hasV6T2Ops() || !hasThumb2());
263
264   // Keep a pointer to static instruction cost data for the specified CPU.
265   SchedModel = getSchedModelForCPU(CPUString);
266
267   // Initialize scheduling itinerary for the specified CPU.
268   InstrItins = getInstrItineraryForCPU(CPUString);
269
270   if (TargetABI == ARM_ABI_UNKNOWN) {
271     switch (TargetTriple.getEnvironment()) {
272     case Triple::Android:
273     case Triple::EABI:
274     case Triple::EABIHF:
275     case Triple::GNUEABI:
276     case Triple::GNUEABIHF:
277       TargetABI = ARM_ABI_AAPCS;
278       break;
279     default:
280       if ((isTargetIOS() && isMClass()) ||
281           (TargetTriple.isOSBinFormatMachO() &&
282            TargetTriple.getOS() == Triple::UnknownOS))
283         TargetABI = ARM_ABI_AAPCS;
284       else
285         TargetABI = ARM_ABI_APCS;
286       break;
287     }
288   }
289
290   // FIXME: this is invalid for WindowsCE
291   if (isTargetWindows()) {
292     TargetABI = ARM_ABI_AAPCS;
293     NoARM = true;
294   }
295
296   if (isAAPCS_ABI())
297     stackAlignment = 8;
298   if (isTargetNaCl())
299     stackAlignment = 16;
300
301   UseMovt = hasV6T2Ops() && ArmUseMOVT;
302
303   if (isTargetMachO()) {
304     IsR9Reserved = ReserveR9 | !HasV6Ops;
305     SupportsTailCall = !isTargetIOS() || !getTargetTriple().isOSVersionLT(5, 0);
306   } else {
307     IsR9Reserved = ReserveR9;
308     SupportsTailCall = !isThumb1Only();
309   }
310
311   switch (Align) {
312     case DefaultAlign:
313       // Assume pre-ARMv6 doesn't support unaligned accesses.
314       //
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.
318       //
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.
324       //
325       // The above behavior is consistent with GCC.
326       AllowsUnalignedMem =
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;
335       break;
336     case StrictAlign:
337       AllowsUnalignedMem = false;
338       break;
339     case NoStrictAlign:
340       AllowsUnalignedMem = true;
341       break;
342   }
343
344   switch (IT) {
345   case DefaultIT:
346     RestrictIT = hasV8Ops() ? true : false;
347     break;
348   case RestrictedIT:
349     RestrictIT = true;
350     break;
351   case NoRestrictedIT:
352     RestrictIT = false;
353     break;
354   }
355
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;
361 }
362
363 /// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol.
364 bool
365 ARMSubtarget::GVIsIndirectSymbol(const GlobalValue *GV,
366                                  Reloc::Model RelocM) const {
367   if (RelocM == Reloc::Static)
368     return false;
369
370   // Materializable GVs (in JIT lazy compilation mode) do not require an extra
371   // load from stub.
372   bool isDecl = GV->hasAvailableExternallyLinkage();
373   if (GV->isDeclaration() && !GV->isMaterializable())
374     isDecl = true;
375
376   if (!isTargetMachO()) {
377     // Extra load is needed for all externally visible.
378     if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
379       return false;
380     return true;
381   } else {
382     if (RelocM == Reloc::PIC_) {
383       // If this is a strong reference to a definition, it is definitely not
384       // through a stub.
385       if (!isDecl && !GV->isWeakForLinker())
386         return false;
387
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.
391         return true;
392
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.
397         return true;
398
399       return false;
400     } else {
401       // If this is a strong reference to a definition, it is definitely not
402       // through a stub.
403       if (!isDecl && !GV->isWeakForLinker())
404         return false;
405
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.
409         return true;
410     }
411   }
412
413   return false;
414 }
415
416 unsigned ARMSubtarget::getMispredictionPenalty() const {
417   return SchedModel.MispredictPenalty;
418 }
419
420 bool ARMSubtarget::hasSinCos() const {
421   return getTargetTriple().getOS() == Triple::IOS &&
422     !getTargetTriple().isOSVersionLT(7, 0);
423 }
424
425 // This overrides the PostRAScheduler bit in the SchedModel for any CPU.
426 bool ARMSubtarget::enablePostMachineScheduler() const {
427   return (!isThumb() || hasThumb2());
428 }
429
430 bool ARMSubtarget::enableAtomicExpand() const {
431   return hasAnyDataBarrier() && !isThumb1Only();
432 }
433
434 bool ARMSubtarget::useMovt(const MachineFunction &MF) const {
435   // NOTE Windows on ARM needs to use mov.w/mov.t pairs to materialise 32-bit
436   // immediates as it is inherently position independent, and may be out of
437   // range otherwise.
438   return UseMovt && (isTargetWindows() ||
439                      !MF.getFunction()->getAttributes().hasAttribute(
440                          AttributeSet::FunctionIndex, Attribute::MinSize));
441 }