[llvm] [InstCombine] Allow overflowing selects to work on commutative arguments (PR #90812)

via llvm-commits llvm-commits at lists.llvm.org
Sun May 5 19:52:58 PDT 2024


================
@@ -2075,12 +2075,28 @@ foldOverflowingAddSubSelect(SelectInst &SI, InstCombiner::BuilderTy &Builder) {
   Value *FalseVal = SI.getFalseValue();
 
   WithOverflowInst *II;
-  if (!match(CondVal, m_ExtractValue<1>(m_WithOverflowInst(II))) ||
-      !match(FalseVal, m_ExtractValue<0>(m_Specific(II))))
+  if (!match(CondVal, m_ExtractValue<1>(m_WithOverflowInst(II))))
     return nullptr;
 
   Value *X = II->getLHS();
   Value *Y = II->getRHS();
+  if (!match(FalseVal, m_ExtractValue<0>(m_Specific(II)))) {
+    // Commutative adds can get missed, so check for X and Y being swapped
+    switch (II->getIntrinsicID()) {
+    case Intrinsic::uadd_with_overflow:
+    case Intrinsic::sadd_with_overflow:
+      if (!match(FalseVal, m_c_Add(m_Specific(X), m_Specific(Y))))
+        return nullptr;
+      break;
+    case Intrinsic::ssub_with_overflow:
+    case Intrinsic::usub_with_overflow:
+      if (!match(FalseVal, m_Sub(m_Specific(X), m_Specific(Y))))
----------------
AtariDreams wrote:

> Is this tested? I don't see any `sub` instructions in your new tests

sub already works as intended. 

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


More information about the llvm-commits mailing list