#include "Hexagon.h"
#include "HexagonInstrInfo.h"
#include "HexagonSubtarget.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineLoopInfo.h"
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/Pass.h"
#include "llvm/Support/BranchProbability.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <iterator>
#define DEBUG_TYPE "hexagon-eif"
using namespace llvm;
namespace llvm {
FunctionPass *createHexagonEarlyIfConversion();
void initializeHexagonEarlyIfConversionPass(PassRegistry& Registry);
}
static cl::opt<bool> EnableHexagonBP("enable-hexagon-br-prob", cl::Hidden,
cl::init(true), cl::desc("Enable branch probability info"));
static cl::opt<unsigned> SizeLimit("eif-limit", cl::init(6), cl::Hidden,
cl::desc("Size limit in Hexagon early if-conversion"));
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"));
namespace {
struct PrintMB {
PrintMB(const MachineBasicBlock *B) : MB(B) {}
const MachineBasicBlock *MB;
};
raw_ostream &operator<< (raw_ostream &OS, const PrintMB &P) {
if (!P.MB)
return OS << "<none>";
return OS << '#' << P.MB->getNumber();
}
struct FlowPattern {
FlowPattern() = default;
FlowPattern(MachineBasicBlock *B, unsigned PR, MachineBasicBlock *TB,
MachineBasicBlock *FB, MachineBasicBlock *JB)
: SplitB(B), TrueB(TB), FalseB(FB), JoinB(JB), PredR(PR) {}
MachineBasicBlock *SplitB = nullptr;
MachineBasicBlock *TrueB = nullptr;
MachineBasicBlock *FalseB = nullptr;
MachineBasicBlock *JoinB = nullptr;
unsigned PredR = 0;
};
struct PrintFP {
PrintFP(const FlowPattern &P, const TargetRegisterInfo &T)
: FP(P), TRI(T) {}
const FlowPattern &FP;
const TargetRegisterInfo &TRI;
friend raw_ostream &operator<< (raw_ostream &OS, const PrintFP &P);
};
raw_ostream &operator<<(raw_ostream &OS,
const PrintFP &P) LLVM_ATTRIBUTE_UNUSED;
raw_ostream &operator<<(raw_ostream &OS, const PrintFP &P) {
OS << "{ SplitB:" << PrintMB(P.FP.SplitB)
<< ", PredR:" << printReg(P.FP.PredR, &P.TRI)
<< ", TrueB:" << PrintMB(P.FP.TrueB)
<< ", FalseB:" << PrintMB(P.FP.FalseB)
<< ", JoinB:" << PrintMB(P.FP.JoinB) << " }";
return OS;
}
class HexagonEarlyIfConversion : public MachineFunctionPass {
public:
static char ID;
HexagonEarlyIfConversion() : MachineFunctionPass(ID) {}
StringRef getPassName() const override {
return "Hexagon early if conversion";
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<MachineBranchProbabilityInfo>();
AU.addRequired<MachineDominatorTree>();
AU.addPreserved<MachineDominatorTree>();
AU.addRequired<MachineLoopInfo>();
MachineFunctionPass::getAnalysisUsage(AU);
}
bool runOnMachineFunction(MachineFunction &MF) override;
private:
using BlockSetType = DenseSet<MachineBasicBlock *>;
bool isPreheader(const MachineBasicBlock *B) const;
bool matchFlowPattern(MachineBasicBlock *B, MachineLoop *L,
FlowPattern &FP);
bool visitBlock(MachineBasicBlock *B, MachineLoop *L);
bool visitLoop(MachineLoop *L);
bool hasEHLabel(const MachineBasicBlock *B) const;
bool hasUncondBranch(const MachineBasicBlock *B) const;
bool isValidCandidate(const MachineBasicBlock *B) const;
bool usesUndefVReg(const MachineInstr *MI) const;
bool isValid(const FlowPattern &FP) const;
unsigned countPredicateDefs(const MachineBasicBlock *B) const;
unsigned computePhiCost(const MachineBasicBlock *B,
const FlowPattern &FP) const;
bool isProfitable(const FlowPattern &FP) const;
bool isPredicableStore(const MachineInstr *MI) const;
bool isSafeToSpeculate(const MachineInstr *MI) const;
bool isPredicate(unsigned R) const;
unsigned getCondStoreOpcode(unsigned Opc, bool IfTrue) const;
void predicateInstr(MachineBasicBlock *ToB, MachineBasicBlock::iterator At,
MachineInstr *MI, unsigned PredR, bool IfTrue);
void predicateBlockNB(MachineBasicBlock *ToB,
MachineBasicBlock::iterator At, MachineBasicBlock *FromB,
unsigned PredR, bool IfTrue);
unsigned buildMux(MachineBasicBlock *B, MachineBasicBlock::iterator At,
const TargetRegisterClass *DRC, unsigned PredR, unsigned TR,
unsigned TSR, unsigned FR, unsigned FSR);
void updatePhiNodes(MachineBasicBlock *WhereB, const FlowPattern &FP);
void convert(const FlowPattern &FP);
void removeBlock(MachineBasicBlock *B);
void eliminatePhis(MachineBasicBlock *B);
void mergeBlocks(MachineBasicBlock *PredB, MachineBasicBlock *SuccB);
void simplifyFlowGraph(const FlowPattern &FP);
const HexagonInstrInfo *HII = nullptr;
const TargetRegisterInfo *TRI = nullptr;
MachineFunction *MFN = nullptr;
MachineRegisterInfo *MRI = nullptr;
MachineDominatorTree *MDT = nullptr;
MachineLoopInfo *MLI = nullptr;
BlockSetType Deleted;
const MachineBranchProbabilityInfo *MBPI = nullptr;
};
}
char HexagonEarlyIfConversion::ID = 0;
INITIALIZE_PASS(HexagonEarlyIfConversion, "hexagon-early-if",
"Hexagon early if conversion", false, false)
bool HexagonEarlyIfConversion::isPreheader(const MachineBasicBlock *B) const {
if (B->succ_size() != 1)
return false;
MachineBasicBlock *SB = *B->succ_begin();
MachineLoop *L = MLI->getLoopFor(SB);
return L && SB == L->getHeader() && MDT->dominates(B, SB);
}
bool HexagonEarlyIfConversion::matchFlowPattern(MachineBasicBlock *B,
MachineLoop *L, FlowPattern &FP) {
LLVM_DEBUG(dbgs() << "Checking flow pattern at " << printMBBReference(*B)
<< "\n");
MachineBasicBlock *TB = nullptr, *FB = nullptr;
MachineBasicBlock::const_iterator T1I = B->getFirstTerminator();
if (T1I == B->end())
return false;
unsigned Opc = T1I->getOpcode();
if (Opc != Hexagon::J2_jumpt && Opc != Hexagon::J2_jumpf)
return false;
Register PredR = T1I->getOperand(0).getReg();
MachineFunction::iterator NextBI = std::next(MachineFunction::iterator(B));
MachineBasicBlock *NextB = (NextBI != MFN->end()) ? &*NextBI : nullptr;
MachineBasicBlock *T1B = T1I->getOperand(1).getMBB();
MachineBasicBlock::const_iterator T2I = std::next(T1I);
assert(T2I == B->end() || T2I->getOpcode() == Hexagon::J2_jump);
MachineBasicBlock *T2B = (T2I == B->end()) ? NextB
: T2I->getOperand(0).getMBB();
if (T1B == T2B) {
return false;
}
if (Opc == Hexagon::J2_jumpt)
TB = T1B, FB = T2B;
else
TB = T2B, FB = T1B;
if (!MDT->properlyDominates(B, TB) || !MDT->properlyDominates(B, FB))
return false;
assert(TB && FB && "Failed to find triangle control flow blocks");
unsigned TNP = TB->pred_size(), FNP = FB->pred_size();
unsigned TNS = TB->succ_size(), FNS = FB->succ_size();
bool TOk = (TNP == 1 && TNS == 1 && MLI->getLoopFor(TB) == L);
bool FOk = (FNP == 1 && FNS == 1 && MLI->getLoopFor(FB) == L);
if (SkipExitBranches && MLI->getLoopFor(TB) != MLI->getLoopFor(FB))
return false;
if (!TOk && !FOk)
return false;
MachineBasicBlock *TSB = (TNS > 0) ? *TB->succ_begin() : nullptr;
MachineBasicBlock *FSB = (FNS > 0) ? *FB->succ_begin() : nullptr;
MachineBasicBlock *JB = nullptr;
if (TOk) {
if (FOk) {
if (TSB == FSB)
JB = TSB;
} else {
if (TSB == FB)
JB = FB;
FB = nullptr;
}
} else {
if (FSB == TB)
JB = TB;
TB = nullptr;
}
if ((TB && isPreheader(TB)) || (FB && isPreheader(FB))) {
LLVM_DEBUG(dbgs() << "One of blocks " << PrintMB(TB) << ", " << PrintMB(FB)
<< " is a loop preheader. Skipping.\n");
return false;
}
FP = FlowPattern(B, PredR, TB, FB, JB);
LLVM_DEBUG(dbgs() << "Detected " << PrintFP(FP, *TRI) << "\n");
return true;
}
bool HexagonEarlyIfConversion::hasEHLabel(const MachineBasicBlock *B) const {
for (auto &I : *B)
if (I.isEHLabel())
return true;
return false;
}
bool HexagonEarlyIfConversion::hasUncondBranch(const MachineBasicBlock *B)
const {
MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
while (I != E) {
if (I->isBarrier())
return true;
++I;
}
return false;
}
bool HexagonEarlyIfConversion::isValidCandidate(const MachineBasicBlock *B)
const {
if (!B)
return true;
if (B->isEHPad() || B->hasAddressTaken())
return false;
if (B->succ_empty())
return false;
for (auto &MI : *B) {
if (MI.isDebugInstr())
continue;
if (MI.isConditionalBranch())
return false;
unsigned Opc = MI.getOpcode();
bool IsJMP = (Opc == Hexagon::J2_jump);
if (!isPredicableStore(&MI) && !IsJMP && !isSafeToSpeculate(&MI))
return false;
for (const MachineOperand &MO : MI.operands()) {
if (!MO.isReg() || !MO.isDef())
continue;
Register R = MO.getReg();
if (!R.isVirtual())
continue;
if (!isPredicate(R))
continue;
for (const MachineOperand &U : MRI->use_operands(R))
if (U.getParent()->isPHI())
return false;
}
}
return true;
}
bool HexagonEarlyIfConversion::usesUndefVReg(const MachineInstr *MI) const {
for (const MachineOperand &MO : MI->operands()) {
if (!MO.isReg() || !MO.isUse())
continue;
Register R = MO.getReg();
if (!R.isVirtual())
continue;
const MachineInstr *DefI = MRI->getVRegDef(R);
assert(DefI && "Expecting a reaching def in MRI");
if (DefI->isImplicitDef())
return true;
}
return false;
}
bool HexagonEarlyIfConversion::isValid(const FlowPattern &FP) const {
if (hasEHLabel(FP.SplitB)) return false;
if (FP.TrueB && !isValidCandidate(FP.TrueB))
return false;
if (FP.FalseB && !isValidCandidate(FP.FalseB))
return false;
if (FP.JoinB) {
const MachineBasicBlock &B = *FP.JoinB;
for (auto &MI : B) {
if (!MI.isPHI())
break;
if (usesUndefVReg(&MI))
return false;
Register DefR = MI.getOperand(0).getReg();
if (isPredicate(DefR))
return false;
}
}
return true;
}
unsigned HexagonEarlyIfConversion::computePhiCost(const MachineBasicBlock *B,
const FlowPattern &FP) const {
if (B->pred_size() < 2)
return 0;
unsigned Cost = 0;
for (const MachineInstr &MI : *B) {
if (!MI.isPHI())
break;
SmallVector<unsigned,2> Inc;
for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
const MachineBasicBlock *BB = MI.getOperand(i+1).getMBB();
if (BB == FP.SplitB || BB == FP.TrueB || BB == FP.FalseB)
Inc.push_back(i);
}
assert(Inc.size() <= 2);
if (Inc.size() < 2)
continue;
const MachineOperand &RA = MI.getOperand(1);
const MachineOperand &RB = MI.getOperand(3);
assert(RA.isReg() && RB.isReg());
if (RA.getSubReg() != 0 || RB.getSubReg() != 0) {
Cost++;
continue;
}
const MachineInstr *Def1 = MRI->getVRegDef(RA.getReg());
const MachineInstr *Def3 = MRI->getVRegDef(RB.getReg());
if (!HII->isPredicable(*Def1) || !HII->isPredicable(*Def3))
Cost++;
}
return Cost;
}
unsigned HexagonEarlyIfConversion::countPredicateDefs(
const MachineBasicBlock *B) const {
unsigned PredDefs = 0;
for (auto &MI : *B) {
for (const MachineOperand &MO : MI.operands()) {
if (!MO.isReg() || !MO.isDef())
continue;
Register R = MO.getReg();
if (!R.isVirtual())
continue;
if (isPredicate(R))
PredDefs++;
}
}
return PredDefs;
}
bool HexagonEarlyIfConversion::isProfitable(const FlowPattern &FP) const {
BranchProbability JumpProb(1, 10);
BranchProbability Prob(9, 10);
if (MBPI && FP.TrueB && !FP.FalseB &&
(MBPI->getEdgeProbability(FP.SplitB, FP.TrueB) < JumpProb ||
MBPI->getEdgeProbability(FP.SplitB, FP.TrueB) > Prob))
return false;
if (MBPI && !FP.TrueB && FP.FalseB &&
(MBPI->getEdgeProbability(FP.SplitB, FP.FalseB) < JumpProb ||
MBPI->getEdgeProbability(FP.SplitB, FP.FalseB) > Prob))
return false;
if (FP.TrueB && FP.FalseB) {
if (MBPI) {
if (MBPI->getEdgeProbability(FP.SplitB, FP.TrueB) > Prob)
return false;
if (MBPI->getEdgeProbability(FP.SplitB, FP.FalseB) > Prob)
return false;
}
MachineBasicBlock *TSB = *FP.TrueB->succ_begin();
MachineBasicBlock *FSB = *FP.FalseB->succ_begin();
if (TSB != FSB)
return false;
if (TSB->pred_size() != 2)
return false;
}
auto TotalCount = [] (const MachineBasicBlock *B, unsigned &Spare) {
if (!B)
return 0u;
unsigned T = std::count_if(B->begin(), B->getFirstTerminator(),
[](const MachineInstr &MI) {
return !MI.isMetaInstruction();
});
if (T < HEXAGON_PACKET_SIZE)
Spare += HEXAGON_PACKET_SIZE-T;
return T;
};
unsigned Spare = 0;
unsigned TotalIn = TotalCount(FP.TrueB, Spare) + TotalCount(FP.FalseB, Spare);
LLVM_DEBUG(
dbgs() << "Total number of instructions to be predicated/speculated: "
<< TotalIn << ", spare room: " << Spare << "\n");
if (TotalIn >= SizeLimit+Spare)
return false;
unsigned TotalPh = 0;
unsigned PredDefs = countPredicateDefs(FP.SplitB);
if (FP.JoinB) {
TotalPh = computePhiCost(FP.JoinB, FP);
PredDefs += countPredicateDefs(FP.JoinB);
} else {
if (FP.TrueB && !FP.TrueB->succ_empty()) {
MachineBasicBlock *SB = *FP.TrueB->succ_begin();
TotalPh += computePhiCost(SB, FP);
PredDefs += countPredicateDefs(SB);
}
if (FP.FalseB && !FP.FalseB->succ_empty()) {
MachineBasicBlock *SB = *FP.FalseB->succ_begin();
TotalPh += computePhiCost(SB, FP);
PredDefs += countPredicateDefs(SB);
}
}
LLVM_DEBUG(dbgs() << "Total number of extra muxes from converted phis: "
<< TotalPh << "\n");
if (TotalIn+TotalPh >= SizeLimit+Spare)
return false;
LLVM_DEBUG(dbgs() << "Total number of predicate registers: " << PredDefs
<< "\n");
if (PredDefs > 4)
return false;
return true;
}
bool HexagonEarlyIfConversion::visitBlock(MachineBasicBlock *B,
MachineLoop *L) {
bool Changed = false;
MachineDomTreeNode *N = MDT->getNode(B);
using GTN = GraphTraits<MachineDomTreeNode *>;
using DTNodeVectType = SmallVector<MachineDomTreeNode *, 4>;
DTNodeVectType Cn(GTN::child_begin(N), GTN::child_end(N));
for (auto &I : Cn) {
MachineBasicBlock *SB = I->getBlock();
if (!Deleted.count(SB))
Changed |= visitBlock(SB, L);
}
if (MLI->getLoopFor(B) != L)
return Changed;
FlowPattern FP;
if (!matchFlowPattern(B, L, FP))
return Changed;
if (!isValid(FP)) {
LLVM_DEBUG(dbgs() << "Conversion is not valid\n");
return Changed;
}
if (!isProfitable(FP)) {
LLVM_DEBUG(dbgs() << "Conversion is not profitable\n");
return Changed;
}
convert(FP);
simplifyFlowGraph(FP);
return true;
}
bool HexagonEarlyIfConversion::visitLoop(MachineLoop *L) {
MachineBasicBlock *HB = L ? L->getHeader() : nullptr;
LLVM_DEBUG((L ? dbgs() << "Visiting loop H:" << PrintMB(HB)
: dbgs() << "Visiting function")
<< "\n");
bool Changed = false;
if (L) {
for (MachineLoop *I : *L)
Changed |= visitLoop(I);
}
MachineBasicBlock *EntryB = GraphTraits<MachineFunction*>::getEntryNode(MFN);
Changed |= visitBlock(L ? HB : EntryB, L);
return Changed;
}
bool HexagonEarlyIfConversion::isPredicableStore(const MachineInstr *MI)
const {
unsigned Opc = MI->getOpcode();
switch (Opc) {
case Hexagon::S2_storerb_io:
case Hexagon::S2_storerbnew_io:
case Hexagon::S2_storerh_io:
case Hexagon::S2_storerhnew_io:
case Hexagon::S2_storeri_io:
case Hexagon::S2_storerinew_io:
case Hexagon::S2_storerd_io:
case Hexagon::S4_storeirb_io:
case Hexagon::S4_storeirh_io:
case Hexagon::S4_storeiri_io:
return true;
}
return MI->mayStore() && HII->isPredicable(const_cast<MachineInstr&>(*MI));
}
bool HexagonEarlyIfConversion::isSafeToSpeculate(const MachineInstr *MI)
const {
if (MI->mayLoadOrStore())
return false;
if (MI->isCall() || MI->isBarrier() || MI->isBranch())
return false;
if (MI->hasUnmodeledSideEffects())
return false;
if (MI->getOpcode() == TargetOpcode::LIFETIME_END)
return false;
return true;
}
bool HexagonEarlyIfConversion::isPredicate(unsigned R) const {
const TargetRegisterClass *RC = MRI->getRegClass(R);
return RC == &Hexagon::PredRegsRegClass ||
RC == &Hexagon::HvxQRRegClass;
}
unsigned HexagonEarlyIfConversion::getCondStoreOpcode(unsigned Opc,
bool IfTrue) const {
return HII->getCondOpcode(Opc, !IfTrue);
}
void HexagonEarlyIfConversion::predicateInstr(MachineBasicBlock *ToB,
MachineBasicBlock::iterator At, MachineInstr *MI,
unsigned PredR, bool IfTrue) {
DebugLoc DL;
if (At != ToB->end())
DL = At->getDebugLoc();
else if (!ToB->empty())
DL = ToB->back().getDebugLoc();
unsigned Opc = MI->getOpcode();
if (isPredicableStore(MI)) {
unsigned COpc = getCondStoreOpcode(Opc, IfTrue);
assert(COpc);
MachineInstrBuilder MIB = BuildMI(*ToB, At, DL, HII->get(COpc));
MachineInstr::mop_iterator MOI = MI->operands_begin();
if (HII->isPostIncrement(*MI)) {
MIB.add(*MOI);
++MOI;
}
MIB.addReg(PredR);
for (const MachineOperand &MO : make_range(MOI, MI->operands_end()))
MIB.add(MO);
MIB.cloneMemRefs(*MI);
MI->eraseFromParent();
return;
}
if (Opc == Hexagon::J2_jump) {
MachineBasicBlock *TB = MI->getOperand(0).getMBB();
const MCInstrDesc &D = HII->get(IfTrue ? Hexagon::J2_jumpt
: Hexagon::J2_jumpf);
BuildMI(*ToB, At, DL, D)
.addReg(PredR)
.addMBB(TB);
MI->eraseFromParent();
return;
}
dbgs() << *MI;
llvm_unreachable("Unexpected instruction");
}
void HexagonEarlyIfConversion::predicateBlockNB(MachineBasicBlock *ToB,
MachineBasicBlock::iterator At, MachineBasicBlock *FromB,
unsigned PredR, bool IfTrue) {
LLVM_DEBUG(dbgs() << "Predicating block " << PrintMB(FromB) << "\n");
MachineBasicBlock::iterator End = FromB->getFirstTerminator();
MachineBasicBlock::iterator I, NextI;
for (I = FromB->begin(); I != End; I = NextI) {
assert(!I->isPHI());
NextI = std::next(I);
if (isSafeToSpeculate(&*I))
ToB->splice(At, FromB, I);
else
predicateInstr(ToB, At, &*I, PredR, IfTrue);
}
}
unsigned HexagonEarlyIfConversion::buildMux(MachineBasicBlock *B,
MachineBasicBlock::iterator At, const TargetRegisterClass *DRC,
unsigned PredR, unsigned TR, unsigned TSR, unsigned FR, unsigned FSR) {
unsigned Opc = 0;
switch (DRC->getID()) {
case Hexagon::IntRegsRegClassID:
case Hexagon::IntRegsLow8RegClassID:
Opc = Hexagon::C2_mux;
break;
case Hexagon::DoubleRegsRegClassID:
case Hexagon::GeneralDoubleLow8RegsRegClassID:
Opc = Hexagon::PS_pselect;
break;
case Hexagon::HvxVRRegClassID:
Opc = Hexagon::PS_vselect;
break;
case Hexagon::HvxWRRegClassID:
Opc = Hexagon::PS_wselect;
break;
default:
llvm_unreachable("unexpected register type");
}
const MCInstrDesc &D = HII->get(Opc);
DebugLoc DL = B->findBranchDebugLoc();
Register MuxR = MRI->createVirtualRegister(DRC);
BuildMI(*B, At, DL, D, MuxR)
.addReg(PredR)
.addReg(TR, 0, TSR)
.addReg(FR, 0, FSR);
return MuxR;
}
void HexagonEarlyIfConversion::updatePhiNodes(MachineBasicBlock *WhereB,
const FlowPattern &FP) {
auto NonPHI = WhereB->getFirstNonPHI();
for (auto I = WhereB->begin(); I != NonPHI; ++I) {
MachineInstr *PN = &*I;
unsigned TR = 0, TSR = 0, FR = 0, FSR = 0, SR = 0, SSR = 0;
for (int i = PN->getNumOperands()-2; i > 0; i -= 2) {
const MachineOperand &RO = PN->getOperand(i), &BO = PN->getOperand(i+1);
if (BO.getMBB() == FP.SplitB)
SR = RO.getReg(), SSR = RO.getSubReg();
else if (BO.getMBB() == FP.TrueB)
TR = RO.getReg(), TSR = RO.getSubReg();
else if (BO.getMBB() == FP.FalseB)
FR = RO.getReg(), FSR = RO.getSubReg();
else
continue;
PN->removeOperand(i+1);
PN->removeOperand(i);
}
if (TR == 0)
TR = SR, TSR = SSR;
else if (FR == 0)
FR = SR, FSR = SSR;
assert(TR || FR);
unsigned MuxR = 0, MuxSR = 0;
if (TR && FR) {
Register DR = PN->getOperand(0).getReg();
const TargetRegisterClass *RC = MRI->getRegClass(DR);
MuxR = buildMux(FP.SplitB, FP.SplitB->getFirstTerminator(), RC,
FP.PredR, TR, TSR, FR, FSR);
} else if (TR) {
MuxR = TR;
MuxSR = TSR;
} else {
MuxR = FR;
MuxSR = FSR;
}
PN->addOperand(MachineOperand::CreateReg(MuxR, false, false, false, false,
false, false, MuxSR));
PN->addOperand(MachineOperand::CreateMBB(FP.SplitB));
}
}
void HexagonEarlyIfConversion::convert(const FlowPattern &FP) {
MachineBasicBlock *TSB = nullptr, *FSB = nullptr;
MachineBasicBlock::iterator OldTI = FP.SplitB->getFirstTerminator();
assert(OldTI != FP.SplitB->end());
DebugLoc DL = OldTI->getDebugLoc();
if (FP.TrueB) {
TSB = *FP.TrueB->succ_begin();
predicateBlockNB(FP.SplitB, OldTI, FP.TrueB, FP.PredR, true);
}
if (FP.FalseB) {
FSB = *FP.FalseB->succ_begin();
MachineBasicBlock::iterator At = FP.SplitB->getFirstTerminator();
predicateBlockNB(FP.SplitB, At, FP.FalseB, FP.PredR, false);
}
MachineBasicBlock *SSB = nullptr;
FP.SplitB->erase(OldTI, FP.SplitB->end());
while (!FP.SplitB->succ_empty()) {
MachineBasicBlock *T = *FP.SplitB->succ_begin();
if (T != FP.TrueB && T != FP.FalseB) {
assert(!SSB);
SSB = T;
}
FP.SplitB->removeSuccessor(FP.SplitB->succ_begin());
}
if (FP.JoinB) {
assert(!SSB || SSB == FP.JoinB);
BuildMI(*FP.SplitB, FP.SplitB->end(), DL, HII->get(Hexagon::J2_jump))
.addMBB(FP.JoinB);
FP.SplitB->addSuccessor(FP.JoinB);
} else {
bool HasBranch = false;
if (TSB) {
BuildMI(*FP.SplitB, FP.SplitB->end(), DL, HII->get(Hexagon::J2_jumpt))
.addReg(FP.PredR)
.addMBB(TSB);
FP.SplitB->addSuccessor(TSB);
HasBranch = true;
}
if (FSB) {
const MCInstrDesc &D = HasBranch ? HII->get(Hexagon::J2_jump)
: HII->get(Hexagon::J2_jumpf);
MachineInstrBuilder MIB = BuildMI(*FP.SplitB, FP.SplitB->end(), DL, D);
if (!HasBranch)
MIB.addReg(FP.PredR);
MIB.addMBB(FSB);
FP.SplitB->addSuccessor(FSB);
}
if (SSB) {
BuildMI(*FP.SplitB, FP.SplitB->end(), DL, HII->get(Hexagon::J2_jump))
.addMBB(SSB);
FP.SplitB->addSuccessor(SSB);
}
}
if (FP.JoinB) {
updatePhiNodes(FP.JoinB, FP);
} else {
if (TSB)
updatePhiNodes(TSB, FP);
if (FSB)
updatePhiNodes(FSB, FP);
}
}
void HexagonEarlyIfConversion::removeBlock(MachineBasicBlock *B) {
LLVM_DEBUG(dbgs() << "Removing block " << PrintMB(B) << "\n");
MachineDomTreeNode *N = MDT->getNode(B);
MachineDomTreeNode *IDN = N->getIDom();
if (IDN) {
MachineBasicBlock *IDB = IDN->getBlock();
using GTN = GraphTraits<MachineDomTreeNode *>;
using DTNodeVectType = SmallVector<MachineDomTreeNode *, 4>;
DTNodeVectType Cn(GTN::child_begin(N), GTN::child_end(N));
for (auto &I : Cn) {
MachineBasicBlock *SB = I->getBlock();
MDT->changeImmediateDominator(SB, IDB);
}
}
while (!B->succ_empty())
B->removeSuccessor(B->succ_begin());
for (MachineBasicBlock *Pred : B->predecessors())
Pred->removeSuccessor(B, true);
Deleted.insert(B);
MDT->eraseNode(B);
MFN->erase(B->getIterator());
}
void HexagonEarlyIfConversion::eliminatePhis(MachineBasicBlock *B) {
LLVM_DEBUG(dbgs() << "Removing phi nodes from block " << PrintMB(B) << "\n");
MachineBasicBlock::iterator I, NextI, NonPHI = B->getFirstNonPHI();
for (I = B->begin(); I != NonPHI; I = NextI) {
NextI = std::next(I);
MachineInstr *PN = &*I;
assert(PN->getNumOperands() == 3 && "Invalid phi node");
MachineOperand &UO = PN->getOperand(1);
Register UseR = UO.getReg(), UseSR = UO.getSubReg();
Register DefR = PN->getOperand(0).getReg();
unsigned NewR = UseR;
if (UseSR) {
const DebugLoc &DL = PN->getDebugLoc();
const TargetRegisterClass *RC = MRI->getRegClass(DefR);
NewR = MRI->createVirtualRegister(RC);
NonPHI = BuildMI(*B, NonPHI, DL, HII->get(TargetOpcode::COPY), NewR)
.addReg(UseR, 0, UseSR);
}
MRI->replaceRegWith(DefR, NewR);
B->erase(I);
}
}
void HexagonEarlyIfConversion::mergeBlocks(MachineBasicBlock *PredB,
MachineBasicBlock *SuccB) {
LLVM_DEBUG(dbgs() << "Merging blocks " << PrintMB(PredB) << " and "
<< PrintMB(SuccB) << "\n");
bool TermOk = hasUncondBranch(SuccB);
eliminatePhis(SuccB);
HII->removeBranch(*PredB);
PredB->removeSuccessor(SuccB);
PredB->splice(PredB->end(), SuccB, SuccB->begin(), SuccB->end());
PredB->transferSuccessorsAndUpdatePHIs(SuccB);
MachineBasicBlock *OldLayoutSuccessor = SuccB->getNextNode();
removeBlock(SuccB);
if (!TermOk)
PredB->updateTerminator(OldLayoutSuccessor);
}
void HexagonEarlyIfConversion::simplifyFlowGraph(const FlowPattern &FP) {
MachineBasicBlock *OldLayoutSuccessor = FP.SplitB->getNextNode();
if (FP.TrueB)
removeBlock(FP.TrueB);
if (FP.FalseB)
removeBlock(FP.FalseB);
FP.SplitB->updateTerminator(OldLayoutSuccessor);
if (FP.SplitB->succ_size() != 1)
return;
MachineBasicBlock *SB = *FP.SplitB->succ_begin();
if (SB->pred_size() != 1)
return;
if (!hasEHLabel(SB) || hasUncondBranch(SB))
mergeBlocks(FP.SplitB, SB);
}
bool HexagonEarlyIfConversion::runOnMachineFunction(MachineFunction &MF) {
if (skipFunction(MF.getFunction()))
return false;
auto &ST = MF.getSubtarget<HexagonSubtarget>();
HII = ST.getInstrInfo();
TRI = ST.getRegisterInfo();
MFN = &MF;
MRI = &MF.getRegInfo();
MDT = &getAnalysis<MachineDominatorTree>();
MLI = &getAnalysis<MachineLoopInfo>();
MBPI = EnableHexagonBP ? &getAnalysis<MachineBranchProbabilityInfo>() :
nullptr;
Deleted.clear();
bool Changed = false;
for (MachineLoop *L : *MLI)
Changed |= visitLoop(L);
Changed |= visitLoop(nullptr);
return Changed;
}
FunctionPass *llvm::createHexagonEarlyIfConversion() {
return new HexagonEarlyIfConversion();
}