[WinEH] Handle a landingpad, resume, and cleanup all rolled into a BB
[oota-llvm.git] / lib / CodeGen / WinEHPrepare.cpp
index afe26ff8224a9b87487a34e209e51f9ba20cfb65..35b944ea309cb4b6169a1aba4a7cd1d8e0b1bd8f 100644 (file)
@@ -433,6 +433,10 @@ bool WinEHPrepare::prepareExceptionHandlers(
       Invoke->setUnwindDest(NewLPadBB);
     }
 
+    // If anyone is still using the old landingpad value, just give them undef
+    // instead. The eh pointer and selector values are not real.
+    LPad->replaceAllUsesWith(UndefValue::get(LPad->getType()));
+
     // Replace the mapping of any nested landing pad that previously mapped
     // to this landing pad with a referenced to the cloned version.
     for (auto &LPadPair : NestedLPtoOriginalLP) {
@@ -636,6 +640,11 @@ void WinEHPrepare::promoteLandingPadValues(LandingPadInst *LPad) {
     PromoteMemToReg(EHAllocas, *DT);
     EHAllocas.clear();
   }
+
+  // After promotion, some extracts may be trivially dead. Remove them.
+  SmallVector<Value *, 4> Users(LPad->user_begin(), LPad->user_end());
+  for (auto *U : Users)
+    RecursivelyDeleteTriviallyDeadInstructions(U);
 }
 
 void WinEHPrepare::completeNestedLandingPad(Function *ParentFn,
@@ -1573,11 +1582,10 @@ void WinEHPrepare::findCleanupHandlers(LandingPadActions &Actions,
       InsertValueInst *Insert1 = nullptr;
       InsertValueInst *Insert2 = nullptr;
       Value *ResumeVal = Resume->getOperand(0);
-      // If there is only one landingpad, we may use the lpad directly with no
-      // insertions.
-      if (isa<LandingPadInst>(ResumeVal))
-        return;
-      if (!isa<PHINode>(ResumeVal)) {
+      // If the resume value isn't a phi or landingpad value, it should be a
+      // series of insertions. Identify them so we can avoid them when scanning
+      // for cleanups.
+      if (!isa<PHINode>(ResumeVal) && !isa<LandingPadInst>(ResumeVal)) {
         Insert2 = dyn_cast<InsertValueInst>(ResumeVal);
         if (!Insert2)
           return createCleanupHandler(Actions, CleanupHandlerMap, BB);
@@ -1693,7 +1701,6 @@ void WinEHPrepare::findCleanupHandlers(LandingPadActions &Actions,
       return;
     BB = Branch->getSuccessor(0);
   }
-  return;
 }
 
 // This is a public function, declared in WinEHFuncInfo.h and is also