1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128-ni:1"
5 target triple = "wasm32-unknown-unknown"
7 %struct.quux = type { i32 }
8 %struct.blam = type <{ i32, %struct.quux }>
11 declare void @bar(ptr)
13 declare i32 @__gxx_wasm_personality_v0(...)
14 ; Function Attrs: noreturn
15 declare void @llvm.wasm.rethrow() #0
17 ; Test that a PHI in catchswitch BB are excluded from combining into a non-PHI
19 define void @test0(i1 %c1) personality ptr @__gxx_wasm_personality_v0 {
20 ; CHECK-LABEL: @test0(
22 ; CHECK-NEXT: [[TMP0:%.*]] = alloca [[STRUCT_BLAM:%.*]], align 4
23 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[BB1:%.*]], label [[BB2:%.*]]
25 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds [[STRUCT_BLAM]], ptr [[TMP0]], i32 0, i32 1
26 ; CHECK-NEXT: invoke void @foo()
27 ; CHECK-NEXT: to label [[BB3:%.*]] unwind label [[BB4:%.*]]
29 ; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds [[STRUCT_BLAM]], ptr [[TMP0]], i32 0, i32 1
30 ; CHECK-NEXT: invoke void @foo()
31 ; CHECK-NEXT: to label [[BB3]] unwind label [[BB4]]
33 ; CHECK-NEXT: unreachable
35 ; CHECK-NEXT: [[TMP3:%.*]] = phi ptr [ [[TMP1]], [[BB1]] ], [ [[TMP2]], [[BB2]] ]
36 ; CHECK-NEXT: [[TMP4:%.*]] = catchswitch within none [label %bb5] unwind label [[BB7:%.*]]
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]]
42 ; CHECK-NEXT: unreachable
44 ; CHECK-NEXT: [[TMP6:%.*]] = cleanuppad within none []
45 ; CHECK-NEXT: call void @bar(ptr nonnull [[TMP3]]) [ "funclet"(token [[TMP6]]) ]
46 ; CHECK-NEXT: unreachable
49 %tmp0 = alloca %struct.blam, align 4
50 br i1 %c1, label %bb1, label %bb2
53 %tmp1 = getelementptr inbounds %struct.blam, ptr %tmp0, i32 0, i32 1
55 to label %bb3 unwind label %bb4
58 %tmp2 = getelementptr inbounds %struct.blam, ptr %tmp0, i32 0, i32 1
60 to label %bb3 unwind label %bb4
62 bb3: ; preds = %bb2, %bb1
65 bb4: ; 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
69 %tmp3 = phi ptr [ %tmp1, %bb1 ], [ %tmp2, %bb2 ]
70 %tmp4 = catchswitch within none [label %bb5] unwind label %bb7
73 %tmp5 = catchpad within %tmp4 [ptr null]
74 invoke void @foo() [ "funclet"(token %tmp5) ]
75 to label %bb6 unwind label %bb7
80 bb7: ; preds = %bb5, %bb4
81 %tmp6 = cleanuppad within none []
82 call void @bar(ptr %tmp3) [ "funclet"(token %tmp6) ]
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.
88 define void @test1() personality ptr @__gxx_wasm_personality_v0 {
89 ; CHECK-LABEL: @test1(
91 ; CHECK-NEXT: invoke void @foo()
92 ; CHECK-NEXT: to label [[INVOKE_CONT:%.*]] unwind label [[CATCH_DISPATCH1:%.*]]
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:%.*]]
100 ; CHECK-NEXT: br label [[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:%.*]]
114 ; CHECK-NEXT: catchret from [[TMP1]] to label [[TRY_CONT]]
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: [[TMP0:%.*]] = and i8 [[AP_2]], 1
124 ; CHECK-NEXT: [[TOBOOL1_NOT:%.*]] = icmp eq i8 [[TMP0]], 0
125 ; CHECK-NEXT: br i1 [[TOBOOL1_NOT]], label [[IF_END1:%.*]], label [[IF_THEN1:%.*]]
127 ; CHECK-NEXT: br label [[IF_END1]]
129 ; CHECK-NEXT: catchret from [[TMP3]] to label [[TRY_CONT]]
131 ; CHECK-NEXT: ret void
132 ; CHECK: unreachable:
133 ; CHECK-NEXT: unreachable
137 to label %invoke.cont unwind label %catch.dispatch1
139 invoke.cont: ; preds = %entry
140 %call = invoke i32 @baz()
141 to label %invoke.cont1 unwind label %catch.dispatch
143 invoke.cont1: ; preds = %invoke.cont
144 %tobool = icmp ne i32 %call, 0
145 br i1 %tobool, label %if.then, label %if.end
147 if.then: ; preds = %invoke.cont1
150 if.end: ; preds = %if.then, %invoke.cont1
151 %ap.0 = phi i8 [ 1, %if.then ], [ 0, %invoke.cont1 ]
153 to label %invoke.cont2 unwind label %catch.dispatch
155 invoke.cont2: ; preds = %if.end
158 catch.dispatch: ; preds = %if.end, %invoke.cont
159 ; %ap.2 in catch.dispatch1 BB has an illegal integer type (i8) in the data
160 ; layout, and it is only used by trunc or trunc(lshr) operations. In this case
161 ; InstCombine will split this PHI in its predecessors, which include this
162 ; catch.dispatch BB. This splitting involves creating non-PHI instructions,
163 ; such as 'and' or 'icmp' in this BB, which is not valid for a catchswitch BB.
164 ; So if one of sliced-up PHI's predecessor is a catchswitch block, we don't
165 ; optimize that case and bail out. This BB should be preserved intact after
166 ; InstCombine and the pass shouldn't produce invalid code.
167 %ap.1 = phi i8 [ %ap.0, %if.end ], [ 0, %invoke.cont ]
168 %tmp0 = catchswitch within none [label %catch.start] unwind label %catch.dispatch1
170 catch.start: ; preds = %catch.dispatch
171 %tmp1 = catchpad within %tmp0 [ptr null]
172 br i1 0, label %catch, label %rethrow
174 catch: ; preds = %catch.start
175 catchret from %tmp1 to label %try.cont
177 rethrow: ; preds = %catch.start
178 invoke void @llvm.wasm.rethrow() #0 [ "funclet"(token %tmp1) ]
179 to label %unreachable unwind label %catch.dispatch1
181 catch.dispatch1: ; preds = %rethrow, %catch.dispatch, %entry
182 %ap.2 = phi i8 [ %ap.1, %catch.dispatch ], [ %ap.1, %rethrow ], [ 0, %entry ]
183 %tmp2 = catchswitch within none [label %catch.start1] unwind to caller
185 catch.start1: ; preds = %catch.dispatch1
186 %tmp3 = catchpad within %tmp2 [ptr null]
187 %tobool1 = trunc i8 %ap.2 to i1
188 br i1 %tobool1, label %if.then1, label %if.end1
190 if.then1: ; preds = %catch.start1
193 if.end1: ; preds = %if.then1, %catch.start1
194 catchret from %tmp3 to label %try.cont
196 try.cont: ; preds = %if.end1, %catch, %invoke.cont2
199 unreachable: ; preds = %rethrow
203 attributes #0 = { noreturn }