Skip to content

Commit 2a61be4

Browse files
committed
[SROA] NFC: Extract code to checkVectorTypesForPromotion
Change-Id: Ib6f237cc791a097f8f2411bc1d6502f11d4a748e
1 parent 6a3ace2 commit 2a61be4

File tree

1 file changed

+99
-82
lines changed

1 file changed

+99
-82
lines changed

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 99 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,8 +2138,9 @@ static bool isVectorPromotionViableForSlice(Partition &P, const Slice &S,
21382138

21392139
/// Test whether a vector type is viable for promotion.
21402140
///
2141-
/// This implements the necessary checking for \c isVectorPromotionViable over
2142-
/// all slices of the alloca for the given VectorType.
2141+
/// This implements the necessary checking for \c checkVectorTypesForPromotion
2142+
/// (and thus isVectorPromotionViable) over all slices of the alloca for the
2143+
/// given VectorType.
21432144
static bool checkVectorTypeForPromotion(Partition &P, VectorType *VTy,
21442145
const DataLayout &DL) {
21452146
uint64_t ElementSize =
@@ -2164,6 +2165,98 @@ static bool checkVectorTypeForPromotion(Partition &P, VectorType *VTy,
21642165
return true;
21652166
}
21662167

2168+
/// Test whether any vector type in \p CandidateTys is viable for promotion.
2169+
///
2170+
/// This implements the necessary checking for \c isVectorPromotionViable over
2171+
/// all slices of the alloca for the given VectorType.
2172+
static VectorType *
2173+
checkVectorTypesForPromotion(Partition &P, const DataLayout &DL,
2174+
SmallVectorImpl<VectorType *> &CandidateTys,
2175+
bool HaveCommonEltTy, Type *CommonEltTy,
2176+
bool HaveVecPtrTy, bool HaveCommonVecPtrTy,
2177+
VectorType *CommonVecPtrTy) {
2178+
// If we didn't find a vector type, nothing to do here.
2179+
if (CandidateTys.empty())
2180+
return nullptr;
2181+
2182+
// Pointer-ness is sticky, if we had a vector-of-pointers candidate type,
2183+
// then we should choose it, not some other alternative.
2184+
// But, we can't perform a no-op pointer address space change via bitcast,
2185+
// so if we didn't have a common pointer element type, bail.
2186+
if (HaveVecPtrTy && !HaveCommonVecPtrTy)
2187+
return nullptr;
2188+
2189+
// Try to pick the "best" element type out of the choices.
2190+
if (!HaveCommonEltTy && HaveVecPtrTy) {
2191+
// If there was a pointer element type, there's really only one choice.
2192+
CandidateTys.clear();
2193+
CandidateTys.push_back(CommonVecPtrTy);
2194+
} else if (!HaveCommonEltTy && !HaveVecPtrTy) {
2195+
// Integer-ify vector types.
2196+
for (VectorType *&VTy : CandidateTys) {
2197+
if (!VTy->getElementType()->isIntegerTy())
2198+
VTy = cast<VectorType>(VTy->getWithNewType(IntegerType::getIntNTy(
2199+
VTy->getContext(), VTy->getScalarSizeInBits())));
2200+
}
2201+
2202+
// Rank the remaining candidate vector types. This is easy because we know
2203+
// they're all integer vectors. We sort by ascending number of elements.
2204+
auto RankVectorTypesComp = [&DL](VectorType *RHSTy, VectorType *LHSTy) {
2205+
(void)DL;
2206+
assert(DL.getTypeSizeInBits(RHSTy).getFixedValue() ==
2207+
DL.getTypeSizeInBits(LHSTy).getFixedValue() &&
2208+
"Cannot have vector types of different sizes!");
2209+
assert(RHSTy->getElementType()->isIntegerTy() &&
2210+
"All non-integer types eliminated!");
2211+
assert(LHSTy->getElementType()->isIntegerTy() &&
2212+
"All non-integer types eliminated!");
2213+
return cast<FixedVectorType>(RHSTy)->getNumElements() <
2214+
cast<FixedVectorType>(LHSTy)->getNumElements();
2215+
};
2216+
auto RankVectorTypesEq = [&DL](VectorType *RHSTy, VectorType *LHSTy) {
2217+
(void)DL;
2218+
assert(DL.getTypeSizeInBits(RHSTy).getFixedValue() ==
2219+
DL.getTypeSizeInBits(LHSTy).getFixedValue() &&
2220+
"Cannot have vector types of different sizes!");
2221+
assert(RHSTy->getElementType()->isIntegerTy() &&
2222+
"All non-integer types eliminated!");
2223+
assert(LHSTy->getElementType()->isIntegerTy() &&
2224+
"All non-integer types eliminated!");
2225+
return cast<FixedVectorType>(RHSTy)->getNumElements() ==
2226+
cast<FixedVectorType>(LHSTy)->getNumElements();
2227+
};
2228+
llvm::sort(CandidateTys, RankVectorTypesComp);
2229+
CandidateTys.erase(std::unique(CandidateTys.begin(), CandidateTys.end(),
2230+
RankVectorTypesEq),
2231+
CandidateTys.end());
2232+
} else {
2233+
// The only way to have the same element type in every vector type is to
2234+
// have the same vector type. Check that and remove all but one.
2235+
#ifndef NDEBUG
2236+
for (VectorType *VTy : CandidateTys) {
2237+
assert(VTy->getElementType() == CommonEltTy &&
2238+
"Unaccounted for element type!");
2239+
assert(VTy == CandidateTys[0] &&
2240+
"Different vector types with the same element type!");
2241+
}
2242+
#endif
2243+
CandidateTys.resize(1);
2244+
}
2245+
2246+
// FIXME: hack. Do we have a named constant for this?
2247+
// SDAG SDNode can't have more than 65535 operands.
2248+
llvm::erase_if(CandidateTys, [](VectorType *VTy) {
2249+
return cast<FixedVectorType>(VTy)->getNumElements() >
2250+
std::numeric_limits<unsigned short>::max();
2251+
});
2252+
2253+
for (VectorType *VTy : CandidateTys)
2254+
if (checkVectorTypeForPromotion(P, VTy, DL))
2255+
return VTy;
2256+
2257+
return nullptr;
2258+
}
2259+
21672260
/// Test whether the given alloca partitioning and range of slices can be
21682261
/// promoted to a vector.
21692262
///
@@ -2211,6 +2304,7 @@ static VectorType *isVectorPromotionViable(Partition &P, const DataLayout &DL) {
22112304
}
22122305
}
22132306
};
2307+
22142308
// Put load and store types into a set for de-duplication.
22152309
for (const Slice &S : P) {
22162310
Type *Ty;
@@ -2246,86 +2340,9 @@ static VectorType *isVectorPromotionViable(Partition &P, const DataLayout &DL) {
22462340
}
22472341
}
22482342

