ModuleSummaryAnalysis.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. //===- ModuleSummaryAnalysis.cpp - Module summary index builder -----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This pass builds a ModuleSummaryIndex object for the module, to be written
  10. // to bitcode or LLVM assembly.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/ModuleSummaryAnalysis.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/DenseSet.h"
  16. #include "llvm/ADT/MapVector.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/ADT/SetVector.h"
  19. #include "llvm/ADT/SmallPtrSet.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/Analysis/BlockFrequencyInfo.h"
  23. #include "llvm/Analysis/BranchProbabilityInfo.h"
  24. #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
  25. #include "llvm/Analysis/LoopInfo.h"
  26. #include "llvm/Analysis/MemoryProfileInfo.h"
  27. #include "llvm/Analysis/ProfileSummaryInfo.h"
  28. #include "llvm/Analysis/StackSafetyAnalysis.h"
  29. #include "llvm/Analysis/TypeMetadataUtils.h"
  30. #include "llvm/IR/Attributes.h"
  31. #include "llvm/IR/BasicBlock.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/Metadata.h"
  42. #include "llvm/IR/Module.h"
  43. #include "llvm/IR/ModuleSummaryIndex.h"
  44. #include "llvm/IR/Use.h"
  45. #include "llvm/IR/User.h"
  46. #include "llvm/InitializePasses.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 "llvm/Support/FileSystem.h"
  53. #include <algorithm>
  54. #include <cassert>
  55. #include <cstdint>
  56. #include <vector>
  57. using namespace llvm;
  58. using namespace llvm::memprof;
  59. #define DEBUG_TYPE "module-summary-analysis"
  60. // Option to force edges cold which will block importing when the
  61. // -import-cold-multiplier is set to 0. Useful for debugging.
  62. namespace llvm {
  63. FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold =
  64. FunctionSummary::FSHT_None;
  65. } // namespace llvm
  66. static cl::opt<FunctionSummary::ForceSummaryHotnessType, true> FSEC(
  67. "force-summary-edges-cold", cl::Hidden, cl::location(ForceSummaryEdgesCold),
  68. cl::desc("Force all edges in the function summary to cold"),
  69. cl::values(clEnumValN(FunctionSummary::FSHT_None, "none", "None."),
  70. clEnumValN(FunctionSummary::FSHT_AllNonCritical,
  71. "all-non-critical", "All non-critical edges."),
  72. clEnumValN(FunctionSummary::FSHT_All, "all", "All edges.")));
  73. static cl::opt<std::string> ModuleSummaryDotFile(
  74. "module-summary-dot-file", cl::Hidden, cl::value_desc("filename"),
  75. cl::desc("File to emit dot graph of new summary into"));
  76. // Walk through the operands of a given User via worklist iteration and populate
  77. // the set of GlobalValue references encountered. Invoked either on an
  78. // Instruction or a GlobalVariable (which walks its initializer).
  79. // Return true if any of the operands contains blockaddress. This is important
  80. // to know when computing summary for global var, because if global variable
  81. // references basic block address we can't import it separately from function
  82. // containing that basic block. For simplicity we currently don't import such
  83. // global vars at all. When importing function we aren't interested if any
  84. // instruction in it takes an address of any basic block, because instruction
  85. // can only take an address of basic block located in the same function.
  86. static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser,
  87. SetVector<ValueInfo> &RefEdges,
  88. SmallPtrSet<const User *, 8> &Visited) {
  89. bool HasBlockAddress = false;
  90. SmallVector<const User *, 32> Worklist;
  91. if (Visited.insert(CurUser).second)
  92. Worklist.push_back(CurUser);
  93. while (!Worklist.empty()) {
  94. const User *U = Worklist.pop_back_val();
  95. const auto *CB = dyn_cast<CallBase>(U);
  96. for (const auto &OI : U->operands()) {
  97. const User *Operand = dyn_cast<User>(OI);
  98. if (!Operand)
  99. continue;
  100. if (isa<BlockAddress>(Operand)) {
  101. HasBlockAddress = true;
  102. continue;
  103. }
  104. if (auto *GV = dyn_cast<GlobalValue>(Operand)) {
  105. // We have a reference to a global value. This should be added to
  106. // the reference set unless it is a callee. Callees are handled
  107. // specially by WriteFunction and are added to a separate list.
  108. if (!(CB && CB->isCallee(&OI)))
  109. RefEdges.insert(Index.getOrInsertValueInfo(GV));
  110. continue;
  111. }
  112. if (Visited.insert(Operand).second)
  113. Worklist.push_back(Operand);
  114. }
  115. }
  116. return HasBlockAddress;
  117. }
  118. static CalleeInfo::HotnessType getHotness(uint64_t ProfileCount,
  119. ProfileSummaryInfo *PSI) {
  120. if (!PSI)
  121. return CalleeInfo::HotnessType::Unknown;
  122. if (PSI->isHotCount(ProfileCount))
  123. return CalleeInfo::HotnessType::Hot;
  124. if (PSI->isColdCount(ProfileCount))
  125. return CalleeInfo::HotnessType::Cold;
  126. return CalleeInfo::HotnessType::None;
  127. }
  128. static bool isNonRenamableLocal(const GlobalValue &GV) {
  129. return GV.hasSection() && GV.hasLocalLinkage();
  130. }
  131. /// Determine whether this call has all constant integer arguments (excluding
  132. /// "this") and summarize it to VCalls or ConstVCalls as appropriate.
  133. static void addVCallToSet(DevirtCallSite Call, GlobalValue::GUID Guid,
  134. SetVector<FunctionSummary::VFuncId> &VCalls,
  135. SetVector<FunctionSummary::ConstVCall> &ConstVCalls) {
  136. std::vector<uint64_t> Args;
  137. // Start from the second argument to skip the "this" pointer.
  138. for (auto &Arg : drop_begin(Call.CB.args())) {
  139. auto *CI = dyn_cast<ConstantInt>(Arg);
  140. if (!CI || CI->getBitWidth() > 64) {
  141. VCalls.insert({Guid, Call.Offset});
  142. return;
  143. }
  144. Args.push_back(CI->getZExtValue());
  145. }
  146. ConstVCalls.insert({{Guid, Call.Offset}, std::move(Args)});
  147. }
  148. /// If this intrinsic call requires that we add information to the function
  149. /// summary, do so via the non-constant reference arguments.
  150. static void addIntrinsicToSummary(
  151. const CallInst *CI, SetVector<GlobalValue::GUID> &TypeTests,
  152. SetVector<FunctionSummary::VFuncId> &TypeTestAssumeVCalls,
  153. SetVector<FunctionSummary::VFuncId> &TypeCheckedLoadVCalls,
  154. SetVector<FunctionSummary::ConstVCall> &TypeTestAssumeConstVCalls,
  155. SetVector<FunctionSummary::ConstVCall> &TypeCheckedLoadConstVCalls,
  156. DominatorTree &DT) {
  157. switch (CI->getCalledFunction()->getIntrinsicID()) {
  158. case Intrinsic::type_test:
  159. case Intrinsic::public_type_test: {
  160. auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(1));
  161. auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
  162. if (!TypeId)
  163. break;
  164. GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
  165. // Produce a summary from type.test intrinsics. We only summarize type.test
  166. // intrinsics that are used other than by an llvm.assume intrinsic.
  167. // Intrinsics that are assumed are relevant only to the devirtualization
  168. // pass, not the type test lowering pass.
  169. bool HasNonAssumeUses = llvm::any_of(CI->uses(), [](const Use &CIU) {
  170. return !isa<AssumeInst>(CIU.getUser());
  171. });
  172. if (HasNonAssumeUses)
  173. TypeTests.insert(Guid);
  174. SmallVector<DevirtCallSite, 4> DevirtCalls;
  175. SmallVector<CallInst *, 4> Assumes;
  176. findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI, DT);
  177. for (auto &Call : DevirtCalls)
  178. addVCallToSet(Call, Guid, TypeTestAssumeVCalls,
  179. TypeTestAssumeConstVCalls);
  180. break;
  181. }
  182. case Intrinsic::type_checked_load: {
  183. auto *TypeMDVal = cast<MetadataAsValue>(CI->getArgOperand(2));
  184. auto *TypeId = dyn_cast<MDString>(TypeMDVal->getMetadata());
  185. if (!TypeId)
  186. break;
  187. GlobalValue::GUID Guid = GlobalValue::getGUID(TypeId->getString());
  188. SmallVector<DevirtCallSite, 4> DevirtCalls;
  189. SmallVector<Instruction *, 4> LoadedPtrs;
  190. SmallVector<Instruction *, 4> Preds;
  191. bool HasNonCallUses = false;
  192. findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds,
  193. HasNonCallUses, CI, DT);
  194. // Any non-call uses of the result of llvm.type.checked.load will
  195. // prevent us from optimizing away the llvm.type.test.
  196. if (HasNonCallUses)
  197. TypeTests.insert(Guid);
  198. for (auto &Call : DevirtCalls)
  199. addVCallToSet(Call, Guid, TypeCheckedLoadVCalls,
  200. TypeCheckedLoadConstVCalls);
  201. break;
  202. }
  203. default:
  204. break;
  205. }
  206. }
  207. static bool isNonVolatileLoad(const Instruction *I) {
  208. if (const auto *LI = dyn_cast<LoadInst>(I))
  209. return !LI->isVolatile();
  210. return false;
  211. }
  212. static bool isNonVolatileStore(const Instruction *I) {
  213. if (const auto *SI = dyn_cast<StoreInst>(I))
  214. return !SI->isVolatile();
  215. return false;
  216. }
  217. // Returns true if the function definition must be unreachable.
  218. //
  219. // Note if this helper function returns true, `F` is guaranteed
  220. // to be unreachable; if it returns false, `F` might still
  221. // be unreachable but not covered by this helper function.
  222. static bool mustBeUnreachableFunction(const Function &F) {
  223. // A function must be unreachable if its entry block ends with an
  224. // 'unreachable'.
  225. assert(!F.isDeclaration());
  226. return isa<UnreachableInst>(F.getEntryBlock().getTerminator());
  227. }
  228. static void computeFunctionSummary(
  229. ModuleSummaryIndex &Index, const Module &M, const Function &F,
  230. BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, DominatorTree &DT,
  231. bool HasLocalsInUsedOrAsm, DenseSet<GlobalValue::GUID> &CantBePromoted,
  232. bool IsThinLTO,
  233. std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) {
  234. // Summary not currently supported for anonymous functions, they should
  235. // have been named.
  236. assert(F.hasName());
  237. unsigned NumInsts = 0;
  238. // Map from callee ValueId to profile count. Used to accumulate profile
  239. // counts for all static calls to a given callee.
  240. MapVector<ValueInfo, CalleeInfo> CallGraphEdges;
  241. SetVector<ValueInfo> RefEdges, LoadRefEdges, StoreRefEdges;
  242. SetVector<GlobalValue::GUID> TypeTests;
  243. SetVector<FunctionSummary::VFuncId> TypeTestAssumeVCalls,
  244. TypeCheckedLoadVCalls;
  245. SetVector<FunctionSummary::ConstVCall> TypeTestAssumeConstVCalls,
  246. TypeCheckedLoadConstVCalls;
  247. ICallPromotionAnalysis ICallAnalysis;
  248. SmallPtrSet<const User *, 8> Visited;
  249. // Add personality function, prefix data and prologue data to function's ref
  250. // list.
  251. findRefEdges(Index, &F, RefEdges, Visited);
  252. std::vector<const Instruction *> NonVolatileLoads;
  253. std::vector<const Instruction *> NonVolatileStores;
  254. std::vector<CallsiteInfo> Callsites;
  255. std::vector<AllocInfo> Allocs;
  256. bool HasInlineAsmMaybeReferencingInternal = false;
  257. bool HasIndirBranchToBlockAddress = false;
  258. bool HasUnknownCall = false;
  259. bool MayThrow = false;
  260. for (const BasicBlock &BB : F) {
  261. // We don't allow inlining of function with indirect branch to blockaddress.
  262. // If the blockaddress escapes the function, e.g., via a global variable,
  263. // inlining may lead to an invalid cross-function reference. So we shouldn't
  264. // import such function either.
  265. if (BB.hasAddressTaken()) {
  266. for (User *U : BlockAddress::get(const_cast<BasicBlock *>(&BB))->users())
  267. if (!isa<CallBrInst>(*U)) {
  268. HasIndirBranchToBlockAddress = true;
  269. break;
  270. }
  271. }
  272. for (const Instruction &I : BB) {
  273. if (I.isDebugOrPseudoInst())
  274. continue;
  275. ++NumInsts;
  276. // Regular LTO module doesn't participate in ThinLTO import,
  277. // so no reference from it can be read/writeonly, since this
  278. // would require importing variable as local copy
  279. if (IsThinLTO) {
  280. if (isNonVolatileLoad(&I)) {
  281. // Postpone processing of non-volatile load instructions
  282. // See comments below
  283. Visited.insert(&I);
  284. NonVolatileLoads.push_back(&I);
  285. continue;
  286. } else if (isNonVolatileStore(&I)) {
  287. Visited.insert(&I);
  288. NonVolatileStores.push_back(&I);
  289. // All references from second operand of store (destination address)
  290. // can be considered write-only if they're not referenced by any
  291. // non-store instruction. References from first operand of store
  292. // (stored value) can't be treated either as read- or as write-only
  293. // so we add them to RefEdges as we do with all other instructions
  294. // except non-volatile load.
  295. Value *Stored = I.getOperand(0);
  296. if (auto *GV = dyn_cast<GlobalValue>(Stored))
  297. // findRefEdges will try to examine GV operands, so instead
  298. // of calling it we should add GV to RefEdges directly.
  299. RefEdges.insert(Index.getOrInsertValueInfo(GV));
  300. else if (auto *U = dyn_cast<User>(Stored))
  301. findRefEdges(Index, U, RefEdges, Visited);
  302. continue;
  303. }
  304. }
  305. findRefEdges(Index, &I, RefEdges, Visited);
  306. const auto *CB = dyn_cast<CallBase>(&I);
  307. if (!CB) {
  308. if (I.mayThrow())
  309. MayThrow = true;
  310. continue;
  311. }
  312. const auto *CI = dyn_cast<CallInst>(&I);
  313. // Since we don't know exactly which local values are referenced in inline
  314. // assembly, conservatively mark the function as possibly referencing
  315. // a local value from inline assembly to ensure we don't export a
  316. // reference (which would require renaming and promotion of the
  317. // referenced value).
  318. if (HasLocalsInUsedOrAsm && CI && CI->isInlineAsm())
  319. HasInlineAsmMaybeReferencingInternal = true;
  320. auto *CalledValue = CB->getCalledOperand();
  321. auto *CalledFunction = CB->getCalledFunction();
  322. if (CalledValue && !CalledFunction) {
  323. CalledValue = CalledValue->stripPointerCasts();
  324. // Stripping pointer casts can reveal a called function.
  325. CalledFunction = dyn_cast<Function>(CalledValue);
  326. }
  327. // Check if this is an alias to a function. If so, get the
  328. // called aliasee for the checks below.
  329. if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
  330. assert(!CalledFunction && "Expected null called function in callsite for alias");
  331. CalledFunction = dyn_cast<Function>(GA->getAliaseeObject());
  332. }
  333. // Check if this is a direct call to a known function or a known
  334. // intrinsic, or an indirect call with profile data.
  335. if (CalledFunction) {
  336. if (CI && CalledFunction->isIntrinsic()) {
  337. addIntrinsicToSummary(
  338. CI, TypeTests, TypeTestAssumeVCalls, TypeCheckedLoadVCalls,
  339. TypeTestAssumeConstVCalls, TypeCheckedLoadConstVCalls, DT);
  340. continue;
  341. }
  342. // We should have named any anonymous globals
  343. assert(CalledFunction->hasName());
  344. auto ScaledCount = PSI->getProfileCount(*CB, BFI);
  345. auto Hotness = ScaledCount ? getHotness(*ScaledCount, PSI)
  346. : CalleeInfo::HotnessType::Unknown;
  347. if (ForceSummaryEdgesCold != FunctionSummary::FSHT_None)
  348. Hotness = CalleeInfo::HotnessType::Cold;
  349. // Use the original CalledValue, in case it was an alias. We want
  350. // to record the call edge to the alias in that case. Eventually
  351. // an alias summary will be created to associate the alias and
  352. // aliasee.
  353. auto &ValueInfo = CallGraphEdges[Index.getOrInsertValueInfo(
  354. cast<GlobalValue>(CalledValue))];
  355. ValueInfo.updateHotness(Hotness);
  356. // Add the relative block frequency to CalleeInfo if there is no profile
  357. // information.
  358. if (BFI != nullptr && Hotness == CalleeInfo::HotnessType::Unknown) {
  359. uint64_t BBFreq = BFI->getBlockFreq(&BB).getFrequency();
  360. uint64_t EntryFreq = BFI->getEntryFreq();
  361. ValueInfo.updateRelBlockFreq(BBFreq, EntryFreq);
  362. }
  363. } else {
  364. HasUnknownCall = true;
  365. // Skip inline assembly calls.
  366. if (CI && CI->isInlineAsm())
  367. continue;
  368. // Skip direct calls.
  369. if (!CalledValue || isa<Constant>(CalledValue))
  370. continue;
  371. // Check if the instruction has a callees metadata. If so, add callees
  372. // to CallGraphEdges to reflect the references from the metadata, and
  373. // to enable importing for subsequent indirect call promotion and
  374. // inlining.
  375. if (auto *MD = I.getMetadata(LLVMContext::MD_callees)) {
  376. for (const auto &Op : MD->operands()) {
  377. Function *Callee = mdconst::extract_or_null<Function>(Op);
  378. if (Callee)
  379. CallGraphEdges[Index.getOrInsertValueInfo(Callee)];
  380. }
  381. }
  382. uint32_t NumVals, NumCandidates;
  383. uint64_t TotalCount;
  384. auto CandidateProfileData =
  385. ICallAnalysis.getPromotionCandidatesForInstruction(
  386. &I, NumVals, TotalCount, NumCandidates);
  387. for (const auto &Candidate : CandidateProfileData)
  388. CallGraphEdges[Index.getOrInsertValueInfo(Candidate.Value)]
  389. .updateHotness(getHotness(Candidate.Count, PSI));
  390. }
  391. // TODO: Skip indirect calls for now. Need to handle these better, likely
  392. // by creating multiple Callsites, one per target, then speculatively
  393. // devirtualize while applying clone info in the ThinLTO backends. This
  394. // will also be important because we will have a different set of clone
  395. // versions per target. This handling needs to match that in the ThinLTO
  396. // backend so we handle things consistently for matching of callsite
  397. // summaries to instructions.
  398. if (!CalledFunction)
  399. continue;
  400. // Compute the list of stack ids first (so we can trim them from the stack
  401. // ids on any MIBs).
  402. CallStack<MDNode, MDNode::op_iterator> InstCallsite(
  403. I.getMetadata(LLVMContext::MD_callsite));
  404. auto *MemProfMD = I.getMetadata(LLVMContext::MD_memprof);
  405. if (MemProfMD) {
  406. std::vector<MIBInfo> MIBs;
  407. for (auto &MDOp : MemProfMD->operands()) {
  408. auto *MIBMD = cast<const MDNode>(MDOp);
  409. MDNode *StackNode = getMIBStackNode(MIBMD);
  410. assert(StackNode);
  411. SmallVector<unsigned> StackIdIndices;
  412. CallStack<MDNode, MDNode::op_iterator> StackContext(StackNode);
  413. // Collapse out any on the allocation call (inlining).
  414. for (auto ContextIter =
  415. StackContext.beginAfterSharedPrefix(InstCallsite);
  416. ContextIter != StackContext.end(); ++ContextIter) {
  417. unsigned StackIdIdx = Index.addOrGetStackIdIndex(*ContextIter);
  418. // If this is a direct recursion, simply skip the duplicate
  419. // entries. If this is mutual recursion, handling is left to
  420. // the LTO link analysis client.
  421. if (StackIdIndices.empty() || StackIdIndices.back() != StackIdIdx)
  422. StackIdIndices.push_back(StackIdIdx);
  423. }
  424. MIBs.push_back(
  425. MIBInfo(getMIBAllocType(MIBMD), std::move(StackIdIndices)));
  426. }
  427. Allocs.push_back(AllocInfo(std::move(MIBs)));
  428. } else if (!InstCallsite.empty()) {
  429. SmallVector<unsigned> StackIdIndices;
  430. for (auto StackId : InstCallsite)
  431. StackIdIndices.push_back(Index.addOrGetStackIdIndex(StackId));
  432. // Use the original CalledValue, in case it was an alias. We want
  433. // to record the call edge to the alias in that case. Eventually
  434. // an alias summary will be created to associate the alias and
  435. // aliasee.
  436. auto CalleeValueInfo =
  437. Index.getOrInsertValueInfo(cast<GlobalValue>(CalledValue));
  438. Callsites.push_back({CalleeValueInfo, StackIdIndices});
  439. }
  440. }
  441. }
  442. Index.addBlockCount(F.size());
  443. std::vector<ValueInfo> Refs;
  444. if (IsThinLTO) {
  445. auto AddRefEdges = [&](const std::vector<const Instruction *> &Instrs,
  446. SetVector<ValueInfo> &Edges,
  447. SmallPtrSet<const User *, 8> &Cache) {
  448. for (const auto *I : Instrs) {
  449. Cache.erase(I);
  450. findRefEdges(Index, I, Edges, Cache);
  451. }
  452. };
  453. // By now we processed all instructions in a function, except
  454. // non-volatile loads and non-volatile value stores. Let's find
  455. // ref edges for both of instruction sets
  456. AddRefEdges(NonVolatileLoads, LoadRefEdges, Visited);
  457. // We can add some values to the Visited set when processing load
  458. // instructions which are also used by stores in NonVolatileStores.
  459. // For example this can happen if we have following code:
  460. //
  461. // store %Derived* @foo, %Derived** bitcast (%Base** @bar to %Derived**)
  462. // %42 = load %Derived*, %Derived** bitcast (%Base** @bar to %Derived**)
  463. //
  464. // After processing loads we'll add bitcast to the Visited set, and if
  465. // we use the same set while processing stores, we'll never see store
  466. // to @bar and @bar will be mistakenly treated as readonly.
  467. SmallPtrSet<const llvm::User *, 8> StoreCache;
  468. AddRefEdges(NonVolatileStores, StoreRefEdges, StoreCache);
  469. // If both load and store instruction reference the same variable
  470. // we won't be able to optimize it. Add all such reference edges
  471. // to RefEdges set.
  472. for (const auto &VI : StoreRefEdges)
  473. if (LoadRefEdges.remove(VI))
  474. RefEdges.insert(VI);
  475. unsigned RefCnt = RefEdges.size();
  476. // All new reference edges inserted in two loops below are either
  477. // read or write only. They will be grouped in the end of RefEdges
  478. // vector, so we can use a single integer value to identify them.
  479. for (const auto &VI : LoadRefEdges)
  480. RefEdges.insert(VI);
  481. unsigned FirstWORef = RefEdges.size();
  482. for (const auto &VI : StoreRefEdges)
  483. RefEdges.insert(VI);
  484. Refs = RefEdges.takeVector();
  485. for (; RefCnt < FirstWORef; ++RefCnt)
  486. Refs[RefCnt].setReadOnly();
  487. for (; RefCnt < Refs.size(); ++RefCnt)
  488. Refs[RefCnt].setWriteOnly();
  489. } else {
  490. Refs = RefEdges.takeVector();
  491. }
  492. // Explicit add hot edges to enforce importing for designated GUIDs for
  493. // sample PGO, to enable the same inlines as the profiled optimized binary.
  494. for (auto &I : F.getImportGUIDs())
  495. CallGraphEdges[Index.getOrInsertValueInfo(I)].updateHotness(
  496. ForceSummaryEdgesCold == FunctionSummary::FSHT_All
  497. ? CalleeInfo::HotnessType::Cold
  498. : CalleeInfo::HotnessType::Critical);
  499. bool NonRenamableLocal = isNonRenamableLocal(F);
  500. bool NotEligibleForImport = NonRenamableLocal ||
  501. HasInlineAsmMaybeReferencingInternal ||
  502. HasIndirBranchToBlockAddress;
  503. GlobalValueSummary::GVFlags Flags(
  504. F.getLinkage(), F.getVisibility(), NotEligibleForImport,
  505. /* Live = */ false, F.isDSOLocal(), F.canBeOmittedFromSymbolTable());
  506. FunctionSummary::FFlags FunFlags{
  507. F.doesNotAccessMemory(), F.onlyReadsMemory() && !F.doesNotAccessMemory(),
  508. F.hasFnAttribute(Attribute::NoRecurse), F.returnDoesNotAlias(),
  509. // FIXME: refactor this to use the same code that inliner is using.
  510. // Don't try to import functions with noinline attribute.
  511. F.getAttributes().hasFnAttr(Attribute::NoInline),
  512. F.hasFnAttribute(Attribute::AlwaysInline),
  513. F.hasFnAttribute(Attribute::NoUnwind), MayThrow, HasUnknownCall,
  514. mustBeUnreachableFunction(F)};
  515. std::vector<FunctionSummary::ParamAccess> ParamAccesses;
  516. if (auto *SSI = GetSSICallback(F))
  517. ParamAccesses = SSI->getParamAccesses(Index);
  518. auto FuncSummary = std::make_unique<FunctionSummary>(
  519. Flags, NumInsts, FunFlags, /*EntryCount=*/0, std::move(Refs),
  520. CallGraphEdges.takeVector(), TypeTests.takeVector(),
  521. TypeTestAssumeVCalls.takeVector(), TypeCheckedLoadVCalls.takeVector(),
  522. TypeTestAssumeConstVCalls.takeVector(),
  523. TypeCheckedLoadConstVCalls.takeVector(), std::move(ParamAccesses),
  524. std::move(Callsites), std::move(Allocs));
  525. if (NonRenamableLocal)
  526. CantBePromoted.insert(F.getGUID());
  527. Index.addGlobalValueSummary(F, std::move(FuncSummary));
  528. }
  529. /// Find function pointers referenced within the given vtable initializer
  530. /// (or subset of an initializer) \p I. The starting offset of \p I within
  531. /// the vtable initializer is \p StartingOffset. Any discovered function
  532. /// pointers are added to \p VTableFuncs along with their cumulative offset
  533. /// within the initializer.
  534. static void findFuncPointers(const Constant *I, uint64_t StartingOffset,
  535. const Module &M, ModuleSummaryIndex &Index,
  536. VTableFuncList &VTableFuncs) {
  537. // First check if this is a function pointer.
  538. if (I->getType()->isPointerTy()) {
  539. auto Fn = dyn_cast<Function>(I->stripPointerCasts());
  540. // We can disregard __cxa_pure_virtual as a possible call target, as
  541. // calls to pure virtuals are UB.
  542. if (Fn && Fn->getName() != "__cxa_pure_virtual")
  543. VTableFuncs.push_back({Index.getOrInsertValueInfo(Fn), StartingOffset});
  544. return;
  545. }
  546. // Walk through the elements in the constant struct or array and recursively
  547. // look for virtual function pointers.
  548. const DataLayout &DL = M.getDataLayout();
  549. if (auto *C = dyn_cast<ConstantStruct>(I)) {
  550. StructType *STy = dyn_cast<StructType>(C->getType());
  551. assert(STy);
  552. const StructLayout *SL = DL.getStructLayout(C->getType());
  553. for (auto EI : llvm::enumerate(STy->elements())) {
  554. auto Offset = SL->getElementOffset(EI.index());
  555. unsigned Op = SL->getElementContainingOffset(Offset);
  556. findFuncPointers(cast<Constant>(I->getOperand(Op)),
  557. StartingOffset + Offset, M, Index, VTableFuncs);
  558. }
  559. } else if (auto *C = dyn_cast<ConstantArray>(I)) {
  560. ArrayType *ATy = C->getType();
  561. Type *EltTy = ATy->getElementType();
  562. uint64_t EltSize = DL.getTypeAllocSize(EltTy);
  563. for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
  564. findFuncPointers(cast<Constant>(I->getOperand(i)),
  565. StartingOffset + i * EltSize, M, Index, VTableFuncs);
  566. }
  567. }
  568. }
  569. // Identify the function pointers referenced by vtable definition \p V.
  570. static void computeVTableFuncs(ModuleSummaryIndex &Index,
  571. const GlobalVariable &V, const Module &M,
  572. VTableFuncList &VTableFuncs) {
  573. if (!V.isConstant())
  574. return;
  575. findFuncPointers(V.getInitializer(), /*StartingOffset=*/0, M, Index,
  576. VTableFuncs);
  577. #ifndef NDEBUG
  578. // Validate that the VTableFuncs list is ordered by offset.
  579. uint64_t PrevOffset = 0;
  580. for (auto &P : VTableFuncs) {
  581. // The findVFuncPointers traversal should have encountered the
  582. // functions in offset order. We need to use ">=" since PrevOffset
  583. // starts at 0.
  584. assert(P.VTableOffset >= PrevOffset);
  585. PrevOffset = P.VTableOffset;
  586. }
  587. #endif
  588. }
  589. /// Record vtable definition \p V for each type metadata it references.
  590. static void
  591. recordTypeIdCompatibleVtableReferences(ModuleSummaryIndex &Index,
  592. const GlobalVariable &V,
  593. SmallVectorImpl<MDNode *> &Types) {
  594. for (MDNode *Type : Types) {
  595. auto TypeID = Type->getOperand(1).get();
  596. uint64_t Offset =
  597. cast<ConstantInt>(
  598. cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
  599. ->getZExtValue();
  600. if (auto *TypeId = dyn_cast<MDString>(TypeID))
  601. Index.getOrInsertTypeIdCompatibleVtableSummary(TypeId->getString())
  602. .push_back({Offset, Index.getOrInsertValueInfo(&V)});
  603. }
  604. }
  605. static void computeVariableSummary(ModuleSummaryIndex &Index,
  606. const GlobalVariable &V,
  607. DenseSet<GlobalValue::GUID> &CantBePromoted,
  608. const Module &M,
  609. SmallVectorImpl<MDNode *> &Types) {
  610. SetVector<ValueInfo> RefEdges;
  611. SmallPtrSet<const User *, 8> Visited;
  612. bool HasBlockAddress = findRefEdges(Index, &V, RefEdges, Visited);
  613. bool NonRenamableLocal = isNonRenamableLocal(V);
  614. GlobalValueSummary::GVFlags Flags(
  615. V.getLinkage(), V.getVisibility(), NonRenamableLocal,
  616. /* Live = */ false, V.isDSOLocal(), V.canBeOmittedFromSymbolTable());
  617. VTableFuncList VTableFuncs;
  618. // If splitting is not enabled, then we compute the summary information
  619. // necessary for index-based whole program devirtualization.
  620. if (!Index.enableSplitLTOUnit()) {
  621. Types.clear();
  622. V.getMetadata(LLVMContext::MD_type, Types);
  623. if (!Types.empty()) {
  624. // Identify the function pointers referenced by this vtable definition.
  625. computeVTableFuncs(Index, V, M, VTableFuncs);
  626. // Record this vtable definition for each type metadata it references.
  627. recordTypeIdCompatibleVtableReferences(Index, V, Types);
  628. }
  629. }
  630. // Don't mark variables we won't be able to internalize as read/write-only.
  631. bool CanBeInternalized =
  632. !V.hasComdat() && !V.hasAppendingLinkage() && !V.isInterposable() &&
  633. !V.hasAvailableExternallyLinkage() && !V.hasDLLExportStorageClass();
  634. bool Constant = V.isConstant();
  635. GlobalVarSummary::GVarFlags VarFlags(CanBeInternalized,
  636. Constant ? false : CanBeInternalized,
  637. Constant, V.getVCallVisibility());
  638. auto GVarSummary = std::make_unique<GlobalVarSummary>(Flags, VarFlags,
  639. RefEdges.takeVector());
  640. if (NonRenamableLocal)
  641. CantBePromoted.insert(V.getGUID());
  642. if (HasBlockAddress)
  643. GVarSummary->setNotEligibleToImport();
  644. if (!VTableFuncs.empty())
  645. GVarSummary->setVTableFuncs(VTableFuncs);
  646. Index.addGlobalValueSummary(V, std::move(GVarSummary));
  647. }
  648. static void computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A,
  649. DenseSet<GlobalValue::GUID> &CantBePromoted) {
  650. // Skip summary for indirect function aliases as summary for aliasee will not
  651. // be emitted.
  652. const GlobalObject *Aliasee = A.getAliaseeObject();
  653. if (isa<GlobalIFunc>(Aliasee))
  654. return;
  655. bool NonRenamableLocal = isNonRenamableLocal(A);
  656. GlobalValueSummary::GVFlags Flags(
  657. A.getLinkage(), A.getVisibility(), NonRenamableLocal,
  658. /* Live = */ false, A.isDSOLocal(), A.canBeOmittedFromSymbolTable());
  659. auto AS = std::make_unique<AliasSummary>(Flags);
  660. auto AliaseeVI = Index.getValueInfo(Aliasee->getGUID());
  661. assert(AliaseeVI && "Alias expects aliasee summary to be available");
  662. assert(AliaseeVI.getSummaryList().size() == 1 &&
  663. "Expected a single entry per aliasee in per-module index");
  664. AS->setAliasee(AliaseeVI, AliaseeVI.getSummaryList()[0].get());
  665. if (NonRenamableLocal)
  666. CantBePromoted.insert(A.getGUID());
  667. Index.addGlobalValueSummary(A, std::move(AS));
  668. }
  669. // Set LiveRoot flag on entries matching the given value name.
  670. static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
  671. if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
  672. for (const auto &Summary : VI.getSummaryList())
  673. Summary->setLive(true);
  674. }
  675. ModuleSummaryIndex llvm::buildModuleSummaryIndex(
  676. const Module &M,
  677. std::function<BlockFrequencyInfo *(const Function &F)> GetBFICallback,
  678. ProfileSummaryInfo *PSI,
  679. std::function<const StackSafetyInfo *(const Function &F)> GetSSICallback) {
  680. assert(PSI);
  681. bool EnableSplitLTOUnit = false;
  682. if (auto *MD = mdconst::extract_or_null<ConstantInt>(
  683. M.getModuleFlag("EnableSplitLTOUnit")))
  684. EnableSplitLTOUnit = MD->getZExtValue();
  685. ModuleSummaryIndex Index(/*HaveGVs=*/true, EnableSplitLTOUnit);
  686. // Identify the local values in the llvm.used and llvm.compiler.used sets,
  687. // which should not be exported as they would then require renaming and
  688. // promotion, but we may have opaque uses e.g. in inline asm. We collect them
  689. // here because we use this information to mark functions containing inline
  690. // assembly calls as not importable.
  691. SmallPtrSet<GlobalValue *, 4> LocalsUsed;
  692. SmallVector<GlobalValue *, 4> Used;
  693. // First collect those in the llvm.used set.
  694. collectUsedGlobalVariables(M, Used, /*CompilerUsed=*/false);
  695. // Next collect those in the llvm.compiler.used set.
  696. collectUsedGlobalVariables(M, Used, /*CompilerUsed=*/true);
  697. DenseSet<GlobalValue::GUID> CantBePromoted;
  698. for (auto *V : Used) {
  699. if (V->hasLocalLinkage()) {
  700. LocalsUsed.insert(V);
  701. CantBePromoted.insert(V->getGUID());
  702. }
  703. }
  704. bool HasLocalInlineAsmSymbol = false;
  705. if (!M.getModuleInlineAsm().empty()) {
  706. // Collect the local values defined by module level asm, and set up
  707. // summaries for these symbols so that they can be marked as NoRename,
  708. // to prevent export of any use of them in regular IR that would require
  709. // renaming within the module level asm. Note we don't need to create a
  710. // summary for weak or global defs, as they don't need to be flagged as
  711. // NoRename, and defs in module level asm can't be imported anyway.
  712. // Also, any values used but not defined within module level asm should
  713. // be listed on the llvm.used or llvm.compiler.used global and marked as
  714. // referenced from there.
  715. ModuleSymbolTable::CollectAsmSymbols(
  716. M, [&](StringRef Name, object::BasicSymbolRef::Flags Flags) {
  717. // Symbols not marked as Weak or Global are local definitions.
  718. if (Flags & (object::BasicSymbolRef::SF_Weak |
  719. object::BasicSymbolRef::SF_Global))
  720. return;
  721. HasLocalInlineAsmSymbol = true;
  722. GlobalValue *GV = M.getNamedValue(Name);
  723. if (!GV)
  724. return;
  725. assert(GV->isDeclaration() && "Def in module asm already has definition");
  726. GlobalValueSummary::GVFlags GVFlags(
  727. GlobalValue::InternalLinkage, GlobalValue::DefaultVisibility,
  728. /* NotEligibleToImport = */ true,
  729. /* Live = */ true,
  730. /* Local */ GV->isDSOLocal(), GV->canBeOmittedFromSymbolTable());
  731. CantBePromoted.insert(GV->getGUID());
  732. // Create the appropriate summary type.
  733. if (Function *F = dyn_cast<Function>(GV)) {
  734. std::unique_ptr<FunctionSummary> Summary =
  735. std::make_unique<FunctionSummary>(
  736. GVFlags, /*InstCount=*/0,
  737. FunctionSummary::FFlags{
  738. F->hasFnAttribute(Attribute::ReadNone),
  739. F->hasFnAttribute(Attribute::ReadOnly),
  740. F->hasFnAttribute(Attribute::NoRecurse),
  741. F->returnDoesNotAlias(),
  742. /* NoInline = */ false,
  743. F->hasFnAttribute(Attribute::AlwaysInline),
  744. F->hasFnAttribute(Attribute::NoUnwind),
  745. /* MayThrow */ true,
  746. /* HasUnknownCall */ true,
  747. /* MustBeUnreachable */ false},
  748. /*EntryCount=*/0, ArrayRef<ValueInfo>{},
  749. ArrayRef<FunctionSummary::EdgeTy>{},
  750. ArrayRef<GlobalValue::GUID>{},
  751. ArrayRef<FunctionSummary::VFuncId>{},
  752. ArrayRef<FunctionSummary::VFuncId>{},
  753. ArrayRef<FunctionSummary::ConstVCall>{},
  754. ArrayRef<FunctionSummary::ConstVCall>{},
  755. ArrayRef<FunctionSummary::ParamAccess>{},
  756. ArrayRef<CallsiteInfo>{}, ArrayRef<AllocInfo>{});
  757. Index.addGlobalValueSummary(*GV, std::move(Summary));
  758. } else {
  759. std::unique_ptr<GlobalVarSummary> Summary =
  760. std::make_unique<GlobalVarSummary>(
  761. GVFlags,
  762. GlobalVarSummary::GVarFlags(
  763. false, false, cast<GlobalVariable>(GV)->isConstant(),
  764. GlobalObject::VCallVisibilityPublic),
  765. ArrayRef<ValueInfo>{});
  766. Index.addGlobalValueSummary(*GV, std::move(Summary));
  767. }
  768. });
  769. }
  770. bool IsThinLTO = true;
  771. if (auto *MD =
  772. mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
  773. IsThinLTO = MD->getZExtValue();
  774. // Compute summaries for all functions defined in module, and save in the
  775. // index.
  776. for (const auto &F : M) {
  777. if (F.isDeclaration())
  778. continue;
  779. DominatorTree DT(const_cast<Function &>(F));
  780. BlockFrequencyInfo *BFI = nullptr;
  781. std::unique_ptr<BlockFrequencyInfo> BFIPtr;
  782. if (GetBFICallback)
  783. BFI = GetBFICallback(F);
  784. else if (F.hasProfileData()) {
  785. LoopInfo LI{DT};
  786. BranchProbabilityInfo BPI{F, LI};
  787. BFIPtr = std::make_unique<BlockFrequencyInfo>(F, BPI, LI);
  788. BFI = BFIPtr.get();
  789. }
  790. computeFunctionSummary(Index, M, F, BFI, PSI, DT,
  791. !LocalsUsed.empty() || HasLocalInlineAsmSymbol,
  792. CantBePromoted, IsThinLTO, GetSSICallback);
  793. }
  794. // Compute summaries for all variables defined in module, and save in the
  795. // index.
  796. SmallVector<MDNode *, 2> Types;
  797. for (const GlobalVariable &G : M.globals()) {
  798. if (G.isDeclaration())
  799. continue;
  800. computeVariableSummary(Index, G, CantBePromoted, M, Types);
  801. }
  802. // Compute summaries for all aliases defined in module, and save in the
  803. // index.
  804. for (const GlobalAlias &A : M.aliases())
  805. computeAliasSummary(Index, A, CantBePromoted);
  806. // Iterate through ifuncs, set their resolvers all alive.
  807. for (const GlobalIFunc &I : M.ifuncs()) {
  808. I.applyAlongResolverPath([&Index](const GlobalValue &GV) {
  809. Index.getGlobalValueSummary(GV)->setLive(true);
  810. });
  811. }
  812. for (auto *V : LocalsUsed) {
  813. auto *Summary = Index.getGlobalValueSummary(*V);
  814. assert(Summary && "Missing summary for global value");
  815. Summary->setNotEligibleToImport();
  816. }
  817. // The linker doesn't know about these LLVM produced values, so we need
  818. // to flag them as live in the index to ensure index-based dead value
  819. // analysis treats them as live roots of the analysis.
  820. setLiveRoot(Index, "llvm.used");
  821. setLiveRoot(Index, "llvm.compiler.used");
  822. setLiveRoot(Index, "llvm.global_ctors");
  823. setLiveRoot(Index, "llvm.global_dtors");
  824. setLiveRoot(Index, "llvm.global.annotations");
  825. for (auto &GlobalList : Index) {
  826. // Ignore entries for references that are undefined in the current module.
  827. if (GlobalList.second.SummaryList.empty())
  828. continue;
  829. assert(GlobalList.second.SummaryList.size() == 1 &&
  830. "Expected module's index to have one summary per GUID");
  831. auto &Summary = GlobalList.second.SummaryList[0];
  832. if (!IsThinLTO) {
  833. Summary->setNotEligibleToImport();
  834. continue;
  835. }
  836. bool AllRefsCanBeExternallyReferenced =
  837. llvm::all_of(Summary->refs(), [&](const ValueInfo &VI) {
  838. return !CantBePromoted.count(VI.getGUID());
  839. });
  840. if (!AllRefsCanBeExternallyReferenced) {
  841. Summary->setNotEligibleToImport();
  842. continue;
  843. }
  844. if (auto *FuncSummary = dyn_cast<FunctionSummary>(Summary.get())) {
  845. bool AllCallsCanBeExternallyReferenced = llvm::all_of(
  846. FuncSummary->calls(), [&](const FunctionSummary::EdgeTy &Edge) {
  847. return !CantBePromoted.count(Edge.first.getGUID());
  848. });
  849. if (!AllCallsCanBeExternallyReferenced)
  850. Summary->setNotEligibleToImport();
  851. }
  852. }
  853. if (!ModuleSummaryDotFile.empty()) {
  854. std::error_code EC;
  855. raw_fd_ostream OSDot(ModuleSummaryDotFile, EC, sys::fs::OpenFlags::OF_None);
  856. if (EC)
  857. report_fatal_error(Twine("Failed to open dot file ") +
  858. ModuleSummaryDotFile + ": " + EC.message() + "\n");
  859. Index.exportToDot(OSDot, {});
  860. }
  861. return Index;
  862. }
  863. AnalysisKey ModuleSummaryIndexAnalysis::Key;
  864. ModuleSummaryIndex
  865. ModuleSummaryIndexAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
  866. ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
  867. auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  868. bool NeedSSI = needsParamAccessSummary(M);
  869. return buildModuleSummaryIndex(
  870. M,
  871. [&FAM](const Function &F) {
  872. return &FAM.getResult<BlockFrequencyAnalysis>(
  873. *const_cast<Function *>(&F));
  874. },
  875. &PSI,
  876. [&FAM, NeedSSI](const Function &F) -> const StackSafetyInfo * {
  877. return NeedSSI ? &FAM.getResult<StackSafetyAnalysis>(
  878. const_cast<Function &>(F))
  879. : nullptr;
  880. });
  881. }
  882. char ModuleSummaryIndexWrapperPass::ID = 0;
  883. INITIALIZE_PASS_BEGIN(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
  884. "Module Summary Analysis", false, true)
  885. INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
  886. INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
  887. INITIALIZE_PASS_DEPENDENCY(StackSafetyInfoWrapperPass)
  888. INITIALIZE_PASS_END(ModuleSummaryIndexWrapperPass, "module-summary-analysis",
  889. "Module Summary Analysis", false, true)
  890. ModulePass *llvm::createModuleSummaryIndexWrapperPass() {
  891. return new ModuleSummaryIndexWrapperPass();
  892. }
  893. ModuleSummaryIndexWrapperPass::ModuleSummaryIndexWrapperPass()
  894. : ModulePass(ID) {
  895. initializeModuleSummaryIndexWrapperPassPass(*PassRegistry::getPassRegistry());
  896. }
  897. bool ModuleSummaryIndexWrapperPass::runOnModule(Module &M) {
  898. auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
  899. bool NeedSSI = needsParamAccessSummary(M);
  900. Index.emplace(buildModuleSummaryIndex(
  901. M,
  902. [this](const Function &F) {
  903. return &(this->getAnalysis<BlockFrequencyInfoWrapperPass>(
  904. *const_cast<Function *>(&F))
  905. .getBFI());
  906. },
  907. PSI,
  908. [&](const Function &F) -> const StackSafetyInfo * {
  909. return NeedSSI ? &getAnalysis<StackSafetyInfoWrapperPass>(
  910. const_cast<Function &>(F))
  911. .getResult()
  912. : nullptr;
  913. }));
  914. return false;
  915. }
  916. bool ModuleSummaryIndexWrapperPass::doFinalization(Module &M) {
  917. Index.reset();
  918. return false;
  919. }
  920. void ModuleSummaryIndexWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  921. AU.setPreservesAll();
  922. AU.addRequired<BlockFrequencyInfoWrapperPass>();
  923. AU.addRequired<ProfileSummaryInfoWrapperPass>();
  924. AU.addRequired<StackSafetyInfoWrapperPass>();
  925. }
  926. char ImmutableModuleSummaryIndexWrapperPass::ID = 0;
  927. ImmutableModuleSummaryIndexWrapperPass::ImmutableModuleSummaryIndexWrapperPass(
  928. const ModuleSummaryIndex *Index)
  929. : ImmutablePass(ID), Index(Index) {
  930. initializeImmutableModuleSummaryIndexWrapperPassPass(
  931. *PassRegistry::getPassRegistry());
  932. }
  933. void ImmutableModuleSummaryIndexWrapperPass::getAnalysisUsage(
  934. AnalysisUsage &AU) const {
  935. AU.setPreservesAll();
  936. }
  937. ImmutablePass *llvm::createImmutableModuleSummaryIndexWrapperPass(
  938. const ModuleSummaryIndex *Index) {
  939. return new ImmutableModuleSummaryIndexWrapperPass(Index);
  940. }
  941. INITIALIZE_PASS(ImmutableModuleSummaryIndexWrapperPass, "module-summary-info",
  942. "Module summary info", false, true)