add
命令をadd
命令を含む命令シーケンスに置き換えるため、以下のプログラムでは、ReplaceInstWithValue()
を使用すると無限ループに入ります。このように、プログラムを印刷xor
の線に沿って何かを、add
、mul
は、...`ReplaceInstWithValue()`を使ってイテレータを無効にするのを避けるにはどうしたらいいですか?
は、私はこの問題は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
これは基本ブロックです。 – Shuzheng