Skip to content

Commit 76b4553

Browse files
committed
[SimplifyLibCalls] Add initial support for non-8-bit bytes
The patch makes CharWidth argument of `getStringLength` mandatory and ensures the correct values are passed in most cases. This is *not* a complete support for unusual byte widths in SimplifyLibCalls since `getConstantStringInfo` returns false for those. The code guarded by `getConstantStringInfo` returning true is unchanged because the changes are currently not testable.
1 parent fe95d64 commit 76b4553

35 files changed

+928
-100
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ struct ConstantDataArraySlice {
404404
/// If successful \p Slice will point to a ConstantDataArray info object
405405
/// with an appropriate offset.
406406
bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice,
407-
unsigned ElementSize, uint64_t Offset = 0);
407+
unsigned ElementBitWidth, uint64_t Offset = 0);
408408

409409
/// This function computes the length of a null-terminated C string pointed to
410410
/// by V. If successful, it returns true and returns the string in Str. If
@@ -417,7 +417,7 @@ bool getConstantStringInfo(const Value *V, StringRef &Str, unsigned CharWidth,
417417

418418
/// If we can compute the length of the string pointed to by the specified
419419
/// pointer, return 'len+1'. If we can't, return 0.
420-
uint64_t GetStringLength(const Value *V, unsigned CharSize = 8);
420+
uint64_t getStringLength(const Value *V, unsigned CharWidth);
421421

422422
/// This function returns call pointer argument that is considered the same by
423423
/// aliasing rules. You CAN'T use it to replace one value with another. If

llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ class LibCallSimplifier {
260260
bool hasFloatVersion(const Module *M, StringRef FuncName);
261261

262262
/// Shared code to optimize strlen+wcslen and strnlen+wcsnlen.
263-
Value *optimizeStringLength(CallInst *CI, IRBuilderBase &B, unsigned CharSize,
264-
Value *Bound = nullptr);
263+
Value *optimizeStringLength(CallInst *CI, IRBuilderBase &B,
264+
unsigned CharWidth, Value *Bound = nullptr);
265265
};
266266
} // End llvm namespace
267267

