d4dd6f8c31de997c5057df5f10f94785fd2be742
[libfirm] / ir / opt / condeval.c
1 /*
2  * Copyright (C) 1995-2007 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
22  * @brief   Partial condition evaluation
23  * @date    10. Sep. 2006
24  * @author  Christoph Mallon, Matthias Braun
25  * @version $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <assert.h>
32 #include "array.h"
33 #include "condeval.h"
34 #include "debug.h"
35 #include "ircons.h"
36 #include "irgmod.h"
37 #include "irgopt.h"
38 #include "irgwalk.h"
39 #include "irnode.h"
40 #include "irnode_t.h"
41 #include "iredges.h"
42 #include "iredges_t.h"
43 #include "irtools.h"
44 #include "irgraph.h"
45 #include "tv.h"
46
47 DEBUG_ONLY(static firm_dbg_module_t *dbg);
48
49 /**
50  * Add the new predecessor x to node node, which is either a Block or a Phi
51  */
52 static void add_pred(ir_node* node, ir_node* x)
53 {
54         ir_node** ins;
55         int n;
56         int i;
57
58         assert(is_Block(node) || is_Phi(node));
59
60         n = get_irn_arity(node);
61         NEW_ARR_A(ir_node*, ins, n + 1);
62         for (i = 0; i < n; i++)
63                 ins[i] = get_irn_n(node, i);
64         ins[n] = x;
65         set_irn_in(node, n + 1, ins);
66 }
67
68 static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode)
69 {
70         int i;
71         int n_cfgpreds;
72         ir_graph *irg;
73         ir_node *phi;
74         ir_node **in;
75
76         // This is needed because we create bads sometimes
77         if(is_Bad(block))
78                 return new_Bad();
79
80         // already processed this block?
81         if(irn_visited(block)) {
82                 ir_node *value = (ir_node*) get_irn_link(block);
83                 return value;
84         }
85
86         // blocks with only 1 pred need no phi
87         n_cfgpreds = get_Block_n_cfgpreds(block);
88         if(n_cfgpreds == 1) {
89                 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
90                 ir_node *value = search_def_and_create_phis(pred_block, mode);
91
92                 set_irn_link(block, value);
93                 mark_irn_visited(block);
94                 return value;
95         }
96
97         // create a new phi
98         NEW_ARR_A(ir_node*, in, n_cfgpreds);
99         for(i = 0; i < n_cfgpreds; ++i)
100                 in[i] = new_Unknown(mode);
101
102         irg = get_irn_irg(block);
103         phi = new_r_Phi(irg, block, n_cfgpreds, in, mode);
104         set_irn_link(block, phi);
105         mark_irn_visited(block);
106
107         // set phi preds
108         for(i = 0; i < n_cfgpreds; ++i) {
109                 ir_node *pred_block = get_Block_cfgpred_block(block, i);
110                 ir_node *pred_val = search_def_and_create_phis(pred_block, mode);
111
112                 set_irn_n(phi, i, pred_val);
113         }
114
115         return phi;
116 }
117
118 /**
119  * Given a set of values this function constructs SSA-form for the users of the
120  * first value (the users are determined through the out-edges of the value).
121  * Uses the irn_visited flags. Works without using the dominance tree.
122  */
123 static void construct_ssa(ir_node * const *blocks, ir_node * const *vals, int n_vals)
124 {
125         int i;
126         ir_graph *irg;
127         ir_mode *mode;
128         const ir_edge_t *edge;
129         const ir_edge_t *next;
130         ir_node *value;
131
132         assert(n_vals > 0);
133
134         irg = get_irn_irg(vals[0]);
135         inc_irg_visited(irg);
136
137         mode = get_irn_mode(vals[0]);
138         for(i = 0; i < n_vals; ++i) {
139                 ir_node *value = vals[i];
140                 ir_node *value_block = blocks[i];
141
142                 assert(get_irn_mode(value) == mode);
143
144                 set_irn_link(value_block, value);
145                 mark_irn_visited(value_block);
146         }
147
148         // Only fix the users of the first, i.e. the original node
149         value = vals[0];
150
151         // this can happen when fixing phi preds, we mustn't fix the users
152         if(get_nodes_block(value) != blocks[0]) return;
153
154         foreach_out_edge_safe(value, edge, next) {
155                 ir_node *user = get_edge_src_irn(edge);
156                 int j = get_edge_src_pos(edge);
157                 ir_node *user_block = get_nodes_block(user);
158                 ir_node *newval;
159
160                 // ignore keeps
161                 if(get_irn_op(user) == op_End)
162                         continue;
163
164                 DB((dbg, LEVEL_3, ">>> Fixing user %+F (pred %d == %+F)\n", user, j, get_irn_n(user, j)));
165
166                 if(is_Phi(user)) {
167                         ir_node *pred_block = get_Block_cfgpred_block(user_block, j);
168                         newval = search_def_and_create_phis(pred_block, mode);
169                 } else {
170                         newval = search_def_and_create_phis(user_block, mode);
171                 }
172
173                 // don't fix newly created phis from the SSA construction
174                 if (newval != user) {
175                         DB((dbg, LEVEL_4, ">>>> Setting input %d of %+F to %+F\n", j, user, newval));
176                         set_irn_n(user, j, newval);
177                 }
178         }
179 }
180
181 static void split_critical_edge(ir_node *block, int pos) {
182         ir_graph *irg = get_irn_irg(block);
183         ir_node *in[1];
184         ir_node *new_block;
185         ir_node *new_jmp;
186
187         in[0] = get_Block_cfgpred(block, pos);
188         new_block = new_r_Block(irg, 1, in);
189         new_jmp = new_r_Jmp(irg, new_block);
190         set_Block_cfgpred(block, pos, new_jmp);
191 }
192
193 typedef struct _condeval_env_t {
194         ir_node *true_block;
195         pn_Cmp pnc;
196         ir_node *cnst;
197         unsigned long visited_nr;
198
199         ir_node *cnst_pred;     /**< the block before the constant */
200         int cnst_pos;       /**< the pos to the constant block (needed to kill that edge later) */
201 } condeval_env_t;
202
203 static void copy_and_fix(ir_node *block, ir_node *copy_block, int j, const condeval_env_t *env) {
204         const ir_edge_t *edge;
205
206         /* Look at all nodes in the cond_block and copy them into pred */
207         foreach_out_edge(block, edge) {
208                 ir_node *node = get_edge_src_irn(edge);
209                 ir_node *copy;
210                 ir_mode *mode = get_irn_mode(node);
211
212                 /* ignore control flow */
213                 if (mode == mode_X || is_Cond(node))
214                         continue;
215                 /* we may not copy mode_b nodes, because this could produce phi with mode_bs which can't
216                    be handled in all backends. Instead we duplicate the node and move it to it's users */
217                 if (mode == mode_b) {
218                         const ir_edge_t *edge, *next;
219                         ir_node *pred;
220                         int      pn;
221
222                         assert(is_Proj(node));
223
224                         pred = get_Proj_pred(node);
225                         pn   = get_Proj_proj(node);
226
227                         foreach_out_edge_safe(node, edge, next) {
228                                 ir_node *cmp_copy;
229                                 ir_node *user       = get_edge_src_irn(edge);
230                                 int pos             = get_edge_src_pos(edge);
231                                 ir_node *user_block = get_nodes_block(user);
232
233                                 if(user_block == block)
234                                         continue;
235
236                                 cmp_copy = exact_copy(pred);
237                                 set_nodes_block(cmp_copy, user_block);
238                                 copy = new_r_Proj(current_ir_graph, user_block, cmp_copy, mode_b, pn);
239                                 set_irn_n(user, pos, copy);
240                         }
241                         continue;
242                 }
243
244                 /* we can evaluate Phis right now, all other nodes get copied */
245                 if (is_Phi(node)) {
246                         copy = get_Phi_pred(node, j);
247                 } else {
248                         copy = exact_copy(node);
249                         set_nodes_block(copy, copy_block);
250                 }
251
252                 set_irn_link(node, copy);
253                 set_irn_visited(node, env->visited_nr);
254
255                 /* we might hit values in blocks that have already been processed by a
256                  * recursive find_phi_with_const call */
257                 assert(get_irn_visited(copy) <= env->visited_nr);
258                 if(get_irn_visited(copy) >= env->visited_nr) {
259                         ir_node *prev_copy = get_irn_link(copy);
260                         if(prev_copy != NULL)
261                                 set_irn_link(node, prev_copy);
262                 }
263         }
264
265         /* fix data-flow (and reconstruct SSA if needed) */
266         foreach_out_edge(block, edge) {
267                 ir_node *vals[2];
268                 ir_node *blocks[2];
269                 ir_node *node = get_edge_src_irn(edge);
270                 ir_mode *mode = get_irn_mode(node);
271
272                 if (mode == mode_X || is_Cond(node))
273                         continue;
274                 if (mode == mode_b)
275                         continue;
276
277                 DB((dbg, LEVEL_2, ">> Fixing users of %+F\n", node));
278
279                 blocks[0] = block;
280                 vals[0] = node;
281                 blocks[1] = copy_block;
282                 vals[1] = get_irn_link(node);
283                 construct_ssa(blocks, vals, 2);
284         }
285 }
286
287 static int eval_cmp(pn_Cmp pnc, tarval *tv1, tarval *tv2) {
288         pn_Cmp cmp_result = tarval_cmp(tv1, tv2);
289
290         // does the compare evaluate to true?
291         if(cmp_result == pn_Cmp_False)
292                 return 0;
293         if((cmp_result & pnc) != cmp_result)
294                 return 0;
295
296         return 1;
297 }
298
299 static ir_node *find_phi_with_const(ir_node *jump, ir_node *value, condeval_env_t *env) {
300         ir_node *block = get_nodes_block(jump);
301
302         if(irn_visited(value))
303                 return NULL;
304         mark_irn_visited(value);
305
306         if(is_Const(value)) {
307                 tarval *tv_const = get_Const_tarval(env->cnst);
308                 tarval *tv = get_Const_tarval(value);
309
310                 if(!eval_cmp(env->pnc, tv, tv_const))
311                         return NULL;
312
313                 DB((
314                         dbg, LEVEL_1,
315                         "> Found condition evaluation candidate %+F->%+F\n",
316                         env->true_block, block
317                 ));
318
319                 // adjust true_block to point directly towards our jump
320                 add_pred(env->true_block, jump);
321
322                 split_critical_edge(env->true_block, 0);
323
324                 // we need a bigger visited nr when going back
325                 env->visited_nr++;
326
327                 return block;
328         }
329
330         if(is_Phi(value)) {
331                 int i, arity;
332
333                 // the phi has to be in the same block as the jump
334                 if(get_nodes_block(value) != block)
335                         return NULL;
336
337                 arity = get_irn_arity(value);
338                 for(i = 0; i < arity; ++i) {
339                         ir_node *copy_block;
340                         ir_node *phi_pred = get_Phi_pred(value, i);
341                         ir_node *cfgpred = get_Block_cfgpred(block, i);
342
343                         copy_block = find_phi_with_const(cfgpred, phi_pred, env);
344                         if(copy_block == NULL)
345                                 continue;
346
347                         /* copy duplicated nodes in copy_block and fix SSA */
348                         copy_and_fix(block, copy_block, i, env);
349
350                         if(copy_block == get_nodes_block(cfgpred)) {
351                                 env->cnst_pred = block;
352                                 env->cnst_pos = i;
353                         }
354
355                         // return now as we can't process more possibilities in 1 run
356                         return copy_block;
357                 }
358         }
359
360         return NULL;
361 }
362
363
364 /**
365  * Block-walker: searches for the following construct
366  *
367  *  Const or Phi with constants
368  *           |
369  *          Cmp
370  *           |
371  *         Cond
372  *          /
373  *       ProjX
374  *        /
375  *     Block
376  */
377 static void cond_eval(ir_node* block, void* data)
378 {
379         condeval_env_t env;
380         int *changed = data;
381         ir_graph *irg = current_ir_graph;
382         ir_node *copy_block;
383         ir_node *pred;
384         ir_node *projx;
385         ir_node *cond;
386         ir_node *cmp;
387         ir_node *left;
388         ir_node *right;
389         ir_node *cond_block;
390         pn_Cmp pnc;
391
392         if(get_Block_n_cfgpreds(block) != 1)
393                 return;
394
395         projx = get_Block_cfgpred(block, 0);
396         if (!is_Proj(projx))
397                 return;
398         assert(get_irn_mode(projx) == mode_X);
399
400         cond = get_Proj_pred(projx);
401         if (!is_Cond(cond))
402                 return;
403
404         pred = get_Cond_selector(cond);
405         // TODO handle switches
406         if (get_irn_mode(pred) != mode_b)
407                 return;
408         if (!is_Proj(pred))
409                 return;
410         pnc = get_Proj_proj(pred);
411
412         cmp = get_Proj_pred(pred);
413         assert(is_Cmp(cmp));
414
415         left  = get_Cmp_left(cmp);
416         right = get_Cmp_right(cmp);
417         assert(get_irn_mode(left) == get_irn_mode(right));
418
419         /* we assume that the cond_block is the true case */
420         if (get_Proj_proj(projx) == pn_Cond_false) {
421                 pnc = get_negated_pnc(pnc, get_irn_mode(left));
422         }
423
424         /* we assume that the constant is on the right side, swap left/right
425          * if needed */
426         if(is_Const(left)) {
427                 ir_node *t = left;
428                 left = right;
429                 right = t;
430
431                 pnc = get_inversed_pnc(pnc);
432         }
433
434         if(!is_Const(right))
435                 return;
436
437         cond_block = get_nodes_block(cond);
438
439         // special case: comparing a constant with a constant
440         if(is_Const(left)) {
441                 tarval *tv1 = get_Const_tarval(left);
442                 tarval *tv2 = get_Const_tarval(right);
443                 ir_node *pred;
444                 if(eval_cmp(pnc, tv1, tv2)) {
445                         pred = new_r_Jmp(irg, cond_block);
446                 } else {
447                         pred = new_Bad();
448                 }
449                 set_Block_cfgpred(block, 0, pred);
450                 *changed = 1;
451                 set_irg_doms_inconsistent(irg);
452                 set_irg_extblk_inconsistent(irg);
453                 set_irg_loopinfo_inconsistent(irg);
454         } else {
455                 if(get_nodes_block(left) != cond_block)
456                         return;
457
458                 // (recursively) look if a pred of a phi is a constant
459                 env.true_block = block;
460                 env.pnc = pnc;
461                 env.cnst = right;
462                 inc_irg_visited(current_ir_graph);
463                 env.visited_nr = get_irg_visited(irg);
464
465                 copy_block = find_phi_with_const(projx, left, &env);
466
467                 if(copy_block != NULL) {
468                         /* we have to remove the edge towards the pred as the pred now
469                          * jumps into the true_block. We also have to shorten phis
470                          * in our block because of this */
471                         const ir_edge_t *edge, *next;
472                         ir_node* bad = new_Bad();
473                         size_t cnst_pos = env.cnst_pos;
474
475                         /* shorten phis */
476                         foreach_out_edge_safe(env.cnst_pred, edge, next) {
477                                 ir_node *node = get_edge_src_irn(edge);
478
479                                 if(is_Phi(node))
480                                         set_Phi_pred(node, cnst_pos, bad);
481                         }
482
483                         set_Block_cfgpred(env.cnst_pred, cnst_pos, bad);
484
485                         /* the graph is changed now */
486                         *changed = 1;
487                 }
488         }
489 }
490
491 void opt_cond_eval(ir_graph* irg)
492 {
493         int changed, rerun;
494
495         FIRM_DBG_REGISTER(dbg, "firm.opt.condeval");
496
497         DB((dbg, LEVEL_1, "===> Performing condition evaluation on %+F\n", irg));
498
499         remove_critical_cf_edges(irg);
500         normalize_proj_nodes(irg);
501
502         edges_assure(irg);
503         set_using_irn_link(irg);
504         set_using_visited(irg);
505
506         changed = 0;
507         do {
508                 rerun = 0;
509                 irg_block_walk_graph(irg, cond_eval, NULL, &rerun);
510                 changed |= rerun;
511         } while (rerun);
512
513         if (changed) {
514                 /* control flow changed, some blocks may become dead */
515                 set_irg_doms_inconsistent(irg);
516                 set_irg_extblk_inconsistent(irg);
517                 set_irg_loopinfo_inconsistent(irg);
518         }
519
520         clear_using_visited(irg);
521         clear_using_irn_link(irg);
522 }