Upstream-Status: Inappropriate [Backport] From 5bc59e25607b755798008d5e0d79ca4cea6711ed Mon Sep 17 00:00:00 2001 From: jakub Date: Sat, 26 Mar 2011 09:23:01 +0000 Subject: [PATCH 005/200] Backport from mainline 2011-03-20 Jakub Jelinek PR c/42544 PR c/48197 * c-common.c (shorten_compare): If primopN is first sign-extended to opN and then zero-extended to result type, set primopN to opN. * gcc.c-torture/execute/pr42544.c: New test. * gcc.c-torture/execute/pr48197.c: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/gcc-4_6-branch@171548 138bc75d-0d04-0410-961f-82ee72b054a4 index 4da9a2d..c0c01b2 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -3300,6 +3300,20 @@ shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr, primop0 = get_narrower (op0, &unsignedp0); primop1 = get_narrower (op1, &unsignedp1); + /* If primopN is first sign-extended from primopN's precision to opN's + precision, then zero-extended from opN's precision to + *restype_ptr precision, shortenings might be invalid. */ + if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0)) + && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr) + && !unsignedp0 + && TYPE_UNSIGNED (TREE_TYPE (op0))) + primop0 = op0; + if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1)) + && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr) + && !unsignedp1 + && TYPE_UNSIGNED (TREE_TYPE (op1))) + primop1 = op1; + /* Handle the case that OP0 does not *contain* a conversion but it *requires* conversion to FINAL_TYPE. */ new file mode 100644 index 0000000..c5951b0 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr42544.c @@ -0,0 +1,14 @@ +/* PR c/42544 */ + +extern void abort (void); + +int +main () +{ + signed short s = -1; + if (sizeof (long long) == sizeof (unsigned int)) + return 0; + if ((unsigned int) s >= 0x100000000ULL) + abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.c-torture/execute/pr48197.c b/gcc/testsuite/gcc.c-torture/execute/pr48197.c new file mode 100644 index 0000000..37812c0 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr48197.c @@ -0,0 +1,25 @@ +/* PR c/48197 */ + +extern void abort (void); +static int y = 0x8000; + +int +main () +{ + unsigned int x = (short)y; + if (sizeof (0LL) == sizeof (0U)) + return 0; + if (0LL > (0U ^ (short)-0x8000)) + abort (); + if (0LL > (0U ^ x)) + abort (); + if (0LL > (0U ^ (short)y)) + abort (); + if ((0U ^ (short)-0x8000) < 0LL) + abort (); + if ((0U ^ x) < 0LL) + abort (); + if ((0U ^ (short)y) < 0LL) + abort (); + return 0; +} -- 1.7.0.4