llvm/lib/Analysis/MemoryBuiltins.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ llvm::getAllocSize(const CallBase *CB, const TargetLibraryInfo *TLI,
379379

380380
// Handle strdup-like functions separately.
381381
if (FnData->AllocTy == StrDupLike) {
382-
APInt Size(IntTyBits, GetStringLength(Mapper(CB->getArgOperand(0))));
382+
APInt Size(IntTyBits, getStringLength(Mapper(CB->getArgOperand(0)),
383+
DL.getByteWidth()));
383384
if (!Size)
384385
return std::nullopt;
385386

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6608,17 +6608,14 @@ bool llvm::isGEPBasedOnPointerToString(const GEPOperator *GEP,
66086608
}
66096609

66106610
// If V refers to an initialized global constant, set Slice either to
6611-
// its initializer if the size of its elements equals ElementSize, or,
6612-
// for ElementSize == 8, to its representation as an array of unsiged
6613-
// char. Return true on success.
6614-
// Offset is in the unit "nr of ElementSize sized elements".
6611+
// its initializer if the bit width of its elements equals ElementBitWidth,
6612+
// or, for ElementBitWidth == CHAR_BIT, to its representation as an array
6613+
// of unsigned char. Return true on success.
6614+
// Offset is in the unit "nr of ElementBitWidth sized elements".
66156615
bool llvm::getConstantDataArrayInfo(const Value *V,
66166616
ConstantDataArraySlice &Slice,
6617-
unsigned ElementSize, uint64_t Offset) {
6617+
unsigned ElementBitWidth, uint64_t Offset) {
66186618
assert(V && "V should not be null.");
6619-
assert((ElementSize % 8) == 0 &&
6620-
"ElementSize expected to be a multiple of the size of a byte.");
6621-
unsigned ElementSizeInBytes = ElementSize / 8;
66226619

66236620
// Drill down into the pointer expression V, ignoring any intervening
66246621
// casts, and determine the identity of the object it references along
@@ -6630,6 +6627,11 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
66306627
return false;
66316628

66326629
const DataLayout &DL = GV->getDataLayout();
6630+
unsigned ByteWidth = DL.getByteWidth();
6631+
assert((ElementBitWidth % ByteWidth) == 0 &&
6632+
"ElementBitWidth is expected to be a multiple of the byte width");
6633+
unsigned ElementSizeInBytes = ElementBitWidth / ByteWidth;
6634+
66336635
APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
66346636

66356637
if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
@@ -6669,7 +6671,7 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
66696671
auto *Init = const_cast<Constant *>(GV->getInitializer());
66706672
if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
66716673
Type *InitElTy = ArrayInit->getElementType();
6672-
if (InitElTy->isIntegerTy(ElementSize)) {
6674+
if (InitElTy->isIntegerTy(ElementBitWidth)) {
66736675
// If Init is an initializer for an array of the expected type
66746676
// and size, use it as is.
66756677
Array = ArrayInit;
@@ -6678,7 +6680,7 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
66786680
}
66796681

66806682
if (!Array) {
6681-
if (ElementSize != 8)
6683+
if (ElementBitWidth != CHAR_BIT)
66826684
// TODO: Handle conversions to larger integral types.
66836685
return false;
66846686

@@ -6756,9 +6758,9 @@ bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
67566758

67576759
/// If we can compute the length of the string pointed to by
67586760
/// the specified pointer, return 'len+1'. If we can't, return 0.
6759-
static uint64_t GetStringLengthH(const Value *V,
6760-
SmallPtrSetImpl<const PHINode*> &PHIs,
6761-
unsigned CharSize) {
6761+
static uint64_t getStringLength(const Value *V,
6762+
SmallPtrSetImpl<const PHINode *> &PHIs,
6763+
unsigned CharWidth) {
67626764
// Look through noop bitcast instructions.
67636765
V = V->stripPointerCasts();
67646766

@@ -6771,7 +6773,7 @@ static uint64_t GetStringLengthH(const Value *V,
67716773
// If it was new, see if all the input strings are the same length.
67726774
uint64_t LenSoFar = ~0ULL;
67736775
for (Value *IncValue : PN->incoming_values()) {
6774-
uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6776+
uint64_t Len = getStringLength(IncValue, PHIs, CharWidth);
67756777
if (Len == 0) return 0; // Unknown length -> unknown.
67766778

67776779
if (Len == ~0ULL) continue;
@@ -6787,9 +6789,9 @@ static uint64_t GetStringLengthH(const Value *V,
67876789

67886790
// strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
67896791
if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6790-
uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6792+
uint64_t Len1 = getStringLength(SI->getTrueValue(), PHIs, CharWidth);
67916793
if (Len1 == 0) return 0;
6792-
uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6794+
uint64_t Len2 = getStringLength(SI->getFalseValue(), PHIs, CharWidth);
67936795
if (Len2 == 0) return 0;
67946796
if (Len1 == ~0ULL) return Len2;
67956797
if (Len2 == ~0ULL) return Len1;
@@ -6799,7 +6801,7 @@ static uint64_t GetStringLengthH(const Value *V,
67996801

68006802
// Otherwise, see if we can read the string.
68016803
ConstantDataArraySlice Slice;
6802-
if (!getConstantDataArrayInfo(V, Slice, CharSize))
6804+
if (!getConstantDataArrayInfo(V, Slice, CharWidth))
68036805
return 0;
68046806

68056807
if (Slice.Array == nullptr)
@@ -6821,12 +6823,12 @@ static uint64_t GetStringLengthH(const Value *V,
68216823

68226824
/// If we can compute the length of the string pointed to by
68236825
/// the specified pointer, return 'len+1'. If we can't, return 0.
6824-
uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6826+
uint64_t llvm::getStringLength(const Value *V, unsigned CharWidth) {
68256827
if (!V->getType()->isPointerTy())
68266828
return 0;
68276829

68286830
SmallPtrSet<const PHINode*, 32> PHIs;
6829-
uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6831+
uint64_t Len = ::getStringLength(V, PHIs, CharWidth);
68306832
// If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
68316833
// an empty string as a length.
68326834
return Len == ~0ULL ? 1 : Len;

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ Instruction *InstCombinerImpl::SimplifyAnyMemTransfer(AnyMemTransferInst *MI) {
157157
uint64_t Size = MemOpLength->getLimitedValue();
158158
assert(Size && "0-sized memory transferring should be removed already.");
159159

160-
if (Size > 8 || (Size&(Size-1)))
160+
uint64_t MemOpWidth = Size * DL.getByteWidth();
161+
if (MemOpWidth > 64 || (Size & (Size - 1)))
161162
return nullptr; // If not 1/2/4/8 bytes, exit.
162163

163164
// If it is an atomic and alignment is less than the size then we will
@@ -169,7 +170,7 @@ Instruction *InstCombinerImpl::SimplifyAnyMemTransfer(AnyMemTransferInst *MI) {
169170
return nullptr;
170171

171172
// Use an integer load+store unless we can find something better.
172-
IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3);
173+
IntegerType *IntType = IntegerType::get(MI->getContext(), MemOpWidth);
173174

174175
// If the memcpy has metadata describing the members, see if we can get the
175176
// TBAA, scope and noalias tags describing our copy.
@@ -245,7 +246,7 @@ Instruction *InstCombinerImpl::SimplifyAnyMemSet(AnyMemSetInst *MI) {
245246
// Extract the length and alignment and fill if they are constant.
246247
ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
247248
ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
248-
if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8))
249+
if (!LenC || !FillC || !FillC->getType()->isIntegerTy(DL.getByteWidth()))
249250
return nullptr;
250251
const uint64_t Len = LenC->getLimitedValue();
251252
assert(Len && "0-sized memory setting should be removed already.");
@@ -260,12 +261,13 @@ Instruction *InstCombinerImpl::SimplifyAnyMemSet(AnyMemSetInst *MI) {
260261
return nullptr;
261262

262263
// memset(s,c,n) -> store s, c (for n=1,2,4,8)
263-
if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
264+
uint64_t MemOpWidth = Len * DL.getByteWidth();
265+
if (MemOpWidth <= 64 && isPowerOf2_32((uint32_t)Len)) {
264266
Value *Dest = MI->getDest();
265267

266268
// Extract the fill value and store.
267269
Constant *FillVal = ConstantInt::get(
268-
MI->getContext(), APInt::getSplat(Len * 8, FillC->getValue()));
270+
MI->getContext(), APInt::getSplat(MemOpWidth, FillC->getValue()));
269271
StoreInst *S = Builder.CreateStore(FillVal, Dest, MI->isVolatile());
270272
S->copyMetadata(*MI, LLVMContext::MD_DIAssignID);
271273
auto replaceOpForAssignmentMarkers = [FillC, FillVal](auto *DbgAssign) {

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2973,8 +2973,9 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
29732973
if (MadeChange)
29742974
return &GEP;
29752975

2976-
// Canonicalize constant GEPs to i8 type.
2977-
if (!GEPEltType->isIntegerTy(8) && GEP.hasAllConstantIndices()) {
2976+
// Canonicalize constant GEPs to byte type.
2977+
if (!GEPEltType->isIntegerTy(DL.getByteWidth()) &&
2978+
GEP.hasAllConstantIndices()) {
29782979
APInt Offset(DL.getIndexTypeSizeInBits(GEPType), 0);
29792980
if (GEP.accumulateConstantOffset(DL, Offset))
29802981
return replaceInstUsesWith(

0 commit comments

Comments
 (0)