From patchwork Mon May 23 11:17:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyrill Tkachov X-Patchwork-Id: 625144 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 3rCwxS46ttz9t0r for ; Mon, 23 May 2016 21:17:31 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=EUMZzpBW; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:subject:content-type; q= dns; s=default; b=tqb1hix6DOF3a3hQCEVbyHskLWtT5hxcb5W7TgdXO3PJCF V0k8F/mvoE8aIbxJkqUHi8fn/lHjpS9RlP/1xlYaTjrqVbXfmurO2T4I9iWQ3g0f l+g/lXSYpZxsG9qdHnxC/u8+uMpcX7oFcUMY3S2xMu3EQS71OIMcNDLQuEj6Y= 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 :message-id:date:from:mime-version:to:subject:content-type; s= default; bh=UNBs4bjP0o9tyLtPSd23PmYOMhE=; b=EUMZzpBWwJdiJCm7AnRJ Okm4nxaVIjaLn0JLvtR2Y7C3rY+h+Baw+gpG3MRJWxj70MrSEYKNsinXQuAj6UWC 1UcvxzzQfX8M37tmRqtgYFA6SlZIxpYCvVH9UChbOHWY3xe4u2TT1C9qnW+he7Qf 6mYAUT/2f/wpoibHnY3VRSU= Received: (qmail 54104 invoked by alias); 23 May 2016 11:17:23 -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 54084 invoked by uid 89); 23 May 2016 11:17:22 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy= X-HELO: foss.arm.com Received: from foss.arm.com (HELO foss.arm.com) (217.140.101.70) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 23 May 2016 11:17:10 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id CBE433C for ; Mon, 23 May 2016 04:17:30 -0700 (PDT) Received: from [10.2.206.43] (e100706-lin.cambridge.arm.com [10.2.206.43]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 844633F246 for ; Mon, 23 May 2016 04:17:08 -0700 (PDT) Message-ID: <5742E6B2.7050904@foss.arm.com> Date: Mon, 23 May 2016 12:17:06 +0100 From: Kyrill Tkachov User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: GCC Patches Subject: [PATCH][RTL ifcvt] PR rtl-optimization/66940: Avoid signed overflow in noce_get_alt_condition Hi all, In this PR we end up hitting a signed overflow in noce_get_alt_condition when we try to increment or decrement a HOST_WIDE_INT that might be HOST_WIDE_INT_MAX or HOST_WIDE_INT_MIN. I've confirmed the overflow by adding an assert before the operation: gcc_assert (desired_val != HOST_WIDE_INT_MAX); This patch fixes those cases by catching the cases when desired_val has the extreme value and avoids the transformation that function is trying to make. Bootstrapped and tested on arm, aarch64, x86_64. I've added the testcase that I used to trigger the assert mentioned above as a compile test, though I'm not sure how much value it has... Ok for trunk? Thanks, Kyrill 2016-05-23 Kyrylo Tkachov PR rtl-optimization/66940 * ifcvt.c (noce_get_alt_condition): Check that incrementing or decrementing desired_val will not overflow before performing these operations. 2016-05-23 Kyrylo Tkachov PR rtl-optimization/66940 * gcc.c-torture/compile/pr66940.c: New test. diff --git a/gcc/ifcvt.c b/gcc/ifcvt.c index 80af4a84363192879cc49ea45f777fc987fda555..05fac71409d401a08d01b7dc7cf164613f8477c4 100644 --- a/gcc/ifcvt.c +++ b/gcc/ifcvt.c @@ -2396,28 +2396,32 @@ noce_get_alt_condition (struct noce_if_info *if_info, rtx target, switch (code) { case LT: - if (actual_val == desired_val + 1) + if (desired_val != HOST_WIDE_INT_MAX + && actual_val == desired_val + 1) { code = LE; op_b = GEN_INT (desired_val); } break; case LE: - if (actual_val == desired_val - 1) + if (desired_val != HOST_WIDE_INT_MIN + && actual_val == desired_val - 1) { code = LT; op_b = GEN_INT (desired_val); } break; case GT: - if (actual_val == desired_val - 1) + if (desired_val != HOST_WIDE_INT_MIN + && actual_val == desired_val - 1) { code = GE; op_b = GEN_INT (desired_val); } break; case GE: - if (actual_val == desired_val + 1) + if (desired_val != HOST_WIDE_INT_MAX + && actual_val == desired_val + 1) { code = GT; op_b = GEN_INT (desired_val); diff --git a/gcc/testsuite/gcc.c-torture/compile/pr66940.c b/gcc/testsuite/gcc.c-torture/compile/pr66940.c new file mode 100644 index 0000000000000000000000000000000000000000..1f3586b49f4389b4a506774cf550a984073f03e6 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr66940.c @@ -0,0 +1,8 @@ +long +foo (int cp, long ival) +{ + if (ival <= 0) + return -0x7fffffffffffffffL - 1; + + return 0x7fffffffffffffffL; +}