28#include "llvm/Config/llvm-config.h"
34#define DEBUG_TYPE "ppc-reduce-cr-ops"
37 "Number of single-use binary CR logical ops contained in a block");
39 "Number of binary CR logical ops that can be used to split blocks");
40STATISTIC(TotalCRLogicals,
"Number of CR logical ops.");
42 "Number of nullary CR logical ops (CRSET/CRUNSET).");
43STATISTIC(TotalUnaryCRLogicals,
"Number of unary CR logical ops.");
44STATISTIC(TotalBinaryCRLogicals,
"Number of CR logical ops.");
46 "Number of blocks split on CR binary logical ops.");
48 "Number of blocks not split due to operands being identical.");
50 "Number of blocks not split due to operands being chained copies.");
52 "Number of blocks not split due to the wrong opcode.");
65 for (
unsigned i = 2, e =
MI.getNumOperands() + 1; i != e; i += 2) {
67 if (MO.
getMBB() == OrigMBB) {
69 if (
MI.getOperand(i - 1).isReg()) {
71 if (
DefMI->getParent() == NewMBB ||
92 "NewMBB must be a successor of OrigMBB");
98 for (
unsigned i = 2, e =
MI.getNumOperands() + 1; i != e; i += 2) {
100 if (MO.
getMBB() == OrigMBB) {
110struct BlockSplitInfo {
111 MachineInstr *OrigBranch;
112 MachineInstr *SplitBefore;
113 MachineInstr *SplitCond;
115 unsigned SplitCondSubreg;
116 bool InvertNewBranch;
117 bool InvertOrigBranch;
118 bool BranchToFallThrough;
119 const MachineBranchProbabilityInfo *MBPI;
120 MachineInstr *MIToDelete;
121 MachineInstr *NewCond;
122 bool allInstrsInSameMBB() {
123 if (!OrigBranch || !SplitBefore || !SplitCond)
128 if (MIToDelete && MIToDelete->getParent() !=
MBB)
130 if (NewCond && NewCond->getParent() !=
MBB)
147 assert(BSI.allInstrsInSameMBB() &&
148 "All instructions must be in the same block.");
153 assert(
MRI->isSSA() &&
"Can only do this while the function is in SSA form.");
156 dbgs() <<
"Don't know how to handle blocks that don't have exactly"
157 <<
" two successors.\n");
162 unsigned OrigBROpcode = BSI.OrigBranch->
getOpcode();
163 unsigned InvertedOpcode =
164 OrigBROpcode == PPC::BC
166 : OrigBROpcode == PPC::BCn
168 : OrigBROpcode == PPC::BCLR ? PPC::BCLRn : PPC::BCLR;
169 unsigned NewBROpcode = BSI.InvertNewBranch ? InvertedOpcode : OrigBROpcode;
175 BSI.BranchToFallThrough ? OrigFallThrough : OrigTarget;
191 if (BSI.BranchToFallThrough) {
193 ProbFallThrough = ProbToNewTarget.
getCompl();
194 ProbOrigFallThrough = ProbToNewTarget / ProbToNewTarget.
getCompl();
195 ProbOrigTarget = ProbOrigFallThrough.
getCompl();
198 ProbFallThrough = ProbToNewTarget.
getCompl();
199 ProbOrigTarget = ProbToNewTarget / ProbToNewTarget.
getCompl();
200 ProbOrigFallThrough = ProbOrigTarget.
getCompl();
227 TII->get(NewBROpcode))
239 assert(FirstTerminator->getOperand(0).isReg() &&
240 "Can't update condition of unconditional branch.");
242 FirstTerminator->getOperand(0).setSubReg(BSI.OrigSubreg);
244 if (BSI.InvertOrigBranch)
245 FirstTerminator->setDesc(
TII->get(InvertedOpcode));
270 return MI.getNumOperands() == 3;
274 return MI.getNumOperands() == 1;
284 bool &InvertNewBranch,
bool &InvertOrigBranch,
285 bool &TargetIsFallThrough) {
290 if (BROp == PPC::BC || BROp == PPC::BCLR) {
296 InvertNewBranch =
false;
297 InvertOrigBranch =
false;
298 TargetIsFallThrough =
false;
301 InvertNewBranch =
true;
302 InvertOrigBranch =
false;
303 TargetIsFallThrough =
true;
306 InvertNewBranch =
true;
307 InvertOrigBranch =
true;
308 TargetIsFallThrough =
false;
311 InvertNewBranch =
false;
312 InvertOrigBranch =
true;
313 TargetIsFallThrough =
true;
316 InvertNewBranch = UsingDef1;
317 InvertOrigBranch = !UsingDef1;
318 TargetIsFallThrough =
false;
321 InvertNewBranch = !UsingDef1;
322 InvertOrigBranch = !UsingDef1;
323 TargetIsFallThrough =
true;
326 }
else if (BROp == PPC::BCn || BROp == PPC::BCLRn) {
332 InvertNewBranch =
true;
333 InvertOrigBranch =
false;
334 TargetIsFallThrough =
true;
337 InvertNewBranch =
false;
338 InvertOrigBranch =
false;
339 TargetIsFallThrough =
false;
342 InvertNewBranch =
false;
343 InvertOrigBranch =
true;
344 TargetIsFallThrough =
true;
347 InvertNewBranch =
true;
348 InvertOrigBranch =
true;
349 TargetIsFallThrough =
false;
352 InvertNewBranch = !UsingDef1;
353 InvertOrigBranch = !UsingDef1;
354 TargetIsFallThrough =
true;
357 InvertNewBranch = UsingDef1;
358 InvertOrigBranch = !UsingDef1;
359 TargetIsFallThrough =
false;
371 struct CRLogicalOpInfo {
374 std::pair<MachineInstr*, MachineInstr*> CopyDefs;
375 std::pair<MachineInstr*, MachineInstr*> TrueDefs;
376 unsigned IsBinary : 1;
377 unsigned IsNullary : 1;
378 unsigned ContainedInBlock : 1;
379 unsigned FeedsISEL : 1;
380 unsigned FeedsBR : 1;
381 unsigned FeedsLogical : 1;
382 unsigned SingleUse : 1;
383 unsigned DefsSingleUse : 1;
386 CRLogicalOpInfo() : MI(nullptr), IsBinary(0), IsNullary(0),
387 ContainedInBlock(0), FeedsISEL(0), FeedsBR(0),
388 FeedsLogical(0), SingleUse(0), DefsSingleUse(1),
389 SubregDef1(0), SubregDef2(0) { }
394 const PPCInstrInfo *TII =
nullptr;
396 MachineRegisterInfo *MRI =
nullptr;
397 const MachineBranchProbabilityInfo *MBPI =
nullptr;
402 void collectCRLogicals();
403 bool handleCROp(
unsigned Idx);
404 bool splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI);
405 static bool isCRLogical(MachineInstr &
MI) {
406 unsigned Opc =
MI.getOpcode();
407 return Opc == PPC::CRAND ||
Opc == PPC::CRNAND ||
Opc == PPC::CROR ||
408 Opc == PPC::CRXOR ||
Opc == PPC::CRNOR ||
Opc == PPC::CRNOT ||
409 Opc == PPC::CREQV ||
Opc == PPC::CRANDC ||
Opc == PPC::CRORC ||
410 Opc == PPC::CRSET ||
Opc == PPC::CRUNSET ||
Opc == PPC::CR6SET ||
411 Opc == PPC::CR6UNSET;
413 bool simplifyCode() {
417 for (
unsigned i = 0; i < AllCRLogicalOps.size(); i++)
423 PPCReduceCRLogicals() : MachineFunctionPass(ID) {}
425 MachineInstr *lookThroughCRCopy(
unsigned Reg,
unsigned &Subreg,
426 MachineInstr *&CpDef);
428 if (skipFunction(MF.getFunction()))
432 const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
433 if (!STI.useCRBits())
438 return simplifyCode();
440 CRLogicalOpInfo createCRLogicalOpInfo(MachineInstr &
MI);
441 void getAnalysisUsage(AnalysisUsage &AU)
const override {
442 AU.
addRequired<MachineBranchProbabilityInfoWrapperPass>();
449#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
451 dbgs() <<
"CRLogicalOpMI: ";
453 dbgs() <<
"IsBinary: " << IsBinary <<
", FeedsISEL: " << FeedsISEL;
454 dbgs() <<
", FeedsBR: " << FeedsBR <<
", FeedsLogical: ";
455 dbgs() << FeedsLogical <<
", SingleUse: " << SingleUse;
456 dbgs() <<
", DefsSingleUse: " << DefsSingleUse;
457 dbgs() <<
", SubregDef1: " << SubregDef1 <<
", SubregDef2: ";
458 dbgs() << SubregDef2 <<
", ContainedInBlock: " << ContainedInBlock;
460 dbgs() <<
"\nDefs:\n";
461 TrueDefs.first->dump();
464 TrueDefs.second->dump();
466 if (CopyDefs.first) {
467 dbgs() <<
"CopyDef1: ";
468 CopyDefs.first->dump();
470 if (CopyDefs.second) {
471 dbgs() <<
"CopyDef2: ";
472 CopyDefs.second->dump();
477PPCReduceCRLogicals::CRLogicalOpInfo
478PPCReduceCRLogicals::createCRLogicalOpInfo(MachineInstr &MIParam) {
484 Ret.TrueDefs = std::make_pair(
nullptr,
nullptr);
485 Ret.CopyDefs = std::make_pair(
nullptr,
nullptr);
488 Ret.SubregDef1, Ret.CopyDefs.first);
490 assert(Def1 &&
"Must be able to find a definition of operand 1.");
499 Ret.CopyDefs.second);
501 assert(Def2 &&
"Must be able to find a definition of operand 2.");
506 Ret.TrueDefs = std::make_pair(Def1, Def2);
508 Ret.TrueDefs = std::make_pair(Def1,
nullptr);
509 Ret.CopyDefs.second =
nullptr;
513 Ret.ContainedInBlock = 1;
515 for (MachineInstr &
UseMI :
518 if (
Opc == PPC::ISEL ||
Opc == PPC::ISEL8)
520 if (
Opc == PPC::BC ||
Opc == PPC::BCn ||
Opc == PPC::BCLR ||
523 Ret.FeedsLogical = isCRLogical(
UseMI);
525 Ret.ContainedInBlock = 0;
530 if (!Ret.IsNullary) {
531 Ret.ContainedInBlock &=
532 (MIParam.
getParent() == Ret.TrueDefs.first->getParent());
534 Ret.ContainedInBlock &=
535 (MIParam.
getParent() == Ret.TrueDefs.second->getParent());
538 if (Ret.IsBinary && Ret.ContainedInBlock && Ret.SingleUse) {
539 NumContainedSingleUseBinOps++;
540 if (Ret.FeedsBR && Ret.DefsSingleUse)
552MachineInstr *PPCReduceCRLogicals::lookThroughCRCopy(
unsigned Reg,
554 MachineInstr *&CpDef) {
555 if (!Register::isVirtualRegister(
Reg))
567 if ((--Me)->modifiesRegister(CopySrc,
TRI))
578 MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
580 AllCRLogicalOps.
clear();
588bool PPCReduceCRLogicals::handleCROp(
unsigned Idx) {
592 CRLogicalOpInfo CRI = AllCRLogicalOps[
Idx];
593 if (CRI.IsBinary && CRI.ContainedInBlock && CRI.SingleUse && CRI.FeedsBR &&
595 Changed = splitBlockOnBinaryCROp(CRI);
597 NumBlocksSplitOnBinaryCROp++;
619bool PPCReduceCRLogicals::splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI) {
620 if (CRI.CopyDefs.first == CRI.CopyDefs.second) {
621 LLVM_DEBUG(
dbgs() <<
"Unable to split as the two operands are the same\n");
622 NumNotSplitIdenticalOperands++;
625 if (CRI.TrueDefs.first->isCopy() || CRI.TrueDefs.second->isCopy() ||
626 CRI.TrueDefs.first->isPHI() || CRI.TrueDefs.second->isPHI()) {
628 dbgs() <<
"Unable to split because one of the operands is a PHI or "
629 "chain of copies.\n");
630 NumNotSplitChainCopies++;
634 if (CRI.MI->getOpcode() != PPC::CROR &&
635 CRI.MI->getOpcode() != PPC::CRAND &&
636 CRI.MI->getOpcode() != PPC::CRNOR &&
637 CRI.MI->getOpcode() != PPC::CRNAND &&
638 CRI.MI->getOpcode() != PPC::CRORC &&
639 CRI.MI->getOpcode() != PPC::CRANDC) {
641 NumNotSplitWrongOpcode++;
644 LLVM_DEBUG(
dbgs() <<
"Splitting the following CR op:\n"; CRI.dump());
648 bool UsingDef1 =
false;
649 MachineInstr *SplitBefore = &*Def2It;
650 for (
auto E = CRI.MI->getParent()->end(); Def2It !=
E; ++Def2It) {
651 if (Def1It == Def2It) {
652 SplitBefore = &*Def1It;
673 UsingDef1 ? CRI.TrueDefs.first : CRI.TrueDefs.second;
675 UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second;
682 if (FirstInstrToMove != SecondInstrToMove)
686 unsigned Opc = CRI.MI->getOpcode();
687 bool InvertOrigBranch, InvertNewBranch, TargetIsFallThrough;
689 InvertNewBranch, InvertOrigBranch,
690 TargetIsFallThrough);
691 MachineInstr *NewCond = CRI.CopyDefs.first;
692 MachineInstr *SplitCond = CRI.CopyDefs.second;
695 std::swap(CRI.SubregDef1, CRI.SubregDef2);
697 LLVM_DEBUG(
dbgs() <<
"We will " << (InvertNewBranch ?
"invert" :
"copy"));
698 LLVM_DEBUG(
dbgs() <<
" the original branch and the target is the "
699 << (TargetIsFallThrough ?
"fallthrough block\n"
700 :
"orig. target block\n"));
703 Branch, SplitBefore, SplitCond, CRI.SubregDef1,
704 CRI.SubregDef2, InvertNewBranch, InvertOrigBranch, TargetIsFallThrough,
705 MBPI, CRI.MI, NewCond};
710 bool Input1CRlogical =
711 CRI.TrueDefs.first && isCRLogical(*CRI.TrueDefs.first);
712 bool Input2CRlogical =
713 CRI.TrueDefs.second && isCRLogical(*CRI.TrueDefs.second);
715 AllCRLogicalOps.
push_back(createCRLogicalOpInfo(*CRI.TrueDefs.first));
717 AllCRLogicalOps.
push_back(createCRLogicalOpInfo(*CRI.TrueDefs.second));
722void PPCReduceCRLogicals::collectCRLogicals() {
723 for (MachineBasicBlock &
MBB : *MF) {
724 for (MachineInstr &
MI :
MBB) {
725 if (isCRLogical(
MI)) {
726 AllCRLogicalOps.
push_back(createCRLogicalOpInfo(
MI));
728 if (AllCRLogicalOps.
back().IsNullary)
729 TotalNullaryCRLogicals++;
730 else if (AllCRLogicalOps.
back().IsBinary)
731 TotalBinaryCRLogicals++;
733 TotalUnaryCRLogicals++;
740 "PowerPC Reduce CR logical Operation",
false,
false)
745char PPCReduceCRLogicals::ID = 0;
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
const HexagonInstrInfo * TII
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
Register const TargetRegisterInfo * TRI
Promote Memory to Register
static bool isBinary(MachineInstr &MI)
static bool isNullary(MachineInstr &MI)
static bool splitMBB(BlockSplitInfo &BSI)
Splits a MachineBasicBlock to branch before SplitBefore.
static void computeBranchTargetAndInversion(unsigned CROp, unsigned BROp, bool UsingDef1, bool &InvertNewBranch, bool &InvertOrigBranch, bool &TargetIsFallThrough)
Given a CR logical operation CROp, branch opcode BROp as well as a flag to indicate if the first oper...
static void addIncomingValuesToPHIs(MachineBasicBlock *Successor, MachineBasicBlock *OrigMBB, MachineBasicBlock *NewMBB, MachineRegisterInfo *MRI)
Given a basic block Successor that potentially contains PHIs, this function will look for PHIs that h...
static void updatePHIs(MachineBasicBlock *Successor, MachineBasicBlock *OrigMBB, MachineBasicBlock *NewMBB, MachineRegisterInfo *MRI)
Given a basic block Successor that potentially contains PHIs, this function will look for any incomin...
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
MachineRegisterInfo & MRI
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, const llvm::StringTable &StandardNames, VectorLibrary VecLib)
Initialize the set of available library functions based on the specified target triple.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
static constexpr BranchProbability getUnknown()
BranchProbability getCompl() const
FunctionPass class - This class is used to implement most global optimizations.
const HexagonRegisterInfo & getRegisterInfo() const
A set of physical registers with utility functions to track liveness when walking backward/forward th...
LLVM_ABI void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
void setCallFrameSize(unsigned N)
Set the call frame size on entry to this basic block.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
succ_iterator succ_begin()
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
unsigned succ_size() const
LLVM_ABI void dump() const
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
succ_reverse_iterator succ_rbegin()
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< succ_iterator > successors()
LLVM_ABI bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI BranchProbability getEdgeProbability(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
Analysis pass which computes a MachineDominatorTree.
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.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
BasicBlockListType::iterator iterator
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI void dump() const
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
MachineBasicBlock * getMBB() const
void setMBB(MachineBasicBlock *MBB)
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI bool hasOneNonDBGUse(Register RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug use of the specified register.
LLVM_ABI LLVM_READONLY MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
use_instr_nodbg_iterator use_instr_nodbg_begin(Register RegNo) const
iterator_range< use_instr_nodbg_iterator > use_nodbg_instructions(Register Reg) const
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
void push_back(const T &Elt)
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
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...
FunctionPass * createPPCReduceCRLogicalsPass()
LLVM_ABI void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.