@@ -6236,17 +6236,14 @@ bool llvm::isGEPBasedOnPointerToString(const GEPOperator *GEP,
62366236}
62376237
62386238// If V refers to an initialized global constant, set Slice either to
6239- // its initializer if the size of its elements equals ElementSize, or ,
6240- // for ElementSize == 8 , to its representation as an array of unsiged
6241- // char. Return true on success.
6242- // Offset is in the unit "nr of ElementSize sized elements".
6239+ // its initializer if the bit width of its elements equals ElementBitWidth ,
6240+ // or, for ElementBitWidth == CHAR_BIT , to its representation as an array
6241+ // of unsigned char. Return true on success.
6242+ // Offset is in the unit "nr of ElementBitWidth sized elements".
62436243bool llvm::getConstantDataArrayInfo (const Value *V,
62446244 ConstantDataArraySlice &Slice,
6245- unsigned ElementSize , uint64_t Offset) {
6245+ unsigned ElementBitWidth , uint64_t Offset) {
62466246 assert (V && " V should not be null." );
6247- assert ((ElementSize % 8 ) == 0 &&
6248- " ElementSize expected to be a multiple of the size of a byte." );
6249- unsigned ElementSizeInBytes = ElementSize / 8 ;
62506247
62516248 // Drill down into the pointer expression V, ignoring any intervening
62526249 // casts, and determine the identity of the object it references along
@@ -6258,6 +6255,11 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
62586255 return false ;
62596256
62606257 const DataLayout &DL = GV->getDataLayout ();
6258+ unsigned ByteWidth = DL.getByteWidth ();
6259+ assert ((ElementBitWidth % ByteWidth) == 0 &&
6260+ " ElementBitWidth is expected to be a multiple of the byte width" );
6261+ unsigned ElementSizeInBytes = ElementBitWidth / ByteWidth;
6262+
62616263 APInt Off (DL.getIndexTypeSizeInBits (V->getType ()), 0 );
62626264
62636265 if (GV != V->stripAndAccumulateConstantOffsets (DL, Off,
@@ -6297,7 +6299,7 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
62976299 auto *Init = const_cast <Constant *>(GV->getInitializer ());
62986300 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
62996301 Type *InitElTy = ArrayInit->getElementType ();
6300- if (InitElTy->isIntegerTy (ElementSize )) {
6302+ if (InitElTy->isIntegerTy (ElementBitWidth )) {
63016303 // If Init is an initializer for an array of the expected type
63026304 // and size, use it as is.
63036305 Array = ArrayInit;
@@ -6306,7 +6308,7 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
63066308 }
63076309
63086310 if (!Array) {
6309- if (ElementSize != 8 )
6311+ if (ElementBitWidth != CHAR_BIT )
63106312 // TODO: Handle conversions to larger integral types.
63116313 return false ;
63126314
@@ -6384,9 +6386,9 @@ bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
63846386
63856387// / If we can compute the length of the string pointed to by
63866388// / the specified pointer, return 'len+1'. If we can't, return 0.
6387- static uint64_t GetStringLengthH (const Value *V,
6388- SmallPtrSetImpl<const PHINode*> &PHIs,
6389- unsigned CharSize ) {
6389+ static uint64_t getStringLength (const Value *V,
6390+ SmallPtrSetImpl<const PHINode *> &PHIs,
6391+ unsigned CharWidth ) {
63906392 // Look through noop bitcast instructions.
63916393 V = V->stripPointerCasts ();
63926394
@@ -6399,7 +6401,7 @@ static uint64_t GetStringLengthH(const Value *V,
63996401 // If it was new, see if all the input strings are the same length.
64006402 uint64_t LenSoFar = ~0ULL ;
64016403 for (Value *IncValue : PN->incoming_values ()) {
6402- uint64_t Len = GetStringLengthH (IncValue, PHIs, CharSize );
6404+ uint64_t Len = getStringLength (IncValue, PHIs, CharWidth );
64036405 if (Len == 0 ) return 0 ; // Unknown length -> unknown.
64046406
64056407 if (Len == ~0ULL ) continue ;
@@ -6415,9 +6417,9 @@ static uint64_t GetStringLengthH(const Value *V,
64156417
64166418 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
64176419 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6418- uint64_t Len1 = GetStringLengthH (SI->getTrueValue (), PHIs, CharSize );
6420+ uint64_t Len1 = getStringLength (SI->getTrueValue (), PHIs, CharWidth );
64196421 if (Len1 == 0 ) return 0 ;
6420- uint64_t Len2 = GetStringLengthH (SI->getFalseValue (), PHIs, CharSize );
6422+ uint64_t Len2 = getStringLength (SI->getFalseValue (), PHIs, CharWidth );
64216423 if (Len2 == 0 ) return 0 ;
64226424 if (Len1 == ~0ULL ) return Len2;
64236425 if (Len2 == ~0ULL ) return Len1;
@@ -6427,7 +6429,7 @@ static uint64_t GetStringLengthH(const Value *V,
64276429
64286430 // Otherwise, see if we can read the string.
64296431 ConstantDataArraySlice Slice;
6430- if (!getConstantDataArrayInfo (V, Slice, CharSize ))
6432+ if (!getConstantDataArrayInfo (V, Slice, CharWidth ))
64316433 return 0 ;
64326434
64336435 if (Slice.Array == nullptr )
@@ -6449,12 +6451,12 @@ static uint64_t GetStringLengthH(const Value *V,
64496451
64506452// / If we can compute the length of the string pointed to by
64516453// / the specified pointer, return 'len+1'. If we can't, return 0.
6452- uint64_t llvm::GetStringLength (const Value *V, unsigned CharSize ) {
6454+ uint64_t llvm::getStringLength (const Value *V, unsigned CharWidth ) {
64536455 if (!V->getType ()->isPointerTy ())
64546456 return 0 ;
64556457
64566458 SmallPtrSet<const PHINode*, 32 > PHIs;
6457- uint64_t Len = GetStringLengthH (V, PHIs, CharSize );
6459+ uint64_t Len = :: getStringLength (V, PHIs, CharWidth );
64586460 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
64596461 // an empty string as a length.
64606462 return Len == ~0ULL ? 1 : Len;
0 commit comments