Index: llvm/include/llvm/IR/CallSite.h =================================================================== --- llvm/include/llvm/IR/CallSite.h +++ llvm/include/llvm/IR/CallSite.h @@ -121,9 +121,13 @@ return true; } - /// Set the callee to the specified value. + /// Set the callee to the specified value. Unlike the function of the same + /// name on CallBase, does not modify the type! void setCalledFunction(Value *V) { assert(getInstruction() && "Not a call or invoke instruction!"); + assert(cast(V->getType())->getElementType() == + cast(getInstruction())->getFunctionType() && + "New callee type does not match FunctionType on call"); *getCallee() = V; } Index: llvm/include/llvm/IR/InstrTypes.h =================================================================== --- llvm/include/llvm/IR/InstrTypes.h +++ llvm/include/llvm/IR/InstrTypes.h @@ -27,6 +27,7 @@ #include "llvm/IR/CallingConv.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/Function.h" #include "llvm/IR/Instruction.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/OperandTraits.h" @@ -1206,10 +1207,8 @@ void setCalledOperand(Value *V) { Op() = V; } /// Sets the function called, including updating the function type. - void setCalledFunction(Value *Fn) { - setCalledFunction( - cast(cast(Fn->getType())->getElementType()), - Fn); + void setCalledFunction(Function *Fn) { + setCalledFunction(Fn->getFunctionType(), Fn); } /// Sets the function called, including updating to the specified function @@ -1218,6 +1217,9 @@ this->FTy = FTy; assert(FTy == cast( cast(Fn->getType())->getElementType())); + // This function doesn't mutate the return type, only the function + // type. Seems broken, but I'm just gonna stick an assert in for now. + assert(getType() == FTy->getReturnType()); setCalledOperand(Fn); } Index: llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp =================================================================== --- llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp +++ llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp @@ -552,7 +552,7 @@ return sincosUseNative(aCI, FInfo); FInfo.setPrefix(AMDGPULibFunc::NATIVE); - Constant *F = getFunction(aCI->getModule(), FInfo); + Function *F = getFunction(aCI->getModule(), FInfo); if (!F) return false; @@ -792,7 +792,7 @@ AMDGPULibFunc nf = FInfo; nf.setPrefix(AMDGPULibFunc::NATIVE); - if (Constant *FPExpr = getFunction(M, nf)) { + if (Function *FPExpr = getFunction(M, nf)) { LLVM_DEBUG(dbgs() << "AMDIC: " << *CI << " ---> "); CI->setCalledFunction(FPExpr); Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -4200,7 +4200,8 @@ // We cannot remove an invoke, because it would change the CFG, just // change the callee to a null pointer. cast(OldCall)->setCalledFunction( - Constant::getNullValue(CalleeF->getType())); + CalleeF->getFunctionType(), + Constant::getNullValue(CalleeF->getType())); return nullptr; } } Index: llvm/lib/Transforms/ObjCARC/ObjCARCContract.cpp =================================================================== --- llvm/lib/Transforms/ObjCARC/ObjCARCContract.cpp +++ llvm/lib/Transforms/ObjCARC/ObjCARCContract.cpp @@ -139,7 +139,7 @@ // We do not have to worry about tail calls/does not throw since // retain/retainRV have the same properties. - Constant *Decl = EP.get(ARCRuntimeEntryPointKind::RetainRV); + Function *Decl = EP.get(ARCRuntimeEntryPointKind::RetainRV); cast(Retain)->setCalledFunction(Decl); LLVM_DEBUG(dbgs() << "New: " << *Retain << "\n"); @@ -188,7 +188,7 @@ " Retain: " << *Retain << "\n"); - Constant *Decl = EP.get(Class == ARCInstKind::AutoreleaseRV + Function *Decl = EP.get(Class == ARCInstKind::AutoreleaseRV ? ARCRuntimeEntryPointKind::RetainAutoreleaseRV : ARCRuntimeEntryPointKind::RetainAutorelease); Retain->setCalledFunction(Decl); Index: llvm/lib/Transforms/ObjCARC/ObjCARCOpts.cpp =================================================================== --- llvm/lib/Transforms/ObjCARC/ObjCARCOpts.cpp +++ llvm/lib/Transforms/ObjCARC/ObjCARCOpts.cpp @@ -641,7 +641,7 @@ "Old = " << *RetainRV << "\n"); - Constant *NewDecl = EP.get(ARCRuntimeEntryPointKind::Retain); + Function *NewDecl = EP.get(ARCRuntimeEntryPointKind::Retain); cast(RetainRV)->setCalledFunction(NewDecl); LLVM_DEBUG(dbgs() << "New = " << *RetainRV << "\n"); @@ -690,7 +690,7 @@ << *AutoreleaseRV << "\n"); CallInst *AutoreleaseRVCI = cast(AutoreleaseRV); - Constant *NewDecl = EP.get(ARCRuntimeEntryPointKind::Autorelease); + Function *NewDecl = EP.get(ARCRuntimeEntryPointKind::Autorelease); AutoreleaseRVCI->setCalledFunction(NewDecl); AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease. Class = ARCInstKind::Autorelease; Index: llvm/lib/Transforms/Utils/CallPromotionUtils.cpp =================================================================== --- llvm/lib/Transforms/Utils/CallPromotionUtils.cpp +++ llvm/lib/Transforms/Utils/CallPromotionUtils.cpp @@ -366,8 +366,9 @@ CastInst **RetBitCast) { assert(!CS.getCalledFunction() && "Only indirect call sites can be promoted"); - // Set the called function of the call site to be the given callee. - CS.setCalledFunction(Callee); + // Set the called function of the call site to be the given callee (but don't + // change the type). + cast(CS.getInstruction())->setCalledOperand(Callee); // Since the call site will no longer be direct, we must clear metadata that // is only appropriate for indirect calls. This includes !prof and !callees Index: llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp =================================================================== --- llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -2035,7 +2035,7 @@ Constant *IPrintFFn = M->getOrInsertFunction("iprintf", FT, Callee->getAttributes()); CallInst *New = cast(CI->clone()); - New->setCalledFunction(IPrintFFn); + New->setCalledFunction(FT, IPrintFFn); B.Insert(New); return New; } @@ -2114,7 +2114,7 @@ Constant *SIPrintFFn = M->getOrInsertFunction("siprintf", FT, Callee->getAttributes()); CallInst *New = cast(CI->clone()); - New->setCalledFunction(SIPrintFFn); + New->setCalledFunction(FT, SIPrintFFn); B.Insert(New); return New; } @@ -2271,7 +2271,7 @@ Constant *FIPrintFFn = M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes()); CallInst *New = cast(CI->clone()); - New->setCalledFunction(FIPrintFFn); + New->setCalledFunction(FT, FIPrintFFn); B.Insert(New); return New; }