[IR] Reformulate LLVM's EH funclet IR

While we have successfully implemented a funclet-oriented EH scheme on
top of LLVM IR, our scheme has some notable deficiencies:
- catchendpad and cleanupendpad are necessary in the current design
  but they are difficult to explain to others, even to seasoned LLVM
  experts.
- catchendpad and cleanupendpad are optimization barriers.  They cannot
  be split and force all potentially throwing call-sites to be invokes.
  This has a noticable effect on the quality of our code generation.
- catchpad, while similar in some aspects to invoke, is fairly awkward.
  It is unsplittable, starts a funclet, and has control flow to other
  funclets.
- The nesting relationship between funclets is currently a property of
  control flow edges.  Because of this, we are forced to carefully
  analyze the flow graph to see if there might potentially exist illegal
  nesting among funclets.  While we have logic to clone funclets when
  they are illegally nested, it would be nicer if we had a
  representation which forbade them upfront.

Let's clean this up a bit by doing the following:
- Instead, make catchpad more like cleanuppad and landingpad: no control
  flow, just a bunch of simple operands;  catchpad would be splittable.
- Introduce catchswitch, a control flow instruction designed to model
  the constraints of funclet oriented EH.
- Make funclet scoping explicit by having funclet instructions consume
  the token produced by the funclet which contains them.
- Remove catchendpad and cleanupendpad.  Their presence can be inferred
  implicitly using coloring information.

N.B.  The state numbering code for the CLR has been updated but the
veracity of it's output cannot be spoken for.  An expert should take a
look to make sure the results are reasonable.

Reviewers: rnk, JosephTremoulet, andrew.w.kaylor

Differential Revision: http://reviews.llvm.org/D15139

llvm-svn: 255422
diff --git a/llvm/test/Transforms/CodeGenPrepare/catchpad-phi-cast.ll b/llvm/test/Transforms/CodeGenPrepare/catchpad-phi-cast.ll
index c0b63b7..8c5e01e 100644
--- a/llvm/test/Transforms/CodeGenPrepare/catchpad-phi-cast.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/catchpad-phi-cast.ll
@@ -18,9 +18,6 @@
 
 ; CHECK-LABEL: @test(
 define void @test(i32* %addr) personality i32 (...)* @__CxxFrameHandler3 {
-; CHECK: entry:
-; CHECK-NEXT: %x = getelementptr i32, i32* %addr, i32 1
-; CHECK-NEXT: %p1 = bitcast i32* %x to i8*
 entry:
   %x = getelementptr i32, i32* %addr, i32 1
   %p1 = bitcast i32* %x to i8*
@@ -29,7 +26,6 @@
 
 ; CHECK: invoke.cont:
 ; CHECK-NEXT: %y = getelementptr i32, i32* %addr, i32 2
-; CHECK-NEXT: %p2 = bitcast i32* %y to i8*
 invoke.cont:
   %y = getelementptr i32, i32* %addr, i32 2
   %p2 = bitcast i32* %y to i8*
@@ -40,23 +36,31 @@
   ret void
 
 catch1:
-  %cp1 = catchpad [] to label %catch.dispatch unwind label %catchend1
+  %cs1 = catchswitch within none [label %handler1] unwind to caller
+
+handler1:
+  %cp1 = catchpad within %cs1 []
+  br label %catch.shared
+; CHECK: handler1:
+; CHECK-NEXT: catchpad within %cs1
+; CHECK: %[[p1:[0-9]+]] = bitcast i32* %x to i8*
 
 catch2:
-  %cp2 = catchpad [] to label %catch.dispatch unwind label %catchend2
+  %cs2 = catchswitch within none [label %handler2] unwind to caller
 
-; CHECK: catch.dispatch:
-; CHECK-NEXT: %p = phi i8* [ %p1, %catch1 ], [ %p2, %catch2 ]
-catch.dispatch:
-  %p = phi i8* [ %p1, %catch1 ], [ %p2, %catch2 ]
+handler2:
+  %cp2 = catchpad within %cs2 []
+  br label %catch.shared
+; CHECK: handler2:
+; CHECK: catchpad within %cs2
+; CHECK: %[[p2:[0-9]+]] = bitcast i32* %y to i8*
+
+; CHECK: catch.shared:
+; CHECK-NEXT: %p = phi i8* [ %[[p1]], %handler1 ], [ %[[p2]], %handler2 ]
+catch.shared:
+  %p = phi i8* [ %p1, %handler1 ], [ %p2, %handler2 ]
   call void @g(i8* %p)
   unreachable
-
-catchend1:
-  catchendpad unwind to caller
-
-catchend2:
-  catchendpad unwind to caller
 }
 
 ; CodeGenPrepare will want to hoist these llvm.dbg.value calls to the phi, but
@@ -75,24 +79,22 @@
 
 catch.dispatch:
   %p = phi i8* [%a, %entry], [%b, %next]
-  %cp1 = catchpad [] to label %catch unwind label %catchend
+  %cs1 = catchswitch within none [label %catch] unwind to caller
 
 catch:
+  %cp1 = catchpad within %cs1 []
   tail call void @llvm.dbg.value(metadata i8* %p, i64 0, metadata !11, metadata !13), !dbg !14
-  invoke void @g(i8* %p) to label %catchret unwind label %catchend
-catchret:
-  catchret %cp1 to label %ret
+  call void @g(i8* %p)
+  catchret from %cp1 to label %ret
 
 ; CHECK: catch.dispatch:
 ; CHECK-NEXT: phi i8
-; CHECK-NEXT: catchpad
+; CHECK-NEXT: catchswitch
 ; CHECK-NOT: llvm.dbg.value
 
 ; CHECK: catch:
+; CHECK-NEXT: catchpad
 ; CHECK-NEXT: call void @llvm.dbg.value
-
-catchend:
-  catchendpad unwind to caller
 }
 
 !llvm.dbg.cu = !{!0}
