[LLVMdev] moving instructions

Chris Lattner sabre at nondot.org
Wed Aug 4 09:52:46 PDT 2004


On Wed, 4 Aug 2004, Patrick Meredith wrote:

> How does one move instructions from one basic block to another?  I tried
> this:
>  (IB is an Instruction* as is current_last, current_BB is a BasicBlock*)
>
>       IB->getParent()->getInstList().remove(IB);
>       current_BB->getInstList().insert(current_last, IB);
>
> and I get this assertion:
>  Assertion `V->getParent() == 0 && "Value already in a container!!"' failed.
> it seems to me that remove should remove it from it's container....

The problem you're having is that remove returns in the instruction and
invalidates its argument.  "IB"  does not point to the instruction any
longer, in fact, it gets invalidated by the operation.  Try this:

  Instruction *I = IB->getParent()->getInstList().remove(IB);
  current_BB->getInstList().insert(current_last, I);

Alternatively, you can use splice, which is a little more efficient:

  current_BB->getInstList().splice(current_last, IB->getParent()->getInstList(),
                                   IB);

splice is a bit more efficient than remove/insert because it doesn't
bother to take the symbol out of the function symbol table (unless of
course you're splicing into a different function).  Good
descriptions of splice can be found in STL references for the list class.

-Chris

-- 
http://llvm.cs.uiuc.edu/
http://nondot.org/sabre/




More information about the llvm-dev mailing list