Add ARM big endian Target (armeb, thumbeb)
[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 "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"
23
24 #define GET_SUBTARGETINFO_TARGET_DESC
25 #define GET_SUBTARGETINFO_CTOR
26 #include "ARMGenSubtargetInfo.inc"
27
28 using namespace llvm;
29
30 static cl::opt<bool>
31 ReserveR9("arm-reserve-r9", cl::Hidden,
32           cl::desc("Reserve R9, making it unavailable as GPR"));
33
34 static cl::opt<bool>
35 ArmUseMOVT("arm-use-movt", cl::init(true), cl::Hidden);
36
37 static cl::opt<bool>
38 UseFusedMulOps("arm-use-mulops",
39                cl::init(true), cl::Hidden);
40
41 enum AlignMode {
42   DefaultAlign,
43   StrictAlign,
44   NoStrictAlign
45 };
46
47 static cl::opt<AlignMode>
48 Align(cl::desc("Load/store alignment support"),
49       cl::Hidden, cl::init(DefaultAlign),
50       cl::values(
51           clEnumValN(DefaultAlign,  "arm-default-align",
52                      "Generate unaligned accesses only on hardware/OS "
53                      "combinations that are known to support them"),
54           clEnumValN(StrictAlign,   "arm-strict-align",
55                      "Disallow all unaligned memory accesses"),
56           clEnumValN(NoStrictAlign, "arm-no-strict-align",
57                      "Allow unaligned memory accesses"),
58           clEnumValEnd));
59
60 enum ITMode {
61   DefaultIT,
62   RestrictedIT,
63   NoRestrictedIT
64 };
65
66 static cl::opt<ITMode>
67 IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT),
68    cl::ZeroOrMore,
69    cl::values(clEnumValN(DefaultIT, "arm-default-it",
70                          "Generate IT block based on arch"),
71               clEnumValN(RestrictedIT, "arm-restrict-it",
72                          "Disallow deprecated IT based on ARMv8"),
73               clEnumValN(NoRestrictedIT, "arm-no-restrict-it",
74                          "Allow IT blocks based on ARMv7"),
75               clEnumValEnd));
76
77 ARMSubtarget::ARMSubtarget(const std::string &TT, const std::string &CPU,
78                            const std::string &FS, bool IsLittle,
79                            const TargetOptions &Options)
80   : ARMGenSubtargetInfo(TT, CPU, FS)
81   , ARMProcFamily(Others)
82   , ARMProcClass(None)
83   , stackAlignment(4)
84   , CPUString(CPU)
85   , IsLittle(IsLittle)
86   , TargetTriple(TT)
87   , Options(Options)
88   , TargetABI(ARM_ABI_UNKNOWN) {
89   initializeEnvironment();
90   resetSubtargetFeatures(CPU, FS);
91 }
92
93 void ARMSubtarget::initializeEnvironment() {
94   HasV4TOps = false;
95   HasV5TOps = false;
96   HasV5TEOps = false;
97   HasV6Ops = false;
98   HasV6MOps = false;
99   HasV6T2Ops = false;
100   HasV7Ops = false;
101   HasV8Ops = false;
102   HasVFPv2 = false;
103   HasVFPv3 = false;
104   HasVFPv4 = false;
105   HasFPARMv8 = false;
106   HasNEON = false;
107   MinSize = false;
108   UseNEONForSinglePrecisionFP = false;
109   UseMulOps = UseFusedMulOps;
110   SlowFPVMLx = false;
111   HasVMLxForwarding = false;
112   SlowFPBrcc = false;
113   InThumbMode = false;
114   HasThumb2 = false;
115   NoARM = false;
116   PostRAScheduler = false;
117   IsR9Reserved = ReserveR9;
118   UseMovt = false;
119   SupportsTailCall = false;
120   HasFP16 = false;
121   HasD16 = false;
122   HasHardwareDivide = false;
123   HasHardwareDivideInARM = false;
124   HasT2ExtractPack = false;
125   HasDataBarrier = false;
126   Pref32BitThumb = false;
127   AvoidCPSRPartialUpdate = false;
128   AvoidMOVsShifterOperand = false;
129   HasRAS = false;
130   HasMPExtension = false;
131   HasVirtualization = false;
132   FPOnlySP = false;
133   HasPerfMon = false;
134   HasTrustZone = false;
135   HasCrypto = false;
136   HasCRC = false;
137   AllowsUnalignedMem = false;
138   Thumb2DSP = false;
139   UseNaClTrap = false;
140   UnsafeFPMath = false;
141 }
142
143 void ARMSubtarget::resetSubtargetFeatures(const MachineFunction *MF) {
144   AttributeSet FnAttrs = MF->getFunction()->getAttributes();
145   Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
146                                            "target-cpu");
147   Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
148                                           "target-features");
149   std::string CPU =
150     !CPUAttr.hasAttribute(Attribute::None) ?CPUAttr.getValueAsString() : "";
151   std::string FS =
152     !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
153   if (!FS.empty()) {
154     initializeEnvironment();
155     resetSubtargetFeatures(CPU, FS);
156   }
157
158   MinSize =
159       FnAttrs.hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
160 }
161
162 void ARMSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
163   if (CPUString.empty()) {
164     if (isTargetIOS() && TargetTriple.getArchName().endswith("v7s"))
165       // Default to the Swift CPU when targeting armv7s/thumbv7s.
166       CPUString = "swift";
167     else
168       CPUString = "generic";
169   }
170
171   // Insert the architecture feature derived from the target triple into the
172   // feature string. This is important for setting features that are implied
173   // based on the architecture version.
174   std::string ArchFS = ARM_MC::ParseARMTriple(TargetTriple.getTriple(),
175                                               CPUString);
176   if (!FS.empty()) {
177     if (!ArchFS.empty())
178       ArchFS = ArchFS + "," + FS.str();
179     else
180       ArchFS = FS;
181   }
182   ParseSubtargetFeatures(CPUString, ArchFS);
183
184   // FIXME: This used enable V6T2 support implicitly for Thumb2 mode.
185   // Assert this for now to make the change obvious.
186   assert(hasV6T2Ops() || !hasThumb2());
187
188   // Keep a pointer to static instruction cost data for the specified CPU.
189   SchedModel = getSchedModelForCPU(CPUString);
190
191   // Initialize scheduling itinerary for the specified CPU.
192   InstrItins = getInstrItineraryForCPU(CPUString);
193
194   if (TargetABI == ARM_ABI_UNKNOWN) {
195     switch (TargetTriple.getEnvironment()) {
196     case Triple::Android:
197     case Triple::EABI:
198     case Triple::EABIHF:
199     case Triple::GNUEABI:
200     case Triple::GNUEABIHF:
201       TargetABI = ARM_ABI_AAPCS;
202       break;
203     default:
204       if ((isTargetIOS() && isMClass()) ||
205           (TargetTriple.isOSBinFormatMachO() &&
206            TargetTriple.getOS() == Triple::UnknownOS))
207         TargetABI = ARM_ABI_AAPCS;
208       else
209         TargetABI = ARM_ABI_APCS;
210       break;
211     }
212   }
213
214   if (isAAPCS_ABI())
215     stackAlignment = 8;
216   if (isTargetNaCl())
217     stackAlignment = 16;
218
219   UseMovt = hasV6T2Ops() && ArmUseMOVT;
220
221   if (isTargetMachO()) {
222     IsR9Reserved = ReserveR9 | !HasV6Ops;
223     SupportsTailCall = !isTargetIOS() || !getTargetTriple().isOSVersionLT(5, 0);
224   } else {
225     IsR9Reserved = ReserveR9;
226     SupportsTailCall = !isThumb1Only();
227   }
228
229   if (!isThumb() || hasThumb2())
230     PostRAScheduler = true;
231
232   switch (Align) {
233     case DefaultAlign:
234       // Assume pre-ARMv6 doesn't support unaligned accesses.
235       //
236       // ARMv6 may or may not support unaligned accesses depending on the
237       // SCTLR.U bit, which is architecture-specific. We assume ARMv6
238       // Darwin targets support unaligned accesses, and others don't.
239       //
240       // ARMv7 always has SCTLR.U set to 1, but it has a new SCTLR.A bit
241       // which raises an alignment fault on unaligned accesses. Linux
242       // defaults this bit to 0 and handles it as a system-wide (not
243       // per-process) setting. It is therefore safe to assume that ARMv7+
244       // Linux targets support unaligned accesses. The same goes for NaCl.
245       //
246       // The above behavior is consistent with GCC.
247       AllowsUnalignedMem =
248           (hasV7Ops() && (isTargetLinux() || isTargetNaCl() ||
249                           isTargetNetBSD())) ||
250           (hasV6Ops() && (isTargetMachO() || isTargetNetBSD()));
251       break;
252     case StrictAlign:
253       AllowsUnalignedMem = false;
254       break;
255     case NoStrictAlign:
256       AllowsUnalignedMem = true;
257       break;
258   }
259
260   switch (IT) {
261   case DefaultIT:
262     RestrictIT = hasV8Ops() ? true : false;
263     break;
264   case RestrictedIT:
265     RestrictIT = true;
266     break;
267   case NoRestrictedIT:
268     RestrictIT = false;
269     break;
270   }
271
272   // NEON f32 ops are non-IEEE 754 compliant. Darwin is ok with it by default.
273   uint64_t Bits = getFeatureBits();
274   if ((Bits & ARM::ProcA5 || Bits & ARM::ProcA8) && // Where this matters
275       (Options.UnsafeFPMath || isTargetDarwin()))
276     UseNEONForSinglePrecisionFP = true;
277 }
278
279 /// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol.
280 bool
281 ARMSubtarget::GVIsIndirectSymbol(const GlobalValue *GV,
282                                  Reloc::Model RelocM) const {
283   if (RelocM == Reloc::Static)
284     return false;
285
286   // Materializable GVs (in JIT lazy compilation mode) do not require an extra
287   // load from stub.
288   bool isDecl = GV->hasAvailableExternallyLinkage();
289   if (GV->isDeclaration() && !GV->isMaterializable())
290     isDecl = true;
291
292   if (!isTargetMachO()) {
293     // Extra load is needed for all externally visible.
294     if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
295       return false;
296     return true;
297   } else {
298     if (RelocM == Reloc::PIC_) {
299       // If this is a strong reference to a definition, it is definitely not
300       // through a stub.
301       if (!isDecl && !GV->isWeakForLinker())
302         return false;
303
304       // Unless we have a symbol with hidden visibility, we have to go through a
305       // normal $non_lazy_ptr stub because this symbol might be resolved late.
306       if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
307         return true;
308
309       // If symbol visibility is hidden, we have a stub for common symbol
310       // references and external declarations.
311       if (isDecl || GV->hasCommonLinkage())
312         // Hidden $non_lazy_ptr reference.
313         return true;
314
315       return false;
316     } else {
317       // If this is a strong reference to a definition, it is definitely not
318       // through a stub.
319       if (!isDecl && !GV->isWeakForLinker())
320         return false;
321
322       // Unless we have a symbol with hidden visibility, we have to go through a
323       // normal $non_lazy_ptr stub because this symbol might be resolved late.
324       if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
325         return true;
326     }
327   }
328
329   return false;
330 }
331
332 unsigned ARMSubtarget::getMispredictionPenalty() const {
333   return SchedModel->MispredictPenalty;
334 }
335
336 bool ARMSubtarget::hasSinCos() const {
337   return getTargetTriple().getOS() == Triple::IOS &&
338     !getTargetTriple().isOSVersionLT(7, 0);
339 }
340
341 bool ARMSubtarget::enablePostRAScheduler(
342            CodeGenOpt::Level OptLevel,
343            TargetSubtargetInfo::AntiDepBreakMode& Mode,
344            RegClassVector& CriticalPathRCs) const {
345   Mode = TargetSubtargetInfo::ANTIDEP_NONE;
346   return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
347 }