mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-09-24 23:28:41 +00:00
IndVarSimplify: Don't let LFTR compare against a poison value
LinearFunctionTestReplace tries to use the *next* indvar to compare against when possible. However, it may be the case that the calculation for the next indvar has NUW/NSW flags and that it may only be safely used inside the loop. Using it in a comparison to calculate the exit condition could result in observing poison. This fixes PR20680. Differential Revision: http://reviews.llvm.org/D5174 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217102 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -1641,15 +1641,27 @@ LinearFunctionTestReplace(Loop *L,
|
||||
// compare against the post-incremented value, otherwise we must compare
|
||||
// against the preincremented value.
|
||||
if (L->getExitingBlock() == L->getLoopLatch()) {
|
||||
// Add one to the "backedge-taken" count to get the trip count.
|
||||
// This addition may overflow, which is valid as long as the comparison is
|
||||
// truncated to BackedgeTakenCount->getType().
|
||||
IVCount = SE->getAddExpr(BackedgeTakenCount,
|
||||
SE->getConstant(BackedgeTakenCount->getType(), 1));
|
||||
// The BackedgeTaken expression contains the number of times that the
|
||||
// backedge branches to the loop header. This is one less than the
|
||||
// number of times the loop executes, so use the incremented indvar.
|
||||
CmpIndVar = IndVar->getIncomingValueForBlock(L->getExitingBlock());
|
||||
llvm::Value *IncrementedIndvar = IndVar->getIncomingValueForBlock(L->getExitingBlock());
|
||||
const auto *IncrementedIndvarSCEV =
|
||||
cast<SCEVAddRecExpr>(SE->getSCEV(IncrementedIndvar));
|
||||
// It is unsafe to use the incremented indvar if it has a wrapping flag, we
|
||||
// don't want to compare against a poison value. Check the SCEV that
|
||||
// corresponds to the incremented indvar, the SCEVExpander will only insert
|
||||
// flags in the IR if the SCEV originally had wrapping flags.
|
||||
if (ScalarEvolution::maskFlags(IncrementedIndvarSCEV->getNoWrapFlags(),
|
||||
SCEV::FlagNUW | SCEV::FlagNSW) ==
|
||||
SCEV::FlagAnyWrap) {
|
||||
// Add one to the "backedge-taken" count to get the trip count.
|
||||
// This addition may overflow, which is valid as long as the comparison is
|
||||
// truncated to BackedgeTakenCount->getType().
|
||||
IVCount =
|
||||
SE->getAddExpr(BackedgeTakenCount,
|
||||
SE->getConstant(BackedgeTakenCount->getType(), 1));
|
||||
CmpIndVar = IncrementedIndvar;
|
||||
}
|
||||
}
|
||||
|
||||
Value *ExitCnt = genLoopLimit(IndVar, IVCount, L, Rewriter, SE);
|
||||
|
Reference in New Issue
Block a user