diff --git a/llvm/test/Transforms/GVN/funclet.ll b/llvm/test/Transforms/GVN/funclet.ll
index c9faf8e..2669256 100644
--- a/llvm/test/Transforms/GVN/funclet.ll
+++ b/llvm/test/Transforms/GVN/funclet.ll
@@ -17,12 +17,12 @@
           to label %unreachable unwind label %catch.dispatch
 
 catch.dispatch:                                   ; preds = %entry
-  %catchpad = catchpad [i8* null, i32 64, i8* null]
-          to label %catch unwind label %catchendblock
+  %cs1 = catchswitch within none [label %catch] unwind to caller
 
 catch:                                            ; preds = %catch.dispatch
+  %catchpad = catchpad within %cs1 [i8* null, i32 64, i8* null]
   store i8 5, i8* %b
-  catchret %catchpad to label %try.cont
+  catchret from %catchpad to label %try.cont
 
 try.cont:                                         ; preds = %catch
   %load_b = load i8, i8* %b
@@ -30,9 +30,6 @@
   %add = add i8 %load_b, %load_c
   ret i8 %add
 
-catchendblock:                                    ; preds = %catch.dispatch
-  catchendpad unwind to caller
-
 unreachable:                                      ; preds = %entry
   unreachable
 }
diff --git a/llvm/test/Transforms/GVN/pre-load.ll b/llvm/test/Transforms/GVN/pre-load.ll
index c165a5a..685df24 100644
--- a/llvm/test/Transforms/GVN/pre-load.ll
+++ b/llvm/test/Transforms/GVN/pre-load.ll
@@ -399,7 +399,7 @@
 ; CHECK-LABEL: @test12(
 block1:
   invoke void @f()
-          to label %block2 unwind label %catch
+          to label %block2 unwind label %catch.dispatch
 
 block2:
   invoke void @f()
@@ -408,31 +408,25 @@
 block3:
   ret void
 
-catch:
-  %c = catchpad []
-    to label %catch.dispatch unwind label %catchend
-
 catch.dispatch:
-  catchret %c to label %block2
+  %cs1 = catchswitch within none [label %catch] unwind label %cleanup2
 
-; CHECK: catchend:
-; CHECK-NOT: load
-; CHECK-NEXT: catchendpad
-catchend:
-  catchendpad unwind label %cleanup2
+catch:
+  %c = catchpad within %cs1 []
+  catchret from %c to label %block2
 
 cleanup:
-  %c1 = cleanuppad []
+  %c1 = cleanuppad within none []
   store i32 0, i32* %p
-  cleanupret %c1 unwind label %cleanup2
+  cleanupret from %c1 unwind label %cleanup2
 
 ; CHECK: cleanup2:
 ; CHECK-NOT: phi
-; CHECK-NEXT: %c2 = cleanuppad []
+; CHECK-NEXT: %c2 = cleanuppad within none []
 ; CHECK-NEXT: %NOTPRE = load i32, i32* %p
 cleanup2:
-  %c2 = cleanuppad []
+  %c2 = cleanuppad within none []
   %NOTPRE = load i32, i32* %p
   call void @g(i32 %NOTPRE)
-  cleanupret %c2 unwind to caller
+  cleanupret from %c2 unwind to caller
 }
diff --git a/llvm/test/Transforms/Inline/PR25155.ll b/llvm/test/Transforms/Inline/PR25155.ll
index f2a9ec3..aed9a58a 100644
--- a/llvm/test/Transforms/Inline/PR25155.ll
+++ b/llvm/test/Transforms/Inline/PR25155.ll
@@ -8,21 +8,23 @@
           to label %try.cont unwind label %catch.dispatch
 
 catch.dispatch:                                   ; preds = %entry
-  %0 = catchpad [i8* null, i32 64, i8* null]
-          to label %catch unwind label %catchendblock
+  %cs1 = catchswitch within none [label %catch] unwind to caller
 
 catch:                                            ; preds = %catch.dispatch
+  %0 = catchpad within %cs1 [i8* null, i32 64, i8* null]
   invoke void @dtor()
-          to label %invoke.cont.1 unwind label %catchendblock
+          to label %invoke.cont.1 unwind label %ehcleanup
 
 invoke.cont.1:                                    ; preds = %catch
-  catchret %0 to label %try.cont
+  catchret from %0 to label %try.cont
 
 try.cont:                                         ; preds = %entry, %invoke.cont.1
   ret void
 
-catchendblock:                                    ; preds = %catch, %catch.dispatch
-  catchendpad unwind to caller
+ehcleanup:
+  %cp2 = cleanuppad within none []
+  call void @g()
+  cleanupret from %cp2 unwind to caller
 }
 
 ; CHECK-LABEL:  define void @f(
@@ -31,10 +33,7 @@
 ; CHECK:                 to label %dtor.exit unwind label %terminate.i
 
 ; CHECK:       terminate.i:
-; CHECK-NEXT:    terminatepad [void ()* @terminate] unwind label %catchendblock
-
-; CHECK:       catchendblock:
-; CHECK-NEXT:    catchendpad unwind to caller
+; CHECK-NEXT:    terminatepad within %0 [void ()* @terminate] unwind label %ehcleanup
 
 declare i32 @__CxxFrameHandler3(...)
 
@@ -47,7 +46,7 @@
   ret void
 
 terminate:                                        ; preds = %entry
-  terminatepad [void ()* @terminate] unwind to caller
+  terminatepad within none [void ()* @terminate] unwind to caller
 }
 
 declare void @g()
