summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms
diff options
context:
space:
mode:
authorJustin Lebar <jlebar@google.com>2018-03-21 14:08:21 +0000
committerJustin Lebar <jlebar@google.com>2018-03-21 14:08:21 +0000
commit038cbc5c13e33052c1b7dad1112c2a062e7c565e (patch)
tree7e0dc8c207bb211bb0e63ffc1185ae1b179b0edd /llvm/lib/Transforms
parentdaf69ac2b2a87198f811eab02d650824af4e0f51 (diff)
downloadbcm5719-llvm-038cbc5c13e33052c1b7dad1112c2a062e7c565e.tar.gz
bcm5719-llvm-038cbc5c13e33052c1b7dad1112c2a062e7c565e.zip
Re-re-land: Teach CorrelatedValuePropagation to reduce the width of udiv/urem instructions.
Summary: If the operands of a udiv/urem can be proved to fit within a smaller power-of-two-sized type, reduce the width of the udiv/urem. Backed out for causing performance regressions. Re-landing because we've determined that these regressions were noise. Original Differential Revision: https://reviews.llvm.org/D44102 llvm-svn: 328096
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r--llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index 5cab6d62b08..efbe2aa8f88 100644
--- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -58,6 +58,7 @@ STATISTIC(NumCmps, "Number of comparisons propagated");
STATISTIC(NumReturns, "Number of return values propagated");
STATISTIC(NumDeadCases, "Number of switch cases removed");
STATISTIC(NumSDivs, "Number of sdiv converted to udiv");
+STATISTIC(NumUDivs, "Number of udivs whose width was decreased");
STATISTIC(NumAShrs, "Number of ashr converted to lshr");
STATISTIC(NumSRems, "Number of srem converted to urem");
STATISTIC(NumOverflows, "Number of overflow checks removed");
@@ -432,6 +433,48 @@ static bool hasPositiveOperands(BinaryOperator *SDI, LazyValueInfo *LVI) {
return true;
}
+/// Try to shrink a udiv/urem's width down to the smallest power of two that's
+/// sufficient to contain its operands.
+static bool processUDivOrURem(BinaryOperator *Instr, LazyValueInfo *LVI) {
+ assert(Instr->getOpcode() == Instruction::UDiv ||
+ Instr->getOpcode() == Instruction::URem);
+ if (Instr->getType()->isVectorTy())
+ return false;
+
+ // Find the smallest power of two bitwidth that's sufficient to hold Instr's
+ // operands.
+ auto OrigWidth = Instr->getType()->getIntegerBitWidth();
+ ConstantRange OperandRange(OrigWidth, /*isFullset=*/false);
+ for (Value *Operand : Instr->operands()) {
+ OperandRange = OperandRange.unionWith(
+ LVI->getConstantRange(Operand, Instr->getParent()));
+ }
+ // Don't shrink below 8 bits wide.
+ unsigned NewWidth = std::max<unsigned>(
+ PowerOf2Ceil(OperandRange.getUnsignedMax().getActiveBits()), 8);
+ // NewWidth might be greater than OrigWidth if OrigWidth is not a power of
+ // two.
+ if (NewWidth >= OrigWidth)
+ return false;
+
+ ++NumUDivs;
+ auto *TruncTy = Type::getIntNTy(Instr->getContext(), NewWidth);
+ auto *LHS = CastInst::Create(Instruction::Trunc, Instr->getOperand(0), TruncTy,
+ Instr->getName() + ".lhs.trunc", Instr);
+ auto *RHS = CastInst::Create(Instruction::Trunc, Instr->getOperand(1), TruncTy,
+ Instr->getName() + ".rhs.trunc", Instr);
+ auto *BO =
+ BinaryOperator::Create(Instr->getOpcode(), LHS, RHS, Instr->getName(), Instr);
+ auto *Zext = CastInst::Create(Instruction::ZExt, BO, Instr->getType(),
+ Instr->getName() + ".zext", Instr);
+ if (BO->getOpcode() == Instruction::UDiv)
+ BO->setIsExact(Instr->isExact());
+
+ Instr->replaceAllUsesWith(Zext);
+ Instr->eraseFromParent();
+ return true;
+}
+
static bool processSRem(BinaryOperator *SDI, LazyValueInfo *LVI) {
if (SDI->getType()->isVectorTy() || !hasPositiveOperands(SDI, LVI))
return false;
@@ -441,6 +484,10 @@ static bool processSRem(BinaryOperator *SDI, LazyValueInfo *LVI) {
SDI->getName(), SDI);
SDI->replaceAllUsesWith(BO);
SDI->eraseFromParent();
+
+ // Try to process our new urem.
+ processUDivOrURem(BO, LVI);
+
return true;
}
@@ -460,6 +507,9 @@ static bool processSDiv(BinaryOperator *SDI, LazyValueInfo *LVI) {
SDI->replaceAllUsesWith(BO);
SDI->eraseFromParent();
+ // Try to simplify our new udiv.
+ processUDivOrURem(BO, LVI);
+
return true;
}
@@ -595,6 +645,10 @@ static bool runImpl(Function &F, LazyValueInfo *LVI, const SimplifyQuery &SQ) {
case Instruction::SDiv:
BBChanged |= processSDiv(cast<BinaryOperator>(II), LVI);
break;
+ case Instruction::UDiv:
+ case Instruction::URem:
+ BBChanged |= processUDivOrURem(cast<BinaryOperator>(II), LVI);
+ break;
case Instruction::AShr:
BBChanged |= processAShr(cast<BinaryOperator>(II), LVI);
break;
OpenPOWER on IntegriCloud