public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Help with clz(0) and optimization
@ 2023-10-31 14:26 Enrico
  2023-10-31 15:22 ` Jeff Law
  0 siblings, 1 reply; 2+ messages in thread
From: Enrico @ 2023-10-31 14:26 UTC (permalink / raw)
  To: gcc

[-- Attachment #1: Type: text/plain, Size: 1239 bytes --]

I am working on GCC for a target architecture where clz(0) is defined and
is 32 (TriCore).
The code under test is the following:

#include <stdio.h>
int f(unsigned int a) {
    unsigned int res = 32 - __builtin_clz(a);
    if(res > 0) printf("test");
    return res;
}

From GCC version greater and equal to 11, the Tree SSA optimization
discards the if condition completely, and always executes the printf. This
is the optimized gimplified code at the end of the optimization for GCC12:

int f (unsigned int a)
{
  int _1;
  int _2;

  <bb 2> [local count: 1073741824]:
  _1 = __builtin_clz (a_4(D));
  _2 = 32 - _1;
  __printf_chk (1, "test");
  return _2;
}

where GCC10 produces:
f (unsigned int a)
{
  int _1;
  int _2;

  <bb 2> [local count: 1073741824]:
  _1 = __builtin_clz (a_4(D));
  _2 = 32 - _1;
  if (_2 != 0)
    goto <bb 3>; [33.00%]
  else
    goto <bb 4>; [67.00%]

  <bb 3> [local count: 354334800]:
  __printf_chk (1, "test");

  <bb 4> [local count: 1073741824]:
  return _2;
}

I tried to rebuild defining  CLZ_DEFINED_VALUE_AT_ZERO(MODE, VALUE)
((VALUE) = 32, 2) but this doesn't seem to have any effect.

I am stuck and need counseling. Can anyone suggest me the next steps? Thank
you.
Kind regards
Enrico Bragante

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Help with clz(0) and optimization
  2023-10-31 14:26 Help with clz(0) and optimization Enrico
@ 2023-10-31 15:22 ` Jeff Law
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff Law @ 2023-10-31 15:22 UTC (permalink / raw)
  To: Enrico, gcc



On 10/31/23 08:26, Enrico via Gcc wrote:
> I am working on GCC for a target architecture where clz(0) is defined and
> is 32 (TriCore).
> The code under test is the following:
> 
> #include <stdio.h>
> int f(unsigned int a) {
>      unsigned int res = 32 - __builtin_clz(a);
>      if(res > 0) printf("test");
>      return res;
> }
> 
>  From GCC version greater and equal to 11, the Tree SSA optimization
> discards the if condition completely, and always executes the printf. This
> is the optimized gimplified code at the end of the optimization for GCC12:
> 
> int f (unsigned int a)
> {
>    int _1;
>    int _2;
> 
>    <bb 2> [local count: 1073741824]:
>    _1 = __builtin_clz (a_4(D));
>    _2 = 32 - _1;
>    __printf_chk (1, "test");
>    return _2;
> }
> 
> where GCC10 produces:
> f (unsigned int a)
> {
>    int _1;
>    int _2;
> 
>    <bb 2> [local count: 1073741824]:
>    _1 = __builtin_clz (a_4(D));
>    _2 = 32 - _1;
>    if (_2 != 0)
>      goto <bb 3>; [33.00%]
>    else
>      goto <bb 4>; [67.00%]
> 
>    <bb 3> [local count: 354334800]:
>    __printf_chk (1, "test");
> 
>    <bb 4> [local count: 1073741824]:
>    return _2;
> }
> 
> I tried to rebuild defining  CLZ_DEFINED_VALUE_AT_ZERO(MODE, VALUE)
> ((VALUE) = 32, 2) but this doesn't seem to have any effect.
I would look to see the first pass where the condition gets removed 
using -fdump-tree-all-details.  That should give you strong hints about 
what to look at next.

Conceptually something has determined that _2 never has the value 0 
which through backwards propagation would indicate that __builtin_clz 
never returns the value 32.

Jeff


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-10-31 15:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-31 14:26 Help with clz(0) and optimization Enrico
2023-10-31 15:22 ` Jeff Law

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).