LLVM "replaceinstwithvalue"不起作用

llvm "replaceinstwithvalue" does not work

本文关键字:不起作用 replaceinstwithvalue LLVM      更新时间:2023-10-16

我是llvm newbie。

我试图编写一个LLVM通行证以优化函数中的代数身份(例如,如果我的功能具有指令a = b * 0,我的通行证应替换所有以下所有用法,用0用0)。<<<<<<<<<<<<<<<</p>

所以,我的通行证看起来如下: -

...
for (Function::iterator f_it = F.begin(), f_ite = F.end(); f_it != f_ite; ++f_it) {
  for(BasicBlock::iterator b_it = f_it->begin(), b_ite = f_it->end(); b_it != b_ite; ++b_it) {
    if(op->getOpcode() == Instruction::Mul) {
      if(ConstantInt *CI_F = dyn_cast<ConstantInt>(&*b_it->getOperand(0))) {
        if(CI_F->isZero()) {
          firstop_zero = 1;
        }
      }
      if(ConstantInt *CI_S = dyn_cast<ConstantInt>(&*b_it->getOperand(1))) {
        if(CI_S->isZero()) {
          secop_zero = 1;
        }
      }
      if(first_zero || second_zero) {   
        errs()<<"found zero operandn";  
        ReplaceInstWithValue(b_it->getParent()->getInstList(),b_it,(first_zero?(&*b_it->getOperand(1)):(&*b_it->getOperand(0))));
      }
    }
  }
}

我可以看到我的评论"发现零操作数在std-err上打印出来,但我看不到由此产生的.bc拆卸中的替换。

我在这里想念什么?任何帮助都是真诚的。

非常感谢!praveena

尝试

for (Function::iterator f_it = F.begin(), f_ite = F.end(); f_it != f_ite; ++f_it) {
for(BasicBlock::iterator b_it = f_it->begin(), b_ite = f_it->end(); b_it != b_ite; ++b_it) {
 Instruction *I = *b_it;
 Value *Zeroval;
if(op->getOpcode() == Instruction::Mul) {
  if(ConstantInt *CI_F = dyn_cast<ConstantInt>(&*b_it->getOperand(0))) {
    if(CI_F->isZero()) {
      firstop_zero = 1;
      Zeroval = CI_F;
    }
  }
  if(ConstantInt *CI_S = dyn_cast<ConstantInt>(&*b_it->getOperand(1))) {
    if(CI_S->isZero()) {
      secop_zero = 1;
      ZeroVal = CI_S;
    }
  }
  if(first_zero || second_zero) {   
    errs()<<"found zero operandn";  
    I->ReplaceAlluseswith(ZeroVal);
  }
}
 }

}