[llvm] [InstCombine] fold `select (trunc nuw X to i1), X, Y` to `select (trunc nuw X to i1), 1, Y` (PR #105914)

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 24 01:05:53 PDT 2024


================
@@ -4201,5 +4201,23 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
     }
   }
 
+  // select (trunc nuw X to i1), X, Y --> select (trunc nuw X to i1), 1, Y
+  // select (trunc nuw X to i1), Y, X --> select (trunc nuw X to i1), Y, 0
+  // select (trunc nsw X to i1), X, Y --> select (trunc nsw X to i1), -1, Y
+  // select (trunc nsw X to i1), Y, X --> select (trunc nsw X to i1), Y, 0
+  Value *Trunc;
+  if (match(CondVal, m_NUWTrunc(m_Value(Trunc)))) {
+    if (match(TrueVal, m_Specific(Trunc)))
+      return replaceOperand(SI, 1, ConstantInt::get(TrueVal->getType(), 1));
+    if (match(FalseVal, m_Specific(Trunc)))
+      return replaceOperand(SI, 2, ConstantInt::get(TrueVal->getType(), 0));
+  }
+  if (match(CondVal, m_NSWTrunc(m_Value(Trunc)))) {
+    if (match(TrueVal, m_Specific(Trunc)))
+      return replaceOperand(SI, 1, ConstantInt::get(TrueVal->getType(), -1));
----------------
c8ef wrote:

I didn't even notice that, thanks for guiding me!

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


More information about the llvm-commits mailing list