summaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/bprob/g++-bprob-1.C
blob: b1a1de77e98ec77c6402bb7458c02a199ae6ca17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* Check that various C constructs (in C++) don't cause problems for
 * profile-directed block ordering.

   Most of this test is the same as bprob-1.c and gcov-4.c in
   gcc.misc-tests.  The "count" comments are left in to make comparisons
   easier; they are ignored for this test. */

extern "C" void abort (void);

/* Check for loops. */

int for_val1;
int for_val2;
int for_temp;

int
test_for1 (int n)
{
  int i;
  for_temp = 1;				/* count(3) */
  for (i = 0; i < n; i++)
    for_temp++;				/* count(9) */
  return for_temp;			/* count(3) */
}

int
test_for2 (int m, int n, int o)
{
  int i, j, k;
  for_temp = 1;				/* count(6) */
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      for (k = 0; k < o; k++)
	for_temp++;			/* count(81) */
  return for_temp;			/* count(6) */
}

int
call_for ()
{
  for_val1 += test_for1 (0);
  for_val1 += test_for1 (2);
  for_val1 += test_for1 (7);

  for_val2 += test_for2 (0, 0, 0);
  for_val2 += test_for2 (1, 0, 0);
  for_val2 += test_for2 (1, 3, 0);
  for_val2 += test_for2 (1, 3, 1);
  for_val2 += test_for2 (3, 1, 5);
  for_val2 += test_for2 (3, 7, 3);
}

/* Check the use of goto. */

int goto_val;

int
test_goto1 (int f)
{
  if (f)				/* count(2) */
    goto lab1;				/* count(1) */
  return 1;				/* count(1) */
lab1:
  return 2;				/* count(1) */
}

int
test_goto2 (int f)
{
  int i;
  for (i = 0; i < 10; i++)		/* count(15) */
    if (i == f) goto lab2;		/* count(14) */
  return 4;				/* count(1) */
lab2:
  return 8;				/* count(1) */
}

void
call_goto ()
{
  goto_val += test_goto1 (0);
  goto_val += test_goto1 (1);
  goto_val += test_goto2 (3);
  goto_val += test_goto2 (30);
}

/* Check nested if-then-else statements. */

int ifelse_val1;
int ifelse_val2;
int ifelse_val3;

int
test_ifelse1 (int i, int j)
{
  int result = 0;
  if (i)				/* count(5) */
    if (j)				/* count(3) */
      result = 4;			/* count(3) */
    else
      result = 1024;
  else
    if (j)				/* count(2) */
      result = 1;			/* count(1) */
    else
      result = 2;			/* count(1) */
  if (i > j)				/* count(5) */
    result *= 2;			/* count(1) */
  if (i > 10)				/* count(5) */
    if (j > 10)				/* count(1) */
      result *= 4;			/* count(1) */
  return result;			/* count(5) */
}

int
test_ifelse2 (int i)
{
  int result = 0;
  if (!i)				/* count(6) */
    result = 1;				/* count(1) */
  if (i == 1)				/* count(6) */
    result = 1024;
  if (i == 2)				/* count(6) */
    result = 2;				/* count(3) */
  if (i == 3)				/* count(6) */
    return 8;				/* count(2) */
  if (i == 4)				/* count(4) */
    return 2048;
  return result;			/* count(4) */
}

int
test_ifelse3 (int i, int j)
{
  int result = 1;
  if (i > 10 && j > i && j < 20)	/* count(11) */
    result = 16;			/* count(1) */
  if (i > 20)				/* count(11) */
    if (j > i)				/* count(5) */
      if (j < 30)			/* count(2) */
	result = 32;			/* count(1) */
  if (i == 3 || j == 47 || i == j)	/* count(11) */
    result = 64;			/* count(3) */
  return result;			/* count(11) */
}

void
call_ifelse ()
{
  ifelse_val1 += test_ifelse1 (0, 2);
  ifelse_val1 += test_ifelse1 (0, 0);
  ifelse_val1 += test_ifelse1 (1, 2);
  ifelse_val1 += test_ifelse1 (10, 2);
  ifelse_val1 += test_ifelse1 (11, 11);

  ifelse_val2 += test_ifelse2 (0);
  ifelse_val2 += test_ifelse2 (2);
  ifelse_val2 += test_ifelse2 (2);
  ifelse_val2 += test_ifelse2 (2);
  ifelse_val2 += test_ifelse2 (3);
  ifelse_val2 += test_ifelse2 (3);

  ifelse_val3 += test_ifelse3 (11, 19);
  ifelse_val3 += test_ifelse3 (25, 27);
  ifelse_val3 += test_ifelse3 (11, 22);
  ifelse_val3 += test_ifelse3 (11, 10);
  ifelse_val3 += test_ifelse3 (21, 32);
  ifelse_val3 += test_ifelse3 (21, 20);
  ifelse_val3 += test_ifelse3 (1, 2);
  ifelse_val3 += test_ifelse3 (32, 31);
  ifelse_val3 += test_ifelse3 (3, 0);
  ifelse_val3 += test_ifelse3 (0, 47);	/* count(1) */
  ifelse_val3 += test_ifelse3 (65, 65);	/* count(1) */
}

/* Check switch statements. */

int switch_val, switch_m;

int
test_switch (int i, int j)
{
  int result = 0;			/* count(5) */

  switch (i)				/* count(5) */
    {
      case 1:
        result = 2;			/* count(1) */
        break;
      case 2:
        result = 1024;
        break;
      case 3:
      case 4:
        if (j == 2)			/* count(3) */
          return 4;			/* count(1) */
        result = 8;			/* count(2) */
        break;
      default:
	result = 32;			/* count(1) */
	switch_m++;			/* count(1) */
        break;
    }
  return result;			/* count(4) */
}

void
call_switch ()
{
  switch_val += test_switch (1, 0);
  switch_val += test_switch (3, 0);
  switch_val += test_switch (3, 2);
  switch_val += test_switch (4, 0);
  switch_val += test_switch (16, 0);	
  switch_val += switch_m;
}

int
main()
{
  call_for ();
  call_goto ();
  call_ifelse ();
  call_switch ();
  if ((for_val1 != 12)
      || (for_val2 != 87)
      || (goto_val != 15)
      || (ifelse_val1 != 31)
      || (ifelse_val2 != 23)
      || (ifelse_val3 != 246)
      || (switch_val != 55))
    abort ();
  return 0;
}