[llvm-commits] [llvm] r78714 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/exact-sdiv.ll

Dan Gohman gohman at apple.com
Tue Aug 11 13:47:47 PDT 2009


Author: djg
Date: Tue Aug 11 15:47:47 2009
New Revision: 78714

URL: http://llvm.org/viewvc/llvm-project?rev=78714&view=rev
Log:
Optimize exact sdiv by a constant power of 2 to ashr.

Added:
    llvm/trunk/test/Transforms/InstCombine/exact-sdiv.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=78714&r1=78713&r2=78714&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Tue Aug 11 15:47:47 2009
@@ -3064,6 +3064,15 @@
     // sdiv X, -1 == -X
     if (RHS->isAllOnesValue())
       return BinaryOperator::CreateNeg(*Context, Op0);
+
+    // sdiv X, C  --> ashr X, log2(C)
+    if (cast<SDivOperator>(&I)->isExact() &&
+        RHS->getValue().isNonNegative() &&
+        RHS->getValue().isPowerOf2()) {
+      Value *ShAmt = llvm::ConstantInt::get(RHS->getType(),
+                                            RHS->getValue().exactLogBase2());
+      return BinaryOperator::CreateAShr(Op0, ShAmt, I.getName());
+    }
   }
 
   // If the sign bits of both operands are zero (i.e. we can prove they are

Added: llvm/trunk/test/Transforms/InstCombine/exact-sdiv.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/exact-sdiv.ll?rev=78714&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/exact-sdiv.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/exact-sdiv.ll Tue Aug 11 15:47:47 2009
@@ -0,0 +1,15 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | FileCheck %s
+
+; CHECK: define i32 @foo
+; CHECK: sdiv i32 %x, 8
+define i32 @foo(i32 %x) {
+  %y = sdiv i32 %x, 8
+  ret i32 %y
+}
+
+; CHECK: define i32 @bar
+; CHECK: ashr i32 %x, 3
+define i32 @bar(i32 %x) {
+  %y = sdiv exact i32 %x, 8
+  ret i32 %y
+}





More information about the llvm-commits mailing list