ModuleSummaryAnalysis.cpp 25 KB

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