1 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128-ni:1"
4 target triple = "wasm32-unknown-unknown"
6 %struct.quux = type { i32 }
7 %struct.blam = type <{ %struct.quux }>
10 declare void @bar(%struct.quux*)
12 declare i32 @__gxx_wasm_personality_v0(...)
13 ; Function Attrs: noreturn
14 declare void @llvm.wasm.rethrow() #0
16 ; Test that a PHI in catchswitch BB are excluded from combining into a non-PHI
18 define void @test0(i1 %c1) personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
20 %tmp0 = alloca %struct.blam, align 4
21 br i1 %c1, label %bb1, label %bb2
24 %tmp1 = getelementptr inbounds %struct.blam, %struct.blam* %tmp0, i32 0, i32 0
26 to label %bb3 unwind label %bb4
29 %tmp2 = getelementptr inbounds %struct.blam, %struct.blam* %tmp0, i32 0, i32 0
31 to label %bb3 unwind label %bb4
33 bb3: ; preds = %bb2, %bb1
36 bb4: ; 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
42 ; CHECK-NEXT: catchswitch
43 %tmp3 = phi %struct.quux* [ %tmp1, %bb1 ], [ %tmp2, %bb2 ]
44 %tmp4 = catchswitch within none [label %bb5] unwind label %bb7
47 %tmp5 = catchpad within %tmp4 [i8* null]
48 invoke void @foo() [ "funclet"(token %tmp5) ]
49 to label %bb6 unwind label %bb7
54 bb7: ; preds = %bb5, %bb4
55 %tmp6 = cleanuppad within none []
56 call void @bar(%struct.quux* %tmp3) [ "funclet"(token %tmp6) ]
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.
62 define void @test1() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
65 to label %invoke.cont unwind label %catch.dispatch1
67 invoke.cont: ; preds = %entry
68 %call = invoke i32 @baz()
69 to label %invoke.cont1 unwind label %catch.dispatch
71 invoke.cont1: ; preds = %invoke.cont
72 %tobool = icmp ne i32 %call, 0
73 br i1 %tobool, label %if.then, label %if.end
75 if.then: ; preds = %invoke.cont1
78 if.end: ; preds = %if.then, %invoke.cont1
79 %ap.0 = phi i8 [ 1, %if.then ], [ 0, %invoke.cont1 ]
81 to label %invoke.cont2 unwind label %catch.dispatch
83 invoke.cont2: ; preds = %if.end
86 catch.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:
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
101 catch.start: ; preds = %catch.dispatch
102 %tmp1 = catchpad within %tmp0 [i8* null]
103 br i1 0, label %catch, label %rethrow
105 catch: ; preds = %catch.start
106 catchret from %tmp1 to label %try.cont
108 rethrow: ; preds = %catch.start
109 invoke void @llvm.wasm.rethrow() #0 [ "funclet"(token %tmp1) ]
110 to label %unreachable unwind label %catch.dispatch1
112 catch.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
116 catch.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
121 if.then1: ; preds = %catch.start1
124 if.end1: ; preds = %if.then1, %catch.start1
125 catchret from %tmp3 to label %try.cont
127 try.cont: ; preds = %if.end1, %catch, %invoke.cont2
130 unreachable: ; preds = %rethrow
134 attributes #0 = { noreturn }