+2015-06-18 Richard Biener <rguenther@suse.de>
+
+ Backport from mainline
+ 2014-12-04 Marek Polacek <polacek@redhat.com>
+
+ PR middle-end/56917
+ * fold-const.c (fold_unary_loc): Perform the negation in A's type
+ when transforming ~ (A - 1) or ~ (A + -1) to -A.
+
2015-06-18 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/66233
&& integer_onep (TREE_OPERAND (arg0, 1)))
|| (TREE_CODE (arg0) == PLUS_EXPR
&& integer_all_onesp (TREE_OPERAND (arg0, 1)))))
- return fold_build1_loc (loc, NEGATE_EXPR, type,
- fold_convert_loc (loc, type,
- TREE_OPERAND (arg0, 0)));
+ {
+ /* Perform the negation in ARG0's type and only then convert
+ to TYPE as to avoid introducing undefined behavior. */
+ tree t = fold_build1_loc (loc, NEGATE_EXPR,
+ TREE_TYPE (TREE_OPERAND (arg0, 0)),
+ TREE_OPERAND (arg0, 0));
+ return fold_convert_loc (loc, type, t);
+ }
/* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
else if (TREE_CODE (arg0) == BIT_XOR_EXPR
&& (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
+2015-06-18 Richard Biener <rguenther@suse.de>
+
+ Backport from mainline
+ 2014-12-04 Marek Polacek <polacek@redhat.com>
+
+ PR middle-end/56917
+ * g++.dg/other/const4.C: New testcase.
+
2015-06-18 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/66233