public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeff Law <jeffreyalaw@gmail.com>
To: Giuseppe Tagliavini <giuseppe.tagliavini@unibo.it>,
	"gcc@gcc.gnu.org" <gcc@gcc.gnu.org>
Subject: Re: Suspecting a wrong behavior in the value range propagation analysis for __builtin_clz
Date: Wed, 1 Nov 2023 10:11:52 -0600	[thread overview]
Message-ID: <aa14b0aa-9413-4a26-bc4a-8b7eb2f398e3@gmail.com> (raw)
In-Reply-To: <DU2PR01MB832780BEBA39D7B47E837AC09EA7A@DU2PR01MB8327.eurprd01.prod.exchangelabs.com>



On 11/1/23 05:29, Giuseppe Tagliavini via Gcc wrote:
> I found an unexpected issue working with an experimental target (available here: https://github.com/EEESlab/tricore-gcc), but I was able to reproduce it on mainstream architectures. For the sake of clarity and reproducibility, I always refer to upstream code in the rest of the discussion.
> 
> Consider this simple test:
> 
> #include <stdio.h>
> int f(unsigned int a) {
>    unsigned int res = 8*sizeof(unsigned int) - __builtin_clz(a);
>    if(res>0) printf("test passed\n");
>    return res-1;
> }
> 
> I tested this code on GCC 9 and GCC 11 branches, obtaining the expected result from GCC 9 and the wrong one from GCC 11. In GCC 11 and newer versions, the condition check is removed by a gimple-level optimization (I will provide details later), and the printf is always invoked at the assembly level with no branch.
> 
> According to the GCC manual, __builtin_clz "returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined." However, it is possible to define a CLZ_DEFINED_VALUE_AT_ZERO in the architecture backend to specify a defined behavior for this case. For instance, this has been done for SPARC and AARCH64 architectures. Compiling my test with SPARC GCC 13.2.0 with the -O3 flag on CompilerExplorer I got this assembly:
> 
> .LC0:
>          .asciz  "test"
> f:
>          save    %sp, -96, %sp
>          call    __clzsi2, 0
>           mov    %i0, %o0
>          mov     %o0, %i0
>          sethi   %hi(.LC0), %o0
>          call    printf, 0
>           or     %o0, %lo(.LC0), %o0
>          mov     31, %g1
>          return  %i7+8
>           sub    %g1, %o0, %o0
> 
> After some investigation, I found this optimization derives from the results of the value range propagation analysis: https://github.com/gcc-mirror/gcc/blob/master/gcc/gimple-range-op.cc#L917
> In this code, I do not understand why CLZ_DEFINED_VALUE_AT_ZERO is verified only if the function call is tagged as internal. A gimple call is tagged as internal at creation time only when there is no associated function declaration (see https://github.com/gcc-mirror/gcc/blob/master/gcc/gimple.cc#L371), which is not the case for the builtins. From my point of view, this condition prevents the computation of the correct upper bound for this case, resulting in a wrong result from the VRP analysis.
> 
> Before considering this behavior as a bug, I prefer to ask the community to understand if there is any aspect I have missed in my reasoning.
It would help if you included the debugging dumps.

Jeff

  reply	other threads:[~2023-11-01 16:11 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-01 11:29 Giuseppe Tagliavini
2023-11-01 16:11 ` Jeff Law [this message]
2023-11-01 17:21   ` Giuseppe Tagliavini
2023-11-02  8:03 ` Richard Biener
2023-11-02 16:44   ` Joseph Myers
2023-11-02 16:52     ` Jakub Jelinek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aa14b0aa-9413-4a26-bc4a-8b7eb2f398e3@gmail.com \
    --to=jeffreyalaw@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=giuseppe.tagliavini@unibo.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).