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
24 * @version $Id: cfopt.c 22579 2008-10-07 14:54:04Z beck $
39 typedef struct cond_pair {
50 static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const res)
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);
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);
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");
67 /* The constants shall be unequal. Local optimisations handle the
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);
76 assert(rel != pn_Cmp_Eq);
78 if (rel == pn_Cmp_Lt) {
88 assert(rel == pn_Cmp_Gt);
105 static ir_node *bool_and(cond_pair* const cpair)
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;
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);
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 */
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 */
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_node *const block = get_nodes_block(cmp_lo);
134 ir_node *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Eq);
136 } else if (pnc_lo == pn_Cmp_Gt) {
137 if (pnc_hi == pn_Cmp_Lg) {
138 /* x > c || x != c + 1 -> x > c + 1 */
139 ir_node *const block = get_nodes_block(cmp_hi);
140 ir_node *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Gt);
142 } else if (pnc_hi == pn_Cmp_Lt) {
143 /* x > c || x < c + 1 -> false */
144 ir_node *const t = new_Const(tarval_b_false);
146 } else if (pnc_hi == pn_Cmp_Le) {
147 /* x > c || x <= c + 1 -> x != c + 1 */
148 ir_node *const block = get_nodes_block(cmp_hi);
149 ir_node *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Eq);
152 } else if (pnc_lo == pn_Cmp_Lg && pnc_hi == pn_Cmp_Lt) {
153 /* x != c || c < c + 1 -> x < c */
154 ir_node *const block = get_nodes_block(cmp_lo);
155 ir_node *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lt);
162 static ir_node *bool_or(cond_pair *const cpair)
164 ir_node *const cmp_lo = cpair->cmp_lo;
165 ir_node *const cmp_hi = cpair->cmp_hi;
166 pn_Cmp const pnc_lo = cpair->pnc_lo;
167 pn_Cmp const pnc_hi = cpair->pnc_hi;
168 ir_node *const proj_lo = cpair->proj_lo;
169 ir_node *const proj_hi = cpair->proj_hi;
170 tarval *const tv_lo = cpair->tv_lo;
171 tarval *const tv_hi = cpair->tv_hi;
173 /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */
174 if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
175 (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
176 /* x >=|>|!= lo | x <|<=|!= hi -> true */
177 ir_node *const t = new_Const(tarval_b_true);
179 } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) &&
180 (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) {
181 /* x <|<=|== lo || x <|<=|!= hi -> x <|<=|!= hi */
183 } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) &&
184 (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) {
185 /* x >=|>|!= lo || x ==|>=|> hi -> x >=|>|!= lo */
187 } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */
188 if (pnc_lo == pn_Cmp_Lt && pnc_hi == pn_Cmp_Ge) {
189 /* x < c || x >= c + 1 -> x != c */
190 ir_node *const block = get_nodes_block(cmp_lo);
191 ir_node *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Lg);
193 } else if (pnc_lo == pn_Cmp_Le) {
194 if (pnc_hi == pn_Cmp_Eq) {
195 /* x <= c || x == c + 1 -> x <= c + 1 */
196 ir_node *const block = get_nodes_block(cmp_hi);
197 ir_node *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Le);
199 } else if (pnc_hi == pn_Cmp_Ge) {
200 /* x <= c || x >= c + 1 -> true */
201 ir_node *const t = new_Const(tarval_b_true);
203 } else if (pnc_hi == pn_Cmp_Gt) {
204 /* x <= c || x > c + 1 -> x != c + 1 */
205 ir_node *const block = get_nodes_block(cmp_hi);
206 ir_node *const p = new_r_Proj(block, cmp_hi, mode_b, pn_Cmp_Lg);
209 } else if (pnc_lo == pn_Cmp_Eq && pnc_hi == pn_Cmp_Ge) {
210 /* x == c || x >= c + 1 -> x >= c */
211 ir_node *const block = get_nodes_block(cmp_lo);
212 ir_node *const p = new_r_Proj(block, cmp_lo, mode_b, pn_Cmp_Ge);
219 static void bool_walk(ir_node *n, void *env)
223 if (get_irn_mode(n) != mode_b)
227 ir_node *const l = get_And_left(n);
228 ir_node *const r = get_And_right(n);
229 ir_node * replacement;
231 if (!find_cond_pair(l, r, &cpair))
233 replacement = bool_and(&cpair);
235 exchange(n, replacement);
236 } else if (is_Or(n)) {
237 ir_node *const l = get_Or_left(n);
238 ir_node *const r = get_Or_right(n);
239 ir_node * replacement;
241 if (!find_cond_pair(l, r, &cpair))
243 replacement = bool_or(&cpair);
245 exchange(n, replacement);
250 * Walker, clear Block mark and Phi list
252 static void clear_block_infos(ir_node *node, void *env)
256 /* we visit blocks before any other nodes (from the block) */
260 /* clear the PHI list */
261 set_Block_phis(node, NULL);
262 set_Block_mark(node, 0);
266 * Walker: collect Phi nodes and update the
268 static void collect_phis(ir_node *node, void *env)
273 ir_node *block = get_nodes_block(node);
274 add_Block_phi(block, node);
278 /* Ignore control flow nodes, these will be removed. */
279 if (get_irn_pinned(node) == op_pin_state_pinned &&
280 !is_Block(node) && !is_cfop(node)) {
281 ir_node *block = get_nodes_block(node);
282 set_Block_mark(block, 1);
287 * If node is a Jmp in a block containing no pinned instruction
288 * and having only one predecessor, skip the block and return its
289 * cf predecessor, else the node itself.
291 static ir_node *skip_empty_block(ir_node *node)
298 block = get_nodes_block(node);
299 if(get_Block_n_cfgpreds(block) != 1)
302 if(get_Block_mark(block))
305 return get_Block_cfgpred(block, 0);
308 static void find_cf_and_or_walker(ir_node *block, void *env)
311 int n_cfgpreds = get_Block_n_cfgpreds(block);
317 /* Find the following structure:
329 for(i = 0; i < n_cfgpreds; ++i) {
330 ir_node *lower_block;
333 ir_node *cond_selector;
336 lower_cf = get_Block_cfgpred(block, i);
337 lower_cf = skip_empty_block(lower_cf);
338 if(!is_Proj(lower_cf))
341 cond = get_Proj_pred(lower_cf);
345 lower_block = get_nodes_block(cond);
346 if(get_Block_n_cfgpreds(lower_block) != 1)
349 /* the block must not produce any side-effects */
350 if(get_Block_mark(lower_block))
353 cond_selector = get_Cond_selector(cond);
354 if(get_irn_mode(cond_selector) != mode_b)
357 lower_pred = get_Block_cfgpred_block(lower_block, 0);
359 for(i2 = 0; i2 < n_cfgpreds; ++i2) {
360 ir_node *upper_block;
363 ir_node *upper_cond_selector;
364 ir_node *replacement;
367 upper_cf = get_Block_cfgpred(block, i2);
368 upper_cf = skip_empty_block(upper_cf);
371 upper_block = get_nodes_block(upper_cf);
372 if(upper_block != lower_pred)
375 assert(is_Proj(upper_cf));
376 upper_cond = get_Proj_pred(upper_cf);
377 assert(is_Cond(upper_cond));
378 upper_cond_selector = get_Cond_selector(upper_cond);
379 if(get_irn_mode(upper_cond_selector) != mode_b)
382 /* we have found the structure */
383 /* TODO: check phis */
384 if(!find_cond_pair(cond_selector, upper_cond_selector, &cpair))
387 /* normalize pncs: we need the true case to jump into the
388 * common block (ie. conjunctive normal form) */
389 if(get_Proj_proj(lower_cf) == pn_Cond_false) {
390 if(cpair.proj_lo == cond_selector) {
391 ir_mode *mode = get_tarval_mode(cpair.tv_lo);
392 cpair.pnc_lo = get_negated_pnc(cpair.pnc_lo, mode);
393 cpair.proj_lo = new_r_Proj(lower_block,
394 get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
396 ir_mode *mode = get_tarval_mode(cpair.tv_hi);
397 assert(cpair.proj_hi == cond_selector);
398 cpair.pnc_hi = get_negated_pnc(cpair.pnc_hi, mode);
399 cpair.proj_hi = new_r_Proj(lower_block,
400 get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
403 if(get_Proj_proj(upper_cf) == pn_Cond_false) {
404 if(cpair.proj_lo == upper_cond_selector) {
405 ir_mode *mode = get_tarval_mode(cpair.tv_lo);
406 cpair.pnc_lo = get_negated_pnc(cpair.pnc_lo, mode);
407 cpair.proj_lo = new_r_Proj(upper_block,
408 get_Proj_pred(cpair.proj_lo), mode_b, cpair.pnc_lo);
410 ir_mode *mode = get_tarval_mode(cpair.tv_hi);
411 assert(cpair.proj_hi == upper_cond_selector);
412 cpair.pnc_hi = get_negated_pnc(cpair.pnc_hi, mode);
413 cpair.proj_hi = new_r_Proj(upper_block,
414 get_Proj_pred(cpair.proj_hi), mode_b, cpair.pnc_hi);
418 /* can we optimize the case? */
419 replacement = bool_or(&cpair);
420 if(replacement == NULL)
423 /* move all nodes from lower block to upper block */
424 exchange(lower_block, upper_block);
426 set_Block_cfgpred(block, i2, new_Bad());
428 /* the optimisations expected the true case to jump */
429 if(get_Proj_proj(lower_cf) == pn_Cond_false) {
430 ir_node *block = get_nodes_block(replacement);
431 replacement = new_rd_Not(NULL, block, replacement, mode_b);
433 set_Cond_selector(cond, replacement);
435 ir_fprintf(stderr, "replaced (ub %+F)\n", upper_block);
441 void opt_bool(ir_graph *const irg)
443 irg_walk_graph(irg, NULL, bool_walk, NULL);
445 ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);
447 irg_walk_graph(irg, clear_block_infos, collect_phis, NULL);
449 irg_block_walk_graph(irg, NULL, find_cf_and_or_walker, NULL);
451 set_irg_outs_inconsistent(irg);
452 set_irg_doms_inconsistent(irg);
453 set_irg_extblk_inconsistent(irg);
454 set_irg_loopinfo_inconsistent(irg);
456 ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST);