Add a subtarget feature 'v8' to the ARM backend.
[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/GlobalValue.h"
19 #include "llvm/IR/Function.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 DarwinUseMOVT("arm-darwin-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 ARMSubtarget::ARMSubtarget(const std::string &TT, const std::string &CPU,
61                            const std::string &FS, const TargetOptions &Options)
62   : ARMGenSubtargetInfo(TT, CPU, FS)
63   , ARMProcFamily(Others)
64   , stackAlignment(4)
65   , CPUString(CPU)
66   , TargetTriple(TT)
67   , Options(Options)
68   , TargetABI(ARM_ABI_APCS) {
69   initializeEnvironment();
70   resetSubtargetFeatures(CPU, FS);
71 }
72
73 void ARMSubtarget::initializeEnvironment() {
74   HasV4TOps = false;
75   HasV5TOps = false;
76   HasV5TEOps = false;
77   HasV6Ops = false;
78   HasV6T2Ops = false;
79   HasV7Ops = false;
80   HasV8Ops = false;
81   HasVFPv2 = false;
82   HasVFPv3 = false;
83   HasVFPv4 = false;
84   HasNEON = false;
85   UseNEONForSinglePrecisionFP = false;
86   UseMulOps = UseFusedMulOps;
87   SlowFPVMLx = false;
88   HasVMLxForwarding = false;
89   SlowFPBrcc = false;
90   InThumbMode = false;
91   HasThumb2 = false;
92   IsMClass = false;
93   NoARM = false;
94   PostRAScheduler = false;
95   IsR9Reserved = ReserveR9;
96   UseMovt = false;
97   SupportsTailCall = false;
98   HasFP16 = false;
99   HasD16 = false;
100   HasHardwareDivide = false;
101   HasHardwareDivideInARM = false;
102   HasT2ExtractPack = false;
103   HasDataBarrier = false;
104   Pref32BitThumb = false;
105   AvoidCPSRPartialUpdate = false;
106   AvoidMOVsShifterOperand = false;
107   HasRAS = false;
108   HasMPExtension = false;
109   FPOnlySP = false;
110   HasPerfMon = false;
111   HasTrustZone = false;
112   AllowsUnalignedMem = false;
113   Thumb2DSP = false;
114   UseNaClTrap = false;
115   UnsafeFPMath = false;
116 }
117
118 void ARMSubtarget::resetSubtargetFeatures(const MachineFunction *MF) {
119   AttributeSet FnAttrs = MF->getFunction()->getAttributes();
120   Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
121                                            "target-cpu");
122   Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
123                                           "target-features");
124   std::string CPU =
125     !CPUAttr.hasAttribute(Attribute::None) ?CPUAttr.getValueAsString() : "";
126   std::string FS =
127     !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
128   if (!FS.empty()) {
129     initializeEnvironment();
130     resetSubtargetFeatures(CPU, FS);
131   }
132 }
133
134 void ARMSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
135   if (CPUString.empty())
136     CPUString = "generic";
137
138   // Insert the architecture feature derived from the target triple into the
139   // feature string. This is important for setting features that are implied
140   // based on the architecture version.
141   std::string ArchFS = ARM_MC::ParseARMTriple(TargetTriple.getTriple(),
142                                               CPUString);
143   if (!FS.empty()) {
144     if (!ArchFS.empty())
145       ArchFS = ArchFS + "," + FS.str();
146     else
147       ArchFS = FS;
148   }
149   ParseSubtargetFeatures(CPUString, ArchFS);
150
151   // Thumb2 implies at least V6T2. FIXME: Fix tests to explicitly specify a
152   // ARM version or CPU and then remove this.
153   if (!HasV6T2Ops && hasThumb2())
154     HasV4TOps = HasV5TOps = HasV5TEOps = HasV6Ops = HasV6T2Ops = true;
155
156   // Keep a pointer to static instruction cost data for the specified CPU.
157   SchedModel = getSchedModelForCPU(CPUString);
158
159   // Initialize scheduling itinerary for the specified CPU.
160   InstrItins = getInstrItineraryForCPU(CPUString);
161
162   if ((TargetTriple.getTriple().find("eabi") != std::string::npos) ||
163       (isTargetIOS() && isMClass()))
164     // FIXME: We might want to separate AAPCS and EABI. Some systems, e.g.
165     // Darwin-EABI conforms to AACPS but not the rest of EABI.
166     TargetABI = ARM_ABI_AAPCS;
167
168   if (isAAPCS_ABI())
169     stackAlignment = 8;
170
171   if (!isTargetIOS())
172     UseMovt = hasV6T2Ops();
173   else {
174     IsR9Reserved = ReserveR9 | !HasV6Ops;
175     UseMovt = DarwinUseMOVT && hasV6T2Ops();
176     SupportsTailCall = !getTargetTriple().isOSVersionLT(5, 0);
177   }
178
179   if (!isThumb() || hasThumb2())
180     PostRAScheduler = true;
181
182   switch (Align) {
183     case DefaultAlign:
184       // Assume pre-ARMv6 doesn't support unaligned accesses.
185       //
186       // ARMv6 may or may not support unaligned accesses depending on the
187       // SCTLR.U bit, which is architecture-specific. We assume ARMv6
188       // Darwin targets support unaligned accesses, and others don't.
189       //
190       // ARMv7 always has SCTLR.U set to 1, but it has a new SCTLR.A bit
191       // which raises an alignment fault on unaligned accesses. Linux
192       // defaults this bit to 0 and handles it as a system-wide (not
193       // per-process) setting. It is therefore safe to assume that ARMv7+
194       // Linux targets support unaligned accesses. The same goes for NaCl.
195       //
196       // The above behavior is consistent with GCC.
197       AllowsUnalignedMem = (
198           (hasV7Ops() && (isTargetLinux() || isTargetNaCl())) ||
199           (hasV6Ops() && isTargetDarwin()));
200       break;
201     case StrictAlign:
202       AllowsUnalignedMem = false;
203       break;
204     case NoStrictAlign:
205       AllowsUnalignedMem = true;
206       break;
207   }
208
209   // NEON f32 ops are non-IEEE 754 compliant. Darwin is ok with it by default.
210   uint64_t Bits = getFeatureBits();
211   if ((Bits & ARM::ProcA5 || Bits & ARM::ProcA8) && // Where this matters
212       (Options.UnsafeFPMath || isTargetDarwin()))
213     UseNEONForSinglePrecisionFP = true;
214 }
215
216 /// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol.
217 bool
218 ARMSubtarget::GVIsIndirectSymbol(const GlobalValue *GV,
219                                  Reloc::Model RelocM) const {
220   if (RelocM == Reloc::Static)
221     return false;
222
223   // Materializable GVs (in JIT lazy compilation mode) do not require an extra
224   // load from stub.
225   bool isDecl = GV->hasAvailableExternallyLinkage();
226   if (GV->isDeclaration() && !GV->isMaterializable())
227     isDecl = true;
228
229   if (!isTargetDarwin()) {
230     // Extra load is needed for all externally visible.
231     if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
232       return false;
233     return true;
234   } else {
235     if (RelocM == Reloc::PIC_) {
236       // If this is a strong reference to a definition, it is definitely not
237       // through a stub.
238       if (!isDecl && !GV->isWeakForLinker())
239         return false;
240
241       // Unless we have a symbol with hidden visibility, we have to go through a
242       // normal $non_lazy_ptr stub because this symbol might be resolved late.
243       if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
244         return true;
245
246       // If symbol visibility is hidden, we have a stub for common symbol
247       // references and external declarations.
248       if (isDecl || GV->hasCommonLinkage())
249         // Hidden $non_lazy_ptr reference.
250         return true;
251
252       return false;
253     } else {
254       // If this is a strong reference to a definition, it is definitely not
255       // through a stub.
256       if (!isDecl && !GV->isWeakForLinker())
257         return false;
258
259       // Unless we have a symbol with hidden visibility, we have to go through a
260       // normal $non_lazy_ptr stub because this symbol might be resolved late.
261       if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
262         return true;
263     }
264   }
265
266   return false;
267 }
268
269 unsigned ARMSubtarget::getMispredictionPenalty() const {
270   return SchedModel->MispredictPenalty;
271 }
272
273 bool ARMSubtarget::enablePostRAScheduler(
274            CodeGenOpt::Level OptLevel,
275            TargetSubtargetInfo::AntiDepBreakMode& Mode,
276            RegClassVector& CriticalPathRCs) const {
277   Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
278   CriticalPathRCs.clear();
279   CriticalPathRCs.push_back(&ARM::GPRRegClass);
280   return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
281 }