ARMSubtarget.cpp 17 KB

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