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"
18 #include "raw_bitset.h"
20 /*--------------------------------------------------------------------*/
21 /* Backedge information. */
22 /*--------------------------------------------------------------------*/
26 * Returns backarray if the node can have backedges, else returns
29 * Does not assert whether the backarray is correct -- use
32 static bitset_t *mere_get_backarray(const ir_node *n)
34 switch (get_irn_opcode(n)) {
36 if (!get_Block_matured(n)) return NULL;
38 assert(n->attr.block.backedge && "backedge array not allocated!");
39 return n->attr.block.backedge;
41 assert(n->attr.phi.u.backedge && "backedge array not allocated!");
42 return n->attr.phi.u.backedge;
50 * Returns backarray if the node can have backedges, else returns
53 static bitset_t *get_backarray(const ir_node *n)
55 bitset_t *ba = mere_get_backarray(n);
59 size_t bal = bitset_size(ba); /* avoid macro expansion in assertion. */
60 size_t inl = get_irn_arity(n);
61 assert(bal == inl && "backedge array with faulty length");
70 * Returns non-zero if node has no backarray, or
71 * if size of backarray == size of in array.
73 static int legal_backarray(const ir_node *n)
75 bitset_t *ba = mere_get_backarray(n);
76 if (ba && (bitset_size(ba) != (unsigned) get_irn_arity(n)))
82 void fix_backedges(struct obstack *obst, ir_node *n)
84 bitset_t *arr = mere_get_backarray(n);
91 arity = get_irn_arity(n);
92 if (bitset_size(arr) != (unsigned) arity) {
93 arr = new_backedge_arr(obst, arity);
95 opc = get_irn_opcode(n);
97 n->attr.phi.u.backedge = arr;
98 else if (opc == iro_Block) {
99 n->attr.block.backedge = arr;
103 assert(legal_backarray(n));
106 int is_backedge(const ir_node *n, int pos)
108 bitset_t *ba = get_backarray(n);
110 return bitset_is_set(ba, pos);
114 void set_backedge(ir_node *n, int pos)
116 bitset_t *ba = get_backarray(n);
117 assert(ba && "can only set backedges at Phi, Block nodes.");
121 void set_not_backedge(ir_node *n, int pos)
123 bitset_t *ba = get_backarray(n);
124 assert(ba && "can only set backedges at Phi, Block nodes.");
125 bitset_clear(ba, pos);
128 int has_backedges(const ir_node *n)
130 bitset_t *ba = get_backarray(n);
132 return !bitset_is_empty(ba);
137 void clear_backedges(ir_node *n)
139 bitset_t *ba = get_backarray(n);
141 bitset_clear_all(ba);
145 bitset_t *new_backedge_arr(struct obstack *obst, size_t size)
147 return bitset_obstack_alloc(obst, size);