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" 1981ad6265SDimitry Andric #include "llvm/IR/Dominators.h" 20e8d8bef9SDimitry Andric #include "llvm/IR/Instructions.h" 2181ad6265SDimitry Andric #include <deque> 22e8d8bef9SDimitry Andric 23e8d8bef9SDimitry Andric using namespace llvm; 24e8d8bef9SDimitry Andric 2581ad6265SDimitry Andric namespace { 2681ad6265SDimitry Andric int64_t getNrBlocksFromCond(const BasicBlock &BB) { 2781ad6265SDimitry Andric int64_t Ret = 0; 28e8d8bef9SDimitry Andric if (const auto *BI = dyn_cast<BranchInst>(BB.getTerminator())) { 29e8d8bef9SDimitry Andric if (BI->isConditional()) 3081ad6265SDimitry Andric Ret += BI->getNumSuccessors(); 31e8d8bef9SDimitry Andric } else if (const auto *SI = dyn_cast<SwitchInst>(BB.getTerminator())) { 3281ad6265SDimitry Andric Ret += (SI->getNumCases() + (nullptr != SI->getDefaultDest())); 3381ad6265SDimitry Andric } 3481ad6265SDimitry Andric return Ret; 35e8d8bef9SDimitry Andric } 36e8d8bef9SDimitry Andric 3781ad6265SDimitry Andric int64_t getUses(const Function &F) { 3881ad6265SDimitry Andric return ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses(); 3981ad6265SDimitry Andric } 4081ad6265SDimitry Andric } // namespace 4181ad6265SDimitry Andric 4281ad6265SDimitry Andric void FunctionPropertiesInfo::reIncludeBB(const BasicBlock &BB) { 4381ad6265SDimitry Andric updateForBB(BB, +1); 4481ad6265SDimitry Andric } 4581ad6265SDimitry Andric 4681ad6265SDimitry Andric void FunctionPropertiesInfo::updateForBB(const BasicBlock &BB, 4781ad6265SDimitry Andric int64_t Direction) { 4881ad6265SDimitry Andric assert(Direction == 1 || Direction == -1); 4981ad6265SDimitry Andric BasicBlockCount += Direction; 5081ad6265SDimitry Andric BlocksReachedFromConditionalInstruction += 5181ad6265SDimitry Andric (Direction * getNrBlocksFromCond(BB)); 52e8d8bef9SDimitry Andric for (const auto &I : BB) { 53e8d8bef9SDimitry Andric if (auto *CS = dyn_cast<CallBase>(&I)) { 54e8d8bef9SDimitry Andric const auto *Callee = CS->getCalledFunction(); 55e8d8bef9SDimitry Andric if (Callee && !Callee->isIntrinsic() && !Callee->isDeclaration()) 5681ad6265SDimitry Andric DirectCallsToDefinedFunctions += Direction; 57e8d8bef9SDimitry Andric } 58e8d8bef9SDimitry Andric if (I.getOpcode() == Instruction::Load) { 5981ad6265SDimitry Andric LoadInstCount += Direction; 60e8d8bef9SDimitry Andric } else if (I.getOpcode() == Instruction::Store) { 6181ad6265SDimitry Andric StoreInstCount += Direction; 62e8d8bef9SDimitry Andric } 63e8d8bef9SDimitry Andric } 6481ad6265SDimitry Andric TotalInstructionCount += Direction * BB.sizeWithoutDebug(); 65e8d8bef9SDimitry Andric } 6681ad6265SDimitry Andric 6781ad6265SDimitry Andric void FunctionPropertiesInfo::updateAggregateStats(const Function &F, 6881ad6265SDimitry Andric const LoopInfo &LI) { 6981ad6265SDimitry Andric 7081ad6265SDimitry Andric Uses = getUses(F); 7181ad6265SDimitry Andric TopLevelLoopCount = llvm::size(LI); 7281ad6265SDimitry Andric MaxLoopDepth = 0; 7381ad6265SDimitry Andric std::deque<const Loop *> Worklist; 7481ad6265SDimitry Andric llvm::append_range(Worklist, LI); 7581ad6265SDimitry Andric while (!Worklist.empty()) { 7681ad6265SDimitry Andric const auto *L = Worklist.front(); 7781ad6265SDimitry Andric MaxLoopDepth = 7881ad6265SDimitry Andric std::max(MaxLoopDepth, static_cast<int64_t>(L->getLoopDepth())); 7981ad6265SDimitry Andric Worklist.pop_front(); 8081ad6265SDimitry Andric llvm::append_range(Worklist, L->getSubLoops()); 8181ad6265SDimitry Andric } 8281ad6265SDimitry Andric } 8381ad6265SDimitry Andric 8481ad6265SDimitry Andric FunctionPropertiesInfo FunctionPropertiesInfo::getFunctionPropertiesInfo( 85*06c3fb27SDimitry Andric Function &F, FunctionAnalysisManager &FAM) { 86*06c3fb27SDimitry Andric return getFunctionPropertiesInfo(F, FAM.getResult<DominatorTreeAnalysis>(F), 87*06c3fb27SDimitry Andric FAM.getResult<LoopAnalysis>(F)); 88*06c3fb27SDimitry Andric } 89*06c3fb27SDimitry Andric 90*06c3fb27SDimitry Andric FunctionPropertiesInfo FunctionPropertiesInfo::getFunctionPropertiesInfo( 91*06c3fb27SDimitry Andric const Function &F, const DominatorTree &DT, const LoopInfo &LI) { 9281ad6265SDimitry Andric 9381ad6265SDimitry Andric FunctionPropertiesInfo FPI; 9481ad6265SDimitry Andric for (const auto &BB : F) 9581ad6265SDimitry Andric if (DT.isReachableFromEntry(&BB)) 9681ad6265SDimitry Andric FPI.reIncludeBB(BB); 9781ad6265SDimitry Andric FPI.updateAggregateStats(F, LI); 98e8d8bef9SDimitry Andric return FPI; 99e8d8bef9SDimitry Andric } 100e8d8bef9SDimitry Andric 101e8d8bef9SDimitry Andric void FunctionPropertiesInfo::print(raw_ostream &OS) const { 102e8d8bef9SDimitry Andric OS << "BasicBlockCount: " << BasicBlockCount << "\n" 103e8d8bef9SDimitry Andric << "BlocksReachedFromConditionalInstruction: " 104e8d8bef9SDimitry Andric << BlocksReachedFromConditionalInstruction << "\n" 105e8d8bef9SDimitry Andric << "Uses: " << Uses << "\n" 106e8d8bef9SDimitry Andric << "DirectCallsToDefinedFunctions: " << DirectCallsToDefinedFunctions 107e8d8bef9SDimitry Andric << "\n" 108e8d8bef9SDimitry Andric << "LoadInstCount: " << LoadInstCount << "\n" 109e8d8bef9SDimitry Andric << "StoreInstCount: " << StoreInstCount << "\n" 110e8d8bef9SDimitry Andric << "MaxLoopDepth: " << MaxLoopDepth << "\n" 11181ad6265SDimitry Andric << "TopLevelLoopCount: " << TopLevelLoopCount << "\n" 11281ad6265SDimitry Andric << "TotalInstructionCount: " << TotalInstructionCount << "\n\n"; 113e8d8bef9SDimitry Andric } 114e8d8bef9SDimitry Andric 115e8d8bef9SDimitry Andric AnalysisKey FunctionPropertiesAnalysis::Key; 116e8d8bef9SDimitry Andric 117e8d8bef9SDimitry Andric FunctionPropertiesInfo 118e8d8bef9SDimitry Andric FunctionPropertiesAnalysis::run(Function &F, FunctionAnalysisManager &FAM) { 11981ad6265SDimitry Andric return FunctionPropertiesInfo::getFunctionPropertiesInfo(F, FAM); 120e8d8bef9SDimitry Andric } 121e8d8bef9SDimitry Andric 122e8d8bef9SDimitry Andric PreservedAnalyses 123e8d8bef9SDimitry Andric FunctionPropertiesPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { 124e8d8bef9SDimitry Andric OS << "Printing analysis results of CFA for function " 125e8d8bef9SDimitry Andric << "'" << F.getName() << "':" 126e8d8bef9SDimitry Andric << "\n"; 127e8d8bef9SDimitry Andric AM.getResult<FunctionPropertiesAnalysis>(F).print(OS); 128e8d8bef9SDimitry Andric return PreservedAnalyses::all(); 129e8d8bef9SDimitry Andric } 13081ad6265SDimitry Andric 13181ad6265SDimitry Andric FunctionPropertiesUpdater::FunctionPropertiesUpdater( 132*06c3fb27SDimitry Andric FunctionPropertiesInfo &FPI, CallBase &CB) 13381ad6265SDimitry Andric : FPI(FPI), CallSiteBB(*CB.getParent()), Caller(*CallSiteBB.getParent()) { 13481ad6265SDimitry Andric assert(isa<CallInst>(CB) || isa<InvokeInst>(CB)); 13581ad6265SDimitry Andric // For BBs that are likely to change, we subtract from feature totals their 13681ad6265SDimitry Andric // contribution. Some features, like max loop counts or depths, are left 13781ad6265SDimitry Andric // invalid, as they will be updated post-inlining. 13881ad6265SDimitry Andric SmallPtrSet<const BasicBlock *, 4> LikelyToChangeBBs; 13981ad6265SDimitry Andric // The CB BB will change - it'll either be split or the callee's body (single 14081ad6265SDimitry Andric // BB) will be pasted in. 14181ad6265SDimitry Andric LikelyToChangeBBs.insert(&CallSiteBB); 14281ad6265SDimitry Andric 14381ad6265SDimitry Andric // The caller's entry BB may change due to new alloca instructions. 14481ad6265SDimitry Andric LikelyToChangeBBs.insert(&*Caller.begin()); 14581ad6265SDimitry Andric 14681ad6265SDimitry Andric // The successors may become unreachable in the case of `invoke` inlining. 14781ad6265SDimitry Andric // We track successors separately, too, because they form a boundary, together 14881ad6265SDimitry Andric // with the CB BB ('Entry') between which the inlined callee will be pasted. 14981ad6265SDimitry Andric Successors.insert(succ_begin(&CallSiteBB), succ_end(&CallSiteBB)); 15081ad6265SDimitry Andric 15181ad6265SDimitry Andric // Inlining only handles invoke and calls. If this is an invoke, and inlining 15281ad6265SDimitry Andric // it pulls another invoke, the original landing pad may get split, so as to 15381ad6265SDimitry Andric // share its content with other potential users. So the edge up to which we 15481ad6265SDimitry Andric // need to invalidate and then re-account BB data is the successors of the 15581ad6265SDimitry Andric // current landing pad. We can leave the current lp, too - if it doesn't get 15681ad6265SDimitry Andric // split, then it will be the place traversal stops. Either way, the 15781ad6265SDimitry Andric // discounted BBs will be checked if reachable and re-added. 15881ad6265SDimitry Andric if (const auto *II = dyn_cast<InvokeInst>(&CB)) { 15981ad6265SDimitry Andric const auto *UnwindDest = II->getUnwindDest(); 16081ad6265SDimitry Andric Successors.insert(succ_begin(UnwindDest), succ_end(UnwindDest)); 16181ad6265SDimitry Andric } 16281ad6265SDimitry Andric 16381ad6265SDimitry Andric // Exclude the CallSiteBB, if it happens to be its own successor (1-BB loop). 16481ad6265SDimitry Andric // We are only interested in BBs the graph moves past the callsite BB to 16581ad6265SDimitry Andric // define the frontier past which we don't want to re-process BBs. Including 16681ad6265SDimitry Andric // the callsite BB in this case would prematurely stop the traversal in 16781ad6265SDimitry Andric // finish(). 16881ad6265SDimitry Andric Successors.erase(&CallSiteBB); 16981ad6265SDimitry Andric 17081ad6265SDimitry Andric for (const auto *BB : Successors) 17181ad6265SDimitry Andric LikelyToChangeBBs.insert(BB); 17281ad6265SDimitry Andric 17381ad6265SDimitry Andric // Commit the change. While some of the BBs accounted for above may play dual 17481ad6265SDimitry Andric // role - e.g. caller's entry BB may be the same as the callsite BB - set 17581ad6265SDimitry Andric // insertion semantics make sure we account them once. This needs to be 17681ad6265SDimitry Andric // followed in `finish`, too. 17781ad6265SDimitry Andric for (const auto *BB : LikelyToChangeBBs) 17881ad6265SDimitry Andric FPI.updateForBB(*BB, -1); 17981ad6265SDimitry Andric } 18081ad6265SDimitry Andric 18181ad6265SDimitry Andric void FunctionPropertiesUpdater::finish(FunctionAnalysisManager &FAM) const { 18281ad6265SDimitry Andric // Update feature values from the BBs that were copied from the callee, or 18381ad6265SDimitry Andric // might have been modified because of inlining. The latter have been 18481ad6265SDimitry Andric // subtracted in the FunctionPropertiesUpdater ctor. 18581ad6265SDimitry Andric // There could be successors that were reached before but now are only 18681ad6265SDimitry Andric // reachable from elsewhere in the CFG. 18781ad6265SDimitry Andric // One example is the following diamond CFG (lines are arrows pointing down): 18881ad6265SDimitry Andric // A 18981ad6265SDimitry Andric // / \ 19081ad6265SDimitry Andric // B C 19181ad6265SDimitry Andric // | | 19281ad6265SDimitry Andric // | D 19381ad6265SDimitry Andric // | | 19481ad6265SDimitry Andric // | E 19581ad6265SDimitry Andric // \ / 19681ad6265SDimitry Andric // F 19781ad6265SDimitry Andric // There's a call site in C that is inlined. Upon doing that, it turns out 19881ad6265SDimitry Andric // it expands to 19981ad6265SDimitry Andric // call void @llvm.trap() 20081ad6265SDimitry Andric // unreachable 20181ad6265SDimitry Andric // F isn't reachable from C anymore, but we did discount it when we set up 20281ad6265SDimitry Andric // FunctionPropertiesUpdater, so we need to re-include it here. 20381ad6265SDimitry Andric // At the same time, D and E were reachable before, but now are not anymore, 20481ad6265SDimitry Andric // so we need to leave D out (we discounted it at setup), and explicitly 20581ad6265SDimitry Andric // remove E. 20681ad6265SDimitry Andric SetVector<const BasicBlock *> Reinclude; 20781ad6265SDimitry Andric SetVector<const BasicBlock *> Unreachable; 20881ad6265SDimitry Andric const auto &DT = 20981ad6265SDimitry Andric FAM.getResult<DominatorTreeAnalysis>(const_cast<Function &>(Caller)); 21081ad6265SDimitry Andric 21181ad6265SDimitry Andric if (&CallSiteBB != &*Caller.begin()) 21281ad6265SDimitry Andric Reinclude.insert(&*Caller.begin()); 21381ad6265SDimitry Andric 21481ad6265SDimitry Andric // Distribute the successors to the 2 buckets. 21581ad6265SDimitry Andric for (const auto *Succ : Successors) 21681ad6265SDimitry Andric if (DT.isReachableFromEntry(Succ)) 21781ad6265SDimitry Andric Reinclude.insert(Succ); 21881ad6265SDimitry Andric else 21981ad6265SDimitry Andric Unreachable.insert(Succ); 22081ad6265SDimitry Andric 22181ad6265SDimitry Andric // For reinclusion, we want to stop at the reachable successors, who are at 22281ad6265SDimitry Andric // the beginning of the worklist; but, starting from the callsite bb and 22381ad6265SDimitry Andric // ending at those successors, we also want to perform a traversal. 22481ad6265SDimitry Andric // IncludeSuccessorsMark is the index after which we include successors. 22581ad6265SDimitry Andric const auto IncludeSuccessorsMark = Reinclude.size(); 22681ad6265SDimitry Andric bool CSInsertion = Reinclude.insert(&CallSiteBB); 22781ad6265SDimitry Andric (void)CSInsertion; 22881ad6265SDimitry Andric assert(CSInsertion); 22981ad6265SDimitry Andric for (size_t I = 0; I < Reinclude.size(); ++I) { 23081ad6265SDimitry Andric const auto *BB = Reinclude[I]; 23181ad6265SDimitry Andric FPI.reIncludeBB(*BB); 23281ad6265SDimitry Andric if (I >= IncludeSuccessorsMark) 23381ad6265SDimitry Andric Reinclude.insert(succ_begin(BB), succ_end(BB)); 23481ad6265SDimitry Andric } 23581ad6265SDimitry Andric 23681ad6265SDimitry Andric // For exclusion, we don't need to exclude the set of BBs that were successors 23781ad6265SDimitry Andric // before and are now unreachable, because we already did that at setup. For 23881ad6265SDimitry Andric // the rest, as long as a successor is unreachable, we want to explicitly 23981ad6265SDimitry Andric // exclude it. 24081ad6265SDimitry Andric const auto AlreadyExcludedMark = Unreachable.size(); 24181ad6265SDimitry Andric for (size_t I = 0; I < Unreachable.size(); ++I) { 24281ad6265SDimitry Andric const auto *U = Unreachable[I]; 24381ad6265SDimitry Andric if (I >= AlreadyExcludedMark) 24481ad6265SDimitry Andric FPI.updateForBB(*U, -1); 24581ad6265SDimitry Andric for (const auto *Succ : successors(U)) 24681ad6265SDimitry Andric if (!DT.isReachableFromEntry(Succ)) 24781ad6265SDimitry Andric Unreachable.insert(Succ); 24881ad6265SDimitry Andric } 24981ad6265SDimitry Andric 25081ad6265SDimitry Andric const auto &LI = FAM.getResult<LoopAnalysis>(const_cast<Function &>(Caller)); 25181ad6265SDimitry Andric FPI.updateAggregateStats(Caller, LI); 252*06c3fb27SDimitry Andric } 253*06c3fb27SDimitry Andric 254*06c3fb27SDimitry Andric bool FunctionPropertiesUpdater::isUpdateValid(Function &F, 255*06c3fb27SDimitry Andric const FunctionPropertiesInfo &FPI, 256*06c3fb27SDimitry Andric FunctionAnalysisManager &FAM) { 257*06c3fb27SDimitry Andric DominatorTree DT(F); 258*06c3fb27SDimitry Andric LoopInfo LI(DT); 259*06c3fb27SDimitry Andric auto Fresh = FunctionPropertiesInfo::getFunctionPropertiesInfo(F, DT, LI); 260*06c3fb27SDimitry Andric return FPI == Fresh; 26181ad6265SDimitry Andric }