[WebAssembly] Make rethrow take an except_ref type argument

Summary:
In the new wasm EH proposal, `rethrow` takes an `except_ref` argument.
This change was missing in r352598.

This patch adds `llvm.wasm.rethrow.in.catch` intrinsic. This is an
intrinsic that's gonna eventually be lowered to wasm `rethrow`
instruction, but this intrinsic can appear only within a catchpad or a
cleanuppad scope. Also this intrinsic needs to be invokable - otherwise
EH pad successor for it will not be correctly generated in clang.

This also adds lowering logic for this intrinsic in
`SelectionDAGBuilder::visitInvoke`. This routine is basically a
specialized and simplified version of
`SelectionDAGBuilder::visitTargetIntrinsic`, but we can't use it
because if is only for `CallInst`s.

This deletes the previous `llvm.wasm.rethrow` intrinsic and related
tests, which was meant to be used within a `__cxa_rethrow` library
function. Turned out this needs some more logic, so the intrinsic for
this purpose will be added later.

LateEHPrepare takes a result value of `catch` and inserts it into
matching `rethrow` as an argument.

`RETHROW_IN_CATCH` is a pseudo instruction that serves as a link between
`llvm.wasm.rethrow.in.catch` and the real wasm `rethrow` instruction. To
generate a `rethrow` instruction, we need an `except_ref` argument,
which is generated from `catch` instruction. But `catch` instrutions are
added in LateEHPrepare pass, so we use `RETHROW_IN_CATCH`, which takes
no argument, until we are able to correctly lower it to `rethrow` in
LateEHPrepare.

Reviewers: dschuff

Subscribers: sbc100, jgravelle-google, sunfish, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D59352

llvm-svn: 356316
diff --git a/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll b/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll
index 01bac8e..5b06a0b 100644
--- a/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll
+++ b/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll
@@ -33,7 +33,7 @@
 ; CHECK:     call      __cxa_end_catch
 ; CHECK:     br        1                               # 1: down to label0
 ; CHECK:   end_block                                   # label3:
-; CHECK:   call      __cxa_rethrow
+; CHECK:   rethrow   {{.*}}                            # to caller
 ; CHECK: end_try                                       # label0:
 define void @test0() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
 entry:
@@ -67,7 +67,7 @@
   catchret from %1 to label %try.cont
 
 rethrow:                                          ; preds = %catch.fallthrough
-  call void @__cxa_rethrow() [ "funclet"(token %1) ]
+  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
   unreachable
 
 try.cont:                                         ; preds = %entry, %catch, %catch2
@@ -108,21 +108,19 @@
 ; CHECK:               br        2                     # 2: down to label9
 ; CHECK:             catch
 ; CHECK:               call      __cxa_end_catch
-; CHECK:               rethrow                         # down to catch3
+; CHECK:               rethrow   {{.*}}                # down to catch3
 ; CHECK:             end_try
 ; CHECK:           end_block                           # label11:
-; CHECK:           call      __cxa_rethrow
-; CHECK:           unreachable
+; CHECK:           rethrow   {{.*}}                    # down to catch3
 ; CHECK:         catch     {{.*}}                      # catch3:
 ; CHECK:           call      __cxa_end_catch
-; CHECK:           rethrow                             # to caller
+; CHECK:           rethrow   {{.*}}                    # to caller
 ; CHECK:         end_try                               # label9:
 ; CHECK:         call      __cxa_end_catch
 ; CHECK:         br        2                           # 2: down to label6
 ; CHECK:       end_try
 ; CHECK:     end_block                                 # label7:
-; CHECK:     call      __cxa_rethrow
-; CHECK:     unreachable
+; CHECK:     rethrow   {{.*}}                          # to caller
 ; CHECK:   end_block                                   # label6:
 ; CHECK:   call      __cxa_end_catch
 ; CHECK: end_try
@@ -172,7 +170,7 @@
   catchret from %9 to label %try.cont
 
 rethrow5:                                         ; preds = %catch.start3
-  invoke void @__cxa_rethrow() [ "funclet"(token %9) ]
+  invoke void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %9) ]
           to label %unreachable unwind label %ehcleanup9
 
 try.cont:                                         ; preds = %catch, %invoke.cont8
@@ -180,7 +178,7 @@
   catchret from %1 to label %try.cont11
 
 rethrow:                                          ; preds = %catch.start
