30 #define DEBUG_TYPE "ppc-pre-emit-peephole" 33 "Number of r+r instructions converted to r+i in pre-emit peephole");
35 "Number of instructions deleted in pre-emit peephole");
37 "Number of self copy instructions eliminated");
39 "Number of folding frame offset by using r+r in pre-emit peephole");
43 cl::desc(
"Run pre-emit peephole optimizations."));
73 LLVM_DEBUG(
dbgs() <<
"Remove redundant load immediates from MBB:\n";
80 if (InstrsToErase.find(&*BBI) != InstrsToErase.end())
83 unsigned Opc = BBI->getOpcode();
84 if (Opc != PPC::LI && Opc != PPC::LI8 && Opc != PPC::LIS &&
89 if (!BBI->getOperand(1).isImm())
91 assert(BBI->getOperand(0).isReg() &&
92 "Expected a register for the first operand");
94 LLVM_DEBUG(
dbgs() <<
"Scanning after load immediate: "; BBI->dump(););
97 int64_t Imm = BBI->getOperand(1).getImm();
99 if (BBI->getOperand(0).isDead()) {
100 DeadOrKillToUnset = &BBI->getOperand(0);
102 <<
" from load immediate " << *BBI
103 <<
" is a unsetting candidate\n");
107 for (
auto AfterBBI = std::next(BBI); AfterBBI != MBB.
instr_end();
111 int KillIdx = AfterBBI->findRegisterUseOperandIdx(Reg,
true, TRI);
113 assert(!DeadOrKillToUnset &&
"Shouldn't kill same register twice");
114 DeadOrKillToUnset = &AfterBBI->getOperand(KillIdx);
116 <<
" Kill flag of " << *DeadOrKillToUnset <<
" from " 117 << *AfterBBI <<
" is a unsetting candidate\n");
120 if (!AfterBBI->modifiesRegister(Reg, TRI))
124 if (AfterBBI->getOpcode() != Opc)
126 assert(AfterBBI->getOperand(0).isReg() &&
127 "Expected a register for the first operand");
130 if (!AfterBBI->getOperand(1).isImm() ||
131 AfterBBI->getOperand(1).getImm() != Imm)
137 if (DeadOrKillToUnset) {
139 <<
" Unset dead/kill flag of " << *DeadOrKillToUnset
140 <<
" from " << *DeadOrKillToUnset->
getParent());
141 if (DeadOrKillToUnset->
isDef())
147 AfterBBI->findRegisterDefOperand(Reg,
true,
true, TRI);
148 if (DeadOrKillToUnset)
150 <<
" Dead flag of " << *DeadOrKillToUnset <<
" from " 151 << *AfterBBI <<
" is a unsetting candidate\n");
152 InstrsToErase.insert(&*AfterBBI);
159 MI->eraseFromParent();
161 NumRemovedInPreEmit += InstrsToErase.size();
162 return !InstrsToErase.empty();
173 if (
MI.getOpcode() == PPC::UNENCODED_NOP)
176 MI->eraseFromParent();
179 bool Changed =
false;
184 Changed |= removeRedundantLIs(MBB, TRI);
186 unsigned Opc =
MI.getOpcode();
187 if (Opc == PPC::UNENCODED_NOP) {
195 MI.getOperand(0).getReg() ==
MI.getOperand(1).getReg() &&
196 MI.getOperand(0).getReg() ==
MI.getOperand(2).getReg()) {
197 NumberOfSelfCopies++;
204 MI.getOperand(0).getReg() ==
MI.getOperand(1).getReg()) {
205 NumberOfSelfCopies++;
215 NumRRConvertedInPreEmit++;
224 NumFrameOffFoldInPreEmit++;
225 LLVM_DEBUG(
dbgs() <<
"Frame offset folding by using index form: ");
234 auto I = MBB.getFirstInstrTerminator();
235 if (I == MBB.instr_end())
243 bool SeenUse =
false;
245 for (It++; It != Er; It++) {
246 if (It->modifiesRegister(CRBit, TRI)) {
247 if ((It->getOpcode() == PPC::CRUNSET ||
248 It->getOpcode() == PPC::CRSET) &&
249 It->getOperand(0).getReg() == CRBit)
253 if (It->readsRegister(CRBit, TRI))
256 if (!CRSetMI)
continue;
259 if ((Br->
getOpcode() == PPC::BCn && CRSetOp == PPC::CRSET) ||
260 (Br->
getOpcode() == PPC::BC && CRSetOp == PPC::CRUNSET)) {
269 for (; It != Er; It++) {
270 if (It->isDebugInstr())
continue;
271 assert(It->isTerminator() &&
"Non-terminator after a terminator");
279 for (
auto &Succ : MBB.successors())
281 MBB.removeSuccessor(Succ);
290 for (
auto &SuccMBB : MBB.successors())
291 if (SuccMBB->isLiveIn(CRBit) || SuccMBB->isLiveIn(CRReg)) {
300 LLVM_DEBUG(
dbgs() <<
"PPC pre-emit peephole: erasing instruction: ");
302 MI->eraseFromParent();
303 NumRemovedInPreEmit++;
312 char PPCPreEmitPeephole::ID = 0;
315 return new PPCPreEmitPeephole();
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
instr_iterator instr_begin()
instr_iterator instr_end()
MachineBasicBlock * getMBB() const
This class represents lattice values for constants.
Implements a dense probed hash-table based set.
void push_back(const T &Elt)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Describe properties that are true of each instruction in the target description file.
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
void setIsDead(bool Val=true)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
const HexagonInstrInfo * TII
MachineBasicBlock iterator that automatically skips over MIs that are inside bundles (i...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
bool foldFrameOffset(MachineInstr &MI) const
initializer< Ty > init(const Ty &Val)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
static bool isSameClassPhysRegCopy(unsigned Opcode)
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const override
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
static unsigned getCRFromCRBit(unsigned SrcReg)
void setIsKill(bool Val=true)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
MachineOperand class - Representation of each machine instruction operand.
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
void initializePPCPreEmitPeepholePass(PassRegistry &)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
FunctionPass * createPPCPreEmitPeepholePass()
static cl::opt< bool > RunPreEmitPeephole("ppc-late-peephole", cl::Hidden, cl::init(true), cl::desc("Run pre-emit peephole optimizations."))
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
bool convertToImmediateForm(MachineInstr &MI, MachineInstr **KilledDef=nullptr) const
Properties which a MachineFunction may have at a given point in time.
Wrapper class representing virtual and physical registers.