@@ -1482,19 +1482,23 @@ LLVM_DUMP_METHOD void AllocaSlices::dump() const { print(dbgs()); }
14821482
14831483// / Walk the range of a partitioning looking for a common type to cover this
14841484// / sequence of slices.
1485- static std::pair<Type *, IntegerType *>
1485+ // / Returns: {CommonType, LargestIntegerType, OnlyIntrinsicUsers}
1486+ static std::tuple<Type *, IntegerType *, bool >
14861487findCommonType (AllocaSlices::const_iterator B, AllocaSlices::const_iterator E,
14871488 uint64_t EndOffset) {
14881489 Type *Ty = nullptr ;
14891490 bool TyIsCommon = true ;
14901491 IntegerType *ITy = nullptr ;
1492+ bool OnlyIntrinsicUsers = true ;
14911493
14921494 // Note that we need to look at *every* alloca slice's Use to ensure we
14931495 // always get consistent results regardless of the order of slices.
14941496 for (AllocaSlices::const_iterator I = B; I != E; ++I) {
14951497 Use *U = I->getUse ();
14961498 if (isa<IntrinsicInst>(*U->getUser ()))
14971499 continue ;
1500+ // We found a non-intrinsic user
1501+ OnlyIntrinsicUsers = false ;
14981502 if (I->beginOffset () != B->beginOffset () || I->endOffset () != EndOffset)
14991503 continue ;
15001504
@@ -1528,7 +1532,7 @@ findCommonType(AllocaSlices::const_iterator B, AllocaSlices::const_iterator E,
15281532 Ty = UserTy;
15291533 }
15301534
1531- return {TyIsCommon ? Ty : nullptr , ITy};
1535+ return {TyIsCommon ? Ty : nullptr , ITy, OnlyIntrinsicUsers };
15321536}
15331537
15341538// / PHI instructions that use an alloca and are subsequently loaded can be
@@ -5206,20 +5210,24 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
52065210
52075211 // Otherwise, check if there is a common type that all slices of the
52085212 // partition use. Collect the largest integer type used as a backup.
5209- auto CommonUseTy = findCommonType (P.begin (), P.end (), P.endOffset ());
5213+ auto [CommonUseTy, LargestIntTy, OnlyIntrinsicUsers] =
5214+ findCommonType (P.begin (), P.end (), P.endOffset ());
52105215 // If there is a common type that spans the partition, use it.
5211- if (CommonUseTy. first ) {
5212- TypeSize CommonUseSize = DL.getTypeAllocSize (CommonUseTy. first );
5216+ if (CommonUseTy) {
5217+ TypeSize CommonUseSize = DL.getTypeAllocSize (CommonUseTy);
52135218 if (CommonUseSize.isFixed () &&
52145219 CommonUseSize.getFixedValue () >= P.size ()) {
52155220
52165221 if (VecTy)
52175222 return {VecTy, false , VecTy};
5218- return {CommonUseTy. first ,
5219- isIntegerWideningViable (P, CommonUseTy. first , DL), nullptr };
5223+ return {CommonUseTy, isIntegerWideningViable (P, CommonUseTy, DL) ,
5224+ nullptr };
52205225 }
52215226 }
52225227
5228+ if (OnlyIntrinsicUsers && DL.isLegalInteger (P.size () * 8 ))
5229+ return {Type::getIntNTy (*C, P.size () * 8 ), false , nullptr };
5230+
52235231 // If not, can we find an appropriate subtype in the original allocated
52245232 // type?
52255233 if (Type *TypePartitionTy = getTypePartition (DL, AI.getAllocatedType (),
@@ -5233,17 +5241,17 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
52335241 return {TypePartitionTy, true , nullptr };
52345242 if (VecTy)
52355243 return {VecTy, false , VecTy};
5236- if (CommonUseTy. second &&
5237- DL.getTypeAllocSize (CommonUseTy. second ).getFixedValue () >= P.size () &&
5238- isIntegerWideningViable (P, CommonUseTy. second , DL))
5239- return {CommonUseTy. second , true , nullptr };
5244+ if (LargestIntTy &&
5245+ DL.getTypeAllocSize (LargestIntTy ).getFixedValue () >= P.size () &&
5246+ isIntegerWideningViable (P, LargestIntTy , DL))
5247+ return {LargestIntTy , true , nullptr };
52405248 return {TypePartitionTy, false , nullptr };
52415249 }
52425250
52435251 // If still not, can we use the largest bitwidth integer type used?
5244- if (CommonUseTy. second &&
5245- DL.getTypeAllocSize (CommonUseTy. second ).getFixedValue () >= P.size ())
5246- return {CommonUseTy. second , false , nullptr };
5252+ if (LargestIntTy &&
5253+ DL.getTypeAllocSize (LargestIntTy ).getFixedValue () >= P.size ())
5254+ return {LargestIntTy , false , nullptr };
52475255
52485256 if (DL.isLegalInteger (P.size () * 8 ))
52495257 return {Type::getIntNTy (*C, P.size () * 8 ), false , nullptr };
0 commit comments