ModuleSummaryAnalysis.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. uint64_t BBFreq = BFI->getBlockFreq(&BB).getFrequency();
  258. uint64_t EntryFreq = BFI->getEntryFreq();
  259. ValueInfo.updateRelBlockFreq(BBFreq, EntryFreq);
  260. }
  261. } else {
  262. // Skip inline assembly calls.
  263. if (CI && CI->isInlineAsm())
  264. continue;
  265. // Skip direct calls.
  266. if (!CalledValue || isa<Constant>(CalledValue))
  267. continue;
  268. uint32_t NumVals, NumCandidates;
  269. uint64_t TotalCount;
  270. auto CandidateProfileData =
  271. ICallAnalysis.getPromotionCandidatesForInstruction(
  272. &I, NumVals, TotalCount, NumCandidates);
  273. for (auto &Candidate : CandidateProfileData)
  274. CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)]
  275. .updateHotness(getHotness(Candidate.Count, PSI));
  276. }
  277. }
  278. // Explicit add hot edges to enforce importing for designated GUIDs for
  279. // sample PGO, to enable the same inlines as the profiled optimized binary.
  280. for (auto &I : F.getImportGUIDs())
  281. CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness(
  282. CalleeInfo::HotnessType::Critical);
  283. bool NonRenamableLocal = isNonRenamableLocal(F);
  284. bool NotEligibleForImport =
  285. NonRenamableLocal || HasInlineAsmMaybeReferencingInternal ||
  286. // Inliner doesn't handle variadic functions.
  287. // FIXME: refactor this to use the same code that inliner is using.
  288. F.isVarArg() ||
  289. // Don't try to import functions with noinline attribute.
  290. F.getAttributes().hasFnAttribute(Attribute::NoInline);
  291. GlobalValueSummary::GVFlags Flags(F.getLinkage(), NotEligibleForImport,
  292. /* Live = */ false, F.isDSOLocal());
  293. FunctionSummary::FFlags FunFlags{
  294. F.hasFnAttribute(Attribute::ReadNone),
  295. F.hasFnAttribute(Attribute::ReadOnly),
  296. F.hasFnAttribute(Attribute::NoRecurse),
  297. F.returnDoesNotAlias(),
  298. };
  299. auto FuncSummary = llvm::make_unique<FunctionSummary>(
  300. Flags, NumInsts, FunFlags, RefEdges.takeVector(),
  301. CallGraphEdges.takeVector(), TypeTests.takeVector(),
  302. TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(),
  303. TypeTestAssumeConstVCalls.takeVector(),
  304. TypeCheckedLoadConstVCalls.takeVector());
  305. if (NonRenamableLocal)
  306. CantBePromoted.insert(F.getGUID());
  307. Index.addGlobalValueSummary(F.getName(), std::move(FuncSummary));
  308. }
  309. static void
  310. computeVariableSummary(ModuleSummaryIndex &Index, const GlobalVariable &V,
  311. DenseSet<GlobalValue::GUID> &CantBePromoted) {
  312. SetVector<ValueInfo> RefEdges;
  313. SmallPtrSet<const User *, 8> Visited;
  314. findRefEdges(Index, &V, RefEdges, Visited);
  315. bool NonRenamableLocal = isNonRenamableLocal(V);
  316. GlobalValueSummary::GVFlags Flags(V.getLinkage(), NonRenamableLocal,
  317. /* Live = */ false, V.isDSOLocal());
  318. auto GVarSummary =
  319. llvm::make_unique<GlobalVarSummary>(Flags, RefEdges.takeVector());
  320. if (NonRenamableLocal)
  321. CantBePromoted.insert(V.getGUID());
  322. Index.addGlobalValueSummary(V.getName(), std::move(GVarSummary));
  323. }
  324. static void
  325. computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A,
  326. DenseSet<GlobalValue::GUID> &CantBePromoted) {
  327. bool NonRenamableLocal = isNonRenamableLocal(A);
  328. GlobalValueSummary::GVFlags Flags(A.getLinkage(), NonRenamableLocal,
  329. /* Live = */ false, A.isDSOLocal());
  330. auto AS = llvm::make_unique<AliasSummary>(Flags);
  331. auto *Aliasee = A.getBaseObject();
  332. auto *AliaseeSummary = Index.getGlobalValueSummary(*Aliasee);
  333. assert(AliaseeSummary && "Alias expects aliasee summary to be parsed");
  334. AS->setAliasee(AliaseeSummary);
  335. if (NonRenamableLocal)
  336. CantBePromoted.insert(A.getGUID());
  337. Index.addGlobalValueSummary(A.getName(), std::move(AS));
  338. }
  339. // Set LiveRoot flag on entries matching the given value name.
  340. static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
  341. if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
  342. for (auto &Summary : VI.getSummaryList())
  343. Summary->setLive(true);
  344. }
  345. ModuleSummaryIndex llvm::buildModuleSummaryIndex(
  346. const Module &M,
  347. std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
  348. ProfileSummaryInfo *PSI) {
  349. assert(PSI);
  350. ModuleSummaryIndex Index(/*IsPerformingAnalysis=*/true);
  351. // Identify the local values in the llvm.used and llvm.compiler.used sets,
  352. // which should not be exported as they would then require renaming and
  353. // promotion, but we may have opaque uses e.g. in inline asm. We collect them
  354. // here because we use this information to mark functions containing inline
  355. // assembly calls as not importable.
  356. SmallPtrSet<GlobalValue *, 8> LocalsUsed;
  357. SmallPtrSet<GlobalValue *, 8> Used;
  358. // First collect those in the llvm.used set.
  359. collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
  360. // Next collect those in the llvm.compiler.used set.
  361. collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
  362. DenseSet<GlobalValue::GUID> CantBePromoted;
  363. for (auto *V : Used) {
  364. if (V->hasLocalLinkage()) {
  365. LocalsUsed.insert(V);
  366. CantBePromoted.insert(V->getGUID());
  367. }
  368. }
  369. bool HasLocalInlineAsmSymbol = false;
  370. if (!M.getModuleInlineAsm().empty()) {
  371. // Collect the local values defined by module level asm, and set up
  372. // summaries for these symbols so that they can be marked as NoRename,
  373. // to prevent export of any use of them in regular IR that would require
  374. // renaming within the module level asm. Note we don't need to create a
  375. // summary for weak or global defs, as they don't need to be flagged as
  376. // NoRename, and defs in module level asm can't be imported anyway.
  377. // Also, any values used but not defined within module level asm should
  378. // be listed on the llvm.used or llvm.compiler.used global and marked as
  379. // referenced from there.
  380. ModuleSymbolTable::CollectAsmSymbols(
  381. M, [&](StringRef Name, object::BasicSymbolRef::Flags Flags) {
  382. // Symbols not marked as Weak or Global are local definitions.
  383. if (Flags & (object::BasicSymbolRef::SF_Weak |
  384. object::BasicSymbolRef::SF_Global))
  385. return;
  386. HasLocalInlineAsmSymbol = true;
  387. GlobalValue *GV = M.getNamedValue(Name);
  388. if (!GV)
  389. return;
  390. assert(GV->isDeclaration() && "Def in module asm already has definition");
  391. GlobalValueSummary::GVFlags GVFlags(GlobalValue::InternalLinkage,
  392. /* NotEligibleToImport = */ true,
  393. /* Live = */ true,
  394. /* Local */ GV->isDSOLocal());
  395. CantBePromoted.insert(GlobalValue::getGUID(Name));
  396. // Create the appropriate summary type.
  397. if (Function *F = dyn_cast<Function>(GV)) {
  398. std::unique_ptr<FunctionSummary> Summary =
  399. llvm::make_unique<FunctionSummary>(
  400. GVFlags, 0,
  401. FunctionSummary::FFlags{
  402. F->hasFnAttribute(Attribute::ReadNone),
  403. F->hasFnAttribute(Attribute::ReadOnly),
  404. F->hasFnAttribute(Attribute::NoRecurse),
  405. F->returnDoesNotAlias()},
  406. ArrayRef<ValueInfo>{}, ArrayRef<FunctionSummary::EdgeTy>{},
  407. ArrayRef<GlobalValue::GUID>{},
  408. ArrayRef<FunctionSummary::VFuncId>{},
  409. ArrayRef<FunctionSummary::VFuncId>{},
  410. ArrayRef<FunctionSummary::ConstVCall>{},
  411. ArrayRef<FunctionSummary::ConstVCall>{});
  412. Index.addGlobalValueSummary(Name, std::move(Summary));
  413. } else {
  414. std::unique_ptr<GlobalVarSummary> Summary =
  415. llvm::make_unique<GlobalVarSummary>(GVFlags,
  416. ArrayRef<ValueInfo>{});
  417. Index.addGlobalValueSummary(Name, std::move(Summary));
  418. }
  419. });
  420. }
  421. // Compute summaries for all functions defined in module, and save in the
  422. // index.
  423. for (auto &F : M) {
  424. if (F.isDeclaration())
  425. continue;
  426. BlockFrequencyInfo *BFI = nullptr;
  427. std::unique_ptr<BlockFrequencyInfo> BFIPtr;
  428. if (GetBFICallback)
  429. BFI = GetBFICallback(F);
  430. else if (F.hasProfileData()) {
  431. LoopInfo LI{DominatorTree(const_cast<Function &>(F))};
  432. BranchProbabilityInfo BPI{F, LI};
  433. BFIPtr = llvm::make_unique<BlockFrequencyInfo>(F, BPI, LI);
  434. BFI = BFIPtr.get();
  435. }
  436. computeFunctionSummary(Index, M, F, BFI, PSI,
  437. !LocalsUsed.empty() || HasLocalInlineAsmSymbol,
  438. CantBePromoted);
  439. }
  440. // Compute summaries for all variables defined in module, and save in the
  441. // index.
  442. for (const GlobalVariable &G : M.globals()) {
  443. if (G.isDeclaration())
  444. continue;
  445. computeVariableSummary(Index, G, CantBePromoted);
  446. }
  447. // Compute summaries for all aliases defined in module, and save in the
  448. // index.
  449. for (const GlobalAlias &A : M.aliases())
  450. computeAliasSummary(Index, A, CantBePromoted);
  451. for (auto *V : LocalsUsed) {
  452. auto *Summary = Index.getGlobalValueSummary(*V);
  453. assert(Summary && "Missing summary for global value");
  454. Summary->setNotEligibleToImport();
  455. }
  456. // The linker doesn't know about these LLVM produced values, so we need
  457. // to flag them as live in the index to ensure index-based dead value
  458. // analysis treats them as live roots of the analysis.
  459. setLiveRoot(Index, "llvm.used");
  460. setLiveRoot(Index, "llvm.compiler.used");
  461. setLiveRoot(Index, "llvm.global_ctors");
  462. setLiveRoot(Index, "llvm.global_dtors");
  463. setLiveRoot(Index, "llvm.global.annotations");
  464. bool IsThinLTO = true;
  465. if (auto *MD =
  466. mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
  467. IsThinLTO = MD->getZExtValue();
  468. for (auto &GlobalList : Index) {
  469. // Ignore entries for references that are undefined in the current module.
  470. if (GlobalList.second.SummaryList.empty())
  471. continue;
  472. assert(GlobalList.second.SummaryList.size() == 1 &&
  473. "Expected module's index to have one summary per GUID");
  474. auto &Summary = GlobalList.second.SummaryList[0];
  475. if (!IsThinLTO) {
  476. Summary->setNotEligibleToImport();
  477. continue;
  478. }
  479. bool AllRefsCanBeExternallyReferenced =
  480. llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) {
  481. return !CantBePromoted.count(VI.getGUID());
  482. });
  483. if (!AllRefsCanBeExternallyReferenced) {
  484. Summary->setNotEligibleToImport();
  485. continue;
  486. }
  487. if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) {
  488. bool AllCallsCanBeExternallyReferenced = llvm::all_of(
  489. FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) {
  490. return !CantBePromoted.count(Edge.first.getGUID());
  491. });
  492. if (!AllCallsCanBeExternallyReferenced)
  493. Summary->setNotEligibleToImport();
  494. }
  495. }
  496. return Index;
  497. }
  498. AnalysisKey ModuleSummaryIndexAnalysis::Key;
  499. ModuleSummaryIndex
  500. ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
  501. ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
  502. auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  503. return buildModuleSummaryIndex(
  504. M,
  505. [&FAM](const Function &F) {
  506. return &FAM.getResult<BlockFrequencyAnalysis>(
  507. *const_cast<Function *>(&F));
  508. },
  509. &PSI);
  510. }
  511. char ModuleSummaryIndexWrapperPass::ID = 0;
  512. INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
  513. "Module Summary Analysis", false, true)
  514. INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
  515. INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
  516. INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
  517. "Module Summary Analysis", false, true)
  518. ModulePass *llvm::createModuleSummaryIndexWrapperPass() {
  519. return new ModuleSummaryIndexWrapperPass();
  520. }
  521. ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass()
  522. : ModulePass(ID) {
  523. initializeModuleSummaryIndexWrapperPassPass(*PassRegistry::getPassRegistry());
  524. }
  525. bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) {
  526. auto &PSI = *getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
  527. Index = buildModuleSummaryIndex(
  528. M,
  529. [this](const Function &F) {
  530. return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>(
  531. *const_cast<Function *>(&F))
  532. .getBFI());
  533. },
  534. &PSI);
  535. return false;
  536. }
  537. bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) {
  538. Index.reset();
  539. return false;
  540. }
  541. void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  542. AU.setPreservesAll();
  543. AU.addRequired<BlockFrequencyInfoWrapperPass>();
  544. AU.addRequired<ProfileSummaryInfoWrapperPass>();
  545. }