@@ -3869,6 +3869,15 @@ static bool arePointersCompatible(Value *Ptr1, Value *Ptr2,
38693869 .getOpcode());
38703870}
38713871
3872+ /// Calculates minimal alignment as a common alignment.
3873+ template <typename T>
3874+ static Align computeCommonAlignment(ArrayRef<Value *> VL) {
3875+ Align CommonAlignment = cast<T>(VL.front())->getAlign();
3876+ for (Value *V : VL.drop_front())
3877+ CommonAlignment = std::min(CommonAlignment, cast<T>(V)->getAlign());
3878+ return CommonAlignment;
3879+ }
3880+
38723881/// Checks if the given array of loads can be represented as a vectorized,
38733882/// scatter or just simple gather.
38743883static LoadsState canVectorizeLoads(ArrayRef<Value *> VL, const Value *VL0,
@@ -3939,10 +3948,7 @@ static LoadsState canVectorizeLoads(ArrayRef<Value *> VL, const Value *VL0,
39393948 return (IsSorted && !GEP && doesNotNeedToBeScheduled(P)) ||
39403949 (GEP && GEP->getNumOperands() == 2);
39413950 })) {
3942- Align CommonAlignment = cast<LoadInst>(VL0)->getAlign();
3943- for (Value *V : VL)
3944- CommonAlignment =
3945- std::min(CommonAlignment, cast<LoadInst>(V)->getAlign());
3951+ Align CommonAlignment = computeCommonAlignment<LoadInst>(VL);
39463952 auto *VecTy = FixedVectorType::get(ScalarTy, VL.size());
39473953 if (TTI.isLegalMaskedGather(VecTy, CommonAlignment) &&
39483954 !TTI.forceScalarizeMaskedGather(VecTy, CommonAlignment))
@@ -7087,10 +7093,8 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
70877093 }
70887094 for (std::pair<unsigned, unsigned> P : ScatterVectorized) {
70897095 auto *LI0 = cast<LoadInst>(VL[P.first]);
7090- Align CommonAlignment = LI0->getAlign();
7091- for (Value *V : VL.slice(P.first + 1, VF - 1))
7092- CommonAlignment =
7093- std::min(CommonAlignment, cast<LoadInst>(V)->getAlign());
7096+ Align CommonAlignment =
7097+ computeCommonAlignment<LoadInst>(VL.slice(P.first + 1, VF - 1));
70947098 GatherCost += TTI.getGatherScatterOpCost(
70957099 Instruction::Load, LoadTy, LI0->getPointerOperand(),
70967100 /*VariableMask=*/false, CommonAlignment, CostKind, LI0);
@@ -8334,10 +8338,8 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
83348338 assert((E->State == TreeEntry::ScatterVectorize ||
83358339 E->State == TreeEntry::PossibleStridedVectorize) &&
83368340 "Unknown EntryState");
8337- Align CommonAlignment = LI0->getAlign();
8338- for (Value *V : UniqueValues)
8339- CommonAlignment =
8340- std::min(CommonAlignment, cast<LoadInst>(V)->getAlign());
8341+ Align CommonAlignment =
8342+ computeCommonAlignment<LoadInst>(UniqueValues.getArrayRef());
83418343 VecLdCost = TTI->getGatherScatterOpCost(
83428344 Instruction::Load, VecTy, LI0->getPointerOperand(),
83438345 /*VariableMask=*/false, CommonAlignment, CostKind);
@@ -11600,10 +11602,7 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E, bool PostponedPHIs) {
1160011602 return E->VectorizedValue;
1160111603 }
1160211604 // Use the minimum alignment of the gathered loads.
11603- Align CommonAlignment = LI->getAlign();
11604- for (Value *V : E->Scalars)
11605- CommonAlignment =
11606- std::min(CommonAlignment, cast<LoadInst>(V)->getAlign());
11605+ Align CommonAlignment = computeCommonAlignment<LoadInst>(E->Scalars);
1160711606 NewLI = Builder.CreateMaskedGather(VecTy, VecPtr, CommonAlignment);
1160811607 }
1160911608 Value *V = propagateMetadata(NewLI, E->Scalars);
0 commit comments