Skip to content

Commit 5c7b3af

Browse files
committed
Add binding for counting argument and find called value of call and invoke instruction from the C API.
Summary: As per title. This remove the need to rely on internal knowledge of call and invoke instruction to find called value and argument count. Reviewers: bogner, chandlerc, echristo, dblaikie, joker.eph, Wallbraker Differential Revision: http://reviews.llvm.org/D17054 llvm-svn: 260332
1 parent e286b8d commit 5c7b3af

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

llvm/include/llvm-c/Core.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,17 @@ LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst);
23852385
* @{
23862386
*/
23872387

2388+
/**
2389+
* Obtain the argument count for a call instruction.
2390+
*
2391+
* This expects an LLVMValueRef that corresponds to a llvm::CallInst or
2392+
* llvm::InvokeInst.
2393+
*
2394+
* @see llvm::CallInst::getNumArgOperands()
2395+
* @see llvm::InvokeInst::getNumArgOperands()
2396+
*/
2397+
unsigned LLVMGetNumArgOperands(LLVMValueRef Instr);
2398+
23882399
/**
23892400
* Set the calling convention for a call instruction.
23902401
*
@@ -2413,6 +2424,17 @@ void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
24132424
void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
24142425
unsigned align);
24152426

2427+
/**
2428+
* Obtain the pointer to the function invoked by this instruction.
2429+
*
2430+
* This expects an LLVMValueRef that corresponds to a llvm::CallInst or
2431+
* llvm::InvokeInst.
2432+
*
2433+
* @see llvm::CallInst::getCalledValue()
2434+
* @see llvm::InvokeInst::getCalledValue()
2435+
*/
2436+
LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr);
2437+
24162438
/**
24172439
* Obtain whether a call instruction is a tail call.
24182440
*

llvm/lib/IR/Core.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,22 +2041,17 @@ LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
20412041

20422042
/*--.. Call and invoke instructions ........................................--*/
20432043

2044+
unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) {
2045+
return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands();
2046+
}
2047+
20442048
unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
2045-
Value *V = unwrap(Instr);
2046-
if (CallInst *CI = dyn_cast<CallInst>(V))
2047-
return CI->getCallingConv();
2048-
if (InvokeInst *II = dyn_cast<InvokeInst>(V))
2049-
return II->getCallingConv();
2050-
llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
2049+
return CallSite(unwrap<Instruction>(Instr)).getCallingConv();
20512050
}
20522051

20532052
void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2054-
Value *V = unwrap(Instr);
2055-
if (CallInst *CI = dyn_cast<CallInst>(V))
2056-
return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
2057-
else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
2058-
return II->setCallingConv(static_cast<CallingConv::ID>(CC));
2059-
llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
2053+
return CallSite(unwrap<Instruction>(Instr))
2054+
.setCallingConv(static_cast<CallingConv::ID>(CC));
20602055
}
20612056

20622057
void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
@@ -2090,6 +2085,10 @@ void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
20902085
index, B)));
20912086
}
20922087

2088+
LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) {
2089+
return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue());
2090+
}
2091+
20932092
/*--.. Operations on call instructions (only) ..............................--*/
20942093

20952094
LLVMBool LLVMIsTailCall(LLVMValueRef Call) {

llvm/tools/llvm-c-test/echo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,10 @@ struct FunCloner {
336336
}
337337
case LLVMCall: {
338338
SmallVector<LLVMValueRef, 8> Args;
339-
int ArgCount = LLVMGetNumOperands(Src) - 1;
339+
int ArgCount = LLVMGetNumArgOperands(Src);
340340
for (int i = 0; i < ArgCount; i++)
341341
Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
342-
LLVMValueRef Fn = CloneValue(LLVMGetOperand(Src, ArgCount));
342+
LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
343343
Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
344344
break;
345345
}

0 commit comments

Comments
 (0)