diff --git a/llvm/test/Transforms/InstCombine/token.ll b/llvm/test/Transforms/InstCombine/token.ll
index e47109b..0929cf7 100644
--- a/llvm/test/Transforms/InstCombine/token.ll
+++ b/llvm/test/Transforms/InstCombine/token.ll
@@ -9,14 +9,14 @@
   unreachable
 
 unreachable:
-  %cl = cleanuppad []
-  cleanupret %cl unwind to caller
+  %cl = cleanuppad within none []
+  cleanupret from %cl unwind to caller
 }
 
 ; CHECK-LABEL: define void @test1(
 ; CHECK: unreachable:
-; CHECK:   %cl = cleanuppad []
-; CHECK:   cleanupret %cl unwind to caller
+; CHECK:   %cl = cleanuppad within none []
+; CHECK:   cleanupret from %cl unwind to caller
 
 define void @test2(i8 %A, i8 %B) personality i32 (...)* @__CxxFrameHandler3 {
 bb:
@@ -33,19 +33,15 @@
 
 catch:
   %phi = phi i32 [ %X, %bb ], [ %Y, %cont ]
-  %cl = catchpad []
-   to label %doit
-   unwind label %endpad
+  %cs = catchswitch within none [label %doit] unwind to caller
 
 doit:
+  %cl = catchpad within %cs []
   call void @g(i32 %phi)
   unreachable
 
 unreachable:
   unreachable
-
-endpad:
-  catchendpad unwind to caller
 }
 
 ; CHECK-LABEL: define void @test2(
@@ -73,19 +69,15 @@
 
 catch:
   %phi = phi i32 [ %X, %bb ], [ %Y, %cont ], [ %Y, %cont2 ]
-  %cl = catchpad []
-   to label %doit
-   unwind label %endpad
+  %cs = catchswitch within none [label %doit] unwind to caller
 
 doit:
+  %cl = catchpad within %cs []
   call void @g(i32 %phi)
   unreachable
 
 unreachable:
   unreachable
-
-endpad:
-  catchendpad unwind to caller
 }
 
 ; CHECK-LABEL: define void @test3(
diff --git a/llvm/test/Transforms/LoopStrengthReduce/funclet.ll b/llvm/test/Transforms/LoopStrengthReduce/funclet.ll
index 03799dd..a2da320 100644
--- a/llvm/test/Transforms/LoopStrengthReduce/funclet.ll
+++ b/llvm/test/Transforms/LoopStrengthReduce/funclet.ll
@@ -20,19 +20,17 @@
 
 pad:                                              ; preds = %throw
   %phi2 = phi i8* [ %tmp96, %throw ]
-  terminatepad [] unwind label %blah
+  terminatepad within none [] unwind label %blah
 
 blah:
-  catchpad [] to label %unreachable unwind label %blah3
+  %cs = catchswitch within none [label %unreachable] unwind label %blah2
 
 unreachable:
+  catchpad within %cs []
   unreachable
 
-blah3:
-  catchendpad unwind label %blah2
-
 blah2:
-  %cleanuppadi4.i.i.i = cleanuppad []
+  %cleanuppadi4.i.i.i = cleanuppad within none []
   br label %loop_body
 
 loop_body:                                        ; preds = %iter, %pad
@@ -45,11 +43,11 @@
   br i1 undef, label %unwind_out, label %loop_body
 
 unwind_out:                                       ; preds = %iter, %loop_body
-  cleanupret %cleanuppadi4.i.i.i unwind to caller
+  cleanupret from %cleanuppadi4.i.i.i unwind to caller
 }
 
 ; CHECK-LABEL: define void @f(
-; CHECK: cleanuppad []
+; CHECK: cleanuppad within none []
 ; CHECK-NEXT: ptrtoint i8* %phi2 to i32
 
 define void @g() personality i32 (...)* @_except_handler3 {
@@ -63,20 +61,18 @@
 
 pad:
   %phi2 = phi i8* [ %tmp96, %throw ]
-  catchpad [] to label %unreachable unwind label %blah
+  %cs = catchswitch within none [label %unreachable, label %blah] unwind to caller
 
 unreachable:
+  catchpad within %cs []
   unreachable
 
 blah:
-  %catchpad = catchpad [] to label %loop_body unwind label %blah3
-
-
-blah3:
-  catchendpad unwind to caller ;label %blah2
+  %catchpad = catchpad within %cs []
+  br label %loop_body
 
 unwind_out:
-  catchret %catchpad to label %leave
+  catchret from %catchpad to label %leave
 
 leave:
   ret void
@@ -93,10 +89,7 @@
 
 ; CHECK-LABEL: define void @g(
 ; CHECK: blah:
-; CHECK-NEXT: catchpad []
-; CHECK-NEXT: to label %loop_body.preheader
-
-; CHECK: loop_body.preheader:
+; CHECK-NEXT: catchpad within %cs []
 ; CHECK-NEXT: ptrtoint i8* %phi2 to i32
 
 
@@ -110,29 +103,25 @@
           to label %throw unwind label %pad
 
 pad:
-  catchpad [] to label %unreachable unwind label %blug
+  %cs = catchswitch within none [label %unreachable, label %blug] unwind to caller
 
 unreachable:
+  catchpad within %cs []
   unreachable
 
 blug:
   %phi2 = phi i8* [ %tmp96, %pad ]
-  %catchpad = catchpad [] to label %blah2 unwind label %blah3
-
-blah2:
+  %catchpad = catchpad within %cs []
   br label %loop_body
 
-blah3:
-  catchendpad unwind to caller ;label %blah2
-
 unwind_out:
-  catchret %catchpad to label %leave
+  catchret from %catchpad to label %leave
 
 leave:
   ret void
 
 loop_body:                                        ; preds = %iter, %pad
-  %tmp99 = phi i8* [ %tmp101, %iter ], [ %phi2, %blah2 ]
+  %tmp99 = phi i8* [ %tmp101, %iter ], [ %phi2, %blug ]
   %tmp100 = icmp eq i8* %tmp99, undef
   br i1 %tmp100, label %unwind_out, label %iter
 
@@ -143,10 +132,7 @@
 
 ; CHECK-LABEL: define void @h(
 ; CHECK: blug:
-; CHECK: catchpad []
-; CHECK-NEXT: to label %blah2
-
-; CHECK: blah2:
+; CHECK: catchpad within %cs []
 ; CHECK-NEXT: ptrtoint i8* %phi2 to i32
 
 define void @i() personality i32 (...)* @_except_handler3 {
@@ -160,16 +146,14 @@
 
 catchpad:                                              ; preds = %throw
   %phi2 = phi i8* [ %tmp96, %throw ]
-  catchpad [] to label %cp_body unwind label %catchendpad
+  %cs = catchswitch within none [label %cp_body] unwind label %cleanuppad
 
 cp_body:
+  catchpad within %cs []
   br label %loop_head
 
-catchendpad:
-  catchendpad unwind label %cleanuppad
-
 cleanuppad:
-  cleanuppad []
+  cleanuppad within none []
   br label %loop_head
 
 loop_head:
@@ -205,39 +189,31 @@
   br label %for.cond
 
 catch.dispatch:                                   ; preds = %for.cond
-  %0 = catchpad [i8* null, i32 64, i8* null]
-          to label %catch unwind label %catchendblock
-
-catchendblock:                                    ; preds = %catch.dispatch
-  catchendpad unwind label %catch.dispatch.2
+  %cs = catchswitch within none [label %catch] unwind label %catch.dispatch.2
 
 catch:                                            ; preds = %catch.dispatch
-  catchret %0 to label %try.cont
+  %0 = catchpad within %cs [i8* null, i32 64, i8* null]
+  catchret from %0 to label %try.cont
 
 try.cont:                                         ; preds = %catch
   invoke void @external(i32* %c)
           to label %try.cont.7 unwind label %catch.dispatch.2
 
 catch.dispatch.2:                                 ; preds = %try.cont, %catchendblock
-  %e.0 = phi i32* [ %c, %try.cont ], [ %b, %catchendblock ]
-  %1 = catchpad [i8* null, i32 64, i8* null]
-          to label %catch.4 unwind label %catchendblock.3
+  %e.0 = phi i32* [ %c, %try.cont ], [ %b, %catch.dispatch ]
+  %cs2 = catchswitch within none [label %catch.4] unwind to caller
 
 catch.4:                                          ; preds = %catch.dispatch.2
+  catchpad within %cs2 [i8* null, i32 64, i8* null]
   unreachable
 
 try.cont.7:                                       ; preds = %try.cont
   ret void
-
-catchendblock.3:                                  ; preds = %catch.dispatch.2
-  catchendpad unwind to caller
 }
 
 ; CHECK-LABEL: define void @test1(
 ; CHECK: for.cond:
 ; CHECK:   %d.0 = phi i32* [ %b, %entry ], [ %incdec.ptr, %for.inc ]
 
-; CHECK: catchendpad unwind label %catch.dispatch.2
-
 ; CHECK: catch.dispatch.2:
-; CHECK: %e.0 = phi i32* [ %c, %try.cont ], [ %b, %catchendblock ]
+; CHECK: %e.0 = phi i32* [ %c, %try.cont ], [ %b, %catch.dispatch ]
diff --git a/llvm/test/Transforms/LoopStrengthReduce/pr25541.ll b/llvm/test/Transforms/LoopStrengthReduce/pr25541.ll
index fa64875..011998b 100644
--- a/llvm/test/Transforms/LoopStrengthReduce/pr25541.ll
+++ b/llvm/test/Transforms/LoopStrengthReduce/pr25541.ll
@@ -12,10 +12,10 @@
           to label %for.inc.i unwind label %catch.dispatch.i
 
 catch.dispatch.i:                                 ; preds = %for.cond.i
-  %0 = catchpad [i8* null, i32 64, i8* null]
-          to label %for.cond.1.preheader.i unwind label %catchendblock.i
+  %cs = catchswitch within none [label %for.cond.1.preheader.i] unwind to caller
 
 for.cond.1.preheader.i:                           ; preds = %catch.dispatch.i
+  %0 = catchpad within %cs [i8* null, i32 64, i8* null]
   %cmp.i = icmp eq i32* %_First.addr.0.i, null
   br label %for.cond.1.i
 
@@ -23,18 +23,15 @@
   br i1 %cmp.i, label %for.end.i, label %for.body.i
 
 for.body.i:                                       ; preds = %for.cond.1.i
-  invoke void @g()
-          to label %for.cond.1.i unwind label %catchendblock.i
-
-catchendblock.i:                                  ; preds = %for.body.i, %catch.dispatch.i
-  catchendpad unwind to caller
+  call void @g()
+  br label %for.cond.1.i
 
 for.inc.i:                                        ; preds = %for.cond.i
   %incdec.ptr.i = getelementptr inbounds i32, i32* %_First.addr.0.i, i64 1
   br label %for.cond.i
 
 for.end.i:                                        ; preds = %for.cond.1.i
-  catchret %0 to label %leave
+  catchret from %0 to label %leave
 
 leave:                                            ; preds = %for.end.i
   ret void
diff --git a/llvm/test/Transforms/LoopUnswitch/cleanuppad.ll b/llvm/test/Transforms/LoopUnswitch/cleanuppad.ll
new file mode 100644
index 0000000..b06ebd7
--- /dev/null
+++ b/llvm/test/Transforms/LoopUnswitch/cleanuppad.ll
@@ -0,0 +1,44 @@
+; RUN: opt -S -loop-unswitch < %s | FileCheck %s
+target triple = "x86_64-pc-win32"
+
+define void @f(i32 %doit, i1 %x, i1 %y) personality i32 (...)* @__CxxFrameHandler3 {
+entry:
+  %tobool = icmp eq i32 %doit, 0
+  br label %for.cond
+
+for.cond:                                         ; preds = %for.inc, %entry
+  br i1 %x, label %for.body, label %for.end
+
+for.body:                                         ; preds = %for.cond
+  br i1 %tobool, label %if.then, label %for.inc
+
+if.then:                                          ; preds = %for.body
+  br i1 %y, label %for.inc, label %delete.notnull
+
+delete.notnull:                                   ; preds = %if.then
+  invoke void @g()
+          to label %invoke.cont unwind label %lpad
+
+invoke.cont:                                      ; preds = %delete.notnull
+  br label %for.inc
+
+lpad:                                             ; preds = %delete.notnull
+  %cp = cleanuppad within none []
+  cleanupret from %cp unwind to caller
+
+for.inc:                                          ; preds = %invoke.cont, %if.then, %for.body
+  br label %for.cond
+
+for.end:                                          ; preds = %for.cond
+  ret void
+}
+
+declare void @g()
+
+declare i32 @__CxxFrameHandler3(...)
+
+; CHECK-LABEL: define void @f(
+; CHECK: cleanuppad within none []
+; CHECK-NOT: cleanuppad
+
+attributes #0 = { ssp uwtable }
diff --git a/llvm/test/Transforms/SimplifyCFG/empty-cleanuppad.ll b/llvm/test/Transforms/SimplifyCFG/empty-cleanuppad.ll
index d04c567..0ba2904 100644
--- a/llvm/test/Transforms/SimplifyCFG/empty-cleanuppad.ll
+++ b/llvm/test/Transforms/SimplifyCFG/empty-cleanuppad.ll
@@ -31,12 +31,12 @@
   ret void
 
 ehcleanup:                                        ; preds = %entry
-  %0 = cleanuppad []
-  cleanupret %0 unwind label %ehcleanup.1
+  %0 = cleanuppad within none []
+  cleanupret from %0 unwind label %ehcleanup.1
 
 ehcleanup.1:                                      ; preds = %ehcleanup
-  %1 = cleanuppad []
-  cleanupret %1 unwind to caller
+  %1 = cleanuppad within none []
+  cleanupret from %1 unwind to caller
 }
 
 
@@ -60,15 +60,14 @@
 ; CHECK: entry:
 ; CHECK:   invoke void @g()
 ; CHECK: ehcleanup:
-; CHECK:   cleanuppad
+; CHECK:   cleanuppad within none
 ; CHECK:   call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %b)
-; CHECK:   cleanupret %0 unwind label %catch.dispatch
+; CHECK:   cleanupret from %0 unwind label %catch.dispatch
 ; CHECK: catch.dispatch:
-; CHECK:   catchpad
+; CHECK:   catchswitch within none [label %catch] unwind to caller
 ; CHECK: catch:
+; CHECK:   catchpad
 ; CHECK:   catchret
-; CHECK: catchendblock:                                    ; preds = %catch.dispatch
-; CHECK:   catchendpad unwind to caller
 ; CHECK-NOT: cleanuppad
 ; CHECK: }
 ;
@@ -81,15 +80,16 @@
   br label %try.cont
 
 ehcleanup:                                        ; preds = %entry
-  %0 = cleanuppad []
+  %0 = cleanuppad within none []
   call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %b)
-  cleanupret %0 unwind label %catch.dispatch
+  cleanupret from %0 unwind label %catch.dispatch
 
 catch.dispatch:                                   ; preds = %ehcleanup
-  %1 = catchpad [i8* null, i32 u0x40, i8* null] to label %catch unwind label %catchendblock
+  %cs1 = catchswitch within none [label %catch] unwind label %ehcleanup.1
 
 catch:                                            ; preds = %catch.dispatch
-  catchret %1 to label %catchret.dest
+  %1 = catchpad within %cs1 [i8* null, i32 u0x40, i8* null]
+  catchret from %1 to label %catchret.dest
 
 catchret.dest:                                    ; preds = %catch
   br label %try.cont
@@ -97,12 +97,9 @@
 try.cont:                                         ; preds = %catchret.dest, %invoke.cont
   ret void
 
-catchendblock:                                    ; preds = %catch.dispatch
-  catchendpad unwind label %ehcleanup.1
-
-ehcleanup.1:                                      ; preds = %catchendblock
-  %2 = cleanuppad []
-  cleanupret %2 unwind to caller
+ehcleanup.1:
+  %2 = cleanuppad within none []
+  cleanupret from %2 unwind to caller
 }
 
 
@@ -121,21 +118,19 @@
 ; In this case the inner cleanup pad should be eliminated and the invoke of g()
 ; should unwind directly to the catchpad.
 ;
-; CHECK: define void @f3()
+; CHECK-LABEL: define void @f3()
 ; CHECK: entry:
 ; CHECK:   invoke void @g()
 ; CHECK:           to label %try.cont unwind label %catch.dispatch
 ; CHECK: catch.dispatch:
-; CHECK:   catchpad [i8* null, i32 64, i8* null]
-; CHECK-NEXT: to label %catch unwind label %catchendblock
+; CHECK-NEXT: catchswitch within none [label %catch] unwind label %ehcleanup.1
 ; CHECK: catch:
+; CHECK:   catchpad within %cs1 [i8* null, i32 64, i8* null]
 ; CHECK:   catchret
-; CHECK: catchendblock:
-; CHECK:   catchendpad unwind label %ehcleanup.1
 ; CHECK: ehcleanup.1:
 ; CHECK:   cleanuppad
 ; CHECK:   call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %a)
-; CHECK:   cleanupret %1 unwind to caller
+; CHECK:   cleanupret from %cp3 unwind to caller
 ; CHECK: }
 ;
 define void @f3() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
