Skip to content

Commit 8fe5774

Browse files
sdesmalen-armmemfrob
authored andcommitted
NFC: Migrate CodeMetrics to work on InstructionCost
This patch migrates cost values and arithmetic to work on InstructionCost. When the interfaces to TargetTransformInfo are changed, any InstructionCost state will propagate naturally. See this patch for the introduction of the type: https://reviews.llvm.org/D91174 See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2020-November/146408.html Reviewed By: david-arm Differential Revision: https://reviews.llvm.org/D96030
1 parent 053436d commit 8fe5774

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

llvm/lib/Analysis/CodeMetrics.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/Analysis/ValueTracking.h"
1919
#include "llvm/IR/Function.h"
2020
#include "llvm/Support/Debug.h"
21+
#include "llvm/Support/InstructionCost.h"
2122

2223
#define DEBUG_TYPE "code-metrics"
2324

@@ -112,7 +113,14 @@ void CodeMetrics::analyzeBasicBlock(
112113
const BasicBlock *BB, const TargetTransformInfo &TTI,
113114
const SmallPtrSetImpl<const Value *> &EphValues, bool PrepareForLTO) {
114115
++NumBlocks;
115-
unsigned NumInstsBeforeThisBB = NumInsts;
116+
// Use a proxy variable for NumInsts of type InstructionCost, so that it can
117+
// use InstructionCost's arithmetic properties such as saturation when this
118+
// feature is added to InstructionCost.
119+
// When storing the value back to NumInsts, we can assume all costs are Valid
120+
// because the IR should not contain any nodes that cannot be costed. If that
121+
// happens the cost-model is broken.
122+
InstructionCost NumInstsProxy = NumInsts;
123+
InstructionCost NumInstsBeforeThisBB = NumInsts;
116124
for (const Instruction &I : *BB) {
117125
// Skip ephemeral values.
118126
if (EphValues.count(&I))
@@ -171,7 +179,8 @@ void CodeMetrics::analyzeBasicBlock(
171179
if (InvI->cannotDuplicate())
172180
notDuplicatable = true;
173181

174-
NumInsts += TTI.getUserCost(&I, TargetTransformInfo::TCK_CodeSize);
182+
NumInstsProxy += TTI.getUserCost(&I, TargetTransformInfo::TCK_CodeSize);
183+
NumInsts = *NumInstsProxy.getValue();
175184
}
176185

177186
if (isa<ReturnInst>(BB->getTerminator()))
@@ -191,5 +200,6 @@ void CodeMetrics::analyzeBasicBlock(
191200
notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator());
192201

193202
// Remember NumInsts for this BB.
194-
NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB;
203+
InstructionCost NumInstsThisBB = NumInstsProxy - NumInstsBeforeThisBB;
204+
NumBBInsts[BB] = *NumInstsThisBB.getValue();
195205
}

0 commit comments

Comments
 (0)