besched: Add and use sched_foreach_safe().
authorChristoph Mallon <christoph.mallon@gmx.de>
Fri, 14 Dec 2012 08:28:56 +0000 (09:28 +0100)
committerChristoph Mallon <christoph.mallon@gmx.de>
Fri, 14 Dec 2012 23:09:50 +0000 (00:09 +0100)
ir/be/bechordal.c
ir/be/beirgmod.c
ir/be/bepbqpcoloring.c
ir/be/besched.h
ir/be/ia32/ia32_finish.c

index 619be70..d2bfcae 100644 (file)
@@ -275,10 +275,8 @@ end:
 static void constraints(ir_node *const bl, void *const data)
 {
        be_chordal_env_t *const env = (be_chordal_env_t*)data;
-       for (ir_node *irn = sched_first(bl); !sched_is_end(irn);) {
-               ir_node *const next = sched_next(irn);
+       sched_foreach_safe(bl, irn) {
                handle_constraints(env, irn);
-               irn = next;
        }
 }
 
index ea14670..aa9b51b 100644 (file)
@@ -285,12 +285,8 @@ static void mark_dead_nodes_walker(ir_node *node, void *data)
 static void remove_dead_nodes_walker(ir_node *block, void *data)
 {
        remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
-       ir_node                 *node, *next;
-
-       for (node = sched_first(block); ! sched_is_end(node); node = next) {
-               /* get next node now, as after calling sched_remove it will be invalid */
-               next = sched_next(node);
 
+       sched_foreach_safe(block, node) {
                if (bitset_is_set(env->reachable, get_irn_idx(node)))
                        continue;
 
index cdc99e1..2411b28 100644 (file)
@@ -540,15 +540,11 @@ static void create_pbqp_coloring_instance(ir_node *block, void *data)
 static void insert_perms(ir_node *block, void *data)
 {
        be_chordal_env_t *env    = (be_chordal_env_t*)data;
-       ir_node          *irn;
 
-       for (irn = sched_first(block); !sched_is_end(irn);) {
-               ir_node   *const next = sched_next(irn);
-               be_insn_t *      insn = be_scan_insn(env, irn);
+       sched_foreach_safe(block, irn) {
+               be_insn_t *insn = be_scan_insn(env, irn);
                if (insn)
                        pre_process_constraints(env, &insn);
-
-               irn = next;
        }
 }
 
index 7b21400..aa8c314 100644 (file)
@@ -178,6 +178,16 @@ static inline bool sched_comes_after(const ir_node *n1, const ir_node *n2)
 #define sched_foreach_reverse(block,irn) \
        sched_foreach_reverse_before((assert(is_Block(block)), block), irn)
 
+/**
+ * A shorthand macro for iterating over a schedule while the current node may be
+ * removed or replaced.
+ *
+ * @param block  The block.
+ * @param irn    A ir node pointer used as an iterator.
+ */
+#define sched_foreach_safe(block, irn) \
+       for (ir_node *irn, *irn##__next = sched_first(block); !sched_is_end(irn = irn##__next) ? irn##__next = sched_next(irn), 1 : 0;)
+
 /**
  * Type for a function scheduling a graph
  */
index 72f46b5..89c123b 100644 (file)
@@ -376,18 +376,14 @@ static void fix_am_source(ir_node *irn)
  */
 static void ia32_finish_irg_walker(ir_node *block, void *env)
 {
-       ir_node *irn, *next;
        (void) env;
 
        /* first: turn back AM source if necessary */
-       for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
-               next = sched_next(irn);
+       sched_foreach_safe(block, irn) {
                fix_am_source(irn);
        }
 
-       for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
-               next = sched_next(irn);
-
+       sched_foreach_safe(block, irn) {
                /* check if there is a sub which need to be transformed */
                if (is_ia32_Sub(irn) || is_ia32_Sbb(irn) || is_ia32_xSub(irn)) {
                        ia32_transform_sub_to_neg_add(irn);
@@ -395,8 +391,7 @@ static void ia32_finish_irg_walker(ir_node *block, void *env)
        }
 
        /* second: insert copies and finish irg */
-       for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
-               next = sched_next(irn);
+       sched_foreach_safe(block, irn) {
                if (is_ia32_irn(irn)) {
                        /* some nodes are just a bit less efficient, but need no fixing if the
                         * should be same requirement is not fulfilled */