From patchwork Fri Nov 3 17:14:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 1859032 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=N/kUg73i; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SMS6J5yX1z1yQq for ; Sat, 4 Nov 2023 04:14:47 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6891D3857C41 for ; Fri, 3 Nov 2023 17:14:45 +0000 (GMT) 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 B402D3858281 for ; Fri, 3 Nov 2023 17:14:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B402D3858281 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B402D3858281 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699031674; cv=none; b=MPAyGWNRdD9t2ENxpsfMOnVH5UUxKr5bgaMEqQ8n5CInpsQRr+MXs+X6J4RRJB6E2Rb7IG8TE5+Ew+cerKRrynf89vV014WRSyrIu9TUStc4qUznn22CspkMotYzRtYvuVHJMMwt6MqYxRMTVMQDC5kCxHnGTmt8PfYdSR50YeY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699031674; c=relaxed/simple; bh=dzTmfHduYH8j0JfMd28DCa16ynB/YVaG6hofHqOv7lo=; h=DKIM-Signature:Message-ID:Date:MIME-Version:From:Subject:To; b=M5W0J3ZsGPpg+yhf40UdM3/+WM/bho7J5Mcw4v23h/m8p6YDVQ1lD2uiRKjiaCVk6d6Tg41wGHQ6lp24muuQC9LE1rzXYBQ/x3MrtHgbNZnNExLGxjS0FScBxfDeNYBIx2SZsmbrNLBHOd+GyYfM9zoPFXSO3J8fa1VR13MLa/s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699031664; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=fq4wYQhUTE4dY8E5JFCTVPK6n+99rI1wwEp2uVVUqdw=; b=N/kUg73iK2vlbdY2olPIFGTUkCpoPDsmE8/2cfzw76XOowbmwT3NaApMmgAEHLCu9hu/eV NTaAvM+97iNe2KhH9vkLOWw8PkElQzHTO4DVOdyaIY0rBidJn6j4t1vy31lefMT3mYIcsG 9+DsaOAyViaJU2lxYkFte9pDBUhbw8k= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-662-JAMReFgZMC2DbzhwxAq74w-1; Fri, 03 Nov 2023 13:14:22 -0400 X-MC-Unique: JAMReFgZMC2DbzhwxAq74w-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7788fa5f1b0so237770185a.2 for ; Fri, 03 Nov 2023 10:14:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699031661; x=1699636461; h=content-language:cc:to:subject:from:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ObEiCN69fpuWatiqaUMsH0pf5cEjCD2S0KLjJLFsM9E=; b=PD5j3+zzNtIIsJB0v7pCUkF13A99FilrfsnMDG3AdNs0H/oM3hCXSOrziACXs2ikF8 C90NXreY33edJiiW09i014o3V9+mCEinIhKRlX9tyjZq8DpdCX0JApTuiP3oR8F9XFQn 8ibPE+C2Rv2s4h9jk/oAuuNm1BXMc84fPPhTMfO5ldzVEu9AV1jnWOk7MpxgW61hOBjr N8XqpCTocZjfsD64LCkq7Mi5dA4virMSdtiuYAi5WCl60neasgn8pXucSCtslyUr/Ib6 4OddF0w1iG3LuIak0jkb1oo5FjNRZG/e/PM1Ee73LP51MNfgJthfi2AiPa8TSxY+Y35v tCcg== X-Gm-Message-State: AOJu0YzUaPGN4NVAAjqZcVejqADoYzoMjPGlWiYC4CsXGCnmnSJWykjY n0Fotln+ylezIQSp67cfoiJCwgE3Ex6hJiDpBkue3+klAX53ix4SrCHIFumJG4J8MK9A/5jKDgr 3Hz9E5awditCJA4oIdSwIN9fxcDKJn/tiUpcGNowF3DRaxFXSuQVzjwcpeTdQ8y8ZX15hFAB8/1 ocGA== X-Received: by 2002:a37:c24a:0:b0:775:9c99:122e with SMTP id j10-20020a37c24a000000b007759c99122emr20292458qkm.71.1699031661325; Fri, 03 Nov 2023 10:14:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHOW1S5Efce4ABhVa6yOnj+zqmA+rv/e5nC+lIRm1habOkQUnArMiQ0kEwPkPqwolpgjfcfFQ== X-Received: by 2002:a37:c24a:0:b0:775:9c99:122e with SMTP id j10-20020a37c24a000000b007759c99122emr20292433qkm.71.1699031660890; Fri, 03 Nov 2023 10:14:20 -0700 (PDT) Received: from ?IPV6:2607:fea8:51dd:2b00::18c7? ([2607:fea8:51dd:2b00::18c7]) by smtp.gmail.com with ESMTPSA id j22-20020a05620a411600b0077402573fb4sm886248qko.124.2023.11.03.10.14.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 03 Nov 2023 10:14:20 -0700 (PDT) Message-ID: <14b069dc-711f-4643-97a0-b64142017f24@redhat.com> Date: Fri, 3 Nov 2023 13:14:19 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Andrew MacLeod Subject: [COMMITTED 1/2] Remove simple ranges from trailing zero bitmasks. To: gcc-patches Cc: "hernandez, aldy" X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-Spam-Status: No, score=-12.7 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, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org WHen we set bitmasks indicating known zero or one bits, we see some "obvious" things once in a while that are easy to prevent. ie unsigned int [2, +INF] MASK 0xfffffffe VALUE 0x1 the range [2, 2] is obviously impossible since the final bit must be a one.   This doesn't usually cause us too much trouble, but the subsequent patch triggers some more interesting situations in which it helps to remove the obvious ranges when we have  mask that is trailing zeros. Its too much of a performance impact to constantly be checking the range every time we set the bitmask, but it turns out that if we simply try to take care of it during intersection operations (which happen at most key times, like changing an existing value), the impact is pretty minimal.. like 0.6% of VRP. This patch looks for trailing zeros in the mask, and replaces the low end range covered by those bits with those bits from the value field. Bootstraps on build-x86_64-pc-linux-gnu with no regressions. Pushed. Andrew From b20f1dce46fb8bb1b142e9087530e546a40edec8 Mon Sep 17 00:00:00 2001 From: Andrew MacLeod Date: Tue, 31 Oct 2023 11:51:34 -0400 Subject: [PATCH 1/2] Remove simple ranges from trailing zero bitmasks. During the intersection operation, it can be helpful to remove any low-end ranges when the bitmask has trailing zeros. This prevents obviously incorrect ranges from appearing without requiring a bitmask check. * value-range.cc (irange_bitmask::adjust_range): New. (irange::intersect_bitmask): Call adjust_range. * value-range.h (irange_bitmask::adjust_range): New prototype. --- gcc/value-range.cc | 30 ++++++++++++++++++++++++++++++ gcc/value-range.h | 2 ++ 2 files changed, 32 insertions(+) diff --git a/gcc/value-range.cc b/gcc/value-range.cc index fcf53efa1dd..a1e72c78f8b 100644 --- a/gcc/value-range.cc +++ b/gcc/value-range.cc @@ -1857,6 +1857,35 @@ irange::get_bitmask_from_range () const return irange_bitmask (wi::zero (prec), min | xorv); } +// Remove trailing ranges that this bitmask indicates can't exist. + +void +irange_bitmask::adjust_range (irange &r) const +{ + if (unknown_p () || r.undefined_p ()) + return; + + int_range_max range; + tree type = r.type (); + int prec = TYPE_PRECISION (type); + // If there are trailing zeros, create a range representing those bits. + gcc_checking_assert (m_mask != 0); + int z = wi::ctz (m_mask); + if (z) + { + wide_int ub = (wi::one (prec) << z) - 1; + range = int_range<5> (type, wi::zero (prec), ub); + // Then remove the specific value these bits contain from the range. + wide_int value = m_value & ub; + range.intersect (int_range<2> (type, value, value, VR_ANTI_RANGE)); + // Inverting produces a list of ranges which can be valid. + range.invert (); + // And finally select R from only those valid values. + r.intersect (range); + return; + } +} + // If the the mask can be trivially converted to a range, do so and // return TRUE. @@ -2002,6 +2031,7 @@ irange::intersect_bitmask (const irange &r) if (!set_range_from_bitmask ()) normalize_kind (); + m_bitmask.adjust_range (*this); if (flag_checking) verify_range (); return true; diff --git a/gcc/value-range.h b/gcc/value-range.h index e9d81d22cd0..84f65ffb591 100644 --- a/gcc/value-range.h +++ b/gcc/value-range.h @@ -139,6 +139,8 @@ public: void verify_mask () const; void dump (FILE *) const; + void adjust_range (irange &r) const; + // Convenience functions for nonzero bitmask compatibility. wide_int get_nonzero_bits () const; void set_nonzero_bits (const wide_int &bits); -- 2.41.0