tv: Remove mul_table[][][] and simply use * and <<.
[libfirm] / ir / be / beirgmod.c
1 /*
2  * Copyright (C) 1995-2008 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  *
26  * This file contains the following IRG modifications for be routines:
27  * - insertion of Perm nodes
28  * - empty block elimination
29  * - a simple dead node elimination (set inputs of unreachable nodes to BAD)
30  */
31 #include "config.h"
32
33 #include <stdlib.h>
34
35 #include "hashptr.h"
36 #include "pdeq.h"
37 #include "pset.h"
38 #include "pmap.h"
39 #include "util.h"
40 #include "debug.h"
41 #include "error.h"
42 #include "xmalloc.h"
43
44 #include "irflag_t.h"
45 #include "ircons_t.h"
46 #include "irnode_t.h"
47 #include "ircons_t.h"
48 #include "irmode_t.h"
49 #include "irdom_t.h"
50 #include "iredges_t.h"
51 #include "irgraph_t.h"
52 #include "irgopt.h"
53 #include "irgmod.h"
54 #include "irprintf.h"
55 #include "irgwalk.h"
56
57 #include "be_t.h"
58 #include "bechordal_t.h"
59 #include "bearch.h"
60 #include "besched.h"
61 #include "belive_t.h"
62 #include "benode.h"
63 #include "beutil.h"
64 #include "beinsn_t.h"
65 #include "bessaconstr.h"
66 #include "beirg.h"
67 #include "beirgmod.h"
68 #include "bemodule.h"
69
70 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
71
72 static int cmp_node_nr(const void *a, const void *b)
73 {
74         ir_node **p1 = (ir_node**)a;
75         ir_node **p2 = (ir_node**)b;
76         long      n1 = get_irn_node_nr(*p1);
77         long      n2 = get_irn_node_nr(*p2);
78         return (n1>n2) - (n1<n2);
79 }
80
81 /*
82   ___                     _     ____
83  |_ _|_ __  ___  ___ _ __| |_  |  _ \ ___ _ __ _ __ ___
84   | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ '__| '_ ` _ \
85   | || | | \__ \  __/ |  | |_  |  __/  __/ |  | | | | | |
86  |___|_| |_|___/\___|_|   \__| |_|   \___|_|  |_| |_| |_|
87
88 */
89
90 ir_node *insert_Perm_before(ir_graph *irg, const arch_register_class_t *cls,
91                                                    ir_node *pos)
92 {
93         be_lv_t     *lv = be_get_irg_liveness(irg);
94         ir_nodeset_t live;
95
96         ir_node *perm, **nodes;
97         size_t i, n;
98
99         DBG((dbg, LEVEL_1, "Insert Perm before: %+F\n", pos));
100
101         ir_nodeset_init(&live);
102         be_liveness_nodes_live_before(lv, cls, pos, &live);
103
104         n = ir_nodeset_size(&live);
105         if (n == 0) {
106                 ir_nodeset_destroy(&live);
107                 return NULL;
108         }
109
110         nodes = XMALLOCN(ir_node*, n);
111
112         DBG((dbg, LEVEL_1, "live:\n"));
113         i = 0;
114         foreach_ir_nodeset(&live, irn, iter) {
115                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
116                 nodes[i] = irn;
117                 i++;
118         }
119         ir_nodeset_destroy(&live);
120         /* make the input order deterministic */
121         qsort(nodes, n, sizeof(nodes[0]), cmp_node_nr);
122
123         ir_node *const bl = get_nodes_block(pos);
124         perm = be_new_Perm(cls, bl, n, nodes);
125         sched_add_before(pos, perm);
126         free(nodes);
127
128         for (i = 0; i < n; ++i) {
129                 ir_node *perm_op = get_irn_n(perm, i);
130                 be_ssa_construction_env_t senv;
131
132                 ir_mode *mode = get_irn_mode(perm_op);
133                 ir_node *proj = new_r_Proj(perm, mode, i);
134
135                 be_ssa_construction_init(&senv, irg);
136                 be_ssa_construction_add_copy(&senv, perm_op);
137                 be_ssa_construction_add_copy(&senv, proj);
138                 be_ssa_construction_fix_users(&senv, perm_op);
139                 be_ssa_construction_update_liveness_phis(&senv, lv);
140                 be_liveness_update(lv, perm_op);
141                 be_liveness_update(lv, proj);
142                 be_ssa_construction_destroy(&senv);
143         }
144
145         return perm;
146 }
147
148 static int blocks_removed;
149
150 /**
151  * Post-block-walker: Find blocks containing only one jump and
152  * remove them.
153  */
154 static void remove_empty_block(ir_node *block)
155 {
156         int        i;
157         int        arity;
158         ir_node   *pred;
159         ir_node   *succ_block;
160         ir_node   *jump = NULL;
161         ir_graph  *irg = get_irn_irg(block);
162         ir_entity *entity;
163
164         if (irn_visited_else_mark(block))
165                 return;
166
167         if (get_Block_n_cfgpreds(block) != 1)
168                 goto check_preds;
169
170         sched_foreach(block, node) {
171                 if (! is_Jmp(node)
172                                 && !(arch_get_irn_flags(node) & arch_irn_flags_simple_jump))
173                         goto check_preds;
174                 if (jump != NULL) {
175                         /* we should never have 2 jumps in a block */
176                         panic("found 2 jumps in a block");
177                 }
178                 jump = node;
179         }
180
181         if (jump == NULL)
182                 goto check_preds;
183
184         entity     = get_Block_entity(block);
185         pred       = get_Block_cfgpred(block, 0);
186         succ_block = NULL;
187         foreach_out_edge_safe(jump, edge) {
188                 int pos = get_edge_src_pos(edge);
189
190                 assert(succ_block == NULL);
191                 succ_block = get_edge_src_irn(edge);
192                 if (get_Block_entity(succ_block) != NULL && entity != NULL) {
193                         /*
194                          * Currently we can add only one label for a block.
195                          * Therefore we cannot combine them if  both block already have one.
196                          */
197                         goto check_preds;
198                 }
199
200                 set_irn_n(succ_block, pos, pred);
201         }
202
203         if (entity != NULL) {
204                 /* move the label to the successor block */
205                 set_Block_entity(succ_block, entity);
206         }
207
208         /* there can be some non-scheduled Pin nodes left in the block, move them
209          * to the succ block (Pin) or pred block (Sync) */
210         foreach_out_edge_safe(block, edge) {
211                 ir_node *const node = get_edge_src_irn(edge);
212
213                 if (node == jump)
214                         continue;
215                 /* we simply kill Pins, because there are some strange interactions
216                  * between jump threading, which produce PhiMs with Pins, we simply
217                  * kill the pins here, everything is scheduled anyway */
218                 if (is_Pin(node)) {
219                         exchange(node, get_Pin_op(node));
220                         continue;
221                 }
222                 if (is_Sync(node)) {
223                         set_nodes_block(node, get_nodes_block(pred));
224                         continue;
225                 }
226                 if (is_End(node)) { /* End-keep, reroute it to the successor */
227                         int pos = get_edge_src_pos(edge);
228                         set_irn_n(node, pos, succ_block);
229                         continue;
230                 }
231                 panic("Unexpected node %+F in block %+F with empty schedule", node, block);
232         }
233
234         set_Block_cfgpred(block, 0, new_r_Bad(irg, mode_X));
235         kill_node(jump);
236         blocks_removed = 1;
237
238         /* check predecessor */
239         remove_empty_block(get_nodes_block(pred));
240         return;
241
242 check_preds:
243         arity = get_Block_n_cfgpreds(block);
244         for (i = 0; i < arity; ++i) {
245                 ir_node *pred = get_Block_cfgpred_block(block, i);
246                 remove_empty_block(pred);
247         }
248 }
249
250 /* removes basic blocks that just contain a jump instruction */
251 int be_remove_empty_blocks(ir_graph *irg)
252 {
253         ir_node *end;
254         int      i, arity;
255
256         blocks_removed = 0;
257
258         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
259         inc_irg_visited(irg);
260         remove_empty_block(get_irg_end_block(irg));
261         end   = get_irg_end(irg);
262         arity = get_irn_arity(end);
263         for (i = 0; i < arity; ++i) {
264                 ir_node *pred = get_irn_n(end, i);
265                 if (!is_Block(pred))
266                         continue;
267                 remove_empty_block(pred);
268         }
269         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
270
271         if (blocks_removed) {
272                 /* invalidate analysis info */
273                 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
274         }
275         return blocks_removed;
276 }
277
278 //---------------------------------------------------------------------------
279
280 typedef struct remove_dead_nodes_env_t_ {
281         bitset_t *reachable;
282         ir_graph *irg;
283         be_lv_t  *lv;
284 } remove_dead_nodes_env_t;
285
286 /**
287  * Post-walker: remember all visited nodes in a bitset.
288  */
289 static void mark_dead_nodes_walker(ir_node *node, void *data)
290 {
291         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
292         bitset_set(env->reachable, get_irn_idx(node));
293 }
294
295 /**
296  * Post-block-walker:
297  * Walk through the schedule of every block and remove all dead nodes from it.
298  */
299 static void remove_dead_nodes_walker(ir_node *block, void *data)
300 {
301         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
302         ir_node                 *node, *next;
303
304         for (node = sched_first(block); ! sched_is_end(node); node = next) {
305                 /* get next node now, as after calling sched_remove it will be invalid */
306                 next = sched_next(node);
307
308                 if (bitset_is_set(env->reachable, get_irn_idx(node)))
309                         continue;
310
311                 if (env->lv != NULL)
312                         be_liveness_remove(env->lv, node);
313                 sched_remove(node);
314
315                 /* kill projs */
316                 if (get_irn_mode(node) == mode_T) {
317                         foreach_out_edge_safe(node, edge) {
318                                 ir_node *proj = get_edge_src_irn(edge);
319                                 if (!is_Proj(proj))
320                                         continue;
321                                 if (env->lv != NULL)
322                                         be_liveness_remove(env->lv, proj);
323                                 kill_node(proj);
324                         }
325                 }
326                 kill_node(node);
327         }
328 }
329
330 void be_remove_dead_nodes_from_schedule(ir_graph *irg)
331 {
332         remove_dead_nodes_env_t env;
333         env.reachable = bitset_alloca(get_irg_last_idx(irg));
334         env.lv        = be_get_irg_liveness(irg);
335         env.irg       = irg;
336
337         // mark all reachable nodes
338         irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);
339
340         // walk schedule and remove non-marked nodes
341         irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);
342 }
343
344 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod)
345 void be_init_irgmod(void)
346 {
347         FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");
348 }