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