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