@@ -5196,7 +5196,10 @@ bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) {
51965196AllocaInst *SROA::rewritePartition (AllocaInst &AI, AllocaSlices &AS,
51975197 Partition &P) {
51985198 const DataLayout &DL = AI.getDataLayout ();
5199- auto ComputePartitionTy = [&]() -> std::tuple<Type *, bool , VectorType *> {
5199+ // Try to compute a friendly type for this partition of the alloca. This
5200+ // won't always succeed, in which case we fall back to a legal integer type
5201+ // or an i8 array of an appropriate size.
5202+ auto SelectPartitionTy = [&]() -> std::tuple<Type *, bool , VectorType *> {
52005203 // First check if the partition is viable for vetor promotion. If it is
52015204 // via a floating-point vector, we are done because we would never prefer
52025205 // integer widening.
@@ -5207,36 +5210,35 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
52075210 return {VecTy, false , VecTy};
52085211 }
52095212 }
5210-
52115213 // Otherwise, check if there is a common type that all slices of the
5212- // partition use. Collect the largest integer type used as a backup .
5214+ // partition use that spans the partition .
52135215 auto [CommonUseTy, LargestIntTy, OnlyIntrinsicUsers] =
52145216 findCommonType (P.begin (), P.end (), P.endOffset ());
5215- // If there is a common type that spans the partition, use it.
52165217 if (CommonUseTy) {
52175218 TypeSize CommonUseSize = DL.getTypeAllocSize (CommonUseTy);
52185219 if (CommonUseSize.isFixed () &&
52195220 CommonUseSize.getFixedValue () >= P.size ()) {
5220-
5221+ // Prefer vector promotion here because we already calculated it.
52215222 if (VecTy)
52225223 return {VecTy, false , VecTy};
52235224 return {CommonUseTy, isIntegerWideningViable (P, CommonUseTy, DL),
52245225 nullptr };
52255226 }
52265227 }
5227-
5228+ // If there are only intrinsic users, try to represent as a legal integer type
5229+ // because we are probably just copying data around and the integer can be promoted.
52285230 if (OnlyIntrinsicUsers && DL.isLegalInteger (P.size () * 8 ))
52295231 return {Type::getIntNTy (*C, P.size () * 8 ), false , nullptr };
5230-
5231- // If not, can we find an appropriate subtype in the original allocated
5232+ // Can we find an appropriate subtype in the original allocated
52325233 // type?
52335234 if (Type *TypePartitionTy = getTypePartition (DL, AI.getAllocatedType (),
52345235 P.beginOffset (), P.size ())) {
5236+ // If the partition is an integer array that can be spanned by a legal integer type,
5237+ // prefer to represent it as a legal integer type because it's more likely to be promotable.
52355238 if (TypePartitionTy->isArrayTy () &&
52365239 TypePartitionTy->getArrayElementType ()->isIntegerTy () &&
52375240 DL.isLegalInteger (P.size () * 8 ))
52385241 TypePartitionTy = Type::getIntNTy (*C, P.size () * 8 );
5239-
52405242 if (isIntegerWideningViable (P, TypePartitionTy, DL))
52415243 return {TypePartitionTy, true , nullptr };
52425244 if (VecTy)
@@ -5259,7 +5261,7 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
52595261 return {ArrayType::get (Type::getInt8Ty (*C), P.size ()), false , nullptr };
52605262 };
52615263
5262- auto [PartitionTy, IsIntegerPromotable, VecTy] = ComputePartitionTy ();
5264+ auto [PartitionTy, IsIntegerPromotable, VecTy] = SelectPartitionTy ();
52635265
52645266 // Check for the case where we're going to rewrite to a new alloca of the
52655267 // exact same type as the original, and with the same access offsets. In that
0 commit comments