下記コードスニペットでは、BasicBlock
がFunction::iterator bs
で、Instruction
の文字を挿入します。内部ループは、BasicBlock
に含まれる命令を反復処理します。今要素を挿入した後にデータ構造を反復処理する方法はありますか?
、内側のループは、これらの命令を挿入した後、プログラムは命令シーケンスで無限ループに入る:
and
mul
xor
and
mul
xor
and
mul
xor
and
mul
xor
and
mul
xor
and
mul
...
私は回避が無限に入っている間、繰り返し処理されたデータ構造に挿入する方法ループ?
どういうわけか、イテレータはナット(または無効)になります。この問題に対処する方法の共通のイディオムはありますか?
for (Function::iterator bs = F.begin(), be = F.end(); bs != be; ++bs) {
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); // THINGS GO WRONG HERE!
}
}
私はちょうど新しいリストを作成し、繰り返しているコレクションを変更するのではなく、そのリストに追加します。マップ/フィルターコンボも機能するはずです。 – Carcigenicate
「ReplaceInstWithValue」はどのように見えますか? – 1201ProgramAlarm
http://llvm.org/docs/doxygen/html/BasicBlockUtils_8cpp_source.html#l00189 – Shuzheng