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