diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -558,7 +558,8 @@ return Type::getInt8PtrTy(Context, AddrSpace); } - /// Fetch the type representing a pointer to an integer value. + /// Fetch the type of an integer with size at least as big as that of a + /// pointer in the given address space. IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) { return DL.getIntPtrType(Context, AddrSpace); } diff --git a/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h b/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h --- a/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h +++ b/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h @@ -237,7 +237,7 @@ const TargetLibraryInfo *TLI); /// Emit a call to the fwrite function. This assumes that Ptr is a pointer, - /// Size is an 'intptr_t', and File is a pointer to FILE. + /// Size is an 'size_t', and File is a pointer to FILE. Value *emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI); diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp --- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -1788,10 +1788,9 @@ !memoryIsNotModifiedBetween(Malloc, MemSet, BatchAA, DL, &DT)) return false; IRBuilder<> IRB(Malloc); - const auto &DL = Malloc->getModule()->getDataLayout(); - auto *Calloc = - emitCalloc(ConstantInt::get(IRB.getIntPtrTy(DL), 1), - Malloc->getArgOperand(0), IRB, TLI); + Type *SizeTTy = Malloc->getArgOperand(0)->getType(); + auto *Calloc = emitCalloc(ConstantInt::get(SizeTTy, 1), + Malloc->getArgOperand(0), IRB, TLI); if (!Calloc) return false; MemorySSAUpdater Updater(&MSSA); diff --git a/llvm/lib/Transforms/Scalar/MergeICmps.cpp b/llvm/lib/Transforms/Scalar/MergeICmps.cpp --- a/llvm/lib/Transforms/Scalar/MergeICmps.cpp +++ b/llvm/lib/Transforms/Scalar/MergeICmps.cpp @@ -645,14 +645,18 @@ Comparisons.begin(), Comparisons.end(), 0u, [](int Size, const BCECmpBlock &C) { return Size + C.SizeBits(); }); + // memcmp expects a 'size_t' argument and returns 'int'. + unsigned SizeTBits = TLI.getSizeTSize(*Phi.getModule()); + unsigned IntBits = TLI.getIntSize(); + // Create memcmp() == 0. const auto &DL = Phi.getModule()->getDataLayout(); Value *const MemCmpCall = emitMemCmp( Lhs, Rhs, - ConstantInt::get(DL.getIntPtrType(Context), TotalSizeBits / 8), Builder, - DL, &TLI); + ConstantInt::get(Builder.getIntNTy(SizeTBits), TotalSizeBits / 8), + Builder, DL, &TLI); IsEqual = Builder.CreateICmpEQ( - MemCmpCall, ConstantInt::get(Type::getInt32Ty(Context), 0)); + MemCmpCall, ConstantInt::get(Builder.getIntNTy(IntBits), 0)); } BasicBlock *const PhiBB = Phi.getParent(); diff --git a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp --- a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp @@ -1421,6 +1421,15 @@ return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr"); } +static IntegerType *getIntTy(IRBuilderBase &B, const TargetLibraryInfo *TLI) { + return B.getIntNTy(TLI->getIntSize()); +} + +static IntegerType *getSizeTTy(IRBuilderBase &B, const TargetLibraryInfo *TLI) { + const Module *M = B.GetInsertBlock()->getModule(); + return B.getIntNTy(TLI->getSizeTSize(*M)); +} + static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType, ArrayRef ParamTypes, ArrayRef Operands, IRBuilderBase &B, @@ -1443,8 +1452,7 @@ Value *llvm::emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { - LLVMContext &Context = B.GetInsertBlock()->getContext(); - Type *SizeTTy = DL.getIntPtrType(Context); + Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_strlen, SizeTTy, B.getInt8PtrTy(), castToCStr(Ptr, B), B, TLI); } @@ -1458,16 +1466,15 @@ Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilderBase &B, const TargetLibraryInfo *TLI) { Type *I8Ptr = B.getInt8PtrTy(); - Type *IntTy = B.getInt32Ty(); + Type *IntTy = getIntTy(B, TLI); 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(); + Type *IntTy = getIntTy(B, TLI); + Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall( LibFunc_strncmp, IntTy, {B.getInt8PtrTy(), B.getInt8PtrTy(), SizeTTy}, @@ -1491,7 +1498,7 @@ Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI) { Type *I8Ptr = B.getInt8PtrTy(); - Type *SizeTTy = Len->getType(); + Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_strncpy, I8Ptr, {I8Ptr, I8Ptr, SizeTTy}, {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI); } @@ -1499,7 +1506,7 @@ Value *llvm::emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI) { Type *I8Ptr = B.getInt8PtrTy(); - Type *SizeTTy = Len->getType(); + Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_stpncpy, I8Ptr, {I8Ptr, I8Ptr, SizeTTy}, {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI); } @@ -1514,9 +1521,8 @@ AttributeList AS; AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex, Attribute::NoUnwind); - LLVMContext &Context = B.GetInsertBlock()->getContext(); Type *I8Ptr = B.getInt8PtrTy(); - Type *SizeTTy = DL.getIntPtrType(Context); + Type *SizeTTy = getSizeTTy(B, TLI); FunctionCallee MemCpy = getOrInsertLibFunc(M, *TLI, LibFunc_memcpy_chk, AttributeList::get(M->getContext(), AS), I8Ptr, I8Ptr, I8Ptr, SizeTTy, SizeTTy); @@ -1531,9 +1537,8 @@ Value *llvm::emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI) { - LLVMContext &Context = B.GetInsertBlock()->getContext(); Type *I8Ptr = B.getInt8PtrTy(); - Type *SizeTTy = DL.getIntPtrType(Context); + Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_mempcpy, I8Ptr, {I8Ptr, I8Ptr, SizeTTy}, {Dst, Src, Len}, B, TLI); @@ -1541,10 +1546,9 @@ Value *llvm::emitMemChr(Value *Ptr, Value *Val, 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); + Type *IntTy = getIntTy(B, TLI); + Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_memchr, I8Ptr, {I8Ptr, IntTy, SizeTTy}, {castToCStr(Ptr, B), Val, Len}, B, TLI); @@ -1552,10 +1556,9 @@ Value *llvm::emitMemRChr(Value *Ptr, Value *Val, 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); + Type *IntTy = getIntTy(B, TLI); + Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_memrchr, I8Ptr, {I8Ptr, IntTy, SizeTTy}, {castToCStr(Ptr, B), Val, Len}, B, TLI); @@ -1563,45 +1566,40 @@ 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, IntTy, - {I8Ptr, I8Ptr, SizeTTy}, - {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); + Type *IntTy = getIntTy(B, TLI); + Type *SizeTTy = getSizeTTy(B, TLI); + return emitLibCall(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, IntTy, - {I8Ptr, I8Ptr, SizeTTy}, - {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); + Type *IntTy = getIntTy(B, TLI); + Type *SizeTTy = getSizeTTy(B, TLI); + return emitLibCall(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, I8Ptr, - {I8Ptr, I8Ptr, IntTy, SizeTTy}, - {Ptr1, Ptr2, Val, Len}, B, TLI); + Type *IntTy = getIntTy(B, TLI); + Type *SizeTTy = getSizeTTy(B, TLI); + return emitLibCall(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(); + Type *IntTy = getIntTy(B, TLI); + Type *SizeTTy = getSizeTTy(B, TLI); SmallVector Args{castToCStr(Dest, B), Size, castToCStr(Fmt, B)}; llvm::append_range(Args, VariadicArgs); return emitLibCall(LibFunc_snprintf, IntTy, @@ -1613,7 +1611,7 @@ ArrayRef VariadicArgs, IRBuilderBase &B, const TargetLibraryInfo *TLI) { Type *I8Ptr = B.getInt8PtrTy(); - Type *IntTy = B.getInt32Ty(); + Type *IntTy = getIntTy(B, TLI); SmallVector Args{castToCStr(Dest, B), castToCStr(Fmt, B)}; llvm::append_range(Args, VariadicArgs); return emitLibCall(LibFunc_sprintf, IntTy, @@ -1631,7 +1629,7 @@ Value *llvm::emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI) { Type *I8Ptr = B.getInt8PtrTy(); - Type *SizeTTy = Size->getType(); + Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_strlcpy, SizeTTy, {I8Ptr, I8Ptr, SizeTTy}, {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); @@ -1640,7 +1638,7 @@ Value *llvm::emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI) { Type *I8Ptr = B.getInt8PtrTy(); - Type *SizeTTy = Size->getType(); + Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_strlcat, SizeTTy, {I8Ptr, I8Ptr, SizeTTy}, {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); @@ -1649,7 +1647,7 @@ Value *llvm::emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI) { Type *I8Ptr = B.getInt8PtrTy(); - Type *SizeTTy = Size->getType(); + Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall(LibFunc_strncat, I8Ptr, {I8Ptr, I8Ptr, SizeTTy}, {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); @@ -1658,8 +1656,8 @@ 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(); + Type *IntTy = getIntTy(B, TLI); + Type *SizeTTy = getSizeTTy(B, TLI); return emitLibCall( LibFunc_vsnprintf, IntTy, {I8Ptr, SizeTTy, I8Ptr, VAList->getType()}, @@ -1669,7 +1667,7 @@ Value *llvm::emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilderBase &B, const TargetLibraryInfo *TLI) { Type *I8Ptr = B.getInt8PtrTy(); - Type *IntTy = B.getInt32Ty(); + Type *IntTy = getIntTy(B, TLI); return emitLibCall(LibFunc_vsprintf, IntTy, {I8Ptr, I8Ptr, VAList->getType()}, {castToCStr(Dest, B), castToCStr(Fmt, B), VAList}, B, TLI); @@ -1800,7 +1798,7 @@ if (!isLibFuncEmittable(M, TLI, LibFunc_putchar)) return nullptr; - Type *IntTy = Char->getType(); + Type *IntTy = getIntTy(B, TLI); StringRef PutCharName = TLI->getName(LibFunc_putchar); FunctionCallee PutChar = getOrInsertLibFunc(M, *TLI, LibFunc_putchar, IntTy, IntTy); @@ -1819,7 +1817,7 @@ if (!isLibFuncEmittable(M, TLI, LibFunc_puts)) return nullptr; - Type *IntTy = B.getInt32Ty(); + Type *IntTy = getIntTy(B, TLI); StringRef PutsName = TLI->getName(LibFunc_puts); FunctionCallee PutS = getOrInsertLibFunc(M, *TLI, LibFunc_puts, IntTy, B.getInt8PtrTy()); @@ -1837,7 +1835,7 @@ if (!isLibFuncEmittable(M, TLI, LibFunc_fputc)) return nullptr; - Type *IntTy = B.getInt32Ty(); + Type *IntTy = getIntTy(B, TLI); StringRef FPutcName = TLI->getName(LibFunc_fputc); FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fputc, IntTy, IntTy, File->getType()); @@ -1859,7 +1857,7 @@ if (!isLibFuncEmittable(M, TLI, LibFunc_fputs)) return nullptr; - Type *IntTy = B.getInt32Ty(); + Type *IntTy = getIntTy(B, TLI); StringRef FPutsName = TLI->getName(LibFunc_fputs); FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fputs, IntTy, B.getInt8PtrTy(), File->getType()); @@ -1879,8 +1877,7 @@ if (!isLibFuncEmittable(M, TLI, LibFunc_fwrite)) return nullptr; - LLVMContext &Context = B.GetInsertBlock()->getContext(); - Type *SizeTTy = DL.getIntPtrType(Context); + Type *SizeTTy = getSizeTTy(B, TLI); StringRef FWriteName = TLI->getName(LibFunc_fwrite); FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fwrite, SizeTTy, B.getInt8PtrTy(), SizeTTy, @@ -1890,7 +1887,7 @@ inferNonMandatoryLibFuncAttrs(M, FWriteName, *TLI); CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, - ConstantInt::get(DL.getIntPtrType(Context), 1), File}); + ConstantInt::get(SizeTTy, 1), File}); if (const Function *Fn = dyn_cast(F.getCallee()->stripPointerCasts())) @@ -1905,8 +1902,7 @@ return nullptr; StringRef MallocName = TLI->getName(LibFunc_malloc); - LLVMContext &Context = B.GetInsertBlock()->getContext(); - Type *SizeTTy = DL.getIntPtrType(Context); + Type *SizeTTy = getSizeTTy(B, TLI); FunctionCallee Malloc = getOrInsertLibFunc(M, *TLI, LibFunc_malloc, B.getInt8PtrTy(), SizeTTy); inferNonMandatoryLibFuncAttrs(M, MallocName, *TLI); @@ -1926,9 +1922,7 @@ return nullptr; StringRef CallocName = TLI.getName(LibFunc_calloc); - LLVMContext &Context = B.GetInsertBlock()->getContext(); - const DataLayout &DL = M->getDataLayout(); - Type *SizeTTy = DL.getIntPtrType(Context); + Type *SizeTTy = getSizeTTy(B, &TLI); FunctionCallee Calloc = getOrInsertLibFunc(M, TLI, LibFunc_calloc, B.getInt8PtrTy(), SizeTTy, SizeTTy); inferNonMandatoryLibFuncAttrs(M, CallocName, TLI); diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -433,14 +433,16 @@ Function *Callee = CI->getCalledFunction(); FunctionType *FT = Callee->getFunctionType(); - if (!FT->getParamType(1)->isIntegerTy(32)) // memchr needs i32. + unsigned IntBits = TLI->getIntSize(); + if (!FT->getParamType(1)->isIntegerTy(IntBits)) // memchr needs 'int'. return nullptr; - return copyFlags( - *CI, - emitMemChr(SrcStr, CharVal, // include nul. - ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len), B, - DL, TLI)); + unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule()); + Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits); + return copyFlags(*CI, + emitMemChr(SrcStr, CharVal, // include nul. + ConstantInt::get(SizeTTy, Len), B, + DL, TLI)); } if (CharC->isZero()) { @@ -487,11 +489,13 @@ return nullptr; } + unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule()); + Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits); + // Try to expand strrchr to the memrchr nonstandard extension if it's // available, or simply fail otherwise. uint64_t NBytes = Str.size() + 1; // Include the terminating nul. - Type *IntPtrType = DL.getIntPtrType(CI->getContext()); - Value *Size = ConstantInt::get(IntPtrType, NBytes); + Value *Size = ConstantInt::get(SizeTTy, NBytes); return copyFlags(*CI, emitMemRChr(SrcStr, CharVal, Size, B, DL, TLI)); } @@ -3190,10 +3194,11 @@ if (FormatStr.contains('%')) return nullptr; // We found a format specifier. + unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule()); + Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits); return copyFlags( *CI, emitFWrite(CI->getArgOperand(1), - ConstantInt::get(DL.getIntPtrType(CI->getContext()), - FormatStr.size()), + ConstantInt::get(SizeTTy, FormatStr.size()), CI->getArgOperand(0), B, DL, TLI)); } @@ -3304,10 +3309,12 @@ return nullptr; // Known to have no uses (see above). + unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule()); + Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits); return copyFlags( *CI, emitFWrite(CI->getArgOperand(0), - ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len - 1), + ConstantInt::get(SizeTTy, Len - 1), CI->getArgOperand(1), B, DL, TLI)); }