From: Bjorn Pettersson Date: Wed, 28 Sep 2022 20:18:59 +0000 (+0200) Subject: [BuildLibCalls] Name types to identify when 'int' and 'size_t' is assumed. NFC X-Git-Tag: upstream/17.0.6~31676 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=73e8d95d28f8afa6f45cd0830641f62025584c07;p=platform%2Fupstream%2Fllvm.git [BuildLibCalls] Name types to identify when 'int' and 'size_t' is assumed. NFC Lots of BuildLibCalls helpers are using Builder::getInt32Ty to get a type matching an 'int', and DataLayout::getIntPtrType to get a type matching 'size_t'. The former is not true for all targets, since and 'int' isn't always 32 bits. And the latter is a bit weird as well as the definition of DataLayout::getIntPtrType isn't clearly mapping it to 'size_t'. This patch is not aiming at solving any such problems. It is merely highlighting when a libcall is expecting to use 'int' and 'size_t' by naming the types as IntTy and SizeTTy when preparing the type signatures for the emitted libcalls. Differential Revision: https://reviews.llvm.org/D135064 --- diff --git a/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h b/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h index 6ea195c..ae287c0 100644 --- a/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h +++ b/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h @@ -85,7 +85,7 @@ namespace llvm { /// Emit a call to the strlen function to the builder, for the specified /// pointer. Ptr is required to be some pointer type, and the return value has - /// 'intptr_t' type. + /// 'size_t' type. Value *emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI); @@ -125,7 +125,7 @@ namespace llvm { const TargetLibraryInfo *TLI); /// Emit a call to the __memcpy_chk function to the builder. This expects that - /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers. + /// the Len and ObjSize have type 'size_t' and Dst/Src are pointers. Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI); @@ -135,7 +135,7 @@ namespace llvm { const DataLayout &DL, const TargetLibraryInfo *TLI); /// Emit a call to the memchr function. This assumes that Ptr is a pointer, - /// Val is an i32 value, and Len is an 'intptr_t' value. + /// Val is an 'int' value, and Len is an 'size_t' value. Value *emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI); @@ -219,15 +219,15 @@ namespace llvm { LibFunc FloatFn, LibFunc LongDoubleFn, IRBuilderBase &B, const AttributeList &Attrs); - /// Emit a call to the putchar function. This assumes that Char is an integer. + /// Emit a call to the putchar function. This assumes that Char is an 'int'. Value *emitPutChar(Value *Char, IRBuilderBase &B, const TargetLibraryInfo *TLI); /// Emit a call to the puts function. This assumes that Str is some pointer. Value *emitPutS(Value *Str, IRBuilderBase &B, const TargetLibraryInfo *TLI); - /// Emit a call to the fputc function. This assumes that Char is an i32, and - /// File is a pointer to FILE. + /// Emit a call to the fputc function. This assumes that Char can be casted to + /// int (currently assuming int is i32), and File is a pointer to FILE. Value *emitFPutC(Value *Char, Value *File, IRBuilderBase &B, const TargetLibraryInfo *TLI); diff --git a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp index b15e14b..80e882d5 100644 --- a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp @@ -1444,7 +1444,8 @@ static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType, Value *llvm::emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { LLVMContext &Context = B.GetInsertBlock()->getContext(); - return emitLibCall(LibFunc_strlen, DL.getIntPtrType(Context), + Type *SizeTTy = DL.getIntPtrType(Context); + return emitLibCall(LibFunc_strlen, SizeTTy, B.getInt8PtrTy(), castToCStr(Ptr, B), B, TLI); } @@ -1457,17 +1458,19 @@ Value *llvm::emitStrDup(Value *Ptr, IRBuilderBase &B, Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilderBase &B, const TargetLibraryInfo *TLI) { Type *I8Ptr = B.getInt8PtrTy(); - Type *I32Ty = B.getInt32Ty(); - return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, I32Ty}, - {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, B, TLI); + Type *IntTy = B.getInt32Ty(); + return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, IntTy}, + {castToCStr(Ptr, B), ConstantInt::get(IntTy, C)}, B, TLI); } Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { LLVMContext &Context = B.GetInsertBlock()->getContext(); + Type *SizeTTy = DL.getIntPtrType(Context); + Type *IntTy = B.getInt32Ty(); return emitLibCall( - LibFunc_strncmp, B.getInt32Ty(), - {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, + LibFunc_strncmp, IntTy, + {B.getInt8PtrTy(), B.getInt8PtrTy(), SizeTTy}, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); } @@ -1488,14 +1491,16 @@ Value *llvm::emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B, Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI) { Type *I8Ptr = B.getInt8PtrTy(); - return emitLibCall(LibFunc_strncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()}, + Type *SizeTTy = Len->getType(); + return emitLibCall(LibFunc_strncpy, I8Ptr, {I8Ptr, I8Ptr, SizeTTy}, {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI); } Value *llvm::emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI) { Type *I8Ptr = B.getInt8PtrTy(); - return emitLibCall(LibFunc_stpncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()}, + Type *SizeTTy = Len->getType(); + return emitLibCall(LibFunc_stpncpy, I8Ptr, {I8Ptr, I8Ptr, SizeTTy}, {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI); } @@ -1510,10 +1515,11 @@ Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex, Attribute::NoUnwind); LLVMContext &Context = B.GetInsertBlock()->getContext(); + Type *I8Ptr = B.getInt8PtrTy(); + Type *SizeTTy = DL.getIntPtrType(Context); FunctionCallee MemCpy = getOrInsertLibFunc(M, *TLI, LibFunc_memcpy_chk, - AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(), - B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), - DL.getIntPtrType(Context)); + AttributeList::get(M->getContext(), AS), I8Ptr, + I8Ptr, I8Ptr, SizeTTy, SizeTTy); Dst = castToCStr(Dst, B); Src = castToCStr(Src, B); CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize}); @@ -1526,73 +1532,92 @@ Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, Value *llvm::emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { LLVMContext &Context = B.GetInsertBlock()->getContext(); - return emitLibCall( - LibFunc_mempcpy, B.getInt8PtrTy(), - {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, - {Dst, Src, Len}, B, TLI); + Type *I8Ptr = B.getInt8PtrTy(); + Type *SizeTTy = DL.getIntPtrType(Context); + return emitLibCall(LibFunc_mempcpy, I8Ptr, + {I8Ptr, I8Ptr, SizeTTy}, + {Dst, Src, Len}, B, TLI); } Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { LLVMContext &Context = B.GetInsertBlock()->getContext(); - return emitLibCall( - LibFunc_memchr, B.getInt8PtrTy(), - {B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context)}, - {castToCStr(Ptr, B), Val, Len}, B, TLI); + Type *I8Ptr = B.getInt8PtrTy(); + Type *IntTy = B.getInt32Ty(); + Type *SizeTTy = DL.getIntPtrType(Context); + return emitLibCall(LibFunc_memchr, I8Ptr, + {I8Ptr, IntTy, SizeTTy}, + {castToCStr(Ptr, B), Val, Len}, B, TLI); } Value *llvm::emitMemRChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { LLVMContext &Context = B.GetInsertBlock()->getContext(); - return emitLibCall( - LibFunc_memrchr, B.getInt8PtrTy(), - {B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context)}, - {castToCStr(Ptr, B), Val, Len}, B, TLI); + Type *I8Ptr = B.getInt8PtrTy(); + Type *IntTy = B.getInt32Ty(); + Type *SizeTTy = DL.getIntPtrType(Context); + return emitLibCall(LibFunc_memrchr, I8Ptr, + {I8Ptr, IntTy, SizeTTy}, + {castToCStr(Ptr, B), Val, Len}, B, TLI); } Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { LLVMContext &Context = B.GetInsertBlock()->getContext(); + Type *I8Ptr = B.getInt8PtrTy(); + Type *IntTy = B.getInt32Ty(); + Type *SizeTTy = DL.getIntPtrType(Context); return emitLibCall( - LibFunc_memcmp, B.getInt32Ty(), - {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, + LibFunc_memcmp, IntTy, + {I8Ptr, I8Ptr, SizeTTy}, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); } Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { LLVMContext &Context = B.GetInsertBlock()->getContext(); + Type *I8Ptr = B.getInt8PtrTy(); + Type *IntTy = B.getInt32Ty(); + Type *SizeTTy = DL.getIntPtrType(Context); return emitLibCall( - LibFunc_bcmp, B.getInt32Ty(), - {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, + LibFunc_bcmp, IntTy, + {I8Ptr, I8Ptr, SizeTTy}, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); } Value *llvm::emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI) { + Type *I8Ptr = B.getInt8PtrTy(); + Type *IntTy = B.getInt32Ty(); + Type *SizeTTy = Len->getType(); return emitLibCall( - LibFunc_memccpy, B.getInt8PtrTy(), - {B.getInt8PtrTy(), B.getInt8PtrTy(), B.getInt32Ty(), Len->getType()}, + LibFunc_memccpy, I8Ptr, + {I8Ptr, I8Ptr, IntTy, SizeTTy}, {Ptr1, Ptr2, Val, Len}, B, TLI); } Value *llvm::emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, ArrayRef VariadicArgs, IRBuilderBase &B, const TargetLibraryInfo *TLI) { + Type *I8Ptr = B.getInt8PtrTy(); + Type *IntTy = B.getInt32Ty(); + Type *SizeTTy = Size->getType(); SmallVector Args{castToCStr(Dest, B), Size, castToCStr(Fmt, B)}; llvm::append_range(Args, VariadicArgs); - return emitLibCall(LibFunc_snprintf, B.getInt32Ty(), - {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy()}, + return emitLibCall(LibFunc_snprintf, IntTy, + {I8Ptr, SizeTTy, I8Ptr}, Args, B, TLI, /*IsVaArgs=*/true); } Value *llvm::emitSPrintf(Value *Dest, Value *Fmt, ArrayRef VariadicArgs, IRBuilderBase &B, const TargetLibraryInfo *TLI) { + Type *I8Ptr = B.getInt8PtrTy(); + Type *IntTy = B.getInt32Ty(); SmallVector Args{castToCStr(Dest, B), castToCStr(Fmt, B)}; llvm::append_range(Args, VariadicArgs); - return emitLibCall(LibFunc_sprintf, B.getInt32Ty(), - {B.getInt8PtrTy(), B.getInt8PtrTy()}, Args, B, TLI, + return emitLibCall(LibFunc_sprintf, IntTy, + {I8Ptr, I8Ptr}, Args, B, TLI, /*IsVaArgs=*/true); } @@ -1605,37 +1630,48 @@ Value *llvm::emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B, Value *llvm::emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - return emitLibCall(LibFunc_strlcpy, Size->getType(), - {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()}, + Type *I8Ptr = B.getInt8PtrTy(); + Type *SizeTTy = Size->getType(); + return emitLibCall(LibFunc_strlcpy, SizeTTy, + {I8Ptr, I8Ptr, SizeTTy}, {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); } Value *llvm::emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - return emitLibCall(LibFunc_strlcat, Size->getType(), - {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()}, + Type *I8Ptr = B.getInt8PtrTy(); + Type *SizeTTy = Size->getType(); + return emitLibCall(LibFunc_strlcat, SizeTTy, + {I8Ptr, I8Ptr, SizeTTy}, {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); } Value *llvm::emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - return emitLibCall(LibFunc_strncat, B.getInt8PtrTy(), - {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()}, + Type *I8Ptr = B.getInt8PtrTy(); + Type *SizeTTy = Size->getType(); + return emitLibCall(LibFunc_strncat, I8Ptr, + {I8Ptr, I8Ptr, SizeTTy}, {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); } Value *llvm::emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, IRBuilderBase &B, const TargetLibraryInfo *TLI) { + Type *I8Ptr = B.getInt8PtrTy(); + Type *IntTy = B.getInt32Ty(); + Type *SizeTTy = Size->getType(); return emitLibCall( - LibFunc_vsnprintf, B.getInt32Ty(), - {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy(), VAList->getType()}, + LibFunc_vsnprintf, IntTy, + {I8Ptr, SizeTTy, I8Ptr, VAList->getType()}, {castToCStr(Dest, B), Size, castToCStr(Fmt, B), VAList}, B, TLI); } Value *llvm::emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - return emitLibCall(LibFunc_vsprintf, B.getInt32Ty(), - {B.getInt8PtrTy(), B.getInt8PtrTy(), VAList->getType()}, + Type *I8Ptr = B.getInt8PtrTy(); + Type *IntTy = B.getInt32Ty(); + return emitLibCall(LibFunc_vsprintf, IntTy, + {I8Ptr, I8Ptr, VAList->getType()}, {castToCStr(Dest, B), castToCStr(Fmt, B), VAList}, B, TLI); } @@ -1764,9 +1800,10 @@ Value *llvm::emitPutChar(Value *Char, IRBuilderBase &B, if (!isLibFuncEmittable(M, TLI, LibFunc_putchar)) return nullptr; - Type *Ty = Char->getType(); + Type *IntTy = Char->getType(); StringRef PutCharName = TLI->getName(LibFunc_putchar); - FunctionCallee PutChar = getOrInsertLibFunc(M, *TLI, LibFunc_putchar, Ty, Ty); + FunctionCallee PutChar = getOrInsertLibFunc(M, *TLI, LibFunc_putchar, + IntTy, IntTy); inferNonMandatoryLibFuncAttrs(M, PutCharName, *TLI); CallInst *CI = B.CreateCall(PutChar, Char, PutCharName); @@ -1782,8 +1819,9 @@ Value *llvm::emitPutS(Value *Str, IRBuilderBase &B, if (!isLibFuncEmittable(M, TLI, LibFunc_puts)) return nullptr; + Type *IntTy = B.getInt32Ty(); StringRef PutsName = TLI->getName(LibFunc_puts); - FunctionCallee PutS = getOrInsertLibFunc(M, *TLI, LibFunc_puts, B.getInt32Ty(), + FunctionCallee PutS = getOrInsertLibFunc(M, *TLI, LibFunc_puts, IntTy, B.getInt8PtrTy()); inferNonMandatoryLibFuncAttrs(M, PutsName, *TLI); CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName); @@ -1799,12 +1837,13 @@ Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilderBase &B, if (!isLibFuncEmittable(M, TLI, LibFunc_fputc)) return nullptr; + Type *IntTy = B.getInt32Ty(); StringRef FPutcName = TLI->getName(LibFunc_fputc); - FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fputc, B.getInt32Ty(), - B.getInt32Ty(), File->getType()); + FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fputc, IntTy, + IntTy, File->getType()); if (File->getType()->isPointerTy()) inferNonMandatoryLibFuncAttrs(M, FPutcName, *TLI); - Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true, + Char = B.CreateIntCast(Char, IntTy, /*isSigned*/true, "chari"); CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName); @@ -1820,8 +1859,9 @@ Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilderBase &B, if (!isLibFuncEmittable(M, TLI, LibFunc_fputs)) return nullptr; + Type *IntTy = B.getInt32Ty(); StringRef FPutsName = TLI->getName(LibFunc_fputs); - FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fputs, B.getInt32Ty(), + FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fputs, IntTy, B.getInt8PtrTy(), File->getType()); if (File->getType()->isPointerTy()) inferNonMandatoryLibFuncAttrs(M, FPutsName, *TLI); @@ -1840,10 +1880,11 @@ Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B, return nullptr; LLVMContext &Context = B.GetInsertBlock()->getContext(); + Type *SizeTTy = DL.getIntPtrType(Context); StringRef FWriteName = TLI->getName(LibFunc_fwrite); FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fwrite, - DL.getIntPtrType(Context), B.getInt8PtrTy(), DL.getIntPtrType(Context), - DL.getIntPtrType(Context), File->getType()); + SizeTTy, B.getInt8PtrTy(), SizeTTy, + SizeTTy, File->getType()); if (File->getType()->isPointerTy()) inferNonMandatoryLibFuncAttrs(M, FWriteName, *TLI); @@ -1865,8 +1906,9 @@ Value *llvm::emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL, StringRef MallocName = TLI->getName(LibFunc_malloc); LLVMContext &Context = B.GetInsertBlock()->getContext(); + Type *SizeTTy = DL.getIntPtrType(Context); FunctionCallee Malloc = getOrInsertLibFunc(M, *TLI, LibFunc_malloc, - B.getInt8PtrTy(), DL.getIntPtrType(Context)); + B.getInt8PtrTy(), SizeTTy); inferNonMandatoryLibFuncAttrs(M, MallocName, *TLI); CallInst *CI = B.CreateCall(Malloc, Num, MallocName); @@ -1884,10 +1926,11 @@ Value *llvm::emitCalloc(Value *Num, Value *Size, IRBuilderBase &B, return nullptr; StringRef CallocName = TLI.getName(LibFunc_calloc); + LLVMContext &Context = B.GetInsertBlock()->getContext(); const DataLayout &DL = M->getDataLayout(); - IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext())); + Type *SizeTTy = DL.getIntPtrType(Context); FunctionCallee Calloc = getOrInsertLibFunc(M, TLI, LibFunc_calloc, - B.getInt8PtrTy(), PtrType, PtrType); + B.getInt8PtrTy(), SizeTTy, SizeTTy); inferNonMandatoryLibFuncAttrs(M, CallocName, TLI); CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);