PPCSubtarget.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //===-- PowerPCSubtarget.cpp - PPC 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 PPC specific subclass of TargetSubtargetInfo.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "PPCSubtarget.h"
  14. #include "PPC.h"
  15. #include "PPCRegisterInfo.h"
  16. #include "llvm/CodeGen/MachineFunction.h"
  17. #include "llvm/CodeGen/MachineScheduler.h"
  18. #include "llvm/IR/Attributes.h"
  19. #include "llvm/IR/Function.h"
  20. #include "llvm/IR/GlobalValue.h"
  21. #include "llvm/Support/Host.h"
  22. #include "llvm/Support/TargetRegistry.h"
  23. #include "llvm/Target/TargetMachine.h"
  24. #include <cstdlib>
  25. using namespace llvm;
  26. #define DEBUG_TYPE "ppc-subtarget"
  27. #define GET_SUBTARGETINFO_TARGET_DESC
  28. #define GET_SUBTARGETINFO_CTOR
  29. #include "PPCGenSubtargetInfo.inc"
  30. /// Return the datalayout string of a subtarget.
  31. static std::string getDataLayoutString(const PPCSubtarget &ST) {
  32. const Triple &T = ST.getTargetTriple();
  33. std::string Ret;
  34. // Most PPC* platforms are big endian, PPC64LE is little endian.
  35. if (ST.isLittleEndian())
  36. Ret = "e";
  37. else
  38. Ret = "E";
  39. Ret += DataLayout::getManglingComponent(T);
  40. // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
  41. // pointers.
  42. if (!ST.isPPC64() || T.getOS() == Triple::Lv2)
  43. Ret += "-p:32:32";
  44. // Note, the alignment values for f64 and i64 on ppc64 in Darwin
  45. // documentation are wrong; these are correct (i.e. "what gcc does").
  46. if (ST.isPPC64() || ST.isSVR4ABI())
  47. Ret += "-i64:64";
  48. else
  49. Ret += "-f64:32:64";
  50. // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
  51. if (ST.isPPC64())
  52. Ret += "-n32:64";
  53. else
  54. Ret += "-n32";
  55. return Ret;
  56. }
  57. PPCSubtarget &PPCSubtarget::initializeSubtargetDependencies(StringRef CPU,
  58. StringRef FS) {
  59. initializeEnvironment();
  60. resetSubtargetFeatures(CPU, FS);
  61. return *this;
  62. }
  63. PPCSubtarget::PPCSubtarget(const std::string &TT, const std::string &CPU,
  64. const std::string &FS, PPCTargetMachine &TM,
  65. bool is64Bit, CodeGenOpt::Level OptLevel)
  66. : PPCGenSubtargetInfo(TT, CPU, FS), IsPPC64(is64Bit), TargetTriple(TT),
  67. OptLevel(OptLevel), TargetABI(PPC_ABI_UNKNOWN),
  68. FrameLowering(initializeSubtargetDependencies(CPU, FS)),
  69. DL(getDataLayoutString(*this)), InstrInfo(*this), JITInfo(*this),
  70. TLInfo(TM), TSInfo(&DL) {}
  71. /// SetJITMode - This is called to inform the subtarget info that we are
  72. /// producing code for the JIT.
  73. void PPCSubtarget::SetJITMode() {
  74. // JIT mode doesn't want lazy resolver stubs, it knows exactly where
  75. // everything is. This matters for PPC64, which codegens in PIC mode without
  76. // stubs.
  77. HasLazyResolverStubs = false;
  78. // Calls to external functions need to use indirect calls
  79. IsJITCodeModel = true;
  80. }
  81. void PPCSubtarget::resetSubtargetFeatures(const MachineFunction *MF) {
  82. AttributeSet FnAttrs = MF->getFunction()->getAttributes();
  83. Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
  84. "target-cpu");
  85. Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
  86. "target-features");
  87. std::string CPU =
  88. !CPUAttr.hasAttribute(Attribute::None) ? CPUAttr.getValueAsString() : "";
  89. std::string FS =
  90. !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
  91. if (!FS.empty()) {
  92. initializeEnvironment();
  93. resetSubtargetFeatures(CPU, FS);
  94. }
  95. }
  96. void PPCSubtarget::initializeEnvironment() {
  97. StackAlignment = 16;
  98. DarwinDirective = PPC::DIR_NONE;
  99. HasMFOCRF = false;
  100. Has64BitSupport = false;
  101. Use64BitRegs = false;
  102. UseCRBits = false;
  103. HasAltivec = false;
  104. HasSPE = false;
  105. HasQPX = false;
  106. HasVSX = false;
  107. HasFCPSGN = false;
  108. HasFSQRT = false;
  109. HasFRE = false;
  110. HasFRES = false;
  111. HasFRSQRTE = false;
  112. HasFRSQRTES = false;
  113. HasRecipPrec = false;
  114. HasSTFIWX = false;
  115. HasLFIWAX = false;
  116. HasFPRND = false;
  117. HasFPCVT = false;
  118. HasISEL = false;
  119. HasPOPCNTD = false;
  120. HasLDBRX = false;
  121. IsBookE = false;
  122. IsPPC4xx = false;
  123. IsPPC6xx = false;
  124. IsE500 = false;
  125. DeprecatedMFTB = false;
  126. DeprecatedDST = false;
  127. HasLazyResolverStubs = false;
  128. IsJITCodeModel = false;
  129. }
  130. void PPCSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
  131. // Determine default and user specified characteristics
  132. std::string CPUName = CPU;
  133. if (CPUName.empty())
  134. CPUName = "generic";
  135. #if (defined(__APPLE__) || defined(__linux__)) && \
  136. (defined(__ppc__) || defined(__powerpc__))
  137. if (CPUName == "generic")
  138. CPUName = sys::getHostCPUName();
  139. #endif
  140. // Initialize scheduling itinerary for the specified CPU.
  141. InstrItins = getInstrItineraryForCPU(CPUName);
  142. // Make sure 64-bit features are available when CPUname is generic
  143. std::string FullFS = FS;
  144. // If we are generating code for ppc64, verify that options make sense.
  145. if (IsPPC64) {
  146. Has64BitSupport = true;
  147. // Silently force 64-bit register use on ppc64.
  148. Use64BitRegs = true;
  149. if (!FullFS.empty())
  150. FullFS = "+64bit," + FullFS;
  151. else
  152. FullFS = "+64bit";
  153. }
  154. // At -O2 and above, track CR bits as individual registers.
  155. if (OptLevel >= CodeGenOpt::Default) {
  156. if (!FullFS.empty())
  157. FullFS = "+crbits," + FullFS;
  158. else
  159. FullFS = "+crbits";
  160. }
  161. // Parse features string.
  162. ParseSubtargetFeatures(CPUName, FullFS);
  163. // If the user requested use of 64-bit regs, but the cpu selected doesn't
  164. // support it, ignore.
  165. if (use64BitRegs() && !has64BitSupport())
  166. Use64BitRegs = false;
  167. // Set up darwin-specific properties.
  168. if (isDarwin())
  169. HasLazyResolverStubs = true;
  170. // QPX requires a 32-byte aligned stack. Note that we need to do this if
  171. // we're compiling for a BG/Q system regardless of whether or not QPX
  172. // is enabled because external functions will assume this alignment.
  173. if (hasQPX() || isBGQ())
  174. StackAlignment = 32;
  175. // Determine endianness.
  176. IsLittleEndian = (TargetTriple.getArch() == Triple::ppc64le);
  177. // FIXME: For now, we disable VSX in little-endian mode until endian
  178. // issues in those instructions can be addressed.
  179. if (IsLittleEndian)
  180. HasVSX = false;
  181. // Determine default ABI.
  182. if (TargetABI == PPC_ABI_UNKNOWN) {
  183. if (!isDarwin() && IsPPC64) {
  184. if (IsLittleEndian)
  185. TargetABI = PPC_ABI_ELFv2;
  186. else
  187. TargetABI = PPC_ABI_ELFv1;
  188. }
  189. }
  190. }
  191. /// hasLazyResolverStub - Return true if accesses to the specified global have
  192. /// to go through a dyld lazy resolution stub. This means that an extra load
  193. /// is required to get the address of the global.
  194. bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV,
  195. const TargetMachine &TM) const {
  196. // We never have stubs if HasLazyResolverStubs=false or if in static mode.
  197. if (!HasLazyResolverStubs || TM.getRelocationModel() == Reloc::Static)
  198. return false;
  199. // If symbol visibility is hidden, the extra load is not needed if
  200. // the symbol is definitely defined in the current translation unit.
  201. bool isDecl = GV->isDeclaration() && !GV->isMaterializable();
  202. if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage())
  203. return false;
  204. return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
  205. GV->hasCommonLinkage() || isDecl;
  206. }
  207. // Embedded cores need aggressive scheduling (and some others also benefit).
  208. static bool needsAggressiveScheduling(unsigned Directive) {
  209. switch (Directive) {
  210. default: return false;
  211. case PPC::DIR_440:
  212. case PPC::DIR_A2:
  213. case PPC::DIR_E500mc:
  214. case PPC::DIR_E5500:
  215. case PPC::DIR_PWR7:
  216. case PPC::DIR_PWR8:
  217. return true;
  218. }
  219. }
  220. bool PPCSubtarget::enableMachineScheduler() const {
  221. // Enable MI scheduling for the embedded cores.
  222. // FIXME: Enable this for all cores (some additional modeling
  223. // may be necessary).
  224. return needsAggressiveScheduling(DarwinDirective);
  225. }
  226. // This overrides the PostRAScheduler bit in the SchedModel for each CPU.
  227. bool PPCSubtarget::enablePostMachineScheduler() const { return true; }
  228. PPCGenSubtargetInfo::AntiDepBreakMode PPCSubtarget::getAntiDepBreakMode() const {
  229. return TargetSubtargetInfo::ANTIDEP_ALL;
  230. }
  231. void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
  232. CriticalPathRCs.clear();
  233. CriticalPathRCs.push_back(isPPC64() ?
  234. &PPC::G8RCRegClass : &PPC::GPRCRegClass);
  235. }
  236. void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
  237. MachineInstr *begin,
  238. MachineInstr *end,
  239. unsigned NumRegionInstrs) const {
  240. if (needsAggressiveScheduling(DarwinDirective)) {
  241. Policy.OnlyTopDown = false;
  242. Policy.OnlyBottomUp = false;
  243. }
  244. // Spilling is generally expensive on all PPC cores, so always enable
  245. // register-pressure tracking.
  246. Policy.ShouldTrackPressure = true;
  247. }
  248. bool PPCSubtarget::useAA() const {
  249. // Use AA during code generation for the embedded cores.
  250. return needsAggressiveScheduling(DarwinDirective);
  251. }