blob: 12763a06127ddf74df25266f90e478989d826e0b [file] [log] [blame]
Derek Schuffccdceda2016-08-18 15:27:25 +00001; RUN: opt < %s -wasm-lower-em-ehsjlj -S | FileCheck %s
Derek Schufff41f67d2016-08-01 21:34:04 +00002
Heejin Ahnc0f18172016-09-01 21:05:15 +00003target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
Sam Clegga5908002018-05-10 17:49:11 +00004target triple = "wasm32-unknown-unknown"
Heejin Ahnc0f18172016-09-01 21:05:15 +00005
Derek Schufff41f67d2016-08-01 21:34:04 +00006@_ZTIi = external constant i8*
7@_ZTIc = external constant i8*
Sam Clegg28b3e992018-07-17 16:40:03 +00008; CHECK-DAG: __THREW__ = weak_odr global i32 0
9; CHECK-DAG: __threwValue = weak_odr global i32 0
10; CHECK-DAG: __tempRet0 = weak_odr global i32 0
Derek Schufff41f67d2016-08-01 21:34:04 +000011
12; Test invoke instruction with clauses (try-catch block)
13define void @clause() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
14; CHECK-LABEL: @clause(
15entry:
16 invoke void @foo(i32 3)
17 to label %invoke.cont unwind label %lpad
18; CHECK: entry:
Sam Clegg28b3e992018-07-17 16:40:03 +000019; CHECK-NEXT: store i32 0, i32* @__THREW__
Derek Schufff41f67d2016-08-01 21:34:04 +000020; CHECK-NEXT: call void @__invoke_void_i32(void (i32)* @foo, i32 3)
Sam Clegg28b3e992018-07-17 16:40:03 +000021; CHECK-NEXT: %[[__THREW__VAL:.*]] = load i32, i32* @__THREW__
22; CHECK-NEXT: store i32 0, i32* @__THREW__
Heejin Ahnc0f18172016-09-01 21:05:15 +000023; CHECK-NEXT: %cmp = icmp eq i32 %[[__THREW__VAL]], 1
24; CHECK-NEXT: br i1 %cmp, label %lpad, label %invoke.cont
Derek Schufff41f67d2016-08-01 21:34:04 +000025
26invoke.cont: ; preds = %entry
27 br label %try.cont
28
29lpad: ; preds = %entry
30 %0 = landingpad { i8*, i32 }
31 catch i8* bitcast (i8** @_ZTIi to i8*)
32 catch i8* null
33 %1 = extractvalue { i8*, i32 } %0, 0
34 %2 = extractvalue { i8*, i32 } %0, 1
35 br label %catch.dispatch
36; CHECK: lpad:
Derek Schuff53b9af02016-08-09 00:29:55 +000037; CHECK-NEXT: %[[FMC:.*]] = call i8* @__cxa_find_matching_catch_4(i8* bitcast (i8** @_ZTIi to i8*), i8* null)
Derek Schufff41f67d2016-08-01 21:34:04 +000038; CHECK-NEXT: %[[IVI1:.*]] = insertvalue { i8*, i32 } undef, i8* %[[FMC]], 0
Sam Clegg28b3e992018-07-17 16:40:03 +000039; CHECK-NEXT: %[[TEMPRET0_VAL:.*]] = load i32, i32* @__tempRet0
Derek Schufff41f67d2016-08-01 21:34:04 +000040; CHECK-NEXT: %[[IVI2:.*]] = insertvalue { i8*, i32 } %[[IVI1]], i32 %[[TEMPRET0_VAL]], 1
41; CHECK-NEXT: extractvalue { i8*, i32 } %[[IVI2]], 0
Derek Schuff53b9af02016-08-09 00:29:55 +000042; CHECK-NEXT: %[[CDR:.*]] = extractvalue { i8*, i32 } %[[IVI2]], 1
Derek Schufff41f67d2016-08-01 21:34:04 +000043
44catch.dispatch: ; preds = %lpad
45 %3 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
46 %matches = icmp eq i32 %2, %3
47 br i1 %matches, label %catch1, label %catch
Derek Schuff53b9af02016-08-09 00:29:55 +000048; CHECK: catch.dispatch:
49; CHECK-NEXT: %[[TYPEID:.*]] = call i32 @llvm_eh_typeid_for(i8* bitcast (i8** @_ZTIi to i8*))
50; CHECK-NEXT: %matches = icmp eq i32 %[[CDR]], %[[TYPEID]]
Derek Schufff41f67d2016-08-01 21:34:04 +000051
52catch1: ; preds = %catch.dispatch
53 %4 = call i8* @__cxa_begin_catch(i8* %1)
54 %5 = bitcast i8* %4 to i32*
55 %6 = load i32, i32* %5, align 4
56 call void @__cxa_end_catch()
57 br label %try.cont
58
59try.cont: ; preds = %catch, %catch1, %invoke.cont
60 ret void
61
62catch: ; preds = %catch.dispatch
63 %7 = call i8* @__cxa_begin_catch(i8* %1)
64 call void @__cxa_end_catch()
65 br label %try.cont
66}
67
68; Test invoke instruction with filters (functions with throw(...) declaration)
69define void @filter() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
70; CHECK-LABEL: @filter(
71entry:
72 invoke void @foo(i32 3)
73 to label %invoke.cont unwind label %lpad
74; CHECK: entry:
Sam Clegg28b3e992018-07-17 16:40:03 +000075; CHECK-NEXT: store i32 0, i32* @__THREW__
Derek Schufff41f67d2016-08-01 21:34:04 +000076; CHECK-NEXT: call void @__invoke_void_i32(void (i32)* @foo, i32 3)
Sam Clegg28b3e992018-07-17 16:40:03 +000077; CHECK-NEXT: %[[__THREW__VAL:.*]] = load i32, i32* @__THREW__
78; CHECK-NEXT: store i32 0, i32* @__THREW__
Heejin Ahnc0f18172016-09-01 21:05:15 +000079; CHECK-NEXT: %cmp = icmp eq i32 %[[__THREW__VAL]], 1
80; CHECK-NEXT: br i1 %cmp, label %lpad, label %invoke.cont
Derek Schufff41f67d2016-08-01 21:34:04 +000081
82invoke.cont: ; preds = %entry
83 ret void
84
85lpad: ; preds = %entry
86 %0 = landingpad { i8*, i32 }
87 filter [2 x i8*] [i8* bitcast (i8** @_ZTIi to i8*), i8* bitcast (i8** @_ZTIc to i8*)]
88 %1 = extractvalue { i8*, i32 } %0, 0
89 %2 = extractvalue { i8*, i32 } %0, 1
90 br label %filter.dispatch
91; CHECK: lpad:
Derek Schuff53b9af02016-08-09 00:29:55 +000092; CHECK-NEXT: %[[FMC:.*]] = call i8* @__cxa_find_matching_catch_4(i8* bitcast (i8** @_ZTIi to i8*), i8* bitcast (i8** @_ZTIc to i8*))
Derek Schufff41f67d2016-08-01 21:34:04 +000093; CHECK-NEXT: %[[IVI1:.*]] = insertvalue { i8*, i32 } undef, i8* %[[FMC]], 0
Sam Clegg28b3e992018-07-17 16:40:03 +000094; CHECK-NEXT: %[[TEMPRET0_VAL:.*]] = load i32, i32* @__tempRet0
Derek Schufff41f67d2016-08-01 21:34:04 +000095; CHECK-NEXT: %[[IVI2:.*]] = insertvalue { i8*, i32 } %[[IVI1]], i32 %[[TEMPRET0_VAL]], 1
96; CHECK-NEXT: extractvalue { i8*, i32 } %[[IVI2]], 0
97; CHECK-NEXT: extractvalue { i8*, i32 } %[[IVI2]], 1
98
99filter.dispatch: ; preds = %lpad
100 %ehspec.fails = icmp slt i32 %2, 0
101 br i1 %ehspec.fails, label %ehspec.unexpected, label %eh.resume
102
103ehspec.unexpected: ; preds = %filter.dispatch
104 call void @__cxa_call_unexpected(i8* %1) #4
105 unreachable
106
107eh.resume: ; preds = %filter.dispatch
108 %lpad.val = insertvalue { i8*, i32 } undef, i8* %1, 0
109 %lpad.val3 = insertvalue { i8*, i32 } %lpad.val, i32 %2, 1
110 resume { i8*, i32 } %lpad.val3
111; CHECK: eh.resume:
112; CHECK-NEXT: insertvalue
113; CHECK-NEXT: %[[LPAD_VAL:.*]] = insertvalue
114; CHECK-NEXT: %[[LOW:.*]] = extractvalue { i8*, i32 } %[[LPAD_VAL]], 0
Derek Schuff53b9af02016-08-09 00:29:55 +0000115; CHECK-NEXT: call void @__resumeException(i8* %[[LOW]])
Derek Schufff41f67d2016-08-01 21:34:04 +0000116; CHECK-NEXT: unreachable
117}
118
Derek Schuff53b9af02016-08-09 00:29:55 +0000119; Test if argument attributes indices in newly created call instructions are correct
120define void @arg_attributes() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
121; CHECK-LABEL: @arg_attributes(
122entry:
123 %0 = invoke noalias i8* @bar(i8 signext 1, i8 zeroext 2)
124 to label %invoke.cont unwind label %lpad
125; CHECK: entry:
Sam Clegg28b3e992018-07-17 16:40:03 +0000126; CHECK-NEXT: store i32 0, i32* @__THREW__
Derek Schuff53b9af02016-08-09 00:29:55 +0000127; CHECK-NEXT: %0 = call noalias i8* @"__invoke_i8*_i8_i8"(i8* (i8, i8)* @bar, i8 signext 1, i8 zeroext 2)
128
129invoke.cont: ; preds = %entry
130 br label %try.cont
131
132lpad: ; preds = %entry
133 %1 = landingpad { i8*, i32 }
134 catch i8* bitcast (i8** @_ZTIi to i8*)
135 catch i8* null
136 %2 = extractvalue { i8*, i32 } %1, 0
137 %3 = extractvalue { i8*, i32 } %1, 1
138 br label %catch.dispatch
139
140catch.dispatch: ; preds = %lpad
141 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
142 %matches = icmp eq i32 %3, %4
143 br i1 %matches, label %catch1, label %catch
144
145catch1: ; preds = %catch.dispatch
146 %5 = call i8* @__cxa_begin_catch(i8* %2)
147 %6 = bitcast i8* %5 to i32*
148 %7 = load i32, i32* %6, align 4
149 call void @__cxa_end_catch()
150 br label %try.cont
151
152try.cont: ; preds = %catch, %catch1, %invoke.cont
153 ret void
154
155catch: ; preds = %catch.dispatch
156 %8 = call i8* @__cxa_begin_catch(i8* %2)
157 call void @__cxa_end_catch()
158 br label %try.cont
159}
160
Derek Schufff41f67d2016-08-01 21:34:04 +0000161declare void @foo(i32)
Derek Schuff53b9af02016-08-09 00:29:55 +0000162declare i8* @bar(i8, i8)
Derek Schufff41f67d2016-08-01 21:34:04 +0000163
164declare i32 @__gxx_personality_v0(...)
165declare i32 @llvm.eh.typeid.for(i8*)
166declare i8* @__cxa_begin_catch(i8*)
167declare void @__cxa_end_catch()
168declare void @__cxa_call_unexpected(i8*)
169
Heejin Ahnc0f18172016-09-01 21:05:15 +0000170; JS glue functions and invoke wrappers declaration
171; CHECK-DAG: declare void @__resumeException(i8*)
172; CHECK-DAG: declare void @__invoke_void_i32(void (i32)*, i32)
173; CHECK-DAG: declare i8* @__cxa_find_matching_catch_4(i8*, i8*)
Derek Schufff41f67d2016-08-01 21:34:04 +0000174
175; setThrew function creation
Sam Clegg28b3e992018-07-17 16:40:03 +0000176; CHECK-LABEL: define weak_odr void @setThrew(i32 %threw, i32 %value) {
Derek Schufff41f67d2016-08-01 21:34:04 +0000177; CHECK: entry:
Sam Clegg28b3e992018-07-17 16:40:03 +0000178; CHECK-NEXT: %__THREW__.val = load i32, i32* @__THREW__
179; CHECK-NEXT: %cmp = icmp eq i32 %__THREW__.val, 0
Derek Schufff41f67d2016-08-01 21:34:04 +0000180; CHECK-NEXT: br i1 %cmp, label %if.then, label %if.end
181; CHECK: if.then:
Sam Clegg28b3e992018-07-17 16:40:03 +0000182; CHECK-NEXT: store i32 %threw, i32* @__THREW__
183; CHECK-NEXT: store i32 %value, i32* @__threwValue
Derek Schufff41f67d2016-08-01 21:34:04 +0000184; CHECK-NEXT: br label %if.end
185; CHECK: if.end:
186; CHECK-NEXT: ret void
187; CHECK: }
188
189; setTempRet0 function creation
Sam Clegg28b3e992018-07-17 16:40:03 +0000190; CHECK-LABEL: define weak_odr void @setTempRet0(i32 %value) {
Derek Schufff41f67d2016-08-01 21:34:04 +0000191; CHECK: entry:
Sam Clegg28b3e992018-07-17 16:40:03 +0000192; CHECK-NEXT: store i32 %value, i32* @__tempRet0
Derek Schufff41f67d2016-08-01 21:34:04 +0000193; CHECK-NEXT: ret void
194; CHECK: }