@@ -155,8 +155,9 @@ class IndVarSimplify {
155
155
bool rewriteFirstIterationLoopExitValues (Loop *L);
156
156
157
157
bool linearFunctionTestReplace (Loop *L, BasicBlock *ExitingBB,
158
- const SCEV *ExitCount,
159
- PHINode *IndVar, SCEVExpander &Rewriter);
158
+ const SCEV *ExitCount, PHINode *IndVar,
159
+ Instruction *IncVar, const SCEV *IVLimit,
160
+ bool UsePostInc, SCEVExpander &Rewriter);
160
161
161
162
bool sinkUnusedInvariants (Loop *L);
162
163
@@ -901,73 +902,38 @@ static PHINode *FindLoopCounter(Loop *L, BasicBlock *ExitingBB,
901
902
return BestPhi;
902
903
}
903
904
904
- // / Insert an IR expression which computes the value held by the IV IndVar
905
- // / (which must be an loop counter w/unit stride) after the backedge of loop L
906
- // / is taken ExitCount times.
907
- static Value *genLoopLimit (PHINode *IndVar, BasicBlock *ExitingBB,
908
- const SCEV *ExitCount, bool UsePostInc, Loop *L,
909
- SCEVExpander &Rewriter, ScalarEvolution *SE) {
910
- assert (isLoopCounter (IndVar, L, SE));
911
- assert (ExitCount->getType ()->isIntegerTy () && " exit count must be integer" );
912
- const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(SE->getSCEV (IndVar));
913
- assert (AR->getStepRecurrence (*SE)->isOne () && " only handles unit stride" );
914
-
905
+ static const SCEV *getIVLimit (PHINode *IndVar, const SCEV *ExitCount,
906
+ bool UsePostInc, ScalarEvolution *SE) {
915
907
// For integer IVs, truncate the IV before computing the limit unless we
916
908
// know apriori that the limit must be a constant when evaluated in the
917
909
// bitwidth of the IV. We prefer (potentially) keeping a truncate of the
918
910
// IV in the loop over a (potentially) expensive expansion of the widened
919
911
// exit count add(zext(add)) expression.
912
+ const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(SE->getSCEV (IndVar));
913
+ assert (AR->getStepRecurrence (*SE)->isOne () && " only handles unit stride" );
920
914
if (IndVar->getType ()->isIntegerTy () &&
921
915
SE->getTypeSizeInBits (AR->getType ()) >
922
- SE->getTypeSizeInBits (ExitCount->getType ())) {
916
+ SE->getTypeSizeInBits (ExitCount->getType ())) {
923
917
const SCEV *IVInit = AR->getStart ();
924
918
if (!isa<SCEVConstant>(IVInit) || !isa<SCEVConstant>(ExitCount))
925
919
AR = cast<SCEVAddRecExpr>(SE->getTruncateExpr (AR, ExitCount->getType ()));
926
920
}
927
-
928
- const SCEVAddRecExpr *ARBase = UsePostInc ? AR->getPostIncExpr (*SE) : AR;
929
- const SCEV *IVLimit = ARBase->evaluateAtIteration (ExitCount, *SE);
930
- assert (SE->isLoopInvariant (IVLimit, L) &&
931
- " Computed iteration count is not loop invariant!" );
932
- return Rewriter.expandCodeFor (IVLimit, ARBase->getType (),
933
- ExitingBB->getTerminator ());
921
+ AR = UsePostInc ? AR->getPostIncExpr (*SE) : AR;
922
+ return AR->evaluateAtIteration (ExitCount, *SE);
934
923
}
935
924
936
925
// / This method rewrites the exit condition of the loop to be a canonical !=
937
926
// / comparison against the incremented loop induction variable. This pass is
938
927
// / able to rewrite the exit tests of any loop where the SCEV analysis can
939
928
// / determine a loop-invariant trip count of the loop, which is actually a much
940
929
// / broader range than just linear tests.
941
- bool IndVarSimplify::
942
- linearFunctionTestReplace (Loop *L, BasicBlock *ExitingBB,
943
- const SCEV *ExitCount,
944
- PHINode *IndVar, SCEVExpander &Rewriter) {
945
- assert (L->getLoopLatch () && " Loop no longer in simplified form?" );
930
+ bool IndVarSimplify::linearFunctionTestReplace (
931
+ Loop *L, BasicBlock *ExitingBB, const SCEV *ExitCount, PHINode *IndVar,
932
+ Instruction *IncVar, const SCEV *IVLimit, bool UsePostInc,
933
+ SCEVExpander &Rewriter) {
946
934
assert (isLoopCounter (IndVar, L, SE));
947
- Instruction * const IncVar =
948
- cast<Instruction>(IndVar->getIncomingValueForBlock (L->getLoopLatch ()));
949
-
950
- // Initialize CmpIndVar to the preincremented IV.
951
- Value *CmpIndVar = IndVar;
952
- bool UsePostInc = false ;
953
-
954
- // If the exiting block is the same as the backedge block, we prefer to
955
- // compare against the post-incremented value, otherwise we must compare
956
- // against the preincremented value.
957
- if (ExitingBB == L->getLoopLatch ()) {
958
- // For pointer IVs, we chose to not strip inbounds which requires us not
959
- // to add a potentially UB introducing use. We need to either a) show
960
- // the loop test we're modifying is already in post-inc form, or b) show
961
- // that adding a use must not introduce UB.
962
- bool SafeToPostInc =
963
- IndVar->getType ()->isIntegerTy () ||
964
- isLoopExitTestBasedOn (IncVar, ExitingBB) ||
965
- mustExecuteUBIfPoisonOnPathTo (IncVar, ExitingBB->getTerminator (), DT);
966
- if (SafeToPostInc) {
967
- UsePostInc = true ;
968
- CmpIndVar = IncVar;
969
- }
970
- }
935
+
936
+ Value *CmpIndVar = UsePostInc ? IncVar : IndVar;
971
937
972
938
// It may be necessary to drop nowrap flags on the incrementing instruction
973
939
// if either LFTR moves from a pre-inc check to a post-inc check (in which
@@ -989,8 +955,11 @@ linearFunctionTestReplace(Loop *L, BasicBlock *ExitingBB,
989
955
BO->setHasNoSignedWrap (AR->hasNoSignedWrap ());
990
956
}
991
957
992
- Value *ExitCnt = genLoopLimit (
993
- IndVar, ExitingBB, ExitCount, UsePostInc, L, Rewriter, SE);
958
+ assert (ExitCount->getType ()->isIntegerTy () && " exit count must be integer" );
959
+ assert (SE->isLoopInvariant (IVLimit, L) &&
960
+ " Computed iteration count is not loop invariant!" );
961
+ Value *ExitCnt = Rewriter.expandCodeFor (IVLimit, IVLimit->getType (),
962
+ ExitingBB->getTerminator ());
994
963
assert (ExitCnt->getType ()->isPointerTy () ==
995
964
IndVar->getType ()->isPointerTy () &&
996
965
" genLoopLimit missed a cast" );
@@ -1950,8 +1919,6 @@ bool IndVarSimplify::run(Loop *L) {
1950
1919
// If we have a trip count expression, rewrite the loop's exit condition
1951
1920
// using it.
1952
1921
if (!DisableLFTR) {
1953
- BasicBlock *PreHeader = L->getLoopPreheader ();
1954
-
1955
1922
SmallVector<BasicBlock*, 16 > ExitingBlocks;
1956
1923
L->getExitingBlocks (ExitingBlocks);
1957
1924
for (BasicBlock *ExitingBB : ExitingBlocks) {
@@ -1983,18 +1950,44 @@ bool IndVarSimplify::run(Loop *L) {
1983
1950
if (!IndVar)
1984
1951
continue ;
1985
1952
1953
+ assert (L->getLoopLatch () && " Loop no longer in simplified form?" );
1954
+
1955
+ Instruction *IncVar = cast<Instruction>(
1956
+ IndVar->getIncomingValueForBlock (L->getLoopLatch ()));
1957
+
1958
+ // For pointer IVs, we chose to not strip inbounds which requires us not
1959
+ // to add a potentially UB introducing use. We need to either a) show
1960
+ // the loop test we're modifying is already in post-inc form, or b) show
1961
+ // that adding a use must not introduce UB.
1962
+ bool SafeToPostInc =
1963
+ IndVar->getType ()->isIntegerTy () ||
1964
+ isLoopExitTestBasedOn (IncVar, ExitingBB) ||
1965
+ mustExecuteUBIfPoisonOnPathTo (IncVar, ExitingBB->getTerminator (), DT);
1966
+
1967
+ // If the exiting block is the same as the backedge block, we prefer to
1968
+ // compare against the post-incremented value, otherwise we must compare
1969
+ // against the preincremented value.
1970
+ bool UsePostInc = ExitingBB == L->getLoopLatch () && SafeToPostInc;
1971
+
1972
+ // IVLimit is the expression that will get expanded later.
1973
+ const SCEV *IVLimit = getIVLimit (IndVar, ExitCount, UsePostInc, SE);
1974
+
1986
1975
// Avoid high cost expansions. Note: This heuristic is questionable in
1987
1976
// that our definition of "high cost" is not exactly principled.
1977
+ // FIXME: ExitCount is not the expression actually being expanded, but we
1978
+ // check it against high-cost expansions anyway to avoid regressions.
1988
1979
if (Rewriter.isHighCostExpansion (ExitCount, L, SCEVCheapExpansionBudget,
1989
- TTI, PreHeader->getTerminator ()))
1980
+ TTI, ExitingBB->getTerminator ()) ||
1981
+ Rewriter.isHighCostExpansion (IVLimit, L, SCEVCheapExpansionBudget,
1982
+ TTI, ExitingBB->getTerminator ()))
1990
1983
continue ;
1991
1984
1992
- if (!Rewriter.isSafeToExpand (ExitCount ))
1985
+ if (!Rewriter.isSafeToExpand (IVLimit ))
1993
1986
continue ;
1994
1987
1995
- Changed |= linearFunctionTestReplace (L, ExitingBB,
1996
- ExitCount, IndVar,
1997
- Rewriter);
1988
+ Changed |=
1989
+ linearFunctionTestReplace (L, ExitingBB, ExitCount, IndVar, IncVar ,
1990
+ IVLimit, UsePostInc, Rewriter);
1998
1991
}
1999
1992
}
2000
1993
// Clear the rewriter cache, because values that are in the rewriter's cache
0 commit comments