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