diff options
| author | Roman Lebedev <lebedev.ri@gmail.com> | 2019-10-14 19:46:34 +0000 |
|---|---|---|
| committer | Roman Lebedev <lebedev.ri@gmail.com> | 2019-10-14 19:46:34 +0000 |
| commit | 76e02af70487ba194db42e3720d1975c317a0629 (patch) | |
| tree | 9dddff2bd4573d7d5b726653145a6fffe04ce808 /llvm/lib | |
| parent | b9c55e2760b1be461f05a10ca92696b3381036d2 (diff) | |
| download | bcm5719-llvm-76e02af70487ba194db42e3720d1975c317a0629.tar.gz bcm5719-llvm-76e02af70487ba194db42e3720d1975c317a0629.zip | |
[LoopIdiom] BCmp: loop exit count must not be wider than size_t that `bcmp` takes
As reported by Joerg Sonnenberger in IRC, for 32-bit systems,
where pointer and size_t are 32-bit, if you use 64-bit-wide variable
in the loop, you could end up with loop exit count being of the type
wider than the size_t. Now, i'm not sure if we can produce `bcmp`
from that (just truncate?), but we certainly should not assert/miscompile.
llvm-svn: 374811
Diffstat (limited to 'llvm/lib')
| -rw-r--r-- | llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp index 116bdd7470f..4f5ae5000a9 100644 --- a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp +++ b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp @@ -2080,6 +2080,10 @@ bool LoopIdiomRecognize::recognizeBCmpLoopSCEV(uint64_t BCmpTyBytes, LLVM_DEBUG(dbgs() << "SCEV expressions for loads are acceptable.\n"); + // bcmp / memcmp take length argument as size_t, so let's conservatively + // assume that the iteration count should be not wider than that. + Type *CmpFuncSizeTy = DL->getIntPtrType(SE->getContext()); + // For how many iterations is loop guaranteed not to exit via LoopLatch? // This is one less than the maximal number of comparisons,and is: n + -1 const SCEV *LoopExitCount = @@ -2089,6 +2093,8 @@ bool LoopIdiomRecognize::recognizeBCmpLoopSCEV(uint64_t BCmpTyBytes, // Exit count, similarly, must be loop-invant that dominates the loop header. if (LoopExitCount == SE->getCouldNotCompute() || !LoopExitCount->getType()->isIntOrPtrTy() || + LoopExitCount->getType()->getScalarSizeInBits() > + CmpFuncSizeTy->getScalarSizeInBits() || !SE->isAvailableAtLoopEntry(LoopExitCount, CurLoop)) { LLVM_DEBUG(dbgs() << "Unsupported SCEV expression for loop latch exit.\n"); return false; |

