From patchwork Fri Nov 11 02:28:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Collison X-Patchwork-Id: 18390 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp496332wru; Thu, 10 Nov 2022 18:28:33 -0800 (PST) X-Google-Smtp-Source: AA0mqf6NDoSQ3qkI4l6OJ+M0+jMzj86LK5z9xjp7qlSN9L+fafeKOJjnm4deTd7xeR6qy4BdVtkk X-Received: by 2002:a17:907:d504:b0:7ad:b6d6:9188 with SMTP id wb4-20020a170907d50400b007adb6d69188mr266215ejc.418.1668133713429; Thu, 10 Nov 2022 18:28:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668133713; cv=none; d=google.com; s=arc-20160816; b=KWOsLgjDvkCGxaYAJ+M5m9z2zUjpwh6bKgDF1JI0PGWNigxPVy5p/olqjs+/MUQEd3 wConbqRdZ/xHcvBUxRvYwhcgB1dJBi2ERCe6DwT4PChqU70IzT+FN3CZc1t6SyT3axuU 4kHoxb9NMUD/5doLj4nDHEE7UqD95MM/G3lbknX3/iBVF1Vc++HOmbK7fS5+wr73dWZp 2MXFQpPAhyCIbhKB95DQLkIjtPcf2uotjY/rWq+WsuyfjXykDWW0NRUV3kRvxMJwzRLe Qv/AQw6RJCEZKqJH6yztjnD6uyOHLuD6y1oIIUdiP+te553xTc3Xyw5rxcGWre9++Uhd LRaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :subject:cc:to:from:content-language:user-agent:mime-version:date :message-id:dkim-signature:dmarc-filter:delivered-to; bh=zK0LPtfXdtyPxD3FW6W3u1rGEXxld0LeCdcdaDQFMR0=; b=vEIyzt07bbNKYI+lvFpR8rwHmpBLVmcea+sImQ07/qIQGEoxg3l+Us0iGGmlM2rbiE R4ng4bkMNCRry9ax1YCv5KxNSafQ3IO2nOiFVAYwn/ObjUjwodzbqlIqnyZsV3+27z5T umUUvvpdeClN64Ba2mqfgaW96b+sDQ2Z6J7nwXJSqbwPHdjTufweM/cxZ0yqiSYKVfrT 8+rrxnqZBzJQfKwxRXB+pi4hfqdJ69nMUbiZmDM/89MOR6A2qn4e49rv3NuYUWoYBPY3 blaCs6EMlymxhI77podyJrkBxEvAncboPeJ+8NaZA6PA8gWJESQOLUqJv1iewn2cTbXW HE0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=vOuGrrhW; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id qk11-20020a1709077f8b00b007a953d421bbsi950875ejc.322.2022.11.10.18.28.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 18:28:33 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=vOuGrrhW; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7F6AF3885C36 for ; Fri, 11 Nov 2022 02:28:28 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qk1-x72e.google.com (mail-qk1-x72e.google.com [IPv6:2607:f8b0:4864:20::72e]) by sourceware.org (Postfix) with ESMTPS id BCAFC3858C27 for ; Fri, 11 Nov 2022 02:28:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BCAFC3858C27 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-x72e.google.com with SMTP id v8so2331969qkg.12 for ; Thu, 10 Nov 2022 18:28:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:subject:cc:to:from:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=zK0LPtfXdtyPxD3FW6W3u1rGEXxld0LeCdcdaDQFMR0=; b=vOuGrrhWQ6CVtv/4L4O1A0Y5miIDCsXiP3zB5XrnOk+mPAGpw12j+1w0d5CbM24R+C D9lxagu4jU6uv5duXilvnCwlLfGPhulDHFaejNk6Cw01pgcVLhWyC6/AEVD9NTH7VlR4 3vpmptxZlQ9lf31AgXW6GiPuamJ1rXU6Xwa0JDPAZIg81WbZhmPPDUkLmrsgfiFnRlkB m+7SfvoEU28uc7JUmnP2mdpSbq9uGE7cG8bJL8AVoi7t1pcW1CGek2bYRt+Gleua/FLk uPy7pJuILoG2YUZ3n/YTgnHDK2wbGtfHKzCQzjIlmB4FinK5/jbE2kF9rFebzeZ4XrfY Gi2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:subject:cc:to:from:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=zK0LPtfXdtyPxD3FW6W3u1rGEXxld0LeCdcdaDQFMR0=; b=c31jWDvuC1UXwr9YOGfizIxjtMRv+306v5kgvjJhLmV1AIiC33SfTYzPmd7s9kMtsw XpjXS7NIB2Eyg/odslCNtxRdQjN2U2m01s/zHwRbFssIePdU/yyaUcP0jYP2yhapHabY NsMittdPQauFb2WBQv3ptx3dNxN4ous5uWWGGBpo6YxSE5hlR5n1GZx8bAFPsQfbIWM4 2TCXYv149vTjlVzfD/wCm8fzzkczIyfvEsOaiTe44gdcSI6ev6RJ58WasZjlNhXfhSmr JprC7AhYR9XeEciO2tLDOA4twhnS2gIPdj6+Bx1xrT3Z+KWy+tQjd7slAhQW6jWuUI4+ c2zA== X-Gm-Message-State: ACrzQf3vGZkGDWUCwBL8nGrn6QEri5tQXLFvxUQ+2X3TdcKPbXDpk+v0 LDACI4KRuJ2pLSDzPRjHMm1a41/Zg/+Kmw== X-Received: by 2002:ae9:df85:0:b0:6fa:1e59:4b72 with SMTP id t127-20020ae9df85000000b006fa1e594b72mr2653885qkf.247.1668133682913; Thu, 10 Nov 2022 18:28:02 -0800 (PST) Received: from [192.168.86.117] ([136.57.172.92]) by smtp.gmail.com with ESMTPSA id hj10-20020a05622a620a00b003a582090530sm529432qtb.83.2022.11.10.18.28.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 10 Nov 2022 18:28:02 -0800 (PST) Message-ID: Date: Thu, 10 Nov 2022 21:28:01 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Content-Language: en-US From: Michael Collison To: gcc-patches@gcc.gnu.org Cc: Richard Biener Subject: [PATCH v2] match.pd: rewrite select to branchless expression X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749164977082644291?= X-GMAIL-MSGID: =?utf-8?q?1749164977082644291?= 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; +} + +/* { dg-final { scan-tree-dump-times " -" 4 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " & " 8 "optimized" } } */ +/* { dg-final { scan-tree-dump-not "if" "optimized" } } */