make compiler more deterministic by sorting Perm inputs
[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_t.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_after(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_node *bl     = is_Block(pos) ? pos : get_nodes_block(pos);
95         ir_nodeset_t          live;
96         ir_nodeset_iterator_t iter;
97
98         ir_node *irn, *perm, **nodes;
99         size_t i, n;
100
101         DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
102
103         ir_nodeset_init(&live);
104         be_liveness_nodes_live_at(lv, cls, pos, &live);
105
106         n = ir_nodeset_size(&live);
107         if (n == 0) {
108                 ir_nodeset_destroy(&live);
109                 return NULL;
110         }
111
112         nodes = XMALLOCN(ir_node*, n);
113
114         DBG((dbg, LEVEL_1, "live:\n"));
115         i = 0;
116         foreach_ir_nodeset(&live, irn, iter) {
117                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
118                 nodes[i] = irn;
119                 i++;
120         }
121         ir_nodeset_destroy(&live);
122         /* make the input order deterministic */
123         qsort(nodes, n, sizeof(nodes[0]), cmp_node_nr);
124
125         perm = be_new_Perm(cls, bl, n, nodes);
126         sched_add_after(pos, perm);
127         free(nodes);
128
129         for (i = 0; i < n; ++i) {
130                 ir_node *perm_op = get_irn_n(perm, i);
131                 const arch_register_t *reg = arch_get_irn_register(perm_op);
132                 be_ssa_construction_env_t senv;
133
134                 ir_mode *mode = get_irn_mode(perm_op);
135                 ir_node *proj = new_r_Proj(perm, mode, i);
136                 arch_set_irn_register(proj, reg);
137
138                 be_ssa_construction_init(&senv, irg);
139                 be_ssa_construction_add_copy(&senv, perm_op);
140                 be_ssa_construction_add_copy(&senv, proj);
141                 be_ssa_construction_fix_users(&senv, perm_op);
142                 be_ssa_construction_update_liveness_phis(&senv, lv);
143                 be_liveness_update(lv, perm_op);
144                 be_liveness_update(lv, proj);
145                 be_ssa_construction_destroy(&senv);
146         }
147
148         return perm;
149 }
150
151 static int blocks_removed;
152
153 /**
154  * Post-block-walker: Find blocks containing only one jump and
155  * remove them.
156  */
157 static void remove_empty_block(ir_node *block)
158 {
159         const ir_edge_t *edge;
160         const ir_edge_t *next;
161         int              i;
162         int              arity;
163         ir_node         *node;
164         ir_node         *pred;
165         ir_node         *succ_block;
166         ir_node         *jump = NULL;
167         ir_graph        *irg = get_irn_irg(block);
168         ir_entity       *entity;
169
170         if (irn_visited_else_mark(block))
171                 return;
172
173         if (get_Block_n_cfgpreds(block) != 1)
174                 goto check_preds;
175
176         sched_foreach(block, node) {
177                 if (! is_Jmp(node)
178                                 && !(arch_get_irn_flags(node) & arch_irn_flags_simple_jump))
179                         goto check_preds;
180                 if (jump != NULL) {
181                         /* we should never have 2 jumps in a block */
182                         panic("found 2 jumps in a block");
183                 }
184                 jump = node;
185         }
186
187         if (jump == NULL)
188                 goto check_preds;
189
190         entity     = get_Block_entity(block);
191         pred       = get_Block_cfgpred(block, 0);
192         succ_block = NULL;
193         foreach_out_edge_safe(jump, edge, next) {
194                 int pos = get_edge_src_pos(edge);
195
196                 assert(succ_block == NULL);
197                 succ_block = get_edge_src_irn(edge);
198                 if (get_Block_entity(succ_block) != NULL && entity != NULL) {
199                         /*
200                          * Currently we can add only one label for a block.
201                          * Therefore we cannot combine them if  both block already have one.
202                          */
203                         goto check_preds;
204                 }
205
206                 set_irn_n(succ_block, pos, pred);
207         }
208
209         if (entity != NULL) {
210                 /* move the label to the successor block */
211                 set_Block_entity(succ_block, entity);
212         }
213
214         /* there can be some non-scheduled Pin nodes left in the block, move them
215          * to the succ block (Pin) or pred block (Sync) */
216         foreach_out_edge_safe(block, edge, next) {
217                 node = get_edge_src_irn(edge);
218
219                 if (node == jump)
220                         continue;
221                 /* we simply kill Pins, because there are some strange interactions
222                  * between jump threading, which produce PhiMs with Pins, we simply
223                  * kill the pins here, everything is scheduled anyway */
224                 if (is_Pin(node)) {
225                         exchange(node, get_Pin_op(node));
226                         continue;
227                 }
228                 if (is_Sync(node)) {
229                         set_nodes_block(node, get_nodes_block(pred));
230                         continue;
231                 }
232                 if (is_End(node)) { /* End-keep, reroute it to the successor */
233                         int pos = get_edge_src_pos(edge);
234                         set_irn_n(node, pos, succ_block);
235                         continue;
236                 }
237                 panic("Unexpected node %+F in block %+F with empty schedule", node, block);
238         }
239
240         set_Block_cfgpred(block, 0, new_r_Bad(irg, mode_X));
241         kill_node(jump);
242         blocks_removed = 1;
243
244         /* check predecessor */
245         remove_empty_block(get_nodes_block(pred));
246         return;
247
248 check_preds:
249         arity = get_Block_n_cfgpreds(block);
250         for (i = 0; i < arity; ++i) {
251                 ir_node *pred = get_Block_cfgpred_block(block, i);
252                 remove_empty_block(pred);
253         }
254 }
255
256 /* removes basic blocks that just contain a jump instruction */
257 int be_remove_empty_blocks(ir_graph *irg)
258 {
259         ir_node *end;
260         int      i, arity;
261
262         blocks_removed = 0;
263
264         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
265         inc_irg_visited(irg);
266         remove_empty_block(get_irg_end_block(irg));
267         end   = get_irg_end(irg);
268         arity = get_irn_arity(end);
269         for (i = 0; i < arity; ++i) {
270                 ir_node *pred = get_irn_n(end, i);
271                 if (!is_Block(pred))
272                         continue;
273                 remove_empty_block(pred);
274         }
275         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
276
277         if (blocks_removed) {
278                 /* invalidate analysis info */
279                 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
280         }
281         return blocks_removed;
282 }
283
284 //---------------------------------------------------------------------------
285
286 typedef struct remove_dead_nodes_env_t_ {
287         bitset_t *reachable;
288         ir_graph *irg;
289         be_lv_t  *lv;
290 } remove_dead_nodes_env_t;
291
292 /**
293  * Post-walker: remember all visited nodes in a bitset.
294  */
295 static void mark_dead_nodes_walker(ir_node *node, void *data)
296 {
297         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
298         bitset_set(env->reachable, get_irn_idx(node));
299 }
300
301 /**
302  * Post-block-walker:
303  * Walk through the schedule of every block and remove all dead nodes from it.
304  */
305 static void remove_dead_nodes_walker(ir_node *block, void *data)
306 {
307         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
308         ir_node                 *node, *next;
309
310         for (node = sched_first(block); ! sched_is_end(node); node = next) {
311                 /* get next node now, as after calling sched_remove it will be invalid */
312                 next = sched_next(node);
313
314                 if (bitset_is_set(env->reachable, get_irn_idx(node)))
315                         continue;
316
317                 if (env->lv != NULL)
318                         be_liveness_remove(env->lv, node);
319                 sched_remove(node);
320
321                 /* kill projs */
322                 if (get_irn_mode(node) == mode_T) {
323                         const ir_edge_t *edge;
324                         const ir_edge_t *next_edge;
325                         foreach_out_edge_safe(node, edge, next_edge) {
326                                 ir_node *proj = get_edge_src_irn(edge);
327                                 if (!is_Proj(proj))
328                                         continue;
329                                 if (env->lv != NULL)
330                                         be_liveness_remove(env->lv, proj);
331                                 kill_node(proj);
332                         }
333                 }
334                 kill_node(node);
335         }
336 }
337
338 void be_remove_dead_nodes_from_schedule(ir_graph *irg)
339 {
340         remove_dead_nodes_env_t env;
341         env.reachable = bitset_alloca(get_irg_last_idx(irg));
342         env.lv        = be_get_irg_liveness(irg);
343         env.irg       = irg;
344
345         // mark all reachable nodes
346         irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);
347
348         // walk schedule and remove non-marked nodes
349         irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);
350 }
351
352 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_irgmod)
353 void be_init_irgmod(void)
354 {
355         FIRM_DBG_REGISTER(dbg, "firm.be.irgmod");
356 }