[llvm] [InstCombine] fold (Binop phi(a, b) phi(b, a)) -> (Binop a, b) while Binop is commutative. (PR #75765)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 20 02:11:30 PST 2023
================
@@ -4237,3 +4240,22 @@ InstCombinerImpl::foldCommutativeIntrinsicOverSelects(IntrinsicInst &II) {
return nullptr;
}
+
+Instruction *
+InstCombinerImpl::foldCommutativeIntrinsicOverPhis(IntrinsicInst &II) {
+ assert(II.isCommutative() && "Instruction should be commutative");
+
+ PHINode *LHS = dyn_cast<PHINode>(II.getOperand(0));
+ PHINode *RHS = dyn_cast<PHINode>(II.getOperand(1));
+
+ if (!LHS || !RHS)
+ return nullptr;
+
+ if (matchSymmetricPhiNodesPair(LHS, RHS)) {
+ replaceOperand(II, 0, LHS->getIncomingValue(0));
+ replaceOperand(II, 1, LHS->getIncomingValue(1));
----------------
nikic wrote:
```suggestion
replaceOperand(II, 1, RHS->getIncomingValue(0));
```
I think with the updated implementation we should do this?
Though I think it would be safer to return something like `std::optional<std::pair<Value *, Value *>>` from `matchSymmetricPhiNodesPair()`, which avoids any implicit contract between these functions.
https://github.com/llvm/llvm-project/pull/75765
More information about the llvm-commits
mailing list