如何避免使用 `ReplaceInstWithValue()` 使迭代器无效?
How do I avoid invaliding the iterator using `ReplaceInstWithValue()`?
在下面的程序中,当我使用 ReplaceInstWithValue()
时,它进入了无限循环,因为我用包含 add
指令的一系列指令替换了 add
指令.因此,该程序会打印 xor
、add
、mul
、...
等行的内容
我想问题与插入到 BasicBlock
指令列表中的指令有关,即执行迭代的列表。
如何解决该问题,以便继续处理列表中的下一个元素,而忽略插入的指令?
将所有插入点放入数据结构并在迭代完成后执行替换是唯一的方法吗?
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include <map>
#include <string>
using namespace llvm;
namespace {
struct CountOp : public FunctionPass {
static char ID;
CountOp() : FunctionPass(ID) {}
virtual bool runOnFunction(Function &F) {
for (Function::iterator bs = F.begin(), be = F.end(); bs != be; ++be) {
for (BasicBlock::iterator is = bs->begin(), ie = be->end(); is != ie; ++is) {
Instruction& inst = *is;
BinaryOperator* binop = dyn_cast<BinaryOperator>(&inst);
if (!binop) {
continue;
}
unsigned opcode = binop->getOpcode();
errs() << binop->getOpcodeName() << "\n";
if (opcode != Instruction::Add) {
continue;
}
IRBuilder<> builder(binop);
Value* v = builder.CreateAdd(builder.CreateXor(binop->getOperand(0), binop->getOperand(1)),
builder.CreateMul(ConstantInt::get(binop->getType(), 2),
builder.CreateAnd(binop->getOperand(0), binop->getOperand(1))));
ReplaceInstWithValue(bs->getInstList(), is, v);
}
}
errs() << "\n";
return true;
}
};
}
char CountOp::ID = 0;
static RegisterPass<CountOp> X("opCounter", "Counts opcodes per functions", false, false);
正如 w1ck3dg0ph3r 指出的,当您应该递增 bs
时,您在外循环中递增 be
。这应该可以解决您的无限循环问题。 llvm::IRBuilder
在传递给构造函数的迭代器指向的指令之前插入指令,因此您不需要进行任何其他更改。
在下面的程序中,当我使用 ReplaceInstWithValue()
时,它进入了无限循环,因为我用包含 add
指令的一系列指令替换了 add
指令.因此,该程序会打印 xor
、add
、mul
、...
我想问题与插入到 BasicBlock
指令列表中的指令有关,即执行迭代的列表。
如何解决该问题,以便继续处理列表中的下一个元素,而忽略插入的指令?
将所有插入点放入数据结构并在迭代完成后执行替换是唯一的方法吗?
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include <map>
#include <string>
using namespace llvm;
namespace {
struct CountOp : public FunctionPass {
static char ID;
CountOp() : FunctionPass(ID) {}
virtual bool runOnFunction(Function &F) {
for (Function::iterator bs = F.begin(), be = F.end(); bs != be; ++be) {
for (BasicBlock::iterator is = bs->begin(), ie = be->end(); is != ie; ++is) {
Instruction& inst = *is;
BinaryOperator* binop = dyn_cast<BinaryOperator>(&inst);
if (!binop) {
continue;
}
unsigned opcode = binop->getOpcode();
errs() << binop->getOpcodeName() << "\n";
if (opcode != Instruction::Add) {
continue;
}
IRBuilder<> builder(binop);
Value* v = builder.CreateAdd(builder.CreateXor(binop->getOperand(0), binop->getOperand(1)),
builder.CreateMul(ConstantInt::get(binop->getType(), 2),
builder.CreateAnd(binop->getOperand(0), binop->getOperand(1))));
ReplaceInstWithValue(bs->getInstList(), is, v);
}
}
errs() << "\n";
return true;
}
};
}
char CountOp::ID = 0;
static RegisterPass<CountOp> X("opCounter", "Counts opcodes per functions", false, false);
正如 w1ck3dg0ph3r 指出的,当您应该递增 bs
时,您在外循环中递增 be
。这应该可以解决您的无限循环问题。 llvm::IRBuilder
在传递给构造函数的迭代器指向的指令之前插入指令,因此您不需要进行任何其他更改。