[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/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