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