PR testsuite/52641
[official-gcc.git] / gcc / testsuite / gcc.dg / tree-ssa / tailrecursion-5.c
blob53a2cdb15e3647b604178731c489749bd5f0882f
1 /* { dg-do run } */
2 /* { dg-options "-O1 -foptimize-sibling-calls -fdump-tree-optimized" } */
4 extern void abort (void);
5 extern void exit (int);
7 int sum (int n)
9 if (n == 0)
10 return 0;
12 return n + sum (n - 1);
15 int fac (int n)
17 if (n == 0)
18 return 1;
20 return n * fac (n - 1);
23 int sq_sum (int n)
25 if (n == 0)
26 return 0;
28 return n * n + sq_sum (n - 1);
31 int pow2m1 (int n)
33 if (n == 0)
34 return 0;
36 return 2 * pow2m1 (n - 1) + 1;
39 int fib (int n)
41 if (n <= 1)
42 return 1;
44 return fib (n - 2) + fib (n - 1);
47 int main(void)
49 if (sum (5) != 15)
50 abort ();
52 if (fac (5) != 120)
53 abort ();
55 if (sq_sum (5) != 55)
56 abort ();
58 if (pow2m1 (5) != 31)
59 abort ();
61 if (fib (5) != 8)
62 abort ();
64 exit (0);
67 /* There is one call of sum in main and then 2 instances of the word in
68 ;; Function sum (sum) and one in the function header. */
69 /* { dg-final { scan-tree-dump-times "\\msum\\M" 4 "optimized"} } */
70 /* { dg-final { scan-tree-dump-times "\\mfac\\M" 4 "optimized"} } */
71 /* { dg-final { scan-tree-dump-times "\\msq_sum\\M" 4 "optimized"} } */
72 /* { dg-final { scan-tree-dump-times "\\mpow2m1\\M" 4 "optimized"} } */
74 /* There is one recursive call to fib. */
75 /* { dg-final { scan-tree-dump-times "\\mfib\\M" 5 "optimized"} } */
77 /* { dg-final { cleanup-tree-dump "optimized" } } */