Home | History | Annotate | Line # | Download | only in gcc
gimple-iterator.h revision 1.1.1.1
      1 /* Header file for gimple iterators.
      2    Copyright (C) 2013-2015 Free Software Foundation, Inc.
      3 
      4 This file is part of GCC.
      5 
      6 GCC is free software; you can redistribute it and/or modify it under
      7 the terms of the GNU General Public License as published by the Free
      8 Software Foundation; either version 3, or (at your option) any later
      9 version.
     10 
     11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
     12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
     13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     14  for more details.
     15 
     16 You should have received a copy of the GNU General Public License
     17 along with GCC; see the file COPYING3.  If not see
     18 <http://www.gnu.org/licenses/>.  */
     19 
     20 #ifndef GCC_GIMPLE_ITERATOR_H
     21 #define GCC_GIMPLE_ITERATOR_H
     22 
     23 /* Iterator object for GIMPLE statement sequences.  */
     24 
     25 struct gimple_stmt_iterator
     26 {
     27   /* Sequence node holding the current statement.  */
     28   gimple_seq_node ptr;
     29 
     30   /* Sequence and basic block holding the statement.  These fields
     31      are necessary to handle edge cases such as when statement is
     32      added to an empty basic block or when the last statement of a
     33      block/sequence is removed.  */
     34   gimple_seq *seq;
     35   basic_block bb;
     36 };
     37 
     38 /* Iterator over GIMPLE_PHI statements.  */
     39 struct gphi_iterator : public gimple_stmt_iterator
     40 {
     41   gphi *phi () const
     42   {
     43     return as_a <gphi *> (ptr);
     44   }
     45 };
     46 
     47 enum gsi_iterator_update
     48 {
     49   GSI_NEW_STMT,		/* Only valid when single statement is added, move
     50 			   iterator to it.  */
     51   GSI_SAME_STMT,	/* Leave the iterator at the same statement.  */
     52   GSI_CONTINUE_LINKING	/* Move iterator to whatever position is suitable
     53 			   for linking other statements in the same
     54 			   direction.  */
     55 };
     56 
     57 extern void gsi_insert_seq_before_without_update (gimple_stmt_iterator *,
     58 						  gimple_seq,
     59 						  enum gsi_iterator_update);
     60 extern void gsi_insert_seq_before (gimple_stmt_iterator *, gimple_seq,
     61 				   enum gsi_iterator_update);
     62 extern void gsi_insert_seq_after_without_update (gimple_stmt_iterator *,
     63 						 gimple_seq,
     64 						 enum gsi_iterator_update);
     65 extern void gsi_insert_seq_after (gimple_stmt_iterator *, gimple_seq,
     66 				  enum gsi_iterator_update);
     67 extern gimple_seq gsi_split_seq_after (gimple_stmt_iterator);
     68 extern void gsi_set_stmt (gimple_stmt_iterator *, gimple);
     69 extern void gsi_split_seq_before (gimple_stmt_iterator *, gimple_seq *);
     70 extern bool gsi_replace (gimple_stmt_iterator *, gimple, bool);
     71 extern void gsi_replace_with_seq (gimple_stmt_iterator *, gimple_seq, bool);
     72 extern void gsi_insert_before_without_update (gimple_stmt_iterator *, gimple,
     73 					      enum gsi_iterator_update);
     74 extern void gsi_insert_before (gimple_stmt_iterator *, gimple,
     75 			       enum gsi_iterator_update);
     76 extern void gsi_insert_after_without_update (gimple_stmt_iterator *, gimple,
     77 					     enum gsi_iterator_update);
     78 extern void gsi_insert_after (gimple_stmt_iterator *, gimple,
     79 			      enum gsi_iterator_update);
     80 extern bool gsi_remove (gimple_stmt_iterator *, bool);
     81 extern gimple_stmt_iterator gsi_for_stmt (gimple);
     82 extern gphi_iterator gsi_for_phi (gphi *);
     83 extern void gsi_move_after (gimple_stmt_iterator *, gimple_stmt_iterator *);
     84 extern void gsi_move_before (gimple_stmt_iterator *, gimple_stmt_iterator *);
     85 extern void gsi_move_to_bb_end (gimple_stmt_iterator *, basic_block);
     86 extern void gsi_insert_on_edge (edge, gimple);
     87 extern void gsi_insert_seq_on_edge (edge, gimple_seq);
     88 extern basic_block gsi_insert_on_edge_immediate (edge, gimple);
     89 extern basic_block gsi_insert_seq_on_edge_immediate (edge, gimple_seq);
     90 extern void gsi_commit_edge_inserts (void);
     91 extern void gsi_commit_one_edge_insert (edge, basic_block *);
     92 extern gphi_iterator gsi_start_phis (basic_block);
     93 
     94 /* Return a new iterator pointing to GIMPLE_SEQ's first statement.  */
     95 
     96 static inline gimple_stmt_iterator
     97 gsi_start_1 (gimple_seq *seq)
     98 {
     99   gimple_stmt_iterator i;
    100 
    101   i.ptr = gimple_seq_first (*seq);
    102   i.seq = seq;
    103   i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
    104 
    105   return i;
    106 }
    107 
    108 #define gsi_start(x) gsi_start_1 (&(x))
    109 
    110 static inline gimple_stmt_iterator
    111 gsi_none (void)
    112 {
    113   gimple_stmt_iterator i;
    114   i.ptr = NULL;
    115   i.seq = NULL;
    116   i.bb = NULL;
    117   return i;
    118 }
    119 
    120 /* Return a new iterator pointing to the first statement in basic block BB.  */
    121 
    122 static inline gimple_stmt_iterator
    123 gsi_start_bb (basic_block bb)
    124 {
    125   gimple_stmt_iterator i;
    126   gimple_seq *seq;
    127 
    128   seq = bb_seq_addr (bb);
    129   i.ptr = gimple_seq_first (*seq);
    130   i.seq = seq;
    131   i.bb = bb;
    132 
    133   return i;
    134 }
    135 
    136 gimple_stmt_iterator gsi_start_edge (edge e);
    137 
    138 /* Return a new iterator initially pointing to GIMPLE_SEQ's last statement.  */
    139 
    140 static inline gimple_stmt_iterator
    141 gsi_last_1 (gimple_seq *seq)
    142 {
    143   gimple_stmt_iterator i;
    144 
    145   i.ptr = gimple_seq_last (*seq);
    146   i.seq = seq;
    147   i.bb = i.ptr ? gimple_bb (i.ptr) : NULL;
    148 
    149   return i;
    150 }
    151 
    152 #define gsi_last(x) gsi_last_1 (&(x))
    153 
    154 /* Return a new iterator pointing to the last statement in basic block BB.  */
    155 
    156 static inline gimple_stmt_iterator
    157 gsi_last_bb (basic_block bb)
    158 {
    159   gimple_stmt_iterator i;
    160   gimple_seq *seq;
    161 
    162   seq = bb_seq_addr (bb);
    163   i.ptr = gimple_seq_last (*seq);
    164   i.seq = seq;
    165   i.bb = bb;
    166 
    167   return i;
    168 }
    169 
    170 /* Return true if I is at the end of its sequence.  */
    171 
    172 static inline bool
    173 gsi_end_p (gimple_stmt_iterator i)
    174 {
    175   return i.ptr == NULL;
    176 }
    177 
    178 /* Return true if I is one statement before the end of its sequence.  */
    179 
    180 static inline bool
    181 gsi_one_before_end_p (gimple_stmt_iterator i)
    182 {
    183   return i.ptr != NULL && i.ptr->next == NULL;
    184 }
    185 
    186 /* Advance the iterator to the next gimple statement.  */
    187 
    188 static inline void
    189 gsi_next (gimple_stmt_iterator *i)
    190 {
    191   i->ptr = i->ptr->next;
    192 }
    193 
    194 /* Advance the iterator to the previous gimple statement.  */
    195 
    196 static inline void
    197 gsi_prev (gimple_stmt_iterator *i)
    198 {
    199   gimple prev = i->ptr->prev;
    200   if (prev->next)
    201     i->ptr = prev;
    202   else
    203     i->ptr = NULL;
    204 }
    205 
    206 /* Return the current stmt.  */
    207 
    208 static inline gimple
    209 gsi_stmt (gimple_stmt_iterator i)
    210 {
    211   return i.ptr;
    212 }
    213 
    214 /* Return a new iterator pointing to the first non-debug statement
    215    in basic block BB.  */
    216 
    217 static inline gimple_stmt_iterator
    218 gsi_start_bb_nondebug (basic_block bb)
    219 {
    220   gimple_stmt_iterator gsi = gsi_start_bb (bb);
    221   while (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
    222     gsi_next (&gsi);
    223 
    224   return gsi;
    225 }
    226 
    227 /* Return a block statement iterator that points to the first non-label
    228    statement in block BB.  */
    229 
    230 static inline gimple_stmt_iterator
    231 gsi_after_labels (basic_block bb)
    232 {
    233   gimple_stmt_iterator gsi = gsi_start_bb (bb);
    234 
    235   while (!gsi_end_p (gsi) && gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
    236     gsi_next (&gsi);
    237 
    238   return gsi;
    239 }
    240 
    241 /* Advance the iterator to the next non-debug gimple statement.  */
    242 
    243 static inline void
    244 gsi_next_nondebug (gimple_stmt_iterator *i)
    245 {
    246   do
    247     {
    248       gsi_next (i);
    249     }
    250   while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
    251 }
    252 
    253 /* Advance the iterator to the previous non-debug gimple statement.  */
    254 
    255 static inline void
    256 gsi_prev_nondebug (gimple_stmt_iterator *i)
    257 {
    258   do
    259     {
    260       gsi_prev (i);
    261     }
    262   while (!gsi_end_p (*i) && is_gimple_debug (gsi_stmt (*i)));
    263 }
    264 
    265 /* Return a new iterator pointing to the first non-debug statement in
    266    basic block BB.  */
    267 
    268 static inline gimple_stmt_iterator
    269 gsi_start_nondebug_bb (basic_block bb)
    270 {
    271   gimple_stmt_iterator i = gsi_start_bb (bb);
    272 
    273   if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
    274     gsi_next_nondebug (&i);
    275 
    276   return i;
    277 }
    278 
    279 /* Return a new iterator pointing to the first non-debug non-label statement in
    280    basic block BB.  */
    281 
    282 static inline gimple_stmt_iterator
    283 gsi_start_nondebug_after_labels_bb (basic_block bb)
    284 {
    285   gimple_stmt_iterator i = gsi_after_labels (bb);
    286 
    287   if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
    288     gsi_next_nondebug (&i);
    289 
    290   return i;
    291 }
    292 
    293 /* Return a new iterator pointing to the last non-debug statement in
    294    basic block BB.  */
    295 
    296 static inline gimple_stmt_iterator
    297 gsi_last_nondebug_bb (basic_block bb)
    298 {
    299   gimple_stmt_iterator i = gsi_last_bb (bb);
    300 
    301   if (!gsi_end_p (i) && is_gimple_debug (gsi_stmt (i)))
    302     gsi_prev_nondebug (&i);
    303 
    304   return i;
    305 }
    306 
    307 /* Iterates I statement iterator to the next non-virtual statement.  */
    308 
    309 static inline void
    310 gsi_next_nonvirtual_phi (gphi_iterator *i)
    311 {
    312   gphi *phi;
    313 
    314   if (gsi_end_p (*i))
    315     return;
    316 
    317   phi = i->phi ();
    318   gcc_assert (phi != NULL);
    319 
    320   while (virtual_operand_p (gimple_phi_result (phi)))
    321     {
    322       gsi_next (i);
    323 
    324       if (gsi_end_p (*i))
    325 	return;
    326 
    327       phi = i->phi ();
    328     }
    329 }
    330 
    331 /* Return the basic block associated with this iterator.  */
    332 
    333 static inline basic_block
    334 gsi_bb (gimple_stmt_iterator i)
    335 {
    336   return i.bb;
    337 }
    338 
    339 /* Return the sequence associated with this iterator.  */
    340 
    341 static inline gimple_seq
    342 gsi_seq (gimple_stmt_iterator i)
    343 {
    344   return *i.seq;
    345 }
    346 
    347 #endif /* GCC_GIMPLE_ITERATOR_H */
    348