ModuleSummaryAnalysis.cpp 27 KB

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