[llvm] [InstCombine] Missing optimization: fold mul (select a, b), (select b, a) to mul a, b (PR #74953)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 9 22:03:54 PST 2023


================
@@ -1132,6 +1132,13 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
   };
 
   if (LHSIsSelect && RHSIsSelect && A == D) {
+    // op(select(%v, %x, %y), select(%v, %y, %x)) --> op(%x, %y)
+    if (I.isCommutative() && B == F && C == E) {
+      Value *BI = Builder.CreateBinOp(I.getOpcode(), B, E);
+      BI->takeName(&I);
----------------
dtcxzyw wrote:

```suggestion
      if (auto *BO = dyn_cast<BinaryOperator>(BI))
        BO->copyIRFlags(&I);
      BI->takeName(&I);
```
Preserve nsw/nuw flags.
Could you please add the following test?
```
define i8 @fold_select_mul_nsw_nuw_preserve(i1 %c, i8 %a, i8 %b) {
  %s0 = select i1 %c, i8 %a, i8 %b
  %s1 = select i1 %c, i8 %b, i8 %a
  %ret = mul nsw nuw i8 %s1, %s0
  ret i8 %ret
}
```

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


More information about the llvm-commits mailing list