16 #include "iredges_t.h"
17 #include "phiclass_t.h"
19 #define DEBUG_LVL SET_LEVEL_0
20 static firm_dbg_module_t *dbg = NULL;
22 size_t phi_irn_data_offset = 0;
24 static void phi_class_build(ir_node *irn, pset *pc) {
26 const ir_edge_t *edge;
28 /* If irn has a phi class assigned already
29 * return immediately to stop recursion */
30 if (_get_phi_class(irn)) {
31 DBG((dbg, LEVEL_2, " already done for %+F\n", irn));
35 /* The initial call to phi_class_build doesn't
36 * provide a pset, so alloc it */
38 DBG((dbg, LEVEL_1, "Computing phi class for %+F\n", irn));
43 /* Add the irn to the phi class */
44 DBG((dbg, LEVEL_1, " adding %+F\n", irn));
45 pset_insert_ptr(pc, irn);
46 _set_phi_class(irn, pc);
48 /* Check the 'neighbour' irns */
49 if (is_Phi(irn) && mode_is_datab(get_irn_mode(irn))) {
50 /* Add all args of the phi to the phi-class. */
51 for (i=0, max=get_irn_arity(irn); i<max; ++i) {
52 DBG((dbg, LEVEL_2, " checking arg %+F\n", get_irn_n(irn, i)));
53 phi_class_build(get_irn_n(irn, i), pc);
57 /* Add a user of the irn to the class,
58 * iff it is a phi node */
59 foreach_out_edge(irn, edge) {
60 ir_node *user = edge->src;
61 DBG((dbg, LEVEL_2, " checking user %+F\n", user));
62 if (is_Phi(user) && mode_is_datab(get_irn_mode(user)))
63 phi_class_build(user, pc);
67 static void phi_class_construction_walker(ir_node *node, void *env) {
68 if (is_Phi(node) && mode_is_datab(get_irn_mode(node)))
69 phi_class_build(node, NULL);
72 static void phi_class_destruction_walker(ir_node *node, void *env) {
73 pset *clss = _get_phi_class(node);
76 for(n = pset_first(clss); n; n = pset_next(clss))
77 _set_phi_class(n, NULL);
82 void phi_class_compute(ir_graph *irg) {
83 irg_walk_graph(irg, phi_class_destruction_walker, NULL, NULL);
84 irg_walk_graph(irg, phi_class_construction_walker, NULL, NULL);
87 pset *phi_class_compute_by_phis(pset *all_phi_nodes) {
90 pset *all_phi_classes = pset_new_ptr_default();
92 if (pset_count(all_phi_nodes)) {
93 ir_graph *irg = get_irn_irg(pset_first(all_phi_nodes));
94 pset_break(all_phi_nodes);
95 irg_walk_graph(irg, phi_class_destruction_walker, NULL, NULL);
97 for (i = 0, phi=pset_first(all_phi_nodes); phi; phi=pset_next(all_phi_nodes)) {
98 assert(is_Phi(phi) && mode_is_datab(get_irn_mode(phi)));
99 phi_class_build(phi, NULL);
100 pset_insert_ptr(all_phi_classes, _get_phi_class(phi));
104 return all_phi_classes;
107 void phi_class_free(ir_graph *irg) {
108 irg_walk_graph(irg, phi_class_destruction_walker, NULL, NULL);
111 pset *get_phi_class(const ir_node *irn) {
112 return get_irn_phi_info(irn)->phi_class;
115 void phi_class_init(void) {
116 dbg = firm_dbg_register("ir.ana.phiclass");
117 firm_dbg_set_mask(dbg, DEBUG_LVL);
118 phi_irn_data_offset = register_additional_node_data(sizeof(phi_info_t));