xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/new-overflow.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -o - | FileCheck %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // rdar://problem/9246208
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc // Basic test.
6*f4a2713aSLionel Sambuc namespace test0 {
7*f4a2713aSLionel Sambuc   struct A {
8*f4a2713aSLionel Sambuc     A();
9*f4a2713aSLionel Sambuc     int x;
10*f4a2713aSLionel Sambuc   };
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc   typedef A elt;
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc   // CHECK:    define [[A:%.*]]* @_ZN5test04testEs(i16 signext
15*f4a2713aSLionel Sambuc   // CHECK:      [[N:%.*]] = sext i16 {{%.*}} to i32
16*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T0:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 [[N]], i32 4)
17*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T1:%.*]] = extractvalue { i32, i1 } [[T0]], 1
18*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = extractvalue { i32, i1 } [[T0]], 0
19*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = select i1 [[T1]], i32 -1, i32 [[T2]]
20*f4a2713aSLionel Sambuc   // CHECK-NEXT: call noalias i8* @_Znaj(i32 [[T3]])
21*f4a2713aSLionel Sambuc   // CHECK:      getelementptr inbounds {{.*}}, i32 [[N]]
test(short s)22*f4a2713aSLionel Sambuc   elt *test(short s) {
23*f4a2713aSLionel Sambuc     return new elt[s];
24*f4a2713aSLionel Sambuc   }
25*f4a2713aSLionel Sambuc }
26*f4a2713aSLionel Sambuc 
27*f4a2713aSLionel Sambuc // test0 with a nested array.
28*f4a2713aSLionel Sambuc namespace test1 {
29*f4a2713aSLionel Sambuc   struct A {
30*f4a2713aSLionel Sambuc     A();
31*f4a2713aSLionel Sambuc     int x;
32*f4a2713aSLionel Sambuc   };
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc   typedef A elt[100];
35*f4a2713aSLionel Sambuc 
36*f4a2713aSLionel Sambuc   // CHECK:    define [100 x [[A:%.*]]]* @_ZN5test14testEs(i16 signext
37*f4a2713aSLionel Sambuc   // CHECK:      [[N:%.*]] = sext i16 {{%.*}} to i32
38*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T0:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 [[N]], i32 400)
39*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T1:%.*]] = extractvalue { i32, i1 } [[T0]], 1
40*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = extractvalue { i32, i1 } [[T0]], 0
41*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = mul i32 [[N]], 100
42*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = select i1 [[T1]], i32 -1, i32 [[T2]]
43*f4a2713aSLionel Sambuc   // CHECK-NEXT: call noalias i8* @_Znaj(i32 [[T4]])
44*f4a2713aSLionel Sambuc   // CHECK:      getelementptr inbounds {{.*}}, i32 [[T3]]
test(short s)45*f4a2713aSLionel Sambuc   elt *test(short s) {
46*f4a2713aSLionel Sambuc     return new elt[s];
47*f4a2713aSLionel Sambuc   }
48*f4a2713aSLionel Sambuc }
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc // test1 with an array cookie.
51*f4a2713aSLionel Sambuc namespace test2 {
52*f4a2713aSLionel Sambuc   struct A {
53*f4a2713aSLionel Sambuc     A();
54*f4a2713aSLionel Sambuc     ~A();
55*f4a2713aSLionel Sambuc     int x;
56*f4a2713aSLionel Sambuc   };
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc   typedef A elt[100];
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc   // CHECK:    define [100 x [[A:%.*]]]* @_ZN5test24testEs(i16 signext
61*f4a2713aSLionel Sambuc   // CHECK:      [[N:%.*]] = sext i16 {{%.*}} to i32
62*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T0:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 [[N]], i32 400)
63*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T1:%.*]] = extractvalue { i32, i1 } [[T0]], 1
64*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = extractvalue { i32, i1 } [[T0]], 0
65*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = mul i32 [[N]], 100
66*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[T2]], i32 4)
67*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T5:%.*]] = extractvalue { i32, i1 } [[T4]], 1
68*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T6:%.*]] = or i1 [[T1]], [[T5]]
69*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T7:%.*]] = extractvalue { i32, i1 } [[T4]], 0
70*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T8:%.*]] = select i1 [[T6]], i32 -1, i32 [[T7]]
71*f4a2713aSLionel Sambuc   // CHECK-NEXT: call noalias i8* @_Znaj(i32 [[T8]])
72*f4a2713aSLionel Sambuc   // CHECK:      getelementptr inbounds {{.*}}, i32 [[T3]]
test(short s)73*f4a2713aSLionel Sambuc   elt *test(short s) {
74*f4a2713aSLionel Sambuc     return new elt[s];
75*f4a2713aSLionel Sambuc   }
76*f4a2713aSLionel Sambuc }
77*f4a2713aSLionel Sambuc 
78*f4a2713aSLionel Sambuc // test0 with a 1-byte element.
79*f4a2713aSLionel Sambuc namespace test4 {
80*f4a2713aSLionel Sambuc   struct A {
81*f4a2713aSLionel Sambuc     A();
82*f4a2713aSLionel Sambuc   };
83*f4a2713aSLionel Sambuc 
84*f4a2713aSLionel Sambuc   typedef A elt;
85*f4a2713aSLionel Sambuc 
86*f4a2713aSLionel Sambuc   // CHECK:    define [[A:%.*]]* @_ZN5test44testEs(i16 signext
87*f4a2713aSLionel Sambuc   // CHECK:      [[N:%.*]] = sext i16 {{%.*}} to i32
88*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T0:%.*]] = icmp slt i32 [[N]], 0
89*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i32 -1, i32 [[N]]
90*f4a2713aSLionel Sambuc   // CHECK-NEXT: call noalias i8* @_Znaj(i32 [[T1]])
91*f4a2713aSLionel Sambuc   // CHECK:      getelementptr inbounds {{.*}}, i32 [[N]]
test(short s)92*f4a2713aSLionel Sambuc   elt *test(short s) {
93*f4a2713aSLionel Sambuc     return new elt[s];
94*f4a2713aSLionel Sambuc   }
95*f4a2713aSLionel Sambuc }
96*f4a2713aSLionel Sambuc 
97*f4a2713aSLionel Sambuc // test4 with no sext required.
98*f4a2713aSLionel Sambuc namespace test5 {
99*f4a2713aSLionel Sambuc   struct A {
100*f4a2713aSLionel Sambuc     A();
101*f4a2713aSLionel Sambuc   };
102*f4a2713aSLionel Sambuc 
103*f4a2713aSLionel Sambuc   typedef A elt;
104*f4a2713aSLionel Sambuc 
105*f4a2713aSLionel Sambuc   // CHECK:    define [[A:%.*]]* @_ZN5test54testEi(i32
106*f4a2713aSLionel Sambuc   // CHECK:      [[N:%.*]] = load i32*
107*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T0:%.*]] = icmp slt i32 [[N]], 0
108*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i32 -1, i32 [[N]]
109*f4a2713aSLionel Sambuc   // CHECK-NEXT: call noalias i8* @_Znaj(i32 [[T1]])
110*f4a2713aSLionel Sambuc   // CHECK:      getelementptr inbounds {{.*}}, i32 [[N]]
test(int s)111*f4a2713aSLionel Sambuc   elt *test(int s) {
112*f4a2713aSLionel Sambuc     return new elt[s];
113*f4a2713aSLionel Sambuc   }
114*f4a2713aSLionel Sambuc }
115*f4a2713aSLionel Sambuc 
116*f4a2713aSLionel Sambuc // test0 with an unsigned size.
117*f4a2713aSLionel Sambuc namespace test6 {
118*f4a2713aSLionel Sambuc   struct A {
119*f4a2713aSLionel Sambuc     A();
120*f4a2713aSLionel Sambuc     int x;
121*f4a2713aSLionel Sambuc   };
122*f4a2713aSLionel Sambuc 
123*f4a2713aSLionel Sambuc   typedef A elt;
124*f4a2713aSLionel Sambuc 
125*f4a2713aSLionel Sambuc   // CHECK:    define [[A:%.*]]* @_ZN5test64testEt(i16 zeroext
126*f4a2713aSLionel Sambuc   // CHECK:      [[N:%.*]] = zext i16 {{%.*}} to i32
127*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T0:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 [[N]], i32 4)
128*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T1:%.*]] = extractvalue { i32, i1 } [[T0]], 1
129*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = extractvalue { i32, i1 } [[T0]], 0
130*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = select i1 [[T1]], i32 -1, i32 [[T2]]
131*f4a2713aSLionel Sambuc   // CHECK-NEXT: call noalias i8* @_Znaj(i32 [[T3]])
132*f4a2713aSLionel Sambuc   // CHECK:      getelementptr inbounds {{.*}}, i32 [[N]]
test(unsigned short s)133*f4a2713aSLionel Sambuc   elt *test(unsigned short s) {
134*f4a2713aSLionel Sambuc     return new elt[s];
135*f4a2713aSLionel Sambuc   }
136*f4a2713aSLionel Sambuc }
137*f4a2713aSLionel Sambuc 
138*f4a2713aSLionel Sambuc // test1 with an unsigned size.
139*f4a2713aSLionel Sambuc namespace test7 {
140*f4a2713aSLionel Sambuc   struct A {
141*f4a2713aSLionel Sambuc     A();
142*f4a2713aSLionel Sambuc     int x;
143*f4a2713aSLionel Sambuc   };
144*f4a2713aSLionel Sambuc 
145*f4a2713aSLionel Sambuc   typedef A elt[100];
146*f4a2713aSLionel Sambuc 
147*f4a2713aSLionel Sambuc   // CHECK:    define [100 x [[A:%.*]]]* @_ZN5test74testEt(i16 zeroext
148*f4a2713aSLionel Sambuc   // CHECK:      [[N:%.*]] = zext i16 {{%.*}} to i32
149*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T0:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 [[N]], i32 400)
150*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T1:%.*]] = extractvalue { i32, i1 } [[T0]], 1
151*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = extractvalue { i32, i1 } [[T0]], 0
152*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = mul i32 [[N]], 100
153*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = select i1 [[T1]], i32 -1, i32 [[T2]]
154*f4a2713aSLionel Sambuc   // CHECK-NEXT: call noalias i8* @_Znaj(i32 [[T4]])
155*f4a2713aSLionel Sambuc   // CHECK:      getelementptr inbounds {{.*}}, i32 [[T3]]
test(unsigned short s)156*f4a2713aSLionel Sambuc   elt *test(unsigned short s) {
157*f4a2713aSLionel Sambuc     return new elt[s];
158*f4a2713aSLionel Sambuc   }
159*f4a2713aSLionel Sambuc }
160*f4a2713aSLionel Sambuc 
161*f4a2713aSLionel Sambuc // test0 with a signed type larger than size_t.
162*f4a2713aSLionel Sambuc namespace test8 {
163*f4a2713aSLionel Sambuc   struct A {
164*f4a2713aSLionel Sambuc     A();
165*f4a2713aSLionel Sambuc     int x;
166*f4a2713aSLionel Sambuc   };
167*f4a2713aSLionel Sambuc 
168*f4a2713aSLionel Sambuc   typedef A elt;
169*f4a2713aSLionel Sambuc 
170*f4a2713aSLionel Sambuc   // CHECK:    define [[A:%.*]]* @_ZN5test84testEx(i64
171*f4a2713aSLionel Sambuc   // CHECK:      [[N:%.*]] = load i64*
172*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T0:%.*]] = icmp uge i64 [[N]], 4294967296
173*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T1:%.*]] = trunc i64 [[N]] to i32
174*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 [[T1]], i32 4)
175*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = extractvalue { i32, i1 } [[T2]], 1
176*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = or i1 [[T0]], [[T3]]
177*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T5:%.*]] = extractvalue { i32, i1 } [[T2]], 0
178*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T6:%.*]] = select i1 [[T4]], i32 -1, i32 [[T5]]
179*f4a2713aSLionel Sambuc   // CHECK-NEXT: call noalias i8* @_Znaj(i32 [[T6]])
180*f4a2713aSLionel Sambuc   // CHECK:      getelementptr inbounds {{.*}}, i32 [[T1]]
test(long long s)181*f4a2713aSLionel Sambuc   elt *test(long long s) {
182*f4a2713aSLionel Sambuc     return new elt[s];
183*f4a2713aSLionel Sambuc   }
184*f4a2713aSLionel Sambuc }
185*f4a2713aSLionel Sambuc 
186*f4a2713aSLionel Sambuc // test8 with an unsigned type.
187*f4a2713aSLionel Sambuc namespace test9 {
188*f4a2713aSLionel Sambuc   struct A {
189*f4a2713aSLionel Sambuc     A();
190*f4a2713aSLionel Sambuc     int x;
191*f4a2713aSLionel Sambuc   };
192*f4a2713aSLionel Sambuc 
193*f4a2713aSLionel Sambuc   typedef A elt;
194*f4a2713aSLionel Sambuc 
195*f4a2713aSLionel Sambuc   // CHECK:    define [[A:%.*]]* @_ZN5test94testEy(i64
196*f4a2713aSLionel Sambuc   // CHECK:      [[N:%.*]] = load i64*
197*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T0:%.*]] = icmp uge i64 [[N]], 4294967296
198*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T1:%.*]] = trunc i64 [[N]] to i32
199*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T2:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 [[T1]], i32 4)
200*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T3:%.*]] = extractvalue { i32, i1 } [[T2]], 1
201*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T4:%.*]] = or i1 [[T0]], [[T3]]
202*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T5:%.*]] = extractvalue { i32, i1 } [[T2]], 0
203*f4a2713aSLionel Sambuc   // CHECK-NEXT: [[T6:%.*]] = select i1 [[T4]], i32 -1, i32 [[T5]]
204*f4a2713aSLionel Sambuc   // CHECK-NEXT: call noalias i8* @_Znaj(i32 [[T6]])
205*f4a2713aSLionel Sambuc   // CHECK:      getelementptr inbounds {{.*}}, i32 [[T1]]
test(unsigned long long s)206*f4a2713aSLionel Sambuc   elt *test(unsigned long long s) {
207*f4a2713aSLionel Sambuc     return new elt[s];
208*f4a2713aSLionel Sambuc   }
209*f4a2713aSLionel Sambuc }
210