2 * @author Daniel Grund, Christian Wuerdig
18 #include "iredges_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 unsigned pure_phi_classes; /* Build pure Phi classes */
27 DEBUG_ONLY(firm_dbg_module_t *dbg);
30 typedef struct _irn_phi_class_t {
31 ir_node ***phi_cls; /* the array of node pointers representing the class */
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);
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);
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]));
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.
58 static void phi_class_build(phi_classes_t *phi_classes, ir_node *irn, ir_node ***pc) {
59 const ir_edge_t *edge;
61 assert((! phi_classes->pure_phi_classes || is_Phi(irn)) && "Node must be Phi when pure_phi_classes set.");
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));
70 /* The initial call to phi_class_build doesn't
71 * provide a nodeset, so alloc it */
73 DBG((phi_classes->dbg, LEVEL_1, "Computing phi class for %+F:\n", 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));
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);
85 /* Check the 'neighbour' irns */
86 if (is_Phi(irn) && mode_is_datab(get_irn_mode(irn))) {
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);
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);
110 DB((phi_classes->dbg, LEVEL_2, "not a Phi, skip\n"));
117 * Call by a walker. If @p node is Phi, it build the Phi class starting from this node.
119 static void phi_class_construction_walker(ir_node *node, void *env) {
120 phi_classes_t *pc = env;
122 if (is_Phi(node) && mode_is_datab(get_irn_mode(node))) {
123 ir_node ***irn_pc = _get_phi_class(&pc->ph, node);
127 phi_class_build(pc, node, NULL);
129 pc_values = *_get_phi_class(&pc->ph, node);
130 DBG((pc->dbg, LEVEL_1, "inserting phiclass %p into all classes\n", pc_values));
132 pset_insert_ptr(pc->all_phi_classes, pc_values);
138 * Walk over the irg and build the Phi classes.
140 static void phi_class_compute(phi_classes_t *pc) {
141 irg_walk_graph(pc->irg, phi_class_construction_walker, NULL, pc);
145 * Build the Phi classes for the set of given Phis.
147 static void phi_class_compute_by_phis(phi_classes_t *pc, pset *all_phi_nodes) {
148 if (pset_count(all_phi_nodes)) {
151 foreach_pset(all_phi_nodes, phi) {
152 ir_node ***irn_pc = _get_phi_class(&pc->ph, phi);
154 assert(is_Phi(phi) && mode_is_datab(get_irn_mode(phi)));
159 phi_class_build(pc, phi, NULL);
161 pc_values = *_get_phi_class(&pc->ph, phi);
162 DBG((pc->dbg, LEVEL_1, "inserting phiclass %p into all classes\n", pc_values));
164 pset_insert_ptr(pc->all_phi_classes, pc_values);
171 * Return the array containing all nodes assigned to the same Phi class as @p irn.
173 ir_node **get_phi_class(phi_classes_t *pc, ir_node *irn) {
174 return *_get_phi_class(&pc->ph, irn);
178 * Assigns a new array of nodes representing the new Phi class to @p irn.
180 void set_phi_class(phi_classes_t *pc, ir_node *irn, ir_node **cls) {
181 _set_phi_class(&pc->ph, irn, &cls);
185 * Returns a set containing all computed Phi classes.
187 pset *get_all_phi_classes(phi_classes_t *pc) {
188 return pc->all_phi_classes;
192 * Builds the Phi classes for all Phis in @p irg.
193 * @return The Phi class object for the @p irg.
195 phi_classes_t *phi_class_new_from_irg(ir_graph *irg, int pure_phi_classes) {
196 phi_classes_t *res = xmalloc(sizeof(*res));
198 FIRM_DBG_REGISTER(res->dbg, "ir.ana.phiclass");
199 phase_init(&res->ph, "phi_classes", irg, PHASE_DEFAULT_GROWTH, irn_phi_class_init);
202 res->all_phi_classes = pset_new_ptr(5);
203 res->pure_phi_classes = pure_phi_classes;
205 phi_class_compute(res);
211 * Builds all Phi classes for the given set of Phis.
212 * @return The Phis class object for @p all_phis.
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));
217 FIRM_DBG_REGISTER(res->dbg, "ir.ana.phiclass");
218 phase_init(&res->ph, "phi_classes", irg, PHASE_DEFAULT_GROWTH, irn_phi_class_init);
221 res->all_phi_classes = pset_new_ptr(5);
222 res->pure_phi_classes = pure_phi_classes;
224 phi_class_compute_by_phis(res, all_phis);
230 * Free all allocated data.
232 void phi_class_free(phi_classes_t *pc) {
234 foreach_pset(pc->all_phi_classes, ipc) {
237 del_pset(pc->all_phi_classes);