From patchwork Tue Nov 8 20:02:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Collison X-Patchwork-Id: 17199 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2920622wru; Tue, 8 Nov 2022 12:02:52 -0800 (PST) X-Google-Smtp-Source: AMsMyM45SK4aHIt3pFyMqDSwbxTjgmDmupxpmclsvKuE/sQ91PWNJv8bjizpNyofIZAMJTpOfmIU X-Received: by 2002:a17:907:2bc1:b0:7ad:d3a4:9df6 with SMTP id gv1-20020a1709072bc100b007add3a49df6mr47315721ejc.188.1667937772199; Tue, 08 Nov 2022 12:02:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667937772; cv=none; d=google.com; s=arc-20160816; b=kEednUel8t+wmH6nbaLMLx+oHUz4ZkKvVgZNy2+JhVFeL/vKV6ecANe0MLqen8dfAB lTMsGU/N7p1em5FTxw4hTdwI0cqzIRq1pRyELSDJw+clijuKC5U7qTlvxKT3PDsNiCEc k3cY/B2w7MDw1RJbVVOsa59SlZ30wrI6Dy+nTVDsKvPvuoviFBO9C0A+LNVC5Paw52YQ 7ohL1/bX0AsiskaRf3TveJhT/ox76WACYYSDHESMMi8PhWg2IM+IFWoHK9eGtKoHA5Dc Lo6CUX0x8cPuRK3rjsB7IscKKD+ZKv+rOKh6Y1/cfY8+38kPIVaVYcEiolCTAOuOywiS pTjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding:cc :content-language:to:subject:from:user-agent:mime-version:date :message-id:dkim-signature:dmarc-filter:delivered-to; bh=vI9CKPcCFEpvuL+oicQElNkVNwQBJIOrvADKk/DWfYM=; b=h+hD2YVjgaI9sqjM+ZlrkpiBu94mTqq/jQ4PISVfZTfjwByArE3oulo1LONr1IFBmK wyQj76Ml/AcAEb/E90K/yNPKNvqfz6bDsxyvF99fvnKv5UmywiK5jla7NgtBBE/w5Rqa nrOaGwFyk+FwT2oofU1UgxlvIYZKCAMENQ0FSnmchnaCxw4MMhXVYIRhH0fNA4Id1T+q cWSL7PqOGkwbYyZgUnxl4EK2Z6U7jfpJmQtcQzYvt0lEhODdt7TEnfRjrM7ZNUxaTqTf VLnZDqiI8ZA0jmVijs4MEIEwtgicK/iKQX8YssAeprIM/BDsO71gDbUKWtaGPPFP4ya+ 2JwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=xk2LHGFU; 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" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id n14-20020a05640205ce00b004511d552be3si16972046edx.4.2022.11.08.12.02.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 12:02:52 -0800 (PST) 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=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=xk2LHGFU; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2ADF5385843F for ; Tue, 8 Nov 2022 20:02:51 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by sourceware.org (Postfix) with ESMTPS id EB7213858D3C for ; Tue, 8 Nov 2022 20:02:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EB7213858D3C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-qk1-x735.google.com with SMTP id v8so9754384qkg.12 for ; Tue, 08 Nov 2022 12:02:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:cc:content-language:to:subject:from :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=vI9CKPcCFEpvuL+oicQElNkVNwQBJIOrvADKk/DWfYM=; b=xk2LHGFUrfLMcVZ952yB8eAwEo+koAAMFGgqrMtVDawogDvALXFV8hkCYBk250jF3r hgqn6ni+ahrVfmVF9JlBB1A2tnulPb/yG6RI+/BEvpxNkr1A8QzkUfTFGR+OzGBOvCPM JGieiHtdkJVC6RseyciIX1LIJ6dAEnlBeelR4PyYiU81nIa8ykeK71wihEcYs7z8QSIb TZ6Qy30s8dCyiuJCNf6fcFlDSP02mYhK+VLFwF5ylOJhijoVHmBx4S4giXhn4ekUn2dO BMPDfUIrFN1XFgUP/fPWJMjGkgCGlHivJBRazmqBwCTCdPLiYt7qXmbAV8jJ5aCq670+ pN9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:content-language:to:subject:from :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=vI9CKPcCFEpvuL+oicQElNkVNwQBJIOrvADKk/DWfYM=; b=CLfUfZO1+kvMxa+wRRA4q6cbdez0lTWuBukNRCurM3pftpYRTTBkWkkUcmw+OIuK/F G9M6EhZe4kKt1UzQcBJXo1+sj+rWgupbqirVuMlWQKZ890p8sCZex8WZhNvmS301zKXy sNDVTgczyPBGTZi8riAVclfQRIIuKGRSoTktV+aKYimJIoq4byABCHqnE3kpHd6dqgZF Y9h/r3XWzNPBENnKXn9pA/mYdRhuYye4RoIF98xpzNMv2tQ6+2FiKHCXOLwnc4mo8wER MyaAKRHUAlPP0NBdCxLaR4BRLtaHfvQqVejC0e6tBKDtMF4owSSJ3Xy8aaazihLWZAzZ oWyg== X-Gm-Message-State: ACrzQf0erwHInzKnGQE7flhK9WP6/hEcwww+icPkBeYSr+CAeE84RVyt tA3JhdXK6/9G28wNjIJyvGbAQ7a4kKIfGw== X-Received: by 2002:ae9:f406:0:b0:6fa:395d:1480 with SMTP id y6-20020ae9f406000000b006fa395d1480mr32238331qkl.555.1667937741115; Tue, 08 Nov 2022 12:02:21 -0800 (PST) Received: from [192.168.86.117] ([136.57.172.92]) by smtp.gmail.com with ESMTPSA id n16-20020a05620a295000b006ce0733caebsm10122166qkp.14.2022.11.08.12.02.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 08 Nov 2022 12:02:20 -0800 (PST) Message-ID: Date: Tue, 8 Nov 2022 15:02:20 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 From: Michael Collison Subject: [PATCH] match.pd: rewrite select to branchless expression To: gcc-patches@gcc.gnu.org Content-Language: en-US Cc: Jeff Law , "jakub@redhat.com >> Jakub Jelinek" X-Spam-Status: No, score=-8.4 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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: , 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?1748959517391444765?= X-GMAIL-MSGID: =?utf-8?q?1748959517391444765?= This patches transforms (cond (and (x , 0x1) == 0), y, (z op y)) into (-(and (x , 0x1)) & z ) op y, where op is a '^' or a '|'. It also transforms (cond (and (x , 0x1) != 0), (z op y), y ) into (-(and (x , 0x1)) & z ) op y. Matching this patterns allows GCC to generate branchless code for one of the functions in coremark. Bootstrapped and tested on x86 and RISC-V. Okay? Michael. 2022-11-08  Michael Collison      * match.pd ((cond (and (x , 0x1) == 0), y, (z op y) )     -> (-(and (x , 0x1)) & z ) op y) 2022-11-08  Michael Collison      * gcc.dg/tree-ssa/branchless-cond.c: New test. ---  gcc/match.pd                                  | 22 ++++++++++++++++  .../gcc.dg/tree-ssa/branchless-cond.c         | 26 +++++++++++++++++++  2 files changed, 48 insertions(+)  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/branchless-cond.c diff --git a/gcc/match.pd b/gcc/match.pd index 194ba8f5188..722f517ac6d 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3486,6 +3486,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)    (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)    (max @2 @1)) +/* (cond (and (x , 0x1) == 0), y, (z ^ y) ) -> (-(and (x , 0x1)) & z ) ^ y */ +(for op (bit_xor bit_ior) + (simplify +  (cond (eq (bit_and @0 integer_onep@1) +            integer_zerop) +        @2 +        (op:c @3 @2)) +  (if (INTEGRAL_TYPE_P (type) +       && (INTEGRAL_TYPE_P (TREE_TYPE (@0)))) +       (op (bit_and (negate (convert:type (bit_and @0 @1))) @3) @2)))) + +/* (cond (and (x , 0x1) != 0), (z ^ y), y ) -> (-(and (x , 0x1)) & z ) ^ y */ +(for op (bit_xor bit_ior) + (simplify +  (cond (ne (bit_and @0 integer_onep@1) +            integer_zerop) +    (op:c @3 @2) +        @2) +  (if (INTEGRAL_TYPE_P (type) +       && (INTEGRAL_TYPE_P (TREE_TYPE (@0)))) +       (op (bit_and (negate (convert:type (bit_and @0 @1))) @3) @2)))) +  /* Simplifications of shift and rotates.  */  (for rotate (lrotate rrotate) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/branchless-cond.c b/gcc/testsuite/gcc.dg/tree-ssa/branchless-cond.c new file mode 100644 index 00000000000..68087ae6568 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/branchless-cond.c @@ -0,0 +1,26 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int f1(unsigned int x, unsigned int y, unsigned int z) +{ +  return ((x & 1) == 0) ? y : z ^ y; +} + +int f2(unsigned int x, unsigned int y, unsigned int z) +{ +  return ((x & 1) != 0) ? z ^ y : y; +} + +int f3(unsigned int x, unsigned int y, unsigned int z) +{ +  return ((x & 1) == 0) ? y : z | y; +} + +int f4(unsigned int x, unsigned int y, unsigned int z) +{ +  return ((x & 1) != 0) ? z | y : y; +} + +/* { dg-final { scan-tree-dump-times " -" 4 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " & " 8 "optimized" } } */ +/* { dg-final { scan-tree-dump-not "if" "optimized" } } */