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