LLVM  5.0.0
ARMSubtarget.cpp
Go to the documentation of this file.
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 "ARM.h"
15 
16 #ifdef LLVM_BUILD_GLOBAL_ISEL
17 #include "ARMCallLowering.h"
18 #include "ARMLegalizerInfo.h"
19 #include "ARMRegisterBankInfo.h"
20 #endif
21 #include "ARMSubtarget.h"
22 #include "ARMFrameLowering.h"
23 #include "ARMInstrInfo.h"
24 #include "ARMSubtarget.h"
25 #include "ARMTargetMachine.h"
27 #include "Thumb1FrameLowering.h"
28 #include "Thumb1InstrInfo.h"
29 #include "Thumb2InstrInfo.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/ADT/Triple.h"
32 #include "llvm/ADT/Twine.h"
33 #ifdef LLVM_BUILD_GLOBAL_ISEL
39 #endif
41 #include "llvm/IR/Function.h"
42 #include "llvm/IR/GlobalValue.h"
43 #include "llvm/MC/MCAsmInfo.h"
45 #include "llvm/Support/CodeGen.h"
49 #include <cassert>
50 #include <string>
51 
52 using namespace llvm;
53 
54 #define DEBUG_TYPE "arm-subtarget"
55 
56 #define GET_SUBTARGETINFO_TARGET_DESC
57 #define GET_SUBTARGETINFO_CTOR
58 #include "ARMGenSubtargetInfo.inc"
59 
60 static cl::opt<bool>
61 UseFusedMulOps("arm-use-mulops",
62  cl::init(true), cl::Hidden);
63 
64 enum ITMode {
68 };
69 
70 static cl::opt<ITMode>
71 IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT),
73  cl::values(clEnumValN(DefaultIT, "arm-default-it",
74  "Generate IT block based on arch"),
75  clEnumValN(RestrictedIT, "arm-restrict-it",
76  "Disallow deprecated IT based on ARMv8"),
77  clEnumValN(NoRestrictedIT, "arm-no-restrict-it",
78  "Allow IT blocks based on ARMv7")));
79 
80 /// ForceFastISel - Use the fast-isel, even for subtargets where it is not
81 /// currently supported (for testing only).
82 static cl::opt<bool>
83 ForceFastISel("arm-force-fast-isel",
84  cl::init(false), cl::Hidden);
85 
86 /// initializeSubtargetDependencies - Initializes using a CPU and feature string
87 /// so that we can use initializer lists for subtarget initialization.
89  StringRef FS) {
90  initializeEnvironment();
91  initSubtargetFeatures(CPU, FS);
92  return *this;
93 }
94 
95 ARMFrameLowering *ARMSubtarget::initializeFrameLowering(StringRef CPU,
96  StringRef FS) {
98  if (STI.isThumb1Only())
99  return (ARMFrameLowering *)new Thumb1FrameLowering(STI);
100 
101  return new ARMFrameLowering(STI);
102 }
103 
104 #ifdef LLVM_BUILD_GLOBAL_ISEL
105 namespace {
106 
107 struct ARMGISelActualAccessor : public GISelAccessor {
108  std::unique_ptr<CallLowering> CallLoweringInfo;
109  std::unique_ptr<InstructionSelector> InstSelector;
110  std::unique_ptr<LegalizerInfo> Legalizer;
111  std::unique_ptr<RegisterBankInfo> RegBankInfo;
112 
113  const CallLowering *getCallLowering() const override {
114  return CallLoweringInfo.get();
115  }
116 
117  const InstructionSelector *getInstructionSelector() const override {
118  return InstSelector.get();
119  }
120 
121  const LegalizerInfo *getLegalizerInfo() const override {
122  return Legalizer.get();
123  }
124 
125  const RegisterBankInfo *getRegBankInfo() const override {
126  return RegBankInfo.get();
127  }
128 };
129 
130 } // end anonymous namespace
131 #endif
132 
133 ARMSubtarget::ARMSubtarget(const Triple &TT, const std::string &CPU,
134  const std::string &FS,
135  const ARMBaseTargetMachine &TM, bool IsLittle)
136  : ARMGenSubtargetInfo(TT, CPU, FS), UseMulOps(UseFusedMulOps),
137  CPUString(CPU), IsLittle(IsLittle), TargetTriple(TT), Options(TM.Options),
138  TM(TM), FrameLowering(initializeFrameLowering(CPU, FS)),
139  // At this point initializeSubtargetDependencies has been called so
140  // we can query directly.
141  InstrInfo(isThumb1Only()
142  ? (ARMBaseInstrInfo *)new Thumb1InstrInfo(*this)
143  : !isThumb()
144  ? (ARMBaseInstrInfo *)new ARMInstrInfo(*this)
145  : (ARMBaseInstrInfo *)new Thumb2InstrInfo(*this)),
146  TLInfo(TM, *this) {
147  assert((isThumb() || hasARMOps()) &&
148  "Target must either be thumb or support ARM operations!");
149 
150 #ifndef LLVM_BUILD_GLOBAL_ISEL
151  GISelAccessor *GISel = new GISelAccessor();
152 #else
153  ARMGISelActualAccessor *GISel = new ARMGISelActualAccessor();
154  GISel->CallLoweringInfo.reset(new ARMCallLowering(*getTargetLowering()));
155  GISel->Legalizer.reset(new ARMLegalizerInfo(*this));
156 
157  auto *RBI = new ARMRegisterBankInfo(*getRegisterInfo());
158 
159  // FIXME: At this point, we can't rely on Subtarget having RBI.
160  // It's awkward to mix passing RBI and the Subtarget; should we pass
161  // TII/TRI as well?
162  GISel->InstSelector.reset(createARMInstructionSelector(
163  *static_cast<const ARMBaseTargetMachine *>(&TM), *this, *RBI));
164 
165  GISel->RegBankInfo.reset(RBI);
166 #endif
167  setGISelAccessor(*GISel);
168 }
169 
171  assert(GISel && "Access to GlobalISel APIs not set");
172  return GISel->getCallLowering();
173 }
174 
176  assert(GISel && "Access to GlobalISel APIs not set");
177  return GISel->getInstructionSelector();
178 }
179 
181  assert(GISel && "Access to GlobalISel APIs not set");
182  return GISel->getLegalizerInfo();
183 }
184 
186  assert(GISel && "Access to GlobalISel APIs not set");
187  return GISel->getRegBankInfo();
188 }
189 
191  // We don't currently suppport Thumb, but Windows requires Thumb.
192  return hasV6Ops() && hasARMOps() && !isTargetWindows();
193 }
194 
195 void ARMSubtarget::initializeEnvironment() {
196  // MCAsmInfo isn't always present (e.g. in opt) so we can't initialize this
197  // directly from it, but we can try to make sure they're consistent when both
198  // available.
200  assert((!TM.getMCAsmInfo() ||
203  "inconsistent sjlj choice between CodeGen and MC");
204 }
205 
206 void ARMSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
207  if (CPUString.empty()) {
208  CPUString = "generic";
209 
210  if (isTargetDarwin()) {
211  StringRef ArchName = TargetTriple.getArchName();
212  unsigned ArchKind = ARM::parseArch(ArchName);
213  if (ArchKind == ARM::AK_ARMV7S)
214  // Default to the Swift CPU when targeting armv7s/thumbv7s.
215  CPUString = "swift";
216  else if (ArchKind == ARM::AK_ARMV7K)
217  // Default to the Cortex-a7 CPU when targeting armv7k/thumbv7k.
218  // ARMv7k does not use SjLj exception handling.
219  CPUString = "cortex-a7";
220  }
221  }
222 
223  // Insert the architecture feature derived from the target triple into the
224  // feature string. This is important for setting features that are implied
225  // based on the architecture version.
226  std::string ArchFS = ARM_MC::ParseARMTriple(TargetTriple, CPUString);
227  if (!FS.empty()) {
228  if (!ArchFS.empty())
229  ArchFS = (Twine(ArchFS) + "," + FS).str();
230  else
231  ArchFS = FS;
232  }
234 
235  // FIXME: This used enable V6T2 support implicitly for Thumb2 mode.
236  // Assert this for now to make the change obvious.
237  assert(hasV6T2Ops() || !hasThumb2());
238 
239  // Execute only support requires movt support
240  if (genExecuteOnly())
241  assert(hasV8MBaselineOps() && !NoMovt && "Cannot generate execute-only code for this target");
242 
243  // Keep a pointer to static instruction cost data for the specified CPU.
244  SchedModel = getSchedModelForCPU(CPUString);
245 
246  // Initialize scheduling itinerary for the specified CPU.
247  InstrItins = getInstrItineraryForCPU(CPUString);
248 
249  // FIXME: this is invalid for WindowsCE
250  if (isTargetWindows())
251  NoARM = true;
252 
253  if (isAAPCS_ABI())
254  stackAlignment = 8;
255  if (isTargetNaCl() || isAAPCS16_ABI())
256  stackAlignment = 16;
257 
258  // FIXME: Completely disable sibcall for Thumb1 since ThumbRegisterInfo::
259  // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
260  // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
261  // support in the assembler and linker to be used. This would need to be
262  // fixed to fully support tail calls in Thumb1.
263  //
264  // For ARMv8-M, we /do/ implement tail calls. Doing this is tricky for v8-M
265  // baseline, since the LDM/POP instruction on Thumb doesn't take LR. This
266  // means if we need to reload LR, it takes extra instructions, which outweighs
267  // the value of the tail call; but here we don't know yet whether LR is going
268  // to be used. We generate the tail call here and turn it back into CALL/RET
269  // in emitEpilogue if LR is used.
270 
271  // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
272  // but we need to make sure there are enough registers; the only valid
273  // registers are the 4 used for parameters. We don't currently do this
274  // case.
275 
277 
278  if (isTargetMachO() && isTargetIOS() && getTargetTriple().isOSVersionLT(5, 0))
279  SupportsTailCall = false;
280 
281  switch (IT) {
282  case DefaultIT:
283  RestrictIT = hasV8Ops();
284  break;
285  case RestrictedIT:
286  RestrictIT = true;
287  break;
288  case NoRestrictedIT:
289  RestrictIT = false;
290  break;
291  }
292 
293  // NEON f32 ops are non-IEEE 754 compliant. Darwin is ok with it by default.
294  const FeatureBitset &Bits = getFeatureBits();
295  if ((Bits[ARM::ProcA5] || Bits[ARM::ProcA8]) && // Where this matters
298 
299  if (isRWPI())
300  ReserveR9 = true;
301 
302  // FIXME: Teach TableGen to deal with these instead of doing it manually here.
303  switch (ARMProcFamily) {
304  case Others:
305  case CortexA5:
306  break;
307  case CortexA7:
309  break;
310  case CortexA8:
312  break;
313  case CortexA9:
316  break;
317  case CortexA12:
318  break;
319  case CortexA15:
323  break;
324  case CortexA17:
325  case CortexA32:
326  case CortexA35:
327  case CortexA53:
328  case CortexA57:
329  case CortexA72:
330  case CortexA73:
331  case CortexR4:
332  case CortexR4F:
333  case CortexR5:
334  case CortexR7:
335  case CortexM3:
336  case ExynosM1:
337  case CortexR52:
338  case Kryo:
339  break;
340  case Krait:
342  break;
343  case Swift:
348  break;
349  }
350 }
351 
355 }
360 }
364 }
365 
366 bool ARMSubtarget::isROPI() const {
367  return TM.getRelocationModel() == Reloc::ROPI ||
369 }
370 bool ARMSubtarget::isRWPI() const {
371  return TM.getRelocationModel() == Reloc::RWPI ||
373 }
374 
376  if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
377  return true;
378 
379  // 32 bit macho has no relocation for a-b if a is undefined, even if b is in
380  // the section that is being relocated. This means we have to use o load even
381  // for GVs that are known to be local to the dso.
383  (GV->isDeclarationForLinker() || GV->hasCommonLinkage()))
384  return true;
385 
386  return false;
387 }
388 
391 }
392 
394  return isTargetWatchOS() ||
395  (isTargetIOS() && !getTargetTriple().isOSVersionLT(7, 0));
396 }
397 
399  // Enable the MachineScheduler before register allocation for out-of-order
400  // architectures where we do not use the PostRA scheduler anymore (for now
401  // restricted to swift).
402  return getSchedModel().isOutOfOrder() && isSwift();
403 }
404 
405 // This overrides the PostRAScheduler bit in the SchedModel for any CPU.
407  // No need for PostRA scheduling on out of order CPUs (for now restricted to
408  // swift).
409  if (getSchedModel().isOutOfOrder() && isSwift())
410  return false;
411  return (!isThumb() || hasThumb2());
412 }
413 
415 
417  // For general targets, the prologue can grow when VFPs are allocated with
418  // stride 4 (more vpush instructions). But WatchOS uses a compact unwind
419  // format which it's more important to get right.
420  return isTargetWatchABI() || (isSwift() && !MF.getFunction()->optForMinSize());
421 }
422 
423 bool ARMSubtarget::useMovt(const MachineFunction &MF) const {
424  // NOTE Windows on ARM needs to use mov.w/mov.t pairs to materialise 32-bit
425  // immediates as it is inherently position independent, and may be out of
426  // range otherwise.
427  return !NoMovt && hasV8MBaselineOps() &&
429 }
430 
432  // Enable fast-isel for any target, for testing only.
433  if (ForceFastISel)
434  return true;
435 
436  // Limit fast-isel to the targets that are or have been tested.
437  if (!hasV6Ops())
438  return false;
439 
440  // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
441  return TM.Options.EnableFastISel &&
442  ((isTargetMachO() && !isThumb1Only()) ||
443  (isTargetLinux() && !isThumb()) || (isTargetNaCl() && !isThumb()));
444 }
bool NoMovt
NoMovt - True if MOVT / MOVW pairs are not used for materialization of 32-bit imms (including global ...
Definition: ARMSubtarget.h:194
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::ZeroOrMore, cl::values(clEnumValN(DefaultIT,"arm-default-it","Generate IT block based on arch"), clEnumValN(RestrictedIT,"arm-restrict-it","Disallow deprecated IT based on ARMv8"), clEnumValN(NoRestrictedIT,"arm-no-restrict-it","Allow IT blocks based on ARMv7")))
unsigned MispredictPenalty
Definition: MCSchedule.h:180
The goal of this helper class is to gather the accessor to all the APIs related to GlobalISel...
Definition: GISelAccessor.h:29
unsigned stackAlignment
stackAlignment - The minimum alignment known to hold of the stack frame on entry to the function and ...
Definition: ARMSubtarget.h:374
Triple TargetTriple
TargetTriple - What processor and OS we're targeting.
Definition: ARMSubtarget.h:396
bool enableMachineScheduler() const override
Returns true if machine scheduler should be enabled.
unsigned getMispredictionPenalty() const
Reloc::Model getRelocationModel() const
Returns the code generation relocation model.
bool isAAPCS16_ABI() const
unsigned EnableFastISel
EnableFastISel - This flag enables fast-path instruction selection which trades away generated code q...
DWARF-like instruction based exceptions.
const ARMTargetLowering * getTargetLowering() const override
Definition: ARMSubtarget.h:441
bool useFastISel() const
True if fast-isel is used.
This class provides the information for the target register banks.
bool isROPI() const
bool hasV6Ops() const
Definition: ARMSubtarget.h:481
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
enum llvm::ARMBaseTargetMachine::ARMABI TargetABI
bool hasV6T2Ops() const
Definition: ARMSubtarget.h:484
const LegalizerInfo * getLegalizerInfo() const override
bool isThumb1Only() const
Definition: ARMSubtarget.h:650
const ARMBaseTargetMachine & TM
Definition: ARMSubtarget.h:407
InstructionSelector * createARMInstructionSelector(const ARMBaseTargetMachine &TM, const ARMSubtarget &STI, const ARMRegisterBankInfo &RBI)
bool SupportsTailCall
SupportsTailCall - True if the OS supports tail call.
Definition: ARMSubtarget.h:199
static bool isThumb(const MCSubtargetInfo &STI)
bool optForMinSize() const
Optimize this function for minimum size (-Oz).
Definition: Function.h:519
Holds all the information related to register banks.
bool hasV8Ops() const
Definition: ARMSubtarget.h:486
bool hasCommonLinkage() const
Definition: GlobalValue.h:420
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
bool hasThumb2() const
Definition: ARMSubtarget.h:652
bool isTargetDarwin() const
Definition: ARMSubtarget.h:580
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
bool useStride4VFPs(const MachineFunction &MF) const
void setGISelAccessor(GISelAccessor &GISel)
This object will take onwership of GISelAccessor.
Definition: ARMSubtarget.h:417
bool isThumb() const
Definition: ARMSubtarget.h:649
const Triple & getTargetTriple() const
Definition: ARMSubtarget.h:578
bool enableAtomicExpand() const override
Can load/store 2 registers/cycle, but needs an extra cycle if the access is not 64-bit aligned...
Definition: ARMSubtarget.h:114
bool isXRaySupported() const override
bool isTargetMachO() const
Definition: ARMSubtarget.h:591
bool NoARM
NoARM - True if subtarget does not support ARM mode execution.
Definition: ARMSubtarget.h:187
This class provides the information for the target register banks.
int PreISelOperandLatencyAdjustment
The adjustment that we need to apply to get the operand latency from the operand cycle returned by th...
Definition: ARMSubtarget.h:390
bool hasAnyDataBarrier() const
Definition: ARMSubtarget.h:529
bool isTargetWatchOS() const
Definition: ARMSubtarget.h:582
bool hasARMOps() const
Definition: ARMSubtarget.h:507
bool isTargetIOS() const
Definition: ARMSubtarget.h:581
ValuesClass values(OptsTy...Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:621
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:401
unsigned UnsafeFPMath
UnsafeFPMath - This flag is enabled when the -enable-unsafe-fp-math flag is specified on the command ...
Container class for subtarget features.
ARMProcFamilyEnum ARMProcFamily
ARMProcFamily - ARM processor family: Cortex-A8, Cortex-A9, and others.
Definition: ARMSubtarget.h:124
bool RestrictIT
RestrictIT - If true, the subtarget disallows generation of deprecated IT blocks to conform to ARMv8 ...
Definition: ARMSubtarget.h:347
This file declares the targeting of the RegisterBankInfo class for ARM.
static cl::opt< bool > UseFusedMulOps("arm-use-mulops", cl::init(true), cl::Hidden)
This file declares the targeting of the Machinelegalizer class for ARM.
bool isPositionIndependent() const
ExceptionHandling getExceptionHandlingType() const
Definition: MCAsmInfo.h:554
bool isTargetWatchABI() const
Definition: ARMSubtarget.h:583
bool hasV8MBaselineOps() const
Definition: ARMSubtarget.h:489
const CallLowering * getCallLowering() const override
bool hasSinCos() const
This function returns true if the target has sincos() routine in its compiler runtime or math librari...
bool shouldAssumeDSOLocal(const Module &M, const GlobalValue *GV) const
bool genExecuteOnly() const
Definition: ARMSubtarget.h:568
bool isRWPI() const
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
ARMSubtarget(const Triple &TT, const std::string &CPU, const std::string &FS, const ARMBaseTargetMachine &TM, bool IsLittle)
This constructor initializes the data members to match that of the specified triple.
bool isTargetNaCl() const
Definition: ARMSubtarget.h:585
bool isSwift() const
Definition: ARMSubtarget.h:500
bool isGVIndirectSymbol(const GlobalValue *GV) const
True if the GV will be accessed via an indirect symbol.
void ParseSubtargetFeatures(StringRef CPU, StringRef FS)
ParseSubtargetFeatures - Parses features string setting specified subtarget options.
const InstructionSelector * getInstructionSelector() const override
MCSchedModel SchedModel
SchedModel - Processor specific instruction costs.
Definition: ARMSubtarget.h:399
const TargetOptions & Options
Options passed via command line that could influence the target.
Definition: ARMSubtarget.h:405
ARMSubtarget & initializeSubtargetDependencies(StringRef CPU, StringRef FS)
initializeSubtargetDependencies - Initializes using a CPU and feature string so that we can use initi...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
bool isOSVersionLT(unsigned Major, unsigned Minor=0, unsigned Micro=0) const
isOSVersionLT - Helper function for doing comparisons against version numbers included in the target ...
Definition: Triple.h:399
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:596
Provides the logic to select generic machine instructions.
bool isAPCS_ABI() const
bool isAAPCS_ABI() const
bool isTargetLinux() const
Definition: ARMSubtarget.h:584
bool ReserveR9
ReserveR9 - True if R9 is not available as a general purpose register.
Definition: ARMSubtarget.h:190
StringRef getArchName() const
getArchName - Get the architecture (first) component of the triple.
Definition: Triple.cpp:921
TargetOptions Options
Definition: TargetMachine.h:95
InstrItineraryData InstrItins
Selected instruction itineraries (one entry per itinerary class.)
Definition: ARMSubtarget.h:402
bool UseNEONForSinglePrecisionFP
UseNEONForSinglePrecisionFP - if the NEONFP attribute has been specified.
Definition: ARMSubtarget.h:160
const ARMBaseRegisterInfo * getRegisterInfo() const override
Definition: ARMSubtarget.h:449
unsigned PartialUpdateClearance
Clearance before partial register updates (in number of instructions)
Definition: ARMSubtarget.h:382
unsigned parseArch(StringRef Arch)
bool useMovt(const MachineFunction &MF) const
This file declares the IRTranslator pass.
ARMLdStMultipleTiming LdStMultipleTiming
What kind of timing do load multiple/store multiple have (double issue, single issue etc)...
Definition: ARMSubtarget.h:386
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:545
bool enablePostRAScheduler() const override
True for some subtargets at > -O0.
Can load/store 1 register/cycle, but needs an extra cycle for address computation and potentially als...
Definition: ARMSubtarget.h:119
std::string ParseARMTriple(const Triple &TT, StringRef CPU)
Can load/store 2 registers/cycle.
Definition: ARMSubtarget.h:111
std::string CPUString
CPUString - String name of used CPU.
Definition: ARMSubtarget.h:377
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
This file describes how to lower LLVM calls to machine code calls.
static cl::opt< bool > ForceFastISel("arm-force-fast-isel", cl::init(false), cl::Hidden)
ForceFastISel - Use the fast-isel, even for subtargets where it is not currently supported (for testi...
ITMode
bool isDeclarationForLinker() const
Definition: GlobalValue.h:503
bool isTargetWindows() const
Definition: ARMSubtarget.h:587
const RegisterBankInfo * getRegBankInfo() const override
bool UseSjLjEH
UseSjLjEH - If true, the target uses SjLj exception handling (e.g. iOS).
Definition: ARMSubtarget.h:366
unsigned MaxInterleaveFactor
Definition: ARMSubtarget.h:379