From patchwork Fri Sep 23 09:18:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tamar Christina X-Patchwork-Id: 1403 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp120772wrt; Fri, 23 Sep 2022 02:20:15 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4xcFlm7FOEA9mzRqFSrLDa3V8/irgvdYwp8ntwjuWfJnUniogpQsxihAMzQnbinTJ/0u0z X-Received: by 2002:a17:907:2d91:b0:782:7301:6602 with SMTP id gt17-20020a1709072d9100b0078273016602mr4165648ejc.107.1663924815186; Fri, 23 Sep 2022 02:20:15 -0700 (PDT) Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id t3-20020a056402524300b004485508bcfbsi8539260edd.30.2022.09.23.02.20.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Sep 2022 02:20:15 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=mGd6SHX0; arc=fail (signature failed); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 D1519385781D for ; Fri, 23 Sep 2022 09:20:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D1519385781D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663924813; bh=VIkzliJbhwjGR9BrQASKlQiJmAocHzgybj0GsBDKuoY=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=mGd6SHX0kz8Gua/Iw2Et/YLkSUEOniEgkiZ9if7rEonElrUhtGqTroqWzKXTcKFnl IVgKSzW6okhoR0FqRYwC2CIdvsZEY21bxiKK/IJP06yLyNL5b1CFD65gbvSbR9j5uf +DsdKovdnxWxTZHxQYYEcr6kE3OPGBurXJk3qzK8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03on2074.outbound.protection.outlook.com [40.107.104.74]) by sourceware.org (Postfix) with ESMTPS id 3352D385828B for ; Fri, 23 Sep 2022 09:19:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3352D385828B ARC-Seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass; b=gl1GrCrwT0HaauFbsJtcezQzZuHZCkdfZk4rH0tWOxKxhMUszUk2xGoL7IRS1kkpMtt5hmsXvooqCbcIIdCk5Bo7Hy6YBT8CG5Si4295rJkfPZLQz5CH4a5HdRVYKsDBFo0RO9VVb5sJVHUZ6MyFh1nW7mSY+s4MKYy0zVNB8i+IOyEWUxMkmGzvWg7BVhJrqiEyxiK2HgNSyLuCUBnoNzVjD1Zi4UkeUrVtP2evPDHp0oTPD+QNDD+lqpJhlbhbpMZ8ByNzzTW8aB0SHeWrG6DaEIWViJ5Dsa1KoNzQWrcATui9nY0AqABwxM/XiSHgjTv6xJcctvWfjBUtC9AC0A== 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=VIkzliJbhwjGR9BrQASKlQiJmAocHzgybj0GsBDKuoY=; b=XvmSko7p2frbUdPkD0nDyExORxHkHhC8qbn6N1mKIJ5VUHj5yaGA7kA64kVlExXUt7yOUMGA2OtiOFplLjclDCrJ3LhkWj/ADYhUXClx+N3kqohICdJXm3N9uosFQ+xc+yl/ZZO3g5oyB2/GITavBbcKGm25eOLGFnOJPg9ehfpdsYynwfkYou80DSxeAR7IUQHRhR+zGL3ZUFzoT0QvynBi1nrtXpJXA4aIJDLDuc0UIqn45kI9maRXq/wY9QlHHxluey4RSLNGd6EYfQZoE/KEqaWY66i2mutKYMZ7PFlQ2125jig0bCsadyeWdNyEXqTgx/u1r3/sWdVJTgVRfQ== 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 DU2PR04CA0030.eurprd04.prod.outlook.com (2603:10a6:10:3b::35) by DU0PR08MB9680.eurprd08.prod.outlook.com (2603:10a6:10:444::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5654.16; Fri, 23 Sep 2022 09:19:21 +0000 Received: from DBAEUR03FT044.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:3b:cafe::af) by DU2PR04CA0030.outlook.office365.com (2603:10a6:10:3b::35) 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:19:21 +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 DBAEUR03FT044.mail.protection.outlook.com (100.127.142.189) 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:19:21 +0000 Received: ("Tessian outbound 0a0431bdcdb4:v124"); Fri, 23 Sep 2022 09:19:21 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 0aa4c897fc2cdbf5 X-CR-MTA-TID: 64aa7808 Received: from 6c0dfce68c84.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id B45226D1-E605-478D-A2F7-666E6F03EA7C.1; Fri, 23 Sep 2022 09:18:21 +0000 Received: from EUR01-VE1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 6c0dfce68c84.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Fri, 23 Sep 2022 09:18:21 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dfzdpDC24yRAX4CxCuJmf91UBm8KIkKcdEtzUwXIFphNwy4ZYxgMgUzz+dhgbB+P3sn4f0QQ47Wy9Ne//ufLn9V/fO8iY/hD3j+ytWejR0NIJ1I09NrMMm7psz/zAJBXFg5Z64D2bI1xn5oxTDuHwMfkjqNeb9dfiuYbHY+ksy9gKo0ZW0KvZJ13KIdhSpliI97vszG+RE8QOKT+UBKaHejfhmFf1zYOYKRAwp7n3MvYkaGhXzetb1uJwWlONat0Mu2d5ndxWHhjzt2gkCUisXdPEDKBXLRRWNiA6F/1e5ZFrJLJ807nBV0Eo8/GLwaTE3B4ICZbTkt34ontL6giYw== 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=VIkzliJbhwjGR9BrQASKlQiJmAocHzgybj0GsBDKuoY=; b=m5kIQYLFkq5zCJ8m0RgKwpybt0ObjSjsy1HHLvyCblP2XKqKA8PtyddjHiKkQF+EocMDboF0iZ7Zf2PMaEq1KZLJcdl+RVvNH0p/qXtaZ75M5xx+PV0jpTxmRoj6lFpv22iwsaeT9hg2wWLfB5PMjKqoo4NtS4TwA+5of2jKuDbWczGbcWuEAiSQtXWFxtt/41srUYQbvVnu0ZDc5QzrHjAvZqnJ2F5epz+P5c/mmvMez4X0b/gN7N+u6aocprRTXBxgtwaGpAKwkc5hgLQFZmcFeGoTCmb8B+DCUAK8jpi2nJubAZ3b+4QpPQrnqWgxzk3bDYBbiL10k+jyFz0neg== 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 DU0PR08MB8113.eurprd08.prod.outlook.com (2603:10a6:10:3e9::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5632.18; Fri, 23 Sep 2022 09:18:18 +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:18:17 +0000 Date: Fri, 23 Sep 2022 10:18:14 +0100 To: gcc-patches@gcc.gnu.org Subject: [PATCH]middle-end Recognize more conditional comparisons idioms. Message-ID: Content-Disposition: inline X-ClientProxiedBy: LO2P265CA0350.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:d::26) To VI1PR08MB5325.eurprd08.prod.outlook.com (2603:10a6:803:13e::17) MIME-Version: 1.0 X-MS-TrafficTypeDiagnostic: VI1PR08MB5325:EE_|DU0PR08MB8113:EE_|DBAEUR03FT044:EE_|DU0PR08MB9680:EE_ X-MS-Office365-Filtering-Correlation-Id: 6e618b2f-b652-459a-0fff-08da9d44b350 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: hf7iovnd08s/zF0Cbr/YEYaVhUNO08KPj90r3GH+9eemaoR+5SVnyY9wCVrnSaQlqG+aQ0Z834KgFE+pn42DRLpfbkFsvIeC5U9tBBuKwBqWzRcrEdTx9m4ppwuiZBBOpa48eAONHENaxn8F980C93tak3PF5Z7R5yxlIAyqZtu1+/niVvb+d4bo4u+zoCNZs8whNrYG3ZRt+Epx4/J2UBUCBuFK0pmy48pcydnqOqxlZLxDJoHMUfxXfFCTWbZYTlzT4LKTpk46hJ/iUAQrGD4+6RO0NENGEFK4V5cXsHnljJjs9m8UCLWyvtY9ABza7+cy1TkuzsPiuqEwiuNbi+5f//7Ea3fa3cMijou9zRmKmh5o67N0ZCAvrZxeKTy7MELxqst6IQhnkzSpZWU/xcHDe+FtGnZK6LRffniwqPSpHTP3zCNsno1XdKZ4MK32BB+FFFoRwD0yTwV0hjINNxo7A1cVnmxQr98HMYnQyGCYtesEIiy6YsLp1ju2ez3sArcAA1NAEaBEsqbL4GiSAuViRebAAAcUV8WKgDEDCLcjGVXULxhCIZYEDzLxgK44OOi5IogQijtcXOUGrnrzSa4tR6bHTAQgL//F4Ot5ZLnx2DXpOc5HdLB4OxpWsal/K4I7NGyuIycYSC8v1B1ZckKUYjncjH8H6q2J4bZr18SqQKgp+nO73gB3afDzodF02/LrlhBPaYzqx+/8VZzV0J6zL7JhmK3S471m7D2ZPgoApKsUWggWqMd0j5JCF2Nnp/auMc4bUbK6LdSYV6B3iW6gMtfVbvjZaf76EJPCqRTUZz/Wm2fVd+VzuDB6ME2zKUIYTkV6NAVAwVBbdCLgsvTxgSlYDb2mBwngD+Hs0A0= 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)(39860400002)(346002)(136003)(366004)(376002)(396003)(451199015)(316002)(26005)(6512007)(66899012)(33964004)(44144004)(41300700001)(38100700002)(6666004)(235185007)(44832011)(66946007)(2616005)(5660300002)(66476007)(66556008)(84970400001)(8676002)(4326008)(478600001)(6506007)(36756003)(6486002)(86362001)(2906002)(186003)(8936002)(83380400001)(6916009)(4216001)(2700100001); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU0PR08MB8113 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: DBAEUR03FT044.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 72a84c0b-9b12-4acc-1c04-08da9d448cfc X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: WV9A4xyKoOEHY2pgOq4F1klC+rOLU46uh/Xay7Hqk+jqkisEnI5hO31D400VJjSypDl6GIn7oxsg0GUHtl51pISbw/RxYG1CPTc96CnW30YDSBEF3ffonoqTq5ZegEL07gReQ5hLnHBBlZQkxeQkt1ia1NvrheVpP9W1aViCCP+vnx89sGa9Kn1XPCp5aqO1+BkUf96QvrSNENdTc2STo1/MeFb85gIHbQnr6RXlbf1CotGlFlFDOJlV/zbVtXsaiERwCscjkNN5ja8MKIGUlH8x38w7Y+HvwlAm8b7VJm64AKc3v3QuOlJwdFPtZwDNIFExHJ0Yf89IawJgWAtCW34mXm/uE+goccMUBJYcgXzW1J/Hk0BWjtkTwrR9eJeQ/1T57RhkqOyqdYMO6IUEalFeIfLSNSrHctzzNaGngW7Qxw7+8C6tOvGCsvpu/46GH2ATtzfiwj1jOca4sTfUil8f1U/uotiLlVg18GKVPVmyCnpKRj0HATzekMuxrwOEpH07GCV7ZM8TV4KH659KkoqJSjX6ytLoxLLBTKKeo5HMp1W5hACwJ5l3P68CRPP7loeAGWYTznNeSDHfj14w9miUY8eMD9h0M5prp7R9t8ntpjvyNsbFE+T93kiH7tJWHbmSxw/TK+yEDyHRW4O7yInsIYUoVm5AjKXFbmxjM/KC5xeOhRQLohEpGrt51LwqvLslnQPc2bYbrLg8kLY0im9uStpNjEaFx3V76o/ER7hy2dgzqMhcHaL2Vp0M1RmLPiFqXLTTNBSD9JfiteF0yTHkl6zaKgaDV1rYU4jeGHxe48OeEdwEqC2tXuq7JU8a4PLEPKTYwGblSbfT6cJAQEtf9KoMT9x6nTmC8QCGw2zQYfm9jKKn4ojo67n+oBau/f+40BQYUg7T/cfzxVE+Tw== 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)(396003)(346002)(451199015)(36840700001)(46966006)(40470700004)(86362001)(6506007)(33964004)(336012)(82310400005)(186003)(83380400001)(40480700001)(2616005)(82740400003)(47076005)(107886003)(44144004)(6666004)(6512007)(41300700001)(36860700001)(26005)(66899012)(84970400001)(36756003)(81166007)(40460700003)(356005)(4326008)(44832011)(8936002)(8676002)(6916009)(5660300002)(70206006)(235185007)(316002)(2906002)(6486002)(70586007)(478600001)(4216001)(2700100001); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Sep 2022 09:19:21.7895 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6e618b2f-b652-459a-0fff-08da9d44b350 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: DBAEUR03FT044.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU0PR08MB9680 X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_DMARC_NONE, KAM_LOTSOFHASH, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, 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?1744751627126149497?= X-GMAIL-MSGID: =?utf-8?q?1744751627126149497?= Hi All, GCC currently recognizes some of these for signed but not unsigned types. It also has trouble dealing with casts in between because these are handled by the fold machinery. This moves the pattern detection to match.pd instead. We fold e.g.: uint32_t min1_32u(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { uint32_t result; uint32_t m = (a >= b) - 1; result = (c & m) | (d & ~m); return result; } into a >= b ? c : d for all integral types. Bootstrapped Regtested on aarch64-none-linux-gnu and no issues. Ok for master? Thanks, Tamar gcc/ChangeLog: * match.pd: New cond select pattern. gcc/testsuite/ChangeLog: * gcc.dg/select_cond_1.c: New test. * gcc.dg/select_cond_2.c: New test. * gcc.dg/select_cond_3.c: New test. --- inline copy of patch -- diff --git a/gcc/match.pd b/gcc/match.pd index 39da61bf117a6eb2924fc8a6473fb37ddadd60e9..7b8f50410acfd0afafc5606e972cfc4e125d3a5d 100644 --- diff --git a/gcc/match.pd b/gcc/match.pd index 39da61bf117a6eb2924fc8a6473fb37ddadd60e9..7b8f50410acfd0afafc5606e972cfc4e125d3a5d 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3577,6 +3577,25 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1) (max @2 @1)) +/* (a & ((c `op` d) - 1)) | (b & ~((c `op` d) - 1)) -> c `op` d ? a : b. */ +(for op (simple_comparison) + (simplify + (bit_xor:c + (convert2? @0) + (bit_and:c + (convert2? (bit_xor:c @1 @0)) + (convert3? (negate (convert? (op@4 @2 @3)))))) + /* Alternative form, where some canonicalization were not done due to the + arguments being signed. */ + (if (INTEGRAL_TYPE_P (type) && tree_zero_one_valued_p (@4)) + (convert:type (cond @4 @1 @0)))) + (simplify + (bit_ior:c + (mult:c @0 (convert (convert2? (op@4 @2 @3)))) + (bit_and:c @1 (convert (plus:c integer_minus_onep (convert (op@4 @2 @3)))))) + (if (INTEGRAL_TYPE_P (type) && tree_zero_one_valued_p (@4)) + (cond @4 @0 @1)))) + /* Simplifications of shift and rotates. */ (for rotate (lrotate rrotate) diff --git a/gcc/testsuite/gcc.dg/select_cond_1.c b/gcc/testsuite/gcc.dg/select_cond_1.c new file mode 100644 index 0000000000000000000000000000000000000000..9eb9959baafe5fffeec24e4e3ae656f8fcfe943c --- /dev/null +++ b/gcc/testsuite/gcc.dg/select_cond_1.c @@ -0,0 +1,97 @@ +/* { dg-do run } */ +/* { dg-additional-options "-O2 -std=c99 -fdump-tree-optimized -save-temps" } */ + +#include + +__attribute__((noipa, noinline)) +uint32_t min1_32u(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t max1_32u(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a <= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min2_32u(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a > b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t max2_32u(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a < b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min3_32u(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a == b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t max3_32u(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a != b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +/* { dg-final { scan-tree-dump-times {_[0-9]+ \? c_[0-9]+\(D\) : d_[0-9]+\(D\)} 6 "optimized" } } */ + +extern void abort (); + +int main () { + + if (min1_32u (3, 5, 7 , 8) != 7) + abort (); + + if (max1_32u (3, 5, 7 , 8) != 8) + abort (); + + if (min1_32u (5, 3, 7 , 8) != 8) + abort (); + + if (max1_32u (5, 3, 7 , 8) != 7) + abort (); + + if (min2_32u (3, 5, 7 , 8) != 7) + abort (); + + if (max2_32u (3, 5, 7 , 8) != 8) + abort (); + + if (min2_32u (5, 3, 7 , 8) != 8) + abort (); + + if (max2_32u (5, 3, 7 , 8) != 7) + abort (); + + if (min3_32u (3, 5, 7 , 8) != 7) + abort (); + + if (max3_32u (3, 5, 7 , 8) != 8) + abort (); + + if (min3_32u (5, 3, 7 , 8) != 7) + abort (); + + if (max3_32u (5, 3, 7 , 8) != 8) + abort (); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/select_cond_2.c b/gcc/testsuite/gcc.dg/select_cond_2.c new file mode 100644 index 0000000000000000000000000000000000000000..623b2272ee7b4b00130d5e9fb8c781dbc5d4189e --- /dev/null +++ b/gcc/testsuite/gcc.dg/select_cond_2.c @@ -0,0 +1,99 @@ +/* { dg-do run } */ +/* { dg-additional-options "-O2 -std=c99 -fdump-tree-optimized -save-temps" } */ + +#include + +__attribute__((noipa, noinline)) +uint32_t min1_32u(uint32_t a, uint32_t b) { + uint32_t result; + uint32_t m = (a >= b) - 1; + result = (a & m) | (b & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t max1_32u(uint32_t a, uint32_t b) { + uint32_t result; + uint32_t m = (a <= b) - 1; + result = (a & m) | (b & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min2_32u(uint32_t a, uint32_t b) { + uint32_t result; + uint32_t m = (a > b) - 1; + result = (a & m) | (b & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t max2_32u(uint32_t a, uint32_t b) { + uint32_t result; + uint32_t m = (a < b) - 1; + result = (a & m) | (b & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min3_32u(uint32_t a, uint32_t b) { + uint32_t result; + uint32_t m = (a == b) - 1; + result = (a & m) | (b & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t max3_32u(uint32_t a, uint32_t b) { + uint32_t result; + uint32_t m = (a != b) - 1; + result = (a & m) | (b & ~m); + return result; +} + +/* { dg-final { scan-tree-dump-not {_[0-9]+ \? c_[0-9]+\(D\) : d_[0-9]+\(D\)} "optimized" } } */ +/* { dg-final { scan-tree-dump-times {MIN_EXPR} 2 "optimized" } } */ +/* { dg-final { scan-tree-dump-times {MAX_EXPR} 2 "optimized" } } */ + +extern void abort (); + +int main () { + + if (min1_32u (7 , 8) != 7) + abort (); + + if (max1_32u (7 , 8) != 8) + abort (); + + if (min1_32u (7 , 8) != 7) + abort (); + + if (max1_32u (7, 8) != 8) + abort (); + + if (min2_32u (7 , 8) != 7) + abort (); + + if (max2_32u (7 , 8) != 8) + abort (); + + if (min2_32u (7 , 8) != 7) + abort (); + + if (max2_32u (7 , 8) != 8) + abort (); + + if (min3_32u (7 , 8) != 7) + abort (); + + if (max3_32u (7 , 8) != 8) + abort (); + + if (min3_32u (7 , 8) != 7) + abort (); + + if (max3_32u (7 , 8) != 8) + abort (); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/select_cond_3.c b/gcc/testsuite/gcc.dg/select_cond_3.c new file mode 100644 index 0000000000000000000000000000000000000000..754e73b121ce360564481b1fa7e1dd4fb27642a3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/select_cond_3.c @@ -0,0 +1,101 @@ +/* { dg-do run } */ +/* { dg-additional-options "-O2 -std=c99 -fdump-tree-optimized -save-temps" } */ + +#include + +__attribute__((noipa, noinline)) +int32_t min1_32u1(int32_t a, int32_t b, int32_t c, int32_t d) { + uint32_t result; + uint32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min1_32u2(uint32_t a, uint32_t b, int32_t c, int32_t d) { + uint32_t result; + uint32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min1_32u3(uint32_t a, uint32_t b, uint32_t c, int32_t d) { + uint32_t result; + uint32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min1_32u4(uint32_t a, uint32_t b, int32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min1_32u5(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min1_32u6(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + int32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min1_32u7(uint8_t a, uint16_t b, uint32_t c, uint32_t d) { + uint32_t result; + int32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min1_32u8(uint32_t a, uint64_t b, uint32_t c, uint32_t d) { + uint32_t result; + int32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +/* { dg-final { scan-tree-dump-times {_[0-9]+ \? c[^ ]+ : d[^ ]+;} 8 "optimized" } } */ + +extern void abort (); + +int main () { + + if (min1_32u1 (3, 5, 7 , 8) != 7) + abort (); + + if (min1_32u2 (3, 5, 7 , 8) != 7) + abort (); + + if (min1_32u3 (3, 5, 7 , 8) != 7) + abort (); + + if (min1_32u4 (3, 5, 7 , 8) != 7) + abort (); + + if (min1_32u5 (3, 5, 7 , 8) != 7) + abort (); + + if (min1_32u6 (3, 5, 7 , 8) != 7) + abort (); + + if (min1_32u7 (3, 5, 7 , 8) != 7) + abort (); + + if (min1_32u8 (3, 5, 7 , 8) != 7) + abort (); + + return 0; +} --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3577,6 +3577,25 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1) (max @2 @1)) +/* (a & ((c `op` d) - 1)) | (b & ~((c `op` d) - 1)) -> c `op` d ? a : b. */ +(for op (simple_comparison) + (simplify + (bit_xor:c + (convert2? @0) + (bit_and:c + (convert2? (bit_xor:c @1 @0)) + (convert3? (negate (convert? (op@4 @2 @3)))))) + /* Alternative form, where some canonicalization were not done due to the + arguments being signed. */ + (if (INTEGRAL_TYPE_P (type) && tree_zero_one_valued_p (@4)) + (convert:type (cond @4 @1 @0)))) + (simplify + (bit_ior:c + (mult:c @0 (convert (convert2? (op@4 @2 @3)))) + (bit_and:c @1 (convert (plus:c integer_minus_onep (convert (op@4 @2 @3)))))) + (if (INTEGRAL_TYPE_P (type) && tree_zero_one_valued_p (@4)) + (cond @4 @0 @1)))) + /* Simplifications of shift and rotates. */ (for rotate (lrotate rrotate) diff --git a/gcc/testsuite/gcc.dg/select_cond_1.c b/gcc/testsuite/gcc.dg/select_cond_1.c new file mode 100644 index 0000000000000000000000000000000000000000..9eb9959baafe5fffeec24e4e3ae656f8fcfe943c --- /dev/null +++ b/gcc/testsuite/gcc.dg/select_cond_1.c @@ -0,0 +1,97 @@ +/* { dg-do run } */ +/* { dg-additional-options "-O2 -std=c99 -fdump-tree-optimized -save-temps" } */ + +#include + +__attribute__((noipa, noinline)) +uint32_t min1_32u(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t max1_32u(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a <= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min2_32u(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a > b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t max2_32u(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a < b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min3_32u(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a == b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t max3_32u(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a != b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +/* { dg-final { scan-tree-dump-times {_[0-9]+ \? c_[0-9]+\(D\) : d_[0-9]+\(D\)} 6 "optimized" } } */ + +extern void abort (); + +int main () { + + if (min1_32u (3, 5, 7 , 8) != 7) + abort (); + + if (max1_32u (3, 5, 7 , 8) != 8) + abort (); + + if (min1_32u (5, 3, 7 , 8) != 8) + abort (); + + if (max1_32u (5, 3, 7 , 8) != 7) + abort (); + + if (min2_32u (3, 5, 7 , 8) != 7) + abort (); + + if (max2_32u (3, 5, 7 , 8) != 8) + abort (); + + if (min2_32u (5, 3, 7 , 8) != 8) + abort (); + + if (max2_32u (5, 3, 7 , 8) != 7) + abort (); + + if (min3_32u (3, 5, 7 , 8) != 7) + abort (); + + if (max3_32u (3, 5, 7 , 8) != 8) + abort (); + + if (min3_32u (5, 3, 7 , 8) != 7) + abort (); + + if (max3_32u (5, 3, 7 , 8) != 8) + abort (); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/select_cond_2.c b/gcc/testsuite/gcc.dg/select_cond_2.c new file mode 100644 index 0000000000000000000000000000000000000000..623b2272ee7b4b00130d5e9fb8c781dbc5d4189e --- /dev/null +++ b/gcc/testsuite/gcc.dg/select_cond_2.c @@ -0,0 +1,99 @@ +/* { dg-do run } */ +/* { dg-additional-options "-O2 -std=c99 -fdump-tree-optimized -save-temps" } */ + +#include + +__attribute__((noipa, noinline)) +uint32_t min1_32u(uint32_t a, uint32_t b) { + uint32_t result; + uint32_t m = (a >= b) - 1; + result = (a & m) | (b & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t max1_32u(uint32_t a, uint32_t b) { + uint32_t result; + uint32_t m = (a <= b) - 1; + result = (a & m) | (b & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min2_32u(uint32_t a, uint32_t b) { + uint32_t result; + uint32_t m = (a > b) - 1; + result = (a & m) | (b & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t max2_32u(uint32_t a, uint32_t b) { + uint32_t result; + uint32_t m = (a < b) - 1; + result = (a & m) | (b & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min3_32u(uint32_t a, uint32_t b) { + uint32_t result; + uint32_t m = (a == b) - 1; + result = (a & m) | (b & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t max3_32u(uint32_t a, uint32_t b) { + uint32_t result; + uint32_t m = (a != b) - 1; + result = (a & m) | (b & ~m); + return result; +} + +/* { dg-final { scan-tree-dump-not {_[0-9]+ \? c_[0-9]+\(D\) : d_[0-9]+\(D\)} "optimized" } } */ +/* { dg-final { scan-tree-dump-times {MIN_EXPR} 2 "optimized" } } */ +/* { dg-final { scan-tree-dump-times {MAX_EXPR} 2 "optimized" } } */ + +extern void abort (); + +int main () { + + if (min1_32u (7 , 8) != 7) + abort (); + + if (max1_32u (7 , 8) != 8) + abort (); + + if (min1_32u (7 , 8) != 7) + abort (); + + if (max1_32u (7, 8) != 8) + abort (); + + if (min2_32u (7 , 8) != 7) + abort (); + + if (max2_32u (7 , 8) != 8) + abort (); + + if (min2_32u (7 , 8) != 7) + abort (); + + if (max2_32u (7 , 8) != 8) + abort (); + + if (min3_32u (7 , 8) != 7) + abort (); + + if (max3_32u (7 , 8) != 8) + abort (); + + if (min3_32u (7 , 8) != 7) + abort (); + + if (max3_32u (7 , 8) != 8) + abort (); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/select_cond_3.c b/gcc/testsuite/gcc.dg/select_cond_3.c new file mode 100644 index 0000000000000000000000000000000000000000..754e73b121ce360564481b1fa7e1dd4fb27642a3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/select_cond_3.c @@ -0,0 +1,101 @@ +/* { dg-do run } */ +/* { dg-additional-options "-O2 -std=c99 -fdump-tree-optimized -save-temps" } */ + +#include + +__attribute__((noipa, noinline)) +int32_t min1_32u1(int32_t a, int32_t b, int32_t c, int32_t d) { + uint32_t result; + uint32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min1_32u2(uint32_t a, uint32_t b, int32_t c, int32_t d) { + uint32_t result; + uint32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min1_32u3(uint32_t a, uint32_t b, uint32_t c, int32_t d) { + uint32_t result; + uint32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min1_32u4(uint32_t a, uint32_t b, int32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min1_32u5(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + uint32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min1_32u6(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + uint32_t result; + int32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min1_32u7(uint8_t a, uint16_t b, uint32_t c, uint32_t d) { + uint32_t result; + int32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +__attribute__((noipa, noinline)) +uint32_t min1_32u8(uint32_t a, uint64_t b, uint32_t c, uint32_t d) { + uint32_t result; + int32_t m = (a >= b) - 1; + result = (c & m) | (d & ~m); + return result; +} + +/* { dg-final { scan-tree-dump-times {_[0-9]+ \? c[^ ]+ : d[^ ]+;} 8 "optimized" } } */ + +extern void abort (); + +int main () { + + if (min1_32u1 (3, 5, 7 , 8) != 7) + abort (); + + if (min1_32u2 (3, 5, 7 , 8) != 7) + abort (); + + if (min1_32u3 (3, 5, 7 , 8) != 7) + abort (); + + if (min1_32u4 (3, 5, 7 , 8) != 7) + abort (); + + if (min1_32u5 (3, 5, 7 , 8) != 7) + abort (); + + if (min1_32u6 (3, 5, 7 , 8) != 7) + abort (); + + if (min1_32u7 (3, 5, 7 , 8) != 7) + abort (); + + if (min1_32u8 (3, 5, 7 , 8) != 7) + abort (); + + return 0; +}