xref: /freebsd/contrib/llvm-project/llvm/lib/Target/PowerPC/PPCPreEmitPeephole.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
2481ad6265SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
25e8d8bef9SDimitry Andric #include "llvm/MC/MCContext.h"
260b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
270b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric using namespace llvm;
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric #define DEBUG_TYPE "ppc-pre-emit-peephole"
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric STATISTIC(NumRRConvertedInPreEmit,
340b57cec5SDimitry Andric           "Number of r+r instructions converted to r+i in pre-emit peephole");
350b57cec5SDimitry Andric STATISTIC(NumRemovedInPreEmit,
360b57cec5SDimitry Andric           "Number of instructions deleted in pre-emit peephole");
370b57cec5SDimitry Andric STATISTIC(NumberOfSelfCopies,
380b57cec5SDimitry Andric           "Number of self copy instructions eliminated");
39480093f4SDimitry Andric STATISTIC(NumFrameOffFoldInPreEmit,
40480093f4SDimitry Andric           "Number of folding frame offset by using r+r in pre-emit peephole");
41*bdd1243dSDimitry Andric STATISTIC(NumCmpsInPreEmit,
42*bdd1243dSDimitry Andric           "Number of compares eliminated in pre-emit peephole");
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric static cl::opt<bool>
45e8d8bef9SDimitry Andric EnablePCRelLinkerOpt("ppc-pcrel-linker-opt", cl::Hidden, cl::init(true),
46e8d8bef9SDimitry Andric                      cl::desc("enable PC Relative linker optimization"));
47e8d8bef9SDimitry Andric 
48e8d8bef9SDimitry Andric static cl::opt<bool>
490b57cec5SDimitry Andric RunPreEmitPeephole("ppc-late-peephole", cl::Hidden, cl::init(true),
500b57cec5SDimitry Andric                    cl::desc("Run pre-emit peephole optimizations."));
510b57cec5SDimitry Andric 
5281ad6265SDimitry Andric static cl::opt<uint64_t>
5381ad6265SDimitry Andric DSCRValue("ppc-set-dscr", cl::Hidden,
5481ad6265SDimitry Andric           cl::desc("Set the Data Stream Control Register."));
5581ad6265SDimitry Andric 
560b57cec5SDimitry Andric namespace {
57e8d8bef9SDimitry Andric 
58e8d8bef9SDimitry Andric static bool hasPCRelativeForm(MachineInstr &Use) {
59e8d8bef9SDimitry Andric   switch (Use.getOpcode()) {
60e8d8bef9SDimitry Andric   default:
61e8d8bef9SDimitry Andric     return false;
62e8d8bef9SDimitry Andric   case PPC::LBZ:
63e8d8bef9SDimitry Andric   case PPC::LBZ8:
64e8d8bef9SDimitry Andric   case PPC::LHA:
65e8d8bef9SDimitry Andric   case PPC::LHA8:
66e8d8bef9SDimitry Andric   case PPC::LHZ:
67e8d8bef9SDimitry Andric   case PPC::LHZ8:
68e8d8bef9SDimitry Andric   case PPC::LWZ:
69e8d8bef9SDimitry Andric   case PPC::LWZ8:
70e8d8bef9SDimitry Andric   case PPC::STB:
71e8d8bef9SDimitry Andric   case PPC::STB8:
72e8d8bef9SDimitry Andric   case PPC::STH:
73e8d8bef9SDimitry Andric   case PPC::STH8:
74e8d8bef9SDimitry Andric   case PPC::STW:
75e8d8bef9SDimitry Andric   case PPC::STW8:
76e8d8bef9SDimitry Andric   case PPC::LD:
77e8d8bef9SDimitry Andric   case PPC::STD:
78e8d8bef9SDimitry Andric   case PPC::LWA:
79e8d8bef9SDimitry Andric   case PPC::LXSD:
80e8d8bef9SDimitry Andric   case PPC::LXSSP:
81e8d8bef9SDimitry Andric   case PPC::LXV:
82e8d8bef9SDimitry Andric   case PPC::STXSD:
83e8d8bef9SDimitry Andric   case PPC::STXSSP:
84e8d8bef9SDimitry Andric   case PPC::STXV:
85e8d8bef9SDimitry Andric   case PPC::LFD:
86e8d8bef9SDimitry Andric   case PPC::LFS:
87e8d8bef9SDimitry Andric   case PPC::STFD:
88e8d8bef9SDimitry Andric   case PPC::STFS:
89e8d8bef9SDimitry Andric   case PPC::DFLOADf32:
90e8d8bef9SDimitry Andric   case PPC::DFLOADf64:
91e8d8bef9SDimitry Andric   case PPC::DFSTOREf32:
92e8d8bef9SDimitry Andric   case PPC::DFSTOREf64:
93e8d8bef9SDimitry Andric     return true;
94e8d8bef9SDimitry Andric   }
95e8d8bef9SDimitry Andric }
96e8d8bef9SDimitry Andric 
970b57cec5SDimitry Andric   class PPCPreEmitPeephole : public MachineFunctionPass {
980b57cec5SDimitry Andric   public:
990b57cec5SDimitry Andric     static char ID;
1000b57cec5SDimitry Andric     PPCPreEmitPeephole() : MachineFunctionPass(ID) {
1010b57cec5SDimitry Andric       initializePPCPreEmitPeepholePass(*PassRegistry::getPassRegistry());
1020b57cec5SDimitry Andric     }
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
1050b57cec5SDimitry Andric       MachineFunctionPass::getAnalysisUsage(AU);
1060b57cec5SDimitry Andric     }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric     MachineFunctionProperties getRequiredProperties() const override {
1090b57cec5SDimitry Andric       return MachineFunctionProperties().set(
1100b57cec5SDimitry Andric           MachineFunctionProperties::Property::NoVRegs);
1110b57cec5SDimitry Andric     }
1120b57cec5SDimitry Andric 
1138bcb0991SDimitry Andric     // This function removes any redundant load immediates. It has two level
1148bcb0991SDimitry Andric     // loops - The outer loop finds the load immediates BBI that could be used
1158bcb0991SDimitry Andric     // to replace following redundancy. The inner loop scans instructions that
1168bcb0991SDimitry Andric     // after BBI to find redundancy and update kill/dead flags accordingly. If
1178bcb0991SDimitry Andric     // AfterBBI is the same as BBI, it is redundant, otherwise any instructions
1188bcb0991SDimitry Andric     // that modify the def register of BBI would break the scanning.
1198bcb0991SDimitry Andric     // DeadOrKillToUnset is a pointer to the previous operand that had the
1208bcb0991SDimitry Andric     // kill/dead flag set. It keeps track of the def register of BBI, the use
1218bcb0991SDimitry Andric     // registers of AfterBBIs and the def registers of AfterBBIs.
1228bcb0991SDimitry Andric     bool removeRedundantLIs(MachineBasicBlock &MBB,
1238bcb0991SDimitry Andric                             const TargetRegisterInfo *TRI) {
1248bcb0991SDimitry Andric       LLVM_DEBUG(dbgs() << "Remove redundant load immediates from MBB:\n";
1258bcb0991SDimitry Andric                  MBB.dump(); dbgs() << "\n");
1268bcb0991SDimitry Andric 
1278bcb0991SDimitry Andric       DenseSet<MachineInstr *> InstrsToErase;
1288bcb0991SDimitry Andric       for (auto BBI = MBB.instr_begin(); BBI != MBB.instr_end(); ++BBI) {
1298bcb0991SDimitry Andric         // Skip load immediate that is marked to be erased later because it
1308bcb0991SDimitry Andric         // cannot be used to replace any other instructions.
131e8d8bef9SDimitry Andric         if (InstrsToErase.contains(&*BBI))
1328bcb0991SDimitry Andric           continue;
1338bcb0991SDimitry Andric         // Skip non-load immediate.
1348bcb0991SDimitry Andric         unsigned Opc = BBI->getOpcode();
1358bcb0991SDimitry Andric         if (Opc != PPC::LI && Opc != PPC::LI8 && Opc != PPC::LIS &&
1368bcb0991SDimitry Andric             Opc != PPC::LIS8)
1378bcb0991SDimitry Andric           continue;
1388bcb0991SDimitry Andric         // Skip load immediate, where the operand is a relocation (e.g., $r3 =
1398bcb0991SDimitry Andric         // LI target-flags(ppc-lo) %const.0).
1408bcb0991SDimitry Andric         if (!BBI->getOperand(1).isImm())
1418bcb0991SDimitry Andric           continue;
1428bcb0991SDimitry Andric         assert(BBI->getOperand(0).isReg() &&
1438bcb0991SDimitry Andric                "Expected a register for the first operand");
1448bcb0991SDimitry Andric 
1458bcb0991SDimitry Andric         LLVM_DEBUG(dbgs() << "Scanning after load immediate: "; BBI->dump(););
1468bcb0991SDimitry Andric 
1478bcb0991SDimitry Andric         Register Reg = BBI->getOperand(0).getReg();
1488bcb0991SDimitry Andric         int64_t Imm = BBI->getOperand(1).getImm();
1498bcb0991SDimitry Andric         MachineOperand *DeadOrKillToUnset = nullptr;
1508bcb0991SDimitry Andric         if (BBI->getOperand(0).isDead()) {
1518bcb0991SDimitry Andric           DeadOrKillToUnset = &BBI->getOperand(0);
1528bcb0991SDimitry Andric           LLVM_DEBUG(dbgs() << " Kill flag of " << *DeadOrKillToUnset
1538bcb0991SDimitry Andric                             << " from load immediate " << *BBI
1548bcb0991SDimitry Andric                             << " is a unsetting candidate\n");
1558bcb0991SDimitry Andric         }
1568bcb0991SDimitry Andric         // This loop scans instructions after BBI to see if there is any
1578bcb0991SDimitry Andric         // redundant load immediate.
1588bcb0991SDimitry Andric         for (auto AfterBBI = std::next(BBI); AfterBBI != MBB.instr_end();
1598bcb0991SDimitry Andric              ++AfterBBI) {
1608bcb0991SDimitry Andric           // Track the operand that kill Reg. We would unset the kill flag of
1618bcb0991SDimitry Andric           // the operand if there is a following redundant load immediate.
1628bcb0991SDimitry Andric           int KillIdx = AfterBBI->findRegisterUseOperandIdx(Reg, true, TRI);
1635ffd83dbSDimitry Andric 
1645ffd83dbSDimitry Andric           // We can't just clear implicit kills, so if we encounter one, stop
1655ffd83dbSDimitry Andric           // looking further.
1665ffd83dbSDimitry Andric           if (KillIdx != -1 && AfterBBI->getOperand(KillIdx).isImplicit()) {
1675ffd83dbSDimitry Andric             LLVM_DEBUG(dbgs()
1685ffd83dbSDimitry Andric                        << "Encountered an implicit kill, cannot proceed: ");
1695ffd83dbSDimitry Andric             LLVM_DEBUG(AfterBBI->dump());
1705ffd83dbSDimitry Andric             break;
1715ffd83dbSDimitry Andric           }
1725ffd83dbSDimitry Andric 
1738bcb0991SDimitry Andric           if (KillIdx != -1) {
1748bcb0991SDimitry Andric             assert(!DeadOrKillToUnset && "Shouldn't kill same register twice");
1758bcb0991SDimitry Andric             DeadOrKillToUnset = &AfterBBI->getOperand(KillIdx);
1768bcb0991SDimitry Andric             LLVM_DEBUG(dbgs()
1778bcb0991SDimitry Andric                        << " Kill flag of " << *DeadOrKillToUnset << " from "
1788bcb0991SDimitry Andric                        << *AfterBBI << " is a unsetting candidate\n");
1798bcb0991SDimitry Andric           }
1808bcb0991SDimitry Andric 
1818bcb0991SDimitry Andric           if (!AfterBBI->modifiesRegister(Reg, TRI))
1828bcb0991SDimitry Andric             continue;
1838bcb0991SDimitry Andric           // Finish scanning because Reg is overwritten by a non-load
1848bcb0991SDimitry Andric           // instruction.
1858bcb0991SDimitry Andric           if (AfterBBI->getOpcode() != Opc)
1868bcb0991SDimitry Andric             break;
1878bcb0991SDimitry Andric           assert(AfterBBI->getOperand(0).isReg() &&
1888bcb0991SDimitry Andric                  "Expected a register for the first operand");
1898bcb0991SDimitry Andric           // Finish scanning because Reg is overwritten by a relocation or a
1908bcb0991SDimitry Andric           // different value.
1918bcb0991SDimitry Andric           if (!AfterBBI->getOperand(1).isImm() ||
1928bcb0991SDimitry Andric               AfterBBI->getOperand(1).getImm() != Imm)
1938bcb0991SDimitry Andric             break;
1948bcb0991SDimitry Andric 
1958bcb0991SDimitry Andric           // It loads same immediate value to the same Reg, which is redundant.
1968bcb0991SDimitry Andric           // We would unset kill flag in previous Reg usage to extend live range
1978bcb0991SDimitry Andric           // of Reg first, then remove the redundancy.
1988bcb0991SDimitry Andric           if (DeadOrKillToUnset) {
1998bcb0991SDimitry Andric             LLVM_DEBUG(dbgs()
2008bcb0991SDimitry Andric                        << " Unset dead/kill flag of " << *DeadOrKillToUnset
2018bcb0991SDimitry Andric                        << " from " << *DeadOrKillToUnset->getParent());
2028bcb0991SDimitry Andric             if (DeadOrKillToUnset->isDef())
2038bcb0991SDimitry Andric               DeadOrKillToUnset->setIsDead(false);
2048bcb0991SDimitry Andric             else
2058bcb0991SDimitry Andric               DeadOrKillToUnset->setIsKill(false);
2068bcb0991SDimitry Andric           }
2078bcb0991SDimitry Andric           DeadOrKillToUnset =
2088bcb0991SDimitry Andric               AfterBBI->findRegisterDefOperand(Reg, true, true, TRI);
2098bcb0991SDimitry Andric           if (DeadOrKillToUnset)
2108bcb0991SDimitry Andric             LLVM_DEBUG(dbgs()
2118bcb0991SDimitry Andric                        << " Dead flag of " << *DeadOrKillToUnset << " from "
2128bcb0991SDimitry Andric                        << *AfterBBI << " is a unsetting candidate\n");
2138bcb0991SDimitry Andric           InstrsToErase.insert(&*AfterBBI);
2148bcb0991SDimitry Andric           LLVM_DEBUG(dbgs() << " Remove redundant load immediate: ";
2158bcb0991SDimitry Andric                      AfterBBI->dump());
2168bcb0991SDimitry Andric         }
2178bcb0991SDimitry Andric       }
2188bcb0991SDimitry Andric 
2198bcb0991SDimitry Andric       for (MachineInstr *MI : InstrsToErase) {
2208bcb0991SDimitry Andric         MI->eraseFromParent();
2218bcb0991SDimitry Andric       }
2228bcb0991SDimitry Andric       NumRemovedInPreEmit += InstrsToErase.size();
2238bcb0991SDimitry Andric       return !InstrsToErase.empty();
2248bcb0991SDimitry Andric     }
2258bcb0991SDimitry Andric 
226e8d8bef9SDimitry Andric     // Check if this instruction is a PLDpc that is part of a GOT indirect
227e8d8bef9SDimitry Andric     // access.
228e8d8bef9SDimitry Andric     bool isGOTPLDpc(MachineInstr &Instr) {
229e8d8bef9SDimitry Andric       if (Instr.getOpcode() != PPC::PLDpc)
230e8d8bef9SDimitry Andric         return false;
231e8d8bef9SDimitry Andric 
232e8d8bef9SDimitry Andric       // The result must be a register.
233e8d8bef9SDimitry Andric       const MachineOperand &LoadedAddressReg = Instr.getOperand(0);
234e8d8bef9SDimitry Andric       if (!LoadedAddressReg.isReg())
235e8d8bef9SDimitry Andric         return false;
236e8d8bef9SDimitry Andric 
237e8d8bef9SDimitry Andric       // Make sure that this is a global symbol.
238e8d8bef9SDimitry Andric       const MachineOperand &SymbolOp = Instr.getOperand(1);
239e8d8bef9SDimitry Andric       if (!SymbolOp.isGlobal())
240e8d8bef9SDimitry Andric         return false;
241e8d8bef9SDimitry Andric 
242e8d8bef9SDimitry Andric       // Finally return true only if the GOT flag is present.
243e8d8bef9SDimitry Andric       return (SymbolOp.getTargetFlags() & PPCII::MO_GOT_FLAG);
244e8d8bef9SDimitry Andric     }
245e8d8bef9SDimitry Andric 
246e8d8bef9SDimitry Andric     bool addLinkerOpt(MachineBasicBlock &MBB, const TargetRegisterInfo *TRI) {
247e8d8bef9SDimitry Andric       MachineFunction *MF = MBB.getParent();
248e8d8bef9SDimitry Andric       // If the linker opt is disabled then just return.
249e8d8bef9SDimitry Andric       if (!EnablePCRelLinkerOpt)
250e8d8bef9SDimitry Andric         return false;
251e8d8bef9SDimitry Andric 
252e8d8bef9SDimitry Andric       // Add this linker opt only if we are using PC Relative memops.
253e8d8bef9SDimitry Andric       if (!MF->getSubtarget<PPCSubtarget>().isUsingPCRelativeCalls())
254e8d8bef9SDimitry Andric         return false;
255e8d8bef9SDimitry Andric 
256e8d8bef9SDimitry Andric       // Struct to keep track of one def/use pair for a GOT indirect access.
257e8d8bef9SDimitry Andric       struct GOTDefUsePair {
258e8d8bef9SDimitry Andric         MachineBasicBlock::iterator DefInst;
259e8d8bef9SDimitry Andric         MachineBasicBlock::iterator UseInst;
260e8d8bef9SDimitry Andric         Register DefReg;
261e8d8bef9SDimitry Andric         Register UseReg;
262e8d8bef9SDimitry Andric         bool StillValid;
263e8d8bef9SDimitry Andric       };
264e8d8bef9SDimitry Andric       // Vector of def/ues pairs in this basic block.
265e8d8bef9SDimitry Andric       SmallVector<GOTDefUsePair, 4> CandPairs;
266e8d8bef9SDimitry Andric       SmallVector<GOTDefUsePair, 4> ValidPairs;
267e8d8bef9SDimitry Andric       bool MadeChange = false;
268e8d8bef9SDimitry Andric 
269e8d8bef9SDimitry Andric       // Run through all of the instructions in the basic block and try to
270e8d8bef9SDimitry Andric       // collect potential pairs of GOT indirect access instructions.
271e8d8bef9SDimitry Andric       for (auto BBI = MBB.instr_begin(); BBI != MBB.instr_end(); ++BBI) {
272e8d8bef9SDimitry Andric         // Look for the initial GOT indirect load.
273e8d8bef9SDimitry Andric         if (isGOTPLDpc(*BBI)) {
274e8d8bef9SDimitry Andric           GOTDefUsePair CurrentPair{BBI, MachineBasicBlock::iterator(),
275e8d8bef9SDimitry Andric                                     BBI->getOperand(0).getReg(),
276e8d8bef9SDimitry Andric                                     PPC::NoRegister, true};
277e8d8bef9SDimitry Andric           CandPairs.push_back(CurrentPair);
278e8d8bef9SDimitry Andric           continue;
279e8d8bef9SDimitry Andric         }
280e8d8bef9SDimitry Andric 
281e8d8bef9SDimitry Andric         // We haven't encountered any new PLD instructions, nothing to check.
282e8d8bef9SDimitry Andric         if (CandPairs.empty())
283e8d8bef9SDimitry Andric           continue;
284e8d8bef9SDimitry Andric 
285e8d8bef9SDimitry Andric         // Run through the candidate pairs and see if any of the registers
286e8d8bef9SDimitry Andric         // defined in the PLD instructions are used by this instruction.
287e8d8bef9SDimitry Andric         // Note: the size of CandPairs can change in the loop.
288e8d8bef9SDimitry Andric         for (unsigned Idx = 0; Idx < CandPairs.size(); Idx++) {
289e8d8bef9SDimitry Andric           GOTDefUsePair &Pair = CandPairs[Idx];
290e8d8bef9SDimitry Andric           // The instruction does not use or modify this PLD's def reg,
291e8d8bef9SDimitry Andric           // ignore it.
292e8d8bef9SDimitry Andric           if (!BBI->readsRegister(Pair.DefReg, TRI) &&
293e8d8bef9SDimitry Andric               !BBI->modifiesRegister(Pair.DefReg, TRI))
294e8d8bef9SDimitry Andric             continue;
295e8d8bef9SDimitry Andric 
296*bdd1243dSDimitry Andric           // The use needs to be used in the address computation and not
297e8d8bef9SDimitry Andric           // as the register being stored for a store.
298e8d8bef9SDimitry Andric           const MachineOperand *UseOp =
299e8d8bef9SDimitry Andric               hasPCRelativeForm(*BBI) ? &BBI->getOperand(2) : nullptr;
300e8d8bef9SDimitry Andric 
301e8d8bef9SDimitry Andric           // Check for a valid use.
302e8d8bef9SDimitry Andric           if (UseOp && UseOp->isReg() && UseOp->getReg() == Pair.DefReg &&
303e8d8bef9SDimitry Andric               UseOp->isUse() && UseOp->isKill()) {
304e8d8bef9SDimitry Andric             Pair.UseInst = BBI;
305e8d8bef9SDimitry Andric             Pair.UseReg = BBI->getOperand(0).getReg();
306e8d8bef9SDimitry Andric             ValidPairs.push_back(Pair);
307e8d8bef9SDimitry Andric           }
308e8d8bef9SDimitry Andric           CandPairs.erase(CandPairs.begin() + Idx);
309e8d8bef9SDimitry Andric         }
310e8d8bef9SDimitry Andric       }
311e8d8bef9SDimitry Andric 
312e8d8bef9SDimitry Andric       // Go through all of the pairs and check for any more valid uses.
313e8d8bef9SDimitry Andric       for (auto Pair = ValidPairs.begin(); Pair != ValidPairs.end(); Pair++) {
314e8d8bef9SDimitry Andric         // We shouldn't be here if we don't have a valid pair.
315e8d8bef9SDimitry Andric         assert(Pair->UseInst.isValid() && Pair->StillValid &&
316e8d8bef9SDimitry Andric                "Kept an invalid def/use pair for GOT PCRel opt");
317e8d8bef9SDimitry Andric         // We have found a potential pair. Search through the instructions
318e8d8bef9SDimitry Andric         // between the def and the use to see if it is valid to mark this as a
319e8d8bef9SDimitry Andric         // linker opt.
320e8d8bef9SDimitry Andric         MachineBasicBlock::iterator BBI = Pair->DefInst;
321e8d8bef9SDimitry Andric         ++BBI;
322e8d8bef9SDimitry Andric         for (; BBI != Pair->UseInst; ++BBI) {
323e8d8bef9SDimitry Andric           if (BBI->readsRegister(Pair->UseReg, TRI) ||
324e8d8bef9SDimitry Andric               BBI->modifiesRegister(Pair->UseReg, TRI)) {
325e8d8bef9SDimitry Andric             Pair->StillValid = false;
326e8d8bef9SDimitry Andric             break;
327e8d8bef9SDimitry Andric           }
328e8d8bef9SDimitry Andric         }
329e8d8bef9SDimitry Andric 
330e8d8bef9SDimitry Andric         if (!Pair->StillValid)
331e8d8bef9SDimitry Andric           continue;
332e8d8bef9SDimitry Andric 
333e8d8bef9SDimitry Andric         // The load/store instruction that uses the address from the PLD will
334e8d8bef9SDimitry Andric         // either use a register (for a store) or define a register (for the
335e8d8bef9SDimitry Andric         // load). That register will be added as an implicit def to the PLD
336e8d8bef9SDimitry Andric         // and as an implicit use on the second memory op. This is a precaution
337e8d8bef9SDimitry Andric         // to prevent future passes from using that register between the two
338e8d8bef9SDimitry Andric         // instructions.
339e8d8bef9SDimitry Andric         MachineOperand ImplDef =
340e8d8bef9SDimitry Andric             MachineOperand::CreateReg(Pair->UseReg, true, true);
341e8d8bef9SDimitry Andric         MachineOperand ImplUse =
342e8d8bef9SDimitry Andric             MachineOperand::CreateReg(Pair->UseReg, false, true);
343e8d8bef9SDimitry Andric         Pair->DefInst->addOperand(ImplDef);
344e8d8bef9SDimitry Andric         Pair->UseInst->addOperand(ImplUse);
345e8d8bef9SDimitry Andric 
346e8d8bef9SDimitry Andric         // Create the symbol.
347e8d8bef9SDimitry Andric         MCContext &Context = MF->getContext();
348e8d8bef9SDimitry Andric         MCSymbol *Symbol = Context.createNamedTempSymbol("pcrel");
349e8d8bef9SDimitry Andric         MachineOperand PCRelLabel =
350e8d8bef9SDimitry Andric             MachineOperand::CreateMCSymbol(Symbol, PPCII::MO_PCREL_OPT_FLAG);
351e8d8bef9SDimitry Andric         Pair->DefInst->addOperand(*MF, PCRelLabel);
352e8d8bef9SDimitry Andric         Pair->UseInst->addOperand(*MF, PCRelLabel);
353e8d8bef9SDimitry Andric         MadeChange |= true;
354e8d8bef9SDimitry Andric       }
355e8d8bef9SDimitry Andric       return MadeChange;
356e8d8bef9SDimitry Andric     }
357e8d8bef9SDimitry Andric 
358e8d8bef9SDimitry Andric     // This function removes redundant pairs of accumulator prime/unprime
359e8d8bef9SDimitry Andric     // instructions. In some situations, it's possible the compiler inserts an
360e8d8bef9SDimitry Andric     // accumulator prime instruction followed by an unprime instruction (e.g.
361e8d8bef9SDimitry Andric     // when we store an accumulator after restoring it from a spill). If the
362e8d8bef9SDimitry Andric     // accumulator is not used between the two, they can be removed. This
363e8d8bef9SDimitry Andric     // function removes these redundant pairs from basic blocks.
364e8d8bef9SDimitry Andric     // The algorithm is quite straightforward - every time we encounter a prime
365e8d8bef9SDimitry Andric     // instruction, the primed register is added to a candidate set. Any use
366e8d8bef9SDimitry Andric     // other than a prime removes the candidate from the set and any de-prime
367e8d8bef9SDimitry Andric     // of a current candidate marks both the prime and de-prime for removal.
368e8d8bef9SDimitry Andric     // This way we ensure we only remove prime/de-prime *pairs* with no
369e8d8bef9SDimitry Andric     // intervening uses.
370e8d8bef9SDimitry Andric     bool removeAccPrimeUnprime(MachineBasicBlock &MBB) {
371e8d8bef9SDimitry Andric       DenseSet<MachineInstr *> InstrsToErase;
372e8d8bef9SDimitry Andric       // Initially, none of the acc registers are candidates.
373e8d8bef9SDimitry Andric       SmallVector<MachineInstr *, 8> Candidates(
374e8d8bef9SDimitry Andric           PPC::UACCRCRegClass.getNumRegs(), nullptr);
375e8d8bef9SDimitry Andric 
376e8d8bef9SDimitry Andric       for (MachineInstr &BBI : MBB.instrs()) {
377e8d8bef9SDimitry Andric         unsigned Opc = BBI.getOpcode();
378e8d8bef9SDimitry Andric         // If we are visiting a xxmtacc instruction, we add it and its operand
379e8d8bef9SDimitry Andric         // register to the candidate set.
380e8d8bef9SDimitry Andric         if (Opc == PPC::XXMTACC) {
381e8d8bef9SDimitry Andric           Register Acc = BBI.getOperand(0).getReg();
382e8d8bef9SDimitry Andric           assert(PPC::ACCRCRegClass.contains(Acc) &&
383e8d8bef9SDimitry Andric                  "Unexpected register for XXMTACC");
384e8d8bef9SDimitry Andric           Candidates[Acc - PPC::ACC0] = &BBI;
385e8d8bef9SDimitry Andric         }
386e8d8bef9SDimitry Andric         // If we are visiting a xxmfacc instruction and its operand register is
387e8d8bef9SDimitry Andric         // in the candidate set, we mark the two instructions for removal.
388e8d8bef9SDimitry Andric         else if (Opc == PPC::XXMFACC) {
389e8d8bef9SDimitry Andric           Register Acc = BBI.getOperand(0).getReg();
390e8d8bef9SDimitry Andric           assert(PPC::ACCRCRegClass.contains(Acc) &&
391e8d8bef9SDimitry Andric                  "Unexpected register for XXMFACC");
392e8d8bef9SDimitry Andric           if (!Candidates[Acc - PPC::ACC0])
393e8d8bef9SDimitry Andric             continue;
394e8d8bef9SDimitry Andric           InstrsToErase.insert(&BBI);
395e8d8bef9SDimitry Andric           InstrsToErase.insert(Candidates[Acc - PPC::ACC0]);
396e8d8bef9SDimitry Andric         }
397e8d8bef9SDimitry Andric         // If we are visiting an instruction using an accumulator register
398e8d8bef9SDimitry Andric         // as operand, we remove it from the candidate set.
399e8d8bef9SDimitry Andric         else {
400e8d8bef9SDimitry Andric           for (MachineOperand &Operand : BBI.operands()) {
401e8d8bef9SDimitry Andric             if (!Operand.isReg())
402e8d8bef9SDimitry Andric               continue;
403e8d8bef9SDimitry Andric             Register Reg = Operand.getReg();
404e8d8bef9SDimitry Andric             if (PPC::ACCRCRegClass.contains(Reg))
405e8d8bef9SDimitry Andric               Candidates[Reg - PPC::ACC0] = nullptr;
406e8d8bef9SDimitry Andric           }
407e8d8bef9SDimitry Andric         }
408e8d8bef9SDimitry Andric       }
409e8d8bef9SDimitry Andric 
410e8d8bef9SDimitry Andric       for (MachineInstr *MI : InstrsToErase)
411e8d8bef9SDimitry Andric         MI->eraseFromParent();
412e8d8bef9SDimitry Andric       NumRemovedInPreEmit += InstrsToErase.size();
413e8d8bef9SDimitry Andric       return !InstrsToErase.empty();
414e8d8bef9SDimitry Andric     }
415e8d8bef9SDimitry Andric 
4160b57cec5SDimitry Andric     bool runOnMachineFunction(MachineFunction &MF) override {
41781ad6265SDimitry Andric       // If the user wants to set the DSCR using command-line options,
41881ad6265SDimitry Andric       // load in the specified value at the start of main.
41981ad6265SDimitry Andric       if (DSCRValue.getNumOccurrences() > 0 && MF.getName().equals("main") &&
42081ad6265SDimitry Andric           MF.getFunction().hasExternalLinkage()) {
42181ad6265SDimitry Andric         DSCRValue = (uint32_t)(DSCRValue & 0x01FFFFFF); // 25-bit DSCR mask
42281ad6265SDimitry Andric         RegScavenger RS;
42381ad6265SDimitry Andric         MachineBasicBlock &MBB = MF.front();
42481ad6265SDimitry Andric         // Find an unused GPR according to register liveness
42581ad6265SDimitry Andric         RS.enterBasicBlock(MBB);
42681ad6265SDimitry Andric         unsigned InDSCR = RS.FindUnusedReg(&PPC::GPRCRegClass);
42781ad6265SDimitry Andric         if (InDSCR) {
42881ad6265SDimitry Andric           const PPCInstrInfo *TII =
42981ad6265SDimitry Andric               MF.getSubtarget<PPCSubtarget>().getInstrInfo();
43081ad6265SDimitry Andric           DebugLoc dl;
43181ad6265SDimitry Andric           MachineBasicBlock::iterator IP = MBB.begin(); // Insert Point
43281ad6265SDimitry Andric           // Copy the 32-bit DSCRValue integer into the GPR InDSCR using LIS and
43381ad6265SDimitry Andric           // ORI, then move to DSCR. If the requested DSCR value is contained
43481ad6265SDimitry Andric           // in a 16-bit signed number, we can emit a single `LI`, but the
43581ad6265SDimitry Andric           // impact of saving one instruction in one function does not warrant
43681ad6265SDimitry Andric           // any additional complexity in the logic here.
43781ad6265SDimitry Andric           BuildMI(MBB, IP, dl, TII->get(PPC::LIS), InDSCR)
43881ad6265SDimitry Andric               .addImm(DSCRValue >> 16);
43981ad6265SDimitry Andric           BuildMI(MBB, IP, dl, TII->get(PPC::ORI), InDSCR)
44081ad6265SDimitry Andric               .addReg(InDSCR)
44181ad6265SDimitry Andric               .addImm(DSCRValue & 0xFFFF);
44281ad6265SDimitry Andric           BuildMI(MBB, IP, dl, TII->get(PPC::MTUDSCR))
44381ad6265SDimitry Andric               .addReg(InDSCR, RegState::Kill);
44481ad6265SDimitry Andric         } else
44581ad6265SDimitry Andric           errs() << "Warning: Ran out of registers - Unable to set DSCR as "
44681ad6265SDimitry Andric                     "requested";
44781ad6265SDimitry Andric       }
44881ad6265SDimitry Andric 
449480093f4SDimitry Andric       if (skipFunction(MF.getFunction()) || !RunPreEmitPeephole) {
450480093f4SDimitry Andric         // Remove UNENCODED_NOP even when this pass is disabled.
451480093f4SDimitry Andric         // This needs to be done unconditionally so we don't emit zeros
452480093f4SDimitry Andric         // in the instruction stream.
453480093f4SDimitry Andric         SmallVector<MachineInstr *, 4> InstrsToErase;
454480093f4SDimitry Andric         for (MachineBasicBlock &MBB : MF)
455480093f4SDimitry Andric           for (MachineInstr &MI : MBB)
456480093f4SDimitry Andric             if (MI.getOpcode() == PPC::UNENCODED_NOP)
457480093f4SDimitry Andric               InstrsToErase.push_back(&MI);
458480093f4SDimitry Andric         for (MachineInstr *MI : InstrsToErase)
459480093f4SDimitry Andric           MI->eraseFromParent();
4600b57cec5SDimitry Andric         return false;
461480093f4SDimitry Andric       }
4620b57cec5SDimitry Andric       bool Changed = false;
4630b57cec5SDimitry Andric       const PPCInstrInfo *TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
4640b57cec5SDimitry Andric       const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
4650b57cec5SDimitry Andric       SmallVector<MachineInstr *, 4> InstrsToErase;
4660b57cec5SDimitry Andric       for (MachineBasicBlock &MBB : MF) {
4678bcb0991SDimitry Andric         Changed |= removeRedundantLIs(MBB, TRI);
468e8d8bef9SDimitry Andric         Changed |= addLinkerOpt(MBB, TRI);
469e8d8bef9SDimitry Andric         Changed |= removeAccPrimeUnprime(MBB);
4700b57cec5SDimitry Andric         for (MachineInstr &MI : MBB) {
4710b57cec5SDimitry Andric           unsigned Opc = MI.getOpcode();
472480093f4SDimitry Andric           if (Opc == PPC::UNENCODED_NOP) {
473480093f4SDimitry Andric             InstrsToErase.push_back(&MI);
474480093f4SDimitry Andric             continue;
475480093f4SDimitry Andric           }
4760b57cec5SDimitry Andric           // Detect self copies - these can result from running AADB.
4770b57cec5SDimitry Andric           if (PPCInstrInfo::isSameClassPhysRegCopy(Opc)) {
4780b57cec5SDimitry Andric             const MCInstrDesc &MCID = TII->get(Opc);
4790b57cec5SDimitry Andric             if (MCID.getNumOperands() == 3 &&
4800b57cec5SDimitry Andric                 MI.getOperand(0).getReg() == MI.getOperand(1).getReg() &&
4810b57cec5SDimitry Andric                 MI.getOperand(0).getReg() == MI.getOperand(2).getReg()) {
4820b57cec5SDimitry Andric               NumberOfSelfCopies++;
4830b57cec5SDimitry Andric               LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: ");
4840b57cec5SDimitry Andric               LLVM_DEBUG(MI.dump());
4850b57cec5SDimitry Andric               InstrsToErase.push_back(&MI);
4860b57cec5SDimitry Andric               continue;
4870b57cec5SDimitry Andric             }
4880b57cec5SDimitry Andric             else if (MCID.getNumOperands() == 2 &&
4890b57cec5SDimitry Andric                      MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) {
4900b57cec5SDimitry Andric               NumberOfSelfCopies++;
4910b57cec5SDimitry Andric               LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: ");
4920b57cec5SDimitry Andric               LLVM_DEBUG(MI.dump());
4930b57cec5SDimitry Andric               InstrsToErase.push_back(&MI);
4940b57cec5SDimitry Andric               continue;
4950b57cec5SDimitry Andric             }
4960b57cec5SDimitry Andric           }
4970b57cec5SDimitry Andric           MachineInstr *DefMIToErase = nullptr;
4980b57cec5SDimitry Andric           if (TII->convertToImmediateForm(MI, &DefMIToErase)) {
4990b57cec5SDimitry Andric             Changed = true;
5000b57cec5SDimitry Andric             NumRRConvertedInPreEmit++;
5010b57cec5SDimitry Andric             LLVM_DEBUG(dbgs() << "Converted instruction to imm form: ");
5020b57cec5SDimitry Andric             LLVM_DEBUG(MI.dump());
5030b57cec5SDimitry Andric             if (DefMIToErase) {
5040b57cec5SDimitry Andric               InstrsToErase.push_back(DefMIToErase);
5050b57cec5SDimitry Andric             }
5060b57cec5SDimitry Andric           }
507480093f4SDimitry Andric           if (TII->foldFrameOffset(MI)) {
508480093f4SDimitry Andric             Changed = true;
509480093f4SDimitry Andric             NumFrameOffFoldInPreEmit++;
510480093f4SDimitry Andric             LLVM_DEBUG(dbgs() << "Frame offset folding by using index form: ");
511480093f4SDimitry Andric             LLVM_DEBUG(MI.dump());
512480093f4SDimitry Andric           }
513*bdd1243dSDimitry Andric           if (TII->optimizeCmpPostRA(MI)) {
514*bdd1243dSDimitry Andric             Changed = true;
515*bdd1243dSDimitry Andric             NumCmpsInPreEmit++;
516*bdd1243dSDimitry Andric             LLVM_DEBUG(dbgs() << "Optimize compare by using record form: ");
517*bdd1243dSDimitry Andric             LLVM_DEBUG(MI.dump());
518*bdd1243dSDimitry Andric             InstrsToErase.push_back(&MI);
519*bdd1243dSDimitry Andric           }
5200b57cec5SDimitry Andric         }
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric         // Eliminate conditional branch based on a constant CR bit by
5230b57cec5SDimitry Andric         // CRSET or CRUNSET. We eliminate the conditional branch or
5240b57cec5SDimitry Andric         // convert it into an unconditional branch. Also, if the CR bit
5250b57cec5SDimitry Andric         // is not used by other instructions, we eliminate CRSET as well.
5260b57cec5SDimitry Andric         auto I = MBB.getFirstInstrTerminator();
5270b57cec5SDimitry Andric         if (I == MBB.instr_end())
5280b57cec5SDimitry Andric           continue;
5290b57cec5SDimitry Andric         MachineInstr *Br = &*I;
5300b57cec5SDimitry Andric         if (Br->getOpcode() != PPC::BC && Br->getOpcode() != PPC::BCn)
5310b57cec5SDimitry Andric           continue;
5320b57cec5SDimitry Andric         MachineInstr *CRSetMI = nullptr;
5338bcb0991SDimitry Andric         Register CRBit = Br->getOperand(0).getReg();
5340b57cec5SDimitry Andric         unsigned CRReg = getCRFromCRBit(CRBit);
5350b57cec5SDimitry Andric         bool SeenUse = false;
5360b57cec5SDimitry Andric         MachineBasicBlock::reverse_iterator It = Br, Er = MBB.rend();
5370b57cec5SDimitry Andric         for (It++; It != Er; It++) {
5380b57cec5SDimitry Andric           if (It->modifiesRegister(CRBit, TRI)) {
5390b57cec5SDimitry Andric             if ((It->getOpcode() == PPC::CRUNSET ||
5400b57cec5SDimitry Andric                  It->getOpcode() == PPC::CRSET) &&
5410b57cec5SDimitry Andric                 It->getOperand(0).getReg() == CRBit)
5420b57cec5SDimitry Andric               CRSetMI = &*It;
5430b57cec5SDimitry Andric             break;
5440b57cec5SDimitry Andric           }
5450b57cec5SDimitry Andric           if (It->readsRegister(CRBit, TRI))
5460b57cec5SDimitry Andric             SeenUse = true;
5470b57cec5SDimitry Andric         }
5480b57cec5SDimitry Andric         if (!CRSetMI) continue;
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric         unsigned CRSetOp = CRSetMI->getOpcode();
5510b57cec5SDimitry Andric         if ((Br->getOpcode() == PPC::BCn && CRSetOp == PPC::CRSET) ||
5520b57cec5SDimitry Andric             (Br->getOpcode() == PPC::BC  && CRSetOp == PPC::CRUNSET)) {
5530b57cec5SDimitry Andric           // Remove this branch since it cannot be taken.
5540b57cec5SDimitry Andric           InstrsToErase.push_back(Br);
5550b57cec5SDimitry Andric           MBB.removeSuccessor(Br->getOperand(1).getMBB());
5560b57cec5SDimitry Andric         }
5570b57cec5SDimitry Andric         else {
5580b57cec5SDimitry Andric           // This conditional branch is always taken. So, remove all branches
5590b57cec5SDimitry Andric           // and insert an unconditional branch to the destination of this.
5600b57cec5SDimitry Andric           MachineBasicBlock::iterator It = Br, Er = MBB.end();
5610b57cec5SDimitry Andric           for (; It != Er; It++) {
5620b57cec5SDimitry Andric             if (It->isDebugInstr()) continue;
5630b57cec5SDimitry Andric             assert(It->isTerminator() && "Non-terminator after a terminator");
5640b57cec5SDimitry Andric             InstrsToErase.push_back(&*It);
5650b57cec5SDimitry Andric           }
5660b57cec5SDimitry Andric           if (!MBB.isLayoutSuccessor(Br->getOperand(1).getMBB())) {
5670b57cec5SDimitry Andric             ArrayRef<MachineOperand> NoCond;
5680b57cec5SDimitry Andric             TII->insertBranch(MBB, Br->getOperand(1).getMBB(), nullptr,
5690b57cec5SDimitry Andric                               NoCond, Br->getDebugLoc());
5700b57cec5SDimitry Andric           }
5710b57cec5SDimitry Andric           for (auto &Succ : MBB.successors())
5720b57cec5SDimitry Andric             if (Succ != Br->getOperand(1).getMBB()) {
5730b57cec5SDimitry Andric               MBB.removeSuccessor(Succ);
5740b57cec5SDimitry Andric               break;
5750b57cec5SDimitry Andric             }
5760b57cec5SDimitry Andric         }
5770b57cec5SDimitry Andric 
5780b57cec5SDimitry Andric         // If the CRBit is not used by another instruction, we can eliminate
5790b57cec5SDimitry Andric         // CRSET/CRUNSET instruction.
5800b57cec5SDimitry Andric         if (!SeenUse) {
5810b57cec5SDimitry Andric           // We need to check use of the CRBit in successors.
5820b57cec5SDimitry Andric           for (auto &SuccMBB : MBB.successors())
5830b57cec5SDimitry Andric             if (SuccMBB->isLiveIn(CRBit) || SuccMBB->isLiveIn(CRReg)) {
5840b57cec5SDimitry Andric               SeenUse = true;
5850b57cec5SDimitry Andric               break;
5860b57cec5SDimitry Andric             }
5870b57cec5SDimitry Andric           if (!SeenUse)
5880b57cec5SDimitry Andric             InstrsToErase.push_back(CRSetMI);
5890b57cec5SDimitry Andric         }
5900b57cec5SDimitry Andric       }
5910b57cec5SDimitry Andric       for (MachineInstr *MI : InstrsToErase) {
5920b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "PPC pre-emit peephole: erasing instruction: ");
5930b57cec5SDimitry Andric         LLVM_DEBUG(MI->dump());
5940b57cec5SDimitry Andric         MI->eraseFromParent();
5950b57cec5SDimitry Andric         NumRemovedInPreEmit++;
5960b57cec5SDimitry Andric       }
5970b57cec5SDimitry Andric       return Changed;
5980b57cec5SDimitry Andric     }
5990b57cec5SDimitry Andric   };
6000b57cec5SDimitry Andric }
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric INITIALIZE_PASS(PPCPreEmitPeephole, DEBUG_TYPE, "PowerPC Pre-Emit Peephole",
6030b57cec5SDimitry Andric                 false, false)
6040b57cec5SDimitry Andric char PPCPreEmitPeephole::ID = 0;
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric FunctionPass *llvm::createPPCPreEmitPeepholePass() {
6070b57cec5SDimitry Andric   return new PPCPreEmitPeephole();
6080b57cec5SDimitry Andric }
609