[llvm] [InstCombine] Simplify `(X == Y) ? (X << 1) : (X + Y)` into `(X + Y)` (PR #76738)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 2 11:17:33 PST 2024


dtcxzyw wrote:

I don't think it is a good approach to address this issue.

https://github.com/llvm/llvm-project/blob/3af59cfe0b5a319c165e3c74300aacdd42827c2d/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp#L3510-L3515
You should teach `foldSelectOpOp` to handle cases that TI and FI don't have the same opcode. In this case, you can treat `X << 1` as `X + X` because another op is an add.

You can add a match helper for `foldSelectOpOp` like `getBinOpsForFactorization`:
https://github.com/llvm/llvm-project/blob/3af59cfe0b5a319c165e3c74300aacdd42827c2d/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp#L606-L634

After folding this pattern into `X + (X == Y ? X : Y)`, `InstCombine` will continue to simplify it into `X + Y`.
godbolt: https://godbolt.org/z/nGbxvhPr1


https://github.com/llvm/llvm-project/pull/76738


More information about the llvm-commits mailing list