diff --git a/llvm/include/llvm/Analysis/TargetLibraryInfo.h b/llvm/include/llvm/Analysis/TargetLibraryInfo.h index cc30e1322c2b..6e3e1380535e 100644 --- a/llvm/include/llvm/Analysis/TargetLibraryInfo.h +++ b/llvm/include/llvm/Analysis/TargetLibraryInfo.h @@ -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. diff --git a/llvm/lib/Analysis/TargetLibraryInfo.cpp b/llvm/lib/Analysis/TargetLibraryInfo.cpp index 530e039579ef..75149dd36fc4 100644 --- a/llvm/lib/Analysis/TargetLibraryInfo.cpp +++ b/llvm/lib/Analysis/TargetLibraryInfo.cpp @@ -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) { @@ -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: @@ -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: @@ -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: @@ -875,20 +872,20 @@ 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: @@ -896,7 +893,7 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy, 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() && @@ -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: @@ -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); @@ -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: @@ -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: @@ -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: @@ -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: @@ -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() && @@ -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: @@ -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() && @@ -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() { diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp index 0a00357ad597..652da0aff325 100644 --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -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