llvm.org GIT mirror llvm / 7f1eb15
[SCEV] Teach SCEV to find maxBECount when loop endbound is variant Summary: This patch teaches SCEV to calculate the maxBECount when the end bound of the loop can vary. Note that we cannot calculate the exactBECount. This will only be done when both conditions are satisfied: 1. the loop termination condition is strictly LT. 2. the IV is proven to not overflow. This provides more information to users of SCEV and can be used to improve identification of finite loops. Reviewers: sanjoy, mkazantsev, silviu.baranga, atrick Reviewed by: mkazantsev Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D38825 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315683 91177308-0d34-0410-b5e6-96231b3b80d8 Anna Thomas 2 years ago
4 changed file(s) with 207 addition(s) and 36 deletion(s). Raw diff Collapse all Expand all
17401740 const SCEV *computeBECount(const SCEV *Delta, const SCEV *Stride,
17411741 bool Equality);
17421742
1743 // Compute the maximum backedge count based on the range of values
1744 // permitted by Start, End, and Stride.
1745 const SCEV *computeMaxBECount(const SCEV *Start, const SCEV *Stride,
1746 const SCEV *End, unsigned BitWidth,
1747 bool IsSigned);
1748
17431749 /// Verify if an linear IV with positive stride can overflow when in a
17441750 /// less-than comparison, knowing the invariant term of the comparison,
17451751 /// the stride and the knowledge of NSW/NUW flags on the recurrence.
96889688 return getUDivExpr(Delta, Step);
96899689 }
96909690
9691 const SCEV *ScalarEvolution::computeMaxBECount(const SCEV *Start,
9692 const SCEV *Stride,
9693 const SCEV *End,
9694 unsigned BitWidth,
9695 bool IsSigned) {
9696
9697 assert(!isKnownNonPositive(Stride) &&
9698 "Stride is expected strictly positive!");
9699 // Calculate the maximum backedge count based on the range of values
9700 // permitted by Start, End, and Stride.
9701 const SCEV *MaxBECount;
9702 APInt MinStart =
9703 IsSigned ? getSignedRangeMin(Start) : getUnsignedRangeMin(Start);
9704
9705 APInt StrideForMaxBECount;
9706
9707 bool PositiveStride = isKnownPositive(Stride);
9708 if (PositiveStride)
9709 StrideForMaxBECount =
9710 IsSigned ? getSignedRangeMin(Stride) : getUnsignedRangeMin(Stride);
9711 else
9712 // Using a stride of 1 is safe when computing max backedge taken count for
9713 // a loop with unknown stride.
9714 StrideForMaxBECount = APInt(BitWidth, 1, IsSigned);
9715
9716 APInt MaxValue = IsSigned ? APInt::getSignedMaxValue(BitWidth)
9717 : APInt::getMaxValue(BitWidth);
9718 APInt Limit = MaxValue - (StrideForMaxBECount - 1);
9719
9720 // Although End can be a MAX expression we estimate MaxEnd considering only
9721 // the case End = RHS of the loop termination condition. This is safe because
9722 // in the other case (End - Start) is zero, leading to a zero maximum backedge
9723 // taken count.
9724 APInt MaxEnd = IsSigned ? APIntOps::smin(getSignedRangeMax(End), Limit)
9725 : APIntOps::umin(getUnsignedRangeMax(End), Limit);
9726
9727 MaxBECount = computeBECount(getConstant(MaxEnd - MinStart) /* Delta */,
9728 getConstant(StrideForMaxBECount) /* Step */,
9729 false /* Equality */);
9730
9731 return MaxBECount;
9732 }
9733
96919734 ScalarEvolution::ExitLimit
96929735 ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
96939736 const Loop *L, bool IsSigned,
96949737 bool ControlsExit, bool AllowPredicates) {
96959738 SmallPtrSet Predicates;
9696 // We handle only IV < Invariant
9697 if (!isLoopInvariant(RHS, L))
9698 return getCouldNotCompute();
96999739
97009740 const SCEVAddRecExpr *IV = dyn_cast(LHS);
97019741 bool PredicatedIV = false;
97789818 : ICmpInst::ICMP_ULT;
97799819 const SCEV *Start = IV->getStart();
97809820 const SCEV *End = RHS;
9821 // When the RHS is not invariant, we do not know the end bound of the loop and
9822 // cannot calculate the ExactBECount needed by ExitLimit. However, we can
9823 // calculate the MaxBECount, given the start, stride and max value for the end
9824 // bound of the loop (RHS), and the fact that IV does not overflow (which is
9825 // checked above).
9826 if (!isLoopInvariant(RHS, L)) {
9827 const SCEV *MaxBECount = computeMaxBECount(
9828 Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
9829 return ExitLimit(getCouldNotCompute() /* ExactNotTaken */, MaxBECount,
9830 false /*MaxOrZero*/, Predicates);
9831 }
97819832 // If the backedge is taken at least once, then it will be taken
97829833 // (End-Start)/Stride times (rounded up to a multiple of Stride), where Start
97839834 // is the LHS value of the less-than comparison the first time it is evaluated
98109861 MaxBECount = BECountIfBackedgeTaken;
98119862 MaxOrZero = true;
98129863 } else {
9813 // Calculate the maximum backedge count based on the range of values
9814 // permitted by Start, End, and Stride.
9815 APInt MinStart = IsSigned ? getSignedRangeMin(Start)
9816 : getUnsignedRangeMin(Start);
9817
9818 unsigned BitWidth = getTypeSizeInBits(LHS->getType());
9819
9820 APInt StrideForMaxBECount;
9821
9822 if (PositiveStride)
9823 StrideForMaxBECount =
9824 IsSigned ? getSignedRangeMin(Stride)
9825 : getUnsignedRangeMin(Stride);
9826 else
9827 // Using a stride of 1 is safe when computing max backedge taken count for
9828 // a loop with unknown stride.
9829 StrideForMaxBECount = APInt(BitWidth, 1, IsSigned);
9830
9831 APInt Limit =
9832 IsSigned ? APInt::getSignedMaxValue(BitWidth) - (StrideForMaxBECount - 1)
9833 : APInt::getMaxValue(BitWidth) - (StrideForMaxBECount - 1);
9834
9835 // Although End can be a MAX expression we estimate MaxEnd considering only
9836 // the case End = RHS. This is safe because in the other case (End - Start)
9837 // is zero, leading to a zero maximum backedge taken count.
9838 APInt MaxEnd =
9839 IsSigned ? APIntOps::smin(getSignedRangeMax(RHS), Limit)
9840 : APIntOps::umin(getUnsignedRangeMax(RHS), Limit);
9841
9842 MaxBECount = computeBECount(getConstant(MaxEnd - MinStart),
9843 getConstant(StrideForMaxBECount), false);
9864 MaxBECount = computeMaxBECount(Start, Stride, RHS,
9865 getTypeSizeInBits(LHS->getType()), IsSigned);
98449866 }
98459867
98469868 if (isa(MaxBECount) &&
287287 exit:
288288 ret i32 0
289289 }
290
291 ; The end bound of the loop can change between iterations, so the exact trip
292 ; count is unknown, but SCEV can calculate the max trip count.
293 define void @changing_end_bound(i32* %n_addr, i32* %addr) {
294 ; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound
295 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
296 ; CHECK: Loop %loop: max backedge-taken count is 2147483646
297 entry:
298 br label %loop
299
300 loop:
301 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
302 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ]
303 %val = load atomic i32, i32* %addr unordered, align 4
304 fence acquire
305 %acc.next = add i32 %acc, %val
306 %iv.next = add nsw i32 %iv, 1
307 %n = load atomic i32, i32* %n_addr unordered, align 4
308 %cmp = icmp slt i32 %iv.next, %n
309 br i1 %cmp, label %loop, label %loop.exit
310
311 loop.exit:
312 ret void
313 }
314
315 ; Similar test as above, but unknown start value.
316 ; Also, there's no nsw on the iv.next, but SCEV knows
317 ; the termination condition is LT, so the IV cannot wrap.
318 define void @changing_end_bound2(i32 %start, i32* %n_addr, i32* %addr) {
319 ; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound2
320 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
321 ; CHECK: Loop %loop: max backedge-taken count is -1
322 entry:
323 br label %loop
324
325 loop:
326 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ]
327 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ]
328 %val = load atomic i32, i32* %addr unordered, align 4
329 fence acquire
330 %acc.next = add i32 %acc, %val
331 %iv.next = add i32 %iv, 1
332 %n = load atomic i32, i32* %n_addr unordered, align 4
333 %cmp = icmp slt i32 %iv.next, %n
334 br i1 %cmp, label %loop, label %loop.exit
335
336 loop.exit:
337 ret void
338 }
339
340 ; changing end bound and greater than one stride
341 define void @changing_end_bound3(i32 %start, i32* %n_addr, i32* %addr) {
342 ; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound3
343 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
344 ; CHECK: Loop %loop: max backedge-taken count is 1073741823
345 entry:
346 br label %loop
347
348 loop:
349 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ]
350 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ]
351 %val = load atomic i32, i32* %addr unordered, align 4
352 fence acquire
353 %acc.next = add i32 %acc, %val
354 %iv.next = add nsw i32 %iv, 4
355 %n = load atomic i32, i32* %n_addr unordered, align 4
356 %cmp = icmp slt i32 %iv.next, %n
357 br i1 %cmp, label %loop, label %loop.exit
358
359 loop.exit:
360 ret void
361 }
362
363 ; same as above test, but the IV can wrap around.
364 ; so the max backedge taken count is unpredictable.
365 define void @changing_end_bound4(i32 %start, i32* %n_addr, i32* %addr) {
366 ; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound4
367 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
368 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
369 entry:
370 br label %loop
371
372 loop:
373 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ]
374 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ]
375 %val = load atomic i32, i32* %addr unordered, align 4
376 fence acquire
377 %acc.next = add i32 %acc, %val
378 %iv.next = add i32 %iv, 4
379 %n = load atomic i32, i32* %n_addr unordered, align 4
380 %cmp = icmp slt i32 %iv.next, %n
381 br i1 %cmp, label %loop, label %loop.exit
382
383 loop.exit:
384 ret void
385 }
386
387 ; unknown stride. Since it's not knownPositive, we do not estimate the max
388 ; backedge taken count.
389 define void @changing_end_bound5(i32 %stride, i32 %start, i32* %n_addr, i32* %addr) {
390 ; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound5
391 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
392 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
393 entry:
394 br label %loop
395
396 loop:
397 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ]
398 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ]
399 %val = load atomic i32, i32* %addr unordered, align 4
400 fence acquire
401 %acc.next = add i32 %acc, %val
402 %iv.next = add nsw i32 %iv, %stride
403 %n = load atomic i32, i32* %n_addr unordered, align 4
404 %cmp = icmp slt i32 %iv.next, %n
405 br i1 %cmp, label %loop, label %loop.exit
406
407 loop.exit:
408 ret void
409 }
410
411 ; negative stride value
412 define void @changing_end_bound6(i32 %start, i32* %n_addr, i32* %addr) {
413 ; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound6
414 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
415 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
416 entry:
417 br label %loop
418
419 loop:
420 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ]
421 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ]
422 %val = load atomic i32, i32* %addr unordered, align 4
423 fence acquire
424 %acc.next = add i32 %acc, %val
425 %iv.next = add nsw i32 %iv, -1
426 %n = load atomic i32, i32* %n_addr unordered, align 4
427 %cmp = icmp slt i32 %iv.next, %n
428 br i1 %cmp, label %loop, label %loop.exit
429
430 loop.exit:
431 ret void
432 }
1212 ; CHECK: %[[PHI:.*]] = phi i32 [ 0, %entry ], [ %{{.*}}, %if.then5 ], [ %[[PHI]], %if.end ]
1313 ; CHECK-LABEL: Determining loop execution counts for: @test
1414 ; CHECK: Loop %for.body18: Unpredictable backedge-taken count.
15 ; CHECK: Loop %for.body18: Unpredictable max backedge-taken count.
15 ; CHECK: Loop %for.body18: max backedge-taken count is 2147483646
1616 ; CHECK: Loop %for.body18: Unpredictable predicated backedge-taken count.
1717 ; CHECK: Loop %for.cond: Unpredictable backedge-taken count.
1818 ; CHECK: Loop %for.cond: Unpredictable max backedge-taken count.
2424 ; CHECK: phi i32 [ %{{.*}}, %if.then5 ], [ 0, %entry ]
2525 ; CHECK-LABEL: Determining loop execution counts for: @test
2626 ; CHECK: Loop %for.body18: Unpredictable backedge-taken count.
27 ; CHECK: Loop %for.body18: Unpredictable max backedge-taken count.
27 ; CHECK: Loop %for.body18: max backedge-taken count is 2147483646
2828 ; CHECK: Loop %for.body18: Unpredictable predicated backedge-taken count.
2929 ; CHECK: Loop %for.cond: Unpredictable backedge-taken count.
3030 ; CHECK: Loop %for.cond: max backedge-taken count is -2147483647