When you are confused about this, it is a good idea to print it out as follows
llvm::raw_fd_ostream outfile("output.ll", EC, llvm::sys::fs::OF_Text);
legacy::PassManager pm;
pm.add(createPrintModulePass(outfile));
pm.run(*module); // this module is the one you generated
// and the same to the function
then you can see the problems in your module or function:)