Index: llvm/include/llvm-c/Core.h =================================================================== --- llvm/include/llvm-c/Core.h +++ llvm/include/llvm-c/Core.h @@ -3100,6 +3100,13 @@ void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, const char *K, unsigned KLen); +/** + * Obtain the function type called by this instruction. + * + * @see llvm::CallBase::getFunctionType() + */ +LLVMTypeRef LLVMGetCalledFunctionType(LLVMValueRef C); + /** * Obtain the pointer to the function invoked by this instruction. * @@ -3662,9 +3669,14 @@ /* Miscellaneous instructions */ LLVMValueRef LLVMBuildPhi(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name); +// LLVMBuildCall is deprecated in favor of LLVMBuildCall2, in preparation for +// opaque pointer types. LLVMValueRef LLVMBuildCall(LLVMBuilderRef, LLVMValueRef Fn, LLVMValueRef *Args, unsigned NumArgs, const char *Name); +LLVMValueRef LLVMBuildCall2(LLVMBuilderRef, LLVMTypeRef, LLVMValueRef Fn, + LLVMValueRef *Args, unsigned NumArgs, + const char *Name); LLVMValueRef LLVMBuildSelect(LLVMBuilderRef, LLVMValueRef If, LLVMValueRef Then, LLVMValueRef Else, const char *Name); Index: llvm/include/llvm/IR/IRBuilder.h =================================================================== --- llvm/include/llvm/IR/IRBuilder.h +++ llvm/include/llvm/IR/IRBuilder.h @@ -1880,15 +1880,8 @@ return Insert(PHINode::Create(Ty, NumReservedValues), Name); } - CallInst *CreateCall(Value *Callee, ArrayRef Args = None, - const Twine &Name = "", MDNode *FPMathTag = nullptr) { - auto *PTy = cast(Callee->getType()); - auto *FTy = cast(PTy->getElementType()); - return CreateCall(FTy, Callee, Args, Name, FPMathTag); - } - CallInst *CreateCall(FunctionType *FTy, Value *Callee, - ArrayRef Args, const Twine &Name = "", + ArrayRef Args = None, const Twine &Name = "", MDNode *FPMathTag = nullptr) { CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles); if (isa(CI)) @@ -1896,20 +1889,44 @@ return Insert(CI, Name); } - CallInst *CreateCall(Value *Callee, ArrayRef Args, + CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef Args, ArrayRef OpBundles, const Twine &Name = "", MDNode *FPMathTag = nullptr) { - CallInst *CI = CallInst::Create(Callee, Args, OpBundles); + CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles); if (isa(CI)) CI = cast(setFPAttrs(CI, FPMathTag, FMF)); return Insert(CI, Name); } - CallInst *CreateCall(Function *Callee, ArrayRef Args, + CallInst *CreateCall(Function *Callee, ArrayRef Args = None, const Twine &Name = "", MDNode *FPMathTag = nullptr) { return CreateCall(Callee->getFunctionType(), Callee, Args, Name, FPMathTag); } + CallInst *CreateCall(Function *Callee, ArrayRef Args, + ArrayRef OpBundles, + const Twine &Name = "", MDNode *FPMathTag = nullptr) { + return CreateCall(Callee->getFunctionType(), Callee, Args, OpBundles, Name, + FPMathTag); + } + + // Deprecated [opaque pointer types] + CallInst *CreateCall(Value *Callee, ArrayRef Args = None, + const Twine &Name = "", MDNode *FPMathTag = nullptr) { + return CreateCall( + cast(Callee->getType()->getPointerElementType()), Callee, + Args, Name, FPMathTag); + } + + // Deprecated [opaque pointer types] + CallInst *CreateCall(Value *Callee, ArrayRef Args, + ArrayRef OpBundles, + const Twine &Name = "", MDNode *FPMathTag = nullptr) { + return CreateCall( + cast(Callee->getType()->getPointerElementType()), Callee, + Args, OpBundles, Name, FPMathTag); + } + Value *CreateSelect(Value *C, Value *True, Value *False, const Twine &Name = "", Instruction *MDFrom = nullptr) { if (auto *CC = dyn_cast(C)) Index: llvm/include/llvm/IR/Instructions.h =================================================================== --- llvm/include/llvm/IR/Instructions.h +++ llvm/include/llvm/IR/Instructions.h @@ -1433,36 +1433,25 @@ ArrayRef Bundles, const Twine &NameStr, Instruction *InsertBefore); - inline CallInst(Value *Func, ArrayRef Args, - ArrayRef Bundles, const Twine &NameStr, - Instruction *InsertBefore) - : CallInst(cast( - cast(Func->getType())->getElementType()), - Func, Args, Bundles, NameStr, InsertBefore) {} - - inline CallInst(Value *Func, ArrayRef Args, const Twine &NameStr, - Instruction *InsertBefore) - : CallInst(Func, Args, None, NameStr, InsertBefore) {} + inline CallInst(FunctionType *Ty, Value *Func, ArrayRef Args, + const Twine &NameStr, Instruction *InsertBefore) + : CallInst(Ty, Func, Args, None, NameStr, InsertBefore) {} /// Construct a CallInst given a range of arguments. /// Construct a CallInst from a range of arguments - inline CallInst(Value *Func, ArrayRef Args, + inline CallInst(FunctionType *Ty, Value *Func, ArrayRef Args, ArrayRef Bundles, const Twine &NameStr, BasicBlock *InsertAtEnd); - explicit CallInst(Value *F, const Twine &NameStr, Instruction *InsertBefore); + explicit CallInst(FunctionType *Ty, Value *F, const Twine &NameStr, + Instruction *InsertBefore); - CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd); + CallInst(FunctionType *ty, Value *F, const Twine &NameStr, + BasicBlock *InsertAtEnd); - void init(Value *Func, ArrayRef Args, - ArrayRef Bundles, const Twine &NameStr) { - init(cast( - cast(Func->getType())->getElementType()), - Func, Args, Bundles, NameStr); - } void init(FunctionType *FTy, Value *Func, ArrayRef Args, ArrayRef Bundles, const Twine &NameStr); - void init(Value *Func, const Twine &NameStr); + void init(FunctionType *FTy, Value *Func, const Twine &NameStr); /// Compute the number of operands to allocate. static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) { @@ -1478,21 +1467,9 @@ CallInst *cloneImpl() const; public: - static CallInst *Create(Value *Func, ArrayRef Args, - ArrayRef Bundles = None, - const Twine &NameStr = "", + static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr = "", Instruction *InsertBefore = nullptr) { - return Create(cast( - cast(Func->getType())->getElementType()), - Func, Args, Bundles, NameStr, InsertBefore); - } - - static CallInst *Create(Value *Func, ArrayRef Args, - const Twine &NameStr, - Instruction *InsertBefore = nullptr) { - return Create(cast( - cast(Func->getType())->getElementType()), - Func, Args, None, NameStr, InsertBefore); + return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertBefore); } static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef Args, @@ -1514,7 +1491,18 @@ CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore); } - static CallInst *Create(Value *Func, ArrayRef Args, + static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr, + BasicBlock *InsertAtEnd) { + return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertAtEnd); + } + + static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef Args, + const Twine &NameStr, BasicBlock *InsertAtEnd) { + return new (ComputeNumOperands(Args.size())) + CallInst(Ty, Func, Args, None, NameStr, InsertAtEnd); + } + + static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef Args, ArrayRef Bundles, const Twine &NameStr, BasicBlock *InsertAtEnd) { const int NumOperands = @@ -1522,23 +1510,80 @@ const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); return new (NumOperands, DescriptorBytes) - CallInst(Func, Args, Bundles, NameStr, InsertAtEnd); + CallInst(Ty, Func, Args, Bundles, NameStr, InsertAtEnd); } - static CallInst *Create(Value *Func, ArrayRef Args, + static CallInst *Create(Function *Func, const Twine &NameStr = "", + Instruction *InsertBefore = nullptr) { + return Create(Func->getFunctionType(), Func, NameStr, InsertBefore); + } + + static CallInst *Create(Function *Func, ArrayRef Args, + const Twine &NameStr = "", + Instruction *InsertBefore = nullptr) { + return Create(Func->getFunctionType(), Func, Args, NameStr, InsertBefore); + } + + static CallInst *Create(Function *Func, const Twine &NameStr, + BasicBlock *InsertAtEnd) { + return Create(Func->getFunctionType(), Func, NameStr, InsertAtEnd); + } + + static CallInst *Create(Function *Func, ArrayRef Args, const Twine &NameStr, BasicBlock *InsertAtEnd) { - return new (ComputeNumOperands(Args.size())) - CallInst(Func, Args, None, NameStr, InsertAtEnd); + return Create(Func->getFunctionType(), Func, Args, NameStr, InsertAtEnd); } - static CallInst *Create(Value *F, const Twine &NameStr = "", + // Deprecated [opaque pointer types] + static CallInst *Create(Value *Func, const Twine &NameStr = "", Instruction *InsertBefore = nullptr) { - return new (ComputeNumOperands(0)) CallInst(F, NameStr, InsertBefore); + return Create(cast( + cast(Func->getType())->getElementType()), + Func, NameStr, InsertBefore); + } + + // Deprecated [opaque pointer types] + static CallInst *Create(Value *Func, ArrayRef Args, + const Twine &NameStr, + Instruction *InsertBefore = nullptr) { + return Create(cast( + cast(Func->getType())->getElementType()), + Func, Args, NameStr, InsertBefore); } - static CallInst *Create(Value *F, const Twine &NameStr, + // Deprecated [opaque pointer types] + static CallInst *Create(Value *Func, ArrayRef Args, + ArrayRef Bundles = None, + const Twine &NameStr = "", + Instruction *InsertBefore = nullptr) { + return Create(cast( + cast(Func->getType())->getElementType()), + Func, Args, Bundles, NameStr, InsertBefore); + } + + // Deprecated [opaque pointer types] + static CallInst *Create(Value *Func, const Twine &NameStr, BasicBlock *InsertAtEnd) { - return new (ComputeNumOperands(0)) CallInst(F, NameStr, InsertAtEnd); + return Create(cast( + cast(Func->getType())->getElementType()), + Func, NameStr, InsertAtEnd); + } + + // Deprecated [opaque pointer types] + static CallInst *Create(Value *Func, ArrayRef Args, + const Twine &NameStr, BasicBlock *InsertAtEnd) { + return Create(cast( + cast(Func->getType())->getElementType()), + Func, Args, NameStr, InsertAtEnd); + } + + // Deprecated [opaque pointer types] + static CallInst *Create(Value *Func, ArrayRef Args, + ArrayRef Bundles, + const Twine &NameStr, BasicBlock *InsertAtEnd) { + return Create(cast( + cast(Func->getType())->getElementType()), + Func, Args, Bundles, NameStr, InsertAtEnd); } /// Create a clone of \p CI with a different set of operand bundles and @@ -1647,18 +1692,15 @@ } }; -CallInst::CallInst(Value *Func, ArrayRef Args, +CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef Args, ArrayRef Bundles, const Twine &NameStr, BasicBlock *InsertAtEnd) - : CallBase(cast( - cast(Func->getType())->getElementType()) - ->getReturnType(), - Instruction::Call, + : CallBase(Ty->getReturnType(), Instruction::Call, OperandTraits::op_end(this) - (Args.size() + CountBundleInputs(Bundles) + 1), unsigned(Args.size() + CountBundleInputs(Bundles) + 1), InsertAtEnd) { - init(Func, Args, Bundles, NameStr); + init(Ty, Func, Args, Bundles, NameStr); } CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef Args, Index: llvm/include/llvm/IR/Module.h =================================================================== --- llvm/include/llvm/IR/Module.h +++ llvm/include/llvm/IR/Module.h @@ -367,6 +367,11 @@ return getOrInsertFunction(Name, AttributeList{}, RetTy, Args...); } + // Avoid an incorrect ordering that'd otherwise compile incorrectly. + template + Constant *getOrInsertFunction(StringRef Name, AttributeList AttributeList, + FunctionType *Invalid, ArgsTy... Args) = delete; + /// Look up the specified function in the module symbol table. If it does not /// exist, return null. Function *getFunction(StringRef Name) const; Index: llvm/lib/IR/Core.cpp =================================================================== --- llvm/lib/IR/Core.cpp +++ llvm/lib/IR/Core.cpp @@ -2729,6 +2729,10 @@ return wrap(unwrap(Instr)->getCalledValue()); } +LLVMTypeRef LLVMGetCalledFunctionType(LLVMValueRef Instr) { + return wrap(unwrap(Instr)->getFunctionType()); +} + /*--.. Operations on call instructions (only) ..............................--*/ LLVMBool LLVMIsTailCall(LLVMValueRef Call) { @@ -3583,9 +3587,20 @@ LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args, unsigned NumArgs, const char *Name) { - return wrap(unwrap(B)->CreateCall(unwrap(Fn), - makeArrayRef(unwrap(Args), NumArgs), - Name)); + Value *V = unwrap(Fn); + FunctionType *FnT = + cast(cast(V->getType())->getElementType()); + + return wrap(unwrap(B)->CreateCall(FnT, unwrap(Fn), + makeArrayRef(unwrap(Args), NumArgs), Name)); +} + +LLVMValueRef LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, + LLVMValueRef *Args, unsigned NumArgs, + const char *Name) { + FunctionType *FTy = unwrap(Ty); + return wrap(unwrap(B)->CreateCall(FTy, unwrap(Fn), + makeArrayRef(unwrap(Args), NumArgs), Name)); } LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If, Index: llvm/lib/IR/Instructions.cpp =================================================================== --- llvm/lib/IR/Instructions.cpp +++ llvm/lib/IR/Instructions.cpp @@ -388,9 +388,8 @@ setName(NameStr); } -void CallInst::init(Value *Func, const Twine &NameStr) { - FTy = - cast(cast(Func->getType())->getElementType()); +void CallInst::init(FunctionType *FTy, Value *Func, const Twine &NameStr) { + this->FTy = FTy; assert(getNumOperands() == 1 && "NumOperands not set up?"); setCalledOperand(Func); @@ -399,22 +398,18 @@ setName(NameStr); } -CallInst::CallInst(Value *Func, const Twine &Name, Instruction *InsertBefore) - : CallBase(cast( - cast(Func->getType())->getElementType()) - ->getReturnType(), - Instruction::Call, OperandTraits::op_end(this) - 1, 1, - InsertBefore) { - init(Func, Name); +CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, + Instruction *InsertBefore) + : CallBase(Ty->getReturnType(), Instruction::Call, + OperandTraits::op_end(this) - 1, 1, InsertBefore) { + init(Ty, Func, Name); } -CallInst::CallInst(Value *Func, const Twine &Name, BasicBlock *InsertAtEnd) - : CallBase(cast( - cast(Func->getType())->getElementType()) - ->getReturnType(), - Instruction::Call, OperandTraits::op_end(this) - 1, 1, - InsertAtEnd) { - init(Func, Name); +CallInst::CallInst(FunctionType *Ty, Value *Func, const Twine &Name, + BasicBlock *InsertAtEnd) + : CallBase(Ty->getReturnType(), Instruction::Call, + OperandTraits::op_end(this) - 1, 1, InsertAtEnd) { + init(Ty, Func, Name); } CallInst::CallInst(const CallInst &CI)