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 B2B4B3858CDA for ; Wed, 31 Aug 2022 10:08:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B2B4B3858CDA 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=1661940503; 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: in-reply-to:in-reply-to:references:references; bh=IeXJ2zbuZzbfitRIVVrCKNcITzIDpep0MBj03uxmWag=; b=fzMFJBizZHWFnTCdSlBdsX9O01BSsFvGA3g3b7aTFlpLozKT8S50Md+CkQVpQ6fX0C9Eqh VugyjV/Rp2+uPiAXf7Ui47gdJ3vGz1nR6vLLqArh2aq9hSLaWGSctzpPk1ifX2ijcIBY0K xepBeJe8oE+dBpOBEO4xeGzm4L1HNAA= Received: from mail-oa1-f69.google.com (mail-oa1-f69.google.com [209.85.160.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-182-HzUr38ZtO2Og9QImvgeP9Q-1; Wed, 31 Aug 2022 06:08:22 -0400 X-MC-Unique: HzUr38ZtO2Og9QImvgeP9Q-1 Received: by mail-oa1-f69.google.com with SMTP id 586e51a60fabf-11ea2e0a080so3469801fac.0 for ; Wed, 31 Aug 2022 03:08:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=IeXJ2zbuZzbfitRIVVrCKNcITzIDpep0MBj03uxmWag=; b=fGuSEXBh0zt6v8NaWsF1sxP1A9WJmsq+Ye911m2cR97bUS1KqezxA58uROV6M8qjxd x3qxCxLXz2WTc19yng3orFzcHtQH+cVP7/I932oMhmyU/cdMOGgqw8ILDH2P5XTmYyFw mGTjmCLHk6UFUDxw52e59HJmfEI5/UOc92hf0/nUhrGdrgXh1Inqy/nXCmli8fGFGKE7 UErDCO7fHqzjxeH8OFArbpfjlihLhP6h4NdNsto5EExu6Ltw6m0P5345SD7o8FGeGfNw 9kQroXDCxRGmw726MDlC4eweoOmNv/k8XHUpK7fJCX+h4SgBg3lKftq+sHp8GESbDQ9Q CLJQ== X-Gm-Message-State: ACgBeo1dmU/gpbBem9rnUfTpKl3FjziM9ubuqh5BgiIm7F5B+yY06WD3 erHDT2HuuNzh52WLZ6fKt7LzV3unszS77AjhC09+ZXk5NAg/c5Zqe5jFBpsLhIzEgR6r+FVGC8K zG2vaURSH2hKEjCDK8kaRt5hFECDDUU2NTw== X-Received: by 2002:a05:6808:f07:b0:344:7739:8e7b with SMTP id m7-20020a0568080f0700b0034477398e7bmr891624oiw.265.1661940501407; Wed, 31 Aug 2022 03:08:21 -0700 (PDT) X-Google-Smtp-Source: AA6agR7VnkhIT6fa5YhmJDUwou4c1bVexVvJsb5kM4k29lE5QjbQnYH2+50STzIwc62le0sjjOEF0RiYQKHrQ8SrORI= X-Received: by 2002:a05:6808:f07:b0:344:7739:8e7b with SMTP id m7-20020a0568080f0700b0034477398e7bmr891611oiw.265.1661940501072; Wed, 31 Aug 2022 03:08:21 -0700 (PDT) MIME-Version: 1.0 References: <20220826154606.1155977-1-aldyh@redhat.com> In-Reply-To: From: Aldy Hernandez Date: Wed, 31 Aug 2022 12:08:10 +0200 Message-ID: Subject: Re: [PATCH] [ranger] x == -0.0 does not mean we can replace x with -0.0 To: Jakub Jelinek Cc: GCC patches , Andrew MacLeod , Jeff Law X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,KAM_NUMSUBJECT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: I think we can model the signed zero problem by keeping track of a sign property, similar to how we keep track of a NAN property. The property can be yes, no, or unknown, and would apply to the entire range. [0.0, 0.0] SIGN => -0.0 singleton [0.0, 0.0] !SIGN => +0.0 singleton [0.0, 0.0] VARYING => [-0.0, +0.0] sign unknown frange::singleton_p() would return the appropriate zero if the sign bit is definitely known, otherwise it would return NULL, which would keep VRP from propagating it. This is a sample of how I envision it working with __builtin_signbit: =========== BB 2 ============ Imports: x_3(D) Exports: _1 x_3(D) _1 : x_3(D)(I) x_3(D) [frange] float VARYING : _1 = __builtin_signbit (x_3(D)); if (_1 == 0) goto ; [INV] else goto ; [INV] 2->3 (T) _1 : [irange] int [0, 0] NONZERO 0x0 2->3 (T) x_3(D) : [frange] float [0.0, Inf] !SIGN 2->4 (F) _1 : [irange] int [-INF, -1][1, +INF] 2->4 (F) x_3(D) : [frange] float [ -Inf, 0.0] SIGN That is, on the TRUE side x_3 can be assumed to be positive, including the zero. On the FALSE side x_3 is negative, also including the zero. We can keep the endpoints in sync with the sign bit for free, since we have endpoints. So, setting the sign bit on a range to either yes or no, would automatically intersect it to [-INF, 0] or [0, +INF] respectively. With this in play, VRP could propagate a 0.0 if it knows the sign. For example: if (x == 0.0 && __builtin_signbit (x) == 0) bark(x); ...would generate: =========== BB 2 ============ Imports: x_3(D) Exports: x_3(D) x_3(D) [frange] float VARYING : if (x_3(D) == 0.0) goto ; [INV] else goto ; [INV] 2->3 (T) x_3(D) : [frange] float [0.0, 0.0] !NAN =========== BB 3 ============ Imports: x_3(D) Exports: _1 x_3(D) _1 : x_3(D)(I) x_3(D) [frange] float [0.0, 0.0] !NAN : _1 = __builtin_signbit (x_3(D)); if (_1 == 0) goto ; [INV] else goto ; [INV] 3->4 (T) _1 : [irange] int [0, 0] NONZERO 0x0 3->4 (T) x_3(D) : [frange] float [0.0, 0.0] !NAN !SIGN 3->5 (F) _1 : [irange] int [-INF, -1][1, +INF] 3->5 (F) x_3(D) : [frange] float [0.0, 0.0] !NAN SIGN =========== BB 4 ============ x_3(D) [frange] float [0.0, 0.0] !NAN !SIGN : bark (0.0); That is, on the 2->3 edge we know x_3 is 0.0 and !NAN, but have no information on the sign bit. Then out of BB3, we know both that x_3 is 0.0 as well as the appropriate sign. Ultimately this leads us to propagate +0.0 in BB4. I have most^Wall of it coded without regressions, with the exception of how to coerce the range-op machinery to play nice with builtins (details below). But I wanted to make sure we're all on the same page. A couple questions: Can I safely assume that +0.0 in the source (say, x = 0.0) has the sign bit cleared, and vice versa for -0.0? What's the deal with __builtin_signbit? Can I fold it to 0/1, or must I return the actual signbit, because I see differing behavior whether we fold a known value or not: abulafia:~$ cat a.c float nzero = -0.0; main(){ printf("0x%x\n", __builtin_signbit(-0.0)); printf("0x%x\n", __builtin_signbit(nzero)); } abulafia:~$ gcc a.c -w && ./a.out 0x1 0x80000000 When Andrew comes back from PTO, we'll need to talk about propagating builtins. Currently range-ops' op1_range is used to unwind back from conditionals. For example: _1 = x_9 + 5 if (_1 == 0) On the TRUE side we use op1_range to solve: 0 = x_9 + 5; We currently only handle assignments and conditionals. We would need to ability to wind back through builtins since __builtin_signbit is not part of the IL: _1 = __builtin_signbit (x_3(D)); if (_1 == 0) We have no way to augment the range for x_3 when examining the builtin. We do have a way of folding the builtin on a forward analysis, but that's a separate thing. Thoughts? Aldy On Mon, Aug 29, 2022 at 3:22 PM Jakub Jelinek wrote: > > On Mon, Aug 29, 2022 at 03:13:21PM +0200, Aldy Hernandez wrote: > > It seems to me we can do this optimization regardless, but then treat > > positive and negative zero the same throughout the frange class. > > Particularly, in frange::singleton_p(). We should never return TRUE > > for any version of 0.0. This will keep VRP from propagating an > > incorrect 0.0, since all VRP does is propagate when a range is > > provably a singleton. Also, frange::zero_p() shall return true for > > any version of 0.0. > > Well, I think for HONOR_SIGNED_ZEROS it would be nice if frange was able to > differentiate between 0.0 and -0.0. > One reason is e.g. to be able to optimize copysign/signbit - if we can > prove that the sign bit on some value will be always cleared or always set, > we can fold those. > On the other side, with -fno-signed-zeros it is invalid to use > copysign/signbit on values that could be zero (well, nothing guarantees > whether the sign bit is set or clear), so for MODE_HAS_SIGNED_ZEROS && > !HONOR_SIGNED_ZEROS it is best to treat contains_p as {-0.0,0.0} being > one thing (just not singleton_p) and not bother with details like whether > a range ends or starts with -0.0 or 0.0, either of them would work the same. > And for !MODE_HAS_SIGNED_ZEROS, obviously 0.0 can be singleton_p. > > Jakub >