From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by sourceware.org (Postfix) with ESMTPS id 000BB3858D1E for ; Fri, 11 Nov 2022 13:00:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 000BB3858D1E 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-x730.google.com with SMTP id i9so2893542qki.10 for ; Fri, 11 Nov 2022 05:00:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=DuTlQbWlupf8eHZpqPeM2AWQO4FLqmz7+duNIfCPIh0=; b=yDqLkgphh0TaLvI4M7uh5PrtZSq93KkjitOgiCV0hIID4toJ2CppVQwgEqHbvFzMUZ lOIn1FRijX9MkqmsKG48pltH5fs5u5JJrA0y/muOZXo8NEVbR82ySrbzwZPPXP+MMdn6 XUCym06KQA4h9/M2pUMpqLhskWj8XdeXPzAI9oHwNbKwFgAzOpX88RFktJMnhAK6aOgO xooX1+lMcNlWoIArBChNF4/8GScWK0PPa5229BkxLyFEKVtLmzffcVm3uUhR6eqETTIv 1/d+nVwjVe7pxjzVjzMFS+QLxhub1AWv3sBlUNrtmtY28o60dsrvFPl9G5OrIfrchauJ sPLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DuTlQbWlupf8eHZpqPeM2AWQO4FLqmz7+duNIfCPIh0=; b=rQHMyTv4l96cTuNQ0SBYLdtJQuy+wUHt4Z/lhKvQiWyRewrOtITj0/t5dljfjLxkL6 8e5ifaiitSwcO4rM9jD3Yynod21eZo+AwDYUkkbeKyuXwzxVJdwmP3D42ReUSPZWEh9I xv5dmXt93KIalLtCj5TVNEKestt+gwJPjLkWSuRw88kiztZAFcjp27UlQQ8xNgrO3UDr IZFtikOb+qimDKySy17WGh2tNt2+RgdyxFEuZm368WJSfK1YFgCFnz+RctpjDLJHgsD4 r0FXJTSm+fFBgHRSVOZa7rsRPsP0rB7mi+H6qnXFyXeIB1xMjEDFoS07SkUZoZ2hrDom 5gTg== X-Gm-Message-State: ANoB5pkAN7bfavTKEDqg0Co82pNmikrHDYIeQlyP5H+cNYC97dsVwQHg oCo6DYRqP0ISDeSSLKkTKd1sHA== X-Google-Smtp-Source: AA0mqf5Kh3EqMjzOtCro1slhhTe0OFt0Xx5JSGGOBQhDRN3DfB6dC2gYEJ8oMOQW9a/ibythkg97Ew== X-Received: by 2002:a37:40b:0:b0:6fa:28d5:4b36 with SMTP id 11-20020a37040b000000b006fa28d54b36mr872685qke.346.1668171607271; Fri, 11 Nov 2022 05:00:07 -0800 (PST) Received: from [192.168.86.117] ([136.57.172.92]) by smtp.gmail.com with ESMTPSA id fc14-20020a05622a488e00b003a54a19c550sm1210073qtb.57.2022.11.11.05.00.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 11 Nov 2022 05:00:06 -0800 (PST) Message-ID: Date: Fri, 11 Nov 2022 08:00:05 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Subject: Re: [PATCH v2] match.pd: rewrite select to branchless expression Content-Language: en-US To: Prathamesh Kulkarni Cc: gcc-patches@gcc.gnu.org, Richard Biener References: From: Michael Collison In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,NICE_REPLY_A,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: Hi Prathamesh, It is my understanding that INTEGRAL_TYPE_P applies to the other integer types you mentioned (chart, short, long). In fact the test function that motivated this match has a mixture of char and short and does not restrict matching. On 11/11/22 02:44, Prathamesh Kulkarni wrote: > On Fri, 11 Nov 2022 at 07:58, Michael Collison wrote: >> This patches transforms ((x & 0x1) == 0) ? y : z y -into >> (-(typeof(y))(x & 0x1) & z) 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-10 Michael Collison >> >> * match.pd ((x & 0x1) == 0) ? y : z y >> -> (-(typeof(y))(x & 0x1) & z) y. >> >> 2022-11-10 Michael Collison >> >> * gcc.dg/tree-ssa/branchless-cond.c: New test. >> >> --- >> >> Changes in v2: >> >> - Rewrite comment to use C syntax >> >> - Guard against 1-bit types >> >> - Simplify pattern by using zero_one_valued_p >> >> gcc/match.pd | 24 +++++++++++++++++ >> .../gcc.dg/tree-ssa/branchless-cond.c | 26 +++++++++++++++++++ >> 2 files changed, 50 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..258531e9046 100644 >> --- a/gcc/match.pd >> +++ b/gcc/match.pd >> @@ -3486,6 +3486,30 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) >> (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1) >> (max @2 @1)) >> >> +/* ((x & 0x1) == 0) ? y : z y -> (-(typeof(y))(x & 0x1) & z) y */ >> +(for op (bit_xor bit_ior) >> + (simplify >> + (cond (eq zero_one_valued_p@0 >> + integer_zerop) >> + @1 >> + (op:c @2 @1)) >> + (if (INTEGRAL_TYPE_P (type) >> + && TYPE_PRECISION (type) > 1 >> + && (INTEGRAL_TYPE_P (TREE_TYPE (@0)))) >> + (op (bit_and (negate (convert:type @0)) @2) @1)))) >> + >> +/* ((x & 0x1) == 0) ? z y : y -> (-(typeof(y))(x & 0x1) & z) y */ >> +(for op (bit_xor bit_ior) >> + (simplify >> + (cond (ne zero_one_valued_p@0 >> + integer_zerop) >> + (op:c @2 @1) >> + @1) >> + (if (INTEGRAL_TYPE_P (type) >> + && TYPE_PRECISION (type) > 1 >> + && (INTEGRAL_TYPE_P (TREE_TYPE (@0)))) >> + (op (bit_and (negate (convert:type @0)) @2) @1)))) >> + >> /* 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; >> +} > Sorry to nitpick -- Since the pattern gates on INTEGRAL_TYPE_P, would > it be a good idea > to have these tests for other integral types too besides int like > {char, short, long} ? > > Thanks, > Prathamesh >> + >> +/* { 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 >>