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