Reduce x - y to -y when we know the 'x' part will get masked off anyways.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@53271 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp
index f8ad98c..675f7ec 100644
--- a/lib/Transforms/Scalar/InstructionCombining.cpp
+++ b/lib/Transforms/Scalar/InstructionCombining.cpp
@@ -3461,6 +3461,17 @@
         // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
           return BinaryOperator::CreateAnd(V, AndRHS);
+
+        // (A - N) & AndRHS -> -N & AndRHS where A & AndRHS == 0
+        if (Op0I->hasOneUse() && MaskedValueIsZero(Op0LHS, AndRHSMask)) {
+          ConstantInt *A = dyn_cast<ConstantInt>(Op0LHS);
+          if (!A || !A->isZero()) {
+            Instruction *NewNeg = BinaryOperator::CreateNeg(Op0RHS);
+            InsertNewInstBefore(NewNeg, I);
+            return BinaryOperator::CreateAnd(NewNeg, AndRHS);
+          }
+        }
+
         break;
       }
 
@@ -3780,7 +3791,7 @@
           }
     }
   }
-      
+
   return Changed ? &I : 0;
 }