Expand SCEVUDiv of power of 2 to a lshr instruction.
authorNick Lewycky <nicholas@mxc.ca>
Tue, 8 Jul 2008 05:05:37 +0000 (05:05 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Tue, 8 Jul 2008 05:05:37 +0000 (05:05 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@53217 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/ScalarEvolutionExpander.h
lib/Analysis/ScalarEvolutionExpander.cpp

index bd9596c7df87f7f2c85a23408f5f9a50e45b2ecc..cd075ef643a26d7bcb8b1c5d348c3255faa95924 100644 (file)
@@ -102,11 +102,7 @@ namespace llvm {
 
     Value *visitMulExpr(SCEVMulExpr *S);
 
-    Value *visitUDivExpr(SCEVUDivExpr *S) {
-      Value *LHS = expand(S->getLHS());
-      Value *RHS = expand(S->getRHS());
-      return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt);
-    }
+    Value *visitUDivExpr(SCEVUDivExpr *S);
 
     Value *visitAddRecExpr(SCEVAddRecExpr *S);
 
index 593e27300d31bcd1377a15ef72c495374ef2129e..628129dc5f05c411b3b879ee124386bbfaf6882f 100644 (file)
@@ -129,6 +129,20 @@ Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) {
   return V;
 }
 
+Value *SCEVExpander::visitUDivExpr(SCEVUDivExpr *S) {
+  Value *LHS = expand(S->getLHS());
+  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) {
+    const APInt &RHS = SC->getValue()->getValue();
+    if (RHS.isPowerOf2())
+      return InsertBinop(Instruction::LShr, LHS,
+                         ConstantInt::get(S->getType(), RHS.logBase2()),
+                         InsertPt);
+  }
+
+  Value *RHS = expand(S->getRHS());
+  return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt);
+}
+
 Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
   const Type *Ty = S->getType();
   const Loop *L = S->getLoop();