Skip to content

Commit 4a4444c

Browse files
committed
[CodeModel] Factor getCost out of CostModelPrinter loop. NFC
This helps in a follow-up so that it can be called multiple times with different cost types.
1 parent f421875 commit 4a4444c

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

llvm/lib/Analysis/CostModel.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ static cl::opt<IntrinsicCostStrategy> IntrinsicCost(
6363
#define CM_NAME "cost-model"
6464
#define DEBUG_TYPE CM_NAME
6565

66+
static InstructionCost getCost(Instruction &Inst, TTI::TargetCostKind CostKind,
67+
TargetTransformInfo &TTI,
68+
TargetLibraryInfo &TLI) {
69+
auto *II = dyn_cast<IntrinsicInst>(&Inst);
70+
if (II && IntrinsicCost != IntrinsicCostStrategy::InstructionCost) {
71+
IntrinsicCostAttributes ICA(
72+
II->getIntrinsicID(), *II, InstructionCost::getInvalid(),
73+
/*TypeBasedOnly=*/IntrinsicCost ==
74+
IntrinsicCostStrategy::TypeBasedIntrinsicCost,
75+
&TLI);
76+
return TTI.getIntrinsicInstrCost(ICA, CostKind);
77+
}
78+
79+
return TTI.getInstructionCost(&Inst, CostKind);
80+
}
81+
6682
PreservedAnalyses CostModelPrinterPass::run(Function &F,
6783
FunctionAnalysisManager &AM) {
6884
auto &TTI = AM.getResult<TargetIRAnalysis>(F);
@@ -72,19 +88,7 @@ PreservedAnalyses CostModelPrinterPass::run(Function &F,
7288
for (Instruction &Inst : B) {
7389
// TODO: Use a pass parameter instead of cl::opt CostKind to determine
7490
// which cost kind to print.
75-
InstructionCost Cost;
76-
auto *II = dyn_cast<IntrinsicInst>(&Inst);
77-
if (II && IntrinsicCost != IntrinsicCostStrategy::InstructionCost) {
78-
IntrinsicCostAttributes ICA(
79-
II->getIntrinsicID(), *II, InstructionCost::getInvalid(),
80-
/*TypeBasedOnly=*/IntrinsicCost ==
81-
IntrinsicCostStrategy::TypeBasedIntrinsicCost,
82-
&TLI);
83-
Cost = TTI.getIntrinsicInstrCost(ICA, CostKind);
84-
} else {
85-
Cost = TTI.getInstructionCost(&Inst, CostKind);
86-
}
87-
91+
InstructionCost Cost = getCost(Inst, CostKind, TTI, TLI);
8892
if (auto CostVal = Cost.getValue())
8993
OS << "Cost Model: Found an estimated cost of " << *CostVal;
9094
else

0 commit comments

Comments
 (0)