-  call void @__cxa_rethrow() [ "funclet"(token %1) ]
+  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
   unreachable
 
 try.cont11:                                       ; preds = %entry, %try.cont
@@ -229,7 +227,7 @@
 ; CHECK:             call      __clang_call_terminate
 ; CHECK:             unreachable
 ; CHECK:           end_try
-; CHECK:           rethrow                             # to caller
+; CHECK:           rethrow   {{.*}}                    # to caller
 ; CHECK:         end_try
 ; CHECK:       end_block                               # label17:
 ; CHECK:       call      __cxa_end_catch
@@ -293,9 +291,9 @@
 declare i32 @__gxx_wasm_personality_v0(...)
 declare i8* @llvm.wasm.get.exception(token)
 declare i32 @llvm.wasm.get.ehselector(token)
+declare void @llvm.wasm.rethrow.in.catch()
 declare i32 @llvm.eh.typeid.for(i8*)
 declare i8* @__cxa_begin_catch(i8*)
 declare void @__cxa_end_catch()
-declare void @__cxa_rethrow()
 declare void @__clang_call_terminate(i8*)
 declare void @_ZSt9terminatev()
diff --git a/llvm/test/CodeGen/WebAssembly/exception.ll b/llvm/test/CodeGen/WebAssembly/exception.ll
index f41588fa..8f7687e 100644
--- a/llvm/test/CodeGen/WebAssembly/exception.ll
+++ b/llvm/test/CodeGen/WebAssembly/exception.ll
@@ -17,14 +17,6 @@
   ret void
 }
 
-; CHECK-LABEL: test_rethrow:
-; CHECK:     rethrow
-; CHECK-NOT: unreachable
-define void @test_rethrow(i8* %p) {
-  call void @llvm.wasm.rethrow()
-  ret void
-}
-
 ; Simple test with a try-catch
 ;
 ; void foo();
@@ -43,7 +35,7 @@
 ; CHECK:       global.set  __stack_pointer
 ; CHECK:       block i32
 ; CHECK:         br_on_exn 0, __cpp_exception, $[[EXCEPT_REF]]
-; CHECK:         rethrow
+; CHECK:         rethrow   $[[EXCEPT_REF]]
 ; CHECK:       end_block
 ; CHECK:       extract_exception $[[EXN:[0-9]+]]=
 ; CHECK-DAG:   i32.store  __wasm_lpad_context
@@ -55,7 +47,7 @@
 ; CHECK:         call      __cxa_end_catch
 ; CHECK:         br        1
 ; CHECK:       end_block
-; CHECK:       call      __cxa_rethrow
+; CHECK:       rethrow   $[[EXCEPT_REF]]
 ; CHECK:     end_try
 define void @test_catch() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
 entry:
@@ -79,7 +71,7 @@
   catchret from %1 to label %try.cont
 
 rethrow:                                          ; preds = %catch.start
-  call void @__cxa_rethrow() [ "funclet"(token %1) ]
+  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
   unreachable
 
 try.cont:                                         ; preds = %entry, %catch
@@ -100,10 +92,10 @@
 ; CHECK-LABEL: test_cleanup:
 ; CHECK: try
 ; CHECK:   call      foo
-; CHECK: catch
+; CHECK: catch     $[[EXCEPT_REF:[0-9]+]]=
 ; CHECK:   global.set  __stack_pointer
 ; CHECK:   i32.call  $drop=, _ZN4TempD2Ev
-; CHECK:   rethrow
+; CHECK:   rethrow   $[[EXCEPT_REF]]
 ; CHECK: end_try
 define void @test_cleanup() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
 entry:
@@ -231,7 +223,7 @@
 ; CHECK-NOT:         global.set  __stack_pointer, $pop{{.+}}
 ; CHECK:           end_try
 ; CHECK:         end_block
-; CHECK:         call      __cxa_rethrow
+; CHECK:         rethrow
 ; CHECK:       end_block
 ; CHECK-NOT:   global.set  __stack_pointer, $pop{{.+}}
 ; CHECK:       call      __cxa_end_catch
@@ -266,7 +258,7 @@
   catchret from %1 to label %try.cont
 
 rethrow:                                          ; preds = %catch.start
-  call void @__cxa_rethrow() [ "funclet"(token %1) ]
+  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
   unreachable
 
 try.cont:                                         ; preds = %entry, %invoke.cont1
