Index: lib/Transforms/Scalar/SROA.cpp =================================================================== --- lib/Transforms/Scalar/SROA.cpp +++ lib/Transforms/Scalar/SROA.cpp @@ -1073,9 +1073,7 @@ static Type *findCommonType(AllocaSlices::const_iterator B, AllocaSlices::const_iterator E, uint64_t EndOffset) { - Type *Ty = nullptr; - bool TyIsCommon = true; - IntegerType *ITy = nullptr; + SmallDenseMap TypeCounters; // Note that we need to look at *every* alloca slice's Use to ensure we // always get consistent results regardless of the order of slices. @@ -1101,22 +1099,30 @@ if (UserITy->getBitWidth() % 8 != 0 || UserITy->getBitWidth() / 8 > (EndOffset - B->beginOffset())) continue; - - // Track the largest bitwidth integer type used in this way in case there - // is no common type. - if (!ITy || ITy->getBitWidth() < UserITy->getBitWidth()) - ITy = UserITy; } - // To avoid depending on the order of slices, Ty and TyIsCommon must not - // depend on types skipped above. - if (!UserTy || (Ty && Ty != UserTy)) - TyIsCommon = false; // Give up on anything but an iN type. - else - Ty = UserTy; + if (UserTy) + ++TypeCounters[UserTy]; + } + + // Look for the type that is most frequently used. + Type *Ty = nullptr; + int max_counter = 0; + for (auto I = TypeCounters.begin(); I != TypeCounters.end(); ++I) { + IntegerType *NewITy = dyn_cast_or_null(I->getFirst()); + if (I->getSecond() > max_counter) { + Ty = I->getFirst(); + max_counter = I->getSecond(); + } else if (I->getSecond() == max_counter && NewITy) { + IntegerType *MaxITy = dyn_cast_or_null(Ty); + if (!MaxITy || MaxITy->getBitWidth() < NewITy->getBitWidth()) { + Ty = I->getFirst(); + max_counter = I->getSecond(); + } + } } - return TyIsCommon ? Ty : ITy; + return Ty; } /// PHI instructions that use an alloca and are subsequently loaded can be