From cfa582e8aaa791b52110791f5e6504121aaf62bf Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Fri, 6 Dec 2024 10:51:14 +1300 Subject: [PATCH] SimplifyLibCalls: Use default globals address space when building new global strings. (#118729) Writing a test for this transitively exposed a number of places in BuildLibCalls where we were failing to propagate address spaces properly, which are additionally fixed. --- .../llvm/Transforms/Utils/BuildLibCalls.h | 32 ++--- .../Scalar/DeadStoreElimination.cpp | 5 +- llvm/lib/Transforms/Utils/BuildLibCalls.cpp | 129 ++++++++++-------- .../lib/Transforms/Utils/SimplifyLibCalls.cpp | 80 ++++++----- .../InstCombine/printf-addrspace.ll | 35 +++++ 5 files changed, 170 insertions(+), 111 deletions(-) create mode 100644 llvm/test/Transforms/InstCombine/printf-addrspace.ll diff --git a/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h b/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h index a8fb38e7260043..0508f92dfd2ff5 100644 --- a/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h +++ b/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h @@ -246,32 +246,32 @@ namespace llvm { const DataLayout &DL, const TargetLibraryInfo *TLI); /// Emit a call to the malloc function. - Value *emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL, - const TargetLibraryInfo *TLI); + Value *emitMalloc(Type *RetTy, Value *Num, IRBuilderBase &B, + const DataLayout &DL, const TargetLibraryInfo *TLI); /// Emit a call to the calloc function. - Value *emitCalloc(Value *Num, Value *Size, IRBuilderBase &B, - const TargetLibraryInfo &TLI, unsigned AddrSpace); + Value *emitCalloc(Type *RetTy, Value *Num, Value *Size, IRBuilderBase &B, + const TargetLibraryInfo &TLI); /// Emit a call to the hot/cold operator new function. - Value *emitHotColdNew(Value *Num, IRBuilderBase &B, + Value *emitHotColdNew(Type *RetTy, Value *Num, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold); - Value *emitHotColdNewNoThrow(Value *Num, Value *NoThrow, IRBuilderBase &B, - const TargetLibraryInfo *TLI, LibFunc NewFunc, - uint8_t HotCold); - Value *emitHotColdNewAligned(Value *Num, Value *Align, IRBuilderBase &B, - const TargetLibraryInfo *TLI, LibFunc NewFunc, - uint8_t HotCold); - Value *emitHotColdNewAlignedNoThrow(Value *Num, Value *Align, Value *NoThrow, - IRBuilderBase &B, + Value *emitHotColdNewNoThrow(Type *RetTy, Value *Num, Value *NoThrow, + IRBuilderBase &B, const TargetLibraryInfo *TLI, + LibFunc NewFunc, uint8_t HotCold); + Value *emitHotColdNewAligned(Type *RetTy, Value *Num, Value *Align, + IRBuilderBase &B, const TargetLibraryInfo *TLI, + LibFunc NewFunc, uint8_t HotCold); + Value *emitHotColdNewAlignedNoThrow(Type *RetTy, Value *Num, Value *Align, + Value *NoThrow, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold); - Value *emitHotColdSizeReturningNew(Value *Num, IRBuilderBase &B, + Value *emitHotColdSizeReturningNew(Type *RetTy, Value *Num, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold); - Value *emitHotColdSizeReturningNewAligned(Value *Num, Value *Align, - IRBuilderBase &B, + Value *emitHotColdSizeReturningNewAligned(Type *RetTy, Value *Num, + Value *Align, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold); } diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp index 5555b5e29cc74f..09e8301b772d96 100644 --- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -2014,9 +2014,8 @@ struct DSEState { return false; IRBuilder<> IRB(Malloc); Type *SizeTTy = Malloc->getArgOperand(0)->getType(); - auto *Calloc = - emitCalloc(ConstantInt::get(SizeTTy, 1), Malloc->getArgOperand(0), IRB, - TLI, Malloc->getType()->getPointerAddressSpace()); + auto *Calloc = emitCalloc(Malloc->getType(), ConstantInt::get(SizeTTy, 1), + Malloc->getArgOperand(0), IRB, TLI); if (!Calloc) return false; diff --git a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp index e4f4052e5e4815..287538179af8f3 100644 --- a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp @@ -1545,20 +1545,20 @@ static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType, Value *llvm::emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Ptr->getType(); Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_strlen, SizeTTy, CharPtrTy, Ptr, B, TLI); } Value *llvm::emitStrDup(Value *Ptr, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Ptr->getType(); return emitLibCall(LibFunc_strdup, CharPtrTy, CharPtrTy, Ptr, B, TLI); } Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Ptr->getType(); Type *IntTy = getIntTy(B, TLI); return emitLibCall(LibFunc_strchr, CharPtrTy, {CharPtrTy, IntTy}, {Ptr, ConstantInt::get(IntTy, C)}, B, TLI); @@ -1566,7 +1566,8 @@ Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilderBase &B, Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Ptr1->getType(); + assert(CharPtrTy == Ptr2->getType()); Type *IntTy = getIntTy(B, TLI); Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall( @@ -1578,20 +1579,23 @@ Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B, const TargetLibraryInfo *TLI) { Type *CharPtrTy = Dst->getType(); + assert(CharPtrTy == Src->getType()); return emitLibCall(LibFunc_strcpy, CharPtrTy, {CharPtrTy, CharPtrTy}, {Dst, Src}, B, TLI); } Value *llvm::emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Dst->getType(); + assert(CharPtrTy == Src->getType()); return emitLibCall(LibFunc_stpcpy, CharPtrTy, {CharPtrTy, CharPtrTy}, {Dst, Src}, B, TLI); } Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Dst->getType(); + assert(CharPtrTy == Src->getType()); Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_strncpy, CharPtrTy, {CharPtrTy, CharPtrTy, SizeTTy}, {Dst, Src, Len}, B, TLI); @@ -1599,7 +1603,8 @@ Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, Value *llvm::emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Dst->getType(); + assert(CharPtrTy == Src->getType()); Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_stpncpy, CharPtrTy, {CharPtrTy, CharPtrTy, SizeTTy}, {Dst, Src, Len}, B, TLI); @@ -1615,7 +1620,8 @@ Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, AttributeList AS; AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex, Attribute::NoUnwind); - Type *VoidPtrTy = B.getPtrTy(); + Type *VoidPtrTy = Dst->getType(); + assert(VoidPtrTy == Src->getType()); Type *SizeTTy = getSizeTTy(B, TLI); FunctionCallee MemCpy = getOrInsertLibFunc(M, *TLI, LibFunc_memcpy_chk, AttributeList::get(M->getContext(), AS), VoidPtrTy, @@ -1629,7 +1635,8 @@ 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) { - Type *VoidPtrTy = B.getPtrTy(); + Type *VoidPtrTy = Dst->getType(); + assert(VoidPtrTy == Src->getType()); Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_mempcpy, VoidPtrTy, {VoidPtrTy, VoidPtrTy, SizeTTy}, @@ -1638,7 +1645,7 @@ Value *llvm::emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { - Type *VoidPtrTy = B.getPtrTy(); + Type *VoidPtrTy = Ptr->getType(); Type *IntTy = getIntTy(B, TLI); Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_memchr, VoidPtrTy, @@ -1648,7 +1655,7 @@ Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, Value *llvm::emitMemRChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { - Type *VoidPtrTy = B.getPtrTy(); + Type *VoidPtrTy = Ptr->getType(); Type *IntTy = getIntTy(B, TLI); Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_memrchr, VoidPtrTy, @@ -1658,7 +1665,8 @@ Value *llvm::emitMemRChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { - Type *VoidPtrTy = B.getPtrTy(); + Type *VoidPtrTy = Ptr1->getType(); + assert(VoidPtrTy == Ptr2->getType()); Type *IntTy = getIntTy(B, TLI); Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_memcmp, IntTy, @@ -1668,7 +1676,8 @@ Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { - Type *VoidPtrTy = B.getPtrTy(); + Type *VoidPtrTy = Ptr1->getType(); + assert(VoidPtrTy == Ptr2->getType()); Type *IntTy = getIntTy(B, TLI); Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_bcmp, IntTy, @@ -1678,7 +1687,8 @@ Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, Value *llvm::emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - Type *VoidPtrTy = B.getPtrTy(); + Type *VoidPtrTy = Ptr1->getType(); + assert(VoidPtrTy == Ptr2->getType()); Type *IntTy = getIntTy(B, TLI); Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_memccpy, VoidPtrTy, @@ -1689,7 +1699,8 @@ Value *llvm::emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, Value *llvm::emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, ArrayRef VariadicArgs, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Dest->getType(); + assert(CharPtrTy == Fmt->getType()); Type *IntTy = getIntTy(B, TLI); Type *SizeTTy = getSizeTTy(B, TLI); SmallVector Args{Dest, Size, Fmt}; @@ -1702,7 +1713,7 @@ Value *llvm::emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *llvm::emitSPrintf(Value *Dest, Value *Fmt, ArrayRef VariadicArgs, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Dest->getType(); Type *IntTy = getIntTy(B, TLI); SmallVector Args{Dest, Fmt}; llvm::append_range(Args, VariadicArgs); @@ -1713,7 +1724,8 @@ Value *llvm::emitSPrintf(Value *Dest, Value *Fmt, Value *llvm::emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Dest->getType(); + assert(CharPtrTy == Src->getType()); return emitLibCall(LibFunc_strcat, CharPtrTy, {CharPtrTy, CharPtrTy}, {Dest, Src}, B, TLI); @@ -1721,7 +1733,8 @@ Value *llvm::emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B, Value *llvm::emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Dest->getType(); + assert(CharPtrTy == Src->getType()); Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_strlcpy, SizeTTy, {CharPtrTy, CharPtrTy, SizeTTy}, @@ -1730,7 +1743,8 @@ Value *llvm::emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, Value *llvm::emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Dest->getType(); + assert(CharPtrTy == Src->getType()); Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_strlcat, SizeTTy, {CharPtrTy, CharPtrTy, SizeTTy}, @@ -1739,7 +1753,8 @@ Value *llvm::emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, Value *llvm::emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Dest->getType(); + assert(CharPtrTy == Src->getType()); Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_strncat, CharPtrTy, {CharPtrTy, CharPtrTy, SizeTTy}, @@ -1748,7 +1763,8 @@ Value *llvm::emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, Value *llvm::emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Dest->getType(); + assert(CharPtrTy == Fmt->getType()); Type *IntTy = getIntTy(B, TLI); Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall( @@ -1759,7 +1775,8 @@ Value *llvm::emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, Value *llvm::emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilderBase &B, const TargetLibraryInfo *TLI) { - Type *CharPtrTy = B.getPtrTy(); + Type *CharPtrTy = Dest->getType(); + assert(CharPtrTy == Fmt->getType()); Type *IntTy = getIntTy(B, TLI); return emitLibCall(LibFunc_vsprintf, IntTy, {CharPtrTy, CharPtrTy, VAList->getType()}, @@ -1912,8 +1929,8 @@ Value *llvm::emitPutS(Value *Str, IRBuilderBase &B, Type *IntTy = getIntTy(B, TLI); StringRef PutsName = TLI->getName(LibFunc_puts); - FunctionCallee PutS = getOrInsertLibFunc(M, *TLI, LibFunc_puts, IntTy, - B.getPtrTy()); + FunctionCallee PutS = + getOrInsertLibFunc(M, *TLI, LibFunc_puts, IntTy, Str->getType()); inferNonMandatoryLibFuncAttrs(M, PutsName, *TLI); CallInst *CI = B.CreateCall(PutS, Str, PutsName); if (const Function *F = @@ -1951,7 +1968,7 @@ Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilderBase &B, Type *IntTy = getIntTy(B, TLI); StringRef FPutsName = TLI->getName(LibFunc_fputs); FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fputs, IntTy, - B.getPtrTy(), File->getType()); + Str->getType(), File->getType()); if (File->getType()->isPointerTy()) inferNonMandatoryLibFuncAttrs(M, FPutsName, *TLI); CallInst *CI = B.CreateCall(F, {Str, File}, FPutsName); @@ -1970,9 +1987,9 @@ Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B, Type *SizeTTy = getSizeTTy(B, TLI); StringRef FWriteName = TLI->getName(LibFunc_fwrite); - FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fwrite, - SizeTTy, B.getPtrTy(), SizeTTy, - SizeTTy, File->getType()); + FunctionCallee F = + getOrInsertLibFunc(M, *TLI, LibFunc_fwrite, SizeTTy, Ptr->getType(), + SizeTTy, SizeTTy, File->getType()); if (File->getType()->isPointerTy()) inferNonMandatoryLibFuncAttrs(M, FWriteName, *TLI); @@ -1986,16 +2003,16 @@ Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B, return CI; } -Value *llvm::emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL, - const TargetLibraryInfo *TLI) { +Value *llvm::emitMalloc(Type *RetTy, Value *Num, IRBuilderBase &B, + const DataLayout &DL, const TargetLibraryInfo *TLI) { Module *M = B.GetInsertBlock()->getModule(); if (!isLibFuncEmittable(M, TLI, LibFunc_malloc)) return nullptr; StringRef MallocName = TLI->getName(LibFunc_malloc); Type *SizeTTy = getSizeTTy(B, TLI); - FunctionCallee Malloc = getOrInsertLibFunc(M, *TLI, LibFunc_malloc, - B.getPtrTy(), SizeTTy); + FunctionCallee Malloc = + getOrInsertLibFunc(M, *TLI, LibFunc_malloc, RetTy, SizeTTy); inferNonMandatoryLibFuncAttrs(M, MallocName, *TLI); CallInst *CI = B.CreateCall(Malloc, Num, MallocName); @@ -2006,16 +2023,16 @@ Value *llvm::emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL, return CI; } -Value *llvm::emitCalloc(Value *Num, Value *Size, IRBuilderBase &B, - const TargetLibraryInfo &TLI, unsigned AddrSpace) { +Value *llvm::emitCalloc(Type *RetTy, Value *Num, Value *Size, IRBuilderBase &B, + const TargetLibraryInfo &TLI) { Module *M = B.GetInsertBlock()->getModule(); if (!isLibFuncEmittable(M, &TLI, LibFunc_calloc)) return nullptr; StringRef CallocName = TLI.getName(LibFunc_calloc); Type *SizeTTy = getSizeTTy(B, &TLI); - FunctionCallee Calloc = getOrInsertLibFunc( - M, TLI, LibFunc_calloc, B.getPtrTy(AddrSpace), SizeTTy, SizeTTy); + FunctionCallee Calloc = + getOrInsertLibFunc(M, TLI, LibFunc_calloc, RetTy, SizeTTy, SizeTTy); inferNonMandatoryLibFuncAttrs(M, CallocName, TLI); CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName); @@ -2026,7 +2043,8 @@ Value *llvm::emitCalloc(Value *Num, Value *Size, IRBuilderBase &B, return CI; } -Value *llvm::emitHotColdSizeReturningNew(Value *Num, IRBuilderBase &B, +Value *llvm::emitHotColdSizeReturningNew(Type *RetPtrTy, Value *Num, + IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc SizeFeedbackNewFunc, uint8_t HotCold) { @@ -2038,7 +2056,7 @@ Value *llvm::emitHotColdSizeReturningNew(Value *Num, IRBuilderBase &B, // __sized_ptr_t struct return type { void*, size_t } StructType *SizedPtrT = - StructType::get(M->getContext(), {B.getPtrTy(), Num->getType()}); + StructType::get(M->getContext(), {RetPtrTy, Num->getType()}); FunctionCallee Func = M->getOrInsertFunction(Name, SizedPtrT, Num->getType(), B.getInt8Ty()); inferNonMandatoryLibFuncAttrs(M, Name, *TLI); @@ -2050,8 +2068,8 @@ Value *llvm::emitHotColdSizeReturningNew(Value *Num, IRBuilderBase &B, return CI; } -Value *llvm::emitHotColdSizeReturningNewAligned(Value *Num, Value *Align, - IRBuilderBase &B, +Value *llvm::emitHotColdSizeReturningNewAligned(Type *RetPtrTy, Value *Num, + Value *Align, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc SizeFeedbackNewFunc, uint8_t HotCold) { @@ -2063,7 +2081,7 @@ Value *llvm::emitHotColdSizeReturningNewAligned(Value *Num, Value *Align, // __sized_ptr_t struct return type { void*, size_t } StructType *SizedPtrT = - StructType::get(M->getContext(), {B.getPtrTy(), Num->getType()}); + StructType::get(M->getContext(), {RetPtrTy, Num->getType()}); FunctionCallee Func = M->getOrInsertFunction(Name, SizedPtrT, Num->getType(), Align->getType(), B.getInt8Ty()); inferNonMandatoryLibFuncAttrs(M, Name, *TLI); @@ -2076,7 +2094,7 @@ Value *llvm::emitHotColdSizeReturningNewAligned(Value *Num, Value *Align, return CI; } -Value *llvm::emitHotColdNew(Value *Num, IRBuilderBase &B, +Value *llvm::emitHotColdNew(Type *RetTy, Value *Num, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold) { Module *M = B.GetInsertBlock()->getModule(); @@ -2084,8 +2102,8 @@ Value *llvm::emitHotColdNew(Value *Num, IRBuilderBase &B, return nullptr; StringRef Name = TLI->getName(NewFunc); - FunctionCallee Func = M->getOrInsertFunction(Name, B.getPtrTy(), - Num->getType(), B.getInt8Ty()); + FunctionCallee Func = + M->getOrInsertFunction(Name, RetTy, Num->getType(), B.getInt8Ty()); inferNonMandatoryLibFuncAttrs(M, Name, *TLI); CallInst *CI = B.CreateCall(Func, {Num, B.getInt8(HotCold)}, Name); @@ -2096,7 +2114,8 @@ Value *llvm::emitHotColdNew(Value *Num, IRBuilderBase &B, return CI; } -Value *llvm::emitHotColdNewNoThrow(Value *Num, Value *NoThrow, IRBuilderBase &B, +Value *llvm::emitHotColdNewNoThrow(Type *RetTy, Value *Num, Value *NoThrow, + IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold) { Module *M = B.GetInsertBlock()->getModule(); @@ -2104,9 +2123,8 @@ Value *llvm::emitHotColdNewNoThrow(Value *Num, Value *NoThrow, IRBuilderBase &B, return nullptr; StringRef Name = TLI->getName(NewFunc); - FunctionCallee Func = - M->getOrInsertFunction(Name, B.getPtrTy(), Num->getType(), - NoThrow->getType(), B.getInt8Ty()); + FunctionCallee Func = M->getOrInsertFunction( + Name, RetTy, Num->getType(), NoThrow->getType(), B.getInt8Ty()); inferNonMandatoryLibFuncAttrs(M, Name, *TLI); CallInst *CI = B.CreateCall(Func, {Num, NoThrow, B.getInt8(HotCold)}, Name); @@ -2117,7 +2135,8 @@ Value *llvm::emitHotColdNewNoThrow(Value *Num, Value *NoThrow, IRBuilderBase &B, return CI; } -Value *llvm::emitHotColdNewAligned(Value *Num, Value *Align, IRBuilderBase &B, +Value *llvm::emitHotColdNewAligned(Type *RetTy, Value *Num, Value *Align, + IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold) { Module *M = B.GetInsertBlock()->getModule(); @@ -2125,8 +2144,8 @@ Value *llvm::emitHotColdNewAligned(Value *Num, Value *Align, IRBuilderBase &B, return nullptr; StringRef Name = TLI->getName(NewFunc); - FunctionCallee Func = M->getOrInsertFunction( - Name, B.getPtrTy(), Num->getType(), Align->getType(), B.getInt8Ty()); + FunctionCallee Func = M->getOrInsertFunction(Name, RetTy, Num->getType(), + Align->getType(), B.getInt8Ty()); inferNonMandatoryLibFuncAttrs(M, Name, *TLI); CallInst *CI = B.CreateCall(Func, {Num, Align, B.getInt8(HotCold)}, Name); @@ -2137,7 +2156,7 @@ Value *llvm::emitHotColdNewAligned(Value *Num, Value *Align, IRBuilderBase &B, return CI; } -Value *llvm::emitHotColdNewAlignedNoThrow(Value *Num, Value *Align, +Value *llvm::emitHotColdNewAlignedNoThrow(Type *RetTy, Value *Num, Value *Align, Value *NoThrow, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold) { @@ -2146,9 +2165,9 @@ Value *llvm::emitHotColdNewAlignedNoThrow(Value *Num, Value *Align, return nullptr; StringRef Name = TLI->getName(NewFunc); - FunctionCallee Func = M->getOrInsertFunction( - Name, B.getPtrTy(), Num->getType(), Align->getType(), - NoThrow->getType(), B.getInt8Ty()); + FunctionCallee Func = + M->getOrInsertFunction(Name, RetTy, Num->getType(), Align->getType(), + NoThrow->getType(), B.getInt8Ty()); inferNonMandatoryLibFuncAttrs(M, Name, *TLI); CallInst *CI = B.CreateCall(Func, {Num, Align, NoThrow, B.getInt8(HotCold)}, Name); diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp index d85e0d99466022..0438ccf36aeaf0 100644 --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -917,8 +917,9 @@ Value *LibCallSimplifier::optimizeStringNCpy(CallInst *CI, bool RetEnd, // Create a bigger, nul-padded array with the same length, SrcLen, // as the original string. SrcStr.resize(N, '\0'); - Src = B.CreateGlobalString(SrcStr, "str", /*AddressSpace=*/0, - /*M=*/nullptr, /*AddNull=*/false); + Src = B.CreateGlobalString( + SrcStr, "str", /*AddressSpace=*/DL.getDefaultGlobalsAddressSpace(), + /*M=*/nullptr, /*AddNull=*/false); } Type *PT = Callee->getFunctionType()->getParamType(0); @@ -1723,7 +1724,8 @@ Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilderBase &B) { Value *LibCallSimplifier::optimizeRealloc(CallInst *CI, IRBuilderBase &B) { if (isa(CI->getArgOperand(0))) - return copyFlags(*CI, emitMalloc(CI->getArgOperand(1), B, DL, TLI)); + return copyFlags(*CI, emitMalloc(CI->getArgOperand(0)->getType(), + CI->getArgOperand(1), B, DL, TLI)); return nullptr; } @@ -1758,122 +1760,124 @@ Value *LibCallSimplifier::optimizeNew(CallInst *CI, IRBuilderBase &B, switch (Func) { case LibFunc_Znwm12__hot_cold_t: if (OptimizeExistingHotColdNew) - return emitHotColdNew(CI->getArgOperand(0), B, TLI, + return emitHotColdNew(CI->getType(), CI->getArgOperand(0), B, TLI, LibFunc_Znwm12__hot_cold_t, HotCold); break; case LibFunc_Znwm: if (HotCold != NotColdNewHintValue) - return emitHotColdNew(CI->getArgOperand(0), B, TLI, + return emitHotColdNew(CI->getType(), CI->getArgOperand(0), B, TLI, LibFunc_Znwm12__hot_cold_t, HotCold); break; case LibFunc_Znam12__hot_cold_t: if (OptimizeExistingHotColdNew) - return emitHotColdNew(CI->getArgOperand(0), B, TLI, + return emitHotColdNew(CI->getType(), CI->getArgOperand(0), B, TLI, LibFunc_Znam12__hot_cold_t, HotCold); break; case LibFunc_Znam: if (HotCold != NotColdNewHintValue) - return emitHotColdNew(CI->getArgOperand(0), B, TLI, + return emitHotColdNew(CI->getType(), CI->getArgOperand(0), B, TLI, LibFunc_Znam12__hot_cold_t, HotCold); break; case LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t: if (OptimizeExistingHotColdNew) return emitHotColdNewNoThrow( - CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, + CI->getType(), CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t, HotCold); break; case LibFunc_ZnwmRKSt9nothrow_t: if (HotCold != NotColdNewHintValue) return emitHotColdNewNoThrow( - CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, + CI->getType(), CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t, HotCold); break; case LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t: if (OptimizeExistingHotColdNew) return emitHotColdNewNoThrow( - CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, + CI->getType(), CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t, HotCold); break; case LibFunc_ZnamRKSt9nothrow_t: if (HotCold != NotColdNewHintValue) return emitHotColdNewNoThrow( - CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, + CI->getType(), CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t, HotCold); break; case LibFunc_ZnwmSt11align_val_t12__hot_cold_t: if (OptimizeExistingHotColdNew) return emitHotColdNewAligned( - CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, + CI->getType(), CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, LibFunc_ZnwmSt11align_val_t12__hot_cold_t, HotCold); break; case LibFunc_ZnwmSt11align_val_t: if (HotCold != NotColdNewHintValue) return emitHotColdNewAligned( - CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, + CI->getType(), CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, LibFunc_ZnwmSt11align_val_t12__hot_cold_t, HotCold); break; case LibFunc_ZnamSt11align_val_t12__hot_cold_t: if (OptimizeExistingHotColdNew) return emitHotColdNewAligned( - CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, + CI->getType(), CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, LibFunc_ZnamSt11align_val_t12__hot_cold_t, HotCold); break; case LibFunc_ZnamSt11align_val_t: if (HotCold != NotColdNewHintValue) return emitHotColdNewAligned( - CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, + CI->getType(), CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, LibFunc_ZnamSt11align_val_t12__hot_cold_t, HotCold); break; case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t: if (OptimizeExistingHotColdNew) return emitHotColdNewAlignedNoThrow( - CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B, - TLI, LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t, - HotCold); + CI->getType(), CI->getArgOperand(0), CI->getArgOperand(1), + CI->getArgOperand(2), B, TLI, + LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t, HotCold); break; case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t: if (HotCold != NotColdNewHintValue) return emitHotColdNewAlignedNoThrow( - CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B, - TLI, LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t, - HotCold); + CI->getType(), CI->getArgOperand(0), CI->getArgOperand(1), + CI->getArgOperand(2), B, TLI, + LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t, HotCold); break; case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t: if (OptimizeExistingHotColdNew) return emitHotColdNewAlignedNoThrow( - CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B, - TLI, LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t, - HotCold); + CI->getType(), CI->getArgOperand(0), CI->getArgOperand(1), + CI->getArgOperand(2), B, TLI, + LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t, HotCold); break; case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t: if (HotCold != NotColdNewHintValue) return emitHotColdNewAlignedNoThrow( - CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B, - TLI, LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t, - HotCold); + CI->getType(), CI->getArgOperand(0), CI->getArgOperand(1), + CI->getArgOperand(2), B, TLI, + LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t, HotCold); break; case LibFunc_size_returning_new: if (HotCold != NotColdNewHintValue) - return emitHotColdSizeReturningNew(CI->getArgOperand(0), B, TLI, - LibFunc_size_returning_new_hot_cold, - HotCold); + return emitHotColdSizeReturningNew( + CI->getType()->getStructElementType(0), CI->getArgOperand(0), B, TLI, + LibFunc_size_returning_new_hot_cold, HotCold); break; case LibFunc_size_returning_new_hot_cold: if (OptimizeExistingHotColdNew) - return emitHotColdSizeReturningNew(CI->getArgOperand(0), B, TLI, - LibFunc_size_returning_new_hot_cold, - HotCold); + return emitHotColdSizeReturningNew( + CI->getType()->getStructElementType(0), CI->getArgOperand(0), B, TLI, + LibFunc_size_returning_new_hot_cold, HotCold); break; case LibFunc_size_returning_new_aligned: if (HotCold != NotColdNewHintValue) return emitHotColdSizeReturningNewAligned( - CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, + CI->getType()->getStructElementType(0), CI->getArgOperand(0), + CI->getArgOperand(1), B, TLI, LibFunc_size_returning_new_aligned_hot_cold, HotCold); break; case LibFunc_size_returning_new_aligned_hot_cold: if (OptimizeExistingHotColdNew) return emitHotColdSizeReturningNewAligned( - CI->getArgOperand(0), CI->getArgOperand(1), B, TLI, + CI->getType()->getStructElementType(0), CI->getArgOperand(0), + CI->getArgOperand(1), B, TLI, LibFunc_size_returning_new_aligned_hot_cold, HotCold); break; default: @@ -3345,7 +3349,8 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) { // printf("%s", str"\n") --> puts(str) if (OperandStr.back() == '\n') { OperandStr = OperandStr.drop_back(); - Value *GV = B.CreateGlobalString(OperandStr, "str"); + Value *GV = B.CreateGlobalString(OperandStr, "str", + DL.getDefaultGlobalsAddressSpace()); return copyFlags(*CI, emitPutS(GV, B, TLI)); } return nullptr; @@ -3357,7 +3362,8 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) { // Create a string literal with no \n on it. We expect the constant merge // pass to be run after this pass, to merge duplicate strings. FormatStr = FormatStr.drop_back(); - Value *GV = B.CreateGlobalString(FormatStr, "str"); + Value *GV = B.CreateGlobalString(FormatStr, "str", + DL.getDefaultGlobalsAddressSpace()); return copyFlags(*CI, emitPutS(GV, B, TLI)); } diff --git a/llvm/test/Transforms/InstCombine/printf-addrspace.ll b/llvm/test/Transforms/InstCombine/printf-addrspace.ll new file mode 100644 index 00000000000000..3cb28ce3460bf1 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/printf-addrspace.ll @@ -0,0 +1,35 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; Test that the printf library call simplifier works correctly. +; +; RUN: opt < %s -passes=instcombine -S | FileCheck %s + +target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-G200" + +@hello_world = addrspace(200) constant [13 x i8] c"hello world\0A\00" +@percent_s = addrspace(200) constant [4 x i8] c"%s\0A\00" + +declare i32 @printf(ptr addrspace(200) , ...) +declare i32 @puts(ptr addrspace(200)) + +; Check printf("foo\n") -> puts("foo"). + +define void @test_simplify1() { +; CHECK-LABEL: @test_simplify1( +; CHECK-NEXT: [[PUTS:%.*]] = call i32 @puts(ptr addrspace(200) @str) +; CHECK-NEXT: ret void +; + call i32 (ptr addrspace(200) , ...) @printf(ptr addrspace(200) @hello_world) + ret void +} + + +; Check printf("%s\n", str) -> puts(str). + +define void @test_simplify2() { +; CHECK-LABEL: @test_simplify2( +; CHECK-NEXT: [[PUTS:%.*]] = call i32 @puts(ptr addrspace(200) @hello_world) +; CHECK-NEXT: ret void +; + call i32 (ptr addrspace(200) , ...) @printf(ptr addrspace(200) @percent_s, ptr addrspace(200) @hello_world) + ret void +}