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/MC/MCAsmInfo.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Target/TargetInstrInfo.h"
32 #include "llvm/Target/TargetOptions.h"
33 #include "llvm/Target/TargetRegisterInfo.h"
34 #include "llvm/Support/TargetParser.h"
35 
36 using namespace llvm;
37 
38 #define DEBUG_TYPE "arm-subtarget"
39 
40 #define GET_SUBTARGETINFO_TARGET_DESC
41 #define GET_SUBTARGETINFO_CTOR
42 #include "ARMGenSubtargetInfo.inc"
43 
44 static cl::opt<bool>
45 UseFusedMulOps("arm-use-mulops",
46                cl::init(true), cl::Hidden);
47 
48 enum ITMode {
49   DefaultIT,
50   RestrictedIT,
51   NoRestrictedIT
52 };
53 
54 static cl::opt<ITMode>
55 IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT),
56    cl::ZeroOrMore,
57    cl::values(clEnumValN(DefaultIT, "arm-default-it",
58                          "Generate IT block based on arch"),
59               clEnumValN(RestrictedIT, "arm-restrict-it",
60                          "Disallow deprecated IT based on ARMv8"),
61               clEnumValN(NoRestrictedIT, "arm-no-restrict-it",
62                          "Allow IT blocks based on ARMv7")));
63 
64 /// ForceFastISel - Use the fast-isel, even for subtargets where it is not
65 /// currently supported (for testing only).
66 static cl::opt<bool>
67 ForceFastISel("arm-force-fast-isel",
68                cl::init(false), cl::Hidden);
69 
70 /// initializeSubtargetDependencies - Initializes using a CPU and feature string
71 /// so that we can use initializer lists for subtarget initialization.
72 ARMSubtarget &ARMSubtarget::initializeSubtargetDependencies(StringRef CPU,
73                                                             StringRef FS) {
74   initializeEnvironment();
75   initSubtargetFeatures(CPU, FS);
76   return *this;
77 }
78 
79 ARMFrameLowering *ARMSubtarget::initializeFrameLowering(StringRef CPU,
80                                                         StringRef FS) {
81   ARMSubtarget &STI = initializeSubtargetDependencies(CPU, FS);
82   if (STI.isThumb1Only())
83     return (ARMFrameLowering *)new Thumb1FrameLowering(STI);
84 
85   return new ARMFrameLowering(STI);
86 }
87 
88 ARMSubtarget::ARMSubtarget(const Triple &TT, const std::string &CPU,
89                            const std::string &FS,
90                            const ARMBaseTargetMachine &TM, bool IsLittle)
91     : ARMGenSubtargetInfo(TT, CPU, FS), UseMulOps(UseFusedMulOps),
92       CPUString(CPU), IsLittle(IsLittle), TargetTriple(TT), Options(TM.Options),
93       TM(TM), 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 bool ARMSubtarget::isXRaySupported() const {
104   // We don't currently suppport Thumb, but Windows requires Thumb.
105   return hasV6Ops() && hasARMOps() && !isTargetWindows();
106 }
107 
108 void ARMSubtarget::initializeEnvironment() {
109   // MCAsmInfo isn't always present (e.g. in opt) so we can't initialize this
110   // directly from it, but we can try to make sure they're consistent when both
111   // available.
112   UseSjLjEH = isTargetDarwin() && !isTargetWatchABI();
113   assert((!TM.getMCAsmInfo() ||
114           (TM.getMCAsmInfo()->getExceptionHandlingType() ==
115            ExceptionHandling::SjLj) == UseSjLjEH) &&
116          "inconsistent sjlj choice between CodeGen and MC");
117 }
118 
119 void ARMSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
120   if (CPUString.empty()) {
121     CPUString = "generic";
122 
123     if (isTargetDarwin()) {
124       StringRef ArchName = TargetTriple.getArchName();
125       unsigned ArchKind = llvm::ARM::parseArch(ArchName);
126       if (ArchKind == llvm::ARM::AK_ARMV7S)
127         // Default to the Swift CPU when targeting armv7s/thumbv7s.
128         CPUString = "swift";
129       else if (ArchKind == llvm::ARM::AK_ARMV7K)
130         // Default to the Cortex-a7 CPU when targeting armv7k/thumbv7k.
131         // ARMv7k does not use SjLj exception handling.
132         CPUString = "cortex-a7";
133     }
134   }
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, CPUString);
140   if (!FS.empty()) {
141     if (!ArchFS.empty())
142       ArchFS = (Twine(ArchFS) + "," + FS).str();
143     else
144       ArchFS = FS;
145   }
146   ParseSubtargetFeatures(CPUString, ArchFS);
147 
148   // FIXME: This used enable V6T2 support implicitly for Thumb2 mode.
149   // Assert this for now to make the change obvious.
150   assert(hasV6T2Ops() || !hasThumb2());
151 
152   // Keep a pointer to static instruction cost data for the specified CPU.
153   SchedModel = getSchedModelForCPU(CPUString);
154 
155   // Initialize scheduling itinerary for the specified CPU.
156   InstrItins = getInstrItineraryForCPU(CPUString);
157 
158   // FIXME: this is invalid for WindowsCE
159   if (isTargetWindows())
160     NoARM = true;
161 
162   if (isAAPCS_ABI())
163     stackAlignment = 8;
164   if (isTargetNaCl() || isAAPCS16_ABI())
165     stackAlignment = 16;
166 
167   // FIXME: Completely disable sibcall for Thumb1 since ThumbRegisterInfo::
168   // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
169   // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
170   // support in the assembler and linker to be used. This would need to be
171   // fixed to fully support tail calls in Thumb1.
172   //
173   // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
174   // LR.  This means if we need to reload LR, it takes an extra instructions,
175   // which outweighs the value of the tail call; but here we don't know yet
176   // whether LR is going to be used.  Probably the right approach is to
177   // generate the tail call here and turn it back into CALL/RET in
178   // emitEpilogue if LR is used.
179 
180   // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
181   // but we need to make sure there are enough registers; the only valid
182   // registers are the 4 used for parameters.  We don't currently do this
183   // case.
184 
185   SupportsTailCall = !isThumb() || hasV8MBaselineOps();
186 
187   if (isTargetMachO() && isTargetIOS() && getTargetTriple().isOSVersionLT(5, 0))
188     SupportsTailCall = false;
189 
190   switch (IT) {
191   case DefaultIT:
192     RestrictIT = hasV8Ops();
193     break;
194   case RestrictedIT:
195     RestrictIT = true;
196     break;
197   case NoRestrictedIT:
198     RestrictIT = false;
199     break;
200   }
201 
202   // NEON f32 ops are non-IEEE 754 compliant. Darwin is ok with it by default.
203   const FeatureBitset &Bits = getFeatureBits();
204   if ((Bits[ARM::ProcA5] || Bits[ARM::ProcA8]) && // Where this matters
205       (Options.UnsafeFPMath || isTargetDarwin()))
206     UseNEONForSinglePrecisionFP = true;
207 
208   if (isRWPI())
209     ReserveR9 = true;
210 
211   // FIXME: Teach TableGen to deal with these instead of doing it manually here.
212   switch (ARMProcFamily) {
213   case Others:
214   case CortexA5:
215     break;
216   case CortexA7:
217     LdStMultipleTiming = DoubleIssue;
218     break;
219   case CortexA8:
220     LdStMultipleTiming = DoubleIssue;
221     break;
222   case CortexA9:
223     LdStMultipleTiming = DoubleIssueCheckUnalignedAccess;
224     PreISelOperandLatencyAdjustment = 1;
225     break;
226   case CortexA12:
227     break;
228   case CortexA15:
229     MaxInterleaveFactor = 2;
230     PreISelOperandLatencyAdjustment = 1;
231     PartialUpdateClearance = 12;
232     break;
233   case CortexA17:
234   case CortexA32:
235   case CortexA35:
236   case CortexA53:
237   case CortexA57:
238   case CortexA72:
239   case CortexA73:
240   case CortexR4:
241   case CortexR4F:
242   case CortexR5:
243   case CortexR7:
244   case CortexM3:
245   case ExynosM1:
246   case CortexR52:
247     break;
248   case Krait:
249     PreISelOperandLatencyAdjustment = 1;
250     break;
251   case Swift:
252     MaxInterleaveFactor = 2;
253     LdStMultipleTiming = SingleIssuePlusExtras;
254     PreISelOperandLatencyAdjustment = 1;
255     PartialUpdateClearance = 12;
256     break;
257   }
258 }
259 
260 bool ARMSubtarget::isAPCS_ABI() const {
261   assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
262   return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_APCS;
263 }
264 bool ARMSubtarget::isAAPCS_ABI() const {
265   assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
266   return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS ||
267          TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16;
268 }
269 bool ARMSubtarget::isAAPCS16_ABI() const {
270   assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
271   return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16;
272 }
273 
274 bool ARMSubtarget::isROPI() const {
275   return TM.getRelocationModel() == Reloc::ROPI ||
276          TM.getRelocationModel() == Reloc::ROPI_RWPI;
277 }
278 bool ARMSubtarget::isRWPI() const {
279   return TM.getRelocationModel() == Reloc::RWPI ||
280          TM.getRelocationModel() == Reloc::ROPI_RWPI;
281 }
282 
283 bool ARMSubtarget::isGVIndirectSymbol(const GlobalValue *GV) const {
284   if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
285     return true;
286 
287   // 32 bit macho has no relocation for a-b if a is undefined, even if b is in
288   // the section that is being relocated. This means we have to use o load even
289   // for GVs that are known to be local to the dso.
290   if (isTargetMachO() && TM.isPositionIndependent() &&
291       (GV->isDeclarationForLinker() || GV->hasCommonLinkage()))
292     return true;
293 
294   return false;
295 }
296 
297 unsigned ARMSubtarget::getMispredictionPenalty() const {
298   return SchedModel.MispredictPenalty;
299 }
300 
301 bool ARMSubtarget::hasSinCos() const {
302   return isTargetWatchOS() ||
303     (isTargetIOS() && !getTargetTriple().isOSVersionLT(7, 0));
304 }
305 
306 bool ARMSubtarget::enableMachineScheduler() const {
307   // Enable the MachineScheduler before register allocation for out-of-order
308   // architectures where we do not use the PostRA scheduler anymore (for now
309   // restricted to swift).
310   return getSchedModel().isOutOfOrder() && isSwift();
311 }
312 
313 // This overrides the PostRAScheduler bit in the SchedModel for any CPU.
314 bool ARMSubtarget::enablePostRAScheduler() const {
315   // No need for PostRA scheduling on out of order CPUs (for now restricted to
316   // swift).
317   if (getSchedModel().isOutOfOrder() && isSwift())
318     return false;
319   return (!isThumb() || hasThumb2());
320 }
321 
322 bool ARMSubtarget::enableAtomicExpand() const {
323   return hasAnyDataBarrier() && (!isThumb() || hasV8MBaselineOps());
324 }
325 
326 bool ARMSubtarget::useStride4VFPs(const MachineFunction &MF) const {
327   // For general targets, the prologue can grow when VFPs are allocated with
328   // stride 4 (more vpush instructions). But WatchOS uses a compact unwind
329   // format which it's more important to get right.
330   return isTargetWatchABI() || (isSwift() && !MF.getFunction()->optForMinSize());
331 }
332 
333 bool ARMSubtarget::useMovt(const MachineFunction &MF) const {
334   // NOTE Windows on ARM needs to use mov.w/mov.t pairs to materialise 32-bit
335   // immediates as it is inherently position independent, and may be out of
336   // range otherwise.
337   return !NoMovt && hasV8MBaselineOps() &&
338          (isTargetWindows() || !MF.getFunction()->optForMinSize());
339 }
340 
341 bool ARMSubtarget::useFastISel() const {
342   // Enable fast-isel for any target, for testing only.
343   if (ForceFastISel)
344     return true;
345 
346   // Limit fast-isel to the targets that are or have been tested.
347   if (!hasV6Ops())
348     return false;
349 
350   // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
351   return TM.Options.EnableFastISel &&
352          ((isTargetMachO() && !isThumb1Only()) ||
353           (isTargetLinux() && !isThumb()) || (isTargetNaCl() && !isThumb()));
354 }
355