- Split bearch.h correctly into bearch.h and bearch_t.h
[libfirm] / ir / be / beirgmod.c
1 /**
2  * @file
3  * @brief
4  * This file contains the following IRG modifications for be routines:
5  * - insertion of Perm nodes
6  * - empty block elimination
7  * - a simple dead node elimination (set inputs of unreachable nodes to BAD)
8  *
9  * @author      Sebastian Hack, Daniel Grund, Matthias Braun, Christian Wuerdig
10  * @date        04.05.2005
11  * @version     $Id$
12  * Copyright:   (c) Universitaet Karlsruhe
13  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
14  */
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18
19 #include <stdlib.h>
20
21 #include "hashptr.h"
22 #include "pdeq.h"
23 #include "pset.h"
24 #include "pmap.h"
25 #include "util.h"
26 #include "debug.h"
27 #include "error.h"
28 #include "xmalloc.h"
29
30 #include "irflag_t.h"
31 #include "ircons_t.h"
32 #include "irnode_t.h"
33 #include "ircons_t.h"
34 #include "irmode_t.h"
35 #include "irdom_t.h"
36 #include "iredges_t.h"
37 #include "irgraph_t.h"
38 #include "irgopt.h"
39 #include "irprintf_t.h"
40 #include "irgwalk.h"
41
42 #include "be_t.h"
43 #include "bechordal_t.h"
44 #include "bearch_t.h"
45 #include "besched_t.h"
46 #include "belive_t.h"
47 #include "benode_t.h"
48 #include "beutil.h"
49 #include "beinsn_t.h"
50 #include "bessaconstr.h"
51
52 #include "beirgmod.h"
53
54 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
55
56 /*
57   ___                     _     ____
58  |_ _|_ __  ___  ___ _ __| |_  |  _ \ ___ _ __ _ __ ___
59   | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ '__| '_ ` _ \
60   | || | | \__ \  __/ |  | |_  |  __/  __/ |  | | | | | |
61  |___|_| |_|___/\___|_|   \__| |_|   \___|_|  |_| |_| |_|
62
63 */
64
65 ir_node *insert_Perm_after(be_irg_t *birg,
66                                                    const arch_register_class_t *cls,
67                                                    ir_node *pos)
68 {
69         const arch_env_t *arch_env = birg->main_env->arch_env;
70         be_lv_t *lv     = birg->lv;
71         ir_node *bl     = is_Block(pos) ? pos : get_nodes_block(pos);
72         ir_graph *irg   = get_irn_irg(bl);
73         pset *live      = pset_new_ptr_default();
74
75         ir_node *curr, *irn, *perm, **nodes;
76         int i, n;
77
78         DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
79
80         be_liveness_nodes_live_at(lv, arch_env, cls, pos, live);
81
82         n = pset_count(live);
83
84         if(n == 0) {
85                 del_pset(live);
86                 return NULL;
87         }
88
89         nodes = xmalloc(n * sizeof(nodes[0]));
90
91         DBG((dbg, LEVEL_1, "live:\n"));
92         for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
93                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
94                 nodes[i] = irn;
95         }
96         del_pset(live);
97
98         perm = be_new_Perm(cls, irg, bl, n, nodes);
99         sched_add_after(pos, perm);
100         free(nodes);
101
102         curr = perm;
103         for (i = 0; i < n; ++i) {
104                 ir_node *perm_op = get_irn_n(perm, i);
105                 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
106                 be_ssa_construction_env_t senv;
107
108                 ir_mode *mode = get_irn_mode(perm_op);
109                 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
110                 arch_set_irn_register(arch_env, proj, reg);
111
112                 sched_add_after(curr, proj);
113                 curr = proj;
114
115                 be_ssa_construction_init(&senv, birg);
116                 be_ssa_construction_add_copy(&senv, perm_op);
117                 be_ssa_construction_add_copy(&senv, proj);
118                 be_ssa_construction_fix_users(&senv, perm_op);
119                 be_ssa_construction_update_liveness_phis(&senv, lv);
120                 be_liveness_update(lv, perm_op);
121                 be_liveness_update(lv, proj);
122                 be_ssa_construction_destroy(&senv);
123         }
124
125         return perm;
126 }
127
128 /**
129  * Post-block-walker: Find blocks containing only one jump and
130  * remove them.
131  */
132 static void remove_empty_block(ir_node *block, void *data) {
133         const ir_edge_t *edge, *next;
134         ir_node *node;
135         int *changed = data;
136         ir_node *jump = NULL;
137
138         assert(is_Block(block));
139
140         if (get_Block_n_cfgpreds(block) != 1)
141                 return;
142
143         sched_foreach(block, node) {
144                 if (! is_Jmp(node))
145                         return;
146                 if (jump != NULL) {
147                         /* we should never have 2 jumps in a block */
148                         panic("We should never have 2 jumps in a block");
149                 }
150                 jump = node;
151         }
152
153         if (jump == NULL)
154                 return;
155
156         node = get_Block_cfgpred(block, 0);
157         foreach_out_edge_safe(jump, edge, next) {
158                 ir_node *block = get_edge_src_irn(edge);
159                 int     pos    = get_edge_src_pos(edge);
160
161                 set_irn_n(block, pos, node);
162         }
163
164         set_Block_cfgpred(block, 0, new_Bad());
165         be_kill_node(jump);
166         *changed = 1;
167 }
168
169 /* removes basic blocks that just contain a jump instruction */
170 int be_remove_empty_blocks(ir_graph *irg) {
171         int changed = 0;
172
173         irg_block_walk_graph(irg, remove_empty_block, NULL, &changed);
174         if (changed) {
175                 /* invalidate analysis info */
176                 set_irg_doms_inconsistent(irg);
177                 set_irg_extblk_inconsistent(irg);
178                 set_irg_outs_inconsistent(irg);
179         }
180         return changed;
181 }
182
183 void be_init_irgmod(void)
184 {
185         FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");
186 }
187
188 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod);