Erasing Instruction

Hi,

Can anyone tell me how to erase an instruction, (specially a
load/store instruction) ?

If I use Instr->eraseFromParent(), I get following error. Note the
instruction does not have any use.

opt: /home/chayan/llvm/llvm-2.6/include/llvm/ADT/ilist.h:218:
llvm::ilist_iterator<NodeTy>&
llvm::ilist_iterator<NodeTy>::operator++() [with NodeTy =
llvm::Instruction]: Assertion `NodePtr && "++'d off the end of an
ilist!"' failed.
0 opt 0x084560a8
Stack dump:
0. Running pass 'Function Pass Manager' on module '<stdin>'.
1. Running pass 'Testing try-outs' on function '@test'
./unoptrun.sh: line 10: 32064 Aborted opt -load
/home/chayan/llvm/llvm-2.6/Release/lib/test.so -test < input.bc >
output.bc
llvm-dis: Invalid bitcode signature

Any idea how to solve this.

Regards,
Chayan

Chayan Sarkar wrote:

Hi,

Can anyone tell me how to erase an instruction, (specially a
load/store instruction) ?

If I use Instr->eraseFromParent(), I get following error. Note the
instruction does not have any use.
  
Are you calling the eraseFromParent() method on an Instruction inside a loop? If so, is that loop iterating over instructions using an iterator?

If so, then the problem might be that you're invalidating the iterator that iterates over instructions. When I delete instructions, I typically iterate over the list of instructions and record those I want to delete into a std::vector<> and then use another loop that fetches the last element in the std::vector<> and deletes it. While less efficient, I never have iterator invalidation errors.

-- John T.