1; RUN: opt < %s -passes=instcombine -S | FileCheck %s
2
3target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128-ni:1"
4target triple = "wasm32-unknown-unknown"
5
6%struct.quux = type { i32 }
7%struct.blam = type <{ %struct.quux }>
8
9declare void @foo()
10declare void @bar(%struct.quux*)
11declare i32 @baz()
12declare i32 @__gxx_wasm_personality_v0(...)
13; Function Attrs: noreturn
14declare void @llvm.wasm.rethrow() #0
15
16; Test that a PHI in catchswitch BB are excluded from combining into a non-PHI
17; instruction.
18define void @test0(i1 %c1) personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
19bb:
20  %tmp0 = alloca %struct.blam, align 4
21  br i1 %c1, label %bb1, label %bb2
22
23bb1:                                              ; preds = %bb
24  %tmp1 = getelementptr inbounds %struct.blam, %struct.blam* %tmp0, i32 0, i32 0
25  invoke void @foo()
26          to label %bb3 unwind label %bb4
27
28bb2:                                              ; preds = %bb
29  %tmp2 = getelementptr inbounds %struct.blam, %struct.blam* %tmp0, i32 0, i32 0
30  invoke void @foo()
31          to label %bb3 unwind label %bb4
32
33bb3:                                              ; preds = %bb2, %bb1
34  unreachable
35
36bb4:                                              ; preds = %bb2, %bb1
37  ; This PHI should not be combined into a non-PHI instruction, because
38  ; catchswitch BB cannot have any non-PHI instruction other than catchswitch
39  ; itself.
40  ; CHECK: bb4:
41  ; CHECK-NEXT: phi
42  ; CHECK-NEXT: catchswitch
43  %tmp3 = phi %struct.quux* [ %tmp1, %bb1 ], [ %tmp2, %bb2 ]
44  %tmp4 = catchswitch within none [label %bb5] unwind label %bb7
45
46bb5:                                              ; preds = %bb4
47  %tmp5 = catchpad within %tmp4 [i8* null]
48  invoke void @foo() [ "funclet"(token %tmp5) ]
49          to label %bb6 unwind label %bb7
50
51bb6:                                              ; preds = %bb5
52  unreachable
53
54bb7:                                              ; preds = %bb5, %bb4
55  %tmp6 = cleanuppad within none []
56  call void @bar(%struct.quux* %tmp3) [ "funclet"(token %tmp6) ]
57  unreachable
58}
59
60; Test that slicing-up of illegal integer type PHI does not happen in catchswitch
61; BBs, which can't have any non-PHI instruction before the catchswitch.
62define void @test1() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
63entry:
64  invoke void @foo()
65          to label %invoke.cont unwind label %catch.dispatch1
66
67invoke.cont:                                      ; preds = %entry
68  %call = invoke i32 @baz()
69          to label %invoke.cont1 unwind label %catch.dispatch
70
71invoke.cont1:                                     ; preds = %invoke.cont
72  %tobool = icmp ne i32 %call, 0
73  br i1 %tobool, label %if.then, label %if.end
74
75if.then:                                          ; preds = %invoke.cont1
76  br label %if.end
77
78if.end:                                           ; preds = %if.then, %invoke.cont1
79  %ap.0 = phi i8 [ 1, %if.then ], [ 0, %invoke.cont1 ]
80  invoke void @foo()
81          to label %invoke.cont2 unwind label %catch.dispatch
82
83invoke.cont2:                                     ; preds = %if.end
84  br label %try.cont
85
86catch.dispatch:                                   ; preds = %if.end, %invoke.cont
87  ; %ap.2 in catch.dispatch1 BB has an illegal integer type (i8) in the data
88  ; layout, and it is only used by trunc or trunc(lshr) operations. In this case
89  ; InstCombine will split this PHI in its predecessors, which include this
90  ; catch.dispatch BB. This splitting involves creating non-PHI instructions,
91  ; such as 'and' or 'icmp' in this BB, which is not valid for a catchswitch BB.
92  ; So if one of sliced-up PHI's predecessor is a catchswitch block, we don't
93  ; optimize that case and bail out. This BB should be preserved intact after
94  ; InstCombine and the pass shouldn't produce invalid code.
95  ; CHECK: catch.dispatch:
96  ; CHECK-NEXT: phi
97  ; CHECK-NEXT: catchswitch
98  %ap.1 = phi i8 [ %ap.0, %if.end ], [ 0, %invoke.cont ]
99  %tmp0 = catchswitch within none [label %catch.start] unwind label %catch.dispatch1
100
101catch.start:                                      ; preds = %catch.dispatch
102  %tmp1 = catchpad within %tmp0 [i8* null]
103  br i1 0, label %catch, label %rethrow
104
105catch:                                            ; preds = %catch.start
106  catchret from %tmp1 to label %try.cont
107
108rethrow:                                          ; preds = %catch.start
109  invoke void @llvm.wasm.rethrow() #0 [ "funclet"(token %tmp1) ]
110          to label %unreachable unwind label %catch.dispatch1
111
112catch.dispatch1:                                  ; preds = %rethrow, %catch.dispatch, %entry
113  %ap.2 = phi i8 [ %ap.1, %catch.dispatch ], [ %ap.1, %rethrow ], [ 0, %entry ]
114  %tmp2 = catchswitch within none [label %catch.start1] unwind to caller
115
116catch.start1:                                     ; preds = %catch.dispatch1
117  %tmp3 = catchpad within %tmp2 [i8* null]
118  %tobool1 = trunc i8 %ap.2 to i1
119  br i1 %tobool1, label %if.then1, label %if.end1
120
121if.then1:                                         ; preds = %catch.start1
122  br label %if.end1
123
124if.end1:                                          ; preds = %if.then1, %catch.start1
125  catchret from %tmp3 to label %try.cont
126
127try.cont:                                         ; preds = %if.end1, %catch, %invoke.cont2
128  ret void
129
130unreachable:                                      ; preds = %rethrow
131  unreachable
132}
133
134attributes #0 = { noreturn }
135