diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -411,7 +411,8 @@
   //   or i1 Op, SI  / select i1 Op, i1 true, i1 SI  (if IsAnd = false)
   // into simplier select instruction using isImpliedCondition.
   Instruction *foldAndOrOfSelectUsingImpliedCond(Value *Op, SelectInst &SI,
-                                                 bool IsAnd);
+                                                 bool IsAnd,
+                                                 bool NotSICond = false);
 
   Instruction *hoistFNegAboveFMulFDiv(Value *FNegOp, Instruction &FMFSource);
 
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -2658,9 +2658,8 @@
   return nullptr;
 }
 
-Instruction *InstCombinerImpl::foldAndOrOfSelectUsingImpliedCond(Value *Op,
-                                                                 SelectInst &SI,
-                                                                 bool IsAnd) {
+Instruction *InstCombinerImpl::foldAndOrOfSelectUsingImpliedCond(
+    Value *Op, SelectInst &SI, bool IsAnd, bool NotSICond) {
   Value *CondVal = SI.getCondition();
   Value *A = SI.getTrueValue();
   Value *B = SI.getFalseValue();
@@ -2672,6 +2671,9 @@
   if (!Res)
     return nullptr;
 
+  if (NotSICond)
+    *Res = !*Res;
+
   Value *Zero = Constant::getNullValue(A->getType());
   Value *One = Constant::getAllOnesValue(A->getType());