[LLVMdev] Is it safe to insert instructions in batches into BBs?

Marcello Maggioni marcello at codeplay.com
Tue Sep 17 16:47:43 PDT 2013


I'm getting a very strange behaviour while adding already created 
instructions in batches into basicblocks instead of creating and 
inserting them immediately.

Because I need to insert instructions in a certain specific order inside 
multiple different BBs I found it easy to use the IRBuilder to create 
instructions without inserting them into a BB, storing them somewhere 
(vector, map ... etc) and later on inserting all of them in one go into 
their positions in the BBs.

What I do is similar to this:

IRBuilder<> IB(Context);

// Other stuff

for (SmallVectorImpl<Instruction*>::iterator I = Vector.begin(), E = 
Vector.end(); I != E; ++I) {

   SomeBB->getInstList().insert(SomeInstruction, *I);


If I add the instructions like this I get strange results. It's like if 
llvm gets corrupted or something similar, because in the backend strange 
things happen, like MCRegisterInfo returning wrong register aliasing 
information in a random fashion (sometimes happens and sometimes doesn't) .

If I create and add the instructions on the fly like this:


for () {

Everything is fine.

So , in the end, is it safe to add instructions like in the first method 
I mentioned or actually the only safe way is to use the second way? In 
case it should be safe, what could be the cause of the random breakages?


PS = Some of the instructions I'm adding are using one the output value 
of the other.

More information about the llvm-dev mailing list