/// If we can compute the length of the string pointed to by
/// the specified pointer, return 'len+1'. If we can't, return 0.
static uint64_t GetStringLengthH(const Value *V,
- SmallPtrSetImpl<const PHINode *> &PHIs,
- const TargetLibraryInfo *TLI,
+ SmallPtrSetImpl<const PHINode*> &PHIs,
unsigned CharSize) {
// Look through noop bitcast instructions.
V = V->stripPointerCasts();
// If it was new, see if all the input strings are the same length.
uint64_t LenSoFar = ~0ULL;
for (Value *IncValue : PN->incoming_values()) {
- uint64_t Len = GetStringLengthH(IncValue, PHIs, TLI, CharSize);
+ uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
if (Len == 0) return 0; // Unknown length -> unknown.
if (Len == ~0ULL) continue;
// strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
- uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, TLI, CharSize);
+ uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
if (Len1 == 0) return 0;
- uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, TLI, CharSize);
+ uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
if (Len2 == 0) return 0;
if (Len1 == ~0ULL) return Len2;
if (Len2 == ~0ULL) return Len1;
return Len1;
}
- if (auto *CB = dyn_cast<CallBase>(V)) {
- Function *Callee = CB->getCalledFunction();
- if (!Callee)
- return 0;
-
- LibFunc TLIFn;
- if (!TLI || !TLI->getLibFunc(*CB->getCalledFunction(), TLIFn) ||
- !TLI->has(TLIFn))
- return 0;
-
- if (TLIFn == LibFunc_strdup || TLIFn == LibFunc_dunder_strdup)
- return GetStringLengthH(CB->getArgOperand(0), PHIs, TLI, CharSize);
-
- return 0;
- }
-
// Otherwise, see if we can read the string.
ConstantDataArraySlice Slice;
if (!getConstantDataArrayInfo(V, Slice, CharSize))
/// If we can compute the length of the string pointed to by
/// the specified pointer, return 'len+1'. If we can't, return 0.
-uint64_t llvm::GetStringLength(const Value *V, const TargetLibraryInfo *TLI,
- unsigned CharSize) {
+uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
if (!V->getType()->isPointerTy())
return 0;
SmallPtrSet<const PHINode*, 32> PHIs;
- uint64_t Len = GetStringLengthH(V, PHIs, TLI, CharSize);
+ uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
// If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
// an empty string as a length.
return Len == ~0ULL ? 1 : Len;
annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
// See if we can get the length of the input string.
- uint64_t Len = GetStringLength(Src, TLI);
+ uint64_t Len = GetStringLength(Src);
if (Len)
annotateDereferenceableBytes(CI, 1, Len);
else
}
// See if we can get the length of the input string.
- uint64_t SrcLen = GetStringLength(Src, TLI);
+ uint64_t SrcLen = GetStringLength(Src);
if (SrcLen) {
annotateDereferenceableBytes(CI, 1, SrcLen);
--SrcLen; // Unbias length.
// of the input string and turn this into memchr.
ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
if (!CharC) {
- uint64_t Len = GetStringLength(SrcStr, TLI);
+ uint64_t Len = GetStringLength(SrcStr);
if (Len)
annotateDereferenceableBytes(CI, 0, Len);
else
CI->getType());
// strcmp(P, "x") -> memcmp(P, "x", 2)
- uint64_t Len1 = GetStringLength(Str1P, TLI);
+ uint64_t Len1 = GetStringLength(Str1P);
if (Len1)
annotateDereferenceableBytes(CI, 0, Len1);
- uint64_t Len2 = GetStringLength(Str2P, TLI);
+ uint64_t Len2 = GetStringLength(Str2P);
if (Len2)
annotateDereferenceableBytes(CI, 1, Len2);
return B.CreateZExt(B.CreateLoad(B.getInt8Ty(), Str1P, "strcmpload"),
CI->getType());
- uint64_t Len1 = GetStringLength(Str1P, TLI);
+ uint64_t Len1 = GetStringLength(Str1P);
if (Len1)
annotateDereferenceableBytes(CI, 0, Len1);
- uint64_t Len2 = GetStringLength(Str2P, TLI);
+ uint64_t Len2 = GetStringLength(Str2P);
if (Len2)
annotateDereferenceableBytes(CI, 1, Len2);
Value *LibCallSimplifier::optimizeStrNDup(CallInst *CI, IRBuilderBase &B) {
Value *Src = CI->getArgOperand(0);
ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1));
- uint64_t SrcLen = GetStringLength(Src, TLI);
+ uint64_t SrcLen = GetStringLength(Src);
if (SrcLen && Size) {
annotateDereferenceableBytes(CI, 0, SrcLen);
if (SrcLen <= Size->getZExtValue() + 1)
annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
// See if we can get the length of the input string.
- uint64_t Len = GetStringLength(Src, TLI);
+ uint64_t Len = GetStringLength(Src);
if (Len)
annotateDereferenceableBytes(CI, 1, Len);
else
}
// See if we can get the length of the input string.
- uint64_t Len = GetStringLength(Src, TLI);
+ uint64_t Len = GetStringLength(Src);
if (Len)
annotateDereferenceableBytes(CI, 1, Len);
else
return Dst;
// See if we can get the length of the input string.
- uint64_t SrcLen = GetStringLength(Src, TLI);
+ uint64_t SrcLen = GetStringLength(Src);
if (SrcLen) {
annotateDereferenceableBytes(CI, 1, SrcLen);
--SrcLen; // Unbias length.
Value *Src = CI->getArgOperand(0);
// Constant folding: strlen("xyz") -> 3
- if (uint64_t Len = GetStringLength(Src, TLI, CharSize))
+ if (uint64_t Len = GetStringLength(Src, CharSize))
return ConstantInt::get(CI->getType(), Len - 1);
// If s is a constant pointer pointing to a string literal, we can fold
// strlen(x?"foo":"bars") --> x ? 3 : 4
if (SelectInst *SI = dyn_cast<SelectInst>(Src)) {
- uint64_t LenTrue = GetStringLength(SI->getTrueValue(), TLI, CharSize);
- uint64_t LenFalse = GetStringLength(SI->getFalseValue(), TLI, CharSize);
+ uint64_t LenTrue = GetStringLength(SI->getTrueValue(), CharSize);
+ uint64_t LenFalse = GetStringLength(SI->getFalseValue(), CharSize);
if (LenTrue && LenFalse) {
ORE.emit([&]() {
return OptimizationRemark("instcombine", "simplify-libcalls", CI)
// sprintf(dest, "%s", str) -> strcpy(dest, str)
return copyFlags(*CI, emitStrCpy(Dest, CI->getArgOperand(2), B, TLI));
- uint64_t SrcLen = GetStringLength(CI->getArgOperand(2), TLI);
+ uint64_t SrcLen = GetStringLength(CI->getArgOperand(2));
if (SrcLen) {
B.CreateMemCpy(
Dest, Align(1), CI->getArgOperand(2), Align(1),
return nullptr;
// fputs(s,F) --> fwrite(s,strlen(s),1,F)
- uint64_t Len = GetStringLength(CI->getArgOperand(0), TLI);
+ uint64_t Len = GetStringLength(CI->getArgOperand(0));
if (!Len)
return nullptr;
if (OnlyLowerUnknownSize)
return false;
if (StrOp) {
- uint64_t Len = GetStringLength(CI->getArgOperand(*StrOp), TLI);
+ uint64_t Len = GetStringLength(CI->getArgOperand(*StrOp));
// If the length is 0 we don't know how long it is and so we can't
// remove the check.
if (Len)
return nullptr;
// Maybe we can stil fold __st[rp]cpy_chk to __memcpy_chk.
- uint64_t Len = GetStringLength(Src, TLI);
+ uint64_t Len = GetStringLength(Src);
if (Len)
annotateDereferenceableBytes(CI, 1, Len);
else