Skip to content

Commit f21673b

Browse files
committed
[SROA] NFC: Extract code to checkVectorTypesForPromotion
Change-Id: Ib6f237cc791a097f8f2411bc1d6502f11d4a748e
1 parent 1c20932 commit f21673b

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
@@ -2108,8 +2108,9 @@ static bool isVectorPromotionViableForSlice(Partition &P, const Slice &S,
21082108

21092109
/// Test whether a vector type is viable for promotion.
21102110
///
2111-
/// This implements the necessary checking for \c isVectorPromotionViable over
2112-
/// all slices of the alloca for the given VectorType.
2111+
/// This implements the necessary checking for \c checkVectorTypesForPromotion
2112+
/// (and thus isVectorPromotionViable) over all slices of the alloca for the
2113+
/// given VectorType.
21132114
static bool checkVectorTypeForPromotion(Partition &P, VectorType *VTy,
21142115
const DataLayout &DL) {
21152116
uint64_t ElementSize =
@@ -2134,6 +2135,98 @@ static bool checkVectorTypeForPromotion(Partition &P, VectorType *VTy,
21342135
return true;
21352136
}
21362137

2138+
/// Test whether any vector type in \p CandidateTys is viable for promotion.
2139+
///
2140+
/// This implements the necessary checking for \c isVectorPromotionViable over
2141+
/// all slices of the alloca for the given VectorType.
2142+
static VectorType *
2143+
checkVectorTypesForPromotion(Partition &P, const DataLayout &DL,
2144+
SmallVectorImpl<VectorType *> &CandidateTys,
2145+
bool HaveCommonEltTy, Type *CommonEltTy,
2146+
bool HaveVecPtrTy, bool HaveCommonVecPtrTy,
2147+
VectorType *CommonVecPtrTy) {
2148+
// If we didn't find a vector type, nothing to do here.
2149+
if (CandidateTys.empty())
2150+
return nullptr;
2151+
2152+
// Pointer-ness is sticky, if we had a vector-of-pointers candidate type,
2153+
// then we should choose it, not some other alternative.
2154+
// But, we can't perform a no-op pointer address space change via bitcast,
2155+
// so if we didn't have a common pointer element type, bail.
2156+
if (HaveVecPtrTy && !HaveCommonVecPtrTy)
2157+
return nullptr;
2158+
2159+
// Try to pick the "best" element type out of the choices.
2160+
if (!HaveCommonEltTy && HaveVecPtrTy) {
2161+
// If there was a pointer element type, there's really only one choice.
2162+
CandidateTys.clear();
2163+
CandidateTys.push_back(CommonVecPtrTy);
2164+
} else if (!HaveCommonEltTy && !HaveVecPtrTy) {
2165+
// Integer-ify vector types.
2166+
for (VectorType *&VTy : CandidateTys) {
2167+
if (!VTy->getElementType()->isIntegerTy())
2168+
VTy = cast<VectorType>(VTy->getWithNewType(IntegerType::getIntNTy(
2169+
VTy->getContext(), VTy->getScalarSizeInBits())));
2170+
}
2171+
2172+
// Rank the remaining candidate vector types. This is easy because we know
2173+
// they're all integer vectors. We sort by ascending number of elements.
2174+
auto RankVectorTypesComp = [&DL](VectorType *RHSTy, VectorType *LHSTy) {
2175+
(void)DL;
2176+
assert(DL.getTypeSizeInBits(RHSTy).getFixedValue() ==
2177+
DL.getTypeSizeInBits(LHSTy).getFixedValue() &&
2178+
"Cannot have vector types of different sizes!");
2179+
assert(RHSTy->getElementType()->isIntegerTy() &&
2180+
"All non-integer types eliminated!");
2181+
assert(LHSTy->getElementType()->isIntegerTy() &&
2182+
"All non-integer types eliminated!");
2183+
return cast<FixedVectorType>(RHSTy)->getNumElements() <
2184+
cast<FixedVectorType>(LHSTy)->getNumElements();
2185+
};
2186+
auto RankVectorTypesEq = [&DL](VectorType *RHSTy, VectorType *LHSTy) {
2187+
(void)DL;
2188+
assert(DL.getTypeSizeInBits(RHSTy).getFixedValue() ==
2189+
DL.getTypeSizeInBits(LHSTy).getFixedValue() &&
2190+
"Cannot have vector types of different sizes!");
2191+
assert(RHSTy->getElementType()->isIntegerTy() &&
2192+
"All non-integer types eliminated!");
2193+
assert(LHSTy->getElementType()->isIntegerTy() &&
2194+
"All non-integer types eliminated!");
2195+
return cast<FixedVectorType>(RHSTy)->getNumElements() ==
2196+
cast<FixedVectorType>(LHSTy)->getNumElements();
2197+
};
2198+
llvm::sort(CandidateTys, RankVectorTypesComp);
2199+
CandidateTys.erase(std::unique(CandidateTys.begin(), CandidateTys.end(),
2200+
RankVectorTypesEq),
2201+
CandidateTys.end());
2202+
} else {
2203+
// The only way to have the same element type in every vector type is to
2204+
// have the same vector type. Check that and remove all but one.
2205+
#ifndef NDEBUG
2206+
for (VectorType *VTy : CandidateTys) {
2207+
assert(VTy->getElementType() == CommonEltTy &&
2208+
"Unaccounted for element type!");
2209+
assert(VTy == CandidateTys[0] &&
2210+
"Different vector types with the same element type!");
2211+
}
2212+
#endif
2213+
CandidateTys.resize(1);
2214+
}
2215+
2216+
// FIXME: hack. Do we have a named constant for this?
2217+
// SDAG SDNode can't have more than 65535 operands.
2218+
llvm::erase_if(CandidateTys, [](VectorType *VTy) {
2219+
return cast<FixedVectorType>(VTy)->getNumElements() >
2220+
std::numeric_limits<unsigned short>::max();
2221+
});
2222+
2223+
for (VectorType *VTy : CandidateTys)
2224+
if (checkVectorTypeForPromotion(P, VTy, DL))
2225+
return VTy;
2226+
2227+
return nullptr;
2228+
}
2229+
21372230
/// Test whether the given alloca partitioning and range of slices can be
21382231
/// promoted to a vector.
21392232
///
@@ -2181,6 +2274,7 @@ static VectorType *isVectorPromotionViable(Partition &P, const DataLayout &DL) {
21812274
}
21822275
}
21832276
};
2277+
21842278
// Put load and store types into a set for de-duplication.
21852279
for (const Slice &S : P) {
21862280
Type *Ty;
@@ -2216,86 +2310,9 @@ static VectorType *isVectorPromotionViable(Partition &P, const DataLayout &DL) {
22162310
}
22172311
}
22182312

