PPCPreEmitPeephole.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. //===--------- PPCPreEmitPeephole.cpp - Late peephole optimizations -------===//
  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. // A pre-emit peephole for catching opportunities introduced by late passes such
  10. // as MachineBlockPlacement.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "PPC.h"
  14. #include "PPCInstrInfo.h"
  15. #include "PPCSubtarget.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/Statistic.h"
  18. #include "llvm/CodeGen/LivePhysRegs.h"
  19. #include "llvm/CodeGen/MachineBasicBlock.h"
  20. #include "llvm/CodeGen/MachineFunctionPass.h"
  21. #include "llvm/CodeGen/MachineInstrBuilder.h"
  22. #include "llvm/CodeGen/MachineRegisterInfo.h"
  23. #include "llvm/MC/MCContext.h"
  24. #include "llvm/Support/CommandLine.h"
  25. #include "llvm/Support/Debug.h"
  26. using namespace llvm;
  27. #define DEBUG_TYPE "ppc-pre-emit-peephole"
  28. STATISTIC(NumRRConvertedInPreEmit,
  29. "Number of r+r instructions converted to r+i in pre-emit peephole");
  30. STATISTIC(NumRemovedInPreEmit,
  31. "Number of instructions deleted in pre-emit peephole");
  32. STATISTIC(NumberOfSelfCopies,
  33. "Number of self copy instructions eliminated");
  34. STATISTIC(NumFrameOffFoldInPreEmit,
  35. "Number of folding frame offset by using r+r in pre-emit peephole");
  36. static cl::opt<bool>
  37. EnablePCRelLinkerOpt("ppc-pcrel-linker-opt", cl::Hidden, cl::init(true),
  38. cl::desc("enable PC Relative linker optimization"));
  39. static cl::opt<bool>
  40. RunPreEmitPeephole("ppc-late-peephole", cl::Hidden, cl::init(true),
  41. cl::desc("Run pre-emit peephole optimizations."));
  42. namespace {
  43. static bool hasPCRelativeForm(MachineInstr &Use) {
  44. switch (Use.getOpcode()) {
  45. default:
  46. return false;
  47. case PPC::LBZ:
  48. case PPC::LBZ8:
  49. case PPC::LHA:
  50. case PPC::LHA8:
  51. case PPC::LHZ:
  52. case PPC::LHZ8:
  53. case PPC::LWZ:
  54. case PPC::LWZ8:
  55. case PPC::STB:
  56. case PPC::STB8:
  57. case PPC::STH:
  58. case PPC::STH8:
  59. case PPC::STW:
  60. case PPC::STW8:
  61. case PPC::LD:
  62. case PPC::STD:
  63. case PPC::LWA:
  64. case PPC::LXSD:
  65. case PPC::LXSSP:
  66. case PPC::LXV:
  67. case PPC::STXSD:
  68. case PPC::STXSSP:
  69. case PPC::STXV:
  70. case PPC::LFD:
  71. case PPC::LFS:
  72. case PPC::STFD:
  73. case PPC::STFS:
  74. case PPC::DFLOADf32:
  75. case PPC::DFLOADf64:
  76. case PPC::DFSTOREf32:
  77. case PPC::DFSTOREf64:
  78. return true;
  79. }
  80. }
  81. class PPCPreEmitPeephole : public MachineFunctionPass {
  82. public:
  83. static char ID;
  84. PPCPreEmitPeephole() : MachineFunctionPass(ID) {
  85. initializePPCPreEmitPeepholePass(*PassRegistry::getPassRegistry());
  86. }
  87. void getAnalysisUsage(AnalysisUsage &AU) const override {
  88. MachineFunctionPass::getAnalysisUsage(AU);
  89. }
  90. MachineFunctionProperties getRequiredProperties() const override {
  91. return MachineFunctionProperties().set(
  92. MachineFunctionProperties::Property::NoVRegs);
  93. }
  94. // This function removes any redundant load immediates. It has two level
  95. // loops - The outer loop finds the load immediates BBI that could be used
  96. // to replace following redundancy. The inner loop scans instructions that
  97. // after BBI to find redundancy and update kill/dead flags accordingly. If
  98. // AfterBBI is the same as BBI, it is redundant, otherwise any instructions
  99. // that modify the def register of BBI would break the scanning.
  100. // DeadOrKillToUnset is a pointer to the previous operand that had the
  101. // kill/dead flag set. It keeps track of the def register of BBI, the use
  102. // registers of AfterBBIs and the def registers of AfterBBIs.
  103. bool removeRedundantLIs(MachineBasicBlock &MBB,
  104. const TargetRegisterInfo *TRI) {
  105. LLVM_DEBUG(dbgs() << "Remove redundant load immediates from MBB:\n";
  106. MBB.dump(); dbgs() << "\n");
  107. DenseSet<MachineInstr *> InstrsToErase;
  108. for (auto BBI = MBB.instr_begin(); BBI != MBB.instr_end(); ++BBI) {
  109. // Skip load immediate that is marked to be erased later because it
  110. // cannot be used to replace any other instructions.
  111. if (InstrsToErase.contains(&*BBI))
  112. continue;
  113. // Skip non-load immediate.
  114. unsigned Opc = BBI->getOpcode();
  115. if (Opc != PPC::LI && Opc != PPC::LI8 && Opc != PPC::LIS &&
  116. Opc != PPC::LIS8)
  117. continue;
  118. // Skip load immediate, where the operand is a relocation (e.g., $r3 =
  119. // LI target-flags(ppc-lo) %const.0).
  120. if (!BBI->getOperand(1).isImm())
  121. continue;
  122. assert(BBI->getOperand(0).isReg() &&
  123. "Expected a register for the first operand");
  124. LLVM_DEBUG(dbgs() << "Scanning after load immediate: "; BBI->dump(););
  125. Register Reg = BBI->getOperand(0).getReg();
  126. int64_t Imm = BBI->getOperand(1).getImm();
  127. MachineOperand *DeadOrKillToUnset = nullptr;
  128. if (BBI->getOperand(0).isDead()) {
  129. DeadOrKillToUnset = &BBI->getOperand(0);
  130. LLVM_DEBUG(dbgs() << " Kill flag of " << *DeadOrKillToUnset
  131. << " from load immediate " << *BBI
  132. << " is a unsetting candidate\n");
  133. }
  134. // This loop scans instructions after BBI to see if there is any
  135. // redundant load immediate.
  136. for (auto AfterBBI = std::next(BBI); AfterBBI != MBB.instr_end();
  137. ++AfterBBI) {
  138. // Track the operand that kill Reg. We would unset the kill flag of
  139. // the operand if there is a following redundant load immediate.
  140. int KillIdx = AfterBBI->findRegisterUseOperandIdx(Reg, true, TRI);
  141. // We can't just clear implicit kills, so if we encounter one, stop
  142. // looking further.
  143. if (KillIdx != -1 && AfterBBI->getOperand(KillIdx).isImplicit()) {
  144. LLVM_DEBUG(dbgs()
  145. << "Encountered an implicit kill, cannot proceed: ");
  146. LLVM_DEBUG(AfterBBI->dump());
  147. break;
  148. }
  149. if (KillIdx != -1) {
  150. assert(!DeadOrKillToUnset && "Shouldn't kill same register twice");
  151. DeadOrKillToUnset = &AfterBBI->getOperand(KillIdx);
  152. LLVM_DEBUG(dbgs()
  153. << " Kill flag of " << *DeadOrKillToUnset << " from "
  154. << *AfterBBI << " is a unsetting candidate\n");
  155. }
  156. if (!AfterBBI->modifiesRegister(Reg, TRI))
  157. continue;
  158. // Finish scanning because Reg is overwritten by a non-load
  159. // instruction.
  160. if (AfterBBI->getOpcode() != Opc)
  161. break;
  162. assert(AfterBBI->getOperand(0).isReg() &&
  163. "Expected a register for the first operand");
  164. // Finish scanning because Reg is overwritten by a relocation or a
  165. // different value.
  166. if (!AfterBBI->getOperand(1).isImm() ||
  167. AfterBBI->getOperand(1).getImm() != Imm)
  168. break;
  169. // It loads same immediate value to the same Reg, which is redundant.
  170. // We would unset kill flag in previous Reg usage to extend live range
  171. // of Reg first, then remove the redundancy.
  172. if (DeadOrKillToUnset) {
  173. LLVM_DEBUG(dbgs()
  174. << " Unset dead/kill flag of " << *DeadOrKillToUnset
  175. << " from " << *DeadOrKillToUnset->getParent());
  176. if (DeadOrKillToUnset->isDef())
  177. DeadOrKillToUnset->setIsDead(false);
  178. else
  179. DeadOrKillToUnset->setIsKill(false);
  180. }
  181. DeadOrKillToUnset =
  182. AfterBBI->findRegisterDefOperand(Reg, true, true, TRI);
  183. if (DeadOrKillToUnset)
  184. LLVM_DEBUG(dbgs()
  185. << " Dead flag of " << *DeadOrKillToUnset << " from "
  186. << *AfterBBI << " is a unsetting candidate\n");
  187. InstrsToErase.insert(&*AfterBBI);
  188. LLVM_DEBUG(dbgs() << " Remove redundant load immediate: ";
  189. AfterBBI->dump());
  190. }
  191. }
  192. for (MachineInstr *MI : InstrsToErase) {
  193. MI->eraseFromParent();
  194. }
  195. NumRemovedInPreEmit += InstrsToErase.size();
  196. return !InstrsToErase.empty();
  197. }
  198. // Check if this instruction is a PLDpc that is part of a GOT indirect
  199. // access.
  200. bool isGOTPLDpc(MachineInstr &Instr) {
  201. if (Instr.getOpcode() != PPC::PLDpc)
  202. return false;
  203. // The result must be a register.
  204. const MachineOperand &LoadedAddressReg = Instr.getOperand(0);
  205. if (!LoadedAddressReg.isReg())
  206. return false;
  207. // Make sure that this is a global symbol.
  208. const MachineOperand &SymbolOp = Instr.getOperand(1);
  209. if (!SymbolOp.isGlobal())
  210. return false;
  211. // Finally return true only if the GOT flag is present.
  212. return (SymbolOp.getTargetFlags() & PPCII::MO_GOT_FLAG);
  213. }
  214. bool addLinkerOpt(MachineBasicBlock &MBB, const TargetRegisterInfo *TRI) {
  215. MachineFunction *MF = MBB.getParent();
  216. // If the linker opt is disabled then just return.
  217. if (!EnablePCRelLinkerOpt)
  218. return false;
  219. // Add this linker opt only if we are using PC Relative memops.
  220. if (!MF->getSubtarget<PPCSubtarget>().isUsingPCRelativeCalls())
  221. return false;
  222. // Struct to keep track of one def/use pair for a GOT indirect access.
  223. struct GOTDefUsePair {
  224. MachineBasicBlock::iterator DefInst;
  225. MachineBasicBlock::iterator UseInst;
  226. Register DefReg;
  227. Register UseReg;
  228. bool StillValid;
  229. };
  230. // Vector of def/ues pairs in this basic block.
  231. SmallVector<GOTDefUsePair, 4> CandPairs;
  232. SmallVector<GOTDefUsePair, 4> ValidPairs;
  233. bool MadeChange = false;
  234. // Run through all of the instructions in the basic block and try to
  235. // collect potential pairs of GOT indirect access instructions.
  236. for (auto BBI = MBB.instr_begin(); BBI != MBB.instr_end(); ++BBI) {
  237. // Look for the initial GOT indirect load.
  238. if (isGOTPLDpc(*BBI)) {
  239. GOTDefUsePair CurrentPair{BBI, MachineBasicBlock::iterator(),
  240. BBI->getOperand(0).getReg(),
  241. PPC::NoRegister, true};
  242. CandPairs.push_back(CurrentPair);
  243. continue;
  244. }
  245. // We haven't encountered any new PLD instructions, nothing to check.
  246. if (CandPairs.empty())
  247. continue;
  248. // Run through the candidate pairs and see if any of the registers
  249. // defined in the PLD instructions are used by this instruction.
  250. // Note: the size of CandPairs can change in the loop.
  251. for (unsigned Idx = 0; Idx < CandPairs.size(); Idx++) {
  252. GOTDefUsePair &Pair = CandPairs[Idx];
  253. // The instruction does not use or modify this PLD's def reg,
  254. // ignore it.
  255. if (!BBI->readsRegister(Pair.DefReg, TRI) &&
  256. !BBI->modifiesRegister(Pair.DefReg, TRI))
  257. continue;
  258. // The use needs to be used in the address compuation and not
  259. // as the register being stored for a store.
  260. const MachineOperand *UseOp =
  261. hasPCRelativeForm(*BBI) ? &BBI->getOperand(2) : nullptr;
  262. // Check for a valid use.
  263. if (UseOp && UseOp->isReg() && UseOp->getReg() == Pair.DefReg &&
  264. UseOp->isUse() && UseOp->isKill()) {
  265. Pair.UseInst = BBI;
  266. Pair.UseReg = BBI->getOperand(0).getReg();
  267. ValidPairs.push_back(Pair);
  268. }
  269. CandPairs.erase(CandPairs.begin() + Idx);
  270. }
  271. }
  272. // Go through all of the pairs and check for any more valid uses.
  273. for (auto Pair = ValidPairs.begin(); Pair != ValidPairs.end(); Pair++) {
  274. // We shouldn't be here if we don't have a valid pair.
  275. assert(Pair->UseInst.isValid() && Pair->StillValid &&
  276. "Kept an invalid def/use pair for GOT PCRel opt");
  277. // We have found a potential pair. Search through the instructions
  278. // between the def and the use to see if it is valid to mark this as a
  279. // linker opt.
  280. MachineBasicBlock::iterator BBI = Pair->DefInst;
  281. ++BBI;
  282. for (; BBI != Pair->UseInst; ++BBI) {
  283. if (BBI->readsRegister(Pair->UseReg, TRI) ||
  284. BBI->modifiesRegister(Pair->UseReg, TRI)) {
  285. Pair->StillValid = false;
  286. break;
  287. }
  288. }
  289. if (!Pair->StillValid)
  290. continue;
  291. // The load/store instruction that uses the address from the PLD will
  292. // either use a register (for a store) or define a register (for the
  293. // load). That register will be added as an implicit def to the PLD
  294. // and as an implicit use on the second memory op. This is a precaution
  295. // to prevent future passes from using that register between the two
  296. // instructions.
  297. MachineOperand ImplDef =
  298. MachineOperand::CreateReg(Pair->UseReg, true, true);
  299. MachineOperand ImplUse =
  300. MachineOperand::CreateReg(Pair->UseReg, false, true);
  301. Pair->DefInst->addOperand(ImplDef);
  302. Pair->UseInst->addOperand(ImplUse);
  303. // Create the symbol.
  304. MCContext &Context = MF->getContext();
  305. MCSymbol *Symbol = Context.createNamedTempSymbol("pcrel");
  306. MachineOperand PCRelLabel =
  307. MachineOperand::CreateMCSymbol(Symbol, PPCII::MO_PCREL_OPT_FLAG);
  308. Pair->DefInst->addOperand(*MF, PCRelLabel);
  309. Pair->UseInst->addOperand(*MF, PCRelLabel);
  310. MadeChange |= true;
  311. }
  312. return MadeChange;
  313. }
  314. // This function removes redundant pairs of accumulator prime/unprime
  315. // instructions. In some situations, it's possible the compiler inserts an
  316. // accumulator prime instruction followed by an unprime instruction (e.g.
  317. // when we store an accumulator after restoring it from a spill). If the
  318. // accumulator is not used between the two, they can be removed. This
  319. // function removes these redundant pairs from basic blocks.
  320. // The algorithm is quite straightforward - every time we encounter a prime
  321. // instruction, the primed register is added to a candidate set. Any use
  322. // other than a prime removes the candidate from the set and any de-prime
  323. // of a current candidate marks both the prime and de-prime for removal.
  324. // This way we ensure we only remove prime/de-prime *pairs* with no
  325. // intervening uses.
  326. bool removeAccPrimeUnprime(MachineBasicBlock &MBB) {
  327. DenseSet<MachineInstr *> InstrsToErase;
  328. // Initially, none of the acc registers are candidates.
  329. SmallVector<MachineInstr *, 8> Candidates(
  330. PPC::UACCRCRegClass.getNumRegs(), nullptr);
  331. for (MachineInstr &BBI : MBB.instrs()) {
  332. unsigned Opc = BBI.getOpcode();
  333. // If we are visiting a xxmtacc instruction, we add it and its operand
  334. // register to the candidate set.
  335. if (Opc == PPC::XXMTACC) {
  336. Register Acc = BBI.getOperand(0).getReg();
  337. assert(PPC::ACCRCRegClass.contains(Acc) &&
  338. "Unexpected register for XXMTACC");
  339. Candidates[Acc - PPC::ACC0] = &BBI;
  340. }
  341. // If we are visiting a xxmfacc instruction and its operand register is
  342. // in the candidate set, we mark the two instructions for removal.
  343. else if (Opc == PPC::XXMFACC) {
  344. Register Acc = BBI.getOperand(0).getReg();
  345. assert(PPC::ACCRCRegClass.contains(Acc) &&
  346. "Unexpected register for XXMFACC");
  347. if (!Candidates[Acc - PPC::ACC0])
  348. continue;
  349. InstrsToErase.insert(&BBI);
  350. InstrsToErase.insert(Candidates[Acc - PPC::ACC0]);
  351. }
  352. // If we are visiting an instruction using an accumulator register
  353. // as operand, we remove it from the candidate set.
  354. else {
  355. for (MachineOperand &Operand : BBI.operands()) {
  356. if (!Operand.isReg())
  357. continue;
  358. Register Reg = Operand.getReg();
  359. if (PPC::ACCRCRegClass.contains(Reg))
  360. Candidates[Reg - PPC::ACC0] = nullptr;
  361. }
  362. }
  363. }
  364. for (MachineInstr *MI : InstrsToErase)
  365. MI->eraseFromParent();
  366. NumRemovedInPreEmit += InstrsToErase.size();
  367. return !InstrsToErase.empty();
  368. }
  369. bool runOnMachineFunction(MachineFunction &MF) override {
  370. if (skipFunction(MF.getFunction()) || !RunPreEmitPeephole) {
  371. // Remove UNENCODED_NOP even when this pass is disabled.
  372. // This needs to be done unconditionally so we don't emit zeros
  373. // in the instruction stream.
  374. SmallVector<MachineInstr *, 4> InstrsToErase;
  375. for (MachineBasicBlock &MBB : MF)
  376. for (MachineInstr &MI : MBB)
  377. if (MI.getOpcode() == PPC::UNENCODED_NOP)
  378. InstrsToErase.push_back(&MI);
  379. for (MachineInstr *MI : InstrsToErase)
  380. MI->eraseFromParent();
  381. return false;
  382. }
  383. bool Changed = false;
  384. const PPCInstrInfo *TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
  385. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  386. SmallVector<MachineInstr *, 4> InstrsToErase;
  387. for (MachineBasicBlock &MBB : MF) {
  388. Changed |= removeRedundantLIs(MBB, TRI);
  389. Changed |= addLinkerOpt(MBB, TRI);
  390. Changed |= removeAccPrimeUnprime(MBB);
  391. for (MachineInstr &MI : MBB) {
  392. unsigned Opc = MI.getOpcode();
  393. if (Opc == PPC::UNENCODED_NOP) {
  394. InstrsToErase.push_back(&MI);
  395. continue;
  396. }
  397. // Detect self copies - these can result from running AADB.
  398. if (PPCInstrInfo::isSameClassPhysRegCopy(Opc)) {
  399. const MCInstrDesc &MCID = TII->get(Opc);
  400. if (MCID.getNumOperands() == 3 &&
  401. MI.getOperand(0).getReg() == MI.getOperand(1).getReg() &&
  402. MI.getOperand(0).getReg() == MI.getOperand(2).getReg()) {
  403. NumberOfSelfCopies++;
  404. LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: ");
  405. LLVM_DEBUG(MI.dump());
  406. InstrsToErase.push_back(&MI);
  407. continue;
  408. }
  409. else if (MCID.getNumOperands() == 2 &&
  410. MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) {
  411. NumberOfSelfCopies++;
  412. LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: ");
  413. LLVM_DEBUG(MI.dump());
  414. InstrsToErase.push_back(&MI);
  415. continue;
  416. }
  417. }
  418. MachineInstr *DefMIToErase = nullptr;
  419. if (TII->convertToImmediateForm(MI, &DefMIToErase)) {
  420. Changed = true;
  421. NumRRConvertedInPreEmit++;
  422. LLVM_DEBUG(dbgs() << "Converted instruction to imm form: ");
  423. LLVM_DEBUG(MI.dump());
  424. if (DefMIToErase) {
  425. InstrsToErase.push_back(DefMIToErase);
  426. }
  427. }
  428. if (TII->foldFrameOffset(MI)) {
  429. Changed = true;
  430. NumFrameOffFoldInPreEmit++;
  431. LLVM_DEBUG(dbgs() << "Frame offset folding by using index form: ");
  432. LLVM_DEBUG(MI.dump());
  433. }
  434. }
  435. // Eliminate conditional branch based on a constant CR bit by
  436. // CRSET or CRUNSET. We eliminate the conditional branch or
  437. // convert it into an unconditional branch. Also, if the CR bit
  438. // is not used by other instructions, we eliminate CRSET as well.
  439. auto I = MBB.getFirstInstrTerminator();
  440. if (I == MBB.instr_end())
  441. continue;
  442. MachineInstr *Br = &*I;
  443. if (Br->getOpcode() != PPC::BC && Br->getOpcode() != PPC::BCn)
  444. continue;
  445. MachineInstr *CRSetMI = nullptr;
  446. Register CRBit = Br->getOperand(0).getReg();
  447. unsigned CRReg = getCRFromCRBit(CRBit);
  448. bool SeenUse = false;
  449. MachineBasicBlock::reverse_iterator It = Br, Er = MBB.rend();
  450. for (It++; It != Er; It++) {
  451. if (It->modifiesRegister(CRBit, TRI)) {
  452. if ((It->getOpcode() == PPC::CRUNSET ||
  453. It->getOpcode() == PPC::CRSET) &&
  454. It->getOperand(0).getReg() == CRBit)
  455. CRSetMI = &*It;
  456. break;
  457. }
  458. if (It->readsRegister(CRBit, TRI))
  459. SeenUse = true;
  460. }
  461. if (!CRSetMI) continue;
  462. unsigned CRSetOp = CRSetMI->getOpcode();
  463. if ((Br->getOpcode() == PPC::BCn && CRSetOp == PPC::CRSET) ||
  464. (Br->getOpcode() == PPC::BC && CRSetOp == PPC::CRUNSET)) {
  465. // Remove this branch since it cannot be taken.
  466. InstrsToErase.push_back(Br);
  467. MBB.removeSuccessor(Br->getOperand(1).getMBB());
  468. }
  469. else {
  470. // This conditional branch is always taken. So, remove all branches
  471. // and insert an unconditional branch to the destination of this.
  472. MachineBasicBlock::iterator It = Br, Er = MBB.end();
  473. for (; It != Er; It++) {
  474. if (It->isDebugInstr()) continue;
  475. assert(It->isTerminator() && "Non-terminator after a terminator");
  476. InstrsToErase.push_back(&*It);
  477. }
  478. if (!MBB.isLayoutSuccessor(Br->getOperand(1).getMBB())) {
  479. ArrayRef<MachineOperand> NoCond;
  480. TII->insertBranch(MBB, Br->getOperand(1).getMBB(), nullptr,
  481. NoCond, Br->getDebugLoc());
  482. }
  483. for (auto &Succ : MBB.successors())
  484. if (Succ != Br->getOperand(1).getMBB()) {
  485. MBB.removeSuccessor(Succ);
  486. break;
  487. }
  488. }
  489. // If the CRBit is not used by another instruction, we can eliminate
  490. // CRSET/CRUNSET instruction.
  491. if (!SeenUse) {
  492. // We need to check use of the CRBit in successors.
  493. for (auto &SuccMBB : MBB.successors())
  494. if (SuccMBB->isLiveIn(CRBit) || SuccMBB->isLiveIn(CRReg)) {
  495. SeenUse = true;
  496. break;
  497. }
  498. if (!SeenUse)
  499. InstrsToErase.push_back(CRSetMI);
  500. }
  501. }
  502. for (MachineInstr *MI : InstrsToErase) {
  503. LLVM_DEBUG(dbgs() << "PPC pre-emit peephole: erasing instruction: ");
  504. LLVM_DEBUG(MI->dump());
  505. MI->eraseFromParent();
  506. NumRemovedInPreEmit++;
  507. }
  508. return Changed;
  509. }
  510. };
  511. }
  512. INITIALIZE_PASS(PPCPreEmitPeephole, DEBUG_TYPE, "PowerPC Pre-Emit Peephole",
  513. false, false)
  514. char PPCPreEmitPeephole::ID = 0;
  515. FunctionPass *llvm::createPPCPreEmitPeepholePass() {
  516. return new PPCPreEmitPeephole();
  517. }