PPCSubtarget.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //===-- PowerPCSubtarget.cpp - PPC 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 PPC specific subclass of TargetSubtargetInfo.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "PPCSubtarget.h"
  13. #include "GISel/PPCCallLowering.h"
  14. #include "GISel/PPCLegalizerInfo.h"
  15. #include "GISel/PPCRegisterBankInfo.h"
  16. #include "PPC.h"
  17. #include "PPCRegisterInfo.h"
  18. #include "PPCTargetMachine.h"
  19. #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
  20. #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
  21. #include "llvm/CodeGen/MachineFunction.h"
  22. #include "llvm/CodeGen/MachineScheduler.h"
  23. #include "llvm/IR/Attributes.h"
  24. #include "llvm/IR/Function.h"
  25. #include "llvm/IR/GlobalValue.h"
  26. #include "llvm/MC/TargetRegistry.h"
  27. #include "llvm/Support/CommandLine.h"
  28. #include "llvm/Target/TargetMachine.h"
  29. #include <cstdlib>
  30. using namespace llvm;
  31. #define DEBUG_TYPE "ppc-subtarget"
  32. #define GET_SUBTARGETINFO_TARGET_DESC
  33. #define GET_SUBTARGETINFO_CTOR
  34. #include "PPCGenSubtargetInfo.inc"
  35. static cl::opt<bool>
  36. UseSubRegLiveness("ppc-track-subreg-liveness",
  37. cl::desc("Enable subregister liveness tracking for PPC"),
  38. cl::init(true), cl::Hidden);
  39. static cl::opt<bool>
  40. EnableMachinePipeliner("ppc-enable-pipeliner",
  41. cl::desc("Enable Machine Pipeliner for PPC"),
  42. cl::init(false), cl::Hidden);
  43. PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU,
  44. StringRef TuneCPU,
  45. StringRef FS) {
  46. initializeEnvironment();
  47. initSubtargetFeatures(CPU, TuneCPU, FS);
  48. return *this;
  49. }
  50. PPCSubtarget::PPCSubtarget(const Triple &TT, const std::string &CPU,
  51. const std::string &TuneCPU, const std::string &FS,
  52. const PPCTargetMachine &TM)
  53. : PPCGenSubtargetInfo(TT, CPU, TuneCPU, FS), TargetTriple(TT),
  54. IsPPC64(TargetTriple.getArch() == Triple::ppc64 ||
  55. TargetTriple.getArch() == Triple::ppc64le),
  56. TM(TM), FrameLowering(initializeSubtargetDependencies(CPU, TuneCPU, FS)),
  57. InstrInfo(*this), TLInfo(TM, *this) {
  58. CallLoweringInfo.reset(new PPCCallLowering(*getTargetLowering()));
  59. Legalizer.reset(new PPCLegalizerInfo(*this));
  60. auto *RBI = new PPCRegisterBankInfo(*getRegisterInfo());
  61. RegBankInfo.reset(RBI);
  62. InstSelector.reset(createPPCInstructionSelector(
  63. *static_cast<const PPCTargetMachine *>(&TM), *this, *RBI));
  64. }
  65. void PPCSubtarget::initializeEnvironment() {
  66. StackAlignment = Align(16);
  67. CPUDirective = PPC::DIR_NONE;
  68. HasPOPCNTD = POPCNTD_Unavailable;
  69. }
  70. void PPCSubtarget::initSubtargetFeatures(StringRef CPU, StringRef TuneCPU,
  71. StringRef FS) {
  72. // Determine default and user specified characteristics
  73. std::string CPUName = std::string(CPU);
  74. if (CPUName.empty() || CPU == "generic") {
  75. // If cross-compiling with -march=ppc64le without -mcpu
  76. if (TargetTriple.getArch() == Triple::ppc64le)
  77. CPUName = "ppc64le";
  78. else if (TargetTriple.getSubArch() == Triple::PPCSubArch_spe)
  79. CPUName = "e500";
  80. else
  81. CPUName = "generic";
  82. }
  83. // Determine the CPU to schedule for.
  84. if (TuneCPU.empty()) TuneCPU = CPUName;
  85. // Initialize scheduling itinerary for the specified CPU.
  86. InstrItins = getInstrItineraryForCPU(CPUName);
  87. // Parse features string.
  88. ParseSubtargetFeatures(CPUName, TuneCPU, FS);
  89. // If the user requested use of 64-bit regs, but the cpu selected doesn't
  90. // support it, ignore.
  91. if (IsPPC64 && has64BitSupport())
  92. Use64BitRegs = true;
  93. if ((TargetTriple.isOSFreeBSD() && TargetTriple.getOSMajorVersion() >= 13) ||
  94. TargetTriple.isOSNetBSD() || TargetTriple.isOSOpenBSD() ||
  95. TargetTriple.isMusl())
  96. IsSecurePlt = true;
  97. if (HasSPE && IsPPC64)
  98. report_fatal_error( "SPE is only supported for 32-bit targets.\n", false);
  99. if (HasSPE && (HasAltivec || HasVSX || HasFPU))
  100. report_fatal_error(
  101. "SPE and traditional floating point cannot both be enabled.\n", false);
  102. // If not SPE, set standard FPU
  103. if (!HasSPE)
  104. HasFPU = true;
  105. StackAlignment = getPlatformStackAlignment();
  106. // Determine endianness.
  107. IsLittleEndian = TM.isLittleEndian();
  108. }
  109. bool PPCSubtarget::enableMachineScheduler() const { return true; }
  110. bool PPCSubtarget::enableMachinePipeliner() const {
  111. return getSchedModel().hasInstrSchedModel() && EnableMachinePipeliner;
  112. }
  113. bool PPCSubtarget::useDFAforSMS() const { return false; }
  114. // This overrides the PostRAScheduler bit in the SchedModel for each CPU.
  115. bool PPCSubtarget::enablePostRAScheduler() const { return true; }
  116. PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
  117. return TargetSubtargetInfo::ANTIDEP_ALL;
  118. }
  119. void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
  120. CriticalPathRCs.clear();
  121. CriticalPathRCs.push_back(isPPC64() ?
  122. &PPC::G8RCRegClass : &PPC::GPRCRegClass);
  123. }
  124. void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
  125. unsigned NumRegionInstrs) const {
  126. // The GenericScheduler that we use defaults to scheduling bottom up only.
  127. // We want to schedule from both the top and the bottom and so we set
  128. // OnlyBottomUp to false.
  129. // We want to do bi-directional scheduling since it provides a more balanced
  130. // schedule leading to better performance.
  131. Policy.OnlyBottomUp = false;
  132. // Spilling is generally expensive on all PPC cores, so always enable
  133. // register-pressure tracking.
  134. Policy.ShouldTrackPressure = true;
  135. }
  136. bool PPCSubtarget::useAA() const {
  137. return true;
  138. }
  139. bool PPCSubtarget::enableSubRegLiveness() const {
  140. return UseSubRegLiveness;
  141. }
  142. bool PPCSubtarget::isGVIndirectSymbol(const GlobalValue *GV) const {
  143. // Large code model always uses the TOC even for local symbols.
  144. if (TM.getCodeModel() == CodeModel::Large)
  145. return true;
  146. if (TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
  147. return false;
  148. return true;
  149. }
  150. bool PPCSubtarget::isELFv2ABI() const { return TM.isELFv2ABI(); }
  151. bool PPCSubtarget::isPPC64() const { return TM.isPPC64(); }
  152. bool PPCSubtarget::isUsingPCRelativeCalls() const {
  153. return isPPC64() && hasPCRelativeMemops() && isELFv2ABI() &&
  154. CodeModel::Medium == getTargetMachine().getCodeModel();
  155. }
  156. // GlobalISEL
  157. const CallLowering *PPCSubtarget::getCallLowering() const {
  158. return CallLoweringInfo.get();
  159. }
  160. const RegisterBankInfo *PPCSubtarget::getRegBankInfo() const {
  161. return RegBankInfo.get();
  162. }
  163. const LegalizerInfo *PPCSubtarget::getLegalizerInfo() const {
  164. return Legalizer.get();
  165. }
  166. InstructionSelector *PPCSubtarget::getInstructionSelector() const {
  167. return InstSelector.get();
  168. }