Vector conditional expression folding corrected to avoid miscompilation.
Compiler prevents scalar operations from being sunk into VEC_COND_EXPR arms, fixing miscompilation.
This commit corrects an issue where the fold_binary_op_with_conditional_arg function would sink scalar-result operations into the arms of a VEC_COND_EXPR. This happened even for whole-vector comparisons used in early-exit tests. Folding each arm independently with constant values could lead to incorrect constant results for the comparison. The fix ensures that the function now punts when a vector operand has a scalar result within a VEC_COND_EXPR, preventing miscompilation, particularly on RISC-V architectures with vector extensions.
In Details
The function fold_binary_op_with_conditional_arg in fold-const.cc previously handled VEC_COND_EXPR by recursively calling itself on the conditional arms, even if the operation being folded had a scalar result. This is problematic when the operation is a whole-vector comparison that's part of an early-break exit test. If the arms of the VEC_COND_EXPR are constant, the comparison could fold to an incorrect constant value. The fix modifies the function to detect when a vector operand of the conditional expression has a scalar result and punt (i.e., not perform the folding) in such cases,…
- VEC_COND_EXPR
- A GCC intermediate representation construct representing a conditional expression operating on vector types. It's akin to a ternary operator (?:) for vectors.
- sink operation
- In compiler optimization, moving an operation to an earlier or more nested part of the program's control flow graph.
- early-break exit test
- A test condition in a loop or operation that, if met, causes the loop or operation to terminate immediately without processing all elements.
- RISC-V
- An open-standard instruction set architecture (ISA) based on reduced instruction set computer (RISC) principles. It is designed to be simple, modular, and extensible.
- Vector extensions
- Hardware features on some architectures that allow a single instruction to operate on multiple data elements simultaneously (SIMD). RISC-V has extensions like VLEN for vector processing.