Fix bitint lowerings for CLZ/CTZ with zero argument to avoid loop verification ICE.
Corrects handling of zero-argument CLZ/CTZ builtins within loops to prevent compiler crashes by properly placing unreachable code.
This commit addresses an issue in GCC’s bitintlower pass where the lowering of one-argument __builtin_clz and __builtin_ctz for a zero input could cause a loop verification ICE (Internal Compiler Error). The problem arose because the basic block containing __builtin_unreachable() inherited the loop father of its surrounding code, leading to incorrect loop analysis. The fix moves this unreachable block to the artificial loop 0, resolving the ICE and ensuring correct loop behavior.
In Details
The gimple-lower-bitint.cc's bitint_large_huge::lower_bit_query function incorrectly handled the __builtin_unreachable() call when lowering one-argument IFN_CLZ or IFN_CTZ for a zero input. This unreachable block inherited the loop_father of the surrounding loop, causing loop verification to fail (ICE) in certain loop contexts. The fix explicitly assigns this block to get_loop(cfun, 0), classifying it as part of the artificial loop 0, which is appropriate for a noreturn block.
- CLZ/CTZ
- Count Leading Zeros (CLZ) and Count Trailing Zeros (CTZ) are bitwise operations that count the number of zero bits from the most significant or least significant bit, respectively, until the first set bit is encountered. These are often implemented as built-in functions.
- bitintlower
- A GCC pass responsible for lowering or transforming arbitrary-precision integer (bit-int) types into lower-level representations.
- ICE
- Internal Compiler Error. A crash within the compiler during translation, usually indicating a bug in the compiler's logic.
- loop_father
- In GCC's control flow graph analysis, this refers to the parent loop in a nested loop structure. Correctly identifying loop hierarchies is crucial for many optimizations.
- __builtin_unreachable
- A GCC built-in function that informs the compiler that the code path following this point will never be executed. This can enable further optimizations.