Index: llvm/lib/Analysis/CodeMetrics.cpp =================================================================== --- llvm/lib/Analysis/CodeMetrics.cpp +++ llvm/lib/Analysis/CodeMetrics.cpp @@ -18,6 +18,7 @@ #include "llvm/Analysis/ValueTracking.h" #include "llvm/IR/Function.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/InstructionCost.h" #define DEBUG_TYPE "code-metrics" @@ -116,7 +117,10 @@ const BasicBlock *BB, const TargetTransformInfo &TTI, const SmallPtrSetImpl &EphValues, bool PrepareForLTO) { ++NumBlocks; - unsigned NumInstsBeforeThisBB = NumInsts; + // Use a proxy for NumInsts, so that it can use InstructionCost's arithmetic + // properties. + InstructionCost NumInstsProxy = NumInsts; + InstructionCost NumInstsBeforeThisBB = NumInsts; for (const Instruction &I : *BB) { // Skip ephemeral values. if (EphValues.count(&I)) @@ -175,7 +179,8 @@ if (InvI->cannotDuplicate()) notDuplicatable = true; - NumInsts += TTI.getUserCost(&I, TargetTransformInfo::TCK_CodeSize); + NumInstsProxy += TTI.getUserCost(&I, TargetTransformInfo::TCK_CodeSize); + NumInsts = *NumInstsProxy.getValue(); } if (isa(BB->getTerminator())) @@ -195,5 +200,6 @@ notDuplicatable |= isa(BB->getTerminator()); // Remember NumInsts for this BB. - NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB; + InstructionCost NumInstsThisBB = NumInstsProxy - NumInstsBeforeThisBB; + NumBBInsts[BB] = *NumInstsThisBB.getValue(); }