#include "PPC.h"
#include "PPCInstrInfo.h"
#include "PPCSubtarget.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/LivePhysRegs.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
#define DEBUG_TYPE "ppc-expand-isel"
STATISTIC(NumExpanded, "Number of ISEL instructions expanded");
STATISTIC(NumRemoved, "Number of ISEL instructions removed");
STATISTIC(NumFolded, "Number of ISEL instructions folded");
static cl::opt<bool>
GenerateISEL("ppc-gen-isel",
cl::desc("Enable generating the ISEL instruction."),
cl::init(true), cl::Hidden);
namespace {
class PPCExpandISEL : public MachineFunctionPass {
DebugLoc dl;
MachineFunction *MF;
const TargetInstrInfo *TII;
bool IsTrueBlockRequired;
bool IsFalseBlockRequired;
MachineBasicBlock *TrueBlock;
MachineBasicBlock *FalseBlock;
MachineBasicBlock *NewSuccessor;
MachineBasicBlock::iterator TrueBlockI;
MachineBasicBlock::iterator FalseBlockI;
typedef SmallVector<MachineInstr *, 4> BlockISELList;
typedef SmallDenseMap<int, BlockISELList> ISELInstructionList;
ISELInstructionList ISELInstructions;
void initialize(MachineFunction &MFParam);
void handleSpecialCases(BlockISELList &BIL, MachineBasicBlock *MBB);
void reorganizeBlockLayout(BlockISELList &BIL, MachineBasicBlock *MBB);
void populateBlocks(BlockISELList &BIL);
void expandMergeableISELs(BlockISELList &BIL);
void expandAndMergeISELs();
bool canMerge(MachineInstr *PrevPushedMI, MachineInstr *MI);
static bool isISEL(const MachineInstr &MI) {
return (MI.getOpcode() == PPC::ISEL || MI.getOpcode() == PPC::ISEL8);
}
static bool isISEL8(const MachineInstr &MI) {
return (MI.getOpcode() == PPC::ISEL8);
}
bool useSameRegister(const MachineOperand &Op1, const MachineOperand &Op2) {
return (Op1.getReg() == Op2.getReg());
}
bool collectISELInstructions();
public:
static char ID;
PPCExpandISEL() : MachineFunctionPass(ID) {
initializePPCExpandISELPass(*PassRegistry::getPassRegistry());
}
static bool isExpandISELEnabled(const MachineFunction &MF);
#ifndef NDEBUG
void DumpISELInstructions() const;
#endif
bool runOnMachineFunction(MachineFunction &MF) override {
LLVM_DEBUG(dbgs() << "Function: "; MF.dump(); dbgs() << "\n");
initialize(MF);
if (!collectISELInstructions()) {
LLVM_DEBUG(dbgs() << "No ISEL instructions in this function\n");
return false;
}
#ifndef NDEBUG
DumpISELInstructions();
#endif
expandAndMergeISELs();
return true;
}
};
}
void PPCExpandISEL::initialize(MachineFunction &MFParam) {
MF = &MFParam;
TII = MF->getSubtarget().getInstrInfo();
ISELInstructions.clear();
}
bool PPCExpandISEL::isExpandISELEnabled(const MachineFunction &MF) {
return !GenerateISEL || !MF.getSubtarget<PPCSubtarget>().hasISEL();
}
bool PPCExpandISEL::collectISELInstructions() {
for (MachineBasicBlock &MBB : *MF) {
BlockISELList thisBlockISELs;
for (MachineInstr &MI : MBB)
if (isISEL(MI))
thisBlockISELs.push_back(&MI);
if (!thisBlockISELs.empty())
ISELInstructions.insert(std::make_pair(MBB.getNumber(), thisBlockISELs));
}
return !ISELInstructions.empty();
}
#ifndef NDEBUG
void PPCExpandISEL::DumpISELInstructions() const {
for (const auto &I : ISELInstructions) {
LLVM_DEBUG(dbgs() << printMBBReference(*MF->getBlockNumbered(I.first))
<< ":\n");
for (const auto &VI : I.second)
LLVM_DEBUG(dbgs() << " "; VI->print(dbgs()));
}
}
#endif
bool PPCExpandISEL::canMerge(MachineInstr *PrevPushedMI, MachineInstr *MI) {
if (!useSameRegister(PrevPushedMI->getOperand(3), MI->getOperand(3)))
return false;
MachineBasicBlock::iterator PrevPushedMBBI = *PrevPushedMI;
MachineBasicBlock::iterator MBBI = *MI;
return (std::prev(MBBI) == PrevPushedMBBI); }
void PPCExpandISEL::expandAndMergeISELs() {
bool ExpandISELEnabled = isExpandISELEnabled(*MF);
for (auto &BlockList : ISELInstructions) {
LLVM_DEBUG(
dbgs() << "Expanding ISEL instructions in "
<< printMBBReference(*MF->getBlockNumbered(BlockList.first))
<< "\n");
BlockISELList &CurrentISELList = BlockList.second;
auto I = CurrentISELList.begin();
auto E = CurrentISELList.end();
while (I != E) {
assert(isISEL(**I) && "Expecting an ISEL instruction");
MachineOperand &Dest = (*I)->getOperand(0);
MachineOperand &TrueValue = (*I)->getOperand(1);
MachineOperand &FalseValue = (*I)->getOperand(2);
if (useSameRegister(Dest, TrueValue) &&
useSameRegister(Dest, FalseValue)) {
LLVM_DEBUG(dbgs() << "Remove redundant ISEL instruction: " << **I
<< "\n");
NumRemoved++;
(*I)->eraseFromParent();
I++;
} else if (useSameRegister(TrueValue, FalseValue)) {
MachineBasicBlock *MBB = (*I)->getParent();
LLVM_DEBUG(
dbgs() << "Fold the ISEL instruction to an unconditional copy:\n");
LLVM_DEBUG(dbgs() << "ISEL: " << **I << "\n");
NumFolded++;
BuildMI(*MBB, (*I), dl, TII->get(isISEL8(**I) ? PPC::OR8 : PPC::OR))
.add(Dest)
.add(TrueValue)
.add(FalseValue);
(*I)->eraseFromParent();
I++;
} else if (ExpandISELEnabled) { LLVM_DEBUG(dbgs() << "Expand ISEL instructions:\n");
LLVM_DEBUG(dbgs() << "ISEL: " << **I << "\n");
BlockISELList SubISELList;
SubISELList.push_back(*I++);
while (I != E && canMerge(SubISELList.back(), *I)) {
LLVM_DEBUG(dbgs() << "ISEL: " << **I << "\n");
SubISELList.push_back(*I++);
}
expandMergeableISELs(SubISELList);
} else { I++; }
} } }
void PPCExpandISEL::handleSpecialCases(BlockISELList &BIL,
MachineBasicBlock *MBB) {
IsTrueBlockRequired = false;
IsFalseBlockRequired = false;
auto MI = BIL.begin();
while (MI != BIL.end()) {
assert(isISEL(**MI) && "Expecting an ISEL instruction");
LLVM_DEBUG(dbgs() << "ISEL: " << **MI << "\n");
MachineOperand &Dest = (*MI)->getOperand(0);
MachineOperand &TrueValue = (*MI)->getOperand(1);
MachineOperand &FalseValue = (*MI)->getOperand(2);
bool IsADDIInstRequired = !useSameRegister(Dest, TrueValue);
bool IsORIInstRequired = !useSameRegister(Dest, FalseValue);
if (!IsADDIInstRequired && !IsORIInstRequired) {
LLVM_DEBUG(dbgs() << "Remove redundant ISEL instruction.");
NumRemoved++;
(*MI)->eraseFromParent();
MI = BIL.erase(MI);
continue;
}
if (useSameRegister(TrueValue, FalseValue) && (BIL.size() == 1)) {
LLVM_DEBUG(
dbgs() << "Fold the ISEL instruction to an unconditional copy.");
NumFolded++;
BuildMI(*MBB, (*MI), dl, TII->get(isISEL8(**MI) ? PPC::OR8 : PPC::OR))
.add(Dest)
.add(TrueValue)
.add(FalseValue);
(*MI)->eraseFromParent();
MI = BIL.erase(MI);
continue;
}
IsTrueBlockRequired |= IsADDIInstRequired;
IsFalseBlockRequired |= IsORIInstRequired;
MI++;
}
}
void PPCExpandISEL::reorganizeBlockLayout(BlockISELList &BIL,
MachineBasicBlock *MBB) {
if (BIL.empty())
return;
assert((IsTrueBlockRequired || IsFalseBlockRequired) &&
"Should have been handled by special cases earlier!");
MachineBasicBlock *Successor = nullptr;
const BasicBlock *LLVM_BB = MBB->getBasicBlock();
MachineBasicBlock::iterator MBBI = (*BIL.back());
NewSuccessor = (MBBI != MBB->getLastNonDebugInstr() || !MBB->canFallThrough())
? MF->CreateMachineBasicBlock(LLVM_BB)
: nullptr;
MachineFunction::iterator It = MBB->getIterator();
++It;
if (!NewSuccessor) {
for (auto &Succ : MBB->successors()) {
if (MBB->isLayoutSuccessor(Succ)) {
Successor = Succ;
break;
}
}
} else
Successor = NewSuccessor;
if (IsFalseBlockRequired) {
FalseBlock = MF->CreateMachineBasicBlock(LLVM_BB);
MF->insert(It, FalseBlock);
}
if (IsTrueBlockRequired) {
TrueBlock = MF->CreateMachineBasicBlock(LLVM_BB);
MF->insert(It, TrueBlock);
}
if (NewSuccessor) {
MF->insert(It, NewSuccessor);
NewSuccessor->splice(NewSuccessor->end(), MBB,
std::next(MachineBasicBlock::iterator(BIL.back())),
MBB->end());
NewSuccessor->transferSuccessorsAndUpdatePHIs(MBB);
LivePhysRegs LPR;
computeAndAddLiveIns(LPR, *NewSuccessor);
} else {
MBB->removeSuccessor(Successor);
}
MBB->addSuccessor(IsTrueBlockRequired ? TrueBlock : Successor);
MBB->addSuccessor(IsFalseBlockRequired ? FalseBlock : Successor);
if (IsTrueBlockRequired) {
TrueBlockI = TrueBlock->begin();
TrueBlock->addSuccessor(Successor);
}
if (IsFalseBlockRequired) {
FalseBlockI = FalseBlock->begin();
FalseBlock->addSuccessor(Successor);
}
BuildMI(*MBB, BIL.back(), dl, TII->get(PPC::BC))
.add(BIL.back()->getOperand(3))
.addMBB(IsTrueBlockRequired ? TrueBlock : Successor);
BuildMI(*(IsFalseBlockRequired ? FalseBlock : MBB),
(IsFalseBlockRequired ? FalseBlockI : BIL.back()), dl,
TII->get(PPC::B))
.addMBB(Successor);
if (IsFalseBlockRequired)
FalseBlockI = FalseBlock->begin(); }
void PPCExpandISEL::populateBlocks(BlockISELList &BIL) {
for (auto &MI : BIL) {
assert(isISEL(*MI) && "Expecting an ISEL instruction");
MachineOperand &Dest = MI->getOperand(0); MachineOperand &TrueValue = MI->getOperand(1); MachineOperand &FalseValue = MI->getOperand(2);
LLVM_DEBUG(dbgs() << "Dest: " << Dest << "\n");
LLVM_DEBUG(dbgs() << "TrueValue: " << TrueValue << "\n");
LLVM_DEBUG(dbgs() << "FalseValue: " << FalseValue << "\n");
LLVM_DEBUG(dbgs() << "ConditionRegister: " << MI->getOperand(3) << "\n");
bool IsADDIInstRequired = !useSameRegister(Dest, TrueValue);
bool IsORIInstRequired = !useSameRegister(Dest, FalseValue);
if (IsADDIInstRequired)
BuildMI(*TrueBlock, TrueBlockI, dl,
TII->get(isISEL8(*MI) ? PPC::ADDI8 : PPC::ADDI))
.add(Dest)
.add(TrueValue)
.add(MachineOperand::CreateImm(0));
if (IsORIInstRequired)
BuildMI(*FalseBlock, FalseBlockI, dl,
TII->get(isISEL8(*MI) ? PPC::ORI8 : PPC::ORI))
.add(Dest)
.add(FalseValue)
.add(MachineOperand::CreateImm(0));
MI->eraseFromParent();
NumExpanded++;
}
if (IsTrueBlockRequired) {
LivePhysRegs LPR;
computeAndAddLiveIns(LPR, *TrueBlock);
}
if (IsFalseBlockRequired) {
LivePhysRegs LPR;
computeAndAddLiveIns(LPR, *FalseBlock);
}
}
void PPCExpandISEL::expandMergeableISELs(BlockISELList &BIL) {
MachineBasicBlock *MBB = BIL.back()->getParent();
handleSpecialCases(BIL, MBB);
reorganizeBlockLayout(BIL, MBB);
populateBlocks(BIL);
}
INITIALIZE_PASS(PPCExpandISEL, DEBUG_TYPE, "PowerPC Expand ISEL Generation",
false, false)
char PPCExpandISEL::ID = 0;
FunctionPass *llvm::createPPCExpandISELPass() { return new PPCExpandISEL(); }