Skip to content

Commit 00f0604

Browse files
committed
[CostModel] Remove optional from InstructionCost::getValue()
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 new getValueOr(CostType) function.
1 parent 81499ed commit 00f0604

22 files changed

+52
-51
lines changed

llvm/include/llvm/CodeGen/BasicTTIImpl.h

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

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

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

28852885
InstructionCost getAddressComputationCost(Type *Ty, ScalarEvolution *,

llvm/include/llvm/Support/InstructionCost.h

Lines changed: 8 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,14 @@ 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;
89+
}
90+
CostType getValueOr(CostType Alt) const {
91+
if (!isValid())
92+
return Alt;
93+
return Value;
9194
}
9295

9396
/// 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
@@ -28496,7 +28496,7 @@ bool AArch64TargetLowering::shouldLocalize(
2849628496
Imm, CI->getType(), TargetTransformInfo::TCK_CodeSize);
2849728497
assert(Cost.isValid() && "Expected a valid imm cost");
2849828498

28499-
unsigned RematCost = *Cost.getValue();
28499+
unsigned RematCost = Cost.getValue();
2850028500
RematCost += AdditionalCost;
2850128501
Register Reg = MI.getOperand(0).getReg();
2850228502
unsigned MaxUses = maxUses(RematCost);

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4596,7 +4596,7 @@ static bool isLoopSizeWithinBudget(Loop *L, AArch64TTIImpl &TTI,
45964596
}
45974597

45984598
if (FinalSize)
4599-
*FinalSize = *LoopCost.getValue();
4599+
*FinalSize = LoopCost.getValue();
46004600
return true;
46014601
}
46024602

llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ 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.getValueOr(TargetTransformInfo::TCC_Expensive);
210209
assert((FnCost + CostVal) >= FnCost && "Overflow!");
211210
FnCost += CostVal;
212211
}

llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp

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

llvm/lib/Target/PowerPC/PPCTargetTransformInfo.cpp

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

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

0 commit comments

Comments
 (0)