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