From: Richard Biener Date: Thu, 18 Jun 2015 14:47:18 +0000 (+0000) Subject: backport: re PR middle-end/56917 (-ftrapv detects a overflow wrongly.) X-Git-Tag: releases/gcc-4.8.5~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6b7a887d1e70bad7b5230fe55a7145a5316457d8;p=thirdparty%2Fgcc.git backport: re PR middle-end/56917 (-ftrapv detects a overflow wrongly.) 2015-06-18 Richard Biener Backport from mainline 2014-12-04 Marek Polacek 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. * g++.dg/other/const4.C: New testcase. From-SVN: r224617 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 4ee4479e2278..2bada22e200c 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,12 @@ +2015-06-18 Richard Biener + + Backport from mainline + 2014-12-04 Marek Polacek + + 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 PR tree-optimization/66233 diff --git a/gcc/fold-const.c b/gcc/fold-const.c index f06bef615736..d3c2569adc35 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -8249,9 +8249,14 @@ fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0) && 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, diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 6331bcbdaeaf..27d71ca069d0 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2015-06-18 Richard Biener + + Backport from mainline + 2014-12-04 Marek Polacek + + PR middle-end/56917 + * g++.dg/other/const4.C: New testcase. + 2015-06-18 Jakub Jelinek PR tree-optimization/66233 diff --git a/gcc/testsuite/g++.dg/other/const4.C b/gcc/testsuite/g++.dg/other/const4.C new file mode 100644 index 000000000000..6e30d9e906cb --- /dev/null +++ b/gcc/testsuite/g++.dg/other/const4.C @@ -0,0 +1,10 @@ +// { dg-do compile } + +int lValue; +int main() +{ + switch (lValue) + { + case -(int)((2U << (8 * sizeof(int) - 2)) - 1) - 1:; + } +}