[ARM] Enable shrink-wrapping by default.
[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 "ARMFrameLowering.h"
16 #include "ARMISelLowering.h"
17 #include "ARMInstrInfo.h"
18 #include "ARMMachineFunctionInfo.h"
19 #include "ARMSelectionDAGInfo.h"
20 #include "ARMSubtarget.h"
21 #include "ARMTargetMachine.h"
22 #include "Thumb1FrameLowering.h"
23 #include "Thumb1InstrInfo.h"
24 #include "Thumb2InstrInfo.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/IR/Attributes.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/GlobalValue.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Target/TargetInstrInfo.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include "llvm/Target/TargetRegisterInfo.h"
33
34 using namespace llvm;
35
36 #define DEBUG_TYPE "arm-subtarget"
37
38 #define GET_SUBTARGETINFO_TARGET_DESC
39 #define GET_SUBTARGETINFO_CTOR
40 #include "ARMGenSubtargetInfo.inc"
41
42 static cl::opt<bool>
43 UseFusedMulOps("arm-use-mulops",
44                cl::init(true), cl::Hidden);
45
46 enum ITMode {
47   DefaultIT,
48   RestrictedIT,
49   NoRestrictedIT
50 };
51
52 static cl::opt<ITMode>
53 IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT),
54    cl::ZeroOrMore,
55    cl::values(clEnumValN(DefaultIT, "arm-default-it",
56                          "Generate IT block based on arch"),
57               clEnumValN(RestrictedIT, "arm-restrict-it",
58                          "Disallow deprecated IT based on ARMv8"),
59               clEnumValN(NoRestrictedIT, "arm-no-restrict-it",
60                          "Allow IT blocks based on ARMv7"),
61               clEnumValEnd));
62
63 /// ForceFastISel - Use the fast-isel, even for subtargets where it is not
64 /// currently supported (for testing only).
65 static cl::opt<bool>
66 ForceFastISel("arm-force-fast-isel",
67                cl::init(false), cl::Hidden);
68
69 /// initializeSubtargetDependencies - Initializes using a CPU and feature string
70 /// so that we can use initializer lists for subtarget initialization.
71 ARMSubtarget &ARMSubtarget::initializeSubtargetDependencies(StringRef CPU,
72                                                             StringRef FS) {
73   initializeEnvironment();
74   initSubtargetFeatures(CPU, FS);
75   return *this;
76 }
77
78 ARMFrameLowering *ARMSubtarget::initializeFrameLowering(StringRef CPU,
79                                                         StringRef FS) {
80   ARMSubtarget &STI = initializeSubtargetDependencies(CPU, FS);
81   if (STI.isThumb1Only())
82     return (ARMFrameLowering *)new Thumb1FrameLowering(STI);
83
84   return new ARMFrameLowering(STI);
85 }
86
87 ARMSubtarget::ARMSubtarget(const Triple &TT, const std::string &CPU,
88                            const std::string &FS,
89                            const ARMBaseTargetMachine &TM, bool IsLittle)
90     : ARMGenSubtargetInfo(TT, CPU, FS), ARMProcFamily(Others),
91       ARMProcClass(None), ARMArch(ARMv4t), stackAlignment(4), CPUString(CPU),
92       IsLittle(IsLittle), TargetTriple(TT), Options(TM.Options), TM(TM),
93       FrameLowering(initializeFrameLowering(CPU, FS)),
94       // At this point initializeSubtargetDependencies has been called so
95       // we can query directly.
96       InstrInfo(isThumb1Only()
97                     ? (ARMBaseInstrInfo *)new Thumb1InstrInfo(*this)
98                     : !isThumb()
99                           ? (ARMBaseInstrInfo *)new ARMInstrInfo(*this)
100                           : (ARMBaseInstrInfo *)new Thumb2InstrInfo(*this)),
101       TLInfo(TM, *this) {}
102
103 void ARMSubtarget::initializeEnvironment() {
104   HasV4TOps = false;
105   HasV5TOps = false;
106   HasV5TEOps = false;
107   HasV6Ops = false;
108   HasV6MOps = false;
109   HasV6KOps = false;
110   HasV6T2Ops = false;
111   HasV7Ops = false;
112   HasV8Ops = false;
113   HasV8_1aOps = false;
114   HasVFPv2 = false;
115   HasVFPv3 = false;
116   HasVFPv4 = false;
117   HasFPARMv8 = false;
118   HasNEON = false;
119   UseNEONForSinglePrecisionFP = false;
120   UseMulOps = UseFusedMulOps;
121   SlowFPVMLx = false;
122   HasVMLxForwarding = false;
123   SlowFPBrcc = false;
124   InThumbMode = false;
125   UseSoftFloat = false;
126   HasThumb2 = false;
127   NoARM = false;
128   ReserveR9 = false;
129   NoMovt = false;
130   SupportsTailCall = false;
131   HasFP16 = false;
132   HasD16 = false;
133   HasHardwareDivide = false;
134   HasHardwareDivideInARM = false;
135   HasT2ExtractPack = false;
136   HasDataBarrier = false;
137   Pref32BitThumb = false;
138   AvoidCPSRPartialUpdate = false;
139   AvoidMOVsShifterOperand = false;
140   HasRAS = false;
141   HasMPExtension = false;
142   HasVirtualization = false;
143   FPOnlySP = false;
144   HasPerfMon = false;
145   HasTrustZone = false;
146   HasCrypto = false;
147   HasCRC = false;
148   HasZeroCycleZeroing = false;
149   StrictAlign = false;
150   HasDSP = false;
151   UseNaClTrap = false;
152   GenLongCalls = false;
153   UnsafeFPMath = false;
154   UseSjLjEH = (isTargetDarwin() &&
155                TargetTriple.getSubArch() != Triple::ARMSubArch_v7k);
156 }
157
158 void ARMSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
159   if (CPUString.empty()) {
160     CPUString = "generic";
161
162     if (isTargetDarwin()) {
163       StringRef ArchName = TargetTriple.getArchName();
164       if (ArchName.endswith("v7s"))
165         // Default to the Swift CPU when targeting armv7s/thumbv7s.
166         CPUString = "swift";
167       else if (ArchName.endswith("v7k"))
168         // Default to the Cortex-a7 CPU when targeting armv7k/thumbv7k.
169         // ARMv7k does not use SjLj exception handling.
170         CPUString = "cortex-a7";
171     }
172   }
173
174   // Insert the architecture feature derived from the target triple into the
175   // feature string. This is important for setting features that are implied
176   // based on the architecture version.
177   std::string ArchFS = ARM_MC::ParseARMTriple(TargetTriple, CPUString);
178   if (!FS.empty()) {
179     if (!ArchFS.empty())
180       ArchFS = (Twine(ArchFS) + "," + FS).str();
181     else
182       ArchFS = FS;
183   }
184   ParseSubtargetFeatures(CPUString, ArchFS);
185
186   // FIXME: This used enable V6T2 support implicitly for Thumb2 mode.
187   // Assert this for now to make the change obvious.
188   assert(hasV6T2Ops() || !hasThumb2());
189
190   // Keep a pointer to static instruction cost data for the specified CPU.
191   SchedModel = getSchedModelForCPU(CPUString);
192
193   // Initialize scheduling itinerary for the specified CPU.
194   InstrItins = getInstrItineraryForCPU(CPUString);
195
196   // FIXME: this is invalid for WindowsCE
197   if (isTargetWindows())
198     NoARM = true;
199
200   if (isAAPCS_ABI())
201     stackAlignment = 8;
202   if (isTargetNaCl() || isAAPCS16_ABI())
203     stackAlignment = 16;
204
205   // FIXME: Completely disable sibcall for Thumb1 since ThumbRegisterInfo::
206   // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
207   // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
208   // support in the assembler and linker to be used. This would need to be
209   // fixed to fully support tail calls in Thumb1.
210   //
211   // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
212   // LR.  This means if we need to reload LR, it takes an extra instructions,
213   // which outweighs the value of the tail call; but here we don't know yet
214   // whether LR is going to be used.  Probably the right approach is to
215   // generate the tail call here and turn it back into CALL/RET in
216   // emitEpilogue if LR is used.
217
218   // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
219   // but we need to make sure there are enough registers; the only valid
220   // registers are the 4 used for parameters.  We don't currently do this
221   // case.
222
223   SupportsTailCall = !isThumb1Only();
224
225   if (isTargetMachO() && isTargetIOS() && getTargetTriple().isOSVersionLT(5, 0))
226     SupportsTailCall = false;
227
228   switch (IT) {
229   case DefaultIT:
230     RestrictIT = hasV8Ops();
231     break;
232   case RestrictedIT:
233     RestrictIT = true;
234     break;
235   case NoRestrictedIT:
236     RestrictIT = false;
237     break;
238   }
239
240   // NEON f32 ops are non-IEEE 754 compliant. Darwin is ok with it by default.
241   const FeatureBitset &Bits = getFeatureBits();
242   if ((Bits[ARM::ProcA5] || Bits[ARM::ProcA8]) && // Where this matters
243       (Options.UnsafeFPMath || isTargetDarwin()))
244     UseNEONForSinglePrecisionFP = true;
245 }
246
247 bool ARMSubtarget::isAPCS_ABI() const {
248   assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
249   return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_APCS;
250 }
251 bool ARMSubtarget::isAAPCS_ABI() const {
252   assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
253   return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS ||
254          TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16;
255 }
256 bool ARMSubtarget::isAAPCS16_ABI() const {
257   assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
258   return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16;
259 }
260
261
262 /// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol.
263 bool
264 ARMSubtarget::GVIsIndirectSymbol(const GlobalValue *GV,
265                                  Reloc::Model RelocM) const {
266   if (RelocM == Reloc::Static)
267     return false;
268
269   bool isDef = GV->isStrongDefinitionForLinker();
270
271   if (!isTargetMachO()) {
272     // Extra load is needed for all externally visible.
273     if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
274       return false;
275     return true;
276   } else {
277     // If this is a strong reference to a definition, it is definitely not
278     // through a stub.
279     if (isDef)
280       return false;
281
282     // Unless we have a symbol with hidden visibility, we have to go through a
283     // normal $non_lazy_ptr stub because this symbol might be resolved late.
284     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
285       return true;
286
287     if (RelocM == Reloc::PIC_) {
288       // If symbol visibility is hidden, we have a stub for common symbol
289       // references and external declarations.
290       if (GV->isDeclarationForLinker() || GV->hasCommonLinkage())
291         // Hidden $non_lazy_ptr reference.
292         return true;
293     }
294   }
295
296   return false;
297 }
298
299 unsigned ARMSubtarget::getMispredictionPenalty() const {
300   return SchedModel.MispredictPenalty;
301 }
302
303 bool ARMSubtarget::hasSinCos() const {
304   return isTargetWatchOS() ||
305     (isTargetIOS() && !getTargetTriple().isOSVersionLT(7, 0));
306 }
307
308 bool ARMSubtarget::enableMachineScheduler() const {
309   // Enable the MachineScheduler before register allocation for out-of-order
310   // architectures where we do not use the PostRA scheduler anymore (for now
311   // restricted to swift).
312   return getSchedModel().isOutOfOrder() && isSwift();
313 }
314
315 // This overrides the PostRAScheduler bit in the SchedModel for any CPU.
316 bool ARMSubtarget::enablePostRAScheduler() const {
317   // No need for PostRA scheduling on out of order CPUs (for now restricted to
318   // swift).
319   if (getSchedModel().isOutOfOrder() && isSwift())
320     return false;
321   return (!isThumb() || hasThumb2());
322 }
323
324 bool ARMSubtarget::enableAtomicExpand() const {
325   return hasAnyDataBarrier() && !isThumb1Only();
326 }
327
328 bool ARMSubtarget::useStride4VFPs(const MachineFunction &MF) const {
329   // For general targets, the prologue can grow when VFPs are allocated with
330   // stride 4 (more vpush instructions). But WatchOS uses a compact unwind
331   // format which it's more important to get right.
332   return isTargetWatchOS() || (isSwift() && !MF.getFunction()->optForMinSize());
333 }
334
335 bool ARMSubtarget::useMovt(const MachineFunction &MF) const {
336   // NOTE Windows on ARM needs to use mov.w/mov.t pairs to materialise 32-bit
337   // immediates as it is inherently position independent, and may be out of
338   // range otherwise.
339   return !NoMovt && hasV6T2Ops() &&
340          (isTargetWindows() || !MF.getFunction()->optForMinSize());
341 }
342
343 bool ARMSubtarget::useFastISel() const {
344   // Enable fast-isel for any target, for testing only.
345   if (ForceFastISel)
346     return true;
347
348   // Limit fast-isel to the targets that are or have been tested.
349   if (!hasV6Ops())
350     return false;
351
352   // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
353   return TM.Options.EnableFastISel &&
354          ((isTargetMachO() && !isThumb1Only()) ||
355           (isTargetLinux() && !isThumb()) || (isTargetNaCl() && !isThumb()));
356 }