1e8d8bef9SDimitry Andric //===- FunctionPropertiesAnalysis.cpp - Function Properties Analysis ------===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric //
9e8d8bef9SDimitry Andric // This file defines the FunctionPropertiesInfo and FunctionPropertiesAnalysis
10e8d8bef9SDimitry Andric // classes used to extract function properties.
11e8d8bef9SDimitry Andric //
12e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
13e8d8bef9SDimitry Andric
14e8d8bef9SDimitry Andric #include "llvm/Analysis/FunctionPropertiesAnalysis.h"
1581ad6265SDimitry Andric #include "llvm/ADT/STLExtras.h"
1681ad6265SDimitry Andric #include "llvm/ADT/SetVector.h"
1781ad6265SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
1881ad6265SDimitry Andric #include "llvm/IR/CFG.h"
19*c9157d92SDimitry Andric #include "llvm/IR/Constants.h"
2081ad6265SDimitry Andric #include "llvm/IR/Dominators.h"
21e8d8bef9SDimitry Andric #include "llvm/IR/Instructions.h"
22*c9157d92SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
23*c9157d92SDimitry Andric #include "llvm/Support/CommandLine.h"
2481ad6265SDimitry Andric #include <deque>
25e8d8bef9SDimitry Andric
26e8d8bef9SDimitry Andric using namespace llvm;
27e8d8bef9SDimitry Andric
28*c9157d92SDimitry Andric namespace llvm {
29*c9157d92SDimitry Andric cl::opt<bool> EnableDetailedFunctionProperties(
30*c9157d92SDimitry Andric "enable-detailed-function-properties", cl::Hidden, cl::init(false),
31*c9157d92SDimitry Andric cl::desc("Whether or not to compute detailed function properties."));
32*c9157d92SDimitry Andric
33*c9157d92SDimitry Andric cl::opt<unsigned> BigBasicBlockInstructionThreshold(
34*c9157d92SDimitry Andric "big-basic-block-instruction-threshold", cl::Hidden, cl::init(500),
35*c9157d92SDimitry Andric cl::desc("The minimum number of instructions a basic block should contain "
36*c9157d92SDimitry Andric "before being considered big."));
37*c9157d92SDimitry Andric
38*c9157d92SDimitry Andric cl::opt<unsigned> MediumBasicBlockInstructionThreshold(
39*c9157d92SDimitry Andric "medium-basic-block-instruction-threshold", cl::Hidden, cl::init(15),
40*c9157d92SDimitry Andric cl::desc("The minimum number of instructions a basic block should contain "
41*c9157d92SDimitry Andric "before being considered medium-sized."));
42*c9157d92SDimitry Andric }
43*c9157d92SDimitry Andric
44*c9157d92SDimitry Andric static cl::opt<unsigned> CallWithManyArgumentsThreshold(
45*c9157d92SDimitry Andric "call-with-many-arguments-threshold", cl::Hidden, cl::init(4),
46*c9157d92SDimitry Andric cl::desc("The minimum number of arguments a function call must have before "
47*c9157d92SDimitry Andric "it is considered having many arguments."));
48*c9157d92SDimitry Andric
4981ad6265SDimitry Andric namespace {
getNrBlocksFromCond(const BasicBlock & BB)5081ad6265SDimitry Andric int64_t getNrBlocksFromCond(const BasicBlock &BB) {
5181ad6265SDimitry Andric int64_t Ret = 0;
52e8d8bef9SDimitry Andric if (const auto *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
53e8d8bef9SDimitry Andric if (BI->isConditional())
5481ad6265SDimitry Andric Ret += BI->getNumSuccessors();
55e8d8bef9SDimitry Andric } else if (const auto *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
5681ad6265SDimitry Andric Ret += (SI->getNumCases() + (nullptr != SI->getDefaultDest()));
5781ad6265SDimitry Andric }
5881ad6265SDimitry Andric return Ret;
59e8d8bef9SDimitry Andric }
60e8d8bef9SDimitry Andric
getUses(const Function & F)6181ad6265SDimitry Andric int64_t getUses(const Function &F) {
6281ad6265SDimitry Andric return ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses();
6381ad6265SDimitry Andric }
6481ad6265SDimitry Andric } // namespace
6581ad6265SDimitry Andric
reIncludeBB(const BasicBlock & BB)6681ad6265SDimitry Andric void FunctionPropertiesInfo::reIncludeBB(const BasicBlock &BB) {
6781ad6265SDimitry Andric updateForBB(BB, +1);
6881ad6265SDimitry Andric }
6981ad6265SDimitry Andric
updateForBB(const BasicBlock & BB,int64_t Direction)7081ad6265SDimitry Andric void FunctionPropertiesInfo::updateForBB(const BasicBlock &BB,
7181ad6265SDimitry Andric int64_t Direction) {
7281ad6265SDimitry Andric assert(Direction == 1 || Direction == -1);
7381ad6265SDimitry Andric BasicBlockCount += Direction;
7481ad6265SDimitry Andric BlocksReachedFromConditionalInstruction +=
7581ad6265SDimitry Andric (Direction * getNrBlocksFromCond(BB));
76e8d8bef9SDimitry Andric for (const auto &I : BB) {
77e8d8bef9SDimitry Andric if (auto *CS = dyn_cast<CallBase>(&I)) {
78e8d8bef9SDimitry Andric const auto *Callee = CS->getCalledFunction();
79e8d8bef9SDimitry Andric if (Callee && !Callee->isIntrinsic() && !Callee->isDeclaration())
8081ad6265SDimitry Andric DirectCallsToDefinedFunctions += Direction;
81e8d8bef9SDimitry Andric }
82e8d8bef9SDimitry Andric if (I.getOpcode() == Instruction::Load) {
8381ad6265SDimitry Andric LoadInstCount += Direction;
84e8d8bef9SDimitry Andric } else if (I.getOpcode() == Instruction::Store) {
8581ad6265SDimitry Andric StoreInstCount += Direction;
86e8d8bef9SDimitry Andric }
87e8d8bef9SDimitry Andric }
8881ad6265SDimitry Andric TotalInstructionCount += Direction * BB.sizeWithoutDebug();
89*c9157d92SDimitry Andric
90*c9157d92SDimitry Andric if (EnableDetailedFunctionProperties) {
91*c9157d92SDimitry Andric unsigned SuccessorCount = succ_size(&BB);
92*c9157d92SDimitry Andric if (SuccessorCount == 1)
93*c9157d92SDimitry Andric BasicBlocksWithSingleSuccessor += Direction;
94*c9157d92SDimitry Andric else if (SuccessorCount == 2)
95*c9157d92SDimitry Andric BasicBlocksWithTwoSuccessors += Direction;
96*c9157d92SDimitry Andric else if (SuccessorCount > 2)
97*c9157d92SDimitry Andric BasicBlocksWithMoreThanTwoSuccessors += Direction;
98*c9157d92SDimitry Andric
99*c9157d92SDimitry Andric unsigned PredecessorCount = pred_size(&BB);
100*c9157d92SDimitry Andric if (PredecessorCount == 1)
101*c9157d92SDimitry Andric BasicBlocksWithSinglePredecessor += Direction;
102*c9157d92SDimitry Andric else if (PredecessorCount == 2)
103*c9157d92SDimitry Andric BasicBlocksWithTwoPredecessors += Direction;
104*c9157d92SDimitry Andric else if (PredecessorCount > 2)
105*c9157d92SDimitry Andric BasicBlocksWithMoreThanTwoPredecessors += Direction;
106*c9157d92SDimitry Andric
107*c9157d92SDimitry Andric if (TotalInstructionCount > BigBasicBlockInstructionThreshold)
108*c9157d92SDimitry Andric BigBasicBlocks += Direction;
109*c9157d92SDimitry Andric else if (TotalInstructionCount > MediumBasicBlockInstructionThreshold)
110*c9157d92SDimitry Andric MediumBasicBlocks += Direction;
111*c9157d92SDimitry Andric else
112*c9157d92SDimitry Andric SmallBasicBlocks += Direction;
113*c9157d92SDimitry Andric
114*c9157d92SDimitry Andric // Calculate critical edges by looking through all successors of a basic
115*c9157d92SDimitry Andric // block that has multiple successors and finding ones that have multiple
116*c9157d92SDimitry Andric // predecessors, which represent critical edges.
117*c9157d92SDimitry Andric if (SuccessorCount > 1) {
118*c9157d92SDimitry Andric for (const auto *Successor : successors(&BB)) {
119*c9157d92SDimitry Andric if (pred_size(Successor) > 1)
120*c9157d92SDimitry Andric CriticalEdgeCount += Direction;
121*c9157d92SDimitry Andric }
122*c9157d92SDimitry Andric }
123*c9157d92SDimitry Andric
124*c9157d92SDimitry Andric ControlFlowEdgeCount += Direction * SuccessorCount;
125*c9157d92SDimitry Andric
126*c9157d92SDimitry Andric if (const auto *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
127*c9157d92SDimitry Andric if (!BI->isConditional())
128*c9157d92SDimitry Andric UnconditionalBranchCount += Direction;
129*c9157d92SDimitry Andric }
130*c9157d92SDimitry Andric
131*c9157d92SDimitry Andric for (const Instruction &I : BB.instructionsWithoutDebug()) {
132*c9157d92SDimitry Andric if (I.isCast())
133*c9157d92SDimitry Andric CastInstructionCount += Direction;
134*c9157d92SDimitry Andric
135*c9157d92SDimitry Andric if (I.getType()->isFloatTy())
136*c9157d92SDimitry Andric FloatingPointInstructionCount += Direction;
137*c9157d92SDimitry Andric else if (I.getType()->isIntegerTy())
138*c9157d92SDimitry Andric IntegerInstructionCount += Direction;
139*c9157d92SDimitry Andric
140*c9157d92SDimitry Andric if (isa<IntrinsicInst>(I))
141*c9157d92SDimitry Andric ++IntrinsicCount;
142*c9157d92SDimitry Andric
143*c9157d92SDimitry Andric if (const auto *Call = dyn_cast<CallInst>(&I)) {
144*c9157d92SDimitry Andric if (Call->isIndirectCall())
145*c9157d92SDimitry Andric IndirectCallCount += Direction;
146*c9157d92SDimitry Andric else
147*c9157d92SDimitry Andric DirectCallCount += Direction;
148*c9157d92SDimitry Andric
149*c9157d92SDimitry Andric if (Call->getType()->isIntegerTy())
150*c9157d92SDimitry Andric CallReturnsIntegerCount += Direction;
151*c9157d92SDimitry Andric else if (Call->getType()->isFloatingPointTy())
152*c9157d92SDimitry Andric CallReturnsFloatCount += Direction;
153*c9157d92SDimitry Andric else if (Call->getType()->isPointerTy())
154*c9157d92SDimitry Andric CallReturnsPointerCount += Direction;
155*c9157d92SDimitry Andric else if (Call->getType()->isVectorTy()) {
156*c9157d92SDimitry Andric if (Call->getType()->getScalarType()->isIntegerTy())
157*c9157d92SDimitry Andric CallReturnsVectorIntCount += Direction;
158*c9157d92SDimitry Andric else if (Call->getType()->getScalarType()->isFloatingPointTy())
159*c9157d92SDimitry Andric CallReturnsVectorFloatCount += Direction;
160*c9157d92SDimitry Andric else if (Call->getType()->getScalarType()->isPointerTy())
161*c9157d92SDimitry Andric CallReturnsVectorPointerCount += Direction;
162*c9157d92SDimitry Andric }
163*c9157d92SDimitry Andric
164*c9157d92SDimitry Andric if (Call->arg_size() > CallWithManyArgumentsThreshold)
165*c9157d92SDimitry Andric CallWithManyArgumentsCount += Direction;
166*c9157d92SDimitry Andric
167*c9157d92SDimitry Andric for (const auto &Arg : Call->args()) {
168*c9157d92SDimitry Andric if (Arg->getType()->isPointerTy()) {
169*c9157d92SDimitry Andric CallWithPointerArgumentCount += Direction;
170*c9157d92SDimitry Andric break;
171*c9157d92SDimitry Andric }
172*c9157d92SDimitry Andric }
173*c9157d92SDimitry Andric }
174*c9157d92SDimitry Andric
175*c9157d92SDimitry Andric #define COUNT_OPERAND(OPTYPE) \
176*c9157d92SDimitry Andric if (isa<OPTYPE>(Operand)) { \
177*c9157d92SDimitry Andric OPTYPE##OperandCount += Direction; \
178*c9157d92SDimitry Andric continue; \
179*c9157d92SDimitry Andric }
180*c9157d92SDimitry Andric
181*c9157d92SDimitry Andric for (unsigned int OperandIndex = 0; OperandIndex < I.getNumOperands();
182*c9157d92SDimitry Andric ++OperandIndex) {
183*c9157d92SDimitry Andric Value *Operand = I.getOperand(OperandIndex);
184*c9157d92SDimitry Andric COUNT_OPERAND(GlobalValue)
185*c9157d92SDimitry Andric COUNT_OPERAND(ConstantInt)
186*c9157d92SDimitry Andric COUNT_OPERAND(ConstantFP)
187*c9157d92SDimitry Andric COUNT_OPERAND(Constant)
188*c9157d92SDimitry Andric COUNT_OPERAND(Instruction)
189*c9157d92SDimitry Andric COUNT_OPERAND(BasicBlock)
190*c9157d92SDimitry Andric COUNT_OPERAND(InlineAsm)
191*c9157d92SDimitry Andric COUNT_OPERAND(Argument)
192*c9157d92SDimitry Andric
193*c9157d92SDimitry Andric // We only get to this point if we haven't matched any of the other
194*c9157d92SDimitry Andric // operand types.
195*c9157d92SDimitry Andric UnknownOperandCount += Direction;
196*c9157d92SDimitry Andric }
197*c9157d92SDimitry Andric
198*c9157d92SDimitry Andric #undef CHECK_OPERAND
199*c9157d92SDimitry Andric }
200*c9157d92SDimitry Andric }
201e8d8bef9SDimitry Andric }
20281ad6265SDimitry Andric
updateAggregateStats(const Function & F,const LoopInfo & LI)20381ad6265SDimitry Andric void FunctionPropertiesInfo::updateAggregateStats(const Function &F,
20481ad6265SDimitry Andric const LoopInfo &LI) {
20581ad6265SDimitry Andric
20681ad6265SDimitry Andric Uses = getUses(F);
20781ad6265SDimitry Andric TopLevelLoopCount = llvm::size(LI);
20881ad6265SDimitry Andric MaxLoopDepth = 0;
20981ad6265SDimitry Andric std::deque<const Loop *> Worklist;
21081ad6265SDimitry Andric llvm::append_range(Worklist, LI);
21181ad6265SDimitry Andric while (!Worklist.empty()) {
21281ad6265SDimitry Andric const auto *L = Worklist.front();
21381ad6265SDimitry Andric MaxLoopDepth =
21481ad6265SDimitry Andric std::max(MaxLoopDepth, static_cast<int64_t>(L->getLoopDepth()));
21581ad6265SDimitry Andric Worklist.pop_front();
21681ad6265SDimitry Andric llvm::append_range(Worklist, L->getSubLoops());
21781ad6265SDimitry Andric }
21881ad6265SDimitry Andric }
21981ad6265SDimitry Andric
getFunctionPropertiesInfo(Function & F,FunctionAnalysisManager & FAM)22081ad6265SDimitry Andric FunctionPropertiesInfo FunctionPropertiesInfo::getFunctionPropertiesInfo(
221fe013be4SDimitry Andric Function &F, FunctionAnalysisManager &FAM) {
222fe013be4SDimitry Andric return getFunctionPropertiesInfo(F, FAM.getResult<DominatorTreeAnalysis>(F),
223fe013be4SDimitry Andric FAM.getResult<LoopAnalysis>(F));
224fe013be4SDimitry Andric }
225fe013be4SDimitry Andric
getFunctionPropertiesInfo(const Function & F,const DominatorTree & DT,const LoopInfo & LI)226fe013be4SDimitry Andric FunctionPropertiesInfo FunctionPropertiesInfo::getFunctionPropertiesInfo(
227fe013be4SDimitry Andric const Function &F, const DominatorTree &DT, const LoopInfo &LI) {
22881ad6265SDimitry Andric
22981ad6265SDimitry Andric FunctionPropertiesInfo FPI;
23081ad6265SDimitry Andric for (const auto &BB : F)
23181ad6265SDimitry Andric if (DT.isReachableFromEntry(&BB))
23281ad6265SDimitry Andric FPI.reIncludeBB(BB);
23381ad6265SDimitry Andric FPI.updateAggregateStats(F, LI);
234e8d8bef9SDimitry Andric return FPI;
235e8d8bef9SDimitry Andric }
236e8d8bef9SDimitry Andric
print(raw_ostream & OS) const237e8d8bef9SDimitry Andric void FunctionPropertiesInfo::print(raw_ostream &OS) const {
238*c9157d92SDimitry Andric #define PRINT_PROPERTY(PROP_NAME) OS << #PROP_NAME ": " << PROP_NAME << "\n";
239*c9157d92SDimitry Andric
240*c9157d92SDimitry Andric PRINT_PROPERTY(BasicBlockCount)
241*c9157d92SDimitry Andric PRINT_PROPERTY(BlocksReachedFromConditionalInstruction)
242*c9157d92SDimitry Andric PRINT_PROPERTY(Uses)
243*c9157d92SDimitry Andric PRINT_PROPERTY(DirectCallsToDefinedFunctions)
244*c9157d92SDimitry Andric PRINT_PROPERTY(LoadInstCount)
245*c9157d92SDimitry Andric PRINT_PROPERTY(StoreInstCount)
246*c9157d92SDimitry Andric PRINT_PROPERTY(MaxLoopDepth)
247*c9157d92SDimitry Andric PRINT_PROPERTY(TopLevelLoopCount)
248*c9157d92SDimitry Andric PRINT_PROPERTY(TotalInstructionCount)
249*c9157d92SDimitry Andric
250*c9157d92SDimitry Andric if (EnableDetailedFunctionProperties) {
251*c9157d92SDimitry Andric PRINT_PROPERTY(BasicBlocksWithSingleSuccessor)
252*c9157d92SDimitry Andric PRINT_PROPERTY(BasicBlocksWithTwoSuccessors)
253*c9157d92SDimitry Andric PRINT_PROPERTY(BasicBlocksWithMoreThanTwoSuccessors)
254*c9157d92SDimitry Andric PRINT_PROPERTY(BasicBlocksWithSinglePredecessor)
255*c9157d92SDimitry Andric PRINT_PROPERTY(BasicBlocksWithTwoPredecessors)
256*c9157d92SDimitry Andric PRINT_PROPERTY(BasicBlocksWithMoreThanTwoPredecessors)
257*c9157d92SDimitry Andric PRINT_PROPERTY(BigBasicBlocks)
258*c9157d92SDimitry Andric PRINT_PROPERTY(MediumBasicBlocks)
259*c9157d92SDimitry Andric PRINT_PROPERTY(SmallBasicBlocks)
260*c9157d92SDimitry Andric PRINT_PROPERTY(CastInstructionCount)
261*c9157d92SDimitry Andric PRINT_PROPERTY(FloatingPointInstructionCount)
262*c9157d92SDimitry Andric PRINT_PROPERTY(IntegerInstructionCount)
263*c9157d92SDimitry Andric PRINT_PROPERTY(ConstantIntOperandCount)
264*c9157d92SDimitry Andric PRINT_PROPERTY(ConstantFPOperandCount)
265*c9157d92SDimitry Andric PRINT_PROPERTY(ConstantOperandCount)
266*c9157d92SDimitry Andric PRINT_PROPERTY(InstructionOperandCount)
267*c9157d92SDimitry Andric PRINT_PROPERTY(BasicBlockOperandCount)
268*c9157d92SDimitry Andric PRINT_PROPERTY(GlobalValueOperandCount)
269*c9157d92SDimitry Andric PRINT_PROPERTY(InlineAsmOperandCount)
270*c9157d92SDimitry Andric PRINT_PROPERTY(ArgumentOperandCount)
271*c9157d92SDimitry Andric PRINT_PROPERTY(UnknownOperandCount)
272*c9157d92SDimitry Andric PRINT_PROPERTY(CriticalEdgeCount)
273*c9157d92SDimitry Andric PRINT_PROPERTY(ControlFlowEdgeCount)
274*c9157d92SDimitry Andric PRINT_PROPERTY(UnconditionalBranchCount)
275*c9157d92SDimitry Andric PRINT_PROPERTY(IntrinsicCount)
276*c9157d92SDimitry Andric PRINT_PROPERTY(DirectCallCount)
277*c9157d92SDimitry Andric PRINT_PROPERTY(IndirectCallCount)
278*c9157d92SDimitry Andric PRINT_PROPERTY(CallReturnsIntegerCount)
279*c9157d92SDimitry Andric PRINT_PROPERTY(CallReturnsFloatCount)
280*c9157d92SDimitry Andric PRINT_PROPERTY(CallReturnsPointerCount)
281*c9157d92SDimitry Andric PRINT_PROPERTY(CallReturnsVectorIntCount)
282*c9157d92SDimitry Andric PRINT_PROPERTY(CallReturnsVectorFloatCount)
283*c9157d92SDimitry Andric PRINT_PROPERTY(CallReturnsVectorPointerCount)
284*c9157d92SDimitry Andric PRINT_PROPERTY(CallWithManyArgumentsCount)
285*c9157d92SDimitry Andric PRINT_PROPERTY(CallWithPointerArgumentCount)
286*c9157d92SDimitry Andric }
287*c9157d92SDimitry Andric
288*c9157d92SDimitry Andric #undef PRINT_PROPERTY
289*c9157d92SDimitry Andric
290*c9157d92SDimitry Andric OS << "\n";
291e8d8bef9SDimitry Andric }
292e8d8bef9SDimitry Andric
293e8d8bef9SDimitry Andric AnalysisKey FunctionPropertiesAnalysis::Key;
294e8d8bef9SDimitry Andric
295e8d8bef9SDimitry Andric FunctionPropertiesInfo
run(Function & F,FunctionAnalysisManager & FAM)296e8d8bef9SDimitry Andric FunctionPropertiesAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
29781ad6265SDimitry Andric return FunctionPropertiesInfo::getFunctionPropertiesInfo(F, FAM);
298e8d8bef9SDimitry Andric }
299e8d8bef9SDimitry Andric
300e8d8bef9SDimitry Andric PreservedAnalyses
run(Function & F,FunctionAnalysisManager & AM)301e8d8bef9SDimitry Andric FunctionPropertiesPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
302e8d8bef9SDimitry Andric OS << "Printing analysis results of CFA for function "
303e8d8bef9SDimitry Andric << "'" << F.getName() << "':"
304e8d8bef9SDimitry Andric << "\n";
305e8d8bef9SDimitry Andric AM.getResult<FunctionPropertiesAnalysis>(F).print(OS);
306e8d8bef9SDimitry Andric return PreservedAnalyses::all();
307e8d8bef9SDimitry Andric }
30881ad6265SDimitry Andric
FunctionPropertiesUpdater(FunctionPropertiesInfo & FPI,CallBase & CB)30981ad6265SDimitry Andric FunctionPropertiesUpdater::FunctionPropertiesUpdater(
310fe013be4SDimitry Andric FunctionPropertiesInfo &FPI, CallBase &CB)
31181ad6265SDimitry Andric : FPI(FPI), CallSiteBB(*CB.getParent()), Caller(*CallSiteBB.getParent()) {
31281ad6265SDimitry Andric assert(isa<CallInst>(CB) || isa<InvokeInst>(CB));
31381ad6265SDimitry Andric // For BBs that are likely to change, we subtract from feature totals their
31481ad6265SDimitry Andric // contribution. Some features, like max loop counts or depths, are left
31581ad6265SDimitry Andric // invalid, as they will be updated post-inlining.
31681ad6265SDimitry Andric SmallPtrSet<const BasicBlock *, 4> LikelyToChangeBBs;
31781ad6265SDimitry Andric // The CB BB will change - it'll either be split or the callee's body (single
31881ad6265SDimitry Andric // BB) will be pasted in.
31981ad6265SDimitry Andric LikelyToChangeBBs.insert(&CallSiteBB);
32081ad6265SDimitry Andric
32181ad6265SDimitry Andric // The caller's entry BB may change due to new alloca instructions.
32281ad6265SDimitry Andric LikelyToChangeBBs.insert(&*Caller.begin());
32381ad6265SDimitry Andric
32481ad6265SDimitry Andric // The successors may become unreachable in the case of `invoke` inlining.
32581ad6265SDimitry Andric // We track successors separately, too, because they form a boundary, together
32681ad6265SDimitry Andric // with the CB BB ('Entry') between which the inlined callee will be pasted.
32781ad6265SDimitry Andric Successors.insert(succ_begin(&CallSiteBB), succ_end(&CallSiteBB));
32881ad6265SDimitry Andric
32981ad6265SDimitry Andric // Inlining only handles invoke and calls. If this is an invoke, and inlining
33081ad6265SDimitry Andric // it pulls another invoke, the original landing pad may get split, so as to
33181ad6265SDimitry Andric // share its content with other potential users. So the edge up to which we
33281ad6265SDimitry Andric // need to invalidate and then re-account BB data is the successors of the
33381ad6265SDimitry Andric // current landing pad. We can leave the current lp, too - if it doesn't get
33481ad6265SDimitry Andric // split, then it will be the place traversal stops. Either way, the
33581ad6265SDimitry Andric // discounted BBs will be checked if reachable and re-added.
33681ad6265SDimitry Andric if (const auto *II = dyn_cast<InvokeInst>(&CB)) {
33781ad6265SDimitry Andric const auto *UnwindDest = II->getUnwindDest();
33881ad6265SDimitry Andric Successors.insert(succ_begin(UnwindDest), succ_end(UnwindDest));
33981ad6265SDimitry Andric }
34081ad6265SDimitry Andric
34181ad6265SDimitry Andric // Exclude the CallSiteBB, if it happens to be its own successor (1-BB loop).
34281ad6265SDimitry Andric // We are only interested in BBs the graph moves past the callsite BB to
34381ad6265SDimitry Andric // define the frontier past which we don't want to re-process BBs. Including
34481ad6265SDimitry Andric // the callsite BB in this case would prematurely stop the traversal in
34581ad6265SDimitry Andric // finish().
34681ad6265SDimitry Andric Successors.erase(&CallSiteBB);
34781ad6265SDimitry Andric
34881ad6265SDimitry Andric for (const auto *BB : Successors)
34981ad6265SDimitry Andric LikelyToChangeBBs.insert(BB);
35081ad6265SDimitry Andric
35181ad6265SDimitry Andric // Commit the change. While some of the BBs accounted for above may play dual
35281ad6265SDimitry Andric // role - e.g. caller's entry BB may be the same as the callsite BB - set
35381ad6265SDimitry Andric // insertion semantics make sure we account them once. This needs to be
35481ad6265SDimitry Andric // followed in `finish`, too.
35581ad6265SDimitry Andric for (const auto *BB : LikelyToChangeBBs)
35681ad6265SDimitry Andric FPI.updateForBB(*BB, -1);
35781ad6265SDimitry Andric }
35881ad6265SDimitry Andric
finish(FunctionAnalysisManager & FAM) const35981ad6265SDimitry Andric void FunctionPropertiesUpdater::finish(FunctionAnalysisManager &FAM) const {
36081ad6265SDimitry Andric // Update feature values from the BBs that were copied from the callee, or
36181ad6265SDimitry Andric // might have been modified because of inlining. The latter have been
36281ad6265SDimitry Andric // subtracted in the FunctionPropertiesUpdater ctor.
36381ad6265SDimitry Andric // There could be successors that were reached before but now are only
36481ad6265SDimitry Andric // reachable from elsewhere in the CFG.
36581ad6265SDimitry Andric // One example is the following diamond CFG (lines are arrows pointing down):
36681ad6265SDimitry Andric // A
36781ad6265SDimitry Andric // / \
36881ad6265SDimitry Andric // B C
36981ad6265SDimitry Andric // | |
37081ad6265SDimitry Andric // | D
37181ad6265SDimitry Andric // | |
37281ad6265SDimitry Andric // | E
37381ad6265SDimitry Andric // \ /
37481ad6265SDimitry Andric // F
37581ad6265SDimitry Andric // There's a call site in C that is inlined. Upon doing that, it turns out
37681ad6265SDimitry Andric // it expands to
37781ad6265SDimitry Andric // call void @llvm.trap()
37881ad6265SDimitry Andric // unreachable
37981ad6265SDimitry Andric // F isn't reachable from C anymore, but we did discount it when we set up
38081ad6265SDimitry Andric // FunctionPropertiesUpdater, so we need to re-include it here.
38181ad6265SDimitry Andric // At the same time, D and E were reachable before, but now are not anymore,
38281ad6265SDimitry Andric // so we need to leave D out (we discounted it at setup), and explicitly
38381ad6265SDimitry Andric // remove E.
38481ad6265SDimitry Andric SetVector<const BasicBlock *> Reinclude;
38581ad6265SDimitry Andric SetVector<const BasicBlock *> Unreachable;
38681ad6265SDimitry Andric const auto &DT =
38781ad6265SDimitry Andric FAM.getResult<DominatorTreeAnalysis>(const_cast<Function &>(Caller));
38881ad6265SDimitry Andric
38981ad6265SDimitry Andric if (&CallSiteBB != &*Caller.begin())
39081ad6265SDimitry Andric Reinclude.insert(&*Caller.begin());
39181ad6265SDimitry Andric
39281ad6265SDimitry Andric // Distribute the successors to the 2 buckets.
39381ad6265SDimitry Andric for (const auto *Succ : Successors)
39481ad6265SDimitry Andric if (DT.isReachableFromEntry(Succ))
39581ad6265SDimitry Andric Reinclude.insert(Succ);
39681ad6265SDimitry Andric else
39781ad6265SDimitry Andric Unreachable.insert(Succ);
39881ad6265SDimitry Andric
39981ad6265SDimitry Andric // For reinclusion, we want to stop at the reachable successors, who are at
40081ad6265SDimitry Andric // the beginning of the worklist; but, starting from the callsite bb and
40181ad6265SDimitry Andric // ending at those successors, we also want to perform a traversal.
40281ad6265SDimitry Andric // IncludeSuccessorsMark is the index after which we include successors.
40381ad6265SDimitry Andric const auto IncludeSuccessorsMark = Reinclude.size();
40481ad6265SDimitry Andric bool CSInsertion = Reinclude.insert(&CallSiteBB);
40581ad6265SDimitry Andric (void)CSInsertion;
40681ad6265SDimitry Andric assert(CSInsertion);
40781ad6265SDimitry Andric for (size_t I = 0; I < Reinclude.size(); ++I) {
40881ad6265SDimitry Andric const auto *BB = Reinclude[I];
40981ad6265SDimitry Andric FPI.reIncludeBB(*BB);
41081ad6265SDimitry Andric if (I >= IncludeSuccessorsMark)
41181ad6265SDimitry Andric Reinclude.insert(succ_begin(BB), succ_end(BB));
41281ad6265SDimitry Andric }
41381ad6265SDimitry Andric
41481ad6265SDimitry Andric // For exclusion, we don't need to exclude the set of BBs that were successors
41581ad6265SDimitry Andric // before and are now unreachable, because we already did that at setup. For
41681ad6265SDimitry Andric // the rest, as long as a successor is unreachable, we want to explicitly
41781ad6265SDimitry Andric // exclude it.
41881ad6265SDimitry Andric const auto AlreadyExcludedMark = Unreachable.size();
41981ad6265SDimitry Andric for (size_t I = 0; I < Unreachable.size(); ++I) {
42081ad6265SDimitry Andric const auto *U = Unreachable[I];
42181ad6265SDimitry Andric if (I >= AlreadyExcludedMark)
42281ad6265SDimitry Andric FPI.updateForBB(*U, -1);
42381ad6265SDimitry Andric for (const auto *Succ : successors(U))
42481ad6265SDimitry Andric if (!DT.isReachableFromEntry(Succ))
42581ad6265SDimitry Andric Unreachable.insert(Succ);
42681ad6265SDimitry Andric }
42781ad6265SDimitry Andric
42881ad6265SDimitry Andric const auto &LI = FAM.getResult<LoopAnalysis>(const_cast<Function &>(Caller));
42981ad6265SDimitry Andric FPI.updateAggregateStats(Caller, LI);
430fe013be4SDimitry Andric }
431fe013be4SDimitry Andric
isUpdateValid(Function & F,const FunctionPropertiesInfo & FPI,FunctionAnalysisManager & FAM)432fe013be4SDimitry Andric bool FunctionPropertiesUpdater::isUpdateValid(Function &F,
433fe013be4SDimitry Andric const FunctionPropertiesInfo &FPI,
434fe013be4SDimitry Andric FunctionAnalysisManager &FAM) {
435fe013be4SDimitry Andric DominatorTree DT(F);
436fe013be4SDimitry Andric LoopInfo LI(DT);
437fe013be4SDimitry Andric auto Fresh = FunctionPropertiesInfo::getFunctionPropertiesInfo(F, DT, LI);
438fe013be4SDimitry Andric return FPI == Fresh;
43981ad6265SDimitry Andric }
440