LLVM  11.0.0git
WholeProgramDevirt.cpp
Go to the documentation of this file.
1 //===- WholeProgramDevirt.cpp - Whole program virtual call optimization ---===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass implements whole program optimization of virtual calls in cases
10 // where we know (via !type metadata) that the list of callees is fixed. This
11 // includes the following:
12 // - Single implementation devirtualization: if a virtual call has a single
13 // possible callee, replace all calls with a direct call to that callee.
14 // - Virtual constant propagation: if the virtual function's return type is an
15 // integer <=64 bits and all possible callees are readnone, for each class and
16 // each list of constant arguments: evaluate the function, store the return
17 // value alongside the virtual table, and rewrite each virtual call as a load
18 // from the virtual table.
19 // - Uniform return value optimization: if the conditions for virtual constant
20 // propagation hold and each function returns the same constant value, replace
21 // each virtual call with that constant.
22 // - Unique return value optimization for i1 return values: if the conditions
23 // for virtual constant propagation hold and a single vtable's function
24 // returns 0, or a single vtable's function returns 1, replace each virtual
25 // call with a comparison of the vptr against that vtable's address.
26 //
27 // This pass is intended to be used during the regular and thin LTO pipelines:
28 //
29 // During regular LTO, the pass determines the best optimization for each
30 // virtual call and applies the resolutions directly to virtual calls that are
31 // eligible for virtual call optimization (i.e. calls that use either of the
32 // llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics).
33 //
34 // During hybrid Regular/ThinLTO, the pass operates in two phases:
35 // - Export phase: this is run during the thin link over a single merged module
36 // that contains all vtables with !type metadata that participate in the link.
37 // The pass computes a resolution for each virtual call and stores it in the
38 // type identifier summary.
39 // - Import phase: this is run during the thin backends over the individual
40 // modules. The pass applies the resolutions previously computed during the
41 // import phase to each eligible virtual call.
42 //
43 // During ThinLTO, the pass operates in two phases:
44 // - Export phase: this is run during the thin link over the index which
45 // contains a summary of all vtables with !type metadata that participate in
46 // the link. It computes a resolution for each virtual call and stores it in
47 // the type identifier summary. Only single implementation devirtualization
48 // is supported.
49 // - Import phase: (same as with hybrid case above).
50 //
51 //===----------------------------------------------------------------------===//
52 
54 #include "llvm/ADT/ArrayRef.h"
55 #include "llvm/ADT/DenseMap.h"
56 #include "llvm/ADT/DenseMapInfo.h"
57 #include "llvm/ADT/DenseSet.h"
58 #include "llvm/ADT/MapVector.h"
59 #include "llvm/ADT/SmallVector.h"
60 #include "llvm/ADT/Triple.h"
68 #include "llvm/IR/Constants.h"
69 #include "llvm/IR/DataLayout.h"
70 #include "llvm/IR/DebugLoc.h"
71 #include "llvm/IR/DerivedTypes.h"
72 #include "llvm/IR/Dominators.h"
73 #include "llvm/IR/Function.h"
74 #include "llvm/IR/GlobalAlias.h"
75 #include "llvm/IR/GlobalVariable.h"
76 #include "llvm/IR/IRBuilder.h"
77 #include "llvm/IR/InstrTypes.h"
78 #include "llvm/IR/Instruction.h"
79 #include "llvm/IR/Instructions.h"
80 #include "llvm/IR/Intrinsics.h"
81 #include "llvm/IR/LLVMContext.h"
82 #include "llvm/IR/Metadata.h"
83 #include "llvm/IR/Module.h"
85 #include "llvm/InitializePasses.h"
86 #include "llvm/Pass.h"
87 #include "llvm/PassRegistry.h"
88 #include "llvm/Support/Casting.h"
90 #include "llvm/Support/Errc.h"
91 #include "llvm/Support/Error.h"
95 #include "llvm/Transforms/IPO.h"
98 #include <algorithm>
99 #include <cstddef>
100 #include <map>
101 #include <set>
102 #include <string>
103 
104 using namespace llvm;
105 using namespace wholeprogramdevirt;
106 
107 #define DEBUG_TYPE "wholeprogramdevirt"
108 
110  "wholeprogramdevirt-summary-action",
111  cl::desc("What to do with the summary when running this pass"),
112  cl::values(clEnumValN(PassSummaryAction::None, "none", "Do nothing"),
114  "Import typeid resolutions from summary and globals"),
116  "Export typeid resolutions to summary and globals")),
117  cl::Hidden);
118 
120  "wholeprogramdevirt-read-summary",
121  cl::desc(
122  "Read summary from given bitcode or YAML file before running pass"),
123  cl::Hidden);
124 
126  "wholeprogramdevirt-write-summary",
127  cl::desc("Write summary to given bitcode or YAML file after running pass. "
128  "Output file format is deduced from extension: *.bc means writing "
129  "bitcode, otherwise YAML"),
130  cl::Hidden);
131 
132 static cl::opt<unsigned>
133  ClThreshold("wholeprogramdevirt-branch-funnel-threshold", cl::Hidden,
135  cl::desc("Maximum number of call targets per "
136  "call site to enable branch funnels"));
137 
138 static cl::opt<bool>
139  PrintSummaryDevirt("wholeprogramdevirt-print-index-based", cl::Hidden,
140  cl::init(false), cl::ZeroOrMore,
141  cl::desc("Print index-based devirtualization messages"));
142 
143 /// Provide a way to force enable whole program visibility in tests.
144 /// This is needed to support legacy tests that don't contain
145 /// !vcall_visibility metadata (the mere presense of type tests
146 /// previously implied hidden visibility).
148  WholeProgramVisibility("whole-program-visibility", cl::init(false),
150  cl::desc("Enable whole program visibility"));
151 
152 /// Provide a way to force disable whole program for debugging or workarounds,
153 /// when enabled via the linker.
155  "disable-whole-program-visibility", cl::init(false), cl::Hidden,
157  cl::desc("Disable whole program visibility (overrides enabling options)"));
158 
159 /// Provide way to prevent certain function from being devirtualized
161  SkipFunctionNames("wholeprogramdevirt-skip",
162  cl::desc("Prevent function(s) from being devirtualized"),
164 
165 namespace {
166 struct PatternList {
167  std::vector<GlobPattern> Patterns;
168  template <class T> void init(const T &StringList) {
169  for (const auto &S : StringList)
171  Patterns.push_back(std::move(*Pat));
172  }
173  bool match(StringRef S) {
174  for (const GlobPattern &P : Patterns)
175  if (P.match(S))
176  return true;
177  return false;
178  }
179 };
180 } // namespace
181 
182 // Find the minimum offset that we may store a value of size Size bits at. If
183 // IsAfter is set, look for an offset before the object, otherwise look for an
184 // offset after the object.
185 uint64_t
187  bool IsAfter, uint64_t Size) {
188  // Find a minimum offset taking into account only vtable sizes.
189  uint64_t MinByte = 0;
190  for (const VirtualCallTarget &Target : Targets) {
191  if (IsAfter)
192  MinByte = std::max(MinByte, Target.minAfterBytes());
193  else
194  MinByte = std::max(MinByte, Target.minBeforeBytes());
195  }
196 
197  // Build a vector of arrays of bytes covering, for each target, a slice of the
198  // used region (see AccumBitVector::BytesUsed in
199  // llvm/Transforms/IPO/WholeProgramDevirt.h) starting at MinByte. Effectively,
200  // this aligns the used regions to start at MinByte.
201  //
202  // In this example, A, B and C are vtables, # is a byte already allocated for
203  // a virtual function pointer, AAAA... (etc.) are the used regions for the
204  // vtables and Offset(X) is the value computed for the Offset variable below
205  // for X.
206  //
207  // Offset(A)
208  // | |
209  // |MinByte
210  // A: ################AAAAAAAA|AAAAAAAA
211  // B: ########BBBBBBBBBBBBBBBB|BBBB
212  // C: ########################|CCCCCCCCCCCCCCCC
213  // | Offset(B) |
214  //
215  // This code produces the slices of A, B and C that appear after the divider
216  // at MinByte.
217  std::vector<ArrayRef<uint8_t>> Used;
218  for (const VirtualCallTarget &Target : Targets) {
219  ArrayRef<uint8_t> VTUsed = IsAfter ? Target.TM->Bits->After.BytesUsed
220  : Target.TM->Bits->Before.BytesUsed;
221  uint64_t Offset = IsAfter ? MinByte - Target.minAfterBytes()
222  : MinByte - Target.minBeforeBytes();
223 
224  // Disregard used regions that are smaller than Offset. These are
225  // effectively all-free regions that do not need to be checked.
226  if (VTUsed.size() > Offset)
227  Used.push_back(VTUsed.slice(Offset));
228  }
229 
230  if (Size == 1) {
231  // Find a free bit in each member of Used.
232  for (unsigned I = 0;; ++I) {
233  uint8_t BitsUsed = 0;
234  for (auto &&B : Used)
235  if (I < B.size())
236  BitsUsed |= B[I];
237  if (BitsUsed != 0xff)
238  return (MinByte + I) * 8 +
239  countTrailingZeros(uint8_t(~BitsUsed), ZB_Undefined);
240  }
241  } else {
242  // Find a free (Size/8) byte region in each member of Used.
243  // FIXME: see if alignment helps.
244  for (unsigned I = 0;; ++I) {
245  for (auto &&B : Used) {
246  unsigned Byte = 0;
247  while ((I + Byte) < B.size() && Byte < (Size / 8)) {
248  if (B[I + Byte])
249  goto NextI;
250  ++Byte;
251  }
252  }
253  return (MinByte + I) * 8;
254  NextI:;
255  }
256  }
257 }
258 
260  MutableArrayRef<VirtualCallTarget> Targets, uint64_t AllocBefore,
261  unsigned BitWidth, int64_t &OffsetByte, uint64_t &OffsetBit) {
262  if (BitWidth == 1)
263  OffsetByte = -(AllocBefore / 8 + 1);
264  else
265  OffsetByte = -((AllocBefore + 7) / 8 + (BitWidth + 7) / 8);
266  OffsetBit = AllocBefore % 8;
267 
268  for (VirtualCallTarget &Target : Targets) {
269  if (BitWidth == 1)
270  Target.setBeforeBit(AllocBefore);
271  else
272  Target.setBeforeBytes(AllocBefore, (BitWidth + 7) / 8);
273  }
274 }
275 
277  MutableArrayRef<VirtualCallTarget> Targets, uint64_t AllocAfter,
278  unsigned BitWidth, int64_t &OffsetByte, uint64_t &OffsetBit) {
279  if (BitWidth == 1)
280  OffsetByte = AllocAfter / 8;
281  else
282  OffsetByte = (AllocAfter + 7) / 8;
283  OffsetBit = AllocAfter % 8;
284 
285  for (VirtualCallTarget &Target : Targets) {
286  if (BitWidth == 1)
287  Target.setAfterBit(AllocAfter);
288  else
289  Target.setAfterBytes(AllocAfter, (BitWidth + 7) / 8);
290  }
291 }
292 
294  : Fn(Fn), TM(TM),
295  IsBigEndian(Fn->getParent()->getDataLayout().isBigEndian()), WasDevirt(false) {}
296 
297 namespace {
298 
299 // A slot in a set of virtual tables. The TypeID identifies the set of virtual
300 // tables, and the ByteOffset is the offset in bytes from the address point to
301 // the virtual function pointer.
302 struct VTableSlot {
303  Metadata *TypeID;
304  uint64_t ByteOffset;
305 };
306 
307 } // end anonymous namespace
308 
309 namespace llvm {
310 
311 template <> struct DenseMapInfo<VTableSlot> {
312  static VTableSlot getEmptyKey() {
315  }
316  static VTableSlot getTombstoneKey() {
319  }
320  static unsigned getHashValue(const VTableSlot &I) {
321  return DenseMapInfo<Metadata *>::getHashValue(I.TypeID) ^
323  }
324  static bool isEqual(const VTableSlot &LHS,
325  const VTableSlot &RHS) {
326  return LHS.TypeID == RHS.TypeID && LHS.ByteOffset == RHS.ByteOffset;
327  }
328 };
329 
330 template <> struct DenseMapInfo<VTableSlotSummary> {
334  }
338  }
339  static unsigned getHashValue(const VTableSlotSummary &I) {
342  }
343  static bool isEqual(const VTableSlotSummary &LHS,
344  const VTableSlotSummary &RHS) {
345  return LHS.TypeID == RHS.TypeID && LHS.ByteOffset == RHS.ByteOffset;
346  }
347 };
348 
349 } // end namespace llvm
350 
351 namespace {
352 
353 // A virtual call site. VTable is the loaded virtual table pointer, and CS is
354 // the indirect virtual call.
355 struct VirtualCallSite {
356  Value *VTable = nullptr;
357  CallBase &CB;
358 
359  // If non-null, this field points to the associated unsafe use count stored in
360  // the DevirtModule::NumUnsafeUsesForTypeTest map below. See the description
361  // of that field for details.
362  unsigned *NumUnsafeUses = nullptr;
363 
364  void
365  emitRemark(const StringRef OptName, const StringRef TargetName,
367  Function *F = CB.getCaller();
368  DebugLoc DLoc = CB.getDebugLoc();
369  BasicBlock *Block = CB.getParent();
370 
371  using namespace ore;
372  OREGetter(F).emit(OptimizationRemark(DEBUG_TYPE, OptName, DLoc, Block)
373  << NV("Optimization", OptName)
374  << ": devirtualized a call to "
375  << NV("FunctionName", TargetName));
376  }
377 
378  void replaceAndErase(
379  const StringRef OptName, const StringRef TargetName, bool RemarksEnabled,
381  Value *New) {
382  if (RemarksEnabled)
383  emitRemark(OptName, TargetName, OREGetter);
384  CB.replaceAllUsesWith(New);
385  if (auto *II = dyn_cast<InvokeInst>(&CB)) {
386  BranchInst::Create(II->getNormalDest(), &CB);
387  II->getUnwindDest()->removePredecessor(II->getParent());
388  }
389  CB.eraseFromParent();
390  // This use is no longer unsafe.
391  if (NumUnsafeUses)
392  --*NumUnsafeUses;
393  }
394 };
395 
396 // Call site information collected for a specific VTableSlot and possibly a list
397 // of constant integer arguments. The grouping by arguments is handled by the
398 // VTableSlotInfo class.
399 struct CallSiteInfo {
400  /// The set of call sites for this slot. Used during regular LTO and the
401  /// import phase of ThinLTO (as well as the export phase of ThinLTO for any
402  /// call sites that appear in the merged module itself); in each of these
403  /// cases we are directly operating on the call sites at the IR level.
404  std::vector<VirtualCallSite> CallSites;
405 
406  /// Whether all call sites represented by this CallSiteInfo, including those
407  /// in summaries, have been devirtualized. This starts off as true because a
408  /// default constructed CallSiteInfo represents no call sites.
409  bool AllCallSitesDevirted = true;
410 
411  // These fields are used during the export phase of ThinLTO and reflect
412  // information collected from function summaries.
413 
414  /// Whether any function summary contains an llvm.assume(llvm.type.test) for
415  /// this slot.
416  bool SummaryHasTypeTestAssumeUsers = false;
417 
418  /// CFI-specific: a vector containing the list of function summaries that use
419  /// the llvm.type.checked.load intrinsic and therefore will require
420  /// resolutions for llvm.type.test in order to implement CFI checks if
421  /// devirtualization was unsuccessful. If devirtualization was successful, the
422  /// pass will clear this vector by calling markDevirt(). If at the end of the
423  /// pass the vector is non-empty, we will need to add a use of llvm.type.test
424  /// to each of the function summaries in the vector.
425  std::vector<FunctionSummary *> SummaryTypeCheckedLoadUsers;
426  std::vector<FunctionSummary *> SummaryTypeTestAssumeUsers;
427 
428  bool isExported() const {
429  return SummaryHasTypeTestAssumeUsers ||
430  !SummaryTypeCheckedLoadUsers.empty();
431  }
432 
433  void addSummaryTypeCheckedLoadUser(FunctionSummary *FS) {
434  SummaryTypeCheckedLoadUsers.push_back(FS);
435  AllCallSitesDevirted = false;
436  }
437 
438  void addSummaryTypeTestAssumeUser(FunctionSummary *FS) {
439  SummaryTypeTestAssumeUsers.push_back(FS);
440  SummaryHasTypeTestAssumeUsers = true;
441  AllCallSitesDevirted = false;
442  }
443 
444  void markDevirt() {
445  AllCallSitesDevirted = true;
446 
447  // As explained in the comment for SummaryTypeCheckedLoadUsers.
448  SummaryTypeCheckedLoadUsers.clear();
449  }
450 };
451 
452 // Call site information collected for a specific VTableSlot.
453 struct VTableSlotInfo {
454  // The set of call sites which do not have all constant integer arguments
455  // (excluding "this").
456  CallSiteInfo CSInfo;
457 
458  // The set of call sites with all constant integer arguments (excluding
459  // "this"), grouped by argument list.
460  std::map<std::vector<uint64_t>, CallSiteInfo> ConstCSInfo;
461 
462  void addCallSite(Value *VTable, CallBase &CB, unsigned *NumUnsafeUses);
463 
464 private:
465  CallSiteInfo &findCallSiteInfo(CallBase &CB);
466 };
467 
468 CallSiteInfo &VTableSlotInfo::findCallSiteInfo(CallBase &CB) {
469  std::vector<uint64_t> Args;
470  auto *CBType = dyn_cast<IntegerType>(CB.getType());
471  if (!CBType || CBType->getBitWidth() > 64 || CB.arg_empty())
472  return CSInfo;
473  for (auto &&Arg : make_range(CB.arg_begin() + 1, CB.arg_end())) {
474  auto *CI = dyn_cast<ConstantInt>(Arg);
475  if (!CI || CI->getBitWidth() > 64)
476  return CSInfo;
477  Args.push_back(CI->getZExtValue());
478  }
479  return ConstCSInfo[Args];
480 }
481 
482 void VTableSlotInfo::addCallSite(Value *VTable, CallBase &CB,
483  unsigned *NumUnsafeUses) {
484  auto &CSI = findCallSiteInfo(CB);
485  CSI.AllCallSitesDevirted = false;
486  CSI.CallSites.push_back({VTable, CB, NumUnsafeUses});
487 }
488 
489 struct DevirtModule {
490  Module &M;
493 
494  ModuleSummaryIndex *ExportSummary;
495  const ModuleSummaryIndex *ImportSummary;
496 
497  IntegerType *Int8Ty;
498  PointerType *Int8PtrTy;
500  IntegerType *Int64Ty;
501  IntegerType *IntPtrTy;
502  /// Sizeless array type, used for imported vtables. This provides a signal
503  /// to analyzers that these imports may alias, as they do for example
504  /// when multiple unique return values occur in the same vtable.
505  ArrayType *Int8Arr0Ty;
506 
507  bool RemarksEnabled;
509 
511 
512  // This map keeps track of the number of "unsafe" uses of a loaded function
513  // pointer. The key is the associated llvm.type.test intrinsic call generated
514  // by this pass. An unsafe use is one that calls the loaded function pointer
515  // directly. Every time we eliminate an unsafe use (for example, by
516  // devirtualizing it or by applying virtual constant propagation), we
517  // decrement the value stored in this map. If a value reaches zero, we can
518  // eliminate the type check by RAUWing the associated llvm.type.test call with
519  // true.
520  std::map<CallInst *, unsigned> NumUnsafeUsesForTypeTest;
521  PatternList FunctionsToSkip;
522 
523  DevirtModule(Module &M, function_ref<AAResults &(Function &)> AARGetter,
525  function_ref<DominatorTree &(Function &)> LookupDomTree,
526  ModuleSummaryIndex *ExportSummary,
527  const ModuleSummaryIndex *ImportSummary)
528  : M(M), AARGetter(AARGetter), LookupDomTree(LookupDomTree),
529  ExportSummary(ExportSummary), ImportSummary(ImportSummary),
530  Int8Ty(Type::getInt8Ty(M.getContext())),
531  Int8PtrTy(Type::getInt8PtrTy(M.getContext())),
533  Int64Ty(Type::getInt64Ty(M.getContext())),
534  IntPtrTy(M.getDataLayout().getIntPtrType(M.getContext(), 0)),
535  Int8Arr0Ty(ArrayType::get(Type::getInt8Ty(M.getContext()), 0)),
536  RemarksEnabled(areRemarksEnabled()), OREGetter(OREGetter) {
537  assert(!(ExportSummary && ImportSummary));
538  FunctionsToSkip.init(SkipFunctionNames);
539  }
540 
541  bool areRemarksEnabled();
542 
543  void scanTypeTestUsers(Function *TypeTestFunc);
544  void scanTypeCheckedLoadUsers(Function *TypeCheckedLoadFunc);
545 
546  void buildTypeIdentifierMap(
547  std::vector<VTableBits> &Bits,
548  DenseMap<Metadata *, std::set<TypeMemberInfo>> &TypeIdMap);
549  bool
550  tryFindVirtualCallTargets(std::vector<VirtualCallTarget> &TargetsForSlot,
551  const std::set<TypeMemberInfo> &TypeMemberInfos,
552  uint64_t ByteOffset);
553 
554  void applySingleImplDevirt(VTableSlotInfo &SlotInfo, Constant *TheFn,
555  bool &IsExported);
556  bool trySingleImplDevirt(ModuleSummaryIndex *ExportSummary,
557  MutableArrayRef<VirtualCallTarget> TargetsForSlot,
558  VTableSlotInfo &SlotInfo,
560 
561  void applyICallBranchFunnel(VTableSlotInfo &SlotInfo, Constant *JT,
562  bool &IsExported);
563  void tryICallBranchFunnel(MutableArrayRef<VirtualCallTarget> TargetsForSlot,
564  VTableSlotInfo &SlotInfo,
565  WholeProgramDevirtResolution *Res, VTableSlot Slot);
566 
567  bool tryEvaluateFunctionsWithArgs(
568  MutableArrayRef<VirtualCallTarget> TargetsForSlot,
570 
571  void applyUniformRetValOpt(CallSiteInfo &CSInfo, StringRef FnName,
572  uint64_t TheRetVal);
573  bool tryUniformRetValOpt(MutableArrayRef<VirtualCallTarget> TargetsForSlot,
574  CallSiteInfo &CSInfo,
576 
577  // Returns the global symbol name that is used to export information about the
578  // given vtable slot and list of arguments.
579  std::string getGlobalName(VTableSlot Slot, ArrayRef<uint64_t> Args,
580  StringRef Name);
581 
582  bool shouldExportConstantsAsAbsoluteSymbols();
583 
584  // This function is called during the export phase to create a symbol
585  // definition containing information about the given vtable slot and list of
586  // arguments.
587  void exportGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args, StringRef Name,
588  Constant *C);
589  void exportConstant(VTableSlot Slot, ArrayRef<uint64_t> Args, StringRef Name,
590  uint32_t Const, uint32_t &Storage);
591 
592  // This function is called during the import phase to create a reference to
593  // the symbol definition created during the export phase.
594  Constant *importGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args,
595  StringRef Name);
596  Constant *importConstant(VTableSlot Slot, ArrayRef<uint64_t> Args,
597  StringRef Name, IntegerType *IntTy,
598  uint32_t Storage);
599 
600  Constant *getMemberAddr(const TypeMemberInfo *M);
601 
602  void applyUniqueRetValOpt(CallSiteInfo &CSInfo, StringRef FnName, bool IsOne,
603  Constant *UniqueMemberAddr);
604  bool tryUniqueRetValOpt(unsigned BitWidth,
605  MutableArrayRef<VirtualCallTarget> TargetsForSlot,
606  CallSiteInfo &CSInfo,
608  VTableSlot Slot, ArrayRef<uint64_t> Args);
609 
610  void applyVirtualConstProp(CallSiteInfo &CSInfo, StringRef FnName,
611  Constant *Byte, Constant *Bit);
612  bool tryVirtualConstProp(MutableArrayRef<VirtualCallTarget> TargetsForSlot,
613  VTableSlotInfo &SlotInfo,
614  WholeProgramDevirtResolution *Res, VTableSlot Slot);
615 
616  void rebuildGlobal(VTableBits &B);
617 
618  // Apply the summary resolution for Slot to all virtual calls in SlotInfo.
619  void importResolution(VTableSlot Slot, VTableSlotInfo &SlotInfo);
620 
621  // If we were able to eliminate all unsafe uses for a type checked load,
622  // eliminate the associated type tests by replacing them with true.
623  void removeRedundantTypeTests();
624 
625  bool run();
626 
627  // Lower the module using the action and summary passed as command line
628  // arguments. For testing purposes only.
629  static bool
630  runForTesting(Module &M, function_ref<AAResults &(Function &)> AARGetter,
632  function_ref<DominatorTree &(Function &)> LookupDomTree);
633 };
634 
635 struct DevirtIndex {
636  ModuleSummaryIndex &ExportSummary;
637  // The set in which to record GUIDs exported from their module by
638  // devirtualization, used by client to ensure they are not internalized.
639  std::set<GlobalValue::GUID> &ExportedGUIDs;
640  // A map in which to record the information necessary to locate the WPD
641  // resolution for local targets in case they are exported by cross module
642  // importing.
643  std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap;
644 
646 
647  PatternList FunctionsToSkip;
648 
649  DevirtIndex(
650  ModuleSummaryIndex &ExportSummary,
651  std::set<GlobalValue::GUID> &ExportedGUIDs,
652  std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap)
653  : ExportSummary(ExportSummary), ExportedGUIDs(ExportedGUIDs),
654  LocalWPDTargetsMap(LocalWPDTargetsMap) {
655  FunctionsToSkip.init(SkipFunctionNames);
656  }
657 
658  bool tryFindVirtualCallTargets(std::vector<ValueInfo> &TargetsForSlot,
659  const TypeIdCompatibleVtableInfo TIdInfo,
660  uint64_t ByteOffset);
661 
662  bool trySingleImplDevirt(MutableArrayRef<ValueInfo> TargetsForSlot,
663  VTableSlotSummary &SlotSummary,
664  VTableSlotInfo &SlotInfo,
666  std::set<ValueInfo> &DevirtTargets);
667 
668  void run();
669 };
670 
671 struct WholeProgramDevirt : public ModulePass {
672  static char ID;
673 
674  bool UseCommandLine = false;
675 
676  ModuleSummaryIndex *ExportSummary = nullptr;
677  const ModuleSummaryIndex *ImportSummary = nullptr;
678 
679  WholeProgramDevirt() : ModulePass(ID), UseCommandLine(true) {
681  }
682 
683  WholeProgramDevirt(ModuleSummaryIndex *ExportSummary,
684  const ModuleSummaryIndex *ImportSummary)
685  : ModulePass(ID), ExportSummary(ExportSummary),
686  ImportSummary(ImportSummary) {
688  }
689 
690  bool runOnModule(Module &M) override {
691  if (skipModule(M))
692  return false;
693 
694  // In the new pass manager, we can request the optimization
695  // remark emitter pass on a per-function-basis, which the
696  // OREGetter will do for us.
697  // In the old pass manager, this is harder, so we just build
698  // an optimization remark emitter on the fly, when we need it.
699  std::unique_ptr<OptimizationRemarkEmitter> ORE;
700  auto OREGetter = [&](Function *F) -> OptimizationRemarkEmitter & {
701  ORE = std::make_unique<OptimizationRemarkEmitter>(F);
702  return *ORE;
703  };
704 
705  auto LookupDomTree = [this](Function &F) -> DominatorTree & {
706  return this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
707  };
708 
709  if (UseCommandLine)
710  return DevirtModule::runForTesting(M, LegacyAARGetter(*this), OREGetter,
711  LookupDomTree);
712 
713  return DevirtModule(M, LegacyAARGetter(*this), OREGetter, LookupDomTree,
714  ExportSummary, ImportSummary)
715  .run();
716  }
717 
718  void getAnalysisUsage(AnalysisUsage &AU) const override {
722  }
723 };
724 
725 } // end anonymous namespace
726 
727 INITIALIZE_PASS_BEGIN(WholeProgramDevirt, "wholeprogramdevirt",
728  "Whole program devirtualization", false, false)
733  "Whole program devirtualization", false, false)
734 char WholeProgramDevirt::ID = 0;
735 
736 ModulePass *
738  const ModuleSummaryIndex *ImportSummary) {
739  return new WholeProgramDevirt(ExportSummary, ImportSummary);
740 }
741 
743  ModuleAnalysisManager &AM) {
744  auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
745  auto AARGetter = [&](Function &F) -> AAResults & {
746  return FAM.getResult<AAManager>(F);
747  };
748  auto OREGetter = [&](Function *F) -> OptimizationRemarkEmitter & {
749  return FAM.getResult<OptimizationRemarkEmitterAnalysis>(*F);
750  };
751  auto LookupDomTree = [&FAM](Function &F) -> DominatorTree & {
752  return FAM.getResult<DominatorTreeAnalysis>(F);
753  };
754  if (!DevirtModule(M, AARGetter, OREGetter, LookupDomTree, ExportSummary,
755  ImportSummary)
756  .run())
757  return PreservedAnalyses::all();
758  return PreservedAnalyses::none();
759 }
760 
761 // Enable whole program visibility if enabled by client (e.g. linker) or
762 // internal option, and not force disabled.
763 static bool hasWholeProgramVisibility(bool WholeProgramVisibilityEnabledInLTO) {
764  return (WholeProgramVisibilityEnabledInLTO || WholeProgramVisibility) &&
766 }
767 
768 namespace llvm {
769 
770 /// If whole program visibility asserted, then upgrade all public vcall
771 /// visibility metadata on vtable definitions to linkage unit visibility in
772 /// Module IR (for regular or hybrid LTO).
774  bool WholeProgramVisibilityEnabledInLTO) {
775  if (!hasWholeProgramVisibility(WholeProgramVisibilityEnabledInLTO))
776  return;
777  for (GlobalVariable &GV : M.globals())
778  // Add linkage unit visibility to any variable with type metadata, which are
779  // the vtable definitions. We won't have an existing vcall_visibility
780  // metadata on vtable definitions with public visibility.
781  if (GV.hasMetadata(LLVMContext::MD_type) &&
782  GV.getVCallVisibility() == GlobalObject::VCallVisibilityPublic)
783  GV.setVCallVisibilityMetadata(GlobalObject::VCallVisibilityLinkageUnit);
784 }
785 
786 /// If whole program visibility asserted, then upgrade all public vcall
787 /// visibility metadata on vtable definition summaries to linkage unit
788 /// visibility in Module summary index (for ThinLTO).
790  bool WholeProgramVisibilityEnabledInLTO) {
791  if (!hasWholeProgramVisibility(WholeProgramVisibilityEnabledInLTO))
792  return;
793  for (auto &P : Index) {
794  for (auto &S : P.second.SummaryList) {
795  auto *GVar = dyn_cast<GlobalVarSummary>(S.get());
796  if (!GVar || GVar->vTableFuncs().empty() ||
797  GVar->getVCallVisibility() != GlobalObject::VCallVisibilityPublic)
798  continue;
800  }
801  }
802 }
803 
805  ModuleSummaryIndex &Summary, std::set<GlobalValue::GUID> &ExportedGUIDs,
806  std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap) {
807  DevirtIndex(Summary, ExportedGUIDs, LocalWPDTargetsMap).run();
808 }
809 
811  ModuleSummaryIndex &Summary,
812  function_ref<bool(StringRef, ValueInfo)> isExported,
813  std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap) {
814  for (auto &T : LocalWPDTargetsMap) {
815  auto &VI = T.first;
816  // This was enforced earlier during trySingleImplDevirt.
817  assert(VI.getSummaryList().size() == 1 &&
818  "Devirt of local target has more than one copy");
819  auto &S = VI.getSummaryList()[0];
820  if (!isExported(S->modulePath(), VI))
821  continue;
822 
823  // It's been exported by a cross module import.
824  for (auto &SlotSummary : T.second) {
825  auto *TIdSum = Summary.getTypeIdSummary(SlotSummary.TypeID);
826  assert(TIdSum);
827  auto WPDRes = TIdSum->WPDRes.find(SlotSummary.ByteOffset);
828  assert(WPDRes != TIdSum->WPDRes.end());
829  WPDRes->second.SingleImplName = ModuleSummaryIndex::getGlobalNameForLocal(
830  WPDRes->second.SingleImplName,
831  Summary.getModuleHash(S->modulePath()));
832  }
833  }
834 }
835 
836 } // end namespace llvm
837 
839  // Check that summary index contains regular LTO module when performing
840  // export to prevent occasional use of index from pure ThinLTO compilation
841  // (-fno-split-lto-module). This kind of summary index is passed to
842  // DevirtIndex::run, not to DevirtModule::run used by opt/runForTesting.
843  const auto &ModPaths = Summary->modulePaths();
846  ModPaths.end())
847  return createStringError(
849  "combined summary should contain Regular LTO module");
850  return ErrorSuccess();
851 }
852 
853 bool DevirtModule::runForTesting(
854  Module &M, function_ref<AAResults &(Function &)> AARGetter,
856  function_ref<DominatorTree &(Function &)> LookupDomTree) {
857  std::unique_ptr<ModuleSummaryIndex> Summary =
858  std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
859 
860  // Handle the command-line summary arguments. This code is for testing
861  // purposes only, so we handle errors directly.
862  if (!ClReadSummary.empty()) {
863  ExitOnError ExitOnErr("-wholeprogramdevirt-read-summary: " + ClReadSummary +
864  ": ");
865  auto ReadSummaryFile =
867  if (Expected<std::unique_ptr<ModuleSummaryIndex>> SummaryOrErr =
868  getModuleSummaryIndex(*ReadSummaryFile)) {
869  Summary = std::move(*SummaryOrErr);
870  ExitOnErr(checkCombinedSummaryForTesting(Summary.get()));
871  } else {
872  // Try YAML if we've failed with bitcode.
873  consumeError(SummaryOrErr.takeError());
874  yaml::Input In(ReadSummaryFile->getBuffer());
875  In >> *Summary;
876  ExitOnErr(errorCodeToError(In.error()));
877  }
878  }
879 
880  bool Changed =
881  DevirtModule(M, AARGetter, OREGetter, LookupDomTree,
882  ClSummaryAction == PassSummaryAction::Export ? Summary.get()
883  : nullptr,
884  ClSummaryAction == PassSummaryAction::Import ? Summary.get()
885  : nullptr)
886  .run();
887 
888  if (!ClWriteSummary.empty()) {
889  ExitOnError ExitOnErr(
890  "-wholeprogramdevirt-write-summary: " + ClWriteSummary + ": ");
891  std::error_code EC;
892  if (StringRef(ClWriteSummary).endswith(".bc")) {
894  ExitOnErr(errorCodeToError(EC));
895  WriteIndexToFile(*Summary, OS);
896  } else {
898  ExitOnErr(errorCodeToError(EC));
899  yaml::Output Out(OS);
900  Out << *Summary;
901  }
902  }
903 
904  return Changed;
905 }
906 
907 void DevirtModule::buildTypeIdentifierMap(
908  std::vector<VTableBits> &Bits,
909  DenseMap<Metadata *, std::set<TypeMemberInfo>> &TypeIdMap) {
911  Bits.reserve(M.getGlobalList().size());
913  for (GlobalVariable &GV : M.globals()) {
914  Types.clear();
915  GV.getMetadata(LLVMContext::MD_type, Types);
916  if (GV.isDeclaration() || Types.empty())
917  continue;
918 
919  VTableBits *&BitsPtr = GVToBits[&GV];
920  if (!BitsPtr) {
921  Bits.emplace_back();
922  Bits.back().GV = &GV;
923  Bits.back().ObjectSize =
924  M.getDataLayout().getTypeAllocSize(GV.getInitializer()->getType());
925  BitsPtr = &Bits.back();
926  }
927 
928  for (MDNode *Type : Types) {
929  auto TypeID = Type->getOperand(1).get();
930 
931  uint64_t Offset =
932  cast<ConstantInt>(
933  cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
934  ->getZExtValue();
935 
936  TypeIdMap[TypeID].insert({BitsPtr, Offset});
937  }
938  }
939 }
940 
941 bool DevirtModule::tryFindVirtualCallTargets(
942  std::vector<VirtualCallTarget> &TargetsForSlot,
943  const std::set<TypeMemberInfo> &TypeMemberInfos, uint64_t ByteOffset) {
944  for (const TypeMemberInfo &TM : TypeMemberInfos) {
945  if (!TM.Bits->GV->isConstant())
946  return false;
947 
948  // We cannot perform whole program devirtualization analysis on a vtable
949  // with public LTO visibility.
950  if (TM.Bits->GV->getVCallVisibility() ==
952  return false;
953 
955  TM.Offset + ByteOffset, M);
956  if (!Ptr)
957  return false;
958 
959  auto Fn = dyn_cast<Function>(Ptr->stripPointerCasts());
960  if (!Fn)
961  return false;
962 
963  if (FunctionsToSkip.match(Fn->getName()))
964  return false;
965 
966  // We can disregard __cxa_pure_virtual as a possible call target, as
967  // calls to pure virtuals are UB.
968  if (Fn->getName() == "__cxa_pure_virtual")
969  continue;
970 
971  TargetsForSlot.push_back({Fn, &TM});
972  }
973 
974  // Give up if we couldn't find any targets.
975  return !TargetsForSlot.empty();
976 }
977 
978 bool DevirtIndex::tryFindVirtualCallTargets(
979  std::vector<ValueInfo> &TargetsForSlot, const TypeIdCompatibleVtableInfo TIdInfo,
980  uint64_t ByteOffset) {
981  for (const TypeIdOffsetVtableInfo &P : TIdInfo) {
982  // Find the first non-available_externally linkage vtable initializer.
983  // We can have multiple available_externally, linkonce_odr and weak_odr
984  // vtable initializers, however we want to skip available_externally as they
985  // do not have type metadata attached, and therefore the summary will not
986  // contain any vtable functions. We can also have multiple external
987  // vtable initializers in the case of comdats, which we cannot check here.
988  // The linker should give an error in this case.
989  //
990  // Also, handle the case of same-named local Vtables with the same path
991  // and therefore the same GUID. This can happen if there isn't enough
992  // distinguishing path when compiling the source file. In that case we
993  // conservatively return false early.
994  const GlobalVarSummary *VS = nullptr;
995  bool LocalFound = false;
996  for (auto &S : P.VTableVI.getSummaryList()) {
997  if (GlobalValue::isLocalLinkage(S->linkage())) {
998  if (LocalFound)
999  return false;
1000  LocalFound = true;
1001  }
1002  if (!GlobalValue::isAvailableExternallyLinkage(S->linkage())) {
1003  VS = cast<GlobalVarSummary>(S->getBaseObject());
1004  // We cannot perform whole program devirtualization analysis on a vtable
1005  // with public LTO visibility.
1007  return false;
1008  }
1009  }
1010  if (!VS->isLive())
1011  continue;
1012  for (auto VTP : VS->vTableFuncs()) {
1013  if (VTP.VTableOffset != P.AddressPointOffset + ByteOffset)
1014  continue;
1015 
1016  TargetsForSlot.push_back(VTP.FuncVI);
1017  }
1018  }
1019 
1020  // Give up if we couldn't find any targets.
1021  return !TargetsForSlot.empty();
1022 }
1023 
1024 void DevirtModule::applySingleImplDevirt(VTableSlotInfo &SlotInfo,
1025  Constant *TheFn, bool &IsExported) {
1026  auto Apply = [&](CallSiteInfo &CSInfo) {
1027  for (auto &&VCallSite : CSInfo.CallSites) {
1028  if (RemarksEnabled)
1029  VCallSite.emitRemark("single-impl",
1030  TheFn->stripPointerCasts()->getName(), OREGetter);
1031  VCallSite.CB.setCalledOperand(ConstantExpr::getBitCast(
1032  TheFn, VCallSite.CB.getCalledOperand()->getType()));
1033  // This use is no longer unsafe.
1034  if (VCallSite.NumUnsafeUses)
1035  --*VCallSite.NumUnsafeUses;
1036  }
1037  if (CSInfo.isExported())
1038  IsExported = true;
1039  CSInfo.markDevirt();
1040  };
1041  Apply(SlotInfo.CSInfo);
1042  for (auto &P : SlotInfo.ConstCSInfo)
1043  Apply(P.second);
1044 }
1045 
1046 static bool AddCalls(VTableSlotInfo &SlotInfo, const ValueInfo &Callee) {
1047  // We can't add calls if we haven't seen a definition
1048  if (Callee.getSummaryList().empty())
1049  return false;
1050 
1051  // Insert calls into the summary index so that the devirtualized targets
1052  // are eligible for import.
1053  // FIXME: Annotate type tests with hotness. For now, mark these as hot
1054  // to better ensure we have the opportunity to inline them.
1055  bool IsExported = false;
1056  auto &S = Callee.getSummaryList()[0];
1057  CalleeInfo CI(CalleeInfo::HotnessType::Hot, /* RelBF = */ 0);
1058  auto AddCalls = [&](CallSiteInfo &CSInfo) {
1059  for (auto *FS : CSInfo.SummaryTypeCheckedLoadUsers) {
1060  FS->addCall({Callee, CI});
1061  IsExported |= S->modulePath() != FS->modulePath();
1062  }
1063  for (auto *FS : CSInfo.SummaryTypeTestAssumeUsers) {
1064  FS->addCall({Callee, CI});
1065  IsExported |= S->modulePath() != FS->modulePath();
1066  }
1067  };
1068  AddCalls(SlotInfo.CSInfo);
1069  for (auto &P : SlotInfo.ConstCSInfo)
1070  AddCalls(P.second);
1071  return IsExported;
1072 }
1073 
1074 bool DevirtModule::trySingleImplDevirt(
1075  ModuleSummaryIndex *ExportSummary,
1076  MutableArrayRef<VirtualCallTarget> TargetsForSlot, VTableSlotInfo &SlotInfo,
1078  // See if the program contains a single implementation of this virtual
1079  // function.
1080  Function *TheFn = TargetsForSlot[0].Fn;
1081  for (auto &&Target : TargetsForSlot)
1082  if (TheFn != Target.Fn)
1083  return false;
1084 
1085  // If so, update each call site to call that implementation directly.
1086  if (RemarksEnabled)
1087  TargetsForSlot[0].WasDevirt = true;
1088 
1089  bool IsExported = false;
1090  applySingleImplDevirt(SlotInfo, TheFn, IsExported);
1091  if (!IsExported)
1092  return false;
1093 
1094  // If the only implementation has local linkage, we must promote to external
1095  // to make it visible to thin LTO objects. We can only get here during the
1096  // ThinLTO export phase.
1097  if (TheFn->hasLocalLinkage()) {
1098  std::string NewName = (TheFn->getName() + "$merged").str();
1099 
1100  // Since we are renaming the function, any comdats with the same name must
1101  // also be renamed. This is required when targeting COFF, as the comdat name
1102  // must match one of the names of the symbols in the comdat.
1103  if (Comdat *C = TheFn->getComdat()) {
1104  if (C->getName() == TheFn->getName()) {
1105  Comdat *NewC = M.getOrInsertComdat(NewName);
1106  NewC->setSelectionKind(C->getSelectionKind());
1107  for (GlobalObject &GO : M.global_objects())
1108  if (GO.getComdat() == C)
1109  GO.setComdat(NewC);
1110  }
1111  }
1112 
1115  TheFn->setName(NewName);
1116  }
1117  if (ValueInfo TheFnVI = ExportSummary->getValueInfo(TheFn->getGUID()))
1118  // Any needed promotion of 'TheFn' has already been done during
1119  // LTO unit split, so we can ignore return value of AddCalls.
1120  AddCalls(SlotInfo, TheFnVI);
1121 
1123  Res->SingleImplName = std::string(TheFn->getName());
1124 
1125  return true;
1126 }
1127 
1128 bool DevirtIndex::trySingleImplDevirt(MutableArrayRef<ValueInfo> TargetsForSlot,
1129  VTableSlotSummary &SlotSummary,
1130  VTableSlotInfo &SlotInfo,
1132  std::set<ValueInfo> &DevirtTargets) {
1133  // See if the program contains a single implementation of this virtual
1134  // function.
1135  auto TheFn = TargetsForSlot[0];
1136  for (auto &&Target : TargetsForSlot)
1137  if (TheFn != Target)
1138  return false;
1139 
1140  // Don't devirtualize if we don't have target definition.
1141  auto Size = TheFn.getSummaryList().size();
1142  if (!Size)
1143  return false;
1144 
1145  // Don't devirtualize function if we're told to skip it
1146  // in -wholeprogramdevirt-skip.
1147  if (FunctionsToSkip.match(TheFn.name()))
1148  return false;
1149 
1150  // If the summary list contains multiple summaries where at least one is
1151  // a local, give up, as we won't know which (possibly promoted) name to use.
1152  for (auto &S : TheFn.getSummaryList())
1153  if (GlobalValue::isLocalLinkage(S->linkage()) && Size > 1)
1154  return false;
1155 
1156  // Collect functions devirtualized at least for one call site for stats.
1157  if (PrintSummaryDevirt)
1158  DevirtTargets.insert(TheFn);
1159 
1160  auto &S = TheFn.getSummaryList()[0];
1161  bool IsExported = AddCalls(SlotInfo, TheFn);
1162  if (IsExported)
1163  ExportedGUIDs.insert(TheFn.getGUID());
1164 
1165  // Record in summary for use in devirtualization during the ThinLTO import
1166  // step.
1168  if (GlobalValue::isLocalLinkage(S->linkage())) {
1169  if (IsExported)
1170  // If target is a local function and we are exporting it by
1171  // devirtualizing a call in another module, we need to record the
1172  // promoted name.
1174  TheFn.name(), ExportSummary.getModuleHash(S->modulePath()));
1175  else {
1176  LocalWPDTargetsMap[TheFn].push_back(SlotSummary);
1177  Res->SingleImplName = std::string(TheFn.name());
1178  }
1179  } else
1180  Res->SingleImplName = std::string(TheFn.name());
1181 
1182  // Name will be empty if this thin link driven off of serialized combined
1183  // index (e.g. llvm-lto). However, WPD is not supported/invoked for the
1184  // legacy LTO API anyway.
1185  assert(!Res->SingleImplName.empty());
1186 
1187  return true;
1188 }
1189 
1190 void DevirtModule::tryICallBranchFunnel(
1191  MutableArrayRef<VirtualCallTarget> TargetsForSlot, VTableSlotInfo &SlotInfo,
1192  WholeProgramDevirtResolution *Res, VTableSlot Slot) {
1193  Triple T(M.getTargetTriple());
1194  if (T.getArch() != Triple::x86_64)
1195  return;
1196 
1197  if (TargetsForSlot.size() > ClThreshold)
1198  return;
1199 
1200  bool HasNonDevirt = !SlotInfo.CSInfo.AllCallSitesDevirted;
1201  if (!HasNonDevirt)
1202  for (auto &P : SlotInfo.ConstCSInfo)
1203  if (!P.second.AllCallSitesDevirted) {
1204  HasNonDevirt = true;
1205  break;
1206  }
1207 
1208  if (!HasNonDevirt)
1209  return;
1210 
1211  FunctionType *FT =
1212  FunctionType::get(Type::getVoidTy(M.getContext()), {Int8PtrTy}, true);
1213  Function *JT;
1214  if (isa<MDString>(Slot.TypeID)) {
1217  getGlobalName(Slot, {}, "branch_funnel"), &M);
1218  JT->setVisibility(GlobalValue::HiddenVisibility);
1219  } else {
1222  "branch_funnel", &M);
1223  }
1224  JT->addAttribute(1, Attribute::Nest);
1225 
1226  std::vector<Value *> JTArgs;
1227  JTArgs.push_back(JT->arg_begin());
1228  for (auto &T : TargetsForSlot) {
1229  JTArgs.push_back(getMemberAddr(T.TM));
1230  JTArgs.push_back(T.Fn);
1231  }
1232 
1233  BasicBlock *BB = BasicBlock::Create(M.getContext(), "", JT, nullptr);
1234  Function *Intr =
1235  Intrinsic::getDeclaration(&M, llvm::Intrinsic::icall_branch_funnel, {});
1236 
1237  auto *CI = CallInst::Create(Intr, JTArgs, "", BB);
1238  CI->setTailCallKind(CallInst::TCK_MustTail);
1239  ReturnInst::Create(M.getContext(), nullptr, BB);
1240 
1241  bool IsExported = false;
1242  applyICallBranchFunnel(SlotInfo, JT, IsExported);
1243  if (IsExported)
1245 }
1246 
1247 void DevirtModule::applyICallBranchFunnel(VTableSlotInfo &SlotInfo,
1248  Constant *JT, bool &IsExported) {
1249  auto Apply = [&](CallSiteInfo &CSInfo) {
1250  if (CSInfo.isExported())
1251  IsExported = true;
1252  if (CSInfo.AllCallSitesDevirted)
1253  return;
1254  for (auto &&VCallSite : CSInfo.CallSites) {
1255  CallBase &CB = VCallSite.CB;
1256 
1257  // Jump tables are only profitable if the retpoline mitigation is enabled.
1258  Attribute FSAttr = CB.getCaller()->getFnAttribute("target-features");
1259  if (FSAttr.hasAttribute(Attribute::None) ||
1260  !FSAttr.getValueAsString().contains("+retpoline"))
1261  continue;
1262 
1263  if (RemarksEnabled)
1264  VCallSite.emitRemark("branch-funnel",
1265  JT->stripPointerCasts()->getName(), OREGetter);
1266 
1267  // Pass the address of the vtable in the nest register, which is r10 on
1268  // x86_64.
1269  std::vector<Type *> NewArgs;
1270  NewArgs.push_back(Int8PtrTy);
1271  for (Type *T : CB.getFunctionType()->params())
1272  NewArgs.push_back(T);
1273  FunctionType *NewFT =
1275  CB.getFunctionType()->isVarArg());
1276  PointerType *NewFTPtr = PointerType::getUnqual(NewFT);
1277 
1278  IRBuilder<> IRB(&CB);
1279  std::vector<Value *> Args;
1280  Args.push_back(IRB.CreateBitCast(VCallSite.VTable, Int8PtrTy));
1281  Args.insert(Args.end(), CB.arg_begin(), CB.arg_end());
1282 
1283  CallBase *NewCS = nullptr;
1284  if (isa<CallInst>(CB))
1285  NewCS = IRB.CreateCall(NewFT, IRB.CreateBitCast(JT, NewFTPtr), Args);
1286  else
1287  NewCS = IRB.CreateInvoke(NewFT, IRB.CreateBitCast(JT, NewFTPtr),
1288  cast<InvokeInst>(CB).getNormalDest(),
1289  cast<InvokeInst>(CB).getUnwindDest(), Args);
1290  NewCS->setCallingConv(CB.getCallingConv());
1291 
1293  std::vector<AttributeSet> NewArgAttrs;
1294  NewArgAttrs.push_back(AttributeSet::get(
1296  M.getContext(), Attribute::Nest)}));
1297  for (unsigned I = 0; I + 2 < Attrs.getNumAttrSets(); ++I)
1298  NewArgAttrs.push_back(Attrs.getParamAttributes(I));
1299  NewCS->setAttributes(
1301  Attrs.getRetAttributes(), NewArgAttrs));
1302 
1303  CB.replaceAllUsesWith(NewCS);
1304  CB.eraseFromParent();
1305 
1306  // This use is no longer unsafe.
1307  if (VCallSite.NumUnsafeUses)
1308  --*VCallSite.NumUnsafeUses;
1309  }
1310  // Don't mark as devirtualized because there may be callers compiled without
1311  // retpoline mitigation, which would mean that they are lowered to
1312  // llvm.type.test and therefore require an llvm.type.test resolution for the
1313  // type identifier.
1314  };
1315  Apply(SlotInfo.CSInfo);
1316  for (auto &P : SlotInfo.ConstCSInfo)
1317  Apply(P.second);
1318 }
1319 
1320 bool DevirtModule::tryEvaluateFunctionsWithArgs(
1321  MutableArrayRef<VirtualCallTarget> TargetsForSlot,
1323  // Evaluate each function and store the result in each target's RetVal
1324  // field.
1325  for (VirtualCallTarget &Target : TargetsForSlot) {
1326  if (Target.Fn->arg_size() != Args.size() + 1)
1327  return false;
1328 
1329  Evaluator Eval(M.getDataLayout(), nullptr);
1330  SmallVector<Constant *, 2> EvalArgs;
1331  EvalArgs.push_back(
1332  Constant::getNullValue(Target.Fn->getFunctionType()->getParamType(0)));
1333  for (unsigned I = 0; I != Args.size(); ++I) {
1334  auto *ArgTy = dyn_cast<IntegerType>(
1335  Target.Fn->getFunctionType()->getParamType(I + 1));
1336  if (!ArgTy)
1337  return false;
1338  EvalArgs.push_back(ConstantInt::get(ArgTy, Args[I]));
1339  }
1340 
1341  Constant *RetVal;
1342  if (!Eval.EvaluateFunction(Target.Fn, RetVal, EvalArgs) ||
1343  !isa<ConstantInt>(RetVal))
1344  return false;
1345  Target.RetVal = cast<ConstantInt>(RetVal)->getZExtValue();
1346  }
1347  return true;
1348 }
1349 
1350 void DevirtModule::applyUniformRetValOpt(CallSiteInfo &CSInfo, StringRef FnName,
1351  uint64_t TheRetVal) {
1352  for (auto Call : CSInfo.CallSites)
1353  Call.replaceAndErase(
1354  "uniform-ret-val", FnName, RemarksEnabled, OREGetter,
1355  ConstantInt::get(cast<IntegerType>(Call.CB.getType()), TheRetVal));
1356  CSInfo.markDevirt();
1357 }
1358 
1359 bool DevirtModule::tryUniformRetValOpt(
1360  MutableArrayRef<VirtualCallTarget> TargetsForSlot, CallSiteInfo &CSInfo,
1362  // Uniform return value optimization. If all functions return the same
1363  // constant, replace all calls with that constant.
1364  uint64_t TheRetVal = TargetsForSlot[0].RetVal;
1365  for (const VirtualCallTarget &Target : TargetsForSlot)
1366  if (Target.RetVal != TheRetVal)
1367  return false;
1368 
1369  if (CSInfo.isExported()) {
1371  Res->Info = TheRetVal;
1372  }
1373 
1374  applyUniformRetValOpt(CSInfo, TargetsForSlot[0].Fn->getName(), TheRetVal);
1375  if (RemarksEnabled)
1376  for (auto &&Target : TargetsForSlot)
1377  Target.WasDevirt = true;
1378  return true;
1379 }
1380 
1381 std::string DevirtModule::getGlobalName(VTableSlot Slot,
1382  ArrayRef<uint64_t> Args,
1383  StringRef Name) {
1384  std::string FullName = "__typeid_";
1385  raw_string_ostream OS(FullName);
1386  OS << cast<MDString>(Slot.TypeID)->getString() << '_' << Slot.ByteOffset;
1387  for (uint64_t Arg : Args)
1388  OS << '_' << Arg;
1389  OS << '_' << Name;
1390  return OS.str();
1391 }
1392 
1393 bool DevirtModule::shouldExportConstantsAsAbsoluteSymbols() {
1394  Triple T(M.getTargetTriple());
1395  return T.isX86() && T.getObjectFormat() == Triple::ELF;
1396 }
1397 
1398 void DevirtModule::exportGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args,
1399  StringRef Name, Constant *C) {
1401  getGlobalName(Slot, Args, Name), C, &M);
1403 }
1404 
1405 void DevirtModule::exportConstant(VTableSlot Slot, ArrayRef<uint64_t> Args,
1406  StringRef Name, uint32_t Const,
1407  uint32_t &Storage) {
1408  if (shouldExportConstantsAsAbsoluteSymbols()) {
1409  exportGlobal(
1410  Slot, Args, Name,
1411  ConstantExpr::getIntToPtr(ConstantInt::get(Int32Ty, Const), Int8PtrTy));
1412  return;
1413  }
1414 
1415  Storage = Const;
1416 }
1417 
1418 Constant *DevirtModule::importGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args,
1419  StringRef Name) {
1420  Constant *C =
1421  M.getOrInsertGlobal(getGlobalName(Slot, Args, Name), Int8Arr0Ty);
1422  auto *GV = dyn_cast<GlobalVariable>(C);
1423  if (GV)
1425  return C;
1426 }
1427 
1428 Constant *DevirtModule::importConstant(VTableSlot Slot, ArrayRef<uint64_t> Args,
1429  StringRef Name, IntegerType *IntTy,
1430  uint32_t Storage) {
1431  if (!shouldExportConstantsAsAbsoluteSymbols())
1432  return ConstantInt::get(IntTy, Storage);
1433 
1434  Constant *C = importGlobal(Slot, Args, Name);
1435  auto *GV = cast<GlobalVariable>(C->stripPointerCasts());
1436  C = ConstantExpr::getPtrToInt(C, IntTy);
1437 
1438  // We only need to set metadata if the global is newly created, in which
1439  // case it would not have hidden visibility.
1440  if (GV->hasMetadata(LLVMContext::MD_absolute_symbol))
1441  return C;
1442 
1443  auto SetAbsRange = [&](uint64_t Min, uint64_t Max) {
1444  auto *MinC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Min));
1445  auto *MaxC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Max));
1446  GV->setMetadata(LLVMContext::MD_absolute_symbol,
1447  MDNode::get(M.getContext(), {MinC, MaxC}));
1448  };
1449  unsigned AbsWidth = IntTy->getBitWidth();
1450  if (AbsWidth == IntPtrTy->getBitWidth())
1451  SetAbsRange(~0ull, ~0ull); // Full set.
1452  else
1453  SetAbsRange(0, 1ull << AbsWidth);
1454  return C;
1455 }
1456 
1457 void DevirtModule::applyUniqueRetValOpt(CallSiteInfo &CSInfo, StringRef FnName,
1458  bool IsOne,
1459  Constant *UniqueMemberAddr) {
1460  for (auto &&Call : CSInfo.CallSites) {
1461  IRBuilder<> B(&Call.CB);
1462  Value *Cmp =
1463  B.CreateICmp(IsOne ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE, Call.VTable,
1464  B.CreateBitCast(UniqueMemberAddr, Call.VTable->getType()));
1465  Cmp = B.CreateZExt(Cmp, Call.CB.getType());
1466  Call.replaceAndErase("unique-ret-val", FnName, RemarksEnabled, OREGetter,
1467  Cmp);
1468  }
1469  CSInfo.markDevirt();
1470 }
1471 
1472 Constant *DevirtModule::getMemberAddr(const TypeMemberInfo *M) {
1473  Constant *C = ConstantExpr::getBitCast(M->Bits->GV, Int8PtrTy);
1474  return ConstantExpr::getGetElementPtr(Int8Ty, C,
1475  ConstantInt::get(Int64Ty, M->Offset));
1476 }
1477 
1478 bool DevirtModule::tryUniqueRetValOpt(
1479  unsigned BitWidth, MutableArrayRef<VirtualCallTarget> TargetsForSlot,
1480  CallSiteInfo &CSInfo, WholeProgramDevirtResolution::ByArg *Res,
1481  VTableSlot Slot, ArrayRef<uint64_t> Args) {
1482  // IsOne controls whether we look for a 0 or a 1.
1483  auto tryUniqueRetValOptFor = [&](bool IsOne) {
1484  const TypeMemberInfo *UniqueMember = nullptr;
1485  for (const VirtualCallTarget &Target : TargetsForSlot) {
1486  if (Target.RetVal == (IsOne ? 1 : 0)) {
1487  if (UniqueMember)
1488  return false;
1489  UniqueMember = Target.TM;
1490  }
1491  }
1492 
1493  // We should have found a unique member or bailed out by now. We already
1494  // checked for a uniform return value in tryUniformRetValOpt.
1495  assert(UniqueMember);
1496 
1497  Constant *UniqueMemberAddr = getMemberAddr(UniqueMember);
1498  if (CSInfo.isExported()) {
1500  Res->Info = IsOne;
1501 
1502  exportGlobal(Slot, Args, "unique_member", UniqueMemberAddr);
1503  }
1504 
1505  // Replace each call with the comparison.
1506  applyUniqueRetValOpt(CSInfo, TargetsForSlot[0].Fn->getName(), IsOne,
1507  UniqueMemberAddr);
1508 
1509  // Update devirtualization statistics for targets.
1510  if (RemarksEnabled)
1511  for (auto &&Target : TargetsForSlot)
1512  Target.WasDevirt = true;
1513 
1514  return true;
1515  };
1516 
1517  if (BitWidth == 1) {
1518  if (tryUniqueRetValOptFor(true))
1519  return true;
1520  if (tryUniqueRetValOptFor(false))
1521  return true;
1522  }
1523  return false;
1524 }
1525 
1526 void DevirtModule::applyVirtualConstProp(CallSiteInfo &CSInfo, StringRef FnName,
1527  Constant *Byte, Constant *Bit) {
1528  for (auto Call : CSInfo.CallSites) {
1529  auto *RetType = cast<IntegerType>(Call.CB.getType());
1530  IRBuilder<> B(&Call.CB);
1531  Value *Addr =
1532  B.CreateGEP(Int8Ty, B.CreateBitCast(Call.VTable, Int8PtrTy), Byte);
1533  if (RetType->getBitWidth() == 1) {
1534  Value *Bits = B.CreateLoad(Int8Ty, Addr);
1535  Value *BitsAndBit = B.CreateAnd(Bits, Bit);
1536  auto IsBitSet = B.CreateICmpNE(BitsAndBit, ConstantInt::get(Int8Ty, 0));
1537  Call.replaceAndErase("virtual-const-prop-1-bit", FnName, RemarksEnabled,
1538  OREGetter, IsBitSet);
1539  } else {
1540  Value *ValAddr = B.CreateBitCast(Addr, RetType->getPointerTo());
1541  Value *Val = B.CreateLoad(RetType, ValAddr);
1542  Call.replaceAndErase("virtual-const-prop", FnName, RemarksEnabled,
1543  OREGetter, Val);
1544  }
1545  }
1546  CSInfo.markDevirt();
1547 }
1548 
1549 bool DevirtModule::tryVirtualConstProp(
1550  MutableArrayRef<VirtualCallTarget> TargetsForSlot, VTableSlotInfo &SlotInfo,
1551  WholeProgramDevirtResolution *Res, VTableSlot Slot) {
1552  // This only works if the function returns an integer.
1553  auto RetType = dyn_cast<IntegerType>(TargetsForSlot[0].Fn->getReturnType());
1554  if (!RetType)
1555  return false;
1556  unsigned BitWidth = RetType->getBitWidth();
1557  if (BitWidth > 64)
1558  return false;
1559 
1560  // Make sure that each function is defined, does not access memory, takes at
1561  // least one argument, does not use its first argument (which we assume is
1562  // 'this'), and has the same return type.
1563  //
1564  // Note that we test whether this copy of the function is readnone, rather
1565  // than testing function attributes, which must hold for any copy of the
1566  // function, even a less optimized version substituted at link time. This is
1567  // sound because the virtual constant propagation optimizations effectively
1568  // inline all implementations of the virtual function into each call site,
1569  // rather than using function attributes to perform local optimization.
1570  for (VirtualCallTarget &Target : TargetsForSlot) {
1571  if (Target.Fn->isDeclaration() ||
1572  computeFunctionBodyMemoryAccess(*Target.Fn, AARGetter(*Target.Fn)) !=
1573  MAK_ReadNone ||
1574  Target.Fn->arg_empty() || !Target.Fn->arg_begin()->use_empty() ||
1575  Target.Fn->getReturnType() != RetType)
1576  return false;
1577  }
1578 
1579  for (auto &&CSByConstantArg : SlotInfo.ConstCSInfo) {
1580  if (!tryEvaluateFunctionsWithArgs(TargetsForSlot, CSByConstantArg.first))
1581  continue;
1582 
1583  WholeProgramDevirtResolution::ByArg *ResByArg = nullptr;
1584  if (Res)
1585  ResByArg = &Res->ResByArg[CSByConstantArg.first];
1586 
1587  if (tryUniformRetValOpt(TargetsForSlot, CSByConstantArg.second, ResByArg))
1588  continue;
1589 
1590  if (tryUniqueRetValOpt(BitWidth, TargetsForSlot, CSByConstantArg.second,
1591  ResByArg, Slot, CSByConstantArg.first))
1592  continue;
1593 
1594  // Find an allocation offset in bits in all vtables associated with the
1595  // type.
1596  uint64_t AllocBefore =
1597  findLowestOffset(TargetsForSlot, /*IsAfter=*/false, BitWidth);
1598  uint64_t AllocAfter =
1599  findLowestOffset(TargetsForSlot, /*IsAfter=*/true, BitWidth);
1600 
1601  // Calculate the total amount of padding needed to store a value at both
1602  // ends of the object.
1603  uint64_t TotalPaddingBefore = 0, TotalPaddingAfter = 0;
1604  for (auto &&Target : TargetsForSlot) {
1605  TotalPaddingBefore += std::max<int64_t>(
1606  (AllocBefore + 7) / 8 - Target.allocatedBeforeBytes() - 1, 0);
1607  TotalPaddingAfter += std::max<int64_t>(
1608  (AllocAfter + 7) / 8 - Target.allocatedAfterBytes() - 1, 0);
1609  }
1610 
1611  // If the amount of padding is too large, give up.
1612  // FIXME: do something smarter here.
1613  if (std::min(TotalPaddingBefore, TotalPaddingAfter) > 128)
1614  continue;
1615 
1616  // Calculate the offset to the value as a (possibly negative) byte offset
1617  // and (if applicable) a bit offset, and store the values in the targets.
1618  int64_t OffsetByte;
1619  uint64_t OffsetBit;
1620  if (TotalPaddingBefore <= TotalPaddingAfter)
1621  setBeforeReturnValues(TargetsForSlot, AllocBefore, BitWidth, OffsetByte,
1622  OffsetBit);
1623  else
1624  setAfterReturnValues(TargetsForSlot, AllocAfter, BitWidth, OffsetByte,
1625  OffsetBit);
1626 
1627  if (RemarksEnabled)
1628  for (auto &&Target : TargetsForSlot)
1629  Target.WasDevirt = true;
1630 
1631 
1632  if (CSByConstantArg.second.isExported()) {
1634  exportConstant(Slot, CSByConstantArg.first, "byte", OffsetByte,
1635  ResByArg->Byte);
1636  exportConstant(Slot, CSByConstantArg.first, "bit", 1ULL << OffsetBit,
1637  ResByArg->Bit);
1638  }
1639 
1640  // Rewrite each call to a load from OffsetByte/OffsetBit.
1641  Constant *ByteConst = ConstantInt::get(Int32Ty, OffsetByte);
1642  Constant *BitConst = ConstantInt::get(Int8Ty, 1ULL << OffsetBit);
1643  applyVirtualConstProp(CSByConstantArg.second,
1644  TargetsForSlot[0].Fn->getName(), ByteConst, BitConst);
1645  }
1646  return true;
1647 }
1648 
1649 void DevirtModule::rebuildGlobal(VTableBits &B) {
1650  if (B.Before.Bytes.empty() && B.After.Bytes.empty())
1651  return;
1652 
1653  // Align the before byte array to the global's minimum alignment so that we
1654  // don't break any alignment requirements on the global.
1655  Align Alignment = M.getDataLayout().getValueOrABITypeAlignment(
1656  B.GV->getAlign(), B.GV->getValueType());
1657  B.Before.Bytes.resize(alignTo(B.Before.Bytes.size(), Alignment));
1658 
1659  // Before was stored in reverse order; flip it now.
1660  for (size_t I = 0, Size = B.Before.Bytes.size(); I != Size / 2; ++I)
1661  std::swap(B.Before.Bytes[I], B.Before.Bytes[Size - 1 - I]);
1662 
1663  // Build an anonymous global containing the before bytes, followed by the
1664  // original initializer, followed by the after bytes.
1665  auto NewInit = ConstantStruct::getAnon(
1666  {ConstantDataArray::get(M.getContext(), B.Before.Bytes),
1667  B.GV->getInitializer(),
1668  ConstantDataArray::get(M.getContext(), B.After.Bytes)});
1669  auto NewGV =
1670  new GlobalVariable(M, NewInit->getType(), B.GV->isConstant(),
1671  GlobalVariable::PrivateLinkage, NewInit, "", B.GV);
1672  NewGV->setSection(B.GV->getSection());
1673  NewGV->setComdat(B.GV->getComdat());
1674  NewGV->setAlignment(MaybeAlign(B.GV->getAlignment()));
1675 
1676  // Copy the original vtable's metadata to the anonymous global, adjusting
1677  // offsets as required.
1678  NewGV->copyMetadata(B.GV, B.Before.Bytes.size());
1679 
1680  // Build an alias named after the original global, pointing at the second
1681  // element (the original initializer).
1682  auto Alias = GlobalAlias::create(
1683  B.GV->getInitializer()->getType(), 0, B.GV->getLinkage(), "",
1685  NewInit->getType(), NewGV,
1687  ConstantInt::get(Int32Ty, 1)}),
1688  &M);
1689  Alias->setVisibility(B.GV->getVisibility());
1690  Alias->takeName(B.GV);
1691 
1692  B.GV->replaceAllUsesWith(Alias);
1693  B.GV->eraseFromParent();
1694 }
1695 
1696 bool DevirtModule::areRemarksEnabled() {
1697  const auto &FL = M.getFunctionList();
1698  for (const Function &Fn : FL) {
1699  const auto &BBL = Fn.getBasicBlockList();
1700  if (BBL.empty())
1701  continue;
1702  auto DI = OptimizationRemark(DEBUG_TYPE, "", DebugLoc(), &BBL.front());
1703  return DI.isEnabled();
1704  }
1705  return false;
1706 }
1707 
1708 void DevirtModule::scanTypeTestUsers(Function *TypeTestFunc) {
1709  // Find all virtual calls via a virtual table pointer %p under an assumption
1710  // of the form llvm.assume(llvm.type.test(%p, %md)). This indicates that %p
1711  // points to a member of the type identifier %md. Group calls by (type ID,
1712  // offset) pair (effectively the identity of the virtual function) and store
1713  // to CallSlots.
1714  for (auto I = TypeTestFunc->use_begin(), E = TypeTestFunc->use_end();
1715  I != E;) {
1716  auto CI = dyn_cast<CallInst>(I->getUser());
1717  ++I;
1718  if (!CI)
1719  continue;
1720 
1721  // Search for virtual calls based on %p and add them to DevirtCalls.
1722  SmallVector<DevirtCallSite, 1> DevirtCalls;
1724  auto &DT = LookupDomTree(*CI->getFunction());
1725  findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI, DT);
1726 
1727  // If we found any, add them to CallSlots.
1728  if (!Assumes.empty()) {
1729  Metadata *TypeId =
1730  cast<MetadataAsValue>(CI->getArgOperand(1))->getMetadata();
1731  Value *Ptr = CI->getArgOperand(0)->stripPointerCasts();
1732  for (DevirtCallSite Call : DevirtCalls)
1733  CallSlots[{TypeId, Call.Offset}].addCallSite(Ptr, Call.CB, nullptr);
1734  }
1735 
1736  // We no longer need the assumes or the type test.
1737  for (auto Assume : Assumes)
1738  Assume->eraseFromParent();
1739  // We can't use RecursivelyDeleteTriviallyDeadInstructions here because we
1740  // may use the vtable argument later.
1741  if (CI->use_empty())
1742  CI->eraseFromParent();
1743  }
1744 }
1745 
1746 void DevirtModule::scanTypeCheckedLoadUsers(Function *TypeCheckedLoadFunc) {
1747  Function *TypeTestFunc = Intrinsic::getDeclaration(&M, Intrinsic::type_test);
1748 
1749  for (auto I = TypeCheckedLoadFunc->use_begin(),
1750  E = TypeCheckedLoadFunc->use_end();
1751  I != E;) {
1752  auto CI = dyn_cast<CallInst>(I->getUser());
1753  ++I;
1754  if (!CI)
1755  continue;
1756 
1757  Value *Ptr = CI->getArgOperand(0);
1758  Value *Offset = CI->getArgOperand(1);
1759  Value *TypeIdValue = CI->getArgOperand(2);
1760  Metadata *TypeId = cast<MetadataAsValue>(TypeIdValue)->getMetadata();
1761 
1762  SmallVector<DevirtCallSite, 1> DevirtCalls;
1763  SmallVector<Instruction *, 1> LoadedPtrs;
1765  bool HasNonCallUses = false;
1766  auto &DT = LookupDomTree(*CI->getFunction());
1767  findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds,
1768  HasNonCallUses, CI, DT);
1769 
1770  // Start by generating "pessimistic" code that explicitly loads the function
1771  // pointer from the vtable and performs the type check. If possible, we will
1772  // eliminate the load and the type check later.
1773 
1774  // If possible, only generate the load at the point where it is used.
1775  // This helps avoid unnecessary spills.
1776  IRBuilder<> LoadB(
1777  (LoadedPtrs.size() == 1 && !HasNonCallUses) ? LoadedPtrs[0] : CI);
1778  Value *GEP = LoadB.CreateGEP(Int8Ty, Ptr, Offset);
1779  Value *GEPPtr = LoadB.CreateBitCast(GEP, PointerType::getUnqual(Int8PtrTy));
1780  Value *LoadedValue = LoadB.CreateLoad(Int8PtrTy, GEPPtr);
1781 
1782  for (Instruction *LoadedPtr : LoadedPtrs) {
1783  LoadedPtr->replaceAllUsesWith(LoadedValue);
1784  LoadedPtr->eraseFromParent();
1785  }
1786 
1787  // Likewise for the type test.
1788  IRBuilder<> CallB((Preds.size() == 1 && !HasNonCallUses) ? Preds[0] : CI);
1789  CallInst *TypeTestCall = CallB.CreateCall(TypeTestFunc, {Ptr, TypeIdValue});
1790 
1791  for (Instruction *Pred : Preds) {
1792  Pred->replaceAllUsesWith(TypeTestCall);
1793  Pred->eraseFromParent();
1794  }
1795 
1796  // We have already erased any extractvalue instructions that refer to the
1797  // intrinsic call, but the intrinsic may have other non-extractvalue uses
1798  // (although this is unlikely). In that case, explicitly build a pair and
1799  // RAUW it.
1800  if (!CI->use_empty()) {
1801  Value *Pair = UndefValue::get(CI->getType());
1802  IRBuilder<> B(CI);
1803  Pair = B.CreateInsertValue(Pair, LoadedValue, {0});
1804  Pair = B.CreateInsertValue(Pair, TypeTestCall, {1});
1805  CI->replaceAllUsesWith(Pair);
1806  }
1807 
1808  // The number of unsafe uses is initially the number of uses.
1809  auto &NumUnsafeUses = NumUnsafeUsesForTypeTest[TypeTestCall];
1810  NumUnsafeUses = DevirtCalls.size();
1811 
1812  // If the function pointer has a non-call user, we cannot eliminate the type
1813  // check, as one of those users may eventually call the pointer. Increment
1814  // the unsafe use count to make sure it cannot reach zero.
1815  if (HasNonCallUses)
1816  ++NumUnsafeUses;
1817  for (DevirtCallSite Call : DevirtCalls) {
1818  CallSlots[{TypeId, Call.Offset}].addCallSite(Ptr, Call.CB,
1819  &NumUnsafeUses);
1820  }
1821 
1822  CI->eraseFromParent();
1823  }
1824 }
1825 
1826 void DevirtModule::importResolution(VTableSlot Slot, VTableSlotInfo &SlotInfo) {
1827  auto *TypeId = dyn_cast<MDString>(Slot.TypeID);
1828  if (!TypeId)
1829  return;
1830  const TypeIdSummary *TidSummary =
1831  ImportSummary->getTypeIdSummary(TypeId->getString());
1832  if (!TidSummary)
1833  return;
1834  auto ResI = TidSummary->WPDRes.find(Slot.ByteOffset);
1835  if (ResI == TidSummary->WPDRes.end())
1836  return;
1837  const WholeProgramDevirtResolution &Res = ResI->second;
1838 
1840  assert(!Res.SingleImplName.empty());
1841  // The type of the function in the declaration is irrelevant because every
1842  // call site will cast it to the correct type.
1843  Constant *SingleImpl =
1844  cast<Constant>(M.getOrInsertFunction(Res.SingleImplName,
1845  Type::getVoidTy(M.getContext()))
1846  .getCallee());
1847 
1848  // This is the import phase so we should not be exporting anything.
1849  bool IsExported = false;
1850  applySingleImplDevirt(SlotInfo, SingleImpl, IsExported);
1851  assert(!IsExported);
1852  }
1853 
1854  for (auto &CSByConstantArg : SlotInfo.ConstCSInfo) {
1855  auto I = Res.ResByArg.find(CSByConstantArg.first);
1856  if (I == Res.ResByArg.end())
1857  continue;
1858  auto &ResByArg = I->second;
1859  // FIXME: We should figure out what to do about the "function name" argument
1860  // to the apply* functions, as the function names are unavailable during the
1861  // importing phase. For now we just pass the empty string. This does not
1862  // impact correctness because the function names are just used for remarks.
1863  switch (ResByArg.TheKind) {
1865  applyUniformRetValOpt(CSByConstantArg.second, "", ResByArg.Info);
1866  break;
1868  Constant *UniqueMemberAddr =
1869  importGlobal(Slot, CSByConstantArg.first, "unique_member");
1870  applyUniqueRetValOpt(CSByConstantArg.second, "", ResByArg.Info,
1871  UniqueMemberAddr);
1872  break;
1873  }
1875  Constant *Byte = importConstant(Slot, CSByConstantArg.first, "byte",
1876  Int32Ty, ResByArg.Byte);
1877  Constant *Bit = importConstant(Slot, CSByConstantArg.first, "bit", Int8Ty,
1878  ResByArg.Bit);
1879  applyVirtualConstProp(CSByConstantArg.second, "", Byte, Bit);
1880  break;
1881  }
1882  default:
1883  break;
1884  }
1885  }
1886 
1888  // The type of the function is irrelevant, because it's bitcast at calls
1889  // anyhow.
1890  Constant *JT = cast<Constant>(
1891  M.getOrInsertFunction(getGlobalName(Slot, {}, "branch_funnel"),
1892  Type::getVoidTy(M.getContext()))
1893  .getCallee());
1894  bool IsExported = false;
1895  applyICallBranchFunnel(SlotInfo, JT, IsExported);
1896  assert(!IsExported);
1897  }
1898 }
1899 
1900 void DevirtModule::removeRedundantTypeTests() {
1901  auto True = ConstantInt::getTrue(M.getContext());
1902  for (auto &&U : NumUnsafeUsesForTypeTest) {
1903  if (U.second == 0) {
1904  U.first->replaceAllUsesWith(True);
1905  U.first->eraseFromParent();
1906  }
1907  }
1908 }
1909 
1910 bool DevirtModule::run() {
1911  // If only some of the modules were split, we cannot correctly perform
1912  // this transformation. We already checked for the presense of type tests
1913  // with partially split modules during the thin link, and would have emitted
1914  // an error if any were found, so here we can simply return.
1915  if ((ExportSummary && ExportSummary->partiallySplitLTOUnits()) ||
1916  (ImportSummary && ImportSummary->partiallySplitLTOUnits()))
1917  return false;
1918 
1919  Function *TypeTestFunc =
1920  M.getFunction(Intrinsic::getName(Intrinsic::type_test));
1921  Function *TypeCheckedLoadFunc =
1922  M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load));
1923  Function *AssumeFunc = M.getFunction(Intrinsic::getName(Intrinsic::assume));
1924 
1925  // Normally if there are no users of the devirtualization intrinsics in the
1926  // module, this pass has nothing to do. But if we are exporting, we also need
1927  // to handle any users that appear only in the function summaries.
1928  if (!ExportSummary &&
1929  (!TypeTestFunc || TypeTestFunc->use_empty() || !AssumeFunc ||
1930  AssumeFunc->use_empty()) &&
1931  (!TypeCheckedLoadFunc || TypeCheckedLoadFunc->use_empty()))
1932  return false;
1933 
1934  if (TypeTestFunc && AssumeFunc)
1935  scanTypeTestUsers(TypeTestFunc);
1936 
1937  if (TypeCheckedLoadFunc)
1938  scanTypeCheckedLoadUsers(TypeCheckedLoadFunc);
1939 
1940  if (ImportSummary) {
1941  for (auto &S : CallSlots)
1942  importResolution(S.first, S.second);
1943 
1944  removeRedundantTypeTests();
1945 
1946  // We have lowered or deleted the type instrinsics, so we will no
1947  // longer have enough information to reason about the liveness of virtual
1948  // function pointers in GlobalDCE.
1949  for (GlobalVariable &GV : M.globals())
1950  GV.eraseMetadata(LLVMContext::MD_vcall_visibility);
1951 
1952  // The rest of the code is only necessary when exporting or during regular
1953  // LTO, so we are done.
1954  return true;
1955  }
1956 
1957  // Rebuild type metadata into a map for easy lookup.
1958  std::vector<VTableBits> Bits;
1960  buildTypeIdentifierMap(Bits, TypeIdMap);
1961  if (TypeIdMap.empty())
1962  return true;
1963 
1964  // Collect information from summary about which calls to try to devirtualize.
1965  if (ExportSummary) {
1967  for (auto &P : TypeIdMap) {
1968  if (auto *TypeId = dyn_cast<MDString>(P.first))
1969  MetadataByGUID[GlobalValue::getGUID(TypeId->getString())].push_back(
1970  TypeId);
1971  }
1972 
1973  for (auto &P : *ExportSummary) {
1974  for (auto &S : P.second.SummaryList) {
1975  auto *FS = dyn_cast<FunctionSummary>(S.get());
1976  if (!FS)
1977  continue;
1978  // FIXME: Only add live functions.
1979  for (FunctionSummary::VFuncId VF : FS->type_test_assume_vcalls()) {
1980  for (Metadata *MD : MetadataByGUID[VF.GUID]) {
1981  CallSlots[{MD, VF.Offset}].CSInfo.addSummaryTypeTestAssumeUser(FS);
1982  }
1983  }
1984  for (FunctionSummary::VFuncId VF : FS->type_checked_load_vcalls()) {
1985  for (Metadata *MD : MetadataByGUID[VF.GUID]) {
1986  CallSlots[{MD, VF.Offset}].CSInfo.addSummaryTypeCheckedLoadUser(FS);
1987  }
1988  }
1989  for (const FunctionSummary::ConstVCall &VC :
1990  FS->type_test_assume_const_vcalls()) {
1991  for (Metadata *MD : MetadataByGUID[VC.VFunc.GUID]) {
1992  CallSlots[{MD, VC.VFunc.Offset}]
1993  .ConstCSInfo[VC.Args]
1994  .addSummaryTypeTestAssumeUser(FS);
1995  }
1996  }
1997  for (const FunctionSummary::ConstVCall &VC :
1998  FS->type_checked_load_const_vcalls()) {
1999  for (Metadata *MD : MetadataByGUID[VC.VFunc.GUID]) {
2000  CallSlots[{MD, VC.VFunc.Offset}]
2001  .ConstCSInfo[VC.Args]
2002  .addSummaryTypeCheckedLoadUser(FS);
2003  }
2004  }
2005  }
2006  }
2007  }
2008 
2009  // For each (type, offset) pair:
2010  bool DidVirtualConstProp = false;
2011  std::map<std::string, Function*> DevirtTargets;
2012  for (auto &S : CallSlots) {
2013  // Search each of the members of the type identifier for the virtual
2014  // function implementation at offset S.first.ByteOffset, and add to
2015  // TargetsForSlot.
2016  std::vector<VirtualCallTarget> TargetsForSlot;
2017  if (tryFindVirtualCallTargets(TargetsForSlot, TypeIdMap[S.first.TypeID],
2018  S.first.ByteOffset)) {
2019  WholeProgramDevirtResolution *Res = nullptr;
2020  if (ExportSummary && isa<MDString>(S.first.TypeID))
2021  Res = &ExportSummary
2023  cast<MDString>(S.first.TypeID)->getString())
2024  .WPDRes[S.first.ByteOffset];
2025 
2026  if (!trySingleImplDevirt(ExportSummary, TargetsForSlot, S.second, Res)) {
2027  DidVirtualConstProp |=
2028  tryVirtualConstProp(TargetsForSlot, S.second, Res, S.first);
2029 
2030  tryICallBranchFunnel(TargetsForSlot, S.second, Res, S.first);
2031  }
2032 
2033  // Collect functions devirtualized at least for one call site for stats.
2034  if (RemarksEnabled)
2035  for (const auto &T : TargetsForSlot)
2036  if (T.WasDevirt)
2037  DevirtTargets[std::string(T.Fn->getName())] = T.Fn;
2038  }
2039 
2040  // CFI-specific: if we are exporting and any llvm.type.checked.load
2041  // intrinsics were *not* devirtualized, we need to add the resulting
2042  // llvm.type.test intrinsics to the function summaries so that the
2043  // LowerTypeTests pass will export them.
2044  if (ExportSummary && isa<MDString>(S.first.TypeID)) {
2045  auto GUID =
2046  GlobalValue::getGUID(cast<MDString>(S.first.TypeID)->getString());
2047  for (auto FS : S.second.CSInfo.SummaryTypeCheckedLoadUsers)
2048  FS->addTypeTest(GUID);
2049  for (auto &CCS : S.second.ConstCSInfo)
2050  for (auto FS : CCS.second.SummaryTypeCheckedLoadUsers)
2051  FS->addTypeTest(GUID);
2052  }
2053  }
2054 
2055  if (RemarksEnabled) {
2056  // Generate remarks for each devirtualized function.
2057  for (const auto &DT : DevirtTargets) {
2058  Function *F = DT.second;
2059 
2060  using namespace ore;
2061  OREGetter(F).emit(OptimizationRemark(DEBUG_TYPE, "Devirtualized", F)
2062  << "devirtualized "
2063  << NV("FunctionName", DT.first));
2064  }
2065  }
2066 
2067  removeRedundantTypeTests();
2068 
2069  // Rebuild each global we touched as part of virtual constant propagation to
2070  // include the before and after bytes.
2071  if (DidVirtualConstProp)
2072  for (VTableBits &B : Bits)
2073  rebuildGlobal(B);
2074 
2075  // We have lowered or deleted the type instrinsics, so we will no
2076  // longer have enough information to reason about the liveness of virtual
2077  // function pointers in GlobalDCE.
2078  for (GlobalVariable &GV : M.globals())
2079  GV.eraseMetadata(LLVMContext::MD_vcall_visibility);
2080 
2081  return true;
2082 }
2083 
2084 void DevirtIndex::run() {
2085  if (ExportSummary.typeIdCompatibleVtableMap().empty())
2086  return;
2087 
2089  for (auto &P : ExportSummary.typeIdCompatibleVtableMap()) {
2090  NameByGUID[GlobalValue::getGUID(P.first)].push_back(P.first);
2091  }
2092 
2093  // Collect information from summary about which calls to try to devirtualize.
2094  for (auto &P : ExportSummary) {
2095  for (auto &S : P.second.SummaryList) {
2096  auto *FS = dyn_cast<FunctionSummary>(S.get());
2097  if (!FS)
2098  continue;
2099  // FIXME: Only add live functions.
2100  for (FunctionSummary::VFuncId VF : FS->type_test_assume_vcalls()) {
2101  for (StringRef Name : NameByGUID[VF.GUID]) {
2102  CallSlots[{Name, VF.Offset}].CSInfo.addSummaryTypeTestAssumeUser(FS);
2103  }
2104  }
2105  for (FunctionSummary::VFuncId VF : FS->type_checked_load_vcalls()) {
2106  for (StringRef Name : NameByGUID[VF.GUID]) {
2107  CallSlots[{Name, VF.Offset}].CSInfo.addSummaryTypeCheckedLoadUser(FS);
2108  }
2109  }
2110  for (const FunctionSummary::ConstVCall &VC :
2111  FS->type_test_assume_const_vcalls()) {
2112  for (StringRef Name : NameByGUID[VC.VFunc.GUID]) {
2113  CallSlots[{Name, VC.VFunc.Offset}]
2114  .ConstCSInfo[VC.Args]
2115  .addSummaryTypeTestAssumeUser(FS);
2116  }
2117  }
2118  for (const FunctionSummary::ConstVCall &VC :
2119  FS->type_checked_load_const_vcalls()) {
2120  for (StringRef Name : NameByGUID[VC.VFunc.GUID]) {
2121  CallSlots[{Name, VC.VFunc.Offset}]
2122  .ConstCSInfo[VC.Args]
2123  .addSummaryTypeCheckedLoadUser(FS);
2124  }
2125  }
2126  }
2127  }
2128 
2129  std::set<ValueInfo> DevirtTargets;
2130  // For each (type, offset) pair:
2131  for (auto &S : CallSlots) {
2132  // Search each of the members of the type identifier for the virtual
2133  // function implementation at offset S.first.ByteOffset, and add to
2134  // TargetsForSlot.
2135  std::vector<ValueInfo> TargetsForSlot;
2136  auto TidSummary = ExportSummary.getTypeIdCompatibleVtableSummary(S.first.TypeID);
2137  assert(TidSummary);
2138  if (tryFindVirtualCallTargets(TargetsForSlot, *TidSummary,
2139  S.first.ByteOffset)) {
2141  &ExportSummary.getOrInsertTypeIdSummary(S.first.TypeID)
2142  .WPDRes[S.first.ByteOffset];
2143 
2144  if (!trySingleImplDevirt(TargetsForSlot, S.first, S.second, Res,
2145  DevirtTargets))
2146  continue;
2147  }
2148  }
2149 
2150  // Optionally have the thin link print message for each devirtualized
2151  // function.
2152  if (PrintSummaryDevirt)
2153  for (const auto &DT : DevirtTargets)
2154  errs() << "Devirtualized call to " << DT << "\n";
2155 
2156  return;
2157 }
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:235
uint64_t CallInst * C
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:115
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:80
unsigned getAlignment() const
FIXME: Remove this function once transition to Align is over.
Definition: GlobalObject.h:74
use_iterator use_end()
Definition: Value.h:365
static Error checkCombinedSummaryForTesting(ModuleSummaryIndex *Summary)
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:249
std::vector< TypeIdOffsetVtableInfo > TypeIdCompatibleVtableInfo
List of vtable definitions decorated by a particular type identifier, and their corresponding offsets...
LLVM_NODISCARD std::enable_if_t< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > dyn_cast(const Y &Val)
Definition: Casting.h:334
bool empty() const
Definition: Function.h:711
bool hasLocalLinkage() const
Definition: GlobalValue.h:445
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
DiagnosticInfoOptimizationBase::Argument NV
Whole program devirtualization
static unsigned getHashValue(const VTableSlot &I)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:769
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Value * CreateGEP(Value *Ptr, ArrayRef< Value *> IdxList, const Twine &Name="")
Definition: IRBuilder.h:1751
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:67
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1165
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Function * getCaller()
Helper to get the caller (the parent function).
void findDevirtualizableCallsForTypeTest(SmallVectorImpl< DevirtCallSite > &DevirtCalls, SmallVectorImpl< CallInst *> &Assumes, const CallInst *CI, DominatorTree &DT)
Given a call to the intrinsic @llvm.type.test, find all devirtualizable call sites based on the call ...
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Global variable summary information to aid decisions and implementation of importing.
VirtualCallTarget(Function *Fn, const TypeMemberInfo *TM)
Subclass of Error for the sole purpose of identifying the success path in the type system...
Definition: Error.h:330
void push_back(const T &Elt)
Definition: SmallVector.h:246
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
GlobalObject::VCallVisibility getVCallVisibility() const
Helper for check-and-exit error handling.
Definition: Error.h:1306
void initializeWholeProgramDevirtPass(PassRegistry &)
const auto & typeIdCompatibleVtableMap() const
This class represents a function call, abstracting a target machine&#39;s calling convention.
This file contains the declarations for metadata subclasses.
An immutable pass that tracks lazily created AssumptionCache objects.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:176
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1990
uint64_t Info
Additional information for the resolution:
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:56
Externally visible function.
Definition: GlobalValue.h:48
const StringMap< std::pair< uint64_t, ModuleHash > > & modulePaths() const
Table of modules, containing module hash and id.
static cl::opt< unsigned > ClThreshold("wholeprogramdevirt-branch-funnel-threshold", cl::Hidden, cl::init(10), cl::ZeroOrMore, cl::desc("Maximum number of call targets per " "call site to enable branch funnels"))
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:37
A debug info location.
Definition: DebugLoc.h:33
Metadata node.
Definition: Metadata.h:870
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1098
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:232
F(f)
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
Definition: InstrTypes.h:1215
const GlobalListType & getGlobalList() const
Get the Module&#39;s list of global variables (constant).
Definition: Module.h:535
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:187
Hexagon Common GEP
MemoryAccessKind computeFunctionBodyMemoryAccess(Function &F, AAResults &AAR)
Returns the memory access properties of this copy of the function.
const TypeIdSummary * getTypeIdSummary(StringRef TypeId) const
This returns either a pointer to the type id summary (if present in the summary map) or null (if not ...
void setAfterReturnValues(MutableArrayRef< VirtualCallTarget > Targets, uint64_t AllocAfter, unsigned BitWidth, int64_t &OffsetByte, uint64_t &OffsetBit)
std::map< uint64_t, WholeProgramDevirtResolution > WPDRes
Mapping from byte offset to whole-program devirt resolution for that (typeid, byte offset) pair...
VCallVisibility getVCallVisibility() const
Definition: Metadata.cpp:1513
static bool AddCalls(VTableSlotInfo &SlotInfo, const ValueInfo &Callee)
static Optional< bool > isBigEndian(const ArrayRef< int64_t > ByteOffsets, int64_t FirstOffset)
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:329
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:328
Export information to summary.
const char * getName() const
getName - Get the target name.
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, Instruction *InsertBefore=nullptr)
void setBeforeReturnValues(MutableArrayRef< VirtualCallTarget > Targets, uint64_t AllocBefore, unsigned BitWidth, int64_t &OffsetByte, uint64_t &OffsetBit)
unsigned countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0&#39;s from the least significant bit to the most stopping at the first 1...
Definition: MathExtras.h:157
void findDevirtualizableCallsForTypeCheckedLoad(SmallVectorImpl< DevirtCallSite > &DevirtCalls, SmallVectorImpl< Instruction *> &LoadedPtrs, SmallVectorImpl< Instruction *> &Preds, bool &HasNonCallUses, const CallInst *CI, DominatorTree &DT)
Given a call to the intrinsic @llvm.type.checked.load, find all devirtualizable call sites based on t...
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
AnalysisUsage & addRequired()
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Definition: Function.cpp:679
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:397
bool arg_empty() const
Definition: InstrTypes.h:1231
When retpoline mitigation is enabled, use a branch funnel that is defined in the merged module...
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:253
The returned value is undefined.
Definition: MathExtras.h:47
void updateIndexWPDForExports(ModuleSummaryIndex &Summary, function_ref< bool(StringRef, ValueInfo)> isExported, std::map< ValueInfo, std::vector< VTableSlotSummary >> &LocalWPDTargetsMap)
Call after cross-module importing to update the recorded single impl devirt target names for any loca...
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
static bool isEqual(const VTableSlotSummary &LHS, const VTableSlotSummary &RHS)
static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash)
Convenience method for creating a promoted global name for the given value name of a local...
This class is a functor to be used in legacy module or SCC passes for computing AA results for a func...
static constexpr const char * getRegularLTOModuleName()
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:55
No attributes have been set.
Definition: Attributes.h:72
AttributeSet getRetAttributes() const
The attributes for the ret value are returned.
unsigned Intr
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2322
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:342
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
static bool isEqual(const VTableSlot &LHS, const VTableSlot &RHS)
enum llvm::WholeProgramDevirtResolution::Kind TheKind
Class to represent function types.
Definition: DerivedTypes.h:108
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
void updateVCallVisibilityInIndex(ModuleSummaryIndex &Index, bool WholeProgramVisibilityEnabledInLTO)
If whole program visibility asserted, then upgrade all public vcall visibility metadata on vtable def...
InvokeInst * CreateInvoke(FunctionType *Ty, Value *Callee, BasicBlock *NormalDest, BasicBlock *UnwindDest, ArrayRef< Value *> Args, ArrayRef< OperandBundleDef > OpBundles, const Twine &Name="")
Create an invoke instruction.
Definition: IRBuilder.h:992
Class to represent array types.
Definition: DerivedTypes.h:358
AttributeSet getParamAttributes(unsigned ArgNo) const
The attributes for the argument or parameter at the given index are returned.
bool isVarArg() const
Definition: DerivedTypes.h:128
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
TypeIdSummary & getOrInsertTypeIdSummary(StringRef TypeId)
Return an existing or new TypeIdSummary entry for TypeId.
LinkageTypes getLinkage() const
Definition: GlobalValue.h:461
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:486
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:71
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
Definition: GlobalObject.h:83
Class to hold module path string table and global value map, and encapsulate methods for operating on...
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:410
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
ArrayRef< std::unique_ptr< GlobalValueSummary > > getSummaryList() const
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1139
The file should be opened in text mode on platforms that make this distinction.
Definition: FileSystem.h:767
Class to represent pointers.
Definition: DerivedTypes.h:662
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:157
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2004
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
Definition: DataLayout.cpp:789
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1172
#define P(N)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:434
bool hasAttribute(AttrKind Val) const
Return true if the attribute is present.
Definition: Attributes.cpp:282
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:137
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
static AttributeSet get(LLVMContext &C, const AttrBuilder &B)
Definition: Attributes.cpp:646
void setVCallVisibility(GlobalObject::VCallVisibility Vis)
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:229
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:298
void runWholeProgramDevirtOnIndex(ModuleSummaryIndex &Summary, std::set< GlobalValue::GUID > &ExportedGUIDs, std::map< ValueInfo, std::vector< VTableSlotSummary >> &LocalWPDTargetsMap)
Perform index-based whole program devirtualization on the Summary index.
Import information from summary.
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
A call site that could be devirtualized.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:156
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:690
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2044
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define DEBUG_TYPE
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:490
A manager for alias analyses.
FunctionType * getFunctionType() const
Definition: InstrTypes.h:1147
cl::list< std::string > SkipFunctionNames("wholeprogramdevirt-skip", cl::desc("Prevent function(s) from being devirtualized"), cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated)
Provide way to prevent certain function from being devirtualized.
Class to accumulate and hold information about a callee.
Diagnostic information for applied optimization remarks.
void eraseFromParent()
eraseFromParent - This method unlinks &#39;this&#39; from the containing module and deletes it...
Definition: Globals.cpp:380
INITIALIZE_PASS_BEGIN(WholeProgramDevirt, "wholeprogramdevirt", "Whole program devirtualization", false, false) INITIALIZE_PASS_END(WholeProgramDevirt
ArrayRef< Type * > params() const
Definition: DerivedTypes.h:135
Expected< T > errorOrToExpected(ErrorOr< T > &&EO)
Convert an ErrorOr<T> to an Expected<T>.
Definition: Error.h:1142
Represent the analysis usage information of a pass.
void addAttribute(unsigned i, Attribute::AttrKind Kind)
adds the attribute to the list of attributes.
Definition: Function.cpp:418
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:170
Type::TypeID TypeID
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:305
void setCallingConv(CallingConv::ID CC)
Definition: InstrTypes.h:1360
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:311
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:100
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again...
Definition: DenseMap.h:104
const ModuleHash & getModuleHash(const StringRef ModPath) const
Get the module SHA1 hash recorded for the given module path.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_NODISCARD bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:465
Class to represent integer types.
Definition: DerivedTypes.h:40
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:147
enum llvm::WholeProgramDevirtResolution::ByArg::Kind TheKind
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:507
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve &#39;CreateLoad(Ty, Ptr, "...")&#39; correctly, instead of converting the string to &#39;bool...
Definition: IRBuilder.h:1602
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1644
const Constant * stripPointerCasts() const
Definition: Constant.h:191
Comdat * getOrInsertComdat(StringRef Name)
Return the Comdat in the module with the specified name.
Definition: Module.cpp:526
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:593
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1006
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:234
bool isEnabled() const override
std::string & str()
Flushes the stream contents to the target string and returns the string&#39;s reference.
Definition: raw_ostream.h:539
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2268
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
An "identifier" for a virtual function.
Struct that holds a reference to a particular GUID in a global value summary.
This class evaluates LLVM IR, producing the Constant representing each SSA instruction.
Definition: Evaluator.h:38
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1952
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the generic address space (address sp...
Definition: DerivedTypes.h:677
This is the shared class of boolean and integer constants.
Definition: Constants.h:77
void setSelectionKind(SelectionKind Val)
Definition: Comdat.h:45
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:350
This struct is a compact representation of a valid (power of two) or undefined (0) alignment...
Definition: Alignment.h:119
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:883
Module.h This file contains the declarations for the Module class.
Single implementation devirtualization.
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:302
Type * getReturnType() const
Definition: DerivedTypes.h:129
static Expected< GlobPattern > create(StringRef Pat)
unsigned getProgramAddressSpace() const
Definition: DataLayout.h:287
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:786
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
ArrayRef< VirtFuncOffset > vTableFuncs() const
AttributeList getAttributes() const
Return the parameter attributes for this call.
Definition: InstrTypes.h:1375
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:742
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:454
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:962
wholeprogramdevirt
Target - Wrapper for Target specific information.
unsigned getNumAttrSets() const
A specification for a virtual function call with all constant integer arguments.
void WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, const std::map< std::string, GVSummaryMapTy > *ModuleToSummariesForIndex=nullptr)
Write the specified module summary index to the given raw output stream, where it will be written in ...
cl::opt< bool > WholeProgramVisibility("whole-program-visibility", cl::init(false), cl::Hidden, cl::ZeroOrMore, cl::desc("Enable whole program visibility"))
Provide a way to force enable whole program visibility in tests.
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:511
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Definition: ArrayRef.h:186
ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const
Return a ValueInfo for the index value_type (convenient when iterating index).
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:665
const Comdat * getComdat() const
Definition: GlobalObject.h:126
ModulePass * createWholeProgramDevirtPass(ModuleSummaryIndex *ExportSummary, const ModuleSummaryIndex *ImportSummary)
This pass implements whole-program devirtualization using type metadata.
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
Definition: InstrTypes.h:1209
use_iterator use_begin()
Definition: Value.h:357
static bool hasWholeProgramVisibility(bool WholeProgramVisibilityEnabledInLTO)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:158
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:354
std::map< std::vector< uint64_t >, ByArg > ResByArg
Resolutions for calls with all constant integer arguments (excluding the first argument, "this"), where the key is the argument vector.
Constant * getOrInsertGlobal(StringRef Name, Type *Ty, function_ref< GlobalVariable *()> CreateGlobalCallback)
Look up the specified global in the module symbol table.
Definition: Module.cpp:204
static cl::opt< std::string > ClWriteSummary("wholeprogramdevirt-write-summary", cl::desc("Write summary to given bitcode or YAML file after running pass. " "Output file format is deduced from extension: *.bc means writing " "bitcode, otherwise YAML"), cl::Hidden)
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:408
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:186
uint64_t findLowestOffset(ArrayRef< VirtualCallTarget > Targets, bool IsAfter, uint64_t Size)
StringRef getValueAsString() const
Return the attribute&#39;s value as a string.
Definition: Attributes.cpp:267
CallingConv::ID getCallingConv() const
Definition: InstrTypes.h:1356
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:270
static Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1976
#define I(x, y, z)
Definition: MD5.cpp:59
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:224
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:571
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
Definition: InstrTypes.h:1379
Type * getValueType() const
Definition: GlobalValue.h:273
const BasicBlockListType & getBasicBlockList() const
Get the underlying elements of the Function...
Definition: Function.h:682
uint32_t Size
Definition: Profile.cpp:46
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Definition: Attributes.cpp:81
Provides passes for computing function attributes based on interprocedural analyses.
Function summary information to aid decisions and implementation of importing.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
static cl::opt< bool > PrintSummaryDevirt("wholeprogramdevirt-print-index-based", cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::desc("Print index-based devirtualization messages"))
static cl::opt< PassSummaryAction > ClSummaryAction("wholeprogramdevirt-summary-action", cl::desc("What to do with the summary when running this pass"), cl::values(clEnumValN(PassSummaryAction::None, "none", "Do nothing"), clEnumValN(PassSummaryAction::Import, "import", "Import typeid resolutions from summary and globals"), clEnumValN(PassSummaryAction::Export, "export", "Export typeid resolutions to summary and globals")), cl::Hidden)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:521
aarch64 promote const
LLVM Value Representation.
Definition: Value.h:74
Expected< std::unique_ptr< ModuleSummaryIndex > > getModuleSummaryIndex(MemoryBufferRef Buffer)
Parse the specified bitcode buffer, returning the module summary index.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
static const Function * getParent(const Value *V)
AttributeSet getFnAttributes() const
The function attributes are returned.
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:340
static Constant * getAnon(ArrayRef< Constant *> V, bool Packed=false)
Return an anonymous struct that has the specified elements.
Definition: Constants.h:475
cl::opt< bool > DisableWholeProgramVisibility("disable-whole-program-visibility", cl::init(false), cl::Hidden, cl::ZeroOrMore, cl::desc("Disable whole program visibility (overrides enabling options)"))
Provide a way to force disable whole program for debugging or workarounds, when enabled via the linke...
static Constant * get(LLVMContext &Context, ArrayRef< ElementTy > Elts)
get() constructor - Return a constant with array type with an element count and element type matching...
Definition: Constants.h:705
iterator_range< global_iterator > globals()
Definition: Module.h:599
iterator_range< global_object_iterator > global_objects()
Definition: Module.cpp:412
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
This is the interface for LLVM&#39;s primary stateless and local alias analysis.
A single uniqued string.
Definition: Metadata.h:602
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:261
static cl::opt< std::string > ClReadSummary("wholeprogramdevirt-read-summary", cl::desc("Read summary from given bitcode or YAML file before running pass"), cl::Hidden)
void updateVCallVisibilityInModule(Module &M, bool WholeProgramVisibilityEnabledInLTO)
If whole program visibility asserted, then upgrade all public vcall visibility metadata on vtable def...
Root of the metadata hierarchy.
Definition: Metadata.h:58
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:184
void setSection(StringRef S)
Change the section for this global.
Definition: Globals.cpp:211
The optimization diagnostic interface.
static unsigned getHashValue(const VTableSlotSummary &I)
bool use_empty() const
Definition: Value.h:341
Constant * getPointerAtOffset(Constant *I, uint64_t Offset, Module &M)
static GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition: Globals.cpp:480
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
The following data structures summarize type metadata information.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1202
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute >> Attrs)
Create an AttributeList with the specified parameters in it.
IntegerType * Int32Ty
const BasicBlock * getParent() const
Definition: Instruction.h:85
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:953