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