LLVMAddGlobalMapping after optimization

I use MCJIT on LLVM-3.8 and use LLVMAddGlobalMapping to register call-back functions. LLVMAddGlobalMapping needs a LLVMValueRef to a function call value. However, is this still valid after optimization? I always suspected that this is not true, but it worked in many occasions anyway. Now, I think I hit a case where it fails.

I do roughly this:

   ...
   funcRef = LLVMAddFunction(module, funcName, funcType);
   ...
   LLVMRunPassManager(passes, module);
   ...
   LLVMCreateExecutionEngineForModule(&ee, module, &err);
   ...
   // Is funcRef still valid here?
   LLVMAddGlobalMapping(ee, funcRef, &myFunc);
   ...

Is this admissible? If not (what I assume), how can I manage call-backs in an optimization-proof way?

Aha, it seems that optimization does not change the LLVMValueRef of a function during optimization. However, optimization may remove an unused function completely, which makes the LLVMValueRef invalid. Now I retrieve the LLVMValueRefs via LLVMGetNamedFunction and check for NULL pointers.