Reputation: 655
I'm trying to copy the CFG in function into a new function to do some operations with old one. I used CloneFunctionInto by the following code
std::vector<Type*> ArgTypes;
ValueToValueMapTy VMap;
FunctionType *FTy = FunctionType::get(F.getFunctionType()->getReturnType(), ArgTypes, F.getFunctionType()->isVarArg());
Function *NewF = Function::Create(FTy, F.getLinkage(), F.getName());
SmallVector<ReturnInst*, 8> Returns;
CloneFunctionInto(NewF, (Function*) &F, VMap, false, Returns, "_", 0, 0);
The error I have got after printing each basic block in the new and old functions :
While deleting: i32 %
Use still stuck around after Def is destroyed: %cmp1_ = icmp sgt i32 %4, 20
opt: Value.cpp:75: virtual llvm::Value::~Value(): Assertion `use_empty() && "Uses remain when a value is destroyed!"' failed.
0 opt 0x0000000000fc7fe2
1 opt 0x0000000000fc8473
2 libpthread.so.0 0x00007f2b28876cb0
3 libc.so.6 0x00007f2b27ac8425 gsignal + 53
4 libc.so.6 0x00007f2b27acbb8b abort + 379
5 libc.so.6 0x00007f2b27ac10ee
6 libc.so.6 0x00007f2b27ac1192
7 opt 0x0000000000f7cd7d
8 opt 0x0000000000ee3c52
9 opt 0x0000000000f5bddf llvm::LLVMContextImpl::~LLVMContextImpl() + 1999
10 opt 0x0000000000f5ad1c
11 opt 0x0000000000fb9d69 llvm::llvm_shutdown() + 41
12 opt 0x00000000005285b3 main + 7251
13 libc.so.6 0x00007f2b27ab376d __libc_start_main + 237
14 opt 0x0000000000521f91
Stack dump:
0. Program arguments: opt -load ../../../Release+Asserts/lib/Blocks.so -blocks
Aborted (core dumped)
To clarify more: it gives me the correct basic blocks with its correct successors, but it also doesn't work well because of this error
Upvotes: 1
Views: 693
Reputation: 273456
It doesn't look like you're copying the arguments over appropriately. Take a look at the llvm::CloneModule
function (in lib/Transforms/Utils/CloneModule.cpp
) to see how to use CloneFunction
correctly.
Upvotes: 1