]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/lib/Analysis/InlineFeaturesAnalysis.cpp
Merge ^/head r364082 through r364250.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / lib / Analysis / InlineFeaturesAnalysis.cpp
1 //===- InlineFeaturesAnalysis.cpp - Feature extraction for ML Policies ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements an analysis extracting function features, which may be
10 // used by ML-driven policies, for example.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Analysis/InlineFeaturesAnalysis.h"
15 #include "llvm/IR/Instructions.h"
16
17 using namespace llvm;
18
19 AnalysisKey InlineFeaturesAnalysis::Key;
20
21 InlineFeaturesAnalysis::Result
22 InlineFeaturesAnalysis::run(const Function &F, FunctionAnalysisManager &FAM) {
23   Result Ret;
24   Ret.Uses = ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses();
25   for (const auto &BB : F) {
26     ++Ret.BasicBlockCount;
27     if (const auto *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
28       if (BI->isConditional())
29         Ret.BlocksReachedFromConditionalInstruction += BI->getNumSuccessors();
30     } else if (const auto *SI = dyn_cast<SwitchInst>(BB.getTerminator()))
31       Ret.BlocksReachedFromConditionalInstruction +=
32           (SI->getNumCases() + (nullptr != SI->getDefaultDest()));
33     for (const auto &I : BB)
34       if (auto *CS = dyn_cast<CallBase>(&I)) {
35         const auto *Callee = CS->getCalledFunction();
36         if (Callee && !Callee->isIntrinsic() && !Callee->isDeclaration())
37           ++Ret.DirectCallsToDefinedFunctions;
38       }
39   }
40   return Ret;
41 }