16995e5daSNemanja Ivanovic //===--------- PPCPreEmitPeephole.cpp - Late peephole optimizations -------===//
26995e5daSNemanja Ivanovic //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66995e5daSNemanja Ivanovic //
76995e5daSNemanja Ivanovic //===----------------------------------------------------------------------===//
86995e5daSNemanja Ivanovic //
96995e5daSNemanja Ivanovic // A pre-emit peephole for catching opportunities introduced by late passes such
106995e5daSNemanja Ivanovic // as MachineBlockPlacement.
116995e5daSNemanja Ivanovic //
126995e5daSNemanja Ivanovic //===----------------------------------------------------------------------===//
136995e5daSNemanja Ivanovic 
146995e5daSNemanja Ivanovic #include "PPC.h"
156995e5daSNemanja Ivanovic #include "PPCInstrInfo.h"
166995e5daSNemanja Ivanovic #include "PPCSubtarget.h"
176995e5daSNemanja Ivanovic #include "llvm/ADT/DenseMap.h"
186995e5daSNemanja Ivanovic #include "llvm/ADT/Statistic.h"
196995e5daSNemanja Ivanovic #include "llvm/CodeGen/LivePhysRegs.h"
2020982f09SHiroshi Inoue #include "llvm/CodeGen/MachineBasicBlock.h"
216995e5daSNemanja Ivanovic #include "llvm/CodeGen/MachineFunctionPass.h"
226995e5daSNemanja Ivanovic #include "llvm/CodeGen/MachineInstrBuilder.h"
236995e5daSNemanja Ivanovic #include "llvm/CodeGen/MachineRegisterInfo.h"
246995e5daSNemanja Ivanovic #include "llvm/Support/CommandLine.h"
256995e5daSNemanja Ivanovic #include "llvm/ADT/Statistic.h"
266995e5daSNemanja Ivanovic #include "llvm/Support/Debug.h"
276995e5daSNemanja Ivanovic 
286995e5daSNemanja Ivanovic using namespace llvm;
296995e5daSNemanja Ivanovic 
306995e5daSNemanja Ivanovic #define DEBUG_TYPE "ppc-pre-emit-peephole"
316995e5daSNemanja Ivanovic 
326995e5daSNemanja Ivanovic STATISTIC(NumRRConvertedInPreEmit,
336995e5daSNemanja Ivanovic           "Number of r+r instructions converted to r+i in pre-emit peephole");
346995e5daSNemanja Ivanovic STATISTIC(NumRemovedInPreEmit,
356995e5daSNemanja Ivanovic           "Number of instructions deleted in pre-emit peephole");
364c0b110eSNemanja Ivanovic STATISTIC(NumberOfSelfCopies,
374c0b110eSNemanja Ivanovic           "Number of self copy instructions eliminated");
3882205914Sczhengsz STATISTIC(NumFrameOffFoldInPreEmit,
3982205914Sczhengsz           "Number of folding frame offset by using r+r in pre-emit peephole");
406995e5daSNemanja Ivanovic 
416995e5daSNemanja Ivanovic static cl::opt<bool>
424e1f5e07SNemanja Ivanovic RunPreEmitPeephole("ppc-late-peephole", cl::Hidden, cl::init(true),
436995e5daSNemanja Ivanovic                    cl::desc("Run pre-emit peephole optimizations."));
446995e5daSNemanja Ivanovic 
456995e5daSNemanja Ivanovic namespace {
466995e5daSNemanja Ivanovic   class PPCPreEmitPeephole : public MachineFunctionPass {
476995e5daSNemanja Ivanovic   public:
486995e5daSNemanja Ivanovic     static char ID;
496995e5daSNemanja Ivanovic     PPCPreEmitPeephole() : MachineFunctionPass(ID) {
506995e5daSNemanja Ivanovic       initializePPCPreEmitPeepholePass(*PassRegistry::getPassRegistry());
516995e5daSNemanja Ivanovic     }
526995e5daSNemanja Ivanovic 
536995e5daSNemanja Ivanovic     void getAnalysisUsage(AnalysisUsage &AU) const override {
546995e5daSNemanja Ivanovic       MachineFunctionPass::getAnalysisUsage(AU);
556995e5daSNemanja Ivanovic     }
566995e5daSNemanja Ivanovic 
576995e5daSNemanja Ivanovic     MachineFunctionProperties getRequiredProperties() const override {
586995e5daSNemanja Ivanovic       return MachineFunctionProperties().set(
596995e5daSNemanja Ivanovic           MachineFunctionProperties::Property::NoVRegs);
606995e5daSNemanja Ivanovic     }
616995e5daSNemanja Ivanovic 
6241a010a4SYi-Hong Lyu     // This function removes any redundant load immediates. It has two level
6341a010a4SYi-Hong Lyu     // loops - The outer loop finds the load immediates BBI that could be used
6441a010a4SYi-Hong Lyu     // to replace following redundancy. The inner loop scans instructions that
6541a010a4SYi-Hong Lyu     // after BBI to find redundancy and update kill/dead flags accordingly. If
6641a010a4SYi-Hong Lyu     // AfterBBI is the same as BBI, it is redundant, otherwise any instructions
6741a010a4SYi-Hong Lyu     // that modify the def register of BBI would break the scanning.
6841a010a4SYi-Hong Lyu     // DeadOrKillToUnset is a pointer to the previous operand that had the
6941a010a4SYi-Hong Lyu     // kill/dead flag set. It keeps track of the def register of BBI, the use
7041a010a4SYi-Hong Lyu     // registers of AfterBBIs and the def registers of AfterBBIs.
7141a010a4SYi-Hong Lyu     bool removeRedundantLIs(MachineBasicBlock &MBB,
7241a010a4SYi-Hong Lyu                             const TargetRegisterInfo *TRI) {
7341a010a4SYi-Hong Lyu       LLVM_DEBUG(dbgs() << "Remove redundant load immediates from MBB:\n";
7441a010a4SYi-Hong Lyu                  MBB.dump(); dbgs() << "\n");
7541a010a4SYi-Hong Lyu 
7641a010a4SYi-Hong Lyu       DenseSet<MachineInstr *> InstrsToErase;
7741a010a4SYi-Hong Lyu       for (auto BBI = MBB.instr_begin(); BBI != MBB.instr_end(); ++BBI) {
7841a010a4SYi-Hong Lyu         // Skip load immediate that is marked to be erased later because it
7941a010a4SYi-Hong Lyu         // cannot be used to replace any other instructions.
8041a010a4SYi-Hong Lyu         if (InstrsToErase.find(&*BBI) != InstrsToErase.end())
8141a010a4SYi-Hong Lyu           continue;
8241a010a4SYi-Hong Lyu         // Skip non-load immediate.
8341a010a4SYi-Hong Lyu         unsigned Opc = BBI->getOpcode();
8441a010a4SYi-Hong Lyu         if (Opc != PPC::LI && Opc != PPC::LI8 && Opc != PPC::LIS &&
8541a010a4SYi-Hong Lyu             Opc != PPC::LIS8)
8641a010a4SYi-Hong Lyu           continue;
8741a010a4SYi-Hong Lyu         // Skip load immediate, where the operand is a relocation (e.g., $r3 =
8841a010a4SYi-Hong Lyu         // LI target-flags(ppc-lo) %const.0).
8941a010a4SYi-Hong Lyu         if (!BBI->getOperand(1).isImm())
9041a010a4SYi-Hong Lyu           continue;
9141a010a4SYi-Hong Lyu         assert(BBI->getOperand(0).isReg() &&
9241a010a4SYi-Hong Lyu                "Expected a register for the first operand");
9341a010a4SYi-Hong Lyu 
9441a010a4SYi-Hong Lyu         LLVM_DEBUG(dbgs() << "Scanning after load immediate: "; BBI->dump(););
9541a010a4SYi-Hong Lyu 
960c476111SDaniel Sanders         Register Reg = BBI->getOperand(0).getReg();
9741a010a4SYi-Hong Lyu         int64_t Imm = BBI->getOperand(1).getImm();
9841a010a4SYi-Hong Lyu         MachineOperand *DeadOrKillToUnset = nullptr;
9941a010a4SYi-Hong Lyu         if (BBI->getOperand(0).isDead()) {
10041a010a4SYi-Hong Lyu           DeadOrKillToUnset = &BBI->getOperand(0);
10141a010a4SYi-Hong Lyu           LLVM_DEBUG(dbgs() << " Kill flag of " << *DeadOrKillToUnset
10241a010a4SYi-Hong Lyu                             << " from load immediate " << *BBI
10341a010a4SYi-Hong Lyu                             << " is a unsetting candidate\n");
10441a010a4SYi-Hong Lyu         }
10541a010a4SYi-Hong Lyu         // This loop scans instructions after BBI to see if there is any
10641a010a4SYi-Hong Lyu         // redundant load immediate.
10741a010a4SYi-Hong Lyu         for (auto AfterBBI = std::next(BBI); AfterBBI != MBB.instr_end();
10841a010a4SYi-Hong Lyu              ++AfterBBI) {
10941a010a4SYi-Hong Lyu           // Track the operand that kill Reg. We would unset the kill flag of
11041a010a4SYi-Hong Lyu           // the operand if there is a following redundant load immediate.
11141a010a4SYi-Hong Lyu           int KillIdx = AfterBBI->findRegisterUseOperandIdx(Reg, true, TRI);
112*7f3787c0SNemanja Ivanovic 
113*7f3787c0SNemanja Ivanovic           // We can't just clear implicit kills, so if we encounter one, stop
114*7f3787c0SNemanja Ivanovic           // looking further.
115*7f3787c0SNemanja Ivanovic           if (KillIdx != -1 && AfterBBI->getOperand(KillIdx).isImplicit()) {
116*7f3787c0SNemanja Ivanovic             LLVM_DEBUG(dbgs()
117*7f3787c0SNemanja Ivanovic                        << "Encountered an implicit kill, cannot proceed: ");
118*7f3787c0SNemanja Ivanovic             LLVM_DEBUG(AfterBBI->dump());
119*7f3787c0SNemanja Ivanovic             break;
120*7f3787c0SNemanja Ivanovic           }
121*7f3787c0SNemanja Ivanovic 
12241a010a4SYi-Hong Lyu           if (KillIdx != -1) {
12341a010a4SYi-Hong Lyu             assert(!DeadOrKillToUnset && "Shouldn't kill same register twice");
12441a010a4SYi-Hong Lyu             DeadOrKillToUnset = &AfterBBI->getOperand(KillIdx);
12541a010a4SYi-Hong Lyu             LLVM_DEBUG(dbgs()
12641a010a4SYi-Hong Lyu                        << " Kill flag of " << *DeadOrKillToUnset << " from "
12741a010a4SYi-Hong Lyu                        << *AfterBBI << " is a unsetting candidate\n");
12841a010a4SYi-Hong Lyu           }
12941a010a4SYi-Hong Lyu 
13041a010a4SYi-Hong Lyu           if (!AfterBBI->modifiesRegister(Reg, TRI))
13141a010a4SYi-Hong Lyu             continue;
13241a010a4SYi-Hong Lyu           // Finish scanning because Reg is overwritten by a non-load
13341a010a4SYi-Hong Lyu           // instruction.
13441a010a4SYi-Hong Lyu           if (AfterBBI->getOpcode() != Opc)
13541a010a4SYi-Hong Lyu             break;
13641a010a4SYi-Hong Lyu           assert(AfterBBI->getOperand(0).isReg() &&
13741a010a4SYi-Hong Lyu                  "Expected a register for the first operand");
13841a010a4SYi-Hong Lyu           // Finish scanning because Reg is overwritten by a relocation or a
13941a010a4SYi-Hong Lyu           // different value.
14041a010a4SYi-Hong Lyu           if (!AfterBBI->getOperand(1).isImm() ||
14141a010a4SYi-Hong Lyu               AfterBBI->getOperand(1).getImm() != Imm)
14241a010a4SYi-Hong Lyu             break;
14341a010a4SYi-Hong Lyu 
14441a010a4SYi-Hong Lyu           // It loads same immediate value to the same Reg, which is redundant.
14541a010a4SYi-Hong Lyu           // We would unset kill flag in previous Reg usage to extend live range
14641a010a4SYi-Hong Lyu           // of Reg first, then remove the redundancy.
1472fbfb04fSYi-Hong Lyu           if (DeadOrKillToUnset) {
1482fbfb04fSYi-Hong Lyu             LLVM_DEBUG(dbgs()
1492fbfb04fSYi-Hong Lyu                        << " Unset dead/kill flag of " << *DeadOrKillToUnset
15041a010a4SYi-Hong Lyu                        << " from " << *DeadOrKillToUnset->getParent());
15141a010a4SYi-Hong Lyu             if (DeadOrKillToUnset->isDef())
15241a010a4SYi-Hong Lyu               DeadOrKillToUnset->setIsDead(false);
15341a010a4SYi-Hong Lyu             else
15441a010a4SYi-Hong Lyu               DeadOrKillToUnset->setIsKill(false);
1552fbfb04fSYi-Hong Lyu           }
15641a010a4SYi-Hong Lyu           DeadOrKillToUnset =
15741a010a4SYi-Hong Lyu               AfterBBI->findRegisterDefOperand(Reg, true, true, TRI);
15841a010a4SYi-Hong Lyu           if (DeadOrKillToUnset)
15941a010a4SYi-Hong Lyu             LLVM_DEBUG(dbgs()
16041a010a4SYi-Hong Lyu                        << " Dead flag of " << *DeadOrKillToUnset << " from "
16141a010a4SYi-Hong Lyu                        << *AfterBBI << " is a unsetting candidate\n");
16241a010a4SYi-Hong Lyu           InstrsToErase.insert(&*AfterBBI);
16341a010a4SYi-Hong Lyu           LLVM_DEBUG(dbgs() << " Remove redundant load immediate: ";
16441a010a4SYi-Hong Lyu                      AfterBBI->dump());
16541a010a4SYi-Hong Lyu         }
16641a010a4SYi-Hong Lyu       }
16741a010a4SYi-Hong Lyu 
16841a010a4SYi-Hong Lyu       for (MachineInstr *MI : InstrsToErase) {
16941a010a4SYi-Hong Lyu         MI->eraseFromParent();
17041a010a4SYi-Hong Lyu       }
17141a010a4SYi-Hong Lyu       NumRemovedInPreEmit += InstrsToErase.size();
17241a010a4SYi-Hong Lyu       return !InstrsToErase.empty();
17341a010a4SYi-Hong Lyu     }
17441a010a4SYi-Hong Lyu 
1756995e5daSNemanja Ivanovic     bool runOnMachineFunction(MachineFunction &MF) override {
176a3db9c08SYi-Hong Lyu       if (skipFunction(MF.getFunction()) || !RunPreEmitPeephole) {
177a3db9c08SYi-Hong Lyu         // Remove UNENCODED_NOP even when this pass is disabled.
178a3db9c08SYi-Hong Lyu         // This needs to be done unconditionally so we don't emit zeros
179a3db9c08SYi-Hong Lyu         // in the instruction stream.
180a3db9c08SYi-Hong Lyu         SmallVector<MachineInstr *, 4> InstrsToErase;
181a3db9c08SYi-Hong Lyu         for (MachineBasicBlock &MBB : MF)
182a3db9c08SYi-Hong Lyu           for (MachineInstr &MI : MBB)
183a3db9c08SYi-Hong Lyu             if (MI.getOpcode() == PPC::UNENCODED_NOP)
184a3db9c08SYi-Hong Lyu               InstrsToErase.push_back(&MI);
185a3db9c08SYi-Hong Lyu         for (MachineInstr *MI : InstrsToErase)
186a3db9c08SYi-Hong Lyu           MI->eraseFromParent();
1876995e5daSNemanja Ivanovic         return false;
188a3db9c08SYi-Hong Lyu       }
1896995e5daSNemanja Ivanovic       bool Changed = false;
1906995e5daSNemanja Ivanovic       const PPCInstrInfo *TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
19120982f09SHiroshi Inoue       const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1926995e5daSNemanja Ivanovic       SmallVector<MachineInstr *, 4> InstrsToErase;
1936995e5daSNemanja Ivanovic       for (MachineBasicBlock &MBB : MF) {
19441a010a4SYi-Hong Lyu         Changed |= removeRedundantLIs(MBB, TRI);
1956995e5daSNemanja Ivanovic         for (MachineInstr &MI : MBB) {
1964c0b110eSNemanja Ivanovic           unsigned Opc = MI.getOpcode();
197a3db9c08SYi-Hong Lyu           if (Opc == PPC::UNENCODED_NOP) {
198a3db9c08SYi-Hong Lyu             InstrsToErase.push_back(&MI);
199a3db9c08SYi-Hong Lyu             continue;
200a3db9c08SYi-Hong Lyu           }
2014c0b110eSNemanja Ivanovic           // Detect self copies - these can result from running AADB.
2024c0b110eSNemanja Ivanovic           if (PPCInstrInfo::isSameClassPhysRegCopy(Opc)) {
2034c0b110eSNemanja Ivanovic             const MCInstrDesc &MCID = TII->get(Opc);
2044c0b110eSNemanja Ivanovic             if (MCID.getNumOperands() == 3 &&
2054c0b110eSNemanja Ivanovic                 MI.getOperand(0).getReg() == MI.getOperand(1).getReg() &&
2064c0b110eSNemanja Ivanovic                 MI.getOperand(0).getReg() == MI.getOperand(2).getReg()) {
2074c0b110eSNemanja Ivanovic               NumberOfSelfCopies++;
2084c0b110eSNemanja Ivanovic               LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: ");
2094c0b110eSNemanja Ivanovic               LLVM_DEBUG(MI.dump());
2104c0b110eSNemanja Ivanovic               InstrsToErase.push_back(&MI);
2114c0b110eSNemanja Ivanovic               continue;
2124c0b110eSNemanja Ivanovic             }
2134c0b110eSNemanja Ivanovic             else if (MCID.getNumOperands() == 2 &&
2144c0b110eSNemanja Ivanovic                      MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) {
2154c0b110eSNemanja Ivanovic               NumberOfSelfCopies++;
2164c0b110eSNemanja Ivanovic               LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: ");
2174c0b110eSNemanja Ivanovic               LLVM_DEBUG(MI.dump());
2184c0b110eSNemanja Ivanovic               InstrsToErase.push_back(&MI);
2194c0b110eSNemanja Ivanovic               continue;
2204c0b110eSNemanja Ivanovic             }
2214c0b110eSNemanja Ivanovic           }
2226995e5daSNemanja Ivanovic           MachineInstr *DefMIToErase = nullptr;
2236995e5daSNemanja Ivanovic           if (TII->convertToImmediateForm(MI, &DefMIToErase)) {
2246995e5daSNemanja Ivanovic             Changed = true;
2256995e5daSNemanja Ivanovic             NumRRConvertedInPreEmit++;
226d34e60caSNicola Zaghen             LLVM_DEBUG(dbgs() << "Converted instruction to imm form: ");
227d34e60caSNicola Zaghen             LLVM_DEBUG(MI.dump());
2286995e5daSNemanja Ivanovic             if (DefMIToErase) {
2296995e5daSNemanja Ivanovic               InstrsToErase.push_back(DefMIToErase);
2306995e5daSNemanja Ivanovic             }
2316995e5daSNemanja Ivanovic           }
23282205914Sczhengsz           if (TII->foldFrameOffset(MI)) {
23382205914Sczhengsz             Changed = true;
23482205914Sczhengsz             NumFrameOffFoldInPreEmit++;
23582205914Sczhengsz             LLVM_DEBUG(dbgs() << "Frame offset folding by using index form: ");
23682205914Sczhengsz             LLVM_DEBUG(MI.dump());
23782205914Sczhengsz           }
2386995e5daSNemanja Ivanovic         }
23920982f09SHiroshi Inoue 
24020982f09SHiroshi Inoue         // Eliminate conditional branch based on a constant CR bit by
24120982f09SHiroshi Inoue         // CRSET or CRUNSET. We eliminate the conditional branch or
24220982f09SHiroshi Inoue         // convert it into an unconditional branch. Also, if the CR bit
24320982f09SHiroshi Inoue         // is not used by other instructions, we eliminate CRSET as well.
24420982f09SHiroshi Inoue         auto I = MBB.getFirstInstrTerminator();
24520982f09SHiroshi Inoue         if (I == MBB.instr_end())
24620982f09SHiroshi Inoue           continue;
24720982f09SHiroshi Inoue         MachineInstr *Br = &*I;
24820982f09SHiroshi Inoue         if (Br->getOpcode() != PPC::BC && Br->getOpcode() != PPC::BCn)
24920982f09SHiroshi Inoue           continue;
25020982f09SHiroshi Inoue         MachineInstr *CRSetMI = nullptr;
2510c476111SDaniel Sanders         Register CRBit = Br->getOperand(0).getReg();
25220982f09SHiroshi Inoue         unsigned CRReg = getCRFromCRBit(CRBit);
25320982f09SHiroshi Inoue         bool SeenUse = false;
25420982f09SHiroshi Inoue         MachineBasicBlock::reverse_iterator It = Br, Er = MBB.rend();
25520982f09SHiroshi Inoue         for (It++; It != Er; It++) {
25620982f09SHiroshi Inoue           if (It->modifiesRegister(CRBit, TRI)) {
25720982f09SHiroshi Inoue             if ((It->getOpcode() == PPC::CRUNSET ||
25820982f09SHiroshi Inoue                  It->getOpcode() == PPC::CRSET) &&
25920982f09SHiroshi Inoue                 It->getOperand(0).getReg() == CRBit)
26020982f09SHiroshi Inoue               CRSetMI = &*It;
26120982f09SHiroshi Inoue             break;
26220982f09SHiroshi Inoue           }
26320982f09SHiroshi Inoue           if (It->readsRegister(CRBit, TRI))
26420982f09SHiroshi Inoue             SeenUse = true;
26520982f09SHiroshi Inoue         }
26620982f09SHiroshi Inoue         if (!CRSetMI) continue;
26720982f09SHiroshi Inoue 
26820982f09SHiroshi Inoue         unsigned CRSetOp = CRSetMI->getOpcode();
26920982f09SHiroshi Inoue         if ((Br->getOpcode() == PPC::BCn && CRSetOp == PPC::CRSET) ||
27020982f09SHiroshi Inoue             (Br->getOpcode() == PPC::BC  && CRSetOp == PPC::CRUNSET)) {
27120982f09SHiroshi Inoue           // Remove this branch since it cannot be taken.
27220982f09SHiroshi Inoue           InstrsToErase.push_back(Br);
27320982f09SHiroshi Inoue           MBB.removeSuccessor(Br->getOperand(1).getMBB());
27420982f09SHiroshi Inoue         }
27520982f09SHiroshi Inoue         else {
27620982f09SHiroshi Inoue           // This conditional branch is always taken. So, remove all branches
27720982f09SHiroshi Inoue           // and insert an unconditional branch to the destination of this.
27820982f09SHiroshi Inoue           MachineBasicBlock::iterator It = Br, Er = MBB.end();
279ecc89b76SWei Mi           for (; It != Er; It++) {
28020982f09SHiroshi Inoue             if (It->isDebugInstr()) continue;
28120982f09SHiroshi Inoue             assert(It->isTerminator() && "Non-terminator after a terminator");
28220982f09SHiroshi Inoue             InstrsToErase.push_back(&*It);
28320982f09SHiroshi Inoue           }
28420982f09SHiroshi Inoue           if (!MBB.isLayoutSuccessor(Br->getOperand(1).getMBB())) {
28520982f09SHiroshi Inoue             ArrayRef<MachineOperand> NoCond;
28620982f09SHiroshi Inoue             TII->insertBranch(MBB, Br->getOperand(1).getMBB(), nullptr,
28720982f09SHiroshi Inoue                               NoCond, Br->getDebugLoc());
28820982f09SHiroshi Inoue           }
28920982f09SHiroshi Inoue           for (auto &Succ : MBB.successors())
29020982f09SHiroshi Inoue             if (Succ != Br->getOperand(1).getMBB()) {
29120982f09SHiroshi Inoue               MBB.removeSuccessor(Succ);
29220982f09SHiroshi Inoue               break;
29320982f09SHiroshi Inoue             }
29420982f09SHiroshi Inoue         }
29520982f09SHiroshi Inoue 
29620982f09SHiroshi Inoue         // If the CRBit is not used by another instruction, we can eliminate
29720982f09SHiroshi Inoue         // CRSET/CRUNSET instruction.
29820982f09SHiroshi Inoue         if (!SeenUse) {
29920982f09SHiroshi Inoue           // We need to check use of the CRBit in successors.
30020982f09SHiroshi Inoue           for (auto &SuccMBB : MBB.successors())
30120982f09SHiroshi Inoue             if (SuccMBB->isLiveIn(CRBit) || SuccMBB->isLiveIn(CRReg)) {
30220982f09SHiroshi Inoue               SeenUse = true;
30320982f09SHiroshi Inoue               break;
30420982f09SHiroshi Inoue             }
30520982f09SHiroshi Inoue           if (!SeenUse)
30620982f09SHiroshi Inoue             InstrsToErase.push_back(CRSetMI);
30720982f09SHiroshi Inoue         }
3086995e5daSNemanja Ivanovic       }
3096995e5daSNemanja Ivanovic       for (MachineInstr *MI : InstrsToErase) {
310d34e60caSNicola Zaghen         LLVM_DEBUG(dbgs() << "PPC pre-emit peephole: erasing instruction: ");
311d34e60caSNicola Zaghen         LLVM_DEBUG(MI->dump());
3126995e5daSNemanja Ivanovic         MI->eraseFromParent();
3136995e5daSNemanja Ivanovic         NumRemovedInPreEmit++;
3146995e5daSNemanja Ivanovic       }
3156995e5daSNemanja Ivanovic       return Changed;
3166995e5daSNemanja Ivanovic     }
3176995e5daSNemanja Ivanovic   };
3186995e5daSNemanja Ivanovic }
3196995e5daSNemanja Ivanovic 
3206995e5daSNemanja Ivanovic INITIALIZE_PASS(PPCPreEmitPeephole, DEBUG_TYPE, "PowerPC Pre-Emit Peephole",
3216995e5daSNemanja Ivanovic                 false, false)
3226995e5daSNemanja Ivanovic char PPCPreEmitPeephole::ID = 0;
3236995e5daSNemanja Ivanovic 
3246995e5daSNemanja Ivanovic FunctionPass *llvm::createPPCPreEmitPeepholePass() {
3256995e5daSNemanja Ivanovic   return new PPCPreEmitPeephole();
3266995e5daSNemanja Ivanovic }
327