This fixes PR67442, wide_int_to_tree used for type extension from
a different signed value (which doesn't work).

Bootstrapped and tested on x86_64-unknown-linux-gnu, applied on trunk.

Richard.

2015-09-16  Richard Biener  <rguent...@suse.de>

        PR middle-end/67442
        * fold-const.c (extract_muldiv_1): Properly extend multiplication
        result before builting a tree via wide_int_to_tree.

        * gcc.dg/torture/pr67442.c: New testcase.

Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c    (revision 227779)
+++ gcc/fold-const.c    (working copy)
@@ -6166,8 +6173,12 @@ extract_muldiv_1 (tree t, tree c, enum t
              && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
            overflow_p = true;
          if (!overflow_p)
-           return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
-                               wide_int_to_tree (ctype, mul));
+           {
+             mul = wide_int::from (mul, TYPE_PRECISION (ctype),
+                                   TYPE_SIGN (TREE_TYPE (op1)));
+             return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
+                                 wide_int_to_tree (ctype, mul));
+           }
        }
 
       /* If these operations "cancel" each other, we have the main
Index: gcc/testsuite/gcc.dg/torture/pr67442.c
===================================================================
--- gcc/testsuite/gcc.dg/torture/pr67442.c      (revision 0)
+++ gcc/testsuite/gcc.dg/torture/pr67442.c      (working copy)
@@ -0,0 +1,12 @@
+/* { dg-do run } */
+
+short foo[100];
+
+int main()
+{
+  short* bar = &foo[50];
+  short i = 1;
+  short j = 1;
+  short value = bar[8 - i * 2 * j];
+  return value;
+}

Reply via email to