26 #include "llvm/Config/llvm-config.h" 32 #define DEBUG_TYPE "ppc-reduce-cr-ops" 35 "Number of single-use binary CR logical ops contained in a block");
37 "Number of binary CR logical ops that can be used to split blocks");
38 STATISTIC(TotalCRLogicals,
"Number of CR logical ops.");
40 "Number of nullary CR logical ops (CRSET/CRUNSET).");
41 STATISTIC(TotalUnaryCRLogicals,
"Number of unary CR logical ops.");
42 STATISTIC(TotalBinaryCRLogicals,
"Number of CR logical ops.");
44 "Number of blocks split on CR binary logical ops.");
46 "Number of blocks not split due to operands being identical.");
48 "Number of blocks not split due to operands being chained copies.");
50 "Number of blocks not split due to the wrong opcode.");
58 for (
auto &
MI : Successor->
instrs()) {
63 for (
unsigned i = 2,
e =
MI.getNumOperands() + 1; i !=
e; i += 2) {
65 if (MO.
getMBB() == OrigMBB) {
67 if (
MI.getOperand(i - 1).isReg()) {
90 "NewMBB must be a successor of OrigMBB");
91 for (
auto &
MI : Successor->
instrs()) {
96 for (
unsigned i = 2,
e =
MI.getNumOperands() + 1; i !=
e; i += 2) {
98 if (MO.
getMBB() == OrigMBB) {
100 MIB.
addReg(
MI.getOperand(i - 1).getReg()).addMBB(NewMBB);
118 if (!OrigBranch || !SplitBefore || !SplitCond)
123 if (MIToDelete && MIToDelete->
getParent() != MBB)
125 if (NewCond && NewCond->
getParent() != MBB)
142 "All instructions must be in the same block.");
147 assert(MRI->
isSSA() &&
"Can only do this while the function is in SSA form.");
148 if (ThisMBB->succ_size() != 2) {
150 dbgs() <<
"Don't know how to handle blocks that don't have exactly" 151 <<
" two successors.\n");
157 unsigned InvertedOpcode =
158 OrigBROpcode == PPC::BC
160 : OrigBROpcode == PPC::BCn
162 : OrigBROpcode == PPC::BCLR ? PPC::BCLRn : PPC::BCLR;
163 unsigned NewBROpcode = BSI.
InvertNewBranch ? InvertedOpcode : OrigBROpcode;
166 ? *ThisMBB->succ_rbegin()
167 : *ThisMBB->succ_begin();
187 ProbFallThrough = ProbToNewTarget.
getCompl();
188 ProbOrigFallThrough = ProbToNewTarget / ProbToNewTarget.
getCompl();
189 ProbOrigTarget = ProbOrigFallThrough.
getCompl();
192 ProbFallThrough = ProbToNewTarget.
getCompl();
193 ProbOrigTarget = ProbToNewTarget / ProbToNewTarget.
getCompl();
194 ProbOrigFallThrough = ProbOrigTarget.
getCompl();
200 const BasicBlock *LLVM_BB = ThisMBB->getBasicBlock();
206 NewMBB->
splice(NewMBB->
end(), ThisMBB, InsertPoint, ThisMBB->end());
216 ThisMBB->addSuccessor(NewBRTarget, ProbToNewTarget);
217 ThisMBB->addSuccessor(NewMBB, ProbFallThrough);
221 TII->get(NewBROpcode))
223 .addMBB(NewBRTarget);
233 assert(FirstTerminator->getOperand(0).isReg() &&
234 "Can't update condition of unconditional branch.");
238 FirstTerminator->setDesc(TII->get(InvertedOpcode));
247 LLVM_DEBUG(
dbgs() <<
"After splitting, ThisMBB:\n"; ThisMBB->dump());
268 bool &InvertNewBranch,
bool &InvertOrigBranch,
269 bool &TargetIsFallThrough) {
274 if (BROp == PPC::BC || BROp == PPC::BCLR) {
280 InvertNewBranch =
false;
281 InvertOrigBranch =
false;
282 TargetIsFallThrough =
false;
285 InvertNewBranch =
true;
286 InvertOrigBranch =
false;
287 TargetIsFallThrough =
true;
290 InvertNewBranch =
true;
291 InvertOrigBranch =
true;
292 TargetIsFallThrough =
false;
295 InvertNewBranch =
false;
296 InvertOrigBranch =
true;
297 TargetIsFallThrough =
true;
300 InvertNewBranch = UsingDef1;
301 InvertOrigBranch = !UsingDef1;
302 TargetIsFallThrough =
false;
305 InvertNewBranch = !UsingDef1;
306 InvertOrigBranch = !UsingDef1;
307 TargetIsFallThrough =
true;
310 }
else if (BROp == PPC::BCn || BROp == PPC::BCLRn) {
316 InvertNewBranch =
true;
317 InvertOrigBranch =
false;
318 TargetIsFallThrough =
true;
321 InvertNewBranch =
false;
322 InvertOrigBranch =
false;
323 TargetIsFallThrough =
false;
326 InvertNewBranch =
false;
327 InvertOrigBranch =
true;
328 TargetIsFallThrough =
true;
331 InvertNewBranch =
true;
332 InvertOrigBranch =
true;
333 TargetIsFallThrough =
false;
336 InvertNewBranch = !UsingDef1;
337 InvertOrigBranch = !UsingDef1;
338 TargetIsFallThrough =
true;
341 InvertNewBranch = UsingDef1;
342 InvertOrigBranch = !UsingDef1;
343 TargetIsFallThrough =
false;
356 struct CRLogicalOpInfo {
359 std::pair<MachineInstr*, MachineInstr*> CopyDefs;
360 std::pair<MachineInstr*, MachineInstr*> TrueDefs;
361 unsigned IsBinary : 1;
362 unsigned IsNullary : 1;
363 unsigned ContainedInBlock : 1;
364 unsigned FeedsISEL : 1;
365 unsigned FeedsBR : 1;
366 unsigned FeedsLogical : 1;
367 unsigned SingleUse : 1;
368 unsigned DefsSingleUse : 1;
371 CRLogicalOpInfo() :
MI(
nullptr), IsBinary(0), IsNullary(0),
372 ContainedInBlock(0), FeedsISEL(0), FeedsBR(0),
373 FeedsLogical(0), SingleUse(0), DefsSingleUse(1),
374 SubregDef1(0), SubregDef2(0) { }
387 void collectCRLogicals();
388 bool handleCROp(
unsigned Idx);
389 bool splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI);
392 return Opc == PPC::CRAND || Opc == PPC::CRNAND || Opc == PPC::CROR ||
393 Opc == PPC::CRXOR || Opc == PPC::CRNOR || Opc == PPC::CREQV ||
394 Opc == PPC::CRANDC || Opc == PPC::CRORC || Opc == PPC::CRSET ||
397 bool simplifyCode() {
398 bool Changed =
false;
401 for (
unsigned i = 0; i < AllCRLogicalOps.
size(); i++)
402 Changed |= handleCROp(i);
424 return simplifyCode();
426 CRLogicalOpInfo createCRLogicalOpInfo(
MachineInstr &MI);
434 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 436 dbgs() <<
"CRLogicalOpMI: ";
438 dbgs() <<
"IsBinary: " << IsBinary <<
", FeedsISEL: " << FeedsISEL;
439 dbgs() <<
", FeedsBR: " << FeedsBR <<
", FeedsLogical: ";
440 dbgs() << FeedsLogical <<
", SingleUse: " << SingleUse;
441 dbgs() <<
", DefsSingleUse: " << DefsSingleUse;
442 dbgs() <<
", SubregDef1: " << SubregDef1 <<
", SubregDef2: ";
443 dbgs() << SubregDef2 <<
", ContainedInBlock: " << ContainedInBlock;
445 dbgs() <<
"\nDefs:\n";
446 TrueDefs.first->dump();
449 TrueDefs.second->dump();
451 if (CopyDefs.first) {
452 dbgs() <<
"CopyDef1: ";
453 CopyDefs.first->dump();
455 if (CopyDefs.second) {
456 dbgs() <<
"CopyDef2: ";
457 CopyDefs.second->dump();
462 PPCReduceCRLogicals::CRLogicalOpInfo
463 PPCReduceCRLogicals::createCRLogicalOpInfo(
MachineInstr &MIParam) {
469 Ret.TrueDefs = std::make_pair(
nullptr,
nullptr);
470 Ret.CopyDefs = std::make_pair(
nullptr,
nullptr);
473 Ret.SubregDef1, Ret.CopyDefs.first);
474 assert(Def1 &&
"Must be able to find a definition of operand 1.");
478 MRI->hasOneNonDBGUse(Ret.CopyDefs.first->getOperand(0).getReg());
483 Ret.CopyDefs.second);
484 assert(Def2 &&
"Must be able to find a definition of operand 2.");
488 MRI->hasOneNonDBGUse(Ret.CopyDefs.second->getOperand(0).getReg());
489 Ret.TrueDefs = std::make_pair(Def1, Def2);
491 Ret.TrueDefs = std::make_pair(Def1,
nullptr);
492 Ret.CopyDefs.second =
nullptr;
496 Ret.ContainedInBlock = 1;
500 unsigned Opc =
UseMI.getOpcode();
501 if (Opc == PPC::ISEL || Opc == PPC::ISEL8)
503 if (Opc == PPC::BC || Opc == PPC::BCn || Opc == PPC::BCLR ||
506 Ret.FeedsLogical = isCRLogical(
UseMI);
508 Ret.ContainedInBlock = 0;
513 if (!Ret.IsNullary) {
514 Ret.ContainedInBlock &=
515 (MIParam.
getParent() == Ret.TrueDefs.first->getParent());
517 Ret.ContainedInBlock &=
518 (MIParam.
getParent() == Ret.TrueDefs.second->getParent());
521 if (Ret.IsBinary && Ret.ContainedInBlock && Ret.SingleUse) {
522 NumContainedSingleUseBinOps++;
523 if (Ret.FeedsBR && Ret.DefsSingleUse)
550 if (CopySrc == PPC::CR0EQ || CopySrc == PPC::CR6EQ)
551 Subreg = PPC::sub_eq;
552 if (CopySrc == PPC::CR0LT || CopySrc == PPC::CR6LT)
553 Subreg = PPC::sub_lt;
554 if (CopySrc == PPC::CR0GT || CopySrc == PPC::CR6GT)
555 Subreg = PPC::sub_gt;
556 if (CopySrc == PPC::CR0UN || CopySrc == PPC::CR6UN)
557 Subreg = PPC::sub_un;
561 if ((--Me)->modifiesRegister(CopySrc, TRI))
565 return MRI->getVRegDef(CopySrc);
570 MRI = &MF->getRegInfo();
572 MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
574 AllCRLogicalOps.clear();
582 bool PPCReduceCRLogicals::handleCROp(
unsigned Idx) {
585 bool Changed =
false;
586 CRLogicalOpInfo CRI = AllCRLogicalOps[Idx];
587 if (CRI.IsBinary && CRI.ContainedInBlock && CRI.SingleUse && CRI.FeedsBR &&
589 Changed = splitBlockOnBinaryCROp(CRI);
591 NumBlocksSplitOnBinaryCROp++;
613 bool PPCReduceCRLogicals::splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI) {
614 if (CRI.CopyDefs.first == CRI.CopyDefs.second) {
615 LLVM_DEBUG(
dbgs() <<
"Unable to split as the two operands are the same\n");
616 NumNotSplitIdenticalOperands++;
619 if (CRI.TrueDefs.first->isCopy() || CRI.TrueDefs.second->isCopy() ||
620 CRI.TrueDefs.first->isPHI() || CRI.TrueDefs.second->isPHI()) {
622 dbgs() <<
"Unable to split because one of the operands is a PHI or " 623 "chain of copies.\n");
624 NumNotSplitChainCopies++;
628 if (CRI.MI->getOpcode() != PPC::CROR &&
629 CRI.MI->getOpcode() != PPC::CRAND &&
630 CRI.MI->getOpcode() != PPC::CRNOR &&
631 CRI.MI->getOpcode() != PPC::CRNAND &&
632 CRI.MI->getOpcode() != PPC::CRORC &&
633 CRI.MI->getOpcode() != PPC::CRANDC) {
635 NumNotSplitWrongOpcode++;
638 LLVM_DEBUG(
dbgs() <<
"Splitting the following CR op:\n"; CRI.dump());
642 bool UsingDef1 =
false;
644 for (
auto E = CRI.MI->getParent()->end(); Def2It !=
E; ++Def2It) {
645 if (Def1It == Def2It) {
646 SplitBefore = &*Def1It;
658 MRI->use_nodbg_begin(CRI.MI->getOperand(0).getReg())->
getParent();
667 UsingDef1 ? CRI.TrueDefs.first : CRI.TrueDefs.second;
669 UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second;
675 MBB->
splice(FirstTerminator, MBB, FirstInstrToMove);
676 if (FirstInstrToMove != SecondInstrToMove)
677 MBB->
splice(FirstTerminator, MBB, SecondInstrToMove);
678 MBB->
splice(FirstTerminator, MBB, CRI.MI);
680 unsigned Opc = CRI.MI->getOpcode();
681 bool InvertOrigBranch, InvertNewBranch, TargetIsFallThrough;
683 InvertNewBranch, InvertOrigBranch,
684 TargetIsFallThrough);
686 UsingDef1 ? CRI.CopyDefs.second : CRI.CopyDefs.first;
687 LLVM_DEBUG(
dbgs() <<
"We will " << (InvertNewBranch ?
"invert" :
"copy"));
688 LLVM_DEBUG(
dbgs() <<
" the original branch and the target is the " 689 << (TargetIsFallThrough ?
"fallthrough block\n" 690 :
"orig. target block\n"));
691 LLVM_DEBUG(
dbgs() <<
"Original branch instruction: "; Branch->dump());
693 InvertOrigBranch, TargetIsFallThrough, MBPI, CRI.MI,
694 UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second };
699 bool Input1CRlogical =
700 CRI.TrueDefs.first && isCRLogical(*CRI.TrueDefs.first);
701 bool Input2CRlogical =
702 CRI.TrueDefs.second && isCRLogical(*CRI.TrueDefs.second);
704 AllCRLogicalOps.push_back(createCRLogicalOpInfo(*CRI.TrueDefs.first));
706 AllCRLogicalOps.push_back(createCRLogicalOpInfo(*CRI.TrueDefs.second));
711 void PPCReduceCRLogicals::collectCRLogicals() {
714 if (isCRLogical(
MI)) {
715 AllCRLogicalOps.push_back(createCRLogicalOpInfo(
MI));
717 if (AllCRLogicalOps.back().IsNullary)
718 TotalNullaryCRLogicals++;
719 else if (AllCRLogicalOps.back().IsBinary)
720 TotalBinaryCRLogicals++;
722 TotalUnaryCRLogicals++;
731 "PowerPC Reduce CR logical Operation",
false,
false)
736 char PPCReduceCRLogicals::
ID = 0;
INITIALIZE_PASS_BEGIN(PPCReduceCRLogicals, DEBUG_TYPE, "PowerPC Reduce CR logical Operation", false, false) INITIALIZE_PASS_END(PPCReduceCRLogicals
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
BranchProbability getCompl() const
MachineBasicBlock * getMBB() const
This class represents lattice values for constants.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
bool allInstrsInSameMBB()
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
unsigned getSubReg() const
MachineInstr * OrigBranch
static void dump(StringRef Title, SpillInfo const &Spills)
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
iterator_range< succ_iterator > successors()
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Retuns the total number of operands.
static bool isNullary(MachineInstr &MI)
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
void initializePPCReduceCRLogicalsPass(PassRegistry &)
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
MachineInstr * getVRegDef(unsigned Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
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...
FunctionPass * createPPCReduceCRLogicalsPass()
MachineInstr * SplitBefore
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringLiteral > StandardNames)
Initialize the set of available library functions based on the specified target triple.
void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
unsigned const MachineRegisterInfo * MRI
LLVM Basic Block Representation.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
PowerPC Reduce CR logical Operation
ch, gl = CR6[UN]SET ch, inglue - Toggle CR bit 6 for SVR4 vararg calls
MachineInstrBuilder & UseMI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void setMBB(MachineBasicBlock *MBB)
Represent the analysis usage information of a pass.
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...
FunctionPass class - This class is used to implement most global optimizations.
succ_iterator succ_begin()
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
static bool isBinary(MachineInstr &MI)
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static BranchProbability getUnknown()
Iterator for intrusive lists based on ilist_node.
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
MachineInstr * MIToDelete
MachineInstrBuilder MachineInstrBuilder & DefMI
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.
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
BranchProbability getEdgeProbability(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
Representation of each machine instruction.
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 '...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool useCRBits() const
useCRBits - Return true if we should store and manipulate i1 values in the individual condition regis...
bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void insert(iterator MBBI, MachineBasicBlock *MBB)
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 bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
static const Function * getParent(const Value *V)
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
const MachineBranchProbabilityInfo * MBPI
static bool splitMBB(BlockSplitInfo &BSI)
Splits a MachineBasicBlock to branch before SplitBefore.
Wrapper class representing virtual and physical registers.