From patchwork Wed Oct 25 21:53:52 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Botcazou X-Patchwork-Id: 830388 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-465136-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="gy1nAI0q"; dkim-atps=neutral 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 3yMkS20Lk9z9t3C for ; Thu, 26 Oct 2017 08:54:09 +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:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=oj1hM/AtrOR5eue2 VQ1rxFXTCr+8LkvP64Q1ePnXKFAIBpBb0dssafg0+7I1kE8ayB/itO0gKXHUiVF6 14hAhLhwANNwN0fsnsXzb/tsaM4CG16ANLmnawuPgEmWaqbkUePsevsEO1A/a7C+ 9InXA//h0tHYNsySg1XI4G0UHHo= 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:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=default; bh=oqusXpii27hcfp7qaSVkkv KgYuk=; b=gy1nAI0qUKwUVMx9XfhaYmQv+HbgukGWLvX2eEsByApBYGyHw9TkUj aJrR9MSPKhH8xva3WIOrP8DDu8+EUX+aaWbSLbshlGdPOqyc9yQ32WOHY5jHLARi WKjveiZw5qEv6necGU8AzILWlcMfesd7wA9grqIYttknGenDUs1IQ= Received: (qmail 86068 invoked by alias); 25 Oct 2017 21:53:57 -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 85954 invoked by uid 89); 25 Oct 2017 21:53:57 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=trail, 81814, specialists X-HELO: smtp.eu.adacore.com Received: from mel.act-europe.fr (HELO smtp.eu.adacore.com) (194.98.77.210) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 25 Oct 2017 21:53:55 +0000 Received: from localhost (localhost [127.0.0.1]) by filtered-smtp.eu.adacore.com (Postfix) with ESMTP id 660AA81C18 for ; Wed, 25 Oct 2017 23:53:53 +0200 (CEST) Received: from smtp.eu.adacore.com ([127.0.0.1]) by localhost (smtp.eu.adacore.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id WGJR8tjN--Xp for ; Wed, 25 Oct 2017 23:53:53 +0200 (CEST) Received: from polaris.localnet (bon31-6-88-161-99-133.fbx.proxad.net [88.161.99.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.eu.adacore.com (Postfix) with ESMTPSA id 3B1DC81398 for ; Wed, 25 Oct 2017 23:53:53 +0200 (CEST) From: Eric Botcazou To: gcc-patches@gcc.gnu.org Subject: Fix PR middle-end/82062 Date: Wed, 25 Oct 2017 23:53:52 +0200 Message-ID: <2049578.4RL6RhDjvc@polaris> User-Agent: KMail/4.14.10 (Linux/3.16.7-53-desktop; KDE/4.14.9; x86_64; ; ) MIME-Version: 1.0 This is the regression in the folding of conditional expressions like unsigned long f1 (int x) { return x > 0 ? (unsigned long) x : 0; } unsigned long f2 (int x, int y) { return x > y ? (unsigned long) x : (unsigned long) y; } introduced by the fix for PR middle-end/81814. These conditional expressions are very common in Ada in the size expressions of unconstrained array types. The attached patch reinstates a limited version of the optimization that was disabled by the aforementioned fix. The long term plan is to move the whole thing to match.pd but I'll leave that to specialists. Bootstrapped/regtested on x86_64-suse-linux, approved by Richard B. in the audit trail of the PR, applied on the mainline. 2017-10-25 Eric Botcazou PR middle-end/82062 * fold-const.c (operand_equal_for_comparison_p): Also return true if ARG0 is a simple variant of ARG1 with narrower precision. (fold_ternary_loc): Always pass unstripped operands to the predicate. 2017-10-25 Eric Botcazou * gcc.dg/fold-cond_expr-1.c: Rename to... * gcc.dg/fold-cond-2.c: ...this. * gcc.dg/fold-cond-3.c: New test. Index: fold-const.c =================================================================== --- fold-const.c (revision 254037) +++ fold-const.c (working copy) @@ -3366,7 +3366,8 @@ operand_equal_p (const_tree arg0, const_ #undef OP_SAME_WITH_NULL } -/* Similar to operand_equal_p, but strip nops first. */ +/* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1 + with a different signedness or a narrower precision. */ static bool operand_equal_for_comparison_p (tree arg0, tree arg1) @@ -3381,9 +3382,20 @@ operand_equal_for_comparison_p (tree arg /* Discard any conversions that don't change the modes of ARG0 and ARG1 and see if the inner values are the same. This removes any signedness comparison, which doesn't matter here. */ - STRIP_NOPS (arg0); - STRIP_NOPS (arg1); - if (operand_equal_p (arg0, arg1, 0)) + tree op0 = arg0; + tree op1 = arg1; + STRIP_NOPS (op0); + STRIP_NOPS (op1); + if (operand_equal_p (op0, op1, 0)) + return true; + + /* Discard a single widening conversion from ARG1 and see if the inner + value is the same as ARG0. */ + if (CONVERT_EXPR_P (arg1) + && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))) + && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0))) + < TYPE_PRECISION (TREE_TYPE (arg1)) + && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) return true; return false; @@ -11169,8 +11181,8 @@ fold_ternary_loc (location_t loc, enum t Also try swapping the arguments and inverting the conditional. */ if (COMPARISON_CLASS_P (arg0) - && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), arg1) - && !HONOR_SIGNED_ZEROS (element_mode (arg1))) + && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1) + && !HONOR_SIGNED_ZEROS (element_mode (op1))) { tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2); if (tem)