diff options
-rw-r--r-- | llvm/include/llvm-c/Core.h | 22 | ||||
-rw-r--r-- | llvm/lib/IR/Core.cpp | 23 | ||||
-rw-r--r-- | llvm/tools/llvm-c-test/echo.cpp | 4 |
3 files changed, 35 insertions, 14 deletions
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index 27eb7aef3fc..019bc7a8044 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -2386,6 +2386,17 @@ LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst); */ /** + * 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 @@ void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 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. diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index cf23a28ccb6..ba27e5042f0 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -2041,22 +2041,17 @@ LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) { /*--.. Call and invoke instructions ........................................--*/ +unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) { + return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands(); +} + unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) { - Value *V = unwrap(Instr); - if (CallInst *CI = dyn_cast<CallInst>(V)) - return CI->getCallingConv(); - if (InvokeInst *II = dyn_cast<InvokeInst>(V)) - return II->getCallingConv(); - llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!"); + return CallSite(unwrap<Instruction>(Instr)).getCallingConv(); } void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) { - Value *V = unwrap(Instr); - if (CallInst *CI = dyn_cast<CallInst>(V)) - return CI->setCallingConv(static_cast<CallingConv::ID>(CC)); - else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) - return II->setCallingConv(static_cast<CallingConv::ID>(CC)); - llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!"); + return CallSite(unwrap<Instruction>(Instr)) + .setCallingConv(static_cast<CallingConv::ID>(CC)); } void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, @@ -2090,6 +2085,10 @@ void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, index, B))); } +LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) { + return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue()); +} + /*--.. Operations on call instructions (only) ..............................--*/ LLVMBool LLVMIsTailCall(LLVMValueRef Call) { diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp index 4e4841b8ed8..cbacec09bbc 100644 --- a/llvm/tools/llvm-c-test/echo.cpp +++ b/llvm/tools/llvm-c-test/echo.cpp @@ -336,10 +336,10 @@ struct FunCloner { } case LLVMCall: { SmallVector<LLVMValueRef, 8> 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; } |