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%]