Is it safe to insert instructions in batches into BBs?

Hi,

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);

Vector.push_back(IB.CreateXXX());
...
// 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:

IB.SetInsertionPoint(SomeInstruction);

for () {
   IB.CreateXXX();
}

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?

Cheers,
Marcello

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

Marcello Maggioni <marcello@codeplay.com> writes:

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.

[snip]

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?

Cheers,
Marcello

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

Does your module pass the verifier? (llvm::verifyModule)

From: llvmdev-bounces@cs.uiuc.edu [mailto:llvmdev-bounces@cs.uiuc.edu]
On Behalf Of Marcello Maggioni
Subject: [LLVMdev] Is it safe to insert instructions in batches into BBs?

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.

You cannot insert the same instruction object into more than one basic block. You might be able to clone instructions and add the clones to the other blocks.

- Chuck

Yes, it does. I get no complaints by the module verifier, and the CFG at the IR level looks fine.

If I try to print the CFG at Machine level though in some occasions the graph is not shown (probably another one of the strange breakages I get together with the RegisterInfo one)

Marcello

I'm not doing that, I only insert an instruction in a single basicblock. I'm using that method of insertion because it makes the code clearer to insert the instructions like that instead of inserting them immediately where they are created (the actual code is more complex than the example I shown , but is conceptually the same)

Marcello