From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 605743857806 for ; Tue, 11 Oct 2022 13:51:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 605743857806 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665496317; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ogCwlZAmlV6I662gcNeOhgxx5kQ8s48Ws73lRgwpT5w=; b=d6xKdHh7+DN8+FvQ8E3hrsrOZ7SXKPQxc0gGEG1iCX+znbY3e79iu23BVjRmXIcnEEyDoT nw6A2KgFBnITtcVUl6cKzQKrIEmHsCLzMoHdVoV+30z3OFNaXfKOBln0/DLih0qmqCZHQF k0fW36JQPRKpqOVNs/jei/6Vy7Ajxv0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-614-Tumo9r3aNzKYdBjBBWVu3Q-1; Tue, 11 Oct 2022 09:51:56 -0400 X-MC-Unique: Tumo9r3aNzKYdBjBBWVu3Q-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D5D8C85A5A6 for ; Tue, 11 Oct 2022 13:51:55 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.194.162]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7010F2166B26; Tue, 11 Oct 2022 13:51:55 +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 29BDprjx369689 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 11 Oct 2022 15:51:53 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 29BDprNP369688; Tue, 11 Oct 2022 15:51:53 +0200 From: Aldy Hernandez To: GCC patches Cc: Andrew MacLeod , Aldy Hernandez Subject: [COMMITTED] Implement ABS_EXPR operator for frange. Date: Tue, 11 Oct 2022 15:51:36 +0200 Message-Id: <20221011135136.369644-4-aldyh@redhat.com> In-Reply-To: <20221011135136.369644-1-aldyh@redhat.com> References: <20221011135136.369644-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.5 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 List-Id: Implementing ABS_EXPR allows us to fold certain __builtin_inf calls since they are expanded into calls to involving ABS_EXPR. This is an adaptation of the integer version. gcc/ChangeLog: * range-op-float.cc (class foperator_abs): New. (floating_op_table::floating_op_table): Add ABS_EXPR entry. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/vrp-float-abs-1.c: New test. --- gcc/range-op-float.cc | 91 +++++++++++++++++++ .../gcc.dg/tree-ssa/vrp-float-abs-1.c | 17 ++++ 2 files changed, 108 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp-float-abs-1.c diff --git a/gcc/range-op-float.cc b/gcc/range-op-float.cc index ef51b7538e3..283eb134c78 100644 --- a/gcc/range-op-float.cc +++ b/gcc/range-op-float.cc @@ -1132,6 +1132,95 @@ foperator_ordered::op1_range (frange &r, tree type, return true; } +class foperator_abs : public range_operator_float +{ + using range_operator_float::fold_range; + using range_operator_float::op1_range; +public: + bool fold_range (frange &r, tree type, + const frange &op1, const frange &, + relation_kind) const final override; + bool op1_range (frange &r, tree type, + const frange &lhs, const frange &op2, + relation_kind rel) const final override; +} fop_abs; + +bool +foperator_abs::fold_range (frange &r, tree type, + const frange &op1, const frange &op2, + relation_kind) const +{ + if (empty_range_varying (r, type, op1, op2)) + return true; + if (op1.known_isnan ()) + { + r.set_nan (type, /*sign=*/false); + return true; + } + + const REAL_VALUE_TYPE lh_lb = op1.lower_bound (); + const REAL_VALUE_TYPE lh_ub = op1.upper_bound (); + // Handle the easy case where everything is positive. + if (real_compare (GE_EXPR, &lh_lb, &dconst0) + && !real_iszero (&lh_lb, /*sign=*/true) + && !op1.maybe_isnan (/*sign=*/true)) + { + r = op1; + return true; + } + + REAL_VALUE_TYPE min = real_value_abs (&lh_lb); + REAL_VALUE_TYPE max = real_value_abs (&lh_ub); + // If the range contains zero then we know that the minimum value in the + // range will be zero. + if (real_compare (LE_EXPR, &lh_lb, &dconst0) + && real_compare (GE_EXPR, &lh_ub, &dconst0)) + { + if (real_compare (GT_EXPR, &min, &max)) + max = min; + min = dconst0; + } + else + { + // If the range was reversed, swap MIN and MAX. + if (real_compare (GT_EXPR, &min, &max)) + std::swap (min, max); + } + + r.set (type, min, max); + if (op1.maybe_isnan ()) + r.update_nan (/*sign=*/false); + else + r.clear_nan (); + return true; +} + +bool +foperator_abs::op1_range (frange &r, tree type, + const frange &lhs, const frange &op2, + relation_kind) const +{ + if (empty_range_varying (r, type, lhs, op2)) + return true; + if (lhs.known_isnan ()) + { + r.set_nan (type); + return true; + } + + // Start with the positives because negatives are an impossible result. + frange positives (type, dconst0, frange_val_max (type)); + positives.update_nan (/*sign=*/false); + positives.intersect (lhs); + r = positives; + // Then add the negative of each pair: + // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20]. + r.union_ (frange (type, + real_value_negate (&positives.upper_bound ()), + real_value_negate (&positives.lower_bound ()))); + return true; +} + class foperator_unordered_lt : public range_operator_float { using range_operator_float::fold_range; @@ -1502,6 +1591,8 @@ floating_op_table::floating_op_table () set (UNEQ_EXPR, fop_unordered_equal); set (ORDERED_EXPR, fop_ordered); set (UNORDERED_EXPR, fop_unordered); + + set (ABS_EXPR, fop_abs); } // Return a pointer to the range_operator_float instance, if there is diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-abs-1.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-abs-1.c new file mode 100644 index 00000000000..4b7b75833e0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp-float-abs-1.c @@ -0,0 +1,17 @@ +// { dg-do compile } +// { dg-options "-O2 -fno-thread-jumps -fdump-tree-evrp" } + +void link_error (); + +void +foo (double x, double y) +{ + if (x > y && __builtin_signbit (y) == 0) + { + // y == +INF is impossible. + if (__builtin_isinf (y)) + link_error (); + } +} + +// { dg-final { scan-tree-dump-not "link_error" "evrp" } } -- 2.37.3