Group fold-mem-offsets changes by shared fold insns
Corrects fold-mem-offsets to group related changes by shared instructions, preventing incorrect optimizations.
This change addresses a bug in the fold-mem-offsets pass where related memory offset computations were not correctly grouped. Previously, changes were grouped based on the final register defined by a fold instruction, which could lead to incorrect zeroing of shared base offsets if different chains ended in different registers. The fix now groups changes by the connected components of roots sharing a fold instruction, ensuring that if any part of a component cannot be updated, the entire group is rolled back, preventing incorrect optimizations and fixing PR126412.
In Details
The fold-mem-offsets pass in fold-mem-offsets.cc previously grouped changes based on the register defined by the last fold insn in a def-chain. This commit refactors this grouping mechanism to use a fold_groups class which identifies connected components of roots that share a fold insn. This is managed using a union-find data structure. If any root within a component cannot be updated (e.g., due to offset limitations like LDRSB range), its entire component is cancelled. Furthermore, all surviving groups are committed in a single change_insns call, eliminating a previous sort over gr…
- fold-mem-offsets
- A GCC optimization pass that folds constant values into memory offset calculations.
- insn
- Abbreviation for instruction, typically referring to machine instructions or RTL (Register Transfer Language) in GCC's internal representation.
- def-chain
- A sequence of definitions where the output of one operation becomes the input of the next.
- RTL
- Register Transfer Language, an intermediate representation used in GCC that describes operations on registers and memory.
- union-find
- A data structure that keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. It is efficient for grouping related items.
- PR126412
- Problem Report 126412, indicating a bug in the GCC compiler that this commit aims to fix.