@@ -147,14 +142,15 @@
   br label %try.cont
 
 ehcleanup:                                        ; preds = %entry
-  %0 = cleanuppad []
-  cleanupret %0 unwind label %catch.dispatch
+  %0 = cleanuppad within none []
+  cleanupret from %0 unwind label %catch.dispatch
 
 catch.dispatch:                                   ; preds = %ehcleanup
-  %1 = catchpad [i8* null, i32 u0x40, i8* null] to label %catch unwind label %catchendblock
+  %cs1 = catchswitch within none [label %catch] unwind label %ehcleanup.1
 
 catch:                                            ; preds = %catch.dispatch
-  catchret %1 to label %catchret.dest
+  %cp2 = catchpad within %cs1 [i8* null, i32 u0x40, i8* null]
+  catchret from %cp2 to label %catchret.dest
 
 catchret.dest:                                    ; preds = %catch
   br label %try.cont
@@ -162,13 +158,10 @@
 try.cont:                                         ; preds = %catchret.dest, %invoke.cont
   ret void
 
-catchendblock:                                    ; preds = %catch.dispatch
-  catchendpad unwind label %ehcleanup.1
-
-ehcleanup.1:                                      ; preds = %catchendblock
-  %2 = cleanuppad []
+ehcleanup.1:
+  %cp3 = cleanuppad within none []
   call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %a)
