28#define DEBUG_TYPE "ppc-pre-emit-peephole"
31 "Number of r+r instructions converted to r+i in pre-emit peephole");
33 "Number of instructions deleted in pre-emit peephole");
35 "Number of self copy instructions eliminated");
37 "Number of folding frame offset by using r+r in pre-emit peephole");
39 "Number of compares eliminated in pre-emit peephole");
44 switch (
Use.getOpcode()) {
83 const PPCOptions *CLOpts =
nullptr;
87 PPCPreEmitPeephole() : MachineFunctionPass(ID) {}
89 void getAnalysisUsage(AnalysisUsage &AU)
const override {
93 MachineFunctionProperties getRequiredProperties()
const override {
94 return MachineFunctionProperties().setNoVRegs();
106 bool removeRedundantLIs(MachineBasicBlock &
MBB,
107 const TargetRegisterInfo *
TRI) {
108 LLVM_DEBUG(
dbgs() <<
"Remove redundant load immediates from MBB:\n";
111 DenseSet<MachineInstr *> InstrsToErase;
115 if (InstrsToErase.contains(&*BBI))
118 unsigned Opc = BBI->getOpcode();
119 if (
Opc != PPC::LI &&
Opc != PPC::LI8 &&
Opc != PPC::LIS &&
124 if (!BBI->getOperand(1).isImm())
126 assert(BBI->getOperand(0).isReg() &&
127 "Expected a register for the first operand");
129 LLVM_DEBUG(
dbgs() <<
"Scanning after load immediate: "; BBI->dump(););
132 int64_t
Imm = BBI->getOperand(1).getImm();
133 MachineOperand *DeadOrKillToUnset =
nullptr;
134 if (BBI->getOperand(0).isDead()) {
135 DeadOrKillToUnset = &BBI->getOperand(0);
137 <<
" from load immediate " << *BBI
138 <<
" is a unsetting candidate\n");
142 for (
auto AfterBBI = std::next(BBI); AfterBBI !=
MBB.
instr_end();
146 int KillIdx = AfterBBI->findRegisterUseOperandIdx(
Reg,
TRI,
true);
150 if (KillIdx != -1 && AfterBBI->getOperand(KillIdx).isImplicit()) {
152 <<
"Encountered an implicit kill, cannot proceed: ");
158 assert(!DeadOrKillToUnset &&
"Shouldn't kill same register twice");
159 DeadOrKillToUnset = &AfterBBI->getOperand(KillIdx);
161 <<
" Kill flag of " << *DeadOrKillToUnset <<
" from "
162 << *AfterBBI <<
" is a unsetting candidate\n");
165 if (!AfterBBI->modifiesRegister(
Reg,
TRI))
169 if (AfterBBI->getOpcode() !=
Opc)
171 assert(AfterBBI->getOperand(0).isReg() &&
172 "Expected a register for the first operand");
175 if (!AfterBBI->getOperand(1).isImm() ||
176 AfterBBI->getOperand(1).getImm() !=
Imm)
182 if (DeadOrKillToUnset) {
184 <<
" Unset dead/kill flag of " << *DeadOrKillToUnset
185 <<
" from " << *DeadOrKillToUnset->
getParent());
186 if (DeadOrKillToUnset->
isDef())
192 AfterBBI->findRegisterDefOperand(
Reg,
TRI,
true,
true);
193 if (DeadOrKillToUnset)
195 <<
" Dead flag of " << *DeadOrKillToUnset <<
" from "
196 << *AfterBBI <<
" is a unsetting candidate\n");
197 InstrsToErase.insert(&*AfterBBI);
203 for (MachineInstr *
MI : InstrsToErase) {
204 MI->eraseFromParent();
206 NumRemovedInPreEmit += InstrsToErase.size();
207 return !InstrsToErase.empty();
212 bool isGOTPLDpc(MachineInstr &Instr) {
213 if (
Instr.getOpcode() != PPC::PLDpc)
217 const MachineOperand &LoadedAddressReg =
Instr.getOperand(0);
218 if (!LoadedAddressReg.
isReg())
222 const MachineOperand &SymbolOp =
Instr.getOperand(1);
230 bool addLinkerOpt(MachineBasicBlock &
MBB,
const TargetRegisterInfo *
TRI) {
233 if (!CLOpts->pcrel_linker_opt)
237 if (!MF->
getSubtarget<PPCSubtarget>().isUsingPCRelativeCalls())
241 struct GOTDefUsePair {
251 bool MadeChange =
false;
257 if (isGOTPLDpc(*BBI)) {
259 BBI->getOperand(0).getReg(),
260 PPC::NoRegister,
true};
266 if (CandPairs.
empty())
272 for (
unsigned Idx = 0;
Idx < CandPairs.
size();
Idx++) {
273 GOTDefUsePair &Pair = CandPairs[
Idx];
276 if (!BBI->readsRegister(Pair.DefReg,
TRI) &&
277 !BBI->modifiesRegister(Pair.DefReg,
TRI))
282 const MachineOperand *UseOp =
283 hasPCRelativeForm(*BBI) ? &BBI->getOperand(2) :
nullptr;
286 if (UseOp && UseOp->
isReg() && UseOp->
getReg() == Pair.DefReg &&
289 Pair.UseReg = BBI->getOperand(0).getReg();
297 for (
auto Pair = ValidPairs.
begin(); Pair != ValidPairs.
end(); Pair++) {
299 assert(Pair->UseInst.isValid() && Pair->StillValid &&
300 "Kept an invalid def/use pair for GOT PCRel opt");
306 for (; BBI != Pair->UseInst; ++BBI) {
307 if (BBI->readsRegister(Pair->UseReg,
TRI) ||
308 BBI->modifiesRegister(Pair->UseReg,
TRI)) {
309 Pair->StillValid =
false;
314 if (!Pair->StillValid)
323 MachineOperand ImplDef =
325 MachineOperand ImplUse =
327 Pair->DefInst->addOperand(ImplDef);
328 Pair->UseInst->addOperand(ImplUse);
333 MachineOperand PCRelLabel =
335 Pair->DefInst->addOperand(*MF, PCRelLabel);
336 Pair->UseInst->addOperand(*MF, PCRelLabel);
354 bool removeAccPrimeUnprime(MachineBasicBlock &
MBB) {
355 DenseSet<MachineInstr *> InstrsToErase;
357 SmallVector<MachineInstr *, 8> Candidates(
358 PPC::UACCRCRegClass.getNumRegs(),
nullptr);
361 unsigned Opc = BBI.getOpcode();
364 if (
Opc == PPC::XXMTACC) {
365 Register Acc = BBI.getOperand(0).getReg();
367 "Unexpected register for XXMTACC");
368 Candidates[Acc - PPC::ACC0] = &BBI;
372 else if (
Opc == PPC::XXMFACC) {
373 Register Acc = BBI.getOperand(0).getReg();
375 "Unexpected register for XXMFACC");
376 if (!Candidates[Acc - PPC::ACC0])
378 InstrsToErase.
insert(&BBI);
379 InstrsToErase.
insert(Candidates[Acc - PPC::ACC0]);
384 for (MachineOperand &Operand : BBI.operands()) {
385 if (!Operand.isReg())
389 Candidates[
Reg - PPC::ACC0] =
nullptr;
394 for (MachineInstr *
MI : InstrsToErase)
395 MI->eraseFromParent();
396 NumRemovedInPreEmit += InstrsToErase.size();
397 return !InstrsToErase.empty();
404 if (CLOpts->set_dscr && MF.
getName() ==
"main" &&
406 uint32_t DSCRValue = *CLOpts->set_dscr & 0x01FFFFFF;
408 MachineBasicBlock &
MBB = MF.
front();
410 RS.enterBasicBlock(
MBB);
411 unsigned InDSCR =
RS.FindUnusedReg(&PPC::GPRCRegClass);
413 const PPCInstrInfo *
TII =
426 .
addImm(DSCRValue & 0xFFFF);
428 .
addReg(InDSCR, RegState::Kill);
430 errs() <<
"Warning: Ran out of registers - Unable to set DSCR as "
434 if (skipFunction(MF.
getFunction()) || !CLOpts->late_peephole) {
438 SmallVector<MachineInstr *, 4> InstrsToErase;
439 for (MachineBasicBlock &
MBB : MF)
440 for (MachineInstr &
MI :
MBB)
441 if (
MI.getOpcode() == PPC::UNENCODED_NOP)
443 for (MachineInstr *
MI : InstrsToErase)
444 MI->eraseFromParent();
448 const PPCInstrInfo *
TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
449 const TargetRegisterInfo *
TRI = MF.getSubtarget().getRegisterInfo();
450 SmallVector<MachineInstr *, 4> InstrsToErase;
451 for (MachineBasicBlock &
MBB : MF) {
455 for (MachineInstr &
MI :
MBB) {
456 unsigned Opc =
MI.getOpcode();
457 if (
Opc == PPC::UNENCODED_NOP) {
463 const MCInstrDesc &MCID =
TII->get(
Opc);
465 MI.getOperand(0).getReg() ==
MI.getOperand(1).getReg() &&
466 MI.getOperand(0).getReg() ==
MI.getOperand(2).getReg()) {
467 NumberOfSelfCopies++;
474 MI.getOperand(0).getReg() ==
MI.getOperand(1).getReg()) {
475 NumberOfSelfCopies++;
482 MachineInstr *DefMIToErase =
nullptr;
483 SmallSet<Register, 4> UpdatedRegs;
484 if (
TII->convertToImmediateForm(
MI, UpdatedRegs, &DefMIToErase)) {
486 NumRRConvertedInPreEmit++;
493 if (
TII->foldFrameOffset(
MI)) {
495 NumFrameOffFoldInPreEmit++;
496 LLVM_DEBUG(
dbgs() <<
"Frame offset folding by using index form: ");
499 if (
TII->optimizeCmpPostRA(
MI)) {
515 MachineInstr *Br = &*
I;
518 MachineInstr *CRSetMI =
nullptr;
521 bool SeenUse =
false;
523 for (It++; It != Er; It++) {
524 if (It->modifiesRegister(CRBit,
TRI)) {
525 if ((It->getOpcode() == PPC::CRUNSET ||
526 It->getOpcode() == PPC::CRSET) &&
527 It->getOperand(0).getReg() == CRBit)
531 if (It->readsRegister(CRBit,
TRI))
534 if (!CRSetMI)
continue;
537 if ((Br->
getOpcode() == PPC::BCn && CRSetOp == PPC::CRSET) ||
538 (Br->
getOpcode() == PPC::BC && CRSetOp == PPC::CRUNSET)) {
547 for (; It != Er; It++) {
548 if (It->isDebugInstr())
continue;
549 assert(It->isTerminator() &&
"Non-terminator after a terminator");
569 if (SuccMBB->isLiveIn(CRBit) || SuccMBB->isLiveIn(CRReg)) {
577 for (MachineInstr *
MI : InstrsToErase) {
578 LLVM_DEBUG(
dbgs() <<
"PPC pre-emit peephole: erasing instruction: ");
580 MI->eraseFromParent();
581 NumRemovedInPreEmit++;
590char PPCPreEmitPeephole::ID = 0;
593 return new PPCPreEmitPeephole();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
static Register UseReg(const MachineOperand &MO)
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
Register const TargetRegisterInfo * TRI
Promote Memory to Register
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file declares the machine register scavenger class.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const override
FunctionPass class - This class is used to implement most global optimizations.
bool hasExternalLinkage() const
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
instr_iterator instr_begin()
LLVM_ABI void dump() const
LLVM_ABI void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
LLVM_ABI bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
instr_iterator instr_end()
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< succ_iterator > successors()
LLVM_ABI instr_iterator getFirstInstrTerminator()
Same getFirstTerminator but it ignores bundles and return an instr_iterator instead.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MCContext & getContext() const
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineBasicBlock & front() const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
static MachineOperand CreateMCSymbol(MCSymbol *Sym, unsigned TargetFlags=0)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
void setIsDead(bool Val=true)
void setIsKill(bool Val=true)
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
unsigned getTargetFlags() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
Register getReg() const
getReg - Returns the register number.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
static bool isSameClassPhysRegCopy(unsigned Opcode)
static bool hasGOTFlag(unsigned TF)
iterator erase(const_iterator CI)
void push_back(const T &Elt)
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
A Use represents the edge between a Value definition and its users.
std::pair< iterator, bool > insert(const ValueT &V)
const AArch64MCOptions & getCLOpts(const MCTargetOptions &Options)
@ MO_PCREL_OPT_FLAG
MO_PCREL_OPT_FLAG - If this bit is set the operand is part of a PC Relative linker optimization.
NodeAddr< InstrNode * > Instr
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createPPCPreEmitPeepholePass()
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
static unsigned getCRFromCRBit(unsigned SrcReg)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
ArrayRef(const T &OneElt) -> ArrayRef< T >