lists.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* List management for the GCC expander.
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "diagnostic-core.h"
  20. #include "rtl.h"
  21. #include "ggc.h"
  22. static void free_list (rtx *, rtx *);
  23. /* Functions for maintaining cache-able lists of EXPR_LIST and INSN_LISTs. */
  24. /* An INSN_LIST containing all INSN_LISTs allocated but currently unused. */
  25. static GTY ((deletable)) rtx unused_insn_list;
  26. /* An EXPR_LIST containing all EXPR_LISTs allocated but currently unused. */
  27. static GTY ((deletable)) rtx unused_expr_list;
  28. /* This function will free an entire list of either EXPR_LIST, INSN_LIST
  29. or DEPS_LIST nodes. This is to be used only on lists that consist
  30. exclusively of nodes of one type only. This is only called by
  31. free_EXPR_LIST_list, free_INSN_LIST_list and free_DEPS_LIST_list. */
  32. static void
  33. free_list (rtx *listp, rtx *unused_listp)
  34. {
  35. rtx link, prev_link;
  36. prev_link = *listp;
  37. link = XEXP (prev_link, 1);
  38. gcc_assert (unused_listp != &unused_insn_list
  39. || GET_CODE (prev_link) == INSN_LIST);
  40. while (link)
  41. {
  42. gcc_assert (unused_listp != &unused_insn_list
  43. || GET_CODE (prev_link) == INSN_LIST);
  44. prev_link = link;
  45. link = XEXP (link, 1);
  46. }
  47. XEXP (prev_link, 1) = *unused_listp;
  48. *unused_listp = *listp;
  49. *listp = 0;
  50. }
  51. /* Find corresponding to ELEM node in the list pointed to by LISTP.
  52. This node must exist in the list. Returns pointer to that node. */
  53. static rtx *
  54. find_list_elem (rtx elem, rtx *listp)
  55. {
  56. while (XEXP (*listp, 0) != elem)
  57. listp = &XEXP (*listp, 1);
  58. return listp;
  59. }
  60. /* Remove the node pointed to by LISTP from the list. */
  61. static void
  62. remove_list_node (rtx *listp)
  63. {
  64. rtx node;
  65. node = *listp;
  66. *listp = XEXP (node, 1);
  67. XEXP (node, 1) = 0;
  68. }
  69. /* Removes corresponding to ELEM node from the list pointed to by LISTP.
  70. Returns that node. */
  71. rtx
  72. remove_list_elem (rtx elem, rtx *listp)
  73. {
  74. rtx node;
  75. listp = find_list_elem (elem, listp);
  76. node = *listp;
  77. remove_list_node (listp);
  78. return node;
  79. }
  80. /* This call is used in place of a gen_rtx_INSN_LIST. If there is a cached
  81. node available, we'll use it, otherwise a call to gen_rtx_INSN_LIST
  82. is made. */
  83. rtx_insn_list *
  84. alloc_INSN_LIST (rtx val, rtx next)
  85. {
  86. rtx_insn_list *r;
  87. if (unused_insn_list)
  88. {
  89. r = as_a <rtx_insn_list *> (unused_insn_list);
  90. unused_insn_list = r->next ();
  91. XEXP (r, 0) = val;
  92. XEXP (r, 1) = next;
  93. PUT_REG_NOTE_KIND (r, VOIDmode);
  94. gcc_assert (GET_CODE (r) == INSN_LIST);
  95. }
  96. else
  97. r = gen_rtx_INSN_LIST (VOIDmode, val, next);
  98. return r;
  99. }
  100. /* This call is used in place of a gen_rtx_EXPR_LIST. If there is a cached
  101. node available, we'll use it, otherwise a call to gen_rtx_EXPR_LIST
  102. is made. */
  103. rtx_expr_list *
  104. alloc_EXPR_LIST (int kind, rtx val, rtx next)
  105. {
  106. rtx_expr_list *r;
  107. if (unused_expr_list)
  108. {
  109. r = as_a <rtx_expr_list *> (unused_expr_list);
  110. unused_expr_list = XEXP (r, 1);
  111. XEXP (r, 0) = val;
  112. XEXP (r, 1) = next;
  113. PUT_REG_NOTE_KIND (r, kind);
  114. }
  115. else
  116. r = gen_rtx_EXPR_LIST ((machine_mode) kind, val, next);
  117. return r;
  118. }
  119. /* This function will free up an entire list of EXPR_LIST nodes. */
  120. void
  121. free_EXPR_LIST_list (rtx_expr_list **listp)
  122. {
  123. if (*listp == 0)
  124. return;
  125. free_list ((rtx *)listp, &unused_expr_list);
  126. }
  127. /* This function will free up an entire list of INSN_LIST nodes. */
  128. void
  129. free_INSN_LIST_list (rtx_insn_list **listp)
  130. {
  131. if (*listp == 0)
  132. return;
  133. free_list ((rtx *)listp, &unused_insn_list);
  134. }
  135. /* Make a copy of the INSN_LIST list LINK and return it. */
  136. rtx_insn_list *
  137. copy_INSN_LIST (rtx_insn_list *link)
  138. {
  139. rtx_insn_list *new_queue;
  140. rtx_insn_list **pqueue = &new_queue;
  141. for (; link; link = link->next ())
  142. {
  143. rtx_insn *x = link->insn ();
  144. rtx_insn_list *newlink = alloc_INSN_LIST (x, NULL);
  145. *pqueue = newlink;
  146. pqueue = (rtx_insn_list **)&XEXP (newlink, 1);
  147. }
  148. *pqueue = NULL;
  149. return new_queue;
  150. }
  151. /* Duplicate the INSN_LIST elements of COPY and prepend them to OLD. */
  152. rtx_insn_list *
  153. concat_INSN_LIST (rtx_insn_list *copy, rtx_insn_list *old)
  154. {
  155. rtx_insn_list *new_rtx = old;
  156. for (; copy ; copy = copy->next ())
  157. {
  158. new_rtx = alloc_INSN_LIST (copy->insn (), new_rtx);
  159. PUT_REG_NOTE_KIND (new_rtx, REG_NOTE_KIND (copy));
  160. }
  161. return new_rtx;
  162. }
  163. /* This function will free up an individual EXPR_LIST node. */
  164. void
  165. free_EXPR_LIST_node (rtx ptr)
  166. {
  167. XEXP (ptr, 1) = unused_expr_list;
  168. unused_expr_list = ptr;
  169. }
  170. /* This function will free up an individual INSN_LIST node. */
  171. void
  172. free_INSN_LIST_node (rtx ptr)
  173. {
  174. gcc_assert (GET_CODE (ptr) == INSN_LIST);
  175. XEXP (ptr, 1) = unused_insn_list;
  176. unused_insn_list = ptr;
  177. }
  178. /* Remove and free corresponding to ELEM node in the INSN_LIST pointed to
  179. by LISTP. */
  180. void
  181. remove_free_INSN_LIST_elem (rtx_insn *elem, rtx_insn_list **listp)
  182. {
  183. free_INSN_LIST_node (remove_list_elem (elem, (rtx *)listp));
  184. }
  185. /* Remove and free the first node in the INSN_LIST pointed to by LISTP. */
  186. rtx_insn *
  187. remove_free_INSN_LIST_node (rtx_insn_list **listp)
  188. {
  189. rtx_insn_list *node = *listp;
  190. rtx_insn *elem = node->insn ();
  191. remove_list_node ((rtx *)listp);
  192. free_INSN_LIST_node (node);
  193. return elem;
  194. }
  195. /* Remove and free the first node in the EXPR_LIST pointed to by LISTP. */
  196. rtx
  197. remove_free_EXPR_LIST_node (rtx_expr_list **listp)
  198. {
  199. rtx_expr_list *node = *listp;
  200. rtx elem = XEXP (node, 0);
  201. remove_list_node ((rtx *)listp);
  202. free_EXPR_LIST_node (node);
  203. return elem;
  204. }
  205. #include "gt-lists.h"