How to delete a instruction?

Hi,

when I delete some instruction, I got some error prompt message.

  • %i.0.reg2mem.0 = phi i32 [ 0, %bb5 ], [ %indvar.next, %bb12 ] ; [#uses=2]
  • %s.0.reg2mem.0 = phi i32 [ 0, %bb5 ], [ %tmp16, %bb12 ] ; [#uses=1]
  • %tmp14 = tail call i32 @foobar(i32 %i.0.reg2mem.0) nounwind ; [#uses=1]
  • %tmp16 = add i32 %tmp14, %s.0.reg2mem.0 ; [#uses=2]
    %indvar.next = add i32 %i.0.reg2mem.0, 1 ; [#uses=2]
    %exitcond = icmp eq i32 %indvar.next, %n ; [#uses=1]
    br i1 %exitcond, label %bb25, label %bb12

For example, I want to delete the instructions above with symbol ‘-’ in front, but when I delete the first one, I got the next prompt message:

While deleting: i32 %s.0.reg2mem.0
Use still stuck around after Def is destroyed: %tmp16 = add i32 %tmp14, %s.0.reg2mem.0 ; [#uses=1]
opt: Value.cpp:81: virtual llvm::Value::~Value(): Assertion `use_empty() && “Uses remain when a value is destroyed!”’ failed.

Program received signal SIGABRT, Aborted.

I know I should do some work before the deletion to guarantee that no other instructions will use the instruction being deleted. Who can tell me what I should do?

With Best Regards!

lucefe wrote:

Hi,

when I delete some instruction, I got some error prompt message.

Deleting instructions can be a little tricky. The problem is that you can't delete an instruction until there are no other instructions that are using it. There are two things that you can do:

1) Order your deletions so that the instructions at the end of def-use chains are deleted first; or
2) Replace all uses of the instructions to delete with an Undef value first and then delete the instruction.

-- John T.

I did a simple test just now, but I alse failed.

I delete several ordered instructions from end to begin,
but after deleting the first instruction(the last instruction of F), the program crashed.
My test code is below (F is a function only containing several sequential instructions):

for (inst_iterator inst == --inst_end(F); inst != inst_begin(F); --inst) {
Instruction * i = &*inst;
i->eraseFromParent();
}

Best Regards!

lucefe wrote:

I did a simple test just now, but I alse failed.
I delete several ordered instructions from end to begin,
but after deleting the first instruction(the last instruction of F), the program crashed.
My test code is below (F is a function only containing several sequential instructions):
for (inst_iterator inst == --inst_end(F); inst != inst_begin(F); --inst) {
Instruction * i = &*inst;
i->eraseFromParent();
}

First, you may be invalidating the iterator i by erasing the value inside the loop. In my code, I almost always iterate through the instructions once and record them in a std::vector. I think loop through the std::vector, processing the end element and then remove it from the std::vector. It is less efficient but ensures no nasty iterator invalidation errors.

Second, this code does not work correctly on loops. A basic block could branch to itself, meaning that a phi node at the beginning of the basic block could use a value computed at the end of itself.

You should do the following:

1) For each instruction to delete, replace its use with an Undef value. When this is done for all instructions you want to delete, none of them will have any uses. You should be able to delete them then.

I can provide sample code tomorrow that illustrates how to do this (I'm busy writing a short paper tonight). In the meantime, someone may be able to show you an even better way.

-- John T.

John, thank you for your patience and good suggestion.

I put all the instructions I want to delete into a std::vector,and replace all the use of the instrutions with an undef value, and then delete the instructions.

For your suggestion, I successed. Thank you very much!

Best Regards!

2010/4/21 John Criswell <criswell@uiuc.edu>