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,
1479cb59b92SVitaly Buka                 cl::init(10), cl::ZeroOrMore,
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::init(false), cl::ZeroOrMore,
154d2df54e6STeresa Johnson                        cl::desc("Print index-based devirtualization messages"));
155d2df54e6STeresa Johnson 
1562f63d549STeresa Johnson /// Provide a way to force enable whole program visibility in tests.
1572f63d549STeresa Johnson /// This is needed to support legacy tests that don't contain
1582f63d549STeresa Johnson /// !vcall_visibility metadata (the mere presense of type tests
1592f63d549STeresa Johnson /// previously implied hidden visibility).
160d8aba75aSFangrui Song static cl::opt<bool>
1612f63d549STeresa Johnson     WholeProgramVisibility("whole-program-visibility", cl::init(false),
1622f63d549STeresa Johnson                            cl::Hidden, cl::ZeroOrMore,
1632f63d549STeresa Johnson                            cl::desc("Enable whole program visibility"));
1642f63d549STeresa Johnson 
1652f63d549STeresa Johnson /// Provide a way to force disable whole program for debugging or workarounds,
1662f63d549STeresa Johnson /// when enabled via the linker.
167d8aba75aSFangrui Song static cl::opt<bool> DisableWholeProgramVisibility(
1682f63d549STeresa Johnson     "disable-whole-program-visibility", cl::init(false), cl::Hidden,
1692f63d549STeresa Johnson     cl::ZeroOrMore,
1702f63d549STeresa Johnson     cl::desc("Disable whole program visibility (overrides enabling options)"));
1712f63d549STeresa Johnson 
1726d2032e2Sevgeny /// Provide way to prevent certain function from being devirtualized
173d8aba75aSFangrui Song static cl::list<std::string>
1746d2032e2Sevgeny     SkipFunctionNames("wholeprogramdevirt-skip",
1756d2032e2Sevgeny                       cl::desc("Prevent function(s) from being devirtualized"),
1766d2032e2Sevgeny                       cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated);
1776d2032e2Sevgeny 
178fee0bde4STeresa Johnson /// Mechanism to add runtime checking of devirtualization decisions, optionally
179fee0bde4STeresa Johnson /// trapping or falling back to indirect call on any that are not correct.
180fee0bde4STeresa Johnson /// Trapping mode is useful for debugging undefined behavior leading to failures
181fee0bde4STeresa Johnson /// with WPD. Fallback mode is useful for ensuring safety when whole program
182fee0bde4STeresa Johnson /// visibility may be compromised.
183fee0bde4STeresa Johnson enum WPDCheckMode { None, Trap, Fallback };
184fee0bde4STeresa Johnson static cl::opt<WPDCheckMode> DevirtCheckMode(
185fee0bde4STeresa Johnson     "wholeprogramdevirt-check", cl::Hidden, cl::ZeroOrMore,
186fee0bde4STeresa Johnson     cl::desc("Type of checking for incorrect devirtualizations"),
187fee0bde4STeresa Johnson     cl::values(clEnumValN(WPDCheckMode::None, "none", "No checking"),
188fee0bde4STeresa Johnson                clEnumValN(WPDCheckMode::Trap, "trap", "Trap when incorrect"),
189fee0bde4STeresa Johnson                clEnumValN(WPDCheckMode::Fallback, "fallback",
190fee0bde4STeresa Johnson                           "Fallback to indirect when incorrect")));
191d55d46f4STeresa Johnson 
1926d2032e2Sevgeny namespace {
1936d2032e2Sevgeny struct PatternList {
1946d2032e2Sevgeny   std::vector<GlobPattern> Patterns;
1956d2032e2Sevgeny   template <class T> void init(const T &StringList) {
1966d2032e2Sevgeny     for (const auto &S : StringList)
1976d2032e2Sevgeny       if (Expected<GlobPattern> Pat = GlobPattern::create(S))
1986d2032e2Sevgeny         Patterns.push_back(std::move(*Pat));
1996d2032e2Sevgeny   }
2006d2032e2Sevgeny   bool match(StringRef S) {
2016d2032e2Sevgeny     for (const GlobPattern &P : Patterns)
2026d2032e2Sevgeny       if (P.match(S))
2036d2032e2Sevgeny         return true;
2046d2032e2Sevgeny     return false;
2056d2032e2Sevgeny   }
2066d2032e2Sevgeny };
2076d2032e2Sevgeny } // namespace
2086d2032e2Sevgeny 
209df49d1bbSPeter Collingbourne // Find the minimum offset that we may store a value of size Size bits at. If
210df49d1bbSPeter Collingbourne // IsAfter is set, look for an offset before the object, otherwise look for an
211df49d1bbSPeter Collingbourne // offset after the object.
212df49d1bbSPeter Collingbourne uint64_t
213df49d1bbSPeter Collingbourne wholeprogramdevirt::findLowestOffset(ArrayRef<VirtualCallTarget> Targets,
214df49d1bbSPeter Collingbourne                                      bool IsAfter, uint64_t Size) {
215df49d1bbSPeter Collingbourne   // Find a minimum offset taking into account only vtable sizes.
216df49d1bbSPeter Collingbourne   uint64_t MinByte = 0;
217df49d1bbSPeter Collingbourne   for (const VirtualCallTarget &Target : Targets) {
218df49d1bbSPeter Collingbourne     if (IsAfter)
219df49d1bbSPeter Collingbourne       MinByte = std::max(MinByte, Target.minAfterBytes());
220df49d1bbSPeter Collingbourne     else
221df49d1bbSPeter Collingbourne       MinByte = std::max(MinByte, Target.minBeforeBytes());
222df49d1bbSPeter Collingbourne   }
223df49d1bbSPeter Collingbourne 
224df49d1bbSPeter Collingbourne   // Build a vector of arrays of bytes covering, for each target, a slice of the
225df49d1bbSPeter Collingbourne   // used region (see AccumBitVector::BytesUsed in
226df49d1bbSPeter Collingbourne   // llvm/Transforms/IPO/WholeProgramDevirt.h) starting at MinByte. Effectively,
227df49d1bbSPeter Collingbourne   // this aligns the used regions to start at MinByte.
228df49d1bbSPeter Collingbourne   //
229df49d1bbSPeter Collingbourne   // In this example, A, B and C are vtables, # is a byte already allocated for
230df49d1bbSPeter Collingbourne   // a virtual function pointer, AAAA... (etc.) are the used regions for the
231df49d1bbSPeter Collingbourne   // vtables and Offset(X) is the value computed for the Offset variable below
232df49d1bbSPeter Collingbourne   // for X.
233df49d1bbSPeter Collingbourne   //
234df49d1bbSPeter Collingbourne   //                    Offset(A)
235df49d1bbSPeter Collingbourne   //                    |       |
236df49d1bbSPeter Collingbourne   //                            |MinByte
237df49d1bbSPeter Collingbourne   // A: ################AAAAAAAA|AAAAAAAA
238df49d1bbSPeter Collingbourne   // B: ########BBBBBBBBBBBBBBBB|BBBB
239df49d1bbSPeter Collingbourne   // C: ########################|CCCCCCCCCCCCCCCC
240df49d1bbSPeter Collingbourne   //            |   Offset(B)   |
241df49d1bbSPeter Collingbourne   //
242df49d1bbSPeter Collingbourne   // This code produces the slices of A, B and C that appear after the divider
243df49d1bbSPeter Collingbourne   // at MinByte.
244df49d1bbSPeter Collingbourne   std::vector<ArrayRef<uint8_t>> Used;
245df49d1bbSPeter Collingbourne   for (const VirtualCallTarget &Target : Targets) {
2467efd7506SPeter Collingbourne     ArrayRef<uint8_t> VTUsed = IsAfter ? Target.TM->Bits->After.BytesUsed
2477efd7506SPeter Collingbourne                                        : Target.TM->Bits->Before.BytesUsed;
248df49d1bbSPeter Collingbourne     uint64_t Offset = IsAfter ? MinByte - Target.minAfterBytes()
249df49d1bbSPeter Collingbourne                               : MinByte - Target.minBeforeBytes();
250df49d1bbSPeter Collingbourne 
251df49d1bbSPeter Collingbourne     // Disregard used regions that are smaller than Offset. These are
252df49d1bbSPeter Collingbourne     // effectively all-free regions that do not need to be checked.
253df49d1bbSPeter Collingbourne     if (VTUsed.size() > Offset)
254df49d1bbSPeter Collingbourne       Used.push_back(VTUsed.slice(Offset));
255df49d1bbSPeter Collingbourne   }
256df49d1bbSPeter Collingbourne 
257df49d1bbSPeter Collingbourne   if (Size == 1) {
258df49d1bbSPeter Collingbourne     // Find a free bit in each member of Used.
259df49d1bbSPeter Collingbourne     for (unsigned I = 0;; ++I) {
260df49d1bbSPeter Collingbourne       uint8_t BitsUsed = 0;
261df49d1bbSPeter Collingbourne       for (auto &&B : Used)
262df49d1bbSPeter Collingbourne         if (I < B.size())
263df49d1bbSPeter Collingbourne           BitsUsed |= B[I];
264df49d1bbSPeter Collingbourne       if (BitsUsed != 0xff)
265df49d1bbSPeter Collingbourne         return (MinByte + I) * 8 +
266df49d1bbSPeter Collingbourne                countTrailingZeros(uint8_t(~BitsUsed), ZB_Undefined);
267df49d1bbSPeter Collingbourne     }
268df49d1bbSPeter Collingbourne   } else {
269df49d1bbSPeter Collingbourne     // Find a free (Size/8) byte region in each member of Used.
270df49d1bbSPeter Collingbourne     // FIXME: see if alignment helps.
271df49d1bbSPeter Collingbourne     for (unsigned I = 0;; ++I) {
272df49d1bbSPeter Collingbourne       for (auto &&B : Used) {
273df49d1bbSPeter Collingbourne         unsigned Byte = 0;
274df49d1bbSPeter Collingbourne         while ((I + Byte) < B.size() && Byte < (Size / 8)) {
275df49d1bbSPeter Collingbourne           if (B[I + Byte])
276df49d1bbSPeter Collingbourne             goto NextI;
277df49d1bbSPeter Collingbourne           ++Byte;
278df49d1bbSPeter Collingbourne         }
279df49d1bbSPeter Collingbourne       }
280df49d1bbSPeter Collingbourne       return (MinByte + I) * 8;
281df49d1bbSPeter Collingbourne     NextI:;
282df49d1bbSPeter Collingbourne     }
283df49d1bbSPeter Collingbourne   }
284df49d1bbSPeter Collingbourne }
285df49d1bbSPeter Collingbourne 
286df49d1bbSPeter Collingbourne void wholeprogramdevirt::setBeforeReturnValues(
287df49d1bbSPeter Collingbourne     MutableArrayRef<VirtualCallTarget> Targets, uint64_t AllocBefore,
288df49d1bbSPeter Collingbourne     unsigned BitWidth, int64_t &OffsetByte, uint64_t &OffsetBit) {
289df49d1bbSPeter Collingbourne   if (BitWidth == 1)
290df49d1bbSPeter Collingbourne     OffsetByte = -(AllocBefore / 8 + 1);
291df49d1bbSPeter Collingbourne   else
292df49d1bbSPeter Collingbourne     OffsetByte = -((AllocBefore + 7) / 8 + (BitWidth + 7) / 8);
293df49d1bbSPeter Collingbourne   OffsetBit = AllocBefore % 8;
294df49d1bbSPeter Collingbourne 
295df49d1bbSPeter Collingbourne   for (VirtualCallTarget &Target : Targets) {
296df49d1bbSPeter Collingbourne     if (BitWidth == 1)
297df49d1bbSPeter Collingbourne       Target.setBeforeBit(AllocBefore);
298df49d1bbSPeter Collingbourne     else
299df49d1bbSPeter Collingbourne       Target.setBeforeBytes(AllocBefore, (BitWidth + 7) / 8);
300df49d1bbSPeter Collingbourne   }
301df49d1bbSPeter Collingbourne }
302df49d1bbSPeter Collingbourne 
303df49d1bbSPeter Collingbourne void wholeprogramdevirt::setAfterReturnValues(
304df49d1bbSPeter Collingbourne     MutableArrayRef<VirtualCallTarget> Targets, uint64_t AllocAfter,
305df49d1bbSPeter Collingbourne     unsigned BitWidth, int64_t &OffsetByte, uint64_t &OffsetBit) {
306df49d1bbSPeter Collingbourne   if (BitWidth == 1)
307df49d1bbSPeter Collingbourne     OffsetByte = AllocAfter / 8;
308df49d1bbSPeter Collingbourne   else
309df49d1bbSPeter Collingbourne     OffsetByte = (AllocAfter + 7) / 8;
310df49d1bbSPeter Collingbourne   OffsetBit = AllocAfter % 8;
311df49d1bbSPeter Collingbourne 
312df49d1bbSPeter Collingbourne   for (VirtualCallTarget &Target : Targets) {
313df49d1bbSPeter Collingbourne     if (BitWidth == 1)
314df49d1bbSPeter Collingbourne       Target.setAfterBit(AllocAfter);
315df49d1bbSPeter Collingbourne     else
316df49d1bbSPeter Collingbourne       Target.setAfterBytes(AllocAfter, (BitWidth + 7) / 8);
317df49d1bbSPeter Collingbourne   }
318df49d1bbSPeter Collingbourne }
319df49d1bbSPeter Collingbourne 
3207efd7506SPeter Collingbourne VirtualCallTarget::VirtualCallTarget(Function *Fn, const TypeMemberInfo *TM)
3217efd7506SPeter Collingbourne     : Fn(Fn), TM(TM),
32289439a79SIvan Krasin       IsBigEndian(Fn->getParent()->getDataLayout().isBigEndian()), WasDevirt(false) {}
323df49d1bbSPeter Collingbourne 
324df49d1bbSPeter Collingbourne namespace {
325df49d1bbSPeter Collingbourne 
3267efd7506SPeter Collingbourne // A slot in a set of virtual tables. The TypeID identifies the set of virtual
327df49d1bbSPeter Collingbourne // tables, and the ByteOffset is the offset in bytes from the address point to
328df49d1bbSPeter Collingbourne // the virtual function pointer.
329df49d1bbSPeter Collingbourne struct VTableSlot {
3307efd7506SPeter Collingbourne   Metadata *TypeID;
331df49d1bbSPeter Collingbourne   uint64_t ByteOffset;
332df49d1bbSPeter Collingbourne };
333df49d1bbSPeter Collingbourne 
334cdc71612SEugene Zelenko } // end anonymous namespace
335df49d1bbSPeter Collingbourne 
3369b656527SPeter Collingbourne namespace llvm {
3379b656527SPeter Collingbourne 
338df49d1bbSPeter Collingbourne template <> struct DenseMapInfo<VTableSlot> {
339df49d1bbSPeter Collingbourne   static VTableSlot getEmptyKey() {
340df49d1bbSPeter Collingbourne     return {DenseMapInfo<Metadata *>::getEmptyKey(),
341df49d1bbSPeter Collingbourne             DenseMapInfo<uint64_t>::getEmptyKey()};
342df49d1bbSPeter Collingbourne   }
343df49d1bbSPeter Collingbourne   static VTableSlot getTombstoneKey() {
344df49d1bbSPeter Collingbourne     return {DenseMapInfo<Metadata *>::getTombstoneKey(),
345df49d1bbSPeter Collingbourne             DenseMapInfo<uint64_t>::getTombstoneKey()};
346df49d1bbSPeter Collingbourne   }
347df49d1bbSPeter Collingbourne   static unsigned getHashValue(const VTableSlot &I) {
3487efd7506SPeter Collingbourne     return DenseMapInfo<Metadata *>::getHashValue(I.TypeID) ^
349df49d1bbSPeter Collingbourne            DenseMapInfo<uint64_t>::getHashValue(I.ByteOffset);
350df49d1bbSPeter Collingbourne   }
351df49d1bbSPeter Collingbourne   static bool isEqual(const VTableSlot &LHS,
352df49d1bbSPeter Collingbourne                       const VTableSlot &RHS) {
3537efd7506SPeter Collingbourne     return LHS.TypeID == RHS.TypeID && LHS.ByteOffset == RHS.ByteOffset;
354df49d1bbSPeter Collingbourne   }
355df49d1bbSPeter Collingbourne };
356df49d1bbSPeter Collingbourne 
357d2df54e6STeresa Johnson template <> struct DenseMapInfo<VTableSlotSummary> {
358d2df54e6STeresa Johnson   static VTableSlotSummary getEmptyKey() {
359d2df54e6STeresa Johnson     return {DenseMapInfo<StringRef>::getEmptyKey(),
360d2df54e6STeresa Johnson             DenseMapInfo<uint64_t>::getEmptyKey()};
361d2df54e6STeresa Johnson   }
362d2df54e6STeresa Johnson   static VTableSlotSummary getTombstoneKey() {
363d2df54e6STeresa Johnson     return {DenseMapInfo<StringRef>::getTombstoneKey(),
364d2df54e6STeresa Johnson             DenseMapInfo<uint64_t>::getTombstoneKey()};
365d2df54e6STeresa Johnson   }
366d2df54e6STeresa Johnson   static unsigned getHashValue(const VTableSlotSummary &I) {
367d2df54e6STeresa Johnson     return DenseMapInfo<StringRef>::getHashValue(I.TypeID) ^
368d2df54e6STeresa Johnson            DenseMapInfo<uint64_t>::getHashValue(I.ByteOffset);
369d2df54e6STeresa Johnson   }
370d2df54e6STeresa Johnson   static bool isEqual(const VTableSlotSummary &LHS,
371d2df54e6STeresa Johnson                       const VTableSlotSummary &RHS) {
372d2df54e6STeresa Johnson     return LHS.TypeID == RHS.TypeID && LHS.ByteOffset == RHS.ByteOffset;
373d2df54e6STeresa Johnson   }
374d2df54e6STeresa Johnson };
375d2df54e6STeresa Johnson 
376cdc71612SEugene Zelenko } // end namespace llvm
3779b656527SPeter Collingbourne 
378df49d1bbSPeter Collingbourne namespace {
379df49d1bbSPeter Collingbourne 
38009a704c5SMingming Liu // Returns true if the function must be unreachable based on ValueInfo.
38109a704c5SMingming Liu //
38209a704c5SMingming Liu // In particular, identifies a function as unreachable in the following
38309a704c5SMingming Liu // conditions
38409a704c5SMingming Liu //   1) All summaries are live.
38509a704c5SMingming Liu //   2) All function summaries indicate it's unreachable
38609a704c5SMingming Liu bool mustBeUnreachableFunction(ValueInfo TheFnVI) {
38709a704c5SMingming Liu   if ((!TheFnVI) || TheFnVI.getSummaryList().empty()) {
38809a704c5SMingming Liu     // Returns false if ValueInfo is absent, or the summary list is empty
38909a704c5SMingming Liu     // (e.g., function declarations).
39009a704c5SMingming Liu     return false;
39109a704c5SMingming Liu   }
39209a704c5SMingming Liu 
39309a704c5SMingming Liu   for (auto &Summary : TheFnVI.getSummaryList()) {
39409a704c5SMingming Liu     // Conservatively returns false if any non-live functions are seen.
39509a704c5SMingming Liu     // In general either all summaries should be live or all should be dead.
39609a704c5SMingming Liu     if (!Summary->isLive())
39709a704c5SMingming Liu       return false;
39809a704c5SMingming Liu     if (auto *FS = dyn_cast<FunctionSummary>(Summary.get())) {
39909a704c5SMingming Liu       if (!FS->fflags().MustBeUnreachable)
40009a704c5SMingming Liu         return false;
40109a704c5SMingming Liu     }
40209a704c5SMingming Liu     // Do nothing if a non-function has the same GUID (which is rare).
40309a704c5SMingming Liu     // This is correct since non-function summaries are not relevant.
40409a704c5SMingming Liu   }
40509a704c5SMingming Liu   // All function summaries are live and all of them agree that the function is
40609a704c5SMingming Liu   // unreachble.
40709a704c5SMingming Liu   return true;
40809a704c5SMingming Liu }
40909a704c5SMingming Liu 
410df49d1bbSPeter Collingbourne // A virtual call site. VTable is the loaded virtual table pointer, and CS is
411df49d1bbSPeter Collingbourne // the indirect virtual call.
412df49d1bbSPeter Collingbourne struct VirtualCallSite {
413cea6f4d5SMircea Trofin   Value *VTable = nullptr;
414cea6f4d5SMircea Trofin   CallBase &CB;
415df49d1bbSPeter Collingbourne 
4160312f614SPeter Collingbourne   // If non-null, this field points to the associated unsafe use count stored in
4170312f614SPeter Collingbourne   // the DevirtModule::NumUnsafeUsesForTypeTest map below. See the description
4180312f614SPeter Collingbourne   // of that field for details.
419cea6f4d5SMircea Trofin   unsigned *NumUnsafeUses = nullptr;
4200312f614SPeter Collingbourne 
421e963c89dSSam Elliott   void
422e963c89dSSam Elliott   emitRemark(const StringRef OptName, const StringRef TargetName,
423e963c89dSSam Elliott              function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter) {
424cea6f4d5SMircea Trofin     Function *F = CB.getCaller();
425cea6f4d5SMircea Trofin     DebugLoc DLoc = CB.getDebugLoc();
426cea6f4d5SMircea Trofin     BasicBlock *Block = CB.getParent();
427e963c89dSSam Elliott 
428e963c89dSSam Elliott     using namespace ore;
4299110cb45SPeter Collingbourne     OREGetter(F).emit(OptimizationRemark(DEBUG_TYPE, OptName, DLoc, Block)
4309110cb45SPeter Collingbourne                       << NV("Optimization", OptName)
4319110cb45SPeter Collingbourne                       << ": devirtualized a call to "
432e963c89dSSam Elliott                       << NV("FunctionName", TargetName));
433e963c89dSSam Elliott   }
434e963c89dSSam Elliott 
435e963c89dSSam Elliott   void replaceAndErase(
436e963c89dSSam Elliott       const StringRef OptName, const StringRef TargetName, bool RemarksEnabled,
437e963c89dSSam Elliott       function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter,
438e963c89dSSam Elliott       Value *New) {
439f3403fd2SIvan Krasin     if (RemarksEnabled)
440e963c89dSSam Elliott       emitRemark(OptName, TargetName, OREGetter);
441cea6f4d5SMircea Trofin     CB.replaceAllUsesWith(New);
442cea6f4d5SMircea Trofin     if (auto *II = dyn_cast<InvokeInst>(&CB)) {
443cea6f4d5SMircea Trofin       BranchInst::Create(II->getNormalDest(), &CB);
444df49d1bbSPeter Collingbourne       II->getUnwindDest()->removePredecessor(II->getParent());
445df49d1bbSPeter Collingbourne     }
446cea6f4d5SMircea Trofin     CB.eraseFromParent();
4470312f614SPeter Collingbourne     // This use is no longer unsafe.
4480312f614SPeter Collingbourne     if (NumUnsafeUses)
4490312f614SPeter Collingbourne       --*NumUnsafeUses;
450df49d1bbSPeter Collingbourne   }
451df49d1bbSPeter Collingbourne };
452df49d1bbSPeter Collingbourne 
45350cbd7ccSPeter Collingbourne // Call site information collected for a specific VTableSlot and possibly a list
45450cbd7ccSPeter Collingbourne // of constant integer arguments. The grouping by arguments is handled by the
45550cbd7ccSPeter Collingbourne // VTableSlotInfo class.
45650cbd7ccSPeter Collingbourne struct CallSiteInfo {
457b406baaeSPeter Collingbourne   /// The set of call sites for this slot. Used during regular LTO and the
458b406baaeSPeter Collingbourne   /// import phase of ThinLTO (as well as the export phase of ThinLTO for any
459b406baaeSPeter Collingbourne   /// call sites that appear in the merged module itself); in each of these
460b406baaeSPeter Collingbourne   /// cases we are directly operating on the call sites at the IR level.
46150cbd7ccSPeter Collingbourne   std::vector<VirtualCallSite> CallSites;
462b406baaeSPeter Collingbourne 
4632974856aSPeter Collingbourne   /// Whether all call sites represented by this CallSiteInfo, including those
4642974856aSPeter Collingbourne   /// in summaries, have been devirtualized. This starts off as true because a
4652974856aSPeter Collingbourne   /// default constructed CallSiteInfo represents no call sites.
4662974856aSPeter Collingbourne   bool AllCallSitesDevirted = true;
4672974856aSPeter Collingbourne 
468b406baaeSPeter Collingbourne   // These fields are used during the export phase of ThinLTO and reflect
469b406baaeSPeter Collingbourne   // information collected from function summaries.
470b406baaeSPeter Collingbourne 
4712325bb34SPeter Collingbourne   /// Whether any function summary contains an llvm.assume(llvm.type.test) for
4722325bb34SPeter Collingbourne   /// this slot.
4732974856aSPeter Collingbourne   bool SummaryHasTypeTestAssumeUsers = false;
4742325bb34SPeter Collingbourne 
475b406baaeSPeter Collingbourne   /// CFI-specific: a vector containing the list of function summaries that use
476b406baaeSPeter Collingbourne   /// the llvm.type.checked.load intrinsic and therefore will require
477b406baaeSPeter Collingbourne   /// resolutions for llvm.type.test in order to implement CFI checks if
478b406baaeSPeter Collingbourne   /// devirtualization was unsuccessful. If devirtualization was successful, the
47959675ba0SPeter Collingbourne   /// pass will clear this vector by calling markDevirt(). If at the end of the
48059675ba0SPeter Collingbourne   /// pass the vector is non-empty, we will need to add a use of llvm.type.test
48159675ba0SPeter Collingbourne   /// to each of the function summaries in the vector.
482b406baaeSPeter Collingbourne   std::vector<FunctionSummary *> SummaryTypeCheckedLoadUsers;
483d2df54e6STeresa Johnson   std::vector<FunctionSummary *> SummaryTypeTestAssumeUsers;
4842325bb34SPeter Collingbourne 
4852325bb34SPeter Collingbourne   bool isExported() const {
4862325bb34SPeter Collingbourne     return SummaryHasTypeTestAssumeUsers ||
4872325bb34SPeter Collingbourne            !SummaryTypeCheckedLoadUsers.empty();
4882325bb34SPeter Collingbourne   }
48959675ba0SPeter Collingbourne 
4902974856aSPeter Collingbourne   void addSummaryTypeCheckedLoadUser(FunctionSummary *FS) {
4912974856aSPeter Collingbourne     SummaryTypeCheckedLoadUsers.push_back(FS);
4922974856aSPeter Collingbourne     AllCallSitesDevirted = false;
4932974856aSPeter Collingbourne   }
4942974856aSPeter Collingbourne 
495d2df54e6STeresa Johnson   void addSummaryTypeTestAssumeUser(FunctionSummary *FS) {
496d2df54e6STeresa Johnson     SummaryTypeTestAssumeUsers.push_back(FS);
497943afb57SEugene Leviant     SummaryHasTypeTestAssumeUsers = true;
498943afb57SEugene Leviant     AllCallSitesDevirted = false;
499d2df54e6STeresa Johnson   }
500d2df54e6STeresa Johnson 
5012974856aSPeter Collingbourne   void markDevirt() {
5022974856aSPeter Collingbourne     AllCallSitesDevirted = true;
5032974856aSPeter Collingbourne 
5042974856aSPeter Collingbourne     // As explained in the comment for SummaryTypeCheckedLoadUsers.
5052974856aSPeter Collingbourne     SummaryTypeCheckedLoadUsers.clear();
5062974856aSPeter Collingbourne   }
50750cbd7ccSPeter Collingbourne };
50850cbd7ccSPeter Collingbourne 
50950cbd7ccSPeter Collingbourne // Call site information collected for a specific VTableSlot.
51050cbd7ccSPeter Collingbourne struct VTableSlotInfo {
51150cbd7ccSPeter Collingbourne   // The set of call sites which do not have all constant integer arguments
51250cbd7ccSPeter Collingbourne   // (excluding "this").
51350cbd7ccSPeter Collingbourne   CallSiteInfo CSInfo;
51450cbd7ccSPeter Collingbourne 
51550cbd7ccSPeter Collingbourne   // The set of call sites with all constant integer arguments (excluding
51650cbd7ccSPeter Collingbourne   // "this"), grouped by argument list.
51750cbd7ccSPeter Collingbourne   std::map<std::vector<uint64_t>, CallSiteInfo> ConstCSInfo;
51850cbd7ccSPeter Collingbourne 
519cea6f4d5SMircea Trofin   void addCallSite(Value *VTable, CallBase &CB, unsigned *NumUnsafeUses);
52050cbd7ccSPeter Collingbourne 
52150cbd7ccSPeter Collingbourne private:
522cea6f4d5SMircea Trofin   CallSiteInfo &findCallSiteInfo(CallBase &CB);
52350cbd7ccSPeter Collingbourne };
52450cbd7ccSPeter Collingbourne 
525cea6f4d5SMircea Trofin CallSiteInfo &VTableSlotInfo::findCallSiteInfo(CallBase &CB) {
52650cbd7ccSPeter Collingbourne   std::vector<uint64_t> Args;
527cea6f4d5SMircea Trofin   auto *CBType = dyn_cast<IntegerType>(CB.getType());
528cea6f4d5SMircea Trofin   if (!CBType || CBType->getBitWidth() > 64 || CB.arg_empty())
52950cbd7ccSPeter Collingbourne     return CSInfo;
53023b0ab2aSKazu Hirata   for (auto &&Arg : drop_begin(CB.args())) {
53150cbd7ccSPeter Collingbourne     auto *CI = dyn_cast<ConstantInt>(Arg);
53250cbd7ccSPeter Collingbourne     if (!CI || CI->getBitWidth() > 64)
53350cbd7ccSPeter Collingbourne       return CSInfo;
53450cbd7ccSPeter Collingbourne     Args.push_back(CI->getZExtValue());
53550cbd7ccSPeter Collingbourne   }
53650cbd7ccSPeter Collingbourne   return ConstCSInfo[Args];
53750cbd7ccSPeter Collingbourne }
53850cbd7ccSPeter Collingbourne 
539cea6f4d5SMircea Trofin void VTableSlotInfo::addCallSite(Value *VTable, CallBase &CB,
54050cbd7ccSPeter Collingbourne                                  unsigned *NumUnsafeUses) {
541cea6f4d5SMircea Trofin   auto &CSI = findCallSiteInfo(CB);
5422974856aSPeter Collingbourne   CSI.AllCallSitesDevirted = false;
543cea6f4d5SMircea Trofin   CSI.CallSites.push_back({VTable, CB, NumUnsafeUses});
54450cbd7ccSPeter Collingbourne }
54550cbd7ccSPeter Collingbourne 
546df49d1bbSPeter Collingbourne struct DevirtModule {
547df49d1bbSPeter Collingbourne   Module &M;
54837317f12SPeter Collingbourne   function_ref<AAResults &(Function &)> AARGetter;
549f24136f1STeresa Johnson   function_ref<DominatorTree &(Function &)> LookupDomTree;
5502b33f653SPeter Collingbourne 
551f7691d8bSPeter Collingbourne   ModuleSummaryIndex *ExportSummary;
552f7691d8bSPeter Collingbourne   const ModuleSummaryIndex *ImportSummary;
5532b33f653SPeter Collingbourne 
554df49d1bbSPeter Collingbourne   IntegerType *Int8Ty;
555df49d1bbSPeter Collingbourne   PointerType *Int8PtrTy;
556df49d1bbSPeter Collingbourne   IntegerType *Int32Ty;
55750cbd7ccSPeter Collingbourne   IntegerType *Int64Ty;
55814dcf02fSPeter Collingbourne   IntegerType *IntPtrTy;
559cc5c5888SBob Haarman   /// Sizeless array type, used for imported vtables. This provides a signal
560cc5c5888SBob Haarman   /// to analyzers that these imports may alias, as they do for example
561cc5c5888SBob Haarman   /// when multiple unique return values occur in the same vtable.
562cc5c5888SBob Haarman   ArrayType *Int8Arr0Ty;
563df49d1bbSPeter Collingbourne 
564f3403fd2SIvan Krasin   bool RemarksEnabled;
565e963c89dSSam Elliott   function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter;
566f3403fd2SIvan Krasin 
56750cbd7ccSPeter Collingbourne   MapVector<VTableSlot, VTableSlotInfo> CallSlots;
568df49d1bbSPeter Collingbourne 
5697110510eSArthur Eubanks   // Calls that have already been optimized. We may add a call to multiple
5707110510eSArthur Eubanks   // VTableSlotInfos if vtable loads are coalesced and need to make sure not to
5717110510eSArthur Eubanks   // optimize a call more than once.
5727110510eSArthur Eubanks   SmallPtrSet<CallBase *, 8> OptimizedCalls;
5737110510eSArthur Eubanks 
5740312f614SPeter Collingbourne   // This map keeps track of the number of "unsafe" uses of a loaded function
5750312f614SPeter Collingbourne   // pointer. The key is the associated llvm.type.test intrinsic call generated
5760312f614SPeter Collingbourne   // by this pass. An unsafe use is one that calls the loaded function pointer
5770312f614SPeter Collingbourne   // directly. Every time we eliminate an unsafe use (for example, by
5780312f614SPeter Collingbourne   // devirtualizing it or by applying virtual constant propagation), we
5790312f614SPeter Collingbourne   // decrement the value stored in this map. If a value reaches zero, we can
5800312f614SPeter Collingbourne   // eliminate the type check by RAUWing the associated llvm.type.test call with
5810312f614SPeter Collingbourne   // true.
5820312f614SPeter Collingbourne   std::map<CallInst *, unsigned> NumUnsafeUsesForTypeTest;
5836d2032e2Sevgeny   PatternList FunctionsToSkip;
5840312f614SPeter Collingbourne 
58537317f12SPeter Collingbourne   DevirtModule(Module &M, function_ref<AAResults &(Function &)> AARGetter,
586e963c89dSSam Elliott                function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter,
587f24136f1STeresa Johnson                function_ref<DominatorTree &(Function &)> LookupDomTree,
588f7691d8bSPeter Collingbourne                ModuleSummaryIndex *ExportSummary,
589f7691d8bSPeter Collingbourne                const ModuleSummaryIndex *ImportSummary)
590f24136f1STeresa Johnson       : M(M), AARGetter(AARGetter), LookupDomTree(LookupDomTree),
591f24136f1STeresa Johnson         ExportSummary(ExportSummary), ImportSummary(ImportSummary),
592f24136f1STeresa Johnson         Int8Ty(Type::getInt8Ty(M.getContext())),
593df49d1bbSPeter Collingbourne         Int8PtrTy(Type::getInt8PtrTy(M.getContext())),
594f3403fd2SIvan Krasin         Int32Ty(Type::getInt32Ty(M.getContext())),
59550cbd7ccSPeter Collingbourne         Int64Ty(Type::getInt64Ty(M.getContext())),
59614dcf02fSPeter Collingbourne         IntPtrTy(M.getDataLayout().getIntPtrType(M.getContext(), 0)),
597cc5c5888SBob Haarman         Int8Arr0Ty(ArrayType::get(Type::getInt8Ty(M.getContext()), 0)),
598e963c89dSSam Elliott         RemarksEnabled(areRemarksEnabled()), OREGetter(OREGetter) {
599f7691d8bSPeter Collingbourne     assert(!(ExportSummary && ImportSummary));
6006d2032e2Sevgeny     FunctionsToSkip.init(SkipFunctionNames);
601f7691d8bSPeter Collingbourne   }
602f3403fd2SIvan Krasin 
603f3403fd2SIvan Krasin   bool areRemarksEnabled();
604df49d1bbSPeter Collingbourne 
6056014c46cSTeresa Johnson   void
6066014c46cSTeresa Johnson   scanTypeTestUsers(Function *TypeTestFunc,
6076014c46cSTeresa Johnson                     DenseMap<Metadata *, std::set<TypeMemberInfo>> &TypeIdMap);
6080312f614SPeter Collingbourne   void scanTypeCheckedLoadUsers(Function *TypeCheckedLoadFunc);
6090312f614SPeter Collingbourne 
6107efd7506SPeter Collingbourne   void buildTypeIdentifierMap(
6117efd7506SPeter Collingbourne       std::vector<VTableBits> &Bits,
6127efd7506SPeter Collingbourne       DenseMap<Metadata *, std::set<TypeMemberInfo>> &TypeIdMap);
61309a704c5SMingming Liu 
6147efd7506SPeter Collingbourne   bool
6157efd7506SPeter Collingbourne   tryFindVirtualCallTargets(std::vector<VirtualCallTarget> &TargetsForSlot,
6167efd7506SPeter Collingbourne                             const std::set<TypeMemberInfo> &TypeMemberInfos,
61709a704c5SMingming Liu                             uint64_t ByteOffset,
61809a704c5SMingming Liu                             ModuleSummaryIndex *ExportSummary);
61950cbd7ccSPeter Collingbourne 
6202325bb34SPeter Collingbourne   void applySingleImplDevirt(VTableSlotInfo &SlotInfo, Constant *TheFn,
6212325bb34SPeter Collingbourne                              bool &IsExported);
622943afb57SEugene Leviant   bool trySingleImplDevirt(ModuleSummaryIndex *ExportSummary,
623943afb57SEugene Leviant                            MutableArrayRef<VirtualCallTarget> TargetsForSlot,
6242325bb34SPeter Collingbourne                            VTableSlotInfo &SlotInfo,
6252325bb34SPeter Collingbourne                            WholeProgramDevirtResolution *Res);
62650cbd7ccSPeter Collingbourne 
6272974856aSPeter Collingbourne   void applyICallBranchFunnel(VTableSlotInfo &SlotInfo, Constant *JT,
6282974856aSPeter Collingbourne                               bool &IsExported);
6292974856aSPeter Collingbourne   void tryICallBranchFunnel(MutableArrayRef<VirtualCallTarget> TargetsForSlot,
6302974856aSPeter Collingbourne                             VTableSlotInfo &SlotInfo,
6312974856aSPeter Collingbourne                             WholeProgramDevirtResolution *Res, VTableSlot Slot);
6322974856aSPeter Collingbourne 
633df49d1bbSPeter Collingbourne   bool tryEvaluateFunctionsWithArgs(
634df49d1bbSPeter Collingbourne       MutableArrayRef<VirtualCallTarget> TargetsForSlot,
63550cbd7ccSPeter Collingbourne       ArrayRef<uint64_t> Args);
63650cbd7ccSPeter Collingbourne 
63750cbd7ccSPeter Collingbourne   void applyUniformRetValOpt(CallSiteInfo &CSInfo, StringRef FnName,
63850cbd7ccSPeter Collingbourne                              uint64_t TheRetVal);
63950cbd7ccSPeter Collingbourne   bool tryUniformRetValOpt(MutableArrayRef<VirtualCallTarget> TargetsForSlot,
64077a8d563SPeter Collingbourne                            CallSiteInfo &CSInfo,
64177a8d563SPeter Collingbourne                            WholeProgramDevirtResolution::ByArg *Res);
64250cbd7ccSPeter Collingbourne 
64359675ba0SPeter Collingbourne   // Returns the global symbol name that is used to export information about the
64459675ba0SPeter Collingbourne   // given vtable slot and list of arguments.
64559675ba0SPeter Collingbourne   std::string getGlobalName(VTableSlot Slot, ArrayRef<uint64_t> Args,
64659675ba0SPeter Collingbourne                             StringRef Name);
64759675ba0SPeter Collingbourne 
648b15a35e6SPeter Collingbourne   bool shouldExportConstantsAsAbsoluteSymbols();
649b15a35e6SPeter Collingbourne 
65059675ba0SPeter Collingbourne   // This function is called during the export phase to create a symbol
65159675ba0SPeter Collingbourne   // definition containing information about the given vtable slot and list of
65259675ba0SPeter Collingbourne   // arguments.
65359675ba0SPeter Collingbourne   void exportGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args, StringRef Name,
65459675ba0SPeter Collingbourne                     Constant *C);
655b15a35e6SPeter Collingbourne   void exportConstant(VTableSlot Slot, ArrayRef<uint64_t> Args, StringRef Name,
656b15a35e6SPeter Collingbourne                       uint32_t Const, uint32_t &Storage);
65759675ba0SPeter Collingbourne 
65859675ba0SPeter Collingbourne   // This function is called during the import phase to create a reference to
65959675ba0SPeter Collingbourne   // the symbol definition created during the export phase.
66059675ba0SPeter Collingbourne   Constant *importGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args,
661b15a35e6SPeter Collingbourne                          StringRef Name);
662b15a35e6SPeter Collingbourne   Constant *importConstant(VTableSlot Slot, ArrayRef<uint64_t> Args,
663b15a35e6SPeter Collingbourne                            StringRef Name, IntegerType *IntTy,
664b15a35e6SPeter Collingbourne                            uint32_t Storage);
66559675ba0SPeter Collingbourne 
6662974856aSPeter Collingbourne   Constant *getMemberAddr(const TypeMemberInfo *M);
6672974856aSPeter Collingbourne 
66850cbd7ccSPeter Collingbourne   void applyUniqueRetValOpt(CallSiteInfo &CSInfo, StringRef FnName, bool IsOne,
66950cbd7ccSPeter Collingbourne                             Constant *UniqueMemberAddr);
670df49d1bbSPeter Collingbourne   bool tryUniqueRetValOpt(unsigned BitWidth,
671f3403fd2SIvan Krasin                           MutableArrayRef<VirtualCallTarget> TargetsForSlot,
67259675ba0SPeter Collingbourne                           CallSiteInfo &CSInfo,
67359675ba0SPeter Collingbourne                           WholeProgramDevirtResolution::ByArg *Res,
67459675ba0SPeter Collingbourne                           VTableSlot Slot, ArrayRef<uint64_t> Args);
67550cbd7ccSPeter Collingbourne 
67650cbd7ccSPeter Collingbourne   void applyVirtualConstProp(CallSiteInfo &CSInfo, StringRef FnName,
67750cbd7ccSPeter Collingbourne                              Constant *Byte, Constant *Bit);
678df49d1bbSPeter Collingbourne   bool tryVirtualConstProp(MutableArrayRef<VirtualCallTarget> TargetsForSlot,
67977a8d563SPeter Collingbourne                            VTableSlotInfo &SlotInfo,
68059675ba0SPeter Collingbourne                            WholeProgramDevirtResolution *Res, VTableSlot Slot);
681df49d1bbSPeter Collingbourne 
682df49d1bbSPeter Collingbourne   void rebuildGlobal(VTableBits &B);
683df49d1bbSPeter Collingbourne 
6846d284fabSPeter Collingbourne   // Apply the summary resolution for Slot to all virtual calls in SlotInfo.
6856d284fabSPeter Collingbourne   void importResolution(VTableSlot Slot, VTableSlotInfo &SlotInfo);
6866d284fabSPeter Collingbourne 
6876d284fabSPeter Collingbourne   // If we were able to eliminate all unsafe uses for a type checked load,
6886d284fabSPeter Collingbourne   // eliminate the associated type tests by replacing them with true.
6896d284fabSPeter Collingbourne   void removeRedundantTypeTests();
6906d284fabSPeter Collingbourne 
691df49d1bbSPeter Collingbourne   bool run();
6922b33f653SPeter Collingbourne 
69309a704c5SMingming Liu   // Look up the corresponding ValueInfo entry of `TheFn` in `ExportSummary`.
69409a704c5SMingming Liu   //
69509a704c5SMingming Liu   // Caller guarantees that `ExportSummary` is not nullptr.
69609a704c5SMingming Liu   static ValueInfo lookUpFunctionValueInfo(Function *TheFn,
69709a704c5SMingming Liu                                            ModuleSummaryIndex *ExportSummary);
69809a704c5SMingming Liu 
6999c49f8d7Sminglotus-6   // Returns true if the function definition must be unreachable.
7009c49f8d7Sminglotus-6   //
7019c49f8d7Sminglotus-6   // Note if this helper function returns true, `F` is guaranteed
7029c49f8d7Sminglotus-6   // to be unreachable; if it returns false, `F` might still
7039c49f8d7Sminglotus-6   // be unreachable but not covered by this helper function.
7049c49f8d7Sminglotus-6   //
7059c49f8d7Sminglotus-6   // Implementation-wise, if function definition is present, IR is analyzed; if
7069c49f8d7Sminglotus-6   // not, look up function flags from ExportSummary as a fallback.
7079c49f8d7Sminglotus-6   static bool mustBeUnreachableFunction(Function *const F,
7089c49f8d7Sminglotus-6                                         ModuleSummaryIndex *ExportSummary);
7099c49f8d7Sminglotus-6 
7102b33f653SPeter Collingbourne   // Lower the module using the action and summary passed as command line
7112b33f653SPeter Collingbourne   // arguments. For testing purposes only.
712f24136f1STeresa Johnson   static bool
713f24136f1STeresa Johnson   runForTesting(Module &M, function_ref<AAResults &(Function &)> AARGetter,
714f24136f1STeresa Johnson                 function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter,
715f24136f1STeresa Johnson                 function_ref<DominatorTree &(Function &)> LookupDomTree);
716df49d1bbSPeter Collingbourne };
717df49d1bbSPeter Collingbourne 
718d2df54e6STeresa Johnson struct DevirtIndex {
719d2df54e6STeresa Johnson   ModuleSummaryIndex &ExportSummary;
720d2df54e6STeresa Johnson   // The set in which to record GUIDs exported from their module by
721d2df54e6STeresa Johnson   // devirtualization, used by client to ensure they are not internalized.
722d2df54e6STeresa Johnson   std::set<GlobalValue::GUID> &ExportedGUIDs;
723d2df54e6STeresa Johnson   // A map in which to record the information necessary to locate the WPD
724d2df54e6STeresa Johnson   // resolution for local targets in case they are exported by cross module
725d2df54e6STeresa Johnson   // importing.
726d2df54e6STeresa Johnson   std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap;
727d2df54e6STeresa Johnson 
728d2df54e6STeresa Johnson   MapVector<VTableSlotSummary, VTableSlotInfo> CallSlots;
729d2df54e6STeresa Johnson 
7306d2032e2Sevgeny   PatternList FunctionsToSkip;
7316d2032e2Sevgeny 
732d2df54e6STeresa Johnson   DevirtIndex(
733d2df54e6STeresa Johnson       ModuleSummaryIndex &ExportSummary,
734d2df54e6STeresa Johnson       std::set<GlobalValue::GUID> &ExportedGUIDs,
735d2df54e6STeresa Johnson       std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap)
736d2df54e6STeresa Johnson       : ExportSummary(ExportSummary), ExportedGUIDs(ExportedGUIDs),
7376d2032e2Sevgeny         LocalWPDTargetsMap(LocalWPDTargetsMap) {
7386d2032e2Sevgeny     FunctionsToSkip.init(SkipFunctionNames);
7396d2032e2Sevgeny   }
740d2df54e6STeresa Johnson 
741d2df54e6STeresa Johnson   bool tryFindVirtualCallTargets(std::vector<ValueInfo> &TargetsForSlot,
742d2df54e6STeresa Johnson                                  const TypeIdCompatibleVtableInfo TIdInfo,
743d2df54e6STeresa Johnson                                  uint64_t ByteOffset);
744d2df54e6STeresa Johnson 
745d2df54e6STeresa Johnson   bool trySingleImplDevirt(MutableArrayRef<ValueInfo> TargetsForSlot,
746d2df54e6STeresa Johnson                            VTableSlotSummary &SlotSummary,
747d2df54e6STeresa Johnson                            VTableSlotInfo &SlotInfo,
748d2df54e6STeresa Johnson                            WholeProgramDevirtResolution *Res,
749d2df54e6STeresa Johnson                            std::set<ValueInfo> &DevirtTargets);
750d2df54e6STeresa Johnson 
751d2df54e6STeresa Johnson   void run();
752d2df54e6STeresa Johnson };
753d2df54e6STeresa Johnson 
754df49d1bbSPeter Collingbourne struct WholeProgramDevirt : public ModulePass {
755df49d1bbSPeter Collingbourne   static char ID;
756cdc71612SEugene Zelenko 
7572b33f653SPeter Collingbourne   bool UseCommandLine = false;
7582b33f653SPeter Collingbourne 
75939c0829aSSimon Pilgrim   ModuleSummaryIndex *ExportSummary = nullptr;
76039c0829aSSimon Pilgrim   const ModuleSummaryIndex *ImportSummary = nullptr;
7612b33f653SPeter Collingbourne 
7622b33f653SPeter Collingbourne   WholeProgramDevirt() : ModulePass(ID), UseCommandLine(true) {
7632b33f653SPeter Collingbourne     initializeWholeProgramDevirtPass(*PassRegistry::getPassRegistry());
7642b33f653SPeter Collingbourne   }
7652b33f653SPeter Collingbourne 
766f7691d8bSPeter Collingbourne   WholeProgramDevirt(ModuleSummaryIndex *ExportSummary,
767f7691d8bSPeter Collingbourne                      const ModuleSummaryIndex *ImportSummary)
768f7691d8bSPeter Collingbourne       : ModulePass(ID), ExportSummary(ExportSummary),
769f7691d8bSPeter Collingbourne         ImportSummary(ImportSummary) {
770df49d1bbSPeter Collingbourne     initializeWholeProgramDevirtPass(*PassRegistry::getPassRegistry());
771df49d1bbSPeter Collingbourne   }
772cdc71612SEugene Zelenko 
773cdc71612SEugene Zelenko   bool runOnModule(Module &M) override {
774aa641a51SAndrew Kaylor     if (skipModule(M))
775aa641a51SAndrew Kaylor       return false;
776e963c89dSSam Elliott 
7779110cb45SPeter Collingbourne     // In the new pass manager, we can request the optimization
7789110cb45SPeter Collingbourne     // remark emitter pass on a per-function-basis, which the
7799110cb45SPeter Collingbourne     // OREGetter will do for us.
7809110cb45SPeter Collingbourne     // In the old pass manager, this is harder, so we just build
7819110cb45SPeter Collingbourne     // an optimization remark emitter on the fly, when we need it.
7829110cb45SPeter Collingbourne     std::unique_ptr<OptimizationRemarkEmitter> ORE;
7839110cb45SPeter Collingbourne     auto OREGetter = [&](Function *F) -> OptimizationRemarkEmitter & {
7840eaee545SJonas Devlieghere       ORE = std::make_unique<OptimizationRemarkEmitter>(F);
7859110cb45SPeter Collingbourne       return *ORE;
7869110cb45SPeter Collingbourne     };
787e963c89dSSam Elliott 
788f24136f1STeresa Johnson     auto LookupDomTree = [this](Function &F) -> DominatorTree & {
789f24136f1STeresa Johnson       return this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
790f24136f1STeresa Johnson     };
791e963c89dSSam Elliott 
792f24136f1STeresa Johnson     if (UseCommandLine)
793f24136f1STeresa Johnson       return DevirtModule::runForTesting(M, LegacyAARGetter(*this), OREGetter,
794f24136f1STeresa Johnson                                          LookupDomTree);
795f24136f1STeresa Johnson 
796f24136f1STeresa Johnson     return DevirtModule(M, LegacyAARGetter(*this), OREGetter, LookupDomTree,
797f24136f1STeresa Johnson                         ExportSummary, ImportSummary)
798f7691d8bSPeter Collingbourne         .run();
79937317f12SPeter Collingbourne   }
80037317f12SPeter Collingbourne 
80137317f12SPeter Collingbourne   void getAnalysisUsage(AnalysisUsage &AU) const override {
80237317f12SPeter Collingbourne     AU.addRequired<AssumptionCacheTracker>();
80337317f12SPeter Collingbourne     AU.addRequired<TargetLibraryInfoWrapperPass>();
804f24136f1STeresa Johnson     AU.addRequired<DominatorTreeWrapperPass>();
805aa641a51SAndrew Kaylor   }
806df49d1bbSPeter Collingbourne };
807df49d1bbSPeter Collingbourne 
808cdc71612SEugene Zelenko } // end anonymous namespace
809df49d1bbSPeter Collingbourne 
81037317f12SPeter Collingbourne INITIALIZE_PASS_BEGIN(WholeProgramDevirt, "wholeprogramdevirt",
81137317f12SPeter Collingbourne                       "Whole program devirtualization", false, false)
81237317f12SPeter Collingbourne INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
81337317f12SPeter Collingbourne INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
814f24136f1STeresa Johnson INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
81537317f12SPeter Collingbourne INITIALIZE_PASS_END(WholeProgramDevirt, "wholeprogramdevirt",
816df49d1bbSPeter Collingbourne                     "Whole program devirtualization", false, false)
817df49d1bbSPeter Collingbourne char WholeProgramDevirt::ID = 0;
818df49d1bbSPeter Collingbourne 
819f7691d8bSPeter Collingbourne ModulePass *
820f7691d8bSPeter Collingbourne llvm::createWholeProgramDevirtPass(ModuleSummaryIndex *ExportSummary,
821f7691d8bSPeter Collingbourne                                    const ModuleSummaryIndex *ImportSummary) {
822f7691d8bSPeter Collingbourne   return new WholeProgramDevirt(ExportSummary, ImportSummary);
823df49d1bbSPeter Collingbourne }
824df49d1bbSPeter Collingbourne 
825164a2aa6SChandler Carruth PreservedAnalyses WholeProgramDevirtPass::run(Module &M,
82637317f12SPeter Collingbourne                                               ModuleAnalysisManager &AM) {
82737317f12SPeter Collingbourne   auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
82837317f12SPeter Collingbourne   auto AARGetter = [&](Function &F) -> AAResults & {
82937317f12SPeter Collingbourne     return FAM.getResult<AAManager>(F);
83037317f12SPeter Collingbourne   };
831e963c89dSSam Elliott   auto OREGetter = [&](Function *F) -> OptimizationRemarkEmitter & {
832e963c89dSSam Elliott     return FAM.getResult<OptimizationRemarkEmitterAnalysis>(*F);
833e963c89dSSam Elliott   };
834f24136f1STeresa Johnson   auto LookupDomTree = [&FAM](Function &F) -> DominatorTree & {
835f24136f1STeresa Johnson     return FAM.getResult<DominatorTreeAnalysis>(F);
836f24136f1STeresa Johnson   };
837460dda07SArthur Eubanks   if (UseCommandLine) {
838460dda07SArthur Eubanks     if (DevirtModule::runForTesting(M, AARGetter, OREGetter, LookupDomTree))
839460dda07SArthur Eubanks       return PreservedAnalyses::all();
840460dda07SArthur Eubanks     return PreservedAnalyses::none();
841460dda07SArthur Eubanks   }
842f24136f1STeresa Johnson   if (!DevirtModule(M, AARGetter, OREGetter, LookupDomTree, ExportSummary,
843f24136f1STeresa Johnson                     ImportSummary)
84428023dbeSTeresa Johnson            .run())
845d737dd2eSDavide Italiano     return PreservedAnalyses::all();
846d737dd2eSDavide Italiano   return PreservedAnalyses::none();
847d737dd2eSDavide Italiano }
848d737dd2eSDavide Italiano 
8492f63d549STeresa Johnson // Enable whole program visibility if enabled by client (e.g. linker) or
8502f63d549STeresa Johnson // internal option, and not force disabled.
8512f63d549STeresa Johnson static bool hasWholeProgramVisibility(bool WholeProgramVisibilityEnabledInLTO) {
8522f63d549STeresa Johnson   return (WholeProgramVisibilityEnabledInLTO || WholeProgramVisibility) &&
8532f63d549STeresa Johnson          !DisableWholeProgramVisibility;
8542f63d549STeresa Johnson }
8552f63d549STeresa Johnson 
856d2df54e6STeresa Johnson namespace llvm {
8572f63d549STeresa Johnson 
8582f63d549STeresa Johnson /// If whole program visibility asserted, then upgrade all public vcall
8592f63d549STeresa Johnson /// visibility metadata on vtable definitions to linkage unit visibility in
8602f63d549STeresa Johnson /// Module IR (for regular or hybrid LTO).
8611487747eSTeresa Johnson void updateVCallVisibilityInModule(
8621487747eSTeresa Johnson     Module &M, bool WholeProgramVisibilityEnabledInLTO,
8631487747eSTeresa Johnson     const DenseSet<GlobalValue::GUID> &DynamicExportSymbols) {
8642f63d549STeresa Johnson   if (!hasWholeProgramVisibility(WholeProgramVisibilityEnabledInLTO))
8652f63d549STeresa Johnson     return;
8662f63d549STeresa Johnson   for (GlobalVariable &GV : M.globals())
8672f63d549STeresa Johnson     // Add linkage unit visibility to any variable with type metadata, which are
8682f63d549STeresa Johnson     // the vtable definitions. We won't have an existing vcall_visibility
8692f63d549STeresa Johnson     // metadata on vtable definitions with public visibility.
8702f63d549STeresa Johnson     if (GV.hasMetadata(LLVMContext::MD_type) &&
8711487747eSTeresa Johnson         GV.getVCallVisibility() == GlobalObject::VCallVisibilityPublic &&
8721487747eSTeresa Johnson         // Don't upgrade the visibility for symbols exported to the dynamic
8731487747eSTeresa Johnson         // linker, as we have no information on their eventual use.
8741487747eSTeresa Johnson         !DynamicExportSymbols.count(GV.getGUID()))
8752f63d549STeresa Johnson       GV.setVCallVisibilityMetadata(GlobalObject::VCallVisibilityLinkageUnit);
8762f63d549STeresa Johnson }
8772f63d549STeresa Johnson 
8782f63d549STeresa Johnson /// If whole program visibility asserted, then upgrade all public vcall
8792f63d549STeresa Johnson /// visibility metadata on vtable definition summaries to linkage unit
8802f63d549STeresa Johnson /// visibility in Module summary index (for ThinLTO).
8811487747eSTeresa Johnson void updateVCallVisibilityInIndex(
8821487747eSTeresa Johnson     ModuleSummaryIndex &Index, bool WholeProgramVisibilityEnabledInLTO,
8831487747eSTeresa Johnson     const DenseSet<GlobalValue::GUID> &DynamicExportSymbols) {
8842f63d549STeresa Johnson   if (!hasWholeProgramVisibility(WholeProgramVisibilityEnabledInLTO))
8852f63d549STeresa Johnson     return;
8862f63d549STeresa Johnson   for (auto &P : Index) {
887dd3f4833STeresa Johnson     // Don't upgrade the visibility for symbols exported to the dynamic
888dd3f4833STeresa Johnson     // linker, as we have no information on their eventual use.
889dd3f4833STeresa Johnson     if (DynamicExportSymbols.count(P.first))
890dd3f4833STeresa Johnson       continue;
8912f63d549STeresa Johnson     for (auto &S : P.second.SummaryList) {
8922f63d549STeresa Johnson       auto *GVar = dyn_cast<GlobalVarSummary>(S.get());
8930a5949dcSTeresa Johnson       if (!GVar ||
894dd3f4833STeresa Johnson           GVar->getVCallVisibility() != GlobalObject::VCallVisibilityPublic)
8952f63d549STeresa Johnson         continue;
8962f63d549STeresa Johnson       GVar->setVCallVisibility(GlobalObject::VCallVisibilityLinkageUnit);
8972f63d549STeresa Johnson     }
8982f63d549STeresa Johnson   }
8992f63d549STeresa Johnson }
9002f63d549STeresa Johnson 
901d2df54e6STeresa Johnson void runWholeProgramDevirtOnIndex(
902d2df54e6STeresa Johnson     ModuleSummaryIndex &Summary, std::set<GlobalValue::GUID> &ExportedGUIDs,
903d2df54e6STeresa Johnson     std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap) {
904d2df54e6STeresa Johnson   DevirtIndex(Summary, ExportedGUIDs, LocalWPDTargetsMap).run();
905d2df54e6STeresa Johnson }
906d2df54e6STeresa Johnson 
907d2df54e6STeresa Johnson void updateIndexWPDForExports(
908d2df54e6STeresa Johnson     ModuleSummaryIndex &Summary,
9093d708bf5Sevgeny     function_ref<bool(StringRef, ValueInfo)> isExported,
910d2df54e6STeresa Johnson     std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap) {
911d2df54e6STeresa Johnson   for (auto &T : LocalWPDTargetsMap) {
912d2df54e6STeresa Johnson     auto &VI = T.first;
913d2df54e6STeresa Johnson     // This was enforced earlier during trySingleImplDevirt.
914d2df54e6STeresa Johnson     assert(VI.getSummaryList().size() == 1 &&
915d2df54e6STeresa Johnson            "Devirt of local target has more than one copy");
916d2df54e6STeresa Johnson     auto &S = VI.getSummaryList()[0];
9173d708bf5Sevgeny     if (!isExported(S->modulePath(), VI))
918d2df54e6STeresa Johnson       continue;
919d2df54e6STeresa Johnson 
920d2df54e6STeresa Johnson     // It's been exported by a cross module import.
921d2df54e6STeresa Johnson     for (auto &SlotSummary : T.second) {
922d2df54e6STeresa Johnson       auto *TIdSum = Summary.getTypeIdSummary(SlotSummary.TypeID);
923d2df54e6STeresa Johnson       assert(TIdSum);
924d2df54e6STeresa Johnson       auto WPDRes = TIdSum->WPDRes.find(SlotSummary.ByteOffset);
925d2df54e6STeresa Johnson       assert(WPDRes != TIdSum->WPDRes.end());
926d2df54e6STeresa Johnson       WPDRes->second.SingleImplName = ModuleSummaryIndex::getGlobalNameForLocal(
927d2df54e6STeresa Johnson           WPDRes->second.SingleImplName,
928d2df54e6STeresa Johnson           Summary.getModuleHash(S->modulePath()));
929d2df54e6STeresa Johnson     }
930d2df54e6STeresa Johnson   }
931d2df54e6STeresa Johnson }
932d2df54e6STeresa Johnson 
933d2df54e6STeresa Johnson } // end namespace llvm
934d2df54e6STeresa Johnson 
9358973fae1SEvgeny Leviant static Error checkCombinedSummaryForTesting(ModuleSummaryIndex *Summary) {
9368973fae1SEvgeny Leviant   // Check that summary index contains regular LTO module when performing
9378973fae1SEvgeny Leviant   // export to prevent occasional use of index from pure ThinLTO compilation
9388973fae1SEvgeny Leviant   // (-fno-split-lto-module). This kind of summary index is passed to
9398973fae1SEvgeny Leviant   // DevirtIndex::run, not to DevirtModule::run used by opt/runForTesting.
9408973fae1SEvgeny Leviant   const auto &ModPaths = Summary->modulePaths();
9418973fae1SEvgeny Leviant   if (ClSummaryAction != PassSummaryAction::Import &&
9428973fae1SEvgeny Leviant       ModPaths.find(ModuleSummaryIndex::getRegularLTOModuleName()) ==
9438973fae1SEvgeny Leviant           ModPaths.end())
9448973fae1SEvgeny Leviant     return createStringError(
9458973fae1SEvgeny Leviant         errc::invalid_argument,
9468973fae1SEvgeny Leviant         "combined summary should contain Regular LTO module");
9478973fae1SEvgeny Leviant   return ErrorSuccess();
9488973fae1SEvgeny Leviant }
9498973fae1SEvgeny Leviant 
95037317f12SPeter Collingbourne bool DevirtModule::runForTesting(
951e963c89dSSam Elliott     Module &M, function_ref<AAResults &(Function &)> AARGetter,
952f24136f1STeresa Johnson     function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter,
953f24136f1STeresa Johnson     function_ref<DominatorTree &(Function &)> LookupDomTree) {
9548973fae1SEvgeny Leviant   std::unique_ptr<ModuleSummaryIndex> Summary =
9558973fae1SEvgeny Leviant       std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
9562b33f653SPeter Collingbourne 
9572b33f653SPeter Collingbourne   // Handle the command-line summary arguments. This code is for testing
9582b33f653SPeter Collingbourne   // purposes only, so we handle errors directly.
9592b33f653SPeter Collingbourne   if (!ClReadSummary.empty()) {
9602b33f653SPeter Collingbourne     ExitOnError ExitOnErr("-wholeprogramdevirt-read-summary: " + ClReadSummary +
9612b33f653SPeter Collingbourne                           ": ");
9622b33f653SPeter Collingbourne     auto ReadSummaryFile =
9632b33f653SPeter Collingbourne         ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ClReadSummary)));
9648973fae1SEvgeny Leviant     if (Expected<std::unique_ptr<ModuleSummaryIndex>> SummaryOrErr =
9658973fae1SEvgeny Leviant             getModuleSummaryIndex(*ReadSummaryFile)) {
9668973fae1SEvgeny Leviant       Summary = std::move(*SummaryOrErr);
9678973fae1SEvgeny Leviant       ExitOnErr(checkCombinedSummaryForTesting(Summary.get()));
9688973fae1SEvgeny Leviant     } else {
9698973fae1SEvgeny Leviant       // Try YAML if we've failed with bitcode.
9708973fae1SEvgeny Leviant       consumeError(SummaryOrErr.takeError());
9712b33f653SPeter Collingbourne       yaml::Input In(ReadSummaryFile->getBuffer());
9728973fae1SEvgeny Leviant       In >> *Summary;
9732b33f653SPeter Collingbourne       ExitOnErr(errorCodeToError(In.error()));
9742b33f653SPeter Collingbourne     }
9758973fae1SEvgeny Leviant   }
9762b33f653SPeter Collingbourne 
977f7691d8bSPeter Collingbourne   bool Changed =
9788973fae1SEvgeny Leviant       DevirtModule(M, AARGetter, OREGetter, LookupDomTree,
9798973fae1SEvgeny Leviant                    ClSummaryAction == PassSummaryAction::Export ? Summary.get()
9808973fae1SEvgeny Leviant                                                                 : nullptr,
9818973fae1SEvgeny Leviant                    ClSummaryAction == PassSummaryAction::Import ? Summary.get()
9828973fae1SEvgeny Leviant                                                                 : nullptr)
983f7691d8bSPeter Collingbourne           .run();
9842b33f653SPeter Collingbourne 
9852b33f653SPeter Collingbourne   if (!ClWriteSummary.empty()) {
9862b33f653SPeter Collingbourne     ExitOnError ExitOnErr(
9872b33f653SPeter Collingbourne         "-wholeprogramdevirt-write-summary: " + ClWriteSummary + ": ");
9882b33f653SPeter Collingbourne     std::error_code EC;
9898973fae1SEvgeny Leviant     if (StringRef(ClWriteSummary).endswith(".bc")) {
9908973fae1SEvgeny Leviant       raw_fd_ostream OS(ClWriteSummary, EC, sys::fs::OF_None);
9918973fae1SEvgeny Leviant       ExitOnErr(errorCodeToError(EC));
9927aaf024dSFangrui Song       writeIndexToFile(*Summary, OS);
9938973fae1SEvgeny Leviant     } else {
99482b3e28eSAbhina Sreeskantharajan       raw_fd_ostream OS(ClWriteSummary, EC, sys::fs::OF_TextWithCRLF);
9952b33f653SPeter Collingbourne       ExitOnErr(errorCodeToError(EC));
9962b33f653SPeter Collingbourne       yaml::Output Out(OS);
9978973fae1SEvgeny Leviant       Out << *Summary;
9988973fae1SEvgeny Leviant     }
9992b33f653SPeter Collingbourne   }
10002b33f653SPeter Collingbourne 
10012b33f653SPeter Collingbourne   return Changed;
10022b33f653SPeter Collingbourne }
10032b33f653SPeter Collingbourne 
10047efd7506SPeter Collingbourne void DevirtModule::buildTypeIdentifierMap(
1005df49d1bbSPeter Collingbourne     std::vector<VTableBits> &Bits,
10067efd7506SPeter Collingbourne     DenseMap<Metadata *, std::set<TypeMemberInfo>> &TypeIdMap) {
1007df49d1bbSPeter Collingbourne   DenseMap<GlobalVariable *, VTableBits *> GVToBits;
10087efd7506SPeter Collingbourne   Bits.reserve(M.getGlobalList().size());
10097efd7506SPeter Collingbourne   SmallVector<MDNode *, 2> Types;
10107efd7506SPeter Collingbourne   for (GlobalVariable &GV : M.globals()) {
10117efd7506SPeter Collingbourne     Types.clear();
10127efd7506SPeter Collingbourne     GV.getMetadata(LLVMContext::MD_type, Types);
10132b70d616SEugene Leviant     if (GV.isDeclaration() || Types.empty())
1014df49d1bbSPeter Collingbourne       continue;
1015df49d1bbSPeter Collingbourne 
10167efd7506SPeter Collingbourne     VTableBits *&BitsPtr = GVToBits[&GV];
10177efd7506SPeter Collingbourne     if (!BitsPtr) {
10187efd7506SPeter Collingbourne       Bits.emplace_back();
10197efd7506SPeter Collingbourne       Bits.back().GV = &GV;
10207efd7506SPeter Collingbourne       Bits.back().ObjectSize =
10217efd7506SPeter Collingbourne           M.getDataLayout().getTypeAllocSize(GV.getInitializer()->getType());
10227efd7506SPeter Collingbourne       BitsPtr = &Bits.back();
10237efd7506SPeter Collingbourne     }
10247efd7506SPeter Collingbourne 
10257efd7506SPeter Collingbourne     for (MDNode *Type : Types) {
10267efd7506SPeter Collingbourne       auto TypeID = Type->getOperand(1).get();
1027df49d1bbSPeter Collingbourne 
1028df49d1bbSPeter Collingbourne       uint64_t Offset =
1029df49d1bbSPeter Collingbourne           cast<ConstantInt>(
10307efd7506SPeter Collingbourne               cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
1031df49d1bbSPeter Collingbourne               ->getZExtValue();
1032df49d1bbSPeter Collingbourne 
10337efd7506SPeter Collingbourne       TypeIdMap[TypeID].insert({BitsPtr, Offset});
1034df49d1bbSPeter Collingbourne     }
1035df49d1bbSPeter Collingbourne   }
1036df49d1bbSPeter Collingbourne }
1037df49d1bbSPeter Collingbourne 
1038df49d1bbSPeter Collingbourne bool DevirtModule::tryFindVirtualCallTargets(
1039df49d1bbSPeter Collingbourne     std::vector<VirtualCallTarget> &TargetsForSlot,
104009a704c5SMingming Liu     const std::set<TypeMemberInfo> &TypeMemberInfos, uint64_t ByteOffset,
104109a704c5SMingming Liu     ModuleSummaryIndex *ExportSummary) {
10427efd7506SPeter Collingbourne   for (const TypeMemberInfo &TM : TypeMemberInfos) {
10437efd7506SPeter Collingbourne     if (!TM.Bits->GV->isConstant())
1044df49d1bbSPeter Collingbourne       return false;
1045df49d1bbSPeter Collingbourne 
10462f63d549STeresa Johnson     // We cannot perform whole program devirtualization analysis on a vtable
10472f63d549STeresa Johnson     // with public LTO visibility.
10482f63d549STeresa Johnson     if (TM.Bits->GV->getVCallVisibility() ==
10492f63d549STeresa Johnson         GlobalObject::VCallVisibilityPublic)
10502f63d549STeresa Johnson       return false;
10512f63d549STeresa Johnson 
10528786754cSPeter Collingbourne     Constant *Ptr = getPointerAtOffset(TM.Bits->GV->getInitializer(),
10533b598b9cSOliver Stannard                                        TM.Offset + ByteOffset, M);
10548786754cSPeter Collingbourne     if (!Ptr)
1055df49d1bbSPeter Collingbourne       return false;
1056df49d1bbSPeter Collingbourne 
10578786754cSPeter Collingbourne     auto Fn = dyn_cast<Function>(Ptr->stripPointerCasts());
1058df49d1bbSPeter Collingbourne     if (!Fn)
1059df49d1bbSPeter Collingbourne       return false;
1060df49d1bbSPeter Collingbourne 
10616d2032e2Sevgeny     if (FunctionsToSkip.match(Fn->getName()))
10626d2032e2Sevgeny       return false;
10636d2032e2Sevgeny 
1064df49d1bbSPeter Collingbourne     // We can disregard __cxa_pure_virtual as a possible call target, as
1065df49d1bbSPeter Collingbourne     // calls to pure virtuals are UB.
1066df49d1bbSPeter Collingbourne     if (Fn->getName() == "__cxa_pure_virtual")
1067df49d1bbSPeter Collingbourne       continue;
1068df49d1bbSPeter Collingbourne 
106909a704c5SMingming Liu     // We can disregard unreachable functions as possible call targets, as
107009a704c5SMingming Liu     // unreachable functions shouldn't be called.
10719c49f8d7Sminglotus-6     if (mustBeUnreachableFunction(Fn, ExportSummary))
107209a704c5SMingming Liu       continue;
107309a704c5SMingming Liu 
10747efd7506SPeter Collingbourne     TargetsForSlot.push_back({Fn, &TM});
1075df49d1bbSPeter Collingbourne   }
1076df49d1bbSPeter Collingbourne 
1077df49d1bbSPeter Collingbourne   // Give up if we couldn't find any targets.
1078df49d1bbSPeter Collingbourne   return !TargetsForSlot.empty();
1079df49d1bbSPeter Collingbourne }
1080df49d1bbSPeter Collingbourne 
1081d2df54e6STeresa Johnson bool DevirtIndex::tryFindVirtualCallTargets(
1082d2df54e6STeresa Johnson     std::vector<ValueInfo> &TargetsForSlot, const TypeIdCompatibleVtableInfo TIdInfo,
1083d2df54e6STeresa Johnson     uint64_t ByteOffset) {
1084098d3347SMark de Wever   for (const TypeIdOffsetVtableInfo &P : TIdInfo) {
10853c4c2050STeresa Johnson     // Find a representative copy of the vtable initializer.
1086c844f884STeresa Johnson     // We can have multiple available_externally, linkonce_odr and weak_odr
10870a5949dcSTeresa Johnson     // vtable initializers. We can also have multiple external vtable
10880a5949dcSTeresa Johnson     // initializers in the case of comdats, which we cannot check here.
10892cefb939STeresa Johnson     // The linker should give an error in this case.
1090c844f884STeresa Johnson     //
1091c844f884STeresa Johnson     // Also, handle the case of same-named local Vtables with the same path
1092c844f884STeresa Johnson     // and therefore the same GUID. This can happen if there isn't enough
1093c844f884STeresa Johnson     // distinguishing path when compiling the source file. In that case we
1094c844f884STeresa Johnson     // conservatively return false early.
1095c844f884STeresa Johnson     const GlobalVarSummary *VS = nullptr;
1096c844f884STeresa Johnson     bool LocalFound = false;
1097c844f884STeresa Johnson     for (auto &S : P.VTableVI.getSummaryList()) {
1098c844f884STeresa Johnson       if (GlobalValue::isLocalLinkage(S->linkage())) {
1099c844f884STeresa Johnson         if (LocalFound)
1100c844f884STeresa Johnson           return false;
1101c844f884STeresa Johnson         LocalFound = true;
1102c844f884STeresa Johnson       }
11033c4c2050STeresa Johnson       auto *CurVS = cast<GlobalVarSummary>(S->getBaseObject());
11040a5949dcSTeresa Johnson       if (!CurVS->vTableFuncs().empty() ||
11050a5949dcSTeresa Johnson           // Previously clang did not attach the necessary type metadata to
11060a5949dcSTeresa Johnson           // available_externally vtables, in which case there would not
11070a5949dcSTeresa Johnson           // be any vtable functions listed in the summary and we need
11080a5949dcSTeresa Johnson           // to treat this case conservatively (in case the bitcode is old).
11090a5949dcSTeresa Johnson           // However, we will also not have any vtable functions in the
11100a5949dcSTeresa Johnson           // case of a pure virtual base class. In that case we do want
11110a5949dcSTeresa Johnson           // to set VS to avoid treating it conservatively.
11120a5949dcSTeresa Johnson           !GlobalValue::isAvailableExternallyLinkage(S->linkage())) {
11133c4c2050STeresa Johnson         VS = CurVS;
11142f63d549STeresa Johnson         // We cannot perform whole program devirtualization analysis on a vtable
11152f63d549STeresa Johnson         // with public LTO visibility.
11162f63d549STeresa Johnson         if (VS->getVCallVisibility() == GlobalObject::VCallVisibilityPublic)
11172f63d549STeresa Johnson           return false;
11180a5949dcSTeresa Johnson       }
11192f63d549STeresa Johnson     }
11203c4c2050STeresa Johnson     // There will be no VS if all copies are available_externally having no
11213c4c2050STeresa Johnson     // type metadata. In that case we can't safely perform WPD.
11223c4c2050STeresa Johnson     if (!VS)
11233c4c2050STeresa Johnson       return false;
1124c844f884STeresa Johnson     if (!VS->isLive())
1125d2df54e6STeresa Johnson       continue;
1126d2df54e6STeresa Johnson     for (auto VTP : VS->vTableFuncs()) {
1127d2df54e6STeresa Johnson       if (VTP.VTableOffset != P.AddressPointOffset + ByteOffset)
1128d2df54e6STeresa Johnson         continue;
1129d2df54e6STeresa Johnson 
11304ab5527cSminglotus-6       if (mustBeUnreachableFunction(VTP.FuncVI))
11314ab5527cSminglotus-6         continue;
11324ab5527cSminglotus-6 
1133d2df54e6STeresa Johnson       TargetsForSlot.push_back(VTP.FuncVI);
1134d2df54e6STeresa Johnson     }
1135d2df54e6STeresa Johnson   }
1136d2df54e6STeresa Johnson 
1137d2df54e6STeresa Johnson   // Give up if we couldn't find any targets.
1138d2df54e6STeresa Johnson   return !TargetsForSlot.empty();
1139d2df54e6STeresa Johnson }
1140d2df54e6STeresa Johnson 
114150cbd7ccSPeter Collingbourne void DevirtModule::applySingleImplDevirt(VTableSlotInfo &SlotInfo,
11422325bb34SPeter Collingbourne                                          Constant *TheFn, bool &IsExported) {
11436e4c1cf2STeresa Johnson   // Don't devirtualize function if we're told to skip it
11446e4c1cf2STeresa Johnson   // in -wholeprogramdevirt-skip.
11456e4c1cf2STeresa Johnson   if (FunctionsToSkip.match(TheFn->stripPointerCasts()->getName()))
11466e4c1cf2STeresa Johnson     return;
114750cbd7ccSPeter Collingbourne   auto Apply = [&](CallSiteInfo &CSInfo) {
114850cbd7ccSPeter Collingbourne     for (auto &&VCallSite : CSInfo.CallSites) {
11497110510eSArthur Eubanks       if (!OptimizedCalls.insert(&VCallSite.CB).second)
11507110510eSArthur Eubanks         continue;
11517110510eSArthur Eubanks 
1152f3403fd2SIvan Krasin       if (RemarksEnabled)
1153b0a1d3bdSTeresa Johnson         VCallSite.emitRemark("single-impl",
1154b0a1d3bdSTeresa Johnson                              TheFn->stripPointerCasts()->getName(), OREGetter);
1155ced9a795STeresa Johnson       NumSingleImpl++;
1156d55d46f4STeresa Johnson       auto &CB = VCallSite.CB;
11577110510eSArthur Eubanks       assert(!CB.getCalledFunction() && "devirtualizing direct call?");
1158d55d46f4STeresa Johnson       IRBuilder<> Builder(&CB);
1159d55d46f4STeresa Johnson       Value *Callee =
1160d55d46f4STeresa Johnson           Builder.CreateBitCast(TheFn, CB.getCalledOperand()->getType());
1161d55d46f4STeresa Johnson 
1162fee0bde4STeresa Johnson       // If trap checking is enabled, add support to compare the virtual
1163fee0bde4STeresa Johnson       // function pointer to the devirtualized target. In case of a mismatch,
1164fee0bde4STeresa Johnson       // perform a debug trap.
1165fee0bde4STeresa Johnson       if (DevirtCheckMode == WPDCheckMode::Trap) {
1166d55d46f4STeresa Johnson         auto *Cond = Builder.CreateICmpNE(CB.getCalledOperand(), Callee);
1167d55d46f4STeresa Johnson         Instruction *ThenTerm =
1168d55d46f4STeresa Johnson             SplitBlockAndInsertIfThen(Cond, &CB, /*Unreachable=*/false);
1169d55d46f4STeresa Johnson         Builder.SetInsertPoint(ThenTerm);
1170d55d46f4STeresa Johnson         Function *TrapFn = Intrinsic::getDeclaration(&M, Intrinsic::debugtrap);
1171d55d46f4STeresa Johnson         auto *CallTrap = Builder.CreateCall(TrapFn);
1172d55d46f4STeresa Johnson         CallTrap->setDebugLoc(CB.getDebugLoc());
1173d55d46f4STeresa Johnson       }
1174d55d46f4STeresa Johnson 
1175fee0bde4STeresa Johnson       // If fallback checking is enabled, add support to compare the virtual
1176fee0bde4STeresa Johnson       // function pointer to the devirtualized target. In case of a mismatch,
1177fee0bde4STeresa Johnson       // fall back to indirect call.
1178fee0bde4STeresa Johnson       if (DevirtCheckMode == WPDCheckMode::Fallback) {
1179fee0bde4STeresa Johnson         MDNode *Weights =
1180fee0bde4STeresa Johnson             MDBuilder(M.getContext()).createBranchWeights((1U << 20) - 1, 1);
1181fee0bde4STeresa Johnson         // Version the indirect call site. If the called value is equal to the
1182fee0bde4STeresa Johnson         // given callee, 'NewInst' will be executed, otherwise the original call
1183fee0bde4STeresa Johnson         // site will be executed.
1184fee0bde4STeresa Johnson         CallBase &NewInst = versionCallSite(CB, Callee, Weights);
1185fee0bde4STeresa Johnson         NewInst.setCalledOperand(Callee);
1186fee0bde4STeresa Johnson         // Since the new call site is direct, we must clear metadata that
1187fee0bde4STeresa Johnson         // is only appropriate for indirect calls. This includes !prof and
1188fee0bde4STeresa Johnson         // !callees metadata.
1189fee0bde4STeresa Johnson         NewInst.setMetadata(LLVMContext::MD_prof, nullptr);
1190fee0bde4STeresa Johnson         NewInst.setMetadata(LLVMContext::MD_callees, nullptr);
1191fee0bde4STeresa Johnson         // Additionally, we should remove them from the fallback indirect call,
1192fee0bde4STeresa Johnson         // so that we don't attempt to perform indirect call promotion later.
1193fee0bde4STeresa Johnson         CB.setMetadata(LLVMContext::MD_prof, nullptr);
1194fee0bde4STeresa Johnson         CB.setMetadata(LLVMContext::MD_callees, nullptr);
1195fee0bde4STeresa Johnson       }
1196fee0bde4STeresa Johnson 
1197fee0bde4STeresa Johnson       // In either trapping or non-checking mode, devirtualize original call.
1198fee0bde4STeresa Johnson       else {
1199fee0bde4STeresa Johnson         // Devirtualize unconditionally.
1200d55d46f4STeresa Johnson         CB.setCalledOperand(Callee);
1201fee0bde4STeresa Johnson         // Since the call site is now direct, we must clear metadata that
1202fee0bde4STeresa Johnson         // is only appropriate for indirect calls. This includes !prof and
1203fee0bde4STeresa Johnson         // !callees metadata.
1204fee0bde4STeresa Johnson         CB.setMetadata(LLVMContext::MD_prof, nullptr);
1205fee0bde4STeresa Johnson         CB.setMetadata(LLVMContext::MD_callees, nullptr);
1206fee0bde4STeresa Johnson       }
1207d55d46f4STeresa Johnson 
12080312f614SPeter Collingbourne       // This use is no longer unsafe.
12090312f614SPeter Collingbourne       if (VCallSite.NumUnsafeUses)
12100312f614SPeter Collingbourne         --*VCallSite.NumUnsafeUses;
1211df49d1bbSPeter Collingbourne     }
12122974856aSPeter Collingbourne     if (CSInfo.isExported())
12132325bb34SPeter Collingbourne       IsExported = true;
121459675ba0SPeter Collingbourne     CSInfo.markDevirt();
121550cbd7ccSPeter Collingbourne   };
121650cbd7ccSPeter Collingbourne   Apply(SlotInfo.CSInfo);
121750cbd7ccSPeter Collingbourne   for (auto &P : SlotInfo.ConstCSInfo)
121850cbd7ccSPeter Collingbourne     Apply(P.second);
121950cbd7ccSPeter Collingbourne }
122050cbd7ccSPeter Collingbourne 
1221943afb57SEugene Leviant static bool AddCalls(VTableSlotInfo &SlotInfo, const ValueInfo &Callee) {
1222943afb57SEugene Leviant   // We can't add calls if we haven't seen a definition
1223943afb57SEugene Leviant   if (Callee.getSummaryList().empty())
1224943afb57SEugene Leviant     return false;
1225943afb57SEugene Leviant 
1226943afb57SEugene Leviant   // Insert calls into the summary index so that the devirtualized targets
1227943afb57SEugene Leviant   // are eligible for import.
1228943afb57SEugene Leviant   // FIXME: Annotate type tests with hotness. For now, mark these as hot
1229943afb57SEugene Leviant   // to better ensure we have the opportunity to inline them.
1230943afb57SEugene Leviant   bool IsExported = false;
1231943afb57SEugene Leviant   auto &S = Callee.getSummaryList()[0];
1232943afb57SEugene Leviant   CalleeInfo CI(CalleeInfo::HotnessType::Hot, /* RelBF = */ 0);
1233943afb57SEugene Leviant   auto AddCalls = [&](CallSiteInfo &CSInfo) {
1234943afb57SEugene Leviant     for (auto *FS : CSInfo.SummaryTypeCheckedLoadUsers) {
1235943afb57SEugene Leviant       FS->addCall({Callee, CI});
1236943afb57SEugene Leviant       IsExported |= S->modulePath() != FS->modulePath();
1237943afb57SEugene Leviant     }
1238943afb57SEugene Leviant     for (auto *FS : CSInfo.SummaryTypeTestAssumeUsers) {
1239943afb57SEugene Leviant       FS->addCall({Callee, CI});
1240943afb57SEugene Leviant       IsExported |= S->modulePath() != FS->modulePath();
1241943afb57SEugene Leviant     }
1242943afb57SEugene Leviant   };
1243943afb57SEugene Leviant   AddCalls(SlotInfo.CSInfo);
1244943afb57SEugene Leviant   for (auto &P : SlotInfo.ConstCSInfo)
1245943afb57SEugene Leviant     AddCalls(P.second);
1246943afb57SEugene Leviant   return IsExported;
1247943afb57SEugene Leviant }
1248943afb57SEugene Leviant 
124950cbd7ccSPeter Collingbourne bool DevirtModule::trySingleImplDevirt(
1250943afb57SEugene Leviant     ModuleSummaryIndex *ExportSummary,
1251943afb57SEugene Leviant     MutableArrayRef<VirtualCallTarget> TargetsForSlot, VTableSlotInfo &SlotInfo,
1252943afb57SEugene Leviant     WholeProgramDevirtResolution *Res) {
125350cbd7ccSPeter Collingbourne   // See if the program contains a single implementation of this virtual
125450cbd7ccSPeter Collingbourne   // function.
125550cbd7ccSPeter Collingbourne   Function *TheFn = TargetsForSlot[0].Fn;
125650cbd7ccSPeter Collingbourne   for (auto &&Target : TargetsForSlot)
125750cbd7ccSPeter Collingbourne     if (TheFn != Target.Fn)
125850cbd7ccSPeter Collingbourne       return false;
125950cbd7ccSPeter Collingbourne 
126050cbd7ccSPeter Collingbourne   // If so, update each call site to call that implementation directly.
1261ced9a795STeresa Johnson   if (RemarksEnabled || AreStatisticsEnabled())
126250cbd7ccSPeter Collingbourne     TargetsForSlot[0].WasDevirt = true;
12632325bb34SPeter Collingbourne 
12642325bb34SPeter Collingbourne   bool IsExported = false;
12652325bb34SPeter Collingbourne   applySingleImplDevirt(SlotInfo, TheFn, IsExported);
12662325bb34SPeter Collingbourne   if (!IsExported)
12672325bb34SPeter Collingbourne     return false;
12682325bb34SPeter Collingbourne 
12692325bb34SPeter Collingbourne   // If the only implementation has local linkage, we must promote to external
12702325bb34SPeter Collingbourne   // to make it visible to thin LTO objects. We can only get here during the
12712325bb34SPeter Collingbourne   // ThinLTO export phase.
12722325bb34SPeter Collingbourne   if (TheFn->hasLocalLinkage()) {
1273c6e5c465SHans Wennborg     std::string NewName = (TheFn->getName() + ".llvm.merged").str();
127488a58cf9SPeter Collingbourne 
127588a58cf9SPeter Collingbourne     // Since we are renaming the function, any comdats with the same name must
127688a58cf9SPeter Collingbourne     // also be renamed. This is required when targeting COFF, as the comdat name
127788a58cf9SPeter Collingbourne     // must match one of the names of the symbols in the comdat.
127888a58cf9SPeter Collingbourne     if (Comdat *C = TheFn->getComdat()) {
127988a58cf9SPeter Collingbourne       if (C->getName() == TheFn->getName()) {
128088a58cf9SPeter Collingbourne         Comdat *NewC = M.getOrInsertComdat(NewName);
128188a58cf9SPeter Collingbourne         NewC->setSelectionKind(C->getSelectionKind());
128288a58cf9SPeter Collingbourne         for (GlobalObject &GO : M.global_objects())
128388a58cf9SPeter Collingbourne           if (GO.getComdat() == C)
128488a58cf9SPeter Collingbourne             GO.setComdat(NewC);
128588a58cf9SPeter Collingbourne       }
128688a58cf9SPeter Collingbourne     }
128788a58cf9SPeter Collingbourne 
12882325bb34SPeter Collingbourne     TheFn->setLinkage(GlobalValue::ExternalLinkage);
12892325bb34SPeter Collingbourne     TheFn->setVisibility(GlobalValue::HiddenVisibility);
129088a58cf9SPeter Collingbourne     TheFn->setName(NewName);
12912325bb34SPeter Collingbourne   }
1292943afb57SEugene Leviant   if (ValueInfo TheFnVI = ExportSummary->getValueInfo(TheFn->getGUID()))
1293943afb57SEugene Leviant     // Any needed promotion of 'TheFn' has already been done during
1294943afb57SEugene Leviant     // LTO unit split, so we can ignore return value of AddCalls.
1295943afb57SEugene Leviant     AddCalls(SlotInfo, TheFnVI);
12962325bb34SPeter Collingbourne 
12972325bb34SPeter Collingbourne   Res->TheKind = WholeProgramDevirtResolution::SingleImpl;
1298adcd0268SBenjamin Kramer   Res->SingleImplName = std::string(TheFn->getName());
12992325bb34SPeter Collingbourne 
1300df49d1bbSPeter Collingbourne   return true;
1301df49d1bbSPeter Collingbourne }
1302df49d1bbSPeter Collingbourne 
1303d2df54e6STeresa Johnson bool DevirtIndex::trySingleImplDevirt(MutableArrayRef<ValueInfo> TargetsForSlot,
1304d2df54e6STeresa Johnson                                       VTableSlotSummary &SlotSummary,
1305d2df54e6STeresa Johnson                                       VTableSlotInfo &SlotInfo,
1306d2df54e6STeresa Johnson                                       WholeProgramDevirtResolution *Res,
1307d2df54e6STeresa Johnson                                       std::set<ValueInfo> &DevirtTargets) {
1308d2df54e6STeresa Johnson   // See if the program contains a single implementation of this virtual
1309d2df54e6STeresa Johnson   // function.
1310d2df54e6STeresa Johnson   auto TheFn = TargetsForSlot[0];
1311d2df54e6STeresa Johnson   for (auto &&Target : TargetsForSlot)
1312d2df54e6STeresa Johnson     if (TheFn != Target)
1313d2df54e6STeresa Johnson       return false;
1314d2df54e6STeresa Johnson 
1315d2df54e6STeresa Johnson   // Don't devirtualize if we don't have target definition.
1316d2df54e6STeresa Johnson   auto Size = TheFn.getSummaryList().size();
1317d2df54e6STeresa Johnson   if (!Size)
1318d2df54e6STeresa Johnson     return false;
1319d2df54e6STeresa Johnson 
13206d2032e2Sevgeny   // Don't devirtualize function if we're told to skip it
13216d2032e2Sevgeny   // in -wholeprogramdevirt-skip.
13226d2032e2Sevgeny   if (FunctionsToSkip.match(TheFn.name()))
13236d2032e2Sevgeny     return false;
13246d2032e2Sevgeny 
1325d2df54e6STeresa Johnson   // If the summary list contains multiple summaries where at least one is
1326d2df54e6STeresa Johnson   // a local, give up, as we won't know which (possibly promoted) name to use.
1327d2df54e6STeresa Johnson   for (auto &S : TheFn.getSummaryList())
1328d2df54e6STeresa Johnson     if (GlobalValue::isLocalLinkage(S->linkage()) && Size > 1)
1329d2df54e6STeresa Johnson       return false;
1330d2df54e6STeresa Johnson 
1331d2df54e6STeresa Johnson   // Collect functions devirtualized at least for one call site for stats.
1332ced9a795STeresa Johnson   if (PrintSummaryDevirt || AreStatisticsEnabled())
1333d2df54e6STeresa Johnson     DevirtTargets.insert(TheFn);
1334d2df54e6STeresa Johnson 
1335d2df54e6STeresa Johnson   auto &S = TheFn.getSummaryList()[0];
1336943afb57SEugene Leviant   bool IsExported = AddCalls(SlotInfo, TheFn);
1337d2df54e6STeresa Johnson   if (IsExported)
1338d2df54e6STeresa Johnson     ExportedGUIDs.insert(TheFn.getGUID());
1339d2df54e6STeresa Johnson 
1340d2df54e6STeresa Johnson   // Record in summary for use in devirtualization during the ThinLTO import
1341d2df54e6STeresa Johnson   // step.
1342d2df54e6STeresa Johnson   Res->TheKind = WholeProgramDevirtResolution::SingleImpl;
1343d2df54e6STeresa Johnson   if (GlobalValue::isLocalLinkage(S->linkage())) {
1344d2df54e6STeresa Johnson     if (IsExported)
1345d2df54e6STeresa Johnson       // If target is a local function and we are exporting it by
1346d2df54e6STeresa Johnson       // devirtualizing a call in another module, we need to record the
1347d2df54e6STeresa Johnson       // promoted name.
1348d2df54e6STeresa Johnson       Res->SingleImplName = ModuleSummaryIndex::getGlobalNameForLocal(
1349d2df54e6STeresa Johnson           TheFn.name(), ExportSummary.getModuleHash(S->modulePath()));
1350d2df54e6STeresa Johnson     else {
1351d2df54e6STeresa Johnson       LocalWPDTargetsMap[TheFn].push_back(SlotSummary);
1352adcd0268SBenjamin Kramer       Res->SingleImplName = std::string(TheFn.name());
1353d2df54e6STeresa Johnson     }
1354d2df54e6STeresa Johnson   } else
1355adcd0268SBenjamin Kramer     Res->SingleImplName = std::string(TheFn.name());
1356d2df54e6STeresa Johnson 
1357d2df54e6STeresa Johnson   // Name will be empty if this thin link driven off of serialized combined
1358d2df54e6STeresa Johnson   // index (e.g. llvm-lto). However, WPD is not supported/invoked for the
1359d2df54e6STeresa Johnson   // legacy LTO API anyway.
1360d2df54e6STeresa Johnson   assert(!Res->SingleImplName.empty());
1361d2df54e6STeresa Johnson 
1362d2df54e6STeresa Johnson   return true;
1363d2df54e6STeresa Johnson }
1364d2df54e6STeresa Johnson 
13652974856aSPeter Collingbourne void DevirtModule::tryICallBranchFunnel(
13662974856aSPeter Collingbourne     MutableArrayRef<VirtualCallTarget> TargetsForSlot, VTableSlotInfo &SlotInfo,
13672974856aSPeter Collingbourne     WholeProgramDevirtResolution *Res, VTableSlot Slot) {
13682974856aSPeter Collingbourne   Triple T(M.getTargetTriple());
13692974856aSPeter Collingbourne   if (T.getArch() != Triple::x86_64)
13702974856aSPeter Collingbourne     return;
13712974856aSPeter Collingbourne 
137266f53d71SVitaly Buka   if (TargetsForSlot.size() > ClThreshold)
13732974856aSPeter Collingbourne     return;
13742974856aSPeter Collingbourne 
13752974856aSPeter Collingbourne   bool HasNonDevirt = !SlotInfo.CSInfo.AllCallSitesDevirted;
13762974856aSPeter Collingbourne   if (!HasNonDevirt)
13772974856aSPeter Collingbourne     for (auto &P : SlotInfo.ConstCSInfo)
13782974856aSPeter Collingbourne       if (!P.second.AllCallSitesDevirted) {
13792974856aSPeter Collingbourne         HasNonDevirt = true;
13802974856aSPeter Collingbourne         break;
13812974856aSPeter Collingbourne       }
13822974856aSPeter Collingbourne 
13832974856aSPeter Collingbourne   if (!HasNonDevirt)
13842974856aSPeter Collingbourne     return;
13852974856aSPeter Collingbourne 
13862974856aSPeter Collingbourne   FunctionType *FT =
13872974856aSPeter Collingbourne       FunctionType::get(Type::getVoidTy(M.getContext()), {Int8PtrTy}, true);
13882974856aSPeter Collingbourne   Function *JT;
13892974856aSPeter Collingbourne   if (isa<MDString>(Slot.TypeID)) {
13902974856aSPeter Collingbourne     JT = Function::Create(FT, Function::ExternalLinkage,
1391f920da00SDylan McKay                           M.getDataLayout().getProgramAddressSpace(),
13922974856aSPeter Collingbourne                           getGlobalName(Slot, {}, "branch_funnel"), &M);
13932974856aSPeter Collingbourne     JT->setVisibility(GlobalValue::HiddenVisibility);
13942974856aSPeter Collingbourne   } else {
1395f920da00SDylan McKay     JT = Function::Create(FT, Function::InternalLinkage,
1396f920da00SDylan McKay                           M.getDataLayout().getProgramAddressSpace(),
1397f920da00SDylan McKay                           "branch_funnel", &M);
13982974856aSPeter Collingbourne   }
139946cf8253SArthur Eubanks   JT->addParamAttr(0, Attribute::Nest);
14002974856aSPeter Collingbourne 
14012974856aSPeter Collingbourne   std::vector<Value *> JTArgs;
14022974856aSPeter Collingbourne   JTArgs.push_back(JT->arg_begin());
14032974856aSPeter Collingbourne   for (auto &T : TargetsForSlot) {
14042974856aSPeter Collingbourne     JTArgs.push_back(getMemberAddr(T.TM));
14052974856aSPeter Collingbourne     JTArgs.push_back(T.Fn);
14062974856aSPeter Collingbourne   }
14072974856aSPeter Collingbourne 
14082974856aSPeter Collingbourne   BasicBlock *BB = BasicBlock::Create(M.getContext(), "", JT, nullptr);
14097976eb58SJames Y Knight   Function *Intr =
14102974856aSPeter Collingbourne       Intrinsic::getDeclaration(&M, llvm::Intrinsic::icall_branch_funnel, {});
14112974856aSPeter Collingbourne 
14122974856aSPeter Collingbourne   auto *CI = CallInst::Create(Intr, JTArgs, "", BB);
14132974856aSPeter Collingbourne   CI->setTailCallKind(CallInst::TCK_MustTail);
14142974856aSPeter Collingbourne   ReturnInst::Create(M.getContext(), nullptr, BB);
14152974856aSPeter Collingbourne 
14162974856aSPeter Collingbourne   bool IsExported = false;
14172974856aSPeter Collingbourne   applyICallBranchFunnel(SlotInfo, JT, IsExported);
14182974856aSPeter Collingbourne   if (IsExported)
14192974856aSPeter Collingbourne     Res->TheKind = WholeProgramDevirtResolution::BranchFunnel;
14202974856aSPeter Collingbourne }
14212974856aSPeter Collingbourne 
14222974856aSPeter Collingbourne void DevirtModule::applyICallBranchFunnel(VTableSlotInfo &SlotInfo,
14232974856aSPeter Collingbourne                                           Constant *JT, bool &IsExported) {
14242974856aSPeter Collingbourne   auto Apply = [&](CallSiteInfo &CSInfo) {
14252974856aSPeter Collingbourne     if (CSInfo.isExported())
14262974856aSPeter Collingbourne       IsExported = true;
14272974856aSPeter Collingbourne     if (CSInfo.AllCallSitesDevirted)
14282974856aSPeter Collingbourne       return;
14292974856aSPeter Collingbourne     for (auto &&VCallSite : CSInfo.CallSites) {
1430cea6f4d5SMircea Trofin       CallBase &CB = VCallSite.CB;
14312974856aSPeter Collingbourne 
14322974856aSPeter Collingbourne       // Jump tables are only profitable if the retpoline mitigation is enabled.
1433cea6f4d5SMircea Trofin       Attribute FSAttr = CB.getCaller()->getFnAttribute("target-features");
1434aab90384SCraig Topper       if (!FSAttr.isValid() ||
14352974856aSPeter Collingbourne           !FSAttr.getValueAsString().contains("+retpoline"))
14362974856aSPeter Collingbourne         continue;
14372974856aSPeter Collingbourne 
1438ced9a795STeresa Johnson       NumBranchFunnel++;
14392974856aSPeter Collingbourne       if (RemarksEnabled)
1440b0a1d3bdSTeresa Johnson         VCallSite.emitRemark("branch-funnel",
1441b0a1d3bdSTeresa Johnson                              JT->stripPointerCasts()->getName(), OREGetter);
14422974856aSPeter Collingbourne 
14432974856aSPeter Collingbourne       // Pass the address of the vtable in the nest register, which is r10 on
14442974856aSPeter Collingbourne       // x86_64.
14452974856aSPeter Collingbourne       std::vector<Type *> NewArgs;
14462974856aSPeter Collingbourne       NewArgs.push_back(Int8PtrTy);
1447e53472deSKazu Hirata       append_range(NewArgs, CB.getFunctionType()->params());
14487976eb58SJames Y Knight       FunctionType *NewFT =
1449cea6f4d5SMircea Trofin           FunctionType::get(CB.getFunctionType()->getReturnType(), NewArgs,
1450cea6f4d5SMircea Trofin                             CB.getFunctionType()->isVarArg());
14517976eb58SJames Y Knight       PointerType *NewFTPtr = PointerType::getUnqual(NewFT);
14522974856aSPeter Collingbourne 
1453cea6f4d5SMircea Trofin       IRBuilder<> IRB(&CB);
14542974856aSPeter Collingbourne       std::vector<Value *> Args;
14552974856aSPeter Collingbourne       Args.push_back(IRB.CreateBitCast(VCallSite.VTable, Int8PtrTy));
14568299fb8fSKazu Hirata       llvm::append_range(Args, CB.args());
14572974856aSPeter Collingbourne 
1458cea6f4d5SMircea Trofin       CallBase *NewCS = nullptr;
1459cea6f4d5SMircea Trofin       if (isa<CallInst>(CB))
14607976eb58SJames Y Knight         NewCS = IRB.CreateCall(NewFT, IRB.CreateBitCast(JT, NewFTPtr), Args);
14612974856aSPeter Collingbourne       else
1462cea6f4d5SMircea Trofin         NewCS = IRB.CreateInvoke(NewFT, IRB.CreateBitCast(JT, NewFTPtr),
1463cea6f4d5SMircea Trofin                                  cast<InvokeInst>(CB).getNormalDest(),
1464cea6f4d5SMircea Trofin                                  cast<InvokeInst>(CB).getUnwindDest(), Args);
1465cea6f4d5SMircea Trofin       NewCS->setCallingConv(CB.getCallingConv());
14662974856aSPeter Collingbourne 
1467cea6f4d5SMircea Trofin       AttributeList Attrs = CB.getAttributes();
14682974856aSPeter Collingbourne       std::vector<AttributeSet> NewArgAttrs;
14692974856aSPeter Collingbourne       NewArgAttrs.push_back(AttributeSet::get(
14702974856aSPeter Collingbourne           M.getContext(), ArrayRef<Attribute>{Attribute::get(
14712974856aSPeter Collingbourne                               M.getContext(), Attribute::Nest)}));
14722974856aSPeter Collingbourne       for (unsigned I = 0; I + 2 <  Attrs.getNumAttrSets(); ++I)
147380ea2bb5SArthur Eubanks         NewArgAttrs.push_back(Attrs.getParamAttrs(I));
1474cea6f4d5SMircea Trofin       NewCS->setAttributes(
147580ea2bb5SArthur Eubanks           AttributeList::get(M.getContext(), Attrs.getFnAttrs(),
147680ea2bb5SArthur Eubanks                              Attrs.getRetAttrs(), NewArgAttrs));
14772974856aSPeter Collingbourne 
1478cea6f4d5SMircea Trofin       CB.replaceAllUsesWith(NewCS);
1479cea6f4d5SMircea Trofin       CB.eraseFromParent();
14802974856aSPeter Collingbourne 
14812974856aSPeter Collingbourne       // This use is no longer unsafe.
14822974856aSPeter Collingbourne       if (VCallSite.NumUnsafeUses)
14832974856aSPeter Collingbourne         --*VCallSite.NumUnsafeUses;
14842974856aSPeter Collingbourne     }
14852974856aSPeter Collingbourne     // Don't mark as devirtualized because there may be callers compiled without
14862974856aSPeter Collingbourne     // retpoline mitigation, which would mean that they are lowered to
14872974856aSPeter Collingbourne     // llvm.type.test and therefore require an llvm.type.test resolution for the
14882974856aSPeter Collingbourne     // type identifier.
14892974856aSPeter Collingbourne   };
14902974856aSPeter Collingbourne   Apply(SlotInfo.CSInfo);
14912974856aSPeter Collingbourne   for (auto &P : SlotInfo.ConstCSInfo)
14922974856aSPeter Collingbourne     Apply(P.second);
14932974856aSPeter Collingbourne }
14942974856aSPeter Collingbourne 
1495df49d1bbSPeter Collingbourne bool DevirtModule::tryEvaluateFunctionsWithArgs(
1496df49d1bbSPeter Collingbourne     MutableArrayRef<VirtualCallTarget> TargetsForSlot,
149750cbd7ccSPeter Collingbourne     ArrayRef<uint64_t> Args) {
1498df49d1bbSPeter Collingbourne   // Evaluate each function and store the result in each target's RetVal
1499df49d1bbSPeter Collingbourne   // field.
1500df49d1bbSPeter Collingbourne   for (VirtualCallTarget &Target : TargetsForSlot) {
1501df49d1bbSPeter Collingbourne     if (Target.Fn->arg_size() != Args.size() + 1)
1502df49d1bbSPeter Collingbourne       return false;
1503df49d1bbSPeter Collingbourne 
1504df49d1bbSPeter Collingbourne     Evaluator Eval(M.getDataLayout(), nullptr);
1505df49d1bbSPeter Collingbourne     SmallVector<Constant *, 2> EvalArgs;
1506df49d1bbSPeter Collingbourne     EvalArgs.push_back(
1507df49d1bbSPeter Collingbourne         Constant::getNullValue(Target.Fn->getFunctionType()->getParamType(0)));
150850cbd7ccSPeter Collingbourne     for (unsigned I = 0; I != Args.size(); ++I) {
150950cbd7ccSPeter Collingbourne       auto *ArgTy = dyn_cast<IntegerType>(
151050cbd7ccSPeter Collingbourne           Target.Fn->getFunctionType()->getParamType(I + 1));
151150cbd7ccSPeter Collingbourne       if (!ArgTy)
151250cbd7ccSPeter Collingbourne         return false;
151350cbd7ccSPeter Collingbourne       EvalArgs.push_back(ConstantInt::get(ArgTy, Args[I]));
151450cbd7ccSPeter Collingbourne     }
151550cbd7ccSPeter Collingbourne 
1516df49d1bbSPeter Collingbourne     Constant *RetVal;
1517df49d1bbSPeter Collingbourne     if (!Eval.EvaluateFunction(Target.Fn, RetVal, EvalArgs) ||
1518df49d1bbSPeter Collingbourne         !isa<ConstantInt>(RetVal))
1519df49d1bbSPeter Collingbourne       return false;
1520df49d1bbSPeter Collingbourne     Target.RetVal = cast<ConstantInt>(RetVal)->getZExtValue();
1521df49d1bbSPeter Collingbourne   }
1522df49d1bbSPeter Collingbourne   return true;
1523df49d1bbSPeter Collingbourne }
1524df49d1bbSPeter Collingbourne 
152550cbd7ccSPeter Collingbourne void DevirtModule::applyUniformRetValOpt(CallSiteInfo &CSInfo, StringRef FnName,
152650cbd7ccSPeter Collingbourne                                          uint64_t TheRetVal) {
15277110510eSArthur Eubanks   for (auto Call : CSInfo.CallSites) {
15287110510eSArthur Eubanks     if (!OptimizedCalls.insert(&Call.CB).second)
15297110510eSArthur Eubanks       continue;
1530ced9a795STeresa Johnson     NumUniformRetVal++;
153150cbd7ccSPeter Collingbourne     Call.replaceAndErase(
1532e963c89dSSam Elliott         "uniform-ret-val", FnName, RemarksEnabled, OREGetter,
1533cea6f4d5SMircea Trofin         ConstantInt::get(cast<IntegerType>(Call.CB.getType()), TheRetVal));
15347110510eSArthur Eubanks   }
153559675ba0SPeter Collingbourne   CSInfo.markDevirt();
153650cbd7ccSPeter Collingbourne }
153750cbd7ccSPeter Collingbourne 
1538df49d1bbSPeter Collingbourne bool DevirtModule::tryUniformRetValOpt(
153977a8d563SPeter Collingbourne     MutableArrayRef<VirtualCallTarget> TargetsForSlot, CallSiteInfo &CSInfo,
154077a8d563SPeter Collingbourne     WholeProgramDevirtResolution::ByArg *Res) {
1541df49d1bbSPeter Collingbourne   // Uniform return value optimization. If all functions return the same
1542df49d1bbSPeter Collingbourne   // constant, replace all calls with that constant.
1543df49d1bbSPeter Collingbourne   uint64_t TheRetVal = TargetsForSlot[0].RetVal;
1544df49d1bbSPeter Collingbourne   for (const VirtualCallTarget &Target : TargetsForSlot)
1545df49d1bbSPeter Collingbourne     if (Target.RetVal != TheRetVal)
1546df49d1bbSPeter Collingbourne       return false;
1547df49d1bbSPeter Collingbourne 
154877a8d563SPeter Collingbourne   if (CSInfo.isExported()) {
154977a8d563SPeter Collingbourne     Res->TheKind = WholeProgramDevirtResolution::ByArg::UniformRetVal;
155077a8d563SPeter Collingbourne     Res->Info = TheRetVal;
155177a8d563SPeter Collingbourne   }
155277a8d563SPeter Collingbourne 
155350cbd7ccSPeter Collingbourne   applyUniformRetValOpt(CSInfo, TargetsForSlot[0].Fn->getName(), TheRetVal);
1554ced9a795STeresa Johnson   if (RemarksEnabled || AreStatisticsEnabled())
1555f3403fd2SIvan Krasin     for (auto &&Target : TargetsForSlot)
1556f3403fd2SIvan Krasin       Target.WasDevirt = true;
1557df49d1bbSPeter Collingbourne   return true;
1558df49d1bbSPeter Collingbourne }
1559df49d1bbSPeter Collingbourne 
156059675ba0SPeter Collingbourne std::string DevirtModule::getGlobalName(VTableSlot Slot,
156159675ba0SPeter Collingbourne                                         ArrayRef<uint64_t> Args,
156259675ba0SPeter Collingbourne                                         StringRef Name) {
156359675ba0SPeter Collingbourne   std::string FullName = "__typeid_";
156459675ba0SPeter Collingbourne   raw_string_ostream OS(FullName);
156559675ba0SPeter Collingbourne   OS << cast<MDString>(Slot.TypeID)->getString() << '_' << Slot.ByteOffset;
156659675ba0SPeter Collingbourne   for (uint64_t Arg : Args)
156759675ba0SPeter Collingbourne     OS << '_' << Arg;
156859675ba0SPeter Collingbourne   OS << '_' << Name;
156959675ba0SPeter Collingbourne   return OS.str();
157059675ba0SPeter Collingbourne }
157159675ba0SPeter Collingbourne 
1572b15a35e6SPeter Collingbourne bool DevirtModule::shouldExportConstantsAsAbsoluteSymbols() {
1573b15a35e6SPeter Collingbourne   Triple T(M.getTargetTriple());
15746904cd94SFangrui Song   return T.isX86() && T.getObjectFormat() == Triple::ELF;
1575b15a35e6SPeter Collingbourne }
1576b15a35e6SPeter Collingbourne 
157759675ba0SPeter Collingbourne void DevirtModule::exportGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args,
157859675ba0SPeter Collingbourne                                 StringRef Name, Constant *C) {
157959675ba0SPeter Collingbourne   GlobalAlias *GA = GlobalAlias::create(Int8Ty, 0, GlobalValue::ExternalLinkage,
158059675ba0SPeter Collingbourne                                         getGlobalName(Slot, Args, Name), C, &M);
158159675ba0SPeter Collingbourne   GA->setVisibility(GlobalValue::HiddenVisibility);
158259675ba0SPeter Collingbourne }
158359675ba0SPeter Collingbourne 
1584b15a35e6SPeter Collingbourne void DevirtModule::exportConstant(VTableSlot Slot, ArrayRef<uint64_t> Args,
1585b15a35e6SPeter Collingbourne                                   StringRef Name, uint32_t Const,
1586b15a35e6SPeter Collingbourne                                   uint32_t &Storage) {
1587b15a35e6SPeter Collingbourne   if (shouldExportConstantsAsAbsoluteSymbols()) {
1588b15a35e6SPeter Collingbourne     exportGlobal(
1589b15a35e6SPeter Collingbourne         Slot, Args, Name,
1590b15a35e6SPeter Collingbourne         ConstantExpr::getIntToPtr(ConstantInt::get(Int32Ty, Const), Int8PtrTy));
1591b15a35e6SPeter Collingbourne     return;
1592b15a35e6SPeter Collingbourne   }
1593b15a35e6SPeter Collingbourne 
1594b15a35e6SPeter Collingbourne   Storage = Const;
1595b15a35e6SPeter Collingbourne }
1596b15a35e6SPeter Collingbourne 
159759675ba0SPeter Collingbourne Constant *DevirtModule::importGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args,
1598b15a35e6SPeter Collingbourne                                      StringRef Name) {
1599cc5c5888SBob Haarman   Constant *C =
1600cc5c5888SBob Haarman       M.getOrInsertGlobal(getGlobalName(Slot, Args, Name), Int8Arr0Ty);
160159675ba0SPeter Collingbourne   auto *GV = dyn_cast<GlobalVariable>(C);
1602b15a35e6SPeter Collingbourne   if (GV)
1603b15a35e6SPeter Collingbourne     GV->setVisibility(GlobalValue::HiddenVisibility);
1604b15a35e6SPeter Collingbourne   return C;
1605b15a35e6SPeter Collingbourne }
1606b15a35e6SPeter Collingbourne 
1607b15a35e6SPeter Collingbourne Constant *DevirtModule::importConstant(VTableSlot Slot, ArrayRef<uint64_t> Args,
1608b15a35e6SPeter Collingbourne                                        StringRef Name, IntegerType *IntTy,
1609b15a35e6SPeter Collingbourne                                        uint32_t Storage) {
1610b15a35e6SPeter Collingbourne   if (!shouldExportConstantsAsAbsoluteSymbols())
1611b15a35e6SPeter Collingbourne     return ConstantInt::get(IntTy, Storage);
1612b15a35e6SPeter Collingbourne 
1613b15a35e6SPeter Collingbourne   Constant *C = importGlobal(Slot, Args, Name);
1614b15a35e6SPeter Collingbourne   auto *GV = cast<GlobalVariable>(C->stripPointerCasts());
1615b15a35e6SPeter Collingbourne   C = ConstantExpr::getPtrToInt(C, IntTy);
1616b15a35e6SPeter Collingbourne 
161714dcf02fSPeter Collingbourne   // We only need to set metadata if the global is newly created, in which
161814dcf02fSPeter Collingbourne   // case it would not have hidden visibility.
16190deb9a9aSBenjamin Kramer   if (GV->hasMetadata(LLVMContext::MD_absolute_symbol))
162059675ba0SPeter Collingbourne     return C;
162114dcf02fSPeter Collingbourne 
162214dcf02fSPeter Collingbourne   auto SetAbsRange = [&](uint64_t Min, uint64_t Max) {
162314dcf02fSPeter Collingbourne     auto *MinC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Min));
162414dcf02fSPeter Collingbourne     auto *MaxC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Max));
162514dcf02fSPeter Collingbourne     GV->setMetadata(LLVMContext::MD_absolute_symbol,
162614dcf02fSPeter Collingbourne                     MDNode::get(M.getContext(), {MinC, MaxC}));
162714dcf02fSPeter Collingbourne   };
1628b15a35e6SPeter Collingbourne   unsigned AbsWidth = IntTy->getBitWidth();
162914dcf02fSPeter Collingbourne   if (AbsWidth == IntPtrTy->getBitWidth())
163014dcf02fSPeter Collingbourne     SetAbsRange(~0ull, ~0ull); // Full set.
1631b15a35e6SPeter Collingbourne   else
163214dcf02fSPeter Collingbourne     SetAbsRange(0, 1ull << AbsWidth);
1633b15a35e6SPeter Collingbourne   return C;
163459675ba0SPeter Collingbourne }
163559675ba0SPeter Collingbourne 
163650cbd7ccSPeter Collingbourne void DevirtModule::applyUniqueRetValOpt(CallSiteInfo &CSInfo, StringRef FnName,
163750cbd7ccSPeter Collingbourne                                         bool IsOne,
163850cbd7ccSPeter Collingbourne                                         Constant *UniqueMemberAddr) {
163950cbd7ccSPeter Collingbourne   for (auto &&Call : CSInfo.CallSites) {
16407110510eSArthur Eubanks     if (!OptimizedCalls.insert(&Call.CB).second)
16417110510eSArthur Eubanks       continue;
1642cea6f4d5SMircea Trofin     IRBuilder<> B(&Call.CB);
1643001052a0SPeter Collingbourne     Value *Cmp =
1644cc5c5888SBob Haarman         B.CreateICmp(IsOne ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE, Call.VTable,
1645cc5c5888SBob Haarman                      B.CreateBitCast(UniqueMemberAddr, Call.VTable->getType()));
1646cea6f4d5SMircea Trofin     Cmp = B.CreateZExt(Cmp, Call.CB.getType());
1647ced9a795STeresa Johnson     NumUniqueRetVal++;
1648e963c89dSSam Elliott     Call.replaceAndErase("unique-ret-val", FnName, RemarksEnabled, OREGetter,
1649e963c89dSSam Elliott                          Cmp);
165050cbd7ccSPeter Collingbourne   }
165159675ba0SPeter Collingbourne   CSInfo.markDevirt();
165250cbd7ccSPeter Collingbourne }
165350cbd7ccSPeter Collingbourne 
16542974856aSPeter Collingbourne Constant *DevirtModule::getMemberAddr(const TypeMemberInfo *M) {
16552974856aSPeter Collingbourne   Constant *C = ConstantExpr::getBitCast(M->Bits->GV, Int8PtrTy);
16562974856aSPeter Collingbourne   return ConstantExpr::getGetElementPtr(Int8Ty, C,
16572974856aSPeter Collingbourne                                         ConstantInt::get(Int64Ty, M->Offset));
16582974856aSPeter Collingbourne }
16592974856aSPeter Collingbourne 
1660df49d1bbSPeter Collingbourne bool DevirtModule::tryUniqueRetValOpt(
1661f3403fd2SIvan Krasin     unsigned BitWidth, MutableArrayRef<VirtualCallTarget> TargetsForSlot,
166259675ba0SPeter Collingbourne     CallSiteInfo &CSInfo, WholeProgramDevirtResolution::ByArg *Res,
166359675ba0SPeter Collingbourne     VTableSlot Slot, ArrayRef<uint64_t> Args) {
1664df49d1bbSPeter Collingbourne   // IsOne controls whether we look for a 0 or a 1.
1665df49d1bbSPeter Collingbourne   auto tryUniqueRetValOptFor = [&](bool IsOne) {
1666cdc71612SEugene Zelenko     const TypeMemberInfo *UniqueMember = nullptr;
1667df49d1bbSPeter Collingbourne     for (const VirtualCallTarget &Target : TargetsForSlot) {
16683866cc5fSPeter Collingbourne       if (Target.RetVal == (IsOne ? 1 : 0)) {
16697efd7506SPeter Collingbourne         if (UniqueMember)
1670df49d1bbSPeter Collingbourne           return false;
16717efd7506SPeter Collingbourne         UniqueMember = Target.TM;
1672df49d1bbSPeter Collingbourne       }
1673df49d1bbSPeter Collingbourne     }
1674df49d1bbSPeter Collingbourne 
16757efd7506SPeter Collingbourne     // We should have found a unique member or bailed out by now. We already
1676df49d1bbSPeter Collingbourne     // checked for a uniform return value in tryUniformRetValOpt.
16777efd7506SPeter Collingbourne     assert(UniqueMember);
1678df49d1bbSPeter Collingbourne 
16792974856aSPeter Collingbourne     Constant *UniqueMemberAddr = getMemberAddr(UniqueMember);
168059675ba0SPeter Collingbourne     if (CSInfo.isExported()) {
168159675ba0SPeter Collingbourne       Res->TheKind = WholeProgramDevirtResolution::ByArg::UniqueRetVal;
168259675ba0SPeter Collingbourne       Res->Info = IsOne;
168359675ba0SPeter Collingbourne 
168459675ba0SPeter Collingbourne       exportGlobal(Slot, Args, "unique_member", UniqueMemberAddr);
168559675ba0SPeter Collingbourne     }
168659675ba0SPeter Collingbourne 
168759675ba0SPeter Collingbourne     // Replace each call with the comparison.
168850cbd7ccSPeter Collingbourne     applyUniqueRetValOpt(CSInfo, TargetsForSlot[0].Fn->getName(), IsOne,
168950cbd7ccSPeter Collingbourne                          UniqueMemberAddr);
169050cbd7ccSPeter Collingbourne 
1691f3403fd2SIvan Krasin     // Update devirtualization statistics for targets.
1692ced9a795STeresa Johnson     if (RemarksEnabled || AreStatisticsEnabled())
1693f3403fd2SIvan Krasin       for (auto &&Target : TargetsForSlot)
1694f3403fd2SIvan Krasin         Target.WasDevirt = true;
1695f3403fd2SIvan Krasin 
1696df49d1bbSPeter Collingbourne     return true;
1697df49d1bbSPeter Collingbourne   };
1698df49d1bbSPeter Collingbourne 
1699df49d1bbSPeter Collingbourne   if (BitWidth == 1) {
1700df49d1bbSPeter Collingbourne     if (tryUniqueRetValOptFor(true))
1701df49d1bbSPeter Collingbourne       return true;
1702df49d1bbSPeter Collingbourne     if (tryUniqueRetValOptFor(false))
1703df49d1bbSPeter Collingbourne       return true;
1704df49d1bbSPeter Collingbourne   }
1705df49d1bbSPeter Collingbourne   return false;
1706df49d1bbSPeter Collingbourne }
1707df49d1bbSPeter Collingbourne 
170850cbd7ccSPeter Collingbourne void DevirtModule::applyVirtualConstProp(CallSiteInfo &CSInfo, StringRef FnName,
170950cbd7ccSPeter Collingbourne                                          Constant *Byte, Constant *Bit) {
171050cbd7ccSPeter Collingbourne   for (auto Call : CSInfo.CallSites) {
17117110510eSArthur Eubanks     if (!OptimizedCalls.insert(&Call.CB).second)
17127110510eSArthur Eubanks       continue;
1713cea6f4d5SMircea Trofin     auto *RetType = cast<IntegerType>(Call.CB.getType());
1714cea6f4d5SMircea Trofin     IRBuilder<> B(&Call.CB);
1715001052a0SPeter Collingbourne     Value *Addr =
1716001052a0SPeter Collingbourne         B.CreateGEP(Int8Ty, B.CreateBitCast(Call.VTable, Int8PtrTy), Byte);
171750cbd7ccSPeter Collingbourne     if (RetType->getBitWidth() == 1) {
171814359ef1SJames Y Knight       Value *Bits = B.CreateLoad(Int8Ty, Addr);
171950cbd7ccSPeter Collingbourne       Value *BitsAndBit = B.CreateAnd(Bits, Bit);
172050cbd7ccSPeter Collingbourne       auto IsBitSet = B.CreateICmpNE(BitsAndBit, ConstantInt::get(Int8Ty, 0));
1721ced9a795STeresa Johnson       NumVirtConstProp1Bit++;
172250cbd7ccSPeter Collingbourne       Call.replaceAndErase("virtual-const-prop-1-bit", FnName, RemarksEnabled,
1723e963c89dSSam Elliott                            OREGetter, IsBitSet);
172450cbd7ccSPeter Collingbourne     } else {
172550cbd7ccSPeter Collingbourne       Value *ValAddr = B.CreateBitCast(Addr, RetType->getPointerTo());
172650cbd7ccSPeter Collingbourne       Value *Val = B.CreateLoad(RetType, ValAddr);
1727ced9a795STeresa Johnson       NumVirtConstProp++;
1728e963c89dSSam Elliott       Call.replaceAndErase("virtual-const-prop", FnName, RemarksEnabled,
1729e963c89dSSam Elliott                            OREGetter, Val);
173050cbd7ccSPeter Collingbourne     }
173150cbd7ccSPeter Collingbourne   }
173214dcf02fSPeter Collingbourne   CSInfo.markDevirt();
173350cbd7ccSPeter Collingbourne }
173450cbd7ccSPeter Collingbourne 
1735df49d1bbSPeter Collingbourne bool DevirtModule::tryVirtualConstProp(
173659675ba0SPeter Collingbourne     MutableArrayRef<VirtualCallTarget> TargetsForSlot, VTableSlotInfo &SlotInfo,
173759675ba0SPeter Collingbourne     WholeProgramDevirtResolution *Res, VTableSlot Slot) {
1738df49d1bbSPeter Collingbourne   // This only works if the function returns an integer.
1739df49d1bbSPeter Collingbourne   auto RetType = dyn_cast<IntegerType>(TargetsForSlot[0].Fn->getReturnType());
1740df49d1bbSPeter Collingbourne   if (!RetType)
1741df49d1bbSPeter Collingbourne     return false;
1742df49d1bbSPeter Collingbourne   unsigned BitWidth = RetType->getBitWidth();
1743df49d1bbSPeter Collingbourne   if (BitWidth > 64)
1744df49d1bbSPeter Collingbourne     return false;
1745df49d1bbSPeter Collingbourne 
174617febdbbSPeter Collingbourne   // Make sure that each function is defined, does not access memory, takes at
174717febdbbSPeter Collingbourne   // least one argument, does not use its first argument (which we assume is
174817febdbbSPeter Collingbourne   // 'this'), and has the same return type.
174937317f12SPeter Collingbourne   //
175037317f12SPeter Collingbourne   // Note that we test whether this copy of the function is readnone, rather
175137317f12SPeter Collingbourne   // than testing function attributes, which must hold for any copy of the
175237317f12SPeter Collingbourne   // function, even a less optimized version substituted at link time. This is
175337317f12SPeter Collingbourne   // sound because the virtual constant propagation optimizations effectively
175437317f12SPeter Collingbourne   // inline all implementations of the virtual function into each call site,
175537317f12SPeter Collingbourne   // rather than using function attributes to perform local optimization.
1756df49d1bbSPeter Collingbourne   for (VirtualCallTarget &Target : TargetsForSlot) {
175737317f12SPeter Collingbourne     if (Target.Fn->isDeclaration() ||
175837317f12SPeter Collingbourne         computeFunctionBodyMemoryAccess(*Target.Fn, AARGetter(*Target.Fn)) !=
1759014f5bcfSFlorian Hahn             FMRB_DoesNotAccessMemory ||
176017febdbbSPeter Collingbourne         Target.Fn->arg_empty() || !Target.Fn->arg_begin()->use_empty() ||
1761df49d1bbSPeter Collingbourne         Target.Fn->getReturnType() != RetType)
1762df49d1bbSPeter Collingbourne       return false;
1763df49d1bbSPeter Collingbourne   }
1764df49d1bbSPeter Collingbourne 
176550cbd7ccSPeter Collingbourne   for (auto &&CSByConstantArg : SlotInfo.ConstCSInfo) {
1766df49d1bbSPeter Collingbourne     if (!tryEvaluateFunctionsWithArgs(TargetsForSlot, CSByConstantArg.first))
1767df49d1bbSPeter Collingbourne       continue;
1768df49d1bbSPeter Collingbourne 
176977a8d563SPeter Collingbourne     WholeProgramDevirtResolution::ByArg *ResByArg = nullptr;
177077a8d563SPeter Collingbourne     if (Res)
177177a8d563SPeter Collingbourne       ResByArg = &Res->ResByArg[CSByConstantArg.first];
177277a8d563SPeter Collingbourne 
177377a8d563SPeter Collingbourne     if (tryUniformRetValOpt(TargetsForSlot, CSByConstantArg.second, ResByArg))
1774df49d1bbSPeter Collingbourne       continue;
1775df49d1bbSPeter Collingbourne 
177659675ba0SPeter Collingbourne     if (tryUniqueRetValOpt(BitWidth, TargetsForSlot, CSByConstantArg.second,
177759675ba0SPeter Collingbourne                            ResByArg, Slot, CSByConstantArg.first))
1778df49d1bbSPeter Collingbourne       continue;
1779df49d1bbSPeter Collingbourne 
17807efd7506SPeter Collingbourne     // Find an allocation offset in bits in all vtables associated with the
17817efd7506SPeter Collingbourne     // type.
1782df49d1bbSPeter Collingbourne     uint64_t AllocBefore =
1783df49d1bbSPeter Collingbourne         findLowestOffset(TargetsForSlot, /*IsAfter=*/false, BitWidth);
1784df49d1bbSPeter Collingbourne     uint64_t AllocAfter =
1785df49d1bbSPeter Collingbourne         findLowestOffset(TargetsForSlot, /*IsAfter=*/true, BitWidth);
1786df49d1bbSPeter Collingbourne 
1787df49d1bbSPeter Collingbourne     // Calculate the total amount of padding needed to store a value at both
1788df49d1bbSPeter Collingbourne     // ends of the object.
1789df49d1bbSPeter Collingbourne     uint64_t TotalPaddingBefore = 0, TotalPaddingAfter = 0;
1790df49d1bbSPeter Collingbourne     for (auto &&Target : TargetsForSlot) {
1791df49d1bbSPeter Collingbourne       TotalPaddingBefore += std::max<int64_t>(
1792df49d1bbSPeter Collingbourne           (AllocBefore + 7) / 8 - Target.allocatedBeforeBytes() - 1, 0);
1793df49d1bbSPeter Collingbourne       TotalPaddingAfter += std::max<int64_t>(
1794df49d1bbSPeter Collingbourne           (AllocAfter + 7) / 8 - Target.allocatedAfterBytes() - 1, 0);
1795df49d1bbSPeter Collingbourne     }
1796df49d1bbSPeter Collingbourne 
1797df49d1bbSPeter Collingbourne     // If the amount of padding is too large, give up.
1798df49d1bbSPeter Collingbourne     // FIXME: do something smarter here.
1799df49d1bbSPeter Collingbourne     if (std::min(TotalPaddingBefore, TotalPaddingAfter) > 128)
1800df49d1bbSPeter Collingbourne       continue;
1801df49d1bbSPeter Collingbourne 
1802df49d1bbSPeter Collingbourne     // Calculate the offset to the value as a (possibly negative) byte offset
1803df49d1bbSPeter Collingbourne     // and (if applicable) a bit offset, and store the values in the targets.
1804df49d1bbSPeter Collingbourne     int64_t OffsetByte;
1805df49d1bbSPeter Collingbourne     uint64_t OffsetBit;
1806df49d1bbSPeter Collingbourne     if (TotalPaddingBefore <= TotalPaddingAfter)
1807df49d1bbSPeter Collingbourne       setBeforeReturnValues(TargetsForSlot, AllocBefore, BitWidth, OffsetByte,
1808df49d1bbSPeter Collingbourne                             OffsetBit);
1809df49d1bbSPeter Collingbourne     else
1810df49d1bbSPeter Collingbourne       setAfterReturnValues(TargetsForSlot, AllocAfter, BitWidth, OffsetByte,
1811df49d1bbSPeter Collingbourne                            OffsetBit);
1812df49d1bbSPeter Collingbourne 
1813ced9a795STeresa Johnson     if (RemarksEnabled || AreStatisticsEnabled())
1814f3403fd2SIvan Krasin       for (auto &&Target : TargetsForSlot)
1815f3403fd2SIvan Krasin         Target.WasDevirt = true;
1816f3403fd2SIvan Krasin 
181714dcf02fSPeter Collingbourne 
181814dcf02fSPeter Collingbourne     if (CSByConstantArg.second.isExported()) {
181914dcf02fSPeter Collingbourne       ResByArg->TheKind = WholeProgramDevirtResolution::ByArg::VirtualConstProp;
1820b15a35e6SPeter Collingbourne       exportConstant(Slot, CSByConstantArg.first, "byte", OffsetByte,
1821b15a35e6SPeter Collingbourne                      ResByArg->Byte);
1822b15a35e6SPeter Collingbourne       exportConstant(Slot, CSByConstantArg.first, "bit", 1ULL << OffsetBit,
1823b15a35e6SPeter Collingbourne                      ResByArg->Bit);
182414dcf02fSPeter Collingbourne     }
182514dcf02fSPeter Collingbourne 
182614dcf02fSPeter Collingbourne     // Rewrite each call to a load from OffsetByte/OffsetBit.
1827b15a35e6SPeter Collingbourne     Constant *ByteConst = ConstantInt::get(Int32Ty, OffsetByte);
1828b15a35e6SPeter Collingbourne     Constant *BitConst = ConstantInt::get(Int8Ty, 1ULL << OffsetBit);
182950cbd7ccSPeter Collingbourne     applyVirtualConstProp(CSByConstantArg.second,
183050cbd7ccSPeter Collingbourne                           TargetsForSlot[0].Fn->getName(), ByteConst, BitConst);
1831df49d1bbSPeter Collingbourne   }
1832df49d1bbSPeter Collingbourne   return true;
1833df49d1bbSPeter Collingbourne }
1834df49d1bbSPeter Collingbourne 
1835df49d1bbSPeter Collingbourne void DevirtModule::rebuildGlobal(VTableBits &B) {
1836df49d1bbSPeter Collingbourne   if (B.Before.Bytes.empty() && B.After.Bytes.empty())
1837df49d1bbSPeter Collingbourne     return;
1838df49d1bbSPeter Collingbourne 
1839ef5cfc2dSPeter Collingbourne   // Align the before byte array to the global's minimum alignment so that we
1840ef5cfc2dSPeter Collingbourne   // don't break any alignment requirements on the global.
1841d3085c25SGuillaume Chatelet   Align Alignment = M.getDataLayout().getValueOrABITypeAlignment(
1842d3085c25SGuillaume Chatelet       B.GV->getAlign(), B.GV->getValueType());
18430e62011dSGuillaume Chatelet   B.Before.Bytes.resize(alignTo(B.Before.Bytes.size(), Alignment));
1844df49d1bbSPeter Collingbourne 
1845df49d1bbSPeter Collingbourne   // Before was stored in reverse order; flip it now.
1846df49d1bbSPeter Collingbourne   for (size_t I = 0, Size = B.Before.Bytes.size(); I != Size / 2; ++I)
1847df49d1bbSPeter Collingbourne     std::swap(B.Before.Bytes[I], B.Before.Bytes[Size - 1 - I]);
1848df49d1bbSPeter Collingbourne 
1849df49d1bbSPeter Collingbourne   // Build an anonymous global containing the before bytes, followed by the
1850df49d1bbSPeter Collingbourne   // original initializer, followed by the after bytes.
1851df49d1bbSPeter Collingbourne   auto NewInit = ConstantStruct::getAnon(
1852df49d1bbSPeter Collingbourne       {ConstantDataArray::get(M.getContext(), B.Before.Bytes),
1853df49d1bbSPeter Collingbourne        B.GV->getInitializer(),
1854df49d1bbSPeter Collingbourne        ConstantDataArray::get(M.getContext(), B.After.Bytes)});
1855df49d1bbSPeter Collingbourne   auto NewGV =
1856df49d1bbSPeter Collingbourne       new GlobalVariable(M, NewInit->getType(), B.GV->isConstant(),
1857df49d1bbSPeter Collingbourne                          GlobalVariable::PrivateLinkage, NewInit, "", B.GV);
1858df49d1bbSPeter Collingbourne   NewGV->setSection(B.GV->getSection());
1859df49d1bbSPeter Collingbourne   NewGV->setComdat(B.GV->getComdat());
18601172712fSArthur Eubanks   NewGV->setAlignment(B.GV->getAlign());
1861df49d1bbSPeter Collingbourne 
18620312f614SPeter Collingbourne   // Copy the original vtable's metadata to the anonymous global, adjusting
18630312f614SPeter Collingbourne   // offsets as required.
18640312f614SPeter Collingbourne   NewGV->copyMetadata(B.GV, B.Before.Bytes.size());
18650312f614SPeter Collingbourne 
1866df49d1bbSPeter Collingbourne   // Build an alias named after the original global, pointing at the second
1867df49d1bbSPeter Collingbourne   // element (the original initializer).
1868df49d1bbSPeter Collingbourne   auto Alias = GlobalAlias::create(
1869df49d1bbSPeter Collingbourne       B.GV->getInitializer()->getType(), 0, B.GV->getLinkage(), "",
1870df49d1bbSPeter Collingbourne       ConstantExpr::getGetElementPtr(
1871df49d1bbSPeter Collingbourne           NewInit->getType(), NewGV,
1872df49d1bbSPeter Collingbourne           ArrayRef<Constant *>{ConstantInt::get(Int32Ty, 0),
1873df49d1bbSPeter Collingbourne                                ConstantInt::get(Int32Ty, 1)}),
1874df49d1bbSPeter Collingbourne       &M);
1875df49d1bbSPeter Collingbourne   Alias->setVisibility(B.GV->getVisibility());
1876df49d1bbSPeter Collingbourne   Alias->takeName(B.GV);
1877df49d1bbSPeter Collingbourne 
1878df49d1bbSPeter Collingbourne   B.GV->replaceAllUsesWith(Alias);
1879df49d1bbSPeter Collingbourne   B.GV->eraseFromParent();
1880df49d1bbSPeter Collingbourne }
1881df49d1bbSPeter Collingbourne 
1882f3403fd2SIvan Krasin bool DevirtModule::areRemarksEnabled() {
1883f3403fd2SIvan Krasin   const auto &FL = M.getFunctionList();
18845e1c0e76STeresa Johnson   for (const Function &Fn : FL) {
1885de53bfb9SAdam Nemet     const auto &BBL = Fn.getBasicBlockList();
1886de53bfb9SAdam Nemet     if (BBL.empty())
18875e1c0e76STeresa Johnson       continue;
1888de53bfb9SAdam Nemet     auto DI = OptimizationRemark(DEBUG_TYPE, "", DebugLoc(), &BBL.front());
1889f3403fd2SIvan Krasin     return DI.isEnabled();
1890f3403fd2SIvan Krasin   }
18915e1c0e76STeresa Johnson   return false;
18925e1c0e76STeresa Johnson }
1893f3403fd2SIvan Krasin 
18946014c46cSTeresa Johnson void DevirtModule::scanTypeTestUsers(
18956014c46cSTeresa Johnson     Function *TypeTestFunc,
18966014c46cSTeresa Johnson     DenseMap<Metadata *, std::set<TypeMemberInfo>> &TypeIdMap) {
1897df49d1bbSPeter Collingbourne   // Find all virtual calls via a virtual table pointer %p under an assumption
18987efd7506SPeter Collingbourne   // of the form llvm.assume(llvm.type.test(%p, %md)). This indicates that %p
18997efd7506SPeter Collingbourne   // points to a member of the type identifier %md. Group calls by (type ID,
19007efd7506SPeter Collingbourne   // offset) pair (effectively the identity of the virtual function) and store
19017efd7506SPeter Collingbourne   // to CallSlots.
19020d182d9dSKazu Hirata   for (Use &U : llvm::make_early_inc_range(TypeTestFunc->uses())) {
19030d182d9dSKazu Hirata     auto *CI = dyn_cast<CallInst>(U.getUser());
1904df49d1bbSPeter Collingbourne     if (!CI)
1905df49d1bbSPeter Collingbourne       continue;
1906df49d1bbSPeter Collingbourne 
1907ccdc225cSPeter Collingbourne     // Search for virtual calls based on %p and add them to DevirtCalls.
1908ccdc225cSPeter Collingbourne     SmallVector<DevirtCallSite, 1> DevirtCalls;
1909df49d1bbSPeter Collingbourne     SmallVector<CallInst *, 1> Assumes;
1910f24136f1STeresa Johnson     auto &DT = LookupDomTree(*CI->getFunction());
1911f24136f1STeresa Johnson     findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI, DT);
1912df49d1bbSPeter Collingbourne 
191380bf137fSTeresa Johnson     Metadata *TypeId =
191480bf137fSTeresa Johnson         cast<MetadataAsValue>(CI->getArgOperand(1))->getMetadata();
19156014c46cSTeresa Johnson     // If we found any, add them to CallSlots.
19166014c46cSTeresa Johnson     if (!Assumes.empty()) {
1917df49d1bbSPeter Collingbourne       Value *Ptr = CI->getArgOperand(0)->stripPointerCasts();
1918d291bd51STeresa Johnson       for (DevirtCallSite Call : DevirtCalls)
1919cea6f4d5SMircea Trofin         CallSlots[{TypeId, Call.Offset}].addCallSite(Ptr, Call.CB, nullptr);
1920ccdc225cSPeter Collingbourne     }
1921df49d1bbSPeter Collingbourne 
19226014c46cSTeresa Johnson     auto RemoveTypeTestAssumes = [&]() {
19237efd7506SPeter Collingbourne       // We no longer need the assumes or the type test.
1924df49d1bbSPeter Collingbourne       for (auto Assume : Assumes)
1925df49d1bbSPeter Collingbourne         Assume->eraseFromParent();
1926df49d1bbSPeter Collingbourne       // We can't use RecursivelyDeleteTriviallyDeadInstructions here because we
1927df49d1bbSPeter Collingbourne       // may use the vtable argument later.
1928df49d1bbSPeter Collingbourne       if (CI->use_empty())
1929df49d1bbSPeter Collingbourne         CI->eraseFromParent();
19306014c46cSTeresa Johnson     };
19316014c46cSTeresa Johnson 
19326014c46cSTeresa Johnson     // At this point we could remove all type test assume sequences, as they
19336014c46cSTeresa Johnson     // were originally inserted for WPD. However, we can keep these in the
19346014c46cSTeresa Johnson     // code stream for later analysis (e.g. to help drive more efficient ICP
19356014c46cSTeresa Johnson     // sequences). They will eventually be removed by a second LowerTypeTests
19366014c46cSTeresa Johnson     // invocation that cleans them up. In order to do this correctly, the first
19376014c46cSTeresa Johnson     // LowerTypeTests invocation needs to know that they have "Unknown" type
19386014c46cSTeresa Johnson     // test resolution, so that they aren't treated as Unsat and lowered to
19396014c46cSTeresa Johnson     // False, which will break any uses on assumes. Below we remove any type
19406014c46cSTeresa Johnson     // test assumes that will not be treated as Unknown by LTT.
19416014c46cSTeresa Johnson 
19426014c46cSTeresa Johnson     // The type test assumes will be treated by LTT as Unsat if the type id is
19436014c46cSTeresa Johnson     // not used on a global (in which case it has no entry in the TypeIdMap).
19446014c46cSTeresa Johnson     if (!TypeIdMap.count(TypeId))
19456014c46cSTeresa Johnson       RemoveTypeTestAssumes();
19466014c46cSTeresa Johnson 
19476014c46cSTeresa Johnson     // For ThinLTO importing, we need to remove the type test assumes if this is
19486014c46cSTeresa Johnson     // an MDString type id without a corresponding TypeIdSummary. Any
19496014c46cSTeresa Johnson     // non-MDString type ids are ignored and treated as Unknown by LTT, so their
19506014c46cSTeresa Johnson     // type test assumes can be kept. If the MDString type id is missing a
19516014c46cSTeresa Johnson     // TypeIdSummary (e.g. because there was no use on a vcall, preventing the
19526014c46cSTeresa Johnson     // exporting phase of WPD from analyzing it), then it would be treated as
19536014c46cSTeresa Johnson     // Unsat by LTT and we need to remove its type test assumes here. If not
19546014c46cSTeresa Johnson     // used on a vcall we don't need them for later optimization use in any
19556014c46cSTeresa Johnson     // case.
19566014c46cSTeresa Johnson     else if (ImportSummary && isa<MDString>(TypeId)) {
19576014c46cSTeresa Johnson       const TypeIdSummary *TidSummary =
19586014c46cSTeresa Johnson           ImportSummary->getTypeIdSummary(cast<MDString>(TypeId)->getString());
19596014c46cSTeresa Johnson       if (!TidSummary)
19606014c46cSTeresa Johnson         RemoveTypeTestAssumes();
19616014c46cSTeresa Johnson       else
19626014c46cSTeresa Johnson         // If one was created it should not be Unsat, because if we reached here
19636014c46cSTeresa Johnson         // the type id was used on a global.
19646014c46cSTeresa Johnson         assert(TidSummary->TTRes.TheKind != TypeTestResolution::Unsat);
19656014c46cSTeresa Johnson     }
1966df49d1bbSPeter Collingbourne   }
19670312f614SPeter Collingbourne }
19680312f614SPeter Collingbourne 
19690312f614SPeter Collingbourne void DevirtModule::scanTypeCheckedLoadUsers(Function *TypeCheckedLoadFunc) {
19700312f614SPeter Collingbourne   Function *TypeTestFunc = Intrinsic::getDeclaration(&M, Intrinsic::type_test);
19710312f614SPeter Collingbourne 
19720d182d9dSKazu Hirata   for (Use &U : llvm::make_early_inc_range(TypeCheckedLoadFunc->uses())) {
19730d182d9dSKazu Hirata     auto *CI = dyn_cast<CallInst>(U.getUser());
19740312f614SPeter Collingbourne     if (!CI)
19750312f614SPeter Collingbourne       continue;
19760312f614SPeter Collingbourne 
19770312f614SPeter Collingbourne     Value *Ptr = CI->getArgOperand(0);
19780312f614SPeter Collingbourne     Value *Offset = CI->getArgOperand(1);
19790312f614SPeter Collingbourne     Value *TypeIdValue = CI->getArgOperand(2);
19800312f614SPeter Collingbourne     Metadata *TypeId = cast<MetadataAsValue>(TypeIdValue)->getMetadata();
19810312f614SPeter Collingbourne 
19820312f614SPeter Collingbourne     SmallVector<DevirtCallSite, 1> DevirtCalls;
19830312f614SPeter Collingbourne     SmallVector<Instruction *, 1> LoadedPtrs;
19840312f614SPeter Collingbourne     SmallVector<Instruction *, 1> Preds;
19850312f614SPeter Collingbourne     bool HasNonCallUses = false;
1986f24136f1STeresa Johnson     auto &DT = LookupDomTree(*CI->getFunction());
19870312f614SPeter Collingbourne     findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds,
1988f24136f1STeresa Johnson                                                HasNonCallUses, CI, DT);
19890312f614SPeter Collingbourne 
19900312f614SPeter Collingbourne     // Start by generating "pessimistic" code that explicitly loads the function
19910312f614SPeter Collingbourne     // pointer from the vtable and performs the type check. If possible, we will
19920312f614SPeter Collingbourne     // eliminate the load and the type check later.
19930312f614SPeter Collingbourne 
19940312f614SPeter Collingbourne     // If possible, only generate the load at the point where it is used.
19950312f614SPeter Collingbourne     // This helps avoid unnecessary spills.
19960312f614SPeter Collingbourne     IRBuilder<> LoadB(
19970312f614SPeter Collingbourne         (LoadedPtrs.size() == 1 && !HasNonCallUses) ? LoadedPtrs[0] : CI);
19980312f614SPeter Collingbourne     Value *GEP = LoadB.CreateGEP(Int8Ty, Ptr, Offset);
19990312f614SPeter Collingbourne     Value *GEPPtr = LoadB.CreateBitCast(GEP, PointerType::getUnqual(Int8PtrTy));
20000312f614SPeter Collingbourne     Value *LoadedValue = LoadB.CreateLoad(Int8PtrTy, GEPPtr);
20010312f614SPeter Collingbourne 
20020312f614SPeter Collingbourne     for (Instruction *LoadedPtr : LoadedPtrs) {
20030312f614SPeter Collingbourne       LoadedPtr->replaceAllUsesWith(LoadedValue);
20040312f614SPeter Collingbourne       LoadedPtr->eraseFromParent();
20050312f614SPeter Collingbourne     }
20060312f614SPeter Collingbourne 
20070312f614SPeter Collingbourne     // Likewise for the type test.
20080312f614SPeter Collingbourne     IRBuilder<> CallB((Preds.size() == 1 && !HasNonCallUses) ? Preds[0] : CI);
20090312f614SPeter Collingbourne     CallInst *TypeTestCall = CallB.CreateCall(TypeTestFunc, {Ptr, TypeIdValue});
20100312f614SPeter Collingbourne 
20110312f614SPeter Collingbourne     for (Instruction *Pred : Preds) {
20120312f614SPeter Collingbourne       Pred->replaceAllUsesWith(TypeTestCall);
20130312f614SPeter Collingbourne       Pred->eraseFromParent();
20140312f614SPeter Collingbourne     }
20150312f614SPeter Collingbourne 
20160312f614SPeter Collingbourne     // We have already erased any extractvalue instructions that refer to the
20170312f614SPeter Collingbourne     // intrinsic call, but the intrinsic may have other non-extractvalue uses
20180312f614SPeter Collingbourne     // (although this is unlikely). In that case, explicitly build a pair and
20190312f614SPeter Collingbourne     // RAUW it.
20200312f614SPeter Collingbourne     if (!CI->use_empty()) {
20210312f614SPeter Collingbourne       Value *Pair = UndefValue::get(CI->getType());
20220312f614SPeter Collingbourne       IRBuilder<> B(CI);
20230312f614SPeter Collingbourne       Pair = B.CreateInsertValue(Pair, LoadedValue, {0});
20240312f614SPeter Collingbourne       Pair = B.CreateInsertValue(Pair, TypeTestCall, {1});
20250312f614SPeter Collingbourne       CI->replaceAllUsesWith(Pair);
20260312f614SPeter Collingbourne     }
20270312f614SPeter Collingbourne 
20280312f614SPeter Collingbourne     // The number of unsafe uses is initially the number of uses.
20290312f614SPeter Collingbourne     auto &NumUnsafeUses = NumUnsafeUsesForTypeTest[TypeTestCall];
20300312f614SPeter Collingbourne     NumUnsafeUses = DevirtCalls.size();
20310312f614SPeter Collingbourne 
20320312f614SPeter Collingbourne     // If the function pointer has a non-call user, we cannot eliminate the type
20330312f614SPeter Collingbourne     // check, as one of those users may eventually call the pointer. Increment
20340312f614SPeter Collingbourne     // the unsafe use count to make sure it cannot reach zero.
20350312f614SPeter Collingbourne     if (HasNonCallUses)
20360312f614SPeter Collingbourne       ++NumUnsafeUses;
20370312f614SPeter Collingbourne     for (DevirtCallSite Call : DevirtCalls) {
2038cea6f4d5SMircea Trofin       CallSlots[{TypeId, Call.Offset}].addCallSite(Ptr, Call.CB,
203950cbd7ccSPeter Collingbourne                                                    &NumUnsafeUses);
20400312f614SPeter Collingbourne     }
20410312f614SPeter Collingbourne 
20420312f614SPeter Collingbourne     CI->eraseFromParent();
20430312f614SPeter Collingbourne   }
20440312f614SPeter Collingbourne }
20450312f614SPeter Collingbourne 
20466d284fabSPeter Collingbourne void DevirtModule::importResolution(VTableSlot Slot, VTableSlotInfo &SlotInfo) {
2047d2df54e6STeresa Johnson   auto *TypeId = dyn_cast<MDString>(Slot.TypeID);
2048d2df54e6STeresa Johnson   if (!TypeId)
2049d2df54e6STeresa Johnson     return;
20509a3f9797SPeter Collingbourne   const TypeIdSummary *TidSummary =
2051d2df54e6STeresa Johnson       ImportSummary->getTypeIdSummary(TypeId->getString());
20529a3f9797SPeter Collingbourne   if (!TidSummary)
20539a3f9797SPeter Collingbourne     return;
20549a3f9797SPeter Collingbourne   auto ResI = TidSummary->WPDRes.find(Slot.ByteOffset);
20559a3f9797SPeter Collingbourne   if (ResI == TidSummary->WPDRes.end())
20569a3f9797SPeter Collingbourne     return;
20579a3f9797SPeter Collingbourne   const WholeProgramDevirtResolution &Res = ResI->second;
20586d284fabSPeter Collingbourne 
20596d284fabSPeter Collingbourne   if (Res.TheKind == WholeProgramDevirtResolution::SingleImpl) {
2060d2df54e6STeresa Johnson     assert(!Res.SingleImplName.empty());
20616d284fabSPeter Collingbourne     // The type of the function in the declaration is irrelevant because every
20626d284fabSPeter Collingbourne     // call site will cast it to the correct type.
206313680223SJames Y Knight     Constant *SingleImpl =
206413680223SJames Y Knight         cast<Constant>(M.getOrInsertFunction(Res.SingleImplName,
206513680223SJames Y Knight                                              Type::getVoidTy(M.getContext()))
206613680223SJames Y Knight                            .getCallee());
20676d284fabSPeter Collingbourne 
20686d284fabSPeter Collingbourne     // This is the import phase so we should not be exporting anything.
20696d284fabSPeter Collingbourne     bool IsExported = false;
20706d284fabSPeter Collingbourne     applySingleImplDevirt(SlotInfo, SingleImpl, IsExported);
20716d284fabSPeter Collingbourne     assert(!IsExported);
20726d284fabSPeter Collingbourne   }
20730152c815SPeter Collingbourne 
20740152c815SPeter Collingbourne   for (auto &CSByConstantArg : SlotInfo.ConstCSInfo) {
20750152c815SPeter Collingbourne     auto I = Res.ResByArg.find(CSByConstantArg.first);
20760152c815SPeter Collingbourne     if (I == Res.ResByArg.end())
20770152c815SPeter Collingbourne       continue;
20780152c815SPeter Collingbourne     auto &ResByArg = I->second;
20790152c815SPeter Collingbourne     // FIXME: We should figure out what to do about the "function name" argument
20800152c815SPeter Collingbourne     // to the apply* functions, as the function names are unavailable during the
20810152c815SPeter Collingbourne     // importing phase. For now we just pass the empty string. This does not
20820152c815SPeter Collingbourne     // impact correctness because the function names are just used for remarks.
20830152c815SPeter Collingbourne     switch (ResByArg.TheKind) {
20840152c815SPeter Collingbourne     case WholeProgramDevirtResolution::ByArg::UniformRetVal:
20850152c815SPeter Collingbourne       applyUniformRetValOpt(CSByConstantArg.second, "", ResByArg.Info);
20860152c815SPeter Collingbourne       break;
208759675ba0SPeter Collingbourne     case WholeProgramDevirtResolution::ByArg::UniqueRetVal: {
208859675ba0SPeter Collingbourne       Constant *UniqueMemberAddr =
208959675ba0SPeter Collingbourne           importGlobal(Slot, CSByConstantArg.first, "unique_member");
209059675ba0SPeter Collingbourne       applyUniqueRetValOpt(CSByConstantArg.second, "", ResByArg.Info,
209159675ba0SPeter Collingbourne                            UniqueMemberAddr);
209259675ba0SPeter Collingbourne       break;
209359675ba0SPeter Collingbourne     }
209414dcf02fSPeter Collingbourne     case WholeProgramDevirtResolution::ByArg::VirtualConstProp: {
2095b15a35e6SPeter Collingbourne       Constant *Byte = importConstant(Slot, CSByConstantArg.first, "byte",
2096b15a35e6SPeter Collingbourne                                       Int32Ty, ResByArg.Byte);
2097b15a35e6SPeter Collingbourne       Constant *Bit = importConstant(Slot, CSByConstantArg.first, "bit", Int8Ty,
2098b15a35e6SPeter Collingbourne                                      ResByArg.Bit);
209914dcf02fSPeter Collingbourne       applyVirtualConstProp(CSByConstantArg.second, "", Byte, Bit);
21000e6694d1SAdrian Prantl       break;
210114dcf02fSPeter Collingbourne     }
21020152c815SPeter Collingbourne     default:
21030152c815SPeter Collingbourne       break;
21040152c815SPeter Collingbourne     }
21050152c815SPeter Collingbourne   }
21062974856aSPeter Collingbourne 
21072974856aSPeter Collingbourne   if (Res.TheKind == WholeProgramDevirtResolution::BranchFunnel) {
210813680223SJames Y Knight     // The type of the function is irrelevant, because it's bitcast at calls
210913680223SJames Y Knight     // anyhow.
211013680223SJames Y Knight     Constant *JT = cast<Constant>(
211113680223SJames Y Knight         M.getOrInsertFunction(getGlobalName(Slot, {}, "branch_funnel"),
211213680223SJames Y Knight                               Type::getVoidTy(M.getContext()))
211313680223SJames Y Knight             .getCallee());
21142974856aSPeter Collingbourne     bool IsExported = false;
21152974856aSPeter Collingbourne     applyICallBranchFunnel(SlotInfo, JT, IsExported);
21162974856aSPeter Collingbourne     assert(!IsExported);
21172974856aSPeter Collingbourne   }
21186d284fabSPeter Collingbourne }
21196d284fabSPeter Collingbourne 
21206d284fabSPeter Collingbourne void DevirtModule::removeRedundantTypeTests() {
21216d284fabSPeter Collingbourne   auto True = ConstantInt::getTrue(M.getContext());
21226d284fabSPeter Collingbourne   for (auto &&U : NumUnsafeUsesForTypeTest) {
21236d284fabSPeter Collingbourne     if (U.second == 0) {
21246d284fabSPeter Collingbourne       U.first->replaceAllUsesWith(True);
21256d284fabSPeter Collingbourne       U.first->eraseFromParent();
21266d284fabSPeter Collingbourne     }
21276d284fabSPeter Collingbourne   }
21286d284fabSPeter Collingbourne }
21296d284fabSPeter Collingbourne 
213009a704c5SMingming Liu ValueInfo
213109a704c5SMingming Liu DevirtModule::lookUpFunctionValueInfo(Function *TheFn,
213209a704c5SMingming Liu                                       ModuleSummaryIndex *ExportSummary) {
213309a704c5SMingming Liu   assert((ExportSummary != nullptr) &&
213409a704c5SMingming Liu          "Caller guarantees ExportSummary is not nullptr");
213509a704c5SMingming Liu 
213609a704c5SMingming Liu   const auto TheFnGUID = TheFn->getGUID();
213709a704c5SMingming Liu   const auto TheFnGUIDWithExportedName = GlobalValue::getGUID(TheFn->getName());
213809a704c5SMingming Liu   // Look up ValueInfo with the GUID in the current linkage.
213909a704c5SMingming Liu   ValueInfo TheFnVI = ExportSummary->getValueInfo(TheFnGUID);
214009a704c5SMingming Liu   // If no entry is found and GUID is different from GUID computed using
214109a704c5SMingming Liu   // exported name, look up ValueInfo with the exported name unconditionally.
214209a704c5SMingming Liu   // This is a fallback.
214309a704c5SMingming Liu   //
214409a704c5SMingming Liu   // The reason to have a fallback:
214509a704c5SMingming Liu   // 1. LTO could enable global value internalization via
214609a704c5SMingming Liu   // `enable-lto-internalization`.
214709a704c5SMingming Liu   // 2. The GUID in ExportedSummary is computed using exported name.
214809a704c5SMingming Liu   if ((!TheFnVI) && (TheFnGUID != TheFnGUIDWithExportedName)) {
214909a704c5SMingming Liu     TheFnVI = ExportSummary->getValueInfo(TheFnGUIDWithExportedName);
215009a704c5SMingming Liu   }
215109a704c5SMingming Liu   return TheFnVI;
215209a704c5SMingming Liu }
215309a704c5SMingming Liu 
21549c49f8d7Sminglotus-6 bool DevirtModule::mustBeUnreachableFunction(
21559c49f8d7Sminglotus-6     Function *const F, ModuleSummaryIndex *ExportSummary) {
21569c49f8d7Sminglotus-6   // First, learn unreachability by analyzing function IR.
21579c49f8d7Sminglotus-6   if (!F->isDeclaration()) {
21589c49f8d7Sminglotus-6     // A function must be unreachable if its entry block ends with an
21599c49f8d7Sminglotus-6     // 'unreachable'.
21609c49f8d7Sminglotus-6     return isa<UnreachableInst>(F->getEntryBlock().getTerminator());
21619c49f8d7Sminglotus-6   }
21629c49f8d7Sminglotus-6   // Learn unreachability from ExportSummary if ExportSummary is present.
21639c49f8d7Sminglotus-6   return ExportSummary &&
21649c49f8d7Sminglotus-6          ::mustBeUnreachableFunction(
21659c49f8d7Sminglotus-6              DevirtModule::lookUpFunctionValueInfo(F, ExportSummary));
21669c49f8d7Sminglotus-6 }
21679c49f8d7Sminglotus-6 
21680312f614SPeter Collingbourne bool DevirtModule::run() {
2169d0b1f30bSTeresa Johnson   // If only some of the modules were split, we cannot correctly perform
2170d0b1f30bSTeresa Johnson   // this transformation. We already checked for the presense of type tests
2171d0b1f30bSTeresa Johnson   // with partially split modules during the thin link, and would have emitted
2172d0b1f30bSTeresa Johnson   // an error if any were found, so here we can simply return.
2173d0b1f30bSTeresa Johnson   if ((ExportSummary && ExportSummary->partiallySplitLTOUnits()) ||
2174d0b1f30bSTeresa Johnson       (ImportSummary && ImportSummary->partiallySplitLTOUnits()))
2175d0b1f30bSTeresa Johnson     return false;
2176d0b1f30bSTeresa Johnson 
21770312f614SPeter Collingbourne   Function *TypeTestFunc =
21780312f614SPeter Collingbourne       M.getFunction(Intrinsic::getName(Intrinsic::type_test));
21790312f614SPeter Collingbourne   Function *TypeCheckedLoadFunc =
21800312f614SPeter Collingbourne       M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load));
21810312f614SPeter Collingbourne   Function *AssumeFunc = M.getFunction(Intrinsic::getName(Intrinsic::assume));
21820312f614SPeter Collingbourne 
2183b406baaeSPeter Collingbourne   // Normally if there are no users of the devirtualization intrinsics in the
2184b406baaeSPeter Collingbourne   // module, this pass has nothing to do. But if we are exporting, we also need
2185b406baaeSPeter Collingbourne   // to handle any users that appear only in the function summaries.
2186f7691d8bSPeter Collingbourne   if (!ExportSummary &&
2187b406baaeSPeter Collingbourne       (!TypeTestFunc || TypeTestFunc->use_empty() || !AssumeFunc ||
21880312f614SPeter Collingbourne        AssumeFunc->use_empty()) &&
21890312f614SPeter Collingbourne       (!TypeCheckedLoadFunc || TypeCheckedLoadFunc->use_empty()))
21900312f614SPeter Collingbourne     return false;
21910312f614SPeter Collingbourne 
21926014c46cSTeresa Johnson   // Rebuild type metadata into a map for easy lookup.
21936014c46cSTeresa Johnson   std::vector<VTableBits> Bits;
21946014c46cSTeresa Johnson   DenseMap<Metadata *, std::set<TypeMemberInfo>> TypeIdMap;
21956014c46cSTeresa Johnson   buildTypeIdentifierMap(Bits, TypeIdMap);
21966014c46cSTeresa Johnson 
21970312f614SPeter Collingbourne   if (TypeTestFunc && AssumeFunc)
21986014c46cSTeresa Johnson     scanTypeTestUsers(TypeTestFunc, TypeIdMap);
21990312f614SPeter Collingbourne 
22000312f614SPeter Collingbourne   if (TypeCheckedLoadFunc)
22010312f614SPeter Collingbourne     scanTypeCheckedLoadUsers(TypeCheckedLoadFunc);
2202df49d1bbSPeter Collingbourne 
2203f7691d8bSPeter Collingbourne   if (ImportSummary) {
22046d284fabSPeter Collingbourne     for (auto &S : CallSlots)
22056d284fabSPeter Collingbourne       importResolution(S.first, S.second);
22066d284fabSPeter Collingbourne 
22076d284fabSPeter Collingbourne     removeRedundantTypeTests();
22086d284fabSPeter Collingbourne 
2209*9727c77dSDavid Green     // We have lowered or deleted the type intrinsics, so we will no longer have
2210*9727c77dSDavid Green     // enough information to reason about the liveness of virtual function
2211*9727c77dSDavid Green     // pointers in GlobalDCE.
22122f63d549STeresa Johnson     for (GlobalVariable &GV : M.globals())
22132f63d549STeresa Johnson       GV.eraseMetadata(LLVMContext::MD_vcall_visibility);
22142f63d549STeresa Johnson 
22156d284fabSPeter Collingbourne     // The rest of the code is only necessary when exporting or during regular
22166d284fabSPeter Collingbourne     // LTO, so we are done.
22176d284fabSPeter Collingbourne     return true;
22186d284fabSPeter Collingbourne   }
22196d284fabSPeter Collingbourne 
22207efd7506SPeter Collingbourne   if (TypeIdMap.empty())
2221df49d1bbSPeter Collingbourne     return true;
2222df49d1bbSPeter Collingbourne 
2223b406baaeSPeter Collingbourne   // Collect information from summary about which calls to try to devirtualize.
2224f7691d8bSPeter Collingbourne   if (ExportSummary) {
2225b406baaeSPeter Collingbourne     DenseMap<GlobalValue::GUID, TinyPtrVector<Metadata *>> MetadataByGUID;
2226b406baaeSPeter Collingbourne     for (auto &P : TypeIdMap) {
2227b406baaeSPeter Collingbourne       if (auto *TypeId = dyn_cast<MDString>(P.first))
2228b406baaeSPeter Collingbourne         MetadataByGUID[GlobalValue::getGUID(TypeId->getString())].push_back(
2229b406baaeSPeter Collingbourne             TypeId);
2230b406baaeSPeter Collingbourne     }
2231b406baaeSPeter Collingbourne 
2232f7691d8bSPeter Collingbourne     for (auto &P : *ExportSummary) {
22339667b91bSPeter Collingbourne       for (auto &S : P.second.SummaryList) {
2234b406baaeSPeter Collingbourne         auto *FS = dyn_cast<FunctionSummary>(S.get());
2235b406baaeSPeter Collingbourne         if (!FS)
2236b406baaeSPeter Collingbourne           continue;
2237b406baaeSPeter Collingbourne         // FIXME: Only add live functions.
22385d8aea10SGeorge Rimar         for (FunctionSummary::VFuncId VF : FS->type_test_assume_vcalls()) {
22395d8aea10SGeorge Rimar           for (Metadata *MD : MetadataByGUID[VF.GUID]) {
2240943afb57SEugene Leviant             CallSlots[{MD, VF.Offset}].CSInfo.addSummaryTypeTestAssumeUser(FS);
22415d8aea10SGeorge Rimar           }
22425d8aea10SGeorge Rimar         }
22435d8aea10SGeorge Rimar         for (FunctionSummary::VFuncId VF : FS->type_checked_load_vcalls()) {
22445d8aea10SGeorge Rimar           for (Metadata *MD : MetadataByGUID[VF.GUID]) {
22452974856aSPeter Collingbourne             CallSlots[{MD, VF.Offset}].CSInfo.addSummaryTypeCheckedLoadUser(FS);
22465d8aea10SGeorge Rimar           }
22475d8aea10SGeorge Rimar         }
2248b406baaeSPeter Collingbourne         for (const FunctionSummary::ConstVCall &VC :
22495d8aea10SGeorge Rimar              FS->type_test_assume_const_vcalls()) {
22505d8aea10SGeorge Rimar           for (Metadata *MD : MetadataByGUID[VC.VFunc.GUID]) {
22512325bb34SPeter Collingbourne             CallSlots[{MD, VC.VFunc.Offset}]
22525d8aea10SGeorge Rimar                 .ConstCSInfo[VC.Args]
2253943afb57SEugene Leviant                 .addSummaryTypeTestAssumeUser(FS);
22545d8aea10SGeorge Rimar           }
22555d8aea10SGeorge Rimar         }
22562325bb34SPeter Collingbourne         for (const FunctionSummary::ConstVCall &VC :
22575d8aea10SGeorge Rimar              FS->type_checked_load_const_vcalls()) {
22585d8aea10SGeorge Rimar           for (Metadata *MD : MetadataByGUID[VC.VFunc.GUID]) {
2259b406baaeSPeter Collingbourne             CallSlots[{MD, VC.VFunc.Offset}]
2260b406baaeSPeter Collingbourne                 .ConstCSInfo[VC.Args]
22612974856aSPeter Collingbourne                 .addSummaryTypeCheckedLoadUser(FS);
2262b406baaeSPeter Collingbourne           }
2263b406baaeSPeter Collingbourne         }
2264b406baaeSPeter Collingbourne       }
22655d8aea10SGeorge Rimar     }
22665d8aea10SGeorge Rimar   }
2267b406baaeSPeter Collingbourne 
22687efd7506SPeter Collingbourne   // For each (type, offset) pair:
2269df49d1bbSPeter Collingbourne   bool DidVirtualConstProp = false;
2270f3403fd2SIvan Krasin   std::map<std::string, Function*> DevirtTargets;
2271df49d1bbSPeter Collingbourne   for (auto &S : CallSlots) {
22727efd7506SPeter Collingbourne     // Search each of the members of the type identifier for the virtual
22737efd7506SPeter Collingbourne     // function implementation at offset S.first.ByteOffset, and add to
22747efd7506SPeter Collingbourne     // TargetsForSlot.
2275df49d1bbSPeter Collingbourne     std::vector<VirtualCallTarget> TargetsForSlot;
22762325bb34SPeter Collingbourne     WholeProgramDevirtResolution *Res = nullptr;
22776014c46cSTeresa Johnson     const std::set<TypeMemberInfo> &TypeMemberInfos = TypeIdMap[S.first.TypeID];
22786014c46cSTeresa Johnson     if (ExportSummary && isa<MDString>(S.first.TypeID) &&
22796014c46cSTeresa Johnson         TypeMemberInfos.size())
22806014c46cSTeresa Johnson       // For any type id used on a global's type metadata, create the type id
22816014c46cSTeresa Johnson       // summary resolution regardless of whether we can devirtualize, so that
22826014c46cSTeresa Johnson       // lower type tests knows the type id is not Unsat. If it was not used on
22836014c46cSTeresa Johnson       // a global's type metadata, the TypeIdMap entry set will be empty, and
22846014c46cSTeresa Johnson       // we don't want to create an entry (with the default Unknown type
22856014c46cSTeresa Johnson       // resolution), which can prevent detection of the Unsat.
2286f7691d8bSPeter Collingbourne       Res = &ExportSummary
22879a3f9797SPeter Collingbourne                  ->getOrInsertTypeIdSummary(
22889a3f9797SPeter Collingbourne                      cast<MDString>(S.first.TypeID)->getString())
22892325bb34SPeter Collingbourne                  .WPDRes[S.first.ByteOffset];
22906014c46cSTeresa Johnson     if (tryFindVirtualCallTargets(TargetsForSlot, TypeMemberInfos,
229109a704c5SMingming Liu                                   S.first.ByteOffset, ExportSummary)) {
22922325bb34SPeter Collingbourne 
2293943afb57SEugene Leviant       if (!trySingleImplDevirt(ExportSummary, TargetsForSlot, S.second, Res)) {
22942974856aSPeter Collingbourne         DidVirtualConstProp |=
22952974856aSPeter Collingbourne             tryVirtualConstProp(TargetsForSlot, S.second, Res, S.first);
22962974856aSPeter Collingbourne 
22972974856aSPeter Collingbourne         tryICallBranchFunnel(TargetsForSlot, S.second, Res, S.first);
22982974856aSPeter Collingbourne       }
2299f3403fd2SIvan Krasin 
2300f3403fd2SIvan Krasin       // Collect functions devirtualized at least for one call site for stats.
2301ced9a795STeresa Johnson       if (RemarksEnabled || AreStatisticsEnabled())
2302f3403fd2SIvan Krasin         for (const auto &T : TargetsForSlot)
2303f3403fd2SIvan Krasin           if (T.WasDevirt)
2304adcd0268SBenjamin Kramer             DevirtTargets[std::string(T.Fn->getName())] = T.Fn;
2305b05e06e4SIvan Krasin     }
2306df49d1bbSPeter Collingbourne 
2307b406baaeSPeter Collingbourne     // CFI-specific: if we are exporting and any llvm.type.checked.load
2308b406baaeSPeter Collingbourne     // intrinsics were *not* devirtualized, we need to add the resulting
2309b406baaeSPeter Collingbourne     // llvm.type.test intrinsics to the function summaries so that the
2310b406baaeSPeter Collingbourne     // LowerTypeTests pass will export them.
2311f7691d8bSPeter Collingbourne     if (ExportSummary && isa<MDString>(S.first.TypeID)) {
2312b406baaeSPeter Collingbourne       auto GUID =
2313b406baaeSPeter Collingbourne           GlobalValue::getGUID(cast<MDString>(S.first.TypeID)->getString());
2314b406baaeSPeter Collingbourne       for (auto FS : S.second.CSInfo.SummaryTypeCheckedLoadUsers)
2315b406baaeSPeter Collingbourne         FS->addTypeTest(GUID);
2316b406baaeSPeter Collingbourne       for (auto &CCS : S.second.ConstCSInfo)
2317b406baaeSPeter Collingbourne         for (auto FS : CCS.second.SummaryTypeCheckedLoadUsers)
2318b406baaeSPeter Collingbourne           FS->addTypeTest(GUID);
2319b406baaeSPeter Collingbourne     }
2320b406baaeSPeter Collingbourne   }
2321b406baaeSPeter Collingbourne 
2322f3403fd2SIvan Krasin   if (RemarksEnabled) {
2323f3403fd2SIvan Krasin     // Generate remarks for each devirtualized function.
2324f3403fd2SIvan Krasin     for (const auto &DT : DevirtTargets) {
2325f3403fd2SIvan Krasin       Function *F = DT.second;
2326e963c89dSSam Elliott 
2327e963c89dSSam Elliott       using namespace ore;
23289110cb45SPeter Collingbourne       OREGetter(F).emit(OptimizationRemark(DEBUG_TYPE, "Devirtualized", F)
23299110cb45SPeter Collingbourne                         << "devirtualized "
2330d2df54e6STeresa Johnson                         << NV("FunctionName", DT.first));
2331b05e06e4SIvan Krasin     }
2332df49d1bbSPeter Collingbourne   }
2333df49d1bbSPeter Collingbourne 
2334ced9a795STeresa Johnson   NumDevirtTargets += DevirtTargets.size();
2335ced9a795STeresa Johnson 
23366d284fabSPeter Collingbourne   removeRedundantTypeTests();
23370312f614SPeter Collingbourne 
2338df49d1bbSPeter Collingbourne   // Rebuild each global we touched as part of virtual constant propagation to
2339df49d1bbSPeter Collingbourne   // include the before and after bytes.
2340df49d1bbSPeter Collingbourne   if (DidVirtualConstProp)
2341df49d1bbSPeter Collingbourne     for (VTableBits &B : Bits)
2342df49d1bbSPeter Collingbourne       rebuildGlobal(B);
2343df49d1bbSPeter Collingbourne 
2344*9727c77dSDavid Green   // We have lowered or deleted the type intrinsics, so we will no longer have
2345*9727c77dSDavid Green   // enough information to reason about the liveness of virtual function
2346*9727c77dSDavid Green   // pointers in GlobalDCE.
23473b598b9cSOliver Stannard   for (GlobalVariable &GV : M.globals())
23483b598b9cSOliver Stannard     GV.eraseMetadata(LLVMContext::MD_vcall_visibility);
23493b598b9cSOliver Stannard 
2350df49d1bbSPeter Collingbourne   return true;
2351df49d1bbSPeter Collingbourne }
2352d2df54e6STeresa Johnson 
2353d2df54e6STeresa Johnson void DevirtIndex::run() {
2354d2df54e6STeresa Johnson   if (ExportSummary.typeIdCompatibleVtableMap().empty())
2355d2df54e6STeresa Johnson     return;
2356d2df54e6STeresa Johnson 
2357d2df54e6STeresa Johnson   DenseMap<GlobalValue::GUID, std::vector<StringRef>> NameByGUID;
2358d2df54e6STeresa Johnson   for (auto &P : ExportSummary.typeIdCompatibleVtableMap()) {
2359d2df54e6STeresa Johnson     NameByGUID[GlobalValue::getGUID(P.first)].push_back(P.first);
2360d2df54e6STeresa Johnson   }
2361d2df54e6STeresa Johnson 
2362d2df54e6STeresa Johnson   // Collect information from summary about which calls to try to devirtualize.
2363d2df54e6STeresa Johnson   for (auto &P : ExportSummary) {
2364d2df54e6STeresa Johnson     for (auto &S : P.second.SummaryList) {
2365d2df54e6STeresa Johnson       auto *FS = dyn_cast<FunctionSummary>(S.get());
2366d2df54e6STeresa Johnson       if (!FS)
2367d2df54e6STeresa Johnson         continue;
2368d2df54e6STeresa Johnson       // FIXME: Only add live functions.
2369d2df54e6STeresa Johnson       for (FunctionSummary::VFuncId VF : FS->type_test_assume_vcalls()) {
2370d2df54e6STeresa Johnson         for (StringRef Name : NameByGUID[VF.GUID]) {
2371d2df54e6STeresa Johnson           CallSlots[{Name, VF.Offset}].CSInfo.addSummaryTypeTestAssumeUser(FS);
2372d2df54e6STeresa Johnson         }
2373d2df54e6STeresa Johnson       }
2374d2df54e6STeresa Johnson       for (FunctionSummary::VFuncId VF : FS->type_checked_load_vcalls()) {
2375d2df54e6STeresa Johnson         for (StringRef Name : NameByGUID[VF.GUID]) {
2376d2df54e6STeresa Johnson           CallSlots[{Name, VF.Offset}].CSInfo.addSummaryTypeCheckedLoadUser(FS);
2377d2df54e6STeresa Johnson         }
2378d2df54e6STeresa Johnson       }
2379d2df54e6STeresa Johnson       for (const FunctionSummary::ConstVCall &VC :
2380d2df54e6STeresa Johnson            FS->type_test_assume_const_vcalls()) {
2381d2df54e6STeresa Johnson         for (StringRef Name : NameByGUID[VC.VFunc.GUID]) {
2382d2df54e6STeresa Johnson           CallSlots[{Name, VC.VFunc.Offset}]
2383d2df54e6STeresa Johnson               .ConstCSInfo[VC.Args]
2384d2df54e6STeresa Johnson               .addSummaryTypeTestAssumeUser(FS);
2385d2df54e6STeresa Johnson         }
2386d2df54e6STeresa Johnson       }
2387d2df54e6STeresa Johnson       for (const FunctionSummary::ConstVCall &VC :
2388d2df54e6STeresa Johnson            FS->type_checked_load_const_vcalls()) {
2389d2df54e6STeresa Johnson         for (StringRef Name : NameByGUID[VC.VFunc.GUID]) {
2390d2df54e6STeresa Johnson           CallSlots[{Name, VC.VFunc.Offset}]
2391d2df54e6STeresa Johnson               .ConstCSInfo[VC.Args]
2392d2df54e6STeresa Johnson               .addSummaryTypeCheckedLoadUser(FS);
2393d2df54e6STeresa Johnson         }
2394d2df54e6STeresa Johnson       }
2395d2df54e6STeresa Johnson     }
2396d2df54e6STeresa Johnson   }
2397d2df54e6STeresa Johnson 
2398d2df54e6STeresa Johnson   std::set<ValueInfo> DevirtTargets;
2399d2df54e6STeresa Johnson   // For each (type, offset) pair:
2400d2df54e6STeresa Johnson   for (auto &S : CallSlots) {
2401d2df54e6STeresa Johnson     // Search each of the members of the type identifier for the virtual
2402d2df54e6STeresa Johnson     // function implementation at offset S.first.ByteOffset, and add to
2403d2df54e6STeresa Johnson     // TargetsForSlot.
2404d2df54e6STeresa Johnson     std::vector<ValueInfo> TargetsForSlot;
2405d2df54e6STeresa Johnson     auto TidSummary = ExportSummary.getTypeIdCompatibleVtableSummary(S.first.TypeID);
2406d2df54e6STeresa Johnson     assert(TidSummary);
24076014c46cSTeresa Johnson     // Create the type id summary resolution regardlness of whether we can
24086014c46cSTeresa Johnson     // devirtualize, so that lower type tests knows the type id is used on
24096014c46cSTeresa Johnson     // a global and not Unsat.
2410d2df54e6STeresa Johnson     WholeProgramDevirtResolution *Res =
2411d2df54e6STeresa Johnson         &ExportSummary.getOrInsertTypeIdSummary(S.first.TypeID)
2412d2df54e6STeresa Johnson              .WPDRes[S.first.ByteOffset];
24136014c46cSTeresa Johnson     if (tryFindVirtualCallTargets(TargetsForSlot, *TidSummary,
24146014c46cSTeresa Johnson                                   S.first.ByteOffset)) {
2415d2df54e6STeresa Johnson 
2416d2df54e6STeresa Johnson       if (!trySingleImplDevirt(TargetsForSlot, S.first, S.second, Res,
2417d2df54e6STeresa Johnson                                DevirtTargets))
2418d2df54e6STeresa Johnson         continue;
2419d2df54e6STeresa Johnson     }
2420d2df54e6STeresa Johnson   }
2421d2df54e6STeresa Johnson 
2422d2df54e6STeresa Johnson   // Optionally have the thin link print message for each devirtualized
2423d2df54e6STeresa Johnson   // function.
2424d2df54e6STeresa Johnson   if (PrintSummaryDevirt)
2425d2df54e6STeresa Johnson     for (const auto &DT : DevirtTargets)
2426d2df54e6STeresa Johnson       errs() << "Devirtualized call to " << DT << "\n";
2427ced9a795STeresa Johnson 
2428ced9a795STeresa Johnson   NumDevirtTargets += DevirtTargets.size();
2429d2df54e6STeresa Johnson }
2430