Skip to content

Commit

Permalink
[TargetLibraryInfo] Refactor size_t checks in isValidProtoForLibFunc.…
Browse files Browse the repository at this point in the history
… NFC

In TargetLibraryInfoImpl::isValidProtoForLibFunc we no longer
need the IsSizeTTy lambda function and the SizeTTy object. Instead
we just follow the regular structure of checking for integer types
given an exepected number of bits.
  • Loading branch information
bjope committed Oct 4, 2021
1 parent f074a6a commit 7f84fa4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/Analysis/TargetLibraryInfo.h
Expand Up @@ -76,7 +76,7 @@ class TargetLibraryInfoImpl {
/// Return true if the function type FTy is valid for the library function
/// F, regardless of whether the function is available.
bool isValidProtoForLibFunc(const FunctionType &FTy, LibFunc F,
const DataLayout &DL) const;
const Module &M) const;

public:
/// List of known vector-functions libraries.
Expand Down
73 changes: 36 additions & 37 deletions llvm/lib/Analysis/TargetLibraryInfo.cpp
Expand Up @@ -784,15 +784,12 @@ bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName, LibFunc &F) const {

bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
LibFunc F,
const DataLayout &DL) const {
LLVMContext &Ctx = FTy.getContext();
const Module &M) const {
// FIXME: There is really no guarantee that sizeof(size_t) is equal to
// sizeof(int*) for every target. So the assumption used here to derive the
// SizeTTy based on DataLayout and getIntPtrType isn't always valid.
Type *SizeTTy = DL.getIntPtrType(Ctx, /*AddressSpace=*/0);
auto IsSizeTTy = [SizeTTy](Type *Ty) {
return Ty == SizeTTy;
};
// SizeTBits based on the size of an integer pointer in address space zero
// isn't always valid.
unsigned SizeTBits = M.getDataLayout().getPointerSizeInBits(/*AddrSpace=*/0);
unsigned NumParams = FTy.getNumParams();

switch (F) {
Expand All @@ -816,12 +813,12 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
FTy.getReturnType()->isIntegerTy(32));
case LibFunc_strlen_chk:
--NumParams;
if (!IsSizeTTy(FTy.getParamType(NumParams)))
if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
return false;
LLVM_FALLTHROUGH;
case LibFunc_strlen:
return NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
IsSizeTTy(FTy.getReturnType());
FTy.getReturnType()->isIntegerTy(SizeTBits);

case LibFunc_strchr:
case LibFunc_strrchr:
Expand All @@ -841,7 +838,7 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
FTy.getParamType(1)->isPointerTy());
case LibFunc_strcat_chk:
--NumParams;
if (!IsSizeTTy(FTy.getParamType(NumParams)))
if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
return false;
LLVM_FALLTHROUGH;
case LibFunc_strcat:
Expand All @@ -851,19 +848,19 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,

case LibFunc_strncat_chk:
--NumParams;
if (!IsSizeTTy(FTy.getParamType(NumParams)))
if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
return false;
LLVM_FALLTHROUGH;
case LibFunc_strncat:
return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
FTy.getParamType(0) == FTy.getReturnType() &&
FTy.getParamType(1) == FTy.getReturnType() &&
IsSizeTTy(FTy.getParamType(2)));
FTy.getParamType(2)->isIntegerTy(SizeTBits));

case LibFunc_strcpy_chk:
case LibFunc_stpcpy_chk:
--NumParams;
if (!IsSizeTTy(FTy.getParamType(NumParams)))
if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
return false;
LLVM_FALLTHROUGH;
case LibFunc_strcpy:
Expand All @@ -875,28 +872,28 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
case LibFunc_strlcat_chk:
case LibFunc_strlcpy_chk:
--NumParams;
if (!IsSizeTTy(FTy.getParamType(NumParams)))
if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
return false;
LLVM_FALLTHROUGH;
case LibFunc_strlcat:
case LibFunc_strlcpy:
return NumParams == 3 && IsSizeTTy(FTy.getReturnType()) &&
return NumParams == 3 && FTy.getReturnType()->isIntegerTy(SizeTBits) &&
FTy.getParamType(0)->isPointerTy() &&
FTy.getParamType(1)->isPointerTy() &&
IsSizeTTy(FTy.getParamType(2));
FTy.getParamType(2)->isIntegerTy(SizeTBits);

