1df49d1bbSPeter Collingbourne //===- WholeProgramDevirt.cpp - Whole program virtual call optimization ---===//
2df49d1bbSPeter Collingbourne //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6df49d1bbSPeter Collingbourne //
7df49d1bbSPeter Collingbourne //===----------------------------------------------------------------------===//
8df49d1bbSPeter Collingbourne //
9df49d1bbSPeter Collingbourne // This pass implements whole program optimization of virtual calls in cases
107efd7506SPeter Collingbourne // where we know (via !type metadata) that the list of callees is fixed. This
11df49d1bbSPeter Collingbourne // includes the following:
12df49d1bbSPeter Collingbourne // - Single implementation devirtualization: if a virtual call has a single
13df49d1bbSPeter Collingbourne //   possible callee, replace all calls with a direct call to that callee.
14df49d1bbSPeter Collingbourne // - Virtual constant propagation: if the virtual function's return type is an
15df49d1bbSPeter Collingbourne //   integer <=64 bits and all possible callees are readnone, for each class and
16df49d1bbSPeter Collingbourne //   each list of constant arguments: evaluate the function, store the return
17df49d1bbSPeter Collingbourne //   value alongside the virtual table, and rewrite each virtual call as a load
18df49d1bbSPeter Collingbourne //   from the virtual table.
19df49d1bbSPeter Collingbourne // - Uniform return value optimization: if the conditions for virtual constant
20df49d1bbSPeter Collingbourne //   propagation hold and each function returns the same constant value, replace
21df49d1bbSPeter Collingbourne //   each virtual call with that constant.
22df49d1bbSPeter Collingbourne // - Unique return value optimization for i1 return values: if the conditions
23df49d1bbSPeter Collingbourne //   for virtual constant propagation hold and a single vtable's function
24df49d1bbSPeter Collingbourne //   returns 0, or a single vtable's function returns 1, replace each virtual
25df49d1bbSPeter Collingbourne //   call with a comparison of the vptr against that vtable's address.
26df49d1bbSPeter Collingbourne //
27d2df54e6STeresa Johnson // This pass is intended to be used during the regular and thin LTO pipelines:
28d2df54e6STeresa Johnson //
29b406baaeSPeter Collingbourne // During regular LTO, the pass determines the best optimization for each
30b406baaeSPeter Collingbourne // virtual call and applies the resolutions directly to virtual calls that are
31b406baaeSPeter Collingbourne // eligible for virtual call optimization (i.e. calls that use either of the
32d2df54e6STeresa Johnson // llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics).
33d2df54e6STeresa Johnson //
34d2df54e6STeresa Johnson // During hybrid Regular/ThinLTO, the pass operates in two phases:
35b406baaeSPeter Collingbourne // - Export phase: this is run during the thin link over a single merged module
36b406baaeSPeter Collingbourne //   that contains all vtables with !type metadata that participate in the link.
37b406baaeSPeter Collingbourne //   The pass computes a resolution for each virtual call and stores it in the
38b406baaeSPeter Collingbourne //   type identifier summary.
39b406baaeSPeter Collingbourne // - Import phase: this is run during the thin backends over the individual
40b406baaeSPeter Collingbourne //   modules. The pass applies the resolutions previously computed during the
41b406baaeSPeter Collingbourne //   import phase to each eligible virtual call.
42b406baaeSPeter Collingbourne //
43d2df54e6STeresa Johnson // During ThinLTO, the pass operates in two phases:
44d2df54e6STeresa Johnson // - Export phase: this is run during the thin link over the index which
45d2df54e6STeresa Johnson //   contains a summary of all vtables with !type metadata that participate in
46d2df54e6STeresa Johnson //   the link. It computes a resolution for each virtual call and stores it in
47d2df54e6STeresa Johnson //   the type identifier summary. Only single implementation devirtualization
48d2df54e6STeresa Johnson //   is supported.
49d2df54e6STeresa Johnson // - Import phase: (same as with hybrid case above).
50d2df54e6STeresa Johnson //
51df49d1bbSPeter Collingbourne //===----------------------------------------------------------------------===//
52df49d1bbSPeter Collingbourne 
53df49d1bbSPeter Collingbourne #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
54b550cb17SMehdi Amini #include "llvm/ADT/ArrayRef.h"
55cdc71612SEugene Zelenko #include "llvm/ADT/DenseMap.h"
56cdc71612SEugene Zelenko #include "llvm/ADT/DenseMapInfo.h"
57df49d1bbSPeter Collingbourne #include "llvm/ADT/DenseSet.h"
58df49d1bbSPeter Collingbourne #include "llvm/ADT/MapVector.h"
59cdc71612SEugene Zelenko #include "llvm/ADT/SmallVector.h"
60ced9a795STeresa Johnson #include "llvm/ADT/Statistic.h"
6106fd973cSSimon Pilgrim #include "llvm/ADT/Triple.h"
626bda14b3SChandler Carruth #include "llvm/ADT/iterator_range.h"
632ce38b3fSdfukalov #include "llvm/Analysis/AssumptionCache.h"
6437317f12SPeter Collingbourne #include "llvm/Analysis/BasicAliasAnalysis.h"
650965da20SAdam Nemet #include "llvm/Analysis/OptimizationRemarkEmitter.h"
667efd7506SPeter Collingbourne #include "llvm/Analysis/TypeMetadataUtils.h"
678973fae1SEvgeny Leviant #include "llvm/Bitcode/BitcodeReader.h"
688973fae1SEvgeny Leviant #include "llvm/Bitcode/BitcodeWriter.h"
69df49d1bbSPeter Collingbourne #include "llvm/IR/Constants.h"
70df49d1bbSPeter Collingbourne #include "llvm/IR/DataLayout.h"
71cdc71612SEugene Zelenko #include "llvm/IR/DebugLoc.h"
72cdc71612SEugene Zelenko #include "llvm/IR/DerivedTypes.h"
73f24136f1STeresa Johnson #include "llvm/IR/Dominators.h"
74cdc71612SEugene Zelenko #include "llvm/IR/Function.h"
75cdc71612SEugene Zelenko #include "llvm/IR/GlobalAlias.h"
76cdc71612SEugene Zelenko #include "llvm/IR/GlobalVariable.h"
77df49d1bbSPeter Collingbourne #include "llvm/IR/IRBuilder.h"
78cdc71612SEugene Zelenko #include "llvm/IR/InstrTypes.h"
79cdc71612SEugene Zelenko #include "llvm/IR/Instruction.h"
80df49d1bbSPeter Collingbourne #include "llvm/IR/Instructions.h"
81df49d1bbSPeter Collingbourne #include "llvm/IR/Intrinsics.h"
82cdc71612SEugene Zelenko #include "llvm/IR/LLVMContext.h"
83fee0bde4STeresa Johnson #include "llvm/IR/MDBuilder.h"
84cdc71612SEugene Zelenko #include "llvm/IR/Metadata.h"
85df49d1bbSPeter Collingbourne #include "llvm/IR/Module.h"
862b33f653SPeter Collingbourne #include "llvm/IR/ModuleSummaryIndexYAML.h"
8705da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
88df49d1bbSPeter Collingbourne #include "llvm/Pass.h"
89cdc71612SEugene Zelenko #include "llvm/PassRegistry.h"
90cdc71612SEugene Zelenko #include "llvm/Support/Casting.h"
914c1a1d3cSReid Kleckner #include "llvm/Support/CommandLine.h"
928973fae1SEvgeny Leviant #include "llvm/Support/Errc.h"
932b33f653SPeter Collingbourne #include "llvm/Support/Error.h"
942b33f653SPeter Collingbourne #include "llvm/Support/FileSystem.h"
956d2032e2Sevgeny #include "llvm/Support/GlobPattern.h"
96cdc71612SEugene Zelenko #include "llvm/Support/MathExtras.h"
97b550cb17SMehdi Amini #include "llvm/Transforms/IPO.h"
9837317f12SPeter Collingbourne #include "llvm/Transforms/IPO/FunctionAttrs.h"
99d55d46f4STeresa Johnson #include "llvm/Transforms/Utils/BasicBlockUtils.h"
100fee0bde4STeresa Johnson #include "llvm/Transforms/Utils/CallPromotionUtils.h"
101df49d1bbSPeter Collingbourne #include "llvm/Transforms/Utils/Evaluator.h"
102cdc71612SEugene Zelenko #include <algorithm>
103cdc71612SEugene Zelenko #include <cstddef>
104cdc71612SEugene Zelenko #include <map>
105df49d1bbSPeter Collingbourne #include <set>
106cdc71612SEugene Zelenko #include <string>
107df49d1bbSPeter Collingbourne 
108df49d1bbSPeter Collingbourne using namespace llvm;
109df49d1bbSPeter Collingbourne using namespace wholeprogramdevirt;
110df49d1bbSPeter Collingbourne 
111df49d1bbSPeter Collingbourne #define DEBUG_TYPE "wholeprogramdevirt"
112df49d1bbSPeter Collingbourne 
113ced9a795STeresa Johnson STATISTIC(NumDevirtTargets, "Number of whole program devirtualization targets");
114ced9a795STeresa Johnson STATISTIC(NumSingleImpl, "Number of single implementation devirtualizations");
115ced9a795STeresa Johnson STATISTIC(NumBranchFunnel, "Number of branch funnels");
116ced9a795STeresa Johnson STATISTIC(NumUniformRetVal, "Number of uniform return value optimizations");
117ced9a795STeresa Johnson STATISTIC(NumUniqueRetVal, "Number of unique return value optimizations");
118ced9a795STeresa Johnson STATISTIC(NumVirtConstProp1Bit,
119ced9a795STeresa Johnson           "Number of 1 bit virtual constant propagations");
120ced9a795STeresa Johnson STATISTIC(NumVirtConstProp, "Number of virtual constant propagations");
121ced9a795STeresa Johnson 
1222b33f653SPeter Collingbourne static cl::opt<PassSummaryAction> ClSummaryAction(
1232b33f653SPeter Collingbourne     "wholeprogramdevirt-summary-action",
1242b33f653SPeter Collingbourne     cl::desc("What to do with the summary when running this pass"),
1252b33f653SPeter Collingbourne     cl::values(clEnumValN(PassSummaryAction::None, "none", "Do nothing"),
1262b33f653SPeter Collingbourne                clEnumValN(PassSummaryAction::Import, "import",
1272b33f653SPeter Collingbourne                           "Import typeid resolutions from summary and globals"),
1282b33f653SPeter Collingbourne                clEnumValN(PassSummaryAction::Export, "export",
1292b33f653SPeter Collingbourne                           "Export typeid resolutions to summary and globals")),
1302b33f653SPeter Collingbourne     cl::Hidden);
1312b33f653SPeter Collingbourne 
1322b33f653SPeter Collingbourne static cl::opt<std::string> ClReadSummary(
1332b33f653SPeter Collingbourne     "wholeprogramdevirt-read-summary",
1348973fae1SEvgeny Leviant     cl::desc(
1358973fae1SEvgeny Leviant         "Read summary from given bitcode or YAML file before running pass"),
1362b33f653SPeter Collingbourne     cl::Hidden);
1372b33f653SPeter Collingbourne 
1382b33f653SPeter Collingbourne static cl::opt<std::string> ClWriteSummary(
1392b33f653SPeter Collingbourne     "wholeprogramdevirt-write-summary",
1408973fae1SEvgeny Leviant     cl::desc("Write summary to given bitcode or YAML file after running pass. "
1418973fae1SEvgeny Leviant              "Output file format is deduced from extension: *.bc means writing "
1428973fae1SEvgeny Leviant              "bitcode, otherwise YAML"),
1432b33f653SPeter Collingbourne     cl::Hidden);
1442b33f653SPeter Collingbourne 
1459cb59b92SVitaly Buka static cl::opt<unsigned>
1469cb59b92SVitaly Buka     ClThreshold("wholeprogramdevirt-branch-funnel-threshold", cl::Hidden,
14736c7d79dSFangrui Song                 cl::init(10),
14866f53d71SVitaly Buka                 cl::desc("Maximum number of call targets per "
14966f53d71SVitaly Buka                          "call site to enable branch funnels"));
15066f53d71SVitaly Buka 
151d2df54e6STeresa Johnson static cl::opt<bool>
152d2df54e6STeresa Johnson     PrintSummaryDevirt("wholeprogramdevirt-print-index-based", cl::Hidden,
153d2df54e6STeresa Johnson                        cl::desc("Print index-based devirtualization messages"));
154d2df54e6STeresa Johnson 
1552f63d549STeresa Johnson /// Provide a way to force enable whole program visibility in tests.
1562f63d549STeresa Johnson /// This is needed to support legacy tests that don't contain
1572f63d549STeresa Johnson /// !vcall_visibility metadata (the mere presense of type tests
1582f63d549STeresa Johnson /// previously implied hidden visibility).
159d8aba75aSFangrui Song static cl::opt<bool>
16036c7d79dSFangrui Song     WholeProgramVisibility("whole-program-visibility", cl::Hidden,
1612f63d549STeresa Johnson                            cl::desc("Enable whole program visibility"));
1622f63d549STeresa Johnson 
1632f63d549STeresa Johnson /// Provide a way to force disable whole program for debugging or workarounds,
1642f63d549STeresa Johnson /// when enabled via the linker.
165d8aba75aSFangrui Song static cl::opt<bool> DisableWholeProgramVisibility(
166d86a206fSFangrui Song     "disable-whole-program-visibility", cl::Hidden,
1672f63d549STeresa Johnson     cl::desc("Disable whole program visibility (overrides enabling options)"));
1682f63d549STeresa Johnson 
1696d2032e2Sevgeny /// Provide way to prevent certain function from being devirtualized
170d8aba75aSFangrui Song static cl::list<std::string>
1716d2032e2Sevgeny     SkipFunctionNames("wholeprogramdevirt-skip",
1726d2032e2Sevgeny                       cl::desc("Prevent function(s) from being devirtualized"),
173d0d1c416SFangrui Song                       cl::Hidden, cl::CommaSeparated);
1746d2032e2Sevgeny 
175fee0bde4STeresa Johnson /// Mechanism to add runtime checking of devirtualization decisions, optionally
176fee0bde4STeresa Johnson /// trapping or falling back to indirect call on any that are not correct.
177fee0bde4STeresa Johnson /// Trapping mode is useful for debugging undefined behavior leading to failures
178fee0bde4STeresa Johnson /// with WPD. Fallback mode is useful for ensuring safety when whole program
179fee0bde4STeresa Johnson /// visibility may be compromised.
180fee0bde4STeresa Johnson enum WPDCheckMode { None, Trap, Fallback };
181fee0bde4STeresa Johnson static cl::opt<WPDCheckMode> DevirtCheckMode(
182557efc9aSFangrui Song     "wholeprogramdevirt-check", cl::Hidden,
183fee0bde4STeresa Johnson     cl::desc("Type of checking for incorrect devirtualizations"),
184fee0bde4STeresa Johnson     cl::values(clEnumValN(WPDCheckMode::None, "none", "No checking"),
185fee0bde4STeresa Johnson                clEnumValN(WPDCheckMode::Trap, "trap", "Trap when incorrect"),
186fee0bde4STeresa Johnson                clEnumValN(WPDCheckMode::Fallback, "fallback",
187fee0bde4STeresa Johnson                           "Fallback to indirect when incorrect")));
188d55d46f4STeresa Johnson 
1896d2032e2Sevgeny namespace {
1906d2032e2Sevgeny struct PatternList {
1916d2032e2Sevgeny   std::vector<GlobPattern> Patterns;
init__anona5c3867a0111::PatternList1926d2032e2Sevgeny   template <class T> void init(const T &StringList) {
1936d2032e2Sevgeny     for (const auto &S : StringList)
1946d2032e2Sevgeny       if (Expected<GlobPattern> Pat = GlobPattern::create(S))
1956d2032e2Sevgeny         Patterns.push_back(std::move(*Pat));
1966d2032e2Sevgeny   }
match__anona5c3867a0111::PatternList1976d2032e2Sevgeny   bool match(StringRef S) {
1986d2032e2Sevgeny     for (const GlobPattern &P : Patterns)
1996d2032e2Sevgeny       if (P.match(S))
2006d2032e2Sevgeny         return true;
2016d2032e2Sevgeny     return false;
2026d2032e2Sevgeny   }
2036d2032e2Sevgeny };
2046d2032e2Sevgeny } // namespace
2056d2032e2Sevgeny 
206df49d1bbSPeter Collingbourne // Find the minimum offset that we may store a value of size Size bits at. If
207df49d1bbSPeter Collingbourne // IsAfter is set, look for an offset before the object, otherwise look for an
208df49d1bbSPeter Collingbourne // offset after the object.
209df49d1bbSPeter Collingbourne uint64_t
findLowestOffset(ArrayRef<VirtualCallTarget> Targets,bool IsAfter,uint64_t Size)210df49d1bbSPeter Collingbourne wholeprogramdevirt::findLowestOffset(ArrayRef<VirtualCallTarget> Targets,
211df49d1bbSPeter Collingbourne                                      bool IsAfter, uint64_t Size) {
212df49d1bbSPeter Collingbourne   // Find a minimum offset taking into account only vtable sizes.
213df49d1bbSPeter Collingbourne   uint64_t MinByte = 0;
214df49d1bbSPeter Collingbourne   for (const VirtualCallTarget &Target : Targets) {
215df49d1bbSPeter Collingbourne     if (IsAfter)
216df49d1bbSPeter Collingbourne       MinByte = std::max(MinByte, Target.minAfterBytes());
217df49d1bbSPeter Collingbourne     else
218df49d1bbSPeter Collingbourne       MinByte = std::max(MinByte, Target.minBeforeBytes());
219df49d1bbSPeter Collingbourne   }
220df49d1bbSPeter Collingbourne 
221df49d1bbSPeter Collingbourne   // Build a vector of arrays of bytes covering, for each target, a slice of the
222df49d1bbSPeter Collingbourne   // used region (see AccumBitVector::BytesUsed in
223df49d1bbSPeter Collingbourne   // llvm/Transforms/IPO/WholeProgramDevirt.h) starting at MinByte. Effectively,
224df49d1bbSPeter Collingbourne   // this aligns the used regions to start at MinByte.
225df49d1bbSPeter Collingbourne   //
226df49d1bbSPeter Collingbourne   // In this example, A, B and C are vtables, # is a byte already allocated for
227df49d1bbSPeter Collingbourne   // a virtual function pointer, AAAA... (etc.) are the used regions for the
228df49d1bbSPeter Collingbourne   // vtables and Offset(X) is the value computed for the Offset variable below
229df49d1bbSPeter Collingbourne   // for X.
230df49d1bbSPeter Collingbourne   //
231df49d1bbSPeter Collingbourne   //                    Offset(A)
232df49d1bbSPeter Collingbourne   //                    |       |
233df49d1bbSPeter Collingbourne   //                            |MinByte
234df49d1bbSPeter Collingbourne   // A: ################AAAAAAAA|AAAAAAAA
235df49d1bbSPeter Collingbourne   // B: ########BBBBBBBBBBBBBBBB|BBBB
236df49d1bbSPeter Collingbourne   // C: ########################|CCCCCCCCCCCCCCCC
237df49d1bbSPeter Collingbourne   //            |   Offset(B)   |
238df49d1bbSPeter Collingbourne   //
239df49d1bbSPeter Collingbourne   // This code produces the slices of A, B and C that appear after the divider
240df49d1bbSPeter Collingbourne   // at MinByte.
241df49d1bbSPeter Collingbourne   std::vector<ArrayRef<uint8_t>> Used;
242df49d1bbSPeter Collingbourne   for (const VirtualCallTarget &Target : Targets) {
2437efd7506SPeter Collingbourne     ArrayRef<uint8_t> VTUsed = IsAfter ? Target.TM->Bits->After.BytesUsed
2447efd7506SPeter Collingbourne                                        : Target.TM->Bits->Before.BytesUsed;
245df49d1bbSPeter Collingbourne     uint64_t Offset = IsAfter ? MinByte - Target.minAfterBytes()
246df49d1bbSPeter Collingbourne                               : MinByte - Target.minBeforeBytes();
247df49d1bbSPeter Collingbourne 
248df49d1bbSPeter Collingbourne     // Disregard used regions that are smaller than Offset. These are
249df49d1bbSPeter Collingbourne     // effectively all-free regions that do not need to be checked.
250df49d1bbSPeter Collingbourne     if (VTUsed.size() > Offset)
251df49d1bbSPeter Collingbourne       Used.push_back(VTUsed.slice(Offset));
252df49d1bbSPeter Collingbourne   }
253df49d1bbSPeter Collingbourne 
254df49d1bbSPeter Collingbourne   if (Size == 1) {
255df49d1bbSPeter Collingbourne     // Find a free bit in each member of Used.
256df49d1bbSPeter Collingbourne     for (unsigned I = 0;; ++I) {
257df49d1bbSPeter Collingbourne       uint8_t BitsUsed = 0;
258df49d1bbSPeter Collingbourne       for (auto &&B : Used)
259df49d1bbSPeter Collingbourne         if (I < B.size())
260df49d1bbSPeter Collingbourne           BitsUsed |= B[I];
261df49d1bbSPeter Collingbourne       if (BitsUsed != 0xff)
262df49d1bbSPeter Collingbourne         return (MinByte + I) * 8 +
263df49d1bbSPeter Collingbourne                countTrailingZeros(uint8_t(~BitsUsed), ZB_Undefined);
264df49d1bbSPeter Collingbourne     }
265df49d1bbSPeter Collingbourne   } else {
266df49d1bbSPeter Collingbourne     // Find a free (Size/8) byte region in each member of Used.
267df49d1bbSPeter Collingbourne     // FIXME: see if alignment helps.
268df49d1bbSPeter Collingbourne     for (unsigned I = 0;; ++I) {
269df49d1bbSPeter Collingbourne       for (auto &&B : Used) {
270df49d1bbSPeter Collingbourne         unsigned Byte = 0;
271df49d1bbSPeter Collingbourne         while ((I + Byte) < B.size() && Byte < (Size / 8)) {
272df49d1bbSPeter Collingbourne           if (B[I + Byte])
273df49d1bbSPeter Collingbourne             goto NextI;
274df49d1bbSPeter Collingbourne           ++Byte;
275df49d1bbSPeter Collingbourne         }
276df49d1bbSPeter Collingbourne       }
277df49d1bbSPeter Collingbourne       return (MinByte + I) * 8;
278df49d1bbSPeter Collingbourne     NextI:;
279df49d1bbSPeter Collingbourne     }
280df49d1bbSPeter Collingbourne   }
281df49d1bbSPeter Collingbourne }
282df49d1bbSPeter Collingbourne 
setBeforeReturnValues(MutableArrayRef<VirtualCallTarget> Targets,uint64_t AllocBefore,unsigned BitWidth,int64_t & OffsetByte,uint64_t & OffsetBit)283df49d1bbSPeter Collingbourne void wholeprogramdevirt::setBeforeReturnValues(
284df49d1bbSPeter Collingbourne     MutableArrayRef<VirtualCallTarget> Targets, uint64_t AllocBefore,
285df49d1bbSPeter Collingbourne     unsigned BitWidth, int64_t &OffsetByte, uint64_t &OffsetBit) {
286df49d1bbSPeter Collingbourne   if (BitWidth == 1)
287df49d1bbSPeter Collingbourne     OffsetByte = -(AllocBefore / 8 + 1);
288df49d1bbSPeter Collingbourne   else
289df49d1bbSPeter Collingbourne     OffsetByte = -((AllocBefore + 7) / 8 + (BitWidth + 7) / 8);
290df49d1bbSPeter Collingbourne   OffsetBit = AllocBefore % 8;
291df49d1bbSPeter Collingbourne 
292df49d1bbSPeter Collingbourne   for (VirtualCallTarget &Target : Targets) {
293df49d1bbSPeter Collingbourne     if (BitWidth == 1)
294df49d1bbSPeter Collingbourne       Target.setBeforeBit(AllocBefore);
295df49d1bbSPeter Collingbourne     else
296df49d1bbSPeter Collingbourne       Target.setBeforeBytes(AllocBefore, (BitWidth + 7) / 8);
297df49d1bbSPeter Collingbourne   }
298df49d1bbSPeter Collingbourne }
299df49d1bbSPeter Collingbourne 
setAfterReturnValues(MutableArrayRef<VirtualCallTarget> Targets,uint64_t AllocAfter,unsigned BitWidth,int64_t & OffsetByte,uint64_t & OffsetBit)300df49d1bbSPeter Collingbourne void wholeprogramdevirt::setAfterReturnValues(
301df49d1bbSPeter Collingbourne     MutableArrayRef<VirtualCallTarget> Targets, uint64_t AllocAfter,
302df49d1bbSPeter Collingbourne     unsigned BitWidth, int64_t &OffsetByte, uint64_t &OffsetBit) {
303df49d1bbSPeter Collingbourne   if (BitWidth == 1)
304df49d1bbSPeter Collingbourne     OffsetByte = AllocAfter / 8;
305df49d1bbSPeter Collingbourne   else
306df49d1bbSPeter Collingbourne     OffsetByte = (AllocAfter + 7) / 8;
307df49d1bbSPeter Collingbourne   OffsetBit = AllocAfter % 8;
308df49d1bbSPeter Collingbourne 
309df49d1bbSPeter Collingbourne   for (VirtualCallTarget &Target : Targets) {
310df49d1bbSPeter Collingbourne     if (BitWidth == 1)
311df49d1bbSPeter Collingbourne       Target.setAfterBit(AllocAfter);
312df49d1bbSPeter Collingbourne     else
313df49d1bbSPeter Collingbourne       Target.setAfterBytes(AllocAfter, (BitWidth + 7) / 8);
314df49d1bbSPeter Collingbourne   }
315df49d1bbSPeter Collingbourne }
316df49d1bbSPeter Collingbourne 
VirtualCallTarget(Function * Fn,const TypeMemberInfo * TM)3177efd7506SPeter Collingbourne VirtualCallTarget::VirtualCallTarget(Function *Fn, const TypeMemberInfo *TM)
3187efd7506SPeter Collingbourne     : Fn(Fn), TM(TM),
31989439a79SIvan Krasin       IsBigEndian(Fn->getParent()->getDataLayout().isBigEndian()), WasDevirt(false) {}
320df49d1bbSPeter Collingbourne 
321df49d1bbSPeter Collingbourne namespace {
322df49d1bbSPeter Collingbourne 
3237efd7506SPeter Collingbourne // A slot in a set of virtual tables. The TypeID identifies the set of virtual
324df49d1bbSPeter Collingbourne // tables, and the ByteOffset is the offset in bytes from the address point to
325df49d1bbSPeter Collingbourne // the virtual function pointer.
326df49d1bbSPeter Collingbourne struct VTableSlot {
3277efd7506SPeter Collingbourne   Metadata *TypeID;
328df49d1bbSPeter Collingbourne   uint64_t ByteOffset;
329df49d1bbSPeter Collingbourne };
330df49d1bbSPeter Collingbourne 
331cdc71612SEugene Zelenko } // end anonymous namespace
332df49d1bbSPeter Collingbourne 
3339b656527SPeter Collingbourne namespace llvm {
3349b656527SPeter Collingbourne 
335df49d1bbSPeter Collingbourne template <> struct DenseMapInfo<VTableSlot> {
getEmptyKeyllvm::DenseMapInfo336df49d1bbSPeter Collingbourne   static VTableSlot getEmptyKey() {
337df49d1bbSPeter Collingbourne     return {DenseMapInfo<Metadata *>::getEmptyKey(),
338df49d1bbSPeter Collingbourne             DenseMapInfo<uint64_t>::getEmptyKey()};
339df49d1bbSPeter Collingbourne   }
getTombstoneKeyllvm::DenseMapInfo340df49d1bbSPeter Collingbourne   static VTableSlot getTombstoneKey() {
341df49d1bbSPeter Collingbourne     return {DenseMapInfo<Metadata *>::getTombstoneKey(),
342df49d1bbSPeter Collingbourne             DenseMapInfo<uint64_t>::getTombstoneKey()};
343df49d1bbSPeter Collingbourne   }
getHashValuellvm::DenseMapInfo344df49d1bbSPeter Collingbourne   static unsigned getHashValue(const VTableSlot &I) {
3457efd7506SPeter Collingbourne     return DenseMapInfo<Metadata *>::getHashValue(I.TypeID) ^
346df49d1bbSPeter Collingbourne            DenseMapInfo<uint64_t>::getHashValue(I.ByteOffset);
347df49d1bbSPeter Collingbourne   }
isEqualllvm::DenseMapInfo348df49d1bbSPeter Collingbourne   static bool isEqual(const VTableSlot &LHS,
349df49d1bbSPeter Collingbourne                       const VTableSlot &RHS) {
3507efd7506SPeter Collingbourne     return LHS.TypeID == RHS.TypeID && LHS.ByteOffset == RHS.ByteOffset;
351df49d1bbSPeter Collingbourne   }
352df49d1bbSPeter Collingbourne };
353df49d1bbSPeter Collingbourne 
354d2df54e6STeresa Johnson template <> struct DenseMapInfo<VTableSlotSummary> {
getEmptyKeyllvm::DenseMapInfo355d2df54e6STeresa Johnson   static VTableSlotSummary getEmptyKey() {
356d2df54e6STeresa Johnson     return {DenseMapInfo<StringRef>::getEmptyKey(),
357d2df54e6STeresa Johnson             DenseMapInfo<uint64_t>::getEmptyKey()};
358d2df54e6STeresa Johnson   }
getTombstoneKeyllvm::DenseMapInfo359d2df54e6STeresa Johnson   static VTableSlotSummary getTombstoneKey() {
360d2df54e6STeresa Johnson     return {DenseMapInfo<StringRef>::getTombstoneKey(),
361d2df54e6STeresa Johnson             DenseMapInfo<uint64_t>::getTombstoneKey()};
362d2df54e6STeresa Johnson   }
getHashValuellvm::DenseMapInfo363d2df54e6STeresa Johnson   static unsigned getHashValue(const VTableSlotSummary &I) {
364d2df54e6STeresa Johnson     return DenseMapInfo<StringRef>::getHashValue(I.TypeID) ^
365d2df54e6STeresa Johnson            DenseMapInfo<uint64_t>::getHashValue(I.ByteOffset);
366d2df54e6STeresa Johnson   }
isEqualllvm::DenseMapInfo367d2df54e6STeresa Johnson   static bool isEqual(const VTableSlotSummary &LHS,
368d2df54e6STeresa Johnson                       const VTableSlotSummary &RHS) {
369d2df54e6STeresa Johnson     return LHS.TypeID == RHS.TypeID && LHS.ByteOffset == RHS.ByteOffset;
370d2df54e6STeresa Johnson   }
371d2df54e6STeresa Johnson };
372d2df54e6STeresa Johnson 
373cdc71612SEugene Zelenko } // end namespace llvm
3749b656527SPeter Collingbourne 
375df49d1bbSPeter Collingbourne namespace {
376df49d1bbSPeter Collingbourne 
37709a704c5SMingming Liu // Returns true if the function must be unreachable based on ValueInfo.
37809a704c5SMingming Liu //
37909a704c5SMingming Liu // In particular, identifies a function as unreachable in the following
38009a704c5SMingming Liu // conditions
38109a704c5SMingming Liu //   1) All summaries are live.
38209a704c5SMingming Liu //   2) All function summaries indicate it's unreachable
mustBeUnreachableFunction(ValueInfo TheFnVI)38309a704c5SMingming Liu bool mustBeUnreachableFunction(ValueInfo TheFnVI) {
38409a704c5SMingming Liu   if ((!TheFnVI) || TheFnVI.getSummaryList().empty()) {
38509a704c5SMingming Liu     // Returns false if ValueInfo is absent, or the summary list is empty
38609a704c5SMingming Liu     // (e.g., function declarations).
38709a704c5SMingming Liu     return false;
38809a704c5SMingming Liu   }
38909a704c5SMingming Liu 
39009a704c5SMingming Liu   for (auto &Summary : TheFnVI.getSummaryList()) {
39109a704c5SMingming Liu     // Conservatively returns false if any non-live functions are seen.
39209a704c5SMingming Liu     // In general either all summaries should be live or all should be dead.
39309a704c5SMingming Liu     if (!Summary->isLive())
39409a704c5SMingming Liu       return false;
39509a704c5SMingming Liu     if (auto *FS = dyn_cast<FunctionSummary>(Summary.get())) {
39609a704c5SMingming Liu       if (!FS->fflags().MustBeUnreachable)
39709a704c5SMingming Liu         return false;
39809a704c5SMingming Liu     }
39909a704c5SMingming Liu     // Do nothing if a non-function has the same GUID (which is rare).
40009a704c5SMingming Liu     // This is correct since non-function summaries are not relevant.
40109a704c5SMingming Liu   }
40209a704c5SMingming Liu   // All function summaries are live and all of them agree that the function is
40309a704c5SMingming Liu   // unreachble.
40409a704c5SMingming Liu   return true;
40509a704c5SMingming Liu }
40609a704c5SMingming Liu 
407df49d1bbSPeter Collingbourne // A virtual call site. VTable is the loaded virtual table pointer, and CS is
408df49d1bbSPeter Collingbourne // the indirect virtual call.
409df49d1bbSPeter Collingbourne struct VirtualCallSite {
410cea6f4d5SMircea Trofin   Value *VTable = nullptr;
411cea6f4d5SMircea Trofin   CallBase &CB;
412df49d1bbSPeter Collingbourne 
4130312f614SPeter Collingbourne   // If non-null, this field points to the associated unsafe use count stored in
4140312f614SPeter Collingbourne   // the DevirtModule::NumUnsafeUsesForTypeTest map below. See the description
4150312f614SPeter Collingbourne   // of that field for details.
416cea6f4d5SMircea Trofin   unsigned *NumUnsafeUses = nullptr;
4170312f614SPeter Collingbourne 
418e963c89dSSam Elliott   void
emitRemark__anona5c3867a0311::VirtualCallSite419e963c89dSSam Elliott   emitRemark(const StringRef OptName, const StringRef TargetName,
420e963c89dSSam Elliott              function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter) {
421cea6f4d5SMircea Trofin     Function *F = CB.getCaller();
422cea6f4d5SMircea Trofin     DebugLoc DLoc = CB.getDebugLoc();
423cea6f4d5SMircea Trofin     BasicBlock *Block = CB.getParent();
424e963c89dSSam Elliott 
425e963c89dSSam Elliott     using namespace ore;
4269110cb45SPeter Collingbourne     OREGetter(F).emit(OptimizationRemark(DEBUG_TYPE, OptName, DLoc, Block)
4279110cb45SPeter Collingbourne                       << NV("Optimization", OptName)
4289110cb45SPeter Collingbourne                       << ": devirtualized a call to "
429e963c89dSSam Elliott                       << NV("FunctionName", TargetName));
430e963c89dSSam Elliott   }
431e963c89dSSam Elliott 
replaceAndErase__anona5c3867a0311::VirtualCallSite432e963c89dSSam Elliott   void replaceAndErase(
433e963c89dSSam Elliott       const StringRef OptName, const StringRef TargetName, bool RemarksEnabled,
434e963c89dSSam Elliott       function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter,
435e963c89dSSam Elliott       Value *New) {
436f3403fd2SIvan Krasin     if (RemarksEnabled)
437e963c89dSSam Elliott       emitRemark(OptName, TargetName, OREGetter);
438cea6f4d5SMircea Trofin     CB.replaceAllUsesWith(New);
439cea6f4d5SMircea Trofin     if (auto *II = dyn_cast<InvokeInst>(&CB)) {
440cea6f4d5SMircea Trofin       BranchInst::Create(II->getNormalDest(), &CB);
441df49d1bbSPeter Collingbourne       II->getUnwindDest()->removePredecessor(II->getParent());
442df49d1bbSPeter Collingbourne     }
443cea6f4d5SMircea Trofin     CB.eraseFromParent();
4440312f614SPeter Collingbourne     // This use is no longer unsafe.
4450312f614SPeter Collingbourne     if (NumUnsafeUses)
4460312f614SPeter Collingbourne       --*NumUnsafeUses;
447df49d1bbSPeter Collingbourne   }
448df49d1bbSPeter Collingbourne };
449df49d1bbSPeter Collingbourne 
45050cbd7ccSPeter Collingbourne // Call site information collected for a specific VTableSlot and possibly a list
45150cbd7ccSPeter Collingbourne // of constant integer arguments. The grouping by arguments is handled by the
45250cbd7ccSPeter Collingbourne // VTableSlotInfo class.
45350cbd7ccSPeter Collingbourne struct CallSiteInfo {
454b406baaeSPeter Collingbourne   /// The set of call sites for this slot. Used during regular LTO and the
455b406baaeSPeter Collingbourne   /// import phase of ThinLTO (as well as the export phase of ThinLTO for any
456b406baaeSPeter Collingbourne   /// call sites that appear in the merged module itself); in each of these
457b406baaeSPeter Collingbourne   /// cases we are directly operating on the call sites at the IR level.
45850cbd7ccSPeter Collingbourne   std::vector<VirtualCallSite> CallSites;
459b406baaeSPeter Collingbourne 
4602974856aSPeter Collingbourne   /// Whether all call sites represented by this CallSiteInfo, including those
4612974856aSPeter Collingbourne   /// in summaries, have been devirtualized. This starts off as true because a
4622974856aSPeter Collingbourne   /// default constructed CallSiteInfo represents no call sites.
4632974856aSPeter Collingbourne   bool AllCallSitesDevirted = true;
4642974856aSPeter Collingbourne 
465b406baaeSPeter Collingbourne   // These fields are used during the export phase of ThinLTO and reflect
466b406baaeSPeter Collingbourne   // information collected from function summaries.
467b406baaeSPeter Collingbourne 
4682325bb34SPeter Collingbourne   /// Whether any function summary contains an llvm.assume(llvm.type.test) for
4692325bb34SPeter Collingbourne   /// this slot.
4702974856aSPeter Collingbourne   bool SummaryHasTypeTestAssumeUsers = false;
4712325bb34SPeter Collingbourne 
472b406baaeSPeter Collingbourne   /// CFI-specific: a vector containing the list of function summaries that use
473b406baaeSPeter Collingbourne   /// the llvm.type.checked.load intrinsic and therefore will require
474b406baaeSPeter Collingbourne   /// resolutions for llvm.type.test in order to implement CFI checks if
475b406baaeSPeter Collingbourne   /// devirtualization was unsuccessful. If devirtualization was successful, the
47659675ba0SPeter Collingbourne   /// pass will clear this vector by calling markDevirt(). If at the end of the
47759675ba0SPeter Collingbourne   /// pass the vector is non-empty, we will need to add a use of llvm.type.test
47859675ba0SPeter Collingbourne   /// to each of the function summaries in the vector.
479b406baaeSPeter Collingbourne   std::vector<FunctionSummary *> SummaryTypeCheckedLoadUsers;
480d2df54e6STeresa Johnson   std::vector<FunctionSummary *> SummaryTypeTestAssumeUsers;
4812325bb34SPeter Collingbourne 
isExported__anona5c3867a0311::CallSiteInfo4822325bb34SPeter Collingbourne   bool isExported() const {
4832325bb34SPeter Collingbourne     return SummaryHasTypeTestAssumeUsers ||
4842325bb34SPeter Collingbourne            !SummaryTypeCheckedLoadUsers.empty();
4852325bb34SPeter Collingbourne   }
48659675ba0SPeter Collingbourne 
addSummaryTypeCheckedLoadUser__anona5c3867a0311::CallSiteInfo4872974856aSPeter Collingbourne   void addSummaryTypeCheckedLoadUser(FunctionSummary *FS) {
4882974856aSPeter Collingbourne     SummaryTypeCheckedLoadUsers.push_back(FS);
4892974856aSPeter Collingbourne     AllCallSitesDevirted = false;
4902974856aSPeter Collingbourne   }
4912974856aSPeter Collingbourne 
addSummaryTypeTestAssumeUser__anona5c3867a0311::CallSiteInfo492d2df54e6STeresa Johnson   void addSummaryTypeTestAssumeUser(FunctionSummary *FS) {
493d2df54e6STeresa Johnson     SummaryTypeTestAssumeUsers.push_back(FS);
494943afb57SEugene Leviant     SummaryHasTypeTestAssumeUsers = true;
495943afb57SEugene Leviant     AllCallSitesDevirted = false;
496d2df54e6STeresa Johnson   }
497d2df54e6STeresa Johnson 
markDevirt__anona5c3867a0311::CallSiteInfo4982974856aSPeter Collingbourne   void markDevirt() {
4992974856aSPeter Collingbourne     AllCallSitesDevirted = true;
5002974856aSPeter Collingbourne 
5012974856aSPeter Collingbourne     // As explained in the comment for SummaryTypeCheckedLoadUsers.
5022974856aSPeter Collingbourne     SummaryTypeCheckedLoadUsers.clear();
5032974856aSPeter Collingbourne   }
50450cbd7ccSPeter Collingbourne };
50550cbd7ccSPeter Collingbourne 
50650cbd7ccSPeter Collingbourne // Call site information collected for a specific VTableSlot.
50750cbd7ccSPeter Collingbourne struct VTableSlotInfo {
50850cbd7ccSPeter Collingbourne   // The set of call sites which do not have all constant integer arguments
50950cbd7ccSPeter Collingbourne   // (excluding "this").
51050cbd7ccSPeter Collingbourne   CallSiteInfo CSInfo;
51150cbd7ccSPeter Collingbourne 
51250cbd7ccSPeter Collingbourne   // The set of call sites with all constant integer arguments (excluding
51350cbd7ccSPeter Collingbourne   // "this"), grouped by argument list.
51450cbd7ccSPeter Collingbourne   std::map<std::vector<uint64_t>, CallSiteInfo> ConstCSInfo;
51550cbd7ccSPeter Collingbourne 
516cea6f4d5SMircea Trofin   void addCallSite(Value *VTable, CallBase &CB, unsigned *NumUnsafeUses);
51750cbd7ccSPeter Collingbourne 
51850cbd7ccSPeter Collingbourne private:
519cea6f4d5SMircea Trofin   CallSiteInfo &findCallSiteInfo(CallBase &CB);
52050cbd7ccSPeter Collingbourne };
52150cbd7ccSPeter Collingbourne 
findCallSiteInfo(CallBase & CB)522cea6f4d5SMircea Trofin CallSiteInfo &VTableSlotInfo::findCallSiteInfo(CallBase &CB) {
52350cbd7ccSPeter Collingbourne   std::vector<uint64_t> Args;
524cea6f4d5SMircea Trofin   auto *CBType = dyn_cast<IntegerType>(CB.getType());
525cea6f4d5SMircea Trofin   if (!CBType || CBType->getBitWidth() > 64 || CB.arg_empty())
52650cbd7ccSPeter Collingbourne     return CSInfo;
52723b0ab2aSKazu Hirata   for (auto &&Arg : drop_begin(CB.args())) {
52850cbd7ccSPeter Collingbourne     auto *CI = dyn_cast<ConstantInt>(Arg);
52950cbd7ccSPeter Collingbourne     if (!CI || CI->getBitWidth() > 64)
53050cbd7ccSPeter Collingbourne       return CSInfo;
53150cbd7ccSPeter Collingbourne     Args.push_back(CI->getZExtValue());
53250cbd7ccSPeter Collingbourne   }
53350cbd7ccSPeter Collingbourne   return ConstCSInfo[Args];
53450cbd7ccSPeter Collingbourne }
53550cbd7ccSPeter Collingbourne 
addCallSite(Value * VTable,CallBase & CB,unsigned * NumUnsafeUses)536cea6f4d5SMircea Trofin void VTableSlotInfo::addCallSite(Value *VTable, CallBase &CB,
53750cbd7ccSPeter Collingbourne                                  unsigned *NumUnsafeUses) {
538cea6f4d5SMircea Trofin   auto &CSI = findCallSiteInfo(CB);
5392974856aSPeter Collingbourne   CSI.AllCallSitesDevirted = false;
540cea6f4d5SMircea Trofin   CSI.CallSites.push_back({VTable, CB, NumUnsafeUses});
54150cbd7ccSPeter Collingbourne }
54250cbd7ccSPeter Collingbourne 
543df49d1bbSPeter Collingbourne struct DevirtModule {
544df49d1bbSPeter Collingbourne   Module &M;
54537317f12SPeter Collingbourne   function_ref<AAResults &(Function &)> AARGetter;
546f24136f1STeresa Johnson   function_ref<DominatorTree &(Function &)> LookupDomTree;
5472b33f653SPeter Collingbourne 
548f7691d8bSPeter Collingbourne   ModuleSummaryIndex *ExportSummary;
549f7691d8bSPeter Collingbourne   const ModuleSummaryIndex *ImportSummary;
5502b33f653SPeter Collingbourne 
551df49d1bbSPeter Collingbourne   IntegerType *Int8Ty;
552df49d1bbSPeter Collingbourne   PointerType *Int8PtrTy;
553df49d1bbSPeter Collingbourne   IntegerType *Int32Ty;
55450cbd7ccSPeter Collingbourne   IntegerType *Int64Ty;
55514dcf02fSPeter Collingbourne   IntegerType *IntPtrTy;
556cc5c5888SBob Haarman   /// Sizeless array type, used for imported vtables. This provides a signal
557cc5c5888SBob Haarman   /// to analyzers that these imports may alias, as they do for example
558cc5c5888SBob Haarman   /// when multiple unique return values occur in the same vtable.
559cc5c5888SBob Haarman   ArrayType *Int8Arr0Ty;
560df49d1bbSPeter Collingbourne 
561f3403fd2SIvan Krasin   bool RemarksEnabled;
562e963c89dSSam Elliott   function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter;
563f3403fd2SIvan Krasin 
56450cbd7ccSPeter Collingbourne   MapVector<VTableSlot, VTableSlotInfo> CallSlots;
565df49d1bbSPeter Collingbourne 
5667110510eSArthur Eubanks   // Calls that have already been optimized. We may add a call to multiple
5677110510eSArthur Eubanks   // VTableSlotInfos if vtable loads are coalesced and need to make sure not to
5687110510eSArthur Eubanks   // optimize a call more than once.
5697110510eSArthur Eubanks   SmallPtrSet<CallBase *, 8> OptimizedCalls;
5707110510eSArthur Eubanks 
5710312f614SPeter Collingbourne   // This map keeps track of the number of "unsafe" uses of a loaded function
5720312f614SPeter Collingbourne   // pointer. The key is the associated llvm.type.test intrinsic call generated
5730312f614SPeter Collingbourne   // by this pass. An unsafe use is one that calls the loaded function pointer
5740312f614SPeter Collingbourne   // directly. Every time we eliminate an unsafe use (for example, by
5750312f614SPeter Collingbourne   // devirtualizing it or by applying virtual constant propagation), we
5760312f614SPeter Collingbourne   // decrement the value stored in this map. If a value reaches zero, we can
5770312f614SPeter Collingbourne   // eliminate the type check by RAUWing the associated llvm.type.test call with
5780312f614SPeter Collingbourne   // true.
5790312f614SPeter Collingbourne   std::map<CallInst *, unsigned> NumUnsafeUsesForTypeTest;
5806d2032e2Sevgeny   PatternList FunctionsToSkip;
5810312f614SPeter Collingbourne 
DevirtModule__anona5c3867a0311::DevirtModule58237317f12SPeter Collingbourne   DevirtModule(Module &M, function_ref<AAResults &(Function &)> AARGetter,
583e963c89dSSam Elliott                function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter,
584f24136f1STeresa Johnson                function_ref<DominatorTree &(Function &)> LookupDomTree,
585f7691d8bSPeter Collingbourne                ModuleSummaryIndex *ExportSummary,
586f7691d8bSPeter Collingbourne                const ModuleSummaryIndex *ImportSummary)
587f24136f1STeresa Johnson       : M(M), AARGetter(AARGetter), LookupDomTree(LookupDomTree),
588f24136f1STeresa Johnson         ExportSummary(ExportSummary), ImportSummary(ImportSummary),
589f24136f1STeresa Johnson         Int8Ty(Type::getInt8Ty(M.getContext())),
590df49d1bbSPeter Collingbourne         Int8PtrTy(Type::getInt8PtrTy(M.getContext())),
591f3403fd2SIvan Krasin         Int32Ty(Type::getInt32Ty(M.getContext())),
59250cbd7ccSPeter Collingbourne         Int64Ty(Type::getInt64Ty(M.getContext())),
59314dcf02fSPeter Collingbourne         IntPtrTy(M.getDataLayout().getIntPtrType(M.getContext(), 0)),
594cc5c5888SBob Haarman         Int8Arr0Ty(ArrayType::get(Type::getInt8Ty(M.getContext()), 0)),
595e963c89dSSam Elliott         RemarksEnabled(areRemarksEnabled()), OREGetter(OREGetter) {
596f7691d8bSPeter Collingbourne     assert(!(ExportSummary && ImportSummary));
5976d2032e2Sevgeny     FunctionsToSkip.init(SkipFunctionNames);
598f7691d8bSPeter Collingbourne   }
599f3403fd2SIvan Krasin 
600f3403fd2SIvan Krasin   bool areRemarksEnabled();
601df49d1bbSPeter Collingbourne 
6026014c46cSTeresa Johnson   void
6036014c46cSTeresa Johnson   scanTypeTestUsers(Function *TypeTestFunc,
6046014c46cSTeresa Johnson                     DenseMap<Metadata *, std::set<TypeMemberInfo>> &TypeIdMap);
6050312f614SPeter Collingbourne   void scanTypeCheckedLoadUsers(Function *TypeCheckedLoadFunc);
6060312f614SPeter Collingbourne 
6077efd7506SPeter Collingbourne   void buildTypeIdentifierMap(
6087efd7506SPeter Collingbourne       std::vector<VTableBits> &Bits,
6097efd7506SPeter Collingbourne       DenseMap<Metadata *, std::set<TypeMemberInfo>> &TypeIdMap);
61009a704c5SMingming Liu 
6117efd7506SPeter Collingbourne   bool
6127efd7506SPeter Collingbourne   tryFindVirtualCallTargets(std::vector<VirtualCallTarget> &TargetsForSlot,
6137efd7506SPeter Collingbourne                             const std::set<TypeMemberInfo> &TypeMemberInfos,
61409a704c5SMingming Liu                             uint64_t ByteOffset,
61509a704c5SMingming Liu                             ModuleSummaryIndex *ExportSummary);
61650cbd7ccSPeter Collingbourne 
6172325bb34SPeter Collingbourne   void applySingleImplDevirt(VTableSlotInfo &SlotInfo, Constant *TheFn,
6182325bb34SPeter Collingbourne                              bool &IsExported);
619943afb57SEugene Leviant   bool trySingleImplDevirt(ModuleSummaryIndex *ExportSummary,
620943afb57SEugene Leviant                            MutableArrayRef<VirtualCallTarget> TargetsForSlot,
6212325bb34SPeter Collingbourne                            VTableSlotInfo &SlotInfo,
6222325bb34SPeter Collingbourne                            WholeProgramDevirtResolution *Res);
62350cbd7ccSPeter Collingbourne 
6242974856aSPeter Collingbourne   void applyICallBranchFunnel(VTableSlotInfo &SlotInfo, Constant *JT,
6252974856aSPeter Collingbourne                               bool &IsExported);
6262974856aSPeter Collingbourne   void tryICallBranchFunnel(MutableArrayRef<VirtualCallTarget> TargetsForSlot,
6272974856aSPeter Collingbourne                             VTableSlotInfo &SlotInfo,
6282974856aSPeter Collingbourne                             WholeProgramDevirtResolution *Res, VTableSlot Slot);
6292974856aSPeter Collingbourne 
630df49d1bbSPeter Collingbourne   bool tryEvaluateFunctionsWithArgs(
631df49d1bbSPeter Collingbourne       MutableArrayRef<VirtualCallTarget> TargetsForSlot,
63250cbd7ccSPeter Collingbourne       ArrayRef<uint64_t> Args);
63350cbd7ccSPeter Collingbourne 
63450cbd7ccSPeter Collingbourne   void applyUniformRetValOpt(CallSiteInfo &CSInfo, StringRef FnName,
63550cbd7ccSPeter Collingbourne                              uint64_t TheRetVal);
63650cbd7ccSPeter Collingbourne   bool tryUniformRetValOpt(MutableArrayRef<VirtualCallTarget> TargetsForSlot,
63777a8d563SPeter Collingbourne                            CallSiteInfo &CSInfo,
63877a8d563SPeter Collingbourne                            WholeProgramDevirtResolution::ByArg *Res);
63950cbd7ccSPeter Collingbourne 
64059675ba0SPeter Collingbourne   // Returns the global symbol name that is used to export information about the
64159675ba0SPeter Collingbourne   // given vtable slot and list of arguments.
64259675ba0SPeter Collingbourne   std::string getGlobalName(VTableSlot Slot, ArrayRef<uint64_t> Args,
64359675ba0SPeter Collingbourne                             StringRef Name);
64459675ba0SPeter Collingbourne 
645b15a35e6SPeter Collingbourne   bool shouldExportConstantsAsAbsoluteSymbols();
646b15a35e6SPeter Collingbourne 
64759675ba0SPeter Collingbourne   // This function is called during the export phase to create a symbol
64859675ba0SPeter Collingbourne   // definition containing information about the given vtable slot and list of
64959675ba0SPeter Collingbourne   // arguments.
65059675ba0SPeter Collingbourne   void exportGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args, StringRef Name,
65159675ba0SPeter Collingbourne                     Constant *C);
652b15a35e6SPeter Collingbourne   void exportConstant(VTableSlot Slot, ArrayRef<uint64_t> Args, StringRef Name,
653b15a35e6SPeter Collingbourne                       uint32_t Const, uint32_t &Storage);
65459675ba0SPeter Collingbourne 
65559675ba0SPeter Collingbourne   // This function is called during the import phase to create a reference to
65659675ba0SPeter Collingbourne   // the symbol definition created during the export phase.
65759675ba0SPeter Collingbourne   Constant *importGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args,
658b15a35e6SPeter Collingbourne                          StringRef Name);
659b15a35e6SPeter Collingbourne   Constant *importConstant(VTableSlot Slot, ArrayRef<uint64_t> Args,
660b15a35e6SPeter Collingbourne                            StringRef Name, IntegerType *IntTy,
661b15a35e6SPeter Collingbourne                            uint32_t Storage);
66259675ba0SPeter Collingbourne 
6632974856aSPeter Collingbourne   Constant *getMemberAddr(const TypeMemberInfo *M);
6642974856aSPeter Collingbourne 
66550cbd7ccSPeter Collingbourne   void applyUniqueRetValOpt(CallSiteInfo &CSInfo, StringRef FnName, bool IsOne,
66650cbd7ccSPeter Collingbourne                             Constant *UniqueMemberAddr);
667df49d1bbSPeter Collingbourne   bool tryUniqueRetValOpt(unsigned BitWidth,
668f3403fd2SIvan Krasin                           MutableArrayRef<VirtualCallTarget> TargetsForSlot,
66959675ba0SPeter Collingbourne                           CallSiteInfo &CSInfo,
67059675ba0SPeter Collingbourne                           WholeProgramDevirtResolution::ByArg *Res,
67159675ba0SPeter Collingbourne                           VTableSlot Slot, ArrayRef<uint64_t> Args);
67250cbd7ccSPeter Collingbourne 
67350cbd7ccSPeter Collingbourne   void applyVirtualConstProp(CallSiteInfo &CSInfo, StringRef FnName,
67450cbd7ccSPeter Collingbourne                              Constant *Byte, Constant *Bit);
675df49d1bbSPeter Collingbourne   bool tryVirtualConstProp(MutableArrayRef<VirtualCallTarget> TargetsForSlot,
67677a8d563SPeter Collingbourne                            VTableSlotInfo &SlotInfo,
67759675ba0SPeter Collingbourne                            WholeProgramDevirtResolution *Res, VTableSlot Slot);
678df49d1bbSPeter Collingbourne 
679df49d1bbSPeter Collingbourne   void rebuildGlobal(VTableBits &B);
680df49d1bbSPeter Collingbourne 
6816d284fabSPeter Collingbourne   // Apply the summary resolution for Slot to all virtual calls in SlotInfo.
6826d284fabSPeter Collingbourne   void importResolution(VTableSlot Slot, VTableSlotInfo &SlotInfo);
6836d284fabSPeter Collingbourne 
6846d284fabSPeter Collingbourne   // If we were able to eliminate all unsafe uses for a type checked load,
6856d284fabSPeter Collingbourne   // eliminate the associated type tests by replacing them with true.
6866d284fabSPeter Collingbourne   void removeRedundantTypeTests();
6876d284fabSPeter Collingbourne 
688df49d1bbSPeter Collingbourne   bool run();
6892b33f653SPeter Collingbourne 
69009a704c5SMingming Liu   // Look up the corresponding ValueInfo entry of `TheFn` in `ExportSummary`.
69109a704c5SMingming Liu   //
69209a704c5SMingming Liu   // Caller guarantees that `ExportSummary` is not nullptr.
69309a704c5SMingming Liu   static ValueInfo lookUpFunctionValueInfo(Function *TheFn,
69409a704c5SMingming Liu                                            ModuleSummaryIndex *ExportSummary);
69509a704c5SMingming Liu 
6969c49f8d7Sminglotus-6   // Returns true if the function definition must be unreachable.
6979c49f8d7Sminglotus-6   //
6989c49f8d7Sminglotus-6   // Note if this helper function returns true, `F` is guaranteed
6999c49f8d7Sminglotus-6   // to be unreachable; if it returns false, `F` might still
7009c49f8d7Sminglotus-6   // be unreachable but not covered by this helper function.
7019c49f8d7Sminglotus-6   //
7029c49f8d7Sminglotus-6   // Implementation-wise, if function definition is present, IR is analyzed; if
7039c49f8d7Sminglotus-6   // not, look up function flags from ExportSummary as a fallback.
7049c49f8d7Sminglotus-6   static bool mustBeUnreachableFunction(Function *const F,
7059c49f8d7Sminglotus-6                                         ModuleSummaryIndex *ExportSummary);
7069c49f8d7Sminglotus-6 
7072b33f653SPeter Collingbourne   // Lower the module using the action and summary passed as command line
7082b33f653SPeter Collingbourne   // arguments. For testing purposes only.
709f24136f1STeresa Johnson   static bool
710f24136f1STeresa Johnson   runForTesting(Module &M, function_ref<AAResults &(Function &)> AARGetter,
711f24136f1STeresa Johnson                 function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter,
712f24136f1STeresa Johnson                 function_ref<DominatorTree &(Function &)> LookupDomTree);
713df49d1bbSPeter Collingbourne };
714df49d1bbSPeter Collingbourne 
715d2df54e6STeresa Johnson struct DevirtIndex {
716d2df54e6STeresa Johnson   ModuleSummaryIndex &ExportSummary;
717d2df54e6STeresa Johnson   // The set in which to record GUIDs exported from their module by
718d2df54e6STeresa Johnson   // devirtualization, used by client to ensure they are not internalized.
719d2df54e6STeresa Johnson   std::set<GlobalValue::GUID> &ExportedGUIDs;
720d2df54e6STeresa Johnson   // A map in which to record the information necessary to locate the WPD
721d2df54e6STeresa Johnson   // resolution for local targets in case they are exported by cross module
722d2df54e6STeresa Johnson   // importing.
723d2df54e6STeresa Johnson   std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap;
724d2df54e6STeresa Johnson 
725d2df54e6STeresa Johnson   MapVector<VTableSlotSummary, VTableSlotInfo> CallSlots;
726d2df54e6STeresa Johnson 
7276d2032e2Sevgeny   PatternList FunctionsToSkip;
7286d2032e2Sevgeny 
DevirtIndex__anona5c3867a0311::DevirtIndex729d2df54e6STeresa Johnson   DevirtIndex(
730d2df54e6STeresa Johnson       ModuleSummaryIndex &ExportSummary,
731d2df54e6STeresa Johnson       std::set<GlobalValue::GUID> &ExportedGUIDs,
732d2df54e6STeresa Johnson       std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap)
733d2df54e6STeresa Johnson       : ExportSummary(ExportSummary), ExportedGUIDs(ExportedGUIDs),
7346d2032e2Sevgeny         LocalWPDTargetsMap(LocalWPDTargetsMap) {
7356d2032e2Sevgeny     FunctionsToSkip.init(SkipFunctionNames);
7366d2032e2Sevgeny   }
737d2df54e6STeresa Johnson 
738d2df54e6STeresa Johnson   bool tryFindVirtualCallTargets(std::vector<ValueInfo> &TargetsForSlot,
739d2df54e6STeresa Johnson                                  const TypeIdCompatibleVtableInfo TIdInfo,
740d2df54e6STeresa Johnson                                  uint64_t ByteOffset);
741d2df54e6STeresa Johnson 
742d2df54e6STeresa Johnson   bool trySingleImplDevirt(MutableArrayRef<ValueInfo> TargetsForSlot,
743d2df54e6STeresa Johnson                            VTableSlotSummary &SlotSummary,
744d2df54e6STeresa Johnson                            VTableSlotInfo &SlotInfo,
745d2df54e6STeresa Johnson                            WholeProgramDevirtResolution *Res,
746d2df54e6STeresa Johnson                            std::set<ValueInfo> &DevirtTargets);
747d2df54e6STeresa Johnson 
748d2df54e6STeresa Johnson   void run();
749d2df54e6STeresa Johnson };
750cdc71612SEugene Zelenko } // end anonymous namespace
751df49d1bbSPeter Collingbourne 
run(Module & M,ModuleAnalysisManager & AM)752164a2aa6SChandler Carruth PreservedAnalyses WholeProgramDevirtPass::run(Module &M,
75337317f12SPeter Collingbourne                                               ModuleAnalysisManager &AM) {
75437317f12SPeter Collingbourne   auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
75537317f12SPeter Collingbourne   auto AARGetter = [&](Function &F) -> AAResults & {
75637317f12SPeter Collingbourne     return FAM.getResult<AAManager>(F);
75737317f12SPeter Collingbourne   };
758e963c89dSSam Elliott   auto OREGetter = [&](Function *F) -> OptimizationRemarkEmitter & {
759e963c89dSSam Elliott     return FAM.getResult<OptimizationRemarkEmitterAnalysis>(*F);
760e963c89dSSam Elliott   };
761f24136f1STeresa Johnson   auto LookupDomTree = [&FAM](Function &F) -> DominatorTree & {
762f24136f1STeresa Johnson     return FAM.getResult<DominatorTreeAnalysis>(F);
763f24136f1STeresa Johnson   };
764460dda07SArthur Eubanks   if (UseCommandLine) {
765460dda07SArthur Eubanks     if (DevirtModule::runForTesting(M, AARGetter, OREGetter, LookupDomTree))
766460dda07SArthur Eubanks       return PreservedAnalyses::all();
767460dda07SArthur Eubanks     return PreservedAnalyses::none();
768460dda07SArthur Eubanks   }
769f24136f1STeresa Johnson   if (!DevirtModule(M, AARGetter, OREGetter, LookupDomTree, ExportSummary,
770f24136f1STeresa Johnson                     ImportSummary)
77128023dbeSTeresa Johnson            .run())
772d737dd2eSDavide Italiano     return PreservedAnalyses::all();
773d737dd2eSDavide Italiano   return PreservedAnalyses::none();
774d737dd2eSDavide Italiano }
775d737dd2eSDavide Italiano 
776*2eade1dbSArthur Eubanks namespace llvm {
7772f63d549STeresa Johnson // Enable whole program visibility if enabled by client (e.g. linker) or
7782f63d549STeresa Johnson // internal option, and not force disabled.
hasWholeProgramVisibility(bool WholeProgramVisibilityEnabledInLTO)779*2eade1dbSArthur Eubanks bool hasWholeProgramVisibility(bool WholeProgramVisibilityEnabledInLTO) {
7802f63d549STeresa Johnson   return (WholeProgramVisibilityEnabledInLTO || WholeProgramVisibility) &&
7812f63d549STeresa Johnson          !DisableWholeProgramVisibility;
7822f63d549STeresa Johnson }
7832f63d549STeresa Johnson 
7842f63d549STeresa Johnson /// If whole program visibility asserted, then upgrade all public vcall
7852f63d549STeresa Johnson /// visibility metadata on vtable definitions to linkage unit visibility in
7862f63d549STeresa Johnson /// Module IR (for regular or hybrid LTO).
updateVCallVisibilityInModule(Module & M,bool WholeProgramVisibilityEnabledInLTO,const DenseSet<GlobalValue::GUID> & DynamicExportSymbols)7871487747eSTeresa Johnson void updateVCallVisibilityInModule(
7881487747eSTeresa Johnson     Module &M, bool WholeProgramVisibilityEnabledInLTO,
7891487747eSTeresa Johnson     const DenseSet<GlobalValue::GUID> &DynamicExportSymbols) {
7902f63d549STeresa Johnson   if (!hasWholeProgramVisibility(WholeProgramVisibilityEnabledInLTO))
7912f63d549STeresa Johnson     return;
792*2eade1dbSArthur Eubanks   for (GlobalVariable &GV : M.globals()) {
7932f63d549STeresa Johnson     // Add linkage unit visibility to any variable with type metadata, which are
7942f63d549STeresa Johnson     // the vtable definitions. We won't have an existing vcall_visibility
7952f63d549STeresa Johnson     // metadata on vtable definitions with public visibility.
7962f63d549STeresa Johnson     if (GV.hasMetadata(LLVMContext::MD_type) &&
7971487747eSTeresa Johnson         GV.getVCallVisibility() == GlobalObject::VCallVisibilityPublic &&
7981487747eSTeresa Johnson         // Don't upgrade the visibility for symbols exported to the dynamic
7991487747eSTeresa Johnson         // linker, as we have no information on their eventual use.
8001487747eSTeresa Johnson         !DynamicExportSymbols.count(GV.getGUID()))
8012f63d549STeresa Johnson       GV.setVCallVisibilityMetadata(GlobalObject::VCallVisibilityLinkageUnit);
8022f63d549STeresa Johnson   }
803*2eade1dbSArthur Eubanks }
804*2eade1dbSArthur Eubanks 
updatePublicTypeTestCalls(Module & M,bool WholeProgramVisibilityEnabledInLTO)805*2eade1dbSArthur Eubanks void updatePublicTypeTestCalls(Module &M,
806*2eade1dbSArthur Eubanks                                bool WholeProgramVisibilityEnabledInLTO) {
807*2eade1dbSArthur Eubanks   Function *PublicTypeTestFunc =
808*2eade1dbSArthur Eubanks       M.getFunction(Intrinsic::getName(Intrinsic::public_type_test));
809*2eade1dbSArthur Eubanks   if (!PublicTypeTestFunc)
810*2eade1dbSArthur Eubanks     return;
811*2eade1dbSArthur Eubanks   if (hasWholeProgramVisibility(WholeProgramVisibilityEnabledInLTO)) {
812*2eade1dbSArthur Eubanks     Function *TypeTestFunc =
813*2eade1dbSArthur Eubanks         Intrinsic::getDeclaration(&M, Intrinsic::type_test);
814*2eade1dbSArthur Eubanks     for (Use &U : make_early_inc_range(PublicTypeTestFunc->uses())) {
815*2eade1dbSArthur Eubanks       auto *CI = cast<CallInst>(U.getUser());
816*2eade1dbSArthur Eubanks       auto *NewCI = CallInst::Create(
817*2eade1dbSArthur Eubanks           TypeTestFunc, {CI->getArgOperand(0), CI->getArgOperand(1)}, None, "",
818*2eade1dbSArthur Eubanks           CI);
819*2eade1dbSArthur Eubanks       CI->replaceAllUsesWith(NewCI);
820*2eade1dbSArthur Eubanks       CI->eraseFromParent();
821*2eade1dbSArthur Eubanks     }
822*2eade1dbSArthur Eubanks   } else {
823*2eade1dbSArthur Eubanks     auto *True = ConstantInt::getTrue(M.getContext());
824*2eade1dbSArthur Eubanks     for (Use &U : make_early_inc_range(PublicTypeTestFunc->uses())) {
825*2eade1dbSArthur Eubanks       auto *CI = cast<CallInst>(U.getUser());
826*2eade1dbSArthur Eubanks       CI->replaceAllUsesWith(True);
827*2eade1dbSArthur Eubanks       CI->eraseFromParent();
828*2eade1dbSArthur Eubanks     }
829*2eade1dbSArthur Eubanks   }
830*2eade1dbSArthur Eubanks }
8312f63d549STeresa Johnson 
8322f63d549STeresa Johnson /// If whole program visibility asserted, then upgrade all public vcall
8332f63d549STeresa Johnson /// visibility metadata on vtable definition summaries to linkage unit
8342f63d549STeresa Johnson /// visibility in Module summary index (for ThinLTO).
updateVCallVisibilityInIndex(ModuleSummaryIndex & Index,bool WholeProgramVisibilityEnabledInLTO,const DenseSet<GlobalValue::GUID> & DynamicExportSymbols)8351487747eSTeresa Johnson void updateVCallVisibilityInIndex(
8361487747eSTeresa Johnson     ModuleSummaryIndex &Index, bool WholeProgramVisibilityEnabledInLTO,
8371487747eSTeresa Johnson     const DenseSet<GlobalValue::GUID> &DynamicExportSymbols) {
8382f63d549STeresa Johnson   if (!hasWholeProgramVisibility(WholeProgramVisibilityEnabledInLTO))
8392f63d549STeresa Johnson     return;
8402f63d549STeresa Johnson   for (auto &P : Index) {
841dd3f4833STeresa Johnson     // Don't upgrade the visibility for symbols exported to the dynamic
842dd3f4833STeresa Johnson     // linker, as we have no information on their eventual use.
843dd3f4833STeresa Johnson     if (DynamicExportSymbols.count(P.first))
844dd3f4833STeresa Johnson       continue;
8452f63d549STeresa Johnson     for (auto &S : P.second.SummaryList) {
8462f63d549STeresa Johnson       auto *GVar = dyn_cast<GlobalVarSummary>(S.get());
8470a5949dcSTeresa Johnson       if (!GVar ||
848dd3f4833STeresa Johnson           GVar->getVCallVisibility() != GlobalObject::VCallVisibilityPublic)
8492f63d549STeresa Johnson         continue;
8502f63d549STeresa Johnson       GVar->setVCallVisibility(GlobalObject::VCallVisibilityLinkageUnit);
8512f63d549STeresa Johnson     }
8522f63d549STeresa Johnson   }
8532f63d549STeresa Johnson }
8542f63d549STeresa Johnson 
runWholeProgramDevirtOnIndex(ModuleSummaryIndex & Summary,std::set<GlobalValue::GUID> & ExportedGUIDs,std::map<ValueInfo,std::vector<VTableSlotSummary>> & LocalWPDTargetsMap)855d2df54e6STeresa Johnson void runWholeProgramDevirtOnIndex(
856d2df54e6STeresa Johnson     ModuleSummaryIndex &Summary, std::set<GlobalValue::GUID> &ExportedGUIDs,
857d2df54e6STeresa Johnson     std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap) {
858d2df54e6STeresa Johnson   DevirtIndex(Summary, ExportedGUIDs, LocalWPDTargetsMap).run();
859d2df54e6STeresa Johnson }
860d2df54e6STeresa Johnson 
updateIndexWPDForExports(ModuleSummaryIndex & Summary,function_ref<bool (StringRef,ValueInfo)> isExported,std::map<ValueInfo,std::vector<VTableSlotSummary>> & LocalWPDTargetsMap)861d2df54e6STeresa Johnson void updateIndexWPDForExports(
862d2df54e6STeresa Johnson     ModuleSummaryIndex &Summary,
8633d708bf5Sevgeny     function_ref<bool(StringRef, ValueInfo)> isExported,
864d2df54e6STeresa Johnson     std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap) {
865d2df54e6STeresa Johnson   for (auto &T : LocalWPDTargetsMap) {
866d2df54e6STeresa Johnson     auto &VI = T.first;
867d2df54e6STeresa Johnson     // This was enforced earlier during trySingleImplDevirt.
868d2df54e6STeresa Johnson     assert(VI.getSummaryList().size() == 1 &&
869d2df54e6STeresa Johnson            "Devirt of local target has more than one copy");
870d2df54e6STeresa Johnson     auto &S = VI.getSummaryList()[0];
8713d708bf5Sevgeny     if (!isExported(S->modulePath(), VI))
872d2df54e6STeresa Johnson       continue;
873d2df54e6STeresa Johnson 
874d2df54e6STeresa Johnson     // It's been exported by a cross module import.
875d2df54e6STeresa Johnson     for (auto &SlotSummary : T.second) {
876d2df54e6STeresa Johnson       auto *TIdSum = Summary.getTypeIdSummary(SlotSummary.TypeID);
877d2df54e6STeresa Johnson       assert(TIdSum);
878d2df54e6STeresa Johnson       auto WPDRes = TIdSum->WPDRes.find(SlotSummary.ByteOffset);
879d2df54e6STeresa Johnson       assert(WPDRes != TIdSum->WPDRes.end());
880d2df54e6STeresa Johnson       WPDRes->second.SingleImplName = ModuleSummaryIndex::getGlobalNameForLocal(
881d2df54e6STeresa Johnson           WPDRes->second.SingleImplName,
882d2df54e6STeresa Johnson           Summary.getModuleHash(S->modulePath()));
883d2df54e6STeresa Johnson     }
884d2df54e6STeresa Johnson   }
885d2df54e6STeresa Johnson }
886d2df54e6STeresa Johnson 
887d2df54e6STeresa Johnson } // end namespace llvm
888d2df54e6STeresa Johnson 
checkCombinedSummaryForTesting(ModuleSummaryIndex * Summary)8898973fae1SEvgeny Leviant static Error checkCombinedSummaryForTesting(ModuleSummaryIndex *Summary) {
8908973fae1SEvgeny Leviant   // Check that summary index contains regular LTO module when performing
8918973fae1SEvgeny Leviant   // export to prevent occasional use of index from pure ThinLTO compilation
8928973fae1SEvgeny Leviant   // (-fno-split-lto-module). This kind of summary index is passed to
8938973fae1SEvgeny Leviant   // DevirtIndex::run, not to DevirtModule::run used by opt/runForTesting.
8948973fae1SEvgeny Leviant   const auto &ModPaths = Summary->modulePaths();
8958973fae1SEvgeny Leviant   if (ClSummaryAction != PassSummaryAction::Import &&
8968973fae1SEvgeny Leviant       ModPaths.find(ModuleSummaryIndex::getRegularLTOModuleName()) ==
8978973fae1SEvgeny Leviant           ModPaths.end())
8988973fae1SEvgeny Leviant     return createStringError(
8998973fae1SEvgeny Leviant         errc::invalid_argument,
9008973fae1SEvgeny Leviant         "combined summary should contain Regular LTO module");
9018973fae1SEvgeny Leviant   return ErrorSuccess();
9028973fae1SEvgeny Leviant }
9038973fae1SEvgeny Leviant 
runForTesting(Module & M,function_ref<AAResults & (Function &)> AARGetter,function_ref<OptimizationRemarkEmitter & (Function *)> OREGetter,function_ref<DominatorTree & (Function &)> LookupDomTree)90437317f12SPeter Collingbourne bool DevirtModule::runForTesting(
905e963c89dSSam Elliott     Module &M, function_ref<AAResults &(Function &)> AARGetter,
906f24136f1STeresa Johnson     function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter,
907f24136f1STeresa Johnson     function_ref<DominatorTree &(Function &)> LookupDomTree) {
9088973fae1SEvgeny Leviant   std::unique_ptr<ModuleSummaryIndex> Summary =
9098973fae1SEvgeny Leviant       std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
9102b33f653SPeter Collingbourne 
9112b33f653SPeter Collingbourne   // Handle the command-line summary arguments. This code is for testing
9122b33f653SPeter Collingbourne   // purposes only, so we handle errors directly.
9132b33f653SPeter Collingbourne   if (!ClReadSummary.empty()) {
9142b33f653SPeter Collingbourne     ExitOnError ExitOnErr("-wholeprogramdevirt-read-summary: " + ClReadSummary +
9152b33f653SPeter Collingbourne                           ": ");
9162b33f653SPeter Collingbourne     auto ReadSummaryFile =
9172b33f653SPeter Collingbourne         ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ClReadSummary)));
9188973fae1SEvgeny Leviant     if (Expected<std::unique_ptr<ModuleSummaryIndex>> SummaryOrErr =
9198973fae1SEvgeny Leviant             getModuleSummaryIndex(*ReadSummaryFile)) {
9208973fae1SEvgeny Leviant       Summary = std::move(*SummaryOrErr);
9218973fae1SEvgeny Leviant       ExitOnErr(checkCombinedSummaryForTesting(Summary.get()));
9228973fae1SEvgeny Leviant     } else {
9238973fae1SEvgeny Leviant       // Try YAML if we've failed with bitcode.
9248973fae1SEvgeny Leviant       consumeError(SummaryOrErr.takeError());
9252b33f653SPeter Collingbourne       yaml::Input In(ReadSummaryFile->getBuffer());
9268973fae1SEvgeny Leviant       In >> *Summary;
9272b33f653SPeter Collingbourne       ExitOnErr(errorCodeToError(In.error()));
9282b33f653SPeter Collingbourne     }
9298973fae1SEvgeny Leviant   }
9302b33f653SPeter Collingbourne 
931f7691d8bSPeter Collingbourne   bool Changed =
9328973fae1SEvgeny Leviant       DevirtModule(M, AARGetter, OREGetter, LookupDomTree,
9338973fae1SEvgeny Leviant                    ClSummaryAction == PassSummaryAction::Export ? Summary.get()
9348973fae1SEvgeny Leviant                                                                 : nullptr,
9358973fae1SEvgeny Leviant                    ClSummaryAction == PassSummaryAction::Import ? Summary.get()
9368973fae1SEvgeny Leviant                                                                 : nullptr)
937f7691d8bSPeter Collingbourne           .run();
9382b33f653SPeter Collingbourne 
9392b33f653SPeter Collingbourne   if (!ClWriteSummary.empty()) {
9402b33f653SPeter Collingbourne     ExitOnError ExitOnErr(
9412b33f653SPeter Collingbourne         "-wholeprogramdevirt-write-summary: " + ClWriteSummary + ": ");
9422b33f653SPeter Collingbourne     std::error_code EC;
9438973fae1SEvgeny Leviant     if (StringRef(ClWriteSummary).endswith(".bc")) {
9448973fae1SEvgeny Leviant       raw_fd_ostream OS(ClWriteSummary, EC, sys::fs::OF_None);
9458973fae1SEvgeny Leviant       ExitOnErr(errorCodeToError(EC));
9467aaf024dSFangrui Song       writeIndexToFile(*Summary, OS);
9478973fae1SEvgeny Leviant     } else {
94882b3e28eSAbhina Sreeskantharajan       raw_fd_ostream OS(ClWriteSummary, EC, sys::fs::OF_TextWithCRLF);
9492b33f653SPeter Collingbourne       ExitOnErr(errorCodeToError(EC));
9502b33f653SPeter Collingbourne       yaml::Output Out(OS);
9518973fae1SEvgeny Leviant       Out << *Summary;
9528973fae1SEvgeny Leviant     }
9532b33f653SPeter Collingbourne   }
9542b33f653SPeter Collingbourne 
9552b33f653SPeter Collingbourne   return Changed;
9562b33f653SPeter Collingbourne }
9572b33f653SPeter Collingbourne 
buildTypeIdentifierMap(std::vector<VTableBits> & Bits,DenseMap<Metadata *,std::set<TypeMemberInfo>> & TypeIdMap)9587efd7506SPeter Collingbourne void DevirtModule::buildTypeIdentifierMap(
959df49d1bbSPeter Collingbourne     std::vector<VTableBits> &Bits,
9607efd7506SPeter Collingbourne     DenseMap<Metadata *, std::set<TypeMemberInfo>> &TypeIdMap) {
961df49d1bbSPeter Collingbourne   DenseMap<GlobalVariable *, VTableBits *> GVToBits;
9627efd7506SPeter Collingbourne   Bits.reserve(M.getGlobalList().size());
9637efd7506SPeter Collingbourne   SmallVector<MDNode *, 2> Types;
9647efd7506SPeter Collingbourne   for (GlobalVariable &GV : M.globals()) {
9657efd7506SPeter Collingbourne     Types.clear();
9667efd7506SPeter Collingbourne     GV.getMetadata(LLVMContext::MD_type, Types);
9672b70d616SEugene Leviant     if (GV.isDeclaration() || Types.empty())
968df49d1bbSPeter Collingbourne       continue;
969df49d1bbSPeter Collingbourne 
9707efd7506SPeter Collingbourne     VTableBits *&BitsPtr = GVToBits[&GV];
9717efd7506SPeter Collingbourne     if (!BitsPtr) {
9727efd7506SPeter Collingbourne       Bits.emplace_back();
9737efd7506SPeter Collingbourne       Bits.back().GV = &GV;
9747efd7506SPeter Collingbourne       Bits.back().ObjectSize =
9757efd7506SPeter Collingbourne           M.getDataLayout().getTypeAllocSize(GV.getInitializer()->getType());
9767efd7506SPeter Collingbourne       BitsPtr = &Bits.back();
9777efd7506SPeter Collingbourne     }
9787efd7506SPeter Collingbourne 
9797efd7506SPeter Collingbourne     for (MDNode *Type : Types) {
9807efd7506SPeter Collingbourne       auto TypeID = Type->getOperand(1).get();
981df49d1bbSPeter Collingbourne 
982df49d1bbSPeter Collingbourne       uint64_t Offset =
983df49d1bbSPeter Collingbourne           cast<ConstantInt>(
9847efd7506SPeter Collingbourne               cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
985df49d1bbSPeter Collingbourne               ->getZExtValue();
986df49d1bbSPeter Collingbourne 
9877efd7506SPeter Collingbourne       TypeIdMap[TypeID].insert({BitsPtr, Offset});
988df49d1bbSPeter Collingbourne     }
989df49d1bbSPeter Collingbourne   }
990df49d1bbSPeter Collingbourne }
991df49d1bbSPeter Collingbourne 
tryFindVirtualCallTargets(std::vector<VirtualCallTarget> & TargetsForSlot,const std::set<TypeMemberInfo> & TypeMemberInfos,uint64_t ByteOffset,ModuleSummaryIndex * ExportSummary)992df49d1bbSPeter Collingbourne bool DevirtModule::tryFindVirtualCallTargets(
993df49d1bbSPeter Collingbourne     std::vector<VirtualCallTarget> &TargetsForSlot,
99409a704c5SMingming Liu     const std::set<TypeMemberInfo> &TypeMemberInfos, uint64_t ByteOffset,
99509a704c5SMingming Liu     ModuleSummaryIndex *ExportSummary) {
9967efd7506SPeter Collingbourne   for (const TypeMemberInfo &TM : TypeMemberInfos) {
9977efd7506SPeter Collingbourne     if (!TM.Bits->GV->isConstant())
998df49d1bbSPeter Collingbourne       return false;
999df49d1bbSPeter Collingbourne 
10002f63d549STeresa Johnson     // We cannot perform whole program devirtualization analysis on a vtable
10012f63d549STeresa Johnson     // with public LTO visibility.
10022f63d549STeresa Johnson     if (TM.Bits->GV->getVCallVisibility() ==
10032f63d549STeresa Johnson         GlobalObject::VCallVisibilityPublic)
10042f63d549STeresa Johnson       return false;
10052f63d549STeresa Johnson 
10068786754cSPeter Collingbourne     Constant *Ptr = getPointerAtOffset(TM.Bits->GV->getInitializer(),
10073b598b9cSOliver Stannard                                        TM.Offset + ByteOffset, M);
10088786754cSPeter Collingbourne     if (!Ptr)
1009df49d1bbSPeter Collingbourne       return false;
1010df49d1bbSPeter Collingbourne 
10118786754cSPeter Collingbourne     auto Fn = dyn_cast<Function>(Ptr->stripPointerCasts());
1012df49d1bbSPeter Collingbourne     if (!Fn)
1013df49d1bbSPeter Collingbourne       return false;
1014df49d1bbSPeter Collingbourne 
10156d2032e2Sevgeny     if (FunctionsToSkip.match(Fn->getName()))
10166d2032e2Sevgeny       return false;
10176d2032e2Sevgeny 
1018df49d1bbSPeter Collingbourne     // We can disregard __cxa_pure_virtual as a possible call target, as
1019df49d1bbSPeter Collingbourne     // calls to pure virtuals are UB.
1020df49d1bbSPeter Collingbourne     if (Fn->getName() == "__cxa_pure_virtual")
1021df49d1bbSPeter Collingbourne       continue;
1022df49d1bbSPeter Collingbourne 
102309a704c5SMingming Liu     // We can disregard unreachable functions as possible call targets, as
102409a704c5SMingming Liu     // unreachable functions shouldn't be called.
10259c49f8d7Sminglotus-6     if (mustBeUnreachableFunction(Fn, ExportSummary))
102609a704c5SMingming Liu       continue;
102709a704c5SMingming Liu 
10287efd7506SPeter Collingbourne     TargetsForSlot.push_back({Fn, &TM});
1029df49d1bbSPeter Collingbourne   }
1030df49d1bbSPeter Collingbourne 
1031df49d1bbSPeter Collingbourne   // Give up if we couldn't find any targets.
1032df49d1bbSPeter Collingbourne   return !TargetsForSlot.empty();
1033df49d1bbSPeter Collingbourne }
1034df49d1bbSPeter Collingbourne 
tryFindVirtualCallTargets(std::vector<ValueInfo> & TargetsForSlot,const TypeIdCompatibleVtableInfo TIdInfo,uint64_t ByteOffset)1035d2df54e6STeresa Johnson bool DevirtIndex::tryFindVirtualCallTargets(
1036d2df54e6STeresa Johnson     std::vector<ValueInfo> &TargetsForSlot, const TypeIdCompatibleVtableInfo TIdInfo,
1037d2df54e6STeresa Johnson     uint64_t ByteOffset) {
1038098d3347SMark de Wever   for (const TypeIdOffsetVtableInfo &P : TIdInfo) {
10393c4c2050STeresa Johnson     // Find a representative copy of the vtable initializer.
1040c844f884STeresa Johnson     // We can have multiple available_externally, linkonce_odr and weak_odr
10410a5949dcSTeresa Johnson     // vtable initializers. We can also have multiple external vtable
10420a5949dcSTeresa Johnson     // initializers in the case of comdats, which we cannot check here.
10432cefb939STeresa Johnson     // The linker should give an error in this case.
1044c844f884STeresa Johnson     //
1045c844f884STeresa Johnson     // Also, handle the case of same-named local Vtables with the same path
1046c844f884STeresa Johnson     // and therefore the same GUID. This can happen if there isn't enough
1047c844f884STeresa Johnson     // distinguishing path when compiling the source file. In that case we
1048c844f884STeresa Johnson     // conservatively return false early.
1049c844f884STeresa Johnson     const GlobalVarSummary *VS = nullptr;
1050c844f884STeresa Johnson     bool LocalFound = false;
1051c844f884STeresa Johnson     for (auto &S : P.VTableVI.getSummaryList()) {
1052c844f884STeresa Johnson       if (GlobalValue::isLocalLinkage(S->linkage())) {
1053c844f884STeresa Johnson         if (LocalFound)
1054c844f884STeresa Johnson           return false;
1055c844f884STeresa Johnson         LocalFound = true;
1056c844f884STeresa Johnson       }
10573c4c2050STeresa Johnson       auto *CurVS = cast<GlobalVarSummary>(S->getBaseObject());
10580a5949dcSTeresa Johnson       if (!CurVS->vTableFuncs().empty() ||
10590a5949dcSTeresa Johnson           // Previously clang did not attach the necessary type metadata to
10600a5949dcSTeresa Johnson           // available_externally vtables, in which case there would not
10610a5949dcSTeresa Johnson           // be any vtable functions listed in the summary and we need
10620a5949dcSTeresa Johnson           // to treat this case conservatively (in case the bitcode is old).
10630a5949dcSTeresa Johnson           // However, we will also not have any vtable functions in the
10640a5949dcSTeresa Johnson           // case of a pure virtual base class. In that case we do want
10650a5949dcSTeresa Johnson           // to set VS to avoid treating it conservatively.
10660a5949dcSTeresa Johnson           !GlobalValue::isAvailableExternallyLinkage(S->linkage())) {
10673c4c2050STeresa Johnson         VS = CurVS;
10682f63d549STeresa Johnson         // We cannot perform whole program devirtualization analysis on a vtable
10692f63d549STeresa Johnson         // with public LTO visibility.
10702f63d549STeresa Johnson         if (VS->getVCallVisibility() == GlobalObject::VCallVisibilityPublic)
10712f63d549STeresa Johnson           return false;
10720a5949dcSTeresa Johnson       }
10732f63d549STeresa Johnson     }
10743c4c2050STeresa Johnson     // There will be no VS if all copies are available_externally having no
10753c4c2050STeresa Johnson     // type metadata. In that case we can't safely perform WPD.
10763c4c2050STeresa Johnson     if (!VS)
10773c4c2050STeresa Johnson       return false;
1078c844f884STeresa Johnson     if (!VS->isLive())
1079d2df54e6STeresa Johnson       continue;
1080d2df54e6STeresa Johnson     for (auto VTP : VS->vTableFuncs()) {
1081d2df54e6STeresa Johnson       if (VTP.VTableOffset != P.AddressPointOffset + ByteOffset)
1082d2df54e6STeresa Johnson         continue;
1083d2df54e6STeresa Johnson 
10844ab5527cSminglotus-6       if (mustBeUnreachableFunction(VTP.FuncVI))
10854ab5527cSminglotus-6         continue;
10864ab5527cSminglotus-6 
1087d2df54e6STeresa Johnson       TargetsForSlot.push_back(VTP.FuncVI);
1088d2df54e6STeresa Johnson     }
1089d2df54e6STeresa Johnson   }
1090d2df54e6STeresa Johnson 
1091d2df54e6STeresa Johnson   // Give up if we couldn't find any targets.
1092d2df54e6STeresa Johnson   return !TargetsForSlot.empty();
1093d2df54e6STeresa Johnson }
1094d2df54e6STeresa Johnson 
applySingleImplDevirt(VTableSlotInfo & SlotInfo,Constant * TheFn,bool & IsExported)109550cbd7ccSPeter Collingbourne void DevirtModule::applySingleImplDevirt(VTableSlotInfo &SlotInfo,
10962325bb34SPeter Collingbourne                                          Constant *TheFn, bool &IsExported) {
10976e4c1cf2STeresa Johnson   // Don't devirtualize function if we're told to skip it
10986e4c1cf2STeresa Johnson   // in -wholeprogramdevirt-skip.
10996e4c1cf2STeresa Johnson   if (FunctionsToSkip.match(TheFn->stripPointerCasts()->getName()))
11006e4c1cf2STeresa Johnson     return;
110150cbd7ccSPeter Collingbourne   auto Apply = [&](CallSiteInfo &CSInfo) {
110250cbd7ccSPeter Collingbourne     for (auto &&VCallSite : CSInfo.CallSites) {
11037110510eSArthur Eubanks       if (!OptimizedCalls.insert(&VCallSite.CB).second)
11047110510eSArthur Eubanks         continue;
11057110510eSArthur Eubanks 
1106f3403fd2SIvan Krasin       if (RemarksEnabled)
1107b0a1d3bdSTeresa Johnson         VCallSite.emitRemark("single-impl",
1108b0a1d3bdSTeresa Johnson                              TheFn->stripPointerCasts()->getName(), OREGetter);
1109ced9a795STeresa Johnson       NumSingleImpl++;
1110d55d46f4STeresa Johnson       auto &CB = VCallSite.CB;
11117110510eSArthur Eubanks       assert(!CB.getCalledFunction() && "devirtualizing direct call?");
1112d55d46f4STeresa Johnson       IRBuilder<> Builder(&CB);
1113d55d46f4STeresa Johnson       Value *Callee =
1114d55d46f4STeresa Johnson           Builder.CreateBitCast(TheFn, CB.getCalledOperand()->getType());
1115d55d46f4STeresa Johnson 
1116fee0bde4STeresa Johnson       // If trap checking is enabled, add support to compare the virtual
1117fee0bde4STeresa Johnson       // function pointer to the devirtualized target. In case of a mismatch,
1118fee0bde4STeresa Johnson       // perform a debug trap.
1119fee0bde4STeresa Johnson       if (DevirtCheckMode == WPDCheckMode::Trap) {
1120d55d46f4STeresa Johnson         auto *Cond = Builder.CreateICmpNE(CB.getCalledOperand(), Callee);
1121d55d46f4STeresa Johnson         Instruction *ThenTerm =
1122d55d46f4STeresa Johnson             SplitBlockAndInsertIfThen(Cond, &CB, /*Unreachable=*/false);
1123d55d46f4STeresa Johnson         Builder.SetInsertPoint(ThenTerm);
1124d55d46f4STeresa Johnson         Function *TrapFn = Intrinsic::getDeclaration(&M, Intrinsic::debugtrap);
1125d55d46f4STeresa Johnson         auto *CallTrap = Builder.CreateCall(TrapFn);
1126d55d46f4STeresa Johnson         CallTrap->setDebugLoc(CB.getDebugLoc());
1127d55d46f4STeresa Johnson       }
1128d55d46f4STeresa Johnson 
1129fee0bde4STeresa Johnson       // If fallback checking is enabled, add support to compare the virtual
1130fee0bde4STeresa Johnson       // function pointer to the devirtualized target. In case of a mismatch,
1131fee0bde4STeresa Johnson       // fall back to indirect call.
1132fee0bde4STeresa Johnson       if (DevirtCheckMode == WPDCheckMode::Fallback) {
1133fee0bde4STeresa Johnson         MDNode *Weights =
1134fee0bde4STeresa Johnson             MDBuilder(M.getContext()).createBranchWeights((1U << 20) - 1, 1);
1135fee0bde4STeresa Johnson         // Version the indirect call site. If the called value is equal to the
1136fee0bde4STeresa Johnson         // given callee, 'NewInst' will be executed, otherwise the original call
1137fee0bde4STeresa Johnson         // site will be executed.
1138fee0bde4STeresa Johnson         CallBase &NewInst = versionCallSite(CB, Callee, Weights);
1139fee0bde4STeresa Johnson         NewInst.setCalledOperand(Callee);
1140fee0bde4STeresa Johnson         // Since the new call site is direct, we must clear metadata that
1141fee0bde4STeresa Johnson         // is only appropriate for indirect calls. This includes !prof and
1142fee0bde4STeresa Johnson         // !callees metadata.
1143fee0bde4STeresa Johnson         NewInst.setMetadata(LLVMContext::MD_prof, nullptr);
1144fee0bde4STeresa Johnson         NewInst.setMetadata(LLVMContext::MD_callees, nullptr);
1145fee0bde4STeresa Johnson         // Additionally, we should remove them from the fallback indirect call,
1146fee0bde4STeresa Johnson         // so that we don't attempt to perform indirect call promotion later.
1147fee0bde4STeresa Johnson         CB.setMetadata(LLVMContext::MD_prof, nullptr);
1148fee0bde4STeresa Johnson         CB.setMetadata(LLVMContext::MD_callees, nullptr);
1149fee0bde4STeresa Johnson       }
1150fee0bde4STeresa Johnson 
1151fee0bde4STeresa Johnson       // In either trapping or non-checking mode, devirtualize original call.
1152fee0bde4STeresa Johnson       else {
1153fee0bde4STeresa Johnson         // Devirtualize unconditionally.
1154d55d46f4STeresa Johnson         CB.setCalledOperand(Callee);
1155fee0bde4STeresa Johnson         // Since the call site is now direct, we must clear metadata that
1156fee0bde4STeresa Johnson         // is only appropriate for indirect calls. This includes !prof and
1157fee0bde4STeresa Johnson         // !callees metadata.
1158fee0bde4STeresa Johnson         CB.setMetadata(LLVMContext::MD_prof, nullptr);
1159fee0bde4STeresa Johnson         CB.setMetadata(LLVMContext::MD_callees, nullptr);
1160fee0bde4STeresa Johnson       }
1161d55d46f4STeresa Johnson 
11620312f614SPeter Collingbourne       // This use is no longer unsafe.
11630312f614SPeter Collingbourne       if (VCallSite.NumUnsafeUses)
11640312f614SPeter Collingbourne         --*VCallSite.NumUnsafeUses;
1165df49d1bbSPeter Collingbourne     }
11662974856aSPeter Collingbourne     if (CSInfo.isExported())
11672325bb34SPeter Collingbourne       IsExported = true;
116859675ba0SPeter Collingbourne     CSInfo.markDevirt();
116950cbd7ccSPeter Collingbourne   };
117050cbd7ccSPeter Collingbourne   Apply(SlotInfo.CSInfo);
117150cbd7ccSPeter Collingbourne   for (auto &P : SlotInfo.ConstCSInfo)
117250cbd7ccSPeter Collingbourne     Apply(P.second);
117350cbd7ccSPeter Collingbourne }
117450cbd7ccSPeter Collingbourne 
AddCalls(VTableSlotInfo & SlotInfo,const ValueInfo & Callee)1175943afb57SEugene Leviant static bool AddCalls(VTableSlotInfo &SlotInfo, const ValueInfo &Callee) {
1176943afb57SEugene Leviant   // We can't add calls if we haven't seen a definition
1177943afb57SEugene Leviant   if (Callee.getSummaryList().empty())
1178943afb57SEugene Leviant     return false;
1179943afb57SEugene Leviant 
1180943afb57SEugene Leviant   // Insert calls into the summary index so that the devirtualized targets
1181943afb57SEugene Leviant   // are eligible for import.
1182943afb57SEugene Leviant   // FIXME: Annotate type tests with hotness. For now, mark these as hot
1183943afb57SEugene Leviant   // to better ensure we have the opportunity to inline them.
1184943afb57SEugene Leviant   bool IsExported = false;
1185943afb57SEugene Leviant   auto &S = Callee.getSummaryList()[0];
1186943afb57SEugene Leviant   CalleeInfo CI(CalleeInfo::HotnessType::Hot, /* RelBF = */ 0);
1187943afb57SEugene Leviant   auto AddCalls = [&](CallSiteInfo &CSInfo) {
1188943afb57SEugene Leviant     for (auto *FS : CSInfo.SummaryTypeCheckedLoadUsers) {
1189943afb57SEugene Leviant       FS->addCall({Callee, CI});
1190943afb57SEugene Leviant       IsExported |= S->modulePath() != FS->modulePath();
1191943afb57SEugene Leviant     }
1192943afb57SEugene Leviant     for (auto *FS : CSInfo.SummaryTypeTestAssumeUsers) {
1193943afb57SEugene Leviant       FS->addCall({Callee, CI});
1194943afb57SEugene Leviant       IsExported |= S->modulePath() != FS->modulePath();
1195943afb57SEugene Leviant     }
1196943afb57SEugene Leviant   };
1197943afb57SEugene Leviant   AddCalls(SlotInfo.CSInfo);
1198943afb57SEugene Leviant   for (auto &P : SlotInfo.ConstCSInfo)
1199943afb57SEugene Leviant     AddCalls(P.second);
1200943afb57SEugene Leviant   return IsExported;
1201943afb57SEugene Leviant }
1202943afb57SEugene Leviant 
trySingleImplDevirt(ModuleSummaryIndex * ExportSummary,MutableArrayRef<VirtualCallTarget> TargetsForSlot,VTableSlotInfo & SlotInfo,WholeProgramDevirtResolution * Res)120350cbd7ccSPeter Collingbourne bool DevirtModule::trySingleImplDevirt(
1204943afb57SEugene Leviant     ModuleSummaryIndex *ExportSummary,
1205943afb57SEugene Leviant     MutableArrayRef<VirtualCallTarget> TargetsForSlot, VTableSlotInfo &SlotInfo,
1206943afb57SEugene Leviant     WholeProgramDevirtResolution *Res) {
120750cbd7ccSPeter Collingbourne   // See if the program contains a single implementation of this virtual
120850cbd7ccSPeter Collingbourne   // function.
120950cbd7ccSPeter Collingbourne   Function *TheFn = TargetsForSlot[0].Fn;
121050cbd7ccSPeter Collingbourne   for (auto &&Target : TargetsForSlot)
121150cbd7ccSPeter Collingbourne     if (TheFn != Target.Fn)
121250cbd7ccSPeter Collingbourne       return false;
121350cbd7ccSPeter Collingbourne 
121450cbd7ccSPeter Collingbourne   // If so, update each call site to call that implementation directly.
1215ced9a795STeresa Johnson   if (RemarksEnabled || AreStatisticsEnabled())
121650cbd7ccSPeter Collingbourne     TargetsForSlot[0].WasDevirt = true;
12172325bb34SPeter Collingbourne 
12182325bb34SPeter Collingbourne   bool IsExported = false;
12192325bb34SPeter Collingbourne   applySingleImplDevirt(SlotInfo, TheFn, IsExported);
12202325bb34SPeter Collingbourne   if (!IsExported)
12212325bb34SPeter Collingbourne     return false;
12222325bb34SPeter Collingbourne 
12232325bb34SPeter Collingbourne   // If the only implementation has local linkage, we must promote to external
12242325bb34SPeter Collingbourne   // to make it visible to thin LTO objects. We can only get here during the
12252325bb34SPeter Collingbourne   // ThinLTO export phase.
12262325bb34SPeter Collingbourne   if (TheFn->hasLocalLinkage()) {
1227c6e5c465SHans Wennborg     std::string NewName = (TheFn->getName() + ".llvm.merged").str();
122888a58cf9SPeter Collingbourne 
122988a58cf9SPeter Collingbourne     // Since we are renaming the function, any comdats with the same name must
123088a58cf9SPeter Collingbourne     // also be renamed. This is required when targeting COFF, as the comdat name
123188a58cf9SPeter Collingbourne     // must match one of the names of the symbols in the comdat.
123288a58cf9SPeter Collingbourne     if (Comdat *C = TheFn->getComdat()) {
123388a58cf9SPeter Collingbourne       if (C->getName() == TheFn->getName()) {
123488a58cf9SPeter Collingbourne         Comdat *NewC = M.getOrInsertComdat(NewName);
123588a58cf9SPeter Collingbourne         NewC->setSelectionKind(C->getSelectionKind());
123688a58cf9SPeter Collingbourne         for (GlobalObject &GO : M.global_objects())
123788a58cf9SPeter Collingbourne           if (GO.getComdat() == C)
123888a58cf9SPeter Collingbourne             GO.setComdat(NewC);
123988a58cf9SPeter Collingbourne       }
124088a58cf9SPeter Collingbourne     }
124188a58cf9SPeter Collingbourne 
12422325bb34SPeter Collingbourne     TheFn->setLinkage(GlobalValue::ExternalLinkage);
12432325bb34SPeter Collingbourne     TheFn->setVisibility(GlobalValue::HiddenVisibility);
124488a58cf9SPeter Collingbourne     TheFn->setName(NewName);
12452325bb34SPeter Collingbourne   }
1246943afb57SEugene Leviant   if (ValueInfo TheFnVI = ExportSummary->getValueInfo(TheFn->getGUID()))
1247943afb57SEugene Leviant     // Any needed promotion of 'TheFn' has already been done during
1248943afb57SEugene Leviant     // LTO unit split, so we can ignore return value of AddCalls.
1249943afb57SEugene Leviant     AddCalls(SlotInfo, TheFnVI);
12502325bb34SPeter Collingbourne 
12512325bb34SPeter Collingbourne   Res->TheKind = WholeProgramDevirtResolution::SingleImpl;
1252adcd0268SBenjamin Kramer   Res->SingleImplName = std::string(TheFn->getName());
12532325bb34SPeter Collingbourne 
1254df49d1bbSPeter Collingbourne   return true;
1255df49d1bbSPeter Collingbourne }
1256df49d1bbSPeter Collingbourne 
trySingleImplDevirt(MutableArrayRef<ValueInfo> TargetsForSlot,VTableSlotSummary & SlotSummary,VTableSlotInfo & SlotInfo,WholeProgramDevirtResolution * Res,std::set<ValueInfo> & DevirtTargets)1257d2df54e6STeresa Johnson bool DevirtIndex::trySingleImplDevirt(MutableArrayRef<ValueInfo> TargetsForSlot,
1258d2df54e6STeresa Johnson                                       VTableSlotSummary &SlotSummary,
1259d2df54e6STeresa Johnson                                       VTableSlotInfo &SlotInfo,
1260d2df54e6STeresa Johnson                                       WholeProgramDevirtResolution *Res,
1261d2df54e6STeresa Johnson                                       std::set<ValueInfo> &DevirtTargets) {
1262d2df54e6STeresa Johnson   // See if the program contains a single implementation of this virtual
1263d2df54e6STeresa Johnson   // function.
1264d2df54e6STeresa Johnson   auto TheFn = TargetsForSlot[0];
1265d2df54e6STeresa Johnson   for (auto &&Target : TargetsForSlot)
1266d2df54e6STeresa Johnson     if (TheFn != Target)
1267d2df54e6STeresa Johnson       return false;
1268d2df54e6STeresa Johnson 
1269d2df54e6STeresa Johnson   // Don't devirtualize if we don't have target definition.
1270d2df54e6STeresa Johnson   auto Size = TheFn.getSummaryList().size();
1271d2df54e6STeresa Johnson   if (!Size)
1272d2df54e6STeresa Johnson     return false;
1273d2df54e6STeresa Johnson 
12746d2032e2Sevgeny   // Don't devirtualize function if we're told to skip it
12756d2032e2Sevgeny   // in -wholeprogramdevirt-skip.
12766d2032e2Sevgeny   if (FunctionsToSkip.match(TheFn.name()))
12776d2032e2Sevgeny     return false;
12786d2032e2Sevgeny 
1279d2df54e6STeresa Johnson   // If the summary list contains multiple summaries where at least one is
1280d2df54e6STeresa Johnson   // a local, give up, as we won't know which (possibly promoted) name to use.
1281d2df54e6STeresa Johnson   for (auto &S : TheFn.getSummaryList())
1282d2df54e6STeresa Johnson     if (GlobalValue::isLocalLinkage(S->linkage()) && Size > 1)
1283d2df54e6STeresa Johnson       return false;
1284d2df54e6STeresa Johnson 
1285d2df54e6STeresa Johnson   // Collect functions devirtualized at least for one call site for stats.
1286ced9a795STeresa Johnson   if (PrintSummaryDevirt || AreStatisticsEnabled())
1287d2df54e6STeresa Johnson     DevirtTargets.insert(TheFn);
1288d2df54e6STeresa Johnson 
1289d2df54e6STeresa Johnson   auto &S = TheFn.getSummaryList()[0];
1290943afb57SEugene Leviant   bool IsExported = AddCalls(SlotInfo, TheFn);
1291d2df54e6STeresa Johnson   if (IsExported)
1292d2df54e6STeresa Johnson     ExportedGUIDs.insert(TheFn.getGUID());
1293d2df54e6STeresa Johnson 
1294d2df54e6STeresa Johnson   // Record in summary for use in devirtualization during the ThinLTO import
1295d2df54e6STeresa Johnson   // step.
1296d2df54e6STeresa Johnson   Res->TheKind = WholeProgramDevirtResolution::SingleImpl;
1297d2df54e6STeresa Johnson   if (GlobalValue::isLocalLinkage(S->linkage())) {
1298d2df54e6STeresa Johnson     if (IsExported)
1299d2df54e6STeresa Johnson       // If target is a local function and we are exporting it by
1300d2df54e6STeresa Johnson       // devirtualizing a call in another module, we need to record the
1301d2df54e6STeresa Johnson       // promoted name.
1302d2df54e6STeresa Johnson       Res->SingleImplName = ModuleSummaryIndex::getGlobalNameForLocal(
1303d2df54e6STeresa Johnson           TheFn.name(), ExportSummary.getModuleHash(S->modulePath()));
1304d2df54e6STeresa Johnson     else {
1305d2df54e6STeresa Johnson       LocalWPDTargetsMap[TheFn].push_back(SlotSummary);
1306adcd0268SBenjamin Kramer       Res->SingleImplName = std::string(TheFn.name());
1307d2df54e6STeresa Johnson     }
1308d2df54e6STeresa Johnson   } else
1309adcd0268SBenjamin Kramer     Res->SingleImplName = std::string(TheFn.name());
1310d2df54e6STeresa Johnson 
1311d2df54e6STeresa Johnson   // Name will be empty if this thin link driven off of serialized combined
1312d2df54e6STeresa Johnson   // index (e.g. llvm-lto). However, WPD is not supported/invoked for the
1313d2df54e6STeresa Johnson   // legacy LTO API anyway.
1314d2df54e6STeresa Johnson   assert(!Res->SingleImplName.empty());
1315d2df54e6STeresa Johnson 
1316d2df54e6STeresa Johnson   return true;
1317d2df54e6STeresa Johnson }
1318d2df54e6STeresa Johnson 
tryICallBranchFunnel(MutableArrayRef<VirtualCallTarget> TargetsForSlot,VTableSlotInfo & SlotInfo,WholeProgramDevirtResolution * Res,VTableSlot Slot)13192974856aSPeter Collingbourne void DevirtModule::tryICallBranchFunnel(
13202974856aSPeter Collingbourne     MutableArrayRef<VirtualCallTarget> TargetsForSlot, VTableSlotInfo &SlotInfo,
13212974856aSPeter Collingbourne     WholeProgramDevirtResolution *Res, VTableSlot Slot) {
13222974856aSPeter Collingbourne   Triple T(M.getTargetTriple());
13232974856aSPeter Collingbourne   if (T.getArch() != Triple::x86_64)
13242974856aSPeter Collingbourne     return;
13252974856aSPeter Collingbourne 
132666f53d71SVitaly Buka   if (TargetsForSlot.size() > ClThreshold)
13272974856aSPeter Collingbourne     return;
13282974856aSPeter Collingbourne 
13292974856aSPeter Collingbourne   bool HasNonDevirt = !SlotInfo.CSInfo.AllCallSitesDevirted;
13302974856aSPeter Collingbourne   if (!HasNonDevirt)
13312974856aSPeter Collingbourne     for (auto &P : SlotInfo.ConstCSInfo)
13322974856aSPeter Collingbourne       if (!P.second.AllCallSitesDevirted) {
13332974856aSPeter Collingbourne         HasNonDevirt = true;
13342974856aSPeter Collingbourne         break;
13352974856aSPeter Collingbourne       }
13362974856aSPeter Collingbourne 
13372974856aSPeter Collingbourne   if (!HasNonDevirt)
13382974856aSPeter Collingbourne     return;
13392974856aSPeter Collingbourne 
13402974856aSPeter Collingbourne   FunctionType *FT =
13412974856aSPeter Collingbourne       FunctionType::get(Type::getVoidTy(M.getContext()), {Int8PtrTy}, true);
13422974856aSPeter Collingbourne   Function *JT;
13432974856aSPeter Collingbourne   if (isa<MDString>(Slot.TypeID)) {
13442974856aSPeter Collingbourne     JT = Function::Create(FT, Function::ExternalLinkage,
1345f920da00SDylan McKay                           M.getDataLayout().getProgramAddressSpace(),
13462974856aSPeter Collingbourne                           getGlobalName(Slot, {}, "branch_funnel"), &M);
13472974856aSPeter Collingbourne     JT->setVisibility(GlobalValue::HiddenVisibility);
13482974856aSPeter Collingbourne   } else {
1349f920da00SDylan McKay     JT = Function::Create(FT, Function::InternalLinkage,
1350f920da00SDylan McKay                           M.getDataLayout().getProgramAddressSpace(),
1351f920da00SDylan McKay                           "branch_funnel", &M);
13522974856aSPeter Collingbourne   }
135346cf8253SArthur Eubanks   JT->addParamAttr(0, Attribute::Nest);
13542974856aSPeter Collingbourne 
13552974856aSPeter Collingbourne   std::vector<Value *> JTArgs;
13562974856aSPeter Collingbourne   JTArgs.push_back(JT->arg_begin());
13572974856aSPeter Collingbourne   for (auto &T : TargetsForSlot) {
13582974856aSPeter Collingbourne     JTArgs.push_back(getMemberAddr(T.TM));
13592974856aSPeter Collingbourne     JTArgs.push_back(T.Fn);
13602974856aSPeter Collingbourne   }
13612974856aSPeter Collingbourne 
13622974856aSPeter Collingbourne   BasicBlock *BB = BasicBlock::Create(M.getContext(), "", JT, nullptr);
13637976eb58SJames Y Knight   Function *Intr =
13642974856aSPeter Collingbourne       Intrinsic::getDeclaration(&M, llvm::Intrinsic::icall_branch_funnel, {});
13652974856aSPeter Collingbourne 
13662974856aSPeter Collingbourne   auto *CI = CallInst::Create(Intr, JTArgs, "", BB);
13672974856aSPeter Collingbourne   CI->setTailCallKind(CallInst::TCK_MustTail);
13682974856aSPeter Collingbourne   ReturnInst::Create(M.getContext(), nullptr, BB);
13692974856aSPeter Collingbourne 
13702974856aSPeter Collingbourne   bool IsExported = false;
13712974856aSPeter Collingbourne   applyICallBranchFunnel(SlotInfo, JT, IsExported);
13722974856aSPeter Collingbourne   if (IsExported)
13732974856aSPeter Collingbourne     Res->TheKind = WholeProgramDevirtResolution::BranchFunnel;
13742974856aSPeter Collingbourne }
13752974856aSPeter Collingbourne 
applyICallBranchFunnel(VTableSlotInfo & SlotInfo,Constant * JT,bool & IsExported)13762974856aSPeter Collingbourne void DevirtModule::applyICallBranchFunnel(VTableSlotInfo &SlotInfo,
13772974856aSPeter Collingbourne                                           Constant *JT, bool &IsExported) {
13782974856aSPeter Collingbourne   auto Apply = [&](CallSiteInfo &CSInfo) {
13792974856aSPeter Collingbourne     if (CSInfo.isExported())
13802974856aSPeter Collingbourne       IsExported = true;
13812974856aSPeter Collingbourne     if (CSInfo.AllCallSitesDevirted)
13822974856aSPeter Collingbourne       return;
13832974856aSPeter Collingbourne     for (auto &&VCallSite : CSInfo.CallSites) {
1384cea6f4d5SMircea Trofin       CallBase &CB = VCallSite.CB;
13852974856aSPeter Collingbourne 
13862974856aSPeter Collingbourne       // Jump tables are only profitable if the retpoline mitigation is enabled.
1387cea6f4d5SMircea Trofin       Attribute FSAttr = CB.getCaller()->getFnAttribute("target-features");
1388aab90384SCraig Topper       if (!FSAttr.isValid() ||
13892974856aSPeter Collingbourne           !FSAttr.getValueAsString().contains("+retpoline"))
13902974856aSPeter Collingbourne         continue;
13912974856aSPeter Collingbourne 
1392ced9a795STeresa Johnson       NumBranchFunnel++;
13932974856aSPeter Collingbourne       if (RemarksEnabled)
1394b0a1d3bdSTeresa Johnson         VCallSite.emitRemark("branch-funnel",
1395b0a1d3bdSTeresa Johnson                              JT->stripPointerCasts()->getName(), OREGetter);
13962974856aSPeter Collingbourne 
13972974856aSPeter Collingbourne       // Pass the address of the vtable in the nest register, which is r10 on
13982974856aSPeter Collingbourne       // x86_64.
13992974856aSPeter Collingbourne       std::vector<Type *> NewArgs;
14002974856aSPeter Collingbourne       NewArgs.push_back(Int8PtrTy);
1401e53472deSKazu Hirata       append_range(NewArgs, CB.getFunctionType()->params());
14027976eb58SJames Y Knight       FunctionType *NewFT =
1403cea6f4d5SMircea Trofin           FunctionType::get(CB.getFunctionType()->getReturnType(), NewArgs,
1404cea6f4d5SMircea Trofin                             CB.getFunctionType()->isVarArg());
14057976eb58SJames Y Knight       PointerType *NewFTPtr = PointerType::getUnqual(NewFT);
14062974856aSPeter Collingbourne 
1407cea6f4d5SMircea Trofin       IRBuilder<> IRB(&CB);
14082974856aSPeter Collingbourne       std::vector<Value *> Args;
14092974856aSPeter Collingbourne       Args.push_back(IRB.CreateBitCast(VCallSite.VTable, Int8PtrTy));
14108299fb8fSKazu Hirata       llvm::append_range(Args, CB.args());
14112974856aSPeter Collingbourne 
1412cea6f4d5SMircea Trofin       CallBase *NewCS = nullptr;
1413cea6f4d5SMircea Trofin       if (isa<CallInst>(CB))
14147976eb58SJames Y Knight         NewCS = IRB.CreateCall(NewFT, IRB.CreateBitCast(JT, NewFTPtr), Args);
14152974856aSPeter Collingbourne       else
1416cea6f4d5SMircea Trofin         NewCS = IRB.CreateInvoke(NewFT, IRB.CreateBitCast(JT, NewFTPtr),
1417cea6f4d5SMircea Trofin                                  cast<InvokeInst>(CB).getNormalDest(),
1418cea6f4d5SMircea Trofin                                  cast<InvokeInst>(CB).getUnwindDest(), Args);
1419cea6f4d5SMircea Trofin       NewCS->setCallingConv(CB.getCallingConv());
14202974856aSPeter Collingbourne 
1421cea6f4d5SMircea Trofin       AttributeList Attrs = CB.getAttributes();
14222974856aSPeter Collingbourne       std::vector<AttributeSet> NewArgAttrs;
14232974856aSPeter Collingbourne       NewArgAttrs.push_back(AttributeSet::get(
14242974856aSPeter Collingbourne           M.getContext(), ArrayRef<Attribute>{Attribute::get(
14252974856aSPeter Collingbourne                               M.getContext(), Attribute::Nest)}));
14262974856aSPeter Collingbourne       for (unsigned I = 0; I + 2 <  Attrs.getNumAttrSets(); ++I)
142780ea2bb5SArthur Eubanks         NewArgAttrs.push_back(Attrs.getParamAttrs(I));
1428cea6f4d5SMircea Trofin       NewCS->setAttributes(
142980ea2bb5SArthur Eubanks           AttributeList::get(M.getContext(), Attrs.getFnAttrs(),
143080ea2bb5SArthur Eubanks                              Attrs.getRetAttrs(), NewArgAttrs));
14312974856aSPeter Collingbourne 
1432cea6f4d5SMircea Trofin       CB.replaceAllUsesWith(NewCS);
1433cea6f4d5SMircea Trofin       CB.eraseFromParent();
14342974856aSPeter Collingbourne 
14352974856aSPeter Collingbourne       // This use is no longer unsafe.
14362974856aSPeter Collingbourne       if (VCallSite.NumUnsafeUses)
14372974856aSPeter Collingbourne         --*VCallSite.NumUnsafeUses;
14382974856aSPeter Collingbourne     }
14392974856aSPeter Collingbourne     // Don't mark as devirtualized because there may be callers compiled without
14402974856aSPeter Collingbourne     // retpoline mitigation, which would mean that they are lowered to
14412974856aSPeter Collingbourne     // llvm.type.test and therefore require an llvm.type.test resolution for the
14422974856aSPeter Collingbourne     // type identifier.
14432974856aSPeter Collingbourne   };
14442974856aSPeter Collingbourne   Apply(SlotInfo.CSInfo);
14452974856aSPeter Collingbourne   for (auto &P : SlotInfo.ConstCSInfo)
14462974856aSPeter Collingbourne     Apply(P.second);
14472974856aSPeter Collingbourne }
14482974856aSPeter Collingbourne 
tryEvaluateFunctionsWithArgs(MutableArrayRef<VirtualCallTarget> TargetsForSlot,ArrayRef<uint64_t> Args)1449df49d1bbSPeter Collingbourne bool DevirtModule::tryEvaluateFunctionsWithArgs(
1450df49d1bbSPeter Collingbourne     MutableArrayRef<VirtualCallTarget> TargetsForSlot,
145150cbd7ccSPeter Collingbourne     ArrayRef<uint64_t> Args) {
1452df49d1bbSPeter Collingbourne   // Evaluate each function and store the result in each target's RetVal
1453df49d1bbSPeter Collingbourne   // field.
1454df49d1bbSPeter Collingbourne   for (VirtualCallTarget &Target : TargetsForSlot) {
1455df49d1bbSPeter Collingbourne     if (Target.Fn->arg_size() != Args.size() + 1)
1456df49d1bbSPeter Collingbourne       return false;
1457df49d1bbSPeter Collingbourne 
1458df49d1bbSPeter Collingbourne     Evaluator Eval(M.getDataLayout(), nullptr);
1459df49d1bbSPeter Collingbourne     SmallVector<Constant *, 2> EvalArgs;
1460df49d1bbSPeter Collingbourne     EvalArgs.push_back(
1461df49d1bbSPeter Collingbourne         Constant::getNullValue(Target.Fn->getFunctionType()->getParamType(0)));
146250cbd7ccSPeter Collingbourne     for (unsigned I = 0; I != Args.size(); ++I) {
146350cbd7ccSPeter Collingbourne       auto *ArgTy = dyn_cast<IntegerType>(
146450cbd7ccSPeter Collingbourne           Target.Fn->getFunctionType()->getParamType(I + 1));
146550cbd7ccSPeter Collingbourne       if (!ArgTy)
146650cbd7ccSPeter Collingbourne         return false;
146750cbd7ccSPeter Collingbourne       EvalArgs.push_back(ConstantInt::get(ArgTy, Args[I]));
146850cbd7ccSPeter Collingbourne     }
146950cbd7ccSPeter Collingbourne 
1470df49d1bbSPeter Collingbourne     Constant *RetVal;
1471df49d1bbSPeter Collingbourne     if (!Eval.EvaluateFunction(Target.Fn, RetVal, EvalArgs) ||
1472df49d1bbSPeter Collingbourne         !isa<ConstantInt>(RetVal))
1473df49d1bbSPeter Collingbourne       return false;
1474df49d1bbSPeter Collingbourne     Target.RetVal = cast<ConstantInt>(RetVal)->getZExtValue();
1475df49d1bbSPeter Collingbourne   }
1476df49d1bbSPeter Collingbourne   return true;
1477df49d1bbSPeter Collingbourne }
1478df49d1bbSPeter Collingbourne 
applyUniformRetValOpt(CallSiteInfo & CSInfo,StringRef FnName,uint64_t TheRetVal)147950cbd7ccSPeter Collingbourne void DevirtModule::applyUniformRetValOpt(CallSiteInfo &CSInfo, StringRef FnName,
148050cbd7ccSPeter Collingbourne                                          uint64_t TheRetVal) {
14817110510eSArthur Eubanks   for (auto Call : CSInfo.CallSites) {
14827110510eSArthur Eubanks     if (!OptimizedCalls.insert(&Call.CB).second)
14837110510eSArthur Eubanks       continue;
1484ced9a795STeresa Johnson     NumUniformRetVal++;
148550cbd7ccSPeter Collingbourne     Call.replaceAndErase(
1486e963c89dSSam Elliott         "uniform-ret-val", FnName, RemarksEnabled, OREGetter,
1487cea6f4d5SMircea Trofin         ConstantInt::get(cast<IntegerType>(Call.CB.getType()), TheRetVal));
14887110510eSArthur Eubanks   }
148959675ba0SPeter Collingbourne   CSInfo.markDevirt();
149050cbd7ccSPeter Collingbourne }
149150cbd7ccSPeter Collingbourne 
tryUniformRetValOpt(MutableArrayRef<VirtualCallTarget> TargetsForSlot,CallSiteInfo & CSInfo,WholeProgramDevirtResolution::ByArg * Res)1492df49d1bbSPeter Collingbourne bool DevirtModule::tryUniformRetValOpt(
149377a8d563SPeter Collingbourne     MutableArrayRef<VirtualCallTarget> TargetsForSlot, CallSiteInfo &CSInfo,
149477a8d563SPeter Collingbourne     WholeProgramDevirtResolution::ByArg *Res) {
1495df49d1bbSPeter Collingbourne   // Uniform return value optimization. If all functions return the same
1496df49d1bbSPeter Collingbourne   // constant, replace all calls with that constant.
1497df49d1bbSPeter Collingbourne   uint64_t TheRetVal = TargetsForSlot[0].RetVal;
1498df49d1bbSPeter Collingbourne   for (const VirtualCallTarget &Target : TargetsForSlot)
1499df49d1bbSPeter Collingbourne     if (Target.RetVal != TheRetVal)
1500df49d1bbSPeter Collingbourne       return false;
1501df49d1bbSPeter Collingbourne 
150277a8d563SPeter Collingbourne   if (CSInfo.isExported()) {
150377a8d563SPeter Collingbourne     Res->TheKind = WholeProgramDevirtResolution::ByArg::UniformRetVal;
150477a8d563SPeter Collingbourne     Res->Info = TheRetVal;
150577a8d563SPeter Collingbourne   }
150677a8d563SPeter Collingbourne 
150750cbd7ccSPeter Collingbourne   applyUniformRetValOpt(CSInfo, TargetsForSlot[0].Fn->getName(), TheRetVal);
1508ced9a795STeresa Johnson   if (RemarksEnabled || AreStatisticsEnabled())
1509f3403fd2SIvan Krasin     for (auto &&Target : TargetsForSlot)
1510f3403fd2SIvan Krasin       Target.WasDevirt = true;
1511df49d1bbSPeter Collingbourne   return true;
1512df49d1bbSPeter Collingbourne }
1513df49d1bbSPeter Collingbourne 
getGlobalName(VTableSlot Slot,ArrayRef<uint64_t> Args,StringRef Name)151459675ba0SPeter Collingbourne std::string DevirtModule::getGlobalName(VTableSlot Slot,
151559675ba0SPeter Collingbourne                                         ArrayRef<uint64_t> Args,
151659675ba0SPeter Collingbourne                                         StringRef Name) {
151759675ba0SPeter Collingbourne   std::string FullName = "__typeid_";
151859675ba0SPeter Collingbourne   raw_string_ostream OS(FullName);
151959675ba0SPeter Collingbourne   OS << cast<MDString>(Slot.TypeID)->getString() << '_' << Slot.ByteOffset;
152059675ba0SPeter Collingbourne   for (uint64_t Arg : Args)
152159675ba0SPeter Collingbourne     OS << '_' << Arg;
152259675ba0SPeter Collingbourne   OS << '_' << Name;
152359675ba0SPeter Collingbourne   return OS.str();
152459675ba0SPeter Collingbourne }
152559675ba0SPeter Collingbourne 
shouldExportConstantsAsAbsoluteSymbols()1526b15a35e6SPeter Collingbourne bool DevirtModule::shouldExportConstantsAsAbsoluteSymbols() {
1527b15a35e6SPeter Collingbourne   Triple T(M.getTargetTriple());
15286904cd94SFangrui Song   return T.isX86() && T.getObjectFormat() == Triple::ELF;
1529b15a35e6SPeter Collingbourne }
1530b15a35e6SPeter Collingbourne 
exportGlobal(VTableSlot Slot,ArrayRef<uint64_t> Args,StringRef Name,Constant * C)153159675ba0SPeter Collingbourne void DevirtModule::exportGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args,
153259675ba0SPeter Collingbourne                                 StringRef Name, Constant *C) {
153359675ba0SPeter Collingbourne   GlobalAlias *GA = GlobalAlias::create(Int8Ty, 0, GlobalValue::ExternalLinkage,
153459675ba0SPeter Collingbourne                                         getGlobalName(Slot, Args, Name), C, &M);
153559675ba0SPeter Collingbourne   GA->setVisibility(GlobalValue::HiddenVisibility);
153659675ba0SPeter Collingbourne }
153759675ba0SPeter Collingbourne 
exportConstant(VTableSlot Slot,ArrayRef<uint64_t> Args,StringRef Name,uint32_t Const,uint32_t & Storage)1538b15a35e6SPeter Collingbourne void DevirtModule::exportConstant(VTableSlot Slot, ArrayRef<uint64_t> Args,
1539b15a35e6SPeter Collingbourne                                   StringRef Name, uint32_t Const,
1540b15a35e6SPeter Collingbourne                                   uint32_t &Storage) {
1541b15a35e6SPeter Collingbourne   if (shouldExportConstantsAsAbsoluteSymbols()) {
1542b15a35e6SPeter Collingbourne     exportGlobal(
1543b15a35e6SPeter Collingbourne         Slot, Args, Name,
1544b15a35e6SPeter Collingbourne         ConstantExpr::getIntToPtr(ConstantInt::get(Int32Ty, Const), Int8PtrTy));
1545b15a35e6SPeter Collingbourne     return;
1546b15a35e6SPeter Collingbourne   }
1547b15a35e6SPeter Collingbourne 
1548b15a35e6SPeter Collingbourne   Storage = Const;
1549b15a35e6SPeter Collingbourne }
1550b15a35e6SPeter Collingbourne 
importGlobal(VTableSlot Slot,ArrayRef<uint64_t> Args,StringRef Name)155159675ba0SPeter Collingbourne Constant *DevirtModule::importGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args,
1552b15a35e6SPeter Collingbourne                                      StringRef Name) {
1553cc5c5888SBob Haarman   Constant *C =
1554cc5c5888SBob Haarman       M.getOrInsertGlobal(getGlobalName(Slot, Args, Name), Int8Arr0Ty);
155559675ba0SPeter Collingbourne   auto *GV = dyn_cast<GlobalVariable>(C);
1556b15a35e6SPeter Collingbourne   if (GV)
1557b15a35e6SPeter Collingbourne     GV->setVisibility(GlobalValue::HiddenVisibility);
1558b15a35e6SPeter Collingbourne   return C;
1559b15a35e6SPeter Collingbourne }
1560b15a35e6SPeter Collingbourne 
importConstant(VTableSlot Slot,ArrayRef<uint64_t> Args,StringRef Name,IntegerType * IntTy,uint32_t Storage)1561b15a35e6SPeter Collingbourne Constant *DevirtModule::importConstant(VTableSlot Slot, ArrayRef<uint64_t> Args,
1562b15a35e6SPeter Collingbourne                                        StringRef Name, IntegerType *IntTy,
1563b15a35e6SPeter Collingbourne                                        uint32_t Storage) {
1564b15a35e6SPeter Collingbourne   if (!shouldExportConstantsAsAbsoluteSymbols())
1565b15a35e6SPeter Collingbourne     return ConstantInt::get(IntTy, Storage);
1566b15a35e6SPeter Collingbourne 
1567b15a35e6SPeter Collingbourne   Constant *C = importGlobal(Slot, Args, Name);
1568b15a35e6SPeter Collingbourne   auto *GV = cast<GlobalVariable>(C->stripPointerCasts());
1569b15a35e6SPeter Collingbourne   C = ConstantExpr::getPtrToInt(C, IntTy);
1570b15a35e6SPeter Collingbourne 
157114dcf02fSPeter Collingbourne   // We only need to set metadata if the global is newly created, in which
157214dcf02fSPeter Collingbourne   // case it would not have hidden visibility.
15730deb9a9aSBenjamin Kramer   if (GV->hasMetadata(LLVMContext::MD_absolute_symbol))
157459675ba0SPeter Collingbourne     return C;
157514dcf02fSPeter Collingbourne 
157614dcf02fSPeter Collingbourne   auto SetAbsRange = [&](uint64_t Min, uint64_t Max) {
157714dcf02fSPeter Collingbourne     auto *MinC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Min));
157814dcf02fSPeter Collingbourne     auto *MaxC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Max));
157914dcf02fSPeter Collingbourne     GV->setMetadata(LLVMContext::MD_absolute_symbol,
158014dcf02fSPeter Collingbourne                     MDNode::get(M.getContext(), {MinC, MaxC}));
158114dcf02fSPeter Collingbourne   };
1582b15a35e6SPeter Collingbourne   unsigned AbsWidth = IntTy->getBitWidth();
158314dcf02fSPeter Collingbourne   if (AbsWidth == IntPtrTy->getBitWidth())
158414dcf02fSPeter Collingbourne     SetAbsRange(~0ull, ~0ull); // Full set.
1585b15a35e6SPeter Collingbourne   else
158614dcf02fSPeter Collingbourne     SetAbsRange(0, 1ull << AbsWidth);
1587b15a35e6SPeter Collingbourne   return C;
158859675ba0SPeter Collingbourne }
158959675ba0SPeter Collingbourne 
applyUniqueRetValOpt(CallSiteInfo & CSInfo,StringRef FnName,bool IsOne,Constant * UniqueMemberAddr)159050cbd7ccSPeter Collingbourne void DevirtModule::applyUniqueRetValOpt(CallSiteInfo &CSInfo, StringRef FnName,
159150cbd7ccSPeter Collingbourne                                         bool IsOne,
159250cbd7ccSPeter Collingbourne                                         Constant *UniqueMemberAddr) {
159350cbd7ccSPeter Collingbourne   for (auto &&Call : CSInfo.CallSites) {
15947110510eSArthur Eubanks     if (!OptimizedCalls.insert(&Call.CB).second)
15957110510eSArthur Eubanks       continue;
1596cea6f4d5SMircea Trofin     IRBuilder<> B(&Call.CB);
1597001052a0SPeter Collingbourne     Value *Cmp =
1598cc5c5888SBob Haarman         B.CreateICmp(IsOne ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE, Call.VTable,
1599cc5c5888SBob Haarman                      B.CreateBitCast(UniqueMemberAddr, Call.VTable->getType()));
1600cea6f4d5SMircea Trofin     Cmp = B.CreateZExt(Cmp, Call.CB.getType());
1601ced9a795STeresa Johnson     NumUniqueRetVal++;
1602e963c89dSSam Elliott     Call.replaceAndErase("unique-ret-val", FnName, RemarksEnabled, OREGetter,
1603e963c89dSSam Elliott                          Cmp);
160450cbd7ccSPeter Collingbourne   }
160559675ba0SPeter Collingbourne   CSInfo.markDevirt();
160650cbd7ccSPeter Collingbourne }
160750cbd7ccSPeter Collingbourne 
getMemberAddr(const TypeMemberInfo * M)16082974856aSPeter Collingbourne Constant *DevirtModule::getMemberAddr(const TypeMemberInfo *M) {
16092974856aSPeter Collingbourne   Constant *C = ConstantExpr::getBitCast(M->Bits->GV, Int8PtrTy);
16102974856aSPeter Collingbourne   return ConstantExpr::getGetElementPtr(Int8Ty, C,
16112974856aSPeter Collingbourne                                         ConstantInt::get(Int64Ty, M->Offset));
16122974856aSPeter Collingbourne }
16132974856aSPeter Collingbourne 
tryUniqueRetValOpt(unsigned BitWidth,MutableArrayRef<VirtualCallTarget> TargetsForSlot,CallSiteInfo & CSInfo,WholeProgramDevirtResolution::ByArg * Res,VTableSlot Slot,ArrayRef<uint64_t> Args)1614df49d1bbSPeter Collingbourne bool DevirtModule::tryUniqueRetValOpt(
1615f3403fd2SIvan Krasin     unsigned BitWidth, MutableArrayRef<VirtualCallTarget> TargetsForSlot,
161659675ba0SPeter Collingbourne     CallSiteInfo &CSInfo, WholeProgramDevirtResolution::ByArg *Res,
161759675ba0SPeter Collingbourne     VTableSlot Slot, ArrayRef<uint64_t> Args) {
1618df49d1bbSPeter Collingbourne   // IsOne controls whether we look for a 0 or a 1.
1619df49d1bbSPeter Collingbourne   auto tryUniqueRetValOptFor = [&](bool IsOne) {
1620cdc71612SEugene Zelenko     const TypeMemberInfo *UniqueMember = nullptr;
1621df49d1bbSPeter Collingbourne     for (const VirtualCallTarget &Target : TargetsForSlot) {
16223866cc5fSPeter Collingbourne       if (Target.RetVal == (IsOne ? 1 : 0)) {
16237efd7506SPeter Collingbourne         if (UniqueMember)
1624df49d1bbSPeter Collingbourne           return false;
16257efd7506SPeter Collingbourne         UniqueMember = Target.TM;
1626df49d1bbSPeter Collingbourne       }
1627df49d1bbSPeter Collingbourne     }
1628df49d1bbSPeter Collingbourne 
16297efd7506SPeter Collingbourne     // We should have found a unique member or bailed out by now. We already
1630df49d1bbSPeter Collingbourne     // checked for a uniform return value in tryUniformRetValOpt.
16317efd7506SPeter Collingbourne     assert(UniqueMember);
1632df49d1bbSPeter Collingbourne 
16332974856aSPeter Collingbourne     Constant *UniqueMemberAddr = getMemberAddr(UniqueMember);
163459675ba0SPeter Collingbourne     if (CSInfo.isExported()) {
163559675ba0SPeter Collingbourne       Res->TheKind = WholeProgramDevirtResolution::ByArg::UniqueRetVal;
163659675ba0SPeter Collingbourne       Res->Info = IsOne;
163759675ba0SPeter Collingbourne 
163859675ba0SPeter Collingbourne       exportGlobal(Slot, Args, "unique_member", UniqueMemberAddr);
163959675ba0SPeter Collingbourne     }
164059675ba0SPeter Collingbourne 
164159675ba0SPeter Collingbourne     // Replace each call with the comparison.
164250cbd7ccSPeter Collingbourne     applyUniqueRetValOpt(CSInfo, TargetsForSlot[0].Fn->getName(), IsOne,
164350cbd7ccSPeter Collingbourne                          UniqueMemberAddr);
164450cbd7ccSPeter Collingbourne 
1645f3403fd2SIvan Krasin     // Update devirtualization statistics for targets.
1646ced9a795STeresa Johnson     if (RemarksEnabled || AreStatisticsEnabled())
1647f3403fd2SIvan Krasin       for (auto &&Target : TargetsForSlot)
1648f3403fd2SIvan Krasin         Target.WasDevirt = true;
1649f3403fd2SIvan Krasin 
1650df49d1bbSPeter Collingbourne     return true;
1651df49d1bbSPeter Collingbourne   };
1652df49d1bbSPeter Collingbourne 
1653df49d1bbSPeter Collingbourne   if (BitWidth == 1) {
1654df49d1bbSPeter Collingbourne     if (tryUniqueRetValOptFor(true))
1655df49d1bbSPeter Collingbourne       return true;
1656df49d1bbSPeter Collingbourne     if (tryUniqueRetValOptFor(false))
1657df49d1bbSPeter Collingbourne       return true;
1658df49d1bbSPeter Collingbourne   }
1659df49d1bbSPeter Collingbourne   return false;
1660df49d1bbSPeter Collingbourne }
1661df49d1bbSPeter Collingbourne 
applyVirtualConstProp(CallSiteInfo & CSInfo,StringRef FnName,Constant * Byte,Constant * Bit)166250cbd7ccSPeter Collingbourne void DevirtModule::applyVirtualConstProp(CallSiteInfo &CSInfo, StringRef FnName,
166350cbd7ccSPeter Collingbourne                                          Constant *Byte, Constant *Bit) {
166450cbd7ccSPeter Collingbourne   for (auto Call : CSInfo.CallSites) {
16657110510eSArthur Eubanks     if (!OptimizedCalls.insert(&Call.CB).second)
16667110510eSArthur Eubanks       continue;
1667cea6f4d5SMircea Trofin     auto *RetType = cast<IntegerType>(Call.CB.getType());
1668cea6f4d5SMircea Trofin     IRBuilder<> B(&Call.CB);
1669001052a0SPeter Collingbourne     Value *Addr =
1670001052a0SPeter Collingbourne         B.CreateGEP(Int8Ty, B.CreateBitCast(Call.VTable, Int8PtrTy), Byte);
167150cbd7ccSPeter Collingbourne     if (RetType->getBitWidth() == 1) {
167214359ef1SJames Y Knight       Value *Bits = B.CreateLoad(Int8Ty, Addr);
167350cbd7ccSPeter Collingbourne       Value *BitsAndBit = B.CreateAnd(Bits, Bit);
167450cbd7ccSPeter Collingbourne       auto IsBitSet = B.CreateICmpNE(BitsAndBit, ConstantInt::get(Int8Ty, 0));
1675ced9a795STeresa Johnson       NumVirtConstProp1Bit++;
167650cbd7ccSPeter Collingbourne       Call.replaceAndErase("virtual-const-prop-1-bit", FnName, RemarksEnabled,
1677e963c89dSSam Elliott                            OREGetter, IsBitSet);
167850cbd7ccSPeter Collingbourne     } else {
167950cbd7ccSPeter Collingbourne       Value *ValAddr = B.CreateBitCast(Addr, RetType->getPointerTo());
168050cbd7ccSPeter Collingbourne       Value *Val = B.CreateLoad(RetType, ValAddr);
1681ced9a795STeresa Johnson       NumVirtConstProp++;
1682e963c89dSSam Elliott       Call.replaceAndErase("virtual-const-prop", FnName, RemarksEnabled,
1683e963c89dSSam Elliott                            OREGetter, Val);
168450cbd7ccSPeter Collingbourne     }
168550cbd7ccSPeter Collingbourne   }
168614dcf02fSPeter Collingbourne   CSInfo.markDevirt();
168750cbd7ccSPeter Collingbourne }
168850cbd7ccSPeter Collingbourne 
tryVirtualConstProp(MutableArrayRef<VirtualCallTarget> TargetsForSlot,VTableSlotInfo & SlotInfo,WholeProgramDevirtResolution * Res,VTableSlot Slot)1689df49d1bbSPeter Collingbourne bool DevirtModule::tryVirtualConstProp(
169059675ba0SPeter Collingbourne     MutableArrayRef<VirtualCallTarget> TargetsForSlot, VTableSlotInfo &SlotInfo,
169159675ba0SPeter Collingbourne     WholeProgramDevirtResolution *Res, VTableSlot Slot) {
1692df49d1bbSPeter Collingbourne   // This only works if the function returns an integer.
1693df49d1bbSPeter Collingbourne   auto RetType = dyn_cast<IntegerType>(TargetsForSlot[0].Fn->getReturnType());
1694df49d1bbSPeter Collingbourne   if (!RetType)
1695df49d1bbSPeter Collingbourne     return false;
1696df49d1bbSPeter Collingbourne   unsigned BitWidth = RetType->getBitWidth();
1697df49d1bbSPeter Collingbourne   if (BitWidth > 64)
1698df49d1bbSPeter Collingbourne     return false;
1699df49d1bbSPeter Collingbourne 
170017febdbbSPeter Collingbourne   // Make sure that each function is defined, does not access memory, takes at
170117febdbbSPeter Collingbourne   // least one argument, does not use its first argument (which we assume is
170217febdbbSPeter Collingbourne   // 'this'), and has the same return type.
170337317f12SPeter Collingbourne   //
170437317f12SPeter Collingbourne   // Note that we test whether this copy of the function is readnone, rather
170537317f12SPeter Collingbourne   // than testing function attributes, which must hold for any copy of the
170637317f12SPeter Collingbourne   // function, even a less optimized version substituted at link time. This is
170737317f12SPeter Collingbourne   // sound because the virtual constant propagation optimizations effectively
170837317f12SPeter Collingbourne   // inline all implementations of the virtual function into each call site,
170937317f12SPeter Collingbourne   // rather than using function attributes to perform local optimization.
1710df49d1bbSPeter Collingbourne   for (VirtualCallTarget &Target : TargetsForSlot) {
171137317f12SPeter Collingbourne     if (Target.Fn->isDeclaration() ||
171237317f12SPeter Collingbourne         computeFunctionBodyMemoryAccess(*Target.Fn, AARGetter(*Target.Fn)) !=
1713014f5bcfSFlorian Hahn             FMRB_DoesNotAccessMemory ||
171417febdbbSPeter Collingbourne         Target.Fn->arg_empty() || !Target.Fn->arg_begin()->use_empty() ||
1715df49d1bbSPeter Collingbourne         Target.Fn->getReturnType() != RetType)
1716df49d1bbSPeter Collingbourne       return false;
1717df49d1bbSPeter Collingbourne   }
1718df49d1bbSPeter Collingbourne 
171950cbd7ccSPeter Collingbourne   for (auto &&CSByConstantArg : SlotInfo.ConstCSInfo) {
1720df49d1bbSPeter Collingbourne     if (!tryEvaluateFunctionsWithArgs(TargetsForSlot, CSByConstantArg.first))
1721df49d1bbSPeter Collingbourne       continue;
1722df49d1bbSPeter Collingbourne 
172377a8d563SPeter Collingbourne     WholeProgramDevirtResolution::ByArg *ResByArg = nullptr;
172477a8d563SPeter Collingbourne     if (Res)
172577a8d563SPeter Collingbourne       ResByArg = &Res->ResByArg[CSByConstantArg.first];
172677a8d563SPeter Collingbourne 
172777a8d563SPeter Collingbourne     if (tryUniformRetValOpt(TargetsForSlot, CSByConstantArg.second, ResByArg))
1728df49d1bbSPeter Collingbourne       continue;
1729df49d1bbSPeter Collingbourne 
173059675ba0SPeter Collingbourne     if (tryUniqueRetValOpt(BitWidth, TargetsForSlot, CSByConstantArg.second,
173159675ba0SPeter Collingbourne                            ResByArg, Slot, CSByConstantArg.first))
1732df49d1bbSPeter Collingbourne       continue;
1733df49d1bbSPeter Collingbourne 
17347efd7506SPeter Collingbourne     // Find an allocation offset in bits in all vtables associated with the
17357efd7506SPeter Collingbourne     // type.
1736df49d1bbSPeter Collingbourne     uint64_t AllocBefore =
1737df49d1bbSPeter Collingbourne         findLowestOffset(TargetsForSlot, /*IsAfter=*/false, BitWidth);
1738df49d1bbSPeter Collingbourne     uint64_t AllocAfter =
1739df49d1bbSPeter Collingbourne         findLowestOffset(TargetsForSlot, /*IsAfter=*/true, BitWidth);
1740df49d1bbSPeter Collingbourne 
1741df49d1bbSPeter Collingbourne     // Calculate the total amount of padding needed to store a value at both
1742df49d1bbSPeter Collingbourne     // ends of the object.
1743df49d1bbSPeter Collingbourne     uint64_t TotalPaddingBefore = 0, TotalPaddingAfter = 0;
1744df49d1bbSPeter Collingbourne     for (auto &&Target : TargetsForSlot) {
1745df49d1bbSPeter Collingbourne       TotalPaddingBefore += std::max<int64_t>(
1746df49d1bbSPeter Collingbourne           (AllocBefore + 7) / 8 - Target.allocatedBeforeBytes() - 1, 0);
1747df49d1bbSPeter Collingbourne       TotalPaddingAfter += std::max<int64_t>(
1748df49d1bbSPeter Collingbourne           (AllocAfter + 7) / 8 - Target.allocatedAfterBytes() - 1, 0);
1749df49d1bbSPeter Collingbourne     }
1750df49d1bbSPeter Collingbourne 
1751df49d1bbSPeter Collingbourne     // If the amount of padding is too large, give up.
1752df49d1bbSPeter Collingbourne     // FIXME: do something smarter here.
1753df49d1bbSPeter Collingbourne     if (std::min(TotalPaddingBefore, TotalPaddingAfter) > 128)
1754df49d1bbSPeter Collingbourne       continue;
1755df49d1bbSPeter Collingbourne 
1756df49d1bbSPeter Collingbourne     // Calculate the offset to the value as a (possibly negative) byte offset
1757df49d1bbSPeter Collingbourne     // and (if applicable) a bit offset, and store the values in the targets.
1758df49d1bbSPeter Collingbourne     int64_t OffsetByte;
1759df49d1bbSPeter Collingbourne     uint64_t OffsetBit;
1760df49d1bbSPeter Collingbourne     if (TotalPaddingBefore <= TotalPaddingAfter)
1761df49d1bbSPeter Collingbourne       setBeforeReturnValues(TargetsForSlot, AllocBefore, BitWidth, OffsetByte,
1762df49d1bbSPeter Collingbourne                             OffsetBit);
1763df49d1bbSPeter Collingbourne     else
1764df49d1bbSPeter Collingbourne       setAfterReturnValues(TargetsForSlot, AllocAfter, BitWidth, OffsetByte,
1765df49d1bbSPeter Collingbourne                            OffsetBit);
1766df49d1bbSPeter Collingbourne 
1767ced9a795STeresa Johnson     if (RemarksEnabled || AreStatisticsEnabled())
1768f3403fd2SIvan Krasin       for (auto &&Target : TargetsForSlot)
1769f3403fd2SIvan Krasin         Target.WasDevirt = true;
1770f3403fd2SIvan Krasin 
177114dcf02fSPeter Collingbourne 
177214dcf02fSPeter Collingbourne     if (CSByConstantArg.second.isExported()) {
177314dcf02fSPeter Collingbourne       ResByArg->TheKind = WholeProgramDevirtResolution::ByArg::VirtualConstProp;
1774b15a35e6SPeter Collingbourne       exportConstant(Slot, CSByConstantArg.first, "byte", OffsetByte,
1775b15a35e6SPeter Collingbourne                      ResByArg->Byte);
1776b15a35e6SPeter Collingbourne       exportConstant(Slot, CSByConstantArg.first, "bit", 1ULL << OffsetBit,
1777b15a35e6SPeter Collingbourne                      ResByArg->Bit);
177814dcf02fSPeter Collingbourne     }
177914dcf02fSPeter Collingbourne 
178014dcf02fSPeter Collingbourne     // Rewrite each call to a load from OffsetByte/OffsetBit.
1781b15a35e6SPeter Collingbourne     Constant *ByteConst = ConstantInt::get(Int32Ty, OffsetByte);
1782b15a35e6SPeter Collingbourne     Constant *BitConst = ConstantInt::get(Int8Ty, 1ULL << OffsetBit);
178350cbd7ccSPeter Collingbourne     applyVirtualConstProp(CSByConstantArg.second,
178450cbd7ccSPeter Collingbourne                           TargetsForSlot[0].Fn->getName(), ByteConst, BitConst);
1785df49d1bbSPeter Collingbourne   }
1786df49d1bbSPeter Collingbourne   return true;
1787df49d1bbSPeter Collingbourne }
1788df49d1bbSPeter Collingbourne 
rebuildGlobal(VTableBits & B)1789df49d1bbSPeter Collingbourne void DevirtModule::rebuildGlobal(VTableBits &B) {
1790df49d1bbSPeter Collingbourne   if (B.Before.Bytes.empty() && B.After.Bytes.empty())
1791df49d1bbSPeter Collingbourne     return;
1792df49d1bbSPeter Collingbourne 
1793ef5cfc2dSPeter Collingbourne   // Align the before byte array to the global's minimum alignment so that we
1794ef5cfc2dSPeter Collingbourne   // don't break any alignment requirements on the global.
1795d3085c25SGuillaume Chatelet   Align Alignment = M.getDataLayout().getValueOrABITypeAlignment(
1796d3085c25SGuillaume Chatelet       B.GV->getAlign(), B.GV->getValueType());
17970e62011dSGuillaume Chatelet   B.Before.Bytes.resize(alignTo(B.Before.Bytes.size(), Alignment));
1798df49d1bbSPeter Collingbourne 
1799df49d1bbSPeter Collingbourne   // Before was stored in reverse order; flip it now.
1800df49d1bbSPeter Collingbourne   for (size_t I = 0, Size = B.Before.Bytes.size(); I != Size / 2; ++I)
1801df49d1bbSPeter Collingbourne     std::swap(B.Before.Bytes[I], B.Before.Bytes[Size - 1 - I]);
1802df49d1bbSPeter Collingbourne 
1803df49d1bbSPeter Collingbourne   // Build an anonymous global containing the before bytes, followed by the
1804df49d1bbSPeter Collingbourne   // original initializer, followed by the after bytes.
1805df49d1bbSPeter Collingbourne   auto NewInit = ConstantStruct::getAnon(
1806df49d1bbSPeter Collingbourne       {ConstantDataArray::get(M.getContext(), B.Before.Bytes),
1807df49d1bbSPeter Collingbourne        B.GV->getInitializer(),
1808df49d1bbSPeter Collingbourne        ConstantDataArray::get(M.getContext(), B.After.Bytes)});
1809df49d1bbSPeter Collingbourne   auto NewGV =
1810df49d1bbSPeter Collingbourne       new GlobalVariable(M, NewInit->getType(), B.GV->isConstant(),
1811df49d1bbSPeter Collingbourne                          GlobalVariable::PrivateLinkage, NewInit, "", B.GV);
1812df49d1bbSPeter Collingbourne   NewGV->setSection(B.GV->getSection());
1813df49d1bbSPeter Collingbourne   NewGV->setComdat(B.GV->getComdat());
18141172712fSArthur Eubanks   NewGV->setAlignment(B.GV->getAlign());
1815df49d1bbSPeter Collingbourne 
18160312f614SPeter Collingbourne   // Copy the original vtable's metadata to the anonymous global, adjusting
18170312f614SPeter Collingbourne   // offsets as required.
18180312f614SPeter Collingbourne   NewGV->copyMetadata(B.GV, B.Before.Bytes.size());
18190312f614SPeter Collingbourne 
1820df49d1bbSPeter Collingbourne   // Build an alias named after the original global, pointing at the second
1821df49d1bbSPeter Collingbourne   // element (the original initializer).
1822df49d1bbSPeter Collingbourne   auto Alias = GlobalAlias::create(
1823df49d1bbSPeter Collingbourne       B.GV->getInitializer()->getType(), 0, B.GV->getLinkage(), "",
1824df49d1bbSPeter Collingbourne       ConstantExpr::getGetElementPtr(
1825df49d1bbSPeter Collingbourne           NewInit->getType(), NewGV,
1826df49d1bbSPeter Collingbourne           ArrayRef<Constant *>{ConstantInt::get(Int32Ty, 0),
1827df49d1bbSPeter Collingbourne                                ConstantInt::get(Int32Ty, 1)}),
1828df49d1bbSPeter Collingbourne       &M);
1829df49d1bbSPeter Collingbourne   Alias->setVisibility(B.GV->getVisibility());
1830df49d1bbSPeter Collingbourne   Alias->takeName(B.GV);
1831df49d1bbSPeter Collingbourne 
1832df49d1bbSPeter Collingbourne   B.GV->replaceAllUsesWith(Alias);
1833df49d1bbSPeter Collingbourne   B.GV->eraseFromParent();
1834df49d1bbSPeter Collingbourne }
1835df49d1bbSPeter Collingbourne 
areRemarksEnabled()1836f3403fd2SIvan Krasin bool DevirtModule::areRemarksEnabled() {
1837f3403fd2SIvan Krasin   const auto &FL = M.getFunctionList();
18385e1c0e76STeresa Johnson   for (const Function &Fn : FL) {
1839de53bfb9SAdam Nemet     const auto &BBL = Fn.getBasicBlockList();
1840de53bfb9SAdam Nemet     if (BBL.empty())
18415e1c0e76STeresa Johnson       continue;
1842de53bfb9SAdam Nemet     auto DI = OptimizationRemark(DEBUG_TYPE, "", DebugLoc(), &BBL.front());
1843f3403fd2SIvan Krasin     return DI.isEnabled();
1844f3403fd2SIvan Krasin   }
18455e1c0e76STeresa Johnson   return false;
18465e1c0e76STeresa Johnson }
1847f3403fd2SIvan Krasin 
scanTypeTestUsers(Function * TypeTestFunc,DenseMap<Metadata *,std::set<TypeMemberInfo>> & TypeIdMap)18486014c46cSTeresa Johnson void DevirtModule::scanTypeTestUsers(
18496014c46cSTeresa Johnson     Function *TypeTestFunc,
18506014c46cSTeresa Johnson     DenseMap<Metadata *, std::set<TypeMemberInfo>> &TypeIdMap) {
1851df49d1bbSPeter Collingbourne   // Find all virtual calls via a virtual table pointer %p under an assumption
18527efd7506SPeter Collingbourne   // of the form llvm.assume(llvm.type.test(%p, %md)). This indicates that %p
18537efd7506SPeter Collingbourne   // points to a member of the type identifier %md. Group calls by (type ID,
18547efd7506SPeter Collingbourne   // offset) pair (effectively the identity of the virtual function) and store
18557efd7506SPeter Collingbourne   // to CallSlots.
18560d182d9dSKazu Hirata   for (Use &U : llvm::make_early_inc_range(TypeTestFunc->uses())) {
18570d182d9dSKazu Hirata     auto *CI = dyn_cast<CallInst>(U.getUser());
1858df49d1bbSPeter Collingbourne     if (!CI)
1859df49d1bbSPeter Collingbourne       continue;
1860df49d1bbSPeter Collingbourne 
1861ccdc225cSPeter Collingbourne     // Search for virtual calls based on %p and add them to DevirtCalls.
1862ccdc225cSPeter Collingbourne     SmallVector<DevirtCallSite, 1> DevirtCalls;
1863df49d1bbSPeter Collingbourne     SmallVector<CallInst *, 1> Assumes;
1864f24136f1STeresa Johnson     auto &DT = LookupDomTree(*CI->getFunction());
1865f24136f1STeresa Johnson     findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI, DT);
1866df49d1bbSPeter Collingbourne 
186780bf137fSTeresa Johnson     Metadata *TypeId =
186880bf137fSTeresa Johnson         cast<MetadataAsValue>(CI->getArgOperand(1))->getMetadata();
18696014c46cSTeresa Johnson     // If we found any, add them to CallSlots.
18706014c46cSTeresa Johnson     if (!Assumes.empty()) {
1871df49d1bbSPeter Collingbourne       Value *Ptr = CI->getArgOperand(0)->stripPointerCasts();
1872d291bd51STeresa Johnson       for (DevirtCallSite Call : DevirtCalls)
1873cea6f4d5SMircea Trofin         CallSlots[{TypeId, Call.Offset}].addCallSite(Ptr, Call.CB, nullptr);
1874ccdc225cSPeter Collingbourne     }
1875df49d1bbSPeter Collingbourne 
18766014c46cSTeresa Johnson     auto RemoveTypeTestAssumes = [&]() {
18777efd7506SPeter Collingbourne       // We no longer need the assumes or the type test.
1878df49d1bbSPeter Collingbourne       for (auto Assume : Assumes)
1879df49d1bbSPeter Collingbourne         Assume->eraseFromParent();
1880df49d1bbSPeter Collingbourne       // We can't use RecursivelyDeleteTriviallyDeadInstructions here because we
1881df49d1bbSPeter Collingbourne       // may use the vtable argument later.
1882df49d1bbSPeter Collingbourne       if (CI->use_empty())
1883df49d1bbSPeter Collingbourne         CI->eraseFromParent();
18846014c46cSTeresa Johnson     };
18856014c46cSTeresa Johnson 
18866014c46cSTeresa Johnson     // At this point we could remove all type test assume sequences, as they
18876014c46cSTeresa Johnson     // were originally inserted for WPD. However, we can keep these in the
18886014c46cSTeresa Johnson     // code stream for later analysis (e.g. to help drive more efficient ICP
18896014c46cSTeresa Johnson     // sequences). They will eventually be removed by a second LowerTypeTests
18906014c46cSTeresa Johnson     // invocation that cleans them up. In order to do this correctly, the first
18916014c46cSTeresa Johnson     // LowerTypeTests invocation needs to know that they have "Unknown" type
18926014c46cSTeresa Johnson     // test resolution, so that they aren't treated as Unsat and lowered to
18936014c46cSTeresa Johnson     // False, which will break any uses on assumes. Below we remove any type
18946014c46cSTeresa Johnson     // test assumes that will not be treated as Unknown by LTT.
18956014c46cSTeresa Johnson 
18966014c46cSTeresa Johnson     // The type test assumes will be treated by LTT as Unsat if the type id is
18976014c46cSTeresa Johnson     // not used on a global (in which case it has no entry in the TypeIdMap).
18986014c46cSTeresa Johnson     if (!TypeIdMap.count(TypeId))
18996014c46cSTeresa Johnson       RemoveTypeTestAssumes();
19006014c46cSTeresa Johnson 
19016014c46cSTeresa Johnson     // For ThinLTO importing, we need to remove the type test assumes if this is
19026014c46cSTeresa Johnson     // an MDString type id without a corresponding TypeIdSummary. Any
19036014c46cSTeresa Johnson     // non-MDString type ids are ignored and treated as Unknown by LTT, so their
19046014c46cSTeresa Johnson     // type test assumes can be kept. If the MDString type id is missing a
19056014c46cSTeresa Johnson     // TypeIdSummary (e.g. because there was no use on a vcall, preventing the
19066014c46cSTeresa Johnson     // exporting phase of WPD from analyzing it), then it would be treated as
19076014c46cSTeresa Johnson     // Unsat by LTT and we need to remove its type test assumes here. If not
19086014c46cSTeresa Johnson     // used on a vcall we don't need them for later optimization use in any
19096014c46cSTeresa Johnson     // case.
19106014c46cSTeresa Johnson     else if (ImportSummary && isa<MDString>(TypeId)) {
19116014c46cSTeresa Johnson       const TypeIdSummary *TidSummary =
19126014c46cSTeresa Johnson           ImportSummary->getTypeIdSummary(cast<MDString>(TypeId)->getString());
19136014c46cSTeresa Johnson       if (!TidSummary)
19146014c46cSTeresa Johnson         RemoveTypeTestAssumes();
19156014c46cSTeresa Johnson       else
19166014c46cSTeresa Johnson         // If one was created it should not be Unsat, because if we reached here
19176014c46cSTeresa Johnson         // the type id was used on a global.
19186014c46cSTeresa Johnson         assert(TidSummary->TTRes.TheKind != TypeTestResolution::Unsat);
19196014c46cSTeresa Johnson     }
1920df49d1bbSPeter Collingbourne   }
19210312f614SPeter Collingbourne }
19220312f614SPeter Collingbourne 
scanTypeCheckedLoadUsers(Function * TypeCheckedLoadFunc)19230312f614SPeter Collingbourne void DevirtModule::scanTypeCheckedLoadUsers(Function *TypeCheckedLoadFunc) {
19240312f614SPeter Collingbourne   Function *TypeTestFunc = Intrinsic::getDeclaration(&M, Intrinsic::type_test);
19250312f614SPeter Collingbourne 
19260d182d9dSKazu Hirata   for (Use &U : llvm::make_early_inc_range(TypeCheckedLoadFunc->uses())) {
19270d182d9dSKazu Hirata     auto *CI = dyn_cast<CallInst>(U.getUser());
19280312f614SPeter Collingbourne     if (!CI)
19290312f614SPeter Collingbourne       continue;
19300312f614SPeter Collingbourne 
19310312f614SPeter Collingbourne     Value *Ptr = CI->getArgOperand(0);
19320312f614SPeter Collingbourne     Value *Offset = CI->getArgOperand(1);
19330312f614SPeter Collingbourne     Value *TypeIdValue = CI->getArgOperand(2);
19340312f614SPeter Collingbourne     Metadata *TypeId = cast<MetadataAsValue>(TypeIdValue)->getMetadata();
19350312f614SPeter Collingbourne 
19360312f614SPeter Collingbourne     SmallVector<DevirtCallSite, 1> DevirtCalls;
19370312f614SPeter Collingbourne     SmallVector<Instruction *, 1> LoadedPtrs;
19380312f614SPeter Collingbourne     SmallVector<Instruction *, 1> Preds;
19390312f614SPeter Collingbourne     bool HasNonCallUses = false;
1940f24136f1STeresa Johnson     auto &DT = LookupDomTree(*CI->getFunction());
19410312f614SPeter Collingbourne     findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds,
1942f24136f1STeresa Johnson                                                HasNonCallUses, CI, DT);
19430312f614SPeter Collingbourne 
19440312f614SPeter Collingbourne     // Start by generating "pessimistic" code that explicitly loads the function
19450312f614SPeter Collingbourne     // pointer from the vtable and performs the type check. If possible, we will
19460312f614SPeter Collingbourne     // eliminate the load and the type check later.
19470312f614SPeter Collingbourne 
19480312f614SPeter Collingbourne     // If possible, only generate the load at the point where it is used.
19490312f614SPeter Collingbourne     // This helps avoid unnecessary spills.
19500312f614SPeter Collingbourne     IRBuilder<> LoadB(
19510312f614SPeter Collingbourne         (LoadedPtrs.size() == 1 && !HasNonCallUses) ? LoadedPtrs[0] : CI);
19520312f614SPeter Collingbourne     Value *GEP = LoadB.CreateGEP(Int8Ty, Ptr, Offset);
19530312f614SPeter Collingbourne     Value *GEPPtr = LoadB.CreateBitCast(GEP, PointerType::getUnqual(Int8PtrTy));
19540312f614SPeter Collingbourne     Value *LoadedValue = LoadB.CreateLoad(Int8PtrTy, GEPPtr);
19550312f614SPeter Collingbourne 
19560312f614SPeter Collingbourne     for (Instruction *LoadedPtr : LoadedPtrs) {
19570312f614SPeter Collingbourne       LoadedPtr->replaceAllUsesWith(LoadedValue);
19580312f614SPeter Collingbourne       LoadedPtr->eraseFromParent();
19590312f614SPeter Collingbourne     }
19600312f614SPeter Collingbourne 
19610312f614SPeter Collingbourne     // Likewise for the type test.
19620312f614SPeter Collingbourne     IRBuilder<> CallB((Preds.size() == 1 && !HasNonCallUses) ? Preds[0] : CI);
19630312f614SPeter Collingbourne     CallInst *TypeTestCall = CallB.CreateCall(TypeTestFunc, {Ptr, TypeIdValue});
19640312f614SPeter Collingbourne 
19650312f614SPeter Collingbourne     for (Instruction *Pred : Preds) {
19660312f614SPeter Collingbourne       Pred->replaceAllUsesWith(TypeTestCall);
19670312f614SPeter Collingbourne       Pred->eraseFromParent();
19680312f614SPeter Collingbourne     }
19690312f614SPeter Collingbourne 
19700312f614SPeter Collingbourne     // We have already erased any extractvalue instructions that refer to the
19710312f614SPeter Collingbourne     // intrinsic call, but the intrinsic may have other non-extractvalue uses
19720312f614SPeter Collingbourne     // (although this is unlikely). In that case, explicitly build a pair and
19730312f614SPeter Collingbourne     // RAUW it.
19740312f614SPeter Collingbourne     if (!CI->use_empty()) {
197553dc0f10SNuno Lopes       Value *Pair = PoisonValue::get(CI->getType());
19760312f614SPeter Collingbourne       IRBuilder<> B(CI);
19770312f614SPeter Collingbourne       Pair = B.CreateInsertValue(Pair, LoadedValue, {0});
19780312f614SPeter Collingbourne       Pair = B.CreateInsertValue(Pair, TypeTestCall, {1});
19790312f614SPeter Collingbourne       CI->replaceAllUsesWith(Pair);
19800312f614SPeter Collingbourne     }
19810312f614SPeter Collingbourne 
19820312f614SPeter Collingbourne     // The number of unsafe uses is initially the number of uses.
19830312f614SPeter Collingbourne     auto &NumUnsafeUses = NumUnsafeUsesForTypeTest[TypeTestCall];
19840312f614SPeter Collingbourne     NumUnsafeUses = DevirtCalls.size();
19850312f614SPeter Collingbourne 
19860312f614SPeter Collingbourne     // If the function pointer has a non-call user, we cannot eliminate the type
19870312f614SPeter Collingbourne     // check, as one of those users may eventually call the pointer. Increment
19880312f614SPeter Collingbourne     // the unsafe use count to make sure it cannot reach zero.
19890312f614SPeter Collingbourne     if (HasNonCallUses)
19900312f614SPeter Collingbourne       ++NumUnsafeUses;
19910312f614SPeter Collingbourne     for (DevirtCallSite Call : DevirtCalls) {
1992cea6f4d5SMircea Trofin       CallSlots[{TypeId, Call.Offset}].addCallSite(Ptr, Call.CB,
199350cbd7ccSPeter Collingbourne                                                    &NumUnsafeUses);
19940312f614SPeter Collingbourne     }
19950312f614SPeter Collingbourne 
19960312f614SPeter Collingbourne     CI->eraseFromParent();
19970312f614SPeter Collingbourne   }
19980312f614SPeter Collingbourne }
19990312f614SPeter Collingbourne 
importResolution(VTableSlot Slot,VTableSlotInfo & SlotInfo)20006d284fabSPeter Collingbourne void DevirtModule::importResolution(VTableSlot Slot, VTableSlotInfo &SlotInfo) {
2001d2df54e6STeresa Johnson   auto *TypeId = dyn_cast<MDString>(Slot.TypeID);
2002d2df54e6STeresa Johnson   if (!TypeId)
2003d2df54e6STeresa Johnson     return;
20049a3f9797SPeter Collingbourne   const TypeIdSummary *TidSummary =
2005d2df54e6STeresa Johnson       ImportSummary->getTypeIdSummary(TypeId->getString());
20069a3f9797SPeter Collingbourne   if (!TidSummary)
20079a3f9797SPeter Collingbourne     return;
20089a3f9797SPeter Collingbourne   auto ResI = TidSummary->WPDRes.find(Slot.ByteOffset);
20099a3f9797SPeter Collingbourne   if (ResI == TidSummary->WPDRes.end())
20109a3f9797SPeter Collingbourne     return;
20119a3f9797SPeter Collingbourne   const WholeProgramDevirtResolution &Res = ResI->second;
20126d284fabSPeter Collingbourne 
20136d284fabSPeter Collingbourne   if (Res.TheKind == WholeProgramDevirtResolution::SingleImpl) {
2014d2df54e6STeresa Johnson     assert(!Res.SingleImplName.empty());
20156d284fabSPeter Collingbourne     // The type of the function in the declaration is irrelevant because every
20166d284fabSPeter Collingbourne     // call site will cast it to the correct type.
201713680223SJames Y Knight     Constant *SingleImpl =
201813680223SJames Y Knight         cast<Constant>(M.getOrInsertFunction(Res.SingleImplName,
201913680223SJames Y Knight                                              Type::getVoidTy(M.getContext()))
202013680223SJames Y Knight                            .getCallee());
20216d284fabSPeter Collingbourne 
20226d284fabSPeter Collingbourne     // This is the import phase so we should not be exporting anything.
20236d284fabSPeter Collingbourne     bool IsExported = false;
20246d284fabSPeter Collingbourne     applySingleImplDevirt(SlotInfo, SingleImpl, IsExported);
20256d284fabSPeter Collingbourne     assert(!IsExported);
20266d284fabSPeter Collingbourne   }
20270152c815SPeter Collingbourne 
20280152c815SPeter Collingbourne   for (auto &CSByConstantArg : SlotInfo.ConstCSInfo) {
20290152c815SPeter Collingbourne     auto I = Res.ResByArg.find(CSByConstantArg.first);
20300152c815SPeter Collingbourne     if (I == Res.ResByArg.end())
20310152c815SPeter Collingbourne       continue;
20320152c815SPeter Collingbourne     auto &ResByArg = I->second;
20330152c815SPeter Collingbourne     // FIXME: We should figure out what to do about the "function name" argument
20340152c815SPeter Collingbourne     // to the apply* functions, as the function names are unavailable during the
20350152c815SPeter Collingbourne     // importing phase. For now we just pass the empty string. This does not
20360152c815SPeter Collingbourne     // impact correctness because the function names are just used for remarks.
20370152c815SPeter Collingbourne     switch (ResByArg.TheKind) {
20380152c815SPeter Collingbourne     case WholeProgramDevirtResolution::ByArg::UniformRetVal:
20390152c815SPeter Collingbourne       applyUniformRetValOpt(CSByConstantArg.second, "", ResByArg.Info);
20400152c815SPeter Collingbourne       break;
204159675ba0SPeter Collingbourne     case WholeProgramDevirtResolution::ByArg::UniqueRetVal: {
204259675ba0SPeter Collingbourne       Constant *UniqueMemberAddr =
204359675ba0SPeter Collingbourne           importGlobal(Slot, CSByConstantArg.first, "unique_member");
204459675ba0SPeter Collingbourne       applyUniqueRetValOpt(CSByConstantArg.second, "", ResByArg.Info,
204559675ba0SPeter Collingbourne                            UniqueMemberAddr);
204659675ba0SPeter Collingbourne       break;
204759675ba0SPeter Collingbourne     }
204814dcf02fSPeter Collingbourne     case WholeProgramDevirtResolution::ByArg::VirtualConstProp: {
2049b15a35e6SPeter Collingbourne       Constant *Byte = importConstant(Slot, CSByConstantArg.first, "byte",
2050b15a35e6SPeter Collingbourne                                       Int32Ty, ResByArg.Byte);
2051b15a35e6SPeter Collingbourne       Constant *Bit = importConstant(Slot, CSByConstantArg.first, "bit", Int8Ty,
2052b15a35e6SPeter Collingbourne                                      ResByArg.Bit);
205314dcf02fSPeter Collingbourne       applyVirtualConstProp(CSByConstantArg.second, "", Byte, Bit);
20540e6694d1SAdrian Prantl       break;
205514dcf02fSPeter Collingbourne     }
20560152c815SPeter Collingbourne     default:
20570152c815SPeter Collingbourne       break;
20580152c815SPeter Collingbourne     }
20590152c815SPeter Collingbourne   }
20602974856aSPeter Collingbourne 
20612974856aSPeter Collingbourne   if (Res.TheKind == WholeProgramDevirtResolution::BranchFunnel) {
206213680223SJames Y Knight     // The type of the function is irrelevant, because it's bitcast at calls
206313680223SJames Y Knight     // anyhow.
206413680223SJames Y Knight     Constant *JT = cast<Constant>(
206513680223SJames Y Knight         M.getOrInsertFunction(getGlobalName(Slot, {}, "branch_funnel"),
206613680223SJames Y Knight                               Type::getVoidTy(M.getContext()))
206713680223SJames Y Knight             .getCallee());
20682974856aSPeter Collingbourne     bool IsExported = false;
20692974856aSPeter Collingbourne     applyICallBranchFunnel(SlotInfo, JT, IsExported);
20702974856aSPeter Collingbourne     assert(!IsExported);
20712974856aSPeter Collingbourne   }
20726d284fabSPeter Collingbourne }
20736d284fabSPeter Collingbourne 
removeRedundantTypeTests()20746d284fabSPeter Collingbourne void DevirtModule::removeRedundantTypeTests() {
20756d284fabSPeter Collingbourne   auto True = ConstantInt::getTrue(M.getContext());
20766d284fabSPeter Collingbourne   for (auto &&U : NumUnsafeUsesForTypeTest) {
20776d284fabSPeter Collingbourne     if (U.second == 0) {
20786d284fabSPeter Collingbourne       U.first->replaceAllUsesWith(True);
20796d284fabSPeter Collingbourne       U.first->eraseFromParent();
20806d284fabSPeter Collingbourne     }
20816d284fabSPeter Collingbourne   }
20826d284fabSPeter Collingbourne }
20836d284fabSPeter Collingbourne 
208409a704c5SMingming Liu ValueInfo
lookUpFunctionValueInfo(Function * TheFn,ModuleSummaryIndex * ExportSummary)208509a704c5SMingming Liu DevirtModule::lookUpFunctionValueInfo(Function *TheFn,
208609a704c5SMingming Liu                                       ModuleSummaryIndex *ExportSummary) {
208709a704c5SMingming Liu   assert((ExportSummary != nullptr) &&
208809a704c5SMingming Liu          "Caller guarantees ExportSummary is not nullptr");
208909a704c5SMingming Liu 
209009a704c5SMingming Liu   const auto TheFnGUID = TheFn->getGUID();
209109a704c5SMingming Liu   const auto TheFnGUIDWithExportedName = GlobalValue::getGUID(TheFn->getName());
209209a704c5SMingming Liu   // Look up ValueInfo with the GUID in the current linkage.
209309a704c5SMingming Liu   ValueInfo TheFnVI = ExportSummary->getValueInfo(TheFnGUID);
209409a704c5SMingming Liu   // If no entry is found and GUID is different from GUID computed using
209509a704c5SMingming Liu   // exported name, look up ValueInfo with the exported name unconditionally.
209609a704c5SMingming Liu   // This is a fallback.
209709a704c5SMingming Liu   //
209809a704c5SMingming Liu   // The reason to have a fallback:
209909a704c5SMingming Liu   // 1. LTO could enable global value internalization via
210009a704c5SMingming Liu   // `enable-lto-internalization`.
210109a704c5SMingming Liu   // 2. The GUID in ExportedSummary is computed using exported name.
210209a704c5SMingming Liu   if ((!TheFnVI) && (TheFnGUID != TheFnGUIDWithExportedName)) {
210309a704c5SMingming Liu     TheFnVI = ExportSummary->getValueInfo(TheFnGUIDWithExportedName);
210409a704c5SMingming Liu   }
210509a704c5SMingming Liu   return TheFnVI;
210609a704c5SMingming Liu }
210709a704c5SMingming Liu 
mustBeUnreachableFunction(Function * const F,ModuleSummaryIndex * ExportSummary)21089c49f8d7Sminglotus-6 bool DevirtModule::mustBeUnreachableFunction(
21099c49f8d7Sminglotus-6     Function *const F, ModuleSummaryIndex *ExportSummary) {
21109c49f8d7Sminglotus-6   // First, learn unreachability by analyzing function IR.
21119c49f8d7Sminglotus-6   if (!F->isDeclaration()) {
21129c49f8d7Sminglotus-6     // A function must be unreachable if its entry block ends with an
21139c49f8d7Sminglotus-6     // 'unreachable'.
21149c49f8d7Sminglotus-6     return isa<UnreachableInst>(F->getEntryBlock().getTerminator());
21159c49f8d7Sminglotus-6   }
21169c49f8d7Sminglotus-6   // Learn unreachability from ExportSummary if ExportSummary is present.
21179c49f8d7Sminglotus-6   return ExportSummary &&
21189c49f8d7Sminglotus-6          ::mustBeUnreachableFunction(
21199c49f8d7Sminglotus-6              DevirtModule::lookUpFunctionValueInfo(F, ExportSummary));
21209c49f8d7Sminglotus-6 }
21219c49f8d7Sminglotus-6 
run()21220312f614SPeter Collingbourne bool DevirtModule::run() {
2123d0b1f30bSTeresa Johnson   // If only some of the modules were split, we cannot correctly perform
2124d0b1f30bSTeresa Johnson   // this transformation. We already checked for the presense of type tests
2125d0b1f30bSTeresa Johnson   // with partially split modules during the thin link, and would have emitted
2126d0b1f30bSTeresa Johnson   // an error if any were found, so here we can simply return.
2127d0b1f30bSTeresa Johnson   if ((ExportSummary && ExportSummary->partiallySplitLTOUnits()) ||
2128d0b1f30bSTeresa Johnson       (ImportSummary && ImportSummary->partiallySplitLTOUnits()))
2129d0b1f30bSTeresa Johnson     return false;
2130d0b1f30bSTeresa Johnson 
21310312f614SPeter Collingbourne   Function *TypeTestFunc =
21320312f614SPeter Collingbourne       M.getFunction(Intrinsic::getName(Intrinsic::type_test));
21330312f614SPeter Collingbourne   Function *TypeCheckedLoadFunc =
21340312f614SPeter Collingbourne       M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load));
21350312f614SPeter Collingbourne   Function *AssumeFunc = M.getFunction(Intrinsic::getName(Intrinsic::assume));
21360312f614SPeter Collingbourne 
2137b406baaeSPeter Collingbourne   // Normally if there are no users of the devirtualization intrinsics in the
2138b406baaeSPeter Collingbourne   // module, this pass has nothing to do. But if we are exporting, we also need
2139b406baaeSPeter Collingbourne   // to handle any users that appear only in the function summaries.
2140f7691d8bSPeter Collingbourne   if (!ExportSummary &&
2141b406baaeSPeter Collingbourne       (!TypeTestFunc || TypeTestFunc->use_empty() || !AssumeFunc ||
21420312f614SPeter Collingbourne        AssumeFunc->use_empty()) &&
21430312f614SPeter Collingbourne       (!TypeCheckedLoadFunc || TypeCheckedLoadFunc->use_empty()))
21440312f614SPeter Collingbourne     return false;
21450312f614SPeter Collingbourne 
21466014c46cSTeresa Johnson   // Rebuild type metadata into a map for easy lookup.
21476014c46cSTeresa Johnson   std::vector<VTableBits> Bits;
21486014c46cSTeresa Johnson   DenseMap<Metadata *, std::set<TypeMemberInfo>> TypeIdMap;
21496014c46cSTeresa Johnson   buildTypeIdentifierMap(Bits, TypeIdMap);
21506014c46cSTeresa Johnson 
21510312f614SPeter Collingbourne   if (TypeTestFunc && AssumeFunc)
21526014c46cSTeresa Johnson     scanTypeTestUsers(TypeTestFunc, TypeIdMap);
21530312f614SPeter Collingbourne 
21540312f614SPeter Collingbourne   if (TypeCheckedLoadFunc)
21550312f614SPeter Collingbourne     scanTypeCheckedLoadUsers(TypeCheckedLoadFunc);
2156df49d1bbSPeter Collingbourne 
2157f7691d8bSPeter Collingbourne   if (ImportSummary) {
21586d284fabSPeter Collingbourne     for (auto &S : CallSlots)
21596d284fabSPeter Collingbourne       importResolution(S.first, S.second);
21606d284fabSPeter Collingbourne 
21616d284fabSPeter Collingbourne     removeRedundantTypeTests();
21626d284fabSPeter Collingbourne 
21639727c77dSDavid Green     // We have lowered or deleted the type intrinsics, so we will no longer have
21649727c77dSDavid Green     // enough information to reason about the liveness of virtual function
21659727c77dSDavid Green     // pointers in GlobalDCE.
21662f63d549STeresa Johnson     for (GlobalVariable &GV : M.globals())
21672f63d549STeresa Johnson       GV.eraseMetadata(LLVMContext::MD_vcall_visibility);
21682f63d549STeresa Johnson 
21696d284fabSPeter Collingbourne     // The rest of the code is only necessary when exporting or during regular
21706d284fabSPeter Collingbourne     // LTO, so we are done.
21716d284fabSPeter Collingbourne     return true;
21726d284fabSPeter Collingbourne   }
21736d284fabSPeter Collingbourne 
21747efd7506SPeter Collingbourne   if (TypeIdMap.empty())
2175df49d1bbSPeter Collingbourne     return true;
2176df49d1bbSPeter Collingbourne 
2177b406baaeSPeter Collingbourne   // Collect information from summary about which calls to try to devirtualize.
2178f7691d8bSPeter Collingbourne   if (ExportSummary) {
2179b406baaeSPeter Collingbourne     DenseMap<GlobalValue::GUID, TinyPtrVector<Metadata *>> MetadataByGUID;
2180b406baaeSPeter Collingbourne     for (auto &P : TypeIdMap) {
2181b406baaeSPeter Collingbourne       if (auto *TypeId = dyn_cast<MDString>(P.first))
2182b406baaeSPeter Collingbourne         MetadataByGUID[GlobalValue::getGUID(TypeId->getString())].push_back(
2183b406baaeSPeter Collingbourne             TypeId);
2184b406baaeSPeter Collingbourne     }
2185b406baaeSPeter Collingbourne 
2186f7691d8bSPeter Collingbourne     for (auto &P : *ExportSummary) {
21879667b91bSPeter Collingbourne       for (auto &S : P.second.SummaryList) {
2188b406baaeSPeter Collingbourne         auto *FS = dyn_cast<FunctionSummary>(S.get());
2189b406baaeSPeter Collingbourne         if (!FS)
2190b406baaeSPeter Collingbourne           continue;
2191b406baaeSPeter Collingbourne         // FIXME: Only add live functions.
21925d8aea10SGeorge Rimar         for (FunctionSummary::VFuncId VF : FS->type_test_assume_vcalls()) {
21935d8aea10SGeorge Rimar           for (Metadata *MD : MetadataByGUID[VF.GUID]) {
2194943afb57SEugene Leviant             CallSlots[{MD, VF.Offset}].CSInfo.addSummaryTypeTestAssumeUser(FS);
21955d8aea10SGeorge Rimar           }
21965d8aea10SGeorge Rimar         }
21975d8aea10SGeorge Rimar         for (FunctionSummary::VFuncId VF : FS->type_checked_load_vcalls()) {
21985d8aea10SGeorge Rimar           for (Metadata *MD : MetadataByGUID[VF.GUID]) {
21992974856aSPeter Collingbourne             CallSlots[{MD, VF.Offset}].CSInfo.addSummaryTypeCheckedLoadUser(FS);
22005d8aea10SGeorge Rimar           }
22015d8aea10SGeorge Rimar         }
2202b406baaeSPeter Collingbourne         for (const FunctionSummary::ConstVCall &VC :
22035d8aea10SGeorge Rimar              FS->type_test_assume_const_vcalls()) {
22045d8aea10SGeorge Rimar           for (Metadata *MD : MetadataByGUID[VC.VFunc.GUID]) {
22052325bb34SPeter Collingbourne             CallSlots[{MD, VC.VFunc.Offset}]
22065d8aea10SGeorge Rimar                 .ConstCSInfo[VC.Args]
2207943afb57SEugene Leviant                 .addSummaryTypeTestAssumeUser(FS);
22085d8aea10SGeorge Rimar           }
22095d8aea10SGeorge Rimar         }
22102325bb34SPeter Collingbourne         for (const FunctionSummary::ConstVCall &VC :
22115d8aea10SGeorge Rimar              FS->type_checked_load_const_vcalls()) {
22125d8aea10SGeorge Rimar           for (Metadata *MD : MetadataByGUID[VC.VFunc.GUID]) {
2213b406baaeSPeter Collingbourne             CallSlots[{MD, VC.VFunc.Offset}]
2214b406baaeSPeter Collingbourne                 .ConstCSInfo[VC.Args]
22152974856aSPeter Collingbourne                 .addSummaryTypeCheckedLoadUser(FS);
2216b406baaeSPeter Collingbourne           }
2217b406baaeSPeter Collingbourne         }
2218b406baaeSPeter Collingbourne       }
22195d8aea10SGeorge Rimar     }
22205d8aea10SGeorge Rimar   }
2221b406baaeSPeter Collingbourne 
22227efd7506SPeter Collingbourne   // For each (type, offset) pair:
2223df49d1bbSPeter Collingbourne   bool DidVirtualConstProp = false;
2224f3403fd2SIvan Krasin   std::map<std::string, Function*> DevirtTargets;
2225df49d1bbSPeter Collingbourne   for (auto &S : CallSlots) {
22267efd7506SPeter Collingbourne     // Search each of the members of the type identifier for the virtual
22277efd7506SPeter Collingbourne     // function implementation at offset S.first.ByteOffset, and add to
22287efd7506SPeter Collingbourne     // TargetsForSlot.
2229df49d1bbSPeter Collingbourne     std::vector<VirtualCallTarget> TargetsForSlot;
22302325bb34SPeter Collingbourne     WholeProgramDevirtResolution *Res = nullptr;
22316014c46cSTeresa Johnson     const std::set<TypeMemberInfo> &TypeMemberInfos = TypeIdMap[S.first.TypeID];
22326014c46cSTeresa Johnson     if (ExportSummary && isa<MDString>(S.first.TypeID) &&
22336014c46cSTeresa Johnson         TypeMemberInfos.size())
22346014c46cSTeresa Johnson       // For any type id used on a global's type metadata, create the type id
22356014c46cSTeresa Johnson       // summary resolution regardless of whether we can devirtualize, so that
22366014c46cSTeresa Johnson       // lower type tests knows the type id is not Unsat. If it was not used on
22376014c46cSTeresa Johnson       // a global's type metadata, the TypeIdMap entry set will be empty, and
22386014c46cSTeresa Johnson       // we don't want to create an entry (with the default Unknown type
22396014c46cSTeresa Johnson       // resolution), which can prevent detection of the Unsat.
2240f7691d8bSPeter Collingbourne       Res = &ExportSummary
22419a3f9797SPeter Collingbourne                  ->getOrInsertTypeIdSummary(
22429a3f9797SPeter Collingbourne                      cast<MDString>(S.first.TypeID)->getString())
22432325bb34SPeter Collingbourne                  .WPDRes[S.first.ByteOffset];
22446014c46cSTeresa Johnson     if (tryFindVirtualCallTargets(TargetsForSlot, TypeMemberInfos,
224509a704c5SMingming Liu                                   S.first.ByteOffset, ExportSummary)) {
22462325bb34SPeter Collingbourne 
2247943afb57SEugene Leviant       if (!trySingleImplDevirt(ExportSummary, TargetsForSlot, S.second, Res)) {
22482974856aSPeter Collingbourne         DidVirtualConstProp |=
22492974856aSPeter Collingbourne             tryVirtualConstProp(TargetsForSlot, S.second, Res, S.first);
22502974856aSPeter Collingbourne 
22512974856aSPeter Collingbourne         tryICallBranchFunnel(TargetsForSlot, S.second, Res, S.first);
22522974856aSPeter Collingbourne       }
2253f3403fd2SIvan Krasin 
2254f3403fd2SIvan Krasin       // Collect functions devirtualized at least for one call site for stats.
2255ced9a795STeresa Johnson       if (RemarksEnabled || AreStatisticsEnabled())
2256f3403fd2SIvan Krasin         for (const auto &T : TargetsForSlot)
2257f3403fd2SIvan Krasin           if (T.WasDevirt)
2258adcd0268SBenjamin Kramer             DevirtTargets[std::string(T.Fn->getName())] = T.Fn;
2259b05e06e4SIvan Krasin     }
2260df49d1bbSPeter Collingbourne 
2261b406baaeSPeter Collingbourne     // CFI-specific: if we are exporting and any llvm.type.checked.load
2262b406baaeSPeter Collingbourne     // intrinsics were *not* devirtualized, we need to add the resulting
2263b406baaeSPeter Collingbourne     // llvm.type.test intrinsics to the function summaries so that the
2264b406baaeSPeter Collingbourne     // LowerTypeTests pass will export them.
2265f7691d8bSPeter Collingbourne     if (ExportSummary && isa<MDString>(S.first.TypeID)) {
2266b406baaeSPeter Collingbourne       auto GUID =
2267b406baaeSPeter Collingbourne           GlobalValue::getGUID(cast<MDString>(S.first.TypeID)->getString());
2268b406baaeSPeter Collingbourne       for (auto FS : S.second.CSInfo.SummaryTypeCheckedLoadUsers)
2269b406baaeSPeter Collingbourne         FS->addTypeTest(GUID);
2270b406baaeSPeter Collingbourne       for (auto &CCS : S.second.ConstCSInfo)
2271b406baaeSPeter Collingbourne         for (auto FS : CCS.second.SummaryTypeCheckedLoadUsers)
2272b406baaeSPeter Collingbourne           FS->addTypeTest(GUID);
2273b406baaeSPeter Collingbourne     }
2274b406baaeSPeter Collingbourne   }
2275b406baaeSPeter Collingbourne 
2276f3403fd2SIvan Krasin   if (RemarksEnabled) {
2277f3403fd2SIvan Krasin     // Generate remarks for each devirtualized function.
2278f3403fd2SIvan Krasin     for (const auto &DT : DevirtTargets) {
2279f3403fd2SIvan Krasin       Function *F = DT.second;
2280e963c89dSSam Elliott 
2281e963c89dSSam Elliott       using namespace ore;
22829110cb45SPeter Collingbourne       OREGetter(F).emit(OptimizationRemark(DEBUG_TYPE, "Devirtualized", F)
22839110cb45SPeter Collingbourne                         << "devirtualized "
2284d2df54e6STeresa Johnson                         << NV("FunctionName", DT.first));
2285b05e06e4SIvan Krasin     }
2286df49d1bbSPeter Collingbourne   }
2287df49d1bbSPeter Collingbourne 
2288ced9a795STeresa Johnson   NumDevirtTargets += DevirtTargets.size();
2289ced9a795STeresa Johnson 
22906d284fabSPeter Collingbourne   removeRedundantTypeTests();
22910312f614SPeter Collingbourne 
2292df49d1bbSPeter Collingbourne   // Rebuild each global we touched as part of virtual constant propagation to
2293df49d1bbSPeter Collingbourne   // include the before and after bytes.
2294df49d1bbSPeter Collingbourne   if (DidVirtualConstProp)
2295df49d1bbSPeter Collingbourne     for (VTableBits &B : Bits)
2296df49d1bbSPeter Collingbourne       rebuildGlobal(B);
2297df49d1bbSPeter Collingbourne 
22989727c77dSDavid Green   // We have lowered or deleted the type intrinsics, so we will no longer have
22999727c77dSDavid Green   // enough information to reason about the liveness of virtual function
23009727c77dSDavid Green   // pointers in GlobalDCE.
23013b598b9cSOliver Stannard   for (GlobalVariable &GV : M.globals())
23023b598b9cSOliver Stannard     GV.eraseMetadata(LLVMContext::MD_vcall_visibility);
23033b598b9cSOliver Stannard 
2304df49d1bbSPeter Collingbourne   return true;
2305df49d1bbSPeter Collingbourne }
2306d2df54e6STeresa Johnson 
run()2307d2df54e6STeresa Johnson void DevirtIndex::run() {
2308d2df54e6STeresa Johnson   if (ExportSummary.typeIdCompatibleVtableMap().empty())
2309d2df54e6STeresa Johnson     return;
2310d2df54e6STeresa Johnson 
2311d2df54e6STeresa Johnson   DenseMap<GlobalValue::GUID, std::vector<StringRef>> NameByGUID;
2312d2df54e6STeresa Johnson   for (auto &P : ExportSummary.typeIdCompatibleVtableMap()) {
2313d2df54e6STeresa Johnson     NameByGUID[GlobalValue::getGUID(P.first)].push_back(P.first);
2314d2df54e6STeresa Johnson   }
2315d2df54e6STeresa Johnson 
2316d2df54e6STeresa Johnson   // Collect information from summary about which calls to try to devirtualize.
2317d2df54e6STeresa Johnson   for (auto &P : ExportSummary) {
2318d2df54e6STeresa Johnson     for (auto &S : P.second.SummaryList) {
2319d2df54e6STeresa Johnson       auto *FS = dyn_cast<FunctionSummary>(S.get());
2320d2df54e6STeresa Johnson       if (!FS)
2321d2df54e6STeresa Johnson         continue;
2322d2df54e6STeresa Johnson       // FIXME: Only add live functions.
2323d2df54e6STeresa Johnson       for (FunctionSummary::VFuncId VF : FS->type_test_assume_vcalls()) {
2324d2df54e6STeresa Johnson         for (StringRef Name : NameByGUID[VF.GUID]) {
2325d2df54e6STeresa Johnson           CallSlots[{Name, VF.Offset}].CSInfo.addSummaryTypeTestAssumeUser(FS);
2326d2df54e6STeresa Johnson         }
2327d2df54e6STeresa Johnson       }
2328d2df54e6STeresa Johnson       for (FunctionSummary::VFuncId VF : FS->type_checked_load_vcalls()) {
2329d2df54e6STeresa Johnson         for (StringRef Name : NameByGUID[VF.GUID]) {
2330d2df54e6STeresa Johnson           CallSlots[{Name, VF.Offset}].CSInfo.addSummaryTypeCheckedLoadUser(FS);
2331d2df54e6STeresa Johnson         }
2332d2df54e6STeresa Johnson       }
2333d2df54e6STeresa Johnson       for (const FunctionSummary::ConstVCall &VC :
2334d2df54e6STeresa Johnson            FS->type_test_assume_const_vcalls()) {
2335d2df54e6STeresa Johnson         for (StringRef Name : NameByGUID[VC.VFunc.GUID]) {
2336d2df54e6STeresa Johnson           CallSlots[{Name, VC.VFunc.Offset}]
2337d2df54e6STeresa Johnson               .ConstCSInfo[VC.Args]
2338d2df54e6STeresa Johnson               .addSummaryTypeTestAssumeUser(FS);
2339d2df54e6STeresa Johnson         }
2340d2df54e6STeresa Johnson       }
2341d2df54e6STeresa Johnson       for (const FunctionSummary::ConstVCall &VC :
2342d2df54e6STeresa Johnson            FS->type_checked_load_const_vcalls()) {
2343d2df54e6STeresa Johnson         for (StringRef Name : NameByGUID[VC.VFunc.GUID]) {
2344d2df54e6STeresa Johnson           CallSlots[{Name, VC.VFunc.Offset}]
2345d2df54e6STeresa Johnson               .ConstCSInfo[VC.Args]
2346d2df54e6STeresa Johnson               .addSummaryTypeCheckedLoadUser(FS);
2347d2df54e6STeresa Johnson         }
2348d2df54e6STeresa Johnson       }
2349d2df54e6STeresa Johnson     }
2350d2df54e6STeresa Johnson   }
2351d2df54e6STeresa Johnson 
2352d2df54e6STeresa Johnson   std::set<ValueInfo> DevirtTargets;
2353d2df54e6STeresa Johnson   // For each (type, offset) pair:
2354d2df54e6STeresa Johnson   for (auto &S : CallSlots) {
2355d2df54e6STeresa Johnson     // Search each of the members of the type identifier for the virtual
2356d2df54e6STeresa Johnson     // function implementation at offset S.first.ByteOffset, and add to
2357d2df54e6STeresa Johnson     // TargetsForSlot.
2358d2df54e6STeresa Johnson     std::vector<ValueInfo> TargetsForSlot;
2359d2df54e6STeresa Johnson     auto TidSummary = ExportSummary.getTypeIdCompatibleVtableSummary(S.first.TypeID);
2360d2df54e6STeresa Johnson     assert(TidSummary);
23616014c46cSTeresa Johnson     // Create the type id summary resolution regardlness of whether we can
23626014c46cSTeresa Johnson     // devirtualize, so that lower type tests knows the type id is used on
23636014c46cSTeresa Johnson     // a global and not Unsat.
2364d2df54e6STeresa Johnson     WholeProgramDevirtResolution *Res =
2365d2df54e6STeresa Johnson         &ExportSummary.getOrInsertTypeIdSummary(S.first.TypeID)
2366d2df54e6STeresa Johnson              .WPDRes[S.first.ByteOffset];
23676014c46cSTeresa Johnson     if (tryFindVirtualCallTargets(TargetsForSlot, *TidSummary,
23686014c46cSTeresa Johnson                                   S.first.ByteOffset)) {
2369d2df54e6STeresa Johnson 
2370d2df54e6STeresa Johnson       if (!trySingleImplDevirt(TargetsForSlot, S.first, S.second, Res,
2371d2df54e6STeresa Johnson                                DevirtTargets))
2372d2df54e6STeresa Johnson         continue;
2373d2df54e6STeresa Johnson     }
2374d2df54e6STeresa Johnson   }
2375d2df54e6STeresa Johnson 
2376d2df54e6STeresa Johnson   // Optionally have the thin link print message for each devirtualized
2377d2df54e6STeresa Johnson   // function.
2378d2df54e6STeresa Johnson   if (PrintSummaryDevirt)
2379d2df54e6STeresa Johnson     for (const auto &DT : DevirtTargets)
2380d2df54e6STeresa Johnson       errs() << "Devirtualized call to " << DT << "\n";
2381ced9a795STeresa Johnson 
2382ced9a795STeresa Johnson   NumDevirtTargets += DevirtTargets.size();
2383d2df54e6STeresa Johnson }
2384