Avoid NEON SP-FP unless unsafe-math or Darwin
[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 static cl::opt<bool>
42 StrictAlign("arm-strict-align", cl::Hidden,
43             cl::desc("Disallow all unaligned memory accesses"));
44
45 ARMSubtarget::ARMSubtarget(const std::string &TT, const std::string &CPU,
46                            const std::string &FS, const TargetOptions &Options)
47   : ARMGenSubtargetInfo(TT, CPU, FS)
48   , ARMProcFamily(Others)
49   , stackAlignment(4)
50   , CPUString(CPU)
51   , TargetTriple(TT)
52   , Options(Options)
53   , TargetABI(ARM_ABI_APCS) {
54   initializeEnvironment();
55   resetSubtargetFeatures(CPU, FS);
56 }
57
58 void ARMSubtarget::initializeEnvironment() {
59   HasV4TOps = false;
60   HasV5TOps = false;
61   HasV5TEOps = false;
62   HasV6Ops = false;
63   HasV6T2Ops = false;
64   HasV7Ops = false;
65   HasVFPv2 = false;
66   HasVFPv3 = false;
67   HasVFPv4 = false;
68   HasNEON = false;
69   UseNEONForSinglePrecisionFP = false;
70   UseMulOps = UseFusedMulOps;
71   SlowFPVMLx = false;
72   HasVMLxForwarding = false;
73   SlowFPBrcc = false;
74   InThumbMode = false;
75   HasThumb2 = false;
76   IsMClass = false;
77   NoARM = false;
78   PostRAScheduler = false;
79   IsR9Reserved = ReserveR9;
80   UseMovt = false;
81   SupportsTailCall = false;
82   HasFP16 = false;
83   HasD16 = false;
84   HasHardwareDivide = false;
85   HasHardwareDivideInARM = false;
86   HasT2ExtractPack = false;
87   HasDataBarrier = false;
88   Pref32BitThumb = false;
89   AvoidCPSRPartialUpdate = false;
90   AvoidMOVsShifterOperand = false;
91   HasRAS = false;
92   HasMPExtension = false;
93   FPOnlySP = false;
94   AllowsUnalignedMem = false;
95   Thumb2DSP = false;
96   UseNaClTrap = false;
97   UnsafeFPMath = false;
98 }
99
100 void ARMSubtarget::resetSubtargetFeatures(const MachineFunction *MF) {
101   AttributeSet FnAttrs = MF->getFunction()->getAttributes();
102   Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
103                                            "target-cpu");
104   Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
105                                           "target-features");
106   std::string CPU =
107     !CPUAttr.hasAttribute(Attribute::None) ?CPUAttr.getValueAsString() : "";
108   std::string FS =
109     !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
110   if (!FS.empty()) {
111     initializeEnvironment();
112     resetSubtargetFeatures(CPU, FS);
113   }
114 }
115
116 void ARMSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
117   if (CPUString.empty())
118     CPUString = "generic";
119
120   // Insert the architecture feature derived from the target triple into the
121   // feature string. This is important for setting features that are implied
122   // based on the architecture version.
123   std::string ArchFS = ARM_MC::ParseARMTriple(TargetTriple.getTriple(),
124                                               CPUString);
125   if (!FS.empty()) {
126     if (!ArchFS.empty())
127       ArchFS = ArchFS + "," + FS.str();
128     else
129       ArchFS = FS;
130   }
131   ParseSubtargetFeatures(CPUString, ArchFS);
132
133   // Thumb2 implies at least V6T2. FIXME: Fix tests to explicitly specify a
134   // ARM version or CPU and then remove this.
135   if (!HasV6T2Ops && hasThumb2())
136     HasV4TOps = HasV5TOps = HasV5TEOps = HasV6Ops = HasV6T2Ops = true;
137
138   // Keep a pointer to static instruction cost data for the specified CPU.
139   SchedModel = getSchedModelForCPU(CPUString);
140
141   // Initialize scheduling itinerary for the specified CPU.
142   InstrItins = getInstrItineraryForCPU(CPUString);
143
144   if ((TargetTriple.getTriple().find("eabi") != std::string::npos) ||
145       (isTargetIOS() && isMClass()))
146     // FIXME: We might want to separate AAPCS and EABI. Some systems, e.g.
147     // Darwin-EABI conforms to AACPS but not the rest of EABI.
148     TargetABI = ARM_ABI_AAPCS;
149
150   if (isAAPCS_ABI())
151     stackAlignment = 8;
152
153   if (!isTargetIOS())
154     UseMovt = hasV6T2Ops();
155   else {
156     IsR9Reserved = ReserveR9 | !HasV6Ops;
157     UseMovt = DarwinUseMOVT && hasV6T2Ops();
158     SupportsTailCall = !getTargetTriple().isOSVersionLT(5, 0);
159   }
160
161   if (!isThumb() || hasThumb2())
162     PostRAScheduler = true;
163
164   // v6+ may or may not support unaligned mem access depending on the system
165   // configuration.
166   if (!StrictAlign && hasV6Ops() && isTargetDarwin())
167     AllowsUnalignedMem = true;
168
169   // NEON f32 ops are non-IEEE 754 compliant. Darwin is ok with it by default.
170   uint64_t Bits = getFeatureBits();
171   if ((Bits & ARM::ProcA5 || Bits & ARM::ProcA8) && // Where this matters
172       (Options.UnsafeFPMath || isTargetDarwin()))
173     UseNEONForSinglePrecisionFP = true;
174 }
175
176 /// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol.
177 bool
178 ARMSubtarget::GVIsIndirectSymbol(const GlobalValue *GV,
179                                  Reloc::Model RelocM) const {
180   if (RelocM == Reloc::Static)
181     return false;
182
183   // Materializable GVs (in JIT lazy compilation mode) do not require an extra
184   // load from stub.
185   bool isDecl = GV->hasAvailableExternallyLinkage();
186   if (GV->isDeclaration() && !GV->isMaterializable())
187     isDecl = true;
188
189   if (!isTargetDarwin()) {
190     // Extra load is needed for all externally visible.
191     if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
192       return false;
193     return true;
194   } else {
195     if (RelocM == Reloc::PIC_) {
196       // If this is a strong reference to a definition, it is definitely not
197       // through a stub.
198       if (!isDecl && !GV->isWeakForLinker())
199         return false;
200
201       // Unless we have a symbol with hidden visibility, we have to go through a
202       // normal $non_lazy_ptr stub because this symbol might be resolved late.
203       if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
204         return true;
205
206       // If symbol visibility is hidden, we have a stub for common symbol
207       // references and external declarations.
208       if (isDecl || GV->hasCommonLinkage())
209         // Hidden $non_lazy_ptr reference.
210         return true;
211
212       return false;
213     } else {
214       // If this is a strong reference to a definition, it is definitely not
215       // through a stub.
216       if (!isDecl && !GV->isWeakForLinker())
217         return false;
218
219       // Unless we have a symbol with hidden visibility, we have to go through a
220       // normal $non_lazy_ptr stub because this symbol might be resolved late.
221       if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
222         return true;
223     }
224   }
225
226   return false;
227 }
228
229 unsigned ARMSubtarget::getMispredictionPenalty() const {
230   return SchedModel->MispredictPenalty;
231 }
232
233 bool ARMSubtarget::enablePostRAScheduler(
234            CodeGenOpt::Level OptLevel,
235            TargetSubtargetInfo::AntiDepBreakMode& Mode,
236            RegClassVector& CriticalPathRCs) const {
237   Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
238   CriticalPathRCs.clear();
239   CriticalPathRCs.push_back(&ARM::GPRRegClass);
240   return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
241 }