ModuleSummaryAnalysis.cpp 23 KB

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