2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief boolean condition/controlflow optimisations
23 * @author Matthias Braun, Christoph Mallon, Michael Beck
24 * @version $Id: cfopt.c 22579 2008-10-07 14:54:04Z beck $
32 #include "../adt/array_t.h"
33 #include "iroptimize.h"
43 /** Describes a pair of relative conditions lo < hi, lo pnc_lo x, hi pnc_hi x */
44 typedef struct cond_pair {
45 ir_node *cmp_lo; /**< the lo compare node. */
46 ir_node *cmp_hi; /**< the hi compare node. */
47 pn_Cmp pnc_lo; /**< the lo relation node. */
48 pn_Cmp pnc_hi; /**< the hi relation node. */
49 ir_node *proj_lo; /**< the mode_b result proj of cmp_lo */
50 ir_node *proj_hi; /**< the mode_b result proj of cmp_hi */
51 tarval *tv_lo; /**< the tarval of lo */
52 tarval *tv_hi; /**< the tarval of hi */
55 /** Environment for all walker in boolopt. */
57 int changed; /**< Set if the graph was changed. */
60 DEBUG_ONLY(static firm_dbg_module_t *dbg);
63 * Check if tho given nodes, l and r, represent two compares with
64 * ... . If yes, return non-zero and fill the res struct.
66 static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const res)
68 if (is_Proj(l) && is_Proj(r)) {
69 ir_node *const lo = get_Proj_pred(l);
70 ir_node *const ro = get_Proj_pred(r);
72 if (is_Cmp(lo) && is_Cmp(ro)) {
73 ir_node *const lol = get_Cmp_left(lo);
74 ir_node *const lor = get_Cmp_right(lo);
75 ir_node *const rol = get_Cmp_left(ro);
76 ir_node *const ror = get_Cmp_right(ro);
78 if (is_Const(lor) && is_Const_null(lor) &&
79 is_Const(ror) && is_Const_null(ror) &&
80 get_Proj_proj(l) == pn_Cmp_Lg &&
81 get_Proj_proj(r) == pn_Cmp_Lg) {
82 /* lo == (lol != NULL) && ro == (rol != NULL) */
84 DB((dbg, LEVEL_1, "found <null null>\n"));
88 /* The constants shall be unequal. Local optimisations handle the
90 if (lol == rol && mode_is_int(get_irn_mode(lol)) && lor != ror && is_Const(lor) && is_Const(ror)) {
91 /* lo == (x CMP c_l), ro == (x cmp c_r), c_l != c_r */
92 tarval *const tv_l = get_Const_tarval(lor);
93 tarval *const tv_r = get_Const_tarval(ror);
94 pn_Cmp const pnc_l = get_Proj_proj(l);
95 pn_Cmp const pnc_r = get_Proj_proj(r);
96 pn_Cmp const rel = tarval_cmp(tv_l, tv_r);
98 assert(rel != pn_Cmp_Eq);
100 if (rel == pn_Cmp_Lt) {
111 assert(rel == pn_Cmp_Gt);
130 * Handle (lo pnc_lo x) AND (hi pnc_hi x)
132 static ir_node *bool_and(cond_pair* const cpair)
134 ir_node *const cmp_lo = cpair->cmp_lo;
135 ir_node *const cmp_hi = cpair->cmp_hi;
136 pn_Cmp const pnc_lo = cpair->pnc_lo;
137 pn_Cmp const pnc_hi = cpair->pnc_hi;
138 ir_node *const proj_lo = cpair->proj_lo;
139 ir_node *const proj_hi = cpair->proj_hi;
140 tarval *const tv_lo = cpair->tv_lo;
141 tarval *const tv_hi = cpair->tv_hi;
143 /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
144 if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
145 (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
146 /* x <|<=|== lo | x ==|>=|> hi ==> false */
147 ir_node *const t = new_Const(tarval_b_false);
149 } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
150 (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
151 /* x <|<=|== lo && x <|<=|!= hi ==> x <|<=|== lo */
153 } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
154 (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
155 /* x >=|>|!= lo || x ==|>=|> hi ==> x ==|>=|> hi */
157 } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
158 if (pnc_lo == pn_Cmp_Ge && pnc_hi == pn_Cmp_Lt) {
159 /* x >= c || x < c + 1 ==> x == c */
160 ir_node *const block = get_nodes_block(cmp_lo);
161 ir_node *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Eq);
163 } else if (pnc_lo == pn_Cmp_Gt) {
164 if (pnc_hi == pn_Cmp_Lg) {
165 /* x > c || x != c + 1 ==> x > c + 1 */
166 ir_node *const block = get_nodes_block(cmp_hi);
167 ir_node *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Gt);
169 } else if (pnc_hi == pn_Cmp_Lt) {
170 /* x > c || x < c + 1 ==> false */
171 ir_node *const t = new_Const(tarval_b_false);
173 } else if (pnc_hi == pn_Cmp_Le) {
174 /* x > c || x <= c + 1 ==> x != c + 1 */
175 ir_node *const block = get_nodes_block(cmp_hi);
176 ir_node *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Eq);
179 } else if (pnc_lo == pn_Cmp_Lg && pnc_hi == pn_Cmp_Lt) {
180 /* x != c || c < c + 1 ==> x < c */
181 ir_node *const block = get_nodes_block(cmp_lo);
182 ir_node *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lt);
190 * Handle (lo pnc_lo x) OR (hi pnc_hi x)
192 static ir_node *bool_or(cond_pair *const cpair)
194 ir_node *const cmp_lo = cpair->cmp_lo;
195 ir_node *const cmp_hi = cpair->cmp_hi;
196 pn_Cmp pnc_lo = cpair->pnc_lo;
197 pn_Cmp const pnc_hi = cpair->pnc_hi;
198 ir_node *const proj_lo = cpair->proj_lo;
199 ir_node *const proj_hi = cpair->proj_hi;
200 tarval * tv_lo = cpair->tv_lo;
201 tarval * tv_hi = cpair->tv_hi;
203 /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
204 if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
205 (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
206 /* x >=|>|!= lo | x <|<=|!= hi ==> true */
207 ir_node *const t = new_Const(tarval_b_true);
209 } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
210 (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
211 /* x <|<=|== lo || x <|<=|!= hi ==> x <|<=|!= hi */
213 } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
214 (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
215 /* x >=|>|!= lo || x ==|>=|> hi ==> x >=|>|!= lo */
217 } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
218 if (pnc_lo == pn_Cmp_Lt && pnc_hi == pn_Cmp_Ge) {
219 /* x < c || x >= c + 1 ==> x != c */
220 ir_node *const block = get_nodes_block(cmp_lo);
221 ir_node *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lg);
223 } else if (pnc_lo == pn_Cmp_Le) {
224 if (pnc_hi == pn_Cmp_Eq) {
225 /* x <= c || x == c + 1 ==> x <= c + 1 */
226 ir_node *const block = get_nodes_block(cmp_hi);
227 ir_node *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Le);
229 } else if (pnc_hi == pn_Cmp_Ge) {
230 /* x <= c || x >= c + 1 ==> true */
231 ir_node *const t = new_Const(tarval_b_true);
233 } else if (pnc_hi == pn_Cmp_Gt) {
234 /* x <= c || x > c + 1 ==> x != c + 1 */
235 ir_node *const block = get_nodes_block(cmp_hi);
236 ir_node *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Lg);
239 } else if (pnc_lo == pn_Cmp_Eq && pnc_hi == pn_Cmp_Ge) {
240 /* x == c || x >= c + 1 ==> x >= c */
241 ir_node *const block = get_nodes_block(cmp_lo);
242 ir_node *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Ge);
245 } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le) &&
246 (pnc_hi == pn_Cmp_Gt || pnc_lo == pn_Cmp_Ge)) {
247 /* works for two-complements only */
248 /* x <|\= lo || x >|>= hi ==> (x - lo) >u|>=u (hi-lo) */
249 if (pnc_lo == pn_Cmp_Lt) {
250 /* must convert to <= */
251 ir_mode *mode = get_tarval_mode(tv_lo);
252 tarval *n = tarval_sub(tv_lo, get_mode_one(mode), NULL);
253 if (n != tarval_bad && tarval_cmp(n, tv_lo) == pn_Cmp_Lt) {
259 if (pnc_lo == pn_Cmp_Le) {
261 ir_node *const block = get_nodes_block(cmp_hi);
262 ir_node * x = get_Cmp_left(cmp_hi);
263 ir_mode * mode = get_irn_mode(x);
264 ir_node *sub, *cmp, *c, *subc, *p;
266 if (mode_is_signed(mode)) {
267 /* convert to unsigned */
268 mode = find_unsigned_mode(mode);
271 x = new_r_Conv(block, x, mode);
272 tv_lo = tarval_convert_to(tv_lo, mode);
273 tv_hi = tarval_convert_to(tv_hi, mode);
274 if (tv_lo == tarval_bad || tv_hi == tarval_bad)
277 c = new_Const(tv_lo);
278 sub = new_r_Sub(block, x, c, mode);
279 subc = new_r_Sub(block, new_Const(tv_hi), c, mode);
280 cmp = new_r_Cmp(block, sub, subc);
281 p = new_r_Proj(block, cmp, mode_b, pnc_hi);
289 * Walker, tries to optimize Andb and Orb nodes.
291 static void bool_walk(ir_node *n, void *ctx)
293 bool_opt_env_t *env = ctx;
295 if (get_irn_mode(n) != mode_b)
299 ir_node *const l = get_And_left(n);
300 ir_node *const r = get_And_right(n);
301 ir_node * replacement;
303 if (!find_cond_pair(l, r, &cpair))
305 replacement = bool_and(&cpair);
307 exchange(n, replacement);
310 } else if (is_Or(n)) {
311 ir_node *const l = get_Or_left(n);
312 ir_node *const r = get_Or_right(n);
313 ir_node * replacement;
315 if (!find_cond_pair(l, r, &cpair))
317 replacement = bool_or(&cpair);
319 exchange(n, replacement);
326 * Walker, clear Block marker and Phi lists.
328 static void clear_block_infos(ir_node *node, void *env)
332 /* we visit blocks before any other nodes (from the block) */
336 /* clear the PHI list */
337 set_Block_phis(node, NULL);
338 set_Block_mark(node, 0);
342 * Walker: collect Phi nodes and mark
344 static void collect_phis(ir_node *node, void *env)
349 ir_node *block = get_nodes_block(node);
350 add_Block_phi(block, node);
354 /* Ignore control flow nodes, these will be removed. */
355 if (get_irn_pinned(node) == op_pin_state_pinned &&
356 !is_Block(node) && !is_cfop(node)) {
357 /* found a pinned non-cf node, mark its block */
358 ir_node *block = get_nodes_block(node);
359 set_Block_mark(block, 1);
364 * If node is a Jmp in a block containing no pinned instruction
365 * and having only one predecessor, skip the block and return its
366 * cf predecessor, else the node itself.
368 static ir_node *skip_empty_blocks(ir_node *node)
370 while (is_Jmp(node)) {
371 ir_node *block = get_nodes_block(node);
373 if (get_Block_n_cfgpreds(block) != 1)
376 if (get_Block_mark(block))
379 node = get_Block_cfgpred(block, 0);
385 * Check if two block inputs can be fused.
386 * This can be done, if block contains no Phi node that depends on
387 * different inputs idx_i and idx_j.
389 static int can_fuse_block_inputs(const ir_node *block, int idx_i, int idx_j) {
392 for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
393 if (get_Phi_pred(phi, idx_i) != get_Phi_pred(phi, idx_j))
400 * Remove block input with given index.
402 static void remove_block_input(ir_node *block, int idx)
404 int i, j, n = get_Block_n_cfgpreds(block) - 1;
407 NEW_ARR_A(ir_node *, ins, n);
410 /* all Phis will be deleted */
413 for (phi = get_Block_phis(block); phi != NULL; phi = next_phi) {
414 next_phi = get_Phi_next(phi);
415 exchange(phi, get_Phi_pred(phi, idx ^ 1));
417 set_Block_phis(block, NULL);
419 for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
420 for (i = j = 0; i <= n; ++i) {
422 ins[j++] = get_Phi_pred(phi, i);
424 set_irn_in(phi, n, ins);
427 for (i = j = 0; i <= n; ++i) {
429 ins[j++] = get_Block_cfgpred(block, i);
431 set_irn_in(block, n, ins);
435 * Under the preposition that we have a chain of blocks from
436 * from_block to to_block, collapse them all into to_block.
438 static void move_nodes_to_block(ir_node *jmp, ir_node *to_block) {
439 ir_node *new_jmp = NULL;
440 ir_node *block, *next_block;
442 for (block = get_nodes_block(jmp); block != to_block; block = next_block) {
443 new_jmp = get_Block_cfgpred(block, 0);
444 next_block = get_nodes_block(new_jmp);
445 exchange(block, to_block);
448 exchange(jmp, new_jmp);
454 * if we can find the following structure,
465 * try to convert it into a (x pnc_lo c_lo) || (x pnc_hi c_hi)
468 static void find_cf_and_or_walker(ir_node *block, void *ctx)
471 int n_cfgpreds = get_Block_n_cfgpreds(block);
472 bool_opt_env_t *env = ctx;
478 for (low_idx = 0; low_idx < n_cfgpreds; ++low_idx) {
479 ir_node *lower_block;
482 ir_node *cond_selector;
485 lower_cf = get_Block_cfgpred(block, low_idx);
486 lower_cf = skip_empty_blocks(lower_cf);
487 if (!is_Proj(lower_cf))
490 cond = get_Proj_pred(lower_cf);
494 lower_block = get_nodes_block(cond);
495 if (get_Block_n_cfgpreds(lower_block) != 1)
498 /* the block must not produce any side-effects */
499 if (get_Block_mark(lower_block))
502 cond_selector = get_Cond_selector(cond);
503 if (get_irn_mode(cond_selector) != mode_b)
506 lower_pred = get_Block_cfgpred_block(lower_block, 0);
508 for (up_idx = 0; up_idx < n_cfgpreds; ++up_idx) {
509 ir_node *upper_block;
512 ir_node *upper_cond_selector;
513 ir_node *replacement;
516 upper_cf = get_Block_cfgpred(block, up_idx);
517 upper_cf = skip_empty_blocks(upper_cf);
518 if (is_Bad(upper_cf))
520 upper_block = get_nodes_block(upper_cf);
521 if (upper_block != lower_pred)
524 assert(is_Proj(upper_cf));
525 upper_cond = get_Proj_pred(upper_cf);
526 assert(is_Cond(upper_cond));
527 upper_cond_selector = get_Cond_selector(upper_cond);
528 if (get_irn_mode(upper_cond_selector) != mode_b)
531 /* we have found the structure */
532 /* check Phis: There must be NO Phi in block that
533 depends on the existence of low block */
534 if (!can_fuse_block_inputs(block, low_idx, up_idx))
537 /* all fine, try it */
538 if (!find_cond_pair(cond_selector, upper_cond_selector, &cpair))
541 /* normalize pncs: we need the true case to jump into the
542 * common block (ie. conjunctive normal form) */
543 if (get_Proj_proj(lower_cf) == pn_Cond_false) {
544 if (cpair.proj_lo == cond_selector) {
545 ir_mode *mode = get_tarval_mode(cpair.tv_lo);
546 cpair.pnc_lo = get_negated_pnc(cpair.pnc_lo, mode);
547 cpair.proj_lo = new_r_Proj(lower_block,
548 get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
550 ir_mode *mode = get_tarval_mode(cpair.tv_hi);
551 assert(cpair.proj_hi == cond_selector);
552 cpair.pnc_hi = get_negated_pnc(cpair.pnc_hi, mode);
553 cpair.proj_hi = new_r_Proj(lower_block,
554 get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
557 if (get_Proj_proj(upper_cf) == pn_Cond_false) {
558 if (cpair.proj_lo == upper_cond_selector) {
559 ir_mode *mode = get_tarval_mode(cpair.tv_lo);
560 cpair.pnc_lo = get_negated_pnc(cpair.pnc_lo, mode);
561 cpair.proj_lo = new_r_Proj(upper_block,
562 get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
564 ir_mode *mode = get_tarval_mode(cpair.tv_hi);
565 assert(cpair.proj_hi == upper_cond_selector);
566 cpair.pnc_hi = get_negated_pnc(cpair.pnc_hi, mode);
567 cpair.proj_hi = new_r_Proj(upper_block,
568 get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
572 /* can we optimize the case? */
573 replacement = bool_or(&cpair);
574 if (replacement == NULL)
579 /* move all expressions on the path to lower/upper block */
580 move_nodes_to_block(get_Block_cfgpred(block, up_idx), upper_block);
581 move_nodes_to_block(get_Block_cfgpred(block, low_idx), lower_block);
583 /* move all nodes from lower block to upper block */
584 exchange(lower_block, upper_block);
586 remove_block_input(block, up_idx);
589 /* the optimisations expected the true case to jump */
590 if (get_Proj_proj(lower_cf) == pn_Cond_false) {
591 ir_node *block = get_nodes_block(replacement);
592 replacement = new_rd_Not(NULL, block, replacement, mode_b);
594 set_Cond_selector(cond, replacement);
596 DB((dbg, LEVEL_1, "%+F: replaced (ub %+F)\n", current_ir_graph, upper_block));
602 void opt_bool(ir_graph *const irg)
606 /* register a debug mask */
607 FIRM_DBG_REGISTER(dbg, "firm.opt.bool");
609 /* works better with one return block only */
610 normalize_one_return(irg);
614 /* optimize simple Andb and Orb cases */
615 irg_walk_graph(irg, NULL, bool_walk, &env);
617 ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
619 /* now more complicated cases: find control flow And/Or and optimize. */
620 irg_walk_graph(irg, clear_block_infos, collect_phis, NULL);
621 irg_block_walk_graph(irg, NULL, find_cf_and_or_walker, &env);
624 set_irg_outs_inconsistent(irg);
625 set_irg_doms_inconsistent(irg);
626 set_irg_extblk_inconsistent(irg);
627 set_irg_loopinfo_inconsistent(irg);
630 ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
633 /* Creates an ir_graph pass for opt_bool. */
634 ir_graph_pass_t *opt_bool_pass(const char *name)
636 return def_graph_pass(name ? name : "opt_bool", opt_bool);
637 } /* opt_bool_pass */