2017-06-08 7 views
0

LLVMで一連の命令を削除する方法を知りました。 は私が最後の反復を除いてすべてが期待どおりに動作します(LLVM-devメーリングリストからのポストに基づいて)複数のllvm命令を削除する

// delete all instructions between [start,end) 

void deleteAllInstructionsInRange(Instruction* startInst,Instruction* endInst) 
{ 
    BasicBlock::iterator it(startInst); 
    BasicBlock::iterator it_end(endInst); 
    it_end--; 

    Instruction* currentInst ; 

    while(it != it_end) 
    { 
     currentInst = &*it; 

     // this cannot be done at the end of the while loop. 
     // has to be incremented before "erasing" the instruction 
     ++it; 

     if (!currentInst->use_empty()) 
     { 
      currentInst->replaceAllUsesWith(UndefValue::get(currentInst->getType())); 
     } 

     currentInst->eraseFromParent(); 


    } 

} 

を以下を試してみました。 誰もがその理由を理解していますか? (私はgdbを使用しようとしましたが、最後の反復の のsegfaultエラーを返します)

答えて

0

ループが構築される方法によって、無効なイテレータである内部コードが削除されます。it == it_end。簡単なif (it == it_end) continue;の後にit++が役立ちます。

LLVMのイテレータがどのように構築されているのか分かりませんが、stlコンテナの場合、eraseはインクリメントされたイテレータを返すので、奇妙なループも必要ありません。 docsを簡単に見て、これを確認しているようです。

関連する問題