From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by sourceware.org (Postfix) with ESMTPS id AD4E9384F4A3 for ; Fri, 18 Nov 2022 12:58:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AD4E9384F4A3 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-x12e.google.com with SMTP id bp15so8042234lfb.13 for ; Fri, 18 Nov 2022 04:58:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=5Gu7W9FUy6m3+hV7603vhR2BDaimuLbUggFj+cKm/JQ=; b=P/oxScWWn9kAJ7z7D3SusYsBe5HRX/MS5qbbnmWRlhBX8oenzP2UlqY7uGmEFlHpKp JZhQbfBw/afr/PRZIp36ckrNtIdTpr+PxXw/0KOs5JwijytWfXdmaAaQkmXap6peMZzn 7d072Hixk7acQJ+BVmIDDxW2H2Mtar+gpgCEnDDXpxLggZAQji7vSgZ+nJP5O6AzwOjx 3Gau3kIX2Ox2yvkccEiUpLU6G0pFo+CqRYunEcyCrWvQmfo8+VZ1jRRK8PEFi4LD2bN9 zdTGZBjeoaaJAvpFHmzaRJXFEDw3Z7An9G2nPtMep5T7UfOIk5FjBCC0RC+iSSpKgEEm RNTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5Gu7W9FUy6m3+hV7603vhR2BDaimuLbUggFj+cKm/JQ=; b=p3tlvC25UeA/p6XiDeGLMuhNJcrrMJAGchQ3VJ19ZPyCRB0CY6aDiS8ykStjKlKxiK ACi8kt1zEmF5GqZ7lML7bKEtFpovUpY6mG1Od3aCMlxaamIdYs9MHa67Y7EBgXQBOncu 3UOYhaY/pjTNSdDOEmDSU2eYVS3QvXpdC0YieI+F2NxYzT4VvMkuGgIUz9Hzfk9SyoRA kqNCnBjudQlEOBNzd2SfLlUCK8bzxT3ETzDF+iMwyIUBgNYjx/zSufyBfpfOWEj/jsFX w4FGhFMIQKz1zRETfMFFB64xLIcOxNHP3S85kBX0AFzWoY5ThrBhIpdIIsxQn0SCGYzF chaA== X-Gm-Message-State: ANoB5pk8gXzWcIR5/Pbco33+vbezm4gRe6btlnWRFtnCJMhBJJ4Rw0u7 fw9tH/QOQ/Jl0Zsw7RiRoD7QjC3P0qPr3nhicXUGhD5u X-Google-Smtp-Source: AA0mqf4n9zaORGIpPQDLOSSmz9qDqFt/nM+3uccrOtHoWFCOB2CYAtO+lvPEeTbOHhqAOQ/JXnTqUDnYKJQRHkxVpD0= X-Received: by 2002:a05:6512:e86:b0:4b4:70d9:5c02 with SMTP id bi6-20020a0565120e8600b004b470d95c02mr2364505lfb.27.1668776280145; Fri, 18 Nov 2022 04:58:00 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Richard Biener Date: Fri, 18 Nov 2022 13:57:46 +0100 Message-ID: Subject: Re: [PATCH v2] match.pd: rewrite select to branchless expression To: Michael Collison Cc: gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,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: 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 >