case LibFunc_strncpy_chk:
case LibFunc_stpncpy_chk:
--NumParams;
if (!IsSizeTTy(FTy.getParamType(NumParams)))
if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
return false;
LLVM_FALLTHROUGH;
case LibFunc_strncpy:
case LibFunc_stpncpy:
return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
FTy.getParamType(0) == FTy.getParamType(1) &&
FTy.getParamType(0)->isPointerTy() &&
IsSizeTTy(FTy.getParamType(2)));
FTy.getParamType(2)->isIntegerTy(SizeTBits));

case LibFunc_strxfrm:
return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
Expand All @@ -911,7 +908,7 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
FTy.getParamType(0)->isPointerTy() &&
FTy.getParamType(0) == FTy.getParamType(1) &&
IsSizeTTy(FTy.getParamType(2)));
FTy.getParamType(2)->isIntegerTy(SizeTBits));

case LibFunc_strspn:
case LibFunc_strcspn:
Expand Down Expand Up @@ -959,21 +956,21 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
case LibFunc_sprintf_chk:
return NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
FTy.getParamType(1)->isIntegerTy(32) &&
IsSizeTTy(FTy.getParamType(2)) &&
FTy.getParamType(2)->isIntegerTy(SizeTBits) &&
FTy.getParamType(3)->isPointerTy() &&
FTy.getReturnType()->isIntegerTy(32);

case LibFunc_snprintf:
return NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
IsSizeTTy(FTy.getParamType(1)) &&
FTy.getParamType(1)->isIntegerTy(SizeTBits) &&
FTy.getParamType(2)->isPointerTy() &&
FTy.getReturnType()->isIntegerTy(32);

case LibFunc_snprintf_chk:
return NumParams == 5 && FTy.getParamType(0)->isPointerTy() &&
IsSizeTTy(FTy.getParamType(1)) &&
FTy.getParamType(1)->isIntegerTy(SizeTBits) &&
FTy.getParamType(2)->isIntegerTy(32) &&
IsSizeTTy(FTy.getParamType(3)) &&
FTy.getParamType(3)->isIntegerTy(SizeTBits) &&
FTy.getParamType(4)->isPointerTy() &&
FTy.getReturnType()->isIntegerTy(32);

Expand All @@ -989,14 +986,15 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
case LibFunc_memcmp:
return NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
FTy.getParamType(0)->isPointerTy() &&
FTy.getParamType(1)->isPointerTy() && IsSizeTTy(FTy.getParamType(2));
FTy.getParamType(1)->isPointerTy() &&
FTy.getParamType(2)->isIntegerTy(SizeTBits);

case LibFunc_memchr:
case LibFunc_memrchr:
return (NumParams == 3 && FTy.getReturnType()->isPointerTy() &&
FTy.getReturnType() == FTy.getParamType(0) &&
FTy.getParamType(1)->isIntegerTy(32) &&
IsSizeTTy(FTy.getParamType(2)));
FTy.getParamType(2)->isIntegerTy(SizeTBits));
case LibFunc_modf:
case LibFunc_modff:
case LibFunc_modfl:
Expand All @@ -1006,7 +1004,7 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
case LibFunc_mempcpy_chk:
case LibFunc_memmove_chk:
--NumParams;
if (!IsSizeTTy(FTy.getParamType(NumParams)))
if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
return false;
LLVM_FALLTHROUGH;
case LibFunc_memcpy:
Expand All @@ -1015,22 +1013,22 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
FTy.getParamType(0)->isPointerTy() &&
FTy.getParamType(1)->isPointerTy() &&
IsSizeTTy(FTy.getParamType(2)));
FTy.getParamType(2)->isIntegerTy(SizeTBits));

case LibFunc_memset_chk:
--NumParams;
if (!IsSizeTTy(FTy.getParamType(NumParams)))
if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
return false;
LLVM_FALLTHROUGH;
case LibFunc_memset:
return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) &&
FTy.getParamType(0)->isPointerTy() &&
FTy.getParamType(1)->isIntegerTy() &&
IsSizeTTy(FTy.getParamType(2)));
FTy.getParamType(2)->isIntegerTy(SizeTBits));

