Typo in comment.
[libfirm] / ir / ana / irconsconfirm.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
22  * @brief    Construction of Confirm nodes
23  * @author   Michael Beck
24  * @date     6.2005
25  * @version  $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include "irgraph_t.h"
32 #include "irnode_t.h"
33 #include "ircons_t.h"
34 #include "irgmod.h"
35 #include "iropt_dbg.h"
36 #include "iredges_t.h"
37 #include "irgwalk.h"
38 #include "irprintf.h"
39 #include "irgopt.h"
40 #include "irtools.h"
41
42 /**
43  * Walker environment.
44  */
45 typedef struct _env_t {
46         unsigned num_confirms;  /**< number of inserted Confirm nodes */
47         unsigned num_consts;    /**< number of constants placed */
48         unsigned num_eq;        /**< number of equalities placed */
49 } env_t;
50
51 /**
52  * Return the effective use block of a node and it's predecessor on
53  * position pos.
54  *
55  * @param node  the node
56  * @param pos   the position of the used input
57  *
58  * This handles correctly Phi nodes.
59  */
60 static ir_node *get_effective_use_block(ir_node *node, int pos) {
61         if (is_Phi(node)) {
62                 /* the effective use of a Phi argument is in its predecessor block */
63                 node = get_nodes_block(node);
64                 return get_Block_cfgpred_block(node, pos);
65         }
66         return get_nodes_block(node);
67 }
68
69 /**
70  * Handle a CASE-branch.
71  *
72  * @param block   the block which is entered by the branch
73  * @param irn     the node expressing the switch value
74  * @param nr      the branch label
75  * @param env     statistical environment
76  *
77  * Branch labels are a simple case. We can replace the value
78  * by a Const with the branch label.
79  */
80 static void handle_case(ir_node *block, ir_node *irn, long nr, env_t *env) {
81         const ir_edge_t *edge, *next;
82         ir_node *c = NULL;
83
84         if (is_Bad(irn))
85                 return;
86
87         for (edge = get_irn_out_edge_first(irn); edge; edge = next) {
88                 ir_node *succ = get_edge_src_irn(edge);
89                 int     pos   = get_edge_src_pos(edge);
90                 ir_node *blk  = get_effective_use_block(succ, pos);
91
92                 next = get_irn_out_edge_next(irn, edge);
93
94                 if (block_dominates(block, blk)) {
95                         /*
96                          * Ok, we found a user of irn that is placed
97                          * in a block dominated by the branch block.
98                          * We can replace the input with the Constant
99                          * branch label.
100                          */
101
102                         if (! c) {
103                                 ir_mode *mode = get_irn_mode(irn);
104                                 ir_type *tp   = get_irn_type(irn);
105                                 tarval *tv    = new_tarval_from_long(nr, mode);
106                                 c = new_r_Const_type(current_ir_graph, block, mode, tv, tp);
107                         }
108
109                         set_irn_n(succ, pos, c);
110                         DBG_OPT_CONFIRM_C(irn, c);
111 //                      ir_printf("1 Replacing input %d of node %n with %n\n", pos, succ, c);
112
113                         env->num_consts += 1;
114                 }
115         }
116 }  /* handle_case */
117
118 /**
119  * Handle a mode_b input of Cond nodes.
120  *
121  * @param block     the block which is entered by the branch
122  * @param selector  the mode_b node expressing the branch condition
123  * @param pnc       the true/false condition branch
124  * @param env       statistical environment
125  */
126 static void handle_modeb(ir_node *block, ir_node *selector, pn_Cond pnc, env_t *env) {
127         ir_node *cond, *old, *cond_block, *other_blk = NULL, *con = NULL;
128         ir_node *c_b = NULL, *c_o = NULL;
129         const ir_edge_t *edge, *next;
130
131         for (edge = get_irn_out_edge_first(selector); edge; edge = next) {
132                 ir_node *user     = get_edge_src_irn(edge);
133                 int     pos       = get_edge_src_pos(edge);
134                 ir_node *user_blk = get_effective_use_block(user, pos);
135
136                 next = get_irn_out_edge_next(selector, edge);
137                 if (block_dominates(block, user_blk)) {
138                         /*
139                          * Ok, we found a usage of selector in a block
140                          * dominated by the branch block.
141                          * We can replace the input with true/false.
142                          */
143                         if (con == NULL) {
144                                 con = new_Const(mode_b, pnc == pn_Cond_true ? tarval_b_true : tarval_b_false);
145                         }
146                         old = get_irn_n(user, pos);
147                         set_irn_n(user, pos, con);
148                         DBG_OPT_CONFIRM_C(old, con);
149
150                         // ir_printf("2 Replacing input %d of node %n with %n\n", pos, user, con);
151
152                         env->num_consts += 1;
153                 } else {
154                         int i, n;
155
156                         /* get the other block */
157                         if (other_blk == NULL) {
158                                 /* we have already tested, that block has only ONE Cond predecessor */
159                                 cond = get_Proj_pred(get_Block_cfgpred(block, 0));
160                                 cond_block = get_nodes_block(cond);
161                                 foreach_out_edge(cond, edge) {
162                                         ir_node *proj = get_edge_src_irn(edge);
163                                         if (get_Proj_proj(proj) == (long)pnc)
164                                                 continue;
165                                         edge = get_irn_out_edge_first(proj);
166                                         other_blk = get_edge_src_irn(edge);
167                                         break;
168                                 }
169                                 assert(other_blk);
170
171                                 /*
172                                  * Note the special case here: if block is a then, there might be no else
173                                  * block. In that case the other_block is the user_blk itself and pred_block
174                                  * is the cond_block ...
175                                  *
176                                  * Best would be to introduce a block here, removing this critical edge.
177                                  * For some reasons I cannot repair dominance here, so I have to remove
178                                  * ALL critical edges...
179                                  * FIXME: This should not be needed if we could repair dominance ...
180                                  */
181                         }
182
183                         n = get_Block_n_cfgpreds(user_blk);
184
185                         /*
186                          * We have found a user in a non-dominated block:
187                          * check, if all its block predecessors are dominated.
188                          * If yes, place a Phi.
189                          */
190                         for (i = n - 1; i >= 0; --i) {
191                                 ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
192
193                                 if (!block_dominates(block, pred_blk) &&
194                                     !block_dominates(other_blk, pred_blk)) {
195                                         /* can't do anything */
196                                         break;
197                                 }
198                         }
199                         if (i < 0) {
200                                 ir_node *phi, **in;
201
202                                 NEW_ARR_A(ir_node *, in, n);
203                                 /* ok, ALL predecessors are either dominated by block OR other block */
204                                 if (c_b == NULL) {
205                                         ir_node *c_true  = new_Const(mode_b, tarval_b_true);
206                                         ir_node *c_false = new_Const(mode_b, tarval_b_false);
207                                         c_b = new_r_Confirm(current_ir_graph, cond_block, selector,
208                                                 pnc == pn_Cond_true ? c_true : c_false, pn_Cmp_Eq);
209                                         c_o = new_r_Confirm(current_ir_graph, cond_block, selector,
210                                                 pnc == pn_Cond_false ? c_true : c_false, pn_Cmp_Eq);
211                                 }
212                                 for (i = n - 1; i >= 0; --i) {
213                                         ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
214
215                                         if (block_dominates(block, pred_blk))
216                                                 in[i] = c_b;
217                                         else
218                                                 in[i] = c_o;
219                                 }
220                                 phi = new_r_Phi(current_ir_graph, user_blk, n, in, mode_b);
221                                 set_irn_n(user, pos, phi);
222                         }
223                 }
224         }
225 }
226
227 /**
228  * Handle an IF-branch.
229  *
230  * @param block   the block which is entered by the branch
231  * @param cmp     the Cmp node expressing the branch condition
232  * @param pnc     the Compare relation for taking this branch
233  * @param env     statistical environment
234  */
235 static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env) {
236         ir_node *left  = get_Cmp_left(cmp);
237         ir_node *right = get_Cmp_right(cmp);
238         ir_node *cond_block;
239         ir_op *op;
240         const ir_edge_t *edge, *next;
241
242         /* Beware of Bads */
243         if (is_Bad(left) || is_Bad(right))
244                 return;
245
246         op = get_irn_op(left);
247
248         /* Do not create Confirm nodes for Cmp(Const, Const) constructs.
249            These are removed anyway */
250         if (op == op_Const && is_Const(right))
251                 return;
252
253         /* try to place the constant on the right side for a Confirm */
254         if (op == op_Const || op == op_SymConst) {
255                 ir_node *t = left;
256
257                 left  = right;
258                 right = t;
259
260                 pnc = get_inversed_pnc(pnc);
261         }
262
263         /*
264          * First case: both values are identical.
265          * replace the left one by the right (potentially const) one.
266          */
267         if (pnc == pn_Cmp_Eq) {
268                 cond_block = get_Block_cfgpred_block(block, 0);
269                 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
270                         ir_node *user = get_edge_src_irn(edge);
271                         int     pos   = get_edge_src_pos(edge);
272                         ir_node *blk  = get_effective_use_block(user, pos);
273
274                         next = get_irn_out_edge_next(left, edge);
275                         if (block_dominates(block, blk)) {
276                                 /*
277                                  * Ok, we found a usage of left in a block
278                                  * dominated by the branch block.
279                                  * We can replace the input with right.
280                                  */
281                                 set_irn_n(user, pos, right);
282                                 DBG_OPT_CONFIRM(left, right);
283
284 //                              ir_printf("2 Replacing input %d of node %n with %n\n", pos, user, right);
285
286                                 env->num_eq += 1;
287                         } else if (block_dominates(blk, cond_block)) {
288                                 if (is_Const(right) && get_irn_pinned(user) == op_pin_state_floats) {
289                                         /*
290                                          * left == Const and we found a movable user of left in a
291                                          * dominator of the Cond block
292                                          */
293                                         const ir_edge_t *edge, *next;
294                                         for (edge = get_irn_out_edge_first(user); edge; edge = next) {
295                                                 ir_node *usr_of_usr = get_edge_src_irn(edge);
296                                                 int      npos = get_edge_src_pos(edge);
297                                                 ir_node *blk  = get_effective_use_block(usr_of_usr, npos);
298
299                                                 next = get_irn_out_edge_next(user, edge);
300                                                 if (block_dominates(block, blk)) {
301                                                         /*
302                                                          * The user of the user is dominated by our true/false
303                                                          * block. So, create a copy of user WITH the constant
304                                                          * replacing it's pos'th input.
305                                                          *
306                                                          * This is always good for unop's and might be good
307                                                          * for binops.
308                                                          *
309                                                          * If user has other user in the false/true block, code
310                                                          * placement will move it down.
311                                                          * If there are users in cond block or upper, we create
312                                                          * "redundant ops", because one will have a const op,
313                                                          * the other no const ...
314                                                          */
315                                                         ir_node *new_op = exact_copy(user);
316                                                         set_nodes_block(new_op, block);
317                                                         set_irn_n(new_op, pos, right);
318                                                         set_irn_n(usr_of_usr, npos, new_op);
319                                                 }
320                                         }
321                                 }
322                         }
323                 }
324         } else { /* not pn_Cmp_Eq cases */
325                 ir_node *c = NULL;
326
327                 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
328                         ir_node *succ = get_edge_src_irn(edge);
329                         int     pos   = get_edge_src_pos(edge);
330                         ir_node *blk  = get_effective_use_block(succ, pos);
331
332                         next = get_irn_out_edge_next(left, edge);
333                         if (block_dominates(block, blk)) {
334                                 /*
335                                  * Ok, we found a usage of left in a block
336                                  * dominated by the branch block.
337                                  * We can replace the input with a Confirm(left, pnc, right).
338                                  */
339                                 if (! c)
340                                         c = new_r_Confirm(current_ir_graph, block, left, right, pnc);
341
342                                 pos = get_edge_src_pos(edge);
343                                 set_irn_n(succ, pos, c);
344 //                              ir_printf("3 Replacing input %d of node %n with %n\n", pos, user, c);
345
346                                 env->num_confirms += 1;
347                         }
348                 }
349         }
350 }  /* handle_if */
351
352 /**
353  * Pre-walker: Called for every block to insert Confirm nodes
354  */
355 static void insert_Confirm(ir_node *block, void *env) {
356         ir_node *cond, *proj, *selector;
357         ir_mode *mode;
358
359         /*
360          * we can only handle blocks with only ONE control flow
361          * predecessor yet.
362          */
363         if (get_Block_n_cfgpreds(block) != 1)
364                 return;
365
366         proj = get_Block_cfgpred(block, 0);
367         if (! is_Proj(proj))
368                 return;
369
370         cond = get_Proj_pred(proj);
371         if (! is_Cond(cond))
372                 return;
373
374         selector = get_Cond_selector(cond);
375         mode = get_irn_mode(selector);
376
377         if (mode == mode_b) {
378                 ir_node *cmp;
379                 pn_Cmp pnc;
380
381                 handle_modeb(block, selector, get_Proj_proj(proj), env);
382
383                 /* this should be an IF, check this */
384                 if (! is_Proj(selector))
385                         return;
386
387                 cmp = get_Proj_pred(selector);
388                 if (! is_Cmp(cmp))
389                         return;
390
391                 pnc = get_Proj_proj(selector);
392
393                 if (get_Proj_proj(proj) != pn_Cond_true) {
394                         /* it's the false branch */
395                         pnc = get_negated_pnc(pnc, mode);
396                 }
397 //              ir_printf("At %n using %n Confirm %=\n", block, cmp, pnc);
398
399                 handle_if(block, cmp, pnc, env);
400         } else if (mode_is_int(mode)) {
401                 long proj_nr = get_Proj_proj(proj);
402
403                 /* this is a CASE, but we cannot handle the default case */
404                 if (proj_nr == get_Cond_defaultProj(cond))
405                         return;
406
407                 handle_case(block, get_Cond_selector(cond), proj_nr, env);
408         }
409 }  /* insert_Confirm */
410
411 /*
412  * Construct Confirm nodes
413  */
414 void construct_confirms(ir_graph *irg) {
415         env_t env;
416         int edges_active = edges_activated(irg);
417
418         remove_critical_cf_edges(irg);
419
420         /* we need dominance info */
421         assure_doms(irg);
422
423         assert(get_irg_pinned(irg) == op_pin_state_pinned &&
424                "Nodes must be placed to insert Confirms");
425
426         if (! edges_active) {
427                 /* We need edges */
428                 edges_activate(irg);
429         }
430
431         env.num_confirms = 0;
432         env.num_consts   = 0;
433         env.num_eq       = 0;
434
435         /* now, visit all blocks and add Confirms where possible */
436         irg_block_walk_graph(irg, insert_Confirm, NULL, &env);
437
438         if (env.num_confirms | env.num_consts | env.num_eq) {
439                 /* we have add nodes or changed DF edges */
440                 set_irg_outs_inconsistent(irg);
441
442                 /* the new nodes are not in the loop info */
443                 set_irg_loopinfo_inconsistent(irg);
444         }
445
446 #if 0
447         printf("# Confirms inserted : %u\n", env.num_confirms);
448         printf("# Const replacements: %u\n", env.num_consts);
449         printf("# node equalities   : %u\n", env.num_eq);
450 #endif
451
452         /* deactivate edges if they where off */
453         if (! edges_active)
454                 edges_deactivate(irg);
455 }  /* construct_confirms */
456
457 #if 0
458 /**
459  * Post-walker: Remove Confirm nodes
460  */
461 static void rem_Confirm(ir_node *n, void *env) {
462         (void) env;
463         if (is_Confirm(n)) {
464                 ir_node *value = get_Confirm_value(n);
465                 if (value != n)
466                         exchange(n, value);
467                 else {
468                         /*
469                          * Strange: a Confirm is it's own bound. This can happen
470                          * in dead blocks when Phi nodes are already removed.
471                          */
472                         exchange(n, new_Bad());
473                 }
474         }
475 }  /* rem_Confirm */
476 #endif
477
478 /*
479  * Remove all Confirm nodes from a graph.
480  */
481 void remove_confirms(ir_graph *irg) {
482         set_opt_remove_confirm(1);
483         optimize_graph_df(irg);
484 }  /* remove_confirms */