ec68c6217dc25991ed30a1f377e80a07610e2272
[libfirm] / ir / opt / boolopt.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   boolean condition/controlflow optimisations
23  * @author  Matthias Braun, Christoph Mallon
24  * @version $Id: cfopt.c 22579 2008-10-07 14:54:04Z beck $
25  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <assert.h>
31 #include <string.h>
32
33 #include "adt/obst.h"
34 #include "ircons.h"
35 #include "irgmod.h"
36 #include "irgwalk.h"
37 #include "irprintf.h"
38 #include "irnode_t.h"
39 #include "tv.h"
40
41 typedef struct cond_pair {
42         ir_node *cmp_lo;
43         ir_node *cmp_hi;
44         pn_Cmp   pnc_lo;
45         pn_Cmp   pnc_hi;
46         ir_node *proj_lo;
47         ir_node *proj_hi;
48         tarval  *tv_lo;
49         tarval  *tv_hi;
50 } cond_pair;
51
52 static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const res)
53 {
54         if (is_Proj(l) && is_Proj(r)) {
55                 ir_node *const lo = get_Proj_pred(l);
56                 ir_node *const ro = get_Proj_pred(r);
57
58                 if (is_Cmp(lo) && is_Cmp(ro)) {
59                         ir_node *const lol = get_Cmp_left(lo);
60                         ir_node *const lor = get_Cmp_right(lo);
61                         ir_node *const rol = get_Cmp_left(ro);
62                         ir_node *const ror = get_Cmp_right(ro);
63
64                         if(is_Const(lor) && is_Const_null(lor) && is_Const(ror) && is_Const_null(ror) && get_Proj_proj(l) == pn_Cmp_Lg && get_Proj_proj(r) == pn_Cmp_Lg) {
65                                 ir_fprintf(stderr, "found zero zero\n");
66                         }
67
68                         /* TODO float */
69                         /* The constants shall be unequal.  Local optimisations handle the
70                          * equal case */
71                         if (lol == rol && mode_is_int(get_irn_mode(lol)) && lor != ror && is_Const(lor) && is_Const(ror)) {
72                                 tarval *const tv_l  = get_Const_tarval(lor);
73                                 tarval *const tv_r  = get_Const_tarval(ror);
74                                 pn_Cmp  const pnc_l = get_Proj_proj(l);
75                                 pn_Cmp  const pnc_r = get_Proj_proj(r);
76                                 pn_Cmp  const rel   = tarval_cmp(tv_l, tv_r);
77
78                                 assert(rel != pn_Cmp_Eq);
79
80                                 if (rel == pn_Cmp_Lt) {
81                                         res->cmp_lo  = lo;
82                                         res->cmp_hi  = ro;
83                                         res->pnc_lo  = pnc_l;
84                                         res->pnc_hi  = pnc_r;
85                                         res->proj_lo = l;
86                                         res->proj_hi = r;
87                                         res->tv_lo   = tv_l;
88                                         res->tv_hi   = tv_r;
89                                 } else {
90                                         assert(rel == pn_Cmp_Gt);
91                                         res->cmp_lo  = ro;
92                                         res->cmp_hi  = lo;
93                                         res->pnc_lo  = pnc_r;
94                                         res->pnc_hi  = pnc_l;
95                                         res->proj_lo = r;
96                                         res->proj_hi = l;
97                                         res->tv_lo   = tv_r;
98                                         res->tv_hi   = tv_l;
99                                 }
100                                 return 1;
101                         }
102                 }
103         }
104         return 0;
105 }
106
107 static ir_node *bool_and(cond_pair* const cpair)
108 {
109         ir_node *const cmp_lo  = cpair->cmp_lo;
110         ir_node *const cmp_hi  = cpair->cmp_hi;
111         pn_Cmp   const pnc_lo  = cpair->pnc_lo;
112         pn_Cmp   const pnc_hi  = cpair->pnc_hi;
113         ir_node *const proj_lo = cpair->proj_lo;
114         ir_node *const proj_hi = cpair->proj_hi;
115         tarval  *const tv_lo   = cpair->tv_lo;
116         tarval  *const tv_hi   = cpair->tv_hi;
117
118         /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
119         if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
120                         (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
121                 /* x <|<=|== lo | x ==|>=|> hi -> false */
122                 ir_node *const t = new_Const(mode_b, tarval_b_false);
123                 return t;
124         } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
125                                                  (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
126                 /* x <|<=|== lo && x <|<=|!= hi -> x <|<=|== lo */
127                 return proj_lo;
128         } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
129                                                  (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
130                 /* x >=|>|!= lo || x ==|>=|> hi -> x ==|>=|> hi */
131                 return proj_hi;
132         } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
133                 if (pnc_lo == pn_Cmp_Ge && pnc_hi == pn_Cmp_Lt) {
134                         /* x >= c || x < c + 1 -> x == c */
135                         ir_graph *const irg   = current_ir_graph;
136                         ir_node  *const block = get_nodes_block(cmp_lo);
137                         ir_node  *const p = new_r_Proj(irg, block, cmp_lo, mode_b, pn_Cmp_Eq);
138                         return p;
139                 } else if (pnc_lo == pn_Cmp_Gt) {
140                         if (pnc_hi == pn_Cmp_Lg) {
141                                 /* x > c || x != c + 1 -> x > c + 1 */
142                                 ir_graph *const irg   = current_ir_graph;
143                                 ir_node  *const block = get_nodes_block(cmp_hi);
144                                 ir_node  *const p = new_r_Proj(irg, block, cmp_hi, mode_b, pn_Cmp_Gt);
145                                 return p;
146                         } else if (pnc_hi == pn_Cmp_Lt) {
147                                 /* x > c || x < c + 1 -> false */
148                                 ir_node *const t = new_Const(mode_b, tarval_b_false);
149                                 return t;
150                         } else if (pnc_hi == pn_Cmp_Le) {
151                                 /* x > c || x <= c + 1 -> x != c + 1 */
152                                 ir_graph *const irg   = current_ir_graph;
153                                 ir_node  *const block = get_nodes_block(cmp_hi);
154                                 ir_node  *const p = new_r_Proj(irg, block, cmp_hi, mode_b, pn_Cmp_Eq);
155                                 return p;
156                         }
157                 } else if (pnc_lo == pn_Cmp_Lg && pnc_hi == pn_Cmp_Lt) {
158                         /* x != c || c < c + 1 -> x < c */
159                         ir_graph *const irg   = current_ir_graph;
160                         ir_node  *const block = get_nodes_block(cmp_lo);
161                         ir_node  *const p     = new_r_Proj(irg, block, cmp_lo, mode_b, pn_Cmp_Lt);
162                         return p;
163                 }
164         }
165         return NULL;
166 }
167
168 static ir_node *bool_or(cond_pair *const cpair)
169 {
170         ir_node *const cmp_lo  = cpair->cmp_lo;
171         ir_node *const cmp_hi  = cpair->cmp_hi;
172         pn_Cmp   const pnc_lo  = cpair->pnc_lo;
173         pn_Cmp   const pnc_hi  = cpair->pnc_hi;
174         ir_node *const proj_lo = cpair->proj_lo;
175         ir_node *const proj_hi = cpair->proj_hi;
176         tarval  *const tv_lo   = cpair->tv_lo;
177         tarval  *const tv_hi   = cpair->tv_hi;
178
179         /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
180         if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
181                         (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
182                 /* x >=|>|!= lo | x <|<=|!= hi -> true */
183                 ir_node *const t = new_Const(mode_b, tarval_b_true);
184                 return t;
185         } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
186                                                  (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
187                 /* x <|<=|== lo || x <|<=|!= hi -> x <|<=|!= hi */
188                 return proj_hi;
189         } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
190                                                  (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
191                 /* x >=|>|!= lo || x ==|>=|> hi -> x >=|>|!= lo */
192                 return proj_lo;
193         } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
194                 if (pnc_lo == pn_Cmp_Lt && pnc_hi == pn_Cmp_Ge) {
195                         /* x < c || x >= c + 1 -> x != c */
196                         ir_graph *const irg   = current_ir_graph;
197                         ir_node  *const block = get_nodes_block(cmp_lo);
198                         ir_node  *const p = new_r_Proj(irg, block, cmp_lo, mode_b, pn_Cmp_Lg);
199                         return p;
200                 } else if (pnc_lo == pn_Cmp_Le) {
201                         if (pnc_hi == pn_Cmp_Eq) {
202                                 /* x <= c || x == c + 1 -> x <= c + 1 */
203                                 ir_graph *const irg   = current_ir_graph;
204                                 ir_node  *const block = get_nodes_block(cmp_hi);
205                                 ir_node  *const p = new_r_Proj(irg, block, cmp_hi, mode_b, pn_Cmp_Le);
206                                 return p;
207                         } else if (pnc_hi == pn_Cmp_Ge) {
208                                 /* x <= c || x >= c + 1 -> true */
209                                 ir_node *const t = new_Const(mode_b, tarval_b_true);
210                                 return t;
211                         } else if (pnc_hi == pn_Cmp_Gt) {
212                                 /* x <= c || x > c + 1 -> x != c + 1 */
213                                 ir_graph *const irg   = current_ir_graph;
214                                 ir_node  *const block = get_nodes_block(cmp_hi);
215                                 ir_node  *const p = new_r_Proj(irg, block, cmp_hi, mode_b, pn_Cmp_Lg);
216                                 return p;
217                         }
218                 } else if (pnc_lo == pn_Cmp_Eq && pnc_hi == pn_Cmp_Ge) {
219                         /* x == c || x >= c + 1 -> x >= c */
220                         ir_graph *const irg   = current_ir_graph;
221                         ir_node  *const block = get_nodes_block(cmp_lo);
222                         ir_node  *const p     = new_r_Proj(irg, block, cmp_lo, mode_b, pn_Cmp_Ge);
223                         return p;
224                 }
225         }
226         return NULL;
227 }
228
229 static void bool_walk(ir_node *n, void *env)
230 {
231         (void)env;
232
233         if (get_irn_mode(n) != mode_b)
234                 return;
235
236         if (is_And(n)) {
237                 ir_node *const l = get_And_left(n);
238                 ir_node *const r = get_And_right(n);
239                 ir_node *      replacement;
240                 cond_pair      cpair;
241                 if (!find_cond_pair(l, r, &cpair))
242                         return;
243                 replacement = bool_and(&cpair);
244                 if (replacement)
245                         exchange(n, replacement);
246         } else if (is_Or(n)) {
247                 ir_node *const l = get_Or_left(n);
248                 ir_node *const r = get_Or_right(n);
249                 ir_node *      replacement;
250                 cond_pair      cpair;
251                 if (!find_cond_pair(l, r, &cpair))
252                         return;
253                 replacement = bool_or(&cpair);
254                 if (replacement)
255                         exchange(n, replacement);
256         }
257 }
258
259 /**
260  * Walker, clear Block mark and Phi list
261  */
262 static void clear_block_infos(ir_node *node, void *env)
263 {
264         (void) env;
265
266         /* we visit blocks before any other nodes (from the block) */
267         if (!is_Block(node))
268                 return;
269
270         /* clear the PHI list */
271         set_Block_phis(node, NULL);
272         set_Block_mark(node, 0);
273 }
274
275 /**
276  * Walker: collect Phi nodes and update the
277  */
278 static void collect_phis(ir_node *node, void *env)
279 {
280         (void) env;
281
282         if (is_Phi(node)) {
283                 ir_node *block = get_nodes_block(node);
284                 add_Block_phi(block, node);
285                 return;
286         }
287
288         /* Ignore control flow nodes, these will be removed. */
289         if (get_irn_pinned(node) == op_pin_state_pinned &&
290                         !is_Block(node) && !is_cfop(node)) {
291                 ir_node *block = get_nodes_block(node);
292                 set_Block_mark(block, 1);
293         }
294 }
295
296 /**
297  * If node is a Jmp in a block containing no pinned instruction
298  * and having only one predecessor, skip the block and return its
299  * cf predecessor, else the node itself.
300  */
301 static ir_node *skip_empty_block(ir_node *node)
302 {
303         ir_node      *block;
304
305         if(!is_Jmp(node))
306                 return node;
307
308         block = get_nodes_block(node);
309         if(get_Block_n_cfgpreds(block) != 1)
310                 return node;
311
312         if(get_Block_mark(block))
313                 return node;
314
315         return get_Block_cfgpred(block, 0);
316 }
317
318 static void find_cf_and_or_walker(ir_node *block, void *env)
319 {
320         int i, i2;
321         int n_cfgpreds = get_Block_n_cfgpreds(block);
322         (void) env;
323
324         if(n_cfgpreds < 2)
325                 return;
326
327         /* Find the following structure:
328          *
329          *        upper_block
330          *         /       |
331          *        /        |
332          *   lower_block   |
333          *     /  \        |
334          *   ...   \       |
335          *           block
336          */
337
338 restart:
339         for(i = 0; i < n_cfgpreds; ++i) {
340                 ir_node      *lower_block;
341                 ir_node      *lower_cf;
342                 ir_node      *cond;
343                 ir_node      *cond_selector;
344                 ir_node      *lower_pred;
345
346                 lower_cf = get_Block_cfgpred(block, i);
347                 lower_cf = skip_empty_block(lower_cf);
348                 if(!is_Proj(lower_cf))
349                         continue;
350
351                 cond = get_Proj_pred(lower_cf);
352                 if(!is_Cond(cond))
353                         continue;
354
355                 lower_block = get_nodes_block(cond);
356                 if(get_Block_n_cfgpreds(lower_block) != 1)
357                         continue;
358
359                 /* the block must not produce any side-effects */
360                 if(get_Block_mark(lower_block))
361                         continue;
362
363                 cond_selector = get_Cond_selector(cond);
364                 if(get_irn_mode(cond_selector) != mode_b)
365                         continue;
366
367                 lower_pred = get_Block_cfgpred_block(lower_block, 0);
368
369                 for(i2 = 0; i2 < n_cfgpreds; ++i2) {
370                         ir_node   *upper_block;
371                         ir_node   *upper_cf;
372                         ir_node   *upper_cond;
373                         ir_node   *upper_cond_selector;
374                         ir_node   *replacement;
375                         ir_graph  *irg;
376                         cond_pair  cpair;
377
378                         upper_cf    = get_Block_cfgpred(block, i2);
379                         upper_cf    = skip_empty_block(upper_cf);
380                         if(is_Bad(upper_cf))
381                                 continue;
382                         upper_block = get_nodes_block(upper_cf);
383                         if(upper_block != lower_pred)
384                                 continue;
385
386                         assert(is_Proj(upper_cf));
387                         upper_cond = get_Proj_pred(upper_cf);
388                         assert(is_Cond(upper_cond));
389                         upper_cond_selector = get_Cond_selector(upper_cond);
390                         if(get_irn_mode(upper_cond_selector) != mode_b)
391                                 continue;
392
393                         /* we have found the structure */
394                         /* TODO: check phis */
395                         if(!find_cond_pair(cond_selector, upper_cond_selector, &cpair))
396                                 continue;
397
398                         /* normalize pncs: we need the true case to jump into the
399                          * common block (ie. conjunctive normal form) */
400                         irg = current_ir_graph;
401                         if(get_Proj_proj(lower_cf) == pn_Cond_false) {
402                                 if(cpair.proj_lo == cond_selector) {
403                                         ir_mode *mode = get_tarval_mode(cpair.tv_lo);
404                                         cpair.pnc_lo  = get_negated_pnc(cpair.pnc_lo, mode);
405                                         cpair.proj_lo = new_r_Proj(irg, lower_block,
406                                                         get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
407                                 } else {
408                                         ir_mode *mode = get_tarval_mode(cpair.tv_hi);
409                                         assert(cpair.proj_hi == cond_selector);
410                                         cpair.pnc_hi  = get_negated_pnc(cpair.pnc_hi, mode);
411                                         cpair.proj_hi = new_r_Proj(irg, lower_block,
412                                                         get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
413                                 }
414                         }
415                         if(get_Proj_proj(upper_cf) == pn_Cond_false) {
416                                 if(cpair.proj_lo == upper_cond_selector) {
417                                         ir_mode *mode = get_tarval_mode(cpair.tv_lo);
418                                         cpair.pnc_lo  = get_negated_pnc(cpair.pnc_lo, mode);
419                                         cpair.proj_lo = new_r_Proj(irg, upper_block,
420                                                         get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
421                                 } else {
422                                         ir_mode *mode = get_tarval_mode(cpair.tv_hi);
423                                         assert(cpair.proj_hi == upper_cond_selector);
424                                         cpair.pnc_hi  = get_negated_pnc(cpair.pnc_hi, mode);
425                                         cpair.proj_hi = new_r_Proj(irg, upper_block,
426                                                         get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
427                                 }
428                         }
429
430                         /* can we optimize the case? */
431                         replacement = bool_or(&cpair);
432                         if(replacement == NULL)
433                                 continue;
434
435                         /* move all nodes from lower block to upper block */
436                         exchange(lower_block, upper_block);
437
438                         set_Block_cfgpred(block, i2, new_Bad());
439
440                         /* the optimisations expected the true case to jump */
441                         if(get_Proj_proj(lower_cf) == pn_Cond_false) {
442                                 ir_node *block = get_nodes_block(replacement);
443                                 replacement    = new_rd_Not(NULL, current_ir_graph, block,
444                                                             replacement, mode_b);
445                         }
446                         set_Cond_selector(cond, replacement);
447
448                         ir_fprintf(stderr, "replaced (ub %+F)\n", upper_block);
449                         goto restart;
450                 }
451         }
452 }
453
454 void opt_bool(ir_graph *const irg)
455 {
456         irg_walk_graph(irg, NULL, bool_walk, NULL);
457
458         ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK);
459
460         irg_walk_graph(irg, clear_block_infos, collect_phis, NULL);
461
462         irg_block_walk_graph(irg, NULL, find_cf_and_or_walker, NULL);
463
464         set_irg_outs_inconsistent(irg);
465         set_irg_doms_inconsistent(irg);
466         set_irg_extblk_inconsistent(irg);
467         set_irg_loopinfo_inconsistent(irg);
468
469         ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK);
470 }