ARMSubtarget.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. //===-- ARMSubtarget.cpp - ARM Subtarget Information ----------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the ARM specific subclass of TargetSubtargetInfo.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ARM.h"
  13. #include "ARMCallLowering.h"
  14. #include "ARMLegalizerInfo.h"
  15. #include "ARMRegisterBankInfo.h"
  16. #include "ARMFrameLowering.h"
  17. #include "ARMInstrInfo.h"
  18. #include "ARMSubtarget.h"
  19. #include "ARMTargetMachine.h"
  20. #include "MCTargetDesc/ARMMCTargetDesc.h"
  21. #include "Thumb1FrameLowering.h"
  22. #include "Thumb1InstrInfo.h"
  23. #include "Thumb2InstrInfo.h"
  24. #include "llvm/ADT/StringRef.h"
  25. #include "llvm/ADT/Triple.h"
  26. #include "llvm/ADT/Twine.h"
  27. #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
  28. #include "llvm/CodeGen/MachineFrameInfo.h"
  29. #include "llvm/CodeGen/MachineFunction.h"
  30. #include "llvm/IR/Function.h"
  31. #include "llvm/IR/GlobalValue.h"
  32. #include "llvm/MC/MCAsmInfo.h"
  33. #include "llvm/MC/MCTargetOptions.h"
  34. #include "llvm/Support/CodeGen.h"
  35. #include "llvm/Support/CommandLine.h"
  36. #include "llvm/Support/ARMTargetParser.h"
  37. #include "llvm/Support/TargetParser.h"
  38. #include "llvm/Target/TargetOptions.h"
  39. using namespace llvm;
  40. #define DEBUG_TYPE "arm-subtarget"
  41. #define GET_SUBTARGETINFO_TARGET_DESC
  42. #define GET_SUBTARGETINFO_CTOR
  43. #include "ARMGenSubtargetInfo.inc"
  44. static cl::opt<bool>
  45. UseFusedMulOps("arm-use-mulops",
  46. cl::init(true), cl::Hidden);
  47. enum ITMode {
  48. DefaultIT,
  49. RestrictedIT
  50. };
  51. static cl::opt<ITMode>
  52. IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT),
  53. cl::values(clEnumValN(DefaultIT, "arm-default-it",
  54. "Generate any type of IT block"),
  55. clEnumValN(RestrictedIT, "arm-restrict-it",
  56. "Disallow complex IT blocks")));
  57. /// ForceFastISel - Use the fast-isel, even for subtargets where it is not
  58. /// currently supported (for testing only).
  59. static cl::opt<bool>
  60. ForceFastISel("arm-force-fast-isel",
  61. cl::init(false), cl::Hidden);
  62. static cl::opt<bool> EnableSubRegLiveness("arm-enable-subreg-liveness",
  63. cl::init(false), cl::Hidden);
  64. /// initializeSubtargetDependencies - Initializes using a CPU and feature string
  65. /// so that we can use initializer lists for subtarget initialization.
  66. ARMSubtarget &ARMSubtarget::initializeSubtargetDependencies(StringRef CPU,
  67. StringRef FS) {
  68. initializeEnvironment();
  69. initSubtargetFeatures(CPU, FS);
  70. return *this;
  71. }
  72. ARMFrameLowering *ARMSubtarget::initializeFrameLowering(StringRef CPU,
  73. StringRef FS) {
  74. ARMSubtarget &STI = initializeSubtargetDependencies(CPU, FS);
  75. if (STI.isThumb1Only())
  76. return (ARMFrameLowering *)new Thumb1FrameLowering(STI);
  77. return new ARMFrameLowering(STI);
  78. }
  79. ARMSubtarget::ARMSubtarget(const Triple &TT, const std::string &CPU,
  80. const std::string &FS,
  81. const ARMBaseTargetMachine &TM, bool IsLittle,
  82. bool MinSize)
  83. : ARMGenSubtargetInfo(TT, CPU, /*TuneCPU*/ CPU, FS),
  84. UseMulOps(UseFusedMulOps), CPUString(CPU), OptMinSize(MinSize),
  85. IsLittle(IsLittle), TargetTriple(TT), Options(TM.Options), TM(TM),
  86. FrameLowering(initializeFrameLowering(CPU, FS)),
  87. // At this point initializeSubtargetDependencies has been called so
  88. // we can query directly.
  89. InstrInfo(isThumb1Only()
  90. ? (ARMBaseInstrInfo *)new Thumb1InstrInfo(*this)
  91. : !isThumb()
  92. ? (ARMBaseInstrInfo *)new ARMInstrInfo(*this)
  93. : (ARMBaseInstrInfo *)new Thumb2InstrInfo(*this)),
  94. TLInfo(TM, *this) {
  95. CallLoweringInfo.reset(new ARMCallLowering(*getTargetLowering()));
  96. Legalizer.reset(new ARMLegalizerInfo(*this));
  97. auto *RBI = new ARMRegisterBankInfo(*getRegisterInfo());
  98. // FIXME: At this point, we can't rely on Subtarget having RBI.
  99. // It's awkward to mix passing RBI and the Subtarget; should we pass
  100. // TII/TRI as well?
  101. InstSelector.reset(createARMInstructionSelector(
  102. *static_cast<const ARMBaseTargetMachine *>(&TM), *this, *RBI));
  103. RegBankInfo.reset(RBI);
  104. }
  105. const CallLowering *ARMSubtarget::getCallLowering() const {
  106. return CallLoweringInfo.get();
  107. }
  108. InstructionSelector *ARMSubtarget::getInstructionSelector() const {
  109. return InstSelector.get();
  110. }
  111. const LegalizerInfo *ARMSubtarget::getLegalizerInfo() const {
  112. return Legalizer.get();
  113. }
  114. const RegisterBankInfo *ARMSubtarget::getRegBankInfo() const {
  115. return RegBankInfo.get();
  116. }
  117. bool ARMSubtarget::isXRaySupported() const {
  118. // We don't currently suppport Thumb, but Windows requires Thumb.
  119. return hasV6Ops() && hasARMOps() && !isTargetWindows();
  120. }
  121. void ARMSubtarget::initializeEnvironment() {
  122. // MCAsmInfo isn't always present (e.g. in opt) so we can't initialize this
  123. // directly from it, but we can try to make sure they're consistent when both
  124. // available.
  125. UseSjLjEH = (isTargetDarwin() && !isTargetWatchABI() &&
  126. Options.ExceptionModel == ExceptionHandling::None) ||
  127. Options.ExceptionModel == ExceptionHandling::SjLj;
  128. assert((!TM.getMCAsmInfo() ||
  129. (TM.getMCAsmInfo()->getExceptionHandlingType() ==
  130. ExceptionHandling::SjLj) == UseSjLjEH) &&
  131. "inconsistent sjlj choice between CodeGen and MC");
  132. }
  133. void ARMSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
  134. if (CPUString.empty()) {
  135. CPUString = "generic";
  136. if (isTargetDarwin()) {
  137. StringRef ArchName = TargetTriple.getArchName();
  138. ARM::ArchKind AK = ARM::parseArch(ArchName);
  139. if (AK == ARM::ArchKind::ARMV7S)
  140. // Default to the Swift CPU when targeting armv7s/thumbv7s.
  141. CPUString = "swift";
  142. else if (AK == ARM::ArchKind::ARMV7K)
  143. // Default to the Cortex-a7 CPU when targeting armv7k/thumbv7k.
  144. // ARMv7k does not use SjLj exception handling.
  145. CPUString = "cortex-a7";
  146. }
  147. }
  148. // Insert the architecture feature derived from the target triple into the
  149. // feature string. This is important for setting features that are implied
  150. // based on the architecture version.
  151. std::string ArchFS = ARM_MC::ParseARMTriple(TargetTriple, CPUString);
  152. if (!FS.empty()) {
  153. if (!ArchFS.empty())
  154. ArchFS = (Twine(ArchFS) + "," + FS).str();
  155. else
  156. ArchFS = std::string(FS);
  157. }
  158. ParseSubtargetFeatures(CPUString, /*TuneCPU*/ CPUString, ArchFS);
  159. // FIXME: This used enable V6T2 support implicitly for Thumb2 mode.
  160. // Assert this for now to make the change obvious.
  161. assert(hasV6T2Ops() || !hasThumb2());
  162. // Execute only support requires movt support
  163. if (genExecuteOnly()) {
  164. NoMovt = false;
  165. assert(hasV8MBaselineOps() && "Cannot generate execute-only code for this target");
  166. }
  167. // Keep a pointer to static instruction cost data for the specified CPU.
  168. SchedModel = getSchedModelForCPU(CPUString);
  169. // Initialize scheduling itinerary for the specified CPU.
  170. InstrItins = getInstrItineraryForCPU(CPUString);
  171. // FIXME: this is invalid for WindowsCE
  172. if (isTargetWindows())
  173. NoARM = true;
  174. if (isAAPCS_ABI())
  175. stackAlignment = Align(8);
  176. if (isTargetNaCl() || isAAPCS16_ABI())
  177. stackAlignment = Align(16);
  178. // FIXME: Completely disable sibcall for Thumb1 since ThumbRegisterInfo::
  179. // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
  180. // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
  181. // support in the assembler and linker to be used. This would need to be
  182. // fixed to fully support tail calls in Thumb1.
  183. //
  184. // For ARMv8-M, we /do/ implement tail calls. Doing this is tricky for v8-M
  185. // baseline, since the LDM/POP instruction on Thumb doesn't take LR. This
  186. // means if we need to reload LR, it takes extra instructions, which outweighs
  187. // the value of the tail call; but here we don't know yet whether LR is going
  188. // to be used. We take the optimistic approach of generating the tail call and
  189. // perhaps taking a hit if we need to restore the LR.
  190. // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
  191. // but we need to make sure there are enough registers; the only valid
  192. // registers are the 4 used for parameters. We don't currently do this
  193. // case.
  194. SupportsTailCall = !isThumb1Only() || hasV8MBaselineOps();
  195. if (isTargetMachO() && isTargetIOS() && getTargetTriple().isOSVersionLT(5, 0))
  196. SupportsTailCall = false;
  197. switch (IT) {
  198. case DefaultIT:
  199. RestrictIT = false;
  200. break;
  201. case RestrictedIT:
  202. RestrictIT = true;
  203. break;
  204. }
  205. // NEON f32 ops are non-IEEE 754 compliant. Darwin is ok with it by default.
  206. const FeatureBitset &Bits = getFeatureBits();
  207. if ((Bits[ARM::ProcA5] || Bits[ARM::ProcA8]) && // Where this matters
  208. (Options.UnsafeFPMath || isTargetDarwin()))
  209. HasNEONForFP = true;
  210. if (isRWPI())
  211. ReserveR9 = true;
  212. // If MVEVectorCostFactor is still 0 (has not been set to anything else), default it to 2
  213. if (MVEVectorCostFactor == 0)
  214. MVEVectorCostFactor = 2;
  215. // FIXME: Teach TableGen to deal with these instead of doing it manually here.
  216. switch (ARMProcFamily) {
  217. case Others:
  218. case CortexA5:
  219. break;
  220. case CortexA7:
  221. LdStMultipleTiming = DoubleIssue;
  222. break;
  223. case CortexA8:
  224. LdStMultipleTiming = DoubleIssue;
  225. break;
  226. case CortexA9:
  227. LdStMultipleTiming = DoubleIssueCheckUnalignedAccess;
  228. PreISelOperandLatencyAdjustment = 1;
  229. break;
  230. case CortexA12:
  231. break;
  232. case CortexA15:
  233. MaxInterleaveFactor = 2;
  234. PreISelOperandLatencyAdjustment = 1;
  235. PartialUpdateClearance = 12;
  236. break;
  237. case CortexA17:
  238. case CortexA32:
  239. case CortexA35:
  240. case CortexA53:
  241. case CortexA55:
  242. case CortexA57:
  243. case CortexA72:
  244. case CortexA73:
  245. case CortexA75:
  246. case CortexA76:
  247. case CortexA77:
  248. case CortexA78:
  249. case CortexA78C:
  250. case CortexA710:
  251. case CortexR4:
  252. case CortexR4F:
  253. case CortexR5:
  254. case CortexR7:
  255. case CortexM3:
  256. case CortexM7:
  257. case CortexR52:
  258. case CortexX1:
  259. case CortexX1C:
  260. break;
  261. case Exynos:
  262. LdStMultipleTiming = SingleIssuePlusExtras;
  263. MaxInterleaveFactor = 4;
  264. if (!isThumb())
  265. PrefLoopLogAlignment = 3;
  266. break;
  267. case Kryo:
  268. break;
  269. case Krait:
  270. PreISelOperandLatencyAdjustment = 1;
  271. break;
  272. case NeoverseN1:
  273. case NeoverseN2:
  274. case NeoverseV1:
  275. break;
  276. case Swift:
  277. MaxInterleaveFactor = 2;
  278. LdStMultipleTiming = SingleIssuePlusExtras;
  279. PreISelOperandLatencyAdjustment = 1;
  280. PartialUpdateClearance = 12;
  281. break;
  282. }
  283. }
  284. bool ARMSubtarget::isTargetHardFloat() const { return TM.isTargetHardFloat(); }
  285. bool ARMSubtarget::isAPCS_ABI() const {
  286. assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
  287. return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_APCS;
  288. }
  289. bool ARMSubtarget::isAAPCS_ABI() const {
  290. assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
  291. return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS ||
  292. TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16;
  293. }
  294. bool ARMSubtarget::isAAPCS16_ABI() const {
  295. assert(TM.TargetABI != ARMBaseTargetMachine::ARM_ABI_UNKNOWN);
  296. return TM.TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16;
  297. }
  298. bool ARMSubtarget::isROPI() const {
  299. return TM.getRelocationModel() == Reloc::ROPI ||
  300. TM.getRelocationModel() == Reloc::ROPI_RWPI;
  301. }
  302. bool ARMSubtarget::isRWPI() const {
  303. return TM.getRelocationModel() == Reloc::RWPI ||
  304. TM.getRelocationModel() == Reloc::ROPI_RWPI;
  305. }
  306. bool ARMSubtarget::isGVIndirectSymbol(const GlobalValue *GV) const {
  307. if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
  308. return true;
  309. // 32 bit macho has no relocation for a-b if a is undefined, even if b is in
  310. // the section that is being relocated. This means we have to use o load even
  311. // for GVs that are known to be local to the dso.
  312. if (isTargetMachO() && TM.isPositionIndependent() &&
  313. (GV->isDeclarationForLinker() || GV->hasCommonLinkage()))
  314. return true;
  315. return false;
  316. }
  317. bool ARMSubtarget::isGVInGOT(const GlobalValue *GV) const {
  318. return isTargetELF() && TM.isPositionIndependent() &&
  319. !TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
  320. }
  321. unsigned ARMSubtarget::getMispredictionPenalty() const {
  322. return SchedModel.MispredictPenalty;
  323. }
  324. bool ARMSubtarget::enableMachineScheduler() const {
  325. // The MachineScheduler can increase register usage, so we use more high
  326. // registers and end up with more T2 instructions that cannot be converted to
  327. // T1 instructions. At least until we do better at converting to thumb1
  328. // instructions, on cortex-m at Oz where we are size-paranoid, don't use the
  329. // Machine scheduler, relying on the DAG register pressure scheduler instead.
  330. if (isMClass() && hasMinSize())
  331. return false;
  332. // Enable the MachineScheduler before register allocation for subtargets
  333. // with the use-misched feature.
  334. return useMachineScheduler();
  335. }
  336. bool ARMSubtarget::enableSubRegLiveness() const {
  337. if (EnableSubRegLiveness.getNumOccurrences())
  338. return EnableSubRegLiveness;
  339. // Enable SubRegLiveness for MVE to better optimize s subregs for mqpr regs
  340. // and q subregs for qqqqpr regs.
  341. return hasMVEIntegerOps();
  342. }
  343. bool ARMSubtarget::enableMachinePipeliner() const {
  344. // Enable the MachinePipeliner before register allocation for subtargets
  345. // with the use-mipipeliner feature.
  346. return getSchedModel().hasInstrSchedModel() && useMachinePipeliner();
  347. }
  348. bool ARMSubtarget::useDFAforSMS() const { return false; }
  349. // This overrides the PostRAScheduler bit in the SchedModel for any CPU.
  350. bool ARMSubtarget::enablePostRAScheduler() const {
  351. if (enableMachineScheduler())
  352. return false;
  353. if (disablePostRAScheduler())
  354. return false;
  355. // Thumb1 cores will generally not benefit from post-ra scheduling
  356. return !isThumb1Only();
  357. }
  358. bool ARMSubtarget::enablePostRAMachineScheduler() const {
  359. if (!enableMachineScheduler())
  360. return false;
  361. if (disablePostRAScheduler())
  362. return false;
  363. return !isThumb1Only();
  364. }
  365. bool ARMSubtarget::useStride4VFPs() const {
  366. // For general targets, the prologue can grow when VFPs are allocated with
  367. // stride 4 (more vpush instructions). But WatchOS uses a compact unwind
  368. // format which it's more important to get right.
  369. return isTargetWatchABI() ||
  370. (useWideStrideVFP() && !OptMinSize);
  371. }
  372. bool ARMSubtarget::useMovt() const {
  373. // NOTE Windows on ARM needs to use mov.w/mov.t pairs to materialise 32-bit
  374. // immediates as it is inherently position independent, and may be out of
  375. // range otherwise.
  376. return !NoMovt && hasV8MBaselineOps() &&
  377. (isTargetWindows() || !OptMinSize || genExecuteOnly());
  378. }
  379. bool ARMSubtarget::useFastISel() const {
  380. // Enable fast-isel for any target, for testing only.
  381. if (ForceFastISel)
  382. return true;
  383. // Limit fast-isel to the targets that are or have been tested.
  384. if (!hasV6Ops())
  385. return false;
  386. // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
  387. return TM.Options.EnableFastISel &&
  388. ((isTargetMachO() && !isThumb1Only()) ||
  389. (isTargetLinux() && !isThumb()) || (isTargetNaCl() && !isThumb()));
  390. }
  391. unsigned ARMSubtarget::getGPRAllocationOrder(const MachineFunction &MF) const {
  392. // The GPR register class has multiple possible allocation orders, with
  393. // tradeoffs preferred by different sub-architectures and optimisation goals.
  394. // The allocation orders are:
  395. // 0: (the default tablegen order, not used)
  396. // 1: r14, r0-r13
  397. // 2: r0-r7
  398. // 3: r0-r7, r12, lr, r8-r11
  399. // Note that the register allocator will change this order so that
  400. // callee-saved registers are used later, as they require extra work in the
  401. // prologue/epilogue (though we sometimes override that).
  402. // For thumb1-only targets, only the low registers are allocatable.
  403. if (isThumb1Only())
  404. return 2;
  405. // Allocate low registers first, so we can select more 16-bit instructions.
  406. // We also (in ignoreCSRForAllocationOrder) override the default behaviour
  407. // with regards to callee-saved registers, because pushing extra registers is
  408. // much cheaper (in terms of code size) than using high registers. After
  409. // that, we allocate r12 (doesn't need to be saved), lr (saving it means we
  410. // can return with the pop, don't need an extra "bx lr") and then the rest of
  411. // the high registers.
  412. if (isThumb2() && MF.getFunction().hasMinSize())
  413. return 3;
  414. // Otherwise, allocate in the default order, using LR first because saving it
  415. // allows a shorter epilogue sequence.
  416. return 1;
  417. }
  418. bool ARMSubtarget::ignoreCSRForAllocationOrder(const MachineFunction &MF,
  419. unsigned PhysReg) const {
  420. // To minimize code size in Thumb2, we prefer the usage of low regs (lower
  421. // cost per use) so we can use narrow encoding. By default, caller-saved
  422. // registers (e.g. lr, r12) are always allocated first, regardless of
  423. // their cost per use. When optForMinSize, we prefer the low regs even if
  424. // they are CSR because usually push/pop can be folded into existing ones.
  425. return isThumb2() && MF.getFunction().hasMinSize() &&
  426. ARM::GPRRegClass.contains(PhysReg);
  427. }
  428. bool ARMSubtarget::splitFramePointerPush(const MachineFunction &MF) const {
  429. const Function &F = MF.getFunction();
  430. if (!MF.getTarget().getMCAsmInfo()->usesWindowsCFI() ||
  431. !F.needsUnwindTableEntry())
  432. return false;
  433. const MachineFrameInfo &MFI = MF.getFrameInfo();
  434. return MFI.hasVarSizedObjects() || getRegisterInfo()->hasStackRealignment(MF);
  435. }