<div dir="ltr">Reverted in r229952, didn't pass 'ninja check' on linux for me. Let me know if this doesn't reproduce easily, but I think it will. Lots of stack traces that look like heap corruption.</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Feb 19, 2015 at 5:00 PM, Reid Kleckner <span dir="ltr"><<a href="mailto:reid@kleckner.net" target="_blank">reid@kleckner.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: rnk<br>
Date: Thu Feb 19 19:00:19 2015<br>
New Revision: 229944<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=229944&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=229944&view=rev</a><br>
Log:<br>
EH: Prune unreachable resume instructions during Dwarf EH preparation<br>
<br>
Today a simple function that only catches exceptions and doesn't run<br>
destructor cleanups ends up containing a dead call to _Unwind_Resume<br>
(PR20300). We can't remove these dead resume instructions during normal<br>
optimization because inlining might introduce additional landingpads<br>
that do have cleanups to run. Instead we can do this during EH<br>
preparation, which is guaranteed to run after inlining.<br>
<br>
Fixes PR20300.<br>
<br>
Reviewers: majnemer<br>
<br>
Differential Revision: <a href="http://reviews.llvm.org/D7744" target="_blank">http://reviews.llvm.org/D7744</a><br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/CodeGen/Passes.h<br>
    llvm/trunk/lib/CodeGen/CodeGen.cpp<br>
    llvm/trunk/lib/CodeGen/DwarfEHPrepare.cpp<br>
    llvm/trunk/lib/CodeGen/WinEHPrepare.cpp<br>
    llvm/trunk/test/CodeGen/Mips/eh.ll<br>
    llvm/trunk/test/CodeGen/X86/dwarf-eh-prepare.ll<br>
    llvm/trunk/test/CodeGen/X86/gcc_except_table.ll<br>
    llvm/trunk/test/CodeGen/X86/gcc_except_table_functions.ll<br>
    llvm/trunk/test/CodeGen/XCore/exception.ll<br>
<br>
Modified: llvm/trunk/include/llvm/CodeGen/Passes.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/Passes.h?rev=229944&r1=229943&r2=229944&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/Passes.h?rev=229944&r1=229943&r2=229944&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/CodeGen/Passes.h (original)<br>
+++ llvm/trunk/include/llvm/CodeGen/Passes.h Thu Feb 19 19:00:19 2015<br>
@@ -612,13 +612,13 @@ namespace llvm {<br>
   ModulePass *createForwardControlFlowIntegrityPass();<br>
 } // End llvm namespace<br>
<br>
-/// This initializer registers TargetMachine constructor, so the pass being<br>
-/// initialized can use target dependent interfaces. Please do not move this<br>
-/// macro to be together with INITIALIZE_PASS, which is a complete target<br>
-/// independent initializer, and we don't want to make libScalarOpts depend<br>
-/// on libCodeGen.<br>
-#define INITIALIZE_TM_PASS(passName, arg, name, cfg, analysis) \<br>
-  static void* initialize##passName##PassOnce(PassRegistry &Registry) { \<br>
+/// Target machine pass initializer for passes with dependencies. Use with<br>
+/// INITIALIZE_TM_PASS_END.<br>
+#define INITIALIZE_TM_PASS_BEGIN INITIALIZE_PASS_BEGIN<br>
+<br>
+/// Target machine pass initializer for passes with dependencies. Use with<br>
+/// INITIALIZE_TM_PASS_BEGIN.<br>
+#define INITIALIZE_TM_PASS_END(passName, arg, name, cfg, analysis) \<br>
     PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \<br>
       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis, \<br>
       PassInfo::TargetMachineCtor_t(callTargetMachineCtor< passName >)); \<br>
@@ -629,4 +629,13 @@ namespace llvm {<br>
     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \<br>
   }<br>
