reorder for loops
[libfirm] / ir / ana / phiclass.c
1 /**
2  * @author Daniel Grund
3  * @date 09.08.2005
4  */
5 #ifdef HAVE_CONFIG_H
6 # include "config.h"
7 #endif
8
9 #ifdef HAVE_STDLIB_H
10 # include <stdlib.h>
11 #endif
12
13 #include "debug.h"
14 #include "irgwalk.h"
15 #include "irop_t.h"
16 #include "iredges_t.h"
17 #include "phiclass_t.h"
18
19 #define DEBUG_LVL SET_LEVEL_0
20 static firm_dbg_module_t *dbg = NULL;
21
22 size_t phi_irn_data_offset = 0;
23
24 static void phi_class_build(ir_node *irn, pset *pc) {
25         int i, max;
26         const ir_edge_t *edge;
27
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));
32                 return;
33         }
34
35         /* The initial call to phi_class_build doesn't
36          * provide a pset, so alloc it */
37         if (!pc) {
38                 DBG((dbg, LEVEL_1, "Computing phi class for %+F\n", irn));
39                 assert(is_Phi(irn));
40                 pc = pset_new_ptr(4);
41         }
42
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);
47
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);
54                  }
55         }
56
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);
64         }
65 }
66
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);
70 }
71
72 static void phi_class_destruction_walker(ir_node *node, void *env) {
73         pset *clss = _get_phi_class(node);
74         if (clss) {
75                 ir_node *n;
76                 for(n = pset_first(clss); n; n = pset_next(clss))
77                         _set_phi_class(n, NULL);
78                 del_pset(clss);
79         }
80 }
81
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);
85 }
86
87 pset *phi_class_compute_by_phis(pset *all_phi_nodes) {
88         int i;
89         ir_node *phi;
90         pset *all_phi_classes = pset_new_ptr_default();
91
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);
96
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));
101                 }
102         }
103
104         return all_phi_classes;
105 }
106
107 void phi_class_free(ir_graph *irg) {
108         irg_walk_graph(irg, phi_class_destruction_walker, NULL, NULL);
109 }
110
111 pset *get_phi_class(const ir_node *irn) {
112         return get_irn_phi_info(irn)->phi_class;
113 }
114
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));
119 }