Skip to content

Commit f571fe6

Browse files
committed
Reland [LoopVectorizer] NFCI: Calculate register usage based on TLI.getTypeLegalizationCost.
This relands https://reviews.llvm.org/D91059 and reverts commit 30fded7. GetRegUsage now returns 0 when Ty is not a valid vector element type.
1 parent f4517bb commit f571fe6

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,9 @@ class TargetTransformInfo {
710710
/// Return true if this type is legal.
711711
bool isTypeLegal(Type *Ty) const;
712712

713+
/// Returns the estimated number of registers required to represent \p Ty.
714+
unsigned getRegUsageForType(Type *Ty) const;
715+
713716
/// Return true if switches should be turned into lookup tables for the
714717
/// target.
715718
bool shouldBuildLookupTables() const;
@@ -1450,6 +1453,7 @@ class TargetTransformInfo::Concept {
14501453
virtual bool isProfitableToHoist(Instruction *I) = 0;
14511454
virtual bool useAA() = 0;
14521455
virtual bool isTypeLegal(Type *Ty) = 0;
1456+
virtual unsigned getRegUsageForType(Type *Ty) = 0;
14531457
virtual bool shouldBuildLookupTables() = 0;
14541458
virtual bool shouldBuildLookupTablesForConstant(Constant *C) = 0;
14551459
virtual bool useColdCCForColdCall(Function &F) = 0;
@@ -1814,6 +1818,9 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
18141818
}
18151819
bool useAA() override { return Impl.useAA(); }
18161820
bool isTypeLegal(Type *Ty) override { return Impl.isTypeLegal(Ty); }
1821+
unsigned getRegUsageForType(Type *Ty) override {
1822+
return Impl.getRegUsageForType(Ty);
1823+
}
18171824
bool shouldBuildLookupTables() override {
18181825
return Impl.shouldBuildLookupTables();
18191826
}

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ class TargetTransformInfoImplBase {
261261

262262
bool isTypeLegal(Type *Ty) { return false; }
263263

264+
unsigned getRegUsageForType(Type *Ty) { return 1; }
265+
264266
bool shouldBuildLookupTables() { return true; }
265267
bool shouldBuildLookupTablesForConstant(Constant *C) { return true; }
266268

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
301301
return getTLI()->isTypeLegal(VT);
302302
}
303303

304+
unsigned getRegUsageForType(Type *Ty) {
305+
return getTLI()->getTypeLegalizationCost(DL, Ty).first;
306+
}
307+
304308
int getGEPCost(Type *PointeeType, const Value *Ptr,
305309
ArrayRef<const Value *> Operands) {
306310
return BaseT::getGEPCost(PointeeType, Ptr, Operands);

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@ bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
486486
return TTIImpl->isTypeLegal(Ty);
487487
}
488488

489+
unsigned TargetTransformInfo::getRegUsageForType(Type *Ty) const {
490+
return TTIImpl->getRegUsageForType(Ty);
491+
}
492+
489493
bool TargetTransformInfo::shouldBuildLookupTables() const {
490494
return TTIImpl->shouldBuildLookupTables();
491495
}

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5793,23 +5793,18 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
57935793
unsigned MaxSafeDepDist = -1U;
57945794
if (Legal->getMaxSafeDepDistBytes() != -1U)
57955795
MaxSafeDepDist = Legal->getMaxSafeDepDistBytes() * 8;
5796-
unsigned WidestRegister =
5797-
std::min(TTI.getRegisterBitWidth(true), MaxSafeDepDist);
5798-
const DataLayout &DL = TheFunction->getParent()->getDataLayout();
57995796

58005797
SmallVector<RegisterUsage, 8> RUs(VFs.size());
58015798
SmallVector<SmallMapVector<unsigned, unsigned, 4>, 8> MaxUsages(VFs.size());
58025799

58035800
LLVM_DEBUG(dbgs() << "LV(REG): Calculating max register usage:\n");
58045801

58055802
// A lambda that gets the register usage for the given type and VF.
5806-
auto GetRegUsage = [&DL, WidestRegister](Type *Ty, ElementCount VF) {
5807-
if (Ty->isTokenTy())
5803+
const auto &TTICapture = TTI;
5804+
auto GetRegUsage = [&TTICapture](Type *Ty, ElementCount VF) {
5805+
if (Ty->isTokenTy() || !VectorType::isValidElementType(Ty))
58085806
return 0U;
5809-
unsigned TypeSize = DL.getTypeSizeInBits(Ty->getScalarType());
5810-
assert(!VF.isScalable() && "scalable vectors not yet supported.");
5811-
return std::max<unsigned>(1, VF.getKnownMinValue() * TypeSize /
5812-
WidestRegister);
5807+
return TTICapture.getRegUsageForType(VectorType::get(Ty, VF));
58135808
};
58145809

58155810
for (unsigned int i = 0, s = IdxToInstr.size(); i < s; ++i) {

0 commit comments

Comments
 (0)