Index: llvm/trunk/include/llvm-c/Core.h =================================================================== --- llvm/trunk/include/llvm-c/Core.h +++ llvm/trunk/include/llvm-c/Core.h @@ -2386,6 +2386,17 @@ */ /** + * Obtain the argument count for a call instruction. + * + * This expects an LLVMValueRef that corresponds to a llvm::CallInst or + * llvm::InvokeInst. + * + * @see llvm::CallInst::getNumArgOperands() + * @see llvm::InvokeInst::getNumArgOperands() + */ +unsigned LLVMGetNumArgOperands(LLVMValueRef Instr); + +/** * Set the calling convention for a call instruction. * * This expects an LLVMValueRef that corresponds to a llvm::CallInst or @@ -2414,6 +2425,17 @@ unsigned align); /** + * Obtain the pointer to the function invoked by this instruction. + * + * This expects an LLVMValueRef that corresponds to a llvm::CallInst or + * llvm::InvokeInst. + * + * @see llvm::CallInst::getCalledValue() + * @see llvm::InvokeInst::getCalledValue() + */ +LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr); + +/** * Obtain whether a call instruction is a tail call. * * This only works on llvm::CallInst instructions. Index: llvm/trunk/lib/IR/Core.cpp =================================================================== --- llvm/trunk/lib/IR/Core.cpp +++ llvm/trunk/lib/IR/Core.cpp @@ -2041,22 +2041,17 @@ /*--.. Call and invoke instructions ........................................--*/ +unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) { + return CallSite(unwrap(Instr)).getNumArgOperands(); +} + unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) { - Value *V = unwrap(Instr); - if (CallInst *CI = dyn_cast(V)) - return CI->getCallingConv(); - if (InvokeInst *II = dyn_cast(V)) - return II->getCallingConv(); - llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!"); + return CallSite(unwrap(Instr)).getCallingConv(); } void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) { - Value *V = unwrap(Instr); - if (CallInst *CI = dyn_cast(V)) - return CI->setCallingConv(static_cast(CC)); - else if (InvokeInst *II = dyn_cast(V)) - return II->setCallingConv(static_cast(CC)); - llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!"); + return CallSite(unwrap(Instr)) + .setCallingConv(static_cast(CC)); } void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, @@ -2090,6 +2085,10 @@ index, B))); } +LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) { + return wrap(CallSite(unwrap(Instr)).getCalledValue()); +} + /*--.. Operations on call instructions (only) ..............................--*/ LLVMBool LLVMIsTailCall(LLVMValueRef Call) { Index: llvm/trunk/tools/llvm-c-test/echo.cpp =================================================================== --- llvm/trunk/tools/llvm-c-test/echo.cpp +++ llvm/trunk/tools/llvm-c-test/echo.cpp @@ -336,10 +336,10 @@ } case LLVMCall: { SmallVector Args; - int ArgCount = LLVMGetNumOperands(Src) - 1; + int ArgCount = LLVMGetNumArgOperands(Src); for (int i = 0; i < ArgCount; i++) Args.push_back(CloneValue(LLVMGetOperand(Src, i))); - LLVMValueRef Fn = CloneValue(LLVMGetOperand(Src, ArgCount)); + LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name); break; }