rtl-iter.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* RTL iterators
  2. Copyright (C) 2014-2019 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. /* This structure describes the subrtxes of an rtx as follows:
  16. - if the rtx has no subrtxes, START and COUNT are both 0.
  17. - if all the subrtxes of an rtx are stored in a contiguous block
  18. of XEXPs ("e"s), START is the index of the first XEXP and COUNT
  19. is the number of them.
  20. - otherwise START is arbitrary and COUNT is UCHAR_MAX.
  21. rtx_all_subrtx_bounds applies to all codes. rtx_nonconst_subrtx_bounds
  22. is like rtx_all_subrtx_bounds except that all constant rtxes are treated
  23. as having no subrtxes. */
  24. struct rtx_subrtx_bound_info {
  25. unsigned char start;
  26. unsigned char count;
  27. };
  28. extern rtx_subrtx_bound_info rtx_all_subrtx_bounds[];
  29. extern rtx_subrtx_bound_info rtx_nonconst_subrtx_bounds[];
  30. /* Return true if CODE has no subrtxes. */
  31. static inline bool
  32. leaf_code_p (enum rtx_code code)
  33. {
  34. return rtx_all_subrtx_bounds[code].count == 0;
  35. }
  36. /* Used to iterate over subrtxes of an rtx. T abstracts the type of
  37. access. */
  38. template <typename T>
  39. class generic_subrtx_iterator
  40. {
  41. static const size_t LOCAL_ELEMS = 16;
  42. typedef typename T::value_type value_type;
  43. typedef typename T::rtx_type rtx_type;
  44. typedef typename T::rtunion_type rtunion_type;
  45. public:
  46. struct array_type
  47. {
  48. array_type ();
  49. ~array_type ();
  50. value_type stack[LOCAL_ELEMS];
  51. vec <value_type, va_heap, vl_embed> *heap;
  52. };
  53. generic_subrtx_iterator (array_type &, value_type,
  54. const rtx_subrtx_bound_info *);
  55. value_type operator * () const;
  56. bool at_end () const;
  57. void next ();
  58. void skip_subrtxes ();
  59. void substitute (value_type);
  60. private:
  61. /* The bounds to use for iterating over subrtxes. */
  62. const rtx_subrtx_bound_info *m_bounds;
  63. /* The storage used for the worklist. */
  64. array_type &m_array;
  65. /* The current rtx. */
  66. value_type m_current;
  67. /* The base of the current worklist. */
  68. value_type *m_base;
  69. /* The number of subrtxes in M_BASE. */
  70. size_t m_end;
  71. /* The following booleans shouldn't end up in registers or memory
  72. but just direct control flow. */
  73. /* True if the iteration is over. */
  74. bool m_done;
  75. /* True if we should skip the subrtxes of M_CURRENT. */
  76. bool m_skip;
  77. /* True if M_CURRENT has been replaced with a different rtx. */
  78. bool m_substitute;
  79. static void free_array (array_type &);
  80. static size_t add_subrtxes_to_queue (array_type &, value_type *, size_t,
  81. rtx_type);
  82. static value_type *add_single_to_queue (array_type &, value_type *, size_t,
  83. value_type);
  84. };
  85. template <typename T>
  86. inline generic_subrtx_iterator <T>::array_type::array_type () : heap (0) {}
  87. template <typename T>
  88. inline generic_subrtx_iterator <T>::array_type::~array_type ()
  89. {
  90. if (__builtin_expect (heap != 0, false))
  91. free_array (*this);
  92. }
  93. /* Iterate over X and its subrtxes, in arbitrary order. Use ARRAY to
  94. store the worklist. We use an external array in order to avoid
  95. capturing the fields of this structure when taking the address of
  96. the array. Use BOUNDS to find the bounds of simple "e"-string codes. */
  97. template <typename T>
  98. inline generic_subrtx_iterator <T>::
  99. generic_subrtx_iterator (array_type &array, value_type x,
  100. const rtx_subrtx_bound_info *bounds)
  101. : m_bounds (bounds),
  102. m_array (array),
  103. m_current (x),
  104. m_base (m_array.stack),
  105. m_end (0),
  106. m_done (false),
  107. m_skip (false),
  108. m_substitute (false)
  109. {
  110. }
  111. /* Return the current subrtx. */
  112. template <typename T>
  113. inline typename T::value_type
  114. generic_subrtx_iterator <T>::operator * () const
  115. {
  116. return m_current;
  117. }
  118. /* Return true if the iteration has finished. */
  119. template <typename T>
  120. inline bool
  121. generic_subrtx_iterator <T>::at_end () const
  122. {
  123. return m_done;
  124. }
  125. /* Move on to the next subrtx. */
  126. template <typename T>
  127. inline void
  128. generic_subrtx_iterator <T>::next ()
  129. {
  130. if (m_substitute)
  131. {
  132. m_substitute = false;
  133. m_skip = false;
  134. return;
  135. }
  136. if (!m_skip)
  137. {
  138. /* Add the subrtxes of M_CURRENT. */
  139. rtx_type x = T::get_rtx (m_current);
  140. if (__builtin_expect (x != 0, true))
  141. {
  142. enum rtx_code code = GET_CODE (x);
  143. ssize_t count = m_bounds[code].count;
  144. if (count > 0)
  145. {
  146. /* Handle the simple case of a single "e" block that is known
  147. to fit into the current array. */
  148. if (__builtin_expect (m_end + count <= LOCAL_ELEMS + 1, true))
  149. {
  150. /* Set M_CURRENT to the first subrtx and queue the rest. */
  151. ssize_t start = m_bounds[code].start;
  152. rtunion_type *src = &x->u.fld[start];
  153. if (__builtin_expect (count > 2, false))
  154. m_base[m_end++] = T::get_value (src[2].rt_rtx);
  155. if (count > 1)
  156. m_base[m_end++] = T::get_value (src[1].rt_rtx);
  157. m_current = T::get_value (src[0].rt_rtx);
  158. return;
  159. }
  160. /* Handle cases which aren't simple "e" sequences or where
  161. the sequence might overrun M_BASE. */
  162. count = add_subrtxes_to_queue (m_array, m_base, m_end, x);
  163. if (count > 0)
  164. {
  165. m_end += count;
  166. if (m_end > LOCAL_ELEMS)
  167. m_base = m_array.heap->address ();
  168. m_current = m_base[--m_end];
  169. return;
  170. }
  171. }
  172. }
  173. }
  174. else
  175. m_skip = false;
  176. if (m_end == 0)
  177. m_done = true;
  178. else
  179. m_current = m_base[--m_end];
  180. }
  181. /* Skip the subrtxes of the current rtx. */
  182. template <typename T>
  183. inline void
  184. generic_subrtx_iterator <T>::skip_subrtxes ()
  185. {
  186. m_skip = true;
  187. }
  188. /* Ignore the subrtxes of the current rtx and look at X instead. */
  189. template <typename T>
  190. inline void
  191. generic_subrtx_iterator <T>::substitute (value_type x)
  192. {
  193. m_substitute = true;
  194. m_current = x;
  195. }
  196. /* Iterators for const_rtx. */
  197. struct const_rtx_accessor
  198. {
  199. typedef const_rtx value_type;
  200. typedef const_rtx rtx_type;
  201. typedef const rtunion rtunion_type;
  202. static rtx_type get_rtx (value_type x) { return x; }
  203. static value_type get_value (rtx_type x) { return x; }
  204. };
  205. typedef generic_subrtx_iterator <const_rtx_accessor> subrtx_iterator;
  206. /* Iterators for non-constant rtx. */
  207. struct rtx_var_accessor
  208. {
  209. typedef rtx value_type;
  210. typedef rtx rtx_type;
  211. typedef rtunion rtunion_type;
  212. static rtx_type get_rtx (value_type x) { return x; }
  213. static value_type get_value (rtx_type x) { return x; }
  214. };
  215. typedef generic_subrtx_iterator <rtx_var_accessor> subrtx_var_iterator;
  216. /* Iterators for rtx *. */
  217. struct rtx_ptr_accessor
  218. {
  219. typedef rtx *value_type;
  220. typedef rtx rtx_type;
  221. typedef rtunion rtunion_type;
  222. static rtx_type get_rtx (value_type ptr) { return *ptr; }
  223. static value_type get_value (rtx_type &x) { return &x; }
  224. };
  225. typedef generic_subrtx_iterator <rtx_ptr_accessor> subrtx_ptr_iterator;
  226. #define ALL_BOUNDS rtx_all_subrtx_bounds
  227. #define NONCONST_BOUNDS rtx_nonconst_subrtx_bounds
  228. /* Use ITER to iterate over const_rtx X and its recursive subrtxes,
  229. using subrtx_iterator::array ARRAY as the storage for the worklist.
  230. ARRAY can be reused for multiple consecutive iterations but shouldn't
  231. be shared by two concurrent iterations. TYPE is ALL if all subrtxes
  232. are of interest or NONCONST if it is safe to ignore subrtxes of
  233. constants. */
  234. #define FOR_EACH_SUBRTX(ITER, ARRAY, X, TYPE) \
  235. for (subrtx_iterator ITER (ARRAY, X, TYPE##_BOUNDS); !ITER.at_end (); \
  236. ITER.next ())
  237. /* Like FOR_EACH_SUBRTX, but iterate over subrtxes of an rtx X. */
  238. #define FOR_EACH_SUBRTX_VAR(ITER, ARRAY, X, TYPE) \
  239. for (subrtx_var_iterator ITER (ARRAY, X, TYPE##_BOUNDS); !ITER.at_end (); \
  240. ITER.next ())
  241. /* Like FOR_EACH_SUBRTX, but iterate over subrtx pointers of rtx pointer X.
  242. For example, if X is &PATTERN (insn) and the pattern is a SET, iterate
  243. over &PATTERN (insn), &SET_DEST (PATTERN (insn)), etc. */
  244. #define FOR_EACH_SUBRTX_PTR(ITER, ARRAY, X, TYPE) \
  245. for (subrtx_ptr_iterator ITER (ARRAY, X, TYPE##_BOUNDS); !ITER.at_end (); \
  246. ITER.next ())