From patchwork Fri Nov 11 09:09:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 18585 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp630902wru; Fri, 11 Nov 2022 01:10:33 -0800 (PST) X-Google-Smtp-Source: AA0mqf5cP8Fve6QSXwsp+J/DMbQ3scNJ/QnXYt0UfIPEThukJuA8g6WPg3lV4Gb/GokvIeeoL4I7 X-Received: by 2002:a17:907:c292:b0:7ad:90dd:4af with SMTP id tk18-20020a170907c29200b007ad90dd04afmr1104663ejc.488.1668157833605; Fri, 11 Nov 2022 01:10:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668157833; cv=none; d=google.com; s=arc-20160816; b=DPQ+yDBVoYTkcrmRbnmy/D55EYLGpb1NIF8EN6XduaADxNwDF5Gi90GQDckXi+Qq95 V94GIPWNL4XRcmhLX9tlTzXxUHHcy8SrBr9qoEHGgCJDHqwsErFgp2Oc0nw5Z0733JR+ WT+2gydYM4PNMnzz/wuks7uiRB0Jv2Rs1cEpz5yg8cpzcALMXlDmZiu10DaWBv3x8GNw cI7lnYytwkhGZdVcL2GFgBtIPH2XOTgf7vA2SXwNLa24DtGa4WtUEmpnVgLb3HSFGWyz r/p6/huXPBFifEjBI5ws60Uaiu8RCOMQP0huTCSJzDbf+bldKIyocgPlKQXeHuXpPe6U kx2g== 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=dmfYQpf9AuiaLs1RYSHtMgG95EOvx356mIzZQIwLUkU=; b=kCjJQBJybwy0mDP2BKJxALm30VxJRKXMxqd5wgN15Fl2171AgyqVxQQEjviktIJIgI J3s0T3YuNsKEDXRaJGdv2vDneS6UIk/O5/ikC1ZS6iuttC4VOCZoK2IsUX1dOt2E2WpE 9T1SQTj5pMIDt/Wf01MU8rH8g8FaA1MOqgZ+MG0I4vG9E/itux2YudW0b1Ua2eglW6h2 gxWwy7Rhoe0Tf7cZXCBt0HO5Gge3nnI9luZ8pvF9opu43jCBO2MCq2ksYmAd8KE7TeEs off3xvjMogxxAURphR9XVqO7vyKA9tdMqZhsaUUQo8uPCiNNRIj/8dQbh5xgc5ZFkIF8 zIww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=WAb5+kbC; 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 ho35-20020a1709070ea300b007ae5b41855fsi1862650ejc.895.2022.11.11.01.10.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Nov 2022 01:10:33 -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=WAb5+kbC; 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 67CBB3858D20 for ; Fri, 11 Nov 2022 09:10:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 67CBB3858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668157832; bh=dmfYQpf9AuiaLs1RYSHtMgG95EOvx356mIzZQIwLUkU=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=WAb5+kbCeEgBbHoviQRLytkXrLZJMma7IcWEN2x6crzKJo0hbPuH3MZzBPgD0I5jv jOjAlue8x7BFmCzTvFgjW9kEXPwdxJ4XWjYRpLb/5MosCMW5BjwDJ5vvjafZcwufor NPvD+JMbmSYS1WlrgaumYku9nc5XA0tX514hR3To= 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 733F63858D1E for ; Fri, 11 Nov 2022 09:09:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 733F63858D1E Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-132-ybrM6jMRPMq3RehknNtctA-1; Fri, 11 Nov 2022 04:09:45 -0500 X-MC-Unique: ybrM6jMRPMq3RehknNtctA-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 8FC4D29AB453 for ; Fri, 11 Nov 2022 09:09:45 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 504AD40E9783; Fri, 11 Nov 2022 09:09:45 +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 2AB99guv3249119 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 11 Nov 2022 10:09:43 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2AB99g1a3249118; Fri, 11 Nov 2022 10:09:42 +0100 Date: Fri, 11 Nov 2022 10:09:42 +0100 To: Aldy Hernandez Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] range-op: Implement floating point division 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.9 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?1749190268165365789?= X-GMAIL-MSGID: =?utf-8?q?1749190268165365789?= Hi! Here is the floating point division fold_range implementation, as I wrote in the last mail, we could outline some of the common parts into static methods with descriptive names and share them between foperator_div and foperator_mult. Bootstrapped/regtested on top of the earlier version of the multiplication fold_range on x86_64-linux and i686-linux, regressions are +FAIL: gcc.dg/pr95115.c execution test +FAIL: libphobos.phobos/std/math/hardware.d execution test +FAIL: libphobos.phobos_shared/std/math/hardware.d execution test The first test is we have: # RANGE [frange] double [] +-NAN _3 = Inf / Inf; if (_3 ord _3) goto ; [INV] else goto ; [INV] : abort (); : before evrp, the range is correct, Inf / Inf is known NAN of unknown sign. evrp correctly folds _3 ord _3 into false and the _3 = Inf / Inf; remains in the IL, but then comes dse1 and removes it as dead statement. So, I think yet another example of the PR107608 problems where DCE? removes dead statements which raise floating point exceptions. And -fno-delete-dead-exceptions doesn't help. 2022-11-11 Jakub Jelinek PR tree-optimization/107569 * range-op-float.cc (foperator_div): New class. (floating_op_table::floating_op_table): Use foperator_div for RDIV_EXPR. Jakub --- gcc/range-op-float.cc.jj 2022-11-10 12:31:57.987917289 +0100 +++ gcc/range-op-float.cc 2022-11-10 17:04:35.743056880 +0100 @@ -2027,6 +2027,183 @@ class foperator_mult : public range_oper } } fop_mult; +class foperator_div : 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) const final override + { + // +-0.0 / +-0.0 or +-INF / +-INF is a known NAN. + if ((real_iszero (&lh_lb) + && real_iszero (&lh_ub) + && real_iszero (&rh_lb) + && real_iszero (&rh_ub)) + || (real_isinf (&lh_lb) + && real_isinf (&lh_ub, real_isneg (&lh_lb)) + && real_isinf (&rh_lb) + && real_isinf (&rh_ub, real_isneg (&rh_lb)))) + { + real_nan (&lb, "", 0, TYPE_MODE (type)); + ub = lb; + maybe_nan = true; + return; + } + + bool both_maybe_zero = false; + bool both_maybe_inf = false; + bool must_have_signbit_zero = false; + bool must_have_signbit_nonzero = false; + + // If +-0.0 is in both ranges, it is a maybe NAN. + if (real_compare (LE_EXPR, &lh_lb, &dconst0) + && real_compare (GE_EXPR, &lh_ub, &dconst0) + && real_compare (LE_EXPR, &rh_lb, &dconst0) + && real_compare (GE_EXPR, &rh_ub, &dconst0)) + { + both_maybe_zero = true; + maybe_nan = true; + } + // If +-INF is in both ranges, it is a maybe NAN. + else if ((real_isinf (&lh_lb) || real_isinf (&lh_ub)) + && (real_isinf (&rh_lb) || real_isinf (&rh_ub))) + { + both_maybe_inf = true; + maybe_nan = true; + } + else + maybe_nan = false; + + if (real_isneg (&lh_lb) == real_isneg (&lh_ub) + && real_isneg (&rh_lb) == real_isneg (&rh_ub)) + { + if (real_isneg (&lh_lb) == real_isneg (&rh_ub)) + must_have_signbit_zero = true; + else + must_have_signbit_nonzero = true; + } + + // If dividend must be zero, the range is just +-0 + // (including if the divisor is +-INF). + // If divisor must be +-INF, the range is just +-0 + // (including if the dividend is zero). + if ((real_iszero (&lh_lb) && real_iszero (&lh_ub)) + || real_isinf (&rh_lb, false) + || real_isinf (&rh_ub, true)) + { + ub = lb = dconst0; + // If all the boundary signs are the same, [+0.0, +0.0]. + if (must_have_signbit_zero) + ; + // If divisor and dividend must have different signs, + // [-0.0, -0.0]. + else if (must_have_signbit_nonzero) + ub = lb = real_value_negate (&dconst0); + // Otherwise -> [-0.0, +0.0]. + else + lb = real_value_negate (&dconst0); + return; + } + + // If divisor must be zero, the range is just +-INF + // (including if the dividend is +-INF). + // If dividend must be +-INF, the range is just +-INF + // (including if the dividend is zero). + if ((real_iszero (&rh_lb) && real_iszero (&rh_ub)) + || real_isinf (&lh_lb, false) + || real_isinf (&lh_ub, true)) + { + // If all the boundary signs are the same, [+INF, +INF]. + if (must_have_signbit_zero) + ub = lb = dconstinf; + // If divisor and dividend must have different signs, + // [-INF, -INF]. + else if (must_have_signbit_nonzero) + ub = lb = dconstninf; + // Otherwise -> [-INF, +INF] (-INF or +INF). + else + { + lb = dconstninf; + ub = dconstinf; + } + return; + } + + // Otherwise if both operands may be zero, divisor could be + // nextafter(0.0, +-1.0) and dividend +-0.0 + // in which case result is going to INF or vice versa and + // result +0.0. So, all we can say for that case is if the + // signs of divisor and dividend are always the same we have + // [+0.0, +INF], if they are always different we have + // [-INF, -0.0]. If they vary, VARING. + // If both may be +-INF, divisor could be INF and dividend FLT_MAX, + // in which case result is going to INF or vice versa and + // result +0.0. So, all we can say for that case is if the + // signs of divisor and dividend are always the same we have + // [+0.0, +INF], if they are always different we have + // [-INF, -0.0]. If they vary, VARYING. + if (both_maybe_zero || both_maybe_inf) + { + if (must_have_signbit_zero) + { + lb = dconst0; + ub = dconstinf; + } + else if (must_have_signbit_nonzero) + { + lb = dconstninf; + ub = real_value_negate (&dconst0); + } + else + { + lb = dconstninf; + ub = dconstinf; + } + return; + } + + REAL_VALUE_TYPE cp[8]; + // Do a cross-division. At this point none of the divisions should + // produce a NAN. + gcc_assert (!maybe_nan); + frange_arithmetic (RDIV_EXPR, type, cp[0], lh_lb, rh_lb, dconstninf); + frange_arithmetic (RDIV_EXPR, type, cp[1], lh_lb, rh_ub, dconstninf); + frange_arithmetic (RDIV_EXPR, type, cp[2], lh_ub, rh_lb, dconstninf); + frange_arithmetic (RDIV_EXPR, type, cp[3], lh_ub, rh_ub, dconstninf); + frange_arithmetic (RDIV_EXPR, type, cp[4], lh_lb, rh_lb, dconstinf); + frange_arithmetic (RDIV_EXPR, type, cp[5], lh_lb, rh_ub, dconstinf); + frange_arithmetic (RDIV_EXPR, type, cp[6], lh_ub, rh_lb, dconstinf); + frange_arithmetic (RDIV_EXPR, type, cp[7], lh_ub, rh_ub, dconstinf); + + 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 divisor may be zero (but is not known to be only zero), + // and dividend can't be zero, the range can go up to -INF or +INF + // depending on the signs. + if (real_compare (LE_EXPR, &rh_lb, &dconst0) + && real_compare (GE_EXPR, &rh_ub, &dconst0)) + { + if (!must_have_signbit_zero) + real_inf (&lb, true); + if (!must_have_signbit_nonzero) + real_inf (&ub, false); + } + } +} fop_div; + // Instantiate a range_op_table for floating point operations. static floating_op_table global_floating_table; @@ -2062,6 +2239,7 @@ floating_op_table::floating_op_table () set (PLUS_EXPR, fop_plus); set (MINUS_EXPR, fop_minus); set (MULT_EXPR, fop_mult); + set (RDIV_EXPR, fop_div); } // Return a pointer to the range_operator_float instance, if there is