CSE optimization avoids incorrect vector duplicate matching
Fixes common subexpression elimination to prevent incorrect optimization of vector duplicates.
An optimization in GCC’s common subexpression elimination (CSE) pass, specifically cse_vec_duplicate_match, was incorrectly identifying and optimizing repeated vec_duplicate instructions. This occurred when the scalar value used in the vec_duplicate was modified between the occurrences of the vec_duplicate instructions. The fix strengthens the safety check to ensure that the scalar register is not modified between the original vec_duplicate and its potential CSE replacement, preventing incorrect code generation.
In Details
This commit corrects a flaw in the cse_vec_duplicate_match optimization within the Common Subexpression Elimination (CSE) pass. The optimization incorrectly reused a vec_duplicate instruction when the underlying scalar reg/v:SI was modified between two such instructions. The existing safety check reg_used_between_p and reg_set_between_p was insufficient. The fix enhances this check to ensure the scalar pseudo-register is not modified between the first and widest vec_duplicate insns, preventing the CSE pass from generating incorrect code where a vector is duplicated from a modified…
- Common Subexpression Elimination (CSE)
- An optimization technique that identifies and eliminates redundant computations of the same expression. If an expression has been computed before and not changed, its previous result is reused.
- GIMPLE
- GCC's internal representation for code during optimization. It's a three-address code form that simplifies analysis and transformation.
- vec_duplicate
- An instruction that duplicates a scalar value into multiple lanes of a vector register.
- SSA_NAME
- Static Single Assignment form. In SSA, every variable is assigned exactly once. This simplifies many compiler optimizations.
- Basic Block (BB)
- A sequence of instructions with a single entry point and a single exit point.