From patchwork Thu Nov 10 13:44:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 18134 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp155038wru; Thu, 10 Nov 2022 05:45:29 -0800 (PST) X-Google-Smtp-Source: AA0mqf5/sSeN9oLellabtv+vmkH7IM4sB5OVHXBw9TpxaXyOHZbj2EqaBozwaUA7BKTcnJT5gpbZ X-Received: by 2002:a17:907:2e02:b0:7ae:4bab:179f with SMTP id ig2-20020a1709072e0200b007ae4bab179fmr23465649ejc.271.1668087929384; Thu, 10 Nov 2022 05:45:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668087929; cv=none; d=google.com; s=arc-20160816; b=S+auWv/UbvgVuMV2eUUH+OZ45RKewbH8poRTvfXtzp/y7SUrLuS2u2I9yF5OF2Izt4 QOMaM7bLK/61spyC9Xm5HoEBr4Y14VGsHYaD79xLgzH/X5EUhwiddUNJhmlKvVXlk4TA aLMuPJVU6ReO08Gv3J8A4DU+CN9RgnHrca0ghUszAbGzB56sE9edPiPQVPPl9Ez1zvwU OjUJAFuoG6GOXCYbRy8KlOCvcPHRimGEkwrmTsPk0GiC5sUxVC88RCDKzgB/UJQnjGWg W4AQpOcL7KmYvlAq0sYKfZOdnL2wbZvZT6CdLsQBnLGqMYYUjmQYhg2nYPoKw9o8W6kd vIWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=VRBa2OtG50tZmYxkpvE4CyGs6nSCj6qy+XGDlg+u5eY=; b=OSq62BRqfP88bSafeSlGOruF95Y+CtJeO0++h6X+jBElcIQ4+2cNZGF/yDwI48x9cx i9jT5cURAjvK741g4DfusbJioJeAmztRzOs27uE4fk1YVEfRFsGpvy0h17C41pmp/7QV porAqIcPYrNzK08UueDvgvFf7uHP+0/falk3G+5pNjGoFcx3eKZ0gBv5pUG80p+pKg6S U02TCAfu682xB/D3ZY4a6r9Nlr1BVHHM8XSTJj3GOdr2++KlxBubfSVl5YySVK0aQ/AL j7q2O74BfBG1jTm1LSQua1Gpj6voYXG/3KkzC77T6slcUxlHGs0fsmvYzRZs7S4tE9iB 7aXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=k3pDEdto; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id he36-20020a1709073da400b007912fb7ad6esi22168906ejc.893.2022.11.10.05.45.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 05:45:29 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=k3pDEdto; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 3660238582BD for ; Thu, 10 Nov 2022 13:45:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3660238582BD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668087928; bh=VRBa2OtG50tZmYxkpvE4CyGs6nSCj6qy+XGDlg+u5eY=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=k3pDEdtoIfJeStUgqiF+Ipbn5VaqFzs8yabz25j7BwxEnuUz+imh1ciaSS9N7gyE7 8Xt/0ONHN4Vtni9Q4X/BobXtwaa/Pe/UxJYkBlzaxwGgJP7Ilj63ZHAzNZaq6hPG/I JpGOhbWTmVAwlMuEvG2uT0fEpuEsAMLznAngbHQU= 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 CD5B53858D1E for ; Thu, 10 Nov 2022 13:44:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CD5B53858D1E 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-6-PedlBxv2MeOeZaOCsI3NDg-1; Thu, 10 Nov 2022 08:44:41 -0500 X-MC-Unique: PedlBxv2MeOeZaOCsI3NDg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F077E185A794 for ; Thu, 10 Nov 2022 13:44:40 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AE13140C6F73; Thu, 10 Nov 2022 13:44:40 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 2AADiXOw1372916 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 10 Nov 2022 14:44:38 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2AADiWnh1372915; Thu, 10 Nov 2022 14:44:32 +0100 Date: Thu, 10 Nov 2022 14:44:32 +0100 To: Aldy Hernandez Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] range-op: Implement floating point multiplication fold_range [PR107569] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek 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?1749116968791526672?= X-GMAIL-MSGID: =?utf-8?q?1749116968791526672?= Hi! The following patch implements frange multiplication, including the special case of x * x. The callers don't tell us that it is x * x, just that it is either z = x * x or if (x == y) z = x * y; For irange that makes no difference, but for frange it can mean x is -0.0 and y is 0.0 if they have the same range that includes both signed and unsigned zeros, so we need to assume result could be -0.0. The patch causes one regression: +FAIL: gcc.dg/fold-overflow-1.c scan-assembler-times 2139095040 2 but that is already tracked in PR107608 and affects not just the newly added multiplication, but addition and other floating point operations (and doesn't seem like a ranger bug but dce or whatever else). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? Once we have division and the reverse ops for all of these, perhaps we can do some cleanups to share common code, but the way I have division now partly written doesn't show up many commonalities. Multiplication is simple, division is a nightmare. 2022-11-10 Jakub Jelinek PR tree-optimization/107569 PR tree-optimization/107591 * range-op.h (range_operator_float::rv_fold): Add relation_kind argument. * range-op-float.cc (range_operator_float::fold_range): Name last argument trio and pass trio.op1_op2 () as last argument to rv_fold. (range_operator_float::rv_fold): Add relation_kind argument. (foperator_plus::rv_fold, foperator_minus::rv_fold): Likewise. (frange_mult): New function. (foperator_mult): New class. (floating_op_table::floating_op_table): Use foperator_mult for MULT_EXPR. Jakub --- gcc/range-op.h.jj 2022-11-10 00:55:09.430219763 +0100 +++ gcc/range-op.h 2022-11-10 11:30:33.594114939 +0100 @@ -123,7 +123,8 @@ public: const REAL_VALUE_TYPE &lh_lb, const REAL_VALUE_TYPE &lh_ub, const REAL_VALUE_TYPE &rh_lb, - const REAL_VALUE_TYPE &rh_ub) const; + const REAL_VALUE_TYPE &rh_ub, + relation_kind) const; // Unary operations have the range of the LHS as op2. virtual bool fold_range (irange &r, tree type, const frange &lh, --- gcc/range-op-float.cc.jj 2022-11-10 00:55:09.318221259 +0100 +++ gcc/range-op-float.cc 2022-11-10 11:31:29.040359082 +0100 @@ -51,7 +51,7 @@ along with GCC; see the file COPYING3. bool range_operator_float::fold_range (frange &r, tree type, const frange &op1, const frange &op2, - relation_trio) const + relation_trio trio) const { if (empty_range_varying (r, type, op1, op2)) return true; @@ -65,7 +65,7 @@ range_operator_float::fold_range (frange bool maybe_nan; rv_fold (lb, ub, maybe_nan, type, op1.lower_bound (), op1.upper_bound (), - op2.lower_bound (), op2.upper_bound ()); + op2.lower_bound (), op2.upper_bound (), trio.op1_op2 ()); // Handle possible NANs by saturating to the appropriate INF if only // one end is a NAN. If both ends are a NAN, just return a NAN. @@ -103,8 +103,8 @@ range_operator_float::rv_fold (REAL_VALU const REAL_VALUE_TYPE &lh_lb ATTRIBUTE_UNUSED, const REAL_VALUE_TYPE &lh_ub ATTRIBUTE_UNUSED, const REAL_VALUE_TYPE &rh_lb ATTRIBUTE_UNUSED, - const REAL_VALUE_TYPE &rh_ub ATTRIBUTE_UNUSED) - const + const REAL_VALUE_TYPE &rh_ub ATTRIBUTE_UNUSED, + relation_kind) const { lb = dconstninf; ub = dconstinf; @@ -1868,7 +1868,8 @@ class foperator_plus : public range_oper const REAL_VALUE_TYPE &lh_lb, const REAL_VALUE_TYPE &lh_ub, const REAL_VALUE_TYPE &rh_lb, - const REAL_VALUE_TYPE &rh_ub) const final override + const REAL_VALUE_TYPE &rh_ub, + relation_kind) const final override { frange_arithmetic (PLUS_EXPR, type, lb, lh_lb, rh_lb, dconstninf); frange_arithmetic (PLUS_EXPR, type, ub, lh_ub, rh_ub, dconstinf); @@ -1892,7 +1893,8 @@ class foperator_minus : public range_ope const REAL_VALUE_TYPE &lh_lb, const REAL_VALUE_TYPE &lh_ub, const REAL_VALUE_TYPE &rh_lb, - const REAL_VALUE_TYPE &rh_ub) const final override + const REAL_VALUE_TYPE &rh_ub, + relation_kind) const final override { frange_arithmetic (MINUS_EXPR, type, lb, lh_lb, rh_ub, dconstninf); frange_arithmetic (MINUS_EXPR, type, ub, lh_ub, rh_lb, dconstinf); @@ -1908,6 +1910,123 @@ class foperator_minus : public range_ope } } fop_minus; +/* Wrapper around frange_arithmetics, that computes the result + if inexact rounded to both directions. Also, if one of the + operands is +-0.0 and another +-INF, return +-0.0 rather than + NAN. */ + +static void +frange_mult (tree type, REAL_VALUE_TYPE &result_lb, REAL_VALUE_TYPE &result_ub, + const REAL_VALUE_TYPE &op1, const REAL_VALUE_TYPE &op2) +{ + if (real_iszero (&op1) && real_isinf (&op2)) + { + result_lb = op1; + if (real_isneg (&op2)) + result_lb = real_value_negate (&result_lb); + result_ub = result_lb; + } + else if (real_isinf (&op1) && real_iszero (&op2)) + { + result_lb = op2; + if (real_isneg (&op1)) + result_lb = real_value_negate (&result_lb); + result_ub = result_lb; + } + else + { + frange_arithmetic (MULT_EXPR, type, result_lb, op1, op2, dconstninf); + frange_arithmetic (MULT_EXPR, type, result_ub, op1, op2, dconstinf); + } +} + +class foperator_mult : public range_operator_float +{ + void rv_fold (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, bool &maybe_nan, + tree type, + const REAL_VALUE_TYPE &lh_lb, + const REAL_VALUE_TYPE &lh_ub, + const REAL_VALUE_TYPE &rh_lb, + const REAL_VALUE_TYPE &rh_ub, + relation_kind kind) const final override + { + REAL_VALUE_TYPE cp[8]; + bool is_square + = (kind == VREL_EQ + && real_equal (&lh_lb, &rh_lb) + && real_equal (&lh_ub, &rh_ub) + && real_isneg (&lh_lb) == real_isneg (&rh_lb) + && real_isneg (&lh_ub) == real_isneg (&rh_ub)); + // Do a cross-product. + frange_mult (type, cp[0], cp[4], lh_lb, rh_lb); + if (is_square) + { + // For x * x we can just do max (lh_lb * lh_lb, lh_ub * lh_ub) + // as maximum and -0.0 as minimum if 0.0 is in the range, + // otherwise min (lh_lb * lh_lb, lh_ub * lh_ub). + // -0.0 rather than 0.0 because VREL_EQ doesn't prove that + // x and y are bitwise equal, just that they compare equal. + if (real_compare (LE_EXPR, &lh_lb, &dconst0) + && real_compare (GE_EXPR, &lh_ub, &dconst0)) + cp[1] = real_value_negate (&dconst0); + else + cp[1] = cp[0]; + cp[2] = cp[0]; + cp[5] = cp[4]; + cp[6] = cp[4]; + } + else + { + frange_mult (type, cp[1], cp[5], lh_lb, rh_ub); + frange_mult (type, cp[2], cp[6], lh_ub, rh_lb); + } + frange_mult (type, cp[3], cp[7], lh_ub, rh_ub); + for (int i = 1; i < 4; ++i) + { + if (real_less (&cp[i], &cp[0]) + || (real_iszero (&cp[0]) && real_isnegzero (&cp[i]))) + std::swap (cp[i], cp[0]); + if (real_less (&cp[4], &cp[i + 4]) + || (real_isnegzero (&cp[4]) && real_iszero (&cp[i + 4]))) + std::swap (cp[i + 4], cp[4]); + } + lb = cp[0]; + ub = cp[4]; + + // If both operands are the same, then we know it can be +-0.0, or +-INF, + // but not both at the same time, so it will never be invalid unless + // operand was already NAN. + if (is_square) + maybe_nan = false; + // [+-0, +-0] * [+INF,+INF] (or [-INF,-INF] or swapped is a known NAN. + else if ((real_iszero (&lh_lb) + && real_iszero (&lh_ub) + && real_isinf (&rh_lb) + && real_isinf (&rh_ub, real_isneg (&rh_lb))) + || (real_iszero (&rh_lb) + && real_iszero (&rh_ub) + && real_isinf (&lh_lb) + && real_isinf (&lh_ub, real_isneg (&lh_lb)))) + { + real_nan (&lb, "", 0, TYPE_MODE (type)); + ub = lb; + maybe_nan = true; + } + // Otherwise, if one range includes zero and the other ends with +-INF, + // it is a maybe NAN. + else if (real_compare (LE_EXPR, &lh_lb, &dconst0) + && real_compare (GE_EXPR, &lh_ub, &dconst0) + && (real_isinf (&rh_lb) || real_isinf (&rh_ub))) + maybe_nan = true; + else if (real_compare (LE_EXPR, &rh_lb, &dconst0) + && real_compare (GE_EXPR, &rh_ub, &dconst0) + && (real_isinf (&lh_lb) || real_isinf (&lh_ub))) + maybe_nan = true; + else + maybe_nan = false; + } +} fop_mult; + // Instantiate a range_op_table for floating point operations. static floating_op_table global_floating_table; @@ -1942,6 +2061,7 @@ floating_op_table::floating_op_table () set (NEGATE_EXPR, fop_negate); set (PLUS_EXPR, fop_plus); set (MINUS_EXPR, fop_minus); + set (MULT_EXPR, fop_mult); } // Return a pointer to the range_operator_float instance, if there is