<br>
+/// This initializer registers TargetMachine constructor, so the pass being<br>
+/// initialized can use target dependent interfaces. Please do not move this<br>
+/// macro to be together with INITIALIZE_PASS, which is a complete target<br>
+/// independent initializer, and we don't want to make libScalarOpts depend<br>
+/// on libCodeGen.<br>
+#define INITIALIZE_TM_PASS(passName, arg, name, cfg, analysis) \<br>
+    INITIALIZE_TM_PASS_BEGIN(passName, arg, name, cfg, analysis) \<br>
+    INITIALIZE_TM_PASS_END(passName, arg, name, cfg, analysis)<br>
+<br>
 #endif<br>
<br>
Modified: llvm/trunk/lib/CodeGen/CodeGen.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/CodeGen.cpp?rev=229944&r1=229943&r2=229944&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/CodeGen.cpp?rev=229944&r1=229943&r2=229944&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/CodeGen/CodeGen.cpp (original)<br>
+++ llvm/trunk/lib/CodeGen/CodeGen.cpp Thu Feb 19 19:00:19 2015<br>
@@ -24,9 +24,10 @@ void llvm::initializeCodeGen(PassRegistr<br>
   initializeBranchFolderPassPass(Registry);<br>
   initializeCodeGenPreparePass(Registry);<br>
   initializeDeadMachineInstructionElimPass(Registry);<br>
+  initializeDwarfEHPreparePass(Registry);<br>
   initializeEarlyIfConverterPass(Registry);<br>
-  initializeExpandPostRAPass(Registry);<br>
   initializeExpandISelPseudosPass(Registry);<br>
+  initializeExpandPostRAPass(Registry);<br>
   initializeFinalizeMachineBundlesPass(Registry);<br>
   initializeGCMachineCodeAnalysisPass(Registry);<br>
   initializeGCModuleInfoPass(Registry);<br>
@@ -36,31 +37,34 @@ void llvm::initializeCodeGen(PassRegistr<br>
   initializeLiveStacksPass(Registry);<br>
   initializeLiveVariablesPass(Registry);<br>
   initializeLocalStackSlotPassPass(Registry);<br>
+  initializeLowerIntrinsicsPass(Registry);<br>
   initializeMachineBlockFrequencyInfoPass(Registry);<br>
   initializeMachineBlockPlacementPass(Registry);<br>
   initializeMachineBlockPlacementStatsPass(Registry);<br>
-  initializeMachineCopyPropagationPass(Registry);<br>
-  initializeMachineCombinerPass(Registry);<br>
   initializeMachineCSEPass(Registry);<br>
+  initializeMachineCombinerPass(Registry);<br>
+  initializeMachineCopyPropagationPass(Registry);<br>
   initializeMachineDominatorTreePass(Registry);<br>
-  initializeMachinePostDominatorTreePass(Registry);<br>
+  initializeMachineFunctionPrinterPassPass(Registry);<br>
   initializeMachineLICMPass(Registry);<br>
   initializeMachineLoopInfoPass(Registry);<br>
   initializeMachineModuleInfoPass(Registry);<br>
+  initializeMachinePostDominatorTreePass(Registry);<br>
   initializeMachineSchedulerPass(Registry);<br>
   initializeMachineSinkingPass(Registry);<br>
   initializeMachineVerifierPassPass(Registry);<br>
   initializeOptimizePHIsPass(Registry);<br>
+  initializePEIPass(Registry);<br>
   initializePHIEliminationPass(Registry);<br>
   initializePeepholeOptimizerPass(Registry);<br>
   initializePostMachineSchedulerPass(Registry);<br>
   initializePostRASchedulerPass(Registry);<br>
   initializeProcessImplicitDefsPass(Registry);<br>
-  initializePEIPass(Registry);<br>
   initializeRegisterCoalescerPass(Registry);<br>
   initializeSlotIndexesPass(Registry);<br>
-  initializeStackProtectorPass(Registry);<br>
   initializeStackColoringPass(Registry);<br>
+  initializeStackMapLivenessPass(Registry);<br>
+  initializeStackProtectorPass(Registry);<br>
   initializeStackSlotColoringPass(Registry);<br>
   initializeTailDuplicatePassPass(Registry);<br>
   initializeTargetPassConfigPass(Registry);<br>
@@ -70,9 +74,7 @@ void llvm::initializeCodeGen(PassRegistr<br>
   initializeUnreachableMachineBlockElimPass(Registry);<br>
   initializeVirtRegMapPass(Registry);<br>
   initializeVirtRegRewriterPass(Registry);<br>
-  initializeLowerIntrinsicsPass(Registry);<br>
-  initializeMachineFunctionPrinterPassPass(Registry);<br>
-  initializeStackMapLivenessPass(Registry);<br>
+  initializeWinEHPreparePass(Registry);<br>
 }<br>
<br>
 void LLVMInitializeCodeGen(LLVMPassRegistryRef R) {<br>
<br>
Modified: llvm/trunk/lib/CodeGen/DwarfEHPrepare.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/DwarfEHPrepare.cpp?rev=229944&r1=229943&r2=229944&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/DwarfEHPrepare.cpp?rev=229944&r1=229943&r2=229944&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/CodeGen/DwarfEHPrepare.cpp (original)<br>
+++ llvm/trunk/lib/CodeGen/DwarfEHPrepare.cpp Thu Feb 19 19:00:19 2015<br>
@@ -13,13 +13,18 @@<br>
 //===----------------------------------------------------------------------===//<br>
<br>
 #include "llvm/CodeGen/Passes.h"<br>
+#include "llvm/ADT/BitVector.h"<br>
 #include "llvm/ADT/Statistic.h"<br>
+#include "llvm/Analysis/CFG.h"<br>
+#include "llvm/Analysis/TargetTransformInfo.h"<br>
+#include "llvm/IR/Dominators.h"<br>
 #include "llvm/IR/Function.h"<br>
 #include "llvm/IR/Instructions.h"<br>
 #include "llvm/IR/Module.h"<br>
 #include "llvm/Pass.h"<br>
 #include "llvm/Target/TargetLowering.h"<br>
 #include "llvm/Target/TargetSubtargetInfo.h"<br>
+#include "llvm/Transforms/Utils/Local.h"<br>
 using namespace llvm;<br>
<br>
 #define DEBUG_TYPE "dwarfehprepare"<br>
@@ -33,18 +38,28 @@ namespace {<br>
     // RewindFunction - _Unwind_Resume or the target equivalent.<br>
     Constant *RewindFunction;<br>
<br>
+    DominatorTree *DT;<br>
+    const TargetLowering *TLI;<br>
+<br>
     bool InsertUnwindResumeCalls(Function &Fn);<br>
     Value *GetExceptionObject(ResumeInst *RI);<br>
+    size_t<br>
+    pruneUnreachableResumes(Function &Fn,<br>
+                            SmallVectorImpl<ResumeInst *> &Resumes,<br>
+                            SmallVectorImpl<LandingPadInst *> &CleanupLPads);<br>
<br>
   public:<br>
     static char ID; // Pass identification, replacement for typeid.<br>
<br>
     // INITIALIZE_TM_PASS requires a default constructor, but it isn't used in<br>
     // practice.<br>
-    DwarfEHPrepare() : FunctionPass(ID), TM(nullptr), RewindFunction(nullptr) {}<br>
+    DwarfEHPrepare()<br>
+        : FunctionPass(ID), TM(nullptr), RewindFunction(nullptr), DT(nullptr),<br>
+          TLI(nullptr) {}<br>
<br>
     DwarfEHPrepare(const TargetMachine *TM)<br>
-        : FunctionPass(ID), TM(TM), RewindFunction(nullptr) {}<br>
+        : FunctionPass(ID), TM(TM), RewindFunction(nullptr), DT(nullptr),<br>
+          TLI(nullptr) {}<br>
<br>
     bool runOnFunction(Function &Fn) override;<br>
<br>
@@ -53,6 +68,8 @@ namespace {<br>
       return false;<br>
     }<br>
<br>
+    void getAnalysisUsage(AnalysisUsage &AU) const override;<br>
+<br>
     const char *getPassName() const override {<br>
       return "Exception handling preparation";<br>
     }<br>
@@ -60,13 +77,22 @@ namespace {<br>
 } // end anonymous namespace<br>
<br>
 char DwarfEHPrepare::ID = 0;<br>
-INITIALIZE_TM_PASS(DwarfEHPrepare, "dwarfehprepare", "Prepare DWARF exceptions",<br>
-                   false, false)<br>
+INITIALIZE_TM_PASS_BEGIN(DwarfEHPrepare, "dwarfehprepare",<br>
+                         "Prepare DWARF exceptions", false, false)<br>
+INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)<br>
+INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)<br>
+INITIALIZE_TM_PASS_END(DwarfEHPrepare, "dwarfehprepare",<br>
+                       "Prepare DWARF exceptions", false, false)<br>
<br>
 FunctionPass *llvm::createDwarfEHPass(const TargetMachine *TM) {<br>
   return new DwarfEHPrepare(TM);<br>
 }<br>
<br>
+void DwarfEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {<br>
+  AU.addRequired<TargetTransformInfoWrapperPass>();<br>
+  AU.addRequired<DominatorTreeWrapperPass>();<br>
+}<br>
+<br>
 /// GetExceptionObject - Return the exception object from the value passed into<br>
 /// the 'resume' instruction (typically an aggregate). Clean up any dead<br>
 /// instructions, including the 'resume' instruction.<br>
@@ -107,21 +133,71 @@ Value *DwarfEHPrepare::GetExceptionObjec<br>
   return ExnObj;<br>
 }<br>
<br>
+/// Replace resumes that are not reachable from a cleanup landing pad with<br>
+/// unreachable and then simplify those blocks.<br>
+size_t DwarfEHPrepare::pruneUnreachableResumes(<br>
+    Function &Fn, SmallVectorImpl<ResumeInst *> &Resumes,<br>
+    SmallVectorImpl<LandingPadInst *> &CleanupLPads) {<br>
+  BitVector ResumeReachable(Resumes.size());<br>
+  size_t ResumeIndex = 0;<br>
+  for (auto *RI : Resumes) {<br>
+    for (auto *LP : CleanupLPads) {<br>
+      if (isPotentiallyReachable(LP, RI, DT)) {<br>
+        ResumeReachable.set(ResumeIndex);<br>
+        break;<br>
+      }<br>
+    }<br>
+    ++ResumeIndex;<br>
+  }<br>
+<br>
+  // If everything is reachable, there is no change.<br>
+  if (ResumeReachable.all())<br>
+    return Resumes.size();<br>
+<br>
+  const TargetTransformInfo &TTI =<br>
+      getAnalysis<TargetTransformInfoWrapperPass>().getTTI(Fn);<br>
+  LLVMContext &Ctx = Fn.getContext();<br>
+<br>
+  // Otherwise, insert unreachable instructions and call simplifycfg.<br>
+  size_t ResumesLeft = 0;<br>
+  for (size_t I = 0, E = Resumes.size(); I < E; ++I) {<br>
+    ResumeInst *RI = Resumes[I];<br>
+    if (ResumeReachable[I]) {<br>
+      Resumes[ResumesLeft++] = RI;<br>
+    } else {<br>
+      BasicBlock *BB = RI->getParent();<br>
+      new UnreachableInst(Ctx, RI);<br>
+      RI->eraseFromParent();<br>
+      SimplifyCFG(BB, TTI, 1, TLI->getDataLayout());<br>
+    }<br>
+  }<br>
+  Resumes.resize(ResumesLeft);<br>
+  return ResumesLeft;<br>
+}<br>
+<br>
 /// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present<br>
 /// into calls to the appropriate _Unwind_Resume function.<br>
 bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {<br>
   SmallVector<ResumeInst*, 16> Resumes;<br>
+  SmallVector<LandingPadInst*, 16> CleanupLPads;<br>
   for (BasicBlock &BB : Fn) {<br>
     if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator()))<br>
       Resumes.push_back(RI);<br>
+    if (auto *LP = BB.getLandingPadInst())<br>
+      if (LP->isCleanup())<br>
+        CleanupLPads.push_back(LP);<br>
   }<br>
