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