2249-
// If we didn't find a vector type, nothing to do here.
2250-
if (CandidateTys.empty())
2251-
return nullptr;
2252-
2253-
// Pointer-ness is sticky, if we had a vector-of-pointers candidate type,
2254-
// then we should choose it, not some other alternative.
2255-
// But, we can't perform a no-op pointer address space change via bitcast,
2256-
// so if we didn't have a common pointer element type, bail.
2257-
if (HaveVecPtrTy && !HaveCommonVecPtrTy)
2258-
return nullptr;
2259-
2260-
// Try to pick the "best" element type out of the choices.
2261-
if (!HaveCommonEltTy && HaveVecPtrTy) {
2262-
// If there was a pointer element type, there's really only one choice.
2263-
CandidateTys.clear();
2264-
CandidateTys.push_back(CommonVecPtrTy);
2265-
} else if (!HaveCommonEltTy && !HaveVecPtrTy) {
2266-
// Integer-ify vector types.
2267-
for (VectorType *&VTy : CandidateTys) {
2268-
if (!VTy->getElementType()->isIntegerTy())
2269-
VTy = cast<VectorType>(VTy->getWithNewType(IntegerType::getIntNTy(
2270-
VTy->getContext(), VTy->getScalarSizeInBits())));
2271-
}
2272-
2273-
// Rank the remaining candidate vector types. This is easy because we know
2274-
// they're all integer vectors. We sort by ascending number of elements.
2275-
auto RankVectorTypesComp = [&DL](VectorType *RHSTy, VectorType *LHSTy) {
2276-
(void)DL;
2277-
assert(DL.getTypeSizeInBits(RHSTy).getFixedValue() ==
2278-
DL.getTypeSizeInBits(LHSTy).getFixedValue() &&
2279-
"Cannot have vector types of different sizes!");
2280-
assert(RHSTy->getElementType()->isIntegerTy() &&
2281-
"All non-integer types eliminated!");
2282-
assert(LHSTy->getElementType()->isIntegerTy() &&
2283-
"All non-integer types eliminated!");
2284-
return cast<FixedVectorType>(RHSTy)->getNumElements() <
2285-
cast<FixedVectorType>(LHSTy)->getNumElements();
2286-
};
2287-
auto RankVectorTypesEq = [&DL](VectorType *RHSTy, VectorType *LHSTy) {
2288-
(void)DL;
2289-
assert(DL.getTypeSizeInBits(RHSTy).getFixedValue() ==
2290-
DL.getTypeSizeInBits(LHSTy).getFixedValue() &&
2291-
"Cannot have vector types of different sizes!");
2292-
assert(RHSTy->getElementType()->isIntegerTy() &&
2293-
"All non-integer types eliminated!");
2294-
assert(LHSTy->getElementType()->isIntegerTy() &&
2295-
"All non-integer types eliminated!");
2296-
return cast<FixedVectorType>(RHSTy)->getNumElements() ==
2297-
cast<FixedVectorType>(LHSTy)->getNumElements();
2298-
};
2299-
llvm::sort(CandidateTys, RankVectorTypesComp);
2300-
CandidateTys.erase(std::unique(CandidateTys.begin(), CandidateTys.end(),
2301-
RankVectorTypesEq),
2302-
CandidateTys.end());
2303-
} else {
2304-
// The only way to have the same element type in every vector type is to
2305-
// have the same vector type. Check that and remove all but one.
2306-
#ifndef NDEBUG
2307-
for (VectorType *VTy : CandidateTys) {
2308-
assert(VTy->getElementType() == CommonEltTy &&
2309-
"Unaccounted for element type!");
2310-
assert(VTy == CandidateTys[0] &&
2311-
"Different vector types with the same element type!");
2312-
}
2313-
#endif
2314-
CandidateTys.resize(1);
2315-
}
2316-
2317-
// FIXME: hack. Do we have a named constant for this?
2318-
// SDAG SDNode can't have more than 65535 operands.
2319-
llvm::erase_if(CandidateTys, [](VectorType *VTy) {
2320-
return cast<FixedVectorType>(VTy)->getNumElements() >
2321-
std::numeric_limits<unsigned short>::max();
2322-
});
2323-
2324-
for (VectorType *VTy : CandidateTys)
2325-
if (checkVectorTypeForPromotion(P, VTy, DL))
2326-
return VTy;
2327-
2328-
return nullptr;
2343+
return checkVectorTypesForPromotion(P, DL, CandidateTys, HaveCommonEltTy,
2344+
CommonEltTy, HaveVecPtrTy,
2345+
HaveCommonVecPtrTy, CommonVecPtrTy);
23292346
}
23302347

23312348
/// Test whether a slice of an alloca is valid for integer widening.

0 commit comments

Comments
 (0)