<br>
   if (Resumes.empty())<br>
     return false;<br>
<br>
-  // Find the rewind function if we didn't already.<br>
-  const TargetLowering *TLI = TM->getSubtargetImpl(Fn)->getTargetLowering();<br>
   LLVMContext &Ctx = Fn.getContext();<br>
+<br>
+  size_t ResumesLeft = pruneUnreachableResumes(Fn, Resumes, CleanupLPads);<br>
+  if (ResumesLeft == 0)<br>
+    return true; // We pruned them all.<br>
+<br>
+  // Find the rewind function if we didn't already.<br>
   if (!RewindFunction) {<br>
     FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),<br>
                                           Type::getInt8PtrTy(Ctx), false);<br>
@@ -130,9 +206,7 @@ bool DwarfEHPrepare::InsertUnwindResumeC<br>
   }<br>
<br>
   // Create the basic block where the _Unwind_Resume call will live.<br>
-  unsigned ResumesSize = Resumes.size();<br>
-<br>
-  if (ResumesSize == 1) {<br>
+  if (ResumesLeft == 1) {<br>
     // Instead of creating a new BB and PHI node, just append the call to<br>
     // _Unwind_Resume to the end of the single resume block.<br>
     ResumeInst *RI = Resumes.front();<br>
@@ -149,7 +223,7 @@ bool DwarfEHPrepare::InsertUnwindResumeC<br>
   }<br>
<br>
   BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);<br>
