From patchwork Tue Oct 4 04:28:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 1669 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1244923wrs; Mon, 3 Oct 2022 21:29:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6vKAGfvXxHZlzMAVyughBixzoVGelhTNmyJ3OeSATKhWm3zWQgjcKqY0ujxbFRVdlO6xLg X-Received: by 2002:a17:907:3e88:b0:78b:ea66:a1e4 with SMTP id hs8-20020a1709073e8800b0078bea66a1e4mr4974921ejc.91.1664857768236; Mon, 03 Oct 2022 21:29:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664857768; cv=none; d=google.com; s=arc-20160816; b=XRsTM/JV3eyvIKZVDzxSE9DhtHnlWOQLoGmw/uinSMivqwKOW139zos0p6VUUcEAnf 6rYH2X9eV2F3Vyeho5A0NQKwraMiZb5GLe/CiAwCBv+FHLXZpnDeEVTCE2zhQJwlCI+v buBaEQHAu7MRlaDx3x2MCMP6lX7Zp2bgv0WvKdMG/qcjjiN01hhoFMfJ50E0C+B/2dnP ulwQm5Vne+x+A2SXPtFZdBHatq9e5OgI7rLr9G2pihnJqcHUriTOMkyS/ZR6zHUmBWFt r2y77mMKyyMmsD+tbBXUiVRFYkHPrZxkOF3Mk6pCQCi3fAeCRRvkXxXSa6d/CBgAhTVK KSDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=++0RSuPnSD3uzXBLBPKTyVNfph3lzydXIqTKU2/R0CM=; b=B9ky0bx1xIxsuvTq/ClFF47T01ImorUKQmtXfDjzcwo4nkNokgzNrnahVO8W5QVqtU EW8RzKS2jRHjEDu1ulaKzrXiWQp9cxZdjixTndSmfUfPn0aZX2XwN7HfC712R4vczyOl Yh8liSEKFtt5SguAKNlAVGpj6g6dsAJ9Ql2gpSqo5URq+KO6RKY0ZUbPqRvcB4/4hOua YybbNDF22PGi2nFbzTx9M6ZJ+g6qSUulcsbzpZ55KoDjnGcGxXSPXaSEehc9U/BYm+eD ah5ZbJHtSu/0WKuBXbY8OxRd92go1NMA26MVeVMXPJmE/JtEqZZMvLHJjTnufu5YH9Xz uccg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=S8fq2F80; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id qa32-20020a17090786a000b0076f0ab97742si10181172ejc.489.2022.10.03.21.29.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Oct 2022 21:29:28 -0700 (PDT) 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=@gcc.gnu.org header.s=default header.b=S8fq2F80; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 45A11385AC29 for ; Tue, 4 Oct 2022 04:29:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 45A11385AC29 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664857763; bh=++0RSuPnSD3uzXBLBPKTyVNfph3lzydXIqTKU2/R0CM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=S8fq2F80jBSmlJofdfw+ghk5v702CFY4peAn84SgReYkatE84FlFl/9IFpNJ+m7dj 4Gu8qBpSHSICePQspYG8g/ecAcSOjlg4QtYzR2ltZ6qi9Dqv7CFbj4+9wUxULFGiRu 47VhgA/XIi25E37zyX51RpUtEB3r2faXVe8KHEwg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id D0A7F3858D1E for ; Tue, 4 Oct 2022 04:28:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D0A7F3858D1E Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-673-G346a7bHOHywnTQAsPryqQ-1; Tue, 04 Oct 2022 00:28:37 -0400 X-MC-Unique: G346a7bHOHywnTQAsPryqQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CD4A938041D8 for ; Tue, 4 Oct 2022 04:28:36 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.41]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7D125140EBF5; Tue, 4 Oct 2022 04:28:36 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.17.1/8.17.1) with ESMTPS id 2944SXBd1419943 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 4 Oct 2022 06:28:33 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 2944SXM61419942; Tue, 4 Oct 2022 06:28:33 +0200 To: GCC patches Subject: [COMMITTED] [PR107130] range-ops: Separate out ffs and popcount optimizations. Date: Tue, 4 Oct 2022 06:28:31 +0200 Message-Id: <20221004042831.1419926-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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: , X-Patchwork-Original-From: Aldy Hernandez via Gcc-patches From: Aldy Hernandez Reply-To: Aldy Hernandez 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?1745729899047634086?= X-GMAIL-MSGID: =?utf-8?q?1745729899047634086?= __builtin_popcount and __builtin_ffs were sharing the same range-ops entry, but the nonzero mask optimization is not valid for ffs. Separate them out into two entries. PR tree-optimization/107130 gcc/ChangeLog: * gimple-range-op.cc (class cfn_popcount): Call op_cfn_ffs. (class cfn_ffs): New. (gimple_range_op_handler::maybe_builtin_call): Separate out CASE_CFN_FFS into its own case. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr107130.c: New test. --- gcc/gimple-range-op.cc | 37 +++++++++++++++++------- gcc/testsuite/gcc.dg/tree-ssa/pr107130.c | 19 ++++++++++++ 2 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr107130.c diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc index 3a7c9074659..5b71d1cce6d 100644 --- a/gcc/gimple-range-op.cc +++ b/gcc/gimple-range-op.cc @@ -387,8 +387,8 @@ cfn_toupper_tolower::fold_range (irange &r, tree type, const irange &lh, return true; } -// Implement range operator for CFN_BUILT_IN_FFS and CFN_BUILT_IN_POPCOUNT. -class cfn_popcount : public range_operator +// Implement range operator for CFN_BUILT_IN_FFS. +class cfn_ffs : public range_operator { public: using range_operator::fold_range; @@ -397,14 +397,6 @@ public: { if (lh.undefined_p ()) return false; - // Calculating the popcount of a singleton is trivial. - if (lh.singleton_p ()) - { - wide_int nz = lh.get_nonzero_bits (); - wide_int pop = wi::shwi (wi::popcount (nz), TYPE_PRECISION (type)); - r.set (type, pop, pop); - return true; - } // __builtin_ffs* and __builtin_popcount* return [0, prec]. int prec = TYPE_PRECISION (lh.type ()); // If arg is non-zero, then ffs or popcount are non-zero. @@ -420,6 +412,26 @@ public: r.set (build_int_cst (type, mini), build_int_cst (type, maxi)); return true; } +} op_cfn_ffs; + +// Implement range operator for CFN_BUILT_IN_POPCOUNT. +class cfn_popcount : public cfn_ffs +{ +public: + using range_operator::fold_range; + virtual bool fold_range (irange &r, tree type, const irange &lh, + const irange &rh, relation_kind rel) const + { + // Calculating the popcount of a singleton is trivial. + if (lh.singleton_p ()) + { + wide_int nz = lh.get_nonzero_bits (); + wide_int pop = wi::shwi (wi::popcount (nz), TYPE_PRECISION (type)); + r.set (type, pop, pop); + return true; + } + return cfn_ffs::fold_range (r, type, lh, rh, rel); + } } op_cfn_popcount; // Implement range operator for CFN_BUILT_IN_CLZ @@ -734,6 +746,11 @@ gimple_range_op_handler::maybe_builtin_call () break; CASE_CFN_FFS: + m_op1 = gimple_call_arg (call, 0); + m_int = &op_cfn_ffs; + m_valid = true; + break; + CASE_CFN_POPCOUNT: m_op1 = gimple_call_arg (call, 0); m_int = &op_cfn_popcount; diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr107130.c b/gcc/testsuite/gcc.dg/tree-ssa/pr107130.c new file mode 100644 index 00000000000..11fb816ad71 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr107130.c @@ -0,0 +1,19 @@ +// { dg-do compile } +// { dg-options "-Os -fno-tree-ccp -fno-tree-forwprop -fno-tree-fre -fdump-tree-vrp1" } + +static inline int +foo (int i) +{ + return __builtin_ffsl (i); +} + +int +main (void) +{ + int x = foo (41); + if (x != 1) + __builtin_abort (); + return 0; +} + +// { dg-final { scan-tree-dump-not "builtin_abort" "vrp1" } }