1 //===--------- PPCPreEmitPeephole.cpp - Late peephole optimizations -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // A pre-emit peephole for catching opportunities introduced by late passes such
10 // as MachineBlockPlacement.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "PPC.h"
15 #include "PPCInstrInfo.h"
16 #include "PPCSubtarget.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/CodeGen/LivePhysRegs.h"
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/Support/Debug.h"
27 
28 using namespace llvm;
29 
30 #define DEBUG_TYPE "ppc-pre-emit-peephole"
31 
32 STATISTIC(NumRRConvertedInPreEmit,
33           "Number of r+r instructions converted to r+i in pre-emit peephole");
34 STATISTIC(NumRemovedInPreEmit,
35           "Number of instructions deleted in pre-emit peephole");
36 STATISTIC(NumberOfSelfCopies,
37           "Number of self copy instructions eliminated");
38 STATISTIC(NumFrameOffFoldInPreEmit,
39           "Number of folding frame offset by using r+r in pre-emit peephole");
40 
41 static cl::opt<bool>
42 RunPreEmitPeephole("ppc-late-peephole", cl::Hidden, cl::init(true),
43                    cl::desc("Run pre-emit peephole optimizations."));
44 
45 namespace {
46   class PPCPreEmitPeephole : public MachineFunctionPass {
47   public:
48     static char ID;
49     PPCPreEmitPeephole() : MachineFunctionPass(ID) {
50       initializePPCPreEmitPeepholePass(*PassRegistry::getPassRegistry());
51     }
52 
53     void getAnalysisUsage(AnalysisUsage &AU) const override {
54       MachineFunctionPass::getAnalysisUsage(AU);
55     }
56 
57     MachineFunctionProperties getRequiredProperties() const override {
58       return MachineFunctionProperties().set(
59           MachineFunctionProperties::Property::NoVRegs);
60     }
61 
62     // This function removes any redundant load immediates. It has two level
63     // loops - The outer loop finds the load immediates BBI that could be used
64     // to replace following redundancy. The inner loop scans instructions that
65     // after BBI to find redundancy and update kill/dead flags accordingly. If
66     // AfterBBI is the same as BBI, it is redundant, otherwise any instructions
67     // that modify the def register of BBI would break the scanning.
68     // DeadOrKillToUnset is a pointer to the previous operand that had the
69     // kill/dead flag set. It keeps track of the def register of BBI, the use
70     // registers of AfterBBIs and the def registers of AfterBBIs.
71     bool removeRedundantLIs(MachineBasicBlock &MBB,
72                             const TargetRegisterInfo *TRI) {
73       LLVM_DEBUG(dbgs() << "Remove redundant load immediates from MBB:\n";
74                  MBB.dump(); dbgs() << "\n");
75 
76       DenseSet<MachineInstr *> InstrsToErase;
77       for (auto BBI = MBB.instr_begin(); BBI != MBB.instr_end(); ++BBI) {
78         // Skip load immediate that is marked to be erased later because it
79         // cannot be used to replace any other instructions.
80         if (InstrsToErase.find(&*BBI) != InstrsToErase.end())
81           continue;
82         // Skip non-load immediate.
83         unsigned Opc = BBI->getOpcode();
84         if (Opc != PPC::LI && Opc != PPC::LI8 && Opc != PPC::LIS &&
85             Opc != PPC::LIS8)
86           continue;
87         // Skip load immediate, where the operand is a relocation (e.g., $r3 =
88         // LI target-flags(ppc-lo) %const.0).
89         if (!BBI->getOperand(1).isImm())
90           continue;
91         assert(BBI->getOperand(0).isReg() &&
92                "Expected a register for the first operand");
93 
94         LLVM_DEBUG(dbgs() << "Scanning after load immediate: "; BBI->dump(););
95 
96         Register Reg = BBI->getOperand(0).getReg();
97         int64_t Imm = BBI->getOperand(1).getImm();
98         MachineOperand *DeadOrKillToUnset = nullptr;
99         if (BBI->getOperand(0).isDead()) {
100           DeadOrKillToUnset = &BBI->getOperand(0);
101           LLVM_DEBUG(dbgs() << " Kill flag of " << *DeadOrKillToUnset
102                             << " from load immediate " << *BBI
103                             << " is a unsetting candidate\n");
104         }
105         // This loop scans instructions after BBI to see if there is any
106         // redundant load immediate.
107         for (auto AfterBBI = std::next(BBI); AfterBBI != MBB.instr_end();
108              ++AfterBBI) {
109           // Track the operand that kill Reg. We would unset the kill flag of
110           // the operand if there is a following redundant load immediate.
111           int KillIdx = AfterBBI->findRegisterUseOperandIdx(Reg, true, TRI);
112           if (KillIdx != -1) {
113             assert(!DeadOrKillToUnset && "Shouldn't kill same register twice");
114             DeadOrKillToUnset = &AfterBBI->getOperand(KillIdx);
115             LLVM_DEBUG(dbgs()
116                        << " Kill flag of " << *DeadOrKillToUnset << " from "
117                        << *AfterBBI << " is a unsetting candidate\n");
118           }
119 
120           if (!AfterBBI->modifiesRegister(Reg, TRI))
121             continue;
122           // Finish scanning because Reg is overwritten by a non-load
123           // instruction.
124           if (AfterBBI->getOpcode() != Opc)
125             break;
126           assert(AfterBBI->getOperand(0).isReg() &&
127                  "Expected a register for the first operand");
128           // Finish scanning because Reg is overwritten by a relocation or a
129           // different value.
130           if (!AfterBBI->getOperand(1).isImm() ||
131               AfterBBI->getOperand(1).getImm() != Imm)
132             break;
133 
134           // It loads same immediate value to the same Reg, which is redundant.
135           // We would unset kill flag in previous Reg usage to extend live range
136           // of Reg first, then remove the redundancy.
137           if (DeadOrKillToUnset) {
138             LLVM_DEBUG(dbgs()
139                        << " Unset dead/kill flag of " << *DeadOrKillToUnset
140                        << " from " << *DeadOrKillToUnset->getParent());
141             if (DeadOrKillToUnset->isDef())
142               DeadOrKillToUnset->setIsDead(false);
143             else
144               DeadOrKillToUnset->setIsKill(false);
145           }
146           DeadOrKillToUnset =
147               AfterBBI->findRegisterDefOperand(Reg, true, true, TRI);
148           if (DeadOrKillToUnset)
149             LLVM_DEBUG(dbgs()
150                        << " Dead flag of " << *DeadOrKillToUnset << " from "
151                        << *AfterBBI << " is a unsetting candidate\n");
152           InstrsToErase.insert(&*AfterBBI);
153           LLVM_DEBUG(dbgs() << " Remove redundant load immediate: ";
154                      AfterBBI->dump());
155         }
156       }
157 
158       for (MachineInstr *MI : InstrsToErase) {
159         MI->eraseFromParent();
160       }
161       NumRemovedInPreEmit += InstrsToErase.size();
162       return !InstrsToErase.empty();
163     }
164 
165     bool runOnMachineFunction(MachineFunction &MF) override {
166       if (skipFunction(MF.getFunction()) || !RunPreEmitPeephole)
167         return false;
168       bool Changed = false;
169       const PPCInstrInfo *TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
170       const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
171       SmallVector<MachineInstr *, 4> InstrsToErase;
172       for (MachineBasicBlock &MBB : MF) {
173         Changed |= removeRedundantLIs(MBB, TRI);
174         for (MachineInstr &MI : MBB) {
175           unsigned Opc = MI.getOpcode();
176           // Detect self copies - these can result from running AADB.
177           if (PPCInstrInfo::isSameClassPhysRegCopy(Opc)) {
178             const MCInstrDesc &MCID = TII->get(Opc);
179             if (MCID.getNumOperands() == 3 &&
180                 MI.getOperand(0).getReg() == MI.getOperand(1).getReg() &&
181                 MI.getOperand(0).getReg() == MI.getOperand(2).getReg()) {
182               NumberOfSelfCopies++;
183               LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: ");
184               LLVM_DEBUG(MI.dump());
185               InstrsToErase.push_back(&MI);
186               continue;
187             }
188             else if (MCID.getNumOperands() == 2 &&
189                      MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) {
190               NumberOfSelfCopies++;
191               LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: ");
192               LLVM_DEBUG(MI.dump());
193               InstrsToErase.push_back(&MI);
194               continue;
195             }
196           }
197           MachineInstr *DefMIToErase = nullptr;
198           if (TII->convertToImmediateForm(MI, &DefMIToErase)) {
199             Changed = true;
200             NumRRConvertedInPreEmit++;
201             LLVM_DEBUG(dbgs() << "Converted instruction to imm form: ");
202             LLVM_DEBUG(MI.dump());
203             if (DefMIToErase) {
204               InstrsToErase.push_back(DefMIToErase);
205             }
206           }
207           if (TII->foldFrameOffset(MI)) {
208             Changed = true;
209             NumFrameOffFoldInPreEmit++;
210             LLVM_DEBUG(dbgs() << "Frame offset folding by using index form: ");
211             LLVM_DEBUG(MI.dump());
212           }
213         }
214 
215         // Eliminate conditional branch based on a constant CR bit by
216         // CRSET or CRUNSET. We eliminate the conditional branch or
217         // convert it into an unconditional branch. Also, if the CR bit
218         // is not used by other instructions, we eliminate CRSET as well.
219         auto I = MBB.getFirstInstrTerminator();
220         if (I == MBB.instr_end())
221           continue;
222         MachineInstr *Br = &*I;
223         if (Br->getOpcode() != PPC::BC && Br->getOpcode() != PPC::BCn)
224           continue;
225         MachineInstr *CRSetMI = nullptr;
226         Register CRBit = Br->getOperand(0).getReg();
227         unsigned CRReg = getCRFromCRBit(CRBit);
228         bool SeenUse = false;
229         MachineBasicBlock::reverse_iterator It = Br, Er = MBB.rend();
230         for (It++; It != Er; It++) {
231           if (It->modifiesRegister(CRBit, TRI)) {
232             if ((It->getOpcode() == PPC::CRUNSET ||
233                  It->getOpcode() == PPC::CRSET) &&
234                 It->getOperand(0).getReg() == CRBit)
235               CRSetMI = &*It;
236             break;
237           }
238           if (It->readsRegister(CRBit, TRI))
239             SeenUse = true;
240         }
241         if (!CRSetMI) continue;
242 
243         unsigned CRSetOp = CRSetMI->getOpcode();
244         if ((Br->getOpcode() == PPC::BCn && CRSetOp == PPC::CRSET) ||
245             (Br->getOpcode() == PPC::BC  && CRSetOp == PPC::CRUNSET)) {
246           // Remove this branch since it cannot be taken.
247           InstrsToErase.push_back(Br);
248           MBB.removeSuccessor(Br->getOperand(1).getMBB());
249         }
250         else {
251           // This conditional branch is always taken. So, remove all branches
252           // and insert an unconditional branch to the destination of this.
253           MachineBasicBlock::iterator It = Br, Er = MBB.end();
254           for (; It != Er; It++) {
255             if (It->isDebugInstr()) continue;
256             assert(It->isTerminator() && "Non-terminator after a terminator");
257             InstrsToErase.push_back(&*It);
258           }
259           if (!MBB.isLayoutSuccessor(Br->getOperand(1).getMBB())) {
260             ArrayRef<MachineOperand> NoCond;
261             TII->insertBranch(MBB, Br->getOperand(1).getMBB(), nullptr,
262                               NoCond, Br->getDebugLoc());
263           }
264           for (auto &Succ : MBB.successors())
265             if (Succ != Br->getOperand(1).getMBB()) {
266               MBB.removeSuccessor(Succ);
267               break;
268             }
269         }
270 
271         // If the CRBit is not used by another instruction, we can eliminate
272         // CRSET/CRUNSET instruction.
273         if (!SeenUse) {
274           // We need to check use of the CRBit in successors.
275           for (auto &SuccMBB : MBB.successors())
276             if (SuccMBB->isLiveIn(CRBit) || SuccMBB->isLiveIn(CRReg)) {
277               SeenUse = true;
278               break;
279             }
280           if (!SeenUse)
281             InstrsToErase.push_back(CRSetMI);
282         }
283       }
284       for (MachineInstr *MI : InstrsToErase) {
285         LLVM_DEBUG(dbgs() << "PPC pre-emit peephole: erasing instruction: ");
286         LLVM_DEBUG(MI->dump());
287         MI->eraseFromParent();
288         NumRemovedInPreEmit++;
289       }
290       return Changed;
291     }
292   };
293 }
294 
295 INITIALIZE_PASS(PPCPreEmitPeephole, DEBUG_TYPE, "PowerPC Pre-Emit Peephole",
296                 false, false)
297 char PPCPreEmitPeephole::ID = 0;
298 
299 FunctionPass *llvm::createPPCPreEmitPeepholePass() {
300   return new PPCPreEmitPeephole();
301 }
302