-  cleanupret %2 unwind to caller
+  cleanupret from %cp3 unwind to caller
 }
 
 
@@ -184,11 +177,10 @@
 ; }
 ;
 ; In this case, the cleanuppad should be eliminated, the invoke outside of the
-; call block should be converted to a call and the catchendpad should unwind
-; to the caller (that is, that is, exception handling continues with the parent
-; frame of the caller).)
+; catch block should be converted to a call (that is, that is, exception
+; handling continues with the parent frame of the caller).)
 ;
-; CHECK: define void @f4()
+; CHECK-LABEL: define void @f4()
 ; CHECK: entry:
 ; CHECK:   call void @g
 ; Note: The cleanuppad simplification will insert an unconditional branch here
@@ -196,11 +188,10 @@
 ; CHECK:   invoke void @g()
 ; CHECK:           to label %try.cont unwind label %catch.dispatch
 ; CHECK: catch.dispatch:
-; CHECK:   catchpad
+; CHECK:   catchswitch within none [label %catch] unwind to caller
 ; CHECK: catch:
+; CHECK:   catchpad
 ; CHECK:   catchret
-; CHECK: catchendblock:
-; CHECK:   catchendpad unwind to caller
 ; CHECK-NOT: cleanuppad
 ; CHECK: }
 ;
