I'm trying to write an llvm pass and want to replace all the instructions having addition BinaryOperator by the same instructions but having multiplication in place of addition. I am running the pass on the below test.cpp code:
int add( int x, int y) {
x = x + y;
// x = x + x;
y = x + y;
return x;
}
I'm able to replace all the additions by the multiplication in the above code, but when I uncomment the line x = x + x
It runs and goes in an infinite loop, and I figured out the infinite loop is for (auto &U : bop->uses())
in the below code of llvm pass.
So, can someone please help me to modify the below code so that it will replace the addition by multiplication in the instruction for the line x = x + x
as well.
bool runOnFunction(llvm::Function &Func) {
std::string func_name = Func.getName();
for(llvm::BasicBlock& b : Func ) {
// llvm::errs() << "\nBasic block '" << b.getName() << "' has " << b.size() << " instructions.\n";
for (llvm::BasicBlock::iterator DI = b.begin(); DI != b.end(); ) {
llvm::Instruction *I = &*DI++;
// llvm::errs() << "Instruction : " << *I << "\n";
if(auto bop = llvm::dyn_cast<llvm::BinaryOperator>(I) ) {
llvm::IRBuilder<> Builder(bop);
auto op0 = bop->getOperand( 0 );
auto op1 = bop->getOperand( 1 );
unsigned op = bop->getOpcode();
if( op == llvm::Instruction::Add ) {
llvm::Value *mul = Builder.CreateMul(op0, op1);
for (auto &U : bop->uses()) {
// llvm::errs() << "Instruction inside uses: " << *I << "\n";
llvm::User *user = U.getUser(); // A User is anything with operands.
user->setOperand(U.getOperandNo(), mul);
}
I->eraseFromParent();
}
}
}
}
return false;
}
Aucun commentaire:
Enregistrer un commentaire