[llvm] r222625 - InstCombine: Propagate exact for (sdiv X, Pow2) -> (udiv X, Pow2)

David Majnemer david.majnemer at gmail.com
Sat Nov 22 12:00:41 PST 2014


Author: majnemer
Date: Sat Nov 22 14:00:41 2014
New Revision: 222625

URL: http://llvm.org/viewvc/llvm-project?rev=222625&view=rev
Log:
InstCombine: Propagate exact for (sdiv X, Pow2) -> (udiv X, Pow2)

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
    llvm/trunk/test/Transforms/InstCombine/div.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp?rev=222625&r1=222624&r2=222625&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Sat Nov 22 14:00:41 2014
@@ -1102,12 +1102,14 @@ Instruction *InstCombiner::visitSDiv(Bin
         return BO;
       }
 
-      if (match(Op1, m_Shl(m_Power2(), m_Value()))) {
+      if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/true, 0, AT, &I, DT)) {
         // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
         // Safe because the only negative value (1 << Y) can take on is
         // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
         // the sign bit set.
-        return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
+        auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
+        BO->setIsExact(I.isExact());
+        return BO;
       }
     }
   }

Modified: llvm/trunk/test/Transforms/InstCombine/div.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/div.ll?rev=222625&r1=222624&r2=222625&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/div.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/div.ll Sat Nov 22 14:00:41 2014
@@ -314,3 +314,14 @@ define i32 @test35(i32 %A) {
 ; CHECK-NEXT: %[[udiv:.*]] = udiv exact i32 %[[and]], 2147483647
 ; CHECK-NEXT: ret i32 %[[udiv]]
 }
+
+define i32 @test36(i32 %A) {
+  %and = and i32 %A, 2147483647
+  %shl = shl nsw i32 1, %A
+  %mul = sdiv exact i32 %and, %shl
+  ret i32 %mul
+; CHECK-LABEL: @test36(
+; CHECK-NEXT: %[[and:.*]] = and i32 %A, 2147483647
+; CHECK-NEXT: %[[shr:.*]] = lshr exact i32 %[[and]], %A
+; CHECK-NEXT: ret i32 %[[shr]]
+}





More information about the llvm-commits mailing list