PPCTargetMachine.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. //===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
  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. // Top-level implementation for the PowerPC target.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "PPCTargetMachine.h"
  13. #include "MCTargetDesc/PPCMCTargetDesc.h"
  14. #include "PPC.h"
  15. #include "PPCMachineFunctionInfo.h"
  16. #include "PPCMachineScheduler.h"
  17. #include "PPCMacroFusion.h"
  18. #include "PPCSubtarget.h"
  19. #include "PPCTargetObjectFile.h"
  20. #include "PPCTargetTransformInfo.h"
  21. #include "TargetInfo/PowerPCTargetInfo.h"
  22. #include "llvm/ADT/STLExtras.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/ADT/Triple.h"
  25. #include "llvm/Analysis/TargetTransformInfo.h"
  26. #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
  27. #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
  28. #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
  29. #include "llvm/CodeGen/GlobalISel/Legalizer.h"
  30. #include "llvm/CodeGen/GlobalISel/Localizer.h"
  31. #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
  32. #include "llvm/CodeGen/MachineScheduler.h"
  33. #include "llvm/CodeGen/Passes.h"
  34. #include "llvm/CodeGen/TargetPassConfig.h"
  35. #include "llvm/IR/Attributes.h"
  36. #include "llvm/IR/DataLayout.h"
  37. #include "llvm/IR/Function.h"
  38. #include "llvm/InitializePasses.h"
  39. #include "llvm/MC/TargetRegistry.h"
  40. #include "llvm/Pass.h"
  41. #include "llvm/Support/CodeGen.h"
  42. #include "llvm/Support/CommandLine.h"
  43. #include "llvm/Target/TargetLoweringObjectFile.h"
  44. #include "llvm/Target/TargetOptions.h"
  45. #include "llvm/Transforms/Scalar.h"
  46. #include <cassert>
  47. #include <memory>
  48. #include <optional>
  49. #include <string>
  50. using namespace llvm;
  51. static cl::opt<bool>
  52. EnableBranchCoalescing("enable-ppc-branch-coalesce", cl::Hidden,
  53. cl::desc("enable coalescing of duplicate branches for PPC"));
  54. static cl::
  55. opt<bool> DisableCTRLoops("disable-ppc-ctrloops", cl::Hidden,
  56. cl::desc("Disable CTR loops for PPC"));
  57. static cl::
  58. opt<bool> DisableInstrFormPrep("disable-ppc-instr-form-prep", cl::Hidden,
  59. cl::desc("Disable PPC loop instr form prep"));
  60. static cl::opt<bool>
  61. VSXFMAMutateEarly("schedule-ppc-vsx-fma-mutation-early",
  62. cl::Hidden, cl::desc("Schedule VSX FMA instruction mutation early"));
  63. static cl::
  64. opt<bool> DisableVSXSwapRemoval("disable-ppc-vsx-swap-removal", cl::Hidden,
  65. cl::desc("Disable VSX Swap Removal for PPC"));
  66. static cl::
  67. opt<bool> DisableMIPeephole("disable-ppc-peephole", cl::Hidden,
  68. cl::desc("Disable machine peepholes for PPC"));
  69. static cl::opt<bool>
  70. EnableGEPOpt("ppc-gep-opt", cl::Hidden,
  71. cl::desc("Enable optimizations on complex GEPs"),
  72. cl::init(true));
  73. static cl::opt<bool>
  74. EnablePrefetch("enable-ppc-prefetching",
  75. cl::desc("enable software prefetching on PPC"),
  76. cl::init(false), cl::Hidden);
  77. static cl::opt<bool>
  78. EnableExtraTOCRegDeps("enable-ppc-extra-toc-reg-deps",
  79. cl::desc("Add extra TOC register dependencies"),
  80. cl::init(true), cl::Hidden);
  81. static cl::opt<bool>
  82. EnableMachineCombinerPass("ppc-machine-combiner",
  83. cl::desc("Enable the machine combiner pass"),
  84. cl::init(true), cl::Hidden);
  85. static cl::opt<bool>
  86. ReduceCRLogical("ppc-reduce-cr-logicals",
  87. cl::desc("Expand eligible cr-logical binary ops to branches"),
  88. cl::init(true), cl::Hidden);
  89. static cl::opt<bool> EnablePPCGenScalarMASSEntries(
  90. "enable-ppc-gen-scalar-mass", cl::init(false),
  91. cl::desc("Enable lowering math functions to their corresponding MASS "
  92. "(scalar) entries"),
  93. cl::Hidden);
  94. extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCTarget() {
  95. // Register the targets
  96. RegisterTargetMachine<PPCTargetMachine> A(getThePPC32Target());
  97. RegisterTargetMachine<PPCTargetMachine> B(getThePPC32LETarget());
  98. RegisterTargetMachine<PPCTargetMachine> C(getThePPC64Target());
  99. RegisterTargetMachine<PPCTargetMachine> D(getThePPC64LETarget());
  100. PassRegistry &PR = *PassRegistry::getPassRegistry();
  101. #ifndef NDEBUG
  102. initializePPCCTRLoopsVerifyPass(PR);
  103. #endif
  104. initializePPCLoopInstrFormPrepPass(PR);
  105. initializePPCTOCRegDepsPass(PR);
  106. initializePPCEarlyReturnPass(PR);
  107. initializePPCVSXCopyPass(PR);
  108. initializePPCVSXFMAMutatePass(PR);
  109. initializePPCVSXSwapRemovalPass(PR);
  110. initializePPCReduceCRLogicalsPass(PR);
  111. initializePPCBSelPass(PR);
  112. initializePPCBranchCoalescingPass(PR);
  113. initializePPCBoolRetToIntPass(PR);
  114. initializePPCExpandISELPass(PR);
  115. initializePPCPreEmitPeepholePass(PR);
  116. initializePPCTLSDynamicCallPass(PR);
  117. initializePPCMIPeepholePass(PR);
  118. initializePPCLowerMASSVEntriesPass(PR);
  119. initializePPCGenScalarMASSEntriesPass(PR);
  120. initializePPCExpandAtomicPseudoPass(PR);
  121. initializeGlobalISel(PR);
  122. initializePPCCTRLoopsPass(PR);
  123. initializePPCDAGToDAGISelPass(PR);
  124. }
  125. static bool isLittleEndianTriple(const Triple &T) {
  126. return T.getArch() == Triple::ppc64le || T.getArch() == Triple::ppcle;
  127. }
  128. /// Return the datalayout string of a subtarget.
  129. static std::string getDataLayoutString(const Triple &T) {
  130. bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
  131. std::string Ret;
  132. // Most PPC* platforms are big endian, PPC(64)LE is little endian.
  133. if (isLittleEndianTriple(T))
  134. Ret = "e";
  135. else
  136. Ret = "E";
  137. Ret += DataLayout::getManglingComponent(T);
  138. // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
  139. // pointers.
  140. if (!is64Bit || T.getOS() == Triple::Lv2)
  141. Ret += "-p:32:32";
  142. // Note, the alignment values for f64 and i64 on ppc64 in Darwin
  143. // documentation are wrong; these are correct (i.e. "what gcc does").
  144. Ret += "-i64:64";
  145. // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
  146. if (is64Bit)
  147. Ret += "-n32:64";
  148. else
  149. Ret += "-n32";
  150. // Specify the vector alignment explicitly. For v256i1 and v512i1, the
  151. // calculated alignment would be 256*alignment(i1) and 512*alignment(i1),
  152. // which is 256 and 512 bytes - way over aligned.
  153. if (is64Bit && (T.isOSAIX() || T.isOSLinux()))
  154. Ret += "-S128-v256:256:256-v512:512:512";
  155. return Ret;
  156. }
  157. static std::string computeFSAdditions(StringRef FS, CodeGenOpt::Level OL,
  158. const Triple &TT) {
  159. std::string FullFS = std::string(FS);
  160. // Make sure 64-bit features are available when CPUname is generic
  161. if (TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le) {
  162. if (!FullFS.empty())
  163. FullFS = "+64bit," + FullFS;
  164. else
  165. FullFS = "+64bit";
  166. }
  167. if (OL >= CodeGenOpt::Default) {
  168. if (!FullFS.empty())
  169. FullFS = "+crbits," + FullFS;
  170. else
  171. FullFS = "+crbits";
  172. }
  173. if (OL != CodeGenOpt::None) {
  174. if (!FullFS.empty())
  175. FullFS = "+invariant-function-descriptors," + FullFS;
  176. else
  177. FullFS = "+invariant-function-descriptors";
  178. }
  179. if (TT.isOSAIX()) {
  180. if (!FullFS.empty())
  181. FullFS = "+aix," + FullFS;
  182. else
  183. FullFS = "+aix";
  184. }
  185. return FullFS;
  186. }
  187. static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
  188. if (TT.isOSAIX())
  189. return std::make_unique<TargetLoweringObjectFileXCOFF>();
  190. return std::make_unique<PPC64LinuxTargetObjectFile>();
  191. }
  192. static PPCTargetMachine::PPCABI computeTargetABI(const Triple &TT,
  193. const TargetOptions &Options) {
  194. if (Options.MCOptions.getABIName().startswith("elfv1"))
  195. return PPCTargetMachine::PPC_ABI_ELFv1;
  196. else if (Options.MCOptions.getABIName().startswith("elfv2"))
  197. return PPCTargetMachine::PPC_ABI_ELFv2;
  198. assert(Options.MCOptions.getABIName().empty() &&
  199. "Unknown target-abi option!");
  200. switch (TT.getArch()) {
  201. case Triple::ppc64le:
  202. return PPCTargetMachine::PPC_ABI_ELFv2;
  203. case Triple::ppc64:
  204. if (TT.isPPC64ELFv2ABI())
  205. return PPCTargetMachine::PPC_ABI_ELFv2;
  206. else
  207. return PPCTargetMachine::PPC_ABI_ELFv1;
  208. default:
  209. return PPCTargetMachine::PPC_ABI_UNKNOWN;
  210. }
  211. }
  212. static Reloc::Model getEffectiveRelocModel(const Triple &TT,
  213. std::optional<Reloc::Model> RM) {
  214. assert((!TT.isOSAIX() || !RM || *RM == Reloc::PIC_) &&
  215. "Invalid relocation model for AIX.");
  216. if (RM)
  217. return *RM;
  218. // Big Endian PPC and AIX default to PIC.
  219. if (TT.getArch() == Triple::ppc64 || TT.isOSAIX())
  220. return Reloc::PIC_;
  221. // Rest are static by default.
  222. return Reloc::Static;
  223. }
  224. static CodeModel::Model
  225. getEffectivePPCCodeModel(const Triple &TT, std::optional<CodeModel::Model> CM,
  226. bool JIT) {
  227. if (CM) {
  228. if (*CM == CodeModel::Tiny)
  229. report_fatal_error("Target does not support the tiny CodeModel", false);
  230. if (*CM == CodeModel::Kernel)
  231. report_fatal_error("Target does not support the kernel CodeModel", false);
  232. return *CM;
  233. }
  234. if (JIT)
  235. return CodeModel::Small;
  236. if (TT.isOSAIX())
  237. return CodeModel::Small;
  238. assert(TT.isOSBinFormatELF() && "All remaining PPC OSes are ELF based.");
  239. if (TT.isArch32Bit())
  240. return CodeModel::Small;
  241. assert(TT.isArch64Bit() && "Unsupported PPC architecture.");
  242. return CodeModel::Medium;
  243. }
  244. static ScheduleDAGInstrs *createPPCMachineScheduler(MachineSchedContext *C) {
  245. const PPCSubtarget &ST = C->MF->getSubtarget<PPCSubtarget>();
  246. ScheduleDAGMILive *DAG =
  247. new ScheduleDAGMILive(C, ST.usePPCPreRASchedStrategy() ?
  248. std::make_unique<PPCPreRASchedStrategy>(C) :
  249. std::make_unique<GenericScheduler>(C));
  250. // add DAG Mutations here.
  251. DAG->addMutation(createCopyConstrainDAGMutation(DAG->TII, DAG->TRI));
  252. if (ST.hasStoreFusion())
  253. DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
  254. if (ST.hasFusion())
  255. DAG->addMutation(createPowerPCMacroFusionDAGMutation());
  256. return DAG;
  257. }
  258. static ScheduleDAGInstrs *createPPCPostMachineScheduler(
  259. MachineSchedContext *C) {
  260. const PPCSubtarget &ST = C->MF->getSubtarget<PPCSubtarget>();
  261. ScheduleDAGMI *DAG =
  262. new ScheduleDAGMI(C, ST.usePPCPostRASchedStrategy() ?
  263. std::make_unique<PPCPostRASchedStrategy>(C) :
  264. std::make_unique<PostGenericScheduler>(C), true);
  265. // add DAG Mutations here.
  266. if (ST.hasStoreFusion())
  267. DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
  268. if (ST.hasFusion())
  269. DAG->addMutation(createPowerPCMacroFusionDAGMutation());
  270. return DAG;
  271. }
  272. // The FeatureString here is a little subtle. We are modifying the feature
  273. // string with what are (currently) non-function specific overrides as it goes
  274. // into the LLVMTargetMachine constructor and then using the stored value in the
  275. // Subtarget constructor below it.
  276. PPCTargetMachine::PPCTargetMachine(const Target &T, const Triple &TT,
  277. StringRef CPU, StringRef FS,
  278. const TargetOptions &Options,
  279. std::optional<Reloc::Model> RM,
  280. std::optional<CodeModel::Model> CM,
  281. CodeGenOpt::Level OL, bool JIT)
  282. : LLVMTargetMachine(T, getDataLayoutString(TT), TT, CPU,
  283. computeFSAdditions(FS, OL, TT), Options,
  284. getEffectiveRelocModel(TT, RM),
  285. getEffectivePPCCodeModel(TT, CM, JIT), OL),
  286. TLOF(createTLOF(getTargetTriple())),
  287. TargetABI(computeTargetABI(TT, Options)),
  288. Endianness(isLittleEndianTriple(TT) ? Endian::LITTLE : Endian::BIG) {
  289. initAsmInfo();
  290. }
  291. PPCTargetMachine::~PPCTargetMachine() = default;
  292. const PPCSubtarget *
  293. PPCTargetMachine::getSubtargetImpl(const Function &F) const {
  294. Attribute CPUAttr = F.getFnAttribute("target-cpu");
  295. Attribute TuneAttr = F.getFnAttribute("tune-cpu");
  296. Attribute FSAttr = F.getFnAttribute("target-features");
  297. std::string CPU =
  298. CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
  299. std::string TuneCPU =
  300. TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
  301. std::string FS =
  302. FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
  303. // FIXME: This is related to the code below to reset the target options,
  304. // we need to know whether or not the soft float flag is set on the
  305. // function before we can generate a subtarget. We also need to use
  306. // it as a key for the subtarget since that can be the only difference
  307. // between two functions.
  308. bool SoftFloat = F.getFnAttribute("use-soft-float").getValueAsBool();
  309. // If the soft float attribute is set on the function turn on the soft float
  310. // subtarget feature.
  311. if (SoftFloat)
  312. FS += FS.empty() ? "-hard-float" : ",-hard-float";
  313. auto &I = SubtargetMap[CPU + TuneCPU + FS];
  314. if (!I) {
  315. // This needs to be done before we create a new subtarget since any
  316. // creation will depend on the TM and the code generation flags on the
  317. // function that reside in TargetOptions.
  318. resetTargetOptions(F);
  319. I = std::make_unique<PPCSubtarget>(
  320. TargetTriple, CPU, TuneCPU,
  321. // FIXME: It would be good to have the subtarget additions here
  322. // not necessary. Anything that turns them on/off (overrides) ends
  323. // up being put at the end of the feature string, but the defaults
  324. // shouldn't require adding them. Fixing this means pulling Feature64Bit
  325. // out of most of the target cpus in the .td file and making it set only
  326. // as part of initialization via the TargetTriple.
  327. computeFSAdditions(FS, getOptLevel(), getTargetTriple()), *this);
  328. }
  329. return I.get();
  330. }
  331. //===----------------------------------------------------------------------===//
  332. // Pass Pipeline Configuration
  333. //===----------------------------------------------------------------------===//
  334. namespace {
  335. /// PPC Code Generator Pass Configuration Options.
  336. class PPCPassConfig : public TargetPassConfig {
  337. public:
  338. PPCPassConfig(PPCTargetMachine &TM, PassManagerBase &PM)
  339. : TargetPassConfig(TM, PM) {
  340. // At any optimization level above -O0 we use the Machine Scheduler and not
  341. // the default Post RA List Scheduler.
  342. if (TM.getOptLevel() != CodeGenOpt::None)
  343. substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
  344. }
  345. PPCTargetMachine &getPPCTargetMachine() const {
  346. return getTM<PPCTargetMachine>();
  347. }
  348. void addIRPasses() override;
  349. bool addPreISel() override;
  350. bool addILPOpts() override;
  351. bool addInstSelector() override;
  352. void addMachineSSAOptimization() override;
  353. void addPreRegAlloc() override;
  354. void addPreSched2() override;
  355. void addPreEmitPass() override;
  356. void addPreEmitPass2() override;
  357. // GlobalISEL
  358. bool addIRTranslator() override;
  359. bool addLegalizeMachineIR() override;
  360. bool addRegBankSelect() override;
  361. bool addGlobalInstructionSelect() override;
  362. ScheduleDAGInstrs *
  363. createMachineScheduler(MachineSchedContext *C) const override {
  364. return createPPCMachineScheduler(C);
  365. }
  366. ScheduleDAGInstrs *
  367. createPostMachineScheduler(MachineSchedContext *C) const override {
  368. return createPPCPostMachineScheduler(C);
  369. }
  370. };
  371. } // end anonymous namespace
  372. TargetPassConfig *PPCTargetMachine::createPassConfig(PassManagerBase &PM) {
  373. return new PPCPassConfig(*this, PM);
  374. }
  375. void PPCPassConfig::addIRPasses() {
  376. if (TM->getOptLevel() != CodeGenOpt::None)
  377. addPass(createPPCBoolRetToIntPass());
  378. addPass(createAtomicExpandPass());
  379. // Lower generic MASSV routines to PowerPC subtarget-specific entries.
  380. addPass(createPPCLowerMASSVEntriesPass());
  381. // Generate PowerPC target-specific entries for scalar math functions
  382. // that are available in IBM MASS (scalar) library.
  383. if (TM->getOptLevel() == CodeGenOpt::Aggressive &&
  384. EnablePPCGenScalarMASSEntries) {
  385. TM->Options.PPCGenScalarMASSEntries = EnablePPCGenScalarMASSEntries;
  386. addPass(createPPCGenScalarMASSEntriesPass());
  387. }
  388. // If explicitly requested, add explicit data prefetch intrinsics.
  389. if (EnablePrefetch.getNumOccurrences() > 0)
  390. addPass(createLoopDataPrefetchPass());
  391. if (TM->getOptLevel() >= CodeGenOpt::Default && EnableGEPOpt) {
  392. // Call SeparateConstOffsetFromGEP pass to extract constants within indices
  393. // and lower a GEP with multiple indices to either arithmetic operations or
  394. // multiple GEPs with single index.
  395. addPass(createSeparateConstOffsetFromGEPPass(true));
  396. // Call EarlyCSE pass to find and remove subexpressions in the lowered
  397. // result.
  398. addPass(createEarlyCSEPass());
  399. // Do loop invariant code motion in case part of the lowered result is
  400. // invariant.
  401. addPass(createLICMPass());
  402. }
  403. TargetPassConfig::addIRPasses();
  404. }
  405. bool PPCPassConfig::addPreISel() {
  406. if (!DisableInstrFormPrep && getOptLevel() != CodeGenOpt::None)
  407. addPass(createPPCLoopInstrFormPrepPass(getPPCTargetMachine()));
  408. if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
  409. addPass(createHardwareLoopsPass());
  410. return false;
  411. }
  412. bool PPCPassConfig::addILPOpts() {
  413. addPass(&EarlyIfConverterID);
  414. if (EnableMachineCombinerPass)
  415. addPass(&MachineCombinerID);
  416. return true;
  417. }
  418. bool PPCPassConfig::addInstSelector() {
  419. // Install an instruction selector.
  420. addPass(createPPCISelDag(getPPCTargetMachine(), getOptLevel()));
  421. #ifndef NDEBUG
  422. if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
  423. addPass(createPPCCTRLoopsVerify());
  424. #endif
  425. addPass(createPPCVSXCopyPass());
  426. return false;
  427. }
  428. void PPCPassConfig::addMachineSSAOptimization() {
  429. // Run CTR loops pass before any cfg modification pass to prevent the
  430. // canonical form of hardware loop from being destroied.
  431. if (!DisableCTRLoops && getOptLevel() != CodeGenOpt::None)
  432. addPass(createPPCCTRLoopsPass());
  433. // PPCBranchCoalescingPass need to be done before machine sinking
  434. // since it merges empty blocks.
  435. if (EnableBranchCoalescing && getOptLevel() != CodeGenOpt::None)
  436. addPass(createPPCBranchCoalescingPass());
  437. TargetPassConfig::addMachineSSAOptimization();
  438. // For little endian, remove where possible the vector swap instructions
  439. // introduced at code generation to normalize vector element order.
  440. if (TM->getTargetTriple().getArch() == Triple::ppc64le &&
  441. !DisableVSXSwapRemoval)
  442. addPass(createPPCVSXSwapRemovalPass());
  443. // Reduce the number of cr-logical ops.
  444. if (ReduceCRLogical && getOptLevel() != CodeGenOpt::None)
  445. addPass(createPPCReduceCRLogicalsPass());
  446. // Target-specific peephole cleanups performed after instruction
  447. // selection.
  448. if (!DisableMIPeephole) {
  449. addPass(createPPCMIPeepholePass());
  450. addPass(&DeadMachineInstructionElimID);
  451. }
  452. }
  453. void PPCPassConfig::addPreRegAlloc() {
  454. if (getOptLevel() != CodeGenOpt::None) {
  455. initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
  456. insertPass(VSXFMAMutateEarly ? &RegisterCoalescerID : &MachineSchedulerID,
  457. &PPCVSXFMAMutateID);
  458. }
  459. // FIXME: We probably don't need to run these for -fPIE.
  460. if (getPPCTargetMachine().isPositionIndependent()) {
  461. // FIXME: LiveVariables should not be necessary here!
  462. // PPCTLSDynamicCallPass uses LiveIntervals which previously dependent on
  463. // LiveVariables. This (unnecessary) dependency has been removed now,
  464. // however a stage-2 clang build fails without LiveVariables computed here.
  465. addPass(&LiveVariablesID);
  466. addPass(createPPCTLSDynamicCallPass());
  467. }
  468. if (EnableExtraTOCRegDeps)
  469. addPass(createPPCTOCRegDepsPass());
  470. if (getOptLevel() != CodeGenOpt::None)
  471. addPass(&MachinePipelinerID);
  472. }
  473. void PPCPassConfig::addPreSched2() {
  474. if (getOptLevel() != CodeGenOpt::None)
  475. addPass(&IfConverterID);
  476. }
  477. void PPCPassConfig::addPreEmitPass() {
  478. addPass(createPPCPreEmitPeepholePass());
  479. addPass(createPPCExpandISELPass());
  480. if (getOptLevel() != CodeGenOpt::None)
  481. addPass(createPPCEarlyReturnPass());
  482. }
  483. void PPCPassConfig::addPreEmitPass2() {
  484. // Schedule the expansion of AMOs at the last possible moment, avoiding the
  485. // possibility for other passes to break the requirements for forward
  486. // progress in the LL/SC block.
  487. addPass(createPPCExpandAtomicPseudoPass());
  488. // Must run branch selection immediately preceding the asm printer.
  489. addPass(createPPCBranchSelectionPass());
  490. }
  491. TargetTransformInfo
  492. PPCTargetMachine::getTargetTransformInfo(const Function &F) const {
  493. return TargetTransformInfo(PPCTTIImpl(this, F));
  494. }
  495. bool PPCTargetMachine::isLittleEndian() const {
  496. assert(Endianness != Endian::NOT_DETECTED &&
  497. "Unable to determine endianness");
  498. return Endianness == Endian::LITTLE;
  499. }
  500. MachineFunctionInfo *PPCTargetMachine::createMachineFunctionInfo(
  501. BumpPtrAllocator &Allocator, const Function &F,
  502. const TargetSubtargetInfo *STI) const {
  503. return PPCFunctionInfo::create<PPCFunctionInfo>(Allocator, F, STI);
  504. }
  505. static MachineSchedRegistry
  506. PPCPreRASchedRegistry("ppc-prera",
  507. "Run PowerPC PreRA specific scheduler",
  508. createPPCMachineScheduler);
  509. static MachineSchedRegistry
  510. PPCPostRASchedRegistry("ppc-postra",
  511. "Run PowerPC PostRA specific scheduler",
  512. createPPCPostMachineScheduler);
  513. // Global ISEL
  514. bool PPCPassConfig::addIRTranslator() {
  515. addPass(new IRTranslator());
  516. return false;
  517. }
  518. bool PPCPassConfig::addLegalizeMachineIR() {
  519. addPass(new Legalizer());
  520. return false;
  521. }
  522. bool PPCPassConfig::addRegBankSelect() {
  523. addPass(new RegBankSelect());
  524. return false;
  525. }
  526. bool PPCPassConfig::addGlobalInstructionSelect() {
  527. addPass(new InstructionSelect(getOptLevel()));
  528. return false;
  529. }