From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) by sourceware.org (Postfix) with ESMTPS id 64AF73858D32 for ; Thu, 1 Dec 2022 18:57:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 64AF73858D32 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-qt1-x833.google.com with SMTP id l15so2061944qtv.4 for ; Thu, 01 Dec 2022 10:57:55 -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=MBzSTEogmiCo/uZohdTTLw9RwdbF5QgoMvtuyRRA3LA=; b=bVyU8EnWUQMFErVmjdXPd1H+DeWpaVSfjsGicFuJB9NoEJR94mGMXV9IxnZ2/Hb5yw Xpv9JFlnPx/8rpP7hUdOho8EQ1l/6eTMvLwHiQZGyFe+ge5ZCv/azZ5K2BdU/x2leMOC f32KE/QXvfkpHKgTXG2kyRwH1bif4GPHqKmdHH8g+ePniCCetuAmKhKd0LJ/h0sgoKkK wy8nuNRvdcvYZKYzMONQT64TJoJ0TLzM2mMM9dT5M42mgTe8c5MBGtI5O9somI5vnTbS BF8OYlRQmJdDIkLCActACwm7PZ1XyZBYX5lx2v13JtuHEOJ2qkHQSDdxLi6EJ+H1E8xi ciEA== 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=MBzSTEogmiCo/uZohdTTLw9RwdbF5QgoMvtuyRRA3LA=; b=LF1Sp7bJbfphsI2f0jNyJbb6kRUfpQ3mAB55lMWk2IQXCT5ZfTfefCDpD8A3pHkGGG yEj3vzi2qo8EafXRnUyZXT5lQo0U8RB5hNUfGJXAwyRZxY5Xm9EBj5iicP7cdOYv9moc C0pdXRff0cD2qv0aLiEQ0Lgk0dHCY4d30P4A0GW8BPftOxrnhmBE0VmWYueyQYz7uSB4 9h7bCHV78nYbfBTZ1NcZbUnFzTsS7u64xMOuwfuEzeJCjOEToYE9AVOSSmsy+eHNP10Q awIe5G5Q5oAOYufZmEGFHxHmFrpXoX6aQ0nwIC8rt2agKtjBmLdCmlhbkbJUsQxBjUwU T/bQ== X-Gm-Message-State: ANoB5pne2gQ7JLS+DhObNgzEsT5597+BC/cT9duNd0voh4OgpIEDQpqM YWUsrxovS5KFTC5JXgUEwTbwQg== X-Google-Smtp-Source: AA0mqf4z7UtP4BD71zIS0Dyf7EHy8ISiiDzhTZGMbADKuPbsjwtbc5Q9EFoSbGxUVpgnz4aVB0lq7A== X-Received: by 2002:ac8:7257:0:b0:3a5:3ea2:fe1d with SMTP id l23-20020ac87257000000b003a53ea2fe1dmr43449924qtp.574.1669921074767; Thu, 01 Dec 2022 10:57:54 -0800 (PST) Received: from [192.168.86.117] ([136.57.172.92]) by smtp.gmail.com with ESMTPSA id az20-20020a05620a171400b006e42a8e9f9bsm3902459qkb.121.2022.12.01.10.57.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 01 Dec 2022 10:57:54 -0800 (PST) Message-ID: <17f48447-7765-a47a-d1c4-0f4f2a3907bb@rivosinc.com> Date: Thu, 1 Dec 2022 13:57:53 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: [PATCH v2] match.pd: rewrite select to branchless expression Content-Language: en-US To: Richard Biener Cc: gcc-patches@gcc.gnu.org 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.9 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: Richard, Can you submit this patch for me while I sort out git write access? On 11/18/22 07:57, Richard Biener wrote: > On Fri, Nov 11, 2022 at 3:28 AM 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? > OK. > > Thanks, > Richard. > >> 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; >> +} >> + >> +/* { 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 >>