#include "LanaiAluCode.h"
#include "LanaiTargetMachine.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/RegisterScavenging.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/Support/CommandLine.h"
using namespace llvm;
#define GET_INSTRMAP_INFO
#include "LanaiGenInstrInfo.inc"
#define DEBUG_TYPE "lanai-mem-alu-combiner"
STATISTIC(NumLdStAluCombined, "Number of memory and ALU instructions combined");
static llvm::cl::opt<bool> DisableMemAluCombiner(
"disable-lanai-mem-alu-combiner", llvm::cl::init(false),
llvm::cl::desc("Do not combine ALU and memory operators"),
llvm::cl::Hidden);
namespace llvm {
void initializeLanaiMemAluCombinerPass(PassRegistry &);
}
namespace {
typedef MachineBasicBlock::iterator MbbIterator;
typedef MachineFunction::iterator MfIterator;
class LanaiMemAluCombiner : public MachineFunctionPass {
public:
static char ID;
explicit LanaiMemAluCombiner() : MachineFunctionPass(ID) {
initializeLanaiMemAluCombinerPass(*PassRegistry::getPassRegistry());
}
StringRef getPassName() const override {
return "Lanai load / store optimization pass";
}
bool runOnMachineFunction(MachineFunction &F) override;
MachineFunctionProperties getRequiredProperties() const override {
return MachineFunctionProperties().set(
MachineFunctionProperties::Property::NoVRegs);
}
private:
MbbIterator findClosestSuitableAluInstr(MachineBasicBlock *BB,
const MbbIterator &MemInstr,
bool Decrement);
void insertMergedInstruction(MachineBasicBlock *BB,
const MbbIterator &MemInstr,
const MbbIterator &AluInstr, bool Before);
bool combineMemAluInBasicBlock(MachineBasicBlock *BB);
const TargetInstrInfo *TII;
};
}
char LanaiMemAluCombiner::ID = 0;
INITIALIZE_PASS(LanaiMemAluCombiner, DEBUG_TYPE,
"Lanai memory ALU combiner pass", false, false)
namespace {
bool isSpls(uint16_t Opcode) { return Lanai::splsIdempotent(Opcode) == Opcode; }
unsigned mergedOpcode(unsigned OldOpcode, bool ImmediateOffset) {
switch (OldOpcode) {
case Lanai::LDW_RI:
case Lanai::LDW_RR:
if (ImmediateOffset)
return Lanai::LDW_RI;
return Lanai::LDW_RR;
case Lanai::LDHs_RI:
case Lanai::LDHs_RR:
if (ImmediateOffset)
return Lanai::LDHs_RI;
return Lanai::LDHs_RR;
case Lanai::LDHz_RI:
case Lanai::LDHz_RR:
if (ImmediateOffset)
return Lanai::LDHz_RI;
return Lanai::LDHz_RR;
case Lanai::LDBs_RI:
case Lanai::LDBs_RR:
if (ImmediateOffset)
return Lanai::LDBs_RI;
return Lanai::LDBs_RR;
case Lanai::LDBz_RI:
case Lanai::LDBz_RR:
if (ImmediateOffset)
return Lanai::LDBz_RI;
return Lanai::LDBz_RR;
case Lanai::SW_RI:
case Lanai::SW_RR:
if (ImmediateOffset)
return Lanai::SW_RI;
return Lanai::SW_RR;
case Lanai::STB_RI:
case Lanai::STB_RR:
if (ImmediateOffset)
return Lanai::STB_RI;
return Lanai::STB_RR;
case Lanai::STH_RI:
case Lanai::STH_RR:
if (ImmediateOffset)
return Lanai::STH_RI;
return Lanai::STH_RR;
default:
return 0;
}
}
bool isNonVolatileMemoryOp(const MachineInstr &MI) {
if (!MI.hasOneMemOperand())
return false;
if (mergedOpcode(MI.getOpcode(), false) == 0)
return false;
const MachineMemOperand *MemOperand = *MI.memoperands_begin();
if (MemOperand->isVolatile() || MemOperand->isAtomic())
return false;
return true;
}
bool isSameOperand(const MachineOperand &Op1, const MachineOperand &Op2) {
if (Op1.getType() != Op2.getType())
return false;
switch (Op1.getType()) {
case MachineOperand::MO_Register:
return Op1.getReg() == Op2.getReg();
case MachineOperand::MO_Immediate:
return Op1.getImm() == Op2.getImm();
default:
return false;
}
}
bool isZeroOperand(const MachineOperand &Op) {
return ((Op.isReg() && Op.getReg() == Lanai::R0) ||
(Op.isImm() && Op.getImm() == 0));
}
bool InstrUsesReg(const MbbIterator &Instr, const MachineOperand *Reg) {
for (MachineInstr::const_mop_iterator Mop = Instr->operands_begin();
Mop != Instr->operands_end(); ++Mop) {
if (isSameOperand(*Mop, *Reg))
return true;
}
return false;
}
LPAC::AluCode mergedAluCode(unsigned AluOpcode) {
switch (AluOpcode) {
case Lanai::ADD_I_LO:
case Lanai::ADD_R:
return LPAC::ADD;
case Lanai::SUB_I_LO:
case Lanai::SUB_R:
return LPAC::SUB;
case Lanai::AND_I_LO:
case Lanai::AND_R:
return LPAC::AND;
case Lanai::OR_I_LO:
case Lanai::OR_R:
return LPAC::OR;
case Lanai::XOR_I_LO:
case Lanai::XOR_R:
return LPAC::XOR;
case Lanai::SHL_R:
return LPAC::SHL;
case Lanai::SRL_R:
return LPAC::SRL;
case Lanai::SRA_R:
return LPAC::SRA;
case Lanai::SA_I:
case Lanai::SL_I:
default:
return LPAC::UNKNOWN;
}
}
void LanaiMemAluCombiner::insertMergedInstruction(MachineBasicBlock *BB,
const MbbIterator &MemInstr,
const MbbIterator &AluInstr,
bool Before) {
MachineOperand Dest = MemInstr->getOperand(0);
MachineOperand Base = MemInstr->getOperand(1);
MachineOperand MemOffset = MemInstr->getOperand(2);
MachineOperand AluOffset = AluInstr->getOperand(2);
assert((AluOffset.isReg() || AluOffset.isImm()) &&
"Unsupported operand type in merge");
LPAC::AluCode AluOpcode = mergedAluCode(AluInstr->getOpcode());
unsigned NewOpc = mergedOpcode(MemInstr->getOpcode(), AluOffset.isImm());
assert(AluOpcode != LPAC::UNKNOWN && "Unknown ALU code in merging");
assert(NewOpc != 0 && "Unknown merged node opcode");
MachineInstrBuilder InstrBuilder =
BuildMI(*BB, MemInstr, MemInstr->getDebugLoc(), TII->get(NewOpc));
InstrBuilder.addReg(Dest.getReg(), getDefRegState(true));
InstrBuilder.addReg(Base.getReg(), getKillRegState(true));
if (AluOffset.isReg())
InstrBuilder.addReg(AluOffset.getReg());
else if (AluOffset.isImm())
InstrBuilder.addImm(AluOffset.getImm());
else
llvm_unreachable("Unsupported ld/st ALU merge.");
if (Before || !isZeroOperand(MemOffset))
InstrBuilder.addImm(LPAC::makePreOp(AluOpcode));
else
InstrBuilder.addImm(LPAC::makePostOp(AluOpcode));
InstrBuilder.setMemRefs(MemInstr->memoperands());
}
bool isSuitableAluInstr(bool IsSpls, const MbbIterator &AluIter,
const MachineOperand &Base,
const MachineOperand &Offset) {
if (AluIter->getNumOperands() != 3)
return false;
MachineOperand &Dest = AluIter->getOperand(0);
MachineOperand &Op1 = AluIter->getOperand(1);
MachineOperand &Op2 = AluIter->getOperand(2);
if (!isSameOperand(Dest, Base) || !isSameOperand(Dest, Op1))
return false;
if (Op2.isImm()) {
if (AluIter->getOpcode() != Lanai::ADD_I_LO)
return false;
if (Offset.isReg() && Offset.getReg() == Lanai::R0)
return true;
if (Offset.isImm() &&
((Offset.getImm() == 0 &&
((IsSpls && isInt<10>(Op2.getImm())) ||
(!IsSpls && isInt<16>(Op2.getImm())))) ||
Offset.getImm() == Op2.getImm()))
return true;
} else if (Op2.isReg()) {
if (Offset.isReg() && Op2.getReg() == Offset.getReg())
return true;
} else
return false;
return false;
}
MbbIterator LanaiMemAluCombiner::findClosestSuitableAluInstr(
MachineBasicBlock *BB, const MbbIterator &MemInstr, const bool Decrement) {
MachineOperand *Base = &MemInstr->getOperand(1);
MachineOperand *Offset = &MemInstr->getOperand(2);
bool IsSpls = isSpls(MemInstr->getOpcode());
MbbIterator First = MemInstr;
MbbIterator Last = Decrement ? BB->begin() : BB->end();
while (First != Last) {
Decrement ? --First : ++First;
if (First == Last)
break;
if (First->isDebugInstr())
continue;
if (isSuitableAluInstr(IsSpls, First, *Base, *Offset)) {
return First;
}
if (First != Last) {
if (InstrUsesReg(First, Base))
break;
if (Offset->isReg() && InstrUsesReg(First, Offset))
break;
}
}
return MemInstr;
}
bool LanaiMemAluCombiner::combineMemAluInBasicBlock(MachineBasicBlock *BB) {
bool Modified = false;
MbbIterator MBBIter = BB->begin(), End = BB->end();
while (MBBIter != End) {
bool IsMemOp = isNonVolatileMemoryOp(*MBBIter);
if (IsMemOp) {
MachineOperand AluOperand = MBBIter->getOperand(3);
unsigned int DestReg = MBBIter->getOperand(0).getReg(),
BaseReg = MBBIter->getOperand(1).getReg();
assert(AluOperand.isImm() && "Unexpected memory operator type");
LPAC::AluCode AluOpcode = static_cast<LPAC::AluCode>(AluOperand.getImm());
if (!LPAC::modifiesOp(AluOpcode) && DestReg != BaseReg) {
for (int Inc = 0; Inc <= 1; ++Inc) {
MbbIterator AluIter =
findClosestSuitableAluInstr(BB, MBBIter, Inc == 0);
if (AluIter != MBBIter) {
insertMergedInstruction(BB, MBBIter, AluIter, Inc == 0);
++NumLdStAluCombined;
Modified = true;
BB->erase(AluIter);
BB->erase(MBBIter++);
break;
}
}
}
}
if (MBBIter == End)
break;
++MBBIter;
}
return Modified;
}
bool LanaiMemAluCombiner::runOnMachineFunction(MachineFunction &MF) {
if (DisableMemAluCombiner)
return false;
TII = MF.getSubtarget<LanaiSubtarget>().getInstrInfo();
bool Modified = false;
for (MachineBasicBlock &MBB : MF)
Modified |= combineMemAluInBasicBlock(&MBB);
return Modified;
}
}
FunctionPass *llvm::createLanaiMemAluCombinerPass() {
return new LanaiMemAluCombiner();
}