2219-
// If we didn't find a vector type, nothing to do here.
2220-
if (CandidateTys.empty())
2221-
return nullptr;
2222-
2223-
// Pointer-ness is sticky, if we had a vector-of-pointers candidate type,
2224-
// then we should choose it, not some other alternative.
2225-
// But, we can't perform a no-op pointer address space change via bitcast,
2226-
// so if we didn't have a common pointer element type, bail.
2227-
if (HaveVecPtrTy && !HaveCommonVecPtrTy)
2228-
return nullptr;
2229-
2230-
// Try to pick the "best" element type out of the choices.
2231-
if (!HaveCommonEltTy && HaveVecPtrTy) {
2232-
// If there was a pointer element type, there's really only one choice.
2233-
CandidateTys.clear();
2234-
CandidateTys.push_back(CommonVecPtrTy);
2235-
} else if (!HaveCommonEltTy && !HaveVecPtrTy) {
2236-
// Integer-ify vector types.
2237-
for (VectorType *&VTy : CandidateTys) {
2238-
if (!VTy->getElementType()->isIntegerTy())
2239-
VTy = cast<VectorType>(VTy->getWithNewType(IntegerType::getIntNTy(
2240-
VTy->getContext(), VTy->getScalarSizeInBits())));
2241-
}
2242-
2243-
// Rank the remaining candidate vector types. This is easy because we know
2244-
// they're all integer vectors. We sort by ascending number of elements.
2245-
auto RankVectorTypesComp = [&DL](VectorType *RHSTy, VectorType *LHSTy) {
2246-
(void)DL;
2247-
assert(DL.getTypeSizeInBits(RHSTy).getFixedValue() ==
2248-
DL.getTypeSizeInBits(LHSTy).getFixedValue() &&
2249-
"Cannot have vector types of different sizes!");
2250-
assert(RHSTy->getElementType()->isIntegerTy() &&
2251-
"All non-integer types eliminated!");
2252-
assert(LHSTy->getElementType()->isIntegerTy() &&
2253-
"All non-integer types eliminated!");
2254-
return cast<FixedVectorType>(RHSTy)->getNumElements() <
2255-
cast<FixedVectorType>(LHSTy)->getNumElements();
2256-
};
2257-
auto RankVectorTypesEq = [&DL](VectorType *RHSTy, VectorType *LHSTy) {
2258-
(void)DL;
2259-
assert(DL.getTypeSizeInBits(RHSTy).getFixedValue() ==
2260-
DL.getTypeSizeInBits(LHSTy).getFixedValue() &&
2261-
"Cannot have vector types of different sizes!");
2262-
assert(RHSTy->getElementType()->isIntegerTy() &&
2263-
"All non-integer types eliminated!");
2264-
assert(LHSTy->getElementType()->isIntegerTy() &&
2265-
"All non-integer types eliminated!");
2266-
return cast<FixedVectorType>(RHSTy)->getNumElements() ==
2267-
cast<FixedVectorType>(LHSTy)->getNumElements();
2268-
};
2269-
llvm::sort(CandidateTys, RankVectorTypesComp);
2270-
CandidateTys.erase(std::unique(CandidateTys.begin(), CandidateTys.end(),
2271-
RankVectorTypesEq),
2272-
CandidateTys.end());
2273-
} else {
2274-
// The only way to have the same element type in every vector type is to
2275-
// have the same vector type. Check that and remove all but one.
2276-
#ifndef NDEBUG
2277-
for (VectorType *VTy : CandidateTys) {
2278-
assert(VTy->getElementType() == CommonEltTy &&
2279-
"Unaccounted for element type!");
2280-
assert(VTy == CandidateTys[0] &&
2281-
"Different vector types with the same element type!");
2282-
}
2283-
#endif
2284-
CandidateTys.resize(1);
2285-
}
2286-
2287-
// FIXME: hack. Do we have a named constant for this?
2288-
// SDAG SDNode can't have more than 65535 operands.
2289-
llvm::erase_if(CandidateTys, [](VectorType *VTy) {
2290-
return cast<FixedVectorType>(VTy)->getNumElements() >
2291-
std::numeric_limits<unsigned short>::max();
2292-
});
2293-
2294-
for (VectorType *VTy : CandidateTys)
2295-
if (checkVectorTypeForPromotion(P, VTy, DL))
2296-
return VTy;
2297-
2298-
return nullptr;
2313+
return checkVectorTypesForPromotion(P, DL, CandidateTys, HaveCommonEltTy,
2314+
CommonEltTy, HaveVecPtrTy,
2315+
HaveCommonVecPtrTy, CommonVecPtrTy);
22992316
}
23002317

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

0 commit comments

Comments
 (0)