No remapping of clone instruction in CloneBasicBlock

Hi,

Found CloneBasicBlock utility only does the cloning without any remapping.

Consider below example:

Input block:
sw.epilog: ; preds = %sw.bb20, %sw.bb15, %sw.bb10, %sw.bb6, %sw.bb2, %sw.bb, %while.body, %if.end29
%no_final.1 = phi i32 [ %no_final.055, %while.body ], [ 1, %if.end29 ], [ %no_final.055, %sw.bb20 ], [ %no_final.055, %sw.bb15 ], [ %no_final.055, %sw.bb10 ], [ %no_final.055, %sw.bb6 ], [ %no_final.055, %sw.bb2 ], [ %no_final.055, %sw.bb ]
%locinput.1 = phi i8* [ %locinput.057, %while.body ], [ %incdec.ptr, %if.end29 ], [ %locinput.057, %sw.bb20 ], [ %locinput.057, %sw.bb15 ], [ %locinput.057, %sw.bb10 ], [ %locinput.057, %sw.bb6 ], [ %locinput.057, %sw.bb2 ], [ %locinput.057, %sw.bb ]
%next30 = getelementptr inbounds %struct.Node, %struct.Node* %scan.056, i64 0, i32 0, !dbg !91
%8 = load %struct.Node*, %struct.Node** %next30, align 8, !dbg !91, !tbaa !92
tail call void @llvm.dbg.value(metadata %struct.Node* %8, i64 0, metadata !28, metadata !34), !dbg !41
%cmp = icmp eq %struct.Node* %8, null, !dbg !52
br i1 %cmp, label %no_silent.loopexit, label %while.body, !dbg !54

Clone Block:
sw.epilog.jl: ; No predecessors!
%no_final.1.jl = phi i32 [ %no_final.055, %while.body ], [ 1, %if.end29 ], [ %no_final.055, %sw.bb20 ], [ %no_final.055, %sw.bb15 ], [ %no_final.055, %sw.bb10 ], [ %no_final.055, %sw.bb6 ], [ %no_final.055, %sw.bb2 ], [ %no_final.055, %sw.bb ]
%locinput.1.jl = phi i8* [ %locinput.057, %while.body ], [ %incdec.ptr, %if.end29 ], [ %locinput.057, %sw.bb20 ], [ %locinput.057, %sw.bb15 ], [ %locinput.057, %sw.bb10 ], [ %locinput.057, %sw.bb6 ], [ %locinput.057, %sw.bb2 ], [ %locinput.057, %sw.bb ]
%next30.jl = getelementptr inbounds %struct.Node, %struct.Node* %scan.056, i64 0, i32 0, !dbg !91
%9 = load %struct.Node*, %struct.Node** %next30, align 8, !dbg !91, !tbaa !92
tail call void @llvm.dbg.value(metadata %struct.Node* %8, i64 0, metadata !28, metadata !34), !dbg !41
%cmp.jl = icmp eq %struct.Node* %8, null, !dbg !52
br i1 %cmp, label %no_silent.loopexit, label %while.body, !dbg !54

Highlighted instruction should be:
%9 = load %struct.Node*, %struct.Node** %next30.jl, align 8, !dbg !91, !tbaa !92
%cmp.jl = icmp eq %struct.Node* %9, null, !dbg !52
br i1 %cmp.jl, label %no_silent.loopexit, label %while.body, !dbg !54

I can see in the comments of this function it clearly mentioned it doesn’t remap the clone instructions.
Wondering why its kept like this, is there any specific reason, cloning without remapping may not be correct always.

Is there any other utility which does both cloning and remapping ?

Thanks,
Ashutosh

Hi Ashuntosh,

Indeed, this is deliberate. When performing cloning operations, often the user requires quite a lot of flexibility. Perhaps by editing the remapping map before remapping is performed (clone with remapping, but change so that %A → %B).

The RemapInstructionsInBlocks function does what you want:

ValueToValueMapTy VM;
CloneBasicBlock(BB, VM, …);
RemapInstructionsInBlocks({BB}, VM);

Cheers,

James

Thanks James, it helps.

Regards,

Ashutosh