PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/misc/pascal/insn16/popt/plopt.c

https://github.com/markangelo/PX4NuttX
C | 250 lines | 161 code | 31 blank | 58 comment | 45 complexity | f088f0ff3c30b18b14e57a3f7d47a81a MD5 | raw file
Possible License(s): GPL-2.0, 0BSD
  1. /**********************************************************************
  2. * plopt.c
  3. * Load/Store Optimizations
  4. *
  5. * Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. **********************************************************************/
  36. /**********************************************************************
  37. * Included Files
  38. **********************************************************************/
  39. #include <stdint.h>
  40. #include <stdio.h>
  41. #include "keywords.h"
  42. #include "pdefs.h"
  43. #include "pinsn16.h"
  44. #include "popt.h"
  45. #include "polocal.h"
  46. #include "plopt.h"
  47. /**********************************************************************/
  48. int16_t LoadOptimize(void)
  49. {
  50. uint16_t val;
  51. int16_t nchanges = 0;
  52. register int16_t i;
  53. TRACE(stderr, "[LoadOptimize]");
  54. /* At least two pcodes are need to perform Load optimizations */
  55. i = 0;
  56. while (i < nops-1)
  57. {
  58. switch (pptr[i]->op)
  59. {
  60. /* Eliminate duplicate loads */
  61. case oLDSH :
  62. if ((pptr[i+1]->op == oLDSH) &&
  63. (pptr[i+1]->arg1 == pptr[i]->arg1) &&
  64. (pptr[i+1]->arg2 == pptr[i]->arg2))
  65. {
  66. pptr[i+1]->op = oDUPH;
  67. pptr[i+1]->arg1 = 0;
  68. pptr[i+1]->arg2 = 0;
  69. nchanges++;
  70. i += 2;
  71. } /* end if */
  72. else i++;
  73. break;
  74. /* Convert loads indexed by a constant to unindexed loads */
  75. case oPUSH :
  76. case oPUSHB :
  77. /* Get the index value */
  78. if (pptr[i]->op == oPUSH)
  79. {
  80. val = pptr[i]->arg2;
  81. }
  82. else
  83. {
  84. val = pptr[i]->arg1;
  85. }
  86. /* If the following instruction is a load, add the constant
  87. * index value to the address and switch the opcode to the
  88. * unindexed form.
  89. */
  90. if (pptr[i+1]->op == oLDSXH)
  91. {
  92. pptr[i+1]->op = oLDSH;
  93. pptr[i+1]->arg2 += val;
  94. deletePcode (i);
  95. nchanges++;
  96. } /* end if */
  97. else if (pptr[i+1]->op == oLASX)
  98. {
  99. pptr[i+1]->op = oLAS;
  100. pptr[i+1]->arg2 += val;
  101. deletePcode (i);
  102. nchanges++;
  103. } /* end else if */
  104. else if (pptr[i+1]->op == oLDSXB)
  105. {
  106. pptr[i+1]->op = oLDSB;
  107. pptr[i+1]->arg2 += val;
  108. deletePcode (i);
  109. nchanges++;
  110. } /* end if */
  111. else if (pptr[i+1]->op == oLDSXM)
  112. {
  113. pptr[i+1]->op = oLDSM;
  114. pptr[i+1]->arg2 += val;
  115. deletePcode (i);
  116. nchanges++;
  117. } /* end if */
  118. else if (val < 256)
  119. {
  120. pptr[i]->op = oPUSHB;
  121. pptr[i]->arg1 = val;
  122. pptr[i]->arg2 = 0;
  123. i++;
  124. } /* end else if */
  125. else i++;
  126. break;
  127. default :
  128. i++;
  129. break;
  130. } /* end switch */
  131. } /* end while */
  132. return (nchanges);
  133. } /* end LoadOptimize */
  134. /**********************************************************************/
  135. int16_t StoreOptimize (void)
  136. {
  137. uint16_t val;
  138. int16_t nchanges = 0;
  139. register int16_t i;
  140. TRACE(stderr, "[StoreOptimize]");
  141. /* At least two pcodes are need to perform the following Store */
  142. /* optimizations */
  143. i = 0;
  144. while (i < nops-1)
  145. {
  146. switch (pptr[i]->op)
  147. {
  148. /* Eliminate store followed by load */
  149. case oSTSH :
  150. if ((pptr[i+1]->op == oLDSH) &&
  151. (pptr[i+1]->arg1 == pptr[i]->arg1) &&
  152. (pptr[i+1]->arg2 == pptr[i]->arg2))
  153. {
  154. pptr[i+1]->op = oSTSH;
  155. pptr[i]->op = oDUPH;
  156. pptr[i]->arg1 = 0;
  157. pptr[i]->arg2 = 0;
  158. nchanges++;
  159. i += 2;
  160. } /* end if */
  161. else i++;
  162. break;
  163. /* Convert stores indexed by a constant to unindexed stores */
  164. case oPUSH :
  165. /* Get the index value */
  166. if (pptr[i]->op == oPUSH)
  167. {
  168. val = pptr[i]->arg2;
  169. }
  170. else
  171. {
  172. val = pptr[i]->arg1;
  173. }
  174. /* If the following instruction is a store, add the constant
  175. * index value to the address and switch the opcode to the
  176. * unindexed form.
  177. */
  178. if (i < nops-2)
  179. {
  180. if (pptr[i+2]->op == oSTSXH)
  181. {
  182. pptr[i+2]->op = oSTSH;
  183. pptr[i+2]->arg2 += pptr[i]->arg2;
  184. deletePcode (i);
  185. nchanges++;
  186. } /* end if */
  187. else if (pptr[i+2]->op == oSTSXB)
  188. {
  189. pptr[i+2]->op = oSTSB;
  190. pptr[i+2]->arg2 += pptr[i]->arg2;
  191. deletePcode (i);
  192. nchanges++;
  193. } /* end if */
  194. else i++;
  195. } /* end if */
  196. else i++;
  197. break;
  198. case oPUSHB :
  199. if (i < nops-2)
  200. {
  201. if (pptr[i+2]->op == oSTSXB)
  202. {
  203. pptr[i+2]->op = oSTSB;
  204. pptr[i+2]->arg2 += pptr[i]->arg2;
  205. deletePcode (i);
  206. nchanges++;
  207. } /* end if */
  208. else i++;
  209. } /* end if */
  210. else i++;
  211. break;
  212. default :
  213. i++;
  214. break;
  215. } /* end switch */
  216. } /* end while */
  217. return (nchanges);
  218. } /* end StoreOptimize */
  219. /**********************************************************************/