15 #include "bephicongr_t.h"
19 size_t phi_irn_data_offset = 0;
20 static firm_dbg_module_t *dbgphi = NULL;
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
27 #define is_Const(n) (get_irn_opcode(n) == iro_Const)
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
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);
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
49 static void phi_class_union(ir_node *n, ir_node *new_tgt) {
53 assert(is_Phi(n) && is_Phi(new_tgt) && "These must be phi nodes.");
54 DBG((dbgphi, 1, "\tcorrect %n\n", n));
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);
62 /* phi class of n is no longer needed */
64 _set_phi_class(n, NULL);
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
73 static void det_phi_congr_class(ir_node *curr_phi) {
76 assert(is_Phi(curr_phi) && "This must be a phi node.");
77 DBG((dbgphi, 1, "Det. phi class of %n.\n", curr_phi));
79 pc = _get_phi_class(curr_phi);
82 _set_phi_class(curr_phi, pc);
83 phi_class_insert(pc, curr_phi, curr_phi);
86 for (i = 0, n = get_irn_arity(curr_phi); i < n; i++) {
89 arg = get_irn_n(curr_phi, i);
90 DBG((dbgphi, 1, " Arg %n\n", 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);
100 DBG((dbgphi, 1, "Size now: %d\n", pset_count(_get_phi_class(curr_phi))));
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);
110 void be_phi_congr_compute(ir_graph *irg) {
111 irg_walk_graph(irg, phi_node_walker, NULL, NULL);
115 pset *get_phi_class(const ir_node *irn) {
116 ir_node *phi = _get_phi(irn);
118 return _get_phi_class(phi);
123 pset *be_phi_congr_classes(pset *all_phi_nodes) {
126 pset *phi_class, *all_phi_classes;
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);
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);
137 pset_insert_ptr(all_phi_classes, phi_class);
140 return all_phi_classes;
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));