WinEH: Create an unwind help alloca for __CxxFrameHandler3 xdata tables
authorReid Kleckner <reid@kleckner.net>
Wed, 25 Mar 2015 20:10:36 +0000 (20:10 +0000)
committerReid Kleckner <reid@kleckner.net>
Wed, 25 Mar 2015 20:10:36 +0000 (20:10 +0000)
We don't have any logic to emit those tables yet, so the sdag lowering
of this intrinsic is just a stub. We can see the intrinsic in the
prepared IR, though.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@233209 91177308-0d34-0410-b5e6-96231b3b80d8

docs/ExceptionHandling.rst
include/llvm/IR/Intrinsics.td
lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
lib/CodeGen/WinEHPrepare.cpp
lib/IR/Verifier.cpp
test/CodeGen/WinEH/cppeh-catch-unwind.ll

index ee157d096a22e1f38110edc80e5c0a5a33c99d8e..21de19b0752c7bbf3983cb966a9a8c1ab7e86eea 100644 (file)
@@ -539,6 +539,18 @@ In order to preserve the structure of the CFG, a call to '``llvm.eh.actions``'
 must be followed by an ':ref:`indirectbr <i_indirectbr>`' instruction that jumps
 to the result of the intrinsic call.
 
+``llvm.eh.unwindhelp``
+----------------------
+
+.. code-block:: llvm
+
+  void @llvm.eh.unwindhelp(i8*)
+
+This intrinsic designates the provided static alloca as the unwind help object.
+This object is used by Windows native exception handling on non-x86 platforms
+where xdata unwind information is used. It is typically an 8 byte chunk of
+memory treated as two 32-bit integers.
+
 
 SJLJ Intrinsics
 ---------------
index 110d6b22e8ae0b7070b3de415c0d4b1abf750d12..da9d8cb61f52536c9a0f730a33bbcc2b10fda503 100644 (file)
@@ -421,6 +421,10 @@ def int_eh_endcatch : Intrinsic<[], []>;
 // Represents the list of actions to take when an exception is thrown.
 def int_eh_actions : Intrinsic<[llvm_ptr_ty], [llvm_vararg_ty], []>;
 
+// Designates the provided static alloca as the unwind help object. Required
+// for WinEH.
+def int_eh_unwindhelp : Intrinsic<[], [llvm_ptr_ty], []>;
+
 // __builtin_unwind_init is an undocumented GCC intrinsic that causes all
 // callee-saved registers to be saved and restored (regardless of whether they
 // are used) in the calling function. It is used by libgcc_eh.
index 6f5ea7c746910affa72f6bdc51f24e1b1444d70c..6c14e7969c54666424cc2e7a7017821c94b2e219 100644 (file)
@@ -5446,6 +5446,16 @@ SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
   case Intrinsic::eh_begincatch:
   case Intrinsic::eh_endcatch:
     llvm_unreachable("begin/end catch intrinsics not lowered in codegen");
+  case Intrinsic::eh_unwindhelp: {
+    AllocaInst *Slot =
+        cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
+    assert(FuncInfo.StaticAllocaMap.count(Slot) &&
+           "can only use static allocas with llvm.eh.unwindhelp");
+    int FI = FuncInfo.StaticAllocaMap[Slot];
+    // TODO: Save this in the not-yet-existant WinEHFuncInfo struct.
+    (void)FI;
+    return nullptr;
+  }
   }
 }
 
index d1b385579b9f1b1dcd42e257479e7970e0508faa..ab0f96ef05fb17d63c45193da3b3f83c09711ef3 100644 (file)
@@ -601,6 +601,19 @@ bool WinEHPrepare::prepareExceptionHandlers(
   Builder.SetInsertPoint(&F.getEntryBlock().back());
   Builder.CreateCall(FrameEscapeFn, AllocasToEscape);
 
+  // Insert an alloca for the EH state in the entry block. On x86, we will also
+  // insert stores to update the EH state, but on other ISAs, the runtime does
+  // it for us.
+  // FIXME: This record is different on x86.
+  Type *UnwindHelpTy = Type::getInt64Ty(Context);
+  AllocaInst *UnwindHelp =
+      new AllocaInst(UnwindHelpTy, "unwindhelp", &F.getEntryBlock().front());
+  Builder.CreateStore(llvm::ConstantInt::get(UnwindHelpTy, -2), UnwindHelp);
+  Function *UnwindHelpFn =
+      Intrinsic::getDeclaration(M, Intrinsic::eh_unwindhelp);
+  Builder.CreateCall(UnwindHelpFn,
+                     Builder.CreateBitCast(UnwindHelp, Int8PtrType));
+
   // Clean up the handler action maps we created for this function
   DeleteContainerSeconds(CatchHandlerMap);
   CatchHandlerMap.clear();
index 5f25891f2b1ce47403813e040a56ff231176e769..ee3582d00ca53f5846515176efb0a18c529225c7 100644 (file)
@@ -2908,6 +2908,13 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
     break;
   }
 
+  case Intrinsic::eh_unwindhelp: {
+    auto *AI = dyn_cast<AllocaInst>(CI.getArgOperand(0)->stripPointerCasts());
+    Assert(AI && AI->isStaticAlloca(),
+           "llvm.eh.unwindhelp requires a static alloca", &CI);
+    break;
+  }
+
   case Intrinsic::experimental_gc_statepoint:
     Assert(!CI.isInlineAsm(),
            "gc.statepoint support for inline assembly unimplemented", &CI);
index 011f2df96ed6341bd6277b6ca3785c455fb1dab6..3db1635a110c8dc7daa813082952c78de646578d 100644 (file)
@@ -33,10 +33,13 @@ $"\01??_R0H@8" = comdat any
 
 ; CHECK-LABEL: define void @"\01?test@@YAXXZ"() #0 {
 ; CHECK: entry:
+; CHECK:   [[UNWIND_HELP:\%.+]] = alloca i64
 ; CHECK:   [[OBJ_PTR:\%.+]] = alloca %class.SomeClass
 ; CHECK:   [[TMP0:\%.+]] = alloca i32, align 4
 ; CHECK:   [[TMP1:\%.+]] = alloca i32, align 4
 ; CHECK:   call void (...)* @llvm.frameescape(i32* [[TMP1]], %class.SomeClass* [[OBJ_PTR]], i32* [[TMP0]])
+; CHECK:   [[UNWIND_HELP_i8:\%.+]] = bitcast i64* [[UNWIND_HELP]] to i8*
+; CHECK:   call void @llvm.eh.unwindhelp(i8* [[UNWIND_HELP_i8]])
 ; CHECK:   %call = invoke %class.SomeClass* @"\01??0SomeClass@@QEAA@XZ"(%class.SomeClass* %obj)
 ; CHECK:           to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]+]]