4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
7 * Performs SSA-Destruction.
14 #include "iredges_t.h"
16 #include "bechordal_t.h"
19 #include "besched_t.h"
21 #define get_reg(irn) arch_get_irn_register(chordal_env->arch_env, irn, 0)
22 #define set_reg(irn, reg) arch_set_irn_register(chordal_env->arch_env, irn, 0, reg)
25 * Maps blocks to perm nodes inserted during phi destruction.
27 typedef struct _block2perm_t {
28 ir_node *block, *perm;
31 static int set_cmp_b2p(const void *x, const void *y, size_t size) {
32 const block2perm_t *b1 = x;
33 const block2perm_t *b2 = y;
34 return b1->block != b2->block;
38 * Adjusts the register allocation for the phi-operands
39 * by inserting perm nodes, if necessary.
40 * @param phi The phi node to adjust operands for
42 static void adjust_arguments(be_main_session_env_t *session, be_chordal_env_t *chordal_env, const ir_node *phi) {
44 ir_node *arg, *perm, *proj;
45 const arch_register_t *phi_reg, *arg_reg, *proj_reg;
46 const ir_edge_t *edge;
48 block2perm_t find, *found;
50 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
52 b2p = chordal_env->data;
53 phi_reg = get_reg(phi);
54 /* all arguments of the phi */
55 for(i=0, max=get_irn_arity(phi); i<max; ++i) {
56 arg = get_irn_n(phi, i);
57 arg_reg = get_reg(arg);
58 /* if registers don't match ...*/
59 if (phi_reg != arg_reg) {
60 /* iff needed insert perm node */
61 find.block = get_nodes_block(arg);
63 found = set_insert(b2p, &find, sizeof(find), HASH_PTR(find.block));
65 found->perm = insert_Perm_after(session, chordal_env->cls, sched_last(find.block));
67 /* now we have the perm in the predecessor block */
69 /* adjust assigned registers for the projs */
70 foreach_out_edge(perm, edge) {
71 proj = get_edge_src_irn(edge);
72 proj_reg = get_reg(proj);
73 if (proj_reg == arg_reg)
74 set_reg(proj, phi_reg);
75 else if (proj_reg == phi_reg)
76 set_reg(proj, arg_reg);
82 static void checker(be_chordal_env_t *chordal_env) {
86 /* iterate over all blocks */
87 pmap_foreach(chordal_env->border_heads, pme) {
89 struct list_head *head = pme->value;
91 /* iterate over all ops in the block */
92 list_for_each_entry_reverse(border_t, curr, head, list)
93 if (curr->is_def && curr->is_real && is_Phi(curr->irn)) {
94 const arch_register_t *phi_reg, *arg_reg;
95 phi_reg = get_reg(curr->irn);
96 /* iterate over all args of phi */
97 for(i=0, max=get_irn_arity(curr->irn); i<max; ++i) {
98 arg_reg = get_reg(get_irn_n(curr->irn, i));
99 assert(phi_reg == arg_reg && "WTF? You can do it better!?");
105 void be_ssa_destruction(be_main_session_env_t *session, be_chordal_env_t *chordal_env) {
109 b2p = new_set(set_cmp_b2p, 32);
110 chordal_env->data = b2p;
111 /* iterate over all blocks */
112 pmap_foreach(chordal_env->border_heads, pme) {
114 struct list_head *head = pme->value;
116 /* iterate over all ops in the block
117 * BETTER: phis are the first ops, so stop iteration on first non-phi-op */
118 list_for_each_entry_reverse(border_t, curr, head, list)
119 if (curr->is_def && curr->is_real && is_Phi(curr->irn))
120 adjust_arguments(session, chordal_env, curr->irn);
123 checker(chordal_env);