diff options
author | Bernd Edlinger <bernd.edlinger@hotmail.de> | 2014-06-25 17:24:34 +0000 |
---|---|---|
committer | Bernd Edlinger <edlinger@gcc.gnu.org> | 2014-06-25 17:24:34 +0000 |
commit | f9bb13f37b87855ae980d9e774bd54f11b8d3a3f (patch) | |
tree | 460061ababfd488b677b7bd4445b5f5990d73291 /gcc | |
parent | d122681a3c4491cec5152d5c528005c1f5083e83 (diff) | |
download | gcc-f9bb13f37b87855ae980d9e774bd54f11b8d3a3f.zip gcc-f9bb13f37b87855ae980d9e774bd54f11b8d3a3f.tar.gz gcc-f9bb13f37b87855ae980d9e774bd54f11b8d3a3f.tar.bz2 |
tree-ssa-forwprop.c (associate_plusminus): For widening conversions check for undefined overflow in (T)(P + A) - (T)P -> (T)A.
2014-06-25 Bernd Edlinger <bernd.edlinger@hotmail.de>
* tree-ssa-forwprop.c (associate_plusminus): For widening conversions
check for undefined overflow in (T)(P + A) - (T)P -> (T)A.
Issue a strict overflow warning if appropriate.
testsuite:
2014-06-25 Bernd Edlinger <bernd.edlinger@hotmail.de>
* gcc.c-torture/execute/20140622-1.c: New test.
From-SVN: r211988
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 4 | ||||
-rw-r--r-- | gcc/testsuite/gcc.c-torture/execute/20140622-1.c | 27 | ||||
-rw-r--r-- | gcc/tree-ssa-forwprop.c | 35 |
4 files changed, 65 insertions, 7 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index e35a8fe..33c5b48 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2014-06-25 Bernd Edlinger <bernd.edlinger@hotmail.de> + + * tree-ssa-forwprop.c (associate_plusminus): For widening conversions + check for undefined overflow in (T)(P + A) - (T)P -> (T)A. + Issue a strict overflow warning if appropriate. + 2014-06-25 Martin Liska <mliska@suse.cz> IPA REF refactoring diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 1370ee4..62c13c2 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2014-06-25 Bernd Edlinger <bernd.edlinger@hotmail.de> + + * gcc.c-torture/execute/20140622-1.c: New test. + 2014-06-25 Paolo Carlini <paolo.carlini@oracle.com> DR 178 diff --git a/gcc/testsuite/gcc.c-torture/execute/20140622-1.c b/gcc/testsuite/gcc.c-torture/execute/20140622-1.c new file mode 100644 index 0000000..d3aa66b --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/20140622-1.c @@ -0,0 +1,27 @@ +unsigned p; + +long __attribute__((noinline, noclone)) +test (unsigned a) +{ + return (long)(p + a) - (long)p; +} + +int +main () +{ + p = (unsigned) -2; + if (test (0) != 0) + __builtin_abort (); + if (test (1) != 1) + __builtin_abort (); + if (test (2) != -(long)(unsigned)-2) + __builtin_abort (); + p = (unsigned) -1; + if (test (0) != 0) + __builtin_abort (); + if (test (1) != -(long)(unsigned)-1) + __builtin_abort (); + if (test (2) != -(long)(unsigned)-2) + __builtin_abort (); + return 0; +} diff --git a/gcc/tree-ssa-forwprop.c b/gcc/tree-ssa-forwprop.c index 33fede2..0e5e619 100644 --- a/gcc/tree-ssa-forwprop.c +++ b/gcc/tree-ssa-forwprop.c @@ -47,6 +47,7 @@ along with GCC; see the file COPYING3. If not see #include "tree-pass.h" #include "langhooks.h" #include "flags.h" +#include "diagnostic.h" #include "expr.h" #include "cfgloop.h" #include "optabs.h" @@ -2666,15 +2667,35 @@ associate_plusminus (gimple_stmt_iterator *gsi) { /* And finally (T)(P + A) - (T)P. */ tree a = gimple_assign_rhs2 (def_stmt2); - /* For pointer types, if the conversion of A to the final - type requires a sign- or zero-extension, then we have - to punt - it is not defined which one is correct. */ - if (!POINTER_TYPE_P (TREE_TYPE (rhs1)) - || TYPE_PRECISION (TREE_TYPE (rhs1)) - <= TYPE_PRECISION (TREE_TYPE (a)) - || (TREE_CODE (a) == INTEGER_CST + if (TYPE_PRECISION (TREE_TYPE (rhs1)) + <= TYPE_PRECISION (TREE_TYPE (a)) + /* For integer types, if A has a smaller type + than T the result depends on the possible + overflow in P + A. + E.g. T=size_t, A=(unsigned)429497295, P>0. + However, if an overflow in P + A would cause + undefined behavior, we can assume that there + is no overflow. */ + || (INTEGRAL_TYPE_P (TREE_TYPE (p)) + && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (p))) + /* For pointer types, if the conversion of A to the + final type requires a sign- or zero-extension, + then we have to punt - it is not defined which + one is correct. */ + || (POINTER_TYPE_P (TREE_TYPE (p)) + && TREE_CODE (a) == INTEGER_CST && tree_int_cst_sign_bit (a) == 0)) { + if (issue_strict_overflow_warning + (WARN_STRICT_OVERFLOW_MISC) + && TYPE_PRECISION (TREE_TYPE (rhs1)) + > TYPE_PRECISION (TREE_TYPE (a)) + && INTEGRAL_TYPE_P (TREE_TYPE (p))) + warning_at (gimple_location (stmt), + OPT_Wstrict_overflow, + "assuming signed overflow does not " + "occur when assuming that " + "(T)(P + A) - (T)P is always (T)A"); if (useless_type_conversion_p (TREE_TYPE (rhs1), TREE_TYPE (a))) code = TREE_CODE (a); |