diff options
author | Sergei Barannikov <barannikov88@gmail.com> | 2024-08-27 00:11:23 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-27 00:11:23 +0300 |
commit | 7134d2e9ac36c833bb9ed90363def74fd84f7d13 (patch) | |
tree | 8b5f650d8a6d9dcb7ba5812bea4022312a7033ae /llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | |
parent | 824cffe152046a24ec660f524a27124fefbbad15 (diff) | |
download | llvm-7134d2e9ac36c833bb9ed90363def74fd84f7d13.zip llvm-7134d2e9ac36c833bb9ed90363def74fd84f7d13.tar.gz llvm-7134d2e9ac36c833bb9ed90363def74fd84f7d13.tar.bz2 |
[SimplifyLibCalls] Fix memchr misoptimization (#106121)
The `ch` argument of memcmp should be truncated to `unsigned char`
before using it in comparisons. This didn't happen on all code paths.
The following program miscompiled at -O1 and higher:
```C++
#include <cstring>
#include <iostream>
char ch = '\x81';
int main() {
bool found = std::strchr("\x80\x81\x82", ch) != nullptr;
std::cout << std::boolalpha << found << '\n';
}
```
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp index fb2efe5..1e6dc88 100644 --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -1454,10 +1454,12 @@ Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilderBase &B) { if (NonContRanges > 2) return nullptr; + // Slice off the character's high end bits. + CharVal = B.CreateTrunc(CharVal, B.getInt8Ty()); + SmallVector<Value *> CharCompares; for (unsigned char C : SortedStr) - CharCompares.push_back( - B.CreateICmpEQ(CharVal, ConstantInt::get(CharVal->getType(), C))); + CharCompares.push_back(B.CreateICmpEQ(CharVal, B.getInt8(C))); return B.CreateIntToPtr(B.CreateOr(CharCompares), CI->getType()); } |