[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/CodeGen/X86/win32-seh-catchpad.ll b/llvm/test/CodeGen/X86/win32-seh-catchpad.ll
index 4e373af..88dea36 100644
--- a/llvm/test/CodeGen/X86/win32-seh-catchpad.ll
+++ b/llvm/test/CodeGen/X86/win32-seh-catchpad.ll
@@ -11,10 +11,11 @@
           to label %invoke.cont unwind label %catch.dispatch
 
 catch.dispatch:                                   ; preds = %entry
-  %0 = catchpad [i8* bitcast (i32 ()* @try_except_filter_catchall to i8*)] to label %__except.ret unwind label %catchendblock
+  %cs1 = catchswitch within none [label %__except.ret] unwind to caller
 
 __except.ret:                                     ; preds = %catch.dispatch
-  catchret %0 to label %__except
+  %0 = catchpad within %cs1 [i8* bitcast (i32 ()* @try_except_filter_catchall to i8*)]
+  catchret from %0 to label %__except
 
 __except:                                         ; preds = %__except.ret
   call void @f(i32 2)
@@ -24,9 +25,6 @@
   call void @f(i32 3)
   ret void
 
-catchendblock:                                    ; preds = %catch.dispatch
-  catchendpad unwind to caller
-
 invoke.cont:                                      ; preds = %entry
   br label %__try.cont
 }
@@ -77,81 +75,69 @@
           to label %__try.cont unwind label %catch.dispatch
 
 catch.dispatch:                                   ; preds = %entry
-  %0 = catchpad [i8* bitcast (i32 ()* @nested_exceptions_filter_catchall to i8*)] to label %__except.ret unwind label %catchendblock
+  %cs1 = catchswitch within none [label %__except.ret] unwind label %catch.dispatch.11
 
 __except.ret:                                     ; preds = %catch.dispatch
-  catchret %0 to label %__try.cont
+  %0 = catchpad within %cs1 [i8* bitcast (i32 ()* @nested_exceptions_filter_catchall to i8*)]
+  catchret from %0 to label %__try.cont
 
 __try.cont:                                       ; preds = %entry, %__except.ret
   invoke void @crash() #3
           to label %__try.cont.9 unwind label %catch.dispatch.5
 
 catch.dispatch.5:                                 ; preds = %__try.cont
-  %1 = catchpad [i8* bitcast (i32 ()* @nested_exceptions_filter_catchall to i8*)] to label %__except.ret.7 unwind label %catchendblock.6
+  %cs2 = catchswitch within none [label %__except.ret.7] unwind label %catch.dispatch.11
 
 __except.ret.7:                                   ; preds = %catch.dispatch.5
-  catchret %1 to label %__try.cont.9
+  %1 = catchpad within %cs2 [i8* bitcast (i32 ()* @nested_exceptions_filter_catchall to i8*)]
+  catchret from %1 to label %__try.cont.9
 
 __try.cont.9:                                     ; preds = %__try.cont, %__except.ret.7
   invoke void @crash() #3
           to label %__try.cont.15 unwind label %catch.dispatch.11
 
 catch.dispatch.11:                                ; preds = %catchendblock, %catchendblock.6, %__try.cont.9
-  %2 = catchpad [i8* bitcast (i32 ()* @nested_exceptions_filter_catchall to i8*)] to label %__except.ret.13 unwind label %catchendblock.12
+  %cs3 = catchswitch within none [label %__except.ret.13] unwind label %catch.dispatch.17
 
 __except.ret.13:                                  ; preds = %catch.dispatch.11
-  catchret %2 to label %__try.cont.15
+  %2 = catchpad within %cs3 [i8* bitcast (i32 ()* @nested_exceptions_filter_catchall to i8*)]
+  catchret from %2 to label %__try.cont.15
 
 __try.cont.15:                                    ; preds = %__try.cont.9, %__except.ret.13
   invoke void @crash() #3
           to label %__try.cont.35 unwind label %catch.dispatch.17
 
 catch.dispatch.17:                                ; preds = %catchendblock.12, %__try.cont.15
-  %3 = catchpad [i8* bitcast (i32 ()* @nested_exceptions_filter_catchall to i8*)] to label %__except.ret.19 unwind label %catchendblock.18
+  %cs4 = catchswitch within none [label %__except.ret.19] unwind to caller
 
 __except.ret.19:                                  ; preds = %catch.dispatch.17
-  catchret %3 to label %__except.20
+  %3 = catchpad within %cs4 [i8* bitcast (i32 ()* @nested_exceptions_filter_catchall to i8*)]
+  catchret from %3 to label %__except.20
 
 __except.20:                                      ; preds = %__except.ret.19
   invoke void @crash() #3
           to label %__try.cont.27 unwind label %catch.dispatch.23
 
 catch.dispatch.23:                                ; preds = %__except.20
