ModuleSummaryIndex.h 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ModuleSummaryIndex.h - Module Summary Index ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. /// @file
  15. /// ModuleSummaryIndex.h This file contains the declarations the classes that
  16. /// hold the module index and summary for function importing.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_IR_MODULESUMMARYINDEX_H
  20. #define LLVM_IR_MODULESUMMARYINDEX_H
  21. #include "llvm/ADT/ArrayRef.h"
  22. #include "llvm/ADT/DenseMap.h"
  23. #include "llvm/ADT/STLExtras.h"
  24. #include "llvm/ADT/SmallString.h"
  25. #include "llvm/ADT/StringExtras.h"
  26. #include "llvm/ADT/StringMap.h"
  27. #include "llvm/ADT/StringRef.h"
  28. #include "llvm/ADT/TinyPtrVector.h"
  29. #include "llvm/IR/ConstantRange.h"
  30. #include "llvm/IR/GlobalValue.h"
  31. #include "llvm/IR/Module.h"
  32. #include "llvm/Support/Allocator.h"
  33. #include "llvm/Support/MathExtras.h"
  34. #include "llvm/Support/ScaledNumber.h"
  35. #include "llvm/Support/StringSaver.h"
  36. #include "llvm/Support/raw_ostream.h"
  37. #include <algorithm>
  38. #include <array>
  39. #include <cassert>
  40. #include <cstddef>
  41. #include <cstdint>
  42. #include <map>
  43. #include <memory>
  44. #include <set>
  45. #include <string>
  46. #include <utility>
  47. #include <vector>
  48. namespace llvm {
  49. namespace yaml {
  50. template <typename T> struct MappingTraits;
  51. } // end namespace yaml
  52. /// Class to accumulate and hold information about a callee.
  53. struct CalleeInfo {
  54. enum class HotnessType : uint8_t {
  55. Unknown = 0,
  56. Cold = 1,
  57. None = 2,
  58. Hot = 3,
  59. Critical = 4
  60. };
  61. // The size of the bit-field might need to be adjusted if more values are
  62. // added to HotnessType enum.
  63. uint32_t Hotness : 3;
  64. /// The value stored in RelBlockFreq has to be interpreted as the digits of
  65. /// a scaled number with a scale of \p -ScaleShift.
  66. uint32_t RelBlockFreq : 29;
  67. static constexpr int32_t ScaleShift = 8;
  68. static constexpr uint64_t MaxRelBlockFreq = (1 << 29) - 1;
  69. CalleeInfo()
  70. : Hotness(static_cast<uint32_t>(HotnessType::Unknown)), RelBlockFreq(0) {}
  71. explicit CalleeInfo(HotnessType Hotness, uint64_t RelBF)
  72. : Hotness(static_cast<uint32_t>(Hotness)), RelBlockFreq(RelBF) {}
  73. void updateHotness(const HotnessType OtherHotness) {
  74. Hotness = std::max(Hotness, static_cast<uint32_t>(OtherHotness));
  75. }
  76. HotnessType getHotness() const { return HotnessType(Hotness); }
  77. /// Update \p RelBlockFreq from \p BlockFreq and \p EntryFreq
  78. ///
  79. /// BlockFreq is divided by EntryFreq and added to RelBlockFreq. To represent
  80. /// fractional values, the result is represented as a fixed point number with
  81. /// scale of -ScaleShift.
  82. void updateRelBlockFreq(uint64_t BlockFreq, uint64_t EntryFreq) {
  83. if (EntryFreq == 0)
  84. return;
  85. using Scaled64 = ScaledNumber<uint64_t>;
  86. Scaled64 Temp(BlockFreq, ScaleShift);
  87. Temp /= Scaled64::get(EntryFreq);
  88. uint64_t Sum =
  89. SaturatingAdd<uint64_t>(Temp.toInt<uint64_t>(), RelBlockFreq);
  90. Sum = std::min(Sum, uint64_t(MaxRelBlockFreq));
  91. RelBlockFreq = static_cast<uint32_t>(Sum);
  92. }
  93. };
  94. inline const char *getHotnessName(CalleeInfo::HotnessType HT) {
  95. switch (HT) {
  96. case CalleeInfo::HotnessType::Unknown:
  97. return "unknown";
  98. case CalleeInfo::HotnessType::Cold:
  99. return "cold";
  100. case CalleeInfo::HotnessType::None:
  101. return "none";
  102. case CalleeInfo::HotnessType::Hot:
  103. return "hot";
  104. case CalleeInfo::HotnessType::Critical:
  105. return "critical";
  106. }
  107. llvm_unreachable("invalid hotness");
  108. }
  109. class GlobalValueSummary;
  110. using GlobalValueSummaryList = std::vector<std::unique_ptr<GlobalValueSummary>>;
  111. struct alignas(8) GlobalValueSummaryInfo {
  112. union NameOrGV {
  113. NameOrGV(bool HaveGVs) {
  114. if (HaveGVs)
  115. GV = nullptr;
  116. else
  117. Name = "";
  118. }
  119. /// The GlobalValue corresponding to this summary. This is only used in
  120. /// per-module summaries and when the IR is available. E.g. when module
  121. /// analysis is being run, or when parsing both the IR and the summary
  122. /// from assembly.
  123. const GlobalValue *GV;
  124. /// Summary string representation. This StringRef points to BC module
  125. /// string table and is valid until module data is stored in memory.
  126. /// This is guaranteed to happen until runThinLTOBackend function is
  127. /// called, so it is safe to use this field during thin link. This field
  128. /// is only valid if summary index was loaded from BC file.
  129. StringRef Name;
  130. } U;
  131. GlobalValueSummaryInfo(bool HaveGVs) : U(HaveGVs) {}
  132. /// List of global value summary structures for a particular value held
  133. /// in the GlobalValueMap. Requires a vector in the case of multiple
  134. /// COMDAT values of the same name.
  135. GlobalValueSummaryList SummaryList;
  136. };
  137. /// Map from global value GUID to corresponding summary structures. Use a
  138. /// std::map rather than a DenseMap so that pointers to the map's value_type
  139. /// (which are used by ValueInfo) are not invalidated by insertion. Also it will
  140. /// likely incur less overhead, as the value type is not very small and the size
  141. /// of the map is unknown, resulting in inefficiencies due to repeated
  142. /// insertions and resizing.
  143. using GlobalValueSummaryMapTy =
  144. std::map<GlobalValue::GUID, GlobalValueSummaryInfo>;
  145. /// Struct that holds a reference to a particular GUID in a global value
  146. /// summary.
  147. struct ValueInfo {
  148. enum Flags { HaveGV = 1, ReadOnly = 2, WriteOnly = 4 };
  149. PointerIntPair<const GlobalValueSummaryMapTy::value_type *, 3, int>
  150. RefAndFlags;
  151. ValueInfo() = default;
  152. ValueInfo(bool HaveGVs, const GlobalValueSummaryMapTy::value_type *R) {
  153. RefAndFlags.setPointer(R);
  154. RefAndFlags.setInt(HaveGVs);
  155. }
  156. explicit operator bool() const { return getRef(); }
  157. GlobalValue::GUID getGUID() const { return getRef()->first; }
  158. const GlobalValue *getValue() const {
  159. assert(haveGVs());
  160. return getRef()->second.U.GV;
  161. }
  162. ArrayRef<std::unique_ptr<GlobalValueSummary>> getSummaryList() const {
  163. return getRef()->second.SummaryList;
  164. }
  165. StringRef name() const {
  166. return haveGVs() ? getRef()->second.U.GV->getName()
  167. : getRef()->second.U.Name;
  168. }
  169. bool haveGVs() const { return RefAndFlags.getInt() & HaveGV; }
  170. bool isReadOnly() const {
  171. assert(isValidAccessSpecifier());
  172. return RefAndFlags.getInt() & ReadOnly;
  173. }
  174. bool isWriteOnly() const {
  175. assert(isValidAccessSpecifier());
  176. return RefAndFlags.getInt() & WriteOnly;
  177. }
  178. unsigned getAccessSpecifier() const {
  179. assert(isValidAccessSpecifier());
  180. return RefAndFlags.getInt() & (ReadOnly | WriteOnly);
  181. }
  182. bool isValidAccessSpecifier() const {
  183. unsigned BadAccessMask = ReadOnly | WriteOnly;
  184. return (RefAndFlags.getInt() & BadAccessMask) != BadAccessMask;
  185. }
  186. void setReadOnly() {
  187. // We expect ro/wo attribute to set only once during
  188. // ValueInfo lifetime.
  189. assert(getAccessSpecifier() == 0);
  190. RefAndFlags.setInt(RefAndFlags.getInt() | ReadOnly);
  191. }
  192. void setWriteOnly() {
  193. assert(getAccessSpecifier() == 0);
  194. RefAndFlags.setInt(RefAndFlags.getInt() | WriteOnly);
  195. }
  196. const GlobalValueSummaryMapTy::value_type *getRef() const {
  197. return RefAndFlags.getPointer();
  198. }
  199. bool isDSOLocal() const;
  200. /// Checks if all copies are eligible for auto-hiding (have flag set).
  201. bool canAutoHide() const;
  202. };
  203. inline raw_ostream &operator<<(raw_ostream &OS, const ValueInfo &VI) {
  204. OS << VI.getGUID();
  205. if (!VI.name().empty())
  206. OS << " (" << VI.name() << ")";
  207. return OS;
  208. }
  209. inline bool operator==(const ValueInfo &A, const ValueInfo &B) {
  210. assert(A.getRef() && B.getRef() &&
  211. "Need ValueInfo with non-null Ref for comparison");
  212. return A.getRef() == B.getRef();
  213. }
  214. inline bool operator!=(const ValueInfo &A, const ValueInfo &B) {
  215. assert(A.getRef() && B.getRef() &&
  216. "Need ValueInfo with non-null Ref for comparison");
  217. return A.getRef() != B.getRef();
  218. }
  219. inline bool operator<(const ValueInfo &A, const ValueInfo &B) {
  220. assert(A.getRef() && B.getRef() &&
  221. "Need ValueInfo with non-null Ref to compare GUIDs");
  222. return A.getGUID() < B.getGUID();
  223. }
  224. template <> struct DenseMapInfo<ValueInfo> {
  225. static inline ValueInfo getEmptyKey() {
  226. return ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8);
  227. }
  228. static inline ValueInfo getTombstoneKey() {
  229. return ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-16);
  230. }
  231. static inline bool isSpecialKey(ValueInfo V) {
  232. return V == getTombstoneKey() || V == getEmptyKey();
  233. }
  234. static bool isEqual(ValueInfo L, ValueInfo R) {
  235. // We are not supposed to mix ValueInfo(s) with different HaveGVs flag
  236. // in a same container.
  237. assert(isSpecialKey(L) || isSpecialKey(R) || (L.haveGVs() == R.haveGVs()));
  238. return L.getRef() == R.getRef();
  239. }
  240. static unsigned getHashValue(ValueInfo I) { return (uintptr_t)I.getRef(); }
  241. };
  242. /// Function and variable summary information to aid decisions and
  243. /// implementation of importing.
  244. class GlobalValueSummary {
  245. public:
  246. /// Sububclass discriminator (for dyn_cast<> et al.)
  247. enum SummaryKind : unsigned { AliasKind, FunctionKind, GlobalVarKind };
  248. /// Group flags (Linkage, NotEligibleToImport, etc.) as a bitfield.
  249. struct GVFlags {
  250. /// The linkage type of the associated global value.
  251. ///
  252. /// One use is to flag values that have local linkage types and need to
  253. /// have module identifier appended before placing into the combined
  254. /// index, to disambiguate from other values with the same name.
  255. /// In the future this will be used to update and optimize linkage
  256. /// types based on global summary-based analysis.
  257. unsigned Linkage : 4;
  258. /// Indicate if the global value cannot be imported (e.g. it cannot
  259. /// be renamed or references something that can't be renamed).
  260. unsigned NotEligibleToImport : 1;
  261. /// In per-module summary, indicate that the global value must be considered
  262. /// a live root for index-based liveness analysis. Used for special LLVM
  263. /// values such as llvm.global_ctors that the linker does not know about.
  264. ///
  265. /// In combined summary, indicate that the global value is live.
  266. unsigned Live : 1;
  267. /// Indicates that the linker resolved the symbol to a definition from
  268. /// within the same linkage unit.
  269. unsigned DSOLocal : 1;
  270. /// In the per-module summary, indicates that the global value is
  271. /// linkonce_odr and global unnamed addr (so eligible for auto-hiding
  272. /// via hidden visibility). In the combined summary, indicates that the
  273. /// prevailing linkonce_odr copy can be auto-hidden via hidden visibility
  274. /// when it is upgraded to weak_odr in the backend. This is legal when
  275. /// all copies are eligible for auto-hiding (i.e. all copies were
  276. /// linkonce_odr global unnamed addr. If any copy is not (e.g. it was
  277. /// originally weak_odr, we cannot auto-hide the prevailing copy as it
  278. /// means the symbol was externally visible.
  279. unsigned CanAutoHide : 1;
  280. /// Convenience Constructors
  281. explicit GVFlags(GlobalValue::LinkageTypes Linkage,
  282. bool NotEligibleToImport, bool Live, bool IsLocal,
  283. bool CanAutoHide)
  284. : Linkage(Linkage), NotEligibleToImport(NotEligibleToImport),
  285. Live(Live), DSOLocal(IsLocal), CanAutoHide(CanAutoHide) {}
  286. };
  287. private:
  288. /// Kind of summary for use in dyn_cast<> et al.
  289. SummaryKind Kind;
  290. GVFlags Flags;
  291. /// This is the hash of the name of the symbol in the original file. It is
  292. /// identical to the GUID for global symbols, but differs for local since the
  293. /// GUID includes the module level id in the hash.
  294. GlobalValue::GUID OriginalName = 0;
  295. /// Path of module IR containing value's definition, used to locate
  296. /// module during importing.
  297. ///
  298. /// This is only used during parsing of the combined index, or when
  299. /// parsing the per-module index for creation of the combined summary index,
  300. /// not during writing of the per-module index which doesn't contain a
  301. /// module path string table.
  302. StringRef ModulePath;
  303. /// List of values referenced by this global value's definition
  304. /// (either by the initializer of a global variable, or referenced
  305. /// from within a function). This does not include functions called, which
  306. /// are listed in the derived FunctionSummary object.
  307. std::vector<ValueInfo> RefEdgeList;
  308. protected:
  309. GlobalValueSummary(SummaryKind K, GVFlags Flags, std::vector<ValueInfo> Refs)
  310. : Kind(K), Flags(Flags), RefEdgeList(std::move(Refs)) {
  311. assert((K != AliasKind || Refs.empty()) &&
  312. "Expect no references for AliasSummary");
  313. }
  314. public:
  315. virtual ~GlobalValueSummary() = default;
  316. /// Returns the hash of the original name, it is identical to the GUID for
  317. /// externally visible symbols, but not for local ones.
  318. GlobalValue::GUID getOriginalName() const { return OriginalName; }
  319. /// Initialize the original name hash in this summary.
  320. void setOriginalName(GlobalValue::GUID Name) { OriginalName = Name; }
  321. /// Which kind of summary subclass this is.
  322. SummaryKind getSummaryKind() const { return Kind; }
  323. /// Set the path to the module containing this function, for use in
  324. /// the combined index.
  325. void setModulePath(StringRef ModPath) { ModulePath = ModPath; }
  326. /// Get the path to the module containing this function.
  327. StringRef modulePath() const { return ModulePath; }
  328. /// Get the flags for this GlobalValue (see \p struct GVFlags).
  329. GVFlags flags() const { return Flags; }
  330. /// Return linkage type recorded for this global value.
  331. GlobalValue::LinkageTypes linkage() const {
  332. return static_cast<GlobalValue::LinkageTypes>(Flags.Linkage);
  333. }
  334. /// Sets the linkage to the value determined by global summary-based
  335. /// optimization. Will be applied in the ThinLTO backends.
  336. void setLinkage(GlobalValue::LinkageTypes Linkage) {
  337. Flags.Linkage = Linkage;
  338. }
  339. /// Return true if this global value can't be imported.
  340. bool notEligibleToImport() const { return Flags.NotEligibleToImport; }
  341. bool isLive() const { return Flags.Live; }
  342. void setLive(bool Live) { Flags.Live = Live; }
  343. void setDSOLocal(bool Local) { Flags.DSOLocal = Local; }
  344. bool isDSOLocal() const { return Flags.DSOLocal; }
  345. void setCanAutoHide(bool CanAutoHide) { Flags.CanAutoHide = CanAutoHide; }
  346. bool canAutoHide() const { return Flags.CanAutoHide; }
  347. /// Flag that this global value cannot be imported.
  348. void setNotEligibleToImport() { Flags.NotEligibleToImport = true; }
  349. /// Return the list of values referenced by this global value definition.
  350. ArrayRef<ValueInfo> refs() const { return RefEdgeList; }
  351. /// If this is an alias summary, returns the summary of the aliased object (a
  352. /// global variable or function), otherwise returns itself.
  353. GlobalValueSummary *getBaseObject();
  354. const GlobalValueSummary *getBaseObject() const;
  355. friend class ModuleSummaryIndex;
  356. };
  357. /// Alias summary information.
  358. class AliasSummary : public GlobalValueSummary {
  359. ValueInfo AliaseeValueInfo;
  360. /// This is the Aliasee in the same module as alias (could get from VI, trades
  361. /// memory for time). Note that this pointer may be null (and the value info
  362. /// empty) when we have a distributed index where the alias is being imported
  363. /// (as a copy of the aliasee), but the aliasee is not.
  364. GlobalValueSummary *AliaseeSummary;
  365. public:
  366. AliasSummary(GVFlags Flags)
  367. : GlobalValueSummary(AliasKind, Flags, ArrayRef<ValueInfo>{}),
  368. AliaseeSummary(nullptr) {}
  369. /// Check if this is an alias summary.
  370. static bool classof(const GlobalValueSummary *GVS) {
  371. return GVS->getSummaryKind() == AliasKind;
  372. }
  373. void setAliasee(ValueInfo &AliaseeVI, GlobalValueSummary *Aliasee) {
  374. AliaseeValueInfo = AliaseeVI;
  375. AliaseeSummary = Aliasee;
  376. }
  377. bool hasAliasee() const {
  378. assert(!!AliaseeSummary == (AliaseeValueInfo &&
  379. !AliaseeValueInfo.getSummaryList().empty()) &&
  380. "Expect to have both aliasee summary and summary list or neither");
  381. return !!AliaseeSummary;
  382. }
  383. const GlobalValueSummary &getAliasee() const {
  384. assert(AliaseeSummary && "Unexpected missing aliasee summary");
  385. return *AliaseeSummary;
  386. }
  387. GlobalValueSummary &getAliasee() {
  388. return const_cast<GlobalValueSummary &>(
  389. static_cast<const AliasSummary *>(this)->getAliasee());
  390. }
  391. ValueInfo getAliaseeVI() const {
  392. assert(AliaseeValueInfo && "Unexpected missing aliasee");
  393. return AliaseeValueInfo;
  394. }
  395. GlobalValue::GUID getAliaseeGUID() const {
  396. assert(AliaseeValueInfo && "Unexpected missing aliasee");
  397. return AliaseeValueInfo.getGUID();
  398. }
  399. };
  400. const inline GlobalValueSummary *GlobalValueSummary::getBaseObject() const {
  401. if (auto *AS = dyn_cast<AliasSummary>(this))
  402. return &AS->getAliasee();
  403. return this;
  404. }
  405. inline GlobalValueSummary *GlobalValueSummary::getBaseObject() {
  406. if (auto *AS = dyn_cast<AliasSummary>(this))
  407. return &AS->getAliasee();
  408. return this;
  409. }
  410. /// Function summary information to aid decisions and implementation of
  411. /// importing.
  412. class FunctionSummary : public GlobalValueSummary {
  413. public:
  414. /// <CalleeValueInfo, CalleeInfo> call edge pair.
  415. using EdgeTy = std::pair<ValueInfo, CalleeInfo>;
  416. /// Types for -force-summary-edges-cold debugging option.
  417. enum ForceSummaryHotnessType : unsigned {
  418. FSHT_None,
  419. FSHT_AllNonCritical,
  420. FSHT_All
  421. };
  422. /// An "identifier" for a virtual function. This contains the type identifier
  423. /// represented as a GUID and the offset from the address point to the virtual
  424. /// function pointer, where "address point" is as defined in the Itanium ABI:
  425. /// https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-general
  426. struct VFuncId {
  427. GlobalValue::GUID GUID;
  428. uint64_t Offset;
  429. };
  430. /// A specification for a virtual function call with all constant integer
  431. /// arguments. This is used to perform virtual constant propagation on the
  432. /// summary.
  433. struct ConstVCall {
  434. VFuncId VFunc;
  435. std::vector<uint64_t> Args;
  436. };
  437. /// All type identifier related information. Because these fields are
  438. /// relatively uncommon we only allocate space for them if necessary.
  439. struct TypeIdInfo {
  440. /// List of type identifiers used by this function in llvm.type.test
  441. /// intrinsics referenced by something other than an llvm.assume intrinsic,
  442. /// represented as GUIDs.
  443. std::vector<GlobalValue::GUID> TypeTests;
  444. /// List of virtual calls made by this function using (respectively)
  445. /// llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics that do
  446. /// not have all constant integer arguments.
  447. std::vector<VFuncId> TypeTestAssumeVCalls, TypeCheckedLoadVCalls;
  448. /// List of virtual calls made by this function using (respectively)
  449. /// llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics with
  450. /// all constant integer arguments.
  451. std::vector<ConstVCall> TypeTestAssumeConstVCalls,
  452. TypeCheckedLoadConstVCalls;
  453. };
  454. /// Flags specific to function summaries.
  455. struct FFlags {
  456. // Function attribute flags. Used to track if a function accesses memory,
  457. // recurses or aliases.
  458. unsigned ReadNone : 1;
  459. unsigned ReadOnly : 1;
  460. unsigned NoRecurse : 1;
  461. unsigned ReturnDoesNotAlias : 1;
  462. // Indicate if the global value cannot be inlined.
  463. unsigned NoInline : 1;
  464. // Indicate if function should be always inlined.
  465. unsigned AlwaysInline : 1;
  466. };
  467. /// Describes the uses of a parameter by the function.
  468. struct ParamAccess {
  469. static constexpr uint32_t RangeWidth = 64;
  470. /// Describes the use of a value in a call instruction, specifying the
  471. /// call's target, the value's parameter number, and the possible range of
  472. /// offsets from the beginning of the value that are passed.
  473. struct Call {
  474. uint64_t ParamNo = 0;
  475. ValueInfo Callee;
  476. ConstantRange Offsets{/*BitWidth=*/RangeWidth, /*isFullSet=*/true};
  477. Call() = default;
  478. Call(uint64_t ParamNo, ValueInfo Callee, const ConstantRange &Offsets)
  479. : ParamNo(ParamNo), Callee(Callee), Offsets(Offsets) {}
  480. };
  481. uint64_t ParamNo = 0;
  482. /// The range contains byte offsets from the parameter pointer which
  483. /// accessed by the function. In the per-module summary, it only includes
  484. /// accesses made by the function instructions. In the combined summary, it
  485. /// also includes accesses by nested function calls.
  486. ConstantRange Use{/*BitWidth=*/RangeWidth, /*isFullSet=*/true};
  487. /// In the per-module summary, it summarizes the byte offset applied to each
  488. /// pointer parameter before passing to each corresponding callee.
  489. /// In the combined summary, it's empty and information is propagated by
  490. /// inter-procedural analysis and applied to the Use field.
  491. std::vector<Call> Calls;
  492. ParamAccess() = default;
  493. ParamAccess(uint64_t ParamNo, const ConstantRange &Use)
  494. : ParamNo(ParamNo), Use(Use) {}
  495. };
  496. /// Create an empty FunctionSummary (with specified call edges).
  497. /// Used to represent external nodes and the dummy root node.
  498. static FunctionSummary
  499. makeDummyFunctionSummary(std::vector<FunctionSummary::EdgeTy> Edges) {
  500. return FunctionSummary(
  501. FunctionSummary::GVFlags(
  502. GlobalValue::LinkageTypes::AvailableExternallyLinkage,
  503. /*NotEligibleToImport=*/true, /*Live=*/true, /*IsLocal=*/false,
  504. /*CanAutoHide=*/false),
  505. /*NumInsts=*/0, FunctionSummary::FFlags{}, /*EntryCount=*/0,
  506. std::vector<ValueInfo>(), std::move(Edges),
  507. std::vector<GlobalValue::GUID>(),
  508. std::vector<FunctionSummary::VFuncId>(),
  509. std::vector<FunctionSummary::VFuncId>(),
  510. std::vector<FunctionSummary::ConstVCall>(),
  511. std::vector<FunctionSummary::ConstVCall>(),
  512. std::vector<FunctionSummary::ParamAccess>());
  513. }
  514. /// A dummy node to reference external functions that aren't in the index
  515. static FunctionSummary ExternalNode;
  516. private:
  517. /// Number of instructions (ignoring debug instructions, e.g.) computed
  518. /// during the initial compile step when the summary index is first built.
  519. unsigned InstCount;
  520. /// Function summary specific flags.
  521. FFlags FunFlags;
  522. /// The synthesized entry count of the function.
  523. /// This is only populated during ThinLink phase and remains unused while
  524. /// generating per-module summaries.
  525. uint64_t EntryCount = 0;
  526. /// List of <CalleeValueInfo, CalleeInfo> call edge pairs from this function.
  527. std::vector<EdgeTy> CallGraphEdgeList;
  528. std::unique_ptr<TypeIdInfo> TIdInfo;
  529. /// Uses for every parameter to this function.
  530. using ParamAccessesTy = std::vector<ParamAccess>;
  531. std::unique_ptr<ParamAccessesTy> ParamAccesses;
  532. public:
  533. FunctionSummary(GVFlags Flags, unsigned NumInsts, FFlags FunFlags,
  534. uint64_t EntryCount, std::vector<ValueInfo> Refs,
  535. std::vector<EdgeTy> CGEdges,
  536. std::vector<GlobalValue::GUID> TypeTests,
  537. std::vector<VFuncId> TypeTestAssumeVCalls,
  538. std::vector<VFuncId> TypeCheckedLoadVCalls,
  539. std::vector<ConstVCall> TypeTestAssumeConstVCalls,
  540. std::vector<ConstVCall> TypeCheckedLoadConstVCalls,
  541. std::vector<ParamAccess> Params)
  542. : GlobalValueSummary(FunctionKind, Flags, std::move(Refs)),
  543. InstCount(NumInsts), FunFlags(FunFlags), EntryCount(EntryCount),
  544. CallGraphEdgeList(std::move(CGEdges)) {
  545. if (!TypeTests.empty() || !TypeTestAssumeVCalls.empty() ||
  546. !TypeCheckedLoadVCalls.empty() || !TypeTestAssumeConstVCalls.empty() ||
  547. !TypeCheckedLoadConstVCalls.empty())
  548. TIdInfo = std::make_unique<TypeIdInfo>(
  549. TypeIdInfo{std::move(TypeTests), std::move(TypeTestAssumeVCalls),
  550. std::move(TypeCheckedLoadVCalls),
  551. std::move(TypeTestAssumeConstVCalls),
  552. std::move(TypeCheckedLoadConstVCalls)});
  553. if (!Params.empty())
  554. ParamAccesses = std::make_unique<ParamAccessesTy>(std::move(Params));
  555. }
  556. // Gets the number of readonly and writeonly refs in RefEdgeList
  557. std::pair<unsigned, unsigned> specialRefCounts() const;
  558. /// Check if this is a function summary.
  559. static bool classof(const GlobalValueSummary *GVS) {
  560. return GVS->getSummaryKind() == FunctionKind;
  561. }
  562. /// Get function summary flags.
  563. FFlags fflags() const { return FunFlags; }
  564. /// Get the instruction count recorded for this function.
  565. unsigned instCount() const { return InstCount; }
  566. /// Get the synthetic entry count for this function.
  567. uint64_t entryCount() const { return EntryCount; }
  568. /// Set the synthetic entry count for this function.
  569. void setEntryCount(uint64_t EC) { EntryCount = EC; }
  570. /// Return the list of <CalleeValueInfo, CalleeInfo> pairs.
  571. ArrayRef<EdgeTy> calls() const { return CallGraphEdgeList; }
  572. void addCall(EdgeTy E) { CallGraphEdgeList.push_back(E); }
  573. /// Returns the list of type identifiers used by this function in
  574. /// llvm.type.test intrinsics other than by an llvm.assume intrinsic,
  575. /// represented as GUIDs.
  576. ArrayRef<GlobalValue::GUID> type_tests() const {
  577. if (TIdInfo)
  578. return TIdInfo->TypeTests;
  579. return {};
  580. }
  581. /// Returns the list of virtual calls made by this function using
  582. /// llvm.assume(llvm.type.test) intrinsics that do not have all constant
  583. /// integer arguments.
  584. ArrayRef<VFuncId> type_test_assume_vcalls() const {
  585. if (TIdInfo)
  586. return TIdInfo->TypeTestAssumeVCalls;
  587. return {};
  588. }
  589. /// Returns the list of virtual calls made by this function using
  590. /// llvm.type.checked.load intrinsics that do not have all constant integer
  591. /// arguments.
  592. ArrayRef<VFuncId> type_checked_load_vcalls() const {
  593. if (TIdInfo)
  594. return TIdInfo->TypeCheckedLoadVCalls;
  595. return {};
  596. }
  597. /// Returns the list of virtual calls made by this function using
  598. /// llvm.assume(llvm.type.test) intrinsics with all constant integer
  599. /// arguments.
  600. ArrayRef<ConstVCall> type_test_assume_const_vcalls() const {
  601. if (TIdInfo)
  602. return TIdInfo->TypeTestAssumeConstVCalls;
  603. return {};
  604. }
  605. /// Returns the list of virtual calls made by this function using
  606. /// llvm.type.checked.load intrinsics with all constant integer arguments.
  607. ArrayRef<ConstVCall> type_checked_load_const_vcalls() const {
  608. if (TIdInfo)
  609. return TIdInfo->TypeCheckedLoadConstVCalls;
  610. return {};
  611. }
  612. /// Returns the list of known uses of pointer parameters.
  613. ArrayRef<ParamAccess> paramAccesses() const {
  614. if (ParamAccesses)
  615. return *ParamAccesses;
  616. return {};
  617. }
  618. /// Sets the list of known uses of pointer parameters.
  619. void setParamAccesses(std::vector<ParamAccess> NewParams) {
  620. if (NewParams.empty())
  621. ParamAccesses.reset();
  622. else if (ParamAccesses)
  623. *ParamAccesses = std::move(NewParams);
  624. else
  625. ParamAccesses = std::make_unique<ParamAccessesTy>(std::move(NewParams));
  626. }
  627. /// Add a type test to the summary. This is used by WholeProgramDevirt if we
  628. /// were unable to devirtualize a checked call.
  629. void addTypeTest(GlobalValue::GUID Guid) {
  630. if (!TIdInfo)
  631. TIdInfo = std::make_unique<TypeIdInfo>();
  632. TIdInfo->TypeTests.push_back(Guid);
  633. }
  634. const TypeIdInfo *getTypeIdInfo() const { return TIdInfo.get(); };
  635. friend struct GraphTraits<ValueInfo>;
  636. };
  637. template <> struct DenseMapInfo<FunctionSummary::VFuncId> {
  638. static FunctionSummary::VFuncId getEmptyKey() { return {0, uint64_t(-1)}; }
  639. static FunctionSummary::VFuncId getTombstoneKey() {
  640. return {0, uint64_t(-2)};
  641. }
  642. static bool isEqual(FunctionSummary::VFuncId L, FunctionSummary::VFuncId R) {
  643. return L.GUID == R.GUID && L.Offset == R.Offset;
  644. }
  645. static unsigned getHashValue(FunctionSummary::VFuncId I) { return I.GUID; }
  646. };
  647. template <> struct DenseMapInfo<FunctionSummary::ConstVCall> {
  648. static FunctionSummary::ConstVCall getEmptyKey() {
  649. return {{0, uint64_t(-1)}, {}};
  650. }
  651. static FunctionSummary::ConstVCall getTombstoneKey() {
  652. return {{0, uint64_t(-2)}, {}};
  653. }
  654. static bool isEqual(FunctionSummary::ConstVCall L,
  655. FunctionSummary::ConstVCall R) {
  656. return DenseMapInfo<FunctionSummary::VFuncId>::isEqual(L.VFunc, R.VFunc) &&
  657. L.Args == R.Args;
  658. }
  659. static unsigned getHashValue(FunctionSummary::ConstVCall I) {
  660. return I.VFunc.GUID;
  661. }
  662. };
  663. /// The ValueInfo and offset for a function within a vtable definition
  664. /// initializer array.
  665. struct VirtFuncOffset {
  666. VirtFuncOffset(ValueInfo VI, uint64_t Offset)
  667. : FuncVI(VI), VTableOffset(Offset) {}
  668. ValueInfo FuncVI;
  669. uint64_t VTableOffset;
  670. };
  671. /// List of functions referenced by a particular vtable definition.
  672. using VTableFuncList = std::vector<VirtFuncOffset>;
  673. /// Global variable summary information to aid decisions and
  674. /// implementation of importing.
  675. ///
  676. /// Global variable summary has two extra flag, telling if it is
  677. /// readonly or writeonly. Both readonly and writeonly variables
  678. /// can be optimized in the backed: readonly variables can be
  679. /// const-folded, while writeonly vars can be completely eliminated
  680. /// together with corresponding stores. We let both things happen
  681. /// by means of internalizing such variables after ThinLTO import.
  682. class GlobalVarSummary : public GlobalValueSummary {
  683. private:
  684. /// For vtable definitions this holds the list of functions and
  685. /// their corresponding offsets within the initializer array.
  686. std::unique_ptr<VTableFuncList> VTableFuncs;
  687. public:
  688. struct GVarFlags {
  689. GVarFlags(bool ReadOnly, bool WriteOnly, bool Constant,
  690. GlobalObject::VCallVisibility Vis)
  691. : MaybeReadOnly(ReadOnly), MaybeWriteOnly(WriteOnly),
  692. Constant(Constant), VCallVisibility(Vis) {}
  693. // If true indicates that this global variable might be accessed
  694. // purely by non-volatile load instructions. This in turn means
  695. // it can be internalized in source and destination modules during
  696. // thin LTO import because it neither modified nor its address
  697. // is taken.
  698. unsigned MaybeReadOnly : 1;
  699. // If true indicates that variable is possibly only written to, so
  700. // its value isn't loaded and its address isn't taken anywhere.
  701. // False, when 'Constant' attribute is set.
  702. unsigned MaybeWriteOnly : 1;
  703. // Indicates that value is a compile-time constant. Global variable
  704. // can be 'Constant' while not being 'ReadOnly' on several occasions:
  705. // - it is volatile, (e.g mapped device address)
  706. // - its address is taken, meaning that unlike 'ReadOnly' vars we can't
  707. // internalize it.
  708. // Constant variables are always imported thus giving compiler an
  709. // opportunity to make some extra optimizations. Readonly constants
  710. // are also internalized.
  711. unsigned Constant : 1;
  712. // Set from metadata on vtable definitions during the module summary
  713. // analysis.
  714. unsigned VCallVisibility : 2;
  715. } VarFlags;
  716. GlobalVarSummary(GVFlags Flags, GVarFlags VarFlags,
  717. std::vector<ValueInfo> Refs)
  718. : GlobalValueSummary(GlobalVarKind, Flags, std::move(Refs)),
  719. VarFlags(VarFlags) {}
  720. /// Check if this is a global variable summary.
  721. static bool classof(const GlobalValueSummary *GVS) {
  722. return GVS->getSummaryKind() == GlobalVarKind;
  723. }
  724. GVarFlags varflags() const { return VarFlags; }
  725. void setReadOnly(bool RO) { VarFlags.MaybeReadOnly = RO; }
  726. void setWriteOnly(bool WO) { VarFlags.MaybeWriteOnly = WO; }
  727. bool maybeReadOnly() const { return VarFlags.MaybeReadOnly; }
  728. bool maybeWriteOnly() const { return VarFlags.MaybeWriteOnly; }
  729. bool isConstant() const { return VarFlags.Constant; }
  730. void setVCallVisibility(GlobalObject::VCallVisibility Vis) {
  731. VarFlags.VCallVisibility = Vis;
  732. }
  733. GlobalObject::VCallVisibility getVCallVisibility() const {
  734. return (GlobalObject::VCallVisibility)VarFlags.VCallVisibility;
  735. }
  736. void setVTableFuncs(VTableFuncList Funcs) {
  737. assert(!VTableFuncs);
  738. VTableFuncs = std::make_unique<VTableFuncList>(std::move(Funcs));
  739. }
  740. ArrayRef<VirtFuncOffset> vTableFuncs() const {
  741. if (VTableFuncs)
  742. return *VTableFuncs;
  743. return {};
  744. }
  745. };
  746. struct TypeTestResolution {
  747. /// Specifies which kind of type check we should emit for this byte array.
  748. /// See http://clang.llvm.org/docs/ControlFlowIntegrityDesign.html for full
  749. /// details on each kind of check; the enumerators are described with
  750. /// reference to that document.
  751. enum Kind {
  752. Unsat, ///< Unsatisfiable type (i.e. no global has this type metadata)
  753. ByteArray, ///< Test a byte array (first example)
  754. Inline, ///< Inlined bit vector ("Short Inline Bit Vectors")
  755. Single, ///< Single element (last example in "Short Inline Bit Vectors")
  756. AllOnes, ///< All-ones bit vector ("Eliminating Bit Vector Checks for
  757. /// All-Ones Bit Vectors")
  758. Unknown, ///< Unknown (analysis not performed, don't lower)
  759. } TheKind = Unknown;
  760. /// Range of size-1 expressed as a bit width. For example, if the size is in
  761. /// range [1,256], this number will be 8. This helps generate the most compact
  762. /// instruction sequences.
  763. unsigned SizeM1BitWidth = 0;
  764. // The following fields are only used if the target does not support the use
  765. // of absolute symbols to store constants. Their meanings are the same as the
  766. // corresponding fields in LowerTypeTestsModule::TypeIdLowering in
  767. // LowerTypeTests.cpp.
  768. uint64_t AlignLog2 = 0;
  769. uint64_t SizeM1 = 0;
  770. uint8_t BitMask = 0;
  771. uint64_t InlineBits = 0;
  772. };
  773. struct WholeProgramDevirtResolution {
  774. enum Kind {
  775. Indir, ///< Just do a regular virtual call
  776. SingleImpl, ///< Single implementation devirtualization
  777. BranchFunnel, ///< When retpoline mitigation is enabled, use a branch funnel
  778. ///< that is defined in the merged module. Otherwise same as
  779. ///< Indir.
  780. } TheKind = Indir;
  781. std::string SingleImplName;
  782. struct ByArg {
  783. enum Kind {
  784. Indir, ///< Just do a regular virtual call
  785. UniformRetVal, ///< Uniform return value optimization
  786. UniqueRetVal, ///< Unique return value optimization
  787. VirtualConstProp, ///< Virtual constant propagation
  788. } TheKind = Indir;
  789. /// Additional information for the resolution:
  790. /// - UniformRetVal: the uniform return value.
  791. /// - UniqueRetVal: the return value associated with the unique vtable (0 or
  792. /// 1).
  793. uint64_t Info = 0;
  794. // The following fields are only used if the target does not support the use
  795. // of absolute symbols to store constants.
  796. uint32_t Byte = 0;
  797. uint32_t Bit = 0;
  798. };
  799. /// Resolutions for calls with all constant integer arguments (excluding the
  800. /// first argument, "this"), where the key is the argument vector.
  801. std::map<std::vector<uint64_t>, ByArg> ResByArg;
  802. };
  803. struct TypeIdSummary {
  804. TypeTestResolution TTRes;
  805. /// Mapping from byte offset to whole-program devirt resolution for that
  806. /// (typeid, byte offset) pair.
  807. std::map<uint64_t, WholeProgramDevirtResolution> WPDRes;
  808. };
  809. /// 160 bits SHA1
  810. using ModuleHash = std::array<uint32_t, 5>;
  811. /// Type used for iterating through the global value summary map.
  812. using const_gvsummary_iterator = GlobalValueSummaryMapTy::const_iterator;
  813. using gvsummary_iterator = GlobalValueSummaryMapTy::iterator;
  814. /// String table to hold/own module path strings, which additionally holds the
  815. /// module ID assigned to each module during the plugin step, as well as a hash
  816. /// of the module. The StringMap makes a copy of and owns inserted strings.
  817. using ModulePathStringTableTy = StringMap<std::pair<uint64_t, ModuleHash>>;
  818. /// Map of global value GUID to its summary, used to identify values defined in
  819. /// a particular module, and provide efficient access to their summary.
  820. using GVSummaryMapTy = DenseMap<GlobalValue::GUID, GlobalValueSummary *>;
  821. /// Map of a type GUID to type id string and summary (multimap used
  822. /// in case of GUID conflicts).
  823. using TypeIdSummaryMapTy =
  824. std::multimap<GlobalValue::GUID, std::pair<std::string, TypeIdSummary>>;
  825. /// The following data structures summarize type metadata information.
  826. /// For type metadata overview see https://llvm.org/docs/TypeMetadata.html.
  827. /// Each type metadata includes both the type identifier and the offset of
  828. /// the address point of the type (the address held by objects of that type
  829. /// which may not be the beginning of the virtual table). Vtable definitions
  830. /// are decorated with type metadata for the types they are compatible with.
  831. ///
  832. /// Holds information about vtable definitions decorated with type metadata:
  833. /// the vtable definition value and its address point offset in a type
  834. /// identifier metadata it is decorated (compatible) with.
  835. struct TypeIdOffsetVtableInfo {
  836. TypeIdOffsetVtableInfo(uint64_t Offset, ValueInfo VI)
  837. : AddressPointOffset(Offset), VTableVI(VI) {}
  838. uint64_t AddressPointOffset;
  839. ValueInfo VTableVI;
  840. };
  841. /// List of vtable definitions decorated by a particular type identifier,
  842. /// and their corresponding offsets in that type identifier's metadata.
  843. /// Note that each type identifier may be compatible with multiple vtables, due
  844. /// to inheritance, which is why this is a vector.
  845. using TypeIdCompatibleVtableInfo = std::vector<TypeIdOffsetVtableInfo>;
  846. /// Class to hold module path string table and global value map,
  847. /// and encapsulate methods for operating on them.
  848. class ModuleSummaryIndex {
  849. private:
  850. /// Map from value name to list of summary instances for values of that
  851. /// name (may be duplicates in the COMDAT case, e.g.).
  852. GlobalValueSummaryMapTy GlobalValueMap;
  853. /// Holds strings for combined index, mapping to the corresponding module ID.
  854. ModulePathStringTableTy ModulePathStringTable;
  855. /// Mapping from type identifier GUIDs to type identifier and its summary
  856. /// information. Produced by thin link.
  857. TypeIdSummaryMapTy TypeIdMap;
  858. /// Mapping from type identifier to information about vtables decorated
  859. /// with that type identifier's metadata. Produced by per module summary
  860. /// analysis and consumed by thin link. For more information, see description
  861. /// above where TypeIdCompatibleVtableInfo is defined.
  862. std::map<std::string, TypeIdCompatibleVtableInfo, std::less<>>
  863. TypeIdCompatibleVtableMap;
  864. /// Mapping from original ID to GUID. If original ID can map to multiple
  865. /// GUIDs, it will be mapped to 0.
  866. std::map<GlobalValue::GUID, GlobalValue::GUID> OidGuidMap;
  867. /// Indicates that summary-based GlobalValue GC has run, and values with
  868. /// GVFlags::Live==false are really dead. Otherwise, all values must be
  869. /// considered live.
  870. bool WithGlobalValueDeadStripping = false;
  871. /// Indicates that summary-based attribute propagation has run and
  872. /// GVarFlags::MaybeReadonly / GVarFlags::MaybeWriteonly are really
  873. /// read/write only.
  874. bool WithAttributePropagation = false;
  875. /// Indicates that summary-based synthetic entry count propagation has run
  876. bool HasSyntheticEntryCounts = false;
  877. /// Indicates that distributed backend should skip compilation of the
  878. /// module. Flag is suppose to be set by distributed ThinLTO indexing
  879. /// when it detected that the module is not needed during the final
  880. /// linking. As result distributed backend should just output a minimal
  881. /// valid object file.
  882. bool SkipModuleByDistributedBackend = false;
  883. /// If true then we're performing analysis of IR module, or parsing along with
  884. /// the IR from assembly. The value of 'false' means we're reading summary
  885. /// from BC or YAML source. Affects the type of value stored in NameOrGV
  886. /// union.
  887. bool HaveGVs;
  888. // True if the index was created for a module compiled with -fsplit-lto-unit.
  889. bool EnableSplitLTOUnit;
  890. // True if some of the modules were compiled with -fsplit-lto-unit and
  891. // some were not. Set when the combined index is created during the thin link.
  892. bool PartiallySplitLTOUnits = false;
  893. /// True if some of the FunctionSummary contains a ParamAccess.
  894. bool HasParamAccess = false;
  895. std::set<std::string> CfiFunctionDefs;
  896. std::set<std::string> CfiFunctionDecls;
  897. // Used in cases where we want to record the name of a global, but
  898. // don't have the string owned elsewhere (e.g. the Strtab on a module).
  899. StringSaver Saver;
  900. BumpPtrAllocator Alloc;
  901. // The total number of basic blocks in the module in the per-module summary or
  902. // the total number of basic blocks in the LTO unit in the combined index.
  903. uint64_t BlockCount;
  904. // YAML I/O support.
  905. friend yaml::MappingTraits<ModuleSummaryIndex>;
  906. GlobalValueSummaryMapTy::value_type *
  907. getOrInsertValuePtr(GlobalValue::GUID GUID) {
  908. return &*GlobalValueMap.emplace(GUID, GlobalValueSummaryInfo(HaveGVs))
  909. .first;
  910. }
  911. public:
  912. // See HaveGVs variable comment.
  913. ModuleSummaryIndex(bool HaveGVs, bool EnableSplitLTOUnit = false)
  914. : HaveGVs(HaveGVs), EnableSplitLTOUnit(EnableSplitLTOUnit), Saver(Alloc),
  915. BlockCount(0) {}
  916. // Current version for the module summary in bitcode files.
  917. // The BitcodeSummaryVersion should be bumped whenever we introduce changes
  918. // in the way some record are interpreted, like flags for instance.
  919. // Note that incrementing this may require changes in both BitcodeReader.cpp
  920. // and BitcodeWriter.cpp.
  921. static constexpr uint64_t BitcodeSummaryVersion = 9;
  922. // Regular LTO module name for ASM writer
  923. static constexpr const char *getRegularLTOModuleName() {
  924. return "[Regular LTO]";
  925. }
  926. bool haveGVs() const { return HaveGVs; }
  927. uint64_t getFlags() const;
  928. void setFlags(uint64_t Flags);
  929. uint64_t getBlockCount() const { return BlockCount; }
  930. void addBlockCount(uint64_t C) { BlockCount += C; }
  931. void setBlockCount(uint64_t C) { BlockCount = C; }
  932. gvsummary_iterator begin() { return GlobalValueMap.begin(); }
  933. const_gvsummary_iterator begin() const { return GlobalValueMap.begin(); }
  934. gvsummary_iterator end() { return GlobalValueMap.end(); }
  935. const_gvsummary_iterator end() const { return GlobalValueMap.end(); }
  936. size_t size() const { return GlobalValueMap.size(); }
  937. /// Convenience function for doing a DFS on a ValueInfo. Marks the function in
  938. /// the FunctionHasParent map.
  939. static void discoverNodes(ValueInfo V,
  940. std::map<ValueInfo, bool> &FunctionHasParent) {
  941. if (!V.getSummaryList().size())
  942. return; // skip external functions that don't have summaries
  943. // Mark discovered if we haven't yet
  944. auto S = FunctionHasParent.emplace(V, false);
  945. // Stop if we've already discovered this node
  946. if (!S.second)
  947. return;
  948. FunctionSummary *F =
  949. dyn_cast<FunctionSummary>(V.getSummaryList().front().get());
  950. assert(F != nullptr && "Expected FunctionSummary node");
  951. for (auto &C : F->calls()) {
  952. // Insert node if necessary
  953. auto S = FunctionHasParent.emplace(C.first, true);
  954. // Skip nodes that we're sure have parents
  955. if (!S.second && S.first->second)
  956. continue;
  957. if (S.second)
  958. discoverNodes(C.first, FunctionHasParent);
  959. else
  960. S.first->second = true;
  961. }
  962. }
  963. // Calculate the callgraph root
  964. FunctionSummary calculateCallGraphRoot() {
  965. // Functions that have a parent will be marked in FunctionHasParent pair.
  966. // Once we've marked all functions, the functions in the map that are false
  967. // have no parent (so they're the roots)
  968. std::map<ValueInfo, bool> FunctionHasParent;
  969. for (auto &S : *this) {
  970. // Skip external functions
  971. if (!S.second.SummaryList.size() ||
  972. !isa<FunctionSummary>(S.second.SummaryList.front().get()))
  973. continue;
  974. discoverNodes(ValueInfo(HaveGVs, &S), FunctionHasParent);
  975. }
  976. std::vector<FunctionSummary::EdgeTy> Edges;
  977. // create edges to all roots in the Index
  978. for (auto &P : FunctionHasParent) {
  979. if (P.second)
  980. continue; // skip over non-root nodes
  981. Edges.push_back(std::make_pair(P.first, CalleeInfo{}));
  982. }
  983. if (Edges.empty()) {
  984. // Failed to find root - return an empty node
  985. return FunctionSummary::makeDummyFunctionSummary({});
  986. }
  987. auto CallGraphRoot = FunctionSummary::makeDummyFunctionSummary(Edges);
  988. return CallGraphRoot;
  989. }
  990. bool withGlobalValueDeadStripping() const {
  991. return WithGlobalValueDeadStripping;
  992. }
  993. void setWithGlobalValueDeadStripping() {
  994. WithGlobalValueDeadStripping = true;
  995. }
  996. bool withAttributePropagation() const { return WithAttributePropagation; }
  997. void setWithAttributePropagation() {
  998. WithAttributePropagation = true;
  999. }
  1000. bool isReadOnly(const GlobalVarSummary *GVS) const {
  1001. return WithAttributePropagation && GVS->maybeReadOnly();
  1002. }
  1003. bool isWriteOnly(const GlobalVarSummary *GVS) const {
  1004. return WithAttributePropagation && GVS->maybeWriteOnly();
  1005. }
  1006. bool hasSyntheticEntryCounts() const { return HasSyntheticEntryCounts; }
  1007. void setHasSyntheticEntryCounts() { HasSyntheticEntryCounts = true; }
  1008. bool skipModuleByDistributedBackend() const {
  1009. return SkipModuleByDistributedBackend;
  1010. }
  1011. void setSkipModuleByDistributedBackend() {
  1012. SkipModuleByDistributedBackend = true;
  1013. }
  1014. bool enableSplitLTOUnit() const { return EnableSplitLTOUnit; }
  1015. void setEnableSplitLTOUnit() { EnableSplitLTOUnit = true; }
  1016. bool partiallySplitLTOUnits() const { return PartiallySplitLTOUnits; }
  1017. void setPartiallySplitLTOUnits() { PartiallySplitLTOUnits = true; }
  1018. bool hasParamAccess() const { return HasParamAccess; }
  1019. bool isGlobalValueLive(const GlobalValueSummary *GVS) const {
  1020. return !WithGlobalValueDeadStripping || GVS->isLive();
  1021. }
  1022. bool isGUIDLive(GlobalValue::GUID GUID) const;
  1023. /// Return a ValueInfo for the index value_type (convenient when iterating
  1024. /// index).
  1025. ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const {
  1026. return ValueInfo(HaveGVs, &R);
  1027. }
  1028. /// Return a ValueInfo for GUID if it exists, otherwise return ValueInfo().
  1029. ValueInfo getValueInfo(GlobalValue::GUID GUID) const {
  1030. auto I = GlobalValueMap.find(GUID);
  1031. return ValueInfo(HaveGVs, I == GlobalValueMap.end() ? nullptr : &*I);
  1032. }
  1033. /// Return a ValueInfo for \p GUID.
  1034. ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID) {
  1035. return ValueInfo(HaveGVs, getOrInsertValuePtr(GUID));
  1036. }
  1037. // Save a string in the Index. Use before passing Name to
  1038. // getOrInsertValueInfo when the string isn't owned elsewhere (e.g. on the
  1039. // module's Strtab).
  1040. StringRef saveString(StringRef String) { return Saver.save(String); }
  1041. /// Return a ValueInfo for \p GUID setting value \p Name.
  1042. ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID, StringRef Name) {
  1043. assert(!HaveGVs);
  1044. auto VP = getOrInsertValuePtr(GUID);
  1045. VP->second.U.Name = Name;
  1046. return ValueInfo(HaveGVs, VP);
  1047. }
  1048. /// Return a ValueInfo for \p GV and mark it as belonging to GV.
  1049. ValueInfo getOrInsertValueInfo(const GlobalValue *GV) {
  1050. assert(HaveGVs);
  1051. auto VP = getOrInsertValuePtr(GV->getGUID());
  1052. VP->second.U.GV = GV;
  1053. return ValueInfo(HaveGVs, VP);
  1054. }
  1055. /// Return the GUID for \p OriginalId in the OidGuidMap.
  1056. GlobalValue::GUID getGUIDFromOriginalID(GlobalValue::GUID OriginalID) const {
  1057. const auto I = OidGuidMap.find(OriginalID);
  1058. return I == OidGuidMap.end() ? 0 : I->second;
  1059. }
  1060. std::set<std::string> &cfiFunctionDefs() { return CfiFunctionDefs; }
  1061. const std::set<std::string> &cfiFunctionDefs() const { return CfiFunctionDefs; }
  1062. std::set<std::string> &cfiFunctionDecls() { return CfiFunctionDecls; }
  1063. const std::set<std::string> &cfiFunctionDecls() const { return CfiFunctionDecls; }
  1064. /// Add a global value summary for a value.
  1065. void addGlobalValueSummary(const GlobalValue &GV,
  1066. std::unique_ptr<GlobalValueSummary> Summary) {
  1067. addGlobalValueSummary(getOrInsertValueInfo(&GV), std::move(Summary));
  1068. }
  1069. /// Add a global value summary for a value of the given name.
  1070. void addGlobalValueSummary(StringRef ValueName,
  1071. std::unique_ptr<GlobalValueSummary> Summary) {
  1072. addGlobalValueSummary(getOrInsertValueInfo(GlobalValue::getGUID(ValueName)),
  1073. std::move(Summary));
  1074. }
  1075. /// Add a global value summary for the given ValueInfo.
  1076. void addGlobalValueSummary(ValueInfo VI,
  1077. std::unique_ptr<GlobalValueSummary> Summary) {
  1078. if (const FunctionSummary *FS = dyn_cast<FunctionSummary>(Summary.get()))
  1079. HasParamAccess |= !FS->paramAccesses().empty();
  1080. addOriginalName(VI.getGUID(), Summary->getOriginalName());
  1081. // Here we have a notionally const VI, but the value it points to is owned
  1082. // by the non-const *this.
  1083. const_cast<GlobalValueSummaryMapTy::value_type *>(VI.getRef())
  1084. ->second.SummaryList.push_back(std::move(Summary));
  1085. }
  1086. /// Add an original name for the value of the given GUID.
  1087. void addOriginalName(GlobalValue::GUID ValueGUID,
  1088. GlobalValue::GUID OrigGUID) {
  1089. if (OrigGUID == 0 || ValueGUID == OrigGUID)
  1090. return;
  1091. if (OidGuidMap.count(OrigGUID) && OidGuidMap[OrigGUID] != ValueGUID)
  1092. OidGuidMap[OrigGUID] = 0;
  1093. else
  1094. OidGuidMap[OrigGUID] = ValueGUID;
  1095. }
  1096. /// Find the summary for ValueInfo \p VI in module \p ModuleId, or nullptr if
  1097. /// not found.
  1098. GlobalValueSummary *findSummaryInModule(ValueInfo VI, StringRef ModuleId) const {
  1099. auto SummaryList = VI.getSummaryList();
  1100. auto Summary =
  1101. llvm::find_if(SummaryList,
  1102. [&](const std::unique_ptr<GlobalValueSummary> &Summary) {
  1103. return Summary->modulePath() == ModuleId;
  1104. });
  1105. if (Summary == SummaryList.end())
  1106. return nullptr;
  1107. return Summary->get();
  1108. }
  1109. /// Find the summary for global \p GUID in module \p ModuleId, or nullptr if
  1110. /// not found.
  1111. GlobalValueSummary *findSummaryInModule(GlobalValue::GUID ValueGUID,
  1112. StringRef ModuleId) const {
  1113. auto CalleeInfo = getValueInfo(ValueGUID);
  1114. if (!CalleeInfo)
  1115. return nullptr; // This function does not have a summary
  1116. return findSummaryInModule(CalleeInfo, ModuleId);
  1117. }
  1118. /// Returns the first GlobalValueSummary for \p GV, asserting that there
  1119. /// is only one if \p PerModuleIndex.
  1120. GlobalValueSummary *getGlobalValueSummary(const GlobalValue &GV,
  1121. bool PerModuleIndex = true) const {
  1122. assert(GV.hasName() && "Can't get GlobalValueSummary for GV with no name");
  1123. return getGlobalValueSummary(GV.getGUID(), PerModuleIndex);
  1124. }
  1125. /// Returns the first GlobalValueSummary for \p ValueGUID, asserting that
  1126. /// there
  1127. /// is only one if \p PerModuleIndex.
  1128. GlobalValueSummary *getGlobalValueSummary(GlobalValue::GUID ValueGUID,
  1129. bool PerModuleIndex = true) const;
  1130. /// Table of modules, containing module hash and id.
  1131. const StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() const {
  1132. return ModulePathStringTable;
  1133. }
  1134. /// Table of modules, containing hash and id.
  1135. StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() {
  1136. return ModulePathStringTable;
  1137. }
  1138. /// Get the module ID recorded for the given module path.
  1139. uint64_t getModuleId(const StringRef ModPath) const {
  1140. return ModulePathStringTable.lookup(ModPath).first;
  1141. }
  1142. /// Get the module SHA1 hash recorded for the given module path.
  1143. const ModuleHash &getModuleHash(const StringRef ModPath) const {
  1144. auto It = ModulePathStringTable.find(ModPath);
  1145. assert(It != ModulePathStringTable.end() && "Module not registered");
  1146. return It->second.second;
  1147. }
  1148. /// Convenience method for creating a promoted global name
  1149. /// for the given value name of a local, and its original module's ID.
  1150. static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash) {
  1151. SmallString<256> NewName(Name);
  1152. NewName += ".llvm.";
  1153. NewName += utostr((uint64_t(ModHash[0]) << 32) |
  1154. ModHash[1]); // Take the first 64 bits
  1155. return std::string(NewName.str());
  1156. }
  1157. /// Helper to obtain the unpromoted name for a global value (or the original
  1158. /// name if not promoted). Split off the rightmost ".llvm.${hash}" suffix,
  1159. /// because it is possible in certain clients (not clang at the moment) for
  1160. /// two rounds of ThinLTO optimization and therefore promotion to occur.
  1161. static StringRef getOriginalNameBeforePromote(StringRef Name) {
  1162. std::pair<StringRef, StringRef> Pair = Name.rsplit(".llvm.");
  1163. return Pair.first;
  1164. }
  1165. typedef ModulePathStringTableTy::value_type ModuleInfo;
  1166. /// Add a new module with the given \p Hash, mapped to the given \p
  1167. /// ModID, and return a reference to the module.
  1168. ModuleInfo *addModule(StringRef ModPath, uint64_t ModId,
  1169. ModuleHash Hash = ModuleHash{{0}}) {
  1170. return &*ModulePathStringTable.insert({ModPath, {ModId, Hash}}).first;
  1171. }
  1172. /// Return module entry for module with the given \p ModPath.
  1173. ModuleInfo *getModule(StringRef ModPath) {
  1174. auto It = ModulePathStringTable.find(ModPath);
  1175. assert(It != ModulePathStringTable.end() && "Module not registered");
  1176. return &*It;
  1177. }
  1178. /// Check if the given Module has any functions available for exporting
  1179. /// in the index. We consider any module present in the ModulePathStringTable
  1180. /// to have exported functions.
  1181. bool hasExportedFunctions(const Module &M) const {
  1182. return ModulePathStringTable.count(M.getModuleIdentifier());
  1183. }
  1184. const TypeIdSummaryMapTy &typeIds() const { return TypeIdMap; }
  1185. /// Return an existing or new TypeIdSummary entry for \p TypeId.
  1186. /// This accessor can mutate the map and therefore should not be used in
  1187. /// the ThinLTO backends.
  1188. TypeIdSummary &getOrInsertTypeIdSummary(StringRef TypeId) {
  1189. auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId));
  1190. for (auto It = TidIter.first; It != TidIter.second; ++It)
  1191. if (It->second.first == TypeId)
  1192. return It->second.second;
  1193. auto It = TypeIdMap.insert(
  1194. {GlobalValue::getGUID(TypeId), {std::string(TypeId), TypeIdSummary()}});
  1195. return It->second.second;
  1196. }
  1197. /// This returns either a pointer to the type id summary (if present in the
  1198. /// summary map) or null (if not present). This may be used when importing.
  1199. const TypeIdSummary *getTypeIdSummary(StringRef TypeId) const {
  1200. auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId));
  1201. for (auto It = TidIter.first; It != TidIter.second; ++It)
  1202. if (It->second.first == TypeId)
  1203. return &It->second.second;
  1204. return nullptr;
  1205. }
  1206. TypeIdSummary *getTypeIdSummary(StringRef TypeId) {
  1207. return const_cast<TypeIdSummary *>(
  1208. static_cast<const ModuleSummaryIndex *>(this)->getTypeIdSummary(
  1209. TypeId));
  1210. }
  1211. const auto &typeIdCompatibleVtableMap() const {
  1212. return TypeIdCompatibleVtableMap;
  1213. }
  1214. /// Return an existing or new TypeIdCompatibleVtableMap entry for \p TypeId.
  1215. /// This accessor can mutate the map and therefore should not be used in
  1216. /// the ThinLTO backends.
  1217. TypeIdCompatibleVtableInfo &
  1218. getOrInsertTypeIdCompatibleVtableSummary(StringRef TypeId) {
  1219. return TypeIdCompatibleVtableMap[std::string(TypeId)];
  1220. }
  1221. /// For the given \p TypeId, this returns the TypeIdCompatibleVtableMap
  1222. /// entry if present in the summary map. This may be used when importing.
  1223. Optional<TypeIdCompatibleVtableInfo>
  1224. getTypeIdCompatibleVtableSummary(StringRef TypeId) const {
  1225. auto I = TypeIdCompatibleVtableMap.find(TypeId);
  1226. if (I == TypeIdCompatibleVtableMap.end())
  1227. return None;
  1228. return I->second;
  1229. }
  1230. /// Collect for the given module the list of functions it defines
  1231. /// (GUID -> Summary).
  1232. void collectDefinedFunctionsForModule(StringRef ModulePath,
  1233. GVSummaryMapTy &GVSummaryMap) const;
  1234. /// Collect for each module the list of Summaries it defines (GUID ->
  1235. /// Summary).
  1236. template <class Map>
  1237. void
  1238. collectDefinedGVSummariesPerModule(Map &ModuleToDefinedGVSummaries) const {
  1239. for (auto &GlobalList : *this) {
  1240. auto GUID = GlobalList.first;
  1241. for (auto &Summary : GlobalList.second.SummaryList) {
  1242. ModuleToDefinedGVSummaries[Summary->modulePath()][GUID] = Summary.get();
  1243. }
  1244. }
  1245. }
  1246. /// Print to an output stream.
  1247. void print(raw_ostream &OS, bool IsForDebug = false) const;
  1248. /// Dump to stderr (for debugging).
  1249. void dump() const;
  1250. /// Export summary to dot file for GraphViz.
  1251. void
  1252. exportToDot(raw_ostream &OS,
  1253. const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) const;
  1254. /// Print out strongly connected components for debugging.
  1255. void dumpSCCs(raw_ostream &OS);
  1256. /// Analyze index and detect unmodified globals
  1257. void propagateAttributes(const DenseSet<GlobalValue::GUID> &PreservedSymbols);
  1258. /// Checks if we can import global variable from another module.
  1259. bool canImportGlobalVar(GlobalValueSummary *S, bool AnalyzeRefs) const;
  1260. };
  1261. /// GraphTraits definition to build SCC for the index
  1262. template <> struct GraphTraits<ValueInfo> {
  1263. typedef ValueInfo NodeRef;
  1264. using EdgeRef = FunctionSummary::EdgeTy &;
  1265. static NodeRef valueInfoFromEdge(FunctionSummary::EdgeTy &P) {
  1266. return P.first;
  1267. }
  1268. using ChildIteratorType =
  1269. mapped_iterator<std::vector<FunctionSummary::EdgeTy>::iterator,
  1270. decltype(&valueInfoFromEdge)>;
  1271. using ChildEdgeIteratorType = std::vector<FunctionSummary::EdgeTy>::iterator;
  1272. static NodeRef getEntryNode(ValueInfo V) { return V; }
  1273. static ChildIteratorType child_begin(NodeRef N) {
  1274. if (!N.getSummaryList().size()) // handle external function
  1275. return ChildIteratorType(
  1276. FunctionSummary::ExternalNode.CallGraphEdgeList.begin(),
  1277. &valueInfoFromEdge);
  1278. FunctionSummary *F =
  1279. cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
  1280. return ChildIteratorType(F->CallGraphEdgeList.begin(), &valueInfoFromEdge);
  1281. }
  1282. static ChildIteratorType child_end(NodeRef N) {
  1283. if (!N.getSummaryList().size()) // handle external function
  1284. return ChildIteratorType(
  1285. FunctionSummary::ExternalNode.CallGraphEdgeList.end(),
  1286. &valueInfoFromEdge);
  1287. FunctionSummary *F =
  1288. cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
  1289. return ChildIteratorType(F->CallGraphEdgeList.end(), &valueInfoFromEdge);
  1290. }
  1291. static ChildEdgeIteratorType child_edge_begin(NodeRef N) {
  1292. if (!N.getSummaryList().size()) // handle external function
  1293. return FunctionSummary::ExternalNode.CallGraphEdgeList.begin();
  1294. FunctionSummary *F =
  1295. cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
  1296. return F->CallGraphEdgeList.begin();
  1297. }
  1298. static ChildEdgeIteratorType child_edge_end(NodeRef N) {
  1299. if (!N.getSummaryList().size()) // handle external function
  1300. return FunctionSummary::ExternalNode.CallGraphEdgeList.end();
  1301. FunctionSummary *F =
  1302. cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject());
  1303. return F->CallGraphEdgeList.end();
  1304. }
  1305. static NodeRef edge_dest(EdgeRef E) { return E.first; }
  1306. };
  1307. template <>
  1308. struct GraphTraits<ModuleSummaryIndex *> : public GraphTraits<ValueInfo> {
  1309. static NodeRef getEntryNode(ModuleSummaryIndex *I) {
  1310. std::unique_ptr<GlobalValueSummary> Root =
  1311. std::make_unique<FunctionSummary>(I->calculateCallGraphRoot());
  1312. GlobalValueSummaryInfo G(I->haveGVs());
  1313. G.SummaryList.push_back(std::move(Root));
  1314. static auto P =
  1315. GlobalValueSummaryMapTy::value_type(GlobalValue::GUID(0), std::move(G));
  1316. return ValueInfo(I->haveGVs(), &P);
  1317. }
  1318. };
  1319. } // end namespace llvm
  1320. #endif // LLVM_IR_MODULESUMMARYINDEX_H
  1321. #ifdef __GNUC__
  1322. #pragma GCC diagnostic pop
  1323. #endif