public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/114032] New: ifcvt may introduce UB calls to __builtin_clz(0)
@ 2024-02-21 14:03 kristerw at gcc dot gnu.org
  2024-02-21 14:37 ` [Bug tree-optimization/114032] " jakub at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: kristerw at gcc dot gnu.org @ 2024-02-21 14:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114032

            Bug ID: 114032
           Summary: ifcvt may introduce UB calls to __builtin_clz(0)
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kristerw at gcc dot gnu.org
  Target Milestone: ---

The ifcvt pass may make the code more UB, which can be seen by compiling the
following function with -O3 for X86_64:


int a, b, i;
int scaleValueSaturate(int value) {
  if (value) {
    int result = __builtin_clz(value);
    if (-result <= a)
      return 0;
  }
  return b;
}
short dst;
short *src;
void scaleValuesSaturate() {
  for (; i; i++)
    dst = scaleValueSaturate(src[i]);
}


What is happening here is that the code for .LOOP_VECTORIZED (1, 2) != 0 always
calls __builtin_clz, even when value is 0:

  <bb 5> [local count: 955630224]:
  # i.5_21 = PHI <_7(9), i.5_20(24)>
  _2 = (long unsigned int) i.5_21;
  _3 = _2 * 2;
  _4 = src.2_1 + _3;
  _5 = *_4;
  value.0_11 = (unsigned int) _5;
  result_14 = __builtin_clz (value.0_11);
  _47 = (unsigned int) result_14;
  _48 = -_47;
  _15 = (int) _48;
  _23 = _5 != 0;
  _28 = _15 <= a.1_16;
  _46 = _23 & _28;
  prephitmp_31 = _46 ? 0 : _30;
  dst = prephitmp_31;
  _7 = i.5_21 + 1;
  i = _7;
  if (_7 != 0)
    goto <bb 9>; [89.00%]
  else
    goto <bb 14>; [11.00%]

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

end of thread, other threads:[~2024-02-22 11:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-21 14:03 [Bug tree-optimization/114032] New: ifcvt may introduce UB calls to __builtin_clz(0) kristerw at gcc dot gnu.org
2024-02-21 14:37 ` [Bug tree-optimization/114032] " jakub at gcc dot gnu.org
2024-02-22 10:50 ` rguenth at gcc dot gnu.org
2024-02-22 11:03 ` jakub at gcc dot gnu.org

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