home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 161_01 / optimtst.c < prev    next >
C/C++ Source or Header  |  1985-08-29  |  6KB  |  337 lines

  1. /*
  2.  * TESTS FOR VARIOUS FORMS OF OPTIMIZATION 
  3.  *
  4.  * The following test helps determine what kinds of optimizations are
  5.  * being done.  
  6.  *
  7.  * Unless specified otherwise, if the size of the first statement
  8.  * is the same as the second statement,
  9.  * even when the optimization flag is not set, then the partiucular
  10.  * optimization is always done.  If the sizes are different 
  11.  * (i.e., the first is larger) when run
  12.  * without optimization but are the same when the optimization flag
  13.  * is included then it is an optimization which is done during the
  14.  * optimization pass.  If the sizes are different in both cases then
  15.  * the optimization is not done.
  16.  */
  17.  
  18. #include "timer1.h"
  19.  
  20. int i, j, k, l, temp;
  21. unsigned int u1;
  22. int arr[15];
  23. int arr2[15][15];
  24. register int *p;
  25.  
  26. /* Constant folding */
  27. DO_STMT("Const folding #1" )
  28. j = 42 * 6 / 3
  29. OD
  30. DO_STMT("Const folding #2" )
  31. j = 84
  32. OD
  33.  
  34. /* Constant folding plus propogation */
  35. DO_STMT("Const fold + prop #1")
  36. i = 3 + 14 / 7 + 6;
  37. j = i + 7
  38. OD
  39. DO_STMT("Const fold + prop #2")
  40. i = 11;
  41. j = 18;
  42. OD
  43.  
  44. /* Algebaric identities */
  45.  
  46. /* Adding 0 */
  47. DO_STMT("Adding 0 #1")
  48. i = j + 0
  49. OD
  50. DO_STMT("Adding 0 #2")
  51. i = j
  52. OD
  53.  
  54. /* Adding 1 to a value*/
  55. /* If code is the same then no optimization for adding 1 */
  56. DO_STMT("Adding 1 #1")
  57. i = i + 1
  58. OD
  59. DO_STMT("Adding 1 #2")
  60. i = i + 49
  61. OD
  62. /* Multiplying by 0 */
  63. DO_STMT("Mult by 0 #1")
  64. j = i * 0
  65. OD
  66. DO_STMT("Mult by 0 #2")
  67. j = 0
  68. OD
  69.  
  70. /* Multiplying by 1 */
  71. DO_STMT("Mult by 1 #1")
  72. j = i * 1
  73. OD
  74. DO_STMT("Mult by 1 #2")
  75. j = i
  76. OD
  77.  
  78. /* Multiplying by a power of 2 */
  79. DO_STMT("Mult by power 2 #1")
  80. i = j * 8
  81. OD
  82. DO_STMT("Mult by power 2 #2")
  83. i = j << 3
  84. OD
  85.  
  86. /* Dividing by a power of 2 */
  87. DO_STMT("Div by power 2 #1")
  88. u1 = u1 / 2
  89. OD
  90. DO_STMT("Div by power 2 #2")
  91. u1 = u1 >> 1
  92. OD
  93.  
  94. /* Cummutativity and rearrangement */
  95. DO_STMT("Commute & rearr #1")
  96. i = i + 1 + i + 2 + i + 3
  97. OD
  98. DO_STMT("Commute & rearr #2")
  99. i = i + i + i + 6
  100. OD
  101.  
  102. /* Special handling assignment of 0 */
  103. /* If both statements are the same then no optimization */
  104. DO_STMT("Assigning 0 #1")
  105. i = 0;
  106. OD
  107. DO_STMT("Assigning 0 #2")
  108. i = 49
  109. OD
  110.  
  111. /* Testing against 0 in a conditional */
  112. /* If both statements are the same then no optimization */
  113. DO_STMT("Test against 0 #1")
  114. if (i < 0)
  115.     j = k;
  116. OD
  117. DO_STMT("Test against 0 #2")
  118. if (i < 49)
  119.     j = k;
  120. OD
  121.  
  122. /* Post-incrementing by 1 */
  123. /* If code is the same then no optimization for post-incrementing by 1 */
  124. DO_STMT("Post-inc by 1 #1")
  125. i++
  126. OD
  127. DO_STMT("Post-inc by 1 #2")
  128. i = i + 49
  129. OD
  130.  
  131. /* Pre-incrementing by 1 */ 
  132. /* If code is the same then no optimization for pre-incrementing by 1 */
  133. DO_STMT("Pre-inc by 1 #1")
  134. ++i
  135. OD
  136. DO_STMT("Pre-inc by 1 #2")
  137. i = i + 49
  138. OD
  139.  
  140. /* Post-incrementing by more than 1 */
  141. /* If code is the same then no optimization for post-incrementing by > 1 */
  142. DO_STMT("Post-inc by >1 #1")
  143. p++
  144. OD
  145. DO_STMT("Post-inc by >1 #2")
  146. j = j + 49 /*don't use p because I don't want  a scaled add */
  147. OD
  148.  
  149. /* Pre-incrementing by  more than 1 */ 
  150. /* If code is the same then no optimization for pre-incrementing by > 1 */
  151. DO_STMT("Pre-inc by >1 #1")
  152. ++p
  153. OD
  154. DO_STMT("Pre-inc by >1 #2")
  155. j = j + 49
  156. OD
  157.  
  158.  
  159. /* Recognizing a constant subscripts */
  160. DO_STMT("Const subscript #1")
  161. i = arr[10]
  162. OD
  163. j = 10;
  164. DO_STMT("Const subscript #2")
  165. i = arr[j]
  166. OD
  167.  
  168. /* Jump to jump elimination */
  169. /* The size difference on this test is unimportant. */
  170. /* The first should be faster than */
  171. /* the second one if the jump to jumps have been eliminated */
  172.     
  173. DO_STMT("Jmp to jmp elim #1")
  174. if (j < 1)
  175.      {
  176.      if (k < 1)
  177.          l = 1;
  178.      else
  179.          l = 2;
  180.      }
  181. else
  182.      {
  183.      if (k < 1)
  184.          l = 3;
  185.      else
  186.          l = 4;
  187.      }
  188. k = 2;
  189. i = 2
  190. OD
  191. DO_STMT("Jmp to jmp elim #2")
  192. i = 7;
  193. if (j < 1)
  194.      {
  195.      if (k < 1)
  196.          l = 1;
  197.      else
  198.          l = 2;
  199.      k = 2;
  200.      }
  201. else
  202.      {
  203.      if (k < 1)
  204.          l = 3;
  205.      else
  206.          l = 4;
  207.      i = 2;
  208.      }
  209. OD
  210.  
  211.  
  212. /* Most of the following are primarily space optimizations */
  213.  
  214. /* Constant test elimination */
  215. DO_STMT("Const test elim #1")
  216. if (1)
  217.     i = j * k
  218. OD
  219. DO_STMT("Const test elim #2")
  220.     i = j * k
  221. OD
  222.  
  223. /* Dead Code Elimination */ 
  224. DO_STMT("Dead code elim #1")
  225.     i = j * 7 / k;
  226.     goto lab1;
  227.     i = j * 3 / l;
  228. lab1:
  229. OD
  230. DO_STMT("Dead code elim #2")
  231.     i = j * 7 / k;
  232.     goto lab2;
  233. lab2:
  234. OD
  235.  
  236. /* Common Subexpression Elimination */
  237. DO_STMT("CSE elim #1")
  238. i = j * k + 9;
  239. k = l + j * k + 9
  240. OD
  241. DO_STMT("CSE elim #1")
  242. i = j * k + 9; 
  243. l = l + i
  244. OD
  245.  
  246. /* Common Tail Elimination */
  247. DO_STMT("Common tail elim #1")
  248. if (i)
  249.     {
  250.     i = j * 5;
  251.     k = 7 * l;
  252.     }
  253. else
  254.     {
  255.     i = j * 7;
  256.     k = 7 * l;
  257.     }
  258. OD
  259. DO_STMT("Common tail elim #2")
  260. if (i)
  261.     i = j * 5;
  262. else
  263.     i = j * 7;
  264. k = 7 * l
  265. OD
  266.  
  267.  
  268. /* Code Hoisting */
  269. DO_STMT("Code Hoisting #1")
  270. if (i)
  271.     {
  272.     k = 7 * l;
  273.     i = j * 5;
  274.     }
  275. else
  276.     {
  277.     k = 7 * l;
  278.     i = j * 7;
  279.     }
  280. OD
  281. DO_STMT("Code Hoisting #2")
  282. k = 7 * l;
  283. if (i)
  284.     i = j * 5;
  285. else
  286.     i = j * 7;
  287. OD
  288.  
  289. /* NOTES FOR OPTIMIZATION CHAPTER */
  290. /* There are machine-dependent optimizations which depend on the */
  291. /* the particular hardware and the instruction set */
  292. /* For example, special addressing modes for combining operations */
  293. /* such as the multiply and add of array indexing or */
  294. /* indirection and increment are supported as a single */
  295. /* addressing mode.  Are there others? */
  296.  
  297. /* Combining multiply and add */
  298. /* I'm not to sure about this test */
  299. DO_STMT("Combine mult-plus #1")
  300. i = j * 2 + 7
  301. OD
  302. DO_STMT("Combine mult-plus #2")
  303. i = j * 2
  304. OD
  305.  
  306. /* Combining indirection and incrementing */
  307. DO_STMT("Combine indr-plus #1")
  308. for (p = arr, j = 0; j < 14; ++j)
  309.     i = *p++;
  310. OD
  311. DO_STMT("Combine indr-plus #2")
  312. for (p = arr, j = 0; j < 14; ++j)
  313.     i = *p, p++;
  314. OD
  315.  
  316. /* MORE TESTS ADDED BY PLUM 85/02 */
  317.  
  318. /* various  if (...) optimizations */
  319. DO_STMT("if (i)")
  320. if (i) ++j;
  321. i = !i;
  322. OD
  323. DO_STMT("if (!i)")
  324. if (!i) ++j;
  325. i = !i;
  326. OD
  327. DO_STMT("if (i != 0)")
  328. if (i != 0) ++j;
  329. i = !i;
  330. OD
  331. DO_STMT("if (i == 0)")
  332. if (i == 0) ++j;
  333. i = !i;
  334. OD
  335.  
  336. }
  337.