https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122212
Bug ID: 122212
Summary: wrong code by forwprop introducing .CLZ
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: kristerw at gcc dot gnu.org
Blocks: 118443
Target Milestone: ---
The following function is miscompiled when compiled for x86_64 with -O1 or
higher.
unsigned int
ZSTD_countLeadingZeros32_fallback(unsigned int val)
{
static const unsigned int DeBruijnClz[32]
= { 0, 9, 1, 10, 13, 21, 2, 29,
11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7,
19, 27, 23, 6, 26, 5, 4, 31};
val |= val >> 1;
val |= val >> 2;
val |= val >> 4;
val |= val >> 8;
val |= val >> 16;
return 31 - DeBruijnClz[(val * 0x07C4ACDDU) >> 27];
}
The issue is that forwprop changes this to:
unsigned int ZSTD_countLeadingZeros32_fallback (unsigned int val)
{
unsigned int _8;
unsigned int _16;
int _17;
int _18;
_Bool _19;
int _20;
<bb 2> :
_17 = .CLZ (val_9(D), 32);
_18 = 31 - _17;
_19 = val_9(D) == 0;
_20 = _19 ? 31 : _18;
_8 = (unsigned int) _20;
_16 = 31 - _8;
return _16;
}
which returns the wrong value for ZSTD_countLeadingZeros32_fallback(0).
Referenced Bugs:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118443
[Bug 118443] [Meta bug] Bugs triggered by and blocking more smtgcc testing