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