BugFix: fixed wrong include
[libfirm] / ir / ana / phiclass.c
1 /**
2  * @author Daniel Grund, Christian Wuerdig
3  * @cvsid  $Id$
4  * @date   09.08.2005
5  */
6 #ifdef HAVE_CONFIG_H
7 # include "config.h"
8 #endif
9
10 #ifdef HAVE_STDLIB_H
11 # include <stdlib.h>
12 #endif
13
14 #include "irnode.h"
15 #include "debug.h"
16 #include "irgwalk.h"
17 #include "irop_t.h"
18 #include "iredges_t.h"
19 #include "phiclass.h"
20 #include "irphase_t.h"
21
22 struct _phi_classes_t {
23         phase_t  ph;                 /* The phase object holding the irn data */
24         pset     *all_phi_classes;   /* A set containing all Phi classes */
25         ir_graph *irg;               /* The irg this is all about */
26         DEBUG_ONLY(firm_dbg_module_t *dbg);
27 };
28
29 typedef struct _irn_phi_class_t {
30         ir_node **phi_cls; /* the array of node pointers representing the class */
31 } irn_phi_class_t;
32
33 static INLINE ir_node **_get_phi_class(phase_t *ph, const ir_node *irn) {
34         irn_phi_class_t *ipc = phase_get_irn_data(ph, irn);
35         return ipc->phi_cls;
36 }
37
38 static INLINE void _set_phi_class(phase_t *ph, ir_node *irn, ir_node **cls) {
39         irn_phi_class_t *ipc = phase_get_irn_data(ph, irn);
40         ipc->phi_cls = cls;
41 }
42
43 /* initialize data structure for given irn in given phase */
44 static void *irn_phi_class_init(phase_t *ph, ir_node *irn, void *data) {
45         irn_phi_class_t *ipc = data ? data : phase_alloc(ph, sizeof(ipc[0]));
46         memset(ipc, 0, sizeof(ipc[0]));
47         return ipc;
48 }
49
50 /**
51  * Builds the phi class, starting from.
52  * @param phi_classes  The phi class object
53  * @param irn          The to start from
54  * @param pc           The phi class this irn should be put into.
55  *                     Set to NULL if you want a new one.
56  */
57 static void phi_class_build(phi_classes_t *phi_classes, ir_node *irn, ir_node **pc) {
58         const ir_edge_t *edge;
59
60         /* If irn has a phi class assigned already
61          * return immediately to stop recursion */
62         if (_get_phi_class(&phi_classes->ph, irn)) {
63                 DBG((phi_classes->dbg, LEVEL_2, "  already done for %+F\n", irn));
64                 return;
65         }
66
67         /* The initial call to phi_class_build doesn't
68          * provide a nodeset, so alloc it */
69         if (! pc) {
70                 DBG((phi_classes->dbg, LEVEL_1, "Computing phi class for %+F\n", irn));
71                 assert(is_Phi(irn));
72                 pc = NEW_ARR_F(ir_node *, 0);
73         }
74
75         /* Add the irn to the phi class */
76         DBG((phi_classes->dbg, LEVEL_1, "  adding %+F\n", irn));
77         ARR_APP1(ir_node *, pc, irn);
78         _set_phi_class(&phi_classes->ph, irn, pc);
79
80         /* Check the 'neighbour' irns */
81         if (is_Phi(irn) && mode_is_datab(get_irn_mode(irn))) {
82                 int i;
83
84                 /* Add all args of the phi to the phi-class. */
85                 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
86                         ir_node *op = get_irn_n(irn, i);
87                         DBG((phi_classes->dbg, LEVEL_2, "  checking arg %+F\n", op));
88                         phi_class_build(phi_classes, op, pc);
89                 }
90         }
91
92         /* Add a user of the irn to the class,
93          * iff it is a phi node  */
94         foreach_out_edge(irn, edge) {
95                 ir_node *user = edge->src;
96                 DBG((phi_classes->dbg, LEVEL_2, "  checking user %+F\n", user));
97                 if (is_Phi(user) && mode_is_datab(get_irn_mode(user)))
98                         phi_class_build(phi_classes, user, pc);
99         }
100 }
101
102 /**
103  * Call by a walker. If @p node is Phi, it build the Phi class starting from this node.
104  */
105 static void phi_class_construction_walker(ir_node *node, void *env) {
106         phi_classes_t *pc = env;
107         if (is_Phi(node) && mode_is_datab(get_irn_mode(node))) {
108                 phi_class_build(pc, node, NULL);
109                 pset_insert_ptr(pc->all_phi_classes, _get_phi_class(&pc->ph, node));
110         }
111 }
112
113 /**
114  * Walk over the irg and build the Phi classes.
115  */
116 static void phi_class_compute(phi_classes_t *pc) {
117         irg_walk_graph(pc->irg, phi_class_construction_walker, NULL, pc);
118 }
119
120 /**
121  * Build the Phi classes for the set of given Phis.
122  */
123 static void phi_class_compute_by_phis(phi_classes_t *pc, pset *all_phi_nodes) {
124         if (pset_count(all_phi_nodes)) {
125                 ir_node  *phi;
126
127                 foreach_pset(all_phi_nodes, phi) {
128                         ir_node **irn_pc = _get_phi_class(&pc->ph, phi);
129
130                         assert(is_Phi(phi) && mode_is_datab(get_irn_mode(phi)));
131
132                         if (! irn_pc) {
133                                 phi_class_build(pc, phi, NULL);
134                                 pset_insert_ptr(pc->all_phi_classes, _get_phi_class(&pc->ph, phi));
135                         }
136                 }
137         }
138 }
139
140 /**
141  * Return the array containing all nodes assigned to the same Phi class as @p irn.
142  */
143 ir_node **get_phi_class(phi_classes_t *pc, const ir_node *irn) {
144         return _get_phi_class(&pc->ph, irn);
145 }
146
147 /**
148  * Assigns a new array of nodes representing the new Phi class to @p irn.
149  */
150 void set_phi_class(phi_classes_t *pc, ir_node *irn, ir_node **cls) {
151         _set_phi_class(&pc->ph, irn, cls);
152 }
153
154 /**
155  * Returns a set containing all computed Phi classes.
156  */
157 pset *get_all_phi_classes(phi_classes_t *pc) {
158         return pc->all_phi_classes;
159 }
160
161 /**
162  * Builds the Phi classes for all Phis in @p irg.
163  * @return The Phi class object for the @p irg.
164  */
165 phi_classes_t *phi_class_new_from_irg(ir_graph *irg) {
166         phi_classes_t *res = xmalloc(sizeof(*res));
167
168         FIRM_DBG_REGISTER(res->dbg, "ir.ana.phiclass");
169         phase_init(&res->ph, "phi_classes", irg, PHASE_DEFAULT_GROWTH, irn_phi_class_init);
170
171         res->irg             = irg;
172         res->all_phi_classes = pset_new_ptr(5);
173
174         phi_class_compute(res);
175
176         return res;
177 }
178
179 /**
180  * Builds all Phi classes for the given set of Phis.
181  * @return The Phis class object for @p all_phis.
182  */
183 phi_classes_t *phi_class_new_from_set(ir_graph *irg, pset *all_phis) {
184         phi_classes_t *res = xmalloc(sizeof(*res));
185
186         FIRM_DBG_REGISTER(res->dbg, "ir.ana.phiclass");
187         phase_init(&res->ph, "phi_classes", irg, PHASE_DEFAULT_GROWTH, irn_phi_class_init);
188
189         res->irg             = irg;
190         res->all_phi_classes = pset_new_ptr(5);
191
192         phi_class_compute_by_phis(res, all_phis);
193
194         return res;
195 }
196
197 /**
198  * Free all allocated data.
199  */
200 void phi_class_free(phi_classes_t *pc) {
201         ir_node **ipc;
202         foreach_pset(pc->all_phi_classes, ipc) {
203                 DEL_ARR_F(ipc);
204         }
205         del_pset(pc->all_phi_classes);
206         phase_free(&pc->ph);
207         xfree(pc);
208 }