case LibFunc_memccpy_chk:
--NumParams;
if (!IsSizeTTy(FTy.getParamType(NumParams)))
if (!FTy.getParamType(NumParams)->isIntegerTy(SizeTBits))
return false;
LLVM_FALLTHROUGH;
case LibFunc_memccpy:
Expand All @@ -1042,7 +1040,7 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
case LibFunc_vec_realloc:
return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
FTy.getParamType(0) == FTy.getReturnType() &&
IsSizeTTy(FTy.getParamType(1)));
FTy.getParamType(1)->isIntegerTy(SizeTBits));
case LibFunc_read:
return (NumParams == 3 && FTy.getParamType(1)->isPointerTy());
case LibFunc_rewind:
Expand Down Expand Up @@ -1123,7 +1121,7 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
return (NumParams != 0 && FTy.getParamType(0)->isPointerTy());
case LibFunc___kmpc_free_shared:
return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() &&
IsSizeTTy(FTy.getParamType(1)));
FTy.getParamType(1)->isIntegerTy(SizeTBits));

case LibFunc_fopen:
return (NumParams == 2 && FTy.getReturnType()->isPointerTy() &&
Expand Down Expand Up @@ -1213,14 +1211,14 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
case LibFunc_vsprintf_chk:
return NumParams == 5 && FTy.getParamType(0)->isPointerTy() &&
FTy.getParamType(1)->isIntegerTy(32) &&
IsSizeTTy(FTy.getParamType(2)) && FTy.getParamType(3)->isPointerTy();
FTy.getParamType(2)->isIntegerTy(SizeTBits) && FTy.getParamType(3)->isPointerTy();
case LibFunc_vsnprintf:
return (NumParams == 4 && FTy.getParamType(0)->isPointerTy() &&
FTy.getParamType(2)->isPointerTy());
case LibFunc_vsnprintf_chk:
return NumParams == 6 && FTy.getParamType(0)->isPointerTy() &&
FTy.getParamType(2)->isIntegerTy(32) &&
IsSizeTTy(FTy.getParamType(3)) && FTy.getParamType(4)->isPointerTy();
FTy.getParamType(3)->isIntegerTy(SizeTBits) && FTy.getParamType(4)->isPointerTy();
case LibFunc_open:
return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy());
case LibFunc_opendir:
Expand Down Expand Up @@ -1632,12 +1630,13 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
case LibFunc_strnlen:
return (NumParams == 2 && FTy.getReturnType() == FTy.getParamType(1) &&
FTy.getParamType(0)->isPointerTy() &&
IsSizeTTy(FTy.getParamType(1)));
FTy.getParamType(1)->isIntegerTy(SizeTBits));

case LibFunc_posix_memalign:
return (NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
FTy.getParamType(0)->isPointerTy() &&
IsSizeTTy(FTy.getParamType(1)) && IsSizeTTy(FTy.getParamType(2)));
FTy.getParamType(1)->isIntegerTy(SizeTBits) &&
FTy.getParamType(2)->isIntegerTy(SizeTBits));

case LibFunc_wcslen:
return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() &&
Expand Down Expand Up @@ -1681,7 +1680,7 @@ bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
assert(M && "Expecting FDecl to be connected to a Module.");

return getLibFunc(FDecl.getName(), F) &&
isValidProtoForLibFunc(*FDecl.getFunctionType(), F, M->getDataLayout());
isValidProtoForLibFunc(*FDecl.getFunctionType(), F, *M);
}

void TargetLibraryInfoImpl::disableAllFunctions() {
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
Expand Up @@ -3292,7 +3292,11 @@ Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
else
return nullptr;

Type *SizeTTy = DL.getIntPtrType(CI->getContext());
// FIXME: There is really no guarantee that sizeof(size_t) is equal to
// sizeof(int*) for every target. So the assumption used here to derive the
// SizeTBits based on the size of an integer pointer in address space zero
// isn't always valid.
Type *SizeTTy = DL.getIntPtrType(CI->getContext(), /*AddressSpace=*/0);
Value *LenV = ConstantInt::get(SizeTTy, Len);
Value *Ret = emitMemCpyChk(Dst, Src, LenV, ObjSize, B, DL, TLI);
// If the function was an __stpcpy_chk, and we were able to fold it into
Expand Down

0 comments on commit 7f84fa4

Please sign in to comment.