From patchwork Fri Nov 1 19:25:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 100055 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 634FE3857BA9 for ; Fri, 1 Nov 2024 19:26:25 +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 ESMTP id F1E7E3858290 for ; Fri, 1 Nov 2024 19:25:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F1E7E3858290 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 F1E7E3858290 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=1730489147; cv=none; b=KmrlAFQhd5di/yr6ry4Q5DM253JFwhXVe8eOypXS9LMWRzJyJp6fYT8sqL2RO47Z5Ry7/mXskYjbPpaCPFH4OuEHeFZW1NoAwDPiygdwxwtB0SjJNnuFX2QZLCC14cBku6LNYmNjmvwpKhh/gHdXQZHbj4U49HNSV115Eq8NN7E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730489147; c=relaxed/simple; bh=jL3lRrbfwu4hwXPEmbeKStvz1/YF4Ma4/dMFjrx5nkE=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=XWdaqPvUO7XsmfwZAhuqTSNRsYEEnGqv4bV3V7xd9b/QkXHmO7JgXTSf7VIvJ0au7KecYy5r19Y8Yw3Sp5Yys3BULWnxdWIJXPFonTUJTcEC0Rs70f6STApi0LMst+50a+Jp8Ipjhc/VXtvWSrOGNzBv2wb9yCc+DhRkaZPuGSc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730489144; 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=FlT8Je65qPSlfj5DlsNHcAahta3oGDYEqzc1QKAMiOg=; b=bLAK+gpoz3QSpSBbk+e5+5hxLE7PWbIHu9qAmwzm0O/9HdgCVCwKM+cORYbQWDZPsmm5yQ ZHhSOkfXmyJBEGfPtzweQwtHGmdv+0ObmkWKr1EvMHzvoJs8o9BfDTnLIq79RS/ZkJUjsY rNSODYdEKiKysrKU8mctazt6iJZrdWM= Received: from mail-oi1-f198.google.com (mail-oi1-f198.google.com [209.85.167.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-226-FiVEoI3XOyO32tnt0Nj0SA-1; Fri, 01 Nov 2024 15:25:43 -0400 X-MC-Unique: FiVEoI3XOyO32tnt0Nj0SA-1 Received: by mail-oi1-f198.google.com with SMTP id 5614622812f47-3e5f78786cbso2895422b6e.1 for ; Fri, 01 Nov 2024 12:25:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730489141; x=1731093941; h=subject:from:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nU2M0xj9GaJvtZAkOiUoxwUUlKGfy/KuChN/ESnKv2c=; b=Twef2RH6uJxxns8J5sN0f5o18ebvZ2tpKb3BcC3NnID2wrGg/lR/1/QoVMsLj7vSUy tOFfxAVRZo3f68wfejq1iurkSG9aMNkV+MsMf62BcKQcHiTC4geHcx4D4+TrHhlf6hLA pM37xc4zJk3sFNEf1nUKb5kjtDnSsz5VYfhpwvkEwWXAj/cPIl0mziXt+72a3X2y796b q52snm1o9t5OHNVartK5wypAC7TAHL1HEP9/KI0WG5VlwCvggPSWTp7rzSFNDlGbXm5W Htoc9lhtNi0pjYxX96PQ61h8jqGHp/D0r9I9el+se1zidpRA3x7FV/o+Dw3Ux50MgLre RkiQ== X-Gm-Message-State: AOJu0YyBnt4LbtOe1r15/X483VlT9vdM2rz2tEw8hb9cmQKFDRbK5scx GEv1Eew7i9r/84DOvmX3AyGIS+nNIfVHxceR2QouwmtvCS2eEhZ1eE8d85MMGP3PjhYFcvLU1b7 PhVK4nxkkiK8OGJ1jJneHpj7r1wJXc+Ef3vxRpo1YmKDM9pClIxXFVpCqMI7+m+WUxT3ZdqmxDu +Uw3T6w1wLrjC4fc9R74ZgDH2M8q34QYmk9muqxeS5ig== X-Received: by 2002:a05:6808:308e:b0:3e5:f7d8:b560 with SMTP id 5614622812f47-3e6608dfc0cmr7351995b6e.18.1730489141611; Fri, 01 Nov 2024 12:25:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGRAp+tvi0PGHtAOfdYineefY/72GMozVPZzzmEasKDfdDxGmFFx6Gh3sUAJJjmilKVg3lExw== X-Received: by 2002:a05:6808:308e:b0:3e5:f7d8:b560 with SMTP id 5614622812f47-3e6608dfc0cmr7351981b6e.18.1730489141211; Fri, 01 Nov 2024 12:25:41 -0700 (PDT) Received: from ?IPV6:2607:fea8:51de:a700::c2e1? ([2607:fea8:51de:a700::c2e1]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6d353fc5f13sm22329096d6.37.2024.11.01.12.25.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 01 Nov 2024 12:25:39 -0700 (PDT) Message-ID: <1e175b59-7c54-4646-b291-e753431d9c59@redhat.com> Date: Fri, 1 Nov 2024 15:25:37 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: gcc-patches Cc: Jakub Jelinek From: Andrew MacLeod Subject: [COMMITTED 3/3] Update bitwise_or op_range. 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 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~patchwork=sourceware.org@gcc.gnu.org When experimenting with more complex flow for the assume pass, I found a case where we were not as precise as we should be, but it was because range-ops hadn't been taught that if the LHS of a bitwise OR was positive, that the 2 RHS operands must also be positive. The testcase is simple a tweaked version of an existing testcase which turned if (a_1 < 0 && b_2 < 0) goto bb3; else goto bb4; into _4 = a_1 | b_2 if (_4 < 0) goto bb3; else goto bb4; on the branch to bb4, the condition is _4 >= 0 and we were not getting positive values for a_1 and b_2 on that edge resulting in ranges that included [-INF, -1] that shouldn't have been there. This patch fixes that by implementing this functionality in op1_range in operator_bitwise_or, and adds the testcase. Bootstrapped on x86_64-pc-linux-gnu with no regressions.  Pushed. Andrew From 9283b1b3593f708a4fc11fb6dfcf208ab218a5d9 Mon Sep 17 00:00:00 2001 From: Andrew MacLeod Date: Thu, 31 Oct 2024 14:07:00 -0400 Subject: [PATCH 3/3] Update bitwise_or op_range. If the LHS of a bitwise OR is positive, then so are both operands when using op1_range or op2_range. gcc/ * range-op.cc (operator_bitwise_or::op1_range): If LHS is signed positive, so are both operands. gcc/testsuite * g++.dg/cpp23/attr-assume-opt.C (f2b): Alternate flow test. --- gcc/range-op.cc | 13 +++++++ gcc/testsuite/g++.dg/cpp23/attr-assume-opt.C | 37 +++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/gcc/range-op.cc b/gcc/range-op.cc index 58734cddd2f..5a1eb59f3cd 100644 --- a/gcc/range-op.cc +++ b/gcc/range-op.cc @@ -3822,6 +3822,19 @@ operator_bitwise_or::op1_range (irange &r, tree type, r.set_zero (type); return true; } + + // if (A < 0 && B < 0) + // Sometimes gets translated to + // _1 = A | B + // if (_1 < 0)) + // It is useful for ranger to recognize a positive LHS means the RHS + // operands are also positive when dealing with the ELSE range.. + if (TYPE_SIGN (type) == SIGNED && wi::ge_p (lhs.lower_bound (), 0, SIGNED)) + { + unsigned prec = TYPE_PRECISION (type); + r.set (type, wi::zero (prec), wi::max_value (prec, SIGNED)); + return true; + } r.set_varying (type); return true; } diff --git a/gcc/testsuite/g++.dg/cpp23/attr-assume-opt.C b/gcc/testsuite/g++.dg/cpp23/attr-assume-opt.C index 88d5e78dbba..e61ba7a27e0 100644 --- a/gcc/testsuite/g++.dg/cpp23/attr-assume-opt.C +++ b/gcc/testsuite/g++.dg/cpp23/attr-assume-opt.C @@ -38,5 +38,40 @@ f3 (int x, int y, int z) return 1; } +// This is the same as f2, except there is more complicated flow and +// required a range-op update to bitwise or. + +void barn(int x); +// assume (x+12 == 14 && y >= 0 && y + 10 < 13 && z + 4 >= 4 && z - 2 < 18) +// in different order and form with function calls to cause branches. +bool assume_func (int x, int y, int z) +{ + if (z - 2 >= 18) + return false; + if (x+12 != 14) + return false; + barn (x); + if (y < 0) + return false; + if (z + 4 < 4) + return false; + barn (y); + if (y + 10 >= 13) + return false; + barn (z); + return true; +} + +int +f2b (int x, int y, int z) +{ + [[assume (assume_func (x, y, z))]]; + unsigned q = x + y + z; + if (q*2 > 46) + return 0; + return 1; +} + + /* { dg-final { scan-tree-dump-times "return 0" 0 "vrp2" } } */ -/* { dg-final { scan-tree-dump-times "return 1" 3 "vrp2" } } */ +/* { dg-final { scan-tree-dump-times "return 1" 4 "vrp2" } } */ -- 2.45.0