llvm.org GIT mirror llvm / 4e1d300
[NFC] Move some functions to LoopUtils git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351179 91177308-0d34-0410-b5e6-96231b3b80d8 Max Kazantsev 1 year, 10 months ago
3 changed file(s) with 59 addition(s) and 42 deletion(s). Raw diff Collapse all Expand all
322322 /// Flag set: NSW, NUW, exact, and all of fast-math.
323323 void propagateIRFlags(Value *I, ArrayRef VL, Value *OpValue = nullptr);
324324
325 /// Returns true if we can prove that \p S is defined and always negative in
326 /// loop \p L.
327 bool isKnownNegativeInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE);
328
329 /// Returns true if we can prove that \p S is defined and always non-negative in
330 /// loop \p L.
331 bool isKnownNonNegativeInLoop(const SCEV *S, const Loop *L,
332 ScalarEvolution &SE);
333
334 /// Returns true if \p S is defined and never is equal to signed/unsigned max.
335 bool cannotBeMaxInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
336 bool Signed);
337
338 /// Returns true if \p S is defined and never is equal to signed/unsigned min.
339 bool cannotBeMinInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
340 bool Signed);
341
325342 } // end namespace llvm
326343
327344 #endif // LLVM_TRANSFORMS_UTILS_LOOPUTILS_H
688688 PN->setIncomingBlock(i, ReplaceBy);
689689 }
690690
691 static bool CannotBeMaxInLoop(const SCEV *BoundSCEV, Loop *L,
692 ScalarEvolution &SE, bool Signed) {
693 unsigned BitWidth = cast(BoundSCEV->getType())->getBitWidth();
694 APInt Max = Signed ? APInt::getSignedMaxValue(BitWidth) :
695 APInt::getMaxValue(BitWidth);
696 auto Predicate = Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
697 return SE.isAvailableAtLoopEntry(BoundSCEV, L) &&
698 SE.isLoopEntryGuardedByCond(L, Predicate, BoundSCEV,
699 SE.getConstant(Max));
700 }
701
702691 /// Given a loop with an deccreasing induction variable, is it possible to
703692 /// safely calculate the bounds of a new loop using the given Predicate.
704693 static bool isSafeDecreasingBound(const SCEV *Start,
792781 return (SE.isLoopEntryGuardedByCond(L, BoundPred, Start,
793782 SE.getAddExpr(BoundSCEV, Step)) &&
794783 SE.isLoopEntryGuardedByCond(L, BoundPred, BoundSCEV, Limit));
795 }
796
797 static bool CannotBeMinInLoop(const SCEV *BoundSCEV, Loop *L,
798 ScalarEvolution &SE, bool Signed) {
799 unsigned BitWidth = cast(BoundSCEV->getType())->getBitWidth();
800 APInt Min = Signed ? APInt::getSignedMinValue(BitWidth) :
801 APInt::getMinValue(BitWidth);
802 auto Predicate = Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
803 return SE.isAvailableAtLoopEntry(BoundSCEV, L) &&
804 SE.isLoopEntryGuardedByCond(L, Predicate, BoundSCEV,
805 SE.getConstant(Min));
806 }
807
808 static bool isKnownNonNegativeInLoop(const SCEV *BoundSCEV, const Loop *L,
809 ScalarEvolution &SE) {
810 const SCEV *Zero = SE.getZero(BoundSCEV->getType());
811 return SE.isAvailableAtLoopEntry(BoundSCEV, L) &&
812 SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SGE, BoundSCEV, Zero);
813 }
814
815 static bool isKnownNegativeInLoop(const SCEV *BoundSCEV, const Loop *L,
816 ScalarEvolution &SE) {
817 const SCEV *Zero = SE.getZero(BoundSCEV->getType());
818 return SE.isAvailableAtLoopEntry(BoundSCEV, L) &&
819 SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SLT, BoundSCEV, Zero);
820784 }
821785
822786 Optional
976940 // ... ...
977941 // } }
978942 if (IndVarBase->getNoWrapFlags(SCEV::FlagNUW) &&
979 CannotBeMinInLoop(RightSCEV, &L, SE, /*Signed*/false)) {
943 cannotBeMinInLoop(RightSCEV, &L, SE, /*Signed*/false)) {
980944 Pred = ICmpInst::ICMP_UGT;
981945 RightSCEV = SE.getMinusSCEV(RightSCEV,
982946 SE.getOne(RightSCEV->getType()));
983947 DecreasedRightValueByOne = true;
984 } else if (CannotBeMinInLoop(RightSCEV, &L, SE, /*Signed*/true)) {
948 } else if (cannotBeMinInLoop(RightSCEV, &L, SE, /*Signed*/true)) {
985949 Pred = ICmpInst::ICMP_SGT;
986950 RightSCEV = SE.getMinusSCEV(RightSCEV,
987951 SE.getOne(RightSCEV->getType()));
10411005 // ... ...
10421006 // } }
10431007 if (IndVarBase->getNoWrapFlags(SCEV::FlagNUW) &&
1044 CannotBeMaxInLoop(RightSCEV, &L, SE, /* Signed */ false)) {
1008 cannotBeMaxInLoop(RightSCEV, &L, SE, /* Signed */ false)) {
10451009 Pred = ICmpInst::ICMP_ULT;
10461010 RightSCEV = SE.getAddExpr(RightSCEV, SE.getOne(RightSCEV->getType()));
10471011 IncreasedRightValueByOne = true;
1048 } else if (CannotBeMaxInLoop(RightSCEV, &L, SE, /* Signed */ true)) {
1012 } else if (cannotBeMaxInLoop(RightSCEV, &L, SE, /* Signed */ true)) {
10491013 Pred = ICmpInst::ICMP_SLT;
10501014 RightSCEV = SE.getAddExpr(RightSCEV, SE.getOne(RightSCEV->getType()));
10511015 IncreasedRightValueByOne = true;
15131477 if (Increasing)
15141478 ExitPreLoopAtSCEV = *SR.LowLimit;
15151479 else {
1516 if (CannotBeMinInLoop(*SR.HighLimit, &OriginalLoop, SE,
1480 if (cannotBeMinInLoop(*SR.HighLimit, &OriginalLoop, SE,
15171481 IsSignedPredicate))
15181482 ExitPreLoopAtSCEV = SE.getAddExpr(*SR.HighLimit, MinusOneS);
15191483 else {
15421506 if (Increasing)
15431507 ExitMainLoopAtSCEV = *SR.HighLimit;
15441508 else {
1545 if (CannotBeMinInLoop(*SR.LowLimit, &OriginalLoop, SE,
1509 if (cannotBeMinInLoop(*SR.LowLimit, &OriginalLoop, SE,
15461510 IsSignedPredicate))
15471511 ExitMainLoopAtSCEV = SE.getAddExpr(*SR.LowLimit, MinusOneS);
15481512 else {
927927 VecOp->andIRFlags(V);
928928 }
929929 }
930
931 bool llvm::isKnownNegativeInLoop(const SCEV *S, const Loop *L,
932 ScalarEvolution &SE) {
933 const SCEV *Zero = SE.getZero(S->getType());
934 return SE.isAvailableAtLoopEntry(S, L) &&
935 SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SLT, S, Zero);
936 }
937
938 bool llvm::isKnownNonNegativeInLoop(const SCEV *S, const Loop *L,
939 ScalarEvolution &SE) {
940 const SCEV *Zero = SE.getZero(S->getType());
941 return SE.isAvailableAtLoopEntry(S, L) &&
942 SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SGE, S, Zero);
943 }
944
945 bool llvm::cannotBeMinInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
946 bool Signed) {
947 unsigned BitWidth = cast(S->getType())->getBitWidth();
948 APInt Min = Signed ? APInt::getSignedMinValue(BitWidth) :
949 APInt::getMinValue(BitWidth);
950 auto Predicate = Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
951 return SE.isAvailableAtLoopEntry(S, L) &&
952 SE.isLoopEntryGuardedByCond(L, Predicate, S,
953 SE.getConstant(Min));
954 }
955
956 bool llvm::cannotBeMaxInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE,
957 bool Signed) {
958 unsigned BitWidth = cast(S->getType())->getBitWidth();
959 APInt Max = Signed ? APInt::getSignedMaxValue(BitWidth) :
960 APInt::getMaxValue(BitWidth);
961 auto Predicate = Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
962 return SE.isAvailableAtLoopEntry(S, L) &&
963 SE.isLoopEntryGuardedByCond(L, Predicate, S,
964 SE.getConstant(Max));
965 }