From 6177fd4fcee4d82692c47e33754ffe285c38cc69 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Tue, 8 Jul 2008 05:05:37 +0000 Subject: [PATCH] Expand SCEVUDiv of power of 2 to a lshr instruction. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@53217 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Analysis/ScalarEvolutionExpander.h | 6 +----- lib/Analysis/ScalarEvolutionExpander.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/include/llvm/Analysis/ScalarEvolutionExpander.h b/include/llvm/Analysis/ScalarEvolutionExpander.h index bd9596c7df8..cd075ef643a 100644 --- a/include/llvm/Analysis/ScalarEvolutionExpander.h +++ b/include/llvm/Analysis/ScalarEvolutionExpander.h @@ -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); diff --git a/lib/Analysis/ScalarEvolutionExpander.cpp b/lib/Analysis/ScalarEvolutionExpander.cpp index 593e27300d3..628129dc5f0 100644 --- a/lib/Analysis/ScalarEvolutionExpander.cpp +++ b/lib/Analysis/ScalarEvolutionExpander.cpp @@ -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(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();