2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Access function for backedges.
9 * @author Goetz Lindenmaier
15 #include "irgraph_t.h"
17 #include "irbackedge_t.h"
19 /*--------------------------------------------------------------------*/
20 /* Backedge information. */
21 /*--------------------------------------------------------------------*/
25 * Returns backarray if the node can have backedges, else returns
28 * Does not assert whether the backarray is correct -- use
31 static bitset_t *mere_get_backarray(const ir_node *n)
33 switch (get_irn_opcode(n)) {
35 if (!get_Block_matured(n)) return NULL;
37 assert(n->attr.block.backedge && "backedge array not allocated!");
38 return n->attr.block.backedge;
40 assert(n->attr.phi.u.backedge && "backedge array not allocated!");
41 return n->attr.phi.u.backedge;
49 * Returns backarray if the node can have backedges, else returns
52 static bitset_t *get_backarray(const ir_node *n)
54 bitset_t *ba = mere_get_backarray(n);
58 size_t bal = bitset_size(ba); /* avoid macro expansion in assertion. */
59 size_t inl = get_irn_arity(n);
60 assert(bal == inl && "backedge array with faulty length");
69 * Returns non-zero if node has no backarray, or
70 * if size of backarray == size of in array.
72 static int legal_backarray(const ir_node *n)
74 bitset_t *ba = mere_get_backarray(n);
75 if (ba && (bitset_size(ba) != (unsigned) get_irn_arity(n)))
81 void fix_backedges(struct obstack *obst, ir_node *n)
83 bitset_t *arr = mere_get_backarray(n);
90 arity = get_irn_arity(n);
91 if (bitset_size(arr) != (unsigned) arity) {
92 arr = new_backedge_arr(obst, arity);
94 opc = get_irn_opcode(n);
96 n->attr.phi.u.backedge = arr;
97 else if (opc == iro_Block) {
98 n->attr.block.backedge = arr;
102 assert(legal_backarray(n));
105 int is_backedge(const ir_node *n, int pos)
107 bitset_t *ba = get_backarray(n);
109 return bitset_is_set(ba, pos);
113 void set_backedge(ir_node *n, int pos)
115 bitset_t *ba = get_backarray(n);
116 assert(ba && "can only set backedges at Phi, Block nodes.");
120 void set_not_backedge(ir_node *n, int pos)
122 bitset_t *ba = get_backarray(n);
123 assert(ba && "can only set backedges at Phi, Block nodes.");
124 bitset_clear(ba, pos);
127 int has_backedges(const ir_node *n)
129 bitset_t *ba = get_backarray(n);
131 return !bitset_is_empty(ba);
136 void clear_backedges(ir_node *n)
138 bitset_t *ba = get_backarray(n);
140 bitset_clear_all(ba);
144 bitset_t *new_backedge_arr(struct obstack *obst, size_t size)
146 return bitset_obstack_alloc(obst, size);