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