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: 1701481 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=rivosinc-com.20210112.gappssmtp.com header.i=@rivosinc-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=xk2LHGFU; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4N6Jt92L0Hz23ll for ; Wed, 9 Nov 2022 07:02:39 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 64B55385840D for ; Tue, 8 Nov 2022 20:02:37 +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-Google-Smtp-Source: AMsMyM6+0lmMFhOa9KtZieTQ5IBUNgcxN+VazGRczxAnnYvO8M5PaDHjIN+KtGY+QoMepb57u2rrXQ== 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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" } } */