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.129.124]) by sourceware.org (Postfix) with ESMTPS id 5FA8B3949F19 for ; Mon, 5 Dec 2022 20:43:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5FA8B3949F19 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=1670273005; 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=JQxKgs9TXBYdcb+vzpQa4ZMx3vhDIO7xw1hU2nCnRpk=; b=NLGbV9IwGVCGljZ76vSJ97lqAqi/WSC0IWYxu0GxN47mtZ3cLCNj+Qb3KhiRWpg6/vRE9q IQ5jA/jbo0ZysdOELi+1tfd/SyQ+rmF8ZWx1yk75/T61qwwvFNP9Zlg460igyckG1VoPsj 0Hq66QEJLkRY2xScVc924TgE44PWEco= Received: from mail-il1-f197.google.com (mail-il1-f197.google.com [209.85.166.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-77-LAMwbRxMOD-EI2o34pBL7w-1; Mon, 05 Dec 2022 15:43:19 -0500 X-MC-Unique: LAMwbRxMOD-EI2o34pBL7w-1 Received: by mail-il1-f197.google.com with SMTP id a13-20020a056e0208ad00b003034c36b8b5so6029595ilt.9 for ; Mon, 05 Dec 2022 12:43:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JQxKgs9TXBYdcb+vzpQa4ZMx3vhDIO7xw1hU2nCnRpk=; b=M0FaSwnmroVlrW5rRV3d1E/YudJlwwNmetBiUXhfnZkXI8reRueh+YgtqOQFOUFNCh EyqgB/3i3iolNZD6YwwBke1B5UX/yNqdbeYitesixIXcTLzqheku2HDLzbwnvX0ESqFj pY1HNLCPzowT1XrL/at0GBpSbFJBR9DFsbtXJYvfkqI2od6YrtA4LImetfgnoR53aoby CTxRd7Efs1TnUEc1P2m320UThwi8TQr6HrR3w/W7yxP/LvIheJOLZgsANr7lDqbev2my Ret64UUGVqYcxhaPkT6CwKUs9GpkLxeq3/VV2O3YU88mb3p/xpqh3yvfkHmK/iiwiPbp 0zBg== X-Gm-Message-State: ANoB5pn8l1uVMi/zLgTP7a7qrv/DyrKWD8Q12/ctL6WBy5hknV3wmtPl /IZ1Z8lrHkNG45hfMPNPvMyYjzCuoE9yw7OLkrBeEtTayWssd4bd/mIJDiGFVOHsr6tZHyJvwT2 Whyv06jhnmitfz+QVug== X-Received: by 2002:a05:6602:234a:b0:6df:f7fd:52bc with SMTP id r10-20020a056602234a00b006dff7fd52bcmr4896436iot.90.1670272998280; Mon, 05 Dec 2022 12:43:18 -0800 (PST) X-Google-Smtp-Source: AA0mqf7wRYQ1MY+aRcaVZze5wD5reWGm0+sTbj+NpvJFLWVwoYUmUptH5qSho76acopob1GgnnRNwA== X-Received: by 2002:a05:6602:234a:b0:6df:f7fd:52bc with SMTP id r10-20020a056602234a00b006dff7fd52bcmr4896425iot.90.1670272997874; Mon, 05 Dec 2022 12:43:17 -0800 (PST) Received: from ?IPV6:2607:fea8:a263:f600::6c01? ([2607:fea8:a263:f600::6c01]) by smtp.gmail.com with ESMTPSA id a17-20020a056638059100b00389e336e92fsm5842399jar.75.2022.12.05.12.43.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 05 Dec 2022 12:43:17 -0800 (PST) Message-ID: Date: Mon, 5 Dec 2022 15:43:16 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Subject: Re: [PATCH] range-op-float: Improve binary reverse operations To: Jakub Jelinek , Aldy Hernandez Cc: gcc-patches@gcc.gnu.org References: <1f2b50a8-8f3c-690a-182b-c636fc2f86ed@redhat.com> <32b96813-616d-ba73-811f-8a36e70f9ecd@redhat.com> From: Andrew MacLeod In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,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 List-Id: On 12/5/22 10:33, Jakub Jelinek wrote: > Hi! > > On Mon, Dec 05, 2022 at 02:29:36PM +0100, Aldy Hernandez wrote: >>> So like this for multiplication op1/2_range if it passes bootstrap/regtest? >>> For division I'll need to go to a drawing board... >> Sure, looks good to me. > Ulrich just filed PR107972, so in the light of that PR the following patch > attempts to do that differently. > > Is this also ok if it passes bootstrap/regtest? Id actually prefer to avoid passing the tree code around... we're trying to avoid that sort of thing even though Aldy temporarily introduced them to range-ops. Hes suppose to remove that next stage 1 :-P   Ideally anything "special" is locally contained to the specific routine. It looks like the only time we actually do anything different is for divide op2_range?   the divide op1_range seems to still call frange_drop_infs() under the same conditions.. In which case, maybe we can just change op2_range to contain all the special casing.. frange_drop_infs doesnt seem overly complicated, so just specialize it?  ie for divide op2_range do something like this instead of the call?: if (!ret)   return false; if (r.known_isnan () || lhs.known_isnan () || r.undefined_p ())   r.set_varying (type); else if (!lhs.maybe_isnan ())   {     r.clear_nan();     if (!contains_zero_p (lhs_lb, lhs_ub))       frange_drop_infs (r, type);   } else   r.update_nan (); return true; or whatever the sequence precisely works out to. Andrew > As for testcase, I've tried both attached testcases, but unfortunately it > seems that in neither of the cases we actually figure out that res range > is finite (or for last function non-zero ordered). So there is further > work needed on that. > > 2022-12-05 Jakub Jelinek > > PR tree-optimization/107972 > * range-op-float.cc (frange_drop_infs): New function. > (float_binary_op_range_finish): Add OP argument. If OP is not > RDIV_EXPR and lhs is finite, r must be finite too. > (foperator_plus::op1_range, foperator_minus::op1_range, > foperator_minus::op2_range, foperator_mult::op1_range): Pass > operation code to float_binary_op_range_finish. > (foperator_div::op1_range): Pass RDIV_EXPR to > float_binary_op_range_finish. If lhs is finite, r must be finite > too. > (foperator_div::op2_range): Pass RDIV_EXPR to > float_binary_op_range_finish. If lhs is not NAN nor zero, r must > be finite. > > --- gcc/range-op-float.cc.jj 2022-12-05 11:17:34.900573272 +0100 > +++ gcc/range-op-float.cc 2022-12-05 16:13:54.414845672 +0100 > @@ -330,6 +330,18 @@ frange_drop_ninf (frange &r, tree type) > r.intersect (tmp); > } > > +// Crop R to [MIN, MAX] where MAX is the maximum representable number > +// for TYPE and MIN the minimum representable number for TYPE. > + > +static inline void > +frange_drop_infs (frange &r, tree type) > +{ > + REAL_VALUE_TYPE max = real_max_representable (type); > + REAL_VALUE_TYPE min = real_min_representable (type); > + frange tmp (type, min, max); > + r.intersect (tmp); > +} > + > // If zero is in R, make sure both -0.0 and +0.0 are in the range. > > static inline void > @@ -1883,7 +1895,7 @@ foperator_unordered_equal::op1_range (fr > > static bool > float_binary_op_range_finish (bool ret, frange &r, tree type, > - const frange &lhs) > + const frange &lhs, enum tree_code op) > { > if (!ret) > return false; > @@ -1904,7 +1916,16 @@ float_binary_op_range_finish (bool ret, > // If lhs isn't NAN, then neither operand could be NAN, > // even if the reverse operation does introduce a maybe_nan. > if (!lhs.maybe_isnan ()) > - r.clear_nan (); > + { > + r.clear_nan (); > + if (op != RDIV_EXPR > + && !real_isinf (&lhs.lower_bound ()) > + && !real_isinf (&lhs.upper_bound ())) > + // For reverse + or - or *, if result is finite, then r must > + // be finite too, as X + INF or X - INF or X * INF is always > + // +-INF or NAN. For division handle it in the caller. > + frange_drop_infs (r, type); > + } > // If lhs is a maybe or known NAN, the operand could be > // NAN. > else > @@ -2020,7 +2041,7 @@ public: > if (!minus) > return false; > return float_binary_op_range_finish (minus.fold_range (r, type, lhs, op2), > - r, type, lhs); > + r, type, lhs, PLUS_EXPR); > } > virtual bool op2_range (frange &r, tree type, > const frange &lhs, > @@ -2067,7 +2088,7 @@ public: > return false; > return float_binary_op_range_finish (fop_plus.fold_range (r, type, lhs, > op2), > - r, type, lhs); > + r, type, lhs, MINUS_EXPR); > } > virtual bool op2_range (frange &r, tree type, > const frange &lhs, > @@ -2077,7 +2098,7 @@ public: > if (lhs.undefined_p ()) > return false; > return float_binary_op_range_finish (fold_range (r, type, op1, lhs), > - r, type, lhs); > + r, type, lhs, MINUS_EXPR); > } > private: > void rv_fold (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, bool &maybe_nan, > @@ -2166,7 +2187,7 @@ public: > // or if lhs must be zero and op2 doesn't include zero, it would be > // UNDEFINED, while rdiv.fold_range computes a zero or singleton INF > // range. Those are supersets of UNDEFINED, so let's keep that way. > - return float_binary_op_range_finish (ret, r, type, lhs); > + return float_binary_op_range_finish (ret, r, type, lhs, MULT_EXPR); > } > virtual bool op2_range (frange &r, tree type, > const frange &lhs, > @@ -2313,7 +2334,12 @@ public: > zero_to_inf_range (lb, ub, signbit_known); > r.set (type, lb, ub); > } > - return float_binary_op_range_finish (ret, r, type, lhs); > + // If lhs must be finite (can't be +-INF nor NAN), then op1 must be > + // finite too - +-INF / anything is either +-INF or NAN (NAN if op2 is > + // +-INF or NAN). > + if (!real_isinf (&lhs_lb) && !real_isinf (&lhs_ub) && !lhs.maybe_isnan ()) > + frange_drop_infs (r, type); > + return float_binary_op_range_finish (ret, r, type, lhs, RDIV_EXPR); > } > virtual bool op2_range (frange &r, tree type, > const frange &lhs, > @@ -2341,7 +2367,11 @@ public: > zero_to_inf_range (lb, ub, signbit_known); > r.set (type, lb, ub); > } > - return float_binary_op_range_finish (ret, r, type, lhs); > + // If lhs can't be zero nor NAN, then op2 must be finite - anything / +-INF > + // is either +-0 or NAN (NAN if op1 is +-INF or NAN). > + if (!contains_zero_p (lhs_lb, lhs_ub) && !lhs.maybe_isnan ()) > + frange_drop_infs (r, type); > + return float_binary_op_range_finish (ret, r, type, lhs, RDIV_EXPR); > } > private: > void rv_fold (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, bool &maybe_nan, > > > Jakub