14 #include "phiclass_t.h"
18 size_t phi_irn_data_offset = 0;
19 static firm_dbg_module_t *dbgphi = NULL;
21 #define _get_phi(irn) get_irn_phi_info(irn)->phi
22 #define _set_phi(irn,p) get_irn_phi_info(irn)->phi = p
23 #define _get_phi_class(irn) get_irn_phi_info(irn)->phi_class
24 #define _set_phi_class(irn,cls) get_irn_phi_info(irn)->phi_class = cls
26 #define is_Const(n) (get_irn_opcode(n) == iro_Const)
30 * Insert a node to the phi class of a phi node
31 * @param class The phi congruence class
32 * @param phi The phi node holding the class
33 * @param arg Node which gets assigned to the class
35 static INLINE void phi_class_insert(pset *class, ir_node *phi, ir_node *member) {
36 DBG((dbgphi, 1, "\tinsert %n in %n\n", member, phi));
37 if (!(is_Const(member) && CONSTS_SPLIT_PHI_CLASSES))
38 _set_phi(member, phi);
39 pset_insert_ptr(class, member);
43 * Unites two 'phi classes' represented by two phi nodes.
44 * @param n Phi node representing phi class to reassign
45 * @param new_tgt Phi node, which will hold the new bigger phi class
47 static void phi_class_union(ir_node *n, ir_node *new_tgt) {
51 assert(is_Phi(n) && is_Phi(new_tgt) && "These must be phi nodes.");
52 DBG((dbgphi, 1, "\tcorrect %n\n", n));
54 /* copy all class members from n to new_tgt. Duplicates eliminated by pset */
55 src = _get_phi_class(n);
56 tgt = _get_phi_class(new_tgt);
57 for (p = (ir_node *)pset_first(src); p; p = (ir_node *)pset_next(src))
58 phi_class_insert(tgt, new_tgt, p);
60 /* phi class of n is no longer needed */
62 _set_phi_class(n, NULL);
66 * Determines the phi congruence class of a phi node.
67 * This will assign a phi class to all operands of this
68 * phi node. Exceptions may be const nodes: See CONSTS_SPLIT_PHI_CLASSES
70 static void phi_class_det(ir_node *curr_phi) {
73 assert(is_Phi(curr_phi) && "This must be a phi node.");
74 DBG((dbgphi, 1, "Det. phi class of %n.\n", curr_phi));
76 pc = _get_phi_class(curr_phi);
79 _set_phi_class(curr_phi, pc);
80 phi_class_insert(pc, curr_phi, curr_phi);
83 for (i = 0, n = get_irn_arity(curr_phi); i < n; i++) {
86 arg = get_irn_n(curr_phi, i);
87 DBG((dbgphi, 1, " Arg %n\n", arg));
90 if (phi == NULL) { /* Argument is not assigned to another phi class. */
91 phi_class_insert(pc, curr_phi, arg);
92 } else if (phi != curr_phi) {
93 assert(!(is_Const(arg) && CONSTS_SPLIT_PHI_CLASSES) && "Const nodes must not have a phi class assigned. See CONSTS_SPLIT_PHI_CLASSES");
94 phi_class_union(phi, curr_phi);
97 DBG((dbgphi, 1, "Size now: %d\n", pset_count(_get_phi_class(curr_phi))));
100 pset *phi_class_compute_by_phis(pset *all_phi_nodes) {
103 pset *phi_class, *all_phi_classes;
105 /* determine all phi classes */
106 for (i = 0, phi = (ir_node *)pset_first(all_phi_nodes); phi; phi = (ir_node *)pset_next(all_phi_nodes))
109 /* store them in a pset for fast retrieval */
110 all_phi_classes = pset_new_ptr(64);
111 for (i = 0, phi = (ir_node *)pset_first(all_phi_nodes); phi; phi = (ir_node *)pset_next(all_phi_nodes)) {
112 phi_class = _get_phi_class(phi);
114 pset_insert_ptr(all_phi_classes, phi_class);
117 return all_phi_classes;
120 static void phi_class_construction_walker(ir_node *node, void *env) {
121 if (is_Phi(node) && mode_is_datab(get_irn_mode(node)))
125 void phi_class_compute(ir_graph *irg) {
126 irg_walk_graph(irg, phi_class_construction_walker, NULL, NULL);
129 static void phi_class_destruction_walker(ir_node *node, void *env) {
130 if (is_Phi(node) && mode_is_datab(get_irn_mode(node))) {
131 pset *clss = _get_phi_class(node);
139 void phi_class_free(ir_graph *irg) {
140 irg_walk_graph(irg, phi_class_destruction_walker, NULL, NULL);
143 pset *get_phi_class(const ir_node *irn) {
145 if (phi = _get_phi(irn), phi)
146 return _get_phi_class(phi);
151 void phi_class_init(void) {
152 dbgphi = firm_dbg_register("ir.ana.phiclass");
153 firm_dbg_set_mask(dbgphi, DEBUG_LVL);
154 phi_irn_data_offset = register_additional_node_data(sizeof(phi_info_t));