From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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" Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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 List-Id: 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" } } */ -- 2.34.1