xref: /llvm-project/llvm/test/Transforms/LowerTypeTests/cfi-unwind-direct-call.ll (revision 10edb4991c12738e60843d55cd9edbf6d702d9eb)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-attributes --include-generated-funcs --version 4
2; RUN: opt < %s -passes='lowertypetests,default<O3>' -S | FileCheck %s
3
4; This IR is based of the following C++
5; which was compiled with:
6; clang -cc1 -fexceptions -fcxx-exceptions \
7; -std=c++11 -internal-isystem llvm-project/build/lib/clang/17/include \
8; -nostdsysteminc -triple x86_64-unknown-linux -fsanitize=cfi-icall \
9; -fsanitize-cfi-cross-dso -fsanitize-trap=cfi-icall -Oz -S -emit-llvm
10; void (*catch_ptr)(int);
11; void throw_e (int num) {
12;   if (num) throw 20;
13; }
14; void call_catch(int num) {
15;   catch_ptr = &throw_e;
16;   try{
17;     catch_ptr(num);
18;   } catch (int i) {
19;   }
20; }
21
22target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
23target triple = "x86_64-unknown-linux"
24
25@catch_ptr = local_unnamed_addr global ptr null, align 8
26@_ZTIi = external constant ptr
27@llvm.used = appending global [1 x ptr] [ptr @__cfi_check_fail], section "llvm.metadata"
28
29; Function Attrs: minsize mustprogress optsize
30define dso_local void @_Z7throw_ei(i32 noundef %num) #0 !type !4 !type !5 !type !6 {
31entry:
32  %tobool.not = icmp eq i32 %num, 0
33  br i1 %tobool.not, label %if.end, label %if.then
34
35if.then:                                          ; preds = %entry
36  %exception = tail call ptr @__cxa_allocate_exception(i64 4) #5
37  store i32 20, ptr %exception, align 16, !tbaa !7
38  tail call void @__cxa_throw(ptr nonnull %exception, ptr nonnull @_ZTIi, ptr null) #6
39  unreachable
40
41if.end:                                           ; preds = %entry
42  ret void
43}
44
45declare ptr @__cxa_allocate_exception(i64) local_unnamed_addr
46
47declare void @__cxa_throw(ptr, ptr, ptr) local_unnamed_addr
48
49; Function Attrs: minsize mustprogress optsize
50define dso_local void @_Z10call_catchi(i32 noundef %num) local_unnamed_addr #0 personality ptr @__gxx_personality_v0 !type !4 !type !5 !type !6 {
51entry:
52  store ptr @_Z7throw_ei, ptr @catch_ptr, align 8, !tbaa !11
53  %0 = tail call i1 @llvm.type.test(ptr nonnull @_Z7throw_ei, metadata !"_ZTSFviE"), !nosanitize !13
54  br i1 %0, label %cfi.cont, label %cfi.slowpath, !prof !14, !nosanitize !13
55
56cfi.slowpath:                                     ; preds = %entry
57  tail call void @__cfi_slowpath(i64 -8738933900360652027, ptr nonnull @_Z7throw_ei) #5, !nosanitize !13
58  br label %cfi.cont, !nosanitize !13
59
60cfi.cont:                                         ; preds = %cfi.slowpath, %entry
61  invoke void @_Z7throw_ei(i32 noundef %num) #7
62  to label %try.cont unwind label %lpad
63
64lpad:                                             ; preds = %cfi.cont
65  %1 = landingpad { ptr, i32 }
66  catch ptr @_ZTIi
67  %2 = extractvalue { ptr, i32 } %1, 1
68  %3 = tail call i32 @llvm.eh.typeid.for.p0(ptr nonnull @_ZTIi) #5
69  %matches = icmp eq i32 %2, %3
70  br i1 %matches, label %catch, label %eh.resume
71
72catch:                                            ; preds = %lpad
73  %4 = extractvalue { ptr, i32 } %1, 0
74  %5 = tail call ptr @__cxa_begin_catch(ptr %4) #5
75  tail call void @__cxa_end_catch() #5
76  br label %try.cont
77
78try.cont:                                         ; preds = %cfi.cont, %catch
79  ret void
80
81eh.resume:                                        ; preds = %lpad
82  resume { ptr, i32 } %1
83}
84
85; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
86declare i1 @llvm.type.test(ptr, metadata) #1
87
88declare void @__cfi_slowpath(i64, ptr) local_unnamed_addr
89
90declare i32 @__gxx_personality_v0(...)
91
92; Function Attrs: nofree nosync nounwind memory(none)
93declare i32 @llvm.eh.typeid.for.p0(ptr) #2
94
95declare ptr @__cxa_begin_catch(ptr) local_unnamed_addr
96
97declare void @__cxa_end_catch() local_unnamed_addr
98
99; Function Attrs: minsize optsize
100define weak_odr hidden void @__cfi_check_fail(ptr noundef %0, ptr noundef %1) #3 {
101entry:
102  %.not = icmp eq ptr %0, null, !nosanitize !13
103  br i1 %.not, label %trap, label %cont, !nosanitize !13
104
105trap:                                             ; preds = %cont, %entry
106  tail call void @llvm.ubsantrap(i8 2) #8, !nosanitize !13
107  unreachable, !nosanitize !13
108
109cont:                                             ; preds = %entry
110  %2 = load i8, ptr %0, align 4, !nosanitize !13
111  %switch = icmp ult i8 %2, 5
112  br i1 %switch, label %trap, label %cont6
113
114cont6:                                            ; preds = %cont
115  ret void, !nosanitize !13
116}
117
118; Function Attrs: cold noreturn nounwind
119declare void @llvm.ubsantrap(i8 immarg) #4
120
121define weak void @__cfi_check(i64 %0, ptr %1, ptr %2) local_unnamed_addr {
122entry:
123  tail call void @llvm.trap()
124  unreachable
125}
126
127; Function Attrs: cold noreturn nounwind
128declare void @llvm.trap() #4
129
130attributes #0 = { minsize mustprogress optsize "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
131attributes #1 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
132attributes #2 = { nofree nosync nounwind memory(none) }
133attributes #3 = { minsize optsize "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" }
134attributes #4 = { cold noreturn nounwind }
135attributes #5 = { nounwind }
136attributes #6 = { noreturn }
137attributes #7 = { minsize optsize }
138attributes #8 = { noreturn nounwind }
139
140!llvm.module.flags = !{!0, !1, !2}
141!llvm.ident = !{!3}
142
143!0 = !{i32 1, !"wchar_size", i32 4}
144!1 = !{i32 4, !"Cross-DSO CFI", i32 1}
145!2 = !{i32 4, !"CFI Canonical Jump Tables", i32 0}
146!3 = !{!"clang version 17.0.2"}
147!4 = !{i64 0, !"_ZTSFviE"}
148!5 = !{i64 0, !"_ZTSFviE.generalized"}
149!6 = !{i64 0, i64 -8738933900360652027}
150!7 = !{!8, !8, i64 0}
151!8 = !{!"int", !9, i64 0}
152!9 = !{!"omnipotent char", !10, i64 0}
153!10 = !{!"Simple C++ TBAA"}
154!11 = !{!12, !12, i64 0}
155!12 = !{!"any pointer", !9, i64 0}
156!13 = !{}
157!14 = !{!"branch_weights", i32 1048575, i32 1}
158; CHECK: Function Attrs: minsize mustprogress optsize
159; CHECK-LABEL: define dso_local void @_Z7throw_ei(
160; CHECK-SAME: i32 noundef [[NUM:%.*]]) #[[ATTR0:[0-9]+]] !type [[META4:![0-9]+]] !type [[META5:![0-9]+]] !type [[META6:![0-9]+]] {
161; CHECK-NEXT:  entry:
162; CHECK-NEXT:    [[TOBOOL_NOT:%.*]] = icmp eq i32 [[NUM]], 0
163; CHECK-NEXT:    br i1 [[TOBOOL_NOT]], label [[IF_END:%.*]], label [[IF_THEN:%.*]]
164; CHECK:       if.then:
165; CHECK-NEXT:    [[EXCEPTION:%.*]] = tail call ptr @__cxa_allocate_exception(i64 4) #[[ATTR6:[0-9]+]]
166; CHECK-NEXT:    store i32 20, ptr [[EXCEPTION]], align 16, !tbaa [[TBAA7:![0-9]+]]
167; CHECK-NEXT:    tail call void @__cxa_throw(ptr nonnull [[EXCEPTION]], ptr nonnull @_ZTIi, ptr null) #[[ATTR7:[0-9]+]]
168; CHECK-NEXT:    unreachable
169; CHECK:       if.end:
170; CHECK-NEXT:    ret void
171;
172;
173; CHECK: Function Attrs: minsize mustprogress optsize
174; CHECK-LABEL: define dso_local void @_Z10call_catchi(
175; CHECK-SAME: i32 noundef [[NUM:%.*]]) local_unnamed_addr #[[ATTR0]] personality ptr @__gxx_personality_v0 !type [[META4]] !type [[META5]] !type [[META6]] {
176; CHECK-NEXT:  entry:
177; CHECK-NEXT:    store ptr @_Z7throw_ei.cfi_jt, ptr @catch_ptr, align 8, !tbaa [[TBAA11:![0-9]+]]
178; CHECK-NEXT:    invoke void @_Z7throw_ei.cfi_jt(i32 noundef [[NUM]]) #[[ATTR8:[0-9]+]]
179; CHECK-NEXT:            to label [[TRY_CONT:%.*]] unwind label [[LPAD:%.*]], !callees [[META13:![0-9]+]]
180; CHECK:       lpad:
181; CHECK-NEXT:    [[TMP0:%.*]] = landingpad { ptr, i32 }
182; CHECK-NEXT:            catch ptr @_ZTIi
183; CHECK-NEXT:    [[TMP1:%.*]] = extractvalue { ptr, i32 } [[TMP0]], 1
184; CHECK-NEXT:    [[TMP2:%.*]] = tail call i32 @llvm.eh.typeid.for.p0(ptr nonnull @_ZTIi) #[[ATTR6]]
185; CHECK-NEXT:    [[MATCHES:%.*]] = icmp eq i32 [[TMP1]], [[TMP2]]
186; CHECK-NEXT:    br i1 [[MATCHES]], label [[CATCH:%.*]], label [[EH_RESUME:%.*]]
187; CHECK:       catch:
188; CHECK-NEXT:    [[TMP3:%.*]] = extractvalue { ptr, i32 } [[TMP0]], 0
189; CHECK-NEXT:    [[TMP4:%.*]] = tail call ptr @__cxa_begin_catch(ptr [[TMP3]]) #[[ATTR6]]
190; CHECK-NEXT:    tail call void @__cxa_end_catch() #[[ATTR6]]
191; CHECK-NEXT:    br label [[TRY_CONT]]
192; CHECK:       try.cont:
193; CHECK-NEXT:    ret void
194; CHECK:       eh.resume:
195; CHECK-NEXT:    resume { ptr, i32 } [[TMP0]]
196;
197;
198; CHECK: Function Attrs: minsize optsize
199; CHECK-LABEL: define weak_odr hidden void @__cfi_check_fail(
200; CHECK-SAME: ptr noundef [[TMP0:%.*]], ptr noundef [[TMP1:%.*]]) #[[ATTR2:[0-9]+]] {
201; CHECK-NEXT:  entry:
202; CHECK-NEXT:    [[DOTNOT:%.*]] = icmp eq ptr [[TMP0]], null, !nosanitize [[META14:![0-9]+]]
203; CHECK-NEXT:    br i1 [[DOTNOT]], label [[TRAP:%.*]], label [[CONT:%.*]], !nosanitize [[META14]]
204; CHECK:       trap:
205; CHECK-NEXT:    tail call void @llvm.ubsantrap(i8 2) #[[ATTR9:[0-9]+]], !nosanitize [[META14]]
206; CHECK-NEXT:    unreachable, !nosanitize [[META14]]
207; CHECK:       cont:
208; CHECK-NEXT:    [[TMP2:%.*]] = load i8, ptr [[TMP0]], align 4, !nosanitize [[META14]]
209; CHECK-NEXT:    [[SWITCH:%.*]] = icmp ult i8 [[TMP2]], 5
210; CHECK-NEXT:    br i1 [[SWITCH]], label [[TRAP]], label [[CONT6:%.*]]
211; CHECK:       cont6:
212; CHECK-NEXT:    ret void, !nosanitize [[META14]]
213;
214;
215; CHECK-LABEL: define weak void @__cfi_check(
216; CHECK-SAME: i64 [[TMP0:%.*]], ptr [[TMP1:%.*]], ptr [[TMP2:%.*]]) local_unnamed_addr {
217; CHECK-NEXT:  entry:
218; CHECK-NEXT:    tail call void @llvm.trap()
219; CHECK-NEXT:    unreachable
220;
221;
222; CHECK: Function Attrs: naked nocf_check noinline
223; CHECK-LABEL: define internal void @_Z7throw_ei.cfi_jt(
224; CHECK-SAME: ) #[[ATTR5:[0-9]+]] align 8 {
225; CHECK-NEXT:  entry:
226; CHECK-NEXT:    tail call void asm sideeffect "jmp ${0:c}@plt\0Aint3\0Aint3\0Aint3\0A", "s"(ptr nonnull @_Z7throw_ei) #[[ATTR6]]
227; CHECK-NEXT:    unreachable
228;
229;.
230; CHECK: [[META4]] = !{i64 0, !"_ZTSFviE"}
231; CHECK: [[META5]] = !{i64 0, !"_ZTSFviE.generalized"}
232; CHECK: [[META6]] = !{i64 0, i64 -8738933900360652027}
233; CHECK: [[TBAA7]] = !{[[META8:![0-9]+]], [[META8]], i64 0}
234; CHECK: [[META8]] = !{!"int", [[META9:![0-9]+]], i64 0}
235; CHECK: [[META9]] = !{!"omnipotent char", [[META10:![0-9]+]], i64 0}
236; CHECK: [[META10]] = !{!"Simple C++ TBAA"}
237; CHECK: [[TBAA11]] = !{[[META12:![0-9]+]], [[META12]], i64 0}
238; CHECK: [[META12]] = !{!"any pointer", [[META9]], i64 0}
239; CHECK: [[META13]] = !{ptr @_Z7throw_ei.cfi_jt}
240; CHECK: [[META14]] = !{}
241;.
242