10b57cec5SDimitry Andric //===--------- PPCPreEmitPeephole.cpp - Late peephole optimizations -------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // A pre-emit peephole for catching opportunities introduced by late passes such 100b57cec5SDimitry Andric // as MachineBlockPlacement. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "PPC.h" 150b57cec5SDimitry Andric #include "PPCInstrInfo.h" 160b57cec5SDimitry Andric #include "PPCSubtarget.h" 170b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 180b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/LivePhysRegs.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 240b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 250b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 260b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric using namespace llvm; 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric #define DEBUG_TYPE "ppc-pre-emit-peephole" 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric STATISTIC(NumRRConvertedInPreEmit, 330b57cec5SDimitry Andric "Number of r+r instructions converted to r+i in pre-emit peephole"); 340b57cec5SDimitry Andric STATISTIC(NumRemovedInPreEmit, 350b57cec5SDimitry Andric "Number of instructions deleted in pre-emit peephole"); 360b57cec5SDimitry Andric STATISTIC(NumberOfSelfCopies, 370b57cec5SDimitry Andric "Number of self copy instructions eliminated"); 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric static cl::opt<bool> 400b57cec5SDimitry Andric RunPreEmitPeephole("ppc-late-peephole", cl::Hidden, cl::init(true), 410b57cec5SDimitry Andric cl::desc("Run pre-emit peephole optimizations.")); 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric namespace { 440b57cec5SDimitry Andric class PPCPreEmitPeephole : public MachineFunctionPass { 450b57cec5SDimitry Andric public: 460b57cec5SDimitry Andric static char ID; 470b57cec5SDimitry Andric PPCPreEmitPeephole() : MachineFunctionPass(ID) { 480b57cec5SDimitry Andric initializePPCPreEmitPeepholePass(*PassRegistry::getPassRegistry()); 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 520b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 530b57cec5SDimitry Andric } 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric MachineFunctionProperties getRequiredProperties() const override { 560b57cec5SDimitry Andric return MachineFunctionProperties().set( 570b57cec5SDimitry Andric MachineFunctionProperties::Property::NoVRegs); 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric 60*8bcb0991SDimitry Andric // This function removes any redundant load immediates. It has two level 61*8bcb0991SDimitry Andric // loops - The outer loop finds the load immediates BBI that could be used 62*8bcb0991SDimitry Andric // to replace following redundancy. The inner loop scans instructions that 63*8bcb0991SDimitry Andric // after BBI to find redundancy and update kill/dead flags accordingly. If 64*8bcb0991SDimitry Andric // AfterBBI is the same as BBI, it is redundant, otherwise any instructions 65*8bcb0991SDimitry Andric // that modify the def register of BBI would break the scanning. 66*8bcb0991SDimitry Andric // DeadOrKillToUnset is a pointer to the previous operand that had the 67*8bcb0991SDimitry Andric // kill/dead flag set. It keeps track of the def register of BBI, the use 68*8bcb0991SDimitry Andric // registers of AfterBBIs and the def registers of AfterBBIs. 69*8bcb0991SDimitry Andric bool removeRedundantLIs(MachineBasicBlock &MBB, 70*8bcb0991SDimitry Andric const TargetRegisterInfo *TRI) { 71*8bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Remove redundant load immediates from MBB:\n"; 72*8bcb0991SDimitry Andric MBB.dump(); dbgs() << "\n"); 73*8bcb0991SDimitry Andric 74*8bcb0991SDimitry Andric DenseSet<MachineInstr *> InstrsToErase; 75*8bcb0991SDimitry Andric for (auto BBI = MBB.instr_begin(); BBI != MBB.instr_end(); ++BBI) { 76*8bcb0991SDimitry Andric // Skip load immediate that is marked to be erased later because it 77*8bcb0991SDimitry Andric // cannot be used to replace any other instructions. 78*8bcb0991SDimitry Andric if (InstrsToErase.find(&*BBI) != InstrsToErase.end()) 79*8bcb0991SDimitry Andric continue; 80*8bcb0991SDimitry Andric // Skip non-load immediate. 81*8bcb0991SDimitry Andric unsigned Opc = BBI->getOpcode(); 82*8bcb0991SDimitry Andric if (Opc != PPC::LI && Opc != PPC::LI8 && Opc != PPC::LIS && 83*8bcb0991SDimitry Andric Opc != PPC::LIS8) 84*8bcb0991SDimitry Andric continue; 85*8bcb0991SDimitry Andric // Skip load immediate, where the operand is a relocation (e.g., $r3 = 86*8bcb0991SDimitry Andric // LI target-flags(ppc-lo) %const.0). 87*8bcb0991SDimitry Andric if (!BBI->getOperand(1).isImm()) 88*8bcb0991SDimitry Andric continue; 89*8bcb0991SDimitry Andric assert(BBI->getOperand(0).isReg() && 90*8bcb0991SDimitry Andric "Expected a register for the first operand"); 91*8bcb0991SDimitry Andric 92*8bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Scanning after load immediate: "; BBI->dump();); 93*8bcb0991SDimitry Andric 94*8bcb0991SDimitry Andric Register Reg = BBI->getOperand(0).getReg(); 95*8bcb0991SDimitry Andric int64_t Imm = BBI->getOperand(1).getImm(); 96*8bcb0991SDimitry Andric MachineOperand *DeadOrKillToUnset = nullptr; 97*8bcb0991SDimitry Andric if (BBI->getOperand(0).isDead()) { 98*8bcb0991SDimitry Andric DeadOrKillToUnset = &BBI->getOperand(0); 99*8bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << " Kill flag of " << *DeadOrKillToUnset 100*8bcb0991SDimitry Andric << " from load immediate " << *BBI 101*8bcb0991SDimitry Andric << " is a unsetting candidate\n"); 102*8bcb0991SDimitry Andric } 103*8bcb0991SDimitry Andric // This loop scans instructions after BBI to see if there is any 104*8bcb0991SDimitry Andric // redundant load immediate. 105*8bcb0991SDimitry Andric for (auto AfterBBI = std::next(BBI); AfterBBI != MBB.instr_end(); 106*8bcb0991SDimitry Andric ++AfterBBI) { 107*8bcb0991SDimitry Andric // Track the operand that kill Reg. We would unset the kill flag of 108*8bcb0991SDimitry Andric // the operand if there is a following redundant load immediate. 109*8bcb0991SDimitry Andric int KillIdx = AfterBBI->findRegisterUseOperandIdx(Reg, true, TRI); 110*8bcb0991SDimitry Andric if (KillIdx != -1) { 111*8bcb0991SDimitry Andric assert(!DeadOrKillToUnset && "Shouldn't kill same register twice"); 112*8bcb0991SDimitry Andric DeadOrKillToUnset = &AfterBBI->getOperand(KillIdx); 113*8bcb0991SDimitry Andric LLVM_DEBUG(dbgs() 114*8bcb0991SDimitry Andric << " Kill flag of " << *DeadOrKillToUnset << " from " 115*8bcb0991SDimitry Andric << *AfterBBI << " is a unsetting candidate\n"); 116*8bcb0991SDimitry Andric } 117*8bcb0991SDimitry Andric 118*8bcb0991SDimitry Andric if (!AfterBBI->modifiesRegister(Reg, TRI)) 119*8bcb0991SDimitry Andric continue; 120*8bcb0991SDimitry Andric // Finish scanning because Reg is overwritten by a non-load 121*8bcb0991SDimitry Andric // instruction. 122*8bcb0991SDimitry Andric if (AfterBBI->getOpcode() != Opc) 123*8bcb0991SDimitry Andric break; 124*8bcb0991SDimitry Andric assert(AfterBBI->getOperand(0).isReg() && 125*8bcb0991SDimitry Andric "Expected a register for the first operand"); 126*8bcb0991SDimitry Andric // Finish scanning because Reg is overwritten by a relocation or a 127*8bcb0991SDimitry Andric // different value. 128*8bcb0991SDimitry Andric if (!AfterBBI->getOperand(1).isImm() || 129*8bcb0991SDimitry Andric AfterBBI->getOperand(1).getImm() != Imm) 130*8bcb0991SDimitry Andric break; 131*8bcb0991SDimitry Andric 132*8bcb0991SDimitry Andric // It loads same immediate value to the same Reg, which is redundant. 133*8bcb0991SDimitry Andric // We would unset kill flag in previous Reg usage to extend live range 134*8bcb0991SDimitry Andric // of Reg first, then remove the redundancy. 135*8bcb0991SDimitry Andric if (DeadOrKillToUnset) { 136*8bcb0991SDimitry Andric LLVM_DEBUG(dbgs() 137*8bcb0991SDimitry Andric << " Unset dead/kill flag of " << *DeadOrKillToUnset 138*8bcb0991SDimitry Andric << " from " << *DeadOrKillToUnset->getParent()); 139*8bcb0991SDimitry Andric if (DeadOrKillToUnset->isDef()) 140*8bcb0991SDimitry Andric DeadOrKillToUnset->setIsDead(false); 141*8bcb0991SDimitry Andric else 142*8bcb0991SDimitry Andric DeadOrKillToUnset->setIsKill(false); 143*8bcb0991SDimitry Andric } 144*8bcb0991SDimitry Andric DeadOrKillToUnset = 145*8bcb0991SDimitry Andric AfterBBI->findRegisterDefOperand(Reg, true, true, TRI); 146*8bcb0991SDimitry Andric if (DeadOrKillToUnset) 147*8bcb0991SDimitry Andric LLVM_DEBUG(dbgs() 148*8bcb0991SDimitry Andric << " Dead flag of " << *DeadOrKillToUnset << " from " 149*8bcb0991SDimitry Andric << *AfterBBI << " is a unsetting candidate\n"); 150*8bcb0991SDimitry Andric InstrsToErase.insert(&*AfterBBI); 151*8bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << " Remove redundant load immediate: "; 152*8bcb0991SDimitry Andric AfterBBI->dump()); 153*8bcb0991SDimitry Andric } 154*8bcb0991SDimitry Andric } 155*8bcb0991SDimitry Andric 156*8bcb0991SDimitry Andric for (MachineInstr *MI : InstrsToErase) { 157*8bcb0991SDimitry Andric MI->eraseFromParent(); 158*8bcb0991SDimitry Andric } 159*8bcb0991SDimitry Andric NumRemovedInPreEmit += InstrsToErase.size(); 160*8bcb0991SDimitry Andric return !InstrsToErase.empty(); 161*8bcb0991SDimitry Andric } 162*8bcb0991SDimitry Andric 1630b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override { 1640b57cec5SDimitry Andric if (skipFunction(MF.getFunction()) || !RunPreEmitPeephole) 1650b57cec5SDimitry Andric return false; 1660b57cec5SDimitry Andric bool Changed = false; 1670b57cec5SDimitry Andric const PPCInstrInfo *TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo(); 1680b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 1690b57cec5SDimitry Andric SmallVector<MachineInstr *, 4> InstrsToErase; 1700b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) { 171*8bcb0991SDimitry Andric Changed |= removeRedundantLIs(MBB, TRI); 1720b57cec5SDimitry Andric for (MachineInstr &MI : MBB) { 1730b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 1740b57cec5SDimitry Andric // Detect self copies - these can result from running AADB. 1750b57cec5SDimitry Andric if (PPCInstrInfo::isSameClassPhysRegCopy(Opc)) { 1760b57cec5SDimitry Andric const MCInstrDesc &MCID = TII->get(Opc); 1770b57cec5SDimitry Andric if (MCID.getNumOperands() == 3 && 1780b57cec5SDimitry Andric MI.getOperand(0).getReg() == MI.getOperand(1).getReg() && 1790b57cec5SDimitry Andric MI.getOperand(0).getReg() == MI.getOperand(2).getReg()) { 1800b57cec5SDimitry Andric NumberOfSelfCopies++; 1810b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: "); 1820b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 1830b57cec5SDimitry Andric InstrsToErase.push_back(&MI); 1840b57cec5SDimitry Andric continue; 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric else if (MCID.getNumOperands() == 2 && 1870b57cec5SDimitry Andric MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) { 1880b57cec5SDimitry Andric NumberOfSelfCopies++; 1890b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: "); 1900b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 1910b57cec5SDimitry Andric InstrsToErase.push_back(&MI); 1920b57cec5SDimitry Andric continue; 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric } 1950b57cec5SDimitry Andric MachineInstr *DefMIToErase = nullptr; 1960b57cec5SDimitry Andric if (TII->convertToImmediateForm(MI, &DefMIToErase)) { 1970b57cec5SDimitry Andric Changed = true; 1980b57cec5SDimitry Andric NumRRConvertedInPreEmit++; 1990b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Converted instruction to imm form: "); 2000b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 2010b57cec5SDimitry Andric if (DefMIToErase) { 2020b57cec5SDimitry Andric InstrsToErase.push_back(DefMIToErase); 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric } 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric // Eliminate conditional branch based on a constant CR bit by 2080b57cec5SDimitry Andric // CRSET or CRUNSET. We eliminate the conditional branch or 2090b57cec5SDimitry Andric // convert it into an unconditional branch. Also, if the CR bit 2100b57cec5SDimitry Andric // is not used by other instructions, we eliminate CRSET as well. 2110b57cec5SDimitry Andric auto I = MBB.getFirstInstrTerminator(); 2120b57cec5SDimitry Andric if (I == MBB.instr_end()) 2130b57cec5SDimitry Andric continue; 2140b57cec5SDimitry Andric MachineInstr *Br = &*I; 2150b57cec5SDimitry Andric if (Br->getOpcode() != PPC::BC && Br->getOpcode() != PPC::BCn) 2160b57cec5SDimitry Andric continue; 2170b57cec5SDimitry Andric MachineInstr *CRSetMI = nullptr; 218*8bcb0991SDimitry Andric Register CRBit = Br->getOperand(0).getReg(); 2190b57cec5SDimitry Andric unsigned CRReg = getCRFromCRBit(CRBit); 2200b57cec5SDimitry Andric bool SeenUse = false; 2210b57cec5SDimitry Andric MachineBasicBlock::reverse_iterator It = Br, Er = MBB.rend(); 2220b57cec5SDimitry Andric for (It++; It != Er; It++) { 2230b57cec5SDimitry Andric if (It->modifiesRegister(CRBit, TRI)) { 2240b57cec5SDimitry Andric if ((It->getOpcode() == PPC::CRUNSET || 2250b57cec5SDimitry Andric It->getOpcode() == PPC::CRSET) && 2260b57cec5SDimitry Andric It->getOperand(0).getReg() == CRBit) 2270b57cec5SDimitry Andric CRSetMI = &*It; 2280b57cec5SDimitry Andric break; 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric if (It->readsRegister(CRBit, TRI)) 2310b57cec5SDimitry Andric SeenUse = true; 2320b57cec5SDimitry Andric } 2330b57cec5SDimitry Andric if (!CRSetMI) continue; 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric unsigned CRSetOp = CRSetMI->getOpcode(); 2360b57cec5SDimitry Andric if ((Br->getOpcode() == PPC::BCn && CRSetOp == PPC::CRSET) || 2370b57cec5SDimitry Andric (Br->getOpcode() == PPC::BC && CRSetOp == PPC::CRUNSET)) { 2380b57cec5SDimitry Andric // Remove this branch since it cannot be taken. 2390b57cec5SDimitry Andric InstrsToErase.push_back(Br); 2400b57cec5SDimitry Andric MBB.removeSuccessor(Br->getOperand(1).getMBB()); 2410b57cec5SDimitry Andric } 2420b57cec5SDimitry Andric else { 2430b57cec5SDimitry Andric // This conditional branch is always taken. So, remove all branches 2440b57cec5SDimitry Andric // and insert an unconditional branch to the destination of this. 2450b57cec5SDimitry Andric MachineBasicBlock::iterator It = Br, Er = MBB.end(); 2460b57cec5SDimitry Andric for (; It != Er; It++) { 2470b57cec5SDimitry Andric if (It->isDebugInstr()) continue; 2480b57cec5SDimitry Andric assert(It->isTerminator() && "Non-terminator after a terminator"); 2490b57cec5SDimitry Andric InstrsToErase.push_back(&*It); 2500b57cec5SDimitry Andric } 2510b57cec5SDimitry Andric if (!MBB.isLayoutSuccessor(Br->getOperand(1).getMBB())) { 2520b57cec5SDimitry Andric ArrayRef<MachineOperand> NoCond; 2530b57cec5SDimitry Andric TII->insertBranch(MBB, Br->getOperand(1).getMBB(), nullptr, 2540b57cec5SDimitry Andric NoCond, Br->getDebugLoc()); 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric for (auto &Succ : MBB.successors()) 2570b57cec5SDimitry Andric if (Succ != Br->getOperand(1).getMBB()) { 2580b57cec5SDimitry Andric MBB.removeSuccessor(Succ); 2590b57cec5SDimitry Andric break; 2600b57cec5SDimitry Andric } 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric // If the CRBit is not used by another instruction, we can eliminate 2640b57cec5SDimitry Andric // CRSET/CRUNSET instruction. 2650b57cec5SDimitry Andric if (!SeenUse) { 2660b57cec5SDimitry Andric // We need to check use of the CRBit in successors. 2670b57cec5SDimitry Andric for (auto &SuccMBB : MBB.successors()) 2680b57cec5SDimitry Andric if (SuccMBB->isLiveIn(CRBit) || SuccMBB->isLiveIn(CRReg)) { 2690b57cec5SDimitry Andric SeenUse = true; 2700b57cec5SDimitry Andric break; 2710b57cec5SDimitry Andric } 2720b57cec5SDimitry Andric if (!SeenUse) 2730b57cec5SDimitry Andric InstrsToErase.push_back(CRSetMI); 2740b57cec5SDimitry Andric } 2750b57cec5SDimitry Andric } 2760b57cec5SDimitry Andric for (MachineInstr *MI : InstrsToErase) { 2770b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "PPC pre-emit peephole: erasing instruction: "); 2780b57cec5SDimitry Andric LLVM_DEBUG(MI->dump()); 2790b57cec5SDimitry Andric MI->eraseFromParent(); 2800b57cec5SDimitry Andric NumRemovedInPreEmit++; 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric return Changed; 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric }; 2850b57cec5SDimitry Andric } 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andric INITIALIZE_PASS(PPCPreEmitPeephole, DEBUG_TYPE, "PowerPC Pre-Emit Peephole", 2880b57cec5SDimitry Andric false, false) 2890b57cec5SDimitry Andric char PPCPreEmitPeephole::ID = 0; 2900b57cec5SDimitry Andric 2910b57cec5SDimitry Andric FunctionPass *llvm::createPPCPreEmitPeepholePass() { 2920b57cec5SDimitry Andric return new PPCPreEmitPeephole(); 2930b57cec5SDimitry Andric } 294