| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 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" |
| 25 |
#include "llvm/IR/ConstantRange.h" |
| 26 |
#include "llvm/IR/GlobalValue.h" |
| 27 |
#include "llvm/IR/Module.h" |
| 28 |
#include "llvm/Support/Allocator.h" |
| 29 |
#include "llvm/Support/MathExtras.h" |
| 30 |
#include "llvm/Support/ScaledNumber.h" |
| 31 |
#include "llvm/Support/StringSaver.h" |
| 32 |
#include "llvm/Support/raw_ostream.h" |
| 33 |
#include <algorithm> |
| 34 |
#include <array> |
| 35 |
#include <cassert> |
| 36 |
#include <cstddef> |
| 37 |
#include <cstdint> |
| 38 |
#include <map> |
| 39 |
#include <memory> |
| 40 |
#include <set> |
| 41 |
#include <string> |
| 42 |
#include <utility> |
| 43 |
#include <vector> |
| 44 |
|
| 45 |
namespace llvm { |
| 46 |
|
| 47 |
template <class GraphType> struct GraphTraits; |
| 48 |
|
| 49 |
namespace yaml { |
| 50 |
|
| 51 |
template <typename T> struct MappingTraits; |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
struct CalleeInfo { |
| 57 |
enum class HotnessType : uint8_t { |
| 58 |
Unknown = 0, |
| 59 |
Cold = 1, |
| 60 |
None = 2, |
| 61 |
Hot = 3, |
| 62 |
Critical = 4 |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
uint32_t Hotness : 3; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
uint32_t RelBlockFreq : 29; |
| 72 |
static constexpr int32_t ScaleShift = 8; |
| 73 |
static constexpr uint64_t MaxRelBlockFreq = (1 << 29) - 1; |
| 74 |
|
| 75 |
CalleeInfo() |
| 76 |
: Hotness(static_cast<uint32_t>(HotnessType::Unknown)), RelBlockFreq(0) {} |
| 77 |
explicit CalleeInfo(HotnessType Hotness, uint64_t RelBF) |
| 78 |
: Hotness(static_cast<uint32_t>(Hotness)), RelBlockFreq(RelBF) {} |
| 79 |
|
| 80 |
void updateHotness(const HotnessType OtherHotness) { |
| 81 |
Hotness = std::max(Hotness, static_cast<uint32_t>(OtherHotness)); |
| 82 |
} |
| 83 |
|
| 84 |
HotnessType getHotness() const { return HotnessType(Hotness); } |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
void updateRelBlockFreq(uint64_t BlockFreq, uint64_t EntryFreq) { |
| 92 |
if (EntryFreq == 0) |
| 93 |
return; |
| 94 |
using Scaled64 = ScaledNumber<uint64_t>; |
| 95 |
Scaled64 Temp(BlockFreq, ScaleShift); |
| 96 |
Temp /= Scaled64::get(EntryFreq); |
| 97 |
|
| 98 |
uint64_t Sum = |
| 99 |
SaturatingAdd<uint64_t>(Temp.toInt<uint64_t>(), RelBlockFreq); |
| 100 |
Sum = std::min(Sum, uint64_t(MaxRelBlockFreq)); |
| 101 |
RelBlockFreq = static_cast<uint32_t>(Sum); |
| 102 |
} |
| 103 |
}; |
| 104 |
|
| 105 |
inline const char *getHotnessName(CalleeInfo::HotnessType HT) { |
| 106 |
switch (HT) { |
| 107 |
case CalleeInfo::HotnessType::Unknown: |
| 108 |
return "unknown"; |
| 109 |
case CalleeInfo::HotnessType::Cold: |
| 110 |
return "cold"; |
| 111 |
case CalleeInfo::HotnessType::None: |
| 112 |
return "none"; |
| 113 |
case CalleeInfo::HotnessType::Hot: |
| 114 |
return "hot"; |
| 115 |
case CalleeInfo::HotnessType::Critical: |
| 116 |
return "critical"; |
| 117 |
} |
| 118 |
llvm_unreachable("invalid hotness"); |
| 119 |
} |
| 120 |
|
| 121 |
class GlobalValueSummary; |
| 122 |
|
| 123 |
using GlobalValueSummaryList = std::vector<std::unique_ptr<GlobalValueSummary>>; |
| 124 |
|
| 125 |
struct alignas(8) GlobalValueSummaryInfo { |
| 126 |
union NameOrGV { |
| 127 |
NameOrGV(bool HaveGVs) { |
| 128 |
if (HaveGVs) |
| 129 |
GV = nullptr; |
| 130 |
else |
| 131 |
Name = ""; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
const GlobalValue *GV; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
StringRef Name; |
| 146 |
} U; |
| 147 |
|
| 148 |
GlobalValueSummaryInfo(bool HaveGVs) : U(HaveGVs) {} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
GlobalValueSummaryList SummaryList; |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
using GlobalValueSummaryMapTy = |
| 163 |
std::map<GlobalValue::GUID, GlobalValueSummaryInfo>; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
struct ValueInfo { |
| 168 |
enum Flags { HaveGV = 1, ReadOnly = 2, WriteOnly = 4 }; |
| 169 |
PointerIntPair<const GlobalValueSummaryMapTy::value_type *, 3, int> |
| 170 |
RefAndFlags; |
| 171 |
|
| 172 |
ValueInfo() = default; |
| 173 |
ValueInfo(bool HaveGVs, const GlobalValueSummaryMapTy::value_type *R) { |
| 174 |
RefAndFlags.setPointer(R); |
| 175 |
RefAndFlags.setInt(HaveGVs); |
| 176 |
} |
| 177 |
|
| 178 |
explicit operator bool() const { return getRef(); } |
| 179 |
|
| 180 |
GlobalValue::GUID getGUID() const { return getRef()->first; } |
| 181 |
const GlobalValue *getValue() const { |
| 182 |
assert(haveGVs()); |
| 183 |
return getRef()->second.U.GV; |
| 184 |
} |
| 185 |
|
| 186 |
ArrayRef<std::unique_ptr<GlobalValueSummary>> getSummaryList() const { |
| 187 |
return getRef()->second.SummaryList; |
| 188 |
} |
| 189 |
|
| 190 |
StringRef name() const { |
| 191 |
return haveGVs() ? getRef()->second.U.GV->getName() |
| 192 |
: getRef()->second.U.Name; |
| 193 |
} |
| 194 |
|
| 195 |
bool haveGVs() const { return RefAndFlags.getInt() & HaveGV; } |
| 196 |
bool isReadOnly() const { |
| 197 |
assert(isValidAccessSpecifier()); |
| 198 |
return RefAndFlags.getInt() & ReadOnly; |
| 199 |
} |
| 200 |
bool isWriteOnly() const { |
| 201 |
assert(isValidAccessSpecifier()); |
| 202 |
return RefAndFlags.getInt() & WriteOnly; |
| 203 |
} |
| 204 |
unsigned getAccessSpecifier() const { |
| 205 |
assert(isValidAccessSpecifier()); |
| 206 |
return RefAndFlags.getInt() & (ReadOnly | WriteOnly); |
| 207 |
} |
| 208 |
bool isValidAccessSpecifier() const { |
| 209 |
unsigned BadAccessMask = ReadOnly | WriteOnly; |
| 210 |
return (RefAndFlags.getInt() & BadAccessMask) != BadAccessMask; |
| 211 |
} |
| 212 |
void setReadOnly() { |
| 213 |
|
| 214 |
|
| 215 |
assert(getAccessSpecifier() == 0); |
| 216 |
RefAndFlags.setInt(RefAndFlags.getInt() | ReadOnly); |
| 217 |
} |
| 218 |
void setWriteOnly() { |
| 219 |
assert(getAccessSpecifier() == 0); |
| 220 |
RefAndFlags.setInt(RefAndFlags.getInt() | WriteOnly); |
| 221 |
} |
| 222 |
|
| 223 |
const GlobalValueSummaryMapTy::value_type *getRef() const { |
| 224 |
return RefAndFlags.getPointer(); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
GlobalValue::VisibilityTypes getELFVisibility() const; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
bool isDSOLocal(bool WithDSOLocalPropagation = false) const; |
| 235 |
|
| 236 |
|
| 237 |
bool canAutoHide() const; |
| 238 |
}; |
| 239 |
|
| 240 |
inline raw_ostream &operator<<(raw_ostream &OS, const ValueInfo &VI) { |
| 241 |
OS << VI.getGUID(); |
| 242 |
if (!VI.name().empty()) |
| 243 |
OS << " (" << VI.name() << ")"; |
| 244 |
return OS; |
| 245 |
} |
| 246 |
|
| 247 |
inline bool operator==(const ValueInfo &A, const ValueInfo &B) { |
| 248 |
assert(A.getRef() && B.getRef() && |
| 249 |
"Need ValueInfo with non-null Ref for comparison"); |
| 250 |
return A.getRef() == B.getRef(); |
| 251 |
} |
| 252 |
|
| 253 |
inline bool operator!=(const ValueInfo &A, const ValueInfo &B) { |
| 254 |
assert(A.getRef() && B.getRef() && |
| 255 |
"Need ValueInfo with non-null Ref for comparison"); |
| 256 |
return A.getRef() != B.getRef(); |
| 257 |
} |
| 258 |
|
| 259 |
inline bool operator<(const ValueInfo &A, const ValueInfo &B) { |
| 260 |
assert(A.getRef() && B.getRef() && |
| 261 |
"Need ValueInfo with non-null Ref to compare GUIDs"); |
| 262 |
return A.getGUID() < B.getGUID(); |
| 263 |
} |
| 264 |
|
| 265 |
template <> struct DenseMapInfo<ValueInfo> { |
| 266 |
static inline ValueInfo getEmptyKey() { |
| 267 |
return ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8); |
| 268 |
} |
| 269 |
|
| 270 |
static inline ValueInfo getTombstoneKey() { |
| 271 |
return ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-16); |
| 272 |
} |
| 273 |
|
| 274 |
static inline bool isSpecialKey(ValueInfo V) { |
| 275 |
return V == getTombstoneKey() || V == getEmptyKey(); |
| 276 |
} |
| 277 |
|
| 278 |
static bool isEqual(ValueInfo L, ValueInfo R) { |
| 279 |
|
| 280 |
|
| 281 |
assert(isSpecialKey(L) || isSpecialKey(R) || (L.haveGVs() == R.haveGVs())); |
| 282 |
return L.getRef() == R.getRef(); |
| 283 |
} |
| 284 |
static unsigned getHashValue(ValueInfo I) { return (uintptr_t)I.getRef(); } |
| 285 |
}; |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
class GlobalValueSummary { |
| 290 |
public: |
| 291 |
|
| 292 |
enum SummaryKind : unsigned { AliasKind, FunctionKind, GlobalVarKind }; |
| 293 |
|
| 294 |
|
| 295 |
struct GVFlags { |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
unsigned Linkage : 4; |
| 304 |
|
| 305 |
|
| 306 |
unsigned Visibility : 2; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
unsigned NotEligibleToImport : 1; |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
unsigned Live : 1; |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
unsigned DSOLocal : 1; |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
unsigned CanAutoHide : 1; |
| 333 |
|
| 334 |
|
| 335 |
explicit GVFlags(GlobalValue::LinkageTypes Linkage, |
| 336 |
GlobalValue::VisibilityTypes Visibility, |
| 337 |
bool NotEligibleToImport, bool Live, bool IsLocal, |
| 338 |
bool CanAutoHide) |
| 339 |
: Linkage(Linkage), Visibility(Visibility), |
| 340 |
NotEligibleToImport(NotEligibleToImport), Live(Live), |
| 341 |
DSOLocal(IsLocal), CanAutoHide(CanAutoHide) {} |
| 342 |
}; |
| 343 |
|
| 344 |
private: |
| 345 |
|
| 346 |
SummaryKind Kind; |
| 347 |
|
| 348 |
GVFlags Flags; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
GlobalValue::GUID OriginalName = 0; |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
StringRef ModulePath; |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
std::vector<ValueInfo> RefEdgeList; |
| 369 |
|
| 370 |
protected: |
| 371 |
GlobalValueSummary(SummaryKind K, GVFlags Flags, std::vector<ValueInfo> Refs) |
| 372 |
: Kind(K), Flags(Flags), RefEdgeList(std::move(Refs)) { |
| 373 |
assert((K != AliasKind || Refs.empty()) && |
| 374 |
"Expect no references for AliasSummary"); |
| 375 |
} |
| 376 |
|
| 377 |
public: |
| 378 |
virtual ~GlobalValueSummary() = default; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
GlobalValue::GUID getOriginalName() const { return OriginalName; } |
| 383 |
|
| 384 |
|
| 385 |
void setOriginalName(GlobalValue::GUID Name) { OriginalName = Name; } |
| 386 |
|
| 387 |
|
| 388 |
SummaryKind getSummaryKind() const { return Kind; } |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
void setModulePath(StringRef ModPath) { ModulePath = ModPath; } |
| 393 |
|
| 394 |
|
| 395 |
StringRef modulePath() const { return ModulePath; } |
| 396 |
|
| 397 |
|
| 398 |
GVFlags flags() const { return Flags; } |
| 399 |
|
| 400 |
|
| 401 |
GlobalValue::LinkageTypes linkage() const { |
| 402 |
return static_cast<GlobalValue::LinkageTypes>(Flags.Linkage); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
void setLinkage(GlobalValue::LinkageTypes Linkage) { |
| 408 |
Flags.Linkage = Linkage; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
bool notEligibleToImport() const { return Flags.NotEligibleToImport; } |
| 413 |
|
| 414 |
bool isLive() const { return Flags.Live; } |
| 415 |
|
| 416 |
void setLive(bool Live) { Flags.Live = Live; } |
| 417 |
|
| 418 |
void setDSOLocal(bool Local) { Flags.DSOLocal = Local; } |
| 419 |
|
| 420 |
bool isDSOLocal() const { return Flags.DSOLocal; } |
| 421 |
|
| 422 |
void setCanAutoHide(bool CanAutoHide) { Flags.CanAutoHide = CanAutoHide; } |
| 423 |
|
| 424 |
bool canAutoHide() const { return Flags.CanAutoHide; } |
| 425 |
|
| 426 |
GlobalValue::VisibilityTypes getVisibility() const { |
| 427 |
return (GlobalValue::VisibilityTypes)Flags.Visibility; |
| 428 |
} |
| 429 |
void setVisibility(GlobalValue::VisibilityTypes Vis) { |
| 430 |
Flags.Visibility = (unsigned)Vis; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
void setNotEligibleToImport() { Flags.NotEligibleToImport = true; } |
| 435 |
|
| 436 |
|
| 437 |
ArrayRef<ValueInfo> refs() const { return RefEdgeList; } |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
GlobalValueSummary *getBaseObject(); |
| 442 |
const GlobalValueSummary *getBaseObject() const; |
| 443 |
|
| 444 |
friend class ModuleSummaryIndex; |
| 445 |
}; |
| 446 |
|
| 447 |
|
| 448 |
class AliasSummary : public GlobalValueSummary { |
| 449 |
ValueInfo AliaseeValueInfo; |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
GlobalValueSummary *AliaseeSummary; |
| 456 |
|
| 457 |
public: |
| 458 |
AliasSummary(GVFlags Flags) |
| 459 |
: GlobalValueSummary(AliasKind, Flags, ArrayRef<ValueInfo>{}), |
| 460 |
AliaseeSummary(nullptr) {} |
| 461 |
|
| 462 |
|
| 463 |
static bool classof(const GlobalValueSummary *GVS) { |
| 464 |
return GVS->getSummaryKind() == AliasKind; |
| 465 |
} |
| 466 |
|
| 467 |
void setAliasee(ValueInfo &AliaseeVI, GlobalValueSummary *Aliasee) { |
| 468 |
AliaseeValueInfo = AliaseeVI; |
| 469 |
AliaseeSummary = Aliasee; |
| 470 |
} |
| 471 |
|
| 472 |
bool hasAliasee() const { |
| 473 |
assert(!!AliaseeSummary == (AliaseeValueInfo && |
| 474 |
!AliaseeValueInfo.getSummaryList().empty()) && |
| 475 |
"Expect to have both aliasee summary and summary list or neither"); |
| 476 |
return !!AliaseeSummary; |
| 477 |
} |
| 478 |
|
| 479 |
const GlobalValueSummary &getAliasee() const { |
| 480 |
assert(AliaseeSummary && "Unexpected missing aliasee summary"); |
| 481 |
return *AliaseeSummary; |
| 482 |
} |
| 483 |
|
| 484 |
GlobalValueSummary &getAliasee() { |
| 485 |
return const_cast<GlobalValueSummary &>( |
| 486 |
static_cast<const AliasSummary *>(this)->getAliasee()); |
| 487 |
} |
| 488 |
ValueInfo getAliaseeVI() const { |
| 489 |
assert(AliaseeValueInfo && "Unexpected missing aliasee"); |
| 490 |
return AliaseeValueInfo; |
| 491 |
} |
| 492 |
GlobalValue::GUID getAliaseeGUID() const { |
| 493 |
assert(AliaseeValueInfo && "Unexpected missing aliasee"); |
| 494 |
return AliaseeValueInfo.getGUID(); |
| 495 |
} |
| 496 |
}; |
| 497 |
|
| 498 |
const inline GlobalValueSummary *GlobalValueSummary::getBaseObject() const { |
| 499 |
if (auto *AS = dyn_cast<AliasSummary>(this)) |
| 500 |
return &AS->getAliasee(); |
| 501 |
return this; |
| 502 |
} |
| 503 |
|
| 504 |
inline GlobalValueSummary *GlobalValueSummary::getBaseObject() { |
| 505 |
if (auto *AS = dyn_cast<AliasSummary>(this)) |
| 506 |
return &AS->getAliasee(); |
| 507 |
return this; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
class FunctionSummary : public GlobalValueSummary { |
| 513 |
public: |
| 514 |
|
| 515 |
using EdgeTy = std::pair<ValueInfo, CalleeInfo>; |
| 516 |
|
| 517 |
|
| 518 |
enum ForceSummaryHotnessType : unsigned { |
| 519 |
FSHT_None, |
| 520 |
FSHT_AllNonCritical, |
| 521 |
FSHT_All |
| 522 |
}; |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
struct VFuncId { |
| 529 |
GlobalValue::GUID GUID; |
| 530 |
uint64_t Offset; |
| 531 |
}; |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
struct ConstVCall { |
| 537 |
VFuncId VFunc; |
| 538 |
std::vector<uint64_t> Args; |
| 539 |
}; |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
struct TypeIdInfo { |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
std::vector<GlobalValue::GUID> TypeTests; |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
std::vector<VFuncId> TypeTestAssumeVCalls, TypeCheckedLoadVCalls; |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
std::vector<ConstVCall> TypeTestAssumeConstVCalls, |
| 558 |
TypeCheckedLoadConstVCalls; |
| 559 |
}; |
| 560 |
|
| 561 |
|
| 562 |
struct FFlags { |
| 563 |
|
| 564 |
|
| 565 |
unsigned ReadNone : 1; |
| 566 |
unsigned ReadOnly : 1; |
| 567 |
unsigned NoRecurse : 1; |
| 568 |
unsigned ReturnDoesNotAlias : 1; |
| 569 |
|
| 570 |
|
| 571 |
unsigned NoInline : 1; |
| 572 |
|
| 573 |
unsigned AlwaysInline : 1; |
| 574 |
|
| 575 |
|
| 576 |
unsigned NoUnwind : 1; |
| 577 |
|
| 578 |
unsigned MayThrow : 1; |
| 579 |
|
| 580 |
|
| 581 |
unsigned HasUnknownCall : 1; |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
unsigned MustBeUnreachable : 1; |
| 589 |
|
| 590 |
FFlags &operator&=(const FFlags &RHS) { |
| 591 |
this->ReadNone &= RHS.ReadNone; |
| 592 |
this->ReadOnly &= RHS.ReadOnly; |
| 593 |
this->NoRecurse &= RHS.NoRecurse; |
| 594 |
this->ReturnDoesNotAlias &= RHS.ReturnDoesNotAlias; |
| 595 |
this->NoInline &= RHS.NoInline; |
| 596 |
this->AlwaysInline &= RHS.AlwaysInline; |
| 597 |
this->NoUnwind &= RHS.NoUnwind; |
| 598 |
this->MayThrow &= RHS.MayThrow; |
| 599 |
this->HasUnknownCall &= RHS.HasUnknownCall; |
| 600 |
this->MustBeUnreachable &= RHS.MustBeUnreachable; |
| 601 |
return *this; |
| 602 |
} |
| 603 |
|
| 604 |
bool anyFlagSet() { |
| 605 |
return this->ReadNone | this->ReadOnly | this->NoRecurse | |
| 606 |
this->ReturnDoesNotAlias | this->NoInline | this->AlwaysInline | |
| 607 |
this->NoUnwind | this->MayThrow | this->HasUnknownCall | |
| 608 |
this->MustBeUnreachable; |
| 609 |
} |
| 610 |
|
| 611 |
operator std::string() { |
| 612 |
std::string Output; |
| 613 |
raw_string_ostream OS(Output); |
| 614 |
OS << "funcFlags: ("; |
| 615 |
OS << "readNone: " << this->ReadNone; |
| 616 |
OS << ", readOnly: " << this->ReadOnly; |
| 617 |
OS << ", noRecurse: " << this->NoRecurse; |
| 618 |
OS << ", returnDoesNotAlias: " << this->ReturnDoesNotAlias; |
| 619 |
OS << ", noInline: " << this->NoInline; |
| 620 |
OS << ", alwaysInline: " << this->AlwaysInline; |
| 621 |
OS << ", noUnwind: " << this->NoUnwind; |
| 622 |
OS << ", mayThrow: " << this->MayThrow; |
| 623 |
OS << ", hasUnknownCall: " << this->HasUnknownCall; |
| 624 |
OS << ", mustBeUnreachable: " << this->MustBeUnreachable; |
| 625 |
OS << ")"; |
| 626 |
return OS.str(); |
| 627 |
} |
| 628 |
}; |
| 629 |
|
| 630 |
|
| 631 |
struct ParamAccess { |
| 632 |
static constexpr uint32_t RangeWidth = 64; |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
struct Call { |
| 638 |
uint64_t ParamNo = 0; |
| 639 |
ValueInfo Callee; |
| 640 |
ConstantRange Offsets{RangeWidth, true}; |
| 641 |
|
| 642 |
Call() = default; |
| 643 |
Call(uint64_t ParamNo, ValueInfo Callee, const ConstantRange &Offsets) |
| 644 |
: ParamNo(ParamNo), Callee(Callee), Offsets(Offsets) {} |
| 645 |
}; |
| 646 |
|
| 647 |
uint64_t ParamNo = 0; |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
ConstantRange Use{RangeWidth, true}; |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
std::vector<Call> Calls; |
| 658 |
|
| 659 |
ParamAccess() = default; |
| 660 |
ParamAccess(uint64_t ParamNo, const ConstantRange &Use) |
| 661 |
: ParamNo(ParamNo), Use(Use) {} |
| 662 |
}; |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
static FunctionSummary |
| 667 |
makeDummyFunctionSummary(std::vector<FunctionSummary::EdgeTy> Edges) { |
| 668 |
return FunctionSummary( |
| 669 |
FunctionSummary::GVFlags( |
| 670 |
GlobalValue::LinkageTypes::AvailableExternallyLinkage, |
| 671 |
GlobalValue::DefaultVisibility, |
| 672 |
true, true, false, |
| 673 |
false), |
| 674 |
0, FunctionSummary::FFlags{}, 0, |
| 675 |
std::vector<ValueInfo>(), std::move(Edges), |
| 676 |
std::vector<GlobalValue::GUID>(), |
| 677 |
std::vector<FunctionSummary::VFuncId>(), |
| 678 |
std::vector<FunctionSummary::VFuncId>(), |
| 679 |
std::vector<FunctionSummary::ConstVCall>(), |
| 680 |
std::vector<FunctionSummary::ConstVCall>(), |
| 681 |
std::vector<FunctionSummary::ParamAccess>()); |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
static FunctionSummary ExternalNode; |
| 686 |
|
| 687 |
private: |
| 688 |
|
| 689 |
|
| 690 |
unsigned InstCount; |
| 691 |
|
| 692 |
|
| 693 |
FFlags FunFlags; |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
uint64_t EntryCount = 0; |
| 699 |
|
| 700 |
|
| 701 |
std::vector<EdgeTy> CallGraphEdgeList; |
| 702 |
|
| 703 |
std::unique_ptr<TypeIdInfo> TIdInfo; |
| 704 |
|
| 705 |
|
| 706 |
using ParamAccessesTy = std::vector<ParamAccess>; |
| 707 |
std::unique_ptr<ParamAccessesTy> ParamAccesses; |
| 708 |
|
| 709 |
public: |
| 710 |
FunctionSummary(GVFlags Flags, unsigned NumInsts, FFlags FunFlags, |
| 711 |
uint64_t EntryCount, std::vector<ValueInfo> Refs, |
| 712 |
std::vector<EdgeTy> CGEdges, |
| 713 |
std::vector<GlobalValue::GUID> TypeTests, |
| 714 |
std::vector<VFuncId> TypeTestAssumeVCalls, |
| 715 |
std::vector<VFuncId> TypeCheckedLoadVCalls, |
| 716 |
std::vector<ConstVCall> TypeTestAssumeConstVCalls, |
| 717 |
std::vector<ConstVCall> TypeCheckedLoadConstVCalls, |
| 718 |
std::vector<ParamAccess> Params) |
| 719 |
: GlobalValueSummary(FunctionKind, Flags, std::move(Refs)), |
| 720 |
InstCount(NumInsts), FunFlags(FunFlags), EntryCount(EntryCount), |
| 721 |
CallGraphEdgeList(std::move(CGEdges)) { |
| 722 |
if (!TypeTests.empty() || !TypeTestAssumeVCalls.empty() || |
| 723 |
!TypeCheckedLoadVCalls.empty() || !TypeTestAssumeConstVCalls.empty() || |
| 724 |
!TypeCheckedLoadConstVCalls.empty()) |
| 725 |
TIdInfo = std::make_unique<TypeIdInfo>( |
| 726 |
TypeIdInfo{std::move(TypeTests), std::move(TypeTestAssumeVCalls), |
| 727 |
std::move(TypeCheckedLoadVCalls), |
| 728 |
std::move(TypeTestAssumeConstVCalls), |
| 729 |
std::move(TypeCheckedLoadConstVCalls)}); |
| 730 |
if (!Params.empty()) |
| 731 |
ParamAccesses = std::make_unique<ParamAccessesTy>(std::move(Params)); |
| 732 |
} |
| 733 |
|
| 734 |
std::pair<unsigned, unsigned> specialRefCounts() const; |
| 735 |
|
| 736 |
|
| 737 |
static bool classof(const GlobalValueSummary *GVS) { |
| 738 |
return GVS->getSummaryKind() == FunctionKind; |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
FFlags fflags() const { return FunFlags; } |
| 743 |
|
| 744 |
void setNoRecurse() { FunFlags.NoRecurse = true; } |
| 745 |
|
| 746 |
void setNoUnwind() { FunFlags.NoUnwind = true; } |
| 747 |
|
| 748 |
|
| 749 |
unsigned instCount() const { return InstCount; } |
| 750 |
|
| 751 |
|
| 752 |
uint64_t entryCount() const { return EntryCount; } |
| 753 |
|
| 754 |
|
| 755 |
void setEntryCount(uint64_t EC) { EntryCount = EC; } |
| 756 |
|
| 757 |
|
| 758 |
ArrayRef<EdgeTy> calls() const { return CallGraphEdgeList; } |
| 759 |
|
| 760 |
std::vector<EdgeTy> &mutableCalls() { return CallGraphEdgeList; } |
| 761 |
|
| 762 |
void addCall(EdgeTy E) { CallGraphEdgeList.push_back(E); } |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
ArrayRef<GlobalValue::GUID> type_tests() const { |
| 768 |
if (TIdInfo) |
| 769 |
return TIdInfo->TypeTests; |
| 770 |
return {}; |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
ArrayRef<VFuncId> type_test_assume_vcalls() const { |
| 777 |
if (TIdInfo) |
| 778 |
return TIdInfo->TypeTestAssumeVCalls; |
| 779 |
return {}; |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
ArrayRef<VFuncId> type_checked_load_vcalls() const { |
| 786 |
if (TIdInfo) |
| 787 |
return TIdInfo->TypeCheckedLoadVCalls; |
| 788 |
return {}; |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
ArrayRef<ConstVCall> type_test_assume_const_vcalls() const { |
| 795 |
if (TIdInfo) |
| 796 |
return TIdInfo->TypeTestAssumeConstVCalls; |
| 797 |
return {}; |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
ArrayRef<ConstVCall> type_checked_load_const_vcalls() const { |
| 803 |
if (TIdInfo) |
| 804 |
return TIdInfo->TypeCheckedLoadConstVCalls; |
| 805 |
return {}; |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
ArrayRef<ParamAccess> paramAccesses() const { |
| 810 |
if (ParamAccesses) |
| 811 |
return *ParamAccesses; |
| 812 |
return {}; |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
void setParamAccesses(std::vector<ParamAccess> NewParams) { |
| 817 |
if (NewParams.empty()) |
| 818 |
ParamAccesses.reset(); |
| 819 |
else if (ParamAccesses) |
| 820 |
*ParamAccesses = std::move(NewParams); |
| 821 |
else |
| 822 |
ParamAccesses = std::make_unique<ParamAccessesTy>(std::move(NewParams)); |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
void addTypeTest(GlobalValue::GUID Guid) { |
| 828 |
if (!TIdInfo) |
| 829 |
TIdInfo = std::make_unique<TypeIdInfo>(); |
| 830 |
TIdInfo->TypeTests.push_back(Guid); |
| 831 |
} |
| 832 |
|
| 833 |
const TypeIdInfo *getTypeIdInfo() const { return TIdInfo.get(); }; |
| 834 |
|
| 835 |
friend struct GraphTraits<ValueInfo>; |
| 836 |
}; |
| 837 |
|
| 838 |
template <> struct DenseMapInfo<FunctionSummary::VFuncId> { |
| 839 |
static FunctionSummary::VFuncId getEmptyKey() { return {0, uint64_t(-1)}; } |
| 840 |
|
| 841 |
static FunctionSummary::VFuncId getTombstoneKey() { |
| 842 |
return {0, uint64_t(-2)}; |
| 843 |
} |
| 844 |
|
| 845 |
static bool isEqual(FunctionSummary::VFuncId L, FunctionSummary::VFuncId R) { |
| 846 |
return L.GUID == R.GUID && L.Offset == R.Offset; |
| 847 |
} |
| 848 |
|
| 849 |
static unsigned getHashValue(FunctionSummary::VFuncId I) { return I.GUID; } |
| 850 |
}; |
| 851 |
|
| 852 |
template <> struct DenseMapInfo<FunctionSummary::ConstVCall> { |
| 853 |
static FunctionSummary::ConstVCall getEmptyKey() { |
| 854 |
return {{0, uint64_t(-1)}, {}}; |
| 855 |
} |
| 856 |
|
| 857 |
static FunctionSummary::ConstVCall getTombstoneKey() { |
| 858 |
return {{0, uint64_t(-2)}, {}}; |
| 859 |
} |
| 860 |
|
| 861 |
static bool isEqual(FunctionSummary::ConstVCall L, |
| 862 |
FunctionSummary::ConstVCall R) { |
| 863 |
return DenseMapInfo<FunctionSummary::VFuncId>::isEqual(L.VFunc, R.VFunc) && |
| 864 |
L.Args == R.Args; |
| 865 |
} |
| 866 |
|
| 867 |
static unsigned getHashValue(FunctionSummary::ConstVCall I) { |
| 868 |
return I.VFunc.GUID; |
| 869 |
} |
| 870 |
}; |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
struct VirtFuncOffset { |
| 875 |
VirtFuncOffset(ValueInfo VI, uint64_t Offset) |
| 876 |
: FuncVI(VI), VTableOffset(Offset) {} |
| 877 |
|
| 878 |
ValueInfo FuncVI; |
| 879 |
uint64_t VTableOffset; |
| 880 |
}; |
| 881 |
|
| 882 |
using VTableFuncList = std::vector<VirtFuncOffset>; |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
class GlobalVarSummary : public GlobalValueSummary { |
| 894 |
private: |
| 895 |
|
| 896 |
|
| 897 |
std::unique_ptr<VTableFuncList> VTableFuncs; |
| 898 |
|
| 899 |
public: |
| 900 |
struct GVarFlags { |
| 901 |
GVarFlags(bool ReadOnly, bool WriteOnly, bool Constant, |
| 902 |
GlobalObject::VCallVisibility Vis) |
| 903 |
: MaybeReadOnly(ReadOnly), MaybeWriteOnly(WriteOnly), |
| 904 |
Constant(Constant), VCallVisibility(Vis) {} |
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
unsigned MaybeReadOnly : 1; |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
unsigned MaybeWriteOnly : 1; |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
unsigned Constant : 1; |
| 925 |
|
| 926 |
|
| 927 |
unsigned VCallVisibility : 2; |
| 928 |
} VarFlags; |
| 929 |
|
| 930 |
GlobalVarSummary(GVFlags Flags, GVarFlags VarFlags, |
| 931 |
std::vector<ValueInfo> Refs) |
| 932 |
: GlobalValueSummary(GlobalVarKind, Flags, std::move(Refs)), |
| 933 |
VarFlags(VarFlags) {} |
| 934 |
|
| 935 |
|
| 936 |
static bool classof(const GlobalValueSummary *GVS) { |
| 937 |
return GVS->getSummaryKind() == GlobalVarKind; |
| 938 |
} |
| 939 |
|
| 940 |
GVarFlags varflags() const { return VarFlags; } |
| 941 |
void setReadOnly(bool RO) { VarFlags.MaybeReadOnly = RO; } |
| 942 |
void setWriteOnly(bool WO) { VarFlags.MaybeWriteOnly = WO; } |
| 943 |
bool maybeReadOnly() const { return VarFlags.MaybeReadOnly; } |
| 944 |
bool maybeWriteOnly() const { return VarFlags.MaybeWriteOnly; } |
| 945 |
bool isConstant() const { return VarFlags.Constant; } |
| 946 |
void setVCallVisibility(GlobalObject::VCallVisibility Vis) { |
| 947 |
VarFlags.VCallVisibility = Vis; |
| 948 |
} |
| 949 |
GlobalObject::VCallVisibility getVCallVisibility() const { |
| 950 |
return (GlobalObject::VCallVisibility)VarFlags.VCallVisibility; |
| 951 |
} |
| 952 |
|
| 953 |
void setVTableFuncs(VTableFuncList Funcs) { |
| 954 |
assert(!VTableFuncs); |
| 955 |
VTableFuncs = std::make_unique<VTableFuncList>(std::move(Funcs)); |
| 956 |
} |
| 957 |
|
| 958 |
ArrayRef<VirtFuncOffset> vTableFuncs() const { |
| 959 |
if (VTableFuncs) |
| 960 |
return *VTableFuncs; |
| 961 |
return {}; |
| 962 |
} |
| 963 |
}; |
| 964 |
|
| 965 |
struct TypeTestResolution { |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
enum Kind { |
| 971 |
Unsat, |
| 972 |
ByteArray, |
| 973 |
Inline, |
| 974 |
Single, |
| 975 |
AllOnes, |
| 976 |
|
| 977 |
Unknown, |
| 978 |
} TheKind = Unknown; |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
unsigned SizeM1BitWidth = 0; |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
uint64_t AlignLog2 = 0; |
| 991 |
uint64_t SizeM1 = 0; |
| 992 |
uint8_t BitMask = 0; |
| 993 |
uint64_t InlineBits = 0; |
| 994 |
}; |
| 995 |
|
| 996 |
struct WholeProgramDevirtResolution { |
| 997 |
enum Kind { |
| 998 |
Indir, |
| 999 |
SingleImpl, |
| 1000 |
BranchFunnel, |
| 1001 |
|
| 1002 |
|
| 1003 |
} TheKind = Indir; |
| 1004 |
|
| 1005 |
std::string SingleImplName; |
| 1006 |
|
| 1007 |
struct ByArg { |
| 1008 |
enum Kind { |
| 1009 |
Indir, |
| 1010 |
UniformRetVal, |
| 1011 |
UniqueRetVal, |
| 1012 |
VirtualConstProp, |
| 1013 |
} TheKind = Indir; |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
uint64_t Info = 0; |
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
uint32_t Byte = 0; |
| 1025 |
uint32_t Bit = 0; |
| 1026 |
}; |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
std::map<std::vector<uint64_t>, ByArg> ResByArg; |
| 1031 |
}; |
| 1032 |
|
| 1033 |
struct TypeIdSummary { |
| 1034 |
TypeTestResolution TTRes; |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
std::map<uint64_t, WholeProgramDevirtResolution> WPDRes; |
| 1039 |
}; |
| 1040 |
|
| 1041 |
|
| 1042 |
using ModuleHash = std::array<uint32_t, 5>; |
| 1043 |
|
| 1044 |
|
| 1045 |
using const_gvsummary_iterator = GlobalValueSummaryMapTy::const_iterator; |
| 1046 |
using gvsummary_iterator = GlobalValueSummaryMapTy::iterator; |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
using ModulePathStringTableTy = StringMap<std::pair<uint64_t, ModuleHash>>; |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
using GVSummaryMapTy = DenseMap<GlobalValue::GUID, GlobalValueSummary *>; |
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
using TypeIdSummaryMapTy = |
| 1060 |
std::multimap<GlobalValue::GUID, std::pair<std::string, TypeIdSummary>>; |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
struct TypeIdOffsetVtableInfo { |
| 1073 |
TypeIdOffsetVtableInfo(uint64_t Offset, ValueInfo VI) |
| 1074 |
: AddressPointOffset(Offset), VTableVI(VI) {} |
| 1075 |
|
| 1076 |
uint64_t AddressPointOffset; |
| 1077 |
ValueInfo VTableVI; |
| 1078 |
}; |
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
using TypeIdCompatibleVtableInfo = std::vector<TypeIdOffsetVtableInfo>; |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
class ModuleSummaryIndex { |
| 1088 |
private: |
| 1089 |
|
| 1090 |
|
| 1091 |
GlobalValueSummaryMapTy GlobalValueMap; |
| 1092 |
|
| 1093 |
|
| 1094 |
ModulePathStringTableTy ModulePathStringTable; |
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
TypeIdSummaryMapTy TypeIdMap; |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
std::map<std::string, TypeIdCompatibleVtableInfo, std::less<>> |
| 1105 |
TypeIdCompatibleVtableMap; |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
std::map<GlobalValue::GUID, GlobalValue::GUID> OidGuidMap; |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
bool WithGlobalValueDeadStripping = false; |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
bool WithAttributePropagation = false; |
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
bool WithDSOLocalPropagation = false; |
| 1124 |
|
| 1125 |
|
| 1126 |
bool HasSyntheticEntryCounts = false; |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
bool SkipModuleByDistributedBackend = false; |
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
bool HaveGVs; |
| 1140 |
|
| 1141 |
|
| 1142 |
bool EnableSplitLTOUnit; |
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
bool PartiallySplitLTOUnits = false; |
| 1147 |
|
| 1148 |
|
| 1149 |
bool HasParamAccess = false; |
| 1150 |
|
| 1151 |
std::set<std::string> CfiFunctionDefs; |
| 1152 |
std::set<std::string> CfiFunctionDecls; |
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
StringSaver Saver; |
| 1157 |
BumpPtrAllocator Alloc; |
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
uint64_t BlockCount; |
| 1162 |
|
| 1163 |
|
| 1164 |
friend yaml::MappingTraits<ModuleSummaryIndex>; |
| 1165 |
|
| 1166 |
GlobalValueSummaryMapTy::value_type * |
| 1167 |
getOrInsertValuePtr(GlobalValue::GUID GUID) { |
| 1168 |
return &*GlobalValueMap.emplace(GUID, GlobalValueSummaryInfo(HaveGVs)) |
| 1169 |
.first; |
| 1170 |
} |
| 1171 |
|
| 1172 |
public: |
| 1173 |
|
| 1174 |
ModuleSummaryIndex(bool HaveGVs, bool EnableSplitLTOUnit = false) |
| 1175 |
: HaveGVs(HaveGVs), EnableSplitLTOUnit(EnableSplitLTOUnit), Saver(Alloc), |
| 1176 |
BlockCount(0) {} |
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
static constexpr uint64_t BitcodeSummaryVersion = 9; |
| 1184 |
|
| 1185 |
|
| 1186 |
static constexpr const char *getRegularLTOModuleName() { |
| 1187 |
return "[Regular LTO]"; |
| 1188 |
} |
| 1189 |
|
| 1190 |
bool haveGVs() const { return HaveGVs; } |
| 1191 |
|
| 1192 |
uint64_t getFlags() const; |
| 1193 |
void setFlags(uint64_t Flags); |
| 1194 |
|
| 1195 |
uint64_t getBlockCount() const { return BlockCount; } |
| 1196 |
void addBlockCount(uint64_t C) { BlockCount += C; } |
| 1197 |
void setBlockCount(uint64_t C) { BlockCount = C; } |
| 1198 |
|
| 1199 |
gvsummary_iterator begin() { return GlobalValueMap.begin(); } |
| 1200 |
const_gvsummary_iterator begin() const { return GlobalValueMap.begin(); } |
| 1201 |
gvsummary_iterator end() { return GlobalValueMap.end(); } |
| 1202 |
const_gvsummary_iterator end() const { return GlobalValueMap.end(); } |
| 1203 |
size_t size() const { return GlobalValueMap.size(); } |
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
static void discoverNodes(ValueInfo V, |
| 1208 |
std::map<ValueInfo, bool> &FunctionHasParent) { |
| 1209 |
if (!V.getSummaryList().size()) |
| 1210 |
return; |
| 1211 |
|
| 1212 |
|
| 1213 |
auto S = FunctionHasParent.emplace(V, false); |
| 1214 |
|
| 1215 |
|
| 1216 |
if (!S.second) |
| 1217 |
return; |
| 1218 |
|
| 1219 |
FunctionSummary *F = |
| 1220 |
dyn_cast<FunctionSummary>(V.getSummaryList().front().get()); |
| 1221 |
assert(F != nullptr && "Expected FunctionSummary node"); |
| 1222 |
|
| 1223 |
for (auto &C : F->calls()) { |
| 1224 |
|
| 1225 |
auto S = FunctionHasParent.emplace(C.first, true); |
| 1226 |
|
| 1227 |
|
| 1228 |
if (!S.second && S.first->second) |
| 1229 |
continue; |
| 1230 |
|
| 1231 |
if (S.second) |
| 1232 |
discoverNodes(C.first, FunctionHasParent); |
| 1233 |
else |
| 1234 |
S.first->second = true; |
| 1235 |
} |
| 1236 |
} |
| 1237 |
|
| 1238 |
|
| 1239 |
FunctionSummary calculateCallGraphRoot() { |
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
std::map<ValueInfo, bool> FunctionHasParent; |
| 1244 |
|
| 1245 |
for (auto &S : *this) { |
| 1246 |
|
| 1247 |
if (!S.second.SummaryList.size() || |
| 1248 |
!isa<FunctionSummary>(S.second.SummaryList.front().get())) |
| 1249 |
continue; |
| 1250 |
discoverNodes(ValueInfo(HaveGVs, &S), FunctionHasParent); |
| 1251 |
} |
| 1252 |
|
| 1253 |
std::vector<FunctionSummary::EdgeTy> Edges; |
| 1254 |
|
| 1255 |
for (auto &P : FunctionHasParent) { |
| 1256 |
if (P.second) |
| 1257 |
continue; |
| 1258 |
Edges.push_back(std::make_pair(P.first, CalleeInfo{})); |
| 1259 |
} |
| 1260 |
if (Edges.empty()) { |
| 1261 |
|
| 1262 |
return FunctionSummary::makeDummyFunctionSummary({}); |
| 1263 |
} |
| 1264 |
auto CallGraphRoot = FunctionSummary::makeDummyFunctionSummary(Edges); |
| 1265 |
return CallGraphRoot; |
| 1266 |
} |
| 1267 |
|
| 1268 |
bool withGlobalValueDeadStripping() const { |
| 1269 |
return WithGlobalValueDeadStripping; |
| 1270 |
} |
| 1271 |
void setWithGlobalValueDeadStripping() { |
| 1272 |
WithGlobalValueDeadStripping = true; |
| 1273 |
} |
| 1274 |
|
| 1275 |
bool withAttributePropagation() const { return WithAttributePropagation; } |
| 1276 |
void setWithAttributePropagation() { |
| 1277 |
WithAttributePropagation = true; |
| 1278 |
} |
| 1279 |
|
| 1280 |
bool withDSOLocalPropagation() const { return WithDSOLocalPropagation; } |
| 1281 |
void setWithDSOLocalPropagation() { WithDSOLocalPropagation = true; } |
| 1282 |
|
| 1283 |
bool isReadOnly(const GlobalVarSummary *GVS) const { |
| 1284 |
return WithAttributePropagation && GVS->maybeReadOnly(); |
| 1285 |
} |
| 1286 |
bool isWriteOnly(const GlobalVarSummary *GVS) const { |
| 1287 |
return WithAttributePropagation && GVS->maybeWriteOnly(); |
| 1288 |
} |
| 1289 |
|
| 1290 |
bool hasSyntheticEntryCounts() const { return HasSyntheticEntryCounts; } |
| 1291 |
void setHasSyntheticEntryCounts() { HasSyntheticEntryCounts = true; } |
| 1292 |
|
| 1293 |
bool skipModuleByDistributedBackend() const { |
| 1294 |
return SkipModuleByDistributedBackend; |
| 1295 |
} |
| 1296 |
void setSkipModuleByDistributedBackend() { |
| 1297 |
SkipModuleByDistributedBackend = true; |
| 1298 |
} |
| 1299 |
|
| 1300 |
bool enableSplitLTOUnit() const { return EnableSplitLTOUnit; } |
| 1301 |
void setEnableSplitLTOUnit() { EnableSplitLTOUnit = true; } |
| 1302 |
|
| 1303 |
bool partiallySplitLTOUnits() const { return PartiallySplitLTOUnits; } |
| 1304 |
void setPartiallySplitLTOUnits() { PartiallySplitLTOUnits = true; } |
| 1305 |
|
| 1306 |
bool hasParamAccess() const { return HasParamAccess; } |
| 1307 |
|
| 1308 |
bool isGlobalValueLive(const GlobalValueSummary *GVS) const { |
| 1309 |
return !WithGlobalValueDeadStripping || GVS->isLive(); |
| 1310 |
} |
| 1311 |
bool isGUIDLive(GlobalValue::GUID GUID) const; |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const { |
| 1316 |
return ValueInfo(HaveGVs, &R); |
| 1317 |
} |
| 1318 |
|
| 1319 |
|
| 1320 |
ValueInfo getValueInfo(GlobalValue::GUID GUID) const { |
| 1321 |
auto I = GlobalValueMap.find(GUID); |
| 1322 |
return ValueInfo(HaveGVs, I == GlobalValueMap.end() ? nullptr : &*I); |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID) { |
| 1327 |
return ValueInfo(HaveGVs, getOrInsertValuePtr(GUID)); |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
StringRef saveString(StringRef String) { return Saver.save(String); } |
| 1334 |
|
| 1335 |
|
| 1336 |
ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID, StringRef Name) { |
| 1337 |
assert(!HaveGVs); |
| 1338 |
auto VP = getOrInsertValuePtr(GUID); |
| 1339 |
VP->second.U.Name = Name; |
| 1340 |
return ValueInfo(HaveGVs, VP); |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
ValueInfo getOrInsertValueInfo(const GlobalValue *GV) { |
| 1345 |
assert(HaveGVs); |
| 1346 |
auto VP = getOrInsertValuePtr(GV->getGUID()); |
| 1347 |
VP->second.U.GV = GV; |
| 1348 |
return ValueInfo(HaveGVs, VP); |
| 1349 |
} |
| 1350 |
|
| 1351 |
|
| 1352 |
GlobalValue::GUID getGUIDFromOriginalID(GlobalValue::GUID OriginalID) const { |
| 1353 |
const auto I = OidGuidMap.find(OriginalID); |
| 1354 |
return I == OidGuidMap.end() ? 0 : I->second; |
| 1355 |
} |
| 1356 |
|
| 1357 |
std::set<std::string> &cfiFunctionDefs() { return CfiFunctionDefs; } |
| 1358 |
const std::set<std::string> &cfiFunctionDefs() const { return CfiFunctionDefs; } |
| 1359 |
|
| 1360 |
std::set<std::string> &cfiFunctionDecls() { return CfiFunctionDecls; } |
| 1361 |
const std::set<std::string> &cfiFunctionDecls() const { return CfiFunctionDecls; } |
| 1362 |
|
| 1363 |
|
| 1364 |
void addGlobalValueSummary(const GlobalValue &GV, |
| 1365 |
std::unique_ptr<GlobalValueSummary> Summary) { |
| 1366 |
addGlobalValueSummary(getOrInsertValueInfo(&GV), std::move(Summary)); |
| 1367 |
} |
| 1368 |
|
| 1369 |
|
| 1370 |
void addGlobalValueSummary(StringRef ValueName, |
| 1371 |
std::unique_ptr<GlobalValueSummary> Summary) { |
| 1372 |
addGlobalValueSummary(getOrInsertValueInfo(GlobalValue::getGUID(ValueName)), |
| 1373 |
std::move(Summary)); |
| 1374 |
} |
| 1375 |
|
| 1376 |
|
| 1377 |
void addGlobalValueSummary(ValueInfo VI, |
| 1378 |
std::unique_ptr<GlobalValueSummary> Summary) { |
| 1379 |
if (const FunctionSummary *FS = dyn_cast<FunctionSummary>(Summary.get())) |
| 1380 |
HasParamAccess |= !FS->paramAccesses().empty(); |
| 1381 |
addOriginalName(VI.getGUID(), Summary->getOriginalName()); |
| 1382 |
|
| 1383 |
|
| 1384 |
const_cast<GlobalValueSummaryMapTy::value_type *>(VI.getRef()) |
| 1385 |
->second.SummaryList.push_back(std::move(Summary)); |
| 1386 |
} |
| 1387 |
|
| 1388 |
|
| 1389 |
void addOriginalName(GlobalValue::GUID ValueGUID, |
| 1390 |
GlobalValue::GUID OrigGUID) { |
| 1391 |
if (OrigGUID == 0 || ValueGUID == OrigGUID) |
| 1392 |
return; |
| 1393 |
if (OidGuidMap.count(OrigGUID) && OidGuidMap[OrigGUID] != ValueGUID) |
| 1394 |
OidGuidMap[OrigGUID] = 0; |
| 1395 |
else |
| 1396 |
OidGuidMap[OrigGUID] = ValueGUID; |
| 1397 |
} |
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
GlobalValueSummary *findSummaryInModule(ValueInfo VI, StringRef ModuleId) const { |
| 1402 |
auto SummaryList = VI.getSummaryList(); |
| 1403 |
auto Summary = |
| 1404 |
llvm::find_if(SummaryList, |
| 1405 |
[&](const std::unique_ptr<GlobalValueSummary> &Summary) { |
| 1406 |
return Summary->modulePath() == ModuleId; |
| 1407 |
}); |
| 1408 |
if (Summary == SummaryList.end()) |
| 1409 |
return nullptr; |
| 1410 |
return Summary->get(); |
| 1411 |
} |
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
GlobalValueSummary *findSummaryInModule(GlobalValue::GUID ValueGUID, |
| 1416 |
StringRef ModuleId) const { |
| 1417 |
auto CalleeInfo = getValueInfo(ValueGUID); |
| 1418 |
if (!CalleeInfo) |
| 1419 |
return nullptr; |
| 1420 |
return findSummaryInModule(CalleeInfo, ModuleId); |
| 1421 |
} |
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
GlobalValueSummary *getGlobalValueSummary(const GlobalValue &GV, |
| 1426 |
bool PerModuleIndex = true) const { |
| 1427 |
assert(GV.hasName() && "Can't get GlobalValueSummary for GV with no name"); |
| 1428 |
return getGlobalValueSummary(GV.getGUID(), PerModuleIndex); |
| 1429 |
} |
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
GlobalValueSummary *getGlobalValueSummary(GlobalValue::GUID ValueGUID, |
| 1435 |
bool PerModuleIndex = true) const; |
| 1436 |
|
| 1437 |
|
| 1438 |
const StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() const { |
| 1439 |
return ModulePathStringTable; |
| 1440 |
} |
| 1441 |
|
| 1442 |
|
| 1443 |
StringMap<std::pair<uint64_t, ModuleHash>> &modulePaths() { |
| 1444 |
return ModulePathStringTable; |
| 1445 |
} |
| 1446 |
|
| 1447 |
|
| 1448 |
uint64_t getModuleId(const StringRef ModPath) const { |
| 1449 |
return ModulePathStringTable.lookup(ModPath).first; |
| 1450 |
} |
| 1451 |
|
| 1452 |
|
| 1453 |
const ModuleHash &getModuleHash(const StringRef ModPath) const { |
| 1454 |
auto It = ModulePathStringTable.find(ModPath); |
| 1455 |
assert(It != ModulePathStringTable.end() && "Module not registered"); |
| 1456 |
return It->second.second; |
| 1457 |
} |
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash) { |
| 1462 |
SmallString<256> NewName(Name); |
| 1463 |
NewName += ".llvm."; |
| 1464 |
NewName += utostr((uint64_t(ModHash[0]) << 32) | |
| 1465 |
ModHash[1]); |
| 1466 |
return std::string(NewName.str()); |
| 1467 |
} |
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
static StringRef getOriginalNameBeforePromote(StringRef Name) { |
| 1474 |
std::pair<StringRef, StringRef> Pair = Name.rsplit(".llvm."); |
| 1475 |
return Pair.first; |
| 1476 |
} |
| 1477 |
|
| 1478 |
typedef ModulePathStringTableTy::value_type ModuleInfo; |
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
ModuleInfo *addModule(StringRef ModPath, uint64_t ModId, |
| 1483 |
ModuleHash Hash = ModuleHash{{0}}) { |
| 1484 |
return &*ModulePathStringTable.insert({ModPath, {ModId, Hash}}).first; |
| 1485 |
} |
| 1486 |
|
| 1487 |
|
| 1488 |
ModuleInfo *getModule(StringRef ModPath) { |
| 1489 |
auto It = ModulePathStringTable.find(ModPath); |
| 1490 |
assert(It != ModulePathStringTable.end() && "Module not registered"); |
| 1491 |
return &*It; |
| 1492 |
} |
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
bool hasExportedFunctions(const Module &M) const { |
| 1498 |
return ModulePathStringTable.count(M.getModuleIdentifier()); |
| 1499 |
} |
| 1500 |
|
| 1501 |
const TypeIdSummaryMapTy &typeIds() const { return TypeIdMap; } |
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
TypeIdSummary &getOrInsertTypeIdSummary(StringRef TypeId) { |
| 1507 |
auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId)); |
| 1508 |
for (auto It = TidIter.first; It != TidIter.second; ++It) |
| 1509 |
if (It->second.first == TypeId) |
| 1510 |
return It->second.second; |
| 1511 |
auto It = TypeIdMap.insert( |
| 1512 |
{GlobalValue::getGUID(TypeId), {std::string(TypeId), TypeIdSummary()}}); |
| 1513 |
return It->second.second; |
| 1514 |
} |
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
const TypeIdSummary *getTypeIdSummary(StringRef TypeId) const { |
| 1519 |
auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId)); |
| 1520 |
for (auto It = TidIter.first; It != TidIter.second; ++It) |
| 1521 |
if (It->second.first == TypeId) |
| 1522 |
return &It->second.second; |
| 1523 |
return nullptr; |
| 1524 |
} |
| 1525 |
|
| 1526 |
TypeIdSummary *getTypeIdSummary(StringRef TypeId) { |
| 1527 |
return const_cast<TypeIdSummary *>( |
| 1528 |
static_cast<const ModuleSummaryIndex *>(this)->getTypeIdSummary( |
| 1529 |
TypeId)); |
| 1530 |
} |
| 1531 |
|
| 1532 |
const auto &typeIdCompatibleVtableMap() const { |
| 1533 |
return TypeIdCompatibleVtableMap; |
| 1534 |
} |
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
TypeIdCompatibleVtableInfo & |
| 1540 |
getOrInsertTypeIdCompatibleVtableSummary(StringRef TypeId) { |
| 1541 |
return TypeIdCompatibleVtableMap[std::string(TypeId)]; |
| 1542 |
} |
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
Optional<TypeIdCompatibleVtableInfo> |
| 1547 |
getTypeIdCompatibleVtableSummary(StringRef TypeId) const { |
| 1548 |
auto I = TypeIdCompatibleVtableMap.find(TypeId); |
| 1549 |
if (I == TypeIdCompatibleVtableMap.end()) |
| 1550 |
return None; |
| 1551 |
return I->second; |
| 1552 |
} |
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
void collectDefinedFunctionsForModule(StringRef ModulePath, |
| 1557 |
GVSummaryMapTy &GVSummaryMap) const; |
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
template <class Map> |
| 1562 |
void |
| 1563 |
collectDefinedGVSummariesPerModule(Map &ModuleToDefinedGVSummaries) const { |
| 1564 |
for (auto &GlobalList : *this) { |
| 1565 |
auto GUID = GlobalList.first; |
| 1566 |
for (auto &Summary : GlobalList.second.SummaryList) { |
| 1567 |
ModuleToDefinedGVSummaries[Summary->modulePath()][GUID] = Summary.get(); |
| 1568 |
} |
| 1569 |
} |
| 1570 |
} |
| 1571 |
|
| 1572 |
|
| 1573 |
void print(raw_ostream &OS, bool IsForDebug = false) const; |
| 1574 |
|
| 1575 |
|
| 1576 |
void dump() const; |
| 1577 |
|
| 1578 |
|
| 1579 |
void |
| 1580 |
exportToDot(raw_ostream &OS, |
| 1581 |
const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) const; |
| 1582 |
|
| 1583 |
|
| 1584 |
void dumpSCCs(raw_ostream &OS); |
| 1585 |
|
| 1586 |
|
| 1587 |
void propagateAttributes(const DenseSet<GlobalValue::GUID> &PreservedSymbols); |
| 1588 |
|
| 1589 |
|
| 1590 |
bool canImportGlobalVar(GlobalValueSummary *S, bool AnalyzeRefs) const; |
| 1591 |
}; |
| 1592 |
|
| 1593 |
|
| 1594 |
template <> struct GraphTraits<ValueInfo> { |
| 1595 |
typedef ValueInfo NodeRef; |
| 1596 |
using EdgeRef = FunctionSummary::EdgeTy &; |
| 1597 |
|
| 1598 |
static NodeRef valueInfoFromEdge(FunctionSummary::EdgeTy &P) { |
| 1599 |
return P.first; |
| 1600 |
} |
| 1601 |
using ChildIteratorType = |
| 1602 |
mapped_iterator<std::vector<FunctionSummary::EdgeTy>::iterator, |
| 1603 |
decltype(&valueInfoFromEdge)>; |
| 1604 |
|
| 1605 |
using ChildEdgeIteratorType = std::vector<FunctionSummary::EdgeTy>::iterator; |
| 1606 |
|
| 1607 |
static NodeRef getEntryNode(ValueInfo V) { return V; } |
| 1608 |
|
| 1609 |
static ChildIteratorType child_begin(NodeRef N) { |
| 1610 |
if (!N.getSummaryList().size()) |
| 1611 |
return ChildIteratorType( |
| 1612 |
FunctionSummary::ExternalNode.CallGraphEdgeList.begin(), |
| 1613 |
&valueInfoFromEdge); |
| 1614 |
FunctionSummary *F = |
| 1615 |
cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject()); |
| 1616 |
return ChildIteratorType(F->CallGraphEdgeList.begin(), &valueInfoFromEdge); |
| 1617 |
} |
| 1618 |
|
| 1619 |
static ChildIteratorType child_end(NodeRef N) { |
| 1620 |
if (!N.getSummaryList().size()) |
| 1621 |
return ChildIteratorType( |
| 1622 |
FunctionSummary::ExternalNode.CallGraphEdgeList.end(), |
| 1623 |
&valueInfoFromEdge); |
| 1624 |
FunctionSummary *F = |
| 1625 |
cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject()); |
| 1626 |
return ChildIteratorType(F->CallGraphEdgeList.end(), &valueInfoFromEdge); |
| 1627 |
} |
| 1628 |
|
| 1629 |
static ChildEdgeIteratorType child_edge_begin(NodeRef N) { |
| 1630 |
if (!N.getSummaryList().size()) |
| 1631 |
return FunctionSummary::ExternalNode.CallGraphEdgeList.begin(); |
| 1632 |
|
| 1633 |
FunctionSummary *F = |
| 1634 |
cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject()); |
| 1635 |
return F->CallGraphEdgeList.begin(); |
| 1636 |
} |
| 1637 |
|
| 1638 |
static ChildEdgeIteratorType child_edge_end(NodeRef N) { |
| 1639 |
if (!N.getSummaryList().size()) |
| 1640 |
return FunctionSummary::ExternalNode.CallGraphEdgeList.end(); |
| 1641 |
|
| 1642 |
FunctionSummary *F = |
| 1643 |
cast<FunctionSummary>(N.getSummaryList().front()->getBaseObject()); |
| 1644 |
return F->CallGraphEdgeList.end(); |
| 1645 |
} |
| 1646 |
|
| 1647 |
static NodeRef edge_dest(EdgeRef E) { return E.first; } |
| 1648 |
}; |
| 1649 |
|
| 1650 |
template <> |
| 1651 |
struct GraphTraits<ModuleSummaryIndex *> : public GraphTraits<ValueInfo> { |
| 1652 |
static NodeRef getEntryNode(ModuleSummaryIndex *I) { |
| 1653 |
std::unique_ptr<GlobalValueSummary> Root = |
| 1654 |
std::make_unique<FunctionSummary>(I->calculateCallGraphRoot()); |
| 1655 |
GlobalValueSummaryInfo G(I->haveGVs()); |
| 1656 |
G.SummaryList.push_back(std::move(Root)); |
| 1657 |
static auto P = |
| 1658 |
GlobalValueSummaryMapTy::value_type(GlobalValue::GUID(0), std::move(G)); |
| 1659 |
return ValueInfo(I->haveGVs(), &P); |
| 1660 |
} |
| 1661 |
}; |
| 1662 |
} |
| 1663 |
|
| 1664 |
#endif |