2017-06-08 92 views
0

我想知道如何刪除LLVM中的一堆指令。 我嘗試以下(基於LLVM開發郵件列表後)刪除一堆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,但是在最後一次迭代中給出了 中的段錯誤)

回答

0

循環的構建方式使內部代碼嘗試刪除無效的迭代器:it == it_endit++之後的簡單if (it == it_end) continue;會有所幫助。

不知道如何構建LLVM迭代器,但對於stl容器,erase將返回遞增的迭代器,因此您甚至不需要奇怪的循環。簡要看一下docs似乎證實了這一點。