Value *IndVar,
BasicBlock *ExitingBlock,
BranchInst *BI,
- SCEVExpander &Rewriter);
+ SCEVExpander &Rewriter,
+ bool SignExtendTripCount);
void RewriteLoopExitValues(Loop *L, SCEV *IterationCount);
void DeleteTriviallyDeadInstructions(SmallPtrSet<Instruction*, 16> &Insts);
Value *IndVar,
BasicBlock *ExitingBlock,
BranchInst *BI,
- SCEVExpander &Rewriter) {
+ SCEVExpander &Rewriter,
+ bool SignExtendTripCount) {
// If the exiting block is not the same as the backedge block, we must compare
// against the preincremented value, otherwise we prefer to compare against
// the post-incremented value.
if ((isa<SCEVConstant>(N) && !N->isZero()) ||
SE->isLoopGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) {
// No overflow. Cast the sum.
- IterationCount = SE->getTruncateOrZeroExtend(N, IndVar->getType());
+ if (SignExtendTripCount)
+ IterationCount = SE->getTruncateOrSignExtend(N, IndVar->getType());
+ else
+ IterationCount = SE->getTruncateOrZeroExtend(N, IndVar->getType());
} else {
// Potential overflow. Cast before doing the add.
- IterationCount = SE->getTruncateOrZeroExtend(IterationCount,
- IndVar->getType());
+ if (SignExtendTripCount)
+ IterationCount = SE->getTruncateOrSignExtend(IterationCount,
+ IndVar->getType());
+ else
+ IterationCount = SE->getTruncateOrZeroExtend(IterationCount,
+ IndVar->getType());
IterationCount =
SE->getAddExpr(IterationCount,
SE->getIntegerSCEV(1, IndVar->getType()));
CmpIndVar = L->getCanonicalInductionVariableIncrement();
} else {
// We have to use the preincremented value...
- IterationCount = SE->getTruncateOrZeroExtend(IterationCount,
- IndVar->getType());
+ if (SignExtendTripCount)
+ IterationCount = SE->getTruncateOrSignExtend(IterationCount,
+ IndVar->getType());
+ else
+ IterationCount = SE->getTruncateOrZeroExtend(IterationCount,
+ IndVar->getType());
CmpIndVar = IndVar;
}
/// TestOrigIVForWrap - Analyze the original induction variable
/// that controls the loop's iteration to determine whether it
-/// would ever undergo signed or unsigned overflow.
+/// would ever undergo signed or unsigned overflow. Also, check
+/// whether an induction variable in the same type that starts
+/// at 0 would undergo signed overflow.
///
-/// In addition to setting the NoSignedWrap and NoUnsignedWrap
-/// variables, return the PHI for this induction variable.
+/// In addition to setting the NoSignedWrap, NoUnsignedWrap, and
+/// SignExtendTripCount variables, return the PHI for this induction
+/// variable.
///
/// TODO: This duplicates a fair amount of ScalarEvolution logic.
/// Perhaps this can be merged with ScalarEvolution::getIterationCount
const BranchInst *BI,
const Instruction *OrigCond,
bool &NoSignedWrap,
- bool &NoUnsignedWrap) {
+ bool &NoUnsignedWrap,
+ bool &SignExtendTripCount) {
// Verify that the loop is sane and find the exit condition.
const ICmpInst *Cmp = dyn_cast<ICmpInst>(OrigCond);
if (!Cmp) return 0;
// The original induction variable will start at some non-max value,
// it counts up by one, and the loop iterates only while it remans
// less than some value in the same type. As such, it will never wrap.
- if (isSigned && !InitialVal->getValue().isMaxSignedValue())
+ if (isSigned && !InitialVal->getValue().isMaxSignedValue()) {
NoSignedWrap = true;
- else if (!isSigned && !InitialVal->getValue().isMaxValue())
+ // If the original induction variable starts at zero or greater,
+ // the trip count can be considered signed.
+ if (InitialVal->getValue().isNonNegative())
+ SignExtendTripCount = true;
+ } else if (!isSigned && !InitialVal->getValue().isMaxValue())
NoUnsignedWrap = true;
return PN;
}
// using it. We can currently only handle loops with a single exit.
bool NoSignedWrap = false;
bool NoUnsignedWrap = false;
+ bool SignExtendTripCount = false;
const PHINode *OrigControllingPHI = 0;
if (!isa<SCEVCouldNotCompute>(IterationCount) && ExitingBlock)
// Can't rewrite non-branch yet.
// Determine if the OrigIV will ever undergo overflow.
OrigControllingPHI =
TestOrigIVForWrap(L, BI, OrigCond,
- NoSignedWrap, NoUnsignedWrap);
+ NoSignedWrap, NoUnsignedWrap,
+ SignExtendTripCount);
// We'll be replacing the original condition, so it'll be dead.
DeadInsts.insert(OrigCond);
}
LinearFunctionTestReplace(L, IterationCount, IndVar,
- ExitingBlock, BI, Rewriter);
+ ExitingBlock, BI, Rewriter,
+ SignExtendTripCount);
}
// Now that we have a canonical induction variable, we can rewrite any
--- /dev/null
+; RUN: llvm-as < %s | opt -indvars | llvm-dis > %t
+; RUN: grep { = sext i32 %n} %t
+; RUN: grep phi %t | count 1
+; RUN: not grep zext %t
+
+define void @foo(i64* nocapture %x, i32 %n) nounwind {
+entry:
+ %tmp102 = icmp sgt i32 %n, 0 ; <i1> [#uses=1]
+ br i1 %tmp102, label %bb.nph, label %return
+
+bb.nph: ; preds = %entry
+ br label %bb
+
+bb: ; preds = %bb7, %bb.nph
+ %i.01 = phi i32 [ %tmp6, %bb7 ], [ 0, %bb.nph ] ; <i32> [#uses=3]
+ %tmp1 = sext i32 %i.01 to i64 ; <i64> [#uses=1]
+ %tmp4 = getelementptr i64* %x, i32 %i.01 ; <i64*> [#uses=1]
+ store i64 %tmp1, i64* %tmp4, align 8
+ %tmp6 = add i32 %i.01, 1 ; <i32> [#uses=2]
+ br label %bb7
+
+bb7: ; preds = %bb
+ %tmp10 = icmp slt i32 %tmp6, %n ; <i1> [#uses=1]
+ br i1 %tmp10, label %bb, label %bb7.return_crit_edge
+
+bb7.return_crit_edge: ; preds = %bb7
+ br label %return
+
+return: ; preds = %bb7.return_crit_edge, %entry
+ ret void
+}