@@ -214,43 +205,41 @@
           to label %try.cont unwind label %catch.dispatch
 
 catch.dispatch:                                   ; preds = %invoke.cont
-  %0 = catchpad [i8* null, i32 u0x40, i8* null] to label %catch unwind label %catchendblock
+  %cs1 = catchswitch within none [label %catch] unwind label %ehcleanup
 
 catch:                                            ; preds = %catch.dispatch
-  catchret %0 to label %try.cont
+  %0 = catchpad within %cs1 [i8* null, i32 u0x40, i8* null]
+  catchret from %0 to label %try.cont
 
 try.cont:                                         ; preds = %catch, %invoke.cont
   ret void
 
-catchendblock:                                    ; preds = %catch.dispatch
-  catchendpad unwind label %ehcleanup
-
-ehcleanup:                                        ; preds = %catchendblock, %entry
-  %1 = cleanuppad []
-  cleanupret %1 unwind to caller
+ehcleanup:
+  %cp2 = cleanuppad within none []
+  cleanupret from %cp2 unwind to caller
 }
 
 ; This tests the case where a terminatepad unwinds to a cleanuppad.
 ; I'm not sure how this case would arise, but it seems to be syntactically
 ; legal so I'm testing it.
 ;
-; CHECK: define void @f5()
+; CHECK-LABEL: define void @f5()
 ; CHECK: entry:
 ; CHECK:   invoke void @g()
 ; CHECK:           to label %try.cont unwind label %terminate
 ; CHECK: terminate:
-; CHECK:   terminatepad [i7 4] unwind to caller
+; CHECK:   terminatepad within none [i7 4] unwind to caller
 ; CHECK-NOT: cleanuppad
 ; CHECK: try.cont:
 ; CHECK:   invoke void @g()
 ; CHECK:           to label %try.cont.1 unwind label %terminate.1
 ; CHECK: terminate.1:
-; CHECK:   terminatepad [i7 4] unwind label %ehcleanup.2
+; CHECK:   terminatepad within none [i7 4] unwind label %ehcleanup.2
 ; CHECK-NOT: ehcleanup.1:
 ; CHECK: ehcleanup.2:
 ; CHECK:   [[TMP:\%.+]] = cleanuppad
 ; CHECK:   call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %a)
-; CHECK:   cleanupret [[TMP]] unwind to caller
+; CHECK:   cleanupret from [[TMP]] unwind to caller
 ; CHECK: }
 define void @f5() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
 entry:
@@ -259,27 +248,27 @@
           to label %try.cont unwind label %terminate
 
 terminate:                                        ; preds = %entry
