Debian Codegraph @

gcc/testsuite/g++.dg/cpp23/attr-assume-opt.C

raw
1 // P1774R8 - Portable assumptions
2 // { dg-do compile { target c++11 } }
3 // { dg-options "-O2 -fdump-tree-vrp2" }
4 // Test the we can optimize based on conditions in assume.
5
6 int
7 f1 (unsigned x, unsigned y, unsigned z)
8 {
9 [[assume (x == 2 && y < 3 && z < 20)]];
10 unsigned q = x + y + z;
11 if (q > 23)
12 return 0;
13 return 1;
14 }
15
16
17 int
18 f2 (int x, int y, int z)
19 {
20 [[assume (x+12 == 14 && y >= 0 && y + 10 < 13 && z + 4 >= 4 && z - 2 < 18)]];
21 unsigned q = x + y + z;
22 if (q*2 > 46)
23 return 0;
24 return 1;
25 }
26
27 int
28 f3 (int x, int y, int z)
29 {
30 [[assume (x + 12 == 14 && z / 2 > 0)]];
31 [[assume (y >= 0 && z - 2 < 18)]];
32 [[assume (y + 10 < 13 && z + 4 >= 2)]];
33 int q = x + y + z;
34 if (q * 2 > 46)
35 return 0;
36 if (z < 0)
37 return 0;
38 return 1;
39 }
40
41 // This is the same as f2, except there is more complicated flow and
42 // required a range-op update to bitwise or.
43
44 void barn(int x);
45 // assume (x+12 == 14 && y >= 0 && y + 10 < 13 && z + 4 >= 4 && z - 2 < 18)
46 // in different order and form with function calls to cause branches.
47 bool assume_func (int x, int y, int z)
48 {
49 if (z - 2 >= 18)
50 return false;
51 if (x+12 != 14)
52 return false;
53 barn (x);
54 if (y < 0)
55 return false;
56 if (z + 4 < 4)
57 return false;
58 barn (y);
59 if (y + 10 >= 13)
60 return false;
61 barn (z);
62 return true;
63 }
64
65 int
66 f2b (int x, int y, int z)
67 {
68 [[assume (assume_func (x, y, z))]];
69 unsigned q = x + y + z;
70 if (q*2 > 46)
71 return 0;
72 return 1;
73 }
74
75
76 /* { dg-final { scan-tree-dump-times "return 0" 0 "vrp2" } } */
77 /* { dg-final { scan-tree-dump-times "return 1" 4 "vrp2" } } */