[llvm-commits] [llvm] r101000 - in /llvm/trunk: lib/Transforms/Scalar/IndVarSimplify.cpp test/Transforms/IndVarSimplify/eliminate-comparison.ll
Dan Gohman
gohman at apple.com
Sun Apr 11 16:10:12 PDT 2010
Author: djg
Date: Sun Apr 11 18:10:12 2010
New Revision: 101000
URL: http://llvm.org/viewvc/llvm-project?rev=101000&view=rev
Log:
Teach IndVarSimplify how to eliminate comparisons involving induction
variables. For example, with code like this:
for (i=0;i<n;++i)
if (i<n)
x[i] = 0;
IndVarSimplify will now recognize that i is always less than n inside
the loop, and eliminate the if.
Added:
llvm/trunk/test/Transforms/IndVarSimplify/eliminate-comparison.ll
Modified:
llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp
Modified: llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp?rev=101000&r1=100999&r2=101000&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp Sun Apr 11 18:10:12 2010
@@ -97,6 +97,7 @@
private:
+ void EliminateIVComparisons();
void RewriteNonIntegerIVs(Loop *L);
ICmpInst *LinearFunctionTestReplace(Loop *L, const SCEV *BackedgeTakenCount,
@@ -336,6 +337,40 @@
SE->forgetLoop(L);
}
+void IndVarSimplify::EliminateIVComparisons() {
+ // Look for ICmp users.
+ for (IVUsers::iterator I = IU->begin(), E = IU->end(); I != E;) {
+ IVStrideUse &UI = *I++;
+ ICmpInst *ICmp = dyn_cast<ICmpInst>(UI.getUser());
+ if (!ICmp) continue;
+
+ bool Swapped = UI.getOperandValToReplace() == ICmp->getOperand(1);
+ ICmpInst::Predicate Pred = ICmp->getPredicate();
+ if (Swapped) Pred = ICmpInst::getSwappedPredicate(Pred);
+
+ // Get the SCEVs for the ICmp operands.
+ const SCEV *S = IU->getReplacementExpr(UI);
+ const SCEV *X = SE->getSCEV(ICmp->getOperand(!Swapped));
+
+ // Simplify unnecessary loops away.
+ const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
+ S = SE->getSCEVAtScope(S, ICmpLoop);
+ X = SE->getSCEVAtScope(X, ICmpLoop);
+
+ // If the condition is always true or always false, replace it with
+ // a constant value.
+ if (SE->isKnownPredicate(Pred, S, X))
+ ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext()));
+ else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X))
+ ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext()));
+ else
+ continue;
+
+ DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
+ ICmp->eraseFromParent();
+ }
+}
+
bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
IU = &getAnalysis<IVUsers>();
LI = &getAnalysis<LoopInfo>();
@@ -362,6 +397,9 @@
if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount))
RewriteLoopExitValues(L, Rewriter);
+ // Simplify ICmp IV users.
+ EliminateIVComparisons();
+
// Compute the type of the largest recurrence expression, and decide whether
// a canonical induction variable should be inserted.
const Type *LargestType = 0;
Added: llvm/trunk/test/Transforms/IndVarSimplify/eliminate-comparison.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/IndVarSimplify/eliminate-comparison.ll?rev=101000&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/IndVarSimplify/eliminate-comparison.ll (added)
+++ llvm/trunk/test/Transforms/IndVarSimplify/eliminate-comparison.ll Sun Apr 11 18:10:12 2010
@@ -0,0 +1,40 @@
+; RUN: opt -indvars -S < %s | FileCheck %s
+
+; Indvars should be able to simplify simple comparisons involving
+; induction variables.
+
+; CHECK: %cond = and i1 %tobool.not, true
+
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
+
+ at X = external global [0 x double]
+
+define void @foo(i64 %n, i32* nocapture %p) nounwind {
+entry:
+ %cmp9 = icmp sgt i64 %n, 0
+ br i1 %cmp9, label %pre, label %return
+
+pre:
+ %t3 = load i32* %p
+ %tobool.not = icmp ne i32 %t3, 0
+ br label %loop
+
+loop:
+ %i = phi i64 [ 0, %pre ], [ %inc, %for.inc ]
+ %cmp6 = icmp slt i64 %i, %n
+ %cond = and i1 %tobool.not, %cmp6
+ br i1 %cond, label %if.then, label %for.inc
+
+if.then:
+ %arrayidx = getelementptr [0 x double]* @X, i64 0, i64 %i
+ store double 3.200000e+00, double* %arrayidx
+ br label %for.inc
+
+for.inc:
+ %inc = add nsw i64 %i, 1
+ %exitcond = icmp sge i64 %inc, %n
+ br i1 %exitcond, label %return, label %loop
+
+return:
+ ret void
+}
More information about the llvm-commits
mailing list