@@ -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".
66156615bool 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;
0 commit comments