Add an irg attribute the Bad nodes: so get_Block_irg() can operate on
[libfirm] / ir / opt / ifconv.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    ir/opt/ifconv.c
22  * @brief   If conversion
23  * @author  Christoph Mallon
24  * @version $Id$
25  */
26
27 #include "config.h"
28
29 #include <assert.h>
30 #include "iroptimize.h"
31 #include "obst.h"
32 #include "irnode_t.h"
33 #include "cdep.h"
34 #include "ircons.h"
35 #include "irgmod.h"
36 #include "irgopt.h"
37 #include "irgwalk.h"
38 #include "irtools.h"
39 #include "array_t.h"
40
41 // debug
42 #include "irdump.h"
43 #include "debug.h"
44
45 /**
46  * Environment for if-conversion.
47  */
48 typedef struct walker_env {
49         const ir_settings_if_conv_t *params; /**< Conversion parameter. */
50         int                         changed; /**< Set if the graph was changed. */
51 } walker_env;
52
53 DEBUG_ONLY(static firm_dbg_module_t *dbg);
54
55 /**
56  * Default callback for Mux creation: allows every Mux to be created.
57  */
58 static int default_allow_ifconv(ir_node *sel, ir_node* phi_list, int i, int j)
59 {
60         (void) sel;
61         (void) phi_list;
62         (void) i;
63         (void) j;
64         return 1;
65 }
66
67 /**
68  * Default options.
69  */
70 static const ir_settings_if_conv_t default_info = {
71         0,    /* doesn't matter for Mux */
72         default_allow_ifconv
73 };
74
75 /**
76  * Returns non-zero if a Block can be emptied.
77  *
78  * @param block  the block
79  */
80 static int can_empty_block(ir_node *block) {
81         return get_Block_mark(block) == 0;
82 }
83
84 /**
85  * Find the ProjX node leading from block dependency to block start.
86  *
87  * @param start       a block that is control depended on dependency
88  * @param dependency  the block that decides whether start is executed
89  *
90  * @return a ProjX node that represent the decision control flow or
91  *         NULL is start is not dependent at all or a block on the way
92  *         cannot be emptied
93  */
94 static ir_node* walk_to_projx(ir_node* start, const ir_node* dependency)
95 {
96         int arity;
97         int i;
98
99         /* No need to find the conditional block if this block cannot be emptied and
100          * therefore not moved */
101         if (!can_empty_block(start)) return NULL;
102
103         arity = get_irn_arity(start);
104         for (i = 0; i < arity; ++i) {
105                 ir_node* pred = get_irn_n(start, i);
106                 ir_node* pred_block = get_nodes_block(skip_Proj(pred));
107
108                 if (pred_block == dependency) {
109                         if (is_Proj(pred)) {
110                                 assert(get_irn_mode(pred) == mode_X);
111                                 /* we found it */
112                                 return pred;
113                         }
114                         /* Not a Proj? Should not happen. */
115                         return NULL;
116                 }
117
118                 if (is_Proj(pred)) {
119                         assert(get_irn_mode(pred) == mode_X);
120                         /* another Proj but not from the control block */
121                         return NULL;
122                 }
123
124                 if (is_cdep_on(pred_block, dependency)) {
125                         return walk_to_projx(pred_block, dependency);
126                 }
127         }
128         return NULL;
129 }
130
131
132 /**
133  * Recursively copies the DAG starting at node to the i-th predecessor
134  * block of src_block
135  * - if node isn't in the src_block, recursion ends and node is returned
136  * - if node is a Phi in the src_block, the i-th predecessor of this Phi is
137  *   returned and recursion ends
138  * otherwise returns a copy of the passed node created in the i-th predecessor of
139  * src_block.
140  *
141  * @param node       a root of a DAG
142  * @param src_block  the block of the DAG
143  * @param i          the position of the predecessor the DAG
144  *                   is moved to
145  *
146  * @return  the root of the copied DAG
147  */
148 static ir_node* copy_to(ir_node* node, ir_node* src_block, int i)
149 {
150         ir_node* dst_block;
151         ir_node* copy;
152         int j;
153
154         if (get_nodes_block(node) != src_block) {
155                 /* already outside src_block, do not copy */
156                 return node;
157         }
158         if (is_Phi(node)) {
159                 /* move through the Phi to the i-th predecessor */
160                 return get_irn_n(node, i);
161         }
162
163         /* else really need a copy */
164         copy = exact_copy(node);
165         dst_block = get_nodes_block(get_irn_n(src_block, i));
166         set_nodes_block(copy, dst_block);
167
168         DB((dbg, LEVEL_1, "Copying node %+F to block %+F, copy is %+F\n",
169                 node, dst_block, copy));
170
171         /* move recursively all predecessors */
172         for (j = get_irn_arity(node) - 1; j >= 0; --j) {
173                 set_irn_n(copy, j, copy_to(get_irn_n(node, j), src_block, i));
174                 DB((dbg, LEVEL_2, "-- pred %d is %+F\n", j, get_irn_n(copy, j)));
175         }
176         return copy;
177 }
178
179
180 /**
181  * Remove predecessors i and j (i < j) from a node and
182  * add an additional predecessor new_pred.
183  *
184  * @param node      the node whose inputs are changed
185  * @param i         the first index to remove
186  * @param j         the second index to remove
187  * @param new_pred  a node that is added as a new input to node
188  */
189 static void rewire(ir_node* node, int i, int j, ir_node* new_pred)
190 {
191         int arity = get_irn_arity(node);
192         ir_node **ins;
193         int k;
194         int l;
195
196         NEW_ARR_A(ir_node *, ins, arity - 1);
197
198         l = 0;
199         for (k = 0; k < i;     ++k) ins[l++] = get_irn_n(node, k);
200         for (++k;   k < j;     ++k) ins[l++] = get_irn_n(node, k);
201         for (++k;   k < arity; ++k) ins[l++] = get_irn_n(node, k);
202         ins[l++] = new_pred;
203         assert(l == arity - 1);
204         set_irn_in(node, l, ins);
205 }
206
207
208 /**
209  * Remove the j-th predecessors from the i-th predecessor of block and add it to block
210  */
211 static void split_block(ir_node* block, int i, int j)
212 {
213         ir_node* pred_block = get_nodes_block(get_irn_n(block, i));
214         int arity = get_irn_arity(block);
215         int new_pred_arity;
216         ir_node *phi, *next;
217         ir_node **ins;
218         ir_node **pred_ins;
219         int k;
220
221         DB((dbg, LEVEL_1, "Splitting predecessor %d of predecessor %d of %+F\n", j, i, block));
222
223         NEW_ARR_A(ir_node*, ins, arity + 1);
224
225         for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
226                 ir_node* copy = copy_to(get_irn_n(phi, i), pred_block, j);
227
228                 for (k = 0; k < i; ++k) ins[k] = get_irn_n(phi, k);
229                 ins[k++] = copy;
230                 for (; k < arity; ++k) ins[k] = get_irn_n(phi, k);
231                 ins[k] = get_irn_n(phi, i);
232                 assert(k == arity);
233                 set_irn_in(phi, arity + 1, ins);
234         }
235
236         for (k = 0; k < i; ++k) ins[k] = get_irn_n(block, k);
237         ins[k++] = get_irn_n(pred_block, j);
238         for (; k < arity; ++k) ins[k] = get_irn_n(block, k);
239         ins[k] = get_irn_n(block, i);
240         assert(k == arity);
241         set_irn_in(block, arity + 1, ins);
242
243         new_pred_arity = get_irn_arity(pred_block) - 1;
244         NEW_ARR_A(ir_node*, pred_ins, new_pred_arity);
245
246         for (phi = get_Block_phis(pred_block); phi != NULL; phi = next) {
247                 for (k = 0; k < j; ++k) pred_ins[k] = get_irn_n(phi, k);
248                 for (; k < new_pred_arity; ++k) pred_ins[k] = get_irn_n(phi, k + 1);
249                 assert(k == new_pred_arity);
250                 next = get_Phi_next(phi);
251                 if (new_pred_arity > 1) {
252                         set_irn_in(phi, new_pred_arity, pred_ins);
253                 } else {
254                         exchange(phi, pred_ins[0]);
255                 }
256         }
257
258         for (k = 0; k < j; ++k) pred_ins[k] = get_irn_n(pred_block, k);
259         for (; k < new_pred_arity; ++k) pred_ins[k] = get_irn_n(pred_block, k + 1);
260         assert(k == new_pred_arity);
261         if (new_pred_arity > 1) {
262                 set_irn_in(pred_block, new_pred_arity, pred_ins);
263         } else {
264                 exchange(pred_block, get_nodes_block(pred_ins[0]));
265         }
266 }
267
268
269 static void prepare_path(ir_node* block, int i, const ir_node* dependency)
270 {
271         ir_node* pred = get_nodes_block(get_irn_n(block, i));
272         int pred_arity;
273         int j;
274
275         DB((dbg, LEVEL_1, "Preparing predecessor %d of %+F\n", i, block));
276
277         pred_arity = get_irn_arity(pred);
278         for (j = 0; j < pred_arity; ++j) {
279                 ir_node* pred_pred = get_nodes_block(get_irn_n(pred, j));
280
281                 if (is_cdep_on(pred_pred, dependency)) {
282                         prepare_path(pred, j, dependency);
283                         split_block(block, i, j);
284                         break;
285                 }
286         }
287 }
288
289 /**
290  * Block walker: Search for diamonds and do the if conversion.
291  */
292 static void if_conv_walker(ir_node *block, void *ctx)
293 {
294         walker_env *env = ctx;
295         int arity;
296         int i;
297
298         /* Bail out, if there are no Phis at all */
299         if (get_Block_phis(block) == NULL) return;
300
301 restart:
302         arity = get_irn_arity(block);
303         for (i = 0; i < arity; ++i) {
304                 ir_node* pred0;
305                 ir_cdep* cdep;
306
307                 pred0 = get_Block_cfgpred_block(block, i);
308                 for (cdep = find_cdep(pred0); cdep != NULL; cdep = cdep->next) {
309                         const ir_node* dependency = cdep->node;
310                         ir_node* projx0 = walk_to_projx(pred0, dependency);
311                         ir_node* cond;
312                         int j;
313
314                         if (projx0 == NULL) continue;
315
316                         cond = get_Proj_pred(projx0);
317                         if (! is_Cond(cond))
318                                 continue;
319
320                         /* We only handle boolean decisions, no switches */
321                         if (get_irn_mode(get_Cond_selector(cond)) != mode_b) continue;
322
323                         for (j = i + 1; j < arity; ++j) {
324                                 ir_node* projx1;
325                                 ir_node* sel;
326                                 ir_node* mux_block;
327                                 ir_node* phi;
328                                 ir_node* pred1;
329                                 dbg_info* cond_dbg;
330
331                                 pred1 = get_Block_cfgpred_block(block, j);
332
333                                 if (!is_cdep_on(pred1, dependency)) continue;
334
335                                 projx1 = walk_to_projx(pred1, dependency);
336
337                                 if (projx1 == NULL) continue;
338
339                                 phi = get_Block_phis(block);
340                                 if (!env->params->allow_ifconv(get_Cond_selector(cond), phi, i, j))
341                                         continue;
342
343                                 DB((dbg, LEVEL_1, "Found Cond %+F with proj %+F and %+F\n",
344                                         cond, projx0, projx1
345                                 ));
346
347                                 env->changed = 1;
348                                 prepare_path(block, i, dependency);
349                                 prepare_path(block, j, dependency);
350                                 arity = get_irn_arity(block);
351
352                                 sel = get_Cond_selector(cond);
353
354                                 mux_block = get_nodes_block(cond);
355                                 cond_dbg = get_irn_dbg_info(cond);
356                                 do {
357                                         ir_node* val_i = get_irn_n(phi, i);
358                                         ir_node* val_j = get_irn_n(phi, j);
359                                         ir_node* mux;
360                                         ir_node* next_phi;
361
362                                         if (val_i == val_j) {
363                                                 mux = val_i;
364                                                 DB((dbg, LEVEL_2,  "Generating no Mux, because both values are equal\n"));
365                                         } else {
366                                                 ir_node *t, *f;
367
368                                                 /* Something is very fishy if two predecessors of a PhiM point into
369                                                  * one block, but not at the same memory node
370                                                  */
371                                                 assert(get_irn_mode(phi) != mode_M);
372                                                 if (get_Proj_proj(projx0) == pn_Cond_true) {
373                                                         t = val_i;
374                                                         f = val_j;
375                                                 } else {
376                                                         t = val_j;
377                                                         f = val_i;
378                                                 }
379
380                                                 mux = new_rd_Mux(cond_dbg, current_ir_graph, mux_block, sel, f, t, get_irn_mode(phi));
381                                                 DB((dbg, LEVEL_2, "Generating %+F for %+F\n", mux, phi));
382                                         }
383
384                                         next_phi = get_Phi_next(phi);
385
386                                         if (arity == 2) {
387                                                 exchange(phi, mux);
388                                         } else {
389                                                 rewire(phi, i, j, mux);
390                                         }
391                                         phi = next_phi;
392                                 } while (phi != NULL);
393
394                                 exchange(get_nodes_block(get_irn_n(block, i)), mux_block);
395                                 exchange(get_nodes_block(get_irn_n(block, j)), mux_block);
396
397                                 if (arity == 2) {
398                                         unsigned mark;
399 #if 1
400                                         DB((dbg, LEVEL_1,  "Welding block %+F and %+F\n", block, mux_block));
401                                         /* copy the block-info from the Mux-block to the block before merging */
402
403                                         mark =  get_Block_mark(mux_block) | get_Block_mark(block);
404                                         set_Block_mark(block, mark);
405                                         set_Block_phis(block, get_Block_phis(mux_block));
406
407                                         set_irn_in(block, get_irn_arity(mux_block), get_irn_in(mux_block) + 1);
408                                         exchange_cdep(mux_block, block);
409                                         exchange(mux_block, block);
410 #else
411                                         DB((dbg, LEVEL_1,  "Welding block %+F to %+F\n", block, mux_block));
412                                         mark =  get_Block_mark(mux_block) | get_Block_mark(block);
413                                         /* mark both block just to be sure, should be enough to mark mux_block */
414                                         set_Block_mark(mux_block, mark);
415                                         exchange(block, mux_block);
416 #endif
417                                         return;
418                                 } else {
419                                         rewire(block, i, j, new_r_Jmp(current_ir_graph, mux_block));
420                                         goto restart;
421                                 }
422                         }
423                 }
424         }
425 }
426
427 /**
428  * Block walker: clear block marks and Phi lists.
429  */
430 static void init_block_link(ir_node *block, void *env)
431 {
432         (void)env;
433         set_Block_mark(block, 0);
434         set_Block_phis(block, NULL);
435 }
436
437
438 /**
439  * Daisy-chain all Phis in a block.
440  * If a non-movable node is encountered set the has_pinned flag in its block.
441  */
442 static void collect_phis(ir_node *node, void *env) {
443         (void) env;
444
445         if (is_Phi(node)) {
446                 ir_node *block = get_nodes_block(node);
447
448                 add_Block_phi(block, node);
449         } else {
450                 if (is_no_Block(node) && get_irn_pinned(node) == op_pin_state_pinned) {
451                         /*
452                          * Ignore control flow nodes (except Raise), these will be removed.
453                          */
454                         if (!is_cfop(node) && !is_Raise(node)) {
455                                 ir_node *block = get_nodes_block(node);
456
457                                 DB((dbg, LEVEL_2, "Node %+F in block %+F is unmovable\n", node, block));
458                                 set_Block_mark(block, 1);
459                         }
460                 }
461         }
462 }
463
464 void opt_if_conv(ir_graph *irg, const ir_settings_if_conv_t *params)
465 {
466         walker_env env;
467
468         /* get the parameters */
469         env.params  = (params != NULL ? params : &default_info);
470         env.changed = 0;
471
472         FIRM_DBG_REGISTER(dbg, "firm.opt.ifconv");
473
474         DB((dbg, LEVEL_1, "Running if-conversion on %+F\n", irg));
475
476         normalize_one_return(irg);
477         remove_critical_cf_edges(irg);
478
479         compute_cdep(irg);
480
481         ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
482
483         irg_block_walk_graph(irg, init_block_link, NULL, NULL);
484         irg_walk_graph(irg, collect_phis, NULL, NULL);
485         irg_block_walk_graph(irg, NULL, if_conv_walker, &env);
486
487         ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
488
489         if (env.changed) {
490                 local_optimize_graph(irg);
491
492                 /* graph has changed, invalidate analysis info */
493                 set_irg_outs_inconsistent(irg);
494                 set_irg_extblk_inconsistent(irg);
495                 set_irg_loopinfo_inconsistent(irg);
496                 set_irg_doms_inconsistent(irg);
497         }
498
499         free_cdep(irg);
500 }