[英]When using std::istream_iterator it seems to skip empty file lines - how do I avoid this if possible?
[英]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
在将迭代器指向的指令传递给构造函数之前插入指令,因此您无需进行任何其他更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.