[PATCH] D30223: [Reassociate] Convert shl by constant into multiply if it feeds a negation.

Chad Rosier via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 21 13:30:11 PST 2017


mcrosier created this revision.

See the test case for an example of what this hits.

This is an alternative to https://reviews.llvm.org/D29777.

Chad


https://reviews.llvm.org/D30223

Files:
  lib/Transforms/Scalar/Reassociate.cpp
  test/Transforms/Reassociate/basictest.ll


Index: test/Transforms/Reassociate/basictest.ll
===================================================================
--- test/Transforms/Reassociate/basictest.ll
+++ test/Transforms/Reassociate/basictest.ll
@@ -222,3 +222,18 @@
 ; CHECK-LABEL: @test15
 ; CHECK: and i1 %A, %B
 }
+
+; -(a << 1)*2 + b --> -4a + b
+define i32 @test16(i1 %cmp, i32 %a, i32 %b) {
+entry:
+  %shl = shl i32 %a, 1
+  %shl.neg = sub i32 0, %shl
+  %add1 = mul i32 %shl.neg, 2
+  %add2 = add i32 %add1, %b
+  ret i32 %add2
+
+; CHECK-LABEL: @test16
+; CHECK: %[[MUL:.*]] = mul i32 %a, -4
+; CHECK: %[[ADD:.*]] = add i32 %[[MUL]], %b
+; CHECK: ret i32 %[[ADD]]
+}
Index: lib/Transforms/Scalar/Reassociate.cpp
===================================================================
--- lib/Transforms/Scalar/Reassociate.cpp
+++ lib/Transforms/Scalar/Reassociate.cpp
@@ -1978,11 +1978,12 @@
 
   if (I->getOpcode() == Instruction::Shl && isa<ConstantInt>(I->getOperand(1)))
     // If an operand of this shift is a reassociable multiply, or if the shift
-    // is used by a reassociable multiply or add, turn into a multiply.
+    // is used by a reassociable multiply, add or negate, turn into a multiply.
     if (isReassociableOp(I->getOperand(0), Instruction::Mul) ||
         (I->hasOneUse() &&
          (isReassociableOp(I->user_back(), Instruction::Mul) ||
-          isReassociableOp(I->user_back(), Instruction::Add)))) {
+          isReassociableOp(I->user_back(), Instruction::Add) ||
+          BinaryOperator::isNeg(I->user_back())))) {
       Instruction *NI = ConvertShiftToMul(I);
       RedoInsts.insert(I);
       MadeChange = true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30223.89272.patch
Type: text/x-patch
Size: 1620 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170221/564a0900/attachment.bin>


More information about the llvm-commits mailing list