2 * @author Daniel Grund, Christian Wuerdig
18 #include "iredges_t.h"
19 #include "phiclass_t.h"
20 #include "irphase_t.h"
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);
29 typedef struct _irn_phi_class_t {
30 ir_node **phi_cls; /* the array of node pointers representing the class */
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);
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);
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]));
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.
57 static void phi_class_build(phi_classes_t *phi_classes, ir_node *irn, ir_node **pc) {
58 const ir_edge_t *edge;
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));
67 /* The initial call to phi_class_build doesn't
68 * provide a nodeset, so alloc it */
70 DBG((phi_classes->dbg, LEVEL_1, "Computing phi class for %+F\n", irn));
72 pc = NEW_ARR_F(ir_node *, 0);
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);
80 /* Check the 'neighbour' irns */
81 if (is_Phi(irn) && mode_is_datab(get_irn_mode(irn))) {
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);
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);
103 * Call by a walker. If @p node is Phi, it build the Phi class starting from this node.
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));
114 * Walk over the irg and build the Phi classes.
116 static void phi_class_compute(phi_classes_t *pc) {
117 irg_walk_graph(pc->irg, phi_class_construction_walker, NULL, pc);
121 * Build the Phi classes for the set of given Phis.
123 static void phi_class_compute_by_phis(phi_classes_t *pc, pset *all_phi_nodes) {
124 if (pset_count(all_phi_nodes)) {
127 foreach_pset(all_phi_nodes, phi) {
128 ir_node **irn_pc = _get_phi_class(&pc->ph, phi);
130 assert(is_Phi(phi) && mode_is_datab(get_irn_mode(phi)));
133 phi_class_build(pc, phi, NULL);
134 pset_insert_ptr(pc->all_phi_classes, _get_phi_class(&pc->ph, phi));
141 * Return the array containing all nodes assigned to the same Phi class as @p irn.
143 ir_node **get_phi_class(phi_classes_t *pc, const ir_node *irn) {
144 return _get_phi_class(&pc->ph, irn);
148 * Assigns a new array of nodes representing the new Phi class to @p irn.
150 void set_phi_class(phi_classes_t *pc, ir_node *irn, ir_node **cls) {
151 _set_phi_class(&pc->ph, irn, cls);
155 * Returns a set containing all computed Phi classes.
157 pset *get_all_phi_classes(phi_classes_t *pc) {
158 return pc->all_phi_classes;
162 * Builds the Phi classes for all Phis in @p irg.
163 * @return The Phi class object for the @p irg.
165 phi_classes_t *phi_class_new_from_irg(ir_graph *irg) {
166 phi_classes_t *res = xmalloc(sizeof(*res));
168 FIRM_DBG_REGISTER(res->dbg, "ir.ana.phiclass");
169 phase_init(&res->ph, "phi_classes", irg, PHASE_DEFAULT_GROWTH, irn_phi_class_init);
172 res->all_phi_classes = pset_new_ptr(5);
174 phi_class_compute(res);
180 * Builds all Phi classes for the given set of Phis.
181 * @return The Phis class object for @p all_phis.
183 phi_classes_t *phi_class_new_from_set(ir_graph *irg, pset *all_phis) {
184 phi_classes_t *res = xmalloc(sizeof(*res));
186 FIRM_DBG_REGISTER(res->dbg, "ir.ana.phiclass");
187 phase_init(&res->ph, "phi_classes", irg, PHASE_DEFAULT_GROWTH, irn_phi_class_init);
190 res->all_phi_classes = pset_new_ptr(5);
192 phi_class_compute_by_phis(res, all_phis);
198 * Free all allocated data.
200 void phi_class_free(phi_classes_t *pc) {
202 foreach_pset(pc->all_phi_classes, ipc) {
205 del_pset(pc->all_phi_classes);