-  PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesSize,<br>
+  PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesLeft,<br>
                                 "exn.obj", UnwindBB);<br>
<br>
   // Extract the exception object from the ResumeInst and add it to the PHI node<br>
@@ -175,6 +249,10 @@ bool DwarfEHPrepare::InsertUnwindResumeC<br>
<br>
 bool DwarfEHPrepare::runOnFunction(Function &Fn) {<br>
   assert(TM && "DWARF EH preparation requires a target machine");<br>
+  DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();<br>
+  TLI = TM->getSubtargetImpl(Fn)->getTargetLowering();<br>
   bool Changed = InsertUnwindResumeCalls(Fn);<br>
+  DT = nullptr;<br>
+  TLI = nullptr;<br>
   return Changed;<br>
 }<br>
<br>
Modified: llvm/trunk/lib/CodeGen/WinEHPrepare.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/WinEHPrepare.cpp?rev=229944&r1=229943&r2=229944&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/WinEHPrepare.cpp?rev=229944&r1=229943&r2=229944&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/CodeGen/WinEHPrepare.cpp (original)<br>
+++ llvm/trunk/lib/CodeGen/WinEHPrepare.cpp Thu Feb 19 19:00:19 2015<br>
@@ -84,8 +84,13 @@ private:<br>
 } // end anonymous namespace<br>
