From patchwork Tue Sep 27 08:46:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 1486 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp218050wrt; Tue, 27 Sep 2022 01:48:34 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4LcfoYQ5ZRTNY47BzmqPuGpMLa9+uO+IBVVZhfFRJcSimjXL/gu6c3DBx8xPa841Qp1C3r X-Received: by 2002:a17:907:868c:b0:782:d6b3:33bc with SMTP id qa12-20020a170907868c00b00782d6b333bcmr14903715ejc.134.1664268514355; Tue, 27 Sep 2022 01:48:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664268514; cv=none; d=google.com; s=arc-20160816; b=eobHKZ4zRnsBlXqbQhXu3QZA5QsqSuSAYdradIR0PjK4uwCfiFMyLkhyJmG9hX0/y0 4cw7BykNrPnOr+M2ypzzObtBiccU4HJc5aF+cMDtw7W4Wt3CzYcx4oUtWRBlxMbJAmC4 n+Dpmgf7vzdn2gMtxSbBKv2YIBfkbdUypXTvs9kdNeyeNhgaHlMpVNuKr6XT0omnVVvs pJ0ez+07hZAuRY0EZhHROkM4EPjJPGFKXybM+TJ2gpuX5OG1JWc8r7dLHpzHep/2AG90 0pfRajMzMaUSbzXEoValiku40Ho0ArqylmnbUzM0gv7L1n9I3qyfgFrgTOd444JNv3lL qqUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc: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=yh9AURlxn4qTxvwi/lSfLj0jfA4erhQgIeJd81uGAD0=; b=OTIRzN3lXoEEvx8oNjHBOwWEkPqa78nKU6A/vaWF6UHitiuEA2dTDOP+g6NlIo9623 0ONQRqbndEMCRqQ9gIzAKOwg3SUm5AyRNxEpYTyYgY+SQNd/2bWE9wezsoL12VRmdke2 E4OinbasIup0+4U4s2bVsPneSz1JNp/fTTaVgiaTfOFRb3CgmAWXLKTiEm7tb3AprZI3 NWUEr4bII7Lwu3yENMvawOq04QI3iOH1UdxWFVTX4fd3JndHrxK2BUB5/xn31e5qEhbI q8PqTNPoUSWhW4UGPls20MJ2dNYMFRVKqxxy6pI2ZeK7CnaDBmBUYz4+R9hxapRWP2t8 8u9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=KvkQXRWw; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id s20-20020a056402521400b00456f283f571si1255155edd.232.2022.09.27.01.48.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Sep 2022 01:48:34 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=KvkQXRWw; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 626723856948 for ; Tue, 27 Sep 2022 08:47:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 626723856948 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664268420; bh=yh9AURlxn4qTxvwi/lSfLj0jfA4erhQgIeJd81uGAD0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=KvkQXRWwt2La9ykgxnFoMSA4LkrH8qHayptaquTlpkR5sKFtfQzQ4eWYzjgMnjmzc bKo9+jKFb7up1Q6ypGCMaGE5AHxMuT80+nF1GwnoVBac/FLtxvASEdGr6cwoA5fsdV sYhoDMaOaEHBLz+l/kRp/IhT8LGdzwWqL0M3PfQM= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 89C17385021D for ; Tue, 27 Sep 2022 08:46:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 89C17385021D 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-278-t720K65pOcqGng1BDNf0AQ-1; Tue, 27 Sep 2022 04:46:15 -0400 X-MC-Unique: t720K65pOcqGng1BDNf0AQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A84C91C0CE6A for ; Tue, 27 Sep 2022 08:46:15 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.195.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 24EC22027061; Tue, 27 Sep 2022 08:46:14 +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 28R8kBug3409660 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 27 Sep 2022 10:46:11 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 28R8kBDX3409659; Tue, 27 Sep 2022 10:46:11 +0200 To: GCC patches Subject: [COMMITTED] irange: keep better track of powers of 2. Date: Tue, 27 Sep 2022 10:46:06 +0200 Message-Id: <20220927084606.3409637-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 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_NONE, 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 Cc: drepper@redhat.com 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?1745112022084293326?= X-GMAIL-MSGID: =?utf-8?q?1745112022084293326?= When setting the nonzero bits to a mask containing only one bit, set the range immediately, as it can be devined from the mask. This helps us keep better track of powers of two. For example, with this patch a nonzero mask of 0x8000 is set to a range of [0,0][0x8000,0x8000] with a nonzero mask of 0x8000. Tested on x86-64 Linux. p.s. Thanks for the bug report Uli. gcc/ChangeLog: * value-range.cc (irange::set_nonzero_bits): Set range when known. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/popcount6.c: New test. --- gcc/testsuite/gcc.dg/tree-ssa/popcount6.c | 12 ++++++++++++ gcc/value-range.cc | 13 +++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/popcount6.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/popcount6.c b/gcc/testsuite/gcc.dg/tree-ssa/popcount6.c new file mode 100644 index 00000000000..1406ad9d33b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/popcount6.c @@ -0,0 +1,12 @@ +// { dg-do compile } +// { dg-options "-O2 -fdump-tree-evrp" } + +int g(int n) +{ + n &= 0x8000; + if (n == 0) + return 1; + return __builtin_popcount(n); +} + +// { dg-final { scan-tree-dump "return 1;" "evrp" } } diff --git a/gcc/value-range.cc b/gcc/value-range.cc index 754379add19..6154d73ccf5 100644 --- a/gcc/value-range.cc +++ b/gcc/value-range.cc @@ -2930,6 +2930,19 @@ irange::set_nonzero_bits (const wide_int_ref &bits) set_nonzero_bits (NULL); return; } + // If we have only one bit set in the mask, we can figure out the + // range immediately. + if (wi::popcount (bits) == 1) + { + bool has_zero = contains_p (build_zero_cst (type ())); + set (type (), bits, bits); + if (has_zero) + { + int_range<2> zero; + zero.set_zero (type ()); + union_ (zero); + } + } set_nonzero_bits (wide_int_to_tree (type (), bits)); }