xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGen/unsigned-overflow.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsanitize=unsigned-integer-overflow %s -emit-llvm -o - | FileCheck %s
2*f4a2713aSLionel Sambuc // Verify checked operations are emitted for integers and longs.
3*f4a2713aSLionel Sambuc // unsigned short/char's tested in unsigned-promotion.c
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc unsigned long li, lj, lk;
6*f4a2713aSLionel Sambuc unsigned int ii, ij, ik;
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc extern void opaquelong(unsigned long);
9*f4a2713aSLionel Sambuc extern void opaqueint(unsigned int);
10*f4a2713aSLionel Sambuc 
11*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @testlongadd()
testlongadd()12*f4a2713aSLionel Sambuc void testlongadd() {
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc   // CHECK:      [[T1:%.*]] = load i64* @lj
15*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = load i64* @lk
16*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 [[T1]], i64 [[T2]])
17*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = extractvalue { i64, i1 } [[T3]], 0
18*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T5:%.*]] = extractvalue { i64, i1 } [[T3]], 1
19*f4a2713aSLionel Sambuc   // CHECK: call void @__ubsan_handle_add_overflow
20*f4a2713aSLionel Sambuc   li = lj + lk;
21*f4a2713aSLionel Sambuc }
22*f4a2713aSLionel Sambuc 
23*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @testlongsub()
testlongsub()24*f4a2713aSLionel Sambuc void testlongsub() {
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc   // CHECK:      [[T1:%.*]] = load i64* @lj
27*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = load i64* @lk
28*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = call { i64, i1 } @llvm.usub.with.overflow.i64(i64 [[T1]], i64 [[T2]])
29*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = extractvalue { i64, i1 } [[T3]], 0
30*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T5:%.*]] = extractvalue { i64, i1 } [[T3]], 1
31*f4a2713aSLionel Sambuc   // CHECK: call void @__ubsan_handle_sub_overflow
32*f4a2713aSLionel Sambuc   li = lj - lk;
33*f4a2713aSLionel Sambuc }
34*f4a2713aSLionel Sambuc 
35*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @testlongmul()
testlongmul()36*f4a2713aSLionel Sambuc void testlongmul() {
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc   // CHECK:      [[T1:%.*]] = load i64* @lj
39*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = load i64* @lk
40*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 [[T1]], i64 [[T2]])
41*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = extractvalue { i64, i1 } [[T3]], 0
42*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T5:%.*]] = extractvalue { i64, i1 } [[T3]], 1
43*f4a2713aSLionel Sambuc   // CHECK: call void @__ubsan_handle_mul_overflow
44*f4a2713aSLionel Sambuc   li = lj * lk;
45*f4a2713aSLionel Sambuc }
46*f4a2713aSLionel Sambuc 
47*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @testlongpostinc()
testlongpostinc()48*f4a2713aSLionel Sambuc void testlongpostinc() {
49*f4a2713aSLionel Sambuc   opaquelong(li++);
50*f4a2713aSLionel Sambuc 
51*f4a2713aSLionel Sambuc   // CHECK:      [[T1:%.*]] = load i64* @li
52*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 [[T1]], i64 1)
53*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = extractvalue { i64, i1 } [[T2]], 0
54*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = extractvalue { i64, i1 } [[T2]], 1
55*f4a2713aSLionel Sambuc   // CHECK:      call void @__ubsan_handle_add_overflow
56*f4a2713aSLionel Sambuc }
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @testlongpreinc()
testlongpreinc()59*f4a2713aSLionel Sambuc void testlongpreinc() {
60*f4a2713aSLionel Sambuc   opaquelong(++li);
61*f4a2713aSLionel Sambuc 
62*f4a2713aSLionel Sambuc   // CHECK:      [[T1:%.*]] = load i64* @li
63*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 [[T1]], i64 1)
64*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = extractvalue { i64, i1 } [[T2]], 0
65*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = extractvalue { i64, i1 } [[T2]], 1
66*f4a2713aSLionel Sambuc   // CHECK:      call void @__ubsan_handle_add_overflow
67*f4a2713aSLionel Sambuc }
68*f4a2713aSLionel Sambuc 
69*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @testintadd()
testintadd()70*f4a2713aSLionel Sambuc void testintadd() {
71*f4a2713aSLionel Sambuc 
72*f4a2713aSLionel Sambuc   // CHECK:      [[T1:%.*]] = load i32* @ij
73*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = load i32* @ik
74*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[T1]], i32 [[T2]])
75*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = extractvalue { i32, i1 } [[T3]], 0
76*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T5:%.*]] = extractvalue { i32, i1 } [[T3]], 1
77*f4a2713aSLionel Sambuc   // CHECK:      call void @__ubsan_handle_add_overflow
78*f4a2713aSLionel Sambuc   ii = ij + ik;
79*f4a2713aSLionel Sambuc }
80*f4a2713aSLionel Sambuc 
81*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @testintsub()
testintsub()82*f4a2713aSLionel Sambuc void testintsub() {
83*f4a2713aSLionel Sambuc 
84*f4a2713aSLionel Sambuc   // CHECK:      [[T1:%.*]] = load i32* @ij
85*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = load i32* @ik
86*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = call { i32, i1 } @llvm.usub.with.overflow.i32(i32 [[T1]], i32 [[T2]])
87*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = extractvalue { i32, i1 } [[T3]], 0
88*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T5:%.*]] = extractvalue { i32, i1 } [[T3]], 1
89*f4a2713aSLionel Sambuc   // CHECK:      call void @__ubsan_handle_sub_overflow
90*f4a2713aSLionel Sambuc   ii = ij - ik;
91*f4a2713aSLionel Sambuc }
92*f4a2713aSLionel Sambuc 
93*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @testintmul()
testintmul()94*f4a2713aSLionel Sambuc void testintmul() {
95*f4a2713aSLionel Sambuc 
96*f4a2713aSLionel Sambuc   // CHECK:      [[T1:%.*]] = load i32* @ij
97*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = load i32* @ik
98*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 [[T1]], i32 [[T2]])
99*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = extractvalue { i32, i1 } [[T3]], 0
100*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T5:%.*]] = extractvalue { i32, i1 } [[T3]], 1
101*f4a2713aSLionel Sambuc   // CHECK:      call void @__ubsan_handle_mul_overflow
102*f4a2713aSLionel Sambuc   ii = ij * ik;
103*f4a2713aSLionel Sambuc }
104*f4a2713aSLionel Sambuc 
105*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @testintpostinc()
testintpostinc()106*f4a2713aSLionel Sambuc void testintpostinc() {
107*f4a2713aSLionel Sambuc   opaqueint(ii++);
108*f4a2713aSLionel Sambuc 
109*f4a2713aSLionel Sambuc   // CHECK:      [[T1:%.*]] = load i32* @ii
110*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[T1]], i32 1)
111*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = extractvalue { i32, i1 } [[T2]], 0
112*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = extractvalue { i32, i1 } [[T2]], 1
113*f4a2713aSLionel Sambuc   // CHECK:      call void @__ubsan_handle_add_overflow
114*f4a2713aSLionel Sambuc }
115*f4a2713aSLionel Sambuc 
116*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @testintpreinc()
testintpreinc()117*f4a2713aSLionel Sambuc void testintpreinc() {
118*f4a2713aSLionel Sambuc   opaqueint(++ii);
119*f4a2713aSLionel Sambuc 
120*f4a2713aSLionel Sambuc   // CHECK:      [[T1:%.*]] = load i32* @ii
121*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[T1]], i32 1)
122*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = extractvalue { i32, i1 } [[T2]], 0
123*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = extractvalue { i32, i1 } [[T2]], 1
124*f4a2713aSLionel Sambuc   // CHECK:      call void @__ubsan_handle_add_overflow
125*f4a2713aSLionel Sambuc }
126