bprob-1.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* Check that various C constructs are handled correctly by profile-directed
  2. block ordering.
  3. This test is the same as gcov-4.c. The "count" comments are left in to
  4. make comparisons easier; they are ignored for this test. */
  5. extern void abort (void);
  6. int do_something (int i)
  7. {
  8. return i;
  9. }
  10. /* Check static inline functions. */
  11. int unref_val;
  12. static inline int
  13. unreferenced (int i, int j)
  14. {
  15. return i - j;
  16. }
  17. static inline int
  18. uncalled (int i, int j)
  19. {
  20. return i * j;
  21. }
  22. static inline int
  23. called (int i, int j)
  24. {
  25. return i + j; /* count(1) */
  26. }
  27. void
  28. call_unref ()
  29. {
  30. if (unref_val) /* count(1) */
  31. unref_val = uncalled (1, 2);
  32. unref_val = called (unref_val, 4); /* count(1) */
  33. }
  34. /* Check for loops. */
  35. int for_val1;
  36. int for_val2;
  37. int for_temp;
  38. int
  39. test_for1 (int n)
  40. {
  41. int i;
  42. for_temp = 1; /* count(3) */
  43. for (i = 0; i < n; i++)
  44. for_temp++; /* count(9) */
  45. return for_temp; /* count(3) */
  46. }
  47. int
  48. test_for2 (int m, int n, int o)
  49. {
  50. int i, j, k;
  51. for_temp = 1; /* count(6) */
  52. for (i = 0; i < n; i++)
  53. for (j = 0; j < m; j++)
  54. for (k = 0; k < o; k++)
  55. for_temp++; /* count(81) */
  56. return for_temp; /* count(6) */
  57. }
  58. void
  59. call_for ()
  60. {
  61. for_val1 += test_for1 (0);
  62. for_val1 += test_for1 (2);
  63. for_val1 += test_for1 (7);
  64. for_val2 += test_for2 (0, 0, 0);
  65. for_val2 += test_for2 (1, 0, 0);
  66. for_val2 += test_for2 (1, 3, 0);
  67. for_val2 += test_for2 (1, 3, 1);
  68. for_val2 += test_for2 (3, 1, 5);
  69. for_val2 += test_for2 (3, 7, 3);
  70. }
  71. /* Check the use of goto. */
  72. int goto_val;
  73. int
  74. test_goto1 (int f)
  75. {
  76. if (f) /* count(2) */
  77. goto lab1; /* count(1) */
  78. return 1; /* count(1) */
  79. lab1:
  80. return 2; /* count(1) */
  81. }
  82. int
  83. test_goto2 (int f)
  84. {
  85. int i;
  86. for (i = 0; i < 10; i++) /* count(15) */
  87. if (i == f) goto lab2; /* count(14) */
  88. return 4; /* count(1) */
  89. lab2:
  90. return 8; /* count(1) */
  91. }
  92. void
  93. call_goto ()
  94. {
  95. goto_val += test_goto1 (0);
  96. goto_val += test_goto1 (1);
  97. goto_val += test_goto2 (3);
  98. goto_val += test_goto2 (30);
  99. }
  100. /* Check nested if-then-else statements. */
  101. int ifelse_val1;
  102. int ifelse_val2;
  103. int ifelse_val3;
  104. int
  105. test_ifelse1 (int i, int j)
  106. {
  107. int result = 0;
  108. if (i) /* count(5) */
  109. if (j) /* count(3) */
  110. result = do_something (4); /* count(3) */
  111. else
  112. result = do_something (1024);
  113. else
  114. if (j) /* count(2) */
  115. result = do_something (1); /* count(1) */
  116. else
  117. result = do_something (2); /* count(1) */
  118. if (i > j) /* count(5) */
  119. result = do_something (result*2); /* count(1) */
  120. if (i > 10) /* count(5) */
  121. if (j > 10) /* count(1) */
  122. result = do_something (result*4); /* count(1) */
  123. return result; /* count(5) */
  124. }
  125. int
  126. test_ifelse2 (int i)
  127. {
  128. int result = 0;
  129. if (!i) /* count(6) */
  130. result = do_something (1); /* count(1) */
  131. if (i == 1) /* count(6) */
  132. result = do_something (1024);
  133. if (i == 2) /* count(6) */
  134. result = do_something (2); /* count(3) */
  135. if (i == 3) /* count(6) */
  136. return do_something (8); /* count(2) */
  137. if (i == 4) /* count(4) */
  138. return do_something (2048);
  139. return result; /* count(4) */
  140. }
  141. int
  142. test_ifelse3 (int i, int j)
  143. {
  144. int result = 1;
  145. if (i > 10 && j > i && j < 20) /* count(11) */
  146. result = do_something (16); /* count(1) */
  147. if (i > 20) /* count(11) */
  148. if (j > i) /* count(5) */
  149. if (j < 30) /* count(2) */
  150. result = do_something (32); /* count(1) */
  151. if (i == 3 || j == 47 || i == j) /* count(11) */
  152. result = do_something (64); /* count(3) */
  153. return result; /* count(11) */
  154. }
  155. void
  156. call_ifelse ()
  157. {
  158. ifelse_val1 += test_ifelse1 (0, 2);
  159. ifelse_val1 += test_ifelse1 (0, 0);
  160. ifelse_val1 += test_ifelse1 (1, 2);
  161. ifelse_val1 += test_ifelse1 (10, 2);
  162. ifelse_val1 += test_ifelse1 (11, 11);
  163. ifelse_val2 += test_ifelse2 (0);
  164. ifelse_val2 += test_ifelse2 (2);
  165. ifelse_val2 += test_ifelse2 (2);
  166. ifelse_val2 += test_ifelse2 (2);
  167. ifelse_val2 += test_ifelse2 (3);
  168. ifelse_val2 += test_ifelse2 (3);
  169. ifelse_val3 += test_ifelse3 (11, 19);
  170. ifelse_val3 += test_ifelse3 (25, 27);
  171. ifelse_val3 += test_ifelse3 (11, 22);
  172. ifelse_val3 += test_ifelse3 (11, 10);
  173. ifelse_val3 += test_ifelse3 (21, 32);
  174. ifelse_val3 += test_ifelse3 (21, 20);
  175. ifelse_val3 += test_ifelse3 (1, 2);
  176. ifelse_val3 += test_ifelse3 (32, 31);
  177. ifelse_val3 += test_ifelse3 (3, 0);
  178. ifelse_val3 += test_ifelse3 (0, 47);
  179. ifelse_val3 += test_ifelse3 (65, 65);
  180. }
  181. /* Check switch statements. */
  182. int switch_val, switch_m;
  183. int
  184. test_switch (int i, int j)
  185. {
  186. int result = 0; /* count(5) */
  187. switch (i) /* count(5) */
  188. {
  189. case 1:
  190. result = do_something (2); /* count(1) */
  191. break;
  192. case 2:
  193. result = do_something (1024);
  194. break;
  195. case 3:
  196. case 4:
  197. if (j == 2) /* count(3) */
  198. return do_something (4); /* count(1) */
  199. result = do_something (8); /* count(2) */
  200. break;
  201. default:
  202. result = do_something (32); /* count(1) */
  203. switch_m++; /* count(1) */
  204. break;
  205. }
  206. return result; /* count(4) */
  207. }
  208. void
  209. call_switch ()
  210. {
  211. switch_val += test_switch (1, 0);
  212. switch_val += test_switch (3, 0);
  213. switch_val += test_switch (3, 2);
  214. switch_val += test_switch (4, 0);
  215. switch_val += test_switch (16, 0);
  216. switch_val += switch_m;
  217. }
  218. int
  219. main()
  220. {
  221. call_for ();
  222. call_goto ();
  223. call_ifelse ();
  224. call_switch ();
  225. call_unref ();
  226. if ((for_val1 != 12)
  227. || (for_val2 != 87)
  228. || (goto_val != 15)
  229. || (ifelse_val1 != 31)
  230. || (ifelse_val2 != 23)
  231. || (ifelse_val3 != 246)
  232. || (switch_val != 55)
  233. || (unref_val != 4))
  234. abort ();
  235. return 0;
  236. }