LCOV - code coverage report
Current view: top level - lib/Analysis - ModuleSummaryAnalysis.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 272 282 96.5 %
Date: 2017-09-14 15:23:50 Functions: 19 20 95.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- ModuleSummaryAnalysis.cpp - Module summary index builder -----------===//
       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             : // This pass builds a ModuleSummaryIndex object for the module, to be written
      11             : // to bitcode or LLVM assembly.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "llvm/Analysis/ModuleSummaryAnalysis.h"
      16             : #include "llvm/ADT/ArrayRef.h"
      17             : #include "llvm/ADT/DenseSet.h"
      18             : #include "llvm/ADT/MapVector.h"
      19             : #include "llvm/ADT/STLExtras.h"
      20             : #include "llvm/ADT/SetVector.h"
      21             : #include "llvm/ADT/SmallPtrSet.h"
      22             : #include "llvm/ADT/SmallVector.h"
      23             : #include "llvm/ADT/StringRef.h"
      24             : #include "llvm/Analysis/BlockFrequencyInfo.h"
      25             : #include "llvm/Analysis/BranchProbabilityInfo.h"
      26             : #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
      27             : #include "llvm/Analysis/LoopInfo.h"
      28             : #include "llvm/Analysis/ProfileSummaryInfo.h"
      29             : #include "llvm/Analysis/TypeMetadataUtils.h"
      30             : #include "llvm/IR/Attributes.h"
      31             : #include "llvm/IR/BasicBlock.h"
      32             : #include "llvm/IR/CallSite.h"
      33             : #include "llvm/IR/Constant.h"
      34             : #include "llvm/IR/Constants.h"
      35             : #include "llvm/IR/Dominators.h"
      36             : #include "llvm/IR/Function.h"
      37             : #include "llvm/IR/GlobalAlias.h"
      38             : #include "llvm/IR/GlobalValue.h"
      39             : #include "llvm/IR/GlobalVariable.h"
      40             : #include "llvm/IR/Instructions.h"
      41             : #include "llvm/IR/IntrinsicInst.h"
      42             : #include "llvm/IR/Intrinsics.h"
      43             : #include "llvm/IR/Metadata.h"
      44             : #include "llvm/IR/Module.h"
      45             : #include "llvm/IR/ModuleSummaryIndex.h"
      46             : #include "llvm/IR/Use.h"
      47             : #include "llvm/IR/User.h"
      48             : #include "llvm/Object/ModuleSymbolTable.h"
      49             : #include "llvm/Object/SymbolicFile.h"
      50             : #include "llvm/Pass.h"
      51             : #include "llvm/Support/Casting.h"
      52             : #include <algorithm>
      53             : #include <cassert>
      54             : #include <cstdint>
      55             : #include <vector>
      56             : 
      57             : using namespace llvm;
      58             : 
      59             : #define DEBUG_TYPE "module-summary-analysis"
      60             : 
      61             : // Walk through the operands of a given User via worklist iteration and populate
      62             : // the set of GlobalValue references encountered. Invoked either on an
      63             : // Instruction or a GlobalVariable (which walks its initializer).
      64        1733 : static void findRefEdges(ModuleSummaryIndex &Index, const User *CurUser,
      65             :                          SetVector<ValueInfo> &RefEdges,
      66             :                          SmallPtrSet<const User *, 8> &Visited) {
      67        3466 :   SmallVector<const User *, 32> Worklist;
      68        1733 :   Worklist.push_back(CurUser);
      69             : 
      70        4209 :   while (!Worklist.empty()) {
      71        2476 :     const User *U = Worklist.pop_back_val();
      72             : 
      73        2476 :     if (!Visited.insert(U).second)
      74         301 :       continue;
      75             : 
      76        2175 :     ImmutableCallSite CS(U);
      77             : 
      78        3557 :     for (const auto &OI : U->operands()) {
      79        1382 :       const User *Operand = dyn_cast<User>(OI);
      80        1382 :       if (!Operand)
      81         796 :         continue;
      82        2450 :       if (isa<BlockAddress>(Operand))
      83           0 :         continue;
      84        2932 :       if (auto *GV = dyn_cast<GlobalValue>(Operand)) {
      85             :         // We have a reference to a global value. This should be added to
      86             :         // the reference set unless it is a callee. Callees are handled
      87             :         // specially by WriteFunction and are added to a separate list.
      88         815 :         if (!(CS && CS.isCallee(&OI)))
      89         151 :           RefEdges.insert(Index.getOrInsertValueInfo(GV));
      90         482 :         continue;
      91             :       }
      92         743 :       Worklist.push_back(Operand);
      93             :     }
      94             :   }
      95        1733 : }
      96             : 
      97          44 : static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount,
      98             :                                           ProfileSummaryInfo *PSI) {
      99          44 :   if (!PSI)
     100             :     return CalleeInfo::HotnessType::Unknown;
     101          44 :   if (PSI->isHotCount(ProfileCount))
     102             :     return CalleeInfo::HotnessType::Hot;
     103          24 :   if (PSI->isColdCount(ProfileCount))
     104             :     return CalleeInfo::HotnessType::Cold;
     105          10 :   return CalleeInfo::HotnessType::None;
     106             : }
     107             : 
     108         733 : static bool isNonRenamableLocal(const GlobalValue &GV) {
     109        1466 :   return GV.hasSection() && GV.hasLocalLinkage();
     110             : }
     111             : 
     112             : /// Determine whether this call has all constant integer arguments (excluding
     113             : /// "this") and summarize it to VCalls or ConstVCalls as appropriate.
     114           8 : static void addVCallToSet(DevirtCallSite Call, GlobalValue::GUID Guid,
     115             :                           SetVector<FunctionSummary::VFuncId> &VCalls,
     116             :                           SetVector<FunctionSummary::ConstVCall> &ConstVCalls) {
     117          12 :   std::vector<uint64_t> Args;
     118             :   // Start from the second argument to skip the "this" pointer.
     119          27 :   for (auto &Arg : make_range(Call.CS.arg_begin() + 1, Call.CS.arg_end())) {
     120           4 :     auto *CI = dyn_cast<ConstantInt>(Arg);
     121           4 :     if (!CI || CI->getBitWidth() > 64) {
     122           4 :       VCalls.insert({Guid, Call.Offset});
     123           4 :       return;
     124             :     }
     125           6 :     Args.push_back(CI->getZExtValue());
     126             :   }
     127          12 :   ConstVCalls.insert({{Guid, Call.Offset}, std::move(Args)});
     128             : }
     129             : 
     130             : /// If this intrinsic call requires that we add information to the function
     131             : /// summary, do so via the non-constant reference arguments.
     132          45 : static void addIntrinsicToSummary(
     133             :     const CallInst *CI, SetVector<GlobalValue::GUID> &TypeTests,
     134             :     SetVector<FunctionSummary::VFuncId> &TypeTestAssumeVCalls,
     135             :     SetVector<FunctionSummary::VFuncId> &TypeCheckedLoadVCalls,
     136             :     SetVector<FunctionSummary::ConstVCall> &TypeTestAssumeConstVCalls,
     137             :     SetVector<FunctionSummary::ConstVCall> &TypeCheckedLoadConstVCalls) {
     138          45 :   switch (CI->getCalledFunction()->getIntrinsicID()) {
     139          25 :   case Intrinsic::type_test: {
     140          50 :     auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1));
     141          49 :     auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
     142             :     if (!TypeId)
     143             :       break;
     144          48 :     GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
     145             : 
     146             :     // Produce a summary from type.test intrinsics. We only summarize type.test
     147             :     // intrinsics that are used other than by an llvm.assume intrinsic.
     148             :     // Intrinsics that are assumed are relevant only to the devirtualization
     149             :     // pass, not the type test lowering pass.
     150          70 :     bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) {
     151          31 :       auto *AssumeCI = dyn_cast<CallInst>(CIU.getUser());
     152             :       if (!AssumeCI)
     153             :         return true;
     154           9 :       Function *F = AssumeCI->getCalledFunction();
     155           9 :       return !F || F->getIntrinsicID() != Intrinsic::assume;
     156          48 :     });
     157          24 :     if (HasNonAssumeUses)
     158          13 :       TypeTests.insert(Guid);
     159             : 
     160          24 :     SmallVector<DevirtCallSite, 4> DevirtCalls;
     161          48 :     SmallVector<CallInst *, 4> Assumes;
     162          24 :     findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI);
     163          78 :     for (auto &Call : DevirtCalls)
     164           6 :       addVCallToSet(Call, Guid, TypeTestAssumeVCalls,
     165             :                     TypeTestAssumeConstVCalls);
     166             : 
     167             :     break;
     168             :   }
     169             : 
     170           5 :   case Intrinsic::type_checked_load: {
     171          10 :     auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(2));
     172           9 :     auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
     173             :     if (!TypeId)
     174             :       break;
     175           8 :     GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
     176             : 
     177           4 :     SmallVector<DevirtCallSite, 4> DevirtCalls;
     178           8 :     SmallVector<Instruction *, 4> LoadedPtrs;
     179           8 :     SmallVector<Instruction *, 4> Preds;
     180           4 :     bool HasNonCallUses = false;
     181           4 :     findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds,
     182             :                                                HasNonCallUses, CI);
     183             :     // Any non-call uses of the result of llvm.type.checked.load will
     184             :     // prevent us from optimizing away the llvm.type.test.
     185           4 :     if (HasNonCallUses)
     186           1 :       TypeTests.insert(Guid);
     187          14 :     for (auto &Call : DevirtCalls)
     188           2 :       addVCallToSet(Call, Guid, TypeCheckedLoadVCalls,
     189             :                     TypeCheckedLoadConstVCalls);
     190             : 
     191             :     break;
     192             :   }
     193             :   default:
     194             :     break;
     195             :   }
     196          45 : }
     197             : 
     198             : static void
     199         509 : computeFunctionSummary(ModuleSummaryIndex &Index, const Module &M,
     200             :                        const Function &F, BlockFrequencyInfo *BFI,
     201             :                        ProfileSummaryInfo *PSI, bool HasLocalsInUsedOrAsm,
     202             :                        DenseSet<GlobalValue::GUID> &CantBePromoted) {
     203             :   // Summary not currently supported for anonymous functions, they should
     204             :   // have been named.
     205             :   assert(F.hasName());
     206             : 
     207         509 :   unsigned NumInsts = 0;
     208             :   // Map from callee ValueId to profile count. Used to accumulate profile
     209             :   // counts for all static calls to a given callee.
     210        1018 :   MapVector<ValueInfo, CalleeInfo> CallGraphEdges;
     211        1018 :   SetVector<ValueInfo> RefEdges;
     212        1018 :   SetVector<GlobalValue::GUID> TypeTests;
     213        1018 :   SetVector<FunctionSummary::VFuncId> TypeTestAssumeVCalls,
     214        1018 :       TypeCheckedLoadVCalls;
     215        1018 :   SetVector<FunctionSummary::ConstVCall> TypeTestAssumeConstVCalls,
     216        1018 :       TypeCheckedLoadConstVCalls;
     217        1018 :   ICallPromotionAnalysis ICallAnalysis;
     218        1018 :   SmallPtrSet<const User *, 8> Visited;
     219             : 
     220             :   // Add personality function, prefix data and prologue data to function's ref
     221             :   // list.
     222         509 :   findRefEdges(Index, &F, RefEdges, Visited);
     223             : 
     224         509 :   bool HasInlineAsmMaybeReferencingInternal = false;
     225        2065 :   for (const BasicBlock &BB : F)
     226        2730 :     for (const Instruction &I : BB) {
     227           4 :       if (isa<DbgInfoIntrinsic>(I))
     228           4 :         continue;
     229        1112 :       ++NumInsts;
     230        1112 :       findRefEdges(Index, &I, RefEdges, Visited);
     231        1112 :       auto CS = ImmutableCallSite(&I);
     232        1112 :       if (!CS)
     233         759 :         continue;
     234             : 
     235         353 :       const auto *CI = dyn_cast<CallInst>(&I);
     236             :       // Since we don't know exactly which local values are referenced in inline
     237             :       // assembly, conservatively mark the function as possibly referencing
     238             :       // a local value from inline assembly to ensure we don't export a
     239             :       // reference (which would require renaming and promotion of the
     240             :       // referenced value).
     241         358 :       if (HasLocalsInUsedOrAsm && CI && CI->isInlineAsm())
     242             :         HasInlineAsmMaybeReferencingInternal = true;
     243             : 
     244         353 :       auto *CalledValue = CS.getCalledValue();
     245         353 :       auto *CalledFunction = CS.getCalledFunction();
     246             :       // Check if this is an alias to a function. If so, get the
     247             :       // called aliasee for the checks below.
     248           3 :       if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
     249             :         assert(!CalledFunction && "Expected null called function in callsite for alias");
     250           3 :         CalledFunction = dyn_cast<Function>(GA->getBaseObject());
     251             :       }
     252             :       // Check if this is a direct call to a known function or a known
     253             :       // intrinsic, or an indirect call with profile data.
     254         353 :       if (CalledFunction) {
     255         707 :         if (CI && CalledFunction->isIntrinsic()) {
     256          45 :           addIntrinsicToSummary(
     257             :               CI, TypeTests, TypeTestAssumeVCalls, TypeCheckedLoadVCalls,
     258             :               TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls);
     259          45 :           continue;
     260             :         }
     261             :         // We should have named any anonymous globals
     262             :         assert(CalledFunction->hasName());
     263         572 :         auto ScaledCount = PSI->getProfileCount(&I, BFI);
     264         326 :         auto Hotness = ScaledCount ? getHotness(ScaledCount.getValue(), PSI)
     265         572 :                                    : CalleeInfo::HotnessType::Unknown;
     266             : 
     267             :         // Use the original CalledValue, in case it was an alias. We want
     268             :         // to record the call edge to the alias in that case. Eventually
     269             :         // an alias summary will be created to associate the alias and
     270             :         // aliasee.
     271         572 :         CallGraphEdges[Index.getOrInsertValueInfo(
     272         572 :                            cast<GlobalValue>(CalledValue))]
     273         286 :             .updateHotness(Hotness);
     274             :       } else {
     275             :         // Skip inline assembly calls.
     276          44 :         if (CI && CI->isInlineAsm())
     277           5 :           continue;
     278             :         // Skip direct calls.
     279          41 :         if (!CS.getCalledValue() || isa<Constant>(CS.getCalledValue()))
     280           1 :           continue;
     281             : 
     282             :         uint32_t NumVals, NumCandidates;
     283             :         uint64_t TotalCount;
     284             :         auto CandidateProfileData =
     285             :             ICallAnalysis.getPromotionCandidatesForInstruction(
     286          19 :                 &I, NumVals, TotalCount, NumCandidates);
     287          65 :         for (auto &Candidate : CandidateProfileData)
     288          12 :           CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)]
     289           8 :               .updateHotness(getHotness(Candidate.Count, PSI));
     290             :       }
     291             :     }
     292             : 
     293             :   // Explicit add hot edges to enforce importing for designated GUIDs for
     294             :   // sample PGO, to enable the same inlines as the profiled optimized binary.
     295        3060 :   for (auto &I : F.getImportGUIDs())
     296           6 :     CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness(
     297             :         CalleeInfo::HotnessType::Critical);
     298             : 
     299         509 :   bool NonRenamableLocal = isNonRenamableLocal(F);
     300             :   bool NotEligibleForImport =
     301        1013 :       NonRenamableLocal || HasInlineAsmMaybeReferencingInternal ||
     302             :       // Inliner doesn't handle variadic functions.
     303             :       // FIXME: refactor this to use the same code that inliner is using.
     304        1013 :       F.isVarArg();
     305             :   GlobalValueSummary::GVFlags Flags(F.getLinkage(), NotEligibleForImport,
     306        1527 :                                     /* Live = */ false);
     307             :   FunctionSummary::FFlags FunFlags{
     308         509 :       F.hasFnAttribute(Attribute::ReadNone),
     309         509 :       F.hasFnAttribute(Attribute::ReadOnly),
     310         509 :       F.hasFnAttribute(Attribute::NoRecurse),
     311         509 :       F.returnDoesNotAlias(),
     312        2036 :   };
     313             :   auto FuncSummary = llvm::make_unique<FunctionSummary>(
     314        1018 :       Flags, NumInsts, FunFlags, RefEdges.takeVector(),
     315        2036 :       CallGraphEdges.takeVector(), TypeTests.takeVector(),
     316        2036 :       TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(),
     317        1018 :       TypeTestAssumeConstVCalls.takeVector(),
     318        2036 :       TypeCheckedLoadConstVCalls.takeVector());
     319         509 :   if (NonRenamableLocal)
     320           6 :     CantBePromoted.insert(F.getGUID());
     321        1527 :   Index.addGlobalValueSummary(F.getName(), std::move(FuncSummary));
     322         509 : }
     323             : 
     324             : static void
     325         112 : computeVariableSummary(ModuleSummaryIndex &Index, const GlobalVariable &V,
     326             :                        DenseSet<GlobalValue::GUID> &CantBePromoted) {
     327         224 :   SetVector<ValueInfo> RefEdges;
     328         224 :   SmallPtrSet<const User *, 8> Visited;
     329         112 :   findRefEdges(Index, &V, RefEdges, Visited);
     330         112 :   bool NonRenamableLocal = isNonRenamableLocal(V);
     331             :   GlobalValueSummary::GVFlags Flags(V.getLinkage(), NonRenamableLocal,
     332         336 :                                     /* Live = */ false);
     333             :   auto GVarSummary =
     334         336 :       llvm::make_unique<GlobalVarSummary>(Flags, RefEdges.takeVector());
     335         112 :   if (NonRenamableLocal)
     336           4 :     CantBePromoted.insert(V.getGUID());
     337         336 :   Index.addGlobalValueSummary(V.getName(), std::move(GVarSummary));
     338         112 : }
     339             : 
     340             : static void
     341         112 : computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A,
     342             :                     DenseSet<GlobalValue::GUID> &CantBePromoted) {
     343         112 :   bool NonRenamableLocal = isNonRenamableLocal(A);
     344             :   GlobalValueSummary::GVFlags Flags(A.getLinkage(), NonRenamableLocal,
     345         336 :                                     /* Live = */ false);
     346         224 :   auto AS = llvm::make_unique<AliasSummary>(Flags);
     347         112 :   auto *Aliasee = A.getBaseObject();
     348         112 :   auto *AliaseeSummary = Index.getGlobalValueSummary(*Aliasee);
     349             :   assert(AliaseeSummary && "Alias expects aliasee summary to be parsed");
     350         224 :   AS->setAliasee(AliaseeSummary);
     351         112 :   if (NonRenamableLocal)
     352           0 :     CantBePromoted.insert(A.getGUID());
     353         336 :   Index.addGlobalValueSummary(A.getName(), std::move(AS));
     354         112 : }
     355             : 
     356             : // Set LiveRoot flag on entries matching the given value name.
     357        1515 : static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
     358        3030 :   if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
     359          33 :     for (auto &Summary : VI.getSummaryList())
     360          22 :       Summary->setLive(true);
     361        1515 : }
     362             : 
     363         303 : ModuleSummaryIndex llvm::buildModuleSummaryIndex(
     364             :     const Module &M,
     365             :     std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
     366             :     ProfileSummaryInfo *PSI) {
     367             :   assert(PSI);
     368         303 :   ModuleSummaryIndex Index;
     369             : 
     370             :   // Identify the local values in the llvm.used and llvm.compiler.used sets,
     371             :   // which should not be exported as they would then require renaming and
     372             :   // promotion, but we may have opaque uses e.g. in inline asm. We collect them
     373             :   // here because we use this information to mark functions containing inline
     374             :   // assembly calls as not importable.
     375         606 :   SmallPtrSet<GlobalValue *, 8> LocalsUsed;
     376         606 :   SmallPtrSet<GlobalValue *, 8> Used;
     377             :   // First collect those in the llvm.used set.
     378         303 :   collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
     379             :   // Next collect those in the llvm.compiler.used set.
     380         303 :   collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
     381         606 :   DenseSet<GlobalValue::GUID> CantBePromoted;
     382         307 :   for (auto *V : Used) {
     383             :     if (V->hasLocalLinkage()) {
     384           4 :       LocalsUsed.insert(V);
     385           8 :       CantBePromoted.insert(V->getGUID());
     386             :     }
     387             :   }
     388             : 
     389         303 :   bool HasLocalInlineAsmSymbol = false;
     390         303 :   if (!M.getModuleInlineAsm().empty()) {
     391             :     // Collect the local values defined by module level asm, and set up
     392             :     // summaries for these symbols so that they can be marked as NoRename,
     393             :     // to prevent export of any use of them in regular IR that would require
     394             :     // renaming within the module level asm. Note we don't need to create a
     395             :     // summary for weak or global defs, as they don't need to be flagged as
     396             :     // NoRename, and defs in module level asm can't be imported anyway.
     397             :     // Also, any values used but not defined within module level asm should
     398             :     // be listed on the llvm.used or llvm.compiler.used global and marked as
     399             :     // referenced from there.
     400          10 :     ModuleSymbolTable::CollectAsmSymbols(
     401          14 :         M, [&](StringRef Name, object::BasicSymbolRef::Flags Flags) {
     402             :           // Symbols not marked as Weak or Global are local definitions.
     403          14 :           if (Flags & (object::BasicSymbolRef::SF_Weak |
     404             :                        object::BasicSymbolRef::SF_Global))
     405          13 :             return;
     406           6 :           HasLocalInlineAsmSymbol = true;
     407           6 :           GlobalValue *GV = M.getNamedValue(Name);
     408           6 :           if (!GV)
     409             :             return;
     410             :           assert(GV->isDeclaration() && "Def in module asm already has definition");
     411             :           GlobalValueSummary::GVFlags GVFlags(GlobalValue::InternalLinkage,
     412             :                                               /* NotEligibleToImport = */ true,
     413           1 :                                               /* Live = */ true);
     414           3 :           CantBePromoted.insert(GlobalValue::getGUID(Name));
     415             :           // Create the appropriate summary type.
     416           1 :           if (Function *F = dyn_cast<Function>(GV)) {
     417             :             std::unique_ptr<FunctionSummary> Summary =
     418             :                 llvm::make_unique<FunctionSummary>(
     419             :                     GVFlags, 0,
     420           5 :                     FunctionSummary::FFlags{
     421           1 :                         F->hasFnAttribute(Attribute::ReadNone),
     422           1 :                         F->hasFnAttribute(Attribute::ReadOnly),
     423           1 :                         F->hasFnAttribute(Attribute::NoRecurse),
     424           1 :                         F->returnDoesNotAlias()},
     425           1 :                     ArrayRef<ValueInfo>{}, ArrayRef<FunctionSummary::EdgeTy>{},
     426           1 :                     ArrayRef<GlobalValue::GUID>{},
     427           1 :                     ArrayRef<FunctionSummary::VFuncId>{},
     428           1 :                     ArrayRef<FunctionSummary::VFuncId>{},
     429           1 :                     ArrayRef<FunctionSummary::ConstVCall>{},
     430           4 :                     ArrayRef<FunctionSummary::ConstVCall>{});
     431           3 :             Index.addGlobalValueSummary(Name, std::move(Summary));
     432             :           } else {
     433             :             std::unique_ptr<GlobalVarSummary> Summary =
     434             :                 llvm::make_unique<GlobalVarSummary>(GVFlags,
     435           0 :                                                     ArrayRef<ValueInfo>{});
     436           0 :             Index.addGlobalValueSummary(Name, std::move(Summary));
     437             :           }
     438             :         });
     439             :   }
     440             : 
     441             :   // Compute summaries for all functions defined in module, and save in the
     442             :   // index.
     443        1666 :   for (auto &F : M) {
     444         757 :     if (F.isDeclaration())
     445         248 :       continue;
     446             : 
     447         509 :     BlockFrequencyInfo *BFI = nullptr;
     448        1018 :     std::unique_ptr<BlockFrequencyInfo> BFIPtr;
     449         509 :     if (GetBFICallback)
     450         472 :       BFI = GetBFICallback(F);
     451          74 :     else if (F.getEntryCount().hasValue()) {
     452           0 :       LoopInfo LI{DominatorTree(const_cast<Function &>(F))};
     453           0 :       BranchProbabilityInfo BPI{F, LI};
     454           0 :       BFIPtr = llvm::make_unique<BlockFrequencyInfo>(F, BPI, LI);
     455           0 :       BFI = BFIPtr.get();
     456             :     }
     457             : 
     458         509 :     computeFunctionSummary(Index, M, F, BFI, PSI,
     459         509 :                            !LocalsUsed.empty() || HasLocalInlineAsmSymbol,
     460             :                            CantBePromoted);
     461             :   }
     462             : 
     463             :   // Compute summaries for all variables defined in module, and save in the
     464             :   // index.
     465         451 :   for (const GlobalVariable &G : M.globals()) {
     466         148 :     if (G.isDeclaration())
     467          36 :       continue;
     468         112 :     computeVariableSummary(Index, G, CantBePromoted);
     469             :   }
     470             : 
     471             :   // Compute summaries for all aliases defined in module, and save in the
     472             :   // index.
     473         415 :   for (const GlobalAlias &A : M.aliases())
     474         112 :     computeAliasSummary(Index, A, CantBePromoted);
     475             : 
     476         307 :   for (auto *V : LocalsUsed) {
     477           4 :     auto *Summary = Index.getGlobalValueSummary(*V);
     478             :     assert(Summary && "Missing summary for global value");
     479           4 :     Summary->setNotEligibleToImport();
     480             :   }
     481             : 
     482             :   // The linker doesn't know about these LLVM produced values, so we need
     483             :   // to flag them as live in the index to ensure index-based dead value
     484             :   // analysis treats them as live roots of the analysis.
     485         303 :   setLiveRoot(Index, "llvm.used");
     486         303 :   setLiveRoot(Index, "llvm.compiler.used");
     487         303 :   setLiveRoot(Index, "llvm.global_ctors");
     488         303 :   setLiveRoot(Index, "llvm.global_dtors");
     489         303 :   setLiveRoot(Index, "llvm.global.annotations");
     490             : 
     491         303 :   bool IsThinLTO = true;
     492          19 :   if (auto *MD =
     493         606 :           mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
     494          19 :     IsThinLTO = MD->getZExtValue();
     495             : 
     496        1943 :   for (auto &GlobalList : Index) {
     497             :     // Ignore entries for references that are undefined in the current module.
     498        2068 :     if (GlobalList.second.SummaryList.empty())
     499         300 :       continue;
     500             : 
     501             :     assert(GlobalList.second.SummaryList.size() == 1 &&
     502             :            "Expected module's index to have one summary per GUID");
     503         734 :     auto &Summary = GlobalList.second.SummaryList[0];
     504         761 :     if (!IsThinLTO) {
     505          54 :       Summary->setNotEligibleToImport();
     506          27 :       continue;
     507             :     }
     508             : 
     509             :     bool AllRefsCanBeExternallyReferenced =
     510        2121 :         llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) {
     511         396 :           return !CantBePromoted.count(VI.getGUID());
     512         707 :         });
     513         717 :     if (!AllRefsCanBeExternallyReferenced) {
     514          20 :       Summary->setNotEligibleToImport();
     515          10 :       continue;
     516             :     }
     517             : 
     518        1196 :     if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) {
     519         499 :       bool AllCallsCanBeExternallyReferenced = llvm::all_of(
     520         499 :           FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) {
     521         801 :             return !CantBePromoted.count(Edge.first.getGUID());
     522         499 :           });
     523         499 :       if (!AllCallsCanBeExternallyReferenced)
     524           1 :         Summary->setNotEligibleToImport();
     525             :     }
     526             :   }
     527             : 
     528         303 :   return Index;
     529             : }
     530             : 
     531             : AnalysisKey ModuleSummaryIndexAnalysis::Key;
     532             : 
     533             : ModuleSummaryIndex
     534          11 : ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
     535          11 :   ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
     536          22 :   auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
     537             :   return buildModuleSummaryIndex(
     538             :       M,
     539          12 :       [&FAM](const Function &F) {
     540             :         return &FAM.getResult<BlockFrequencyAnalysis>(
     541             :             *const_cast<Function *>(&F));
     542          12 :       },
     543          33 :       &PSI);
     544             : }
     545             : 
     546             : char ModuleSummaryIndexWrapperPass::ID = 0;
     547             : 
     548        9253 : INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
     549             :                       "Module Summary Analysis", false, true)
     550        9253 : INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
     551        9253 : INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
     552       49178 : INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
     553             :                     "Module Summary Analysis", false, true)
     554             : 
     555           0 : ModulePass *llvm::createModuleSummaryIndexWrapperPass() {
     556           0 :   return new ModuleSummaryIndexWrapperPass();
     557             : }
     558             : 
     559         258 : ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass()
     560         774 :     : ModulePass(ID) {
     561         258 :   initializeModuleSummaryIndexWrapperPassPass(*PassRegistry::getPassRegistry());
     562         258 : }
     563             : 
     564         258 : bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) {
     565         516 :   auto &PSI = *getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
     566        1032 :   Index = buildModuleSummaryIndex(
     567             :       M,
     568         460 :       [this](const Function &F) {
     569             :         return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>(
     570         920 :                          *const_cast<Function *>(&F))
     571             :                      .getBFI());
     572         460 :       },
     573         258 :       &PSI);
     574         258 :   return false;
     575             : }
     576             : 
     577         258 : bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) {
     578         516 :   Index.reset();
     579         258 :   return false;
     580             : }
     581             : 
     582         258 : void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
     583         516 :   AU.setPreservesAll();
     584         258 :   AU.addRequired<BlockFrequencyInfoWrapperPass>();
     585         258 :   AU.addRequired<ProfileSummaryInfoWrapperPass>();
     586         258 : }

Generated by: LCOV version 1.13