From patchwork Tue Nov 10 08:30:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 1397420 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=OQPF3Tpw; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CVh0C1jWMz9sRK for ; Tue, 10 Nov 2020 19:31:05 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 23D333982C7F; Tue, 10 Nov 2020 08:31:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 23D333982C7F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1604997060; bh=YbEfvhIHSgF/+Q92YXTN9cZmHwgg4+dXOyulyksRW9I=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=OQPF3TpwIMW9I4VS24Ze2owqoOobAQGzFtMuQSBOxzlpXnS2ZNsA361Xl3epEWgLg u9/F/UNX/MzVMZfCmrbW8Be2TKCs6TNcljX64saAWHR8yffpYN6a+X3KraUTD37Rg9 0m44qJ2byQO9t2/FqvKWkWgWPwf0H9zEnoYkkVek= 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 [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id 6CC50385DC37 for ; Tue, 10 Nov 2020 08:30:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 6CC50385DC37 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-164-8vK3NNmuOLaZ-7SdFDgbsg-1; Tue, 10 Nov 2020 03:30:54 -0500 X-MC-Unique: 8vK3NNmuOLaZ-7SdFDgbsg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D78E51006C9E for ; Tue, 10 Nov 2020 08:30:53 +0000 (UTC) Received: from abulafia.quesejoda.com (ovpn-113-118.ams2.redhat.com [10.36.113.118]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 64EEC6EF76; Tue, 10 Nov 2020 08:30:53 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.15.2/8.15.2) with ESMTPS id 0AA8UoGf779935 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 10 Nov 2020 09:30:50 +0100 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.15.2/8.15.2/Submit) id 0AA8UotH779934; Tue, 10 Nov 2020 09:30:50 +0100 To: GCC patches Subject: [PATCH] Normalize VARYING for -fstrict-enums. Date: Tue, 10 Nov 2020 09:30:27 +0100 Message-Id: <20201110083026.779841-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.2 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_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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@gcc.gnu.org Sender: "Gcc-patches" The problem here is that the representation for VARYING in -fstrict-enums is different between value_range and irange. The helper function irange::normalize_min_max() will normalize to VARYING only if setting the range to the entire domain of the underlying type. That is, [0, 0xff..ff], not the domain as defined by -fstrict-enums. This causes problems because the multi-range version of varying_p() will return true if the range is the domain as defined by -fstrict-enums. Thus, normalize_min_max and varying_p have different concepts of varying for multi-ranges. (BTW, legacy ranges are different because they never look at the extremes of a range to determine varying-ness. They only look at the kind field.) One approach is to change all the code to limit ranges to the domain in the -fstrict-enums world, but this won't work because there are various instances of gimple where the values assigned or compared are beyond the limits of TYPE_{MIN,MAX}_VALUE. One example is the addition of 0xffffffff to represent subtraction. This patch fixes multi-range varying_p() and set_varying() to agree with the normalization code, using the extremes of the underlying type, to represent varying. Pushed. gcc/ChangeLog: PR tree-optimization/97767 * value-range.cc (dump_bound_with_infinite_markers): Use wi::min_value and wi::max_value. (range_tests_strict_enum): New. (range_tests): Call range_tests_strict_enum. * value-range.h (irange::varying_p): Use wi::min_value and wi::max_value. (irange::set_varying): Same. (irange::normalize_min_max): Remove comment. gcc/testsuite/ChangeLog: * g++.dg/opt/pr97767.C: New test. --- gcc/testsuite/g++.dg/opt/pr97767.C | 10 +++++++ gcc/value-range.cc | 42 ++++++++++++++++++++++++++++-- gcc/value-range.h | 23 +++++++--------- 3 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 gcc/testsuite/g++.dg/opt/pr97767.C diff --git a/gcc/testsuite/g++.dg/opt/pr97767.C b/gcc/testsuite/g++.dg/opt/pr97767.C new file mode 100644 index 00000000000..da0879d0567 --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/pr97767.C @@ -0,0 +1,10 @@ +// { dg-do compile } +// { dg-options "-O -fstrict-enums -ftree-vrp -w" } + +enum { E0 = 0, E1 = 1, E2 = 2 } e; + +int +foo (void) +{ + return __builtin_popcount ((int) e); +} diff --git a/gcc/value-range.cc b/gcc/value-range.cc index 61f7da278d6..f83a824a982 100644 --- a/gcc/value-range.cc +++ b/gcc/value-range.cc @@ -1866,12 +1866,17 @@ static void dump_bound_with_infinite_markers (FILE *file, tree bound) { tree type = TREE_TYPE (bound); + wide_int type_min = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type)); + wide_int type_max = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type)); + if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type) - && vrp_val_is_min (bound) + && TREE_CODE (bound) == INTEGER_CST + && wi::to_wide (bound) == type_min && TYPE_PRECISION (type) != 1) fprintf (file, "-INF"); - else if (vrp_val_is_max (bound) + else if (TREE_CODE (bound) == INTEGER_CST + && wi::to_wide (bound) == type_max && TYPE_PRECISION (type) != 1) fprintf (file, "+INF"); else @@ -2241,6 +2246,38 @@ range_tests_legacy () } } +// Simulate -fstrict-enums where the domain of a type is less than the +// underlying type. + +static void +range_tests_strict_enum () +{ + // The enum can only hold [0, 3]. + tree rtype = copy_node (unsigned_type_node); + TYPE_MIN_VALUE (rtype) = build_int_cstu (rtype, 0); + TYPE_MAX_VALUE (rtype) = build_int_cstu (rtype, 3); + + // Test that even though vr1 covers the strict enum domain ([0, 3]), + // it does not cover the domain of the underlying type. + int_range<1> vr1 (build_int_cstu (rtype, 0), build_int_cstu (rtype, 1)); + int_range<1> vr2 (build_int_cstu (rtype, 2), build_int_cstu (rtype, 3)); + vr1.union_ (vr2); + ASSERT_TRUE (vr1 == int_range<1> (build_int_cstu (rtype, 0), + build_int_cstu (rtype, 3))); + ASSERT_FALSE (vr1.varying_p ()); + + // Test that copying to a multi-range does not change things. + int_range<2> ir1 (vr1); + ASSERT_TRUE (ir1 == vr1); + ASSERT_FALSE (ir1.varying_p ()); + + // The same test as above, but using TYPE_{MIN,MAX}_VALUE instead of [0,3]. + vr1 = int_range<1> (TYPE_MIN_VALUE (rtype), TYPE_MAX_VALUE (rtype)); + ir1 = vr1; + ASSERT_TRUE (ir1 == vr1); + ASSERT_FALSE (ir1.varying_p ()); +} + static void range_tests_misc () { @@ -2442,6 +2479,7 @@ range_tests () range_tests_legacy (); range_tests_irange3 (); range_tests_int_range_max (); + range_tests_strict_enum (); range_tests_misc (); } diff --git a/gcc/value-range.h b/gcc/value-range.h index a483fc802dd..7428c91ea57 100644 --- a/gcc/value-range.h +++ b/gcc/value-range.h @@ -280,12 +280,14 @@ irange::varying_p () const tree l = m_base[0]; tree u = m_base[1]; tree t = TREE_TYPE (l); + unsigned prec = TYPE_PRECISION (t); + signop sign = TYPE_SIGN (t); if (INTEGRAL_TYPE_P (t)) - return l == TYPE_MIN_VALUE (t) && u == TYPE_MAX_VALUE (t); + return (wi::to_wide (l) == wi::min_value (prec, sign) + && wi::to_wide (u) == wi::max_value (prec, sign)); if (POINTER_TYPE_P (t)) - return wi::to_wide (l) == 0 - && wi::to_wide (u) == wi::max_value (TYPE_PRECISION (t), - TYPE_SIGN (t)); + return (wi::to_wide (l) == 0 + && wi::to_wide (u) == wi::max_value (prec, sign)); return true; } @@ -469,8 +471,10 @@ irange::set_varying (tree type) m_num_ranges = 1; if (INTEGRAL_TYPE_P (type)) { - m_base[0] = TYPE_MIN_VALUE (type); - m_base[1] = TYPE_MAX_VALUE (type); + wide_int min = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type)); + wide_int max = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type)); + m_base[0] = wide_int_to_tree (type, min); + m_base[1] = wide_int_to_tree (type, max); } else if (POINTER_TYPE_P (type)) { @@ -566,13 +570,6 @@ irange::set_zero (tree type) } // Normalize a range to VARYING or UNDEFINED if possible. -// -// Avoid using TYPE_{MIN,MAX}_VALUE because -fstrict-enums can -// restrict those to a subset of what actually fits in the type. -// Instead use the extremes of the type precision which will allow -// compare_range_with_value() to check if a value is inside a range, -// whereas if we used TYPE_*_VAL, said function would just punt upon -// seeing a VARYING. inline void irange::normalize_min_max ()