1 //===-- CGProfile.cpp -----------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Transforms/Instrumentation/CGProfile.h" 11 12 #include "llvm/ADT/MapVector.h" 13 #include "llvm/Analysis/BlockFrequencyInfo.h" 14 #include "llvm/Analysis/TargetTransformInfo.h" 15 #include "llvm/IR/CallSite.h" 16 #include "llvm/IR/Constants.h" 17 #include "llvm/IR/Instructions.h" 18 #include "llvm/IR/MDBuilder.h" 19 #include "llvm/IR/PassManager.h" 20 #include "llvm/ProfileData/InstrProf.h" 21 #include "llvm/Transforms/Instrumentation.h" 22 23 #include <array> 24 25 using namespace llvm; 26 27 PreservedAnalyses CGProfilePass::run(Module &M, ModuleAnalysisManager &MAM) { 28 MapVector<std::pair<Function *, Function *>, uint64_t> Counts; 29 FunctionAnalysisManager &FAM = 30 MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 31 InstrProfSymtab Symtab; 32 auto UpdateCounts = [&](TargetTransformInfo &TTI, Function *F, 33 Function *CalledF, uint64_t NewCount) { 34 if (!CalledF || !TTI.isLoweredToCall(CalledF)) 35 return; 36 uint64_t &Count = Counts[std::make_pair(F, CalledF)]; 37 Count = SaturatingAdd(Count, NewCount); 38 }; 39 // Ignore error here. Indirect calls are ignored if this fails. 40 (void)(bool)Symtab.create(M); 41 for (auto &F : M) { 42 if (F.isDeclaration()) 43 continue; 44 auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(F); 45 if (BFI.getEntryFreq() == 0) 46 continue; 47 TargetTransformInfo &TTI = FAM.getResult<TargetIRAnalysis>(F); 48 for (auto &BB : F) { 49 Optional<uint64_t> BBCount = BFI.getBlockProfileCount(&BB); 50 if (!BBCount) 51 continue; 52 for (auto &I : BB) { 53 CallSite CS(&I); 54 if (!CS) 55 continue; 56 if (CS.isIndirectCall()) { 57 InstrProfValueData ValueData[8]; 58 uint32_t ActualNumValueData; 59 uint64_t TotalC; 60 if (!getValueProfDataFromInst(*CS.getInstruction(), 61 IPVK_IndirectCallTarget, 8, ValueData, 62 ActualNumValueData, TotalC)) 63 continue; 64 for (const auto &VD : 65 ArrayRef<InstrProfValueData>(ValueData, ActualNumValueData)) { 66 UpdateCounts(TTI, &F, Symtab.getFunction(VD.Value), VD.Count); 67 } 68 continue; 69 } 70 UpdateCounts(TTI, &F, CS.getCalledFunction(), *BBCount); 71 } 72 } 73 } 74 75 addModuleFlags(M, Counts); 76 77 return PreservedAnalyses::all(); 78 } 79 80 void CGProfilePass::addModuleFlags( 81 Module &M, 82 MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) const { 83 if (Counts.empty()) 84 return; 85 86 LLVMContext &Context = M.getContext(); 87 MDBuilder MDB(Context); 88 std::vector<Metadata *> Nodes; 89 90 for (auto E : Counts) { 91 Metadata *Vals[] = {ValueAsMetadata::get(E.first.first), 92 ValueAsMetadata::get(E.first.second), 93 MDB.createConstant(ConstantInt::get( 94 Type::getInt64Ty(Context), E.second))}; 95 Nodes.push_back(MDNode::get(Context, Vals)); 96 } 97 98 M.addModuleFlag(Module::Append, "CG Profile", MDNode::get(Context, Nodes)); 99 } 100