fix wrong usage of notify_edge for add_End_keepalive, add_Sync_pred and nodes with...
[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         unsigned pure_phi_classes;   /* Build pure Phi classes */
27         DEBUG_ONLY(firm_dbg_module_t *dbg);
28 };
29
30 typedef struct _irn_phi_class_t {
31         ir_node ***phi_cls; /* the array of node pointers representing the class */
32 } irn_phi_class_t;
33
34 static INLINE ir_node ***_get_phi_class(phase_t *ph, ir_node *irn) {
35         irn_phi_class_t *ipc = phase_get_or_set_irn_data(ph, irn);
36         return ipc->phi_cls;
37 }
38
39 static INLINE void _set_phi_class(phase_t *ph, ir_node *irn, ir_node ***cls) {
40         irn_phi_class_t *ipc = phase_get_or_set_irn_data(ph, irn);
41         ipc->phi_cls = cls;
42 }
43
44 /* initialize data structure for given irn in given phase */
45 static void *irn_phi_class_init(phase_t *ph, ir_node *irn, void *data) {
46         irn_phi_class_t *ipc = data ? data : phase_alloc(ph, sizeof(ipc[0]));
47         memset(ipc, 0, sizeof(ipc[0]));
48         return ipc;
49 }
50
51 /**
52  * Builds the phi class, starting from.
53  * @param phi_classes  The phi class object
54  * @param irn          The to start from
55  * @param pc           The phi class this irn should be put into.
56  *                     Set to NULL if you want a new one.
57  */
58 static void phi_class_build(phi_classes_t *phi_classes, ir_node *irn, ir_node ***pc) {
59         const ir_edge_t *edge;
60
61         assert((! phi_classes->pure_phi_classes || is_Phi(irn)) && "Node must be Phi when pure_phi_classes set.");
62
63         /* If irn has a phi class assigned already
64          * return immediately to stop recursion */
65         if (_get_phi_class(&phi_classes->ph, irn)) {
66                 DBG((phi_classes->dbg, LEVEL_2, "\talready done for %+F\n", irn));
67                 return;
68         }
69
70         /* The initial call to phi_class_build doesn't
71          * provide a nodeset, so alloc it */
72         if (! pc) {
73                 DBG((phi_classes->dbg, LEVEL_1, "Computing phi class for %+F:\n", irn));
74                 assert(is_Phi(irn));
75                 pc  = phase_alloc(&phi_classes->ph, sizeof(*pc));
76                 *pc = NEW_ARR_F(ir_node *, 0);
77                 DBG((phi_classes->dbg, LEVEL_2, "\tcreated class %p in container %p\n", *pc, pc));
78         }
79
80         /* Add the irn to the phi class */
81         DBG((phi_classes->dbg, LEVEL_1, "\t\tadding %+F to class %p, container %p\n", irn, *pc, pc));
82         ARR_APP1(ir_node *, *pc, irn);
83         _set_phi_class(&phi_classes->ph, irn, pc);
84
85         /* Check the 'neighbour' irns */
86         if (is_Phi(irn) && mode_is_datab(get_irn_mode(irn))) {
87                 int i;
88                 DBG((phi_classes->dbg, LEVEL_2, "\tchecking args of %+F:\n", irn));
89                 /* Add all args of the phi to the phi-class. */
90                 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
91                         ir_node *op = get_irn_n(irn, i);
92                         DBG((phi_classes->dbg, LEVEL_2, "\tchecking arg %+F\n", op));
93                         if (! phi_classes->pure_phi_classes || is_Phi(op))
94                                 phi_class_build(phi_classes, op, pc);
95                 }
96         }
97
98         /* Add a user of the irn to the class,
99          * iff it is a phi node  */
100         if (! phi_classes->pure_phi_classes || 1) {
101                 DBG((phi_classes->dbg, LEVEL_2, "\tchecking users of %+F:\n", irn));
102                 foreach_out_edge(irn, edge) {
103                         ir_node *user = edge->src;
104                         DBG((phi_classes->dbg, LEVEL_2, "\tchecking user %+F ... ", user));
105                         if (is_Phi(user) && mode_is_datab(get_irn_mode(user))) {
106                                 DB((phi_classes->dbg, LEVEL_2, "is a Phi, descend\n"));
107                                 phi_class_build(phi_classes, user, pc);
108                         }
109                         else {
110                                 DB((phi_classes->dbg, LEVEL_2, "not a Phi, skip\n"));
111                         }
112                 }
113         }
114 }
115
116 /**
117  * Call by a walker. If @p node is Phi, it build the Phi class starting from this node.
118  */
119 static void phi_class_construction_walker(ir_node *node, void *env) {
120         phi_classes_t *pc = env;
121
122         if (is_Phi(node) && mode_is_datab(get_irn_mode(node))) {
123                 ir_node ***irn_pc = _get_phi_class(&pc->ph, node);
124
125                 if (! irn_pc) {
126                         ir_node **pc_values;
127                         phi_class_build(pc, node, NULL);
128
129                         pc_values = *_get_phi_class(&pc->ph, node);
130                         DBG((pc->dbg, LEVEL_1, "inserting phiclass %p into all classes\n", pc_values));
131
132                         pset_insert_ptr(pc->all_phi_classes, pc_values);
133                 }
134         }
135 }
136
137 /**
138  * Walk over the irg and build the Phi classes.
139  */
140 static void phi_class_compute(phi_classes_t *pc) {
141         irg_walk_graph(pc->irg, phi_class_construction_walker, NULL, pc);
142 }
143
144 /**
145  * Build the Phi classes for the set of given Phis.
146  */
147 static void phi_class_compute_by_phis(phi_classes_t *pc, pset *all_phi_nodes) {
148         if (pset_count(all_phi_nodes)) {
149                 ir_node *phi;
150
151                 foreach_pset(all_phi_nodes, phi) {
152                         ir_node ***irn_pc = _get_phi_class(&pc->ph, phi);
153
154                         assert(is_Phi(phi) && mode_is_datab(get_irn_mode(phi)));
155
156                         if (! irn_pc) {
157                                 ir_node **pc_values;
158
159                                 phi_class_build(pc, phi, NULL);
160
161                                 pc_values = *_get_phi_class(&pc->ph, phi);
162                                 DBG((pc->dbg, LEVEL_1, "inserting phiclass %p into all classes\n", pc_values));
163
164                                 pset_insert_ptr(pc->all_phi_classes, pc_values);
165                         }
166                 }
167         }
168 }
169
170 /**
171  * Return the array containing all nodes assigned to the same Phi class as @p irn.
172  */
173 ir_node **get_phi_class(phi_classes_t *pc, ir_node *irn) {
174         return *_get_phi_class(&pc->ph, irn);
175 }
176
177 /**
178  * Assigns a new array of nodes representing the new Phi class to @p irn.
179  */
180 void set_phi_class(phi_classes_t *pc, ir_node *irn, ir_node **cls) {
181         _set_phi_class(&pc->ph, irn, &cls);
182 }
183
184 /**
185  * Returns a set containing all computed Phi classes.
186  */
187 pset *get_all_phi_classes(phi_classes_t *pc) {
188         return pc->all_phi_classes;
189 }
190
191 /**
192  * Builds the Phi classes for all Phis in @p irg.
193  * @return The Phi class object for the @p irg.
194  */
195 phi_classes_t *phi_class_new_from_irg(ir_graph *irg, int pure_phi_classes) {
196         phi_classes_t *res = xmalloc(sizeof(*res));
197
198         FIRM_DBG_REGISTER(res->dbg, "ir.ana.phiclass");
199         phase_init(&res->ph, "phi_classes", irg, PHASE_DEFAULT_GROWTH, irn_phi_class_init);
200
201         res->irg              = irg;
202         res->all_phi_classes  = pset_new_ptr(5);
203         res->pure_phi_classes = pure_phi_classes;
204
205         phi_class_compute(res);
206
207         return res;
208 }
209
210 /**
211  * Builds all Phi classes for the given set of Phis.
212  * @return The Phis class object for @p all_phis.
213  */
214 phi_classes_t *phi_class_new_from_set(ir_graph *irg, pset *all_phis, int pure_phi_classes) {
215         phi_classes_t *res = xmalloc(sizeof(*res));
216
217         FIRM_DBG_REGISTER(res->dbg, "ir.ana.phiclass");
218         phase_init(&res->ph, "phi_classes", irg, PHASE_DEFAULT_GROWTH, irn_phi_class_init);
219
220         res->irg              = irg;
221         res->all_phi_classes  = pset_new_ptr(5);
222         res->pure_phi_classes = pure_phi_classes;
223
224         phi_class_compute_by_phis(res, all_phis);
225
226         return res;
227 }
228
229 /**
230  * Free all allocated data.
231  */
232 void phi_class_free(phi_classes_t *pc) {
233         ir_node **ipc;
234         foreach_pset(pc->all_phi_classes, ipc) {
235                 DEL_ARR_F(ipc);
236         }
237         del_pset(pc->all_phi_classes);
238         phase_free(&pc->ph);
239         xfree(pc);
240 }