<br>
 char WinEHPrepare::ID = 0;<br>
-INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare", "Prepare Windows exceptions",<br>
-                   false, false)<br>
+INITIALIZE_TM_PASS_BEGIN(WinEHPrepare, "winehprepare",<br>
+                         "Prepare Windows exceptions", false, false)<br>
+INITIALIZE_PASS_DEPENDENCY(DwarfEHPrepare)<br>
+INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)<br>
+INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)<br>
+INITIALIZE_TM_PASS_END(WinEHPrepare, "winehprepare",<br>
+                       "Prepare Windows exceptions", false, false)<br>
<br>
 FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) {<br>
   return new WinEHPrepare(TM);<br>
@@ -114,8 +119,12 @@ bool WinEHPrepare::runOnFunction(Functio<br>
   EHPersonality Pers = classifyEHPersonality(LPads.back()->getPersonalityFn());<br>
<br>
   // Delegate through to the DWARF pass if this is unrecognized.<br>
-  if (!isMSVCPersonality(Pers))<br>
+  if (!isMSVCPersonality(Pers)) {<br>
+    // Use the resolver from our pass manager in the dwarf pass.<br>
+    assert(getResolver());<br>
+    DwarfPrepare->setResolver(getResolver());<br>
     return DwarfPrepare->runOnFunction(Fn);<br>
+  }<br>
<br>
   // FIXME: This only returns true if the C++ EH handlers were outlined.<br>
   //        When that code is complete, it should always return whatever<br>
<br>
Modified: llvm/trunk/test/CodeGen/Mips/eh.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/Mips/eh.ll?rev=229944&r1=229943&r2=229944&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/Mips/eh.ll?rev=229944&r1=229943&r2=229944&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/Mips/eh.ll (original)<br>
+++ llvm/trunk/test/CodeGen/Mips/eh.ll Thu Feb 19 19:00:19 2015<br>
@@ -27,6 +27,7 @@ lpad:<br>
 ; CHECK-EL:  bne $5<br>
<br>
   %exn.val = landingpad { i8*, i32 } personality i32 (...)* @__gxx_personality_v0<br>
+           cleanup<br>
            catch i8* bitcast (i8** @_ZTId to i8*)<br>
   %exn = extractvalue { i8*, i32 } %exn.val, 0<br>
   %sel = extractvalue { i8*, i32 } %exn.val, 1<br>
<br>
Modified: llvm/trunk/test/CodeGen/X86/dwarf-eh-prepare.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/dwarf-eh-prepare.ll?rev=229944&r1=229943&r2=229944&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/dwarf-eh-prepare.ll?rev=229944&r1=229943&r2=229944&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/X86/dwarf-eh-prepare.ll (original)<br>
+++ llvm/trunk/test/CodeGen/X86/dwarf-eh-prepare.ll Thu Feb 19 19:00:19 2015<br>
@@ -7,12 +7,13 @@<br>
 @int_typeinfo = global i8 0<br>
<br>
 declare void @might_throw()<br>
+declare void @cleanup()<br>
<br>
-define i32 @simple_catch() {<br>
+define i32 @simple_cleanup_catch() {<br>
   invoke void @might_throw()<br>
           to label %cont unwind label %lpad<br>
<br>
-; CHECK: define i32 @simple_catch()<br>
+; CHECK-LABEL: define i32 @simple_cleanup_catch()<br>
 ; CHECK: invoke void @might_throw()<br>
<br>
 cont:<br>
@@ -22,15 +23,18 @@ cont:<br>
<br>
 lpad:<br>
   %ehvals = landingpad { i8*, i32 } personality i32 (...)* @__gxx_personality_v0<br>
+      cleanup<br>
       catch i8* @int_typeinfo<br>
   %ehptr = extractvalue { i8*, i32 } %ehvals, 0<br>
   %ehsel = extractvalue { i8*, i32 } %ehvals, 1<br>
+  call void @cleanup()<br>
   %int_sel = call i32 @llvm.eh.typeid.for(i8* @int_typeinfo)<br>
   %int_match = icmp eq i32 %ehsel, %int_sel<br>
   br i1 %int_match, label %catch_int, label %eh.resume<br>
<br>
 ; CHECK: lpad:<br>
 ; CHECK: landingpad { i8*, i32 } personality i32 (...)* @__gxx_personality_v0<br>
+; CHECK: call void @cleanup()<br>
 ; CHECK: call i32 @llvm.eh.typeid.for<br>
 ; CHECK: br i1<br>
<br>
@@ -41,11 +45,114 @@ catch_int:<br>
 ; CHECK: ret i32 1<br>
<br>
 eh.resume:<br>
-  resume { i8*, i32 } %ehvals<br>
+  %tmp_ehvals = insertvalue { i8*, i32 } undef, i8* %ehptr, 0<br>
+  %new_ehvals = insertvalue { i8*, i32 } %tmp_ehvals, i32 %ehsel, 1<br>
+  resume { i8*, i32 } %new_ehvals<br>
<br>
 ; CHECK: eh.resume:<br>
-; CHECK: call void @_Unwind_Resume(i8* %{{.*}})<br>
+; CHECK-NEXT: call void @_Unwind_Resume(i8* %ehptr)<br>
 }<br>
<br>
+<br>
+define i32 @catch_no_resume() {<br>
+  invoke void @might_throw()<br>
+          to label %cont unwind label %lpad<br>
+<br>
+cont:<br>
+  ret i32 0<br>
+<br>
+lpad:<br>
+  %ehvals = landingpad { i8*, i32 } personality i32 (...)* @__gxx_personality_v0<br>
+      catch i8* @int_typeinfo<br>
+  %ehptr = extractvalue { i8*, i32 } %ehvals, 0<br>
+  %ehsel = extractvalue { i8*, i32 } %ehvals, 1<br>
+  %int_sel = call i32 @llvm.eh.typeid.for(i8* @int_typeinfo)<br>
+  %int_match = icmp eq i32 %ehsel, %int_sel<br>
+  br i1 %int_match, label %catch_int, label %eh.resume<br>
+<br>
+catch_int:<br>
+  ret i32 1<br>
+<br>
+eh.resume:<br>
+  %tmp_ehvals = insertvalue { i8*, i32 } undef, i8* %ehptr, 0<br>
+  %new_ehvals = insertvalue { i8*, i32 } %tmp_ehvals, i32 %ehsel, 1<br>
+  resume { i8*, i32 } %new_ehvals<br>
+}<br>
+<br>
+; Check that we can prune the unreachable resume instruction.<br>
+<br>
+; CHECK-LABEL: define i32 @catch_no_resume() {<br>
+; CHECK: invoke void @might_throw()<br>
+; CHECK: ret i32 0<br>
+; CHECK: lpad:<br>
+; CHECK: landingpad { i8*, i32 } personality i32 (...)* @__gxx_personality_v0<br>
+; CHECK-NOT: br i1<br>
+; CHECK: ret i32 1<br>
+; CHECK-NOT: call void @_Unwind_Resume<br>
+; CHECK: {{^[}]}}<br>
+<br>
+<br>
+define i32 @catch_cleanup_merge() {<br>
+  invoke void @might_throw()<br>
+          to label %inner_invoke unwind label %outer_lpad<br>
+inner_invoke:<br>
+  invoke void @might_throw()<br>
+          to label %cont unwind label %inner_lpad<br>
+cont:<br>
+  ret i32 0<br>
+<br>
+outer_lpad:<br>
+  %ehvals1 = landingpad { i8*, i32 } personality i32 (...)* @__gxx_personality_v0<br>
+      catch i8* @int_typeinfo<br>
+  br label %catch.dispatch<br>
+<br>
+inner_lpad:<br>
+  %ehvals2 = landingpad { i8*, i32 } personality i32 (...)* @__gxx_personality_v0<br>
+      cleanup<br>
+      catch i8* @int_typeinfo<br>
+  call void @cleanup()<br>
+  br label %catch.dispatch<br>
+<br>
+catch.dispatch:<br>
+  %ehvals = phi { i8*, i32 } [ %ehvals1, %outer_lpad ], [ %ehvals2, %inner_lpad ]<br>
+  %ehptr = extractvalue { i8*, i32 } %ehvals, 0<br>
+  %ehsel = extractvalue { i8*, i32 } %ehvals, 1<br>
+  %int_sel = call i32 @llvm.eh.typeid.for(i8* @int_typeinfo)<br>
+  %int_match = icmp eq i32 %ehsel, %int_sel<br>
+  br i1 %int_match, label %catch_int, label %eh.resume<br>
+<br>
+catch_int:<br>
+  ret i32 1<br>
+<br>
+eh.resume:<br>
+  %tmp_ehvals = insertvalue { i8*, i32 } undef, i8* %ehptr, 0<br>
+  %new_ehvals = insertvalue { i8*, i32 } %tmp_ehvals, i32 %ehsel, 1<br>
+  resume { i8*, i32 } %new_ehvals<br>
+}<br>
+<br>
+; We can't prune this merge because one landingpad is a cleanup pad.<br>
+<br>
+; CHECK-LABEL: define i32 @catch_cleanup_merge()<br>
+; CHECK: invoke void @might_throw()<br>
+; CHECK: invoke void @might_throw()<br>
+; CHECK: ret i32 0<br>
+;<br>
+; CHECK: outer_lpad:<br>
+; CHECK: landingpad { i8*, i32 } personality i32 (...)* @__gxx_personality_v0<br>
+; CHECK: br label %catch.dispatch<br>
+;<br>
+; CHECK: inner_lpad:<br>
+; CHECK: landingpad { i8*, i32 } personality i32 (...)* @__gxx_personality_v0<br>
+; CHECK: call void @cleanup()<br>
+; CHECK: br label %catch.dispatch<br>
+;<br>
+; CHECK: catch.dispatch:<br>
+; CHECK: call i32 @llvm.eh.typeid.for<br>
+; CHECK: br i1<br>
+; CHECK: catch_int:<br>
+; CHECK: ret i32 1<br>
+; CHECK: eh.resume:<br>
+; CHECK-NEXT: call void @_Unwind_Resume(i8* %ehptr)<br>
+<br>
 declare i32 @__gxx_personality_v0(...)<br>
 declare i32 @llvm.eh.typeid.for(i8*)<br>
<br>
Modified: llvm/trunk/test/CodeGen/X86/gcc_except_table.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/gcc_except_table.ll?rev=229944&r1=229943&r2=229944&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/gcc_except_table.ll?rev=229944&r1=229943&r2=229944&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/X86/gcc_except_table.ll (original)<br>
+++ llvm/trunk/test/CodeGen/X86/gcc_except_table.ll Thu Feb 19 19:00:19 2015<br>
@@ -37,6 +37,7 @@ entry:<br>
<br>
 lpad:<br>
   %0 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)<br>
+          cleanup<br>
           catch i8* bitcast (i8** @_ZTIi to i8*)<br>
   br label %eh.resume<br>
<br>
<br>
Modified: llvm/trunk/test/CodeGen/X86/gcc_except_table_functions.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/gcc_except_table_functions.ll?rev=229944&r1=229943&r2=229944&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/gcc_except_table_functions.ll?rev=229944&r1=229943&r2=229944&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/X86/gcc_except_table_functions.ll (original)<br>
+++ llvm/trunk/test/CodeGen/X86/gcc_except_table_functions.ll Thu Feb 19 19:00:19 2015<br>
@@ -20,6 +20,7 @@ try.cont:<br>
<br>
 lpad:<br>
   %0 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)<br>
+          cleanup<br>
           catch i8* bitcast (void ()* @filt0 to i8*)<br>
           catch i8* bitcast (void ()* @filt1 to i8*)<br>
   %sel = extractvalue { i8*, i32 } %0, 1<br>
<br>
Modified: llvm/trunk/test/CodeGen/XCore/exception.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/XCore/exception.ll?rev=229944&r1=229943&r2=229944&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/XCore/exception.ll?rev=229944&r1=229943&r2=229944&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/XCore/exception.ll (original)<br>
+++ llvm/trunk/test/CodeGen/XCore/exception.ll Thu Feb 19 19:00:19 2015<br>
@@ -78,6 +78,7 @@ cont:<br>
 ; CHECK: bl __cxa_end_catch<br>
 lpad:<br>
   %0 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)<br>
+          cleanup<br>
           catch i8* bitcast (i8** @_ZTIi to i8*)<br>
           catch i8* bitcast (i8** @_ZTId to i8*)<br>
   %1 = extractvalue { i8*, i32 } %0, 0<br>
@@ -110,13 +111,14 @@ Exit:<br>
 ; CHECK: .long [[PRE_G]]-[[START]]<br>
 ; CHECK: .long [[POST_G]]-[[PRE_G]]<br>
 ; CHECK: .long [[LANDING]]-[[START]]<br>
-; CHECK: .byte 3<br>
+; CHECK: .byte 5<br>
 ; CHECK: .long [[POST_G]]-[[START]]<br>
 ; CHECK: .long [[END]]-[[POST_G]]<br>
 ; CHECK: .long 0<br>
 ; CHECK: .byte 0<br>
-; CHECK: .byte 1<br>
 ; CHECK: .byte 0<br>
+; CHECK: .byte 1<br>
+; CHECK: .byte 125<br>
 ; CHECK: .byte 2<br>
 ; CHECK: .byte 125<br>
 ; CHECK: .long _ZTIi<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>