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

0 commit comments

Comments
 (0)