- add support for statistics and merge debug info
[libfirm] / ir / ana / phiclass.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief  Implementation of phiclass analysis
23  * @author Daniel Grund, Christian Wuerdig
24  * @cvsid  $Id$
25  * @date   09.08.2005
26  */
27 #include "config.h"
28
29 #include <stdlib.h>
30
31 #include "irnode.h"
32 #include "debug.h"
33 #include "irgwalk.h"
34 #include "irop_t.h"
35 #include "iredges_t.h"
36 #include "phiclass.h"
37 #include "irphase_t.h"
38 #include "irnodeset.h"
39
40 struct _phi_classes_t {
41         ir_phase ph;                 /* The phase object holding the irn data */
42         pset     *all_phi_classes;   /* A set containing all Phi classes */
43         ir_graph *irg;               /* The irg this is all about */
44         unsigned pure_phi_classes;   /* Build pure Phi classes */
45         DEBUG_ONLY(firm_dbg_module_t *dbg);
46 };
47
48 typedef struct _irn_phi_class_t {
49         ir_node ***phi_cls; /* the array of node pointers representing the class */
50 } irn_phi_class_t;
51
52 static inline ir_node ***_get_phi_class(ir_phase *ph, ir_node *irn) {
53         irn_phi_class_t *ipc = phase_get_or_set_irn_data(ph, irn);
54         return ipc->phi_cls;
55 }
56
57 static inline void _set_phi_class(ir_phase *ph, ir_node *irn, ir_node ***cls) {
58         irn_phi_class_t *ipc = phase_get_or_set_irn_data(ph, irn);
59         ipc->phi_cls = cls;
60 }
61
62 /* initialize data structure for given irn in given phase */
63 static void *irn_phi_class_init(ir_phase *ph, const ir_node *irn, void *data) {
64         irn_phi_class_t *ipc = data ? data : phase_alloc(ph, sizeof(ipc[0]));
65         (void) irn;
66         memset(ipc, 0, sizeof(ipc[0]));
67         return ipc;
68 }
69
70 /**
71  * Builds the phi class, starting from.
72  * @param phi_classes  The phi class object
73  * @param irn          The to start from
74  * @param pc           The phi class this irn should be put into.
75  *                     Set to NULL if you want a new one.
76  */
77 static void phi_class_build(phi_classes_t *phi_classes, ir_node *irn, ir_node ***pc) {
78         const ir_edge_t *edge;
79
80         assert((! phi_classes->pure_phi_classes || is_Phi(irn)) && "Node must be Phi when pure_phi_classes set.");
81
82         /* If irn has a phi class assigned already
83          * return immediately to stop recursion */
84         if (_get_phi_class(&phi_classes->ph, irn)) {
85                 DBG((phi_classes->dbg, LEVEL_2, "\talready done for %+F\n", irn));
86                 return;
87         }
88
89         /* The initial call to phi_class_build doesn't
90          * provide a nodeset, so alloc it */
91         if (! pc) {
92                 DBG((phi_classes->dbg, LEVEL_1, "Computing phi class for %+F:\n", irn));
93                 assert(is_Phi(irn));
94                 pc  = phase_alloc(&phi_classes->ph, sizeof(*pc));
95                 *pc = NEW_ARR_F(ir_node *, 0);
96                 DBG((phi_classes->dbg, LEVEL_2, "\tcreated class %p in container %p\n", *pc, pc));
97         }
98
99         /* Add the irn to the phi class */
100         DBG((phi_classes->dbg, LEVEL_1, "\t\tadding %+F to class %p, container %p\n", irn, *pc, pc));
101         ARR_APP1(ir_node *, *pc, irn);
102         _set_phi_class(&phi_classes->ph, irn, pc);
103
104         /* Check the 'neighbour' irns */
105         if (is_Phi(irn) && mode_is_datab(get_irn_mode(irn))) {
106                 int i;
107                 DBG((phi_classes->dbg, LEVEL_2, "\tchecking args of %+F:\n", irn));
108                 /* Add all args of the phi to the phi-class. */
109                 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
110                         ir_node *op = get_irn_n(irn, i);
111                         DBG((phi_classes->dbg, LEVEL_2, "\tchecking arg %+F\n", op));
112                         if (! phi_classes->pure_phi_classes || is_Phi(op))
113                                 phi_class_build(phi_classes, op, pc);
114                 }
115         }
116
117         /* Add a user of the irn to the class,
118          * iff it is a phi node  */
119         if (! phi_classes->pure_phi_classes || 1) {
120                 DBG((phi_classes->dbg, LEVEL_2, "\tchecking users of %+F:\n", irn));
121                 foreach_out_edge(irn, edge) {
122                         ir_node *user = edge->src;
123                         DBG((phi_classes->dbg, LEVEL_2, "\tchecking user %+F ... ", user));
124                         if (is_Phi(user) && mode_is_datab(get_irn_mode(user))) {
125                                 DB((phi_classes->dbg, LEVEL_2, "is a Phi, descend\n"));
126                                 phi_class_build(phi_classes, user, pc);
127                         }
128                         else {
129                                 DB((phi_classes->dbg, LEVEL_2, "not a Phi, skip\n"));
130                         }
131                 }
132         }
133 }
134
135 /**
136  * Call by a walker. If @p node is Phi, it build the Phi class starting from this node.
137  */
138 static void phi_class_construction_walker(ir_node *node, void *env) {
139         phi_classes_t *pc = env;
140
141         if (is_Phi(node) && mode_is_datab(get_irn_mode(node))) {
142                 ir_node ***irn_pc = _get_phi_class(&pc->ph, node);
143
144                 if (! irn_pc) {
145                         ir_node **pc_values;
146                         phi_class_build(pc, node, NULL);
147
148                         pc_values = *_get_phi_class(&pc->ph, node);
149                         DBG((pc->dbg, LEVEL_1, "inserting phiclass %p (%d members) into all classes\n", pc_values, ARR_LEN(pc_values)));
150
151                         pset_insert_ptr(pc->all_phi_classes, pc_values);
152                 }
153         }
154 }
155
156 /**
157  * Walk over the irg and build the Phi classes.
158  */
159 static void phi_class_compute(phi_classes_t *pc) {
160         irg_walk_graph(pc->irg, phi_class_construction_walker, NULL, pc);
161 }
162
163 /**
164  * Build the Phi classes for the set of given Phis.
165  */
166 static void phi_class_compute_by_phis(phi_classes_t *pc, ir_nodeset_t *all_phi_nodes) {
167         if (ir_nodeset_size(all_phi_nodes) > 0) {
168                 ir_nodeset_iterator_t iter;
169                 ir_node               *phi;
170
171                 foreach_ir_nodeset(all_phi_nodes, phi, iter) {
172                         ir_node ***irn_pc = _get_phi_class(&pc->ph, phi);
173
174                         assert(is_Phi(phi) && mode_is_datab(get_irn_mode(phi)));
175
176                         if (! irn_pc) {
177                                 ir_node **pc_values;
178
179                                 phi_class_build(pc, phi, NULL);
180
181                                 pc_values = *_get_phi_class(&pc->ph, phi);
182                                 DBG((pc->dbg, LEVEL_1, "inserting phiclass %p into all classes\n", pc_values));
183
184                                 pset_insert_ptr(pc->all_phi_classes, pc_values);
185                         }
186                 }
187         }
188 }
189
190 /**
191  * Return the array containing all nodes assigned to the same Phi class as @p irn.
192  */
193 ir_node **get_phi_class(phi_classes_t *pc, ir_node *irn) {
194         return *_get_phi_class(&pc->ph, irn);
195 }
196
197 /**
198  * Assigns a new array of nodes representing the new Phi class to @p irn.
199  */
200 void set_phi_class(phi_classes_t *pc, ir_node *irn, ir_node **cls) {
201         _set_phi_class(&pc->ph, irn, &cls);
202 }
203
204 /**
205  * Returns a set containing all computed Phi classes.
206  */
207 pset *get_all_phi_classes(phi_classes_t *pc) {
208         return pc->all_phi_classes;
209 }
210
211 /**
212  * Builds the Phi classes for all Phis in @p irg.
213  * @return The Phi class object for the @p irg.
214  */
215 phi_classes_t *phi_class_new_from_irg(ir_graph *irg, int pure_phi_classes) {
216         phi_classes_t *res = XMALLOC(phi_classes_t);
217
218         FIRM_DBG_REGISTER(res->dbg, "ir.ana.phiclass");
219         phase_init(&res->ph, "phi_classes", irg, PHASE_DEFAULT_GROWTH, irn_phi_class_init, NULL);
220
221         res->irg              = irg;
222         res->all_phi_classes  = pset_new_ptr(5);
223         res->pure_phi_classes = pure_phi_classes;
224
225         phi_class_compute(res);
226
227         return res;
228 }
229
230 /**
231  * Builds all Phi classes for the given set of Phis.
232  * @return The Phis class object for @p all_phis.
233  */
234 phi_classes_t *phi_class_new_from_set(ir_graph *irg, ir_nodeset_t *all_phis, int pure_phi_classes) {
235         phi_classes_t *res = XMALLOC(phi_classes_t);
236
237         FIRM_DBG_REGISTER(res->dbg, "ir.ana.phiclass");
238         phase_init(&res->ph, "phi_classes", irg, PHASE_DEFAULT_GROWTH, irn_phi_class_init, NULL);
239
240         res->irg              = irg;
241         res->all_phi_classes  = pset_new_ptr(5);
242         res->pure_phi_classes = pure_phi_classes;
243
244         phi_class_compute_by_phis(res, all_phis);
245
246         return res;
247 }
248
249 /**
250  * Free all allocated data.
251  */
252 void phi_class_free(phi_classes_t *pc) {
253         ir_node **ipc;
254         foreach_pset(pc->all_phi_classes, ipc) {
255                 DEL_ARR_F(ipc);
256         }
257         del_pset(pc->all_phi_classes);
258         phase_free(&pc->ph);
259         xfree(pc);
260 }