simplify-rtx: Widen memory attributes in "c ? a : a" fold
Fixes a bug where folding conditional expressions incorrectly narrowed memory alias information, potentially leading to dead code elimination.
A bug in GCC’s simplify-rtx pass allowed the folding of ternary operations like ‘c ? a : a’ to incorrectly narrow memory attributes. This happened because rtx_equal_p ignored memory attributes, leading to a single MEM reference being used even when the two conditional arms had incompatible alias sets. This could result in subsequent passes incorrectly removing seemingly dead store operations, leading to stale return values. The fix ensures the folded expression inherits only the memory attributes guaranteed by both operands, merging alias sets and other attributes appropriately.
In Details
The simplify_ternary_operation pass incorrectly folds IF_THEN_ELSE(cond, a, a) to a using rtx_equal_p, which bypasses memory attribute checks. This led to the loss of alias set information when a represented memory loads with different attributes. A specific test case involving ce1 and subsequent dead code elimination exposed this, causing stale values to be returned. The fix modifies the fold to merge memory attributes using logic similar to merge_memattrs, ensuring only common attributes (like alias set 0, minimal alignment, and shared MEM_EXPR) are retained. Flags like `MEM…
- simplify-rtx
- A GCC compiler pass that simplifies and optimizes Runtime Text Exchange (RTX) expressions, which represent the compiler's internal intermediate representation.
- RTX
- Runtime Text Exchange is GCC's internal representation of code. It's a set of C data structures used to represent programs during compilation, allowing various optimization passes to operate on the code.
- fold
- In compiler optimization, 'folding' refers to replacing an expression with a simpler, equivalent one. For example, folding a constant expression like '2 + 3' into '5'.
- memory attributes
- Metadata associated with memory accesses that provides information about aliasing, trapping behavior, and read-only status. These attributes help the compiler make correct optimization decisions.
- alias set
- A grouping of memory accesses that are known not to refer to the same memory location. Accesses within the same alias set might alias, while accesses in different sets are guaranteed not to.
- MEM_EXPR
- An RTX expression type representing a memory reference, often used in optimizations where the specific memory location or its properties are important.
- dead code elimination
- A compiler optimization that removes code that does not affect the program's output or behavior.