Daily bump.
[official-gcc.git] / gcc / testsuite / gcc.dg / builtin-arith-overflow-1.c
blob5db9368927ea4ce5118c186218b72835887a6657
1 /* { dg-do run } */
2 /* { dg-options "-O2 -fdump-tree-optimized -g" } */
4 /* SUB_OVERFLOW should be folded into unsigned subtraction,
5 because ovf is never used. */
6 __attribute__((noinline, noclone)) int
7 fn1 (int x, unsigned int y)
9 int res;
10 int ovf = __builtin_sub_overflow (x, y, &res);
11 int res2 = res;
12 int res3 = res2 - 2;
13 (void) ovf;
14 return res;
17 /* MUL_OVERFLOW should be folded into unsigned multiplication,
18 because ovf is never used. */
19 __attribute__((noinline, noclone)) int
20 fn2 (signed char x, long int y)
22 short int res;
23 int ovf = __builtin_mul_overflow (x, y, &res);
24 int res2 = res;
25 int res3 = res2 - 2;
26 (void) ovf;
27 return res;
30 #if __SIZEOF_INT__ > __SIZEOF_SHORT__ && __SIZEOF_INT__ > 1
31 /* ADD_OVERFLOW should be folded into unsigned addition,
32 because it never overflows. */
33 __attribute__((noinline, noclone)) int
34 fn3 (signed char x, unsigned short y, int *ovf)
36 int res;
37 *ovf = __builtin_add_overflow (x, y, &res);
38 return res;
40 #endif
42 /* MUL_OVERFLOW should be folded into unsigned multiplication,
43 because it never overflows. */
44 __attribute__((noinline, noclone)) long int
45 fn4 (long int x, long int y, int *ovf)
47 long int res;
48 x &= 65535;
49 y = (y & 65535) - 32768;
50 *ovf = __builtin_mul_overflow (x, y, &res);
51 return res;
54 #if __SIZEOF_INT__ > 1
55 /* MUL_OVERFLOW should be folded into unsigned multiplication,
56 because it always overflows. */
57 __attribute__((noinline, noclone)) signed char
58 fn5 (long int x, long int y, int *ovf)
60 signed char res;
61 x = (x & 63) + (__SCHAR_MAX__ / 4);
62 y = (y & 3) + 5;
63 *ovf = __builtin_mul_overflow (x, y, &res);
64 return res;
66 #endif
68 /* ADD_OVERFLOW should be folded into unsigned additrion,
69 because it never overflows. */
70 __attribute__((noinline, noclone)) unsigned char
71 fn6 (unsigned char x, unsigned char y, int *ovf)
73 unsigned char res;
74 x = (x & 63) + ((unsigned char) ~0 - 66);
75 y = (y & 3);
76 *ovf = __builtin_add_overflow (x, y, &res);
77 return res;
80 /* ADD_OVERFLOW should be folded into unsigned additrion,
81 because it always overflows. */
82 __attribute__((noinline, noclone)) unsigned char
83 fn7 (unsigned char x, unsigned char y, int *ovf)
85 unsigned char res;
86 x = (x & 15) + ((unsigned char) ~0 - 15);
87 y = (y & 3) + 16;
88 *ovf = __builtin_add_overflow (x, y, &res);
89 return res;
92 int
93 main ()
95 int ovf;
96 if (fn1 (-10, __INT_MAX__) != (int) (-10U - __INT_MAX__)
97 || fn2 (0, 0) != 0
98 || fn2 (32, 16383) != (short int) 524256ULL)
99 __builtin_abort ();
100 #if __SIZEOF_INT__ > __SIZEOF_SHORT__ && __SIZEOF_INT__ > 1
101 if (fn3 (__SCHAR_MAX__, (unsigned short) ~0, &ovf) != (int) (__SCHAR_MAX__ + (unsigned short) ~0)
102 || ovf
103 || fn3 (-__SCHAR_MAX__ - 1, 0, &ovf) != (int) (-__SCHAR_MAX__ - 1)
104 || ovf)
105 __builtin_abort ();
106 #endif
107 if (fn4 (65535, 0, &ovf) != 65535L * -32768 || ovf)
108 __builtin_abort ();
109 #if __SIZEOF_INT__ > 1
110 if (fn5 (0, 0, &ovf) != (signed char) (__SCHAR_MAX__ / 4 * 5)
111 || !ovf
112 || fn5 (63, 3, &ovf) != (signed char) ((__SCHAR_MAX__ / 4 + 63) * 8)
113 || !ovf)
114 __builtin_abort ();
115 #endif
116 if (fn6 (0, 0, &ovf) != (unsigned char) ~0 - 66
117 || ovf
118 || fn6 (63, 3, &ovf) != (unsigned char) ~0
119 || ovf)
120 __builtin_abort ();
121 if (fn7 (0, 0, &ovf) != 0
122 || !ovf
123 || fn7 (63, 3, &ovf) != 18
124 || !ovf)
125 __builtin_abort ();
126 return 0;
129 /* { dg-final { scan-tree-dump-not "ADD_OVERFLOW" "optimized" } } */
130 /* { dg-final { scan-tree-dump-not "SUB_OVERFLOW" "optimized" } } */
131 /* { dg-final { scan-tree-dump-not "MUL_OVERFLOW" "optimized" } } */