[LLVMdev] how to correctly erase a bunch of instructions

Frank Winter fwinter at jlab.org
Sat Jun 20 08:03:21 PDT 2015


I have a basic block BB and a SetVector<Value*> for_erasure. The set 
vector contains the instructions which should be erased from BB but they 
are in no particular order. Thus something like this

for ( Value* v: for_erasure ) {
     if (Instruction *Inst = dyn_cast<Instruction>(v)) {
       Inst->dropAllReferences();
       Inst->eraseFromParent();
     }
   }

results in

While deleting: float* %
Use still stuck around after Def is destroyed: <badref> = getelementptr

I was thinking of reverse iterating through the basic block and testing 
each instruction whether it is contained in the set vector. I have 
however no success with it (segfaults). I think it's because erasing the 
instruction invalidates the (reverse) iterator.

Can I somehow declare instructions as dead and then happily erase them 
(so that the above error's not happening)?

What's the standard way of erasing instructions in the given case? Some 
pseudo-codewould be appreciated.

Thanks
Frank




More information about the llvm-dev mailing list