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