Skip to content

Commit 98b6f8d

Browse files
authored
[CostModel] Remove optional from InstructionCost::getValue() (#135596)
InstructionCost is already an optional value, containing an Invalid state that can be checked with isValid(). There is little point in returning another optional from getValue(). Most uses do not make use of it being a std::optional, dereferencing the value directly (either isValid has been checked previously or the Cost is assumed to be valid). The one case that does in AMDGPU used value_or which has been replaced by a isValid() check.
1 parent 665914f commit 98b6f8d

22 files changed

+42
-45
lines changed

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
16101610

16111611
// Scale the cost of the load by the fraction of legal instructions that
16121612
// will be used.
1613-
Cost = divideCeil(UsedInsts.count() * *Cost.getValue(), NumLegalInsts);
1613+
Cost = divideCeil(UsedInsts.count() * Cost.getValue(), NumLegalInsts);
16141614
}
16151615

16161616
// Then plus the cost of interleave operation.
@@ -2878,7 +2878,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
28782878
SubTp && SubTp->getElementType() == FTp->getElementType())
28792879
return divideCeil(FTp->getNumElements(), SubTp->getNumElements());
28802880
}
2881-
return *LT.first.getValue();
2881+
return LT.first.getValue();
28822882
}
28832883

28842884
InstructionCost getAddressComputationCost(Type *Ty, ScalarEvolution *,

llvm/include/llvm/Support/InstructionCost.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#include "llvm/Support/MathExtras.h"
2222
#include <limits>
23-
#include <optional>
2423

2524
namespace llvm {
2625

@@ -84,10 +83,9 @@ class InstructionCost {
8483
/// This function is intended to be used as sparingly as possible, since the
8584
/// class provides the full range of operator support required for arithmetic
8685
/// and comparisons.
87-
std::optional<CostType> getValue() const {
88-
if (isValid())
89-
return Value;
90-
return std::nullopt;
86+
CostType getValue() const {
87+
assert(isValid());
88+
return Value;
9189
}
9290

9391
/// For all of the arithmetic operators provided here any invalid state is

llvm/include/llvm/Transforms/Utils/UnrollLoop.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class UnrollCostEstimator {
143143
/// Whether it is legal to unroll this loop.
144144
bool canUnroll() const;
145145

146-
uint64_t getRolledLoopSize() const { return *LoopSize.getValue(); }
146+
uint64_t getRolledLoopSize() const { return LoopSize.getValue(); }
147147

148148
/// Returns loop size estimation for unrolled loop, given the unrolling
149149
/// configuration specified by UP.

llvm/lib/Analysis/CostModel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ PreservedAnalyses CostModelPrinterPass::run(Function &F,
128128
} else {
129129
InstructionCost Cost =
130130
getCost(Inst, OutputCostKindToTargetCostKind(CostKind), TTI, TLI);
131-
if (auto CostVal = Cost.getValue())
132-
OS << "Found an estimated cost of " << *CostVal;
131+
if (Cost.isValid())
132+
OS << "Found an estimated cost of " << Cost.getValue();
133133
else
134134
OS << "Invalid cost";
135135
OS << " for instruction: " << Inst << "\n";

llvm/lib/CodeGen/SelectOptimize.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class SelectOptimizeImpl {
206206
getI()->getOpcode(), I->getType(), TargetTransformInfo::TCK_Latency,
207207
{TargetTransformInfo::OK_AnyValue, TargetTransformInfo::OP_None},
208208
{TTI::OK_UniformConstantValue, TTI::OP_PowerOf2});
209-
auto TotalCost = Scaled64::get(*Cost.getValue());
209+
auto TotalCost = Scaled64::get(Cost.getValue());
210210
if (auto *OpI = dyn_cast<Instruction>(I->getOperand(1 - CondIdx))) {
211211
auto It = InstCostMap.find(OpI);
212212
if (It != InstCostMap.end())
@@ -1380,8 +1380,8 @@ std::optional<uint64_t>
13801380
SelectOptimizeImpl::computeInstCost(const Instruction *I) {
13811381
InstructionCost ICost =
13821382
TTI->getInstructionCost(I, TargetTransformInfo::TCK_Latency);
1383-
if (auto OC = ICost.getValue())
1384-
return std::optional<uint64_t>(*OC);
1383+
if (ICost.isValid())
1384+
return std::optional<uint64_t>(ICost.getValue());
13851385
return std::nullopt;
13861386
}
13871387

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28530,7 +28530,7 @@ bool AArch64TargetLowering::shouldLocalize(
2853028530
Imm, CI->getType(), TargetTransformInfo::TCK_CodeSize);
2853128531
assert(Cost.isValid() && "Expected a valid imm cost");
2853228532

28533-
unsigned RematCost = *Cost.getValue();
28533+
unsigned RematCost = Cost.getValue();
2853428534
RematCost += AdditionalCost;
2853528535
Register Reg = MI.getOperand(0).getReg();
2853628536
unsigned MaxUses = maxUses(RematCost);

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4618,7 +4618,7 @@ static bool isLoopSizeWithinBudget(Loop *L, const AArch64TTIImpl &TTI,
46184618
}
46194619

46204620
if (FinalSize)
4621-
*FinalSize = *LoopCost.getValue();
4621+
*FinalSize = LoopCost.getValue();
46224622
return true;
46234623
}
46244624

llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ static CostType calculateFunctionCosts(GetTTIFn GetTTI, Module &M,
205205
TTI.getInstructionCost(&I, TargetTransformInfo::TCK_CodeSize);
206206
assert(Cost != InstructionCost::getMax());
207207
// Assume expensive if we can't tell the cost of an instruction.
208-
CostType CostVal =
209-
Cost.getValue().value_or(TargetTransformInfo::TCC_Expensive);
208+
CostType CostVal = Cost.isValid() ? Cost.getValue()
209+
: TargetTransformInfo::TCC_Expensive;
210210
assert((FnCost + CostVal) >= FnCost && "Overflow!");
211211
FnCost += CostVal;
212212
}

llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,9 +1277,9 @@ static unsigned adjustInliningThresholdUsingCallee(const CallBase *CB,
12771277
// The penalty cost is computed relative to the cost of instructions and does
12781278
// not model any storage costs.
12791279
adjustThreshold += std::max(0, SGPRsInUse - NrOfSGPRUntilSpill) *
1280-
*ArgStackCost.getValue() * InlineConstants::getInstrCost();
1280+
ArgStackCost.getValue() * InlineConstants::getInstrCost();
12811281
adjustThreshold += std::max(0, VGPRsInUse - NrOfVGPRUntilSpill) *
1282-
*ArgStackCost.getValue() * InlineConstants::getInstrCost();
1282+
ArgStackCost.getValue() * InlineConstants::getInstrCost();
12831283
return adjustThreshold;
12841284
}
12851285

llvm/lib/Target/PowerPC/PPCTargetTransformInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ InstructionCost PPCTTIImpl::getVPMemoryOpCost(unsigned Opcode, Type *Src,
10961096
float AlignmentProb = ((float)Alignment.value()) / DesiredAlignment.value();
10971097
float MisalignmentProb = 1.0 - AlignmentProb;
10981098
return (MisalignmentProb * P9PipelineFlushEstimate) +
1099-
(AlignmentProb * *Cost.getValue());
1099+
(AlignmentProb * Cost.getValue());
11001100
}
11011101

11021102
// Usually we should not get to this point, but the following is an attempt to

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2909,7 +2909,7 @@ InstructionCost RISCVTargetLowering::getVRGatherVVCost(MVT VT) const {
29092909
bool Log2CostModel =
29102910
Subtarget.getVRGatherCostModel() == llvm::RISCVSubtarget::NLog2N;
29112911
if (Log2CostModel && LMULCost.isValid()) {
2912-
unsigned Log = Log2_64(*LMULCost.getValue());
2912+
unsigned Log = Log2_64(LMULCost.getValue());
29132913
if (Log > 0)
29142914
return LMULCost * Log;
29152915
}

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ costShuffleViaVRegSplitting(const RISCVTTIImpl &TTI, MVT LegalVT,
483483
auto *SingleOpTy = FixedVectorType::get(Tp->getElementType(),
484484
LegalVT.getVectorNumElements());
485485

486-
unsigned E = *NumOfDests.getValue();
486+
unsigned E = NumOfDests.getValue();
487487
unsigned NormalizedVF =
488488
LegalVT.getVectorNumElements() * std::max(NumOfSrcs, E);
489489
unsigned NumOfSrcRegs = NormalizedVF / LegalVT.getVectorNumElements();

llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ void SystemZTTIImpl::getUnrollingPreferences(
380380
// The z13 processor will run out of store tags if too many stores
381381
// are fed into it too quickly. Therefore make sure there are not
382382
// too many stores in the resulting unrolled loop.
383-
unsigned const NumStoresVal = *NumStores.getValue();
383+
unsigned const NumStoresVal = NumStores.getValue();
384384
unsigned const Max = (NumStoresVal ? (12 / NumStoresVal) : UINT_MAX);
385385

386386
if (HasCall) {

llvm/lib/Target/X86/X86TargetTransformInfo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ InstructionCost X86TTIImpl::getShuffleCost(
17481748
getTypeLegalizationCost(
17491749
FixedVectorType::get(BaseTp->getElementType(), Mask.size()))
17501750
.first;
1751-
unsigned E = *NumOfDests.getValue();
1751+
unsigned E = NumOfDests.getValue();
17521752
unsigned NormalizedVF =
17531753
LegalVT.getVectorNumElements() * std::max(NumOfSrcs, E);
17541754
unsigned NumOfSrcRegs = NormalizedVF / LegalVT.getVectorNumElements();
@@ -4931,7 +4931,7 @@ InstructionCost X86TTIImpl::getScalarizationOverhead(
49314931
(LegalVectorBitWidth % LaneBitWidth) == 0) &&
49324932
"Illegal vector");
49334933

4934-
const int NumLegalVectors = *LT.first.getValue();
4934+
const int NumLegalVectors = LT.first.getValue();
49354935
assert(NumLegalVectors >= 0 && "Negative cost!");
49364936

49374937
// For insertions, a ISD::BUILD_VECTOR style vector initialization can be much
@@ -6164,7 +6164,7 @@ InstructionCost X86TTIImpl::getGSVectorCost(unsigned Opcode,
61646164
std::pair<InstructionCost, MVT> IdxsLT = getTypeLegalizationCost(IndexVTy);
61656165
std::pair<InstructionCost, MVT> SrcLT = getTypeLegalizationCost(SrcVTy);
61666166
InstructionCost::CostType SplitFactor =
6167-
*std::max(IdxsLT.first, SrcLT.first).getValue();
6167+
std::max(IdxsLT.first, SrcLT.first).getValue();
61686168
if (SplitFactor > 1) {
61696169
// Handle splitting of vector of pointers
61706170
auto *SplitSrcTy =

llvm/lib/Transforms/IPO/FunctionSpecialization.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ FunctionSpecializer::~FunctionSpecializer() {
662662
/// non-negative, which is true for both TCK_CodeSize and TCK_Latency, and
663663
/// always Valid.
664664
static unsigned getCostValue(const Cost &C) {
665-
int64_t Value = *C.getValue();
665+
int64_t Value = C.getValue();
666666

667667
assert(Value >= 0 && "CodeSize and Latency cannot be negative");
668668
// It is safe to down cast since we know the arguments cannot be negative and
@@ -713,7 +713,7 @@ bool FunctionSpecializer::run() {
713713
if (!SpecializeLiteralConstant && !Inserted && !Metrics.isRecursive)
714714
continue;
715715

716-
int64_t Sz = *Metrics.NumInsts.getValue();
716+
int64_t Sz = Metrics.NumInsts.getValue();
717717
assert(Sz > 0 && "CodeSize should be positive");
718718
// It is safe to down cast from int64_t, NumInsts is always positive.
719719
unsigned FuncSize = static_cast<unsigned>(Sz);

llvm/lib/Transforms/IPO/PartialInlining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,7 @@ bool PartialInlinerImpl::tryPartialInline(FunctionCloner &Cloner) {
13201320
RelativeToEntryFreq = BranchProbability(0, 1);
13211321

13221322
BlockFrequency WeightedRcost =
1323-
BlockFrequency(*NonWeightedRcost.getValue()) * RelativeToEntryFreq;
1323+
BlockFrequency(NonWeightedRcost.getValue()) * RelativeToEntryFreq;
13241324

13251325
// The call sequence(s) to the outlined function(s) are larger than the sum of
13261326
// the original outlined region size(s), it does not increase the chances of

llvm/lib/Transforms/Scalar/ConstantHoisting.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ void ConstantHoistingPass::collectConstantCandidates(
386386
ConstIntCandVec.push_back(ConstantCandidate(ConstInt));
387387
Itr->second = ConstIntCandVec.size() - 1;
388388
}
389-
ConstIntCandVec[Itr->second].addUser(Inst, Idx, *Cost.getValue());
389+
ConstIntCandVec[Itr->second].addUser(Inst, Idx, Cost.getValue());
390390
LLVM_DEBUG(if (isa<ConstantInt>(Inst->getOperand(Idx))) dbgs()
391391
<< "Collect constant " << *ConstInt << " from " << *Inst
392392
<< " with cost " << Cost << '\n';
@@ -446,7 +446,7 @@ void ConstantHoistingPass::collectConstantCandidates(
446446
ConstExpr));
447447
Itr->second = ExprCandVec.size() - 1;
448448
}
449-
ExprCandVec[Itr->second].addUser(Inst, Idx, *Cost.getValue());
449+
ExprCandVec[Itr->second].addUser(Inst, Idx, Cost.getValue());
450450
}
451451

452452
/// Check the operand for instruction Inst at index Idx.

llvm/lib/Transforms/Scalar/LoopDataPrefetch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ bool LoopDataPrefetch::runOnLoop(Loop *L) {
304304
if (!Metrics.NumInsts.isValid())
305305
return MadeChange;
306306

307-
unsigned LoopSize = *Metrics.NumInsts.getValue();
307+
unsigned LoopSize = Metrics.NumInsts.getValue();
308308
if (!LoopSize)
309309
LoopSize = 1;
310310

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,7 @@ void Cost::RateFormula(const Formula &F,
15351535
C.NumBaseAdds += (F.UnfoldedOffset.isNonZero());
15361536

15371537
// Accumulate non-free scaling amounts.
1538-
C.ScaleCost += *getScalingFactorCost(*TTI, LU, F, *L).getValue();
1538+
C.ScaleCost += getScalingFactorCost(*TTI, LU, F, *L).getValue();
15391539

15401540
// Tally up the non-zero immediates.
15411541
for (const LSRFixup &Fixup : LU.Fixups) {

llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,8 +677,8 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
677677
LLVM_DEBUG(dbgs() << "Analysis finished:\n"
678678
<< "UnrolledCost: " << UnrolledCost << ", "
679679
<< "RolledDynamicCost: " << RolledDynamicCost << "\n");
680-
return {{unsigned(*UnrolledCost.getValue()),
681-
unsigned(*RolledDynamicCost.getValue())}};
680+
return {{unsigned(UnrolledCost.getValue()),
681+
unsigned(RolledDynamicCost.getValue())}};
682682
}
683683

684684
UnrollCostEstimator::UnrollCostEstimator(
@@ -729,7 +729,7 @@ bool UnrollCostEstimator::canUnroll() const {
729729
uint64_t UnrollCostEstimator::getUnrolledLoopSize(
730730
const TargetTransformInfo::UnrollingPreferences &UP,
731731
unsigned CountOverwrite) const {
732-
unsigned LS = *LoopSize.getValue();
732+
unsigned LS = LoopSize.getValue();
733733
assert(LS >= UP.BEInsns && "LoopSize should not be less than BEInsns!");
734734
if (CountOverwrite)
735735
return static_cast<uint64_t>(LS - UP.BEInsns) * CountOverwrite + UP.BEInsns;

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,7 +2002,7 @@ class GeneratedRTChecks {
20022002
InstructionCost NewMemCheckCost = MemCheckCost / BestTripCount;
20032003

20042004
// Let's ensure the cost is always at least 1.
2005-
NewMemCheckCost = std::max(*NewMemCheckCost.getValue(),
2005+
NewMemCheckCost = std::max(NewMemCheckCost.getValue(),
20062006
(InstructionCost::CostType)1);
20072007

20082008
if (BestTripCount > 1)
@@ -5314,7 +5314,7 @@ LoopVectorizationCostModel::selectInterleaveCount(VPlan &Plan, ElementCount VF,
53145314
// to estimate the cost of the loop and interleave until the cost of the
53155315
// loop overhead is about 5% of the cost of the loop.
53165316
unsigned SmallIC = std::min(IC, (unsigned)llvm::bit_floor<uint64_t>(
5317-
SmallLoopCost / *LoopCost.getValue()));
5317+
SmallLoopCost / LoopCost.getValue()));
53185318

53195319
// Interleave until store/load ports (estimated by max interleave count) are
53205320
// saturated.
@@ -7659,7 +7659,7 @@ InstructionCost LoopVectorizationPlanner::cost(VPlan &Plan,
76597659
LLVM_DEBUG(dbgs() << "Cost for VF " << VF << ": " << Cost
76607660
<< " (Estimated cost per lane: ");
76617661
if (Cost.isValid()) {
7662-
double CostPerLane = double(*Cost.getValue()) / EstimatedWidth;
7662+
double CostPerLane = double(Cost.getValue()) / EstimatedWidth;
76637663
LLVM_DEBUG(dbgs() << format("%.1f", CostPerLane));
76647664
} else /* No point dividing an invalid cost - it will still be invalid */
76657665
LLVM_DEBUG(dbgs() << "Invalid");
@@ -10478,7 +10478,7 @@ static bool isOutsideLoopWorkProfitable(GeneratedRTChecks &Checks,
1047810478

1047910479
// The scalar cost should only be 0 when vectorizing with a user specified
1048010480
// VF/IC. In those cases, runtime checks should always be generated.
10481-
uint64_t ScalarC = *VF.ScalarCost.getValue();
10481+
uint64_t ScalarC = VF.ScalarCost.getValue();
1048210482
if (ScalarC == 0)
1048310483
return true;
1048410484

@@ -10513,8 +10513,8 @@ static bool isOutsideLoopWorkProfitable(GeneratedRTChecks &Checks,
1051310513
// the computations are performed on doubles, not integers and the result
1051410514
// is rounded up, hence we get an upper estimate of the TC.
1051510515
unsigned IntVF = getEstimatedRuntimeVF(VF.Width, VScale);
10516-
uint64_t RtC = *TotalCost.getValue();
10517-
uint64_t Div = ScalarC * IntVF - *VF.Cost.getValue();
10516+
uint64_t RtC = TotalCost.getValue();
10517+
uint64_t Div = ScalarC * IntVF - VF.Cost.getValue();
1051810518
uint64_t MinTC1 = Div == 0 ? 0 : divideCeil(RtC * IntVF, Div);
1051910519

1052010520
// Second, compute a minimum iteration count so that the cost of the

llvm/unittests/Support/InstructionCostTest.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ TEST_F(CostTest, DefaultCtor) {
2323
InstructionCost DefaultCost;
2424

2525
ASSERT_TRUE(DefaultCost.isValid());
26-
EXPECT_EQ(*(DefaultCost.getValue()), 0);
26+
EXPECT_EQ(DefaultCost.getValue(), 0);
2727
}
2828

2929
TEST_F(CostTest, Operators) {
@@ -70,8 +70,7 @@ TEST_F(CostTest, Operators) {
7070
EXPECT_FALSE(TmpCost.isValid());
7171

7272
// Test value extraction
73-
EXPECT_EQ(*(VThree.getValue()), 3);
74-
EXPECT_EQ(IThreeA.getValue(), std::nullopt);
73+
EXPECT_EQ(VThree.getValue(), 3);
7574

7675
EXPECT_EQ(std::min(VThree, VNegTwo), -2);
7776
EXPECT_EQ(std::max(VThree, VSix), 6);

0 commit comments

Comments
 (0)