llvm.org GIT mirror llvm / 52fddd3
Fix the the ceiling-division used in computing the MaxBECount so that it doesn't have trouble with an intermediate add overflowing. Also, be more conservative about the case where the induction variable in an SLT loop exit can step past the RHS of the SLT and overflow in a single step. Make getSignedRange more aggressive, to recover for some common cases which the above fixes pessimized. This addresses rdar://7561161. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@94512 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
3 changed file(s) with 486 addition(s) and 56 deletion(s). Raw diff Collapse all Expand all
29112911 if (const SCEVConstant *C = dyn_cast(S))
29122912 return ConstantRange(C->getValue()->getValue());
29132913
2914 unsigned BitWidth = getTypeSizeInBits(S->getType());
2915 ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
2916
2917 // If the value has known zeros, the maximum signed value will have those
2918 // known zeros as well.
2919 uint32_t TZ = GetMinTrailingZeros(S);
2920 if (TZ != 0)
2921 ConservativeResult =
2922 ConstantRange(APInt::getSignedMinValue(BitWidth),
2923 APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1);
2924
29142925 if (const SCEVAddExpr *Add = dyn_cast(S)) {
29152926 ConstantRange X = getSignedRange(Add->getOperand(0));
29162927 for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
29172928 X = X.add(getSignedRange(Add->getOperand(i)));
2918 return X;
2929 return ConservativeResult.intersectWith(X);
29192930 }
29202931
29212932 if (const SCEVMulExpr *Mul = dyn_cast(S)) {
29222933 ConstantRange X = getSignedRange(Mul->getOperand(0));
29232934 for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
29242935 X = X.multiply(getSignedRange(Mul->getOperand(i)));
2925 return X;
2936 return ConservativeResult.intersectWith(X);
29262937 }
29272938
29282939 if (const SCEVSMaxExpr *SMax = dyn_cast(S)) {
29292940 ConstantRange X = getSignedRange(SMax->getOperand(0));
29302941 for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
29312942 X = X.smax(getSignedRange(SMax->getOperand(i)));
2932 return X;
2943 return ConservativeResult.intersectWith(X);
29332944 }
29342945
29352946 if (const SCEVUMaxExpr *UMax = dyn_cast(S)) {
29362947 ConstantRange X = getSignedRange(UMax->getOperand(0));
29372948 for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
29382949 X = X.umax(getSignedRange(UMax->getOperand(i)));
2939 return X;
2950 return ConservativeResult.intersectWith(X);
29402951 }
29412952
29422953 if (const SCEVUDivExpr *UDiv = dyn_cast(S)) {
29432954 ConstantRange X = getSignedRange(UDiv->getLHS());
29442955 ConstantRange Y = getSignedRange(UDiv->getRHS());
2945 return X.udiv(Y);
2956 return ConservativeResult.intersectWith(X.udiv(Y));
29462957 }
29472958
29482959 if (const SCEVZeroExtendExpr *ZExt = dyn_cast(S)) {
29492960 ConstantRange X = getSignedRange(ZExt->getOperand());
2950 return X.zeroExtend(cast(ZExt->getType())->getBitWidth());
2961 return ConservativeResult.intersectWith(X.zeroExtend(BitWidth));
29512962 }
29522963
29532964 if (const SCEVSignExtendExpr *SExt = dyn_cast(S)) {
29542965 ConstantRange X = getSignedRange(SExt->getOperand());
2955 return X.signExtend(cast(SExt->getType())->getBitWidth());
2966 return ConservativeResult.intersectWith(X.signExtend(BitWidth));
29562967 }
29572968
29582969 if (const SCEVTruncateExpr *Trunc = dyn_cast(S)) {
29592970 ConstantRange X = getSignedRange(Trunc->getOperand());
2960 return X.truncate(cast(Trunc->getType())->getBitWidth());
2961 }
2962
2963 ConstantRange FullSet(getTypeSizeInBits(S->getType()), true);
2971 return ConservativeResult.intersectWith(X.truncate(BitWidth));
2972 }
29642973
29652974 if (const SCEVAddRecExpr *AddRec = dyn_cast(S)) {
29662975 const SCEV *T = getBackedgeTakenCount(AddRec->getLoop());
29672976 const SCEVConstant *Trip = dyn_cast(T);
2968 ConstantRange ConservativeResult = FullSet;
29692977
29702978 // If there's no signed wrap, and all the operands have the same sign or
29712979 // zero, the value won't ever change sign.
29762984 if (!isKnownNonNegative(AddRec->getOperand(i))) AllNonNeg = false;
29772985 if (!isKnownNonPositive(AddRec->getOperand(i))) AllNonPos = false;
29782986 }
2979 unsigned BitWidth = getTypeSizeInBits(AddRec->getType());
29802987 if (AllNonNeg)
2981 ConservativeResult = ConstantRange(APInt(BitWidth, 0),
2982 APInt::getSignedMinValue(BitWidth));
2988 ConservativeResult = ConservativeResult.intersectWith(
2989 ConstantRange(APInt(BitWidth, 0),
2990 APInt::getSignedMinValue(BitWidth)));
29832991 else if (AllNonPos)
2984 ConservativeResult = ConstantRange(APInt::getSignedMinValue(BitWidth),
2985 APInt(BitWidth, 1));
2992 ConservativeResult = ConservativeResult.intersectWith(
2993 ConstantRange(APInt::getSignedMinValue(BitWidth),
2994 APInt(BitWidth, 1)));
29862995 }
29872996
29882997 // TODO: non-affine addrec
29892998 if (Trip && AddRec->isAffine()) {
29902999 const Type *Ty = AddRec->getType();
29913000 const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
2992 if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) {
3001 if (getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) {
29933002 MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty);
29943003
29953004 const SCEV *Start = AddRec->getStart();
30073016 EndRange.getSignedMax());
30083017 if (Min.isMinSignedValue() && Max.isMaxSignedValue())
30093018 return ConservativeResult;
3010 return ConstantRange(Min, Max+1);
3019 return ConservativeResult.intersectWith(ConstantRange(Min, Max+1));
30113020 }
30123021 }
30133022
30163025
30173026 if (const SCEVUnknown *U = dyn_cast(S)) {
30183027 // For a SCEVUnknown, ask ValueTracking.
3019 unsigned BitWidth = getTypeSizeInBits(U->getType());
30203028 if (!U->getValue()->getType()->isInteger() && !TD)
3021 return FullSet;
3029 return ConservativeResult;
30223030 unsigned NS = ComputeNumSignBits(U->getValue(), TD);
30233031 if (NS == 1)
3024 return FullSet;
3025 return
3032 return ConservativeResult;
3033 return ConservativeResult.intersectWith(
30263034 ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
3027 APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1);
3028 }
3029
3030 return FullSet;
3035 APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1));
3036 }
3037
3038 return ConservativeResult;
30313039 }
30323040
30333041 /// createSCEV - We know that there is no SCEV for the specified value.
49464954 const SCEV *End,
49474955 const SCEV *Step,
49484956 bool NoWrap) {
4957 assert(!isKnownNegative(Step) &&
4958 "This code doesn't handle negative strides yet!");
4959
49494960 const Type *Ty = Start->getType();
49504961 const SCEV *NegOne = getIntegerSCEV(-1, Ty);
49514962 const SCEV *Diff = getMinusSCEV(End, Start);
49884999 AddRec->hasNoUnsignedWrap();
49895000
49905001 if (AddRec->isAffine()) {
4991 // FORNOW: We only support unit strides.
49925002 unsigned BitWidth = getTypeSizeInBits(AddRec->getType());
49935003 const SCEV *Step = AddRec->getStepRecurrence(*this);
49945004
4995 // TODO: handle non-constant strides.
4996 const SCEVConstant *CStep = dyn_cast(Step);
4997 if (!CStep || CStep->isZero())
5005 if (Step->isZero())
49985006 return getCouldNotCompute();
4999 if (CStep->isOne()) {
5007 if (Step->isOne()) {
50005008 // With unit stride, the iteration never steps past the limit value.
5001 } else if (CStep->getValue()->getValue().isStrictlyPositive()) {
5002 if (NoWrap) {
5003 // We know the iteration won't step past the maximum value for its type.
5004 ;
5005 } else if (const SCEVConstant *CLimit = dyn_cast(RHS)) {
5006 // Test whether a positive iteration iteration can step past the limit
5007 // value and past the maximum value for its type in a single step.
5008 if (isSigned) {
5009 APInt Max = APInt::getSignedMaxValue(BitWidth);
5010 if ((Max - CStep->getValue()->getValue())
5011 .slt(CLimit->getValue()->getValue()))
5012 return getCouldNotCompute();
5013 } else {
5014 APInt Max = APInt::getMaxValue(BitWidth);
5015 if ((Max - CStep->getValue()->getValue())
5016 .ult(CLimit->getValue()->getValue()))
5017 return getCouldNotCompute();
5018 }
5019 } else
5020 // TODO: handle non-constant limit values below.
5021 return getCouldNotCompute();
5009 } else if (isKnownPositive(Step)) {
5010 // Test whether a positive iteration iteration can step past the limit
5011 // value and past the maximum value for its type in a single step.
5012 // Note that it's not sufficient to check NoWrap here, because even
5013 // though the value after a wrap is undefined, it's not undefined
5014 // behavior, so if wrap does occur, the loop could either terminate or
5015 // loop infinately, but in either case, the loop is guaranteed to
5016 // iterate at least until the iteration where the wrapping occurs.
5017 const SCEV *One = getIntegerSCEV(1, Step->getType());
5018 if (isSigned) {
5019 APInt Max = APInt::getSignedMaxValue(BitWidth);
5020 if ((Max - getSignedRange(getMinusSCEV(Step, One)).getSignedMax())
5021 .slt(getSignedRange(RHS).getSignedMax()))
5022 return getCouldNotCompute();
5023 } else {
5024 APInt Max = APInt::getMaxValue(BitWidth);
5025 if ((Max - getUnsignedRange(getMinusSCEV(Step, One)).getUnsignedMax())
5026 .ult(getUnsignedRange(RHS).getUnsignedMax()))
5027 return getCouldNotCompute();
5028 }
50225029 } else
5023 // TODO: handle negative strides below.
5030 // TODO: Handle negative strides here and below.
50245031 return getCouldNotCompute();
50255032
50265033 // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant
50525059 const SCEV *MaxEnd = getConstant(isSigned ?
50535060 getSignedRange(End).getSignedMax() :
50545061 getUnsignedRange(End).getUnsignedMax());
5062
5063 // If MaxEnd is within a step of the maximum integer value in its type,
5064 // adjust it down to the minimum value which would produce the same effect.
5065 // This allows the subsequent ceiling divison of (N+(step-1))/step to
5066 // compute the correct value.
5067 const SCEV *StepMinusOne = getMinusSCEV(Step,
5068 getIntegerSCEV(1, Step->getType()));
5069 MaxEnd = isSigned ?
5070 getSMinExpr(MaxEnd,
5071 getMinusSCEV(getConstant(APInt::getSignedMaxValue(BitWidth)),
5072 StepMinusOne)) :
5073 getUMinExpr(MaxEnd,
5074 getMinusSCEV(getConstant(APInt::getMaxValue(BitWidth)),
5075 StepMinusOne));
50555076
50565077 // Finally, we subtract these two values and divide, rounding up, to get
50575078 // the number of times the backedge is executed.
55
66 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
77
8 define void @foo(i32 %n, double* nocapture %d, double* nocapture %q) nounwind {
8 define void @foo(i32 %no, double* nocapture %d, double* nocapture %q) nounwind {
99 entry:
10 %n = and i32 %no, 4294967294
1011 %0 = icmp sgt i32 %n, 0 ; [#uses=1]
1112 br i1 %0, label %bb.nph, label %return
1213
7273 }
7374
7475 ; CHECK: Loop %bb: backedge-taken count is ((-1 + %n) /u 2)
75 ; CHECK: Loop %bb: max backedge-taken count is 1073741823
76 ; CHECK: Loop %bb: max backedge-taken count is 1073741822
0 ; RUN: opt -analyze -scalar-evolution -S < %s | FileCheck %s
1
2 ; Every combination of
3 ; - starting at 0, 1, or %x
4 ; - steping by 1 or 2
5 ; - stopping at %n or %n*2
6 ; - using nsw, or not
7
8 ; Some of these represent missed opportunities.
9
10 ; CHECK: Determining loop execution counts for: @foo
11 ; CHECK: Loop %loop: backedge-taken count is (-1 + %n)
12 ; CHECK: Loop %loop: max backedge-taken count is 6
13 define void @foo(i4 %n) {
14 entry:
15 %s = icmp sgt i4 %n, 0
16 br i1 %s, label %loop, label %exit
17 loop:
18 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
19 %i.next = add i4 %i, 1
20 %t = icmp slt i4 %i.next, %n
21 br i1 %t, label %loop, label %exit
22 exit:
23 ret void
24 }
25
26 ; CHECK: Determining loop execution counts for: @step2
27 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
28 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
29 define void @step2(i4 %n) {
30 entry:
31 %s = icmp sgt i4 %n, 0
32 br i1 %s, label %loop, label %exit
33 loop:
34 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
35 %i.next = add i4 %i, 2
36 %t = icmp slt i4 %i.next, %n
37 br i1 %t, label %loop, label %exit
38 exit:
39 ret void
40 }
41
42 ; CHECK: Determining loop execution counts for: @start1
43 ; CHECK: Loop %loop: backedge-taken count is (-2 + (2 smax %n))
44 ; CHECK: Loop %loop: max backedge-taken count is 5
45 define void @start1(i4 %n) {
46 entry:
47 %s = icmp sgt i4 %n, 0
48 br i1 %s, label %loop, label %exit
49 loop:
50 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
51 %i.next = add i4 %i, 1
52 %t = icmp slt i4 %i.next, %n
53 br i1 %t, label %loop, label %exit
54 exit:
55 ret void
56 }
57
58 ; CHECK: Determining loop execution counts for: @start1_step2
59 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
60 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
61 define void @start1_step2(i4 %n) {
62 entry:
63 %s = icmp sgt i4 %n, 0
64 br i1 %s, label %loop, label %exit
65 loop:
66 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
67 %i.next = add i4 %i, 2
68 %t = icmp slt i4 %i.next, %n
69 br i1 %t, label %loop, label %exit
70 exit:
71 ret void
72 }
73
74 ; CHECK: Determining loop execution counts for: @startx
75 ; CHECK: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax %n))
76 ; CHECK: Loop %loop: max backedge-taken count is -1
77 define void @startx(i4 %n, i4 %x) {
78 entry:
79 %s = icmp sgt i4 %n, 0
80 br i1 %s, label %loop, label %exit
81 loop:
82 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
83 %i.next = add i4 %i, 1
84 %t = icmp slt i4 %i.next, %n
85 br i1 %t, label %loop, label %exit
86 exit:
87 ret void
88 }
89
90 ; CHECK: Determining loop execution counts for: @startx_step2
91 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
92 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
93 define void @startx_step2(i4 %n, i4 %x) {
94 entry:
95 %s = icmp sgt i4 %n, 0
96 br i1 %s, label %loop, label %exit
97 loop:
98 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
99 %i.next = add i4 %i, 2
100 %t = icmp slt i4 %i.next, %n
101 br i1 %t, label %loop, label %exit
102 exit:
103 ret void
104 }
105
106 ; CHECK: Determining loop execution counts for: @nsw
107 ; CHECK: Loop %loop: backedge-taken count is (-1 + %n)
108 ; CHECK: Loop %loop: max backedge-taken count is 6
109 define void @nsw(i4 %n) {
110 entry:
111 %s = icmp sgt i4 %n, 0
112 br i1 %s, label %loop, label %exit
113 loop:
114 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
115 %i.next = add nsw i4 %i, 1
116 %t = icmp slt i4 %i.next, %n
117 br i1 %t, label %loop, label %exit
118 exit:
119 ret void
120 }
121
122 ; Be careful with this one. If %n is INT4_MAX, %i.next will wrap. The nsw bit
123 ; says that the result is undefined, but ScalarEvolution must respect that
124 ; subsequent passes may result the undefined behavior in predictable ways.
125 ; CHECK: Determining loop execution counts for: @nsw_step2
126 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
127 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
128 define void @nsw_step2(i4 %n) {
129 entry:
130 %s = icmp sgt i4 %n, 0
131 br i1 %s, label %loop, label %exit
132 loop:
133 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
134 %i.next = add nsw i4 %i, 2
135 %t = icmp slt i4 %i.next, %n
136 br i1 %t, label %loop, label %exit
137 exit:
138 ret void
139 }
140
141 ; CHECK: Determining loop execution counts for: @nsw_start1
142 ; CHECK: Loop %loop: backedge-taken count is (-2 + (2 smax %n))
143 ; CHECK: Loop %loop: max backedge-taken count is 5
144 define void @nsw_start1(i4 %n) {
145 entry:
146 %s = icmp sgt i4 %n, 0
147 br i1 %s, label %loop, label %exit
148 loop:
149 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
150 %i.next = add nsw i4 %i, 1
151 %t = icmp slt i4 %i.next, %n
152 br i1 %t, label %loop, label %exit
153 exit:
154 ret void
155 }
156
157 ; CHECK: Determining loop execution counts for: @nsw_start1_step2
158 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
159 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
160 define void @nsw_start1_step2(i4 %n) {
161 entry:
162 %s = icmp sgt i4 %n, 0
163 br i1 %s, label %loop, label %exit
164 loop:
165 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
166 %i.next = add nsw i4 %i, 2
167 %t = icmp slt i4 %i.next, %n
168 br i1 %t, label %loop, label %exit
169 exit:
170 ret void
171 }
172
173 ; CHECK: Determining loop execution counts for: @nsw_startx
174 ; CHECK: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax %n))
175 ; CHECK: Loop %loop: max backedge-taken count is -1
176 define void @nsw_startx(i4 %n, i4 %x) {
177 entry:
178 %s = icmp sgt i4 %n, 0
179 br i1 %s, label %loop, label %exit
180 loop:
181 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
182 %i.next = add nsw i4 %i, 1
183 %t = icmp slt i4 %i.next, %n
184 br i1 %t, label %loop, label %exit
185 exit:
186 ret void
187 }
188
189 ; CHECK: Determining loop execution counts for: @nsw_startx_step2
190 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
191 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
192 define void @nsw_startx_step2(i4 %n, i4 %x) {
193 entry:
194 %s = icmp sgt i4 %n, 0
195 br i1 %s, label %loop, label %exit
196 loop:
197 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
198 %i.next = add nsw i4 %i, 2
199 %t = icmp slt i4 %i.next, %n
200 br i1 %t, label %loop, label %exit
201 exit:
202 ret void
203 }
204
205 ; CHECK: Determining loop execution counts for: @even
206 ; CHECK: Loop %loop: backedge-taken count is (-1 + (2 * %n))
207 ; CHECK: Loop %loop: max backedge-taken count is 5
208 define void @even(i4 %n) {
209 entry:
210 %m = shl i4 %n, 1
211 %s = icmp sgt i4 %m, 0
212 br i1 %s, label %loop, label %exit
213 loop:
214 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
215 %i.next = add i4 %i, 1
216 %t = icmp slt i4 %i.next, %m
217 br i1 %t, label %loop, label %exit
218 exit:
219 ret void
220 }
221
222 ; CHECK: Determining loop execution counts for: @even_step2
223 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
224 ; CHECK: Loop %loop: max backedge-taken count is 2
225 define void @even_step2(i4 %n) {
226 entry:
227 %m = shl i4 %n, 1
228 %s = icmp sgt i4 %m, 0
229 br i1 %s, label %loop, label %exit
230 loop:
231 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
232 %i.next = add i4 %i, 2
233 %t = icmp slt i4 %i.next, %m
234 br i1 %t, label %loop, label %exit
235 exit:
236 ret void
237 }
238
239 ; CHECK: Determining loop execution counts for: @even_start1
240 ; CHECK: Loop %loop: backedge-taken count is (-2 + (2 smax (2 * %n)))
241 ; CHECK: Loop %loop: max backedge-taken count is 4
242 define void @even_start1(i4 %n) {
243 entry:
244 %m = shl i4 %n, 1
245 %s = icmp sgt i4 %m, 0
246 br i1 %s, label %loop, label %exit
247 loop:
248 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
249 %i.next = add i4 %i, 1
250 %t = icmp slt i4 %i.next, %m
251 br i1 %t, label %loop, label %exit
252 exit:
253 ret void
254 }
255
256 ; CHECK: Determining loop execution counts for: @even_start1_step2
257 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
258 ; CHECK: Loop %loop: max backedge-taken count is 2
259 define void @even_start1_step2(i4 %n) {
260 entry:
261 %m = shl i4 %n, 1
262 %s = icmp sgt i4 %m, 0
263 br i1 %s, label %loop, label %exit
264 loop:
265 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
266 %i.next = add i4 %i, 2
267 %t = icmp slt i4 %i.next, %m
268 br i1 %t, label %loop, label %exit
269 exit:
270 ret void
271 }
272
273 ; CHECK: Determining loop execution counts for: @even_startx
274 ; CHECK: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax (2 * %n)))
275 ; CHECK: Loop %loop: max backedge-taken count is -1
276 define void @even_startx(i4 %n, i4 %x) {
277 entry:
278 %m = shl i4 %n, 1
279 %s = icmp sgt i4 %m, 0
280 br i1 %s, label %loop, label %exit
281 loop:
282 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
283 %i.next = add i4 %i, 1
284 %t = icmp slt i4 %i.next, %m
285 br i1 %t, label %loop, label %exit
286 exit:
287 ret void
288 }
289
290 ; CHECK: Determining loop execution counts for: @even_startx_step2
291 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
292 ; CHECK: Loop %loop: max backedge-taken count is 7
293 define void @even_startx_step2(i4 %n, i4 %x) {
294 entry:
295 %m = shl i4 %n, 1
296 %s = icmp sgt i4 %m, 0
297 br i1 %s, label %loop, label %exit
298 loop:
299 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
300 %i.next = add i4 %i, 2
301 %t = icmp slt i4 %i.next, %m
302 br i1 %t, label %loop, label %exit
303 exit:
304 ret void
305 }
306
307 ; CHECK: Determining loop execution counts for: @even_nsw
308 ; CHECK: Loop %loop: backedge-taken count is (-1 + (2 * %n))
309 ; CHECK: Loop %loop: max backedge-taken count is 5
310 define void @even_nsw(i4 %n) {
311 entry:
312 %m = shl i4 %n, 1
313 %s = icmp sgt i4 %m, 0
314 br i1 %s, label %loop, label %exit
315 loop:
316 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
317 %i.next = add nsw i4 %i, 1
318 %t = icmp slt i4 %i.next, %m
319 br i1 %t, label %loop, label %exit
320 exit:
321 ret void
322 }
323
324 ; CHECK: Determining loop execution counts for: @even_nsw_step2
325 ; CHECK: Loop %loop: backedge-taken count is ((-1 + (2 * %n)) /u 2)
326 ; CHECK: Loop %loop: max backedge-taken count is 2
327 define void @even_nsw_step2(i4 %n) {
328 entry:
329 %m = shl i4 %n, 1
330 %s = icmp sgt i4 %m, 0
331 br i1 %s, label %loop, label %exit
332 loop:
333 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
334 %i.next = add nsw i4 %i, 2
335 %t = icmp slt i4 %i.next, %m
336 br i1 %t, label %loop, label %exit
337 exit:
338 ret void
339 }
340
341 ; CHECK: Determining loop execution counts for: @even_nsw_start1
342 ; CHECK: Loop %loop: backedge-taken count is (-2 + (2 smax (2 * %n)))
343 ; CHECK: Loop %loop: max backedge-taken count is 4
344 define void @even_nsw_start1(i4 %n) {
345 entry:
346 %m = shl i4 %n, 1
347 %s = icmp sgt i4 %m, 0
348 br i1 %s, label %loop, label %exit
349 loop:
350 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
351 %i.next = add nsw i4 %i, 1
352 %t = icmp slt i4 %i.next, %m
353 br i1 %t, label %loop, label %exit
354 exit:
355 ret void
356 }
357
358 ; CHECK: Determining loop execution counts for: @even_nsw_start1_step2
359 ; CHECK: Loop %loop: backedge-taken count is ((-2 + (3 smax (2 * %n))) /u 2)
360 ; CHECK: Loop %loop: max backedge-taken count is 2
361 define void @even_nsw_start1_step2(i4 %n) {
362 entry:
363 %m = shl i4 %n, 1
364 %s = icmp sgt i4 %m, 0
365 br i1 %s, label %loop, label %exit
366 loop:
367 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
368 %i.next = add nsw i4 %i, 2
369 %t = icmp slt i4 %i.next, %m
370 br i1 %t, label %loop, label %exit
371 exit:
372 ret void
373 }
374
375 ; CHECK: Determining loop execution counts for: @even_nsw_startx
376 ; CHECK: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax (2 * %n)))
377 ; CHECK: Loop %loop: max backedge-taken count is -1
378 define void @even_nsw_startx(i4 %n, i4 %x) {
379 entry:
380 %m = shl i4 %n, 1
381 %s = icmp sgt i4 %m, 0
382 br i1 %s, label %loop, label %exit
383 loop:
384 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
385 %i.next = add nsw i4 %i, 1
386 %t = icmp slt i4 %i.next, %m
387 br i1 %t, label %loop, label %exit
388 exit:
389 ret void
390 }
391
392 ; CHECK: Determining loop execution counts for: @even_nsw_startx_step2
393 ; CHECK: Loop %loop: backedge-taken count is ((-1 + (-1 * %x) + ((2 + %x) smax (2 * %n))) /u 2)
394 ; CHECK: Loop %loop: max backedge-taken count is 7
395 define void @even_nsw_startx_step2(i4 %n, i4 %x) {
396 entry:
397 %m = shl i4 %n, 1
398 %s = icmp sgt i4 %m, 0
399 br i1 %s, label %loop, label %exit
400 loop:
401 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
402 %i.next = add nsw i4 %i, 2
403 %t = icmp slt i4 %i.next, %m
404 br i1 %t, label %loop, label %exit
405 exit:
406 ret void
407 }