From patchwork Tue Nov 11 17:40:25 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 409562 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id A33B6140100 for ; Wed, 12 Nov 2014 04:40:40 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=dk81mKc9uvF+vB84s3C40Gv4B7nFa9rtAnNw2kBkri5pfco6+jA5C OZO3/S9YSpTMGDw+ndsRvrA+Ex5L/yim8hnf18yR6uiPNemD7p7n+uDyjMUQCAJs AXp/x8+Jy8rhL6Ai8PT3XdB33HdAuTazzvvdGmb92eNnkRPXm5fNko= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=BbA7aea/LHLxeHK5eXlu9kmBAeo=; b=Rtqzj9DTn6L80rAktn7a g5Zs9y8/OhL2ytm4o+l5JpzfJxeACS3bGE7CAqZWT5R9PoM/pylhmy/GF4tfsq5L UFdN7s41QJ7LmVN9pVj5gFfqylheQpa7TRyWAAbctLYvk1GbJll1DScosAs5OQUO nV24pU9AqG1ELOue65evFrk= Received: (qmail 9628 invoked by alias); 11 Nov 2014 17:40:33 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 9617 invoked by uid 89); 11 Nov 2014 17:40:32 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.8 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Tue, 11 Nov 2014 17:40:32 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id sABHeUmZ015181 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 11 Nov 2014 12:40:30 -0500 Received: from redhat.com (ovpn-116-30.ams2.redhat.com [10.36.116.30]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id sABHeQEr019949 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NO); Tue, 11 Nov 2014 12:40:29 -0500 Date: Tue, 11 Nov 2014 18:40:25 +0100 From: Marek Polacek To: GCC Patches , Richard Biener , Jakub Jelinek Subject: [PATCH] Peg down -(-A) -> A transformation Message-ID: <20141111174025.GI10852@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) While match.pd has been changed to not transform -(-A) to A when the overflow does not wrap or the signed integer ovreflow checking is enabled, fold_negate_expr still happily does such a transformation. That's bad because then we can't detect an overflow, as on the following testcase. But I allowed this transformation for constants, because VRP seems to rely on that (abs_extent_range calls fold_unary). Bootstrapped/regtested on x86_64-linux, ok for trunk? 2014-11-11 Marek Polacek * fold-const.c (fold_unary_loc): Don't call fold_negate_expr when doing signed integer sanitization or when the type overflow wraps. * c-c++-common/ubsan/overflow-negate-3.c: New test. Marek diff --git gcc/fold-const.c gcc/fold-const.c index f3562ff..33311fb 100644 --- gcc/fold-const.c +++ gcc/fold-const.c @@ -7862,9 +7862,15 @@ fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0) return fold_view_convert_expr (type, op0); case NEGATE_EXPR: - tem = fold_negate_expr (loc, arg0); - if (tem) - return fold_convert_loc (loc, type, tem); + if (TREE_CODE (arg0) == INTEGER_CST + || TREE_CODE (arg0) == REAL_CST + || TYPE_OVERFLOW_WRAPS (type) + || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0) + { + tem = fold_negate_expr (loc, arg0); + if (tem) + return fold_convert_loc (loc, type, tem); + } return NULL_TREE; case ABS_EXPR: diff --git gcc/testsuite/c-c++-common/ubsan/overflow-negate-3.c gcc/testsuite/c-c++-common/ubsan/overflow-negate-3.c index e69de29..cf4f9bc 100644 --- gcc/testsuite/c-c++-common/ubsan/overflow-negate-3.c +++ gcc/testsuite/c-c++-common/ubsan/overflow-negate-3.c @@ -0,0 +1,17 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=signed-integer-overflow" } */ + +#define INT_MIN (-__INT_MAX__ - 1) + +int +main () +{ + volatile int x = INT_MIN; + int y = x; + y = -(-y); + x = y; + return 0; +} + +/* { dg-output "negation of -2147483648 cannot be represented in type 'int'\[^\n\r]*; cast to an unsigned type to negate this value to itself\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*negation of -2147483648 cannot be represented in type 'int'\[^\n\r]*; cast to an unsigned type to negate this value to itself\[^\n\r]*(\n|\r\n|\r)" } */