@@ -5200,35 +5200,41 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
52005200 // won't always succeed, in which case we fall back to a legal integer type
52015201 // or an i8 array of an appropriate size.
52025202 auto SelectPartitionTy = [&]() -> std::tuple<Type *, bool , VectorType *> {
5203- // First check if the partition is viable for vetor promotion. If it is
5204- // via a floating-point vector, we are done because we would never prefer
5205- // integer widening.
5203+ // First check if the partition is viable for vetor promotion.
5204+ // We prefer vector promotion over integer widening promotion when:
5205+ // - The vector element type is a floating-point type.
5206+ // - All the loads/stores to the alloca are vector loads/stores to the entire alloca.
5207+ // Otherwise when there is a integer vector with mixed loads/stores we prefer integer widening
5208+ // promotion because it's more likely the user is doing bitwise arithmetic and we
5209+ // generate better code.
52065210 VectorType *VecTy =
52075211 isVectorPromotionViable (P, DL, AI.getFunction ()->getVScaleValue ());
5208- if (VecTy) {
5209- if (VecTy->getElementType ()->isFloatingPointTy ()) {
5210- return {VecTy, false , VecTy};
5211- }
5212- }
5213- // Otherwise, check if there is a common type that all slices of the
5214- // partition use that spans the partition.
5212+ // If the vector element type is a floating-point type, we prefer vector promotion.
5213+ if (VecTy && VecTy->getElementType ()->isFloatingPointTy ())
5214+ return {VecTy, false , VecTy};
5215+
5216+ // Check if there is a common type that all slices of the partition use that spans the partition.
52155217 auto [CommonUseTy, LargestIntTy, OnlyIntrinsicUsers] =
52165218 findCommonType (P.begin (), P.end (), P.endOffset ());
52175219 if (CommonUseTy) {
52185220 TypeSize CommonUseSize = DL.getTypeAllocSize (CommonUseTy);
52195221 if (CommonUseSize.isFixed () &&
52205222 CommonUseSize.getFixedValue () >= P.size ()) {
5221- // Prefer vector promotion here because we already calculated it.
5223+ // We prefer vector promotion here because if vector promotion is viable and
5224+ // there is a common type used, then it implies the second listed condition for prefering
5225+ // vector promotion is true.
52225226 if (VecTy)
52235227 return {VecTy, false , VecTy};
52245228 return {CommonUseTy, isIntegerWideningViable (P, CommonUseTy, DL),
52255229 nullptr };
52265230 }
52275231 }
5232+
52285233 // If there are only intrinsic users, try to represent as a legal integer type
52295234 // because we are probably just copying data around and the integer can be promoted.
52305235 if (OnlyIntrinsicUsers && DL.isLegalInteger (P.size () * 8 ))
52315236 return {Type::getIntNTy (*C, P.size () * 8 ), false , nullptr };
5237+
52325238 // Can we find an appropriate subtype in the original allocated
52335239 // type?
52345240 if (Type *TypePartitionTy = getTypePartition (DL, AI.getAllocatedType (),
@@ -5239,25 +5245,30 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
52395245 TypePartitionTy->getArrayElementType ()->isIntegerTy () &&
52405246 DL.isLegalInteger (P.size () * 8 ))
52415247 TypePartitionTy = Type::getIntNTy (*C, P.size () * 8 );
5248+ // There was no common type used, so we prefer integer widening promotion.
52425249 if (isIntegerWideningViable (P, TypePartitionTy, DL))
52435250 return {TypePartitionTy, true , nullptr };
52445251 if (VecTy)
52455252 return {VecTy, false , VecTy};
5253+ // If we couldn't promotion with TypePartitionTy, try with the largest integer type used.
52465254 if (LargestIntTy &&
52475255 DL.getTypeAllocSize (LargestIntTy).getFixedValue () >= P.size () &&
52485256 isIntegerWideningViable (P, LargestIntTy, DL))
52495257 return {LargestIntTy, true , nullptr };
5258+ // Fallback to TypePartitionTy and we probably won't promote.
52505259 return {TypePartitionTy, false , nullptr };
52515260 }
52525261
5253- // If still not, can we use the largest bitwidth integer type used?
5262+ // Select the largest integer type used if it spans the partition.
52545263 if (LargestIntTy &&
52555264 DL.getTypeAllocSize (LargestIntTy).getFixedValue () >= P.size ())
52565265 return {LargestIntTy, false , nullptr };
52575266
5267+ // Select a legal integer type if it spans the partition.
52585268 if (DL.isLegalInteger (P.size () * 8 ))
52595269 return {Type::getIntNTy (*C, P.size () * 8 ), false , nullptr };
52605270
5271+ // Fallback to an i8 array.
52615272 return {ArrayType::get (Type::getInt8Ty (*C), P.size ()), false , nullptr };
52625273 };
52635274
0 commit comments