1*0b57cec5SDimitry Andric //===--------- PPCPreEmitPeephole.cpp - Late peephole optimizations -------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // A pre-emit peephole for catching opportunities introduced by late passes such 10*0b57cec5SDimitry Andric // as MachineBlockPlacement. 11*0b57cec5SDimitry Andric // 12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13*0b57cec5SDimitry Andric 14*0b57cec5SDimitry Andric #include "PPC.h" 15*0b57cec5SDimitry Andric #include "PPCInstrInfo.h" 16*0b57cec5SDimitry Andric #include "PPCSubtarget.h" 17*0b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 18*0b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 19*0b57cec5SDimitry Andric #include "llvm/CodeGen/LivePhysRegs.h" 20*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 21*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 22*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 23*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 24*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 25*0b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 26*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 27*0b57cec5SDimitry Andric 28*0b57cec5SDimitry Andric using namespace llvm; 29*0b57cec5SDimitry Andric 30*0b57cec5SDimitry Andric #define DEBUG_TYPE "ppc-pre-emit-peephole" 31*0b57cec5SDimitry Andric 32*0b57cec5SDimitry Andric STATISTIC(NumRRConvertedInPreEmit, 33*0b57cec5SDimitry Andric "Number of r+r instructions converted to r+i in pre-emit peephole"); 34*0b57cec5SDimitry Andric STATISTIC(NumRemovedInPreEmit, 35*0b57cec5SDimitry Andric "Number of instructions deleted in pre-emit peephole"); 36*0b57cec5SDimitry Andric STATISTIC(NumberOfSelfCopies, 37*0b57cec5SDimitry Andric "Number of self copy instructions eliminated"); 38*0b57cec5SDimitry Andric 39*0b57cec5SDimitry Andric static cl::opt<bool> 40*0b57cec5SDimitry Andric RunPreEmitPeephole("ppc-late-peephole", cl::Hidden, cl::init(true), 41*0b57cec5SDimitry Andric cl::desc("Run pre-emit peephole optimizations.")); 42*0b57cec5SDimitry Andric 43*0b57cec5SDimitry Andric namespace { 44*0b57cec5SDimitry Andric class PPCPreEmitPeephole : public MachineFunctionPass { 45*0b57cec5SDimitry Andric public: 46*0b57cec5SDimitry Andric static char ID; 47*0b57cec5SDimitry Andric PPCPreEmitPeephole() : MachineFunctionPass(ID) { 48*0b57cec5SDimitry Andric initializePPCPreEmitPeepholePass(*PassRegistry::getPassRegistry()); 49*0b57cec5SDimitry Andric } 50*0b57cec5SDimitry Andric 51*0b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 52*0b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 53*0b57cec5SDimitry Andric } 54*0b57cec5SDimitry Andric 55*0b57cec5SDimitry Andric MachineFunctionProperties getRequiredProperties() const override { 56*0b57cec5SDimitry Andric return MachineFunctionProperties().set( 57*0b57cec5SDimitry Andric MachineFunctionProperties::Property::NoVRegs); 58*0b57cec5SDimitry Andric } 59*0b57cec5SDimitry Andric 60*0b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override { 61*0b57cec5SDimitry Andric if (skipFunction(MF.getFunction()) || !RunPreEmitPeephole) 62*0b57cec5SDimitry Andric return false; 63*0b57cec5SDimitry Andric bool Changed = false; 64*0b57cec5SDimitry Andric const PPCInstrInfo *TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo(); 65*0b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 66*0b57cec5SDimitry Andric SmallVector<MachineInstr *, 4> InstrsToErase; 67*0b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) { 68*0b57cec5SDimitry Andric for (MachineInstr &MI : MBB) { 69*0b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 70*0b57cec5SDimitry Andric // Detect self copies - these can result from running AADB. 71*0b57cec5SDimitry Andric if (PPCInstrInfo::isSameClassPhysRegCopy(Opc)) { 72*0b57cec5SDimitry Andric const MCInstrDesc &MCID = TII->get(Opc); 73*0b57cec5SDimitry Andric if (MCID.getNumOperands() == 3 && 74*0b57cec5SDimitry Andric MI.getOperand(0).getReg() == MI.getOperand(1).getReg() && 75*0b57cec5SDimitry Andric MI.getOperand(0).getReg() == MI.getOperand(2).getReg()) { 76*0b57cec5SDimitry Andric NumberOfSelfCopies++; 77*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: "); 78*0b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 79*0b57cec5SDimitry Andric InstrsToErase.push_back(&MI); 80*0b57cec5SDimitry Andric continue; 81*0b57cec5SDimitry Andric } 82*0b57cec5SDimitry Andric else if (MCID.getNumOperands() == 2 && 83*0b57cec5SDimitry Andric MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) { 84*0b57cec5SDimitry Andric NumberOfSelfCopies++; 85*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: "); 86*0b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 87*0b57cec5SDimitry Andric InstrsToErase.push_back(&MI); 88*0b57cec5SDimitry Andric continue; 89*0b57cec5SDimitry Andric } 90*0b57cec5SDimitry Andric } 91*0b57cec5SDimitry Andric MachineInstr *DefMIToErase = nullptr; 92*0b57cec5SDimitry Andric if (TII->convertToImmediateForm(MI, &DefMIToErase)) { 93*0b57cec5SDimitry Andric Changed = true; 94*0b57cec5SDimitry Andric NumRRConvertedInPreEmit++; 95*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Converted instruction to imm form: "); 96*0b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 97*0b57cec5SDimitry Andric if (DefMIToErase) { 98*0b57cec5SDimitry Andric InstrsToErase.push_back(DefMIToErase); 99*0b57cec5SDimitry Andric } 100*0b57cec5SDimitry Andric } 101*0b57cec5SDimitry Andric } 102*0b57cec5SDimitry Andric 103*0b57cec5SDimitry Andric // Eliminate conditional branch based on a constant CR bit by 104*0b57cec5SDimitry Andric // CRSET or CRUNSET. We eliminate the conditional branch or 105*0b57cec5SDimitry Andric // convert it into an unconditional branch. Also, if the CR bit 106*0b57cec5SDimitry Andric // is not used by other instructions, we eliminate CRSET as well. 107*0b57cec5SDimitry Andric auto I = MBB.getFirstInstrTerminator(); 108*0b57cec5SDimitry Andric if (I == MBB.instr_end()) 109*0b57cec5SDimitry Andric continue; 110*0b57cec5SDimitry Andric MachineInstr *Br = &*I; 111*0b57cec5SDimitry Andric if (Br->getOpcode() != PPC::BC && Br->getOpcode() != PPC::BCn) 112*0b57cec5SDimitry Andric continue; 113*0b57cec5SDimitry Andric MachineInstr *CRSetMI = nullptr; 114*0b57cec5SDimitry Andric unsigned CRBit = Br->getOperand(0).getReg(); 115*0b57cec5SDimitry Andric unsigned CRReg = getCRFromCRBit(CRBit); 116*0b57cec5SDimitry Andric bool SeenUse = false; 117*0b57cec5SDimitry Andric MachineBasicBlock::reverse_iterator It = Br, Er = MBB.rend(); 118*0b57cec5SDimitry Andric for (It++; It != Er; It++) { 119*0b57cec5SDimitry Andric if (It->modifiesRegister(CRBit, TRI)) { 120*0b57cec5SDimitry Andric if ((It->getOpcode() == PPC::CRUNSET || 121*0b57cec5SDimitry Andric It->getOpcode() == PPC::CRSET) && 122*0b57cec5SDimitry Andric It->getOperand(0).getReg() == CRBit) 123*0b57cec5SDimitry Andric CRSetMI = &*It; 124*0b57cec5SDimitry Andric break; 125*0b57cec5SDimitry Andric } 126*0b57cec5SDimitry Andric if (It->readsRegister(CRBit, TRI)) 127*0b57cec5SDimitry Andric SeenUse = true; 128*0b57cec5SDimitry Andric } 129*0b57cec5SDimitry Andric if (!CRSetMI) continue; 130*0b57cec5SDimitry Andric 131*0b57cec5SDimitry Andric unsigned CRSetOp = CRSetMI->getOpcode(); 132*0b57cec5SDimitry Andric if ((Br->getOpcode() == PPC::BCn && CRSetOp == PPC::CRSET) || 133*0b57cec5SDimitry Andric (Br->getOpcode() == PPC::BC && CRSetOp == PPC::CRUNSET)) { 134*0b57cec5SDimitry Andric // Remove this branch since it cannot be taken. 135*0b57cec5SDimitry Andric InstrsToErase.push_back(Br); 136*0b57cec5SDimitry Andric MBB.removeSuccessor(Br->getOperand(1).getMBB()); 137*0b57cec5SDimitry Andric } 138*0b57cec5SDimitry Andric else { 139*0b57cec5SDimitry Andric // This conditional branch is always taken. So, remove all branches 140*0b57cec5SDimitry Andric // and insert an unconditional branch to the destination of this. 141*0b57cec5SDimitry Andric MachineBasicBlock::iterator It = Br, Er = MBB.end(); 142*0b57cec5SDimitry Andric for (; It != Er; It++) { 143*0b57cec5SDimitry Andric if (It->isDebugInstr()) continue; 144*0b57cec5SDimitry Andric assert(It->isTerminator() && "Non-terminator after a terminator"); 145*0b57cec5SDimitry Andric InstrsToErase.push_back(&*It); 146*0b57cec5SDimitry Andric } 147*0b57cec5SDimitry Andric if (!MBB.isLayoutSuccessor(Br->getOperand(1).getMBB())) { 148*0b57cec5SDimitry Andric ArrayRef<MachineOperand> NoCond; 149*0b57cec5SDimitry Andric TII->insertBranch(MBB, Br->getOperand(1).getMBB(), nullptr, 150*0b57cec5SDimitry Andric NoCond, Br->getDebugLoc()); 151*0b57cec5SDimitry Andric } 152*0b57cec5SDimitry Andric for (auto &Succ : MBB.successors()) 153*0b57cec5SDimitry Andric if (Succ != Br->getOperand(1).getMBB()) { 154*0b57cec5SDimitry Andric MBB.removeSuccessor(Succ); 155*0b57cec5SDimitry Andric break; 156*0b57cec5SDimitry Andric } 157*0b57cec5SDimitry Andric } 158*0b57cec5SDimitry Andric 159*0b57cec5SDimitry Andric // If the CRBit is not used by another instruction, we can eliminate 160*0b57cec5SDimitry Andric // CRSET/CRUNSET instruction. 161*0b57cec5SDimitry Andric if (!SeenUse) { 162*0b57cec5SDimitry Andric // We need to check use of the CRBit in successors. 163*0b57cec5SDimitry Andric for (auto &SuccMBB : MBB.successors()) 164*0b57cec5SDimitry Andric if (SuccMBB->isLiveIn(CRBit) || SuccMBB->isLiveIn(CRReg)) { 165*0b57cec5SDimitry Andric SeenUse = true; 166*0b57cec5SDimitry Andric break; 167*0b57cec5SDimitry Andric } 168*0b57cec5SDimitry Andric if (!SeenUse) 169*0b57cec5SDimitry Andric InstrsToErase.push_back(CRSetMI); 170*0b57cec5SDimitry Andric } 171*0b57cec5SDimitry Andric } 172*0b57cec5SDimitry Andric for (MachineInstr *MI : InstrsToErase) { 173*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "PPC pre-emit peephole: erasing instruction: "); 174*0b57cec5SDimitry Andric LLVM_DEBUG(MI->dump()); 175*0b57cec5SDimitry Andric MI->eraseFromParent(); 176*0b57cec5SDimitry Andric NumRemovedInPreEmit++; 177*0b57cec5SDimitry Andric } 178*0b57cec5SDimitry Andric return Changed; 179*0b57cec5SDimitry Andric } 180*0b57cec5SDimitry Andric }; 181*0b57cec5SDimitry Andric } 182*0b57cec5SDimitry Andric 183*0b57cec5SDimitry Andric INITIALIZE_PASS(PPCPreEmitPeephole, DEBUG_TYPE, "PowerPC Pre-Emit Peephole", 184*0b57cec5SDimitry Andric false, false) 185*0b57cec5SDimitry Andric char PPCPreEmitPeephole::ID = 0; 186*0b57cec5SDimitry Andric 187*0b57cec5SDimitry Andric FunctionPass *llvm::createPPCPreEmitPeepholePass() { 188*0b57cec5SDimitry Andric return new PPCPreEmitPeephole(); 189*0b57cec5SDimitry Andric } 190