X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fopt%2Fboolopt.c;h=08168a827d193ba7d18a63a8b1a1b05a1dd57434;hb=4cef2a70fbdc26498af3540fb1d07d96fbd56d56;hp=a86c4a517c597a00e421f642ac79ce5c65f05890;hpb=7603e336b06a31379716d1b5ad627087b26de7c8;p=libfirm diff --git a/ir/opt/boolopt.c b/ir/opt/boolopt.c index a86c4a517..08168a827 100644 --- a/ir/opt/boolopt.c +++ b/ir/opt/boolopt.c @@ -1,3 +1,30 @@ +/* + * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved. + * + * This file is part of libFirm. + * + * This file may be distributed and/or modified under the terms of the + * GNU General Public License version 2 as published by the Free Software + * Foundation and appearing in the file LICENSE.GPL included in the + * packaging of this file. + * + * Licensees holding valid libFirm Professional Edition licenses may use + * this file in accordance with the libFirm Commercial License. + * Agreement provided with the Software. + * + * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE + * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE. + */ + +/** + * @file + * @brief boolean condition/controlflow optimisations + * @author Matthias Braun, Christoph Mallon + * @version $Id: cfopt.c 22579 2008-10-07 14:54:04Z beck $ + */ +#include "config.h" + #include #include @@ -86,20 +113,21 @@ static ir_node *bool_and(cond_pair* const cpair) tarval *const tv_lo = cpair->tv_lo; tarval *const tv_hi = cpair->tv_hi; + /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */ if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) && (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) { /* x <|<=|== lo | x ==|>=|> hi -> false */ - ir_node *const t = new_Const(mode_b, tarval_b_false); + ir_node *const t = new_Const(tarval_b_false); return t; } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) && - (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Ne)) { + (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) { /* x <|<=|== lo && x <|<=|!= hi -> x <|<=|== lo */ return proj_lo; - } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Ne) && + } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) && (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) { /* x >=|>|!= lo || x ==|>=|> hi -> x ==|>=|> hi */ return proj_hi; - } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo))) { /* lo + 1 == hi */ + } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */ if (pnc_lo == pn_Cmp_Ge && pnc_hi == pn_Cmp_Lt) { /* x >= c || x < c + 1 -> x == c */ ir_graph *const irg = current_ir_graph; @@ -107,7 +135,7 @@ static ir_node *bool_and(cond_pair* const cpair) ir_node *const p = new_r_Proj(irg, block, cmp_lo, mode_b, pn_Cmp_Eq); return p; } else if (pnc_lo == pn_Cmp_Gt) { - if (pnc_hi == pn_Cmp_Ne) { + if (pnc_hi == pn_Cmp_Lg) { /* x > c || x != c + 1 -> x > c + 1 */ ir_graph *const irg = current_ir_graph; ir_node *const block = get_nodes_block(cmp_hi); @@ -115,7 +143,7 @@ static ir_node *bool_and(cond_pair* const cpair) return p; } else if (pnc_hi == pn_Cmp_Lt) { /* x > c || x < c + 1 -> false */ - ir_node *const t = new_Const(mode_b, tarval_b_false); + ir_node *const t = new_Const(tarval_b_false); return t; } else if (pnc_hi == pn_Cmp_Le) { /* x > c || x <= c + 1 -> x != c + 1 */ @@ -124,7 +152,7 @@ static ir_node *bool_and(cond_pair* const cpair) ir_node *const p = new_r_Proj(irg, block, cmp_hi, mode_b, pn_Cmp_Eq); return p; } - } else if (pnc_lo == pn_Cmp_Ne && pnc_hi == pn_Cmp_Lt) { + } else if (pnc_lo == pn_Cmp_Lg && pnc_hi == pn_Cmp_Lt) { /* x != c || c < c + 1 -> x < c */ ir_graph *const irg = current_ir_graph; ir_node *const block = get_nodes_block(cmp_lo); @@ -146,25 +174,26 @@ static ir_node *bool_or(cond_pair *const cpair) tarval *const tv_lo = cpair->tv_lo; tarval *const tv_hi = cpair->tv_hi; - if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Ne) && - (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Ne)) { + /* Beware of NaN's, we can only check for (ordered) != here (which is Lg, not Ne) */ + if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) && + (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) { /* x >=|>|!= lo | x <|<=|!= hi -> true */ - ir_node *const t = new_Const(mode_b, tarval_b_true); + ir_node *const t = new_Const(tarval_b_true); return t; } else if ((pnc_lo == pn_Cmp_Lt || pnc_lo == pn_Cmp_Le || pnc_lo == pn_Cmp_Eq) && - (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Ne)) { + (pnc_hi == pn_Cmp_Lt || pnc_hi == pn_Cmp_Le || pnc_hi == pn_Cmp_Lg)) { /* x <|<=|== lo || x <|<=|!= hi -> x <|<=|!= hi */ return proj_hi; - } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Ne) && + } else if ((pnc_lo == pn_Cmp_Ge || pnc_lo == pn_Cmp_Gt || pnc_lo == pn_Cmp_Lg) && (pnc_hi == pn_Cmp_Eq || pnc_hi == pn_Cmp_Ge || pnc_hi == pn_Cmp_Gt)) { /* x >=|>|!= lo || x ==|>=|> hi -> x >=|>|!= lo */ return proj_lo; - } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo))) { /* lo + 1 == hi */ + } else if (tarval_is_one(tarval_sub(tv_hi, tv_lo, NULL))) { /* lo + 1 == hi */ if (pnc_lo == pn_Cmp_Lt && pnc_hi == pn_Cmp_Ge) { /* x < c || x >= c + 1 -> x != c */ ir_graph *const irg = current_ir_graph; ir_node *const block = get_nodes_block(cmp_lo); - ir_node *const p = new_r_Proj(irg, block, cmp_lo, mode_b, pn_Cmp_Ne); + ir_node *const p = new_r_Proj(irg, block, cmp_lo, mode_b, pn_Cmp_Lg); return p; } else if (pnc_lo == pn_Cmp_Le) { if (pnc_hi == pn_Cmp_Eq) { @@ -175,17 +204,17 @@ static ir_node *bool_or(cond_pair *const cpair) return p; } else if (pnc_hi == pn_Cmp_Ge) { /* x <= c || x >= c + 1 -> true */ - ir_node *const t = new_Const(mode_b, tarval_b_true); + ir_node *const t = new_Const(tarval_b_true); return t; } else if (pnc_hi == pn_Cmp_Gt) { /* x <= c || x > c + 1 -> x != c + 1 */ ir_graph *const irg = current_ir_graph; ir_node *const block = get_nodes_block(cmp_hi); - ir_node *const p = new_r_Proj(irg, block, cmp_hi, mode_b, pn_Cmp_Ne); + ir_node *const p = new_r_Proj(irg, block, cmp_hi, mode_b, pn_Cmp_Lg); return p; } } else if (pnc_lo == pn_Cmp_Eq && pnc_hi == pn_Cmp_Ge) { - /* x == c || c >= c + 1 -> x >= c */ + /* x == c || x >= c + 1 -> x >= c */ ir_graph *const irg = current_ir_graph; ir_node *const block = get_nodes_block(cmp_lo); ir_node *const p = new_r_Proj(irg, block, cmp_lo, mode_b, pn_Cmp_Ge); @@ -262,7 +291,12 @@ static void collect_phis(ir_node *node, void *env) } } -ir_node *skip_empty_block(ir_node *node) +/** + * If node is a Jmp in a block containing no pinned instruction + * and having only one predecessor, skip the block and return its + * cf predecessor, else the node itself. + */ +static ir_node *skip_empty_block(ir_node *node) { ir_node *block; @@ -312,22 +346,22 @@ restart: if(!is_Proj(lower_cf)) continue; - lower_block = get_nodes_block(lower_cf); - if(get_Block_n_cfgpreds(lower_block) != 1) - continue; - cond = get_Proj_pred(lower_cf); if(!is_Cond(cond)) continue; - cond_selector = get_Cond_selector(cond); - if(get_irn_mode(cond_selector) != mode_b) + lower_block = get_nodes_block(cond); + if(get_Block_n_cfgpreds(lower_block) != 1) continue; /* the block must not produce any side-effects */ if(get_Block_mark(lower_block)) continue; + cond_selector = get_Cond_selector(cond); + if(get_irn_mode(cond_selector) != mode_b) + continue; + lower_pred = get_Block_cfgpred_block(lower_block, 0); for(i2 = 0; i2 < n_cfgpreds; ++i2) { @@ -360,7 +394,7 @@ restart: continue; /* normalize pncs: we need the true case to jump into the - * common block */ + * common block (ie. conjunctive normal form) */ irg = current_ir_graph; if(get_Proj_proj(lower_cf) == pn_Cond_false) { if(cpair.proj_lo == cond_selector) { @@ -419,7 +453,7 @@ void opt_bool(ir_graph *const irg) { irg_walk_graph(irg, NULL, bool_walk, NULL); - set_using_block_mark(irg); + ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST); irg_walk_graph(irg, clear_block_infos, collect_phis, NULL); @@ -430,5 +464,5 @@ void opt_bool(ir_graph *const irg) set_irg_extblk_inconsistent(irg); set_irg_loopinfo_inconsistent(irg); - clear_using_block_mark(irg); + ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_PHI_LIST); }