Skip to content

Commit 955fc63

Browse files
yamtaeubanks
authored andcommitted
[llvm-c] Add LLVMSetTailCallKind and LLVMGetTailCallKind
Reviewed By: aeubanks Differential Revision: https://reviews.llvm.org/D153107
1 parent 8f38138 commit 955fc63

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-1
lines changed

llvm/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ Changes to the Python bindings
116116
Changes to the C API
117117
--------------------
118118

119+
* Added ``LLVMGetTailCallKind`` and ``LLVMSetTailCallKind`` to
120+
allow getting and setting ``tail``, ``musttail``, and ``notail``
121+
attributes on call instructions.
122+
119123
Changes to the CodeGen infrastructure
120124
-------------------------------------
121125

llvm/include/llvm-c/Core.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,20 @@ enum {
468468
LLVMAttributeFunctionIndex = -1,
469469
};
470470

471+
/**
472+
* Tail call kind for LLVMSetTailCallKind and LLVMGetTailCallKind.
473+
*
474+
* Note that 'musttail' implies 'tail'.
475+
*
476+
* @see CallInst::TailCallKind
477+
*/
478+
typedef enum {
479+
LLVMTailCallKindNone = 0,
480+
LLVMTailCallKindTail = 1,
481+
LLVMTailCallKindMustTail = 2,
482+
LLVMTailCallKindNoTail = 3,
483+
} LLVMTailCallKind;
484+
471485
typedef unsigned LLVMAttributeIndex;
472486

473487
/**
@@ -3429,6 +3443,20 @@ LLVMBool LLVMIsTailCall(LLVMValueRef CallInst);
34293443
*/
34303444
void LLVMSetTailCall(LLVMValueRef CallInst, LLVMBool IsTailCall);
34313445

3446+
/**
3447+
* Obtain a tail call kind of the call instruction.
3448+
*
3449+
* @see llvm::CallInst::setTailCallKind()
3450+
*/
3451+
LLVMTailCallKind LLVMGetTailCallKind(LLVMValueRef CallInst);
3452+
3453+
/**
3454+
* Set the call kind of the call instruction.
3455+
*
3456+
* @see llvm::CallInst::getTailCallKind()
3457+
*/
3458+
void LLVMSetTailCallKind(LLVMValueRef CallInst, LLVMTailCallKind kind);
3459+
34323460
/**
34333461
* Return the normal destination basic block.
34343462
*

llvm/lib/IR/Core.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2896,6 +2896,14 @@ void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
28962896
unwrap<CallInst>(Call)->setTailCall(isTailCall);
28972897
}
28982898

2899+
LLVMTailCallKind LLVMGetTailCallKind(LLVMValueRef Call) {
2900+
return (LLVMTailCallKind)unwrap<CallInst>(Call)->getTailCallKind();
2901+
}
2902+
2903+
void LLVMSetTailCallKind(LLVMValueRef Call, LLVMTailCallKind kind) {
2904+
unwrap<CallInst>(Call)->setTailCallKind((CallInst::TailCallKind)kind);
2905+
}
2906+
28992907
/*--.. Operations on invoke instructions (only) ............................--*/
29002908

29012909
LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {

llvm/test/Bindings/llvm-c/echo.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ define i32 @call() {
9898
ret i32 %1
9999
}
100100

101+
define i32 @tailcall() {
102+
%1 = tail call i32 @call()
103+
ret i32 %1
104+
}
105+
106+
define i32 @musttailcall() {
107+
%1 = musttail call i32 @call()
108+
ret i32 %1
109+
}
110+
111+
define i32 @notailcall() {
112+
%1 = notail call i32 @call()
113+
ret i32 %1
114+
}
115+
101116
define i32 @cond(i32 %a, i32 %b) {
102117
br label %br
103118
unreachable:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ struct FunCloner {
736736
LLVMTypeRef FnTy = CloneType(LLVMGetCalledFunctionType(Src));
737737
LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
738738
Dst = LLVMBuildCall2(Builder, FnTy, Fn, Args.data(), ArgCount, Name);
739-
LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
739+
LLVMSetTailCallKind(Dst, LLVMGetTailCallKind(Src));
740740
CloneAttrs(Src, Dst);
741741
break;
742742
}

0 commit comments

Comments
 (0)