-  terminatepad [i7 4] unwind label %ehcleanup
+  terminatepad within none [i7 4] unwind label %ehcleanup
 
 ehcleanup:                                        ; preds = %terminate
-  %0 = cleanuppad []
-  cleanupret %0 unwind to caller
+  %0 = cleanuppad within none []
+  cleanupret from %0 unwind to caller
 
 try.cont:                                         ; preds = %entry
   invoke void @g()
           to label %try.cont.1 unwind label %terminate.1
 
 terminate.1:                                      ; preds = %try.cont
-  terminatepad [i7 4] unwind label %ehcleanup.1
+  terminatepad within none [i7 4] unwind label %ehcleanup.1
 
 ehcleanup.1:                                      ; preds = %terminate.1
-  %1 = cleanuppad []
-  cleanupret %1 unwind label %ehcleanup.2
+  %1 = cleanuppad within none []
+  cleanupret from %1 unwind label %ehcleanup.2
 
 ehcleanup.2:                                      ; preds = %ehcleanup.1
-  %2 = cleanuppad []
+  %2 = cleanuppad within none []
   call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %a)
-  cleanupret %2 unwind to caller
+  cleanupret from %2 unwind to caller
 
 try.cont.1:                                       ; preds = %try.cont
   ret void
@@ -304,7 +293,7 @@
 ; In this case, the cleanup pad should be eliminated and the PHI node in the
 ; cleanup pad should be sunk into the catch dispatch block.
 ;
-; CHECK: define i32 @f6()
+; CHECK-LABEL: define i32 @f6()
 ; CHECK: entry:
 ; CHECK:   invoke void @g()
 ; CHECK: invoke.cont:
@@ -325,17 +314,15 @@
 
 ehcleanup:                                        ; preds = %invoke.cont, %entry
   %state.0 = phi i32 [ 2, %invoke.cont ], [ 1, %entry ]
-  %0 = cleanuppad []
-  cleanupret %0 unwind label %catch.dispatch
+  %0 = cleanuppad within none []
+  cleanupret from %0 unwind label %catch.dispatch
 
 catch.dispatch:                                   ; preds = %ehcleanup
-  %1 = catchpad [i8* null, i32 u0x40, i8* null] to label %catch unwind label %catchendblock
+  %cs1 = catchswitch within none [label %catch] unwind to caller
 
 catch:                                            ; preds = %catch.dispatch
-  catchret %1 to label %return
-
-catchendblock:                                    ; preds = %catch.dispatch
-  catchendpad unwind to caller
+  %1 = catchpad within %cs1 [i8* null, i32 u0x40, i8* null]
+  catchret from %1 to label %return
 
 return:                                           ; preds = %invoke.cont, %catch
   %retval.0 = phi i32 [ %state.0, %catch ], [ 0, %invoke.cont ]
@@ -363,7 +350,7 @@
 ; In this case, the cleanup pad should be eliminated and the PHI node in the
 ; cleanup pad should be merged with the PHI node in the catch dispatch block.
 ;
-; CHECK: define i32 @f7()
+; CHECK-LABEL: define i32 @f7()
 ; CHECK: entry:
 ; CHECK:   invoke void @g()
 ; CHECK: invoke.cont:
@@ -390,18 +377,16 @@
 
 ehcleanup:                                        ; preds = %invoke.cont.1, %invoke.cont
   %state.0 = phi i32 [ 3, %invoke.cont.1 ], [ 2, %invoke.cont ]
-  %0 = cleanuppad []
-  cleanupret %0 unwind label %catch.dispatch
+  %0 = cleanuppad within none []
+  cleanupret from %0 unwind label %catch.dispatch
 
 catch.dispatch:                                   ; preds = %ehcleanup, %entry
   %state.1 = phi i32 [ %state.0, %ehcleanup ], [ 1, %entry ]
-  %1 = catchpad [i8* null, i32 u0x40, i8* null] to label %catch unwind label %catchendblock
+  %cs1 = catchswitch within none [label %catch] unwind to caller
 
 catch:                                            ; preds = %catch.dispatch
-  catchret %1 to label %return
-
-catchendblock:                                    ; preds = %catch.dispatch
-  catchendpad unwind to caller
+  %1 = catchpad within %cs1 [i8* null, i32 u0x40, i8* null]
+  catchret from %1 to label %return
 
 return:                                           ; preds = %invoke.cont.1, %catch
   %retval.0 = phi i32 [ %state.1, %catch ], [ 0, %invoke.cont.1 ]
@@ -435,7 +420,7 @@
 ; should have an incoming value entry for path from 'foo' that references the
 ; PHI node itself.
 ;
-; CHECK: define void @f8()
+; CHECK-LABEL: define void @f8()
 ; CHECK: entry:
 ; CHECK:   invoke void @g()
 ; CHECK: invoke.cont:
@@ -456,18 +441,16 @@
 
 ehcleanup:                                        ; preds = %invoke.cont, %entry
   %x = phi i32 [ 2, %invoke.cont ], [ 1, %entry ]
-  %0 = cleanuppad []
-  cleanupret %0 unwind label %catch.dispatch
+  %0 = cleanuppad within none []
+  cleanupret from %0 unwind label %catch.dispatch
 
 catch.dispatch:                                   ; preds = %ehcleanup, %catch.cont
-  %1 = catchpad [i8* null, i32 u0x40, i8* null] to label %catch unwind label %catchendblock
+  %cs1 = catchswitch within none [label %catch] unwind to caller
 
 catch:                                            ; preds = %catch.dispatch
+  %1 = catchpad within %cs1 [i8* null, i32 u0x40, i8* null]
   call void @use_x(i32 %x)
-  catchret %1 to label %catch.cont
-
-catchendblock:                                    ; preds = %catch.dispatch
-  catchendpad unwind to caller
+  catchret from %1 to label %catch.cont
 
 catch.cont:                                       ; preds = %catch
   invoke void @g()
diff --git a/llvm/test/Transforms/SimplifyCFG/wineh-unreachable.ll b/llvm/test/Transforms/SimplifyCFG/wineh-unreachable.ll
index 31d567f..aa1feb7 100644
--- a/llvm/test/Transforms/SimplifyCFG/wineh-unreachable.ll
+++ b/llvm/test/Transforms/SimplifyCFG/wineh-unreachable.ll
@@ -12,7 +12,7 @@
 exit:
   ret void
 unreachable.unwind:
-  cleanuppad []
+  cleanuppad within none []
   unreachable  
 }
 
@@ -22,24 +22,21 @@
   invoke void @f()
     to label %exit unwind label %catch.pad
 catch.pad:
-  ; CHECK: catchpad []
-  ; CHECK-NEXT: to label %catch.body unwind label %catch.end
-  %catch = catchpad []
-    to label %catch.body unwind label %catch.end
+  %cs1 = catchswitch within none [label %catch.body] unwind label %unreachable.unwind
+  ; CHECK: catch.pad:
+  ; CHECK-NEXT: catchswitch within none [label %catch.body] unwind to caller
 catch.body:
   ; CHECK:      catch.body:
+  ; CHECK-NEXT:   catchpad within %cs1
   ; CHECK-NEXT:   call void @f()
   ; CHECK-NEXT:   unreachable
+  %catch = catchpad within %cs1 []
   call void @f()
-  catchret %catch to label %unreachable
-catch.end:
-  ; CHECK: catch.end:
-  ; CHECK-NEXT: catchendpad unwind to caller
-  catchendpad unwind label %unreachable.unwind
+  catchret from %catch to label %unreachable
 exit:
   ret void
 unreachable.unwind:
-  cleanuppad []
+  cleanuppad within none []
   unreachable
 unreachable:
   unreachable
@@ -51,24 +48,20 @@
   invoke void @f()
     to label %exit unwind label %cleanup.pad
 cleanup.pad:
-  ; CHECK: %cleanup = cleanuppad []
+  ; CHECK: %cleanup = cleanuppad within none []
   ; CHECK-NEXT: call void @f()
   ; CHECK-NEXT: unreachable
-  %cleanup = cleanuppad []
+  %cleanup = cleanuppad within none []
   invoke void @f()
-    to label %cleanup.ret unwind label %cleanup.end
+    to label %cleanup.ret unwind label %unreachable.unwind
 cleanup.ret:
   ; This cleanupret should be rewritten to unreachable,
   ; and merged into the pred block.
-  cleanupret %cleanup unwind label %unreachable.unwind
-cleanup.end:
-  ; This cleanupendpad should be rewritten to unreachable,
-  ; causing the invoke to be rewritten to a call.
-  cleanupendpad %cleanup unwind label %unreachable.unwind
+  cleanupret from %cleanup unwind label %unreachable.unwind
 exit:
   ret void
 unreachable.unwind:
-  cleanuppad []
+  cleanuppad within none []
   unreachable
 }
 
@@ -78,12 +71,12 @@
   invoke void @f()
     to label %exit unwind label %terminate.pad
 terminate.pad:
-  ; CHECK: terminatepad [] unwind to caller
-  terminatepad [] unwind label %unreachable.unwind
+  ; CHECK: terminatepad within none [] unwind to caller
+  terminatepad within none [] unwind label %unreachable.unwind
 exit:
   ret void
 unreachable.unwind:
-  cleanuppad []
+  cleanuppad within none []
   unreachable
 }
 
@@ -94,14 +87,11 @@
           to label %exit unwind label %catch.pad
 
 catch.pad:
-  %catch = catchpad []
-          to label %catch.body unwind label %catch.end
+  %cs1 = catchswitch within none [label %catch.body] unwind to caller
 
 catch.body:
-  catchret %catch to label %exit
-
-catch.end:
-  catchendpad unwind to caller
+  %catch = catchpad within %cs1 []
+  catchret from %catch to label %exit
 
 exit:
   unreachable
diff --git a/llvm/test/Transforms/Sink/catchswitch.ll b/llvm/test/Transforms/Sink/catchswitch.ll
new file mode 100644
index 0000000..2648f85
--- /dev/null
+++ b/llvm/test/Transforms/Sink/catchswitch.ll
@@ -0,0 +1,37 @@
+; RUN: opt -sink -S < %s | FileCheck %s
+
+target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32"
+target triple = "i686-pc-windows-msvc"
+
+define void @h() personality i32 (...)* @__CxxFrameHandler3 {
+entry:
+  %call = call i32 @g(i32 1) readnone
+  invoke void @_CxxThrowException(i8* null, i8* null) noreturn
+          to label %unreachable unwind label %catch.dispatch
+
+catch.dispatch:                                   ; preds = %entry
+  %cs = catchswitch within none [label %catch] unwind to caller
+
+catch:                                            ; preds = %catch.dispatch
+  %cp = catchpad within %cs [i8* null, i32 64, i8* null]
+  catchret from %cp to label %try.cont
+
+try.cont:                                         ; preds = %catch
+  call void @k(i32 %call)
+  ret void
+
+unreachable:                                      ; preds = %entry
+  unreachable
+}
+
+declare x86_stdcallcc void @_CxxThrowException(i8*, i8*)
+
+declare i32 @__CxxFrameHandler3(...)
+
+declare i32 @g(i32) readnone
+
+declare void @k(i32)
+
+; CHECK-LABEL: define void @h(
+; CHECK: call i32 @g(i32 1)
+; CHECK-NEXT: invoke void @_CxxThrowException(