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