From patchwork Tue Mar 19 16:08:29 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Glisse X-Patchwork-Id: 229107 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 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 518BF2C00BF for ; Wed, 20 Mar 2013 03:08:49 +1100 (EST) 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= dkim1; b=uAIn9RWzPc5InFP7njIbOBMpigb5FaTZjnwb+WGN0Cy0GylmuSx9ltF 5bQ8EKgJ1xgBlRKuN6t2AeZ3oaF8Z5672uo87bzpJoszhwnpaPihOwt88GqrIkNL H0GbsTk0J3SXE4HzZA7Kxgp/9yF1G86msQ1eY3artce+fm36dguI= DKIM-Signature: v=1; a=rsa-sha1; c=simple; 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=dkim1; bh=6SBtbRO0VPMOmtZ4IqVCF2NQONU=; b=iLNOuuH0f/EV54F21gdz+1LhEBGu xsbHnlFIhDW2uO7pgrT5zhACbn7awZMLq2F2Qt5kHFv3ryFY8IA4xPr+CoM6b4IO NrUhErCKA0FGY2KMZ451W+1ULzlJNiOjJ0IFu6PiuR6o7Lrn8JvJ2rEwryVM86/h FxEHGY4nYzE65IA= Received: (qmail 7067 invoked by alias); 19 Mar 2013 16:08:43 -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 7021 invoked by uid 89); 19 Mar 2013 16:08:36 -0000 X-Spam-Sware-Status: No, score=-8.9 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD autolearn=ham version=3.3.1 Received: from mail2-relais-roc.national.inria.fr (HELO mail2-relais-roc.national.inria.fr) (192.134.164.83) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Tue, 19 Mar 2013 16:08:32 +0000 Received: from stedding.saclay.inria.fr ([193.55.250.194]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES128-SHA; 19 Mar 2013 17:08:30 +0100 Received: from glisse (helo=localhost) by stedding.saclay.inria.fr with local-esmtp (Exim 4.80) (envelope-from ) id 1UHz5S-0001Uf-47 for gcc-patches@gcc.gnu.org; Tue, 19 Mar 2013 17:08:30 +0100 Date: Tue, 19 Mar 2013 17:08:29 +0100 (CET) From: Marc Glisse To: gcc-patches@gcc.gnu.org Subject: i * i is nonnegative Message-ID: User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) MIME-Version: 1.0 Hello, this patch extends the property that x*x is non-negative, which was already known for floats, to integers with undefined overflow. 2013-03-19 Marc Glisse PR tree-optimization/56355 gcc/ * fold-const.c (tree_binary_nonnegative_warnv_p) : Also handle integers with undefined overflow. gcc/testsuite/ * gcc.dg/pr56355-1.c: New file. Index: gcc/testsuite/gcc.dg/pr56355-1.c =================================================================== --- gcc/testsuite/gcc.dg/pr56355-1.c (revision 0) +++ gcc/testsuite/gcc.dg/pr56355-1.c (revision 0) @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wstrict-overflow=4" } */ + +int +f (int i) +{ + return __builtin_abs (i * i); /* { dg-warning "assuming signed overflow" } */ +} Property changes on: gcc/testsuite/gcc.dg/pr56355-1.c ___________________________________________________________________ Added: svn:eol-style + native Added: svn:keywords + Author Date Id Revision URL Index: gcc/fold-const.c =================================================================== --- gcc/fold-const.c (revision 196633) +++ gcc/fold-const.c (working copy) @@ -15286,29 +15286,32 @@ tree_binary_nonnegative_warnv_p (enum tr && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2)) { unsigned int prec = MAX (TYPE_PRECISION (inner1), TYPE_PRECISION (inner2)) + 1; return prec < TYPE_PRECISION (type); } } break; case MULT_EXPR: - if (FLOAT_TYPE_P (type)) + if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) { - /* x * x for floating point x is always non-negative. */ - if (operand_equal_p (op0, op1, 0)) - return true; - return (tree_expr_nonnegative_warnv_p (op0, - strict_overflow_p) - && tree_expr_nonnegative_warnv_p (op1, - strict_overflow_p)); + /* x * x is always non-negative for floating point x + or without overflow. */ + if (operand_equal_p (op0, op1, 0) + || (tree_expr_nonnegative_warnv_p (op0, strict_overflow_p) + && tree_expr_nonnegative_warnv_p (op1, strict_overflow_p))) + { + if (TYPE_OVERFLOW_UNDEFINED (type)) + *strict_overflow_p = true; + return true; + } } /* zero_extend(x) * zero_extend(y) is non-negative if x and y are both unsigned and their total bits is shorter than the result. */ if (TREE_CODE (type) == INTEGER_TYPE && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST) && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST)) { tree inner0 = (TREE_CODE (op0) == NOP_EXPR) ? TREE_TYPE (TREE_OPERAND (op0, 0))