From cfca06d7963fa0909f90483b42a6d7d194d01e08 Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Sun, 26 Jul 2020 19:36:28 +0000 Subject: Vendor import of llvm-project master 2e10b7a39b9, the last commit before the llvmorg-12-init tag, from which release/11.x was branched. --- llvm/lib/Analysis/InlineFeaturesAnalysis.cpp | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 llvm/lib/Analysis/InlineFeaturesAnalysis.cpp (limited to 'llvm/lib/Analysis/InlineFeaturesAnalysis.cpp') diff --git a/llvm/lib/Analysis/InlineFeaturesAnalysis.cpp b/llvm/lib/Analysis/InlineFeaturesAnalysis.cpp new file mode 100644 index 0000000000000..90f521bbaab48 --- /dev/null +++ b/llvm/lib/Analysis/InlineFeaturesAnalysis.cpp @@ -0,0 +1,41 @@ +//===- InlineFeaturesAnalysis.cpp - Feature extraction for ML Policies ----===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements an analysis extracting function features, which may be +// used by ML-driven policies, for example. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Analysis/InlineFeaturesAnalysis.h" +#include "llvm/IR/Instructions.h" + +using namespace llvm; + +AnalysisKey InlineFeaturesAnalysis::Key; + +InlineFeaturesAnalysis::Result +InlineFeaturesAnalysis::run(const Function &F, FunctionAnalysisManager &FAM) { + Result Ret; + Ret.Uses = ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses(); + for (const auto &BB : F) { + ++Ret.BasicBlockCount; + if (const auto *BI = dyn_cast(BB.getTerminator())) { + if (BI->isConditional()) + Ret.BlocksReachedFromConditionalInstruction += BI->getNumSuccessors(); + } else if (const auto *SI = dyn_cast(BB.getTerminator())) + Ret.BlocksReachedFromConditionalInstruction += + (SI->getNumCases() + (nullptr != SI->getDefaultDest())); + for (const auto &I : BB) + if (auto *CS = dyn_cast(&I)) { + const auto *Callee = CS->getCalledFunction(); + if (Callee && !Callee->isIntrinsic() && !Callee->isDeclaration()) + ++Ret.DirectCallsToDefinedFunctions; + } + } + return Ret; +} \ No newline at end of file -- cgit v1.2.3