ModuleSummaryAnalysis.cpp 24 KB

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