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 $
41 typedef struct cond_pair {
52 static int find_cond_pair(ir_node *const l, ir_node *const r, cond_pair *const res)
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);
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);
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");
69 /* The constants shall be unequal. Local optimisations handle the
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);
78 assert(rel != pn_Cmp_Eq);
80 if (rel == pn_Cmp_Lt) {
90 assert(rel == pn_Cmp_Gt);
107 static ir_node *bool_and(cond_pair* const cpair)
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;
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);
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 */
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 */
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);
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);
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);
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);
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);
168 static ir_node *bool_or(cond_pair *const cpair)
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;
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);
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 */
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 */
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);
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);
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);
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);
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);
229 static void bool_walk(ir_node *n, void *env)
233 if (get_irn_mode(n) != mode_b)
237 ir_node *const l = get_And_left(n);
238 ir_node *const r = get_And_right(n);
239 ir_node * replacement;
241 if (!find_cond_pair(l, r, &cpair))
243 replacement = bool_and(&cpair);
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;
251 if (!find_cond_pair(l, r, &cpair))
253 replacement = bool_or(&cpair);
255 exchange(n, replacement);
260 * Walker, clear Block mark and Phi list
262 static void clear_block_infos(ir_node *node, void *env)
266 /* we visit blocks before any other nodes (from the block) */
270 /* clear the PHI list */
271 set_Block_phis(node, NULL);
272 set_Block_mark(node, 0);
276 * Walker: collect Phi nodes and update the
278 static void collect_phis(ir_node *node, void *env)
283 ir_node *block = get_nodes_block(node);
284 add_Block_phi(block, node);
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);
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.
301 static ir_node *skip_empty_block(ir_node *node)
308 block = get_nodes_block(node);
309 if(get_Block_n_cfgpreds(block) != 1)
312 if(get_Block_mark(block))
315 return get_Block_cfgpred(block, 0);
318 static void find_cf_and_or_walker(ir_node *block, void *env)
321 int n_cfgpreds = get_Block_n_cfgpreds(block);
327 /* Find the following structure:
339 for(i = 0; i < n_cfgpreds; ++i) {
340 ir_node *lower_block;
343 ir_node *cond_selector;
346 lower_cf = get_Block_cfgpred(block, i);
347 lower_cf = skip_empty_block(lower_cf);
348 if(!is_Proj(lower_cf))
351 cond = get_Proj_pred(lower_cf);
355 lower_block = get_nodes_block(cond);
356 if(get_Block_n_cfgpreds(lower_block) != 1)
359 /* the block must not produce any side-effects */
360 if(get_Block_mark(lower_block))
363 cond_selector = get_Cond_selector(cond);
364 if(get_irn_mode(cond_selector) != mode_b)
367 lower_pred = get_Block_cfgpred_block(lower_block, 0);
369 for(i2 = 0; i2 < n_cfgpreds; ++i2) {
370 ir_node *upper_block;
373 ir_node *upper_cond_selector;
374 ir_node *replacement;
378 upper_cf = get_Block_cfgpred(block, i2);
379 upper_cf = skip_empty_block(upper_cf);
382 upper_block = get_nodes_block(upper_cf);
383 if(upper_block != lower_pred)
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)
393 /* we have found the structure */
394 /* TODO: check phis */
395 if(!find_cond_pair(cond_selector, upper_cond_selector, &cpair))
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);
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);
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);
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);
430 /* can we optimize the case? */
431 replacement = bool_or(&cpair);
432 if(replacement == NULL)
435 /* move all nodes from lower block to upper block */
436 exchange(lower_block, upper_block);
438 set_Block_cfgpred(block, i2, new_Bad());
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);
446 set_Cond_selector(cond, replacement);
448 ir_fprintf(stderr, "replaced (ub %+F)\n", upper_block);
454 void opt_bool(ir_graph *const irg)
456 irg_walk_graph(irg, NULL, bool_walk, NULL);
458 ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK);
460 irg_walk_graph(irg, clear_block_infos, collect_phis, NULL);
462 irg_block_walk_graph(irg, NULL, find_cf_and_or_walker, NULL);
464 set_irg_outs_inconsistent(irg);
465 set_irg_doms_inconsistent(irg);
466 set_irg_extblk_inconsistent(irg);
467 set_irg_loopinfo_inconsistent(irg);
469 ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK);