WinEH: Remove vestigial EH object
authorReid Kleckner <reid@kleckner.net>
Tue, 3 Mar 2015 23:20:30 +0000 (23:20 +0000)
committerReid Kleckner <reid@kleckner.net>
Tue, 3 Mar 2015 23:20:30 +0000 (23:20 +0000)
Ultimately, we'll need to leave something behind to indicate which
alloca will hold the exception, but we can figure that out when it comes
time to emit the __CxxFrameHandler3 catch handler table.

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

lib/CodeGen/WinEHPrepare.cpp
test/CodeGen/WinEH/cppeh-catch-all.ll
test/CodeGen/WinEH/cppeh-catch-scalar.ll
test/CodeGen/WinEH/cppeh-frame-vars.ll
test/CodeGen/WinEH/cppeh-inalloca.ll
test/CodeGen/WinEH/cppeh-nonalloca-frame-values.ll

index 68498a5aade863d0bfe5e83c89463170b4a0d01a..148c0a4217588c013234787cf558587f7225c296 100644 (file)
@@ -73,8 +73,7 @@ private:
                             SmallVectorImpl<LandingPadInst *> &LPads);
   bool outlineHandler(HandlerType CatchOrCleanup, Function *SrcFn,
                       Constant *SelectorType, LandingPadInst *LPad,
-                      CallInst *&EHAlloc, AllocaInst *&EHObjPtr,
-                      FrameVarInfoMap &VarInfo);
+                      CallInst *&EHAlloc, FrameVarInfoMap &VarInfo);
 };
 
 class WinEHFrameVariableMaterializer : public ValueMaterializer {
@@ -132,9 +131,9 @@ protected:
 class WinEHCatchDirector : public WinEHCloningDirectorBase {
 public:
   WinEHCatchDirector(LandingPadInst *LPI, Function *CatchFn, Value *Selector,
-                     Value *EHObj, FrameVarInfoMap &VarInfo)
+                     FrameVarInfoMap &VarInfo)
       : WinEHCloningDirectorBase(LPI, CatchFn, VarInfo),
-        CurrentSelector(Selector->stripPointerCasts()), EHObj(EHObj) {}
+        CurrentSelector(Selector->stripPointerCasts()) {}
 
   CloningAction handleBeginCatch(ValueToValueMapTy &VMap,
                                  const Instruction *Inst,
@@ -149,7 +148,6 @@ public:
 
 private:
   Value *CurrentSelector;
-  Value *EHObj;
 };
 
 class WinEHCleanupDirector : public WinEHCloningDirectorBase {
@@ -239,7 +237,6 @@ bool WinEHPrepare::prepareCPPEHHandlers(
   // handlers are outlined.
   FrameVarInfoMap FrameVarInfo;
   SmallVector<CallInst *, 4> HandlerAllocs;
-  SmallVector<AllocaInst *, 4> HandlerEHObjPtrs;
 
   bool HandlersOutlined = false;
 
@@ -267,17 +264,14 @@ bool WinEHPrepare::prepareCPPEHHandlers(
         // Create a new instance of the handler data structure in the
         // HandlerData vector.
         CallInst *EHAlloc = nullptr;
-        AllocaInst *EHObjPtr = nullptr;
         bool Outlined = outlineHandler(Catch, &F, LPad->getClause(Idx), LPad,
-                                       EHAlloc, EHObjPtr, FrameVarInfo);
+                                       EHAlloc, FrameVarInfo);
         if (Outlined) {
           HandlersOutlined = true;
           // These values must be resolved after all handlers have been
           // outlined.
           if (EHAlloc)
             HandlerAllocs.push_back(EHAlloc);
-          if (EHObjPtr)
-            HandlerEHObjPtrs.push_back(EHObjPtr);
         }
       } // End if (isCatch)
     }   // End for each clause
@@ -290,9 +284,8 @@ bool WinEHPrepare::prepareCPPEHHandlers(
     //        when landing pad block analysis is added.
     if (LPad->isCleanup()) {
       CallInst *EHAlloc = nullptr;
-      AllocaInst *IgnoreEHObjPtr = nullptr;
-      bool Outlined = outlineHandler(Cleanup, &F, nullptr, LPad, EHAlloc,
-                                     IgnoreEHObjPtr, FrameVarInfo);
+      bool Outlined =
+          outlineHandler(Cleanup, &F, nullptr, LPad, EHAlloc, FrameVarInfo);
       if (Outlined) {
         HandlersOutlined = true;
         // This value must be resolved after all handlers have been outlined.
@@ -399,18 +392,6 @@ bool WinEHPrepare::prepareCPPEHHandlers(
     EHDataMap[EHAlloc->getParent()->getParent()] = EHData;
   }
 
-  // Next, replace the place-holder EHObjPtr allocas with GEP instructions
-  // that pull the EHObjPtr from the frame alloc structure
-  for (AllocaInst *EHObjPtr : HandlerEHObjPtrs) {
-    Value *EHData = EHDataMap[EHObjPtr->getParent()->getParent()];
-    Builder.SetInsertPoint(EHObjPtr);
-    Value *ElementPtr = Builder.CreateConstInBoundsGEP2_32(EHData, 0, 1);
-    EHObjPtr->replaceAllUsesWith(ElementPtr);
-    EHObjPtr->removeFromParent();
-    ElementPtr->takeName(EHObjPtr);
-    delete EHObjPtr;
-  }
-
   // Finally, replace all of the temporary allocas for frame variables used in
   // the outlined handlers and the original frame allocas with GEP instructions
   // that get the equivalent pointer from the frame allocation struct.
@@ -490,7 +471,7 @@ bool WinEHPrepare::prepareCPPEHHandlers(
 
 bool WinEHPrepare::outlineHandler(HandlerType CatchOrCleanup, Function *SrcFn,
                                   Constant *SelectorType, LandingPadInst *LPad,
-                                  CallInst *&EHAlloc, AllocaInst *&EHObjPtr,
+                                  CallInst *&EHAlloc,
                                   FrameVarInfoMap &VarInfo) {
   Module *M = SrcFn->getParent();
   LLVMContext &Context = M->getContext();
@@ -539,20 +520,8 @@ bool WinEHPrepare::outlineHandler(HandlerType CatchOrCleanup, Function *SrcFn,
   std::unique_ptr<WinEHCloningDirectorBase> Director;
 
   if (CatchOrCleanup == Catch) {
-    // This alloca is only temporary.  We'll be replacing it once we know all
-    // the frame variables that need to go in the frame allocation structure.
-    EHObjPtr = Builder.CreateAlloca(Int8PtrType, 0, "eh.obj.ptr");
-
-    // This will give us a raw pointer to the exception object, which
-    // corresponds to the formal parameter of the catch statement.  If the
-    // handler uses this object, we will generate code during the outlining
-    // process to cast the pointer to the appropriate type and deference it
-    // as necessary.  The un-outlined landing pad code represents the
-    // exception object as the result of the llvm.eh.begincatch call.
-    Value *EHObj = Builder.CreateLoad(EHObjPtr, false, "eh.obj");
-
     Director.reset(
-        new WinEHCatchDirector(LPad, Handler, SelectorType, EHObj, VarInfo));
+        new WinEHCatchDirector(LPad, Handler, SelectorType, VarInfo));
   } else {
     Director.reset(new WinEHCleanupDirector(LPad, Handler, VarInfo));
   }
@@ -668,10 +637,11 @@ CloningDirector::CloningAction WinEHCatchDirector::handleBeginCatch(
   // The argument to the call is some form of the first element of the
   // landingpad aggregate value, but that doesn't matter.  It isn't used
   // here.
-  // The return value of this instruction, however, is used to access the
-  // EH object pointer.  We have generated an instruction to get that value
-  // from the EH alloc block, so we can just map to that here.
-  VMap[Inst] = EHObj;
+  // The second argument is an outparameter where the exception object will be
+  // stored. Typically the exception object is a scalar, but it can be an
+  // aggregate when catching by value.
+  // FIXME: Leave something behind to indicate where the exception object lives
+  // for this handler. Should it be part of llvm.eh.actions?
   return CloningDirector::SkipInstruction;
 }
 
index c8d54aca7e3160e1278e7d92e3ce396feffe80e7..0958d745250c0e874ad96dca662dd182915a6091 100644 (file)
@@ -56,8 +56,6 @@ try.cont:                                         ; preds = %invoke.cont2, %invo
 ; CHECK: entry:
 ; CHECK:   %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1)
 ; CHECK:   %eh.data = bitcast i8* %eh.alloc to %struct._Z4testv.ehdata*
-; CHECK:   %eh.obj.ptr = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 1
-; CHECK:   %eh.obj = load i8*, i8** %eh.obj.ptr
 ; CHECK:   call void @_Z16handle_exceptionv()
 ; CHECK:   ret i8* blockaddress(@_Z4testv, %try.cont)
 ; CHECK: }
index ce3f1b52f3119ead2eda18abd7d7a265ffb20df6..7ed846eaa43e41800b64b88e14883d9884ad9e27 100644 (file)
@@ -87,8 +87,6 @@ eh.resume:                                        ; preds = %catch.dispatch
 ; CHECK: entry:
 ; CHECK:   %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1)
 ; CHECK:   %eh.data = bitcast i8* %eh.alloc to %struct._Z4testv.ehdata*
-; CHECK:   %eh.obj.ptr = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 1
-; CHECK:   %eh.obj = load i8*, i8** %eh.obj.ptr
 ; CHECK:   %i = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 2
 ; CHECK:   %tmp7 = load i32, i32* %i, align 4
 ; CHECK:   call void @_Z10handle_inti(i32 %tmp7)
index 6d6644084b9dc2c3c1d871319a92f2c17cedaf19..4aafad0a9e8e8d3324684e3a9070657eaddbe29b 100644 (file)
@@ -181,8 +181,6 @@ eh.resume:                                        ; preds = %catch.dispatch
 ; CHECK: entry:
 ; CHECK:   %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1)
 ; CHECK:   %eh.data = bitcast i8* %eh.alloc to %"struct.\01?test@@YAXXZ.ehdata"*
-; CHECK:   %eh.obj.ptr = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 1
-; CHECK:   %eh.obj = load i8*, i8** %eh.obj.ptr
 ; CHECK:   %e = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 2
 ; CHECK:   %NumExceptions = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 3
 ; CHECK:   %ExceptionVal = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 4
index 72175a6e7859d4c0eaf708d0dd8f54bd9a2e496b..13f3e6c9660f98803dae8798b54c62f9d20a1380 100644 (file)
@@ -134,8 +134,6 @@ eh.resume:                                        ; preds = %ehcleanup
 ; CHECK: entry:
 ; CHECK:   %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1)
 ; CHECK:   %eh.data = bitcast i8* %eh.alloc to %"struct.\01?test@@YAHUA@@@Z.ehdata"*
-; CHECK:   %eh.obj.ptr = getelementptr inbounds %"struct.\01?test@@YAHUA@@@Z.ehdata", %"struct.\01?test@@YAHUA@@@Z.ehdata"* %eh.data, i32 0, i32 1
-; CHECK:   %eh.obj = load i8*, i8** %eh.obj.ptr
 ; CHECK:   %e = getelementptr inbounds %"struct.\01?test@@YAHUA@@@Z.ehdata", %"struct.\01?test@@YAHUA@@@Z.ehdata"* %eh.data, i32 0, i32 2
 ; CHECK:   %eh.temp.alloca = getelementptr inbounds %"struct.\01?test@@YAHUA@@@Z.ehdata", %"struct.\01?test@@YAHUA@@@Z.ehdata"* %eh.data, i32 0, i32 3
 ; CHECK:   %.reload = load <{ %struct.A }>*, <{ %struct.A }>** %eh.temp.alloca
index 1239f267bac8a3c55e68b09f1085c4ffea143992..354f40954864493c4a41d91c33bbf2a6cc12e066 100644 (file)
@@ -189,8 +189,6 @@ eh.resume:                                        ; preds = %lpad
 ; CHECK: entry:
 ; CHECK:   %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1)
 ; CHECK:   %eh.data = bitcast i8* %eh.alloc to %"struct.\01?test@@YAXXZ.ehdata"*
-; CHECK:   %eh.obj.ptr = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 1
-; CHECK:   %eh.obj = load i8*, i8** %eh.obj.ptr
 ; CHECK:   %e = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 2
 ; CHECK:   %eh.temp.alloca = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 3
 ; CHECK:   %NumExceptions.020.reload = load i32, i32* %eh.temp.alloca