llvm.org GIT mirror llvm / dd643f2
Add a utility function that detects whether a loop is guaranteed to be finite. Use it to safely handle less-than-or-equals-to exit conditions in loops. These also occur when the loop exit branch is exit on true because SCEV inverses the icmp predicate. Use it again to handle non-zero strides, but only with an unsigned comparison in the exit condition. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@59528 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 11 years ago
4 changed file(s) with 219 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
14761476 /// specified less-than comparison will execute. If not computable, return
14771477 /// UnknownValue. isSigned specifies whether the less-than is signed.
14781478 SCEVHandle HowManyLessThans(SCEV *LHS, SCEV *RHS, const Loop *L,
1479 bool isSigned);
1479 bool isSigned, bool trueWhenEqual);
14801480
14811481 /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
14821482 /// (which may not be an immediate predecessor) which has exactly one
14861486
14871487 /// executesAtLeastOnce - Test whether entry to the loop is protected by
14881488 /// a conditional between LHS and RHS.
1489 bool executesAtLeastOnce(const Loop *L, bool isSigned, SCEV *LHS, SCEV *RHS);
1489 bool executesAtLeastOnce(const Loop *L, bool isSigned, bool trueWhenEqual,
1490 SCEV *LHS, SCEV *RHS);
1491
1492 /// potentialInfiniteLoop - Test whether the loop might jump over the exit value
1493 /// due to wrapping.
1494 bool potentialInfiniteLoop(SCEV *Stride, SCEV *RHS, bool isSigned,
1495 bool trueWhenEqual);
14901496
14911497 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
14921498 /// in the header of its containing loop, we know the loop executes a
20242030 break;
20252031 }
20262032 case ICmpInst::ICMP_SLT: {
2027 SCEVHandle TC = HowManyLessThans(LHS, RHS, L, true);
2033 SCEVHandle TC = HowManyLessThans(LHS, RHS, L, true, false);
20282034 if (!isa(TC)) return TC;
20292035 break;
20302036 }
20312037 case ICmpInst::ICMP_SGT: {
20322038 SCEVHandle TC = HowManyLessThans(SE.getNotSCEV(LHS),
2033 SE.getNotSCEV(RHS), L, true);
2039 SE.getNotSCEV(RHS), L, true, false);
20342040 if (!isa(TC)) return TC;
20352041 break;
20362042 }
20372043 case ICmpInst::ICMP_ULT: {
2038 SCEVHandle TC = HowManyLessThans(LHS, RHS, L, false);
2044 SCEVHandle TC = HowManyLessThans(LHS, RHS, L, false, false);
20392045 if (!isa(TC)) return TC;
20402046 break;
20412047 }
20422048 case ICmpInst::ICMP_UGT: {
20432049 SCEVHandle TC = HowManyLessThans(SE.getNotSCEV(LHS),
2044 SE.getNotSCEV(RHS), L, false);
2050 SE.getNotSCEV(RHS), L, false, false);
2051 if (!isa(TC)) return TC;
2052 break;
2053 }
2054 case ICmpInst::ICMP_SLE: {
2055 SCEVHandle TC = HowManyLessThans(LHS, RHS, L, true, true);
2056 if (!isa(TC)) return TC;
2057 break;
2058 }
2059 case ICmpInst::ICMP_SGE: {
2060 SCEVHandle TC = HowManyLessThans(SE.getNotSCEV(LHS),
2061 SE.getNotSCEV(RHS), L, true, true);
2062 if (!isa(TC)) return TC;
2063 break;
2064 }
2065 case ICmpInst::ICMP_ULE: {
2066 SCEVHandle TC = HowManyLessThans(LHS, RHS, L, false, true);
2067 if (!isa(TC)) return TC;
2068 break;
2069 }
2070 case ICmpInst::ICMP_UGE: {
2071 SCEVHandle TC = HowManyLessThans(SE.getNotSCEV(LHS),
2072 SE.getNotSCEV(RHS), L, false, true);
20452073 if (!isa(TC)) return TC;
20462074 break;
20472075 }
27372765 /// executesAtLeastOnce - Test whether entry to the loop is protected by
27382766 /// a conditional between LHS and RHS.
27392767 bool ScalarEvolutionsImpl::executesAtLeastOnce(const Loop *L, bool isSigned,
2768 bool trueWhenEqual,
27402769 SCEV *LHS, SCEV *RHS) {
27412770 BasicBlock *Preheader = L->getLoopPreheader();
27422771 BasicBlock *PreheaderDest = L->getHeader();
27692798
27702799 switch (Cond) {
27712800 case ICmpInst::ICMP_UGT:
2772 if (isSigned) continue;
2801 if (isSigned || trueWhenEqual) continue;
27732802 std::swap(PreCondLHS, PreCondRHS);
27742803 Cond = ICmpInst::ICMP_ULT;
27752804 break;
27762805 case ICmpInst::ICMP_SGT:
2777 if (!isSigned) continue;
2806 if (!isSigned || trueWhenEqual) continue;
27782807 std::swap(PreCondLHS, PreCondRHS);
27792808 Cond = ICmpInst::ICMP_SLT;
27802809 break;
27812810 case ICmpInst::ICMP_ULT:
2782 if (isSigned) continue;
2811 if (isSigned || trueWhenEqual) continue;
27832812 break;
27842813 case ICmpInst::ICMP_SLT:
2785 if (!isSigned) continue;
2814 if (!isSigned || trueWhenEqual) continue;
2815 break;
2816 case ICmpInst::ICMP_UGE:
2817 if (isSigned || !trueWhenEqual) continue;
2818 std::swap(PreCondLHS, PreCondRHS);
2819 Cond = ICmpInst::ICMP_ULE;
2820 break;
2821 case ICmpInst::ICMP_SGE:
2822 if (!isSigned || !trueWhenEqual) continue;
2823 std::swap(PreCondLHS, PreCondRHS);
2824 Cond = ICmpInst::ICMP_SLE;
2825 break;
2826 case ICmpInst::ICMP_ULE:
2827 if (isSigned || !trueWhenEqual) continue;
2828 break;
2829 case ICmpInst::ICMP_SLE:
2830 if (!isSigned || !trueWhenEqual) continue;
27862831 break;
27872832 default:
27882833 continue;
28012846 return false;
28022847 }
28032848
2849 /// potentialInfiniteLoop - Test whether the loop might jump over the exit value
2850 /// due to wrapping around 2^n.
2851 bool ScalarEvolutionsImpl::potentialInfiniteLoop(SCEV *Stride, SCEV *RHS,
2852 bool isSigned, bool trueWhenEqual) {
2853 // Return true when the distance from RHS to maxint > Stride.
2854
2855 if (!isa(Stride))
2856 return true;
2857 SCEVConstant *SC = cast(Stride);
2858
2859 if (SC->getValue()->isZero())
2860 return true;
2861 if (!trueWhenEqual && SC->getValue()->isOne())
2862 return false;
2863
2864 if (!isa(RHS))
2865 return true;
2866 SCEVConstant *R = cast(RHS);
2867
2868 if (isSigned)
2869 return true; // XXX: because we don't have an sdiv scev.
2870
2871 // If negative, it wraps around every iteration, but we don't care about that.
2872 APInt S = SC->getValue()->getValue().abs();
2873
2874 APInt Dist = APInt::getMaxValue(R->getValue()->getBitWidth()) -
2875 R->getValue()->getValue();
2876
2877 if (trueWhenEqual)
2878 return !S.ult(Dist);
2879 else
2880 return !S.ule(Dist);
2881 }
2882
28042883 /// HowManyLessThans - Return the number of times a backedge containing the
28052884 /// specified less-than comparison will execute. If not computable, return
28062885 /// UnknownValue.
28072886 SCEVHandle ScalarEvolutionsImpl::
2808 HowManyLessThans(SCEV *LHS, SCEV *RHS, const Loop *L, bool isSigned) {
2887 HowManyLessThans(SCEV *LHS, SCEV *RHS, const Loop *L,
2888 bool isSigned, bool trueWhenEqual) {
28092889 // Only handle: "ADDREC < LoopInvariant".
28102890 if (!RHS->isLoopInvariant(L)) return UnknownValue;
28112891
28142894 return UnknownValue;
28152895
28162896 if (AddRec->isAffine()) {
2817 // FORNOW: We only support unit strides.
2818 SCEVHandle One = SE.getIntegerSCEV(1, RHS->getType());
2819 if (AddRec->getOperand(1) != One)
2897 SCEVHandle Stride = AddRec->getOperand(1);
2898 if (potentialInfiniteLoop(Stride, RHS, isSigned, trueWhenEqual))
28202899 return UnknownValue;
28212900
2822 // We know the LHS is of the form {n,+,1} and the RHS is some loop-invariant
2823 // m. So, we count the number of iterations in which {n,+,1} < m is true.
2824 // Note that we cannot simply return max(m-n,0) because it's not safe to
2901 // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant
2902 // m. So, we count the number of iterations in which {n,+,s} < m is true.
2903 // Note that we cannot simply return max(m-n,0)/s because it's not safe to
28252904 // treat m-n as signed nor unsigned due to overflow possibility.
28262905
28272906 // First, we get the value of the LHS in the first iteration: n
28282907 SCEVHandle Start = AddRec->getOperand(0);
28292908
2830 if (executesAtLeastOnce(L, isSigned,
2831 SE.getMinusSCEV(AddRec->getOperand(0), One), RHS)) {
2832 // Since we know that the condition is true in order to enter the loop,
2833 // we know that it will run exactly m-n times.
2834 return SE.getMinusSCEV(RHS, Start);
2835 } else {
2836 // Then, we get the value of the LHS in the first iteration in which the
2837 // above condition doesn't hold. This equals to max(m,n).
2838 SCEVHandle End = isSigned ? SE.getSMaxExpr(RHS, Start)
2839 : SE.getUMaxExpr(RHS, Start);
2840
2841 // Finally, we subtract these two values to get the number of times the
2842 // backedge is executed: max(m,n)-n.
2843 return SE.getMinusSCEV(End, Start);
2844 }
2909 SCEVHandle One = SE.getIntegerSCEV(1, RHS->getType());
2910
2911 // Assuming that the loop will run at least once, we know that it will
2912 // run (m-n)/s times.
2913 SCEVHandle End = RHS;
2914
2915 if (!executesAtLeastOnce(L, isSigned, trueWhenEqual,
2916 SE.getMinusSCEV(Start, One), RHS)) {
2917 // If not, we get the value of the LHS in the first iteration in which
2918 // the above condition doesn't hold. This equals to max(m,n).
2919 End = isSigned ? SE.getSMaxExpr(RHS, Start)
2920 : SE.getUMaxExpr(RHS, Start);
2921 }
2922
2923 // If the expression is less-than-or-equal to, we need to extend the
2924 // loop by one iteration.
2925 //
2926 // The loop won't actually run (m-n)/s times because the loop iterations
2927 // won't divide evenly. For example, if you have {2,+,5} u< 10 the
2928 // division would equal one, but the loop runs twice putting the
2929 // induction variable at 12.
2930
2931 if (!trueWhenEqual)
2932 // (Stride - 1) is correct only because we know it's unsigned.
2933 // What we really want is to decrease the magnitude of Stride by one.
2934 Start = SE.getMinusSCEV(Start, SE.getMinusSCEV(Stride, One));
2935 else
2936 Start = SE.getMinusSCEV(Start, Stride);
2937
2938 // Finally, we subtract these two values to get the number of times the
2939 // backedge is executed: max(m,n)-n.
2940 return SE.getUDivExpr(SE.getMinusSCEV(End, Start), Stride);
28452941 }
28462942
28472943 return UnknownValue;
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& \
1 ; RUN: grep {Loop bb: (7 + (-1 \\* %argc)) iterations!}
2
3 define i32 @main(i32 %argc, i8** %argv) nounwind {
4 entry:
5 %0 = icmp ugt i32 %argc, 7 ; [#uses=1]
6 br i1 %0, label %bb2, label %bb.nph
7
8 bb.nph: ; preds = %entry
9 br label %bb
10
11 bb: ; preds = %bb.nph, %bb1
12 %indvar = phi i32 [ 0, %bb.nph ], [ %indvar.next, %bb1 ] ; [#uses=2]
13 %argc_addr.04 = add i32 %indvar, %argc ; [#uses=1]
14 tail call void (...)* @Test() nounwind
15 %1 = add i32 %argc_addr.04, 1 ; [#uses=1]
16 br label %bb1
17
18 bb1: ; preds = %bb
19 %phitmp = icmp ugt i32 %1, 7 ; [#uses=1]
20 %indvar.next = add i32 %indvar, 1 ; [#uses=1]
21 br i1 %phitmp, label %bb1.bb2_crit_edge, label %bb
22
23 bb1.bb2_crit_edge: ; preds = %bb1
24 br label %bb2
25
26 bb2: ; preds = %bb1.bb2_crit_edge, %entry
27 ret i32 0
28 }
29
30 declare void @Test(...)
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& grep {/u 3}
1
2 define i32 @f(i32 %x) nounwind readnone {
3 entry:
4 %0 = icmp ugt i32 %x, 4 ; [#uses=1]
5 br i1 %0, label %bb.nph, label %bb2
6
7 bb.nph: ; preds = %entry
8 br label %bb
9
10 bb: ; preds = %bb.nph, %bb1
11 %indvar = phi i32 [ 0, %bb.nph ], [ %indvar.next, %bb1 ] ; [#uses=2]
12 %tmp = mul i32 %indvar, -3 ; [#uses=1]
13 %x_addr.04 = add i32 %tmp, %x ; [#uses=1]
14 %1 = add i32 %x_addr.04, -3 ; [#uses=2]
15 br label %bb1
16
17 bb1: ; preds = %bb
18 %2 = icmp ugt i32 %1, 4 ; [#uses=1]
19 %indvar.next = add i32 %indvar, 1 ; [#uses=1]
20 br i1 %2, label %bb, label %bb1.bb2_crit_edge
21
22 bb1.bb2_crit_edge: ; preds = %bb1
23 %.lcssa = phi i32 [ %1, %bb1 ] ; [#uses=1]
24 br label %bb2
25
26 bb2: ; preds = %bb1.bb2_crit_edge, %entry
27 %x_addr.0.lcssa = phi i32 [ %.lcssa, %bb1.bb2_crit_edge ], [ %x, %entry ] ; [#uses=1]
28 ret i32 %x_addr.0.lcssa
29 }
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& grep {/u 3}
1
2 define i32 @f(i32 %x) nounwind readnone {
3 entry:
4 %0 = icmp ugt i32 %x, 999 ; [#uses=1]
5 br i1 %0, label %bb2, label %bb.nph
6
7 bb.nph: ; preds = %entry
8 br label %bb
9
10 bb: ; preds = %bb.nph, %bb1
11 %indvar = phi i32 [ 0, %bb.nph ], [ %indvar.next, %bb1 ] ; [#uses=2]
12 %tmp = mul i32 %indvar, 3 ; [#uses=1]
13 %x_addr.04 = add i32 %tmp, %x ; [#uses=1]
14 %1 = add i32 %x_addr.04, 3 ; [#uses=2]
15 br label %bb1
16
17 bb1: ; preds = %bb
18 %2 = icmp ugt i32 %1, 999 ; [#uses=1]
19 %indvar.next = add i32 %indvar, 1 ; [#uses=1]
20 br i1 %2, label %bb1.bb2_crit_edge, label %bb
21
22 bb1.bb2_crit_edge: ; preds = %bb1
23 %.lcssa = phi i32 [ %1, %bb1 ] ; [#uses=1]
24 br label %bb2
25
26 bb2: ; preds = %bb1.bb2_crit_edge, %entry
27 %x_addr.0.lcssa = phi i32 [ %.lcssa, %bb1.bb2_crit_edge ], [ %x, %entry ] ; [#uses=1]
28 ret i32 %x_addr.0.lcssa
29 }