[clang] [llvm] [InstCombine] Reassociate nested add/sub with constant (PR #191022)
Max Graey via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 9 13:14:58 PDT 2026
================
@@ -1523,6 +1523,34 @@ static Instruction *foldBoxMultiply(BinaryOperator &I) {
return nullptr;
}
+/// Canonicalize a nested add/sub with a constant on the inner RHS by
+/// sinking the constant to the outer RHS.
+/// (X +/- C) +/- Y -> (X +/- Y) +/- C
----------------
MaxGraey wrote:
Yeah, but as far as I know, Reassociate pass only works when the inner and outer associative operations are the same. To avoid this conflict, we can stop this reassociating in this case. It’s actually strange why this is done, since simple binary canonization in LLVM (and not only here) is designed so that a constant is always on the right-hand side; therefore, to simplify the constant folding and reduce the number of isomorphic forms for matching, it makes sense to push constants from the nested expression nodes in the same direction if this possible.
https://github.com/llvm/llvm-project/pull/191022
More information about the cfe-commits
mailing list