From patchwork Fri Sep 23 09:21:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamar Christina X-Patchwork-Id: 1404 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp121622wrt; Fri, 23 Sep 2022 02:22:30 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4dUHojbMv1Lct+1YQEI5Z4RCsUa1+rOeEWpkL3OM9SqtyOUz7Hl5IwuwQCpqg4TqWUAXFy X-Received: by 2002:a17:907:b1b:b0:781:320f:b76c with SMTP id h27-20020a1709070b1b00b00781320fb76cmr6142954ejl.671.1663924950061; Fri, 23 Sep 2022 02:22:30 -0700 (PDT) Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id qw4-20020a1709066a0400b0077d264918b9si4173184ejc.685.2022.09.23.02.22.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Sep 2022 02:22:30 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=U1TjALOt; arc=fail (signature failed); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EA4713857BA2 for ; Fri, 23 Sep 2022 09:22:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EA4713857BA2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663924949; bh=UzfRVKW4XTAnySfYFkipcq0ho7hVFnLqA0tfhkBhPZQ=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=U1TjALOt0skL0auaRRuADxd8ZgtTX8NcCpsw/412OEfpwL3AzGNXvsGK7GNGahOq1 ZjwInAA1KVwryTr71IU5dd/4juopqVEDz5Uc04YyoCAECHOp43Va1P+NaHb6HOVgQw 8jYlZszpHbd3x0fAbgmayPy69pEUlPIho07P207g= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from EUR02-VE1-obe.outbound.protection.outlook.com (mail-eopbgr20058.outbound.protection.outlook.com [40.107.2.58]) by sourceware.org (Postfix) with ESMTPS id 802AA3858C52 for ; Fri, 23 Sep 2022 09:21:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 802AA3858C52 ARC-Seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass; b=NPdjVfiM3rsIWHRW4zXNdbWSuuNobft4cH8GlSKbPoJbNSPbEDIcFtplSOBo2QTgF9uh6EujTSh/ThailFTaGY0f4QPQaQi+SMUgSwutpn0ZKw/0zhOxNtjAiHKL9Io55+22IyeDlPCgeRXkaw9rGLTOEGOcXsqmYNLartouQxtcpth7mseJw9NR6dtzNI3JQarHkXhSRBL2sixqm21nrCU31PtQv+JExvvJymzido7Fk9jXiZTqzmQ1S2GhhusoWLBppOLPRgTxJKebmsMBxs0UJX2IFodPrFh0UDh0MJhWTLhC83wO6RLbJv/pfIvFZ7epDjgw+1GVtHpebKSeag== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=UzfRVKW4XTAnySfYFkipcq0ho7hVFnLqA0tfhkBhPZQ=; b=ZqahE8c6wR7J/lJl53CqUfl8iLhcTJ3AtFj4W7ToBbw+pJHGiCP8ls58VW3X5TyFa8vrFTVkuTD/GvSfHNXIIzd1tWgmu3c+qz2dn+p0C1fzvJ2BySgV+Cvb1WnZrpgugIC0GqAqTabhv5vXgNjFhvTbDrgI2yc/KL51oVKe5GgaXoHqC445wjNsVvJ4LQfR+XK4N1gmINnonmZeBOS+7iJKhG6Y6xoeQwNhq061N3W6/doeaMI6y4FU50+F6+ljrN/c4m5YGxH1b6ps01Rq+6Hk2jCmF3u/9o1vzuYPQHQz1cMFwa22fQQVFAID/u1m/XQ7M7+ERWZedH6hwzZw2g== ARC-Authentication-Results: i=2; mx.microsoft.com 1; spf=pass (sender ip is 63.35.35.123) smtp.rcpttodomain=gcc.gnu.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com; arc=pass (0 oda=1 ltdi=1 spf=[1,1,smtp.mailfrom=arm.com] dkim=[1,1,header.d=arm.com] dmarc=[1,1,header.from=arm.com]) Received: from AM5PR0402CA0009.eurprd04.prod.outlook.com (2603:10a6:203:90::19) by VE1PR08MB5613.eurprd08.prod.outlook.com (2603:10a6:800:1a7::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5654.20; Fri, 23 Sep 2022 09:21:34 +0000 Received: from AM7EUR03FT052.eop-EUR03.prod.protection.outlook.com (2603:10a6:203:90:cafe::74) by AM5PR0402CA0009.outlook.office365.com (2603:10a6:203:90::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5654.20 via Frontend Transport; Fri, 23 Sep 2022 09:21:33 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; pr=C Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM7EUR03FT052.mail.protection.outlook.com (100.127.140.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5654.14 via Frontend Transport; Fri, 23 Sep 2022 09:21:33 +0000 Received: ("Tessian outbound fc2405f9ecaf:v124"); Fri, 23 Sep 2022 09:21:33 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 6a52a95293897441 X-CR-MTA-TID: 64aa7808 Received: from 8fd02cd93c9d.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 0EB50785-0991-43D1-8EE9-9605001A1DDD.1; Fri, 23 Sep 2022 09:21:27 +0000 Received: from EUR05-VI1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 8fd02cd93c9d.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Fri, 23 Sep 2022 09:21:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PdNYe0PBhRFbWJjP4oSybRmKzpUwsPZ+1U0Y7TlzumMQUV41QxVRasQaIaNDAOY8KQbhNc8fNiPeY5VIlSS8zIROKrJhFtzeOMR9h1Fcrk7/OJxi0gt82moltQvBeK4g3a6TuE2a5Hzj6sQKkY6ZBI5R9/8nRoFQVtWNp+WEIdkyALcEwb+a0bbs45q85U1i8RlFdcDa6s5SFWkvuJ63FAihA+pOUrZ1w9HR2b2b9ytx5ilwlbWz/8dvBZnhJ2SPiefngxBJpKIByWjYWtiJwCE+ZMIzsEwScGwlE9gNfNsf2gsKFiKWJRvlOLir61LzyqsfayEuBZ9L9M46mLUk8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=UzfRVKW4XTAnySfYFkipcq0ho7hVFnLqA0tfhkBhPZQ=; b=QYFB5GCUZfoMM6CWCJRXDbIlKJUh9TsW0Q4KYXR2eHN9S6T/2oKaujDYC3COGDwZIBdrOwHYpW6HmNk4LGsi+RS8nfpI8W4SPhiDZrFO49gi3Rgk6LmpQnfj/F5GJeUPTu6ESAWVwlM1XL39QNTMfDhh1V9m7GOaVWjyTxkvyAC+MD4ZVaynxTN+SKCebC6M0xQzb3OeJR/tGRpLQGcggM0q7JnI3Sm87junKbBRzMEFygIslpFuVB6NNMYsLbkfZXfI+ClGByAHOu7dBtgkwDJUNnVS5/hkGdrjNmayqZLZb9+YEjdxfEkJbH/fGYzGwreY0s1MC3uhcPj8eygQ2w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none Authentication-Results-Original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; Received: from VI1PR08MB5325.eurprd08.prod.outlook.com (2603:10a6:803:13e::17) by DBBPR08MB5900.eurprd08.prod.outlook.com (2603:10a6:10:200::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5654.20; Fri, 23 Sep 2022 09:21:25 +0000 Received: from VI1PR08MB5325.eurprd08.prod.outlook.com ([fe80::6529:66e5:e7d4:1a40]) by VI1PR08MB5325.eurprd08.prod.outlook.com ([fe80::6529:66e5:e7d4:1a40%4]) with mapi id 15.20.5632.021; Fri, 23 Sep 2022 09:21:25 +0000 Date: Fri, 23 Sep 2022 10:21:20 +0100 To: gcc-patches@gcc.gnu.org Subject: [PATCH]middle-end fix floating out of constants in conditionals Message-ID: Content-Disposition: inline X-ClientProxiedBy: LO2P123CA0020.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:a6::32) To VI1PR08MB5325.eurprd08.prod.outlook.com (2603:10a6:803:13e::17) MIME-Version: 1.0 X-MS-TrafficTypeDiagnostic: VI1PR08MB5325:EE_|DBBPR08MB5900:EE_|AM7EUR03FT052:EE_|VE1PR08MB5613:EE_ X-MS-Office365-Filtering-Correlation-Id: 4961f00d-889c-48b9-ab4c-08da9d45020c x-checkrecipientrouted: true NoDisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: lA7qhZu4u17GOBVm9hvmN4WDjzLg/+ktBU9CbrmRbawM4ZpfTVg7zjdaPsiWijME2pltBBFmMXAH9Gs8VnF8z7YV/vqwAXcucc5BwrPDQ3xFhLlpz6oKj/jabhAJvuOOoED+PcNdKKkYBpOArVgQ8oefSAHXMFd8132aYfiKbUC42zkYVbNw0jO7eoN6GGdZaOq4H0zBOghXwuB3BbIfU2el4Kq+iupHxd6KjHZEVFiABIJBHg1cY8H9T5c4CgqCHmDQ9+JkoOYSP7RlpOcI14csxmKEl7hEkrq129+Aw42OEFFRnGmAGxG0/v0MdNzx4lPFMNL/4VcK/REb9p0H4onOEcdU+s+FmZse4qOLRnNnM4AdJ8x+EReOdNlPCJhFOnxSOFsANquDMiyjE1WO5CvzgcORWMjNsz+8RUsz1un+/FOwRaIvpcoLP709QY6GxGmHRgolR6q7hlYxYqit9Q45uix9a5Va+gY6xWvXHdgLdBGgGYd9P4mF/YG8ixYW6BHLYbfMecIyigXd24Ttp1VQroCTF+PK0jyXM85jJObqWyTPHAp9KWJiFBms+Z5IWjKe8dtl8AuJjQOfMNLItGerDkFPNzuPNgne3bEIg6kc9BUO8bt7XYN1+hfIMjXuaK7myIJTjYG2J6y21JIN3zPe0lRKpv6Taqa1JFOb7GB5Jw9s8adDj4cngFj2vpgo3Dj9TRI4oZbK8QgdL9XBbKW2zdscfkF3N4pHwAVU/Z0q2cXdxE/lle28EyGUftzZeRS9ZlrCBW71qz2Ya28dCyJHw/f8UQ7AGSWTes954va2TIuMkKGCHZWz1vns61l2 X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:VI1PR08MB5325.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(136003)(346002)(376002)(396003)(39860400002)(366004)(451199015)(44832011)(2906002)(66556008)(4326008)(66476007)(8676002)(66946007)(5660300002)(8936002)(235185007)(41300700001)(84970400001)(6486002)(186003)(6916009)(478600001)(2616005)(6666004)(6506007)(36756003)(316002)(4743002)(6512007)(44144004)(33964004)(26005)(83380400001)(38100700002)(86362001)(4216001)(2700100001); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR08MB5900 Original-Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM7EUR03FT052.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: b314513f-c04d-4726-fffc-08da9d44fc9b X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fHojDcUE+d0FBrHrl2mSDKRtHdjSUr1zn1IuYt4TMMT8OS3d4fT8pNr5NHbPPYFlbMebvsZTEKMmShSq74ZTDJgh3dyc+aXSAhKxiAxn8bYTMjUoPbrAs1kPZXJTN4R+KODcHH20+jNtjxOr7lhZzrcc43y/VLxg57YlxH1mjbigEQpmewfjs08vNt+6Z/VryIUQVwb0aF5tZGSMfFwHueDTHTdnWGa0UOaOjIfIOgRE8KXLuxgnkpf8RPElqtgDGiDE1ItPWpBL187NZROAvM2v201l4DJGCPcheyXiSKOHDSb5L+tV8EHcRySbq6il/BACZ+C487sZ5UpeBScgW+6YHw5MfpmG1jLs8iQBfMWg6e2M+3MfvOMd/O6Mxgi7+WCWHt8sFKaj4clRsQonz9nQw5+zBMGZd+v01IsUAAGV5zlPZDJyAD0oK2Wc52N1pwbVbcdWHColK78+i+SEZrRCsJ2NCv+A4+8M01Q7zIW0KjiTzrWVdeOn74an7WHBBiANJDZ81zaSoJHTbE40ha2s6bick3xBto3uz4B1EPfu9wGy3qKLjlJdYXfMnGA/sysSc8yfR5bWGhgPG+P6U+O3YS1ShW2BltCwSEmmfdUnawLAAYcXZb+p5mkZgFm7/dnVobNFGwOt2Q1nKfn+LKIlXpXEbIkdHtYmV+UyMuMh6qLLIiM4rsKY42MacN19Lbb+JO6wZlQK3lhY/bcRLzilgrwxwotd6eIGmbmi+bVONbbqiEucOA2LKE/6nJlQTSzhe1iHQA4FgYlzpAttfARerk5knTD5gg3J+b4GxJ8aerohp1UXVYxUiS1HNbg7LFTDUI19uWn3qu6CpkIhTYvmN4QY3l6F2FkDR1rbAls= X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFS:(13230022)(4636009)(376002)(39860400002)(136003)(346002)(396003)(451199015)(40470700004)(36840700001)(46966006)(83380400001)(6916009)(84970400001)(36756003)(44144004)(26005)(4743002)(6512007)(5660300002)(33964004)(2906002)(6666004)(107886003)(235185007)(8936002)(8676002)(70586007)(4326008)(41300700001)(70206006)(478600001)(40460700003)(6486002)(44832011)(316002)(6506007)(81166007)(47076005)(336012)(86362001)(2616005)(186003)(40480700001)(36860700001)(356005)(82310400005)(82740400003)(4216001)(2700100001); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Sep 2022 09:21:33.8254 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4961f00d-889c-48b9-ab4c-08da9d45020c X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: AM7EUR03FT052.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: VE1PR08MB5613 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FORGED_SPF_HELO, GIT_PATCH_0, KAM_DMARC_NONE, KAM_LOTSOFHASH, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tamar Christina via Gcc-patches From: Tamar Christina Reply-To: Tamar Christina Cc: nd@arm.com, rguenther@suse.de Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1744751768786595601?= X-GMAIL-MSGID: =?utf-8?q?1744751768786595601?= Hi All, The following testcase: int zoo1 (int a, int b, int c, int d) { return (a > b ? c : d) & 1; } gets de-optimized by the front-end since somewhere around GCC 4.x due to a fix that was added to fold_binary_op_with_conditional_arg. The folding is supposed to succeed only if we have folded at least one of the branches, however the check doesn't tests that all of the values are non-constant. So if one of the operators are a constant it accepts the folding. This ends up folding return (a > b ? c : d) & 1; into return (a > b ? c & 1 : d & 1); and thus performing the AND twice. change changes it to reject the folding if one of the arguments are a constant and if the operations being performed are the same. Secondly it adds a new match.pd rule to now also fold the opposite direction, so it now also folds: return (a > b ? c & 1 : d & 1); into return (a > b ? c : d) & 1; Bootstrapped Regtested on aarch64-none-linux-gnu, x86_64-pc-linux-gnu and issues. Ok for master? Thanks, Tamar gcc/ChangeLog: * fold-const.cc (fold_binary_op_with_conditional_arg): Add relaxation. * match.pd: Add ternary constant fold rule. * tree-cfg.cc (verify_gimple_assign_ternary): RHS1 of a COND_EXPR isn't a value but an expression itself. gcc/testsuite/ChangeLog: * gcc.target/aarch64/if-compare_3.c: New test. --- inline copy of patch -- diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 4f4ec81c8d4b6937ade3141a14c695b67c874c35..0ee083f290d12104969f1b335dc33917c97b4808 100644 --- diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 4f4ec81c8d4b6937ade3141a14c695b67c874c35..0ee083f290d12104969f1b335dc33917c97b4808 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -7212,7 +7212,9 @@ fold_binary_op_with_conditional_arg (location_t loc, } /* Check that we have simplified at least one of the branches. */ - if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs)) + if ((!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs)) + || (TREE_CONSTANT (arg) && TREE_CODE (lhs) == TREE_CODE (rhs) + && !TREE_CONSTANT (lhs))) return NULL_TREE; return fold_build3_loc (loc, cond_code, type, test, lhs, rhs); diff --git a/gcc/match.pd b/gcc/match.pd index b225d36dc758f1581502c8d03761544bfd499c01..b61ed70e69b881a49177f10f20c1f92712bb8665 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4318,6 +4318,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (op @3 (vec_cond:s @0 @1 @2)) (vec_cond @0 (op! @3 @1) (op! @3 @2)))) +/* Float out binary operations from branches if they can't be folded. + Fold (a ? (b op c) : (d op c)) --> (op (a ? b : d) c). */ +(for op (plus mult min max bit_and bit_ior bit_xor minus lshift rshift rdiv + trunc_div ceil_div floor_div round_div trunc_mod ceil_mod floor_mod + round_mod) + (simplify + (cond @0 (op @1 @2) (op @3 @2)) + (if (!FLOAT_TYPE_P (type) || !(HONOR_NANS (@1) && flag_trapping_math)) + (op (cond @0 @1 @3) @2)))) + #if GIMPLE (match (nop_atomic_bit_test_and_p @0 @1 @4) (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3)) diff --git a/gcc/testsuite/gcc.target/aarch64/if-compare_3.c b/gcc/testsuite/gcc.target/aarch64/if-compare_3.c new file mode 100644 index 0000000000000000000000000000000000000000..1d97da5c0d6454175881c219927471a567a6f0c7 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/if-compare_3.c @@ -0,0 +1,27 @@ +/* { dg-do compile } */ +/* { dg-additional-options "-O3 -std=c99" } */ +/* { dg-final { check-function-bodies "**" "" "" { target { le } } } } */ + +/* +**zoo: +** cmp w0, w1 +** csel w0, w3, w2, le +** ret +*/ +int zoo (int a, int b, int c, int d) +{ + return a > b ? c : d; +} + +/* +**zoo1: +** cmp w0, w1 +** csel w0, w3, w2, le +** and w0, w0, 1 +** ret +*/ +int zoo1 (int a, int b, int c, int d) +{ + return (a > b ? c : d) & 1; +} + diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc index b19710392940cf469de52d006603ae1e3deb6b76..aaf1b29da5c598add25dad2c38b828eaa89c49ce 100644 --- a/gcc/tree-cfg.cc +++ b/gcc/tree-cfg.cc @@ -4244,7 +4244,9 @@ verify_gimple_assign_ternary (gassign *stmt) return true; } - if (!is_gimple_val (rhs1) + /* In a COND_EXPR the rhs1 is the condition and thus isn't + a gimple value by definition. */ + if ((!is_gimple_val (rhs1) && rhs_code != COND_EXPR) || !is_gimple_val (rhs2) || !is_gimple_val (rhs3)) { --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -7212,7 +7212,9 @@ fold_binary_op_with_conditional_arg (location_t loc, } /* Check that we have simplified at least one of the branches. */ - if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs)) + if ((!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs)) + || (TREE_CONSTANT (arg) && TREE_CODE (lhs) == TREE_CODE (rhs) + && !TREE_CONSTANT (lhs))) return NULL_TREE; return fold_build3_loc (loc, cond_code, type, test, lhs, rhs); diff --git a/gcc/match.pd b/gcc/match.pd index b225d36dc758f1581502c8d03761544bfd499c01..b61ed70e69b881a49177f10f20c1f92712bb8665 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4318,6 +4318,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (op @3 (vec_cond:s @0 @1 @2)) (vec_cond @0 (op! @3 @1) (op! @3 @2)))) +/* Float out binary operations from branches if they can't be folded. + Fold (a ? (b op c) : (d op c)) --> (op (a ? b : d) c). */ +(for op (plus mult min max bit_and bit_ior bit_xor minus lshift rshift rdiv + trunc_div ceil_div floor_div round_div trunc_mod ceil_mod floor_mod + round_mod) + (simplify + (cond @0 (op @1 @2) (op @3 @2)) + (if (!FLOAT_TYPE_P (type) || !(HONOR_NANS (@1) && flag_trapping_math)) + (op (cond @0 @1 @3) @2)))) + #if GIMPLE (match (nop_atomic_bit_test_and_p @0 @1 @4) (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3)) diff --git a/gcc/testsuite/gcc.target/aarch64/if-compare_3.c b/gcc/testsuite/gcc.target/aarch64/if-compare_3.c new file mode 100644 index 0000000000000000000000000000000000000000..1d97da5c0d6454175881c219927471a567a6f0c7 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/if-compare_3.c @@ -0,0 +1,27 @@ +/* { dg-do compile } */ +/* { dg-additional-options "-O3 -std=c99" } */ +/* { dg-final { check-function-bodies "**" "" "" { target { le } } } } */ + +/* +**zoo: +** cmp w0, w1 +** csel w0, w3, w2, le +** ret +*/ +int zoo (int a, int b, int c, int d) +{ + return a > b ? c : d; +} + +/* +**zoo1: +** cmp w0, w1 +** csel w0, w3, w2, le +** and w0, w0, 1 +** ret +*/ +int zoo1 (int a, int b, int c, int d) +{ + return (a > b ? c : d) & 1; +} + diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc index b19710392940cf469de52d006603ae1e3deb6b76..aaf1b29da5c598add25dad2c38b828eaa89c49ce 100644 --- a/gcc/tree-cfg.cc +++ b/gcc/tree-cfg.cc @@ -4244,7 +4244,9 @@ verify_gimple_assign_ternary (gassign *stmt) return true; } - if (!is_gimple_val (rhs1) + /* In a COND_EXPR the rhs1 is the condition and thus isn't + a gimple value by definition. */ + if ((!is_gimple_val (rhs1) && rhs_code != COND_EXPR) || !is_gimple_val (rhs2) || !is_gimple_val (rhs3)) {