LLVM  14.0.0git
FunctionPropertiesAnalysis.cpp
Go to the documentation of this file.
1 //===- FunctionPropertiesAnalysis.cpp - Function Properties Analysis ------===//
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 defines the FunctionPropertiesInfo and FunctionPropertiesAnalysis
10 // classes used to extract function properties.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/IR/Instructions.h"
16 
17 using namespace llvm;
18 
21  const LoopInfo &LI) {
22 
24 
25  FPI.Uses = ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses();
26 
27  for (const auto &BB : F) {
28  ++FPI.BasicBlockCount;
29 
30  if (const auto *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
31  if (BI->isConditional())
32  FPI.BlocksReachedFromConditionalInstruction += BI->getNumSuccessors();
33  } else if (const auto *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
35  (SI->getNumCases() + (nullptr != SI->getDefaultDest()));
36  }
37 
38  for (const auto &I : BB) {
39  if (auto *CS = dyn_cast<CallBase>(&I)) {
40  const auto *Callee = CS->getCalledFunction();
41  if (Callee && !Callee->isIntrinsic() && !Callee->isDeclaration())
43  }
44  if (I.getOpcode() == Instruction::Load) {
45  ++FPI.LoadInstCount;
46  } else if (I.getOpcode() == Instruction::Store) {
47  ++FPI.StoreInstCount;
48  }
49  }
50  // Loop Depth of the Basic Block
51  int64_t LoopDepth;
52  LoopDepth = LI.getLoopDepth(&BB);
53  if (FPI.MaxLoopDepth < LoopDepth)
54  FPI.MaxLoopDepth = LoopDepth;
55  }
56  FPI.TopLevelLoopCount += llvm::size(LI);
57  return FPI;
58 }
59 
61  OS << "BasicBlockCount: " << BasicBlockCount << "\n"
62  << "BlocksReachedFromConditionalInstruction: "
64  << "Uses: " << Uses << "\n"
65  << "DirectCallsToDefinedFunctions: " << DirectCallsToDefinedFunctions
66  << "\n"
67  << "LoadInstCount: " << LoadInstCount << "\n"
68  << "StoreInstCount: " << StoreInstCount << "\n"
69  << "MaxLoopDepth: " << MaxLoopDepth << "\n"
70  << "TopLevelLoopCount: " << TopLevelLoopCount << "\n\n";
71 }
72 
74 
79 }
80 
83  OS << "Printing analysis results of CFA for function "
84  << "'" << F.getName() << "':"
85  << "\n";
87  return PreservedAnalyses::all();
88 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
print
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
Definition: ArchiveWriter.cpp:147
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:779
llvm::Function
Definition: Function.h:61
llvm::FunctionPropertiesPrinterPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: FunctionPropertiesAnalysis.cpp:82
llvm::FunctionPropertiesInfo::TopLevelLoopCount
int64_t TopLevelLoopCount
Definition: FunctionPropertiesAnalysis.h:59
FAM
FunctionAnalysisManager FAM
Definition: PassBuilderBindings.cpp:59
llvm::FunctionPropertiesAnalysis::run
Result run(Function &F, FunctionAnalysisManager &FAM)
Definition: FunctionPropertiesAnalysis.cpp:76
llvm::FunctionPropertiesInfo::LoadInstCount
int64_t LoadInstCount
Definition: FunctionPropertiesAnalysis.h:50
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::FunctionPropertiesInfo::getFunctionPropertiesInfo
static FunctionPropertiesInfo getFunctionPropertiesInfo(const Function &F, const LoopInfo &LI)
Definition: FunctionPropertiesAnalysis.cpp:20
llvm::FunctionPropertiesAnalysis::Key
static AnalysisKey Key
Definition: FunctionPropertiesAnalysis.h:67
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::SPII::Load
@ Load
Definition: SparcInstrInfo.h:32
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:72
I
#define I(x, y, z)
Definition: MD5.cpp:59
SI
StandardInstrumentations SI(Debug, VerifyEach)
llvm::SPII::Store
@ Store
Definition: SparcInstrInfo.h:33
llvm::FunctionPropertiesInfo::print
void print(raw_ostream &OS) const
Definition: FunctionPropertiesAnalysis.cpp:60
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1528
llvm::FunctionPropertiesInfo::Uses
int64_t Uses
Number of uses of this function, plus 1 if the function is callable outside the module.
Definition: FunctionPropertiesAnalysis.h:43
llvm::LoopInfoBase::getLoopDepth
unsigned getLoopDepth(const BlockT *BB) const
Return the loop nesting level of the specified block.
Definition: LoopInfo.h:974
llvm::FunctionPropertiesAnalysis
Definition: FunctionPropertiesAnalysis.h:63
llvm::LoopInfo
Definition: LoopInfo.h:1083
llvm::FunctionPropertiesInfo::MaxLoopDepth
int64_t MaxLoopDepth
Definition: FunctionPropertiesAnalysis.h:56
Callee
amdgpu Simplify well known AMD library false FunctionCallee Callee
Definition: AMDGPULibCalls.cpp:206
FunctionPropertiesAnalysis.h
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
llvm::FunctionPropertiesInfo::BlocksReachedFromConditionalInstruction
int64_t BlocksReachedFromConditionalInstruction
Number of blocks reached from a conditional instruction, or that are 'cases' of a SwitchInstr.
Definition: FunctionPropertiesAnalysis.h:39
Instructions.h
llvm::FunctionPropertiesInfo::BasicBlockCount
int64_t BasicBlockCount
Number of basic blocks.
Definition: FunctionPropertiesAnalysis.h:31
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::FunctionPropertiesInfo
Definition: FunctionPropertiesAnalysis.h:23
llvm::FunctionPropertiesInfo::StoreInstCount
int64_t StoreInstCount
Definition: FunctionPropertiesAnalysis.h:53
llvm::LoopAnalysis
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:1243
llvm::FunctionPropertiesInfo::DirectCallsToDefinedFunctions
int64_t DirectCallsToDefinedFunctions
Number of direct calls made from this function to other functions defined in this module.
Definition: FunctionPropertiesAnalysis.h:47