[llvm] r261254 - Add support for invoke/landingpad/resume in C API test
Amaury Sechet via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 18 12:38:33 PST 2016
Author: deadalnix
Date: Thu Feb 18 14:38:32 2016
New Revision: 261254
URL: http://llvm.org/viewvc/llvm-project?rev=261254&view=rev
Log:
Add support for invoke/landingpad/resume in C API test
Summary: As per title. There was a lot of part missing in the C API, so I had to extend the invoke and landingpad API.
Reviewers: echristo, joker.eph, Wallbraker
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D17359
Added:
llvm/trunk/test/Bindings/llvm-c/invoke.ll
Modified:
llvm/trunk/include/llvm-c/Core.h
llvm/trunk/lib/IR/Core.cpp
llvm/trunk/tools/llvm-c-test/echo.cpp
Modified: llvm/trunk/include/llvm-c/Core.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm-c/Core.h?rev=261254&r1=261253&r2=261254&view=diff
==============================================================================
--- llvm/trunk/include/llvm-c/Core.h (original)
+++ llvm/trunk/include/llvm-c/Core.h Thu Feb 18 14:38:32 2016
@@ -1804,6 +1804,13 @@ LLVMValueRef LLVMAddAlias(LLVMModuleRef
void LLVMDeleteFunction(LLVMValueRef Fn);
/**
+ * Check whether the given function has a personality function.
+ *
+ * @see llvm::Function::hasPersonalityFn()
+ */
+LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn);
+
+/**
* Obtain the personality function attached to the function.
*
* @see llvm::Function::getPersonalityFn()
@@ -2469,6 +2476,42 @@ LLVMBool LLVMIsTailCall(LLVMValueRef Cal
void LLVMSetTailCall(LLVMValueRef CallInst, LLVMBool IsTailCall);
/**
+ * Return the normal destination basic block.
+ *
+ * This only works on llvm::InvokeInst instructions.
+ *
+ * @see llvm::InvokeInst::getNormalDest()
+ */
+LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef InvokeInst);
+
+/**
+ * Return the unwind destination basic block.
+ *
+ * This only works on llvm::InvokeInst instructions.
+ *
+ * @see llvm::InvokeInst::getUnwindDest()
+ */
+LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef InvokeInst);
+
+/**
+ * Set the normal destination basic block.
+ *
+ * This only works on llvm::InvokeInst instructions.
+ *
+ * @see llvm::InvokeInst::setNormalDest()
+ */
+void LLVMSetNormalDest(LLVMValueRef InvokeInst, LLVMBasicBlockRef B);
+
+/**
+ * Set the unwind destination basic block.
+ *
+ * This only works on llvm::InvokeInst instructions.
+ *
+ * @see llvm::InvokeInst::setUnwindDest()
+ */
+void LLVMSetUnwindDest(LLVMValueRef InvokeInst, LLVMBasicBlockRef B);
+
+/**
* @}
*/
@@ -2706,9 +2749,18 @@ void LLVMAddCase(LLVMValueRef Switch, LL
/* Add a destination to the indirectbr instruction */
void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest);
+/* Get the number of clauses on the landingpad instruction */
+unsigned LLVMGetNumClauses(LLVMValueRef LandingPad);
+
+/* Get the value of the clause at idnex Idx on the landingpad instruction */
+LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx);
+
/* Add a catch or filter clause to the landingpad instruction */
void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal);
+/* Get the 'cleanup' flag in the landingpad instruction */
+LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad);
+
/* Set the 'cleanup' flag in the landingpad instruction */
void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val);
Modified: llvm/trunk/lib/IR/Core.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Core.cpp?rev=261254&r1=261253&r2=261254&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Core.cpp (original)
+++ llvm/trunk/lib/IR/Core.cpp Thu Feb 18 14:38:32 2016
@@ -1701,6 +1701,10 @@ void LLVMDeleteFunction(LLVMValueRef Fn)
unwrap<Function>(Fn)->eraseFromParent();
}
+LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) {
+ return unwrap<Function>(Fn)->hasPersonalityFn();
+}
+
LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
return wrap(unwrap<Function>(Fn)->getPersonalityFn());
}
@@ -2107,6 +2111,24 @@ void LLVMSetTailCall(LLVMValueRef Call,
unwrap<CallInst>(Call)->setTailCall(isTailCall);
}
+/*--.. Operations on invoke instructions (only) ............................--*/
+
+LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) {
+ return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
+}
+
+LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) {
+ return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
+}
+
+void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
+ unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
+}
+
+void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) {
+ unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
+}
+
/*--.. Operations on terminators ...........................................--*/
unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
@@ -2342,11 +2364,23 @@ void LLVMAddDestination(LLVMValueRef Ind
unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
}
+unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
+ return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
+}
+
+LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
+ return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
+}
+
void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
unwrap<LandingPadInst>(LandingPad)->
addClause(cast<Constant>(unwrap(ClauseVal)));
}
+LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) {
+ return unwrap<LandingPadInst>(LandingPad)->isCleanup();
+}
+
void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
}
Added: llvm/trunk/test/Bindings/llvm-c/invoke.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bindings/llvm-c/invoke.ll?rev=261254&view=auto
==============================================================================
--- llvm/trunk/test/Bindings/llvm-c/invoke.ll (added)
+++ llvm/trunk/test/Bindings/llvm-c/invoke.ll Thu Feb 18 14:38:32 2016
@@ -0,0 +1,82 @@
+; RUN: llvm-as < %s | llvm-dis > %t.orig
+; RUN: llvm-as < %s | llvm-c-test --echo > %t.echo
+; RUN: diff -w %t.orig %t.echo
+
+%C6object9ClassInfo = type { %C6object9ClassInfo__vtbl*, %C6object9ClassInfo* }
+%C6object9ClassInfo__vtbl = type { %C6object9ClassInfo* }
+%C6object9Exception__vtbl = type { %C6object9ClassInfo* }
+%C6object6Object = type { %C6object6Object__vtbl* }
+%C6object6Object__vtbl = type { %C6object9ClassInfo* }
+%C6object9Throwable = type { %C6object9Throwable__vtbl* }
+%C6object9Throwable__vtbl = type { %C6object9ClassInfo* }
+
+ at C6object9ClassInfo__ClassInfo = linkonce_odr constant %C6object9ClassInfo { %C6object9ClassInfo__vtbl* @C6object9ClassInfo__vtblZ, %C6object9ClassInfo* @C6object8TypeInfo__ClassInfo }
+ at C6object9ClassInfo__vtblZ = linkonce_odr constant %C6object9ClassInfo__vtbl { %C6object9ClassInfo* @C6object9ClassInfo__ClassInfo }
+ at C6object8TypeInfo__ClassInfo = linkonce_odr constant %C6object9ClassInfo { %C6object9ClassInfo__vtbl* @C6object9ClassInfo__vtblZ, %C6object9ClassInfo* @C6object6Object__ClassInfo }
+ at C6object6Object__ClassInfo = linkonce_odr constant %C6object9ClassInfo { %C6object9ClassInfo__vtbl* @C6object9ClassInfo__vtblZ, %C6object9ClassInfo* @C6object6Object__ClassInfo }
+ at C6object9Throwable__ClassInfo = linkonce_odr constant %C6object9ClassInfo { %C6object9ClassInfo__vtbl* @C6object9ClassInfo__vtblZ, %C6object9ClassInfo* @C6object6Object__ClassInfo }
+ at C6object9Exception__ClassInfo = linkonce_odr constant %C6object9ClassInfo { %C6object9ClassInfo__vtbl* @C6object9ClassInfo__vtblZ, %C6object9ClassInfo* @C6object9Throwable__ClassInfo }
+ at C6object9Exception__vtblZ = linkonce_odr constant %C6object9Exception__vtbl { %C6object9ClassInfo* @C6object9Exception__ClassInfo }
+ at C6object5Error__ClassInfo = linkonce_odr constant %C6object9ClassInfo { %C6object9ClassInfo__vtbl* @C6object9ClassInfo__vtblZ, %C6object9ClassInfo* @C6object9Throwable__ClassInfo }
+
+define i32 @_D8test01494mainFMZi() personality i32 (i32, i32, i64, i8*, i8*)* @__sd_eh_personality {
+body:
+ %0 = invoke i8* @_d_allocmemory(i64 8)
+ to label %then unwind label %landingPad
+
+then: ; preds = %body
+ %1 = bitcast i8* %0 to i8**
+ store i8* bitcast (%C6object9Exception__vtbl* @C6object9Exception__vtblZ to i8*), i8** %1, align 8
+ %2 = bitcast i8* %0 to %C6object6Object*
+ invoke void @_D6object6Object6__ctorFMC6object6ObjectZv(%C6object6Object* %2)
+ to label %then1 unwind label %landingPad
+
+then1: ; preds = %then
+ %3 = bitcast i8* %0 to %C6object9Throwable*
+ invoke void @__sd_eh_throw(%C6object9Throwable* %3)
+ to label %then2 unwind label %landingPad
+
+then2: ; preds = %then1
+ unreachable
+
+landingPad: ; preds = %then1, %then, %body
+ %4 = landingpad { i8*, i32 }
+ cleanup
+ catch %C6object9ClassInfo* @C6object5Error__ClassInfo
+ catch %C6object9ClassInfo* @C6object9Exception__ClassInfo
+ catch %C6object9ClassInfo* @C6object9Throwable__ClassInfo
+ %5 = extractvalue { i8*, i32 } %4, 1
+ %6 = tail call i32 @llvm.eh.typeid.for(i8* bitcast (%C6object9ClassInfo* @C6object5Error__ClassInfo to i8*))
+ %7 = icmp eq i32 %6, %5
+ br i1 %7, label %catch, label %unwind3
+
+catch: ; preds = %unwind5, %unwind3, %landingPad
+ %merge = phi i32 [ 23, %landingPad ], [ 19, %unwind3 ], [ 13, %unwind5 ]
+ ret i32 %merge
+
+unwind3: ; preds = %landingPad
+ %8 = tail call i32 @llvm.eh.typeid.for(i8* bitcast (%C6object9ClassInfo* @C6object9Exception__ClassInfo to i8*))
+ %9 = icmp eq i32 %8, %5
+ br i1 %9, label %catch, label %unwind5
+
+unwind5: ; preds = %unwind3
+ %10 = tail call i32 @llvm.eh.typeid.for(i8* bitcast (%C6object9ClassInfo* @C6object9Throwable__ClassInfo to i8*))
+ %11 = icmp eq i32 %10, %5
+ br i1 %11, label %catch, label %unwind7
+
+unwind7: ; preds = %unwind5
+ resume { i8*, i32 } %4
+}
+
+declare void @_D6object6Object6__ctorFMC6object6ObjectZv(%C6object6Object*)
+
+declare i8* @_d_allocmemory(i64)
+
+declare i32 @__sd_eh_personality(i32, i32, i64, i8*, i8*)
+
+declare void @__sd_eh_throw(%C6object9Throwable*)
+
+; Function Attrs: nounwind readnone
+declare i32 @llvm.eh.typeid.for(i8*) #0
+
+attributes #0 = { nounwind readnone }
\ No newline at end of file
Modified: llvm/trunk/tools/llvm-c-test/echo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-c-test/echo.cpp?rev=261254&r1=261253&r2=261254&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-c-test/echo.cpp (original)
+++ llvm/trunk/tools/llvm-c-test/echo.cpp Thu Feb 18 14:38:32 2016
@@ -388,8 +388,19 @@ struct FunCloner {
}
case LLVMSwitch:
case LLVMIndirectBr:
- case LLVMInvoke:
break;
+ case LLVMInvoke: {
+ SmallVector<LLVMValueRef, 8> Args;
+ int ArgCount = LLVMGetNumArgOperands(Src);
+ for (int i = 0; i < ArgCount; i++)
+ Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
+ LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
+ LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src));
+ LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src));
+ Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount,
+ Then, Unwind, Name);
+ break;
+ }
case LLVMUnreachable:
Dst = LLVMBuildUnreachable(Builder);
break;
@@ -536,6 +547,20 @@ struct FunCloner {
Args.push_back(CloneValue(LLVMGetOperand(Src, i)));
LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src));
Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name);
+ LLVMSetTailCall(Dst, LLVMIsTailCall(Src));
+ break;
+ }
+ case LLVMResume: {
+ Dst = LLVMBuildResume(Builder, CloneValue(LLVMGetOperand(Src, 0)));
+ break;
+ }
+ case LLVMLandingPad: {
+ // The landing pad API is a bit screwed up for historical reasons.
+ Dst = LLVMBuildLandingPad(Builder, CloneType(Src), nullptr, 0, Name);
+ unsigned NumClauses = LLVMGetNumClauses(Src);
+ for (unsigned i = 0; i < NumClauses; ++i)
+ LLVMAddClause(Dst, CloneValue(LLVMGetClause(Src, i)));
+ LLVMSetCleanup(Dst, LLVMIsCleanup(Src));
break;
}
case LLVMExtractValue: {
@@ -788,6 +813,15 @@ FunClone:
LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
if (!Fun)
report_fatal_error("Function must have been declared already");
+
+ if (LLVMHasPersonalityFn(Cur)) {
+ const char *FName = LLVMGetValueName(LLVMGetPersonalityFn(Cur));
+ LLVMValueRef P = LLVMGetNamedFunction(M, FName);
+ if (!P)
+ report_fatal_error("Could not find personality function");
+ LLVMSetPersonalityFn(Fun, P);
+ }
+
FunCloner FC(Cur, Fun);
FC.CloneBBs(Cur);
More information about the llvm-commits
mailing list