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