@@ -342,13 +334,12 @@
 declare void @bar(i32*)
 declare i32 @__gxx_wasm_personality_v0(...)
 declare void @llvm.wasm.throw(i32, i8*)
-declare void @llvm.wasm.rethrow()
 declare i8* @llvm.wasm.get.exception(token)
 declare i32 @llvm.wasm.get.ehselector(token)
+declare void @llvm.wasm.rethrow.in.catch()
 declare i32 @llvm.eh.typeid.for(i8*)
 declare i8* @__cxa_begin_catch(i8*)
 declare void @__cxa_end_catch()
-declare void @__cxa_rethrow()
 declare void @__clang_call_terminate(i8*)
 declare %struct.Temp* @_ZN4TempD2Ev(%struct.Temp* returned)
 
diff --git a/llvm/test/CodeGen/WebAssembly/wasmehprepare.ll b/llvm/test/CodeGen/WebAssembly/wasmehprepare.ll
index b4699ef..d06f95f 100644
--- a/llvm/test/CodeGen/WebAssembly/wasmehprepare.ll
+++ b/llvm/test/CodeGen/WebAssembly/wasmehprepare.ll
@@ -54,7 +54,7 @@
 ; CHECK-NEXT:  call i8* @__cxa_begin_catch(i8* %[[EXN]])
 
 rethrow:                                          ; preds = %catch.start
-  call void @__cxa_rethrow() [ "funclet"(token %1) ]
+  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
   unreachable
 
 try.cont:                                         ; preds = %entry, %catch
@@ -125,7 +125,7 @@
   catchret from %6 to label %try.cont7
 
 rethrow:                                          ; preds = %catch.start3
-  call void @__cxa_rethrow() [ "funclet"(token %6) ]
+  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %6) ]
   unreachable
 
 try.cont7:                                        ; preds = %try.cont, %catch4
@@ -189,7 +189,7 @@
   catchret from %7 to label %try.cont
 
 rethrow5:                                         ; preds = %catch.start3
-  invoke void @__cxa_rethrow() [ "funclet"(token %7) ]
+  invoke void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %7) ]
           to label %unreachable unwind label %ehcleanup
 
 try.cont:                                         ; preds = %catch, %catch6
@@ -197,7 +197,7 @@
   catchret from %1 to label %try.cont9
 
 rethrow:                                          ; preds = %catch.start
-  call void @__cxa_rethrow() [ "funclet"(token %1) ]
+  call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
   unreachable
 
 try.cont9:                                        ; preds = %entry, %try.cont
@@ -392,34 +392,6 @@
   ret i32 0
 }
 
-; Tests if instructions after a call to @llvm.wasm.rethrow are deleted and the
-; BB's dead children are deleted.
-
-; CHECK-LABEL: @test6
-define i32 @test6(i1 %b, i8* %p) {
-entry:
-  br i1 %b, label %bb.true, label %bb.false
-
-; CHECK:      bb.true:
-; CHECK-NEXT:   call void @llvm.wasm.rethrow()
-; CHECK-NEXT:   unreachable
-bb.true:                                          ; preds = %entry
-  call void @llvm.wasm.rethrow()
-  br label %bb.true.0
-
-; CHECK-NOT:  bb.true.0
-bb.true.0:                                        ; preds = %bb.true
-  br label %merge
-
-; CHECK:      bb.false
-bb.false:                                         ; preds = %entry
-  br label %merge
-
-; CHECK:      merge
-merge:                                            ; preds = %bb.true.0, %bb.false
-  ret i32 0
-}
-
 declare void @foo()
 declare void @bar(i32)
 declare %struct.Temp* @_ZN4TempD2Ev(%struct.Temp* returned)
@@ -428,10 +400,9 @@
 declare i32 @llvm.wasm.get.ehselector(token)
 declare i32 @llvm.eh.typeid.for(i8*)
 declare void @llvm.wasm.throw(i32, i8*)
-declare void @llvm.wasm.rethrow()
+declare void @llvm.wasm.rethrow.in.catch()
 declare i8* @__cxa_begin_catch(i8*)
 declare void @__cxa_end_catch()
-declare void @__cxa_rethrow()
 declare void @__clang_call_terminate(i8*)
 
 ; CHECK-DAG: declare void @llvm.wasm.landingpad.index(token, i32)