@@ -6472,17 +6472,14 @@ bool llvm::isGEPBasedOnPointerToString(const GEPOperator *GEP,
64726472}
64736473
64746474// If V refers to an initialized global constant, set Slice either to
6475- // its initializer if the size of its elements equals ElementSize, or ,
6476- // for ElementSize == 8 , to its representation as an array of unsiged
6477- // char. Return true on success.
6478- // Offset is in the unit "nr of ElementSize sized elements".
6475+ // its initializer if the bit width of its elements equals ElementBitWidth ,
6476+ // or, for ElementBitWidth == CHAR_BIT , to its representation as an array
6477+ // of unsigned char. Return true on success.
6478+ // Offset is in the unit "nr of ElementBitWidth sized elements".
64796479bool llvm::getConstantDataArrayInfo (const Value *V,
64806480 ConstantDataArraySlice &Slice,
6481- unsigned ElementSize , uint64_t Offset) {
6481+ unsigned ElementBitWidth , uint64_t Offset) {
64826482 assert (V && " V should not be null." );
6483- assert ((ElementSize % 8 ) == 0 &&
6484- " ElementSize expected to be a multiple of the size of a byte." );
6485- unsigned ElementSizeInBytes = ElementSize / 8 ;
64866483
64876484 // Drill down into the pointer expression V, ignoring any intervening
64886485 // casts, and determine the identity of the object it references along
@@ -6494,6 +6491,11 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
64946491 return false ;
64956492
64966493 const DataLayout &DL = GV->getDataLayout ();
6494+ unsigned ByteWidth = DL.getByteWidth ();
6495+ assert ((ElementBitWidth % ByteWidth) == 0 &&
6496+ " ElementBitWidth is expected to be a multiple of the byte width" );
6497+ unsigned ElementSizeInBytes = ElementBitWidth / ByteWidth;
6498+
64976499 APInt Off (DL.getIndexTypeSizeInBits (V->getType ()), 0 );
64986500
64996501 if (GV != V->stripAndAccumulateConstantOffsets (DL, Off,
@@ -6533,7 +6535,7 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
65336535 auto *Init = const_cast <Constant *>(GV->getInitializer ());
65346536 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
65356537 Type *InitElTy = ArrayInit->getElementType ();
6536- if (InitElTy->isIntegerTy (ElementSize )) {
6538+ if (InitElTy->isIntegerTy (ElementBitWidth )) {
65376539 // If Init is an initializer for an array of the expected type
65386540 // and size, use it as is.
65396541 Array = ArrayInit;
@@ -6542,7 +6544,7 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
65426544 }
65436545
65446546 if (!Array) {
6545- if (ElementSize != 8 )
6547+ if (ElementBitWidth != CHAR_BIT )
65466548 // TODO: Handle conversions to larger integral types.
65476549 return false ;
65486550
@@ -6620,9 +6622,9 @@ bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
66206622
66216623// / If we can compute the length of the string pointed to by
66226624// / the specified pointer, return 'len+1'. If we can't, return 0.
6623- static uint64_t GetStringLengthH (const Value *V,
6624- SmallPtrSetImpl<const PHINode*> &PHIs,
6625- unsigned CharSize ) {
6625+ static uint64_t getStringLength (const Value *V,
6626+ SmallPtrSetImpl<const PHINode *> &PHIs,
6627+ unsigned CharWidth ) {
66266628 // Look through noop bitcast instructions.
66276629 V = V->stripPointerCasts ();
66286630
@@ -6635,7 +6637,7 @@ static uint64_t GetStringLengthH(const Value *V,
66356637 // If it was new, see if all the input strings are the same length.
66366638 uint64_t LenSoFar = ~0ULL ;
66376639 for (Value *IncValue : PN->incoming_values ()) {
6638- uint64_t Len = GetStringLengthH (IncValue, PHIs, CharSize );
6640+ uint64_t Len = getStringLength (IncValue, PHIs, CharWidth );
66396641 if (Len == 0 ) return 0 ; // Unknown length -> unknown.
66406642
66416643 if (Len == ~0ULL ) continue ;
@@ -6651,9 +6653,9 @@ static uint64_t GetStringLengthH(const Value *V,
66516653
66526654 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
66536655 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6654- uint64_t Len1 = GetStringLengthH (SI->getTrueValue (), PHIs, CharSize );
6656+ uint64_t Len1 = getStringLength (SI->getTrueValue (), PHIs, CharWidth );
66556657 if (Len1 == 0 ) return 0 ;
6656- uint64_t Len2 = GetStringLengthH (SI->getFalseValue (), PHIs, CharSize );
6658+ uint64_t Len2 = getStringLength (SI->getFalseValue (), PHIs, CharWidth );
66576659 if (Len2 == 0 ) return 0 ;
66586660 if (Len1 == ~0ULL ) return Len2;
66596661 if (Len2 == ~0ULL ) return Len1;
@@ -6663,7 +6665,7 @@ static uint64_t GetStringLengthH(const Value *V,
66636665
66646666 // Otherwise, see if we can read the string.
66656667 ConstantDataArraySlice Slice;
6666- if (!getConstantDataArrayInfo (V, Slice, CharSize ))
6668+ if (!getConstantDataArrayInfo (V, Slice, CharWidth ))
66676669 return 0 ;
66686670
66696671 if (Slice.Array == nullptr )
@@ -6685,12 +6687,12 @@ static uint64_t GetStringLengthH(const Value *V,
66856687
66866688// / If we can compute the length of the string pointed to by
66876689// / the specified pointer, return 'len+1'. If we can't, return 0.
6688- uint64_t llvm::GetStringLength (const Value *V, unsigned CharSize ) {
6690+ uint64_t llvm::getStringLength (const Value *V, unsigned CharWidth ) {
66896691 if (!V->getType ()->isPointerTy ())
66906692 return 0 ;
66916693
66926694 SmallPtrSet<const PHINode*, 32 > PHIs;
6693- uint64_t Len = GetStringLengthH (V, PHIs, CharSize );
6695+ uint64_t Len = :: getStringLength (V, PHIs, CharWidth );
66946696 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
66956697 // an empty string as a length.
66966698 return Len == ~0ULL ? 1 : Len;
0 commit comments