2 * @author Daniel Grund, Christian Wuerdig
18 #include "iredges_t.h"
20 #include "irphase_t.h"
21 #include "irnodeset.h"
23 struct _phi_classes_t {
24 ir_phase ph; /* The phase object holding the irn data */
25 pset *all_phi_classes; /* A set containing all Phi classes */
26 ir_graph *irg; /* The irg this is all about */
27 unsigned pure_phi_classes; /* Build pure Phi classes */
28 DEBUG_ONLY(firm_dbg_module_t *dbg);
31 typedef struct _irn_phi_class_t {
32 ir_node ***phi_cls; /* the array of node pointers representing the class */
35 static INLINE ir_node ***_get_phi_class(ir_phase *ph, ir_node *irn) {
36 irn_phi_class_t *ipc = phase_get_or_set_irn_data(ph, irn);
40 static INLINE void _set_phi_class(ir_phase *ph, ir_node *irn, ir_node ***cls) {
41 irn_phi_class_t *ipc = phase_get_or_set_irn_data(ph, irn);
45 /* initialize data structure for given irn in given phase */
46 static void *irn_phi_class_init(ir_phase *ph, ir_node *irn, void *data) {
47 irn_phi_class_t *ipc = data ? data : phase_alloc(ph, sizeof(ipc[0]));
48 memset(ipc, 0, sizeof(ipc[0]));
53 * Builds the phi class, starting from.
54 * @param phi_classes The phi class object
55 * @param irn The to start from
56 * @param pc The phi class this irn should be put into.
57 * Set to NULL if you want a new one.
59 static void phi_class_build(phi_classes_t *phi_classes, ir_node *irn, ir_node ***pc) {
60 const ir_edge_t *edge;
62 assert((! phi_classes->pure_phi_classes || is_Phi(irn)) && "Node must be Phi when pure_phi_classes set.");
64 /* If irn has a phi class assigned already
65 * return immediately to stop recursion */
66 if (_get_phi_class(&phi_classes->ph, irn)) {
67 DBG((phi_classes->dbg, LEVEL_2, "\talready done for %+F\n", irn));
71 /* The initial call to phi_class_build doesn't
72 * provide a nodeset, so alloc it */
74 DBG((phi_classes->dbg, LEVEL_1, "Computing phi class for %+F:\n", irn));
76 pc = phase_alloc(&phi_classes->ph, sizeof(*pc));
77 *pc = NEW_ARR_F(ir_node *, 0);
78 DBG((phi_classes->dbg, LEVEL_2, "\tcreated class %p in container %p\n", *pc, pc));
81 /* Add the irn to the phi class */
82 DBG((phi_classes->dbg, LEVEL_1, "\t\tadding %+F to class %p, container %p\n", irn, *pc, pc));
83 ARR_APP1(ir_node *, *pc, irn);
84 _set_phi_class(&phi_classes->ph, irn, pc);
86 /* Check the 'neighbour' irns */
87 if (is_Phi(irn) && mode_is_datab(get_irn_mode(irn))) {
89 DBG((phi_classes->dbg, LEVEL_2, "\tchecking args of %+F:\n", irn));
90 /* Add all args of the phi to the phi-class. */
91 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
92 ir_node *op = get_irn_n(irn, i);
93 DBG((phi_classes->dbg, LEVEL_2, "\tchecking arg %+F\n", op));
94 if (! phi_classes->pure_phi_classes || is_Phi(op))
95 phi_class_build(phi_classes, op, pc);
99 /* Add a user of the irn to the class,
100 * iff it is a phi node */
101 if (! phi_classes->pure_phi_classes || 1) {
102 DBG((phi_classes->dbg, LEVEL_2, "\tchecking users of %+F:\n", irn));
103 foreach_out_edge(irn, edge) {
104 ir_node *user = edge->src;
105 DBG((phi_classes->dbg, LEVEL_2, "\tchecking user %+F ... ", user));
106 if (is_Phi(user) && mode_is_datab(get_irn_mode(user))) {
107 DB((phi_classes->dbg, LEVEL_2, "is a Phi, descend\n"));
108 phi_class_build(phi_classes, user, pc);
111 DB((phi_classes->dbg, LEVEL_2, "not a Phi, skip\n"));
118 * Call by a walker. If @p node is Phi, it build the Phi class starting from this node.
120 static void phi_class_construction_walker(ir_node *node, void *env) {
121 phi_classes_t *pc = env;
123 if (is_Phi(node) && mode_is_datab(get_irn_mode(node))) {
124 ir_node ***irn_pc = _get_phi_class(&pc->ph, node);
128 phi_class_build(pc, node, NULL);
130 pc_values = *_get_phi_class(&pc->ph, node);
131 DBG((pc->dbg, LEVEL_1, "inserting phiclass %p (%d members) into all classes\n", pc_values, ARR_LEN(pc_values)));
133 pset_insert_ptr(pc->all_phi_classes, pc_values);
139 * Walk over the irg and build the Phi classes.
141 static void phi_class_compute(phi_classes_t *pc) {
142 irg_walk_graph(pc->irg, phi_class_construction_walker, NULL, pc);
146 * Build the Phi classes for the set of given Phis.
148 static void phi_class_compute_by_phis(phi_classes_t *pc, ir_nodeset_t *all_phi_nodes) {
149 if (ir_nodeset_size(all_phi_nodes) > 0) {
150 ir_nodeset_iterator_t iter;
153 foreach_ir_nodeset(all_phi_nodes, phi, iter) {
154 ir_node ***irn_pc = _get_phi_class(&pc->ph, phi);
156 assert(is_Phi(phi) && mode_is_datab(get_irn_mode(phi)));
161 phi_class_build(pc, phi, NULL);
163 pc_values = *_get_phi_class(&pc->ph, phi);
164 DBG((pc->dbg, LEVEL_1, "inserting phiclass %p into all classes\n", pc_values));
166 pset_insert_ptr(pc->all_phi_classes, pc_values);
173 * Return the array containing all nodes assigned to the same Phi class as @p irn.
175 ir_node **get_phi_class(phi_classes_t *pc, ir_node *irn) {
176 return *_get_phi_class(&pc->ph, irn);
180 * Assigns a new array of nodes representing the new Phi class to @p irn.
182 void set_phi_class(phi_classes_t *pc, ir_node *irn, ir_node **cls) {
183 _set_phi_class(&pc->ph, irn, &cls);
187 * Returns a set containing all computed Phi classes.
189 pset *get_all_phi_classes(phi_classes_t *pc) {
190 return pc->all_phi_classes;
194 * Builds the Phi classes for all Phis in @p irg.
195 * @return The Phi class object for the @p irg.
197 phi_classes_t *phi_class_new_from_irg(ir_graph *irg, int pure_phi_classes) {
198 phi_classes_t *res = xmalloc(sizeof(*res));
200 FIRM_DBG_REGISTER(res->dbg, "ir.ana.phiclass");
201 phase_init(&res->ph, "phi_classes", irg, PHASE_DEFAULT_GROWTH, irn_phi_class_init);
204 res->all_phi_classes = pset_new_ptr(5);
205 res->pure_phi_classes = pure_phi_classes;
207 phi_class_compute(res);
213 * Builds all Phi classes for the given set of Phis.
214 * @return The Phis class object for @p all_phis.
216 phi_classes_t *phi_class_new_from_set(ir_graph *irg, ir_nodeset_t *all_phis, int pure_phi_classes) {
217 phi_classes_t *res = xmalloc(sizeof(*res));
219 FIRM_DBG_REGISTER(res->dbg, "ir.ana.phiclass");
220 phase_init(&res->ph, "phi_classes", irg, PHASE_DEFAULT_GROWTH, irn_phi_class_init);
223 res->all_phi_classes = pset_new_ptr(5);
224 res->pure_phi_classes = pure_phi_classes;
226 phi_class_compute_by_phis(res, all_phis);
232 * Free all allocated data.
234 void phi_class_free(phi_classes_t *pc) {
236 foreach_pset(pc->all_phi_classes, ipc) {
239 del_pset(pc->all_phi_classes);