[InstCombine] Support ~(c-X) --> X+(-c-1) and ~(X-c) --> (-c-1)-X for splat vectors.

llvm-svn: 310195
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 7b32812..e684f58 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2419,17 +2419,33 @@
     return replaceInstUsesWith(I, Op0);
   }
 
+  {
+    const APInt *RHSC;
+    if (match(Op1, m_APInt(RHSC))) {
+      Value *V;
+      const APInt *C;
+      if (match(Op0, m_Sub(m_APInt(C), m_Value(V)))) {
+        // ~(c-X) == X-c-1 == X+(-c-1)
+        if (RHSC->isAllOnesValue()) {
+          Constant *NewC = ConstantInt::get(I.getType(), -(*C) - 1);
+          return BinaryOperator::CreateAdd(V, NewC);
+        }
+      } else if (match(Op0, m_Add(m_Value(V), m_APInt(C)))) {
+        // ~(X-c) --> (-c-1)-X
+        if (RHSC->isAllOnesValue()) {
+          Constant *NewC = ConstantInt::get(I.getType(), -(*C) - 1);
+          return BinaryOperator::CreateSub(NewC, V);
+        }
+      }
+    }
+  }
+
   if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) {
     if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
-      // ~(c-X) == X-c-1 == X+(-c-1)
       if (Op0I->getOpcode() == Instruction::Sub)
         if (ConstantInt *Op0I0C = dyn_cast<ConstantInt>(Op0I->getOperand(0))) {
-          if (RHSC->isMinusOne()) {
-            Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
-            return BinaryOperator::CreateAdd(Op0I->getOperand(1),
-                                             SubOne(NegOp0I0C));
-          } else if (RHSC->getValue().isSignMask()) {
-            // (C - X) ^ signmask -> (C + signmask - X)
+          // (C - X) ^ signmask -> (C + signmask - X)
+          if (RHSC->getValue().isSignMask()) {
             Constant *C = Builder.getInt(RHSC->getValue() + Op0I0C->getValue());
             return BinaryOperator::CreateSub(C, Op0I->getOperand(1));
           }
@@ -2437,13 +2453,8 @@
 
       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
         if (Op0I->getOpcode() == Instruction::Add) {
-          // ~(X-c) --> (-c-1)-X
-          if (RHSC->isMinusOne()) {
-            Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
-            return BinaryOperator::CreateSub(SubOne(NegOp0CI),
-                                             Op0I->getOperand(0));
-          } else if (RHSC->getValue().isSignMask()) {
-            // (X + C) ^ signmask -> (X + C + signmask)
+          // (X + C) ^ signmask -> (X + C + signmask)
+          if (RHSC->getValue().isSignMask()) {
             Constant *C = Builder.getInt(RHSC->getValue() + Op0CI->getValue());
             return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
           }