[llvm] r310186 - [InstCombine] Fold (C - X) ^ signmask -> (C + signmask - X).

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 5 13:00:44 PDT 2017


Author: ctopper
Date: Sat Aug  5 13:00:44 2017
New Revision: 310186

URL: http://llvm.org/viewvc/llvm-project?rev=310186&view=rev
Log:
[InstCombine] Fold (C - X) ^ signmask -> (C + signmask - X).

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
    llvm/trunk/test/Transforms/InstCombine/xor.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp?rev=310186&r1=310185&r2=310186&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp Sat Aug  5 13:00:44 2017
@@ -2422,11 +2422,17 @@ Instruction *InstCombiner::visitXor(Bina
   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 && RHSC->isMinusOne())
-        if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
-          Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
-          return BinaryOperator::CreateAdd(Op0I->getOperand(1),
-                                           SubOne(NegOp0I0C));
+      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)
+            Constant *C = Builder.getInt(RHSC->getValue() + Op0I0C->getValue());
+            return BinaryOperator::CreateSub(C, Op0I->getOperand(1));
+          }
         }
 
       if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
@@ -2440,7 +2446,6 @@ Instruction *InstCombiner::visitXor(Bina
             // (X + C) ^ signmask -> (X + C + signmask)
             Constant *C = Builder.getInt(RHSC->getValue() + Op0CI->getValue());
             return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
-
           }
         } else if (Op0I->getOpcode() == Instruction::Or) {
           // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0

Modified: llvm/trunk/test/Transforms/InstCombine/xor.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/xor.ll?rev=310186&r1=310185&r2=310186&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/xor.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/xor.ll Sat Aug  5 13:00:44 2017
@@ -353,6 +353,15 @@ define i32 @test28(i32 %indvar) {
   ret i32 %t214
 }
 
+define i32 @test28_sub(i32 %indvar) {
+; CHECK-LABEL: @test28_sub(
+; CHECK-NEXT:    ret i32 -2147483648
+;
+  %t7 = sub i32 -2147483647, %indvar
+  %t214 = xor i32 %t7, -2147483648
+  ret i32 %t214
+}
+
 define i32 @test29(i1 %C) {
 ; CHECK-LABEL: @test29(
 ; CHECK-NEXT:    [[V:%.*]] = select i1 [[C:%.*]], i32 915, i32 113




More information about the llvm-commits mailing list