[PATCH] D62990: [SCEV][WIP] Try fix PR42175
Z. Zheng via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 6 18:16:40 PDT 2019
zzheng created this revision.
zzheng added reviewers: efriedma, sanjoy.
Herald added a subscriber: javed.absar.
Herald added a project: LLVM.
https://bugs.llvm.org/show_bug.cgi?id=42175
$ cat loop-small-runtime-upperbound.ll
target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
@global = dso_local local_unnamed_addr global i32 0, align 4
@global.1 = dso_local local_unnamed_addr global i8* null, align 4
define dso_local void @hoge(i8 %arg) {
entry:
%x = load i32, i32* @global, align 4
%0 = icmp ult i32 %x, 17
br i1 %0, label %loop, label %exit
loop:
%iv = phi i32 [ %x, %entry ], [ %iv.next, %loop ]
%iv.next = add nuw i32 %iv, 8
%1 = load i8*, i8** @global.1, align 4
%2 = getelementptr inbounds i8, i8* %1, i32 1
store i8* %2, i8** @global.1, align 4
store i8 %arg, i8* %1, align 1
%3 = icmp ult i32 %iv.next, 17
br i1 %3, label %loop, label %exit
exit: ; preds = %bb12, %bb
ret void
}
$ opt loop-small-runtime-upperbound.ll -analyze -scalar-evolution
...
The loop runs a max of 3 iters, but SCEV computes max BE-taken count as 3.
The same issue is also found in test/Analysis/ScalarEvolution/2008-11-18-Stride2.ll, where max BE-taken count is 333 instead of 334.
In computeMaxBECountForLT(), when Start is a (C + %x), where C is a constant and %x is an unknown, getUnsignedRangeMin(Start) returns full-set because of %x.
But loop entry is guarded by:
%0 = icmp ult i32 %x, 17
so x is known in [0, 17), thus MinStart shall be C rather than 0.
Repository:
rL LLVM
https://reviews.llvm.org/D62990
Files:
include/llvm/Analysis/ScalarEvolution.h
lib/Analysis/ScalarEvolution.cpp
test/Analysis/ScalarEvolution/2008-11-18-Stride2.ll
Index: test/Analysis/ScalarEvolution/2008-11-18-Stride2.ll
===================================================================
--- test/Analysis/ScalarEvolution/2008-11-18-Stride2.ll
+++ test/Analysis/ScalarEvolution/2008-11-18-Stride2.ll
@@ -1,7 +1,7 @@
; RUN: opt < %s -analyze -scalar-evolution 2>&1 | FileCheck %s
; CHECK: Loop %bb: backedge-taken count is ((999 + (-1 * %x)) /u 3)
-; CHECK: Loop %bb: max backedge-taken count is 334
+; CHECK: Loop %bb: max backedge-taken count is 333
; This is a tricky testcase for unsigned wrap detection which ScalarEvolution
Index: lib/Analysis/ScalarEvolution.cpp
===================================================================
--- lib/Analysis/ScalarEvolution.cpp
+++ lib/Analysis/ScalarEvolution.cpp
@@ -10434,8 +10434,8 @@
const SCEV *Stride,
const SCEV *End,
unsigned BitWidth,
- bool IsSigned) {
-
+ bool IsSigned,
+ const Loop *L) {
assert(!isKnownNonPositive(Stride) &&
"Stride is expected strictly positive!");
// Calculate the maximum backedge count based on the range of values
@@ -10444,6 +10444,12 @@
APInt MinStart =
IsSigned ? getSignedRangeMin(Start) : getUnsignedRangeMin(Start);
+ // If loop entry is guarded by an ULT, we know (Start-Stride) is in [0, End).
+ if (!IsSigned && !isa<SCEVConstant>(Start) && isLoopInvariant(End, L) &&
+ isLoopEntryGuardedByCond(L, ICmpInst::ICMP_ULT,
+ getMinusSCEV(Start, Stride), End))
+ MinStart += getUnsignedRangeMin(Stride);
+
APInt StrideForMaxBECount =
IsSigned ? getSignedRangeMin(Stride) : getUnsignedRangeMin(Stride);
@@ -10567,7 +10573,7 @@
// checked above).
if (!isLoopInvariant(RHS, L)) {
const SCEV *MaxBECount = computeMaxBECountForLT(
- Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
+ Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned, L);
return ExitLimit(getCouldNotCompute() /* ExactNotTaken */, MaxBECount,
false /*MaxOrZero*/, Predicates);
}
@@ -10604,7 +10610,7 @@
MaxOrZero = true;
} else {
MaxBECount = computeMaxBECountForLT(
- Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
+ Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned, L);
}
if (isa<SCEVCouldNotCompute>(MaxBECount) &&
Index: include/llvm/Analysis/ScalarEvolution.h
===================================================================
--- include/llvm/Analysis/ScalarEvolution.h
+++ include/llvm/Analysis/ScalarEvolution.h
@@ -1838,7 +1838,7 @@
/// assert these preconditions so please be careful.
const SCEV *computeMaxBECountForLT(const SCEV *Start, const SCEV *Stride,
const SCEV *End, unsigned BitWidth,
- bool IsSigned);
+ bool IsSigned, const Loop *L);
/// Verify if an linear IV with positive stride can overflow when in a
/// less-than comparison, knowing the invariant term of the comparison,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D62990.203473.patch
Type: text/x-patch
Size: 3346 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190607/293c5d16/attachment.bin>
More information about the llvm-commits
mailing list