Use SimplifyDemandedBits on div instructions.
authorBenjamin Kramer <benny.kra@googlemail.com>
Sat, 30 Apr 2011 18:16:00 +0000 (18:16 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sat, 30 Apr 2011 18:16:00 +0000 (18:16 +0000)
This folds away silly stuff like (a&255)/1000 -> 0.

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

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

index 665138748151b069953cedf3540b5a7a4e29f28b..b4f4da6fb0e74cfd0c6c098c25517b1a6443a57c 100644 (file)
@@ -320,6 +320,10 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
     }
   }
 
+  // See if we can fold away this div instruction.
+  if (SimplifyDemandedInstructionBits(I))
+    return &I;
+
   // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y
   Value *X = 0, *Z = 0;
   if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) { // (X - Z) / Y; Y = Op1
index 7ef640b4203349aa1b8a85f37e4c67002aa313ca..2e24f19dce4caf2e302e93c2777a4bbfcc02cf28 100644 (file)
@@ -111,3 +111,10 @@ define i32 @test13(i32 %x) nounwind  {
 ; CHECK-NEXT: ret i32 1
 }
 
+define i32 @test14(i8 %x) nounwind {
+       %zext = zext i8 %x to i32
+       %div = udiv i32 %zext, 257      ; 0
+       ret i32 %div
+; CHECK: @test14
+; CHECK-NEXT: ret i32 0
+}