instructions copy

Hi,

I want to copy some dependent statements, like a = b, b = c, from one basicblock to another basicblocks.
Because of SSA, a = b, will be like %1 = load %b, store %1, %a.
If I just use clone() method in Instruction class, it will be like = load %b, store , %a.
If I need remap the virtual registers, this map just will affect the whole module? And how to use it? I am a bit confused.

Any suggestion will be appreciated.

Best,
Yuxi
Uchicago

I'm Just a beginner so i may be wrong.
Does Block Insert work?
Create a new block with your instructions with (new BasicBlock) and (new BranchInst) , split using SplitBlock and insert.
http://stackoverflow.com/questions/13275577/inserting-a-block-between-two-blocks-in-llvm

If it is possible to just add instructions, can someone please suggest
how this can be done.

It must be possible for inline function instructions.

Thank You.

I'm Just a beginner so i may be wrong.
Does Block Insert work?
Create a new block with your instructions with (new BasicBlock) and (new BranchInst) , split using SplitBlock and insert.
http://stackoverflow.com/questions/13275577/inserting-a-block-between-two-blocks-in-llvm

If it is possible to just add instructions, can someone please suggest
how this can be done.

It must be possible for inline function instructions.

Thank You.

Hi Yuxi,

Yes, you need to remap virtual registers. In your example, the clone of “store %1, %a” still uses the old %1 right after cloning, so you need to set the first operand to the clone of “load %b”.

You may want to mimic llvm::CloneFunctionInto. In particular, CloneBasicBlock clones instructions in a basic block and creates a mapping from original to cloned. Then, RemapInstruction applies the mapping to the operands of the cloned instructions.

Jingyue

Dear Yuxi,

I haven't used the clone() method of the Instruction class, but if I had to guess, the problem is that you're not changing the operands of the new instructions to reference the new instructions. When you clone an instruction, the operands of the new instruction are identical to the operands of the old instruction.

In your example, if you clone the store, the store is still referencing %1 from the original LoadInst in the original basic block. You need to change the new store's operand to be the new LoadInst that you created in your new BasicBlock.

You may also want to look at the llvm::CloneBasicBlock() function. This utility function clones basic blocks; you can take a look at what it does and see how it works.

Regards,

John Criswell

Hi Keutoi,

Thanks for your reply. I get your point. But the problem when you copy instuctions, you need to remap the virtual register. A naive way is to keep a map of old virtual registers and new virtual registers, then iterate all cloned instruction, replace the old virtual registers with new virtual registers.

Thanks again.
Yuxi

Hi Jingyue,

Thanks for your reply. Yep, I just mimic some programming like llvm::CloneFUnctionInto. It works.

Best,
Yuxi

Hi John,

I really appreciate your reply. Right now, I write some programming like CloneBasicBlock(), it works.

Thanks,
Yuxi