Add a small transform: transform -(X<<Y) to (-X<<Y) when the shift has a single
authorEli Friedman <eli.friedman@gmail.com>
Sun, 31 Jan 2010 02:30:23 +0000 (02:30 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Sun, 31 Jan 2010 02:30:23 +0000 (02:30 +0000)
use and X is free to negate.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@94941 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineAddSub.cpp
test/Transforms/InstCombine/sub.ll

index 4891ff00e7b1ff7e0e1b1bf72f280aa9ca4c3472..119b1cad632abae462134d067cb8af1179928df2 100644 (file)
@@ -676,6 +676,13 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
               return BinaryOperator::CreateSDiv(Op1I->getOperand(0),
                                           ConstantExpr::getNeg(DivRHS));
 
+      // 0 - (C << X)  -> (-C << X)
+      if (Op1I->getOpcode() == Instruction::Shl)
+        if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
+          if (CSI->isZero())
+            if (Value *ShlLHSNeg = dyn_castNegVal(Op1I->getOperand(0)))
+              return BinaryOperator::CreateShl(ShlLHSNeg, Op1I->getOperand(1));
+
       // X - X*C --> X * (1-C)
       ConstantInt *C2 = 0;
       if (dyn_castFoldableMul(Op1I, C2) == Op0) {
index fa0322a44e80d92b796ddc9652bf80b5d69d983e..29bd7be2ff84c8c471c653974db4f126a5cf4c9c 100644 (file)
@@ -272,4 +272,12 @@ define i64 @test25(i8* %P, i64 %A){
 ; CHECK-NEXT: ret i64 
 }
 
+define i32 @test26(i32 %x) {
+  %shl = shl i32 3, %x
+  %neg = sub i32 0, %shl
+  ret i32 %neg
+; CHECK: @test26
+; CHECK-NEXT: shl i32 -3
+; CHECK-NEXT: ret i32
+}