Moving instructions from source Basic Block to dest Basic Block


I have been trying to move some instructions between basic blocks ,

After looking at the API , I found llvm::Instruction::clone() but there

is no result value for this.

For example-

source Basic block :

continuation: ; preds = %else, %then
%iftmp = phi i32 [ 5, %then ], [ 9, %else ]
store i32 %iftmp, i32* %datasize

; 3 instructions below being copied

store i8000000 0, i8000000* %res
%res4 = load i8000000, i8000000* %res
ret i8000000 %res4

After copying the 3 instructions above to destination basic block.

destination basic block :

else: ; preds = %entry
store i8000000 0, i8000000* %res
%1---->issue = load i8000000, i8000000* %res
ret i8000000 %res4 -----> issue , need to get result of instruction above.
br label %continuation

I had tried using a simple Instruction::insert() earlier but ran

into an assertion error complaining that the Function contained duplicated instruction.

My Question : How to copy an instruction ( along with it’s name , or the LHS of the instruction )

Is it possible to do name the LHS of the instruction using clone() ?



It’s a bit unclear what exactly you’re looking to do. The subject talks about moving instructions and the subsequent question mentions copying them.

I imagine you can get a pretty good example of how moving is done by looking at splitBasicBlock() for the moving case (it may in fact turn out that you actually want llvm::splitBlock() from lib/Transforms/Utils). You might want to look at the numerous uses of IRBuilder if you’re looking to create new instructions that replicate the existing ones.

If none of this leads you to the answer, feel free to clarify the question a bit and re-post. If you specify exactly why you want this, you may get very good input in terms of how best to achieve the actual goal.

Ah ok. So you really don’t want to clone the instructions. You can build new ones using IRBuilder.

I suppose you can clone the first one, then use the resulting llvm::Value to build the next one. And so on.