Cloning block for newbie

Hello everybody.

I’m quite new to LLVM and I’m encontering problems with cloning basic blocks. My two basic blocks are in the same function and it doesn’t really matter how the cloned one behave for the moment. Of course, to do so, I used the cloning.h 's method “CloneBasicBlock” but I have the “Instruction does not dominate all uses!” error.
I know what it means, I just don’t know how to get rid of it without getting more complicated errors. (I tried manipulating the VMap, the metadatas, cloning each instruction one by one,…).

Is there a way to know if an instruction is a definition (so I could remove or rename the value)?
Is there a VMap book for newbies?
Is there some documentations I forgot to look at?

Thank you.

virtual BasicBlock* createAlteredBasicBlock(BasicBlock * basicBlock, const Twine & Name = “”, Function * F = 0){

ValueToValueMapTy VMap;
BasicBlock * alteredBB = llvm::CloneBasicBlock (basicBlock, VMap, Name, F);

return alteredBB;

CloneBasicBlock does a fairly shallow cloning; the instructions
themselves are cloned but their operands are not replaced with any
previously cloned values.

  %a = ...
  %b = fadd %a, ...

  %a.clone = ...
  %b.clone = fadd %a, ... ; Note that this references the old %a and
not %a.clone!

You can loop over the instruction's operands to see if they are
contained in VMap and replace them if need be. You'll find it helpful
to view the IR after doing the clone, e.g. "alteredBB->dump()". You
may also need to update phi nodes in the cloned block and elsewhere,
depending on how you plan on inserting the clone into your CFG.

Please reply-all so that the thread is kept on llvmdev.

The LLVM Programmer's Manual has examples of how to iterate over many
common structures, such as instructions in a basic block[1]. Other
than that, you can check the source code or doxygen[2].

Basically, you loop over the instructions as detailed in the
programmer's manual[1], and loop over the operands using User's
op_iterators[2] checking to see if they have entries in ValueMap[3].
If they do, you set that operand to use ValueMap's entry.


Thank you for your help. It’s working now.

I wasn’t aware of the usefulness of the User class. The ValueMapper.cpp’s “RemapInstruction()” also helped me a lot to remap phi nodes and metadatas.

Thank you very much.

2012/6/21 Michael Ilseman <>

Am also new to llvm, and trying to clone an instruction into newly created
basic block of newly created function.

%a = ...
%b = fadd %a, ...

%a.clone = ...
%b.clone = fadd %a, ... ; Note that this references the old %a and
not %a.clone!

Am also facing this problem, and inspite of your so clear explanation I
still not been able to resolve it.

Can you tell me how to use ValueMap and RemapInstruction on this context?