add Abs(-x) = Abs(x)
authorMichael Beck <beck@ipd.info.uni-karlsruhe.de>
Wed, 21 Nov 2007 16:45:37 +0000 (16:45 +0000)
committerMichael Beck <beck@ipd.info.uni-karlsruhe.de>
Wed, 21 Nov 2007 16:45:37 +0000 (16:45 +0000)
[r16816]

include/libfirm/firmstat.h
ir/be/test/localopts.c
ir/ir/iropt.c
ir/stat/stat_dmp.c

index d7d90c6..e7a7e91 100644 (file)
@@ -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 */
index e690b75..c3980a5 100644 (file)
@@ -1,5 +1,6 @@
 /*$ -fno-inline $*/
 #include <stdio.h>
+#include <math.h>
 
 #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);
 }
index 8488c1a..cdab35e 100644 (file)
@@ -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 */
 
 /**
index 42fd8e0..ae209e1 100644 (file)
@@ -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" },