xref: /llvm-project/llvm/test/Transforms/InstCombine/catchswitch-phi.ll (revision 462cb3cd6cecd0511ecaf0e3ebcaba455ece587d)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3
4target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128-ni:1"
5target triple = "wasm32-unknown-unknown"
6
7%struct.quux = type { i32 }
8%struct.blam = type <{ i32, %struct.quux }>
9
10declare void @foo()
11declare void @bar(ptr)
12declare i32 @baz()
13declare i32 @__gxx_wasm_personality_v0(...)
14; Function Attrs: noreturn
15declare void @llvm.wasm.rethrow() #0
16
17; Test that a PHI in catchswitch BB are excluded from combining into a non-PHI
18; instruction.
19define void @test0(i1 %c1) personality ptr @__gxx_wasm_personality_v0 {
20; CHECK-LABEL: @test0(
21; CHECK-NEXT:  bb:
22; CHECK-NEXT:    [[TMP0:%.*]] = alloca [[STRUCT_BLAM:%.*]], align 4
23; CHECK-NEXT:    br i1 [[C1:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
24; CHECK:       bb1:
25; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP0]], i32 4
26; CHECK-NEXT:    invoke void @foo()
27; CHECK-NEXT:            to label [[BB3:%.*]] unwind label [[BB4:%.*]]
28; CHECK:       bb2:
29; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP0]], i32 4
30; CHECK-NEXT:    invoke void @foo()
31; CHECK-NEXT:            to label [[BB3]] unwind label [[BB4]]
32; CHECK:       bb3:
33; CHECK-NEXT:    unreachable
34; CHECK:       bb4:
35; CHECK-NEXT:    [[TMP3:%.*]] = phi ptr [ [[TMP1]], [[BB1]] ], [ [[TMP2]], [[BB2]] ]
36; CHECK-NEXT:    [[TMP4:%.*]] = catchswitch within none [label %bb5] unwind label [[BB7:%.*]]
37; CHECK:       bb5:
38; CHECK-NEXT:    [[TMP5:%.*]] = catchpad within [[TMP4]] [ptr null]
39; CHECK-NEXT:    invoke void @foo() [ "funclet"(token [[TMP5]]) ]
40; CHECK-NEXT:            to label [[BB6:%.*]] unwind label [[BB7]]
41; CHECK:       bb6:
42; CHECK-NEXT:    unreachable
43; CHECK:       bb7:
44; CHECK-NEXT:    [[TMP6:%.*]] = cleanuppad within none []
45; CHECK-NEXT:    call void @bar(ptr nonnull [[TMP3]]) [ "funclet"(token [[TMP6]]) ]
46; CHECK-NEXT:    unreachable
47;
48bb:
49  %tmp0 = alloca %struct.blam, align 4
50  br i1 %c1, label %bb1, label %bb2
51
52bb1:                                              ; preds = %bb
53  %tmp1 = getelementptr inbounds %struct.blam, ptr %tmp0, i32 0, i32 1
54  invoke void @foo()
55  to label %bb3 unwind label %bb4
56
57bb2:                                              ; preds = %bb
58  %tmp2 = getelementptr inbounds %struct.blam, ptr %tmp0, i32 0, i32 1
59  invoke void @foo()
60  to label %bb3 unwind label %bb4
61
62bb3:                                              ; preds = %bb2, %bb1
63  unreachable
64
65bb4:                                              ; preds = %bb2, %bb1
66  ; This PHI should not be combined into a non-PHI instruction, because
67  ; catchswitch BB cannot have any non-PHI instruction other than catchswitch
68  ; itself.
69  %tmp3 = phi ptr [ %tmp1, %bb1 ], [ %tmp2, %bb2 ]
70  %tmp4 = catchswitch within none [label %bb5] unwind label %bb7
71
72bb5:                                              ; preds = %bb4
73  %tmp5 = catchpad within %tmp4 [ptr null]
74  invoke void @foo() [ "funclet"(token %tmp5) ]
75  to label %bb6 unwind label %bb7
76
77bb6:                                              ; preds = %bb5
78  unreachable
79
80bb7:                                              ; preds = %bb5, %bb4
81  %tmp6 = cleanuppad within none []
82  call void @bar(ptr %tmp3) [ "funclet"(token %tmp6) ]
83  unreachable
84}
85
86; Test that slicing-up of illegal integer type PHI does not happen in catchswitch
87; BBs, which can't have any non-PHI instruction before the catchswitch.
88define void @test1() personality ptr @__gxx_wasm_personality_v0 {
89; CHECK-LABEL: @test1(
90; CHECK-NEXT:  entry:
91; CHECK-NEXT:    invoke void @foo()
92; CHECK-NEXT:            to label [[INVOKE_CONT:%.*]] unwind label [[CATCH_DISPATCH1:%.*]]
93; CHECK:       invoke.cont:
94; CHECK-NEXT:    [[CALL:%.*]] = invoke i32 @baz()
95; CHECK-NEXT:            to label [[INVOKE_CONT1:%.*]] unwind label [[CATCH_DISPATCH:%.*]]
96; CHECK:       invoke.cont1:
97; CHECK-NEXT:    [[TOBOOL_NOT:%.*]] = icmp eq i32 [[CALL]], 0
98; CHECK-NEXT:    br i1 [[TOBOOL_NOT]], label [[IF_END:%.*]], label [[IF_THEN:%.*]]
99; CHECK:       if.then:
100; CHECK-NEXT:    br label [[IF_END]]
101; CHECK:       if.end:
102; CHECK-NEXT:    [[AP_0:%.*]] = phi i8 [ 1, [[IF_THEN]] ], [ 0, [[INVOKE_CONT1]] ]
103; CHECK-NEXT:    invoke void @foo()
104; CHECK-NEXT:            to label [[INVOKE_CONT2:%.*]] unwind label [[CATCH_DISPATCH]]
105; CHECK:       invoke.cont2:
106; CHECK-NEXT:    br label [[TRY_CONT:%.*]]
107; CHECK:       catch.dispatch:
108; CHECK-NEXT:    [[AP_1:%.*]] = phi i8 [ [[AP_0]], [[IF_END]] ], [ 0, [[INVOKE_CONT]] ]
109; CHECK-NEXT:    [[TMP0:%.*]] = catchswitch within none [label %catch.start] unwind label [[CATCH_DISPATCH1]]
110; CHECK:       catch.start:
111; CHECK-NEXT:    [[TMP1:%.*]] = catchpad within [[TMP0]] [ptr null]
112; CHECK-NEXT:    br i1 false, label [[CATCH:%.*]], label [[RETHROW:%.*]]
113; CHECK:       catch:
114; CHECK-NEXT:    catchret from [[TMP1]] to label [[TRY_CONT]]
115; CHECK:       rethrow:
116; CHECK-NEXT:    invoke void @llvm.wasm.rethrow() #[[ATTR0:[0-9]+]] [ "funclet"(token [[TMP1]]) ]
117; CHECK-NEXT:            to label [[UNREACHABLE:%.*]] unwind label [[CATCH_DISPATCH1]]
118; CHECK:       catch.dispatch1:
119; CHECK-NEXT:    [[AP_2:%.*]] = phi i8 [ [[AP_1]], [[CATCH_DISPATCH]] ], [ [[AP_1]], [[RETHROW]] ], [ 0, [[ENTRY:%.*]] ]
120; CHECK-NEXT:    [[TMP2:%.*]] = catchswitch within none [label %catch.start1] unwind to caller
121; CHECK:       catch.start1:
122; CHECK-NEXT:    [[TMP3:%.*]] = catchpad within [[TMP2]] [ptr null]
123; CHECK-NEXT:    [[TOBOOL1:%.*]] = trunc i8 [[AP_2]] to i1
124; CHECK-NEXT:    br i1 [[TOBOOL1]], label [[IF_THEN1:%.*]], label [[IF_END1:%.*]]
125; CHECK:       if.then1:
126; CHECK-NEXT:    br label [[IF_END1]]
127; CHECK:       if.end1:
128; CHECK-NEXT:    catchret from [[TMP3]] to label [[TRY_CONT]]
129; CHECK:       try.cont:
130; CHECK-NEXT:    ret void
131; CHECK:       unreachable:
132; CHECK-NEXT:    unreachable
133;
134entry:
135  invoke void @foo()
136  to label %invoke.cont unwind label %catch.dispatch1
137
138invoke.cont:                                      ; preds = %entry
139  %call = invoke i32 @baz()
140  to label %invoke.cont1 unwind label %catch.dispatch
141
142invoke.cont1:                                     ; preds = %invoke.cont
143  %tobool = icmp ne i32 %call, 0
144  br i1 %tobool, label %if.then, label %if.end
145
146if.then:                                          ; preds = %invoke.cont1
147  br label %if.end
148
149if.end:                                           ; preds = %if.then, %invoke.cont1
150  %ap.0 = phi i8 [ 1, %if.then ], [ 0, %invoke.cont1 ]
151  invoke void @foo()
152  to label %invoke.cont2 unwind label %catch.dispatch
153
154invoke.cont2:                                     ; preds = %if.end
155  br label %try.cont
156
157catch.dispatch:                                   ; preds = %if.end, %invoke.cont
158  ; %ap.2 in catch.dispatch1 BB has an illegal integer type (i8) in the data
159  ; layout, and it is only used by trunc or trunc(lshr) operations. In this case
160  ; InstCombine will split this PHI in its predecessors, which include this
161  ; catch.dispatch BB. This splitting involves creating non-PHI instructions,
162  ; such as 'and' or 'icmp' in this BB, which is not valid for a catchswitch BB.
163  ; So if one of sliced-up PHI's predecessor is a catchswitch block, we don't
164  ; optimize that case and bail out. This BB should be preserved intact after
165  ; InstCombine and the pass shouldn't produce invalid code.
166  %ap.1 = phi i8 [ %ap.0, %if.end ], [ 0, %invoke.cont ]
167  %tmp0 = catchswitch within none [label %catch.start] unwind label %catch.dispatch1
168
169catch.start:                                      ; preds = %catch.dispatch
170  %tmp1 = catchpad within %tmp0 [ptr null]
171  br i1 0, label %catch, label %rethrow
172
173catch:                                            ; preds = %catch.start
174  catchret from %tmp1 to label %try.cont
175
176rethrow:                                          ; preds = %catch.start
177  invoke void @llvm.wasm.rethrow() #0 [ "funclet"(token %tmp1) ]
178  to label %unreachable unwind label %catch.dispatch1
179
180catch.dispatch1:                                  ; preds = %rethrow, %catch.dispatch, %entry
181  %ap.2 = phi i8 [ %ap.1, %catch.dispatch ], [ %ap.1, %rethrow ], [ 0, %entry ]
182  %tmp2 = catchswitch within none [label %catch.start1] unwind to caller
183
184catch.start1:                                     ; preds = %catch.dispatch1
185  %tmp3 = catchpad within %tmp2 [ptr null]
186  %tobool1 = trunc i8 %ap.2 to i1
187  br i1 %tobool1, label %if.then1, label %if.end1
188
189if.then1:                                         ; preds = %catch.start1
190  br label %if.end1
191
192if.end1:                                          ; preds = %if.then1, %catch.start1
193  catchret from %tmp3 to label %try.cont
194
195try.cont:                                         ; preds = %if.end1, %catch, %invoke.cont2
196  ret void
197
198unreachable:                                      ; preds = %rethrow
199  unreachable
200}
201
202attributes #0 = { noreturn }
203