Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dummy RISCV LLVM Pass to Insert Machine Instruction before each Call instruction
#include "RISCV.h"
#include "RISCVInstrInfo.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
using namespace llvm;
#define RISCV_MACHINEINSTR_PRINTER_PASS_NAME \
"Dummy RISCV machineinstr printer pass"
namespace {
class RISCVMachineInstrPrinter : public MachineFunctionPass {
public:
static char ID;
RISCVMachineInstrPrinter() : MachineFunctionPass(ID) {
initializeRISCVMachineInstrPrinterPass(*PassRegistry::getPassRegistry());
}
bool runOnMachineFunction(MachineFunction &MF) override;
StringRef getPassName() const override {
return RISCV_MACHINEINSTR_PRINTER_PASS_NAME;
}
};
char RISCVMachineInstrPrinter::ID = 0;
bool RISCVMachineInstrPrinter::runOnMachineFunction(MachineFunction &MF) {
for (auto &MBB : MF) {
/*
outs() << "Contents of MachineBasicBlock:\n";
outs() << MBB << "\n";
const BasicBlock *BB = MBB.getBasicBlock();
outs() << "Contents of BasicBlock corresponding to MachineBasicBlock:\n";
outs() << BB << "\n";
*/
for (auto &MI : MBB) {
MachineInstr *currInstrPtr = &MI;
// is this a load instruction?
// if (CallInst *CI = isa<CallInst>(currInstrPtr))
if (MI.mayStore()) {
outs() << "Found Store\n";
}
if (MI.isCall()) {
outs() << "Found Call\n";
outs() << MI.getOpcode() << "\n";
const TargetInstrInfo *XII = MF.getSubtarget().getInstrInfo(); // target instruction info
DebugLoc DL;
MachineBasicBlock::iterator MBBI = BuildMI(MBB, MI ,DL, XII->get(RISCV::SW), RISCV::X1)
.addReg(RISCV::X31)
.addImm(0);
return true;
}
}
}
return false;
}
} // end of anonymous namespace
INITIALIZE_PASS(RISCVMachineInstrPrinter, "RISCV-machineinstr-printer",
RISCV_MACHINEINSTR_PRINTER_PASS_NAME,
true, // is CFG only?
true // is analysis?
)
namespace llvm {
FunctionPass *createRISCVMachineInstrPrinterPass() {
return new RISCVMachineInstrPrinter();
}
} // namespace llvm
@ibndias
Copy link
Author

ibndias commented Aug 31, 2020

Ported to RISCV from X86 MachineFunctionPass tutorial tutorial by @RadhikaG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment