gimple-iterator.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* Header file for gimple iterators.
  2. Copyright (C) 2013-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. #ifndef GCC_GIMPLE_ITERATOR_H
  16. #define GCC_GIMPLE_ITERATOR_H
  17. /* Iterator object for GIMPLE statement sequences. */
  18. struct gimple_stmt_iterator
  19. {
  20. /* Sequence node holding the current statement. */
  21. gimple_seq_node ptr;
  22. /* Sequence and basic block holding the statement. These fields
  23. are necessary to handle edge cases such as when statement is
  24. added to an empty basic block or when the last statement of a
  25. block/sequence is removed. */
  26. gimple_seq *seq;
  27. basic_block bb;
  28. };
  29. /* Iterator over GIMPLE_PHI statements. */
  30. struct gphi_iterator : public gimple_stmt_iterator
  31. {
  32. gphi *phi () const
  33. {
  34. return as_a <gphi *> (ptr);
  35. }
  36. };
  37. enum gsi_iterator_update
  38. {
  39. GSI_NEW_STMT, /* Only valid when single statement is added, move
  40. iterator to it. */
  41. GSI_SAME_STMT, /* Leave the iterator at the same statement. */
  42. GSI_CONTINUE_LINKING /* Move iterator to whatever position is suitable
  43. for linking other statements in the same
  44. direction. */
  45. };
  46. extern void gsi_insert_seq_before_without_update (gimple_stmt_iterator *,
  47. gimple_seq,
  48. enum gsi_iterator_update);
  49. extern void gsi_insert_seq_before (gimple_stmt_iterator *, gimple_seq,
  50. enum gsi_iterator_update);
  51. extern void gsi_insert_seq_after_without_update (gimple_stmt_iterator *,
  52. gimple_seq,
  53. enum gsi_iterator_update);
  54. extern void gsi_insert_seq_after (gimple_stmt_iterator *, gimple_seq,
  55. enum gsi_iterator_update);
  56. extern gimple_seq gsi_split_seq_after (gimple_stmt_iterator);
  57. extern void gsi_set_stmt (gimple_stmt_iterator *, gimple);
  58. extern void gsi_split_seq_before (gimple_stmt_iterator *, gimple_seq *);
  59. extern bool gsi_replace (gimple_stmt_iterator *, gimple, bool);
  60. extern void gsi_replace_with_seq (gimple_stmt_iterator *, gimple_seq, bool);
  61. extern void gsi_insert_before_without_update (gimple_stmt_iterator *, gimple,
  62. enum gsi_iterator_update);
  63. extern void gsi_insert_before (gimple_stmt_iterator *, gimple,
  64. enum gsi_iterator_update);
  65. extern void gsi_insert_after_without_update (gimple_stmt_iterator *, gimple,
  66. enum gsi_iterator_update);
  67. extern void gsi_insert_after (gimple_stmt_iterator *, gimple,
  68. enum gsi_iterator_update);
  69. extern bool gsi_remove (gimple_stmt_iterator *, bool);
  70. extern gimple_stmt_iterator gsi_for_stmt (gimple);
  71. extern gphi_iterator gsi_for_phi (gphi *);
  72. extern void gsi_move_after (gimple_stmt_iterator *, gimple_stmt_iterator *);
  73. extern void gsi_move_before (gimple_stmt_iterator *, gimple_stmt_iterator *);
  74. extern void gsi_move_to_bb_end (gimple_stmt_iterator *, basic_block);
  75. extern void gsi_insert_on_edge (edge, gimple);
  76. extern void gsi_insert_seq_on_edge (edge, gimple_seq);
  77. extern basic_block gsi_insert_on_edge_immediate (edge, gimple);
  78. extern basic_block gsi_insert_seq_on_edge_immediate (edge, gimple_seq);
  79. extern void gsi_commit_edge_inserts (void);
  80. extern void gsi_commit_one_edge_insert (edge, basic_block *);
  81. extern gphi_iterator gsi_start_phis (basic_block);
  82. /* Return a new iterator pointing to GIMPLE_SEQ's first statement. */
  83. static inline gimple_stmt_iterator
  84. gsi_start_1 (gimple_seq *seq)
  85. {
  86. gimple_stmt_iterator i;
  87. i.ptr = gimple_seq_first (*seq);
  88. i.seq = seq;
  89. i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
  90. return i;
  91. }
  92. #define gsi_start(x) gsi_start_1 (&(x))
  93. static inline gimple_stmt_iterator
  94. gsi_none (void)
  95. {
  96. gimple_stmt_iterator i;
  97. i.ptr = NULL;
  98. i.seq = NULL;
  99. i.bb = NULL;
  100. return i;
  101. }
  102. /* Return a new iterator pointing to the first statement in basic block BB. */
  103. static inline gimple_stmt_iterator
  104. gsi_start_bb (basic_block bb)
  105. {
  106. gimple_stmt_iterator i;
  107. gimple_seq *seq;
  108. seq = bb_seq_addr (bb);
  109. i.ptr = gimple_seq_first (*seq);
  110. i.seq = seq;
  111. i.bb = bb;
  112. return i;
  113. }
  114. gimple_stmt_iterator gsi_start_edge (edge e);
  115. /* Return a new iterator initially pointing to GIMPLE_SEQ's last statement. */
  116. static inline gimple_stmt_iterator
  117. gsi_last_1 (gimple_seq *seq)
  118. {
  119. gimple_stmt_iterator i;
  120. i.ptr = gimple_seq_last (*seq);
  121. i.seq = seq;
  122. i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
  123. return i;
  124. }
  125. #define gsi_last(x) gsi_last_1 (&(x))
  126. /* Return a new iterator pointing to the last statement in basic block BB. */
  127. static inline gimple_stmt_iterator
  128. gsi_last_bb (basic_block bb)
  129. {
  130. gimple_stmt_iterator i;
  131. gimple_seq *seq;
  132. seq = bb_seq_addr (bb);
  133. i.ptr = gimple_seq_last (*seq);
  134. i.seq = seq;
  135. i.bb = bb;
  136. return i;
  137. }
  138. /* Return true if I is at the end of its sequence. */
  139. static inline bool
  140. gsi_end_p (gimple_stmt_iterator i)
  141. {
  142. return i.ptr == NULL;
  143. }
  144. /* Return true if I is one statement before the end of its sequence. */
  145. static inline bool
  146. gsi_one_before_end_p (gimple_stmt_iterator i)
  147. {
  148. return i.ptr != NULL && i.ptr->next == NULL;
  149. }
  150. /* Advance the iterator to the next gimple statement. */
  151. static inline void
  152. gsi_next (gimple_stmt_iterator *i)
  153. {
  154. i->ptr = i->ptr->next;
  155. }
  156. /* Advance the iterator to the previous gimple statement. */
  157. static inline void
  158. gsi_prev (gimple_stmt_iterator *i)
  159. {
  160. gimple prev = i->ptr->prev;
  161. if (prev->next)
  162. i->ptr = prev;
  163. else
  164. i->ptr = NULL;
  165. }
  166. /* Return the current stmt. */
  167. static inline gimple
  168. gsi_stmt (gimple_stmt_iterator i)
  169. {
  170. return i.ptr;
  171. }
  172. /* Return a new iterator pointing to the first non-debug statement
  173. in basic block BB. */
  174. static inline gimple_stmt_iterator
  175. gsi_start_bb_nondebug (basic_block bb)
  176. {
  177. gimple_stmt_iterator gsi = gsi_start_bb (bb);
  178. while (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
  179. gsi_next (&gsi);
  180. return gsi;
  181. }
  182. /* Return a block statement iterator that points to the first non-label
  183. statement in block BB. */
  184. static inline gimple_stmt_iterator
  185. gsi_after_labels (basic_block bb)
  186. {
  187. gimple_stmt_iterator gsi = gsi_start_bb (bb);
  188. while (!gsi_end_p (gsi) && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
  189. gsi_next (&gsi);
  190. return gsi;
  191. }
  192. /* Advance the iterator to the next non-debug gimple statement. */
  193. static inline void
  194. gsi_next_nondebug (gimple_stmt_iterator *i)
  195. {
  196. do
  197. {
  198. gsi_next (i);
  199. }
  200. while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
  201. }
  202. /* Advance the iterator to the previous non-debug gimple statement. */
  203. static inline void
  204. gsi_prev_nondebug (gimple_stmt_iterator *i)
  205. {
  206. do
  207. {
  208. gsi_prev (i);
  209. }
  210. while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
  211. }
  212. /* Return a new iterator pointing to the first non-debug statement in
  213. basic block BB. */
  214. static inline gimple_stmt_iterator
  215. gsi_start_nondebug_bb (basic_block bb)
  216. {
  217. gimple_stmt_iterator i = gsi_start_bb (bb);
  218. if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
  219. gsi_next_nondebug (&i);
  220. return i;
  221. }
  222. /* Return a new iterator pointing to the first non-debug non-label statement in
  223. basic block BB. */
  224. static inline gimple_stmt_iterator
  225. gsi_start_nondebug_after_labels_bb (basic_block bb)
  226. {
  227. gimple_stmt_iterator i = gsi_after_labels (bb);
  228. if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
  229. gsi_next_nondebug (&i);
  230. return i;
  231. }
  232. /* Return a new iterator pointing to the last non-debug statement in
  233. basic block BB. */
  234. static inline gimple_stmt_iterator
  235. gsi_last_nondebug_bb (basic_block bb)
  236. {
  237. gimple_stmt_iterator i = gsi_last_bb (bb);
  238. if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
  239. gsi_prev_nondebug (&i);
  240. return i;
  241. }
  242. /* Iterates I statement iterator to the next non-virtual statement. */
  243. static inline void
  244. gsi_next_nonvirtual_phi (gphi_iterator *i)
  245. {
  246. gphi *phi;
  247. if (gsi_end_p (*i))
  248. return;
  249. phi = i->phi ();
  250. gcc_assert (phi != NULL);
  251. while (virtual_operand_p (gimple_phi_result (phi)))
  252. {
  253. gsi_next (i);
  254. if (gsi_end_p (*i))
  255. return;
  256. phi = i->phi ();
  257. }
  258. }
  259. /* Return the basic block associated with this iterator. */
  260. static inline basic_block
  261. gsi_bb (gimple_stmt_iterator i)
  262. {
  263. return i.bb;
  264. }
  265. /* Return the sequence associated with this iterator. */
  266. static inline gimple_seq
  267. gsi_seq (gimple_stmt_iterator i)
  268. {
  269. return *i.seq;
  270. }
  271. #endif /* GCC_GIMPLE_ITERATOR_H */