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"); 38*82205914Sczhengsz STATISTIC(NumFrameOffFoldInPreEmit, 39*82205914Sczhengsz "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); 11241a010a4SYi-Hong Lyu if (KillIdx != -1) { 11341a010a4SYi-Hong Lyu assert(!DeadOrKillToUnset && "Shouldn't kill same register twice"); 11441a010a4SYi-Hong Lyu DeadOrKillToUnset = &AfterBBI->getOperand(KillIdx); 11541a010a4SYi-Hong Lyu LLVM_DEBUG(dbgs() 11641a010a4SYi-Hong Lyu << " Kill flag of " << *DeadOrKillToUnset << " from " 11741a010a4SYi-Hong Lyu << *AfterBBI << " is a unsetting candidate\n"); 11841a010a4SYi-Hong Lyu } 11941a010a4SYi-Hong Lyu 12041a010a4SYi-Hong Lyu if (!AfterBBI->modifiesRegister(Reg, TRI)) 12141a010a4SYi-Hong Lyu continue; 12241a010a4SYi-Hong Lyu // Finish scanning because Reg is overwritten by a non-load 12341a010a4SYi-Hong Lyu // instruction. 12441a010a4SYi-Hong Lyu if (AfterBBI->getOpcode() != Opc) 12541a010a4SYi-Hong Lyu break; 12641a010a4SYi-Hong Lyu assert(AfterBBI->getOperand(0).isReg() && 12741a010a4SYi-Hong Lyu "Expected a register for the first operand"); 12841a010a4SYi-Hong Lyu // Finish scanning because Reg is overwritten by a relocation or a 12941a010a4SYi-Hong Lyu // different value. 13041a010a4SYi-Hong Lyu if (!AfterBBI->getOperand(1).isImm() || 13141a010a4SYi-Hong Lyu AfterBBI->getOperand(1).getImm() != Imm) 13241a010a4SYi-Hong Lyu break; 13341a010a4SYi-Hong Lyu 13441a010a4SYi-Hong Lyu // It loads same immediate value to the same Reg, which is redundant. 13541a010a4SYi-Hong Lyu // We would unset kill flag in previous Reg usage to extend live range 13641a010a4SYi-Hong Lyu // of Reg first, then remove the redundancy. 1372fbfb04fSYi-Hong Lyu if (DeadOrKillToUnset) { 1382fbfb04fSYi-Hong Lyu LLVM_DEBUG(dbgs() 1392fbfb04fSYi-Hong Lyu << " Unset dead/kill flag of " << *DeadOrKillToUnset 14041a010a4SYi-Hong Lyu << " from " << *DeadOrKillToUnset->getParent()); 14141a010a4SYi-Hong Lyu if (DeadOrKillToUnset->isDef()) 14241a010a4SYi-Hong Lyu DeadOrKillToUnset->setIsDead(false); 14341a010a4SYi-Hong Lyu else 14441a010a4SYi-Hong Lyu DeadOrKillToUnset->setIsKill(false); 1452fbfb04fSYi-Hong Lyu } 14641a010a4SYi-Hong Lyu DeadOrKillToUnset = 14741a010a4SYi-Hong Lyu AfterBBI->findRegisterDefOperand(Reg, true, true, TRI); 14841a010a4SYi-Hong Lyu if (DeadOrKillToUnset) 14941a010a4SYi-Hong Lyu LLVM_DEBUG(dbgs() 15041a010a4SYi-Hong Lyu << " Dead flag of " << *DeadOrKillToUnset << " from " 15141a010a4SYi-Hong Lyu << *AfterBBI << " is a unsetting candidate\n"); 15241a010a4SYi-Hong Lyu InstrsToErase.insert(&*AfterBBI); 15341a010a4SYi-Hong Lyu LLVM_DEBUG(dbgs() << " Remove redundant load immediate: "; 15441a010a4SYi-Hong Lyu AfterBBI->dump()); 15541a010a4SYi-Hong Lyu } 15641a010a4SYi-Hong Lyu } 15741a010a4SYi-Hong Lyu 15841a010a4SYi-Hong Lyu for (MachineInstr *MI : InstrsToErase) { 15941a010a4SYi-Hong Lyu MI->eraseFromParent(); 16041a010a4SYi-Hong Lyu } 16141a010a4SYi-Hong Lyu NumRemovedInPreEmit += InstrsToErase.size(); 16241a010a4SYi-Hong Lyu return !InstrsToErase.empty(); 16341a010a4SYi-Hong Lyu } 16441a010a4SYi-Hong Lyu 1656995e5daSNemanja Ivanovic bool runOnMachineFunction(MachineFunction &MF) override { 166f1caa283SMatthias Braun if (skipFunction(MF.getFunction()) || !RunPreEmitPeephole) 1676995e5daSNemanja Ivanovic return false; 1686995e5daSNemanja Ivanovic bool Changed = false; 1696995e5daSNemanja Ivanovic const PPCInstrInfo *TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo(); 17020982f09SHiroshi Inoue const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 1716995e5daSNemanja Ivanovic SmallVector<MachineInstr *, 4> InstrsToErase; 1726995e5daSNemanja Ivanovic for (MachineBasicBlock &MBB : MF) { 17341a010a4SYi-Hong Lyu Changed |= removeRedundantLIs(MBB, TRI); 1746995e5daSNemanja Ivanovic for (MachineInstr &MI : MBB) { 1754c0b110eSNemanja Ivanovic unsigned Opc = MI.getOpcode(); 1764c0b110eSNemanja Ivanovic // Detect self copies - these can result from running AADB. 1774c0b110eSNemanja Ivanovic if (PPCInstrInfo::isSameClassPhysRegCopy(Opc)) { 1784c0b110eSNemanja Ivanovic const MCInstrDesc &MCID = TII->get(Opc); 1794c0b110eSNemanja Ivanovic if (MCID.getNumOperands() == 3 && 1804c0b110eSNemanja Ivanovic MI.getOperand(0).getReg() == MI.getOperand(1).getReg() && 1814c0b110eSNemanja Ivanovic MI.getOperand(0).getReg() == MI.getOperand(2).getReg()) { 1824c0b110eSNemanja Ivanovic NumberOfSelfCopies++; 1834c0b110eSNemanja Ivanovic LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: "); 1844c0b110eSNemanja Ivanovic LLVM_DEBUG(MI.dump()); 1854c0b110eSNemanja Ivanovic InstrsToErase.push_back(&MI); 1864c0b110eSNemanja Ivanovic continue; 1874c0b110eSNemanja Ivanovic } 1884c0b110eSNemanja Ivanovic else if (MCID.getNumOperands() == 2 && 1894c0b110eSNemanja Ivanovic MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) { 1904c0b110eSNemanja Ivanovic NumberOfSelfCopies++; 1914c0b110eSNemanja Ivanovic LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: "); 1924c0b110eSNemanja Ivanovic LLVM_DEBUG(MI.dump()); 1934c0b110eSNemanja Ivanovic InstrsToErase.push_back(&MI); 1944c0b110eSNemanja Ivanovic continue; 1954c0b110eSNemanja Ivanovic } 1964c0b110eSNemanja Ivanovic } 1976995e5daSNemanja Ivanovic MachineInstr *DefMIToErase = nullptr; 1986995e5daSNemanja Ivanovic if (TII->convertToImmediateForm(MI, &DefMIToErase)) { 1996995e5daSNemanja Ivanovic Changed = true; 2006995e5daSNemanja Ivanovic NumRRConvertedInPreEmit++; 201d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Converted instruction to imm form: "); 202d34e60caSNicola Zaghen LLVM_DEBUG(MI.dump()); 2036995e5daSNemanja Ivanovic if (DefMIToErase) { 2046995e5daSNemanja Ivanovic InstrsToErase.push_back(DefMIToErase); 2056995e5daSNemanja Ivanovic } 2066995e5daSNemanja Ivanovic } 207*82205914Sczhengsz if (TII->foldFrameOffset(MI)) { 208*82205914Sczhengsz Changed = true; 209*82205914Sczhengsz NumFrameOffFoldInPreEmit++; 210*82205914Sczhengsz LLVM_DEBUG(dbgs() << "Frame offset folding by using index form: "); 211*82205914Sczhengsz LLVM_DEBUG(MI.dump()); 212*82205914Sczhengsz } 2136995e5daSNemanja Ivanovic } 21420982f09SHiroshi Inoue 21520982f09SHiroshi Inoue // Eliminate conditional branch based on a constant CR bit by 21620982f09SHiroshi Inoue // CRSET or CRUNSET. We eliminate the conditional branch or 21720982f09SHiroshi Inoue // convert it into an unconditional branch. Also, if the CR bit 21820982f09SHiroshi Inoue // is not used by other instructions, we eliminate CRSET as well. 21920982f09SHiroshi Inoue auto I = MBB.getFirstInstrTerminator(); 22020982f09SHiroshi Inoue if (I == MBB.instr_end()) 22120982f09SHiroshi Inoue continue; 22220982f09SHiroshi Inoue MachineInstr *Br = &*I; 22320982f09SHiroshi Inoue if (Br->getOpcode() != PPC::BC && Br->getOpcode() != PPC::BCn) 22420982f09SHiroshi Inoue continue; 22520982f09SHiroshi Inoue MachineInstr *CRSetMI = nullptr; 2260c476111SDaniel Sanders Register CRBit = Br->getOperand(0).getReg(); 22720982f09SHiroshi Inoue unsigned CRReg = getCRFromCRBit(CRBit); 22820982f09SHiroshi Inoue bool SeenUse = false; 22920982f09SHiroshi Inoue MachineBasicBlock::reverse_iterator It = Br, Er = MBB.rend(); 23020982f09SHiroshi Inoue for (It++; It != Er; It++) { 23120982f09SHiroshi Inoue if (It->modifiesRegister(CRBit, TRI)) { 23220982f09SHiroshi Inoue if ((It->getOpcode() == PPC::CRUNSET || 23320982f09SHiroshi Inoue It->getOpcode() == PPC::CRSET) && 23420982f09SHiroshi Inoue It->getOperand(0).getReg() == CRBit) 23520982f09SHiroshi Inoue CRSetMI = &*It; 23620982f09SHiroshi Inoue break; 23720982f09SHiroshi Inoue } 23820982f09SHiroshi Inoue if (It->readsRegister(CRBit, TRI)) 23920982f09SHiroshi Inoue SeenUse = true; 24020982f09SHiroshi Inoue } 24120982f09SHiroshi Inoue if (!CRSetMI) continue; 24220982f09SHiroshi Inoue 24320982f09SHiroshi Inoue unsigned CRSetOp = CRSetMI->getOpcode(); 24420982f09SHiroshi Inoue if ((Br->getOpcode() == PPC::BCn && CRSetOp == PPC::CRSET) || 24520982f09SHiroshi Inoue (Br->getOpcode() == PPC::BC && CRSetOp == PPC::CRUNSET)) { 24620982f09SHiroshi Inoue // Remove this branch since it cannot be taken. 24720982f09SHiroshi Inoue InstrsToErase.push_back(Br); 24820982f09SHiroshi Inoue MBB.removeSuccessor(Br->getOperand(1).getMBB()); 24920982f09SHiroshi Inoue } 25020982f09SHiroshi Inoue else { 25120982f09SHiroshi Inoue // This conditional branch is always taken. So, remove all branches 25220982f09SHiroshi Inoue // and insert an unconditional branch to the destination of this. 25320982f09SHiroshi Inoue MachineBasicBlock::iterator It = Br, Er = MBB.end(); 254ecc89b76SWei Mi for (; It != Er; It++) { 25520982f09SHiroshi Inoue if (It->isDebugInstr()) continue; 25620982f09SHiroshi Inoue assert(It->isTerminator() && "Non-terminator after a terminator"); 25720982f09SHiroshi Inoue InstrsToErase.push_back(&*It); 25820982f09SHiroshi Inoue } 25920982f09SHiroshi Inoue if (!MBB.isLayoutSuccessor(Br->getOperand(1).getMBB())) { 26020982f09SHiroshi Inoue ArrayRef<MachineOperand> NoCond; 26120982f09SHiroshi Inoue TII->insertBranch(MBB, Br->getOperand(1).getMBB(), nullptr, 26220982f09SHiroshi Inoue NoCond, Br->getDebugLoc()); 26320982f09SHiroshi Inoue } 26420982f09SHiroshi Inoue for (auto &Succ : MBB.successors()) 26520982f09SHiroshi Inoue if (Succ != Br->getOperand(1).getMBB()) { 26620982f09SHiroshi Inoue MBB.removeSuccessor(Succ); 26720982f09SHiroshi Inoue break; 26820982f09SHiroshi Inoue } 26920982f09SHiroshi Inoue } 27020982f09SHiroshi Inoue 27120982f09SHiroshi Inoue // If the CRBit is not used by another instruction, we can eliminate 27220982f09SHiroshi Inoue // CRSET/CRUNSET instruction. 27320982f09SHiroshi Inoue if (!SeenUse) { 27420982f09SHiroshi Inoue // We need to check use of the CRBit in successors. 27520982f09SHiroshi Inoue for (auto &SuccMBB : MBB.successors()) 27620982f09SHiroshi Inoue if (SuccMBB->isLiveIn(CRBit) || SuccMBB->isLiveIn(CRReg)) { 27720982f09SHiroshi Inoue SeenUse = true; 27820982f09SHiroshi Inoue break; 27920982f09SHiroshi Inoue } 28020982f09SHiroshi Inoue if (!SeenUse) 28120982f09SHiroshi Inoue InstrsToErase.push_back(CRSetMI); 28220982f09SHiroshi Inoue } 2836995e5daSNemanja Ivanovic } 2846995e5daSNemanja Ivanovic for (MachineInstr *MI : InstrsToErase) { 285d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "PPC pre-emit peephole: erasing instruction: "); 286d34e60caSNicola Zaghen LLVM_DEBUG(MI->dump()); 2876995e5daSNemanja Ivanovic MI->eraseFromParent(); 2886995e5daSNemanja Ivanovic NumRemovedInPreEmit++; 2896995e5daSNemanja Ivanovic } 2906995e5daSNemanja Ivanovic return Changed; 2916995e5daSNemanja Ivanovic } 2926995e5daSNemanja Ivanovic }; 2936995e5daSNemanja Ivanovic } 2946995e5daSNemanja Ivanovic 2956995e5daSNemanja Ivanovic INITIALIZE_PASS(PPCPreEmitPeephole, DEBUG_TYPE, "PowerPC Pre-Emit Peephole", 2966995e5daSNemanja Ivanovic false, false) 2976995e5daSNemanja Ivanovic char PPCPreEmitPeephole::ID = 0; 2986995e5daSNemanja Ivanovic 2996995e5daSNemanja Ivanovic FunctionPass *llvm::createPPCPreEmitPeepholePass() { 3006995e5daSNemanja Ivanovic return new PPCPreEmitPeephole(); 3016995e5daSNemanja Ivanovic } 302