becopyilp: Do not advertise the switch to dump the solution, because this is not...
[libfirm] / ir / ana / irbackedge.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief     Access function for backedges.
9  * @author    Goetz Lindenmaier
10  * @date      7.2002
11  */
12 #include "config.h"
13
14 #include "irnode_t.h"
15 #include "irgraph_t.h"
16 #include "array.h"
17 #include "irbackedge_t.h"
18 #include "raw_bitset.h"
19
20 /*--------------------------------------------------------------------*/
21 /* Backedge information.                                              */
22 /*--------------------------------------------------------------------*/
23
24
25 /**
26  * Returns backarray if the node can have backedges, else returns
27  * NULL.
28  *
29  * Does not assert whether the backarray is correct -- use
30  * very careful!
31  */
32 static bitset_t *mere_get_backarray(const ir_node *n)
33 {
34         switch (get_irn_opcode(n)) {
35         case iro_Block:
36                 if (!get_Block_matured(n)) return NULL;
37
38                 assert(n->attr.block.backedge && "backedge array not allocated!");
39                 return n->attr.block.backedge;
40         case iro_Phi:
41                 assert(n->attr.phi.u.backedge && "backedge array not allocated!");
42                 return n->attr.phi.u.backedge;
43         default:
44                 break;
45         }
46         return NULL;
47 }
48
49 /**
50  * Returns backarray if the node can have backedges, else returns
51  * NULL.
52  */
53 static bitset_t *get_backarray(const ir_node *n)
54 {
55         bitset_t *ba = mere_get_backarray(n);
56
57 #ifndef NDEBUG
58         if (ba) {
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");
62         }
63 #endif
64
65         return ba;
66 }
67
68 #ifndef NDEBUG
69 /**
70  * Returns non-zero if node has no backarray, or
71  *                  if size of backarray == size of in array.
72  */
73 static int legal_backarray(const ir_node *n)
74 {
75         bitset_t *ba = mere_get_backarray(n);
76         if (ba && (bitset_size(ba) != (unsigned) get_irn_arity(n)))
77                 return 0;
78         return 1;
79 }
80 #endif
81
82 void fix_backedges(struct obstack *obst, ir_node *n)
83 {
84         bitset_t *arr = mere_get_backarray(n);
85         unsigned opc;
86         int arity;
87
88         if (! arr)
89                 return;
90
91         arity = get_irn_arity(n);
92         if (bitset_size(arr) != (unsigned) arity) {
93                 arr = new_backedge_arr(obst, arity);
94
95                 opc = get_irn_opcode(n);
96                 if (opc == iro_Phi)
97                         n->attr.phi.u.backedge = arr;
98                 else if (opc == iro_Block) {
99                         n->attr.block.backedge = arr;
100                 }
101         }
102
103         assert(legal_backarray(n));
104 }
105
106 int is_backedge(const ir_node *n, int pos)
107 {
108         bitset_t *ba = get_backarray(n);
109         if (ba)
110                 return bitset_is_set(ba, pos);
111         return 0;
112 }
113
114 void set_backedge(ir_node *n, int pos)
115 {
116         bitset_t *ba = get_backarray(n);
117         assert(ba && "can only set backedges at Phi, Block nodes.");
118         bitset_set(ba, pos);
119 }
120
121 void set_not_backedge(ir_node *n, int pos)
122 {
123         bitset_t *ba = get_backarray(n);
124         assert(ba && "can only set backedges at Phi, Block nodes.");
125         bitset_clear(ba, pos);
126 }
127
128 int has_backedges(const ir_node *n)
129 {
130         bitset_t *ba = get_backarray(n);
131         if (ba != NULL) {
132                 return !bitset_is_empty(ba);
133         }
134         return 0;
135 }
136
137 void clear_backedges(ir_node *n)
138 {
139         bitset_t *ba = get_backarray(n);
140         if (ba != NULL) {
141                 bitset_clear_all(ba);
142         }
143 }
144
145 bitset_t *new_backedge_arr(struct obstack *obst, size_t size)
146 {
147         return bitset_obstack_alloc(obst, size);
148 }