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

Reply via email to