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" 24*e8d8bef9SDimitry Andric #include "llvm/MC/MCContext.h" 250b57cec5SDimitry Andric #include "llvm/Support/CommandLine.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"); 38480093f4SDimitry Andric STATISTIC(NumFrameOffFoldInPreEmit, 39480093f4SDimitry Andric "Number of folding frame offset by using r+r in pre-emit peephole"); 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric static cl::opt<bool> 42*e8d8bef9SDimitry Andric EnablePCRelLinkerOpt("ppc-pcrel-linker-opt", cl::Hidden, cl::init(true), 43*e8d8bef9SDimitry Andric cl::desc("enable PC Relative linker optimization")); 44*e8d8bef9SDimitry Andric 45*e8d8bef9SDimitry Andric static cl::opt<bool> 460b57cec5SDimitry Andric RunPreEmitPeephole("ppc-late-peephole", cl::Hidden, cl::init(true), 470b57cec5SDimitry Andric cl::desc("Run pre-emit peephole optimizations.")); 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric namespace { 50*e8d8bef9SDimitry Andric 51*e8d8bef9SDimitry Andric static bool hasPCRelativeForm(MachineInstr &Use) { 52*e8d8bef9SDimitry Andric switch (Use.getOpcode()) { 53*e8d8bef9SDimitry Andric default: 54*e8d8bef9SDimitry Andric return false; 55*e8d8bef9SDimitry Andric case PPC::LBZ: 56*e8d8bef9SDimitry Andric case PPC::LBZ8: 57*e8d8bef9SDimitry Andric case PPC::LHA: 58*e8d8bef9SDimitry Andric case PPC::LHA8: 59*e8d8bef9SDimitry Andric case PPC::LHZ: 60*e8d8bef9SDimitry Andric case PPC::LHZ8: 61*e8d8bef9SDimitry Andric case PPC::LWZ: 62*e8d8bef9SDimitry Andric case PPC::LWZ8: 63*e8d8bef9SDimitry Andric case PPC::STB: 64*e8d8bef9SDimitry Andric case PPC::STB8: 65*e8d8bef9SDimitry Andric case PPC::STH: 66*e8d8bef9SDimitry Andric case PPC::STH8: 67*e8d8bef9SDimitry Andric case PPC::STW: 68*e8d8bef9SDimitry Andric case PPC::STW8: 69*e8d8bef9SDimitry Andric case PPC::LD: 70*e8d8bef9SDimitry Andric case PPC::STD: 71*e8d8bef9SDimitry Andric case PPC::LWA: 72*e8d8bef9SDimitry Andric case PPC::LXSD: 73*e8d8bef9SDimitry Andric case PPC::LXSSP: 74*e8d8bef9SDimitry Andric case PPC::LXV: 75*e8d8bef9SDimitry Andric case PPC::STXSD: 76*e8d8bef9SDimitry Andric case PPC::STXSSP: 77*e8d8bef9SDimitry Andric case PPC::STXV: 78*e8d8bef9SDimitry Andric case PPC::LFD: 79*e8d8bef9SDimitry Andric case PPC::LFS: 80*e8d8bef9SDimitry Andric case PPC::STFD: 81*e8d8bef9SDimitry Andric case PPC::STFS: 82*e8d8bef9SDimitry Andric case PPC::DFLOADf32: 83*e8d8bef9SDimitry Andric case PPC::DFLOADf64: 84*e8d8bef9SDimitry Andric case PPC::DFSTOREf32: 85*e8d8bef9SDimitry Andric case PPC::DFSTOREf64: 86*e8d8bef9SDimitry Andric return true; 87*e8d8bef9SDimitry Andric } 88*e8d8bef9SDimitry Andric } 89*e8d8bef9SDimitry Andric 900b57cec5SDimitry Andric class PPCPreEmitPeephole : public MachineFunctionPass { 910b57cec5SDimitry Andric public: 920b57cec5SDimitry Andric static char ID; 930b57cec5SDimitry Andric PPCPreEmitPeephole() : MachineFunctionPass(ID) { 940b57cec5SDimitry Andric initializePPCPreEmitPeepholePass(*PassRegistry::getPassRegistry()); 950b57cec5SDimitry Andric } 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 980b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric MachineFunctionProperties getRequiredProperties() const override { 1020b57cec5SDimitry Andric return MachineFunctionProperties().set( 1030b57cec5SDimitry Andric MachineFunctionProperties::Property::NoVRegs); 1040b57cec5SDimitry Andric } 1050b57cec5SDimitry Andric 1068bcb0991SDimitry Andric // This function removes any redundant load immediates. It has two level 1078bcb0991SDimitry Andric // loops - The outer loop finds the load immediates BBI that could be used 1088bcb0991SDimitry Andric // to replace following redundancy. The inner loop scans instructions that 1098bcb0991SDimitry Andric // after BBI to find redundancy and update kill/dead flags accordingly. If 1108bcb0991SDimitry Andric // AfterBBI is the same as BBI, it is redundant, otherwise any instructions 1118bcb0991SDimitry Andric // that modify the def register of BBI would break the scanning. 1128bcb0991SDimitry Andric // DeadOrKillToUnset is a pointer to the previous operand that had the 1138bcb0991SDimitry Andric // kill/dead flag set. It keeps track of the def register of BBI, the use 1148bcb0991SDimitry Andric // registers of AfterBBIs and the def registers of AfterBBIs. 1158bcb0991SDimitry Andric bool removeRedundantLIs(MachineBasicBlock &MBB, 1168bcb0991SDimitry Andric const TargetRegisterInfo *TRI) { 1178bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Remove redundant load immediates from MBB:\n"; 1188bcb0991SDimitry Andric MBB.dump(); dbgs() << "\n"); 1198bcb0991SDimitry Andric 1208bcb0991SDimitry Andric DenseSet<MachineInstr *> InstrsToErase; 1218bcb0991SDimitry Andric for (auto BBI = MBB.instr_begin(); BBI != MBB.instr_end(); ++BBI) { 1228bcb0991SDimitry Andric // Skip load immediate that is marked to be erased later because it 1238bcb0991SDimitry Andric // cannot be used to replace any other instructions. 124*e8d8bef9SDimitry Andric if (InstrsToErase.contains(&*BBI)) 1258bcb0991SDimitry Andric continue; 1268bcb0991SDimitry Andric // Skip non-load immediate. 1278bcb0991SDimitry Andric unsigned Opc = BBI->getOpcode(); 1288bcb0991SDimitry Andric if (Opc != PPC::LI && Opc != PPC::LI8 && Opc != PPC::LIS && 1298bcb0991SDimitry Andric Opc != PPC::LIS8) 1308bcb0991SDimitry Andric continue; 1318bcb0991SDimitry Andric // Skip load immediate, where the operand is a relocation (e.g., $r3 = 1328bcb0991SDimitry Andric // LI target-flags(ppc-lo) %const.0). 1338bcb0991SDimitry Andric if (!BBI->getOperand(1).isImm()) 1348bcb0991SDimitry Andric continue; 1358bcb0991SDimitry Andric assert(BBI->getOperand(0).isReg() && 1368bcb0991SDimitry Andric "Expected a register for the first operand"); 1378bcb0991SDimitry Andric 1388bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Scanning after load immediate: "; BBI->dump();); 1398bcb0991SDimitry Andric 1408bcb0991SDimitry Andric Register Reg = BBI->getOperand(0).getReg(); 1418bcb0991SDimitry Andric int64_t Imm = BBI->getOperand(1).getImm(); 1428bcb0991SDimitry Andric MachineOperand *DeadOrKillToUnset = nullptr; 1438bcb0991SDimitry Andric if (BBI->getOperand(0).isDead()) { 1448bcb0991SDimitry Andric DeadOrKillToUnset = &BBI->getOperand(0); 1458bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << " Kill flag of " << *DeadOrKillToUnset 1468bcb0991SDimitry Andric << " from load immediate " << *BBI 1478bcb0991SDimitry Andric << " is a unsetting candidate\n"); 1488bcb0991SDimitry Andric } 1498bcb0991SDimitry Andric // This loop scans instructions after BBI to see if there is any 1508bcb0991SDimitry Andric // redundant load immediate. 1518bcb0991SDimitry Andric for (auto AfterBBI = std::next(BBI); AfterBBI != MBB.instr_end(); 1528bcb0991SDimitry Andric ++AfterBBI) { 1538bcb0991SDimitry Andric // Track the operand that kill Reg. We would unset the kill flag of 1548bcb0991SDimitry Andric // the operand if there is a following redundant load immediate. 1558bcb0991SDimitry Andric int KillIdx = AfterBBI->findRegisterUseOperandIdx(Reg, true, TRI); 1565ffd83dbSDimitry Andric 1575ffd83dbSDimitry Andric // We can't just clear implicit kills, so if we encounter one, stop 1585ffd83dbSDimitry Andric // looking further. 1595ffd83dbSDimitry Andric if (KillIdx != -1 && AfterBBI->getOperand(KillIdx).isImplicit()) { 1605ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() 1615ffd83dbSDimitry Andric << "Encountered an implicit kill, cannot proceed: "); 1625ffd83dbSDimitry Andric LLVM_DEBUG(AfterBBI->dump()); 1635ffd83dbSDimitry Andric break; 1645ffd83dbSDimitry Andric } 1655ffd83dbSDimitry Andric 1668bcb0991SDimitry Andric if (KillIdx != -1) { 1678bcb0991SDimitry Andric assert(!DeadOrKillToUnset && "Shouldn't kill same register twice"); 1688bcb0991SDimitry Andric DeadOrKillToUnset = &AfterBBI->getOperand(KillIdx); 1698bcb0991SDimitry Andric LLVM_DEBUG(dbgs() 1708bcb0991SDimitry Andric << " Kill flag of " << *DeadOrKillToUnset << " from " 1718bcb0991SDimitry Andric << *AfterBBI << " is a unsetting candidate\n"); 1728bcb0991SDimitry Andric } 1738bcb0991SDimitry Andric 1748bcb0991SDimitry Andric if (!AfterBBI->modifiesRegister(Reg, TRI)) 1758bcb0991SDimitry Andric continue; 1768bcb0991SDimitry Andric // Finish scanning because Reg is overwritten by a non-load 1778bcb0991SDimitry Andric // instruction. 1788bcb0991SDimitry Andric if (AfterBBI->getOpcode() != Opc) 1798bcb0991SDimitry Andric break; 1808bcb0991SDimitry Andric assert(AfterBBI->getOperand(0).isReg() && 1818bcb0991SDimitry Andric "Expected a register for the first operand"); 1828bcb0991SDimitry Andric // Finish scanning because Reg is overwritten by a relocation or a 1838bcb0991SDimitry Andric // different value. 1848bcb0991SDimitry Andric if (!AfterBBI->getOperand(1).isImm() || 1858bcb0991SDimitry Andric AfterBBI->getOperand(1).getImm() != Imm) 1868bcb0991SDimitry Andric break; 1878bcb0991SDimitry Andric 1888bcb0991SDimitry Andric // It loads same immediate value to the same Reg, which is redundant. 1898bcb0991SDimitry Andric // We would unset kill flag in previous Reg usage to extend live range 1908bcb0991SDimitry Andric // of Reg first, then remove the redundancy. 1918bcb0991SDimitry Andric if (DeadOrKillToUnset) { 1928bcb0991SDimitry Andric LLVM_DEBUG(dbgs() 1938bcb0991SDimitry Andric << " Unset dead/kill flag of " << *DeadOrKillToUnset 1948bcb0991SDimitry Andric << " from " << *DeadOrKillToUnset->getParent()); 1958bcb0991SDimitry Andric if (DeadOrKillToUnset->isDef()) 1968bcb0991SDimitry Andric DeadOrKillToUnset->setIsDead(false); 1978bcb0991SDimitry Andric else 1988bcb0991SDimitry Andric DeadOrKillToUnset->setIsKill(false); 1998bcb0991SDimitry Andric } 2008bcb0991SDimitry Andric DeadOrKillToUnset = 2018bcb0991SDimitry Andric AfterBBI->findRegisterDefOperand(Reg, true, true, TRI); 2028bcb0991SDimitry Andric if (DeadOrKillToUnset) 2038bcb0991SDimitry Andric LLVM_DEBUG(dbgs() 2048bcb0991SDimitry Andric << " Dead flag of " << *DeadOrKillToUnset << " from " 2058bcb0991SDimitry Andric << *AfterBBI << " is a unsetting candidate\n"); 2068bcb0991SDimitry Andric InstrsToErase.insert(&*AfterBBI); 2078bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << " Remove redundant load immediate: "; 2088bcb0991SDimitry Andric AfterBBI->dump()); 2098bcb0991SDimitry Andric } 2108bcb0991SDimitry Andric } 2118bcb0991SDimitry Andric 2128bcb0991SDimitry Andric for (MachineInstr *MI : InstrsToErase) { 2138bcb0991SDimitry Andric MI->eraseFromParent(); 2148bcb0991SDimitry Andric } 2158bcb0991SDimitry Andric NumRemovedInPreEmit += InstrsToErase.size(); 2168bcb0991SDimitry Andric return !InstrsToErase.empty(); 2178bcb0991SDimitry Andric } 2188bcb0991SDimitry Andric 219*e8d8bef9SDimitry Andric // Check if this instruction is a PLDpc that is part of a GOT indirect 220*e8d8bef9SDimitry Andric // access. 221*e8d8bef9SDimitry Andric bool isGOTPLDpc(MachineInstr &Instr) { 222*e8d8bef9SDimitry Andric if (Instr.getOpcode() != PPC::PLDpc) 223*e8d8bef9SDimitry Andric return false; 224*e8d8bef9SDimitry Andric 225*e8d8bef9SDimitry Andric // The result must be a register. 226*e8d8bef9SDimitry Andric const MachineOperand &LoadedAddressReg = Instr.getOperand(0); 227*e8d8bef9SDimitry Andric if (!LoadedAddressReg.isReg()) 228*e8d8bef9SDimitry Andric return false; 229*e8d8bef9SDimitry Andric 230*e8d8bef9SDimitry Andric // Make sure that this is a global symbol. 231*e8d8bef9SDimitry Andric const MachineOperand &SymbolOp = Instr.getOperand(1); 232*e8d8bef9SDimitry Andric if (!SymbolOp.isGlobal()) 233*e8d8bef9SDimitry Andric return false; 234*e8d8bef9SDimitry Andric 235*e8d8bef9SDimitry Andric // Finally return true only if the GOT flag is present. 236*e8d8bef9SDimitry Andric return (SymbolOp.getTargetFlags() & PPCII::MO_GOT_FLAG); 237*e8d8bef9SDimitry Andric } 238*e8d8bef9SDimitry Andric 239*e8d8bef9SDimitry Andric bool addLinkerOpt(MachineBasicBlock &MBB, const TargetRegisterInfo *TRI) { 240*e8d8bef9SDimitry Andric MachineFunction *MF = MBB.getParent(); 241*e8d8bef9SDimitry Andric // If the linker opt is disabled then just return. 242*e8d8bef9SDimitry Andric if (!EnablePCRelLinkerOpt) 243*e8d8bef9SDimitry Andric return false; 244*e8d8bef9SDimitry Andric 245*e8d8bef9SDimitry Andric // Add this linker opt only if we are using PC Relative memops. 246*e8d8bef9SDimitry Andric if (!MF->getSubtarget<PPCSubtarget>().isUsingPCRelativeCalls()) 247*e8d8bef9SDimitry Andric return false; 248*e8d8bef9SDimitry Andric 249*e8d8bef9SDimitry Andric // Struct to keep track of one def/use pair for a GOT indirect access. 250*e8d8bef9SDimitry Andric struct GOTDefUsePair { 251*e8d8bef9SDimitry Andric MachineBasicBlock::iterator DefInst; 252*e8d8bef9SDimitry Andric MachineBasicBlock::iterator UseInst; 253*e8d8bef9SDimitry Andric Register DefReg; 254*e8d8bef9SDimitry Andric Register UseReg; 255*e8d8bef9SDimitry Andric bool StillValid; 256*e8d8bef9SDimitry Andric }; 257*e8d8bef9SDimitry Andric // Vector of def/ues pairs in this basic block. 258*e8d8bef9SDimitry Andric SmallVector<GOTDefUsePair, 4> CandPairs; 259*e8d8bef9SDimitry Andric SmallVector<GOTDefUsePair, 4> ValidPairs; 260*e8d8bef9SDimitry Andric bool MadeChange = false; 261*e8d8bef9SDimitry Andric 262*e8d8bef9SDimitry Andric // Run through all of the instructions in the basic block and try to 263*e8d8bef9SDimitry Andric // collect potential pairs of GOT indirect access instructions. 264*e8d8bef9SDimitry Andric for (auto BBI = MBB.instr_begin(); BBI != MBB.instr_end(); ++BBI) { 265*e8d8bef9SDimitry Andric // Look for the initial GOT indirect load. 266*e8d8bef9SDimitry Andric if (isGOTPLDpc(*BBI)) { 267*e8d8bef9SDimitry Andric GOTDefUsePair CurrentPair{BBI, MachineBasicBlock::iterator(), 268*e8d8bef9SDimitry Andric BBI->getOperand(0).getReg(), 269*e8d8bef9SDimitry Andric PPC::NoRegister, true}; 270*e8d8bef9SDimitry Andric CandPairs.push_back(CurrentPair); 271*e8d8bef9SDimitry Andric continue; 272*e8d8bef9SDimitry Andric } 273*e8d8bef9SDimitry Andric 274*e8d8bef9SDimitry Andric // We haven't encountered any new PLD instructions, nothing to check. 275*e8d8bef9SDimitry Andric if (CandPairs.empty()) 276*e8d8bef9SDimitry Andric continue; 277*e8d8bef9SDimitry Andric 278*e8d8bef9SDimitry Andric // Run through the candidate pairs and see if any of the registers 279*e8d8bef9SDimitry Andric // defined in the PLD instructions are used by this instruction. 280*e8d8bef9SDimitry Andric // Note: the size of CandPairs can change in the loop. 281*e8d8bef9SDimitry Andric for (unsigned Idx = 0; Idx < CandPairs.size(); Idx++) { 282*e8d8bef9SDimitry Andric GOTDefUsePair &Pair = CandPairs[Idx]; 283*e8d8bef9SDimitry Andric // The instruction does not use or modify this PLD's def reg, 284*e8d8bef9SDimitry Andric // ignore it. 285*e8d8bef9SDimitry Andric if (!BBI->readsRegister(Pair.DefReg, TRI) && 286*e8d8bef9SDimitry Andric !BBI->modifiesRegister(Pair.DefReg, TRI)) 287*e8d8bef9SDimitry Andric continue; 288*e8d8bef9SDimitry Andric 289*e8d8bef9SDimitry Andric // The use needs to be used in the address compuation and not 290*e8d8bef9SDimitry Andric // as the register being stored for a store. 291*e8d8bef9SDimitry Andric const MachineOperand *UseOp = 292*e8d8bef9SDimitry Andric hasPCRelativeForm(*BBI) ? &BBI->getOperand(2) : nullptr; 293*e8d8bef9SDimitry Andric 294*e8d8bef9SDimitry Andric // Check for a valid use. 295*e8d8bef9SDimitry Andric if (UseOp && UseOp->isReg() && UseOp->getReg() == Pair.DefReg && 296*e8d8bef9SDimitry Andric UseOp->isUse() && UseOp->isKill()) { 297*e8d8bef9SDimitry Andric Pair.UseInst = BBI; 298*e8d8bef9SDimitry Andric Pair.UseReg = BBI->getOperand(0).getReg(); 299*e8d8bef9SDimitry Andric ValidPairs.push_back(Pair); 300*e8d8bef9SDimitry Andric } 301*e8d8bef9SDimitry Andric CandPairs.erase(CandPairs.begin() + Idx); 302*e8d8bef9SDimitry Andric } 303*e8d8bef9SDimitry Andric } 304*e8d8bef9SDimitry Andric 305*e8d8bef9SDimitry Andric // Go through all of the pairs and check for any more valid uses. 306*e8d8bef9SDimitry Andric for (auto Pair = ValidPairs.begin(); Pair != ValidPairs.end(); Pair++) { 307*e8d8bef9SDimitry Andric // We shouldn't be here if we don't have a valid pair. 308*e8d8bef9SDimitry Andric assert(Pair->UseInst.isValid() && Pair->StillValid && 309*e8d8bef9SDimitry Andric "Kept an invalid def/use pair for GOT PCRel opt"); 310*e8d8bef9SDimitry Andric // We have found a potential pair. Search through the instructions 311*e8d8bef9SDimitry Andric // between the def and the use to see if it is valid to mark this as a 312*e8d8bef9SDimitry Andric // linker opt. 313*e8d8bef9SDimitry Andric MachineBasicBlock::iterator BBI = Pair->DefInst; 314*e8d8bef9SDimitry Andric ++BBI; 315*e8d8bef9SDimitry Andric for (; BBI != Pair->UseInst; ++BBI) { 316*e8d8bef9SDimitry Andric if (BBI->readsRegister(Pair->UseReg, TRI) || 317*e8d8bef9SDimitry Andric BBI->modifiesRegister(Pair->UseReg, TRI)) { 318*e8d8bef9SDimitry Andric Pair->StillValid = false; 319*e8d8bef9SDimitry Andric break; 320*e8d8bef9SDimitry Andric } 321*e8d8bef9SDimitry Andric } 322*e8d8bef9SDimitry Andric 323*e8d8bef9SDimitry Andric if (!Pair->StillValid) 324*e8d8bef9SDimitry Andric continue; 325*e8d8bef9SDimitry Andric 326*e8d8bef9SDimitry Andric // The load/store instruction that uses the address from the PLD will 327*e8d8bef9SDimitry Andric // either use a register (for a store) or define a register (for the 328*e8d8bef9SDimitry Andric // load). That register will be added as an implicit def to the PLD 329*e8d8bef9SDimitry Andric // and as an implicit use on the second memory op. This is a precaution 330*e8d8bef9SDimitry Andric // to prevent future passes from using that register between the two 331*e8d8bef9SDimitry Andric // instructions. 332*e8d8bef9SDimitry Andric MachineOperand ImplDef = 333*e8d8bef9SDimitry Andric MachineOperand::CreateReg(Pair->UseReg, true, true); 334*e8d8bef9SDimitry Andric MachineOperand ImplUse = 335*e8d8bef9SDimitry Andric MachineOperand::CreateReg(Pair->UseReg, false, true); 336*e8d8bef9SDimitry Andric Pair->DefInst->addOperand(ImplDef); 337*e8d8bef9SDimitry Andric Pair->UseInst->addOperand(ImplUse); 338*e8d8bef9SDimitry Andric 339*e8d8bef9SDimitry Andric // Create the symbol. 340*e8d8bef9SDimitry Andric MCContext &Context = MF->getContext(); 341*e8d8bef9SDimitry Andric MCSymbol *Symbol = Context.createNamedTempSymbol("pcrel"); 342*e8d8bef9SDimitry Andric MachineOperand PCRelLabel = 343*e8d8bef9SDimitry Andric MachineOperand::CreateMCSymbol(Symbol, PPCII::MO_PCREL_OPT_FLAG); 344*e8d8bef9SDimitry Andric Pair->DefInst->addOperand(*MF, PCRelLabel); 345*e8d8bef9SDimitry Andric Pair->UseInst->addOperand(*MF, PCRelLabel); 346*e8d8bef9SDimitry Andric MadeChange |= true; 347*e8d8bef9SDimitry Andric } 348*e8d8bef9SDimitry Andric return MadeChange; 349*e8d8bef9SDimitry Andric } 350*e8d8bef9SDimitry Andric 351*e8d8bef9SDimitry Andric // This function removes redundant pairs of accumulator prime/unprime 352*e8d8bef9SDimitry Andric // instructions. In some situations, it's possible the compiler inserts an 353*e8d8bef9SDimitry Andric // accumulator prime instruction followed by an unprime instruction (e.g. 354*e8d8bef9SDimitry Andric // when we store an accumulator after restoring it from a spill). If the 355*e8d8bef9SDimitry Andric // accumulator is not used between the two, they can be removed. This 356*e8d8bef9SDimitry Andric // function removes these redundant pairs from basic blocks. 357*e8d8bef9SDimitry Andric // The algorithm is quite straightforward - every time we encounter a prime 358*e8d8bef9SDimitry Andric // instruction, the primed register is added to a candidate set. Any use 359*e8d8bef9SDimitry Andric // other than a prime removes the candidate from the set and any de-prime 360*e8d8bef9SDimitry Andric // of a current candidate marks both the prime and de-prime for removal. 361*e8d8bef9SDimitry Andric // This way we ensure we only remove prime/de-prime *pairs* with no 362*e8d8bef9SDimitry Andric // intervening uses. 363*e8d8bef9SDimitry Andric bool removeAccPrimeUnprime(MachineBasicBlock &MBB) { 364*e8d8bef9SDimitry Andric DenseSet<MachineInstr *> InstrsToErase; 365*e8d8bef9SDimitry Andric // Initially, none of the acc registers are candidates. 366*e8d8bef9SDimitry Andric SmallVector<MachineInstr *, 8> Candidates( 367*e8d8bef9SDimitry Andric PPC::UACCRCRegClass.getNumRegs(), nullptr); 368*e8d8bef9SDimitry Andric 369*e8d8bef9SDimitry Andric for (MachineInstr &BBI : MBB.instrs()) { 370*e8d8bef9SDimitry Andric unsigned Opc = BBI.getOpcode(); 371*e8d8bef9SDimitry Andric // If we are visiting a xxmtacc instruction, we add it and its operand 372*e8d8bef9SDimitry Andric // register to the candidate set. 373*e8d8bef9SDimitry Andric if (Opc == PPC::XXMTACC) { 374*e8d8bef9SDimitry Andric Register Acc = BBI.getOperand(0).getReg(); 375*e8d8bef9SDimitry Andric assert(PPC::ACCRCRegClass.contains(Acc) && 376*e8d8bef9SDimitry Andric "Unexpected register for XXMTACC"); 377*e8d8bef9SDimitry Andric Candidates[Acc - PPC::ACC0] = &BBI; 378*e8d8bef9SDimitry Andric } 379*e8d8bef9SDimitry Andric // If we are visiting a xxmfacc instruction and its operand register is 380*e8d8bef9SDimitry Andric // in the candidate set, we mark the two instructions for removal. 381*e8d8bef9SDimitry Andric else if (Opc == PPC::XXMFACC) { 382*e8d8bef9SDimitry Andric Register Acc = BBI.getOperand(0).getReg(); 383*e8d8bef9SDimitry Andric assert(PPC::ACCRCRegClass.contains(Acc) && 384*e8d8bef9SDimitry Andric "Unexpected register for XXMFACC"); 385*e8d8bef9SDimitry Andric if (!Candidates[Acc - PPC::ACC0]) 386*e8d8bef9SDimitry Andric continue; 387*e8d8bef9SDimitry Andric InstrsToErase.insert(&BBI); 388*e8d8bef9SDimitry Andric InstrsToErase.insert(Candidates[Acc - PPC::ACC0]); 389*e8d8bef9SDimitry Andric } 390*e8d8bef9SDimitry Andric // If we are visiting an instruction using an accumulator register 391*e8d8bef9SDimitry Andric // as operand, we remove it from the candidate set. 392*e8d8bef9SDimitry Andric else { 393*e8d8bef9SDimitry Andric for (MachineOperand &Operand : BBI.operands()) { 394*e8d8bef9SDimitry Andric if (!Operand.isReg()) 395*e8d8bef9SDimitry Andric continue; 396*e8d8bef9SDimitry Andric Register Reg = Operand.getReg(); 397*e8d8bef9SDimitry Andric if (PPC::ACCRCRegClass.contains(Reg)) 398*e8d8bef9SDimitry Andric Candidates[Reg - PPC::ACC0] = nullptr; 399*e8d8bef9SDimitry Andric } 400*e8d8bef9SDimitry Andric } 401*e8d8bef9SDimitry Andric } 402*e8d8bef9SDimitry Andric 403*e8d8bef9SDimitry Andric for (MachineInstr *MI : InstrsToErase) 404*e8d8bef9SDimitry Andric MI->eraseFromParent(); 405*e8d8bef9SDimitry Andric NumRemovedInPreEmit += InstrsToErase.size(); 406*e8d8bef9SDimitry Andric return !InstrsToErase.empty(); 407*e8d8bef9SDimitry Andric } 408*e8d8bef9SDimitry Andric 4090b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override { 410480093f4SDimitry Andric if (skipFunction(MF.getFunction()) || !RunPreEmitPeephole) { 411480093f4SDimitry Andric // Remove UNENCODED_NOP even when this pass is disabled. 412480093f4SDimitry Andric // This needs to be done unconditionally so we don't emit zeros 413480093f4SDimitry Andric // in the instruction stream. 414480093f4SDimitry Andric SmallVector<MachineInstr *, 4> InstrsToErase; 415480093f4SDimitry Andric for (MachineBasicBlock &MBB : MF) 416480093f4SDimitry Andric for (MachineInstr &MI : MBB) 417480093f4SDimitry Andric if (MI.getOpcode() == PPC::UNENCODED_NOP) 418480093f4SDimitry Andric InstrsToErase.push_back(&MI); 419480093f4SDimitry Andric for (MachineInstr *MI : InstrsToErase) 420480093f4SDimitry Andric MI->eraseFromParent(); 4210b57cec5SDimitry Andric return false; 422480093f4SDimitry Andric } 4230b57cec5SDimitry Andric bool Changed = false; 4240b57cec5SDimitry Andric const PPCInstrInfo *TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo(); 4250b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 4260b57cec5SDimitry Andric SmallVector<MachineInstr *, 4> InstrsToErase; 4270b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) { 4288bcb0991SDimitry Andric Changed |= removeRedundantLIs(MBB, TRI); 429*e8d8bef9SDimitry Andric Changed |= addLinkerOpt(MBB, TRI); 430*e8d8bef9SDimitry Andric Changed |= removeAccPrimeUnprime(MBB); 4310b57cec5SDimitry Andric for (MachineInstr &MI : MBB) { 4320b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 433480093f4SDimitry Andric if (Opc == PPC::UNENCODED_NOP) { 434480093f4SDimitry Andric InstrsToErase.push_back(&MI); 435480093f4SDimitry Andric continue; 436480093f4SDimitry Andric } 4370b57cec5SDimitry Andric // Detect self copies - these can result from running AADB. 4380b57cec5SDimitry Andric if (PPCInstrInfo::isSameClassPhysRegCopy(Opc)) { 4390b57cec5SDimitry Andric const MCInstrDesc &MCID = TII->get(Opc); 4400b57cec5SDimitry Andric if (MCID.getNumOperands() == 3 && 4410b57cec5SDimitry Andric MI.getOperand(0).getReg() == MI.getOperand(1).getReg() && 4420b57cec5SDimitry Andric MI.getOperand(0).getReg() == MI.getOperand(2).getReg()) { 4430b57cec5SDimitry Andric NumberOfSelfCopies++; 4440b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: "); 4450b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 4460b57cec5SDimitry Andric InstrsToErase.push_back(&MI); 4470b57cec5SDimitry Andric continue; 4480b57cec5SDimitry Andric } 4490b57cec5SDimitry Andric else if (MCID.getNumOperands() == 2 && 4500b57cec5SDimitry Andric MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) { 4510b57cec5SDimitry Andric NumberOfSelfCopies++; 4520b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: "); 4530b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 4540b57cec5SDimitry Andric InstrsToErase.push_back(&MI); 4550b57cec5SDimitry Andric continue; 4560b57cec5SDimitry Andric } 4570b57cec5SDimitry Andric } 4580b57cec5SDimitry Andric MachineInstr *DefMIToErase = nullptr; 4590b57cec5SDimitry Andric if (TII->convertToImmediateForm(MI, &DefMIToErase)) { 4600b57cec5SDimitry Andric Changed = true; 4610b57cec5SDimitry Andric NumRRConvertedInPreEmit++; 4620b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Converted instruction to imm form: "); 4630b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 4640b57cec5SDimitry Andric if (DefMIToErase) { 4650b57cec5SDimitry Andric InstrsToErase.push_back(DefMIToErase); 4660b57cec5SDimitry Andric } 4670b57cec5SDimitry Andric } 468480093f4SDimitry Andric if (TII->foldFrameOffset(MI)) { 469480093f4SDimitry Andric Changed = true; 470480093f4SDimitry Andric NumFrameOffFoldInPreEmit++; 471480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "Frame offset folding by using index form: "); 472480093f4SDimitry Andric LLVM_DEBUG(MI.dump()); 473480093f4SDimitry Andric } 4740b57cec5SDimitry Andric } 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric // Eliminate conditional branch based on a constant CR bit by 4770b57cec5SDimitry Andric // CRSET or CRUNSET. We eliminate the conditional branch or 4780b57cec5SDimitry Andric // convert it into an unconditional branch. Also, if the CR bit 4790b57cec5SDimitry Andric // is not used by other instructions, we eliminate CRSET as well. 4800b57cec5SDimitry Andric auto I = MBB.getFirstInstrTerminator(); 4810b57cec5SDimitry Andric if (I == MBB.instr_end()) 4820b57cec5SDimitry Andric continue; 4830b57cec5SDimitry Andric MachineInstr *Br = &*I; 4840b57cec5SDimitry Andric if (Br->getOpcode() != PPC::BC && Br->getOpcode() != PPC::BCn) 4850b57cec5SDimitry Andric continue; 4860b57cec5SDimitry Andric MachineInstr *CRSetMI = nullptr; 4878bcb0991SDimitry Andric Register CRBit = Br->getOperand(0).getReg(); 4880b57cec5SDimitry Andric unsigned CRReg = getCRFromCRBit(CRBit); 4890b57cec5SDimitry Andric bool SeenUse = false; 4900b57cec5SDimitry Andric MachineBasicBlock::reverse_iterator It = Br, Er = MBB.rend(); 4910b57cec5SDimitry Andric for (It++; It != Er; It++) { 4920b57cec5SDimitry Andric if (It->modifiesRegister(CRBit, TRI)) { 4930b57cec5SDimitry Andric if ((It->getOpcode() == PPC::CRUNSET || 4940b57cec5SDimitry Andric It->getOpcode() == PPC::CRSET) && 4950b57cec5SDimitry Andric It->getOperand(0).getReg() == CRBit) 4960b57cec5SDimitry Andric CRSetMI = &*It; 4970b57cec5SDimitry Andric break; 4980b57cec5SDimitry Andric } 4990b57cec5SDimitry Andric if (It->readsRegister(CRBit, TRI)) 5000b57cec5SDimitry Andric SeenUse = true; 5010b57cec5SDimitry Andric } 5020b57cec5SDimitry Andric if (!CRSetMI) continue; 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric unsigned CRSetOp = CRSetMI->getOpcode(); 5050b57cec5SDimitry Andric if ((Br->getOpcode() == PPC::BCn && CRSetOp == PPC::CRSET) || 5060b57cec5SDimitry Andric (Br->getOpcode() == PPC::BC && CRSetOp == PPC::CRUNSET)) { 5070b57cec5SDimitry Andric // Remove this branch since it cannot be taken. 5080b57cec5SDimitry Andric InstrsToErase.push_back(Br); 5090b57cec5SDimitry Andric MBB.removeSuccessor(Br->getOperand(1).getMBB()); 5100b57cec5SDimitry Andric } 5110b57cec5SDimitry Andric else { 5120b57cec5SDimitry Andric // This conditional branch is always taken. So, remove all branches 5130b57cec5SDimitry Andric // and insert an unconditional branch to the destination of this. 5140b57cec5SDimitry Andric MachineBasicBlock::iterator It = Br, Er = MBB.end(); 5150b57cec5SDimitry Andric for (; It != Er; It++) { 5160b57cec5SDimitry Andric if (It->isDebugInstr()) continue; 5170b57cec5SDimitry Andric assert(It->isTerminator() && "Non-terminator after a terminator"); 5180b57cec5SDimitry Andric InstrsToErase.push_back(&*It); 5190b57cec5SDimitry Andric } 5200b57cec5SDimitry Andric if (!MBB.isLayoutSuccessor(Br->getOperand(1).getMBB())) { 5210b57cec5SDimitry Andric ArrayRef<MachineOperand> NoCond; 5220b57cec5SDimitry Andric TII->insertBranch(MBB, Br->getOperand(1).getMBB(), nullptr, 5230b57cec5SDimitry Andric NoCond, Br->getDebugLoc()); 5240b57cec5SDimitry Andric } 5250b57cec5SDimitry Andric for (auto &Succ : MBB.successors()) 5260b57cec5SDimitry Andric if (Succ != Br->getOperand(1).getMBB()) { 5270b57cec5SDimitry Andric MBB.removeSuccessor(Succ); 5280b57cec5SDimitry Andric break; 5290b57cec5SDimitry Andric } 5300b57cec5SDimitry Andric } 5310b57cec5SDimitry Andric 5320b57cec5SDimitry Andric // If the CRBit is not used by another instruction, we can eliminate 5330b57cec5SDimitry Andric // CRSET/CRUNSET instruction. 5340b57cec5SDimitry Andric if (!SeenUse) { 5350b57cec5SDimitry Andric // We need to check use of the CRBit in successors. 5360b57cec5SDimitry Andric for (auto &SuccMBB : MBB.successors()) 5370b57cec5SDimitry Andric if (SuccMBB->isLiveIn(CRBit) || SuccMBB->isLiveIn(CRReg)) { 5380b57cec5SDimitry Andric SeenUse = true; 5390b57cec5SDimitry Andric break; 5400b57cec5SDimitry Andric } 5410b57cec5SDimitry Andric if (!SeenUse) 5420b57cec5SDimitry Andric InstrsToErase.push_back(CRSetMI); 5430b57cec5SDimitry Andric } 5440b57cec5SDimitry Andric } 5450b57cec5SDimitry Andric for (MachineInstr *MI : InstrsToErase) { 5460b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "PPC pre-emit peephole: erasing instruction: "); 5470b57cec5SDimitry Andric LLVM_DEBUG(MI->dump()); 5480b57cec5SDimitry Andric MI->eraseFromParent(); 5490b57cec5SDimitry Andric NumRemovedInPreEmit++; 5500b57cec5SDimitry Andric } 5510b57cec5SDimitry Andric return Changed; 5520b57cec5SDimitry Andric } 5530b57cec5SDimitry Andric }; 5540b57cec5SDimitry Andric } 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric INITIALIZE_PASS(PPCPreEmitPeephole, DEBUG_TYPE, "PowerPC Pre-Emit Peephole", 5570b57cec5SDimitry Andric false, false) 5580b57cec5SDimitry Andric char PPCPreEmitPeephole::ID = 0; 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric FunctionPass *llvm::createPPCPreEmitPeepholePass() { 5610b57cec5SDimitry Andric return new PPCPreEmitPeephole(); 5620b57cec5SDimitry Andric } 563