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"
42 /** Describes a pair of relative conditions lo < hi, lo pnc_lo x, hi pnc_hi x */
43 typedef struct cond_pair {
44 ir_node *cmp_lo; /**< the lo compare node. */
45 ir_node *cmp_hi; /**< the hi compare node. */
46 pn_Cmp pnc_lo; /**< the lo relation node. */
47 pn_Cmp pnc_hi; /**< the hi relation node. */
48 ir_node *proj_lo; /**< the mode_b result proj of cmp_lo */
49 ir_node *proj_hi; /**< the mode_b result proj of cmp_hi */
50 tarval *tv_lo; /**< the tarval of lo */
51 tarval *tv_hi; /**< the tarval of hi */
54 /** Environment for all walker in boolopt. */
56 int changed; /**< Set if the graph was changed. */
60 * Check if tho given nodes, l and r, represent two compares with
61 * ... . If yes, return non-zero and fill the res struct.
63 static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const res)
65 if (is_Proj(l) && is_Proj(r)) {
66 ir_node *const lo = get_Proj_pred(l);
67 ir_node *const ro = get_Proj_pred(r);
69 if (is_Cmp(lo) && is_Cmp(ro)) {
70 ir_node *const lol = get_Cmp_left(lo);
71 ir_node *const lor = get_Cmp_right(lo);
72 ir_node *const rol = get_Cmp_left(ro);
73 ir_node *const ror = get_Cmp_right(ro);
75 if (is_Const(lor) && is_Const_null(lor) &&
76 is_Const(ror) && is_Const_null(ror) &&
77 get_Proj_proj(l) == pn_Cmp_Lg &&
78 get_Proj_proj(r) == pn_Cmp_Lg) {
79 /* lo == (lol != NULL) && ro == (rol != NULL) */
81 /* TODO: found <null null> */
85 /* The constants shall be unequal. Local optimisations handle the
87 if (lol == rol && mode_is_int(get_irn_mode(lol)) && lor != ror && is_Const(lor) && is_Const(ror)) {
88 /* lo == (x CMP c_l), ro == (x cmp c_r), c_l != c_r */
89 tarval *const tv_l = get_Const_tarval(lor);
90 tarval *const tv_r = get_Const_tarval(ror);
91 pn_Cmp const pnc_l = get_Proj_proj(l);
92 pn_Cmp const pnc_r = get_Proj_proj(r);
93 pn_Cmp const rel = tarval_cmp(tv_l, tv_r);
95 assert(rel != pn_Cmp_Eq);
97 if (rel == pn_Cmp_Lt) {
108 assert(rel == pn_Cmp_Gt);
127 * Handle (lo pnc_lo x) AND (hi pnc_hi x)
129 static ir_node *bool_and(cond_pair* const cpair)
131 ir_node *const cmp_lo = cpair->cmp_lo;
132 ir_node *const cmp_hi = cpair->cmp_hi;
133 pn_Cmp const pnc_lo = cpair->pnc_lo;
134 pn_Cmp const pnc_hi = cpair->pnc_hi;
135 ir_node *const proj_lo = cpair->proj_lo;
136 ir_node *const proj_hi = cpair->proj_hi;
137 tarval *const tv_lo = cpair->tv_lo;
138 tarval *const tv_hi = cpair->tv_hi;
140 /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
141 if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
142 (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
143 /* x <|<=|== lo | x ==|>=|> hi -> false */
144 ir_node *const t = new_Const(tarval_b_false);
146 } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
147 (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
148 /* x <|<=|== lo && x <|<=|!= hi -> x <|<=|== lo */
150 } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
151 (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
152 /* x >=|>|!= lo || x ==|>=|> hi -> x ==|>=|> hi */
154 } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
155 if (pnc_lo == pn_Cmp_Ge && pnc_hi == pn_Cmp_Lt) {
156 /* x >= c || x < c + 1 -> x == c */
157 ir_node *const block = get_nodes_block(cmp_lo);
158 ir_node *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Eq);
160 } else if (pnc_lo == pn_Cmp_Gt) {
161 if (pnc_hi == pn_Cmp_Lg) {
162 /* x > c || x != c + 1 -> x > c + 1 */
163 ir_node *const block = get_nodes_block(cmp_hi);
164 ir_node *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Gt);
166 } else if (pnc_hi == pn_Cmp_Lt) {
167 /* x > c || x < c + 1 -> false */
168 ir_node *const t = new_Const(tarval_b_false);
170 } else if (pnc_hi == pn_Cmp_Le) {
171 /* x > c || x <= c + 1 -> x != c + 1 */
172 ir_node *const block = get_nodes_block(cmp_hi);
173 ir_node *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Eq);
176 } else if (pnc_lo == pn_Cmp_Lg && pnc_hi == pn_Cmp_Lt) {
177 /* x != c || c < c + 1 -> x < c */
178 ir_node *const block = get_nodes_block(cmp_lo);
179 ir_node *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lt);
187 * Handle (lo pnc_lo x) OR (hi pnc_hi x)
189 static ir_node *bool_or(cond_pair *const cpair)
191 ir_node *const cmp_lo = cpair->cmp_lo;
192 ir_node *const cmp_hi = cpair->cmp_hi;
193 pn_Cmp const pnc_lo = cpair->pnc_lo;
194 pn_Cmp const pnc_hi = cpair->pnc_hi;
195 ir_node *const proj_lo = cpair->proj_lo;
196 ir_node *const proj_hi = cpair->proj_hi;
197 tarval *const tv_lo = cpair->tv_lo;
198 tarval *const tv_hi = cpair->tv_hi;
200 /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
201 if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
202 (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
203 /* x >=|>|!= lo | x <|<=|!= hi -> true */
204 ir_node *const t = new_Const(tarval_b_true);
206 } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
207 (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
208 /* x <|<=|== lo || x <|<=|!= hi -> x <|<=|!= hi */
210 } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
211 (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
212 /* x >=|>|!= lo || x ==|>=|> hi -> x >=|>|!= lo */
214 } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
215 if (pnc_lo == pn_Cmp_Lt && pnc_hi == pn_Cmp_Ge) {
216 /* x < c || x >= c + 1 -> x != c */
217 ir_node *const block = get_nodes_block(cmp_lo);
218 ir_node *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lg);
220 } else if (pnc_lo == pn_Cmp_Le) {
221 if (pnc_hi == pn_Cmp_Eq) {
222 /* x <= c || x == c + 1 -> x <= c + 1 */
223 ir_node *const block = get_nodes_block(cmp_hi);
224 ir_node *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Le);
226 } else if (pnc_hi == pn_Cmp_Ge) {
227 /* x <= c || x >= c + 1 -> true */
228 ir_node *const t = new_Const(tarval_b_true);
230 } else if (pnc_hi == pn_Cmp_Gt) {
231 /* x <= c || x > c + 1 -> x != c + 1 */
232 ir_node *const block = get_nodes_block(cmp_hi);
233 ir_node *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Lg);
236 } else if (pnc_lo == pn_Cmp_Eq && pnc_hi == pn_Cmp_Ge) {
237 /* x == c || x >= c + 1 -> x >= c */
238 ir_node *const block = get_nodes_block(cmp_lo);
239 ir_node *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Ge);
247 * Walker, tries to optimize Andb and Orb nodes.
249 static void bool_walk(ir_node *n, void *ctx)
251 bool_opt_env_t *env = ctx;
253 if (get_irn_mode(n) != mode_b)
257 ir_node *const l = get_And_left(n);
258 ir_node *const r = get_And_right(n);
259 ir_node * replacement;
261 if (!find_cond_pair(l, r, &cpair))
263 replacement = bool_and(&cpair);
265 exchange(n, replacement);
268 } else if (is_Or(n)) {
269 ir_node *const l = get_Or_left(n);
270 ir_node *const r = get_Or_right(n);
271 ir_node * replacement;
273 if (!find_cond_pair(l, r, &cpair))
275 replacement = bool_or(&cpair);
277 exchange(n, replacement);
284 * Walker, clear Block marker and Phi lists.
286 static void clear_block_infos(ir_node *node, void *env)
290 /* we visit blocks before any other nodes (from the block) */
294 /* clear the PHI list */
295 set_Block_phis(node, NULL);
296 set_Block_mark(node, 0);
300 * Walker: collect Phi nodes and mark
302 static void collect_phis(ir_node *node, void *env)
307 ir_node *block = get_nodes_block(node);
308 add_Block_phi(block, node);
312 /* Ignore control flow nodes, these will be removed. */
313 if (get_irn_pinned(node) == op_pin_state_pinned &&
314 !is_Block(node) && !is_cfop(node)) {
315 /* found a pinned non-cf node, mark its block */
316 ir_node *block = get_nodes_block(node);
317 set_Block_mark(block, 1);
322 * If node is a Jmp in a block containing no pinned instruction
323 * and having only one predecessor, skip the block and return its
324 * cf predecessor, else the node itself.
326 static ir_node *skip_empty_blocks(ir_node *node)
328 while (is_Jmp(node)) {
329 ir_node *block = get_nodes_block(node);
331 if (get_Block_n_cfgpreds(block) != 1)
334 if (get_Block_mark(block))
337 node = get_Block_cfgpred(block, 0);
343 * Check if two block inputs can be fused.
344 * This can be done, if block contains no Phi node that depends on
345 * different inputs idx_i and idx_j.
347 static int can_fuse_block_inputs(const ir_node *block, int idx_i, int idx_j) {
350 for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
351 if (get_Phi_pred(phi, idx_i) != get_Phi_pred(phi, idx_j))
358 * Remove block input with given index.
360 static void remove_block_input(ir_node *block, int idx)
362 int i, j, n = get_Block_n_cfgpreds(block) - 1;
365 NEW_ARR_A(ir_node *, ins, n);
368 /* all Phis will be deleted */
371 for (phi = get_Block_phis(block); phi != NULL; phi = next_phi) {
372 next_phi = get_Phi_next(phi);
373 exchange(phi, get_Phi_pred(phi, idx ^ 1));
375 set_Block_phis(block, NULL);
377 for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
378 for (i = j = 0; i <= n; ++i) {
380 ins[j++] = get_Phi_pred(phi, i);
382 set_irn_in(phi, n, ins);
385 for (i = j = 0; i <= n; ++i) {
387 ins[j++] = get_Block_cfgpred(block, i);
389 set_irn_in(block, n, ins);
393 * Under the preposition that we have a chain of blocks from
394 * from_block to to_block, collapse them all into to_block.
396 static void move_nodes_to_block(ir_node *from_block, ir_node *to_block) {
397 ir_node *block, *next_block;
399 for (block = from_block; block != to_block; block = next_block) {
400 next_block = get_Block_cfgpred(block, 0);
401 exchange(block, to_block);
408 * if we can find the following structure,
419 * try to convert it into a (x pnc_lo c_lo) || (x pnc_hi c_hi)
422 static void find_cf_and_or_walker(ir_node *block, void *ctx)
425 int n_cfgpreds = get_Block_n_cfgpreds(block);
426 bool_opt_env_t *env = ctx;
432 for (low_idx = 0; low_idx < n_cfgpreds; ++low_idx) {
433 ir_node *lower_block;
436 ir_node *cond_selector;
439 lower_cf = get_Block_cfgpred(block, low_idx);
440 lower_cf = skip_empty_blocks(lower_cf);
441 if (!is_Proj(lower_cf))
444 cond = get_Proj_pred(lower_cf);
448 lower_block = get_nodes_block(cond);
449 if (get_Block_n_cfgpreds(lower_block) != 1)
452 /* the block must not produce any side-effects */
453 if (get_Block_mark(lower_block))
456 cond_selector = get_Cond_selector(cond);
457 if (get_irn_mode(cond_selector) != mode_b)
460 lower_pred = get_Block_cfgpred_block(lower_block, 0);
462 for (up_idx = 0; up_idx < n_cfgpreds; ++up_idx) {
463 ir_node *upper_block;
466 ir_node *upper_cond_selector;
467 ir_node *replacement;
470 upper_cf = get_Block_cfgpred(block, up_idx);
471 upper_cf = skip_empty_blocks(upper_cf);
472 if (is_Bad(upper_cf))
474 upper_block = get_nodes_block(upper_cf);
475 if (upper_block != lower_pred)
478 assert(is_Proj(upper_cf));
479 upper_cond = get_Proj_pred(upper_cf);
480 assert(is_Cond(upper_cond));
481 upper_cond_selector = get_Cond_selector(upper_cond);
482 if (get_irn_mode(upper_cond_selector) != mode_b)
485 /* we have found the structure */
486 /* check Phis: There must be NO Phi in block that
487 depends on the existence of low block */
488 if (!can_fuse_block_inputs(block, low_idx, up_idx))
491 /* all fine, try it */
492 if (!find_cond_pair(cond_selector, upper_cond_selector, &cpair))
495 /* normalize pncs: we need the true case to jump into the
496 * common block (ie. conjunctive normal form) */
497 if (get_Proj_proj(lower_cf) == pn_Cond_false) {
498 if (cpair.proj_lo == cond_selector) {
499 ir_mode *mode = get_tarval_mode(cpair.tv_lo);
500 cpair.pnc_lo = get_negated_pnc(cpair.pnc_lo, mode);
501 cpair.proj_lo = new_r_Proj(lower_block,
502 get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
504 ir_mode *mode = get_tarval_mode(cpair.tv_hi);
505 assert(cpair.proj_hi == cond_selector);
506 cpair.pnc_hi = get_negated_pnc(cpair.pnc_hi, mode);
507 cpair.proj_hi = new_r_Proj(lower_block,
508 get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
511 if (get_Proj_proj(upper_cf) == pn_Cond_false) {
512 if (cpair.proj_lo == upper_cond_selector) {
513 ir_mode *mode = get_tarval_mode(cpair.tv_lo);
514 cpair.pnc_lo = get_negated_pnc(cpair.pnc_lo, mode);
515 cpair.proj_lo = new_r_Proj(upper_block,
516 get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
518 ir_mode *mode = get_tarval_mode(cpair.tv_hi);
519 assert(cpair.proj_hi == upper_cond_selector);
520 cpair.pnc_hi = get_negated_pnc(cpair.pnc_hi, mode);
521 cpair.proj_hi = new_r_Proj(upper_block,
522 get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
526 /* can we optimize the case? */
527 replacement = bool_or(&cpair);
528 if (replacement == NULL)
533 /* move all expressions on the path to lower/upper block */
534 move_nodes_to_block(get_Block_cfgpred(block, up_idx), upper_block);
535 move_nodes_to_block(get_Block_cfgpred(block, low_idx), lower_block);
537 /* move all nodes from lower block to upper block */
538 exchange(lower_block, upper_block);
540 remove_block_input(block, up_idx);
542 /* the optimisations expected the true case to jump */
543 if (get_Proj_proj(lower_cf) == pn_Cond_false) {
544 ir_node *block = get_nodes_block(replacement);
545 replacement = new_rd_Not(NULL, block, replacement, mode_b);
547 set_Cond_selector(cond, replacement);
549 ir_fprintf(stderr, "replaced (ub %+F)\n", upper_block);
555 void opt_bool(ir_graph *const irg)
559 /* works better with one return block only */
560 normalize_one_return(irg);
564 /* optimize simple Andb and Orb cases */
565 irg_walk_graph(irg, NULL, bool_walk, &env);
567 ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
569 /* now more complicated cases: find control flow And/Or and optimize. */
570 irg_walk_graph(irg, clear_block_infos, collect_phis, NULL);
571 irg_block_walk_graph(irg, NULL, find_cf_and_or_walker, &env);
574 set_irg_outs_inconsistent(irg);
575 set_irg_doms_inconsistent(irg);
576 set_irg_extblk_inconsistent(irg);
577 set_irg_loopinfo_inconsistent(irg);
580 ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
583 /* Creates an ir_graph pass for opt_bool. */
584 ir_graph_pass_t *opt_bool_pass(const char *name)
586 return def_graph_pass(name ? name : "opt_bool", opt_bool);
587 } /* opt_bool_pass */