/misc/pascal/insn32/popt/plopt.c

https://github.com/l--putt/nuttx-bb · C · 215 lines · 127 code · 30 blank · 58 comment · 32 complexity · 17956c4b8bb2836c9cbb21fec8bc3770 MD5 · raw file

  1. /**********************************************************************
  2. * plopt.c
  3. * Load/Store Optimizations
  4. *
  5. * Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
  6. * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
  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 "pinsn32.h"
  44. #include "popt.h"
  45. #include "polocal.h"
  46. #include "plopt.h"
  47. /**********************************************************************/
  48. int LoadOptimize(void)
  49. {
  50. uint32_t val;
  51. int nchanges = 0;
  52. register int 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 (GETOP(pptr[i]))
  59. {
  60. /* Eliminate duplicate loads */
  61. case oLDSH :
  62. if ((GETOP(pptr[i+1]) == oLDSH) &&
  63. (GETARG(pptr[i+1]) == GETARG(pptr[i])))
  64. {
  65. PUTOP(pptr[i+1], oDUP);
  66. PUTARG(pptr[i+1], 0);
  67. nchanges++;
  68. i += 2;
  69. } /* end if */
  70. else i++;
  71. break;
  72. /* Convert loads indexed by a constant to unindexed loads */
  73. case oPUSH :
  74. /* Get the index value */
  75. val = (int32_t)GETARG(pptr[i]);
  76. /* If the following instruction is a load, add the constant
  77. * index value to the address and switch the opcode to the
  78. * unindexed form.
  79. */
  80. if (GETOP(pptr[i+1]) == oLDSXH)
  81. {
  82. PUTOP(pptr[i+1], oLDSH);
  83. val += GETARG(pptr[i+1]);
  84. PUTARG(pptr[i+1], val);
  85. deletePcode (i);
  86. nchanges++;
  87. } /* end if */
  88. else if (GETOP(pptr[i+1]) == oLASX)
  89. {
  90. PUTOP(pptr[i+1], oLAS);
  91. val += GETARG(pptr[i+1]);
  92. PUTARG(pptr[i+1], val);
  93. deletePcode (i);
  94. nchanges++;
  95. } /* end else if */
  96. else if (GETOP(pptr[i+1]) == oLDSXB)
  97. {
  98. PUTOP(pptr[i+1], oLDSB);
  99. val += GETARG(pptr[i+1]);
  100. PUTARG(pptr[i+1], val);
  101. deletePcode (i);
  102. nchanges++;
  103. } /* end if */
  104. else if (GETOP(pptr[i+1]) == oLDSXM)
  105. {
  106. PUTOP(pptr[i+1], oLDSM);
  107. val += GETARG(pptr[i+1]);
  108. PUTARG(pptr[i+1], val);
  109. deletePcode (i);
  110. nchanges++;
  111. } /* end if */
  112. else i++;
  113. break;
  114. default :
  115. i++;
  116. break;
  117. } /* end switch */
  118. } /* end while */
  119. return (nchanges);
  120. } /* end LoadOptimize */
  121. /**********************************************************************/
  122. int StoreOptimize (void)
  123. {
  124. uint32_t val;
  125. int nchanges = 0;
  126. register int i;
  127. TRACE(stderr, "[StoreOptimize]");
  128. /* At least two pcodes are need to perform the following Store */
  129. /* optimizations */
  130. i = 0;
  131. while (i < nops-1)
  132. {
  133. switch (GETOP(pptr[i]))
  134. {
  135. /* Eliminate store followed by load */
  136. case oSTSH :
  137. if ((GETOP(pptr[i+1]) == oLDSH) &&
  138. (GETARG(pptr[i+1]) == GETARG(pptr[i])))
  139. {
  140. PUTOP(pptr[i+1], oSTSH);
  141. PUTOP(pptr[i], oDUP);
  142. PUTARG(pptr[i], 0);
  143. nchanges++;
  144. i += 2;
  145. } /* end if */
  146. else i++;
  147. break;
  148. /* Convert stores indexed by a constant to unindexed stores */
  149. case oPUSH :
  150. /* Get the index value */
  151. val = (int32_t)GETARG(pptr[i]);
  152. /* If the following instruction is a store, add the constant
  153. * index value to the address and switch the opcode to the
  154. * unindexed form.
  155. */
  156. if (i < nops-2)
  157. {
  158. if (GETOP(pptr[i+2]) == oSTSXH)
  159. {
  160. PUTOP(pptr[i+2], oSTSH);
  161. val += GETARG(pptr[i+2]);
  162. PUTARG(pptr[i+2], val);
  163. deletePcode (i);
  164. nchanges++;
  165. } /* end if */
  166. else if (GETOP(pptr[i+2]) == oSTSXB)
  167. {
  168. PUTOP(pptr[i+2], oSTSB);
  169. val += GETARG(pptr[i+2]);
  170. PUTARG(pptr[i+2], val);
  171. deletePcode (i);
  172. nchanges++;
  173. } /* end if */
  174. else i++;
  175. } /* end if */
  176. else i++;
  177. break;
  178. default :
  179. i++;
  180. break;
  181. } /* end switch */
  182. } /* end while */
  183. return (nchanges);
  184. } /* end StoreOptimize */
  185. /**********************************************************************/