1 //===--------- PPCPreEmitPeephole.cpp - Late peephole optimizations -------===//
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
7 //===----------------------------------------------------------------------===//
9 // A pre-emit peephole for catching opportunities introduced by late passes such
10 // as MachineBlockPlacement.
12 //===----------------------------------------------------------------------===//
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"
30 #define DEBUG_TYPE "ppc-pre-emit-peephole"
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");
40 RunPreEmitPeephole("ppc-late-peephole", cl::Hidden
, cl::init(true),
41 cl::desc("Run pre-emit peephole optimizations."));
44 class PPCPreEmitPeephole
: public MachineFunctionPass
{
47 PPCPreEmitPeephole() : MachineFunctionPass(ID
) {
48 initializePPCPreEmitPeepholePass(*PassRegistry::getPassRegistry());
51 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
52 MachineFunctionPass::getAnalysisUsage(AU
);
55 MachineFunctionProperties
getRequiredProperties() const override
{
56 return MachineFunctionProperties().set(
57 MachineFunctionProperties::Property::NoVRegs
);
60 // This function removes any redundant load immediates. It has two level
61 // loops - The outer loop finds the load immediates BBI that could be used
62 // to replace following redundancy. The inner loop scans instructions that
63 // after BBI to find redundancy and update kill/dead flags accordingly. If
64 // AfterBBI is the same as BBI, it is redundant, otherwise any instructions
65 // that modify the def register of BBI would break the scanning.
66 // DeadOrKillToUnset is a pointer to the previous operand that had the
67 // kill/dead flag set. It keeps track of the def register of BBI, the use
68 // registers of AfterBBIs and the def registers of AfterBBIs.
69 bool removeRedundantLIs(MachineBasicBlock
&MBB
,
70 const TargetRegisterInfo
*TRI
) {
71 LLVM_DEBUG(dbgs() << "Remove redundant load immediates from MBB:\n";
72 MBB
.dump(); dbgs() << "\n");
74 DenseSet
<MachineInstr
*> InstrsToErase
;
75 for (auto BBI
= MBB
.instr_begin(); BBI
!= MBB
.instr_end(); ++BBI
) {
76 // Skip load immediate that is marked to be erased later because it
77 // cannot be used to replace any other instructions.
78 if (InstrsToErase
.find(&*BBI
) != InstrsToErase
.end())
80 // Skip non-load immediate.
81 unsigned Opc
= BBI
->getOpcode();
82 if (Opc
!= PPC::LI
&& Opc
!= PPC::LI8
&& Opc
!= PPC::LIS
&&
85 // Skip load immediate, where the operand is a relocation (e.g., $r3 =
86 // LI target-flags(ppc-lo) %const.0).
87 if (!BBI
->getOperand(1).isImm())
89 assert(BBI
->getOperand(0).isReg() &&
90 "Expected a register for the first operand");
92 LLVM_DEBUG(dbgs() << "Scanning after load immediate: "; BBI
->dump(););
94 Register Reg
= BBI
->getOperand(0).getReg();
95 int64_t Imm
= BBI
->getOperand(1).getImm();
96 MachineOperand
*DeadOrKillToUnset
= nullptr;
97 if (BBI
->getOperand(0).isDead()) {
98 DeadOrKillToUnset
= &BBI
->getOperand(0);
99 LLVM_DEBUG(dbgs() << " Kill flag of " << *DeadOrKillToUnset
100 << " from load immediate " << *BBI
101 << " is a unsetting candidate\n");
103 // This loop scans instructions after BBI to see if there is any
104 // redundant load immediate.
105 for (auto AfterBBI
= std::next(BBI
); AfterBBI
!= MBB
.instr_end();
107 // Track the operand that kill Reg. We would unset the kill flag of
108 // the operand if there is a following redundant load immediate.
109 int KillIdx
= AfterBBI
->findRegisterUseOperandIdx(Reg
, true, TRI
);
111 assert(!DeadOrKillToUnset
&& "Shouldn't kill same register twice");
112 DeadOrKillToUnset
= &AfterBBI
->getOperand(KillIdx
);
114 << " Kill flag of " << *DeadOrKillToUnset
<< " from "
115 << *AfterBBI
<< " is a unsetting candidate\n");
118 if (!AfterBBI
->modifiesRegister(Reg
, TRI
))
120 // Finish scanning because Reg is overwritten by a non-load
122 if (AfterBBI
->getOpcode() != Opc
)
124 assert(AfterBBI
->getOperand(0).isReg() &&
125 "Expected a register for the first operand");
126 // Finish scanning because Reg is overwritten by a relocation or a
128 if (!AfterBBI
->getOperand(1).isImm() ||
129 AfterBBI
->getOperand(1).getImm() != Imm
)
132 // It loads same immediate value to the same Reg, which is redundant.
133 // We would unset kill flag in previous Reg usage to extend live range
134 // of Reg first, then remove the redundancy.
135 if (DeadOrKillToUnset
) {
137 << " Unset dead/kill flag of " << *DeadOrKillToUnset
138 << " from " << *DeadOrKillToUnset
->getParent());
139 if (DeadOrKillToUnset
->isDef())
140 DeadOrKillToUnset
->setIsDead(false);
142 DeadOrKillToUnset
->setIsKill(false);
145 AfterBBI
->findRegisterDefOperand(Reg
, true, true, TRI
);
146 if (DeadOrKillToUnset
)
148 << " Dead flag of " << *DeadOrKillToUnset
<< " from "
149 << *AfterBBI
<< " is a unsetting candidate\n");
150 InstrsToErase
.insert(&*AfterBBI
);
151 LLVM_DEBUG(dbgs() << " Remove redundant load immediate: ";
156 for (MachineInstr
*MI
: InstrsToErase
) {
157 MI
->eraseFromParent();
159 NumRemovedInPreEmit
+= InstrsToErase
.size();
160 return !InstrsToErase
.empty();
163 bool runOnMachineFunction(MachineFunction
&MF
) override
{
164 if (skipFunction(MF
.getFunction()) || !RunPreEmitPeephole
)
166 bool Changed
= false;
167 const PPCInstrInfo
*TII
= MF
.getSubtarget
<PPCSubtarget
>().getInstrInfo();
168 const TargetRegisterInfo
*TRI
= MF
.getSubtarget().getRegisterInfo();
169 SmallVector
<MachineInstr
*, 4> InstrsToErase
;
170 for (MachineBasicBlock
&MBB
: MF
) {
171 Changed
|= removeRedundantLIs(MBB
, TRI
);
172 for (MachineInstr
&MI
: MBB
) {
173 unsigned Opc
= MI
.getOpcode();
174 // Detect self copies - these can result from running AADB.
175 if (PPCInstrInfo::isSameClassPhysRegCopy(Opc
)) {
176 const MCInstrDesc
&MCID
= TII
->get(Opc
);
177 if (MCID
.getNumOperands() == 3 &&
178 MI
.getOperand(0).getReg() == MI
.getOperand(1).getReg() &&
179 MI
.getOperand(0).getReg() == MI
.getOperand(2).getReg()) {
180 NumberOfSelfCopies
++;
181 LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: ");
182 LLVM_DEBUG(MI
.dump());
183 InstrsToErase
.push_back(&MI
);
186 else if (MCID
.getNumOperands() == 2 &&
187 MI
.getOperand(0).getReg() == MI
.getOperand(1).getReg()) {
188 NumberOfSelfCopies
++;
189 LLVM_DEBUG(dbgs() << "Deleting self-copy instruction: ");
190 LLVM_DEBUG(MI
.dump());
191 InstrsToErase
.push_back(&MI
);
195 MachineInstr
*DefMIToErase
= nullptr;
196 if (TII
->convertToImmediateForm(MI
, &DefMIToErase
)) {
198 NumRRConvertedInPreEmit
++;
199 LLVM_DEBUG(dbgs() << "Converted instruction to imm form: ");
200 LLVM_DEBUG(MI
.dump());
202 InstrsToErase
.push_back(DefMIToErase
);
207 // Eliminate conditional branch based on a constant CR bit by
208 // CRSET or CRUNSET. We eliminate the conditional branch or
209 // convert it into an unconditional branch. Also, if the CR bit
210 // is not used by other instructions, we eliminate CRSET as well.
211 auto I
= MBB
.getFirstInstrTerminator();
212 if (I
== MBB
.instr_end())
214 MachineInstr
*Br
= &*I
;
215 if (Br
->getOpcode() != PPC::BC
&& Br
->getOpcode() != PPC::BCn
)
217 MachineInstr
*CRSetMI
= nullptr;
218 Register CRBit
= Br
->getOperand(0).getReg();
219 unsigned CRReg
= getCRFromCRBit(CRBit
);
220 bool SeenUse
= false;
221 MachineBasicBlock::reverse_iterator It
= Br
, Er
= MBB
.rend();
222 for (It
++; It
!= Er
; It
++) {
223 if (It
->modifiesRegister(CRBit
, TRI
)) {
224 if ((It
->getOpcode() == PPC::CRUNSET
||
225 It
->getOpcode() == PPC::CRSET
) &&
226 It
->getOperand(0).getReg() == CRBit
)
230 if (It
->readsRegister(CRBit
, TRI
))
233 if (!CRSetMI
) continue;
235 unsigned CRSetOp
= CRSetMI
->getOpcode();
236 if ((Br
->getOpcode() == PPC::BCn
&& CRSetOp
== PPC::CRSET
) ||
237 (Br
->getOpcode() == PPC::BC
&& CRSetOp
== PPC::CRUNSET
)) {
238 // Remove this branch since it cannot be taken.
239 InstrsToErase
.push_back(Br
);
240 MBB
.removeSuccessor(Br
->getOperand(1).getMBB());
243 // This conditional branch is always taken. So, remove all branches
244 // and insert an unconditional branch to the destination of this.
245 MachineBasicBlock::iterator It
= Br
, Er
= MBB
.end();
246 for (; It
!= Er
; It
++) {
247 if (It
->isDebugInstr()) continue;
248 assert(It
->isTerminator() && "Non-terminator after a terminator");
249 InstrsToErase
.push_back(&*It
);
251 if (!MBB
.isLayoutSuccessor(Br
->getOperand(1).getMBB())) {
252 ArrayRef
<MachineOperand
> NoCond
;
253 TII
->insertBranch(MBB
, Br
->getOperand(1).getMBB(), nullptr,
254 NoCond
, Br
->getDebugLoc());
256 for (auto &Succ
: MBB
.successors())
257 if (Succ
!= Br
->getOperand(1).getMBB()) {
258 MBB
.removeSuccessor(Succ
);
263 // If the CRBit is not used by another instruction, we can eliminate
264 // CRSET/CRUNSET instruction.
266 // We need to check use of the CRBit in successors.
267 for (auto &SuccMBB
: MBB
.successors())
268 if (SuccMBB
->isLiveIn(CRBit
) || SuccMBB
->isLiveIn(CRReg
)) {
273 InstrsToErase
.push_back(CRSetMI
);
276 for (MachineInstr
*MI
: InstrsToErase
) {
277 LLVM_DEBUG(dbgs() << "PPC pre-emit peephole: erasing instruction: ");
278 LLVM_DEBUG(MI
->dump());
279 MI
->eraseFromParent();
280 NumRemovedInPreEmit
++;
287 INITIALIZE_PASS(PPCPreEmitPeephole
, DEBUG_TYPE
, "PowerPC Pre-Emit Peephole",
289 char PPCPreEmitPeephole::ID
= 0;
291 FunctionPass
*llvm::createPPCPreEmitPeepholePass() {
292 return new PPCPreEmitPeephole();