From: Michael Beck Date: Wed, 21 Nov 2007 16:45:37 +0000 (+0000) Subject: add Abs(-x) = Abs(x) X-Git-Url: http://nsz.repo.hu/git/?a=commitdiff_plain;h=86b557b6be3fd996c4043152e79d21bf911c561d;p=libfirm add Abs(-x) = Abs(x) [r16816] --- diff --git a/include/libfirm/firmstat.h b/include/libfirm/firmstat.h index d7d90c624..e7a7e914f 100644 --- a/include/libfirm/firmstat.h +++ b/include/libfirm/firmstat.h @@ -92,6 +92,7 @@ enum firmstat_optimizations_t { FS_OPT_NOT_PLUS_1, /**< ~x + 1 = -x */ FS_OPT_ADD_X_NOT_X, /**< ~x + x = -1 */ FS_OPT_FP_INV_MUL, /**< x / y = x * (1.0/y) */ + FS_OPT_ABS_MINUS_X, /**< Abs(-x) = Abs(x) */ FS_OPT_CONST_PHI, /**< Constant evaluation on Phi */ FS_OPT_PREDICATE, /**< Predicate optimization */ FS_OPT_DEMORGAN, /**< optimization using DeMorgan's law */ diff --git a/ir/be/test/localopts.c b/ir/be/test/localopts.c index e690b75cb..c3980a54e 100644 --- a/ir/be/test/localopts.c +++ b/ir/be/test/localopts.c @@ -1,5 +1,6 @@ /*$ -fno-inline $*/ #include +#include #define CONST 42 @@ -188,27 +189,31 @@ int conv3(signed char a) { } int phi1(int x) { - int a = x ? 23 : 42; - int b = x ? 42 : 23; - return a + b; + int a = x ? 23 : 42; + int b = x ? 42 : 23; + return a + b; } int phi2(int x) { - int a = x ? 16 : 8; - int b = x ? 4 : 2; - return a / b; + int a = x ? 16 : 8; + int b = x ? 4 : 2; + return a / b; } int phi3(int x) { - int a = x ? 5 : 9; - int b = x ? 2 : 4; - return a % b; + int a = x ? 5 : 9; + int b = x ? 2 : 4; + return a % b; } int phi4(int x) { - int a = x ? 5 : 9; - int b = x ? 2 : 4; - return (a / b) + (a % b); + int a = x ? 5 : 9; + int b = x ? 2 : 4; + return (a / b) + (a % b); +} + +int abs1(int x) { + return abs(-x); } int main(void) @@ -275,4 +280,5 @@ int main(void) TU(phi2, 1, 4); TU(phi3, 1, 1); TU(phi4, 1, 3); + TU(abs1, 1, 1); } diff --git a/ir/ir/iropt.c b/ir/ir/iropt.c index 8488c1a3a..cdab35eb3 100644 --- a/ir/ir/iropt.c +++ b/ir/ir/iropt.c @@ -2815,6 +2815,7 @@ static ir_node *transform_node_Quot(ir_node *n) { /** * Optimize Abs(x) into x if x is Confirmed >= 0 * Optimize Abs(x) into -x if x is Confirmed <= 0 + * Optimize Abs(-x) int Abs(x) */ static ir_node *transform_node_Abs(ir_node *n) { ir_node *c, *oldn = n; @@ -2835,7 +2836,7 @@ static ir_node *transform_node_Abs(ir_node *n) { * not run it in the equivalent_node() context. */ n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, - get_irn_n(n, -1), a, mode); + get_nodes_block(n), a, mode); DBG_OPT_CONFIRM(oldn, n); return n; @@ -2846,8 +2847,17 @@ static ir_node *transform_node_Abs(ir_node *n) { DBG_OPT_CONFIRM(oldn, n); return n; default: + break; + } + if (is_Minus(a)) { + /* Abs(-x) = Abs(x) */ + mode = get_irn_mode(n); + n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, + get_nodes_block(n), get_Minus_op(a), mode); + DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X); return n; } + return n; } /* transform_node_Abs */ /** diff --git a/ir/stat/stat_dmp.c b/ir/stat/stat_dmp.c index 42fd8e0d9..ae209e1df 100644 --- a/ir/stat/stat_dmp.c +++ b/ir/stat/stat_dmp.c @@ -109,7 +109,8 @@ static const struct { { FS_OPT_NOT_MINUS_1, "algebraic simplification: ~(x - 1) = -x" }, { FS_OPT_NOT_PLUS_1, "algebraic simplification: ~x + 1 = -x" }, { FS_OPT_ADD_X_NOT_X, "algebraic simplification: ~x + x = -1" }, - { FS_OPT_FP_INV_MUL, "algebraic simplification: x / y = x * (1.0/y)" }, + { FS_OPT_FP_INV_MUL, "algebraic simplification: x / y = x * (1.0/y)" }, + { FS_OPT_ABS_MINUS_X, "algebraic simplification: Abs(-x) = Abs(x)" }, { FS_OPT_CONST_PHI, "constant evaluation on Phi node" }, { FS_OPT_PREDICATE, "predicate optimization" }, { FS_OPT_DEMORGAN, "optimization using DeMorgan's law" },