Good day and welcome to the FIRM XMALLOC*() macros. These macros are provided for...
[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 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <assert.h>
32 #include "iroptimize.h"
33 #include "obst.h"
34 #include "irnode_t.h"
35 #include "cdep.h"
36 #include "ircons.h"
37 #include "irdom.h"
38 #include "irgmod.h"
39 #include "irgopt.h"
40 #include "irgwalk.h"
41 #include "irtools.h"
42 #include "array_t.h"
43
44 // debug
45 #include "irdump.h"
46 #include "debug.h"
47
48 DEBUG_ONLY(static firm_dbg_module_t *dbg);
49
50 /** allow every Mux to be created. */
51 static int default_allow_ifconv(ir_node *sel, ir_node* phi_list, int i, int j)
52 {
53         (void) sel;
54         (void) phi_list;
55         (void) i;
56         (void) j;
57         return 1;
58 }
59
60 /**
61  * Default options.
62  */
63 static const ir_settings_if_conv_t default_info = {
64         0,    /* doesn't matter for Mux */
65         default_allow_ifconv
66 };
67
68 /**
69  * Returns non-zero if a Block can be emptied.
70  */
71 static int can_empty_block(ir_node *block) {
72         return get_Block_mark(block) == 0;
73 }
74
75
76 static ir_node* walk_to_projx(ir_node* start, const ir_node* dependency)
77 {
78         int arity;
79         int i;
80
81         /* No need to find the conditional block if this block cannot be emptied and
82          * therefore not moved */
83         if (!can_empty_block(start)) return NULL;
84
85         arity = get_irn_arity(start);
86         for (i = 0; i < arity; ++i) {
87                 ir_node* pred = get_irn_n(start, i);
88                 ir_node* pred_block = get_nodes_block(pred);
89
90                 if (pred_block == dependency) {
91                         if (is_Proj(pred)) {
92                                 assert(get_irn_mode(pred) == mode_X);
93                                 return pred;
94                         }
95                         return NULL;
96                 }
97
98                 if (is_Proj(pred)) {
99                         assert(get_irn_mode(pred) == mode_X);
100                         return NULL;
101                 }
102
103                 if (is_cdep_on(pred_block, dependency)) {
104                         return walk_to_projx(pred_block, dependency);
105                 }
106         }
107         return NULL;
108 }
109
110
111 /**
112  * Copies the DAG starting at node to the ith predecessor block of src_block
113  * -if the node isn't in the src_block, this is a nop and the node is returned
114  * -if the node is a phi in the src_block, the ith predecessor of the phi is
115  *   returned
116  * otherwise returns the copy of the passed node
117  */
118 static ir_node* copy_to(ir_node* node, ir_node* src_block, int i)
119 {
120         ir_node* dst_block;
121         ir_node* copy;
122         int arity;
123         int j;
124
125         if (get_nodes_block(node) != src_block) return node;
126         if (get_irn_op(node) == op_Phi) return get_irn_n(node, i);
127
128         copy = exact_copy(node);
129         dst_block = get_nodes_block(get_irn_n(src_block, i));
130         set_nodes_block(copy, dst_block);
131
132         DB((dbg, LEVEL_1, "Copying node %+F to block %+F, copy is %+F\n",
133                 node, dst_block, copy));
134
135         arity = get_irn_arity(node);
136         for (j = 0; j < arity; ++j) {
137                 set_irn_n(copy, j, copy_to(get_irn_n(node, j), src_block, i));
138                 DB((dbg, LEVEL_2, "-- pred %d is %+F\n", j, get_irn_n(copy, j)));
139         }
140         return copy;
141 }
142
143
144 /**
145  * Remove predecessors i and j from node and add predecessor new_pred
146  */
147 static void rewire(ir_node* node, int i, int j, ir_node* new_pred)
148 {
149         int arity = get_irn_arity(node);
150         ir_node **ins;
151         int k;
152         int l;
153
154         NEW_ARR_A(ir_node *, ins, arity - 1);
155
156         l = 0;
157         for (k = 0; k < i;     ++k) ins[l++] = get_irn_n(node, k);
158         for (++k;   k < j;     ++k) ins[l++] = get_irn_n(node, k);
159         for (++k;   k < arity; ++k) ins[l++] = get_irn_n(node, k);
160         ins[l++] = new_pred;
161         assert(l == arity - 1);
162         set_irn_in(node, l, ins);
163 }
164
165
166 /**
167  * Remove the jth predecessors from the ith predecessor of block and add it to block
168  */
169 static void split_block(ir_node* block, int i, int j)
170 {
171         ir_node* pred_block = get_nodes_block(get_irn_n(block, i));
172         int arity = get_irn_arity(block);
173         int new_pred_arity;
174         ir_node *phi, *next;
175         ir_node **ins;
176         ir_node **pred_ins;
177         int k;
178
179         DB((dbg, LEVEL_1, "Splitting predecessor %d of predecessor %d of %+F\n", j, i, block));
180
181         NEW_ARR_A(ir_node*, ins, arity + 1);
182
183         for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
184                 ir_node* copy = copy_to(get_irn_n(phi, i), pred_block, j);
185
186                 for (k = 0; k < i; ++k) ins[k] = get_irn_n(phi, k);
187                 ins[k++] = copy;
188                 for (; k < arity; ++k) ins[k] = get_irn_n(phi, k);
189                 ins[k] = get_irn_n(phi, i);
190                 assert(k == arity);
191                 set_irn_in(phi, arity + 1, ins);
192         }
193
194         for (k = 0; k < i; ++k) ins[k] = get_irn_n(block, k);
195         ins[k++] = get_irn_n(pred_block, j);
196         for (; k < arity; ++k) ins[k] = get_irn_n(block, k);
197         ins[k] = get_irn_n(block, i);
198         assert(k == arity);
199         set_irn_in(block, arity + 1, ins);
200
201         new_pred_arity = get_irn_arity(pred_block) - 1;
202         NEW_ARR_A(ir_node*, pred_ins, new_pred_arity);
203
204         for (phi = get_Block_phis(pred_block); phi != NULL; phi = next) {
205                 for (k = 0; k < j; ++k) pred_ins[k] = get_irn_n(phi, k);
206                 for (; k < new_pred_arity; ++k) pred_ins[k] = get_irn_n(phi, k + 1);
207                 assert(k == new_pred_arity);
208                 next = get_Phi_next(phi);
209                 if (new_pred_arity > 1) {
210                         set_irn_in(phi, new_pred_arity, pred_ins);
211                 } else {
212                         exchange(phi, pred_ins[0]);
213                 }
214         }
215
216         for (k = 0; k < j; ++k) pred_ins[k] = get_irn_n(pred_block, k);
217         for (; k < new_pred_arity; ++k) pred_ins[k] = get_irn_n(pred_block, k + 1);
218         assert(k == new_pred_arity);
219         if (new_pred_arity > 1) {
220                 set_irn_in(pred_block, new_pred_arity, pred_ins);
221         } else {
222                 exchange(pred_block, get_nodes_block(pred_ins[0]));
223         }
224 }
225
226
227 static void prepare_path(ir_node* block, int i, const ir_node* dependency)
228 {
229         ir_node* pred = get_nodes_block(get_irn_n(block, i));
230         int pred_arity;
231         int j;
232
233         DB((dbg, LEVEL_1, "Preparing predecessor %d of %+F\n", i, block));
234
235         pred_arity = get_irn_arity(pred);
236         for (j = 0; j < pred_arity; ++j) {
237                 ir_node* pred_pred = get_nodes_block(get_irn_n(pred, j));
238
239                 if (is_cdep_on(pred_pred, dependency)) {
240                         prepare_path(pred, j, dependency);
241                         split_block(block, i, j);
242                         break;
243                 }
244         }
245 }
246
247
248 static void if_conv_walker(ir_node* block, void* env)
249 {
250         ir_settings_if_conv_t* opt_info = env;
251         int arity;
252         int i;
253
254         /* Bail out, if there are no Phis at all */
255         if (get_Block_phis(block) == NULL) return;
256
257 restart:
258         arity = get_irn_arity(block);
259         for (i = 0; i < arity; ++i) {
260                 ir_node* pred0;
261                 ir_cdep* cdep;
262
263                 pred0 = get_Block_cfgpred_block(block, i);
264                 for (cdep = find_cdep(pred0); cdep != NULL; cdep = cdep->next) {
265                         const ir_node* dependency = cdep->node;
266                         ir_node* projx0 = walk_to_projx(pred0, dependency);
267                         ir_node* cond;
268                         int j;
269
270                         if (projx0 == NULL) continue;
271
272                         cond = get_Proj_pred(projx0);
273                         if (! is_Cond(cond))
274                                 continue;
275
276                         /* We only handle boolean decisions, no switches */
277                         if (get_irn_mode(get_Cond_selector(cond)) != mode_b) continue;
278
279                         for (j = i + 1; j < arity; ++j) {
280                                 ir_node* projx1;
281                                 ir_node* sel;
282                                 ir_node* mux_block;
283                                 ir_node* phi;
284                                 ir_node* pred1;
285                                 dbg_info* cond_dbg;
286
287                                 pred1 = get_Block_cfgpred_block(block, j);
288
289                                 if (!is_cdep_on(pred1, dependency)) continue;
290
291                                 projx1 = walk_to_projx(pred1, dependency);
292
293                                 if (projx1 == NULL) continue;
294
295                                 phi = get_Block_phis(block);
296                                 if (!opt_info->allow_ifconv(get_Cond_selector(cond), phi, i, j)) continue;
297
298                                 DB((dbg, LEVEL_1, "Found Cond %+F with proj %+F and %+F\n",
299                                         cond, projx0, projx1
300                                 ));
301
302                                 prepare_path(block, i, dependency);
303                                 prepare_path(block, j, dependency);
304                                 arity = get_irn_arity(block);
305
306                                 sel = get_Cond_selector(cond);
307
308                                 mux_block = get_nodes_block(cond);
309                                 cond_dbg = get_irn_dbg_info(cond);
310                                 do {
311                                         ir_node* val_i = get_irn_n(phi, i);
312                                         ir_node* val_j = get_irn_n(phi, j);
313                                         ir_node* mux;
314                                         ir_node* next_phi;
315
316                                         if (val_i == val_j) {
317                                                 mux = val_i;
318                                                 DB((dbg, LEVEL_2,  "Generating no Mux, because both values are equal\n"));
319                                         } else {
320                                                 ir_node *t, *f;
321
322                                                 /* Something is very fishy if two predecessors of a PhiM point into
323                                                  * one block, but not at the same memory node
324                                                  */
325                                                 assert(get_irn_mode(phi) != mode_M);
326                                                 if (get_Proj_proj(projx0) == pn_Cond_true) {
327                                                         t = val_i;
328                                                         f = val_j;
329                                                 } else {
330                                                         t = val_j;
331                                                         f = val_i;
332                                                 }
333
334                                                 mux = new_rd_Mux(cond_dbg, current_ir_graph, mux_block, sel, f, t, get_irn_mode(phi));
335                                                 DB((dbg, LEVEL_2, "Generating %+F for %+F\n", mux, phi));
336                                         }
337
338                                         next_phi = get_Phi_next(phi);
339
340                                         if (arity == 2) {
341                                                 exchange(phi, mux);
342                                         } else {
343                                                 rewire(phi, i, j, mux);
344                                         }
345
346                                         phi = next_phi;
347                                 } while (phi != NULL);
348
349                                 exchange(get_nodes_block(get_irn_n(block, i)), mux_block);
350                                 exchange(get_nodes_block(get_irn_n(block, j)), mux_block);
351
352                                 if (arity == 2) {
353                                         unsigned mark;
354 #if 1
355                                         DB((dbg, LEVEL_1,  "Welding block %+F and %+F\n", block, mux_block));
356                                         /* copy the block-info from the Mux-block to the block before merging */
357
358                                         mark =  get_Block_mark(mux_block) | get_Block_mark(block);
359                                         set_Block_mark(block, mark);
360                                         set_Block_phis(block, get_Block_phis(mux_block));
361
362                                         set_irn_in(block, get_irn_arity(mux_block), get_irn_in(mux_block) + 1);
363                                         exchange_cdep(mux_block, block);
364                                         exchange(mux_block, block);
365 #else
366                                         DB((dbg, LEVEL_1,  "Welding block %+F to %+F\n", block, mux_block));
367                                         mark =  get_Block_mark(mux_block) | get_Block_mark(block);
368                                         /* mark both block just to be sure, should be enough to mark mux_block */
369                                         set_Block_mark(mux_block, mark);
370                                         exchange(block, mux_block);
371 #endif
372                                         return;
373                                 } else {
374                                         rewire(block, i, j, new_r_Jmp(current_ir_graph, mux_block));
375                                         goto restart;
376                                 }
377                         }
378                 }
379         }
380 }
381
382 /**
383  * Block walker: clear block mark and Phi list
384  */
385 static void init_block_link(ir_node *block, void *env)
386 {
387         (void)env;
388         set_Block_mark(block, 0);
389         set_Block_phis(block, NULL);
390 }
391
392
393 /**
394  * Daisy-chain all phis in a block
395  * If a non-movable node is encountered set the has_pinned flag in its block.
396  */
397 static void collect_phis(ir_node *node, void *env) {
398         (void) env;
399
400         if (is_Phi(node)) {
401                 ir_node *block = get_nodes_block(node);
402
403                 add_Block_phi(block, node);
404         } else {
405                 if (is_no_Block(node) && get_irn_pinned(node) == op_pin_state_pinned) {
406                         /*
407                          * Ignore control flow nodes, these will be removed.
408                          * This ignores Raise. That is surely bad. FIXME.
409                          */
410                         if (!is_cfop(node)) {
411                                 ir_node *block = get_nodes_block(node);
412
413                                 DB((dbg, LEVEL_2, "Node %+F in block %+F is unmovable\n", node, block));
414                                 set_Block_mark(block, 1);
415                         }
416                 }
417         }
418 }
419
420 static void optimise_muxs_0(ir_node* mux, void* env)
421 {
422         ir_node* t;
423         ir_node* f;
424
425         (void) env;
426
427         if (!is_Mux(mux)) return;
428
429         t = get_Mux_true(mux);
430         f = get_Mux_false(mux);
431
432         DB((dbg, LEVEL_3, "Simplify %+F T=%+F F=%+F\n", mux, t, f));
433
434         if (is_Unknown(t)) {
435                 DB((dbg, LEVEL_3, "Replace Mux with unknown operand by %+F\n", f));
436                 exchange(mux, f);
437                 return;
438         }
439         if (is_Unknown(f)) {
440                 DB((dbg, LEVEL_3, "Replace Mux with unknown operand by %+F\n", t));
441                 exchange(mux, t);
442                 return;
443         }
444
445         if (is_Mux(t)) {
446                 ir_graph* irg   = current_ir_graph;
447                 ir_node*  block = get_nodes_block(mux);
448                 ir_mode*  mode  = get_irn_mode(mux);
449                 ir_node*  c0    = get_Mux_sel(mux);
450                 ir_node*  c1    = get_Mux_sel(t);
451                 ir_node*  t1    = get_Mux_true(t);
452                 ir_node*  f1    = get_Mux_false(t);
453                 if (f == f1) {
454                         /* Mux(c0, Mux(c1, x, y), y) -> typical if (c0 && c1) x else y */
455                         ir_node* and_    = new_r_And(irg, block, c0, c1, mode_b);
456                         ir_node* new_mux = new_r_Mux(irg, block, and_, f1, t1, mode);
457                         exchange(mux, new_mux);
458                 } else if (f == t1) {
459                         /* Mux(c0, Mux(c1, x, y), x) */
460                         ir_node* not_c1 = new_r_Not(irg, block, c1, mode_b);
461                         ir_node* and_   = new_r_And(irg, block, c0, not_c1, mode_b);
462                         ir_node* new_mux = new_r_Mux(irg, block, and_, t1, f1, mode);
463                         exchange(mux, new_mux);
464                 }
465         } else if (is_Mux(f)) {
466                 ir_graph* irg   = current_ir_graph;
467                 ir_node*  block = get_nodes_block(mux);
468                 ir_mode*  mode  = get_irn_mode(mux);
469                 ir_node*  c0    = get_Mux_sel(mux);
470                 ir_node*  c1    = get_Mux_sel(f);
471                 ir_node*  t1    = get_Mux_true(f);
472                 ir_node*  f1    = get_Mux_false(f);
473                 if (t == t1) {
474                         /* Mux(c0, x, Mux(c1, x, y)) -> typical if (c0 || c1) x else y */
475                         ir_node* or_     = new_r_Or(irg, block, c0, c1, mode_b);
476                         ir_node* new_mux = new_r_Mux(irg, block, or_, f1, t1, mode);
477                         exchange(mux, new_mux);
478                 } else if (t == f1) {
479                         /* Mux(c0, x, Mux(c1, y, x)) */
480                         ir_node* not_c1  = new_r_Not(irg, block, c1, mode_b);
481                         ir_node* or_     = new_r_Or(irg, block, c0, not_c1, mode_b);
482                         ir_node* new_mux = new_r_Mux(irg, block, or_, t1, f1, mode);
483                         exchange(mux, new_mux);
484                 }
485         }
486 }
487
488
489 static void optimise_muxs_1(ir_node* mux, void* env)
490 {
491         ir_node* t;
492         ir_node* f;
493         ir_mode* mode;
494
495         (void) env;
496
497         if (!is_Mux(mux)) return;
498
499         t = get_Mux_true(mux);
500         f = get_Mux_false(mux);
501
502         DB((dbg, LEVEL_3, "Simplify %+F T=%+F F=%+F\n", mux, t, f));
503
504         mode = get_irn_mode(mux);
505
506         if (is_Const(t) && is_Const(f) && (mode_is_int(mode))) {
507                 ir_node* block = get_nodes_block(mux);
508                 ir_node* c     = get_Mux_sel(mux);
509                 tarval* tv_t = get_Const_tarval(t);
510                 tarval* tv_f = get_Const_tarval(f);
511                 if (tarval_is_one(tv_t) && tarval_is_null(tv_f)) {
512                         ir_node* conv  = new_r_Conv(current_ir_graph, block, c, mode);
513                         exchange(mux, conv);
514                 } else if (tarval_is_null(tv_t) && tarval_is_one(tv_f)) {
515                         ir_node* not_  = new_r_Not(current_ir_graph, block, c, mode_b);
516                         ir_node* conv  = new_r_Conv(current_ir_graph, block, not_, mode);
517                         exchange(mux, conv);
518                 }
519         }
520 }
521
522
523 void opt_if_conv(ir_graph *irg, const ir_settings_if_conv_t *params)
524 {
525         ir_settings_if_conv_t p;
526
527         /* get the parameters */
528         p = (params != NULL ? *params : default_info);
529
530         FIRM_DBG_REGISTER(dbg, "firm.opt.ifconv");
531
532         DB((dbg, LEVEL_1, "Running if-conversion on %+F\n", irg));
533
534         normalize_one_return(irg);
535         remove_critical_cf_edges(irg);
536
537         compute_cdep(irg);
538         assure_doms(irg);
539
540         ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK);
541
542         irg_block_walk_graph(irg, init_block_link, NULL, NULL);
543         irg_walk_graph(irg, collect_phis, NULL, NULL);
544         irg_block_walk_graph(irg, NULL, if_conv_walker, &p);
545
546         ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK);
547
548         local_optimize_graph(irg);
549
550         irg_walk_graph(irg, NULL, optimise_muxs_0, NULL);
551 #if 1
552         irg_walk_graph(irg, NULL, optimise_muxs_1, NULL);
553 #endif
554
555         /* TODO: graph might be changed, handle more graceful */
556         set_irg_outs_inconsistent(irg);
557         set_irg_extblk_inconsistent(irg);
558         set_irg_loopinfo_inconsistent(irg);
559         free_dom(irg);
560
561         free_cdep(irg);
562 }