Transform any logical shift of a power of two into an exact/NUW shift when
authorChris Lattner <sabre@nondot.org>
Mon, 23 May 2011 00:21:50 +0000 (00:21 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 23 May 2011 00:21:50 +0000 (00:21 +0000)
in a known-non-zero context.

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

lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
test/Transforms/InstCombine/shift.ll

index fdec6407b800a2893bcb481f9d3e571fbdb67f12..f3d10611ad2a709d2d30e65701fe1b770250e8e3 100644 (file)
@@ -29,6 +29,23 @@ static Value *simplifyValueKnownNonZero(Value *V, InstCombiner &IC) {
   // code.
   if (!V->hasOneUse()) return 0;
   
+  
+  // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it
+  // inexact.  Similarly for <<.
+  if (BinaryOperator *I = dyn_cast<BinaryOperator>(V))
+    if (I->isLogicalShift() &&
+        isPowerOfTwo(I->getOperand(0), IC.getTargetData())) {
+      if (I->getOpcode() == Instruction::LShr && !I->isExact()) {
+        I->setIsExact();
+        return I;
+      }
+      
+      if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) {
+        I->setHasNoUnsignedWrap();
+        return I;
+      }
+    }
+      
   // ((1 << A) >>u B) --> (1 << (A-B))
   // Because V cannot be zero, we know that B is less than A.
   Value *A = 0, *B = 0, *PowerOf2 = 0;
index bded68ac4755840cba6e238ce36bb0b24f2ba6c7..d9ac9cbfe9ee0401bf56c441102b57c7e368f82f 100644 (file)
@@ -506,3 +506,24 @@ define i32 @test41(i32 %a, i32 %b) nounwind {
 ; CHECK-NEXT: shl i32 8, %b
 ; CHECK-NEXT: ret i32
 }
+
+define i32 @test42(i32 %a, i32 %b) nounwind {
+  %div = lshr i32 4096, %b    ; must be exact otherwise we'd divide by zero
+  %div2 = udiv i32 %a, %div
+  ret i32 %div2
+; CHECK: @test42
+; CHECK-NEXT: lshr exact i32 4096, %b
+}
+
+define i32 @test43(i32 %a, i32 %b) nounwind {
+  %div = shl i32 4096, %b    ; must be exact otherwise we'd divide by zero
+  %div2 = udiv i32 %a, %div
+  ret i32 %div2
+; CHECK: @test43
+; CHECK-NEXT: add i32 %b, 12
+; CHECK-NEXT: lshr
+; CHECK-NEXT: ret
+}
+
+
+