-  %4 = catchpad [i8* bitcast (i32 ()* @nested_exceptions_filter_catchall to i8*)] to label %__except.ret.25 unwind label %catchendblock.24
+  %cs5 = catchswitch within none [label %__except.ret.25] unwind to caller
 
 __except.ret.25:                                  ; preds = %catch.dispatch.23
-  catchret %4 to label %__try.cont.27
+  %4 = catchpad within %cs5 [i8* bitcast (i32 ()* @nested_exceptions_filter_catchall to i8*)]
+  catchret from %4 to label %__try.cont.27
 
 __try.cont.27:                                    ; preds = %__except.20, %__except.ret.25
   invoke void @crash() #3
           to label %__try.cont.35 unwind label %catch.dispatch.30
 
 catch.dispatch.30:                                ; preds = %__try.cont.27
-  %5 = catchpad [i8* bitcast (i32 ()* @nested_exceptions_filter_catchall to i8*)] to label %__except.ret.32 unwind label %catchendblock.31
+  %cs6 = catchswitch within none [label %__except.ret.32] unwind to caller
 
 __except.ret.32:                                  ; preds = %catch.dispatch.30
-  catchret %5 to label %__try.cont.35
+  %5 = catchpad within %cs6 [i8* bitcast (i32 ()* @nested_exceptions_filter_catchall to i8*)]
+  catchret from %5 to label %__try.cont.35
 
 __try.cont.35:                                    ; preds = %__try.cont.15, %__try.cont.27, %__except.ret.32
   ret void
-
-catchendblock.31:                                 ; preds = %catch.dispatch.30
-  catchendpad unwind to caller
-
-catchendblock.24:                                 ; preds = %catch.dispatch.23
-  catchendpad unwind to caller
-
-catchendblock.18:                                 ; preds = %catch.dispatch.17
-  catchendpad unwind to caller
-
-catchendblock.12:                                 ; preds = %catch.dispatch.11
-  catchendpad unwind label %catch.dispatch.17
-
-catchendblock.6:                                  ; preds = %catch.dispatch.5
-  catchendpad unwind label %catch.dispatch.11
-
-catchendblock:                                    ; preds = %catch.dispatch
-  catchendpad unwind label %catch.dispatch.11
 }
 
 ; This table is equivalent to the one produced by MSVC, even if it isn't in
@@ -162,19 +148,19 @@
 ; CHECK:         .long   -1
 ; CHECK:         .long   _nested_exceptions_filter_catchall
 ; CHECK:         .long   LBB
-; CHECK:         .long   -1
+; CHECK:         .long   0
+; CHECK:         .long   _nested_exceptions_filter_catchall
+; CHECK:         .long   LBB
+; CHECK:         .long   1
+; CHECK:         .long   _nested_exceptions_filter_catchall
+; CHECK:         .long   LBB
+; CHECK:         .long   1
 ; CHECK:         .long   _nested_exceptions_filter_catchall
 ; CHECK:         .long   LBB
 ; CHECK:         .long   -1
 ; CHECK:         .long   _nested_exceptions_filter_catchall
 ; CHECK:         .long   LBB
-; CHECK:         .long   2
-; CHECK:         .long   _nested_exceptions_filter_catchall
-; CHECK:         .long   LBB
-; CHECK:         .long   3
-; CHECK:         .long   _nested_exceptions_filter_catchall
-; CHECK:         .long   LBB
-; CHECK:         .long   3
+; CHECK:         .long   -1
 ; CHECK:         .long   _nested_exceptions_filter_catchall
 ; CHECK:         .long   LBB
 
@@ -203,21 +189,19 @@
           to label %__except unwind label %catch.dispatch
 
 catch.dispatch:                                   ; preds = %entry
-  %0 = catchpad [i8* bitcast (i32 ()* @try_except_filter_catchall to i8*)] to label %__except.ret unwind label %catchendblock
+  %cs1 = catchswitch within none [label %__except.ret] unwind to caller
 
 __except.ret:                                     ; preds = %catch.dispatch
+  %0 = catchpad within %cs1 [i8* bitcast (i32 ()* @try_except_filter_catchall to i8*)]
   call void @f(i32 2)
-  catchret %0 to label %__except
+  catchret from %0 to label %__except
 
 __except:
   ret void
-
-catchendblock:                                    ; preds = %catch.dispatch
-  catchendpad unwind to caller
 }
 
 ; CHECK-LABEL: _code_in_catchpad:
-; CHECK: # %catch.dispatch
+; CHECK: # %__except.ret
 ; CHECK-NEXT:         movl    -24(%ebp), %esp
 ; CHECK-NEXT:         addl    $12, %ebp
 ; CHECK-NEXT:         movl    $-1, -16(%ebp)