90#define DEBUG_TYPE "hexagon-eif"
104 cl::desc(
"Size limit in Hexagon early if-conversion"));
117 return OS <<
"<none>";
118 return OS <<
'#' <<
P.MB->getNumber();
122 FlowPattern() =
default;
125 : SplitB(
B), TrueB(
TB), FalseB(FB), JoinB(JB), PredR(PR) {}
138 const FlowPattern &
FP;
145 OS <<
"{ SplitB:" << PrintMB(
P.FP.SplitB)
147 <<
", TrueB:" << PrintMB(
P.FP.TrueB)
148 <<
", FalseB:" << PrintMB(
P.FP.FalseB)
149 <<
", JoinB:" << PrintMB(
P.FP.JoinB) <<
" }";
160 return "Hexagon early if conversion";
186 bool isValid(
const FlowPattern &
FP)
const;
189 const FlowPattern &
FP)
const;
193 bool isPredicate(
unsigned R)
const;
195 unsigned getCondStoreOpcode(
unsigned Opc,
bool IfTrue)
const;
200 unsigned PredR,
bool IfTrue);
204 unsigned TSR,
unsigned FR,
unsigned FSR);
211 void simplifyFlowGraph(
const FlowPattern &
FP);
225char HexagonEarlyIfConversion::ID = 0;
228 "Hexagon early if conversion",
false,
false)
231 if (
B->succ_size() != 1)
235 return L && SB == L->getHeader() && MDT->dominates(
B, SB);
250 unsigned Opc = T1I->getOpcode();
251 if (Opc != Hexagon::J2_jumpt && Opc != Hexagon::J2_jumpf)
253 Register PredR = T1I->getOperand(0).getReg();
262 assert(T2I ==
B->end() || T2I->getOpcode() == Hexagon::J2_jump);
264 : T2I->getOperand(0).getMBB();
273 if (Opc == Hexagon::J2_jumpt)
278 if (!MDT->properlyDominates(
B, TB) || !MDT->properlyDominates(
B, FB))
285 assert(TB && FB &&
"Failed to find triangle control flow blocks");
286 unsigned TNP =
TB->pred_size(), FNP = FB->pred_size();
287 unsigned TNS =
TB->succ_size(), FNS = FB->succ_size();
294 bool TOk = (TNP == 1 && TNS == 1 && MLI->getLoopFor(TB) ==
L);
295 bool FOk = (FNP == 1 && FNS == 1 && MLI->getLoopFor(FB) ==
L);
328 if ((TB && isPreheader(TB)) || (FB && isPreheader(FB))) {
329 LLVM_DEBUG(
dbgs() <<
"One of blocks " << PrintMB(TB) <<
", " << PrintMB(FB)
330 <<
" is a loop preheader. Skipping.\n");
334 FP = FlowPattern(
B, PredR, TB, FB, JB);
365 if (
B->isEHPad() ||
B->hasAddressTaken())
370 for (
auto &
MI : *
B) {
371 if (
MI.isDebugInstr())
373 if (
MI.isConditionalBranch())
375 unsigned Opc =
MI.getOpcode();
376 bool IsJMP = (Opc == Hexagon::J2_jump);
377 if (!isPredicableStore(&
MI) && !IsJMP && !isSafeToSpeculate(&
MI))
386 if (!MO.isReg() || !MO.isDef())
394 if (
U.getParent()->isPHI())
401bool HexagonEarlyIfConversion::usesUndefVReg(
const MachineInstr *
MI)
const {
403 if (!MO.isReg() || !MO.isUse())
410 assert(DefI &&
"Expecting a reaching def in MRI");
417bool HexagonEarlyIfConversion::isValid(
const FlowPattern &
FP)
const {
418 if (hasEHLabel(
FP.SplitB))
420 if (
FP.TrueB && !isValidCandidate(
FP.TrueB))
422 if (
FP.FalseB && !isValidCandidate(
FP.FalseB))
439 if (usesUndefVReg(&
MI))
442 if (isPredicate(DefR))
450 const FlowPattern &
FP)
const {
451 if (
B->pred_size() < 2)
463 for (
unsigned i = 1, e =
MI.getNumOperands(); i != e; i += 2) {
465 if (BB ==
FP.SplitB || BB ==
FP.TrueB || BB ==
FP.FalseB)
482 if (!HII->isPredicable(*Def1) || !HII->isPredicable(*Def3))
488unsigned HexagonEarlyIfConversion::countPredicateDefs(
490 unsigned PredDefs = 0;
491 for (
auto &
MI : *
B) {
493 if (!MO.isReg() || !MO.isDef())
505bool HexagonEarlyIfConversion::isProfitable(
const FlowPattern &
FP)
const {
508 if (MBPI &&
FP.TrueB && !
FP.FalseB &&
509 (MBPI->getEdgeProbability(
FP.SplitB,
FP.TrueB) < JumpProb ||
510 MBPI->getEdgeProbability(
FP.SplitB,
FP.TrueB) > Prob))
513 if (MBPI && !
FP.TrueB &&
FP.FalseB &&
514 (MBPI->getEdgeProbability(
FP.SplitB,
FP.FalseB) < JumpProb ||
515 MBPI->getEdgeProbability(
FP.SplitB,
FP.FalseB) > Prob))
518 if (
FP.TrueB &&
FP.FalseB) {
521 if (MBPI->getEdgeProbability(
FP.SplitB,
FP.TrueB) > Prob)
523 if (MBPI->getEdgeProbability(
FP.SplitB,
FP.FalseB) > Prob)
545 unsigned T = std::count_if(
B->begin(),
B->getFirstTerminator(),
547 return !MI.isMetaInstruction();
554 unsigned TotalIn = TotalCount(
FP.TrueB, Spare) + TotalCount(
FP.FalseB, Spare);
556 dbgs() <<
"Total number of instructions to be predicated/speculated: "
557 << TotalIn <<
", spare room: " << Spare <<
"\n");
567 unsigned TotalPh = 0;
568 unsigned PredDefs = countPredicateDefs(
FP.SplitB);
570 TotalPh = computePhiCost(
FP.JoinB,
FP);
571 PredDefs += countPredicateDefs(
FP.JoinB);
573 if (
FP.TrueB && !
FP.TrueB->succ_empty()) {
575 TotalPh += computePhiCost(SB,
FP);
576 PredDefs += countPredicateDefs(SB);
578 if (
FP.FalseB && !
FP.FalseB->succ_empty()) {
580 TotalPh += computePhiCost(SB,
FP);
581 PredDefs += countPredicateDefs(SB);
584 LLVM_DEBUG(
dbgs() <<
"Total number of extra muxes from converted phis: "
589 LLVM_DEBUG(
dbgs() <<
"Total number of predicate registers: " << PredDefs
599 bool Changed =
false;
612 DTNodeVectType Cn(llvm::children<MachineDomTreeNode *>(
N));
616 Changed |= visitBlock(SB, L);
621 if (MLI->getLoopFor(
B) != L)
625 if (!matchFlowPattern(
B, L,
FP))
638 simplifyFlowGraph(
FP);
642bool HexagonEarlyIfConversion::visitLoop(
MachineLoop *L) {
645 :
dbgs() <<
"Visiting function")
647 bool Changed =
false;
650 Changed |= visitLoop(
I);
654 Changed |= visitBlock(L ? HB : EntryB, L);
658bool HexagonEarlyIfConversion::isPredicableStore(
const MachineInstr *
MI)
663 unsigned Opc =
MI->getOpcode();
665 case Hexagon::S2_storerb_io:
666 case Hexagon::S2_storerbnew_io:
667 case Hexagon::S2_storerh_io:
668 case Hexagon::S2_storerhnew_io:
669 case Hexagon::S2_storeri_io:
670 case Hexagon::S2_storerinew_io:
671 case Hexagon::S2_storerd_io:
672 case Hexagon::S4_storeirb_io:
673 case Hexagon::S4_storeirh_io:
674 case Hexagon::S4_storeiri_io:
682bool HexagonEarlyIfConversion::isSafeToSpeculate(
const MachineInstr *
MI)
684 if (
MI->mayLoadOrStore())
686 if (
MI->isCall() ||
MI->isBarrier() ||
MI->isBranch())
688 if (
MI->hasUnmodeledSideEffects())
690 if (
MI->getOpcode() == TargetOpcode::LIFETIME_END)
696bool HexagonEarlyIfConversion::isPredicate(
unsigned R)
const {
698 return RC == &Hexagon::PredRegsRegClass ||
699 RC == &Hexagon::HvxQRRegClass;
702unsigned HexagonEarlyIfConversion::getCondStoreOpcode(
unsigned Opc,
704 return HII->getCondOpcode(Opc, !IfTrue);
709 unsigned PredR,
bool IfTrue) {
711 if (At != ToB->
end())
712 DL = At->getDebugLoc();
713 else if (!ToB->
empty())
716 unsigned Opc =
MI->getOpcode();
718 if (isPredicableStore(
MI)) {
719 unsigned COpc = getCondStoreOpcode(Opc, IfTrue);
723 if (HII->isPostIncrement(*
MI)) {
734 MI->eraseFromParent();
738 if (Opc == Hexagon::J2_jump) {
740 const MCInstrDesc &
D = HII->get(IfTrue ? Hexagon::J2_jumpt
741 : Hexagon::J2_jumpf);
745 MI->eraseFromParent();
760 unsigned PredR,
bool IfTrue) {
761 LLVM_DEBUG(
dbgs() <<
"Predicating block " << PrintMB(FromB) <<
"\n");
767 NextI = std::next(
I);
768 if (isSafeToSpeculate(&*
I))
771 predicateInstr(ToB, At, &*
I, PredR, IfTrue);
777 unsigned PredR,
unsigned TR,
unsigned TSR,
unsigned FR,
unsigned FSR) {
779 switch (DRC->
getID()) {
780 case Hexagon::IntRegsRegClassID:
781 case Hexagon::IntRegsLow8RegClassID:
782 Opc = Hexagon::C2_mux;
784 case Hexagon::DoubleRegsRegClassID:
785 case Hexagon::GeneralDoubleLow8RegsRegClassID:
786 Opc = Hexagon::PS_pselect;
788 case Hexagon::HvxVRRegClassID:
789 Opc = Hexagon::PS_vselect;
791 case Hexagon::HvxWRRegClassID:
792 Opc = Hexagon::PS_wselect;
809 const FlowPattern &
FP) {
813 for (
auto I = WhereB->
begin();
I != NonPHI; ++
I) {
816 unsigned TR = 0, TSR = 0, FR = 0, FSR = 0, SR = 0, SSR = 0;
819 if (BO.getMBB() ==
FP.SplitB)
821 else if (BO.getMBB() ==
FP.TrueB)
823 else if (BO.getMBB() ==
FP.FalseB)
836 unsigned MuxR = 0, MuxSR = 0;
841 MuxR = buildMux(
FP.SplitB,
FP.SplitB->getFirstTerminator(), RC,
842 FP.PredR, TR, TSR, FR, FSR);
852 false,
false, MuxSR));
857void HexagonEarlyIfConversion::convert(
const FlowPattern &
FP) {
864 TSB = *
FP.TrueB->succ_begin();
865 predicateBlockNB(
FP.SplitB, OldTI,
FP.TrueB,
FP.PredR,
true);
868 FSB = *
FP.FalseB->succ_begin();
870 predicateBlockNB(
FP.SplitB, At,
FP.FalseB,
FP.PredR,
false);
877 FP.SplitB->erase(OldTI,
FP.SplitB->end());
878 while (!
FP.SplitB->succ_empty()) {
894 if (
T !=
FP.TrueB &&
T !=
FP.FalseB) {
898 FP.SplitB->removeSuccessor(
FP.SplitB->succ_begin());
907 BuildMI(*
FP.SplitB,
FP.SplitB->end(),
DL, HII->get(Hexagon::J2_jump))
909 FP.SplitB->addSuccessor(
FP.JoinB);
911 bool HasBranch =
false;
913 BuildMI(*
FP.SplitB,
FP.SplitB->end(),
DL, HII->get(Hexagon::J2_jumpt))
916 FP.SplitB->addSuccessor(TSB);
920 const MCInstrDesc &
D = HasBranch ? HII->get(Hexagon::J2_jump)
921 : HII->get(Hexagon::J2_jumpf);
926 FP.SplitB->addSuccessor(FSB);
933 BuildMI(*
FP.SplitB,
FP.SplitB->end(),
DL, HII->get(Hexagon::J2_jump))
935 FP.SplitB->addSuccessor(SSB);
964 DTNodeVectType Cn(GTN::child_begin(
N), GTN::child_end(
N));
967 MDT->changeImmediateDominator(SB, IDB);
971 while (!
B->succ_empty())
972 B->removeSuccessor(
B->succ_begin());
975 Pred->removeSuccessor(
B,
true);
979 MFN->erase(
B->getIterator());
983 LLVM_DEBUG(
dbgs() <<
"Removing phi nodes from block " << PrintMB(
B) <<
"\n");
985 for (
I =
B->begin();
I != NonPHI;
I = NextI) {
986 NextI = std::next(
I);
992 unsigned NewR = UseR;
999 NewR =
MRI->createVirtualRegister(RC);
1000 NonPHI =
BuildMI(*
B, NonPHI,
DL, HII->get(TargetOpcode::COPY), NewR)
1003 MRI->replaceRegWith(DefR, NewR);
1010 LLVM_DEBUG(
dbgs() <<
"Merging blocks " << PrintMB(PredB) <<
" and "
1011 << PrintMB(SuccB) <<
"\n");
1012 bool TermOk = hasUncondBranch(SuccB);
1013 eliminatePhis(SuccB);
1014 HII->removeBranch(*PredB);
1024void HexagonEarlyIfConversion::simplifyFlowGraph(
const FlowPattern &
FP) {
1027 removeBlock(
FP.TrueB);
1029 removeBlock(
FP.FalseB);
1031 FP.SplitB->updateTerminator(OldLayoutSuccessor);
1032 if (
FP.SplitB->succ_size() != 1)
1044 if (!hasEHLabel(SB) || hasUncondBranch(SB))
1045 mergeBlocks(
FP.SplitB, SB);
1048bool HexagonEarlyIfConversion::runOnMachineFunction(
MachineFunction &MF) {
1053 HII =
ST.getInstrInfo();
1054 TRI =
ST.getRegisterInfo();
1057 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
1058 MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
1060 ? &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI()
1064 bool Changed =
false;
1067 Changed |= visitLoop(L);
1068 Changed |= visitLoop(
nullptr);
1077 return new HexagonEarlyIfConversion();
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_ATTRIBUTE_UNUSED
This file defines the DenseSet and SmallDenseSet classes.
static cl::opt< bool > EnableHexagonBP("enable-hexagon-br-prob", cl::Hidden, cl::init(true), cl::desc("Enable branch probability info"))
static cl::opt< bool > SkipExitBranches("eif-no-loop-exit", cl::init(false), cl::Hidden, cl::desc("Do not convert branches that may exit the loop"))
static cl::opt< unsigned > SizeLimit("eif-limit", cl::init(6), cl::Hidden, cl::desc("Size limit in Hexagon early if-conversion"))
#define HEXAGON_PACKET_SIZE
unsigned const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SI optimize exec mask operations pre RA
This file defines the SmallVector class.
static bool isProfitable(const SmallVector< std::unique_ptr< StableFunctionMap::StableFunctionEntry > > &SFS)
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Implements a dense probed hash-table based set.
Base class for the actual dominator tree node.
FunctionPass class - This class is used to implement most global optimizations.
Describe properties that are true of each instruction in the target description file.
unsigned pred_size() const
void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
void updateTerminator(MachineBasicBlock *PreviousLayoutSuccessor)
Update the terminator instructions in block to account for changes to block layout which may have bee...
succ_iterator succ_begin()
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode 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 '...
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & cloneMemRefs(const MachineInstr &OtherMI) const
Representation of each machine instruction.
bool isImplicitDef() const
unsigned getNumOperands() const
Retuns the total number of operands.
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
void removeOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
bool isReg() const
isReg - Tests if this is a MO_Register 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 MachineOperand CreateMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Wrapper class representing virtual and physical registers.
A global registry used in conjunction with static constructors to make pluggable components (like tar...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
unsigned getID() const
Return the register class ID number.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
This class implements an extremely fast bulk output stream that can only output to a stream.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ TB
TB - TwoByte - Set if this instruction has a two byte opcode, which starts with a 0x0F byte before th...
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
FunctionPass * createHexagonEarlyIfConversion()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void initializeHexagonEarlyIfConversionPass(PassRegistry &Registry)
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
void updatePhiNodes(BasicBlock *DestBB, BasicBlock *OldPred, BasicBlock *NewPred, PHINode *Until=nullptr)
Replaces all uses of OldPred with the NewPred block in all PHINodes in a block.
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.