Added prototype for phi_ops_interfere
[libfirm] / ir / be / bephicongr.c
1 /**
2  * @author Daniel Grund
3  * @date 09.12.2004
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include "debug.h"
11 #include "impl.h"
12 #include "irprog.h"
13 #include "irgwalk.h"
14 #include "irop.h"
15 #include "bephicongr_t.h"
16
17 #define DEBUG_LVL 0
18
19 size_t phi_irn_data_offset = 0;
20 static firm_dbg_module_t *dbgphi = NULL;
21
22 #define _get_phi(irn)            get_irn_phi_info(irn)->phi
23 #define _set_phi(irn, p)         get_irn_phi_info(irn)->phi = p
24 #define _get_phi_class(irn)             get_irn_phi_info(irn)->phi_class
25 #define _set_phi_class(irn, cls) get_irn_phi_info(irn)->phi_class = cls
26
27 #define is_Const(n)       (get_irn_opcode(n) == iro_Const)
28
29
30 /**
31  * Insert a node to the phi class of a phi node
32  * @param class The phi congruence class
33  * @param phi The phi node holding the class
34  * @param arg Node which gets assigned to the class
35  */
36 static INLINE void phi_class_insert(pset *class, ir_node *phi, ir_node *member) {
37         DBG((dbgphi, 1, "\tinsert %n in %n\n", member, phi));
38         if (!(is_Const(member) && CONSTS_SPLIT_PHI_CLASSES))
39                 _set_phi(member, phi);
40         pset_insert_ptr(class, member);
41 }
42
43
44 /**
45  * Unites two phi classes repesented by two phi nodes.
46  * @param n Phi node representing phi class to reassign
47  * @param new_tgt Phi node, which will hold the new bigger phi class
48  */
49 static void phi_class_union(ir_node *n, ir_node *new_tgt) {
50         ir_node *p;
51         pset *src, *tgt;
52
53     assert(is_Phi(n) && is_Phi(new_tgt) && "These must be phi nodes.");
54         DBG((dbgphi, 1, "\tcorrect %n\n", n));
55
56         /* copy all class members from n to new_tgt. Duplicates eliminated by pset */
57         src = _get_phi_class(n);
58         tgt = _get_phi_class(new_tgt);
59         for (p = (ir_node *)pset_first(src); p; p = (ir_node *)pset_next(src))
60                 phi_class_insert(tgt, new_tgt, p);
61
62         /* phi class of n is no longer needed */
63         del_pset(src);
64         _set_phi_class(n, NULL);
65 }
66
67
68 /**
69  * Determines the phi congruence class of a phi node.
70  * This will assign a phi class to all operands of this
71  * phi node. Exceptions may be const nodes: See CONSTS_SPLIT_PHI_CLASSES
72  */
73 static void det_phi_congr_class(ir_node *curr_phi) {
74         pset *pc;
75     int i, n;
76     assert(is_Phi(curr_phi) && "This must be a phi node.");
77     DBG((dbgphi, 1, "Det. phi class of %n.\n", curr_phi));
78
79         pc = _get_phi_class(curr_phi);
80         if (!pc) {
81                 pc = pset_new_ptr(2);
82                 _set_phi_class(curr_phi, pc);
83                 phi_class_insert(pc, curr_phi, curr_phi);
84         }
85
86         for (i = 0, n = get_irn_arity(curr_phi); i < n; i++) {
87                 ir_node *arg, *phi;
88
89                 arg = get_irn_n(curr_phi, i);
90                 DBG((dbgphi, 1, "    Arg %n\n", arg));
91
92                 phi = _get_phi(arg);
93                 if (phi == NULL) { /* Argument is not assigned to another phi class. */
94                         phi_class_insert(pc, curr_phi, arg);
95                 } else if (phi != curr_phi) {
96                         assert(!(is_Const(arg) && CONSTS_SPLIT_PHI_CLASSES) && "Const nodes must not have a phi class assigned. See CONSTS_SPLIT_PHI_CLASSES");
97                         phi_class_union(phi, curr_phi);
98                 }
99         }
100         DBG((dbgphi, 1, "Size now: %d\n", pset_count(_get_phi_class(curr_phi))));
101 }
102
103
104 static void phi_node_walker(ir_node *node, void *env) {
105         if (is_Phi(node) && mode_is_datab(get_irn_mode(node)))
106                 det_phi_congr_class(node);
107 }
108
109
110 void be_phi_congr_compute(ir_graph *irg) {
111         irg_walk_graph(irg, phi_node_walker, NULL, NULL);
112 }
113
114
115 pset *get_phi_class(const ir_node *irn) {
116         ir_node *phi = _get_phi(irn);
117         if (phi)
118                 return _get_phi_class(phi);
119         else
120                 return NULL;
121 }
122
123 pset *be_phi_congr_classes(pset *all_phi_nodes) {
124         int i;
125         ir_node *phi;
126         pset *phi_class, *all_phi_classes;
127
128         /* determine all phi classes */
129         for (i = 0, phi = (ir_node *)pset_first(all_phi_nodes); phi; phi = (ir_node *)pset_next(all_phi_nodes))
130                 det_phi_congr_class(phi);
131
132         /* store them in a pset for fast retrieval */
133         all_phi_classes = pset_new_ptr(64);
134         for (i = 0, phi = (ir_node *)pset_first(all_phi_nodes); phi; phi = (ir_node *)pset_next(all_phi_nodes)) {
135                 phi_class = _get_phi_class(phi);
136                 if (phi_class)
137                         pset_insert_ptr(all_phi_classes, phi_class);
138         }
139
140         return all_phi_classes;
141 }
142
143
144 void be_phi_congr_class_init(void) {
145         dbgphi = firm_dbg_register("Phi classes");
146         firm_dbg_set_mask(dbgphi, DEBUG_LVL);
147         phi_irn_data_offset = register_additional_node_data(sizeof(phi_info_t));
148 }