#include "llvm/Analysis/CodeMetrics.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/InstructionCost.h"
#define DEBUG_TYPE "code-metrics"
using namespace llvm;
static void
appendSpeculatableOperands(const Value *V,
SmallPtrSetImpl<const Value *> &Visited,
SmallVectorImpl<const Value *> &Worklist) {
const User *U = dyn_cast<User>(V);
if (!U)
return;
for (const Value *Operand : U->operands())
if (Visited.insert(Operand).second)
if (const auto *I = dyn_cast<Instruction>(Operand))
if (!I->mayHaveSideEffects() && !I->isTerminator())
Worklist.push_back(I);
}
static void completeEphemeralValues(SmallPtrSetImpl<const Value *> &Visited,
SmallVectorImpl<const Value *> &Worklist,
SmallPtrSetImpl<const Value *> &EphValues) {
for (int i = 0; i < (int)Worklist.size(); ++i) {
const Value *V = Worklist[i];
assert(Visited.count(V) &&
"Failed to add a worklist entry to our visited set!");
if (!all_of(V->users(), [&](const User *U) { return EphValues.count(U); }))
continue;
EphValues.insert(V);
LLVM_DEBUG(dbgs() << "Ephemeral Value: " << *V << "\n");
appendSpeculatableOperands(V, Visited, Worklist);
}
}
void CodeMetrics::collectEphemeralValues(
const Loop *L, AssumptionCache *AC,
SmallPtrSetImpl<const Value *> &EphValues) {
SmallPtrSet<const Value *, 32> Visited;
SmallVector<const Value *, 16> Worklist;
for (auto &AssumeVH : AC->assumptions()) {
if (!AssumeVH)
continue;
Instruction *I = cast<Instruction>(AssumeVH);
if (!L->contains(I->getParent()))
continue;
if (EphValues.insert(I).second)
appendSpeculatableOperands(I, Visited, Worklist);
}
completeEphemeralValues(Visited, Worklist, EphValues);
}
void CodeMetrics::collectEphemeralValues(
const Function *F, AssumptionCache *AC,
SmallPtrSetImpl<const Value *> &EphValues) {
SmallPtrSet<const Value *, 32> Visited;
SmallVector<const Value *, 16> Worklist;
for (auto &AssumeVH : AC->assumptions()) {
if (!AssumeVH)
continue;
Instruction *I = cast<Instruction>(AssumeVH);
assert(I->getParent()->getParent() == F &&
"Found assumption for the wrong function!");
if (EphValues.insert(I).second)
appendSpeculatableOperands(I, Visited, Worklist);
}
completeEphemeralValues(Visited, Worklist, EphValues);
}
void CodeMetrics::analyzeBasicBlock(
const BasicBlock *BB, const TargetTransformInfo &TTI,
const SmallPtrSetImpl<const Value *> &EphValues, bool PrepareForLTO) {
++NumBlocks;
InstructionCost NumInstsBeforeThisBB = NumInsts;
for (const Instruction &I : *BB) {
if (EphValues.count(&I))
continue;
if (const auto *Call = dyn_cast<CallBase>(&I)) {
if (const Function *F = Call->getCalledFunction()) {
bool IsLoweredToCall = TTI.isLoweredToCall(F);
if (!Call->isNoInline() && IsLoweredToCall &&
((F->hasInternalLinkage() && F->hasOneLiveUse()) ||
PrepareForLTO)) {
++NumInlineCandidates;
}
if (F == BB->getParent())
isRecursive = true;
if (IsLoweredToCall)
++NumCalls;
} else {
if (!Call->isInlineAsm())
++NumCalls;
}
}
if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
if (!AI->isStaticAlloca())
this->usesDynamicAlloca = true;
}
if (isa<ExtractElementInst>(I) || I.getType()->isVectorTy())
++NumVectorInsts;
if (I.getType()->isTokenTy() && I.isUsedOutsideOfBlock(BB))
notDuplicatable = true;
if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
if (CI->cannotDuplicate())
notDuplicatable = true;
if (CI->isConvergent())
convergent = true;
}
if (const InvokeInst *InvI = dyn_cast<InvokeInst>(&I))
if (InvI->cannotDuplicate())
notDuplicatable = true;
NumInsts += TTI.getUserCost(&I, TargetTransformInfo::TCK_CodeSize);
}
if (isa<ReturnInst>(BB->getTerminator()))
++NumRets;
notDuplicatable |= isa<IndirectBrInst>(BB->getTerminator());
InstructionCost NumInstsThisBB = NumInsts - NumInstsBeforeThisBB;
NumBBInsts[BB] = NumInstsThisBB;
}