From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 124165 invoked by alias); 3 Apr 2017 11:48:24 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 124117 invoked by uid 89); 3 Apr 2017 11:48:19 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.1 required=5.0 tests=BAYES_00,GIT_PATCH_2,GIT_PATCH_3,KAM_ASCII_DIVIDERS,RP_MATCHES_RCVD,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx2.suse.de Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 03 Apr 2017 11:48:18 +0000 Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id E75A6AB5D for ; Mon, 3 Apr 2017 11:48:17 +0000 (UTC) Date: Mon, 03 Apr 2017 11:48:00 -0000 From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix PR80281 Message-ID: User-Agent: Alpine 2.20 (LSU 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-SW-Source: 2017-04/txt/msg00074.txt.bz2 The following fixes PR80281. Bootstrapped on x86_64-unknown-linux-gnu, testing in progress. Richard. 2017-04-03 Richard Biener PR middle-end/80281 * match.pd (A + (-B) -> A - B): Make sure to preserve unsigned arithmetic done for the negate or the plus. * fold-const.c (split_tree): Make sure to not negate pointers. * gcc.dg/torture/pr80281.c: New testcase. Index: gcc/match.pd =================================================================== --- gcc/match.pd (revision 246642) +++ gcc/match.pd (working copy) @@ -1153,7 +1153,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) && tree_nop_conversion_p (type, TREE_TYPE (@1)) && !TYPE_OVERFLOW_SANITIZED (type)) - (minus (convert @0) (convert @1)))) + (with + { + tree t1 = type; + if (INTEGRAL_TYPE_P (type) + && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) + t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1); + } + (convert (minus (convert:t1 @0) (convert:t1 @1)))))) /* A - (-B) -> A + B */ (simplify (minus (convert1? @0) (convert2? (negate @1))) Index: gcc/fold-const.c =================================================================== --- gcc/fold-const.c (revision 246642) +++ gcc/fold-const.c (working copy) @@ -831,8 +831,12 @@ split_tree (location_t loc, tree in, tre /* Now do any needed negations. */ if (neg_litp_p) *minus_litp = *litp, *litp = 0; - if (neg_conp_p) - *conp = negate_expr (*conp); + if (neg_conp_p && *conp) + { + /* Convert to TYPE before negating. */ + *conp = fold_convert_loc (loc, type, *conp); + *conp = negate_expr (*conp); + } if (neg_var_p && var) { /* Convert to TYPE before negating. */ @@ -859,7 +863,12 @@ split_tree (location_t loc, tree in, tre *minus_litp = *litp, *litp = 0; else if (*minus_litp) *litp = *minus_litp, *minus_litp = 0; - *conp = negate_expr (*conp); + if (*conp) + { + /* Convert to TYPE before negating. */ + *conp = fold_convert_loc (loc, type, *conp); + *conp = negate_expr (*conp); + } if (var) { /* Convert to TYPE before negating. */ Index: gcc/testsuite/gcc.dg/torture/pr80281.c =================================================================== --- gcc/testsuite/gcc.dg/torture/pr80281.c (revision 0) +++ gcc/testsuite/gcc.dg/torture/pr80281.c (working copy) @@ -0,0 +1,14 @@ +/* { dg-run } */ +/* { dg-require-effective-target int32plus } */ + +int +main () +{ + volatile int a = 0; + long long b = 2147483648LL; + int c = a % 2; + int x = ((int) -b + c) % -2147483647; + if (x != -1) + __builtin_abort (); + return 0; +}