LLVM  15.0.0git
LowerTypeTests.cpp
Go to the documentation of this file.
1 //===- LowerTypeTests.cpp - type metadata lowering pass -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass lowers type metadata and calls to the llvm.type.test intrinsic.
10 // It also ensures that globals are properly laid out for the
11 // llvm.icall.branch.funnel intrinsic.
12 // See http://llvm.org/docs/TypeMetadata.html for more information.
13 //
14 //===----------------------------------------------------------------------===//
15 
17 #include "llvm/ADT/APInt.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/TinyPtrVector.h"
27 #include "llvm/ADT/Triple.h"
30 #include "llvm/IR/Attributes.h"
31 #include "llvm/IR/BasicBlock.h"
32 #include "llvm/IR/Constant.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/DataLayout.h"
35 #include "llvm/IR/DerivedTypes.h"
36 #include "llvm/IR/Function.h"
37 #include "llvm/IR/GlobalAlias.h"
38 #include "llvm/IR/GlobalObject.h"
39 #include "llvm/IR/GlobalValue.h"
40 #include "llvm/IR/GlobalVariable.h"
41 #include "llvm/IR/IRBuilder.h"
42 #include "llvm/IR/InlineAsm.h"
43 #include "llvm/IR/Instruction.h"
44 #include "llvm/IR/Instructions.h"
45 #include "llvm/IR/IntrinsicInst.h"
46 #include "llvm/IR/Intrinsics.h"
47 #include "llvm/IR/LLVMContext.h"
48 #include "llvm/IR/Metadata.h"
49 #include "llvm/IR/Module.h"
52 #include "llvm/IR/Operator.h"
53 #include "llvm/IR/PassManager.h"
54 #include "llvm/IR/Type.h"
55 #include "llvm/IR/Use.h"
56 #include "llvm/IR/User.h"
57 #include "llvm/IR/Value.h"
58 #include "llvm/InitializePasses.h"
59 #include "llvm/Pass.h"
60 #include "llvm/Support/Allocator.h"
61 #include "llvm/Support/Casting.h"
63 #include "llvm/Support/Debug.h"
64 #include "llvm/Support/Error.h"
72 #include "llvm/Transforms/IPO.h"
75 #include <algorithm>
76 #include <cassert>
77 #include <cstdint>
78 #include <memory>
79 #include <set>
80 #include <string>
81 #include <system_error>
82 #include <utility>
83 #include <vector>
84 
85 using namespace llvm;
86 using namespace lowertypetests;
87 
88 #define DEBUG_TYPE "lowertypetests"
89 
90 STATISTIC(ByteArraySizeBits, "Byte array size in bits");
91 STATISTIC(ByteArraySizeBytes, "Byte array size in bytes");
92 STATISTIC(NumByteArraysCreated, "Number of byte arrays created");
93 STATISTIC(NumTypeTestCallsLowered, "Number of type test calls lowered");
94 STATISTIC(NumTypeIdDisjointSets, "Number of disjoint sets of type identifiers");
95 
97  "lowertypetests-avoid-reuse",
98  cl::desc("Try to avoid reuse of byte array addresses using aliases"),
99  cl::Hidden, cl::init(true));
100 
102  "lowertypetests-summary-action",
103  cl::desc("What to do with the summary when running this pass"),
104  cl::values(clEnumValN(PassSummaryAction::None, "none", "Do nothing"),
106  "Import typeid resolutions from summary and globals"),
108  "Export typeid resolutions to summary and globals")),
109  cl::Hidden);
110 
112  "lowertypetests-read-summary",
113  cl::desc("Read summary from given YAML file before running pass"),
114  cl::Hidden);
115 
117  "lowertypetests-write-summary",
118  cl::desc("Write summary to given YAML file after running pass"),
119  cl::Hidden);
120 
121 static cl::opt<bool>
122  ClDropTypeTests("lowertypetests-drop-type-tests",
123  cl::desc("Simply drop type test assume sequences"),
124  cl::Hidden, cl::init(false));
125 
127  if (Offset < ByteOffset)
128  return false;
129 
130  if ((Offset - ByteOffset) % (uint64_t(1) << AlignLog2) != 0)
131  return false;
132 
133  uint64_t BitOffset = (Offset - ByteOffset) >> AlignLog2;
134  if (BitOffset >= BitSize)
135  return false;
136 
137  return Bits.count(BitOffset);
138 }
139 
141  OS << "offset " << ByteOffset << " size " << BitSize << " align "
142  << (1 << AlignLog2);
143 
144  if (isAllOnes()) {
145  OS << " all-ones\n";
146  return;
147  }
148 
149  OS << " { ";
150  for (uint64_t B : Bits)
151  OS << B << ' ';
152  OS << "}\n";
153 }
154 
156  if (Min > Max)
157  Min = 0;
158 
159  // Normalize each offset against the minimum observed offset, and compute
160  // the bitwise OR of each of the offsets. The number of trailing zeros
161  // in the mask gives us the log2 of the alignment of all offsets, which
162  // allows us to compress the bitset by only storing one bit per aligned
163  // address.
164  uint64_t Mask = 0;
165  for (uint64_t &Offset : Offsets) {
166  Offset -= Min;
167  Mask |= Offset;
168  }
169 
170  BitSetInfo BSI;
171  BSI.ByteOffset = Min;
172 
173  BSI.AlignLog2 = 0;
174  if (Mask != 0)
176 
177  // Build the compressed bitset while normalizing the offsets against the
178  // computed alignment.
179  BSI.BitSize = ((Max - Min) >> BSI.AlignLog2) + 1;
180  for (uint64_t Offset : Offsets) {
181  Offset >>= BSI.AlignLog2;
182  BSI.Bits.insert(Offset);
183  }
184 
185  return BSI;
186 }
187 
188 void GlobalLayoutBuilder::addFragment(const std::set<uint64_t> &F) {
189  // Create a new fragment to hold the layout for F.
190  Fragments.emplace_back();
191  std::vector<uint64_t> &Fragment = Fragments.back();
192  uint64_t FragmentIndex = Fragments.size() - 1;
193 
194  for (auto ObjIndex : F) {
195  uint64_t OldFragmentIndex = FragmentMap[ObjIndex];
196  if (OldFragmentIndex == 0) {
197  // We haven't seen this object index before, so just add it to the current
198  // fragment.
199  Fragment.push_back(ObjIndex);
200  } else {
201  // This index belongs to an existing fragment. Copy the elements of the
202  // old fragment into this one and clear the old fragment. We don't update
203  // the fragment map just yet, this ensures that any further references to
204  // indices from the old fragment in this fragment do not insert any more
205  // indices.
206  std::vector<uint64_t> &OldFragment = Fragments[OldFragmentIndex];
207  llvm::append_range(Fragment, OldFragment);
208  OldFragment.clear();
209  }
210  }
211 
212  // Update the fragment map to point our object indices to this fragment.
213  for (uint64_t ObjIndex : Fragment)
214  FragmentMap[ObjIndex] = FragmentIndex;
215 }
216 
217 void ByteArrayBuilder::allocate(const std::set<uint64_t> &Bits,
218  uint64_t BitSize, uint64_t &AllocByteOffset,
219  uint8_t &AllocMask) {
220  // Find the smallest current allocation.
221  unsigned Bit = 0;
222  for (unsigned I = 1; I != BitsPerByte; ++I)
223  if (BitAllocs[I] < BitAllocs[Bit])
224  Bit = I;
225 
226  AllocByteOffset = BitAllocs[Bit];
227 
228  // Add our size to it.
229  unsigned ReqSize = AllocByteOffset + BitSize;
230  BitAllocs[Bit] = ReqSize;
231  if (Bytes.size() < ReqSize)
232  Bytes.resize(ReqSize);
233 
234  // Set our bits.
235  AllocMask = 1 << Bit;
236  for (uint64_t B : Bits)
237  Bytes[AllocByteOffset + B] |= AllocMask;
238 }
239 
241  if (F->isDeclarationForLinker())
242  return false;
243  auto *CI = mdconst::extract_or_null<ConstantInt>(
244  F->getParent()->getModuleFlag("CFI Canonical Jump Tables"));
245  if (!CI || CI->getZExtValue() != 0)
246  return true;
247  return F->hasFnAttribute("cfi-canonical-jump-table");
248 }
249 
250 namespace {
251 
252 struct ByteArrayInfo {
253  std::set<uint64_t> Bits;
254  uint64_t BitSize;
255  GlobalVariable *ByteArray;
256  GlobalVariable *MaskGlobal;
257  uint8_t *MaskPtr = nullptr;
258 };
259 
260 /// A POD-like structure that we use to store a global reference together with
261 /// its metadata types. In this pass we frequently need to query the set of
262 /// metadata types referenced by a global, which at the IR level is an expensive
263 /// operation involving a map lookup; this data structure helps to reduce the
264 /// number of times we need to do this lookup.
265 class GlobalTypeMember final : TrailingObjects<GlobalTypeMember, MDNode *> {
266  friend TrailingObjects;
267 
268  GlobalObject *GO;
269  size_t NTypes;
270 
271  // For functions: true if the jump table is canonical. This essentially means
272  // whether the canonical address (i.e. the symbol table entry) of the function
273  // is provided by the local jump table. This is normally the same as whether
274  // the function is defined locally, but if canonical jump tables are disabled
275  // by the user then the jump table never provides a canonical definition.
276  bool IsJumpTableCanonical;
277 
278  // For functions: true if this function is either defined or used in a thinlto
279  // module and its jumptable entry needs to be exported to thinlto backends.
280  bool IsExported;
281 
282  size_t numTrailingObjects(OverloadToken<MDNode *>) const { return NTypes; }
283 
284 public:
285  static GlobalTypeMember *create(BumpPtrAllocator &Alloc, GlobalObject *GO,
286  bool IsJumpTableCanonical, bool IsExported,
287  ArrayRef<MDNode *> Types) {
288  auto *GTM = static_cast<GlobalTypeMember *>(Alloc.Allocate(
289  totalSizeToAlloc<MDNode *>(Types.size()), alignof(GlobalTypeMember)));
290  GTM->GO = GO;
291  GTM->NTypes = Types.size();
292  GTM->IsJumpTableCanonical = IsJumpTableCanonical;
293  GTM->IsExported = IsExported;
294  std::uninitialized_copy(Types.begin(), Types.end(),
295  GTM->getTrailingObjects<MDNode *>());
296  return GTM;
297  }
298 
299  GlobalObject *getGlobal() const {
300  return GO;
301  }
302 
303  bool isJumpTableCanonical() const {
304  return IsJumpTableCanonical;
305  }
306 
307  bool isExported() const {
308  return IsExported;
309  }
310 
311  ArrayRef<MDNode *> types() const {
312  return makeArrayRef(getTrailingObjects<MDNode *>(), NTypes);
313  }
314 };
315 
316 struct ICallBranchFunnel final
317  : TrailingObjects<ICallBranchFunnel, GlobalTypeMember *> {
318  static ICallBranchFunnel *create(BumpPtrAllocator &Alloc, CallInst *CI,
320  unsigned UniqueId) {
321  auto *Call = static_cast<ICallBranchFunnel *>(
322  Alloc.Allocate(totalSizeToAlloc<GlobalTypeMember *>(Targets.size()),
323  alignof(ICallBranchFunnel)));
324  Call->CI = CI;
325  Call->UniqueId = UniqueId;
326  Call->NTargets = Targets.size();
327  std::uninitialized_copy(Targets.begin(), Targets.end(),
328  Call->getTrailingObjects<GlobalTypeMember *>());
329  return Call;
330  }
331 
332  CallInst *CI;
334  return makeArrayRef(getTrailingObjects<GlobalTypeMember *>(), NTargets);
335  }
336 
337  unsigned UniqueId;
338 
339 private:
340  size_t NTargets;
341 };
342 
343 struct ScopedSaveAliaseesAndUsed {
344  Module &M;
345  SmallVector<GlobalValue *, 4> Used, CompilerUsed;
346  std::vector<std::pair<GlobalAlias *, Function *>> FunctionAliases;
347  std::vector<std::pair<GlobalIFunc *, Function *>> ResolverIFuncs;
348 
349  ScopedSaveAliaseesAndUsed(Module &M) : M(M) {
350  // The users of this class want to replace all function references except
351  // for aliases and llvm.used/llvm.compiler.used with references to a jump
352  // table. We avoid replacing aliases in order to avoid introducing a double
353  // indirection (or an alias pointing to a declaration in ThinLTO mode), and
354  // we avoid replacing llvm.used/llvm.compiler.used because these global
355  // variables describe properties of the global, not the jump table (besides,
356  // offseted references to the jump table in llvm.used are invalid).
357  // Unfortunately, LLVM doesn't have a "RAUW except for these (possibly
358  // indirect) users", so what we do is save the list of globals referenced by
359  // llvm.used/llvm.compiler.used and aliases, erase the used lists, let RAUW
360  // replace the aliasees and then set them back to their original values at
361  // the end.
362  if (GlobalVariable *GV = collectUsedGlobalVariables(M, Used, false))
363  GV->eraseFromParent();
364  if (GlobalVariable *GV = collectUsedGlobalVariables(M, CompilerUsed, true))
365  GV->eraseFromParent();
366 
367  for (auto &GA : M.aliases()) {
368  // FIXME: This should look past all aliases not just interposable ones,
369  // see discussion on D65118.
370  if (auto *F = dyn_cast<Function>(GA.getAliasee()->stripPointerCasts()))
371  FunctionAliases.push_back({&GA, F});
372  }
373 
374  for (auto &GI : M.ifuncs())
375  if (auto *F = dyn_cast<Function>(GI.getResolver()->stripPointerCasts()))
376  ResolverIFuncs.push_back({&GI, F});
377  }
378 
379  ~ScopedSaveAliaseesAndUsed() {
380  appendToUsed(M, Used);
381  appendToCompilerUsed(M, CompilerUsed);
382 
383  for (auto P : FunctionAliases)
384  P.first->setAliasee(
385  ConstantExpr::getBitCast(P.second, P.first->getType()));
386 
387  for (auto P : ResolverIFuncs) {
388  // This does not preserve pointer casts that may have been stripped by the
389  // constructor, but the resolver's type is different from that of the
390  // ifunc anyway.
391  P.first->setResolver(P.second);
392  }
393  }
394 };
395 
396 class LowerTypeTestsModule {
397  Module &M;
398 
399  ModuleSummaryIndex *ExportSummary;
400  const ModuleSummaryIndex *ImportSummary;
401  // Set when the client has invoked this to simply drop all type test assume
402  // sequences.
403  bool DropTypeTests;
404 
405  Triple::ArchType Arch;
406  Triple::OSType OS;
407  Triple::ObjectFormatType ObjectFormat;
408 
409  IntegerType *Int1Ty = Type::getInt1Ty(M.getContext());
410  IntegerType *Int8Ty = Type::getInt8Ty(M.getContext());
411  PointerType *Int8PtrTy = Type::getInt8PtrTy(M.getContext());
412  ArrayType *Int8Arr0Ty = ArrayType::get(Type::getInt8Ty(M.getContext()), 0);
413  IntegerType *Int32Ty = Type::getInt32Ty(M.getContext());
415  IntegerType *Int64Ty = Type::getInt64Ty(M.getContext());
416  IntegerType *IntPtrTy = M.getDataLayout().getIntPtrType(M.getContext(), 0);
417 
418  // Indirect function call index assignment counter for WebAssembly
419  uint64_t IndirectIndex = 1;
420 
421  // Mapping from type identifiers to the call sites that test them, as well as
422  // whether the type identifier needs to be exported to ThinLTO backends as
423  // part of the regular LTO phase of the ThinLTO pipeline (see exportTypeId).
424  struct TypeIdUserInfo {
425  std::vector<CallInst *> CallSites;
426  bool IsExported = false;
427  };
429 
430  /// This structure describes how to lower type tests for a particular type
431  /// identifier. It is either built directly from the global analysis (during
432  /// regular LTO or the regular LTO phase of ThinLTO), or indirectly using type
433  /// identifier summaries and external symbol references (in ThinLTO backends).
434  struct TypeIdLowering {
436 
437  /// All except Unsat: the start address within the combined global.
438  Constant *OffsetedGlobal;
439 
440  /// ByteArray, Inline, AllOnes: log2 of the required global alignment
441  /// relative to the start address.
442  Constant *AlignLog2;
443 
444  /// ByteArray, Inline, AllOnes: one less than the size of the memory region
445  /// covering members of this type identifier as a multiple of 2^AlignLog2.
446  Constant *SizeM1;
447 
448  /// ByteArray: the byte array to test the address against.
449  Constant *TheByteArray;
450 
451  /// ByteArray: the bit mask to apply to bytes loaded from the byte array.
452  Constant *BitMask;
453 
454  /// Inline: the bit mask to test the address against.
455  Constant *InlineBits;
456  };
457 
458  std::vector<ByteArrayInfo> ByteArrayInfos;
459 
460  Function *WeakInitializerFn = nullptr;
461 
462  bool shouldExportConstantsAsAbsoluteSymbols();
463  uint8_t *exportTypeId(StringRef TypeId, const TypeIdLowering &TIL);
464  TypeIdLowering importTypeId(StringRef TypeId);
465  void importTypeTest(CallInst *CI);
466  void importFunction(Function *F, bool isJumpTableCanonical,
467  std::vector<GlobalAlias *> &AliasesToErase);
468 
469  BitSetInfo
470  buildBitSet(Metadata *TypeId,
471  const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout);
472  ByteArrayInfo *createByteArray(BitSetInfo &BSI);
473  void allocateByteArrays();
474  Value *createBitSetTest(IRBuilder<> &B, const TypeIdLowering &TIL,
475  Value *BitOffset);
476  void lowerTypeTestCalls(
477  ArrayRef<Metadata *> TypeIds, Constant *CombinedGlobalAddr,
478  const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout);
479  Value *lowerTypeTestCall(Metadata *TypeId, CallInst *CI,
480  const TypeIdLowering &TIL);
481 
482  void buildBitSetsFromGlobalVariables(ArrayRef<Metadata *> TypeIds,
484  unsigned getJumpTableEntrySize();
485  Type *getJumpTableEntryType();
486  void createJumpTableEntry(raw_ostream &AsmOS, raw_ostream &ConstraintOS,
487  Triple::ArchType JumpTableArch,
488  SmallVectorImpl<Value *> &AsmArgs, Function *Dest);
489  void verifyTypeMDNode(GlobalObject *GO, MDNode *Type);
490  void buildBitSetsFromFunctions(ArrayRef<Metadata *> TypeIds,
491  ArrayRef<GlobalTypeMember *> Functions);
492  void buildBitSetsFromFunctionsNative(ArrayRef<Metadata *> TypeIds,
493  ArrayRef<GlobalTypeMember *> Functions);
494  void buildBitSetsFromFunctionsWASM(ArrayRef<Metadata *> TypeIds,
495  ArrayRef<GlobalTypeMember *> Functions);
496  void
497  buildBitSetsFromDisjointSet(ArrayRef<Metadata *> TypeIds,
499  ArrayRef<ICallBranchFunnel *> ICallBranchFunnels);
500 
501  void replaceWeakDeclarationWithJumpTablePtr(Function *F, Constant *JT,
502  bool IsJumpTableCanonical);
503  void moveInitializerToModuleConstructor(GlobalVariable *GV);
504  void findGlobalVariableUsersOf(Constant *C,
506 
507  void createJumpTable(Function *F, ArrayRef<GlobalTypeMember *> Functions);
508 
509  /// replaceCfiUses - Go through the uses list for this definition
510  /// and make each use point to "V" instead of "this" when the use is outside
511  /// the block. 'This's use list is expected to have at least one element.
512  /// Unlike replaceAllUsesWith this function skips blockaddr and direct call
513  /// uses.
514  void replaceCfiUses(Function *Old, Value *New, bool IsJumpTableCanonical);
515 
516  /// replaceDirectCalls - Go through the uses list for this definition and
517  /// replace each use, which is a direct function call.
518  void replaceDirectCalls(Value *Old, Value *New);
519 
520 public:
521  LowerTypeTestsModule(Module &M, ModuleSummaryIndex *ExportSummary,
522  const ModuleSummaryIndex *ImportSummary,
523  bool DropTypeTests);
524 
525  bool lower();
526 
527  // Lower the module using the action and summary passed as command line
528  // arguments. For testing purposes only.
529  static bool runForTesting(Module &M);
530 };
531 
532 struct LowerTypeTests : public ModulePass {
533  static char ID;
534 
535  bool UseCommandLine = false;
536 
537  ModuleSummaryIndex *ExportSummary;
538  const ModuleSummaryIndex *ImportSummary;
539  bool DropTypeTests;
540 
541  LowerTypeTests() : ModulePass(ID), UseCommandLine(true) {
543  }
544 
545  LowerTypeTests(ModuleSummaryIndex *ExportSummary,
546  const ModuleSummaryIndex *ImportSummary, bool DropTypeTests)
547  : ModulePass(ID), ExportSummary(ExportSummary),
548  ImportSummary(ImportSummary),
549  DropTypeTests(DropTypeTests || ClDropTypeTests) {
551  }
552 
553  bool runOnModule(Module &M) override {
554  if (UseCommandLine)
555  return LowerTypeTestsModule::runForTesting(M);
556  return LowerTypeTestsModule(M, ExportSummary, ImportSummary, DropTypeTests)
557  .lower();
558  }
559 };
560 
561 } // end anonymous namespace
562 
563 char LowerTypeTests::ID = 0;
564 
565 INITIALIZE_PASS(LowerTypeTests, "lowertypetests", "Lower type metadata", false,
566  false)
567 
568 ModulePass *
570  const ModuleSummaryIndex *ImportSummary,
571  bool DropTypeTests) {
572  return new LowerTypeTests(ExportSummary, ImportSummary, DropTypeTests);
573 }
574 
575 /// Build a bit set for TypeId using the object layouts in
576 /// GlobalLayout.
577 BitSetInfo LowerTypeTestsModule::buildBitSet(
578  Metadata *TypeId,
579  const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout) {
580  BitSetBuilder BSB;
581 
582  // Compute the byte offset of each address associated with this type
583  // identifier.
584  for (auto &GlobalAndOffset : GlobalLayout) {
585  for (MDNode *Type : GlobalAndOffset.first->types()) {
586  if (Type->getOperand(1) != TypeId)
587  continue;
588  uint64_t Offset =
589  cast<ConstantInt>(
590  cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
591  ->getZExtValue();
592  BSB.addOffset(GlobalAndOffset.second + Offset);
593  }
594  }
595 
596  return BSB.build();
597 }
598 
599 /// Build a test that bit BitOffset mod sizeof(Bits)*8 is set in
600 /// Bits. This pattern matches to the bt instruction on x86.
602  Value *BitOffset) {
603  auto BitsType = cast<IntegerType>(Bits->getType());
604  unsigned BitWidth = BitsType->getBitWidth();
605 
606  BitOffset = B.CreateZExtOrTrunc(BitOffset, BitsType);
607  Value *BitIndex =
608  B.CreateAnd(BitOffset, ConstantInt::get(BitsType, BitWidth - 1));
609  Value *BitMask = B.CreateShl(ConstantInt::get(BitsType, 1), BitIndex);
610  Value *MaskedBits = B.CreateAnd(Bits, BitMask);
611  return B.CreateICmpNE(MaskedBits, ConstantInt::get(BitsType, 0));
612 }
613 
614 ByteArrayInfo *LowerTypeTestsModule::createByteArray(BitSetInfo &BSI) {
615  // Create globals to stand in for byte arrays and masks. These never actually
616  // get initialized, we RAUW and erase them later in allocateByteArrays() once
617  // we know the offset and mask to use.
618  auto ByteArrayGlobal = new GlobalVariable(
619  M, Int8Ty, /*isConstant=*/true, GlobalValue::PrivateLinkage, nullptr);
620  auto MaskGlobal = new GlobalVariable(M, Int8Ty, /*isConstant=*/true,
621  GlobalValue::PrivateLinkage, nullptr);
622 
623  ByteArrayInfos.emplace_back();
624  ByteArrayInfo *BAI = &ByteArrayInfos.back();
625 
626  BAI->Bits = BSI.Bits;
627  BAI->BitSize = BSI.BitSize;
628  BAI->ByteArray = ByteArrayGlobal;
629  BAI->MaskGlobal = MaskGlobal;
630  return BAI;
631 }
632 
633 void LowerTypeTestsModule::allocateByteArrays() {
634  llvm::stable_sort(ByteArrayInfos,
635  [](const ByteArrayInfo &BAI1, const ByteArrayInfo &BAI2) {
636  return BAI1.BitSize > BAI2.BitSize;
637  });
638 
639  std::vector<uint64_t> ByteArrayOffsets(ByteArrayInfos.size());
640 
641  ByteArrayBuilder BAB;
642  for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) {
643  ByteArrayInfo *BAI = &ByteArrayInfos[I];
644 
645  uint8_t Mask;
646  BAB.allocate(BAI->Bits, BAI->BitSize, ByteArrayOffsets[I], Mask);
647 
648  BAI->MaskGlobal->replaceAllUsesWith(
649  ConstantExpr::getIntToPtr(ConstantInt::get(Int8Ty, Mask), Int8PtrTy));
650  BAI->MaskGlobal->eraseFromParent();
651  if (BAI->MaskPtr)
652  *BAI->MaskPtr = Mask;
653  }
654 
655  Constant *ByteArrayConst = ConstantDataArray::get(M.getContext(), BAB.Bytes);
656  auto ByteArray =
657  new GlobalVariable(M, ByteArrayConst->getType(), /*isConstant=*/true,
658  GlobalValue::PrivateLinkage, ByteArrayConst);
659 
660  for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) {
661  ByteArrayInfo *BAI = &ByteArrayInfos[I];
662 
663  Constant *Idxs[] = {ConstantInt::get(IntPtrTy, 0),
664  ConstantInt::get(IntPtrTy, ByteArrayOffsets[I])};
666  ByteArrayConst->getType(), ByteArray, Idxs);
667 
668  // Create an alias instead of RAUW'ing the gep directly. On x86 this ensures
669  // that the pc-relative displacement is folded into the lea instead of the
670  // test instruction getting another displacement.
672  Int8Ty, 0, GlobalValue::PrivateLinkage, "bits", GEP, &M);
673  BAI->ByteArray->replaceAllUsesWith(Alias);
674  BAI->ByteArray->eraseFromParent();
675  }
676 
677  ByteArraySizeBits = BAB.BitAllocs[0] + BAB.BitAllocs[1] + BAB.BitAllocs[2] +
678  BAB.BitAllocs[3] + BAB.BitAllocs[4] + BAB.BitAllocs[5] +
679  BAB.BitAllocs[6] + BAB.BitAllocs[7];
680  ByteArraySizeBytes = BAB.Bytes.size();
681 }
682 
683 /// Build a test that bit BitOffset is set in the type identifier that was
684 /// lowered to TIL, which must be either an Inline or a ByteArray.
685 Value *LowerTypeTestsModule::createBitSetTest(IRBuilder<> &B,
686  const TypeIdLowering &TIL,
687  Value *BitOffset) {
688  if (TIL.TheKind == TypeTestResolution::Inline) {
689  // If the bit set is sufficiently small, we can avoid a load by bit testing
690  // a constant.
691  return createMaskedBitTest(B, TIL.InlineBits, BitOffset);
692  } else {
693  Constant *ByteArray = TIL.TheByteArray;
694  if (AvoidReuse && !ImportSummary) {
695  // Each use of the byte array uses a different alias. This makes the
696  // backend less likely to reuse previously computed byte array addresses,
697  // improving the security of the CFI mechanism based on this pass.
698  // This won't work when importing because TheByteArray is external.
699  ByteArray = GlobalAlias::create(Int8Ty, 0, GlobalValue::PrivateLinkage,
700  "bits_use", ByteArray, &M);
701  }
702 
703  Value *ByteAddr = B.CreateGEP(Int8Ty, ByteArray, BitOffset);
704  Value *Byte = B.CreateLoad(Int8Ty, ByteAddr);
705 
706  Value *ByteAndMask =
707  B.CreateAnd(Byte, ConstantExpr::getPtrToInt(TIL.BitMask, Int8Ty));
708  return B.CreateICmpNE(ByteAndMask, ConstantInt::get(Int8Ty, 0));
709  }
710 }
711 
712 static bool isKnownTypeIdMember(Metadata *TypeId, const DataLayout &DL,
713  Value *V, uint64_t COffset) {
714  if (auto GV = dyn_cast<GlobalObject>(V)) {
716  GV->getMetadata(LLVMContext::MD_type, Types);
717  for (MDNode *Type : Types) {
718  if (Type->getOperand(1) != TypeId)
719  continue;
720  uint64_t Offset =
721  cast<ConstantInt>(
722  cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
723  ->getZExtValue();
724  if (COffset == Offset)
725  return true;
726  }
727  return false;
728  }
729 
730  if (auto GEP = dyn_cast<GEPOperator>(V)) {
731  APInt APOffset(DL.getPointerSizeInBits(0), 0);
732  bool Result = GEP->accumulateConstantOffset(DL, APOffset);
733  if (!Result)
734  return false;
735  COffset += APOffset.getZExtValue();
736  return isKnownTypeIdMember(TypeId, DL, GEP->getPointerOperand(), COffset);
737  }
738 
739  if (auto Op = dyn_cast<Operator>(V)) {
740  if (Op->getOpcode() == Instruction::BitCast)
741  return isKnownTypeIdMember(TypeId, DL, Op->getOperand(0), COffset);
742 
743  if (Op->getOpcode() == Instruction::Select)
744  return isKnownTypeIdMember(TypeId, DL, Op->getOperand(1), COffset) &&
745  isKnownTypeIdMember(TypeId, DL, Op->getOperand(2), COffset);
746  }
747 
748  return false;
749 }
750 
751 /// Lower a llvm.type.test call to its implementation. Returns the value to
752 /// replace the call with.
753 Value *LowerTypeTestsModule::lowerTypeTestCall(Metadata *TypeId, CallInst *CI,
754  const TypeIdLowering &TIL) {
755  // Delay lowering if the resolution is currently unknown.
756  if (TIL.TheKind == TypeTestResolution::Unknown)
757  return nullptr;
758  if (TIL.TheKind == TypeTestResolution::Unsat)
759  return ConstantInt::getFalse(M.getContext());
760 
761  Value *Ptr = CI->getArgOperand(0);
762  const DataLayout &DL = M.getDataLayout();
763  if (isKnownTypeIdMember(TypeId, DL, Ptr, 0))
764  return ConstantInt::getTrue(M.getContext());
765 
766  BasicBlock *InitialBB = CI->getParent();
767 
768  IRBuilder<> B(CI);
769 
770  Value *PtrAsInt = B.CreatePtrToInt(Ptr, IntPtrTy);
771 
772  Constant *OffsetedGlobalAsInt =
773  ConstantExpr::getPtrToInt(TIL.OffsetedGlobal, IntPtrTy);
774  if (TIL.TheKind == TypeTestResolution::Single)
775  return B.CreateICmpEQ(PtrAsInt, OffsetedGlobalAsInt);
776 
777  Value *PtrOffset = B.CreateSub(PtrAsInt, OffsetedGlobalAsInt);
778 
779  // We need to check that the offset both falls within our range and is
780  // suitably aligned. We can check both properties at the same time by
781  // performing a right rotate by log2(alignment) followed by an integer
782  // comparison against the bitset size. The rotate will move the lower
783  // order bits that need to be zero into the higher order bits of the
784  // result, causing the comparison to fail if they are nonzero. The rotate
785  // also conveniently gives us a bit offset to use during the load from
786  // the bitset.
787  Value *OffsetSHR =
788  B.CreateLShr(PtrOffset, ConstantExpr::getZExt(TIL.AlignLog2, IntPtrTy));
789  Value *OffsetSHL = B.CreateShl(
790  PtrOffset, ConstantExpr::getZExt(
792  ConstantInt::get(Int8Ty, DL.getPointerSizeInBits(0)),
793  TIL.AlignLog2),
794  IntPtrTy));
795  Value *BitOffset = B.CreateOr(OffsetSHR, OffsetSHL);
796 
797  Value *OffsetInRange = B.CreateICmpULE(BitOffset, TIL.SizeM1);
798 
799  // If the bit set is all ones, testing against it is unnecessary.
800  if (TIL.TheKind == TypeTestResolution::AllOnes)
801  return OffsetInRange;
802 
803  // See if the intrinsic is used in the following common pattern:
804  // br(llvm.type.test(...), thenbb, elsebb)
805  // where nothing happens between the type test and the br.
806  // If so, create slightly simpler IR.
807  if (CI->hasOneUse())
808  if (auto *Br = dyn_cast<BranchInst>(*CI->user_begin()))
809  if (CI->getNextNode() == Br) {
810  BasicBlock *Then = InitialBB->splitBasicBlock(CI->getIterator());
811  BasicBlock *Else = Br->getSuccessor(1);
812  BranchInst *NewBr = BranchInst::Create(Then, Else, OffsetInRange);
813  NewBr->setMetadata(LLVMContext::MD_prof,
814  Br->getMetadata(LLVMContext::MD_prof));
815  ReplaceInstWithInst(InitialBB->getTerminator(), NewBr);
816 
817  // Update phis in Else resulting from InitialBB being split
818  for (auto &Phi : Else->phis())
819  Phi.addIncoming(Phi.getIncomingValueForBlock(Then), InitialBB);
820 
821  IRBuilder<> ThenB(CI);
822  return createBitSetTest(ThenB, TIL, BitOffset);
823  }
824 
825  IRBuilder<> ThenB(SplitBlockAndInsertIfThen(OffsetInRange, CI, false));
826 
827  // Now that we know that the offset is in range and aligned, load the
828  // appropriate bit from the bitset.
829  Value *Bit = createBitSetTest(ThenB, TIL, BitOffset);
830 
831  // The value we want is 0 if we came directly from the initial block
832  // (having failed the range or alignment checks), or the loaded bit if
833  // we came from the block in which we loaded it.
834  B.SetInsertPoint(CI);
835  PHINode *P = B.CreatePHI(Int1Ty, 2);
836  P->addIncoming(ConstantInt::get(Int1Ty, 0), InitialBB);
837  P->addIncoming(Bit, ThenB.GetInsertBlock());
838  return P;
839 }
840 
841 /// Given a disjoint set of type identifiers and globals, lay out the globals,
842 /// build the bit sets and lower the llvm.type.test calls.
843 void LowerTypeTestsModule::buildBitSetsFromGlobalVariables(
845  // Build a new global with the combined contents of the referenced globals.
846  // This global is a struct whose even-indexed elements contain the original
847  // contents of the referenced globals and whose odd-indexed elements contain
848  // any padding required to align the next element to the next power of 2 plus
849  // any additional padding required to meet its alignment requirements.
850  std::vector<Constant *> GlobalInits;
851  const DataLayout &DL = M.getDataLayout();
853  Align MaxAlign;
854  uint64_t CurOffset = 0;
855  uint64_t DesiredPadding = 0;
856  for (GlobalTypeMember *G : Globals) {
857  auto *GV = cast<GlobalVariable>(G->getGlobal());
858  Align Alignment =
859  DL.getValueOrABITypeAlignment(GV->getAlign(), GV->getValueType());
860  MaxAlign = std::max(MaxAlign, Alignment);
861  uint64_t GVOffset = alignTo(CurOffset + DesiredPadding, Alignment);
862  GlobalLayout[G] = GVOffset;
863  if (GVOffset != 0) {
864  uint64_t Padding = GVOffset - CurOffset;
865  GlobalInits.push_back(
866  ConstantAggregateZero::get(ArrayType::get(Int8Ty, Padding)));
867  }
868 
869  GlobalInits.push_back(GV->getInitializer());
870  uint64_t InitSize = DL.getTypeAllocSize(GV->getValueType());
871  CurOffset = GVOffset + InitSize;
872 
873  // Compute the amount of padding that we'd like for the next element.
874  DesiredPadding = NextPowerOf2(InitSize - 1) - InitSize;
875 
876  // Experiments of different caps with Chromium on both x64 and ARM64
877  // have shown that the 32-byte cap generates the smallest binary on
878  // both platforms while different caps yield similar performance.
879  // (see https://lists.llvm.org/pipermail/llvm-dev/2018-July/124694.html)
880  if (DesiredPadding > 32)
881  DesiredPadding = alignTo(InitSize, 32) - InitSize;
882  }
883 
884  Constant *NewInit = ConstantStruct::getAnon(M.getContext(), GlobalInits);
885  auto *CombinedGlobal =
886  new GlobalVariable(M, NewInit->getType(), /*isConstant=*/true,
887  GlobalValue::PrivateLinkage, NewInit);
888  CombinedGlobal->setAlignment(MaxAlign);
889 
890  StructType *NewTy = cast<StructType>(NewInit->getType());
891  lowerTypeTestCalls(TypeIds, CombinedGlobal, GlobalLayout);
892 
893  // Build aliases pointing to offsets into the combined global for each
894  // global from which we built the combined global, and replace references
895  // to the original globals with references to the aliases.
896  for (unsigned I = 0; I != Globals.size(); ++I) {
897  GlobalVariable *GV = cast<GlobalVariable>(Globals[I]->getGlobal());
898 
899  // Multiply by 2 to account for padding elements.
900  Constant *CombinedGlobalIdxs[] = {ConstantInt::get(Int32Ty, 0),
901  ConstantInt::get(Int32Ty, I * 2)};
902  Constant *CombinedGlobalElemPtr = ConstantExpr::getGetElementPtr(
903  NewInit->getType(), CombinedGlobal, CombinedGlobalIdxs);
904  assert(GV->getType()->getAddressSpace() == 0);
905  GlobalAlias *GAlias =
906  GlobalAlias::create(NewTy->getElementType(I * 2), 0, GV->getLinkage(),
907  "", CombinedGlobalElemPtr, &M);
908  GAlias->setVisibility(GV->getVisibility());
909  GAlias->takeName(GV);
910  GV->replaceAllUsesWith(GAlias);
911  GV->eraseFromParent();
912  }
913 }
914 
915 bool LowerTypeTestsModule::shouldExportConstantsAsAbsoluteSymbols() {
916  return (Arch == Triple::x86 || Arch == Triple::x86_64) &&
917  ObjectFormat == Triple::ELF;
918 }
919 
920 /// Export the given type identifier so that ThinLTO backends may import it.
921 /// Type identifiers are exported by adding coarse-grained information about how
922 /// to test the type identifier to the summary, and creating symbols in the
923 /// object file (aliases and absolute symbols) containing fine-grained
924 /// information about the type identifier.
925 ///
926 /// Returns a pointer to the location in which to store the bitmask, if
927 /// applicable.
928 uint8_t *LowerTypeTestsModule::exportTypeId(StringRef TypeId,
929  const TypeIdLowering &TIL) {
930  TypeTestResolution &TTRes =
931  ExportSummary->getOrInsertTypeIdSummary(TypeId).TTRes;
932  TTRes.TheKind = TIL.TheKind;
933 
934  auto ExportGlobal = [&](StringRef Name, Constant *C) {
935  GlobalAlias *GA =
937  "__typeid_" + TypeId + "_" + Name, C, &M);
939  };
940 
941  auto ExportConstant = [&](StringRef Name, uint64_t &Storage, Constant *C) {
942  if (shouldExportConstantsAsAbsoluteSymbols())
943  ExportGlobal(Name, ConstantExpr::getIntToPtr(C, Int8PtrTy));
944  else
945  Storage = cast<ConstantInt>(C)->getZExtValue();
946  };
947 
948  if (TIL.TheKind != TypeTestResolution::Unsat)
949  ExportGlobal("global_addr", TIL.OffsetedGlobal);
950 
951  if (TIL.TheKind == TypeTestResolution::ByteArray ||
952  TIL.TheKind == TypeTestResolution::Inline ||
953  TIL.TheKind == TypeTestResolution::AllOnes) {
954  ExportConstant("align", TTRes.AlignLog2, TIL.AlignLog2);
955  ExportConstant("size_m1", TTRes.SizeM1, TIL.SizeM1);
956 
957  uint64_t BitSize = cast<ConstantInt>(TIL.SizeM1)->getZExtValue() + 1;
958  if (TIL.TheKind == TypeTestResolution::Inline)
959  TTRes.SizeM1BitWidth = (BitSize <= 32) ? 5 : 6;
960  else
961  TTRes.SizeM1BitWidth = (BitSize <= 128) ? 7 : 32;
962  }
963 
964  if (TIL.TheKind == TypeTestResolution::ByteArray) {
965  ExportGlobal("byte_array", TIL.TheByteArray);
966  if (shouldExportConstantsAsAbsoluteSymbols())
967  ExportGlobal("bit_mask", TIL.BitMask);
968  else
969  return &TTRes.BitMask;
970  }
971 
972  if (TIL.TheKind == TypeTestResolution::Inline)
973  ExportConstant("inline_bits", TTRes.InlineBits, TIL.InlineBits);
974 
975  return nullptr;
976 }
977 
978 LowerTypeTestsModule::TypeIdLowering
979 LowerTypeTestsModule::importTypeId(StringRef TypeId) {
980  const TypeIdSummary *TidSummary = ImportSummary->getTypeIdSummary(TypeId);
981  if (!TidSummary)
982  return {}; // Unsat: no globals match this type id.
983  const TypeTestResolution &TTRes = TidSummary->TTRes;
984 
985  TypeIdLowering TIL;
986  TIL.TheKind = TTRes.TheKind;
987 
988  auto ImportGlobal = [&](StringRef Name) {
989  // Give the global a type of length 0 so that it is not assumed not to alias
990  // with any other global.
991  Constant *C = M.getOrInsertGlobal(("__typeid_" + TypeId + "_" + Name).str(),
992  Int8Arr0Ty);
993  if (auto *GV = dyn_cast<GlobalVariable>(C))
995  C = ConstantExpr::getBitCast(C, Int8PtrTy);
996  return C;
997  };
998 
999  auto ImportConstant = [&](StringRef Name, uint64_t Const, unsigned AbsWidth,
1000  Type *Ty) {
1001  if (!shouldExportConstantsAsAbsoluteSymbols()) {
1002  Constant *C =
1003  ConstantInt::get(isa<IntegerType>(Ty) ? Ty : Int64Ty, Const);
1004  if (!isa<IntegerType>(Ty))
1005  C = ConstantExpr::getIntToPtr(C, Ty);
1006  return C;
1007  }
1008 
1009  Constant *C = ImportGlobal(Name);
1010  auto *GV = cast<GlobalVariable>(C->stripPointerCasts());
1011  if (isa<IntegerType>(Ty))
1012  C = ConstantExpr::getPtrToInt(C, Ty);
1013  if (GV->getMetadata(LLVMContext::MD_absolute_symbol))
1014  return C;
1015 
1016  auto SetAbsRange = [&](uint64_t Min, uint64_t Max) {
1017  auto *MinC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Min));
1018  auto *MaxC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Max));
1019  GV->setMetadata(LLVMContext::MD_absolute_symbol,
1020  MDNode::get(M.getContext(), {MinC, MaxC}));
1021  };
1022  if (AbsWidth == IntPtrTy->getBitWidth())
1023  SetAbsRange(~0ull, ~0ull); // Full set.
1024  else
1025  SetAbsRange(0, 1ull << AbsWidth);
1026  return C;
1027  };
1028 
1029  if (TIL.TheKind != TypeTestResolution::Unsat)
1030  TIL.OffsetedGlobal = ImportGlobal("global_addr");
1031 
1032  if (TIL.TheKind == TypeTestResolution::ByteArray ||
1033  TIL.TheKind == TypeTestResolution::Inline ||
1034  TIL.TheKind == TypeTestResolution::AllOnes) {
1035  TIL.AlignLog2 = ImportConstant("align", TTRes.AlignLog2, 8, Int8Ty);
1036  TIL.SizeM1 =
1037  ImportConstant("size_m1", TTRes.SizeM1, TTRes.SizeM1BitWidth, IntPtrTy);
1038  }
1039 
1040  if (TIL.TheKind == TypeTestResolution::ByteArray) {
1041  TIL.TheByteArray = ImportGlobal("byte_array");
1042  TIL.BitMask = ImportConstant("bit_mask", TTRes.BitMask, 8, Int8PtrTy);
1043  }
1044 
1045  if (TIL.TheKind == TypeTestResolution::Inline)
1046  TIL.InlineBits = ImportConstant(
1047  "inline_bits", TTRes.InlineBits, 1 << TTRes.SizeM1BitWidth,
1048  TTRes.SizeM1BitWidth <= 5 ? Int32Ty : Int64Ty);
1049 
1050  return TIL;
1051 }
1052 
1053 void LowerTypeTestsModule::importTypeTest(CallInst *CI) {
1054  auto TypeIdMDVal = dyn_cast<MetadataAsValue>(CI->getArgOperand(1));
1055  if (!TypeIdMDVal)
1056  report_fatal_error("Second argument of llvm.type.test must be metadata");
1057 
1058  auto TypeIdStr = dyn_cast<MDString>(TypeIdMDVal->getMetadata());
1059  // If this is a local unpromoted type, which doesn't have a metadata string,
1060  // treat as Unknown and delay lowering, so that we can still utilize it for
1061  // later optimizations.
1062  if (!TypeIdStr)
1063  return;
1064 
1065  TypeIdLowering TIL = importTypeId(TypeIdStr->getString());
1066  Value *Lowered = lowerTypeTestCall(TypeIdStr, CI, TIL);
1067  if (Lowered) {
1068  CI->replaceAllUsesWith(Lowered);
1069  CI->eraseFromParent();
1070  }
1071 }
1072 
1073 // ThinLTO backend: the function F has a jump table entry; update this module
1074 // accordingly. isJumpTableCanonical describes the type of the jump table entry.
1075 void LowerTypeTestsModule::importFunction(
1077  std::vector<GlobalAlias *> &AliasesToErase) {
1078  assert(F->getType()->getAddressSpace() == 0);
1079 
1080  GlobalValue::VisibilityTypes Visibility = F->getVisibility();
1081  std::string Name = std::string(F->getName());
1082 
1083  if (F->isDeclarationForLinker() && isJumpTableCanonical) {
1084  // Non-dso_local functions may be overriden at run time,
1085  // don't short curcuit them
1086  if (F->isDSOLocal()) {
1087  Function *RealF = Function::Create(F->getFunctionType(),
1089  F->getAddressSpace(),
1090  Name + ".cfi", &M);
1092  replaceDirectCalls(F, RealF);
1093  }
1094  return;
1095  }
1096 
1097  Function *FDecl;
1098  if (!isJumpTableCanonical) {
1099  // Either a declaration of an external function or a reference to a locally
1100  // defined jump table.
1101  FDecl = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage,
1102  F->getAddressSpace(), Name + ".cfi_jt", &M);
1104  } else {
1105  F->setName(Name + ".cfi");
1106  F->setLinkage(GlobalValue::ExternalLinkage);
1107  FDecl = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage,
1108  F->getAddressSpace(), Name, &M);
1109  FDecl->setVisibility(Visibility);
1110  Visibility = GlobalValue::HiddenVisibility;
1111 
1112  // Delete aliases pointing to this function, they'll be re-created in the
1113  // merged output. Don't do it yet though because ScopedSaveAliaseesAndUsed
1114  // will want to reset the aliasees first.
1115  for (auto &U : F->uses()) {
1116  if (auto *A = dyn_cast<GlobalAlias>(U.getUser())) {
1117  Function *AliasDecl = Function::Create(
1118  F->getFunctionType(), GlobalValue::ExternalLinkage,
1119  F->getAddressSpace(), "", &M);
1120  AliasDecl->takeName(A);
1121  A->replaceAllUsesWith(AliasDecl);
1122  AliasesToErase.push_back(A);
1123  }
1124  }
1125  }
1126 
1127  if (F->hasExternalWeakLinkage())
1128  replaceWeakDeclarationWithJumpTablePtr(F, FDecl, isJumpTableCanonical);
1129  else
1130  replaceCfiUses(F, FDecl, isJumpTableCanonical);
1131 
1132  // Set visibility late because it's used in replaceCfiUses() to determine
1133  // whether uses need to to be replaced.
1134  F->setVisibility(Visibility);
1135 }
1136 
1137 void LowerTypeTestsModule::lowerTypeTestCalls(
1138  ArrayRef<Metadata *> TypeIds, Constant *CombinedGlobalAddr,
1139  const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout) {
1140  CombinedGlobalAddr = ConstantExpr::getBitCast(CombinedGlobalAddr, Int8PtrTy);
1141 
1142  // For each type identifier in this disjoint set...
1143  for (Metadata *TypeId : TypeIds) {
1144  // Build the bitset.
1145  BitSetInfo BSI = buildBitSet(TypeId, GlobalLayout);
1146  LLVM_DEBUG({
1147  if (auto MDS = dyn_cast<MDString>(TypeId))
1148  dbgs() << MDS->getString() << ": ";
1149  else
1150  dbgs() << "<unnamed>: ";
1151  BSI.print(dbgs());
1152  });
1153 
1154  ByteArrayInfo *BAI = nullptr;
1155  TypeIdLowering TIL;
1156  TIL.OffsetedGlobal = ConstantExpr::getGetElementPtr(
1157  Int8Ty, CombinedGlobalAddr, ConstantInt::get(IntPtrTy, BSI.ByteOffset)),
1158  TIL.AlignLog2 = ConstantInt::get(Int8Ty, BSI.AlignLog2);
1159  TIL.SizeM1 = ConstantInt::get(IntPtrTy, BSI.BitSize - 1);
1160  if (BSI.isAllOnes()) {
1161  TIL.TheKind = (BSI.BitSize == 1) ? TypeTestResolution::Single
1163  } else if (BSI.BitSize <= 64) {
1164  TIL.TheKind = TypeTestResolution::Inline;
1165  uint64_t InlineBits = 0;
1166  for (auto Bit : BSI.Bits)
1167  InlineBits |= uint64_t(1) << Bit;
1168  if (InlineBits == 0)
1169  TIL.TheKind = TypeTestResolution::Unsat;
1170  else
1171  TIL.InlineBits = ConstantInt::get(
1172  (BSI.BitSize <= 32) ? Int32Ty : Int64Ty, InlineBits);
1173  } else {
1174  TIL.TheKind = TypeTestResolution::ByteArray;
1175  ++NumByteArraysCreated;
1176  BAI = createByteArray(BSI);
1177  TIL.TheByteArray = BAI->ByteArray;
1178  TIL.BitMask = BAI->MaskGlobal;
1179  }
1180 
1181  TypeIdUserInfo &TIUI = TypeIdUsers[TypeId];
1182 
1183  if (TIUI.IsExported) {
1184  uint8_t *MaskPtr = exportTypeId(cast<MDString>(TypeId)->getString(), TIL);
1185  if (BAI)
1186  BAI->MaskPtr = MaskPtr;
1187  }
1188 
1189  // Lower each call to llvm.type.test for this type identifier.
1190  for (CallInst *CI : TIUI.CallSites) {
1191  ++NumTypeTestCallsLowered;
1192  Value *Lowered = lowerTypeTestCall(TypeId, CI, TIL);
1193  if (Lowered) {
1194  CI->replaceAllUsesWith(Lowered);
1195  CI->eraseFromParent();
1196  }
1197  }
1198  }
1199 }
1200 
1201 void LowerTypeTestsModule::verifyTypeMDNode(GlobalObject *GO, MDNode *Type) {
1202  if (Type->getNumOperands() != 2)
1203  report_fatal_error("All operands of type metadata must have 2 elements");
1204 
1205  if (GO->isThreadLocal())
1206  report_fatal_error("Bit set element may not be thread-local");
1207  if (isa<GlobalVariable>(GO) && GO->hasSection())
1209  "A member of a type identifier may not have an explicit section");
1210 
1211  // FIXME: We previously checked that global var member of a type identifier
1212  // must be a definition, but the IR linker may leave type metadata on
1213  // declarations. We should restore this check after fixing PR31759.
1214 
1215  auto OffsetConstMD = dyn_cast<ConstantAsMetadata>(Type->getOperand(0));
1216  if (!OffsetConstMD)
1217  report_fatal_error("Type offset must be a constant");
1218  auto OffsetInt = dyn_cast<ConstantInt>(OffsetConstMD->getValue());
1219  if (!OffsetInt)
1220  report_fatal_error("Type offset must be an integer constant");
1221 }
1222 
1223 static const unsigned kX86JumpTableEntrySize = 8;
1224 static const unsigned kARMJumpTableEntrySize = 4;
1225 static const unsigned kARMBTIJumpTableEntrySize = 8;
1226 static const unsigned kRISCVJumpTableEntrySize = 8;
1227 
1228 unsigned LowerTypeTestsModule::getJumpTableEntrySize() {
1229  switch (Arch) {
1230  case Triple::x86:
1231  case Triple::x86_64:
1232  return kX86JumpTableEntrySize;
1233  case Triple::arm:
1234  case Triple::thumb:
1235  return kARMJumpTableEntrySize;
1236  case Triple::aarch64:
1237  if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
1238  M.getModuleFlag("branch-target-enforcement")))
1239  if (BTE->getZExtValue())
1241  return kARMJumpTableEntrySize;
1242  case Triple::riscv32:
1243  case Triple::riscv64:
1244  return kRISCVJumpTableEntrySize;
1245  default:
1246  report_fatal_error("Unsupported architecture for jump tables");
1247  }
1248 }
1249 
1250 // Create a jump table entry for the target. This consists of an instruction
1251 // sequence containing a relative branch to Dest. Appends inline asm text,
1252 // constraints and arguments to AsmOS, ConstraintOS and AsmArgs.
1253 void LowerTypeTestsModule::createJumpTableEntry(
1254  raw_ostream &AsmOS, raw_ostream &ConstraintOS,
1255  Triple::ArchType JumpTableArch, SmallVectorImpl<Value *> &AsmArgs,
1256  Function *Dest) {
1257  unsigned ArgIndex = AsmArgs.size();
1258 
1259  if (JumpTableArch == Triple::x86 || JumpTableArch == Triple::x86_64) {
1260  AsmOS << "jmp ${" << ArgIndex << ":c}@plt\n";
1261  AsmOS << "int3\nint3\nint3\n";
1262  } else if (JumpTableArch == Triple::arm) {
1263  AsmOS << "b $" << ArgIndex << "\n";
1264  } else if (JumpTableArch == Triple::aarch64) {
1265  if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
1266  Dest->getParent()->getModuleFlag("branch-target-enforcement")))
1267  if (BTE->getZExtValue())
1268  AsmOS << "bti c\n";
1269  AsmOS << "b $" << ArgIndex << "\n";
1270  } else if (JumpTableArch == Triple::thumb) {
1271  AsmOS << "b.w $" << ArgIndex << "\n";
1272  } else if (JumpTableArch == Triple::riscv32 ||
1273  JumpTableArch == Triple::riscv64) {
1274  AsmOS << "tail $" << ArgIndex << "@plt\n";
1275  } else {
1276  report_fatal_error("Unsupported architecture for jump tables");
1277  }
1278 
1279  ConstraintOS << (ArgIndex > 0 ? ",s" : "s");
1280  AsmArgs.push_back(Dest);
1281 }
1282 
1283 Type *LowerTypeTestsModule::getJumpTableEntryType() {
1284  return ArrayType::get(Int8Ty, getJumpTableEntrySize());
1285 }
1286 
1287 /// Given a disjoint set of type identifiers and functions, build the bit sets
1288 /// and lower the llvm.type.test calls, architecture dependently.
1289 void LowerTypeTestsModule::buildBitSetsFromFunctions(
1291  if (Arch == Triple::x86 || Arch == Triple::x86_64 || Arch == Triple::arm ||
1292  Arch == Triple::thumb || Arch == Triple::aarch64 ||
1293  Arch == Triple::riscv32 || Arch == Triple::riscv64)
1294  buildBitSetsFromFunctionsNative(TypeIds, Functions);
1295  else if (Arch == Triple::wasm32 || Arch == Triple::wasm64)
1296  buildBitSetsFromFunctionsWASM(TypeIds, Functions);
1297  else
1298  report_fatal_error("Unsupported architecture for jump tables");
1299 }
1300 
1301 void LowerTypeTestsModule::moveInitializerToModuleConstructor(
1302  GlobalVariable *GV) {
1303  if (WeakInitializerFn == nullptr) {
1304  WeakInitializerFn = Function::Create(
1305  FunctionType::get(Type::getVoidTy(M.getContext()),
1306  /* IsVarArg */ false),
1308  M.getDataLayout().getProgramAddressSpace(),
1309  "__cfi_global_var_init", &M);
1310  BasicBlock *BB =
1311  BasicBlock::Create(M.getContext(), "entry", WeakInitializerFn);
1312  ReturnInst::Create(M.getContext(), BB);
1313  WeakInitializerFn->setSection(
1314  ObjectFormat == Triple::MachO
1315  ? "__TEXT,__StaticInit,regular,pure_instructions"
1316  : ".text.startup");
1317  // This code is equivalent to relocation application, and should run at the
1318  // earliest possible time (i.e. with the highest priority).
1319  appendToGlobalCtors(M, WeakInitializerFn, /* Priority */ 0);
1320  }
1321 
1322  IRBuilder<> IRB(WeakInitializerFn->getEntryBlock().getTerminator());
1323  GV->setConstant(false);
1324  IRB.CreateAlignedStore(GV->getInitializer(), GV, GV->getAlign());
1326 }
1327 
1328 void LowerTypeTestsModule::findGlobalVariableUsersOf(
1330  for (auto *U : C->users()){
1331  if (auto *GV = dyn_cast<GlobalVariable>(U))
1332  Out.insert(GV);
1333  else if (auto *C2 = dyn_cast<Constant>(U))
1334  findGlobalVariableUsersOf(C2, Out);
1335  }
1336 }
1337 
1338 // Replace all uses of F with (F ? JT : 0).
1339 void LowerTypeTestsModule::replaceWeakDeclarationWithJumpTablePtr(
1340  Function *F, Constant *JT, bool IsJumpTableCanonical) {
1341  // The target expression can not appear in a constant initializer on most
1342  // (all?) targets. Switch to a runtime initializer.
1343  SmallSetVector<GlobalVariable *, 8> GlobalVarUsers;
1344  findGlobalVariableUsersOf(F, GlobalVarUsers);
1345  for (auto GV : GlobalVarUsers)
1346  moveInitializerToModuleConstructor(GV);
1347 
1348  // Can not RAUW F with an expression that uses F. Replace with a temporary
1349  // placeholder first.
1350  Function *PlaceholderFn =
1351  Function::Create(cast<FunctionType>(F->getValueType()),
1353  F->getAddressSpace(), "", &M);
1354  replaceCfiUses(F, PlaceholderFn, IsJumpTableCanonical);
1355 
1358  Constant::getNullValue(F->getType())),
1359  JT, Constant::getNullValue(F->getType()));
1360  PlaceholderFn->replaceAllUsesWith(Target);
1361  PlaceholderFn->eraseFromParent();
1362 }
1363 
1364 static bool isThumbFunction(Function *F, Triple::ArchType ModuleArch) {
1365  Attribute TFAttr = F->getFnAttribute("target-features");
1366  if (TFAttr.isValid()) {
1367  SmallVector<StringRef, 6> Features;
1368  TFAttr.getValueAsString().split(Features, ',');
1369  for (StringRef Feature : Features) {
1370  if (Feature == "-thumb-mode")
1371  return false;
1372  else if (Feature == "+thumb-mode")
1373  return true;
1374  }
1375  }
1376 
1377  return ModuleArch == Triple::thumb;
1378 }
1379 
1380 // Each jump table must be either ARM or Thumb as a whole for the bit-test math
1381 // to work. Pick one that matches the majority of members to minimize interop
1382 // veneers inserted by the linker.
1383 static Triple::ArchType
1385  Triple::ArchType ModuleArch) {
1386  if (ModuleArch != Triple::arm && ModuleArch != Triple::thumb)
1387  return ModuleArch;
1388 
1389  unsigned ArmCount = 0, ThumbCount = 0;
1390  for (const auto GTM : Functions) {
1391  if (!GTM->isJumpTableCanonical()) {
1392  // PLT stubs are always ARM.
1393  // FIXME: This is the wrong heuristic for non-canonical jump tables.
1394  ++ArmCount;
1395  continue;
1396  }
1397 
1398  Function *F = cast<Function>(GTM->getGlobal());
1399  ++(isThumbFunction(F, ModuleArch) ? ThumbCount : ArmCount);
1400  }
1401 
1402  return ArmCount > ThumbCount ? Triple::arm : Triple::thumb;
1403 }
1404 
1405 void LowerTypeTestsModule::createJumpTable(
1406  Function *F, ArrayRef<GlobalTypeMember *> Functions) {
1407  std::string AsmStr, ConstraintStr;
1408  raw_string_ostream AsmOS(AsmStr), ConstraintOS(ConstraintStr);
1409  SmallVector<Value *, 16> AsmArgs;
1410  AsmArgs.reserve(Functions.size() * 2);
1411 
1412  Triple::ArchType JumpTableArch = selectJumpTableArmEncoding(Functions, Arch);
1413 
1414  for (unsigned I = 0; I != Functions.size(); ++I)
1415  createJumpTableEntry(AsmOS, ConstraintOS, JumpTableArch, AsmArgs,
1416  cast<Function>(Functions[I]->getGlobal()));
1417 
1418  // Align the whole table by entry size.
1419  F->setAlignment(Align(getJumpTableEntrySize()));
1420  // Skip prologue.
1421  // Disabled on win32 due to https://llvm.org/bugs/show_bug.cgi?id=28641#c3.
1422  // Luckily, this function does not get any prologue even without the
1423  // attribute.
1424  if (OS != Triple::Win32)
1425  F->addFnAttr(Attribute::Naked);
1426  if (JumpTableArch == Triple::arm)
1427  F->addFnAttr("target-features", "-thumb-mode");
1428  if (JumpTableArch == Triple::thumb) {
1429  F->addFnAttr("target-features", "+thumb-mode");
1430  // Thumb jump table assembly needs Thumb2. The following attribute is added
1431  // by Clang for -march=armv7.
1432  F->addFnAttr("target-cpu", "cortex-a8");
1433  }
1434  if (JumpTableArch == Triple::aarch64) {
1435  F->addFnAttr("branch-target-enforcement", "false");
1436  F->addFnAttr("sign-return-address", "none");
1437  }
1438  if (JumpTableArch == Triple::riscv32 || JumpTableArch == Triple::riscv64) {
1439  // Make sure the jump table assembly is not modified by the assembler or
1440  // the linker.
1441  F->addFnAttr("target-features", "-c,-relax");
1442  }
1443  // Make sure we don't emit .eh_frame for this function.
1444  F->addFnAttr(Attribute::NoUnwind);
1445 
1446  BasicBlock *BB = BasicBlock::Create(M.getContext(), "entry", F);
1447  IRBuilder<> IRB(BB);
1448 
1449  SmallVector<Type *, 16> ArgTypes;
1450  ArgTypes.reserve(AsmArgs.size());
1451  for (const auto &Arg : AsmArgs)
1452  ArgTypes.push_back(Arg->getType());
1453  InlineAsm *JumpTableAsm =
1454  InlineAsm::get(FunctionType::get(IRB.getVoidTy(), ArgTypes, false),
1455  AsmOS.str(), ConstraintOS.str(),
1456  /*hasSideEffects=*/true);
1457 
1458  IRB.CreateCall(JumpTableAsm, AsmArgs);
1459  IRB.CreateUnreachable();
1460 }
1461 
1462 /// Given a disjoint set of type identifiers and functions, build a jump table
1463 /// for the functions, build the bit sets and lower the llvm.type.test calls.
1464 void LowerTypeTestsModule::buildBitSetsFromFunctionsNative(
1466  // Unlike the global bitset builder, the function bitset builder cannot
1467  // re-arrange functions in a particular order and base its calculations on the
1468  // layout of the functions' entry points, as we have no idea how large a
1469  // particular function will end up being (the size could even depend on what
1470  // this pass does!) Instead, we build a jump table, which is a block of code
1471  // consisting of one branch instruction for each of the functions in the bit
1472  // set that branches to the target function, and redirect any taken function
1473  // addresses to the corresponding jump table entry. In the object file's
1474  // symbol table, the symbols for the target functions also refer to the jump
1475  // table entries, so that addresses taken outside the module will pass any
1476  // verification done inside the module.
1477  //
1478  // In more concrete terms, suppose we have three functions f, g, h which are
1479  // of the same type, and a function foo that returns their addresses:
1480  //
1481  // f:
1482  // mov 0, %eax
1483  // ret
1484  //
1485  // g:
1486  // mov 1, %eax
1487  // ret
1488  //
1489  // h:
1490  // mov 2, %eax
1491  // ret
1492  //
1493  // foo:
1494  // mov f, %eax
1495  // mov g, %edx
1496  // mov h, %ecx
1497  // ret
1498  //
1499  // We output the jump table as module-level inline asm string. The end result
1500  // will (conceptually) look like this:
1501  //
1502  // f = .cfi.jumptable
1503  // g = .cfi.jumptable + 4
1504  // h = .cfi.jumptable + 8
1505  // .cfi.jumptable:
1506  // jmp f.cfi ; 5 bytes
1507  // int3 ; 1 byte
1508  // int3 ; 1 byte
1509  // int3 ; 1 byte
1510  // jmp g.cfi ; 5 bytes
1511  // int3 ; 1 byte
1512  // int3 ; 1 byte
1513  // int3 ; 1 byte
1514  // jmp h.cfi ; 5 bytes
1515  // int3 ; 1 byte
1516  // int3 ; 1 byte
1517  // int3 ; 1 byte
1518  //
1519  // f.cfi:
1520  // mov 0, %eax
1521  // ret
1522  //
1523  // g.cfi:
1524  // mov 1, %eax
1525  // ret
1526  //
1527  // h.cfi:
1528  // mov 2, %eax
1529  // ret
1530  //
1531  // foo:
1532  // mov f, %eax
1533  // mov g, %edx
1534  // mov h, %ecx
1535  // ret
1536  //
1537  // Because the addresses of f, g, h are evenly spaced at a power of 2, in the
1538  // normal case the check can be carried out using the same kind of simple
1539  // arithmetic that we normally use for globals.
1540 
1541  // FIXME: find a better way to represent the jumptable in the IR.
1542  assert(!Functions.empty());
1543 
1544  // Build a simple layout based on the regular layout of jump tables.
1546  unsigned EntrySize = getJumpTableEntrySize();
1547  for (unsigned I = 0; I != Functions.size(); ++I)
1548  GlobalLayout[Functions[I]] = I * EntrySize;
1549 
1550  Function *JumpTableFn =
1552  /* IsVarArg */ false),
1554  M.getDataLayout().getProgramAddressSpace(),
1555  ".cfi.jumptable", &M);
1557  ArrayType::get(getJumpTableEntryType(), Functions.size());
1558  auto JumpTable =
1559  ConstantExpr::getPointerCast(JumpTableFn, JumpTableType->getPointerTo(0));
1560 
1561  lowerTypeTestCalls(TypeIds, JumpTable, GlobalLayout);
1562 
1563  {
1564  ScopedSaveAliaseesAndUsed S(M);
1565 
1566  // Build aliases pointing to offsets into the jump table, and replace
1567  // references to the original functions with references to the aliases.
1568  for (unsigned I = 0; I != Functions.size(); ++I) {
1569  Function *F = cast<Function>(Functions[I]->getGlobal());
1570  bool IsJumpTableCanonical = Functions[I]->isJumpTableCanonical();
1571 
1572  Constant *CombinedGlobalElemPtr = ConstantExpr::getBitCast(
1576  ConstantInt::get(IntPtrTy, I)}),
1577  F->getType());
1578 
1579  const bool IsExported = Functions[I]->isExported();
1580  if (!IsJumpTableCanonical) {
1581  GlobalValue::LinkageTypes LT = IsExported
1584  GlobalAlias *JtAlias = GlobalAlias::create(F->getValueType(), 0, LT,
1585  F->getName() + ".cfi_jt",
1586  CombinedGlobalElemPtr, &M);
1587  if (IsExported)
1589  else
1590  appendToUsed(M, {JtAlias});
1591  }
1592 
1593  if (IsExported) {
1594  if (IsJumpTableCanonical)
1595  ExportSummary->cfiFunctionDefs().insert(std::string(F->getName()));
1596  else
1597  ExportSummary->cfiFunctionDecls().insert(std::string(F->getName()));
1598  }
1599 
1600  if (!IsJumpTableCanonical) {
1601  if (F->hasExternalWeakLinkage())
1602  replaceWeakDeclarationWithJumpTablePtr(F, CombinedGlobalElemPtr,
1603  IsJumpTableCanonical);
1604  else
1605  replaceCfiUses(F, CombinedGlobalElemPtr, IsJumpTableCanonical);
1606  } else {
1607  assert(F->getType()->getAddressSpace() == 0);
1608 
1609  GlobalAlias *FAlias =
1610  GlobalAlias::create(F->getValueType(), 0, F->getLinkage(), "",
1611  CombinedGlobalElemPtr, &M);
1612  FAlias->setVisibility(F->getVisibility());
1613  FAlias->takeName(F);
1614  if (FAlias->hasName())
1615  F->setName(FAlias->getName() + ".cfi");
1616  replaceCfiUses(F, FAlias, IsJumpTableCanonical);
1617  if (!F->hasLocalLinkage())
1618  F->setVisibility(GlobalVariable::HiddenVisibility);
1619  }
1620  }
1621  }
1622 
1623  createJumpTable(JumpTableFn, Functions);
1624 }
1625 
1626 /// Assign a dummy layout using an incrementing counter, tag each function
1627 /// with its index represented as metadata, and lower each type test to an
1628 /// integer range comparison. During generation of the indirect function call
1629 /// table in the backend, it will assign the given indexes.
1630 /// Note: Dynamic linking is not supported, as the WebAssembly ABI has not yet
1631 /// been finalized.
1632 void LowerTypeTestsModule::buildBitSetsFromFunctionsWASM(
1634  assert(!Functions.empty());
1635 
1636  // Build consecutive monotonic integer ranges for each call target set
1638 
1639  for (GlobalTypeMember *GTM : Functions) {
1640  Function *F = cast<Function>(GTM->getGlobal());
1641 
1642  // Skip functions that are not address taken, to avoid bloating the table
1643  if (!F->hasAddressTaken())
1644  continue;
1645 
1646  // Store metadata with the index for each function
1647  MDNode *MD = MDNode::get(F->getContext(),
1649  ConstantInt::get(Int64Ty, IndirectIndex))));
1650  F->setMetadata("wasm.index", MD);
1651 
1652  // Assign the counter value
1653  GlobalLayout[GTM] = IndirectIndex++;
1654  }
1655 
1656  // The indirect function table index space starts at zero, so pass a NULL
1657  // pointer as the subtracted "jump table" offset.
1658  lowerTypeTestCalls(TypeIds, ConstantPointerNull::get(Int32PtrTy),
1659  GlobalLayout);
1660 }
1661 
1662 void LowerTypeTestsModule::buildBitSetsFromDisjointSet(
1664  ArrayRef<ICallBranchFunnel *> ICallBranchFunnels) {
1665  DenseMap<Metadata *, uint64_t> TypeIdIndices;
1666  for (unsigned I = 0; I != TypeIds.size(); ++I)
1667  TypeIdIndices[TypeIds[I]] = I;
1668 
1669  // For each type identifier, build a set of indices that refer to members of
1670  // the type identifier.
1671  std::vector<std::set<uint64_t>> TypeMembers(TypeIds.size());
1672  unsigned GlobalIndex = 0;
1674  for (GlobalTypeMember *GTM : Globals) {
1675  for (MDNode *Type : GTM->types()) {
1676  // Type = { offset, type identifier }
1677  auto I = TypeIdIndices.find(Type->getOperand(1));
1678  if (I != TypeIdIndices.end())
1679  TypeMembers[I->second].insert(GlobalIndex);
1680  }
1681  GlobalIndices[GTM] = GlobalIndex;
1682  GlobalIndex++;
1683  }
1684 
1685  for (ICallBranchFunnel *JT : ICallBranchFunnels) {
1686  TypeMembers.emplace_back();
1687  std::set<uint64_t> &TMSet = TypeMembers.back();
1688  for (GlobalTypeMember *T : JT->targets())
1689  TMSet.insert(GlobalIndices[T]);
1690  }
1691 
1692  // Order the sets of indices by size. The GlobalLayoutBuilder works best
1693  // when given small index sets first.
1694  llvm::stable_sort(TypeMembers, [](const std::set<uint64_t> &O1,
1695  const std::set<uint64_t> &O2) {
1696  return O1.size() < O2.size();
1697  });
1698 
1699  // Create a GlobalLayoutBuilder and provide it with index sets as layout
1700  // fragments. The GlobalLayoutBuilder tries to lay out members of fragments as
1701  // close together as possible.
1702  GlobalLayoutBuilder GLB(Globals.size());
1703  for (auto &&MemSet : TypeMembers)
1704  GLB.addFragment(MemSet);
1705 
1706  // Build a vector of globals with the computed layout.
1707  bool IsGlobalSet =
1708  Globals.empty() || isa<GlobalVariable>(Globals[0]->getGlobal());
1709  std::vector<GlobalTypeMember *> OrderedGTMs(Globals.size());
1710  auto OGTMI = OrderedGTMs.begin();
1711  for (auto &&F : GLB.Fragments) {
1712  for (auto &&Offset : F) {
1713  if (IsGlobalSet != isa<GlobalVariable>(Globals[Offset]->getGlobal()))
1714  report_fatal_error("Type identifier may not contain both global "
1715  "variables and functions");
1716  *OGTMI++ = Globals[Offset];
1717  }
1718  }
1719 
1720  // Build the bitsets from this disjoint set.
1721  if (IsGlobalSet)
1722  buildBitSetsFromGlobalVariables(TypeIds, OrderedGTMs);
1723  else
1724  buildBitSetsFromFunctions(TypeIds, OrderedGTMs);
1725 }
1726 
1727 /// Lower all type tests in this module.
1728 LowerTypeTestsModule::LowerTypeTestsModule(
1729  Module &M, ModuleSummaryIndex *ExportSummary,
1730  const ModuleSummaryIndex *ImportSummary, bool DropTypeTests)
1731  : M(M), ExportSummary(ExportSummary), ImportSummary(ImportSummary),
1732  DropTypeTests(DropTypeTests || ClDropTypeTests) {
1733  assert(!(ExportSummary && ImportSummary));
1734  Triple TargetTriple(M.getTargetTriple());
1735  Arch = TargetTriple.getArch();
1736  OS = TargetTriple.getOS();
1737  ObjectFormat = TargetTriple.getObjectFormat();
1738 }
1739 
1740 bool LowerTypeTestsModule::runForTesting(Module &M) {
1741  ModuleSummaryIndex Summary(/*HaveGVs=*/false);
1742 
1743  // Handle the command-line summary arguments. This code is for testing
1744  // purposes only, so we handle errors directly.
1745  if (!ClReadSummary.empty()) {
1746  ExitOnError ExitOnErr("-lowertypetests-read-summary: " + ClReadSummary +
1747  ": ");
1748  auto ReadSummaryFile =
1749  ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ClReadSummary)));
1750 
1751  yaml::Input In(ReadSummaryFile->getBuffer());
1752  In >> Summary;
1753  ExitOnErr(errorCodeToError(In.error()));
1754  }
1755 
1756  bool Changed =
1757  LowerTypeTestsModule(
1758  M, ClSummaryAction == PassSummaryAction::Export ? &Summary : nullptr,
1759  ClSummaryAction == PassSummaryAction::Import ? &Summary : nullptr,
1760  /*DropTypeTests*/ false)
1761  .lower();
1762 
1763  if (!ClWriteSummary.empty()) {
1764  ExitOnError ExitOnErr("-lowertypetests-write-summary: " + ClWriteSummary +
1765  ": ");
1766  std::error_code EC;
1768  ExitOnErr(errorCodeToError(EC));
1769 
1770  yaml::Output Out(OS);
1771  Out << Summary;
1772  }
1773 
1774  return Changed;
1775 }
1776 
1777 static bool isDirectCall(Use& U) {
1778  auto *Usr = dyn_cast<CallInst>(U.getUser());
1779  if (Usr) {
1780  auto *CB = dyn_cast<CallBase>(Usr);
1781  if (CB && CB->isCallee(&U))
1782  return true;
1783  }
1784  return false;
1785 }
1786 
1787 void LowerTypeTestsModule::replaceCfiUses(Function *Old, Value *New,
1788  bool IsJumpTableCanonical) {
1790  for (Use &U : llvm::make_early_inc_range(Old->uses())) {
1791  // Skip block addresses and no_cfi values, which refer to the function
1792  // body instead of the jump table.
1793  if (isa<BlockAddress, NoCFIValue>(U.getUser()))
1794  continue;
1795 
1796  // Skip direct calls to externally defined or non-dso_local functions
1797  if (isDirectCall(U) && (Old->isDSOLocal() || !IsJumpTableCanonical))
1798  continue;
1799 
1800  // Must handle Constants specially, we cannot call replaceUsesOfWith on a
1801  // constant because they are uniqued.
1802  if (auto *C = dyn_cast<Constant>(U.getUser())) {
1803  if (!isa<GlobalValue>(C)) {
1804  // Save unique users to avoid processing operand replacement
1805  // more than once.
1806  Constants.insert(C);
1807  continue;
1808  }
1809  }
1810 
1811  U.set(New);
1812  }
1813 
1814  // Process operand replacement of saved constants.
1815  for (auto *C : Constants)
1816  C->handleOperandChange(Old, New);
1817 }
1818 
1819 void LowerTypeTestsModule::replaceDirectCalls(Value *Old, Value *New) {
1820  Old->replaceUsesWithIf(New, isDirectCall);
1821 }
1822 
1823 bool LowerTypeTestsModule::lower() {
1824  Function *TypeTestFunc =
1825  M.getFunction(Intrinsic::getName(Intrinsic::type_test));
1826 
1827  if (DropTypeTests && TypeTestFunc) {
1828  for (Use &U : llvm::make_early_inc_range(TypeTestFunc->uses())) {
1829  auto *CI = cast<CallInst>(U.getUser());
1830  // Find and erase llvm.assume intrinsics for this llvm.type.test call.
1831  for (Use &CIU : llvm::make_early_inc_range(CI->uses()))
1832  if (auto *Assume = dyn_cast<AssumeInst>(CIU.getUser()))
1833  Assume->eraseFromParent();
1834  // If the assume was merged with another assume, we might have a use on a
1835  // phi (which will feed the assume). Simply replace the use on the phi
1836  // with "true" and leave the merged assume.
1837  if (!CI->use_empty()) {
1838  assert(all_of(CI->users(),
1839  [](User *U) -> bool { return isa<PHINode>(U); }));
1840  CI->replaceAllUsesWith(ConstantInt::getTrue(M.getContext()));
1841  }
1842  CI->eraseFromParent();
1843  }
1844 
1845  // We have deleted the type intrinsics, so we no longer have enough
1846  // information to reason about the liveness of virtual function pointers
1847  // in GlobalDCE.
1848  for (GlobalVariable &GV : M.globals())
1849  GV.eraseMetadata(LLVMContext::MD_vcall_visibility);
1850 
1851  return true;
1852  }
1853 
1854  // If only some of the modules were split, we cannot correctly perform
1855  // this transformation. We already checked for the presense of type tests
1856  // with partially split modules during the thin link, and would have emitted
1857  // an error if any were found, so here we can simply return.
1858  if ((ExportSummary && ExportSummary->partiallySplitLTOUnits()) ||
1859  (ImportSummary && ImportSummary->partiallySplitLTOUnits()))
1860  return false;
1861 
1862  Function *ICallBranchFunnelFunc =
1863  M.getFunction(Intrinsic::getName(Intrinsic::icall_branch_funnel));
1864  if ((!TypeTestFunc || TypeTestFunc->use_empty()) &&
1865  (!ICallBranchFunnelFunc || ICallBranchFunnelFunc->use_empty()) &&
1866  !ExportSummary && !ImportSummary)
1867  return false;
1868 
1869  if (ImportSummary) {
1870  if (TypeTestFunc)
1871  for (Use &U : llvm::make_early_inc_range(TypeTestFunc->uses()))
1872  importTypeTest(cast<CallInst>(U.getUser()));
1873 
1874  if (ICallBranchFunnelFunc && !ICallBranchFunnelFunc->use_empty())
1876  "unexpected call to llvm.icall.branch.funnel during import phase");
1877 
1880  for (auto &F : M) {
1881  // CFI functions are either external, or promoted. A local function may
1882  // have the same name, but it's not the one we are looking for.
1883  if (F.hasLocalLinkage())
1884  continue;
1885  if (ImportSummary->cfiFunctionDefs().count(std::string(F.getName())))
1886  Defs.push_back(&F);
1887  else if (ImportSummary->cfiFunctionDecls().count(
1888  std::string(F.getName())))
1889  Decls.push_back(&F);
1890  }
1891 
1892  std::vector<GlobalAlias *> AliasesToErase;
1893  {
1894  ScopedSaveAliaseesAndUsed S(M);
1895  for (auto F : Defs)
1896  importFunction(F, /*isJumpTableCanonical*/ true, AliasesToErase);
1897  for (auto F : Decls)
1898  importFunction(F, /*isJumpTableCanonical*/ false, AliasesToErase);
1899  }
1900  for (GlobalAlias *GA : AliasesToErase)
1901  GA->eraseFromParent();
1902 
1903  return true;
1904  }
1905 
1906  // Equivalence class set containing type identifiers and the globals that
1907  // reference them. This is used to partition the set of type identifiers in
1908  // the module into disjoint sets.
1909  using GlobalClassesTy = EquivalenceClasses<
1911  GlobalClassesTy GlobalClasses;
1912 
1913  // Verify the type metadata and build a few data structures to let us
1914  // efficiently enumerate the type identifiers associated with a global:
1915  // a list of GlobalTypeMembers (a GlobalObject stored alongside a vector
1916  // of associated type metadata) and a mapping from type identifiers to their
1917  // list of GlobalTypeMembers and last observed index in the list of globals.
1918  // The indices will be used later to deterministically order the list of type
1919  // identifiers.
1920  BumpPtrAllocator Alloc;
1921  struct TIInfo {
1922  unsigned UniqueId;
1923  std::vector<GlobalTypeMember *> RefGlobals;
1924  };
1925  DenseMap<Metadata *, TIInfo> TypeIdInfo;
1926  unsigned CurUniqueId = 0;
1928 
1929  // Cross-DSO CFI emits jumptable entries for exported functions as well as
1930  // address taken functions in case they are address taken in other modules.
1931  const bool CrossDsoCfi = M.getModuleFlag("Cross-DSO CFI") != nullptr;
1932 
1933  struct ExportedFunctionInfo {
1935  MDNode *FuncMD; // {name, linkage, type[, type...]}
1936  };
1938  if (ExportSummary) {
1939  // A set of all functions that are address taken by a live global object.
1940  DenseSet<GlobalValue::GUID> AddressTaken;
1941  for (auto &I : *ExportSummary)
1942  for (auto &GVS : I.second.SummaryList)
1943  if (GVS->isLive())
1944  for (auto &Ref : GVS->refs())
1945  AddressTaken.insert(Ref.getGUID());
1946 
1947  NamedMDNode *CfiFunctionsMD = M.getNamedMetadata("cfi.functions");
1948  if (CfiFunctionsMD) {
1949  for (auto FuncMD : CfiFunctionsMD->operands()) {
1950  assert(FuncMD->getNumOperands() >= 2);
1951  StringRef FunctionName =
1952  cast<MDString>(FuncMD->getOperand(0))->getString();
1954  cast<ConstantAsMetadata>(FuncMD->getOperand(1))
1955  ->getValue()
1956  ->getUniqueInteger()
1957  .getZExtValue());
1958  const GlobalValue::GUID GUID = GlobalValue::getGUID(
1959  GlobalValue::dropLLVMManglingEscape(FunctionName));
1960  // Do not emit jumptable entries for functions that are not-live and
1961  // have no live references (and are not exported with cross-DSO CFI.)
1962  if (!ExportSummary->isGUIDLive(GUID))
1963  continue;
1964  if (!AddressTaken.count(GUID)) {
1965  if (!CrossDsoCfi || Linkage != CFL_Definition)
1966  continue;
1967 
1968  bool Exported = false;
1969  if (auto VI = ExportSummary->getValueInfo(GUID))
1970  for (auto &GVS : VI.getSummaryList())
1971  if (GVS->isLive() && !GlobalValue::isLocalLinkage(GVS->linkage()))
1972  Exported = true;
1973 
1974  if (!Exported)
1975  continue;
1976  }
1977  auto P = ExportedFunctions.insert({FunctionName, {Linkage, FuncMD}});
1978  if (!P.second && P.first->second.Linkage != CFL_Definition)
1979  P.first->second = {Linkage, FuncMD};
1980  }
1981 
1982  for (const auto &P : ExportedFunctions) {
1983  StringRef FunctionName = P.first;
1984  CfiFunctionLinkage Linkage = P.second.Linkage;
1985  MDNode *FuncMD = P.second.FuncMD;
1986  Function *F = M.getFunction(FunctionName);
1987  if (F && F->hasLocalLinkage()) {
1988  // Locally defined function that happens to have the same name as a
1989  // function defined in a ThinLTO module. Rename it to move it out of
1990  // the way of the external reference that we're about to create.
1991  // Note that setName will find a unique name for the function, so even
1992  // if there is an existing function with the suffix there won't be a
1993  // name collision.
1994  F->setName(F->getName() + ".1");
1995  F = nullptr;
1996  }
1997 
1998  if (!F)
1999  F = Function::Create(
2000  FunctionType::get(Type::getVoidTy(M.getContext()), false),
2001  GlobalVariable::ExternalLinkage,
2002  M.getDataLayout().getProgramAddressSpace(), FunctionName, &M);
2003 
2004  // If the function is available_externally, remove its definition so
2005  // that it is handled the same way as a declaration. Later we will try
2006  // to create an alias using this function's linkage, which will fail if
2007  // the linkage is available_externally. This will also result in us
2008  // following the code path below to replace the type metadata.
2009  if (F->hasAvailableExternallyLinkage()) {
2010  F->setLinkage(GlobalValue::ExternalLinkage);
2011  F->deleteBody();
2012  F->setComdat(nullptr);
2013  F->clearMetadata();
2014  }
2015 
2016  // Update the linkage for extern_weak declarations when a definition
2017  // exists.
2018  if (Linkage == CFL_Definition && F->hasExternalWeakLinkage())
2019  F->setLinkage(GlobalValue::ExternalLinkage);
2020 
2021  // If the function in the full LTO module is a declaration, replace its
2022  // type metadata with the type metadata we found in cfi.functions. That
2023  // metadata is presumed to be more accurate than the metadata attached
2024  // to the declaration.
2025  if (F->isDeclaration()) {
2027  F->setLinkage(GlobalValue::ExternalWeakLinkage);
2028 
2029  F->eraseMetadata(LLVMContext::MD_type);
2030  for (unsigned I = 2; I < FuncMD->getNumOperands(); ++I)
2031  F->addMetadata(LLVMContext::MD_type,
2032  *cast<MDNode>(FuncMD->getOperand(I).get()));
2033  }
2034  }
2035  }
2036  }
2037 
2039  for (GlobalObject &GO : M.global_objects()) {
2040  if (isa<GlobalVariable>(GO) && GO.isDeclarationForLinker())
2041  continue;
2042 
2043  Types.clear();
2044  GO.getMetadata(LLVMContext::MD_type, Types);
2045 
2046  bool IsJumpTableCanonical = false;
2047  bool IsExported = false;
2048  if (Function *F = dyn_cast<Function>(&GO)) {
2049  IsJumpTableCanonical = isJumpTableCanonical(F);
2050  if (ExportedFunctions.count(F->getName())) {
2051  IsJumpTableCanonical |=
2052  ExportedFunctions[F->getName()].Linkage == CFL_Definition;
2053  IsExported = true;
2054  // TODO: The logic here checks only that the function is address taken,
2055  // not that the address takers are live. This can be updated to check
2056  // their liveness and emit fewer jumptable entries once monolithic LTO
2057  // builds also emit summaries.
2058  } else if (!F->hasAddressTaken()) {
2059  if (!CrossDsoCfi || !IsJumpTableCanonical || F->hasLocalLinkage())
2060  continue;
2061  }
2062  }
2063 
2064  auto *GTM = GlobalTypeMember::create(Alloc, &GO, IsJumpTableCanonical,
2065  IsExported, Types);
2066  GlobalTypeMembers[&GO] = GTM;
2067  for (MDNode *Type : Types) {
2068  verifyTypeMDNode(&GO, Type);
2069  auto &Info = TypeIdInfo[Type->getOperand(1)];
2070  Info.UniqueId = ++CurUniqueId;
2071  Info.RefGlobals.push_back(GTM);
2072  }
2073  }
2074 
2075  auto AddTypeIdUse = [&](Metadata *TypeId) -> TypeIdUserInfo & {
2076  // Add the call site to the list of call sites for this type identifier. We
2077  // also use TypeIdUsers to keep track of whether we have seen this type
2078  // identifier before. If we have, we don't need to re-add the referenced
2079  // globals to the equivalence class.
2080  auto Ins = TypeIdUsers.insert({TypeId, {}});
2081  if (Ins.second) {
2082  // Add the type identifier to the equivalence class.
2083  GlobalClassesTy::iterator GCI = GlobalClasses.insert(TypeId);
2084  GlobalClassesTy::member_iterator CurSet = GlobalClasses.findLeader(GCI);
2085 
2086  // Add the referenced globals to the type identifier's equivalence class.
2087  for (GlobalTypeMember *GTM : TypeIdInfo[TypeId].RefGlobals)
2088  CurSet = GlobalClasses.unionSets(
2089  CurSet, GlobalClasses.findLeader(GlobalClasses.insert(GTM)));
2090  }
2091 
2092  return Ins.first->second;
2093  };
2094 
2095  if (TypeTestFunc) {
2096  for (const Use &U : TypeTestFunc->uses()) {
2097  auto CI = cast<CallInst>(U.getUser());
2098  // If this type test is only used by llvm.assume instructions, it
2099  // was used for whole program devirtualization, and is being kept
2100  // for use by other optimization passes. We do not need or want to
2101  // lower it here. We also don't want to rewrite any associated globals
2102  // unnecessarily. These will be removed by a subsequent LTT invocation
2103  // with the DropTypeTests flag set.
2104  bool OnlyAssumeUses = !CI->use_empty();
2105  for (const Use &CIU : CI->uses()) {
2106  if (isa<AssumeInst>(CIU.getUser()))
2107  continue;
2108  OnlyAssumeUses = false;
2109  break;
2110  }
2111  if (OnlyAssumeUses)
2112  continue;
2113 
2114  auto TypeIdMDVal = dyn_cast<MetadataAsValue>(CI->getArgOperand(1));
2115  if (!TypeIdMDVal)
2116  report_fatal_error("Second argument of llvm.type.test must be metadata");
2117  auto TypeId = TypeIdMDVal->getMetadata();
2118  AddTypeIdUse(TypeId).CallSites.push_back(CI);
2119  }
2120  }
2121 
2122  if (ICallBranchFunnelFunc) {
2123  for (const Use &U : ICallBranchFunnelFunc->uses()) {
2124  if (Arch != Triple::x86_64)
2126  "llvm.icall.branch.funnel not supported on this target");
2127 
2128  auto CI = cast<CallInst>(U.getUser());
2129 
2130  std::vector<GlobalTypeMember *> Targets;
2131  if (CI->arg_size() % 2 != 1)
2132  report_fatal_error("number of arguments should be odd");
2133 
2134  GlobalClassesTy::member_iterator CurSet;
2135  for (unsigned I = 1; I != CI->arg_size(); I += 2) {
2136  int64_t Offset;
2137  auto *Base = dyn_cast<GlobalObject>(GetPointerBaseWithConstantOffset(
2138  CI->getOperand(I), Offset, M.getDataLayout()));
2139  if (!Base)
2141  "Expected branch funnel operand to be global value");
2142 
2143  GlobalTypeMember *GTM = GlobalTypeMembers[Base];
2144  Targets.push_back(GTM);
2145  GlobalClassesTy::member_iterator NewSet =
2146  GlobalClasses.findLeader(GlobalClasses.insert(GTM));
2147  if (I == 1)
2148  CurSet = NewSet;
2149  else
2150  CurSet = GlobalClasses.unionSets(CurSet, NewSet);
2151  }
2152 
2153  GlobalClasses.unionSets(
2154  CurSet, GlobalClasses.findLeader(
2155  GlobalClasses.insert(ICallBranchFunnel::create(
2156  Alloc, CI, Targets, ++CurUniqueId))));
2157  }
2158  }
2159 
2160  if (ExportSummary) {
2162  for (auto &P : TypeIdInfo) {
2163  if (auto *TypeId = dyn_cast<MDString>(P.first))
2164  MetadataByGUID[GlobalValue::getGUID(TypeId->getString())].push_back(
2165  TypeId);
2166  }
2167 
2168  for (auto &P : *ExportSummary) {
2169  for (auto &S : P.second.SummaryList) {
2170  if (!ExportSummary->isGlobalValueLive(S.get()))
2171  continue;
2172  if (auto *FS = dyn_cast<FunctionSummary>(S->getBaseObject()))
2173  for (GlobalValue::GUID G : FS->type_tests())
2174  for (Metadata *MD : MetadataByGUID[G])
2175  AddTypeIdUse(MD).IsExported = true;
2176  }
2177  }
2178  }
2179 
2180  if (GlobalClasses.empty())
2181  return false;
2182 
2183  // Build a list of disjoint sets ordered by their maximum global index for
2184  // determinism.
2185  std::vector<std::pair<GlobalClassesTy::iterator, unsigned>> Sets;
2186  for (GlobalClassesTy::iterator I = GlobalClasses.begin(),
2187  E = GlobalClasses.end();
2188  I != E; ++I) {
2189  if (!I->isLeader())
2190  continue;
2191  ++NumTypeIdDisjointSets;
2192 
2193  unsigned MaxUniqueId = 0;
2194  for (GlobalClassesTy::member_iterator MI = GlobalClasses.member_begin(I);
2195  MI != GlobalClasses.member_end(); ++MI) {
2196  if (auto *MD = MI->dyn_cast<Metadata *>())
2197  MaxUniqueId = std::max(MaxUniqueId, TypeIdInfo[MD].UniqueId);
2198  else if (auto *BF = MI->dyn_cast<ICallBranchFunnel *>())
2199  MaxUniqueId = std::max(MaxUniqueId, BF->UniqueId);
2200  }
2201  Sets.emplace_back(I, MaxUniqueId);
2202  }
2203  llvm::sort(Sets, llvm::less_second());
2204 
2205  // For each disjoint set we found...
2206  for (const auto &S : Sets) {
2207  // Build the list of type identifiers in this disjoint set.
2208  std::vector<Metadata *> TypeIds;
2209  std::vector<GlobalTypeMember *> Globals;
2210  std::vector<ICallBranchFunnel *> ICallBranchFunnels;
2211  for (GlobalClassesTy::member_iterator MI =
2212  GlobalClasses.member_begin(S.first);
2213  MI != GlobalClasses.member_end(); ++MI) {
2214  if (MI->is<Metadata *>())
2215  TypeIds.push_back(MI->get<Metadata *>());
2216  else if (MI->is<GlobalTypeMember *>())
2217  Globals.push_back(MI->get<GlobalTypeMember *>());
2218  else
2219  ICallBranchFunnels.push_back(MI->get<ICallBranchFunnel *>());
2220  }
2221 
2222  // Order type identifiers by unique ID for determinism. This ordering is
2223  // stable as there is a one-to-one mapping between metadata and unique IDs.
2224  llvm::sort(TypeIds, [&](Metadata *M1, Metadata *M2) {
2225  return TypeIdInfo[M1].UniqueId < TypeIdInfo[M2].UniqueId;
2226  });
2227 
2228  // Same for the branch funnels.
2229  llvm::sort(ICallBranchFunnels,
2230  [&](ICallBranchFunnel *F1, ICallBranchFunnel *F2) {
2231  return F1->UniqueId < F2->UniqueId;
2232  });
2233 
2234  // Build bitsets for this disjoint set.
2235  buildBitSetsFromDisjointSet(TypeIds, Globals, ICallBranchFunnels);
2236  }
2237 
2238  allocateByteArrays();
2239 
2240  // Parse alias data to replace stand-in function declarations for aliases
2241  // with an alias to the intended target.
2242  if (ExportSummary) {
2243  if (NamedMDNode *AliasesMD = M.getNamedMetadata("aliases")) {
2244  for (auto AliasMD : AliasesMD->operands()) {
2245  assert(AliasMD->getNumOperands() >= 4);
2246  StringRef AliasName =
2247  cast<MDString>(AliasMD->getOperand(0))->getString();
2248  StringRef Aliasee = cast<MDString>(AliasMD->getOperand(1))->getString();
2249 
2250  if (!ExportedFunctions.count(Aliasee) ||
2251  ExportedFunctions[Aliasee].Linkage != CFL_Definition ||
2252  !M.getNamedAlias(Aliasee))
2253  continue;
2254 
2255  GlobalValue::VisibilityTypes Visibility =
2256  static_cast<GlobalValue::VisibilityTypes>(
2257  cast<ConstantAsMetadata>(AliasMD->getOperand(2))
2258  ->getValue()
2259  ->getUniqueInteger()
2260  .getZExtValue());
2261  bool Weak =
2262  static_cast<bool>(cast<ConstantAsMetadata>(AliasMD->getOperand(3))
2263  ->getValue()
2264  ->getUniqueInteger()
2265  .getZExtValue());
2266 
2267  auto *Alias = GlobalAlias::create("", M.getNamedAlias(Aliasee));
2268  Alias->setVisibility(Visibility);
2269  if (Weak)
2270  Alias->setLinkage(GlobalValue::WeakAnyLinkage);
2271 
2272  if (auto *F = M.getFunction(AliasName)) {
2273  Alias->takeName(F);
2274  F->replaceAllUsesWith(Alias);
2275  F->eraseFromParent();
2276  } else {
2277  Alias->setName(AliasName);
2278  }
2279  }
2280  }
2281  }
2282 
2283  // Emit .symver directives for exported functions, if they exist.
2284  if (ExportSummary) {
2285  if (NamedMDNode *SymversMD = M.getNamedMetadata("symvers")) {
2286  for (auto Symver : SymversMD->operands()) {
2287  assert(Symver->getNumOperands() >= 2);
2289  cast<MDString>(Symver->getOperand(0))->getString();
2290  StringRef Alias = cast<MDString>(Symver->getOperand(1))->getString();
2291 
2292  if (!ExportedFunctions.count(SymbolName))
2293  continue;
2294 
2295  M.appendModuleInlineAsm(
2296  (llvm::Twine(".symver ") + SymbolName + ", " + Alias).str());
2297  }
2298  }
2299  }
2300 
2301  return true;
2302 }
2303 
2305  ModuleAnalysisManager &AM) {
2306  bool Changed;
2307  if (UseCommandLine)
2308  Changed = LowerTypeTestsModule::runForTesting(M);
2309  else
2310  Changed =
2311  LowerTypeTestsModule(M, ExportSummary, ImportSummary, DropTypeTests)
2312  .lower();
2313  if (!Changed)
2314  return PreservedAnalyses::all();
2315  return PreservedAnalyses::none();
2316 }
AvoidReuse
static cl::opt< bool > AvoidReuse("lowertypetests-avoid-reuse", cl::desc("Try to avoid reuse of byte array addresses using aliases"), cl::Hidden, cl::init(true))
llvm::codeview::SimpleTypeKind::Byte
@ Byte
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
MemoryBuffer.h
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:156
llvm::Triple::riscv64
@ riscv64
Definition: Triple.h:76
Int32Ty
IntegerType * Int32Ty
Definition: NVVMIntrRange.cpp:67
llvm::GlobalVariable::eraseFromParent
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:451
TrailingObjects.h
llvm::less_second
Function object to check whether the second component of a std::pair compares less than the second co...
Definition: STLExtras.h:1362
targets
should just be implemented with a CLZ instruction Since there are other targets
Definition: README.txt:709
ClReadSummary
static cl::opt< std::string > ClReadSummary("lowertypetests-read-summary", cl::desc("Read summary from given YAML file before running pass"), cl::Hidden)
getName
static StringRef getName(Value *V)
Definition: ProvenanceAnalysisEvaluator.cpp:42
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:104
MathExtras.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::Attribute::isValid
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:182
llvm::Type::getInt1Ty
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:236
llvm::dwarf::Constants
Constants
Definition: Dwarf.h:432
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::NamedMDNode
A tuple of MDNodes.
Definition: Metadata.h:1572
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
FileSystem.h
llvm::Value::hasOneUse
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:434
llvm::lowertypetests::BitSetInfo::containsGlobalOffset
bool containsGlobalOffset(uint64_t Offset) const
Definition: LowerTypeTests.cpp:126
Metadata.h
llvm::Type::getInt8PtrTy
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:291
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:248
IntrinsicInst.h
llvm::GlobalVariable::setConstant
void setConstant(bool Val)
Definition: GlobalVariable.h:153
llvm::Triple::wasm32
@ wasm32
Definition: Triple.h:103
llvm::ConstantExpr::getZExt
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2156
llvm::GlobalValue::getLinkage
LinkageTypes getLinkage() const
Definition: GlobalValue.h:509
llvm::createLowerTypeTestsPass
ModulePass * createLowerTypeTestsPass(ModuleSummaryIndex *ExportSummary, const ModuleSummaryIndex *ImportSummary, bool DropTypeTests=false)
This pass lowers type metadata and the llvm.type.test intrinsic to bitsets.
llvm::HexagonISD::JT
@ JT
Definition: HexagonISelLowering.h:52
llvm::lowertypetests::GlobalLayoutBuilder::FragmentMap
std::vector< uint64_t > FragmentMap
Mapping from object index to fragment index.
Definition: LowerTypeTests.h:132
llvm::GlobalValue::HiddenVisibility
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:64
llvm::TypeTestResolution::Kind
Kind
Specifies which kind of type check we should emit for this byte array.
Definition: ModuleSummaryIndex.h:970
T
llvm::Function
Definition: Function.h:60
llvm::lowertypetests::ByteArrayBuilder::BitsPerByte
@ BitsPerByte
Definition: LowerTypeTests.h:178
llvm::Attribute
Definition: Attributes.h:65
StringRef.h
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
Pass.h
llvm::Triple::x86
@ x86
Definition: Triple.h:85
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:632
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:145
llvm::Function::eraseFromParent
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Function.cpp:364
llvm::ilist_node_with_parent::getNextNode
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:289
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
Statistic.h
llvm::GlobalObject::getAlign
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
Definition: GlobalObject.h:79
InlineAsm.h
llvm::ConstantExpr::getICmp
static Constant * getICmp(unsigned short pred, Constant *LHS, Constant *RHS, bool OnlyIfReduced=false)
get* - Return some common constants without having to specify the full Instruction::OPCODE identifier...
Definition: Constants.cpp:2535
llvm::initializeLowerTypeTestsPass
void initializeLowerTypeTestsPass(PassRegistry &)
llvm::EquivalenceClasses
EquivalenceClasses - This represents a collection of equivalence classes and supports three efficient...
Definition: EquivalenceClasses.h:60
llvm::TypeTestResolution::InlineBits
uint64_t InlineBits
Definition: ModuleSummaryIndex.h:993
llvm::lowertypetests::ByteArrayBuilder
This class is used to build a byte array containing overlapping bit sets.
Definition: LowerTypeTests.h:174
llvm::Value::hasName
bool hasName() const
Definition: Value.h:261
ErrorHandling.h
llvm::TypeTestResolution::Unsat
@ Unsat
Unsatisfiable type (i.e. no global has this type metadata)
Definition: ModuleSummaryIndex.h:971
llvm::IRBuilder<>
llvm::GlobalVariable
Definition: GlobalVariable.h:39
Allocator.h
llvm::PointerType::getAddressSpace
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:682
llvm::GlobalObject::getMetadata
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Metadata.cpp:1295
llvm::CmpInst::ICMP_NE
@ ICMP_NE
not equal
Definition: InstrTypes.h:741
llvm::ConstantExpr::getBitCast
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2266
llvm::ModuleSummaryIndex::getOrInsertTypeIdSummary
TypeIdSummary & getOrInsertTypeIdSummary(StringRef TypeId)
Return an existing or new TypeIdSummary entry for TypeId.
Definition: ModuleSummaryIndex.h:1506
llvm::FunctionType::get
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:361
llvm::GlobalAlias
Definition: GlobalAlias.h:28
ValueTracking.h
Error.h
llvm::ConstantExpr::getSelect
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2, Type *OnlyIfReducedTy=nullptr)
Select constant expr.
Definition: Constants.cpp:2453
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
llvm::TypeTestResolution::AllOnes
@ AllOnes
All-ones bit vector ("Eliminating Bit Vector Checks for All-Ones Bit Vectors")
Definition: ModuleSummaryIndex.h:975
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:139
llvm::CFL_Definition
@ CFL_Definition
Definition: TypeMetadataUtils.h:32
llvm::Module::getModuleFlag
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition: Module.cpp:322
APInt.h
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1886
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
DenseMap.h
llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Used
@ Used
Module.h
llvm::Triple::x86_64
@ x86_64
Definition: Triple.h:86
llvm::tgtok::Bits
@ Bits
Definition: TGLexer.h:50
llvm::lowertypetests::GlobalLayoutBuilder
This class implements a layout algorithm for globals referenced by bit sets that tries to keep member...
Definition: LowerTypeTests.h:126
llvm::ExitOnError
Helper for check-and-exit error handling.
Definition: Error.h:1348
llvm::BasicBlock::splitBasicBlock
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="", bool Before=false)
Split the basic block into two basic blocks at the specified instruction.
Definition: BasicBlock.cpp:378
llvm::ConstantAsMetadata::get
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:420
GlobalObject.h
llvm::ModuleSummaryIndex::getTypeIdSummary
const TypeIdSummary * getTypeIdSummary(StringRef TypeId) const
This returns either a pointer to the type id summary (if present in the summary map) or null (if not ...
Definition: ModuleSummaryIndex.h:1518
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:79
Operator.h
llvm::Value::user_begin
user_iterator user_begin()
Definition: Value.h:397
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::ConstantExpr::getInBoundsGetElementPtr
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1274
llvm::lowertypetests::GlobalLayoutBuilder::Fragments
std::vector< std::vector< uint64_t > > Fragments
The computed layout.
Definition: LowerTypeTests.h:129
llvm::GlobalValue::LinkageTypes
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
llvm::ArrayType
Class to represent array types.
Definition: DerivedTypes.h:357
llvm::tgtok::Else
@ Else
Definition: TGLexer.h:76
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::insert
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::count
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:97
llvm::Type::getInt8Ty
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:237
llvm::lowertypetests::BitSetInfo::BitSize
uint64_t BitSize
Definition: LowerTypeTests.h:41
Use.h
llvm::lowertypetests::BitSetBuilder
Definition: LowerTypeTests.h:61
llvm::Type::getInt32Ty
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:239
llvm::ZB_Undefined
@ ZB_Undefined
The returned value is undefined.
Definition: MathExtras.h:46
llvm::PassSummaryAction::Export
@ Export
Export information to summary.
ModuleSummaryIndex.h
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:159
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1384
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::collectUsedGlobalVariables
GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition: Module.cpp:786
llvm::Instruction::setMetadata
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1434
llvm::ConstantExpr::getPointerCast
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:2078
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
llvm::MDNode::getNumOperands
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1284
llvm::AArch64CC::LT
@ LT
Definition: AArch64BaseInfo.h:266
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::ConstantExpr::getSub
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2734
llvm::JumpTable::JumpTableType
JumpTableType
Definition: TargetOptions.h:44
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:186
llvm::BitmaskEnumDetail::Mask
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:80
Instruction.h
CommandLine.h
llvm::GlobalObject::eraseMetadata
bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
Definition: Metadata.cpp:1377
INITIALIZE_PASS
INITIALIZE_PASS(LowerTypeTests, "lowertypetests", "Lower type metadata", false, false) ModulePass *llvm
Definition: LowerTypeTests.cpp:565
llvm::all_of
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1617
llvm::tgtok::Then
@ Then
Definition: TGLexer.h:52
GlobalValue.h
llvm::lowertypetests::BitSetInfo::AlignLog2
unsigned AlignLog2
Definition: LowerTypeTests.h:46
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::Triple::ArchType
ArchType
Definition: Triple.h:46
llvm::GlobalValue::VisibilityTypes
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition: GlobalValue.h:62
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
Constants.h
llvm::lowertypetests::ByteArrayBuilder::allocate
void allocate(const std::set< uint64_t > &Bits, uint64_t BitSize, uint64_t &AllocByteOffset, uint8_t &AllocMask)
Allocate BitSize bits in the byte array where Bits contains the bits to set.
Definition: LowerTypeTests.cpp:217
llvm::lowertypetests::BitSetBuilder::addOffset
void addOffset(uint64_t Offset)
Definition: LowerTypeTests.h:68
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::StringRef::split
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:753
llvm::User
Definition: User.h:44
llvm::lowertypetests::GlobalLayoutBuilder::addFragment
void addFragment(const std::set< uint64_t > &F)
Add F to the layout while trying to keep its indices contiguous.
Definition: LowerTypeTests.cpp:188
Intrinsics.h
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::ARM_PROC::A
@ A
Definition: ARMBaseInfo.h:34
llvm::ConstantExpr::getIntToPtr
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2252
llvm::GlobalObject
Definition: GlobalObject.h:27
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:90
llvm::PassSummaryAction::None
@ None
Do nothing.
TypeMetadataUtils.h
llvm::ConstantExpr::getPtrToInt
static Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2238
llvm::ReplaceInstWithInst
void ReplaceInstWithInst(BasicBlock::InstListType &BIL, BasicBlock::iterator &BI, Instruction *I)
Replace the instruction specified by BI with the instruction specified by I.
Definition: BasicBlockUtils.cpp:477
llvm::Value::uses
iterator_range< use_iterator > uses()
Definition: Value.h:376
llvm::NextPowerOf2
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition: MathExtras.h:710
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::IntegerType
Class to represent integer types.
Definition: DerivedTypes.h:40
kX86JumpTableEntrySize
static const unsigned kX86JumpTableEntrySize
Definition: LowerTypeTests.cpp:1223
kRISCVJumpTableEntrySize
static const unsigned kRISCVJumpTableEntrySize
Definition: LowerTypeTests.cpp:1226
llvm::Triple::Win32
@ Win32
Definition: Triple.h:198
llvm::appendToCompilerUsed
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.
Definition: ModuleUtils.cpp:109
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:143
llvm::APInt::getZExtValue
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1466
llvm::CFL_WeakDeclaration
@ CFL_WeakDeclaration
Definition: TypeMetadataUtils.h:34
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::Value::setName
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:372
llvm::lowertypetests::BitSetInfo
Definition: LowerTypeTests.h:33
llvm::ConstantInt::get
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:928
llvm::Use::getUser
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:72
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::lowertypetests::BitSetInfo::ByteOffset
uint64_t ByteOffset
Definition: LowerTypeTests.h:38
Align
uint64_t Align
Definition: ELFObjHandler.cpp:81
ExportedFunctions
static ManagedStatic< std::map< const Function *, ExFunc > > ExportedFunctions
Definition: ExternalFunctions.cpp:63
llvm::GlobalValue::InternalLinkage
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::MCID::Call
@ Call
Definition: MCInstrDesc.h:155
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::TypeTestResolution::BitMask
uint8_t BitMask
Definition: ModuleSummaryIndex.h:992
llvm::TypeTestResolution::Inline
@ Inline
Inlined bit vector ("Short Inline Bit Vectors")
Definition: ModuleSummaryIndex.h:973
isDirectCall
static bool isDirectCall(Use &U)
Definition: LowerTypeTests.cpp:1777
llvm::Attribute::getValueAsString
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:304
llvm::PassSummaryAction::Import
@ Import
Import information from summary.
llvm::Value::use_empty
bool use_empty() const
Definition: Value.h:344
llvm::InlineAsm::get
static InlineAsm * get(FunctionType *Ty, StringRef AsmString, StringRef Constraints, bool hasSideEffects, bool isAlignStack=false, AsmDialect asmDialect=AD_ATT, bool canThrow=false)
InlineAsm::get - Return the specified uniqued inline asm string.
Definition: InlineAsm.cpp:41
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Type.h
llvm::lowertypetests::BitSetInfo::Bits
std::set< uint64_t > Bits
Definition: LowerTypeTests.h:35
llvm::dxil::PointerTypeAnalysis::run
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
Definition: PointerTypeAnalysis.cpp:101
llvm::sys::fs::OF_TextWithCRLF
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition: FileSystem.h:770
kARMBTIJumpTableEntrySize
static const unsigned kARMBTIJumpTableEntrySize
Definition: LowerTypeTests.cpp:1225
isThumbFunction
static bool isThumbFunction(Function *F, Triple::ArchType ModuleArch)
Definition: LowerTypeTests.cpp:1364
llvm::TypeTestResolution::AlignLog2
uint64_t AlignLog2
Definition: ModuleSummaryIndex.h:990
llvm::TypeTestResolution::Single
@ Single
Single element (last example in "Short Inline Bit Vectors")
Definition: ModuleSummaryIndex.h:974
llvm::MDNode::getOperand
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1278
llvm::InlineAsm
Definition: InlineAsm.h:31
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:200
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::tgtok::In
@ In
Definition: TGLexer.h:51
isKnownTypeIdMember
static bool isKnownTypeIdMember(Metadata *TypeId, const DataLayout &DL, Value *V, uint64_t COffset)
Definition: LowerTypeTests.cpp:712
llvm::SPIRV::Decoration::Alignment
@ Alignment
BasicBlock.h
llvm::cl::opt< bool >
llvm::GlobalObject::hasSection
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:103
llvm::Triple::ELF
@ ELF
Definition: Triple.h:274
llvm::GlobalVariable::getInitializer
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
Definition: GlobalVariable.h:135
VI
@ VI
Definition: SIInstrInfo.cpp:7824
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::cl::values
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:685
llvm::Instruction::eraseFromParent
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:77
llvm::AMDGPU::Hwreg::Offset
Offset
Definition: SIDefines.h:413
llvm::PointerType::getUnqual
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:651
uint64_t
llvm::GlobalValue::getVisibility
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:234
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:620
const
aarch64 promote const
Definition: AArch64PromoteConstant.cpp:232
llvm::ConstantPointerNull::get
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1763
IPO.h
llvm::BumpPtrAllocatorImpl
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:63
ClSummaryAction
static cl::opt< PassSummaryAction > ClSummaryAction("lowertypetests-summary-action", cl::desc("What to do with the summary when running this pass"), cl::values(clEnumValN(PassSummaryAction::None, "none", "Do nothing"), clEnumValN(PassSummaryAction::Import, "import", "Import typeid resolutions from summary and globals"), clEnumValN(PassSummaryAction::Export, "export", "Export typeid resolutions to summary and globals")), cl::Hidden)
llvm::lowertypetests::BitSetBuilder::build
BitSetInfo build()
Definition: LowerTypeTests.cpp:155
llvm::lowertypetests::ByteArrayBuilder::Bytes
std::vector< uint8_t > Bytes
The byte array built so far.
Definition: LowerTypeTests.h:176
llvm::BranchInst::Create
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
Definition: Instructions.h:3142
llvm::DenseMap
Definition: DenseMap.h:716
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
llvm::appendToUsed
void appendToUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.used list.
Definition: ModuleUtils.cpp:105
llvm::make_early_inc_range
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:618
llvm::PointerType
Class to represent pointers.
Definition: DerivedTypes.h:632
llvm::GlobalValue::setLinkage
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:502
ArrayRef.h
llvm::TypeIdSummary
Definition: ModuleSummaryIndex.h:1033
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::find
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:152
llvm::ConstantStruct::getAnon
static Constant * getAnon(ArrayRef< Constant * > V, bool Packed=false)
Return an anonymous struct that has the specified elements.
Definition: Constants.h:463
llvm::Function::Create
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:137
IRBuilder.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::lowertypetests::BitSetBuilder::Offsets
SmallVector< uint64_t, 16 > Offsets
Definition: LowerTypeTests.h:62
llvm::X86AS::FS
@ FS
Definition: X86.h:192
llvm::codeview::CompileSym2Flags::EC
@ EC
llvm::GlobalValue::isThreadLocal
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
Definition: GlobalValue.h:249
PointerUnion.h
llvm::ArrayType::get
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:638
llvm::lowertypetests::BitSetInfo::print
void print(raw_ostream &OS) const
Definition: LowerTypeTests.cpp:140
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::ModuleSummaryIndex::cfiFunctionDefs
std::set< std::string > & cfiFunctionDefs()
Definition: ModuleSummaryIndex.h:1357
llvm::MDNode
Metadata node.
Definition: Metadata.h:944
TinyPtrVector.h
llvm::lowertypetests::BitSetInfo::isAllOnes
bool isAllOnes() const
Definition: LowerTypeTests.h:52
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:75
llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::insert
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:141
llvm::ModuleSummaryIndex::partiallySplitLTOUnits
bool partiallySplitLTOUnits() const
Definition: ModuleSummaryIndex.h:1303
Triple.h
YAMLTraits.h
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::lowertypetests::BitSetBuilder::Min
uint64_t Min
Definition: LowerTypeTests.h:63
llvm::Triple::arm
@ arm
Definition: Triple.h:49
ModuleSummaryIndexYAML.h
DataLayout.h
llvm::StructType
Class to represent struct types.
Definition: DerivedTypes.h:213
llvm::countTrailingZeros
unsigned countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition: MathExtras.h:156
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
llvm::ConstantDataArray::get
static Constant * get(LLVMContext &Context, ArrayRef< ElementTy > Elts)
get() constructor - Return a constant with array type with an element count and element type matching...
Definition: Constants.h:692
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:529
llvm::BasicBlock::Create
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:97
llvm::Triple::thumb
@ thumb
Definition: Triple.h:83
llvm::append_range
void append_range(Container &C, Range &&R)
Wrapper function to append a range to a container.
Definition: STLExtras.h:1823
llvm::ilist_node_impl::getIterator
self_iterator getIterator()
Definition: ilist_node.h:82
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
clEnumValN
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:660
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::TypeTestResolution
Definition: ModuleSummaryIndex.h:965
llvm::ModuleSummaryIndex::cfiFunctionDecls
std::set< std::string > & cfiFunctionDecls()
Definition: ModuleSummaryIndex.h:1360
LowerTypeTests.h
llvm::TypeTestResolution::TheKind
enum llvm::TypeTestResolution::Kind TheKind
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:305
llvm::TypeTestResolution::Unknown
@ Unknown
Unknown (analysis not performed, don't lower)
Definition: ModuleSummaryIndex.h:977
llvm::Triple::ObjectFormatType
ObjectFormatType
Definition: Triple.h:269
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:209
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:444
llvm::NamedMDNode::operands
iterator_range< op_iterator > operands()
Definition: Metadata.h:1668
llvm::PointerUnion
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:112
llvm::ConstantInt::getFalse
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:883
llvm::MCID::Select
@ Select
Definition: MCInstrDesc.h:164
Attributes.h
llvm::lowertypetests::ByteArrayBuilder::BitAllocs
uint64_t BitAllocs[BitsPerByte]
The number of bytes allocated so far for each of the bits.
Definition: LowerTypeTests.h:181
Constant.h
llvm::AMDGPU::HSAMD::Kernel::Key::SymbolName
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
Definition: AMDGPUMetadata.h:386
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::stable_sort
void stable_sort(R &&Range)
Definition: STLExtras.h:1761
llvm::TypeTestResolution::SizeM1
uint64_t SizeM1
Definition: ModuleSummaryIndex.h:991
llvm::Type::getInt64Ty
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:240
llvm::Triple::OSType
OSType
Definition: Triple.h:181
llvm::ConstantInt::getTrue
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:876
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::Constant::getNullValue
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:350
llvm::GlobalAlias::create
static GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition: Globals.cpp:503
get
Should compile to something r4 addze r3 instead we get
Definition: README.txt:24
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::end
iterator end()
Definition: DenseMap.h:84
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:345
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:152
JumpTable
MIPS Relocation Principles In there are several elements of the llvm::ISD::NodeType enum that deal with addresses and or relocations These are defined in include llvm Target TargetSelectionDAG td JumpTable
Definition: Relocation.txt:6
llvm::CallBase::arg_size
unsigned arg_size() const
Definition: InstrTypes.h:1339
GlobalVariable.h
llvm::BumpPtrAllocatorImpl::Allocate
LLVM_ATTRIBUTE_RETURNS_NONNULL void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:146
llvm::Triple::riscv32
@ riscv32
Definition: Triple.h:75
Casting.h
llvm::Triple::MachO
@ MachO
Definition: Triple.h:276
Function.h
llvm::BitWidth
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:147
selectJumpTableArmEncoding
static Triple::ArchType selectJumpTableArmEncoding(ArrayRef< GlobalTypeMember * > Functions, Triple::ArchType ModuleArch)
Definition: LowerTypeTests.cpp:1384
ClWriteSummary
static cl::opt< std::string > ClWriteSummary("lowertypetests-write-summary", cl::desc("Write summary to given YAML file after running pass"), cl::Hidden)
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1562
PassManager.h
EquivalenceClasses.h
llvm::ReturnInst::Create
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, Instruction *InsertBefore=nullptr)
Definition: Instructions.h:3031
llvm::TrailingObjects
See the file comment for details on the usage of the TrailingObjects type.
Definition: TrailingObjects.h:212
llvm::GetPointerBaseWithConstantOffset
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset.
Definition: ValueTracking.h:278
llvm::ConstantExpr::getGetElementPtr
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1247
llvm::SmallVectorImpl::clear
void clear()
Definition: SmallVector.h:591
llvm::CfiFunctionLinkage
CfiFunctionLinkage
The type of CFI jumptable needed for a function.
Definition: TypeMetadataUtils.h:31
GlobalAlias.h
llvm::codeview::ModifierOptions::Const
@ Const
llvm::pdb::PDB_ColorItem::Padding
@ Padding
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:475
llvm::GlobalValue::ExternalLinkage
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
llvm::Type::getVoidTy
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:222
llvm::GlobalObject::setMetadata
void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1332
llvm::errorOrToExpected
Expected< T > errorOrToExpected(ErrorOr< T > &&EO)
Convert an ErrorOr<T> to an Expected<T>.
Definition: Error.h:1179
llvm::GlobalValue::PrivateLinkage
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:56
Instructions.h
llvm::lowertypetests::BitSetBuilder::Max
uint64_t Max
Definition: LowerTypeTests.h:64
llvm::pdb::DbgHeaderType::Max
@ Max
llvm::TypeIdSummary::TTRes
TypeTestResolution TTRes
Definition: ModuleSummaryIndex.h:1034
SmallVector.h
User.h
llvm::ModuleSummaryIndex
Class to hold module path string table and global value map, and encapsulate methods for operating on...
Definition: ModuleSummaryIndex.h:1087
ModuleUtils.h
llvm::CallBase::getArgOperand
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1341
llvm::Instruction::getParent
const BasicBlock * getParent() const
Definition: Instruction.h:91
ClDropTypeTests
static cl::opt< bool > ClDropTypeTests("lowertypetests-drop-type-tests", cl::desc("Simply drop type test assume sequences"), cl::Hidden, cl::init(false))
getTrue
static Constant * getTrue(Type *Ty)
For a boolean type or a vector of boolean type, return true or a vector with every element true.
Definition: InstructionSimplify.cpp:125
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:164
llvm::IntegerType::getBitWidth
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:72
llvm::tgtok::Bit
@ Bit
Definition: TGLexer.h:50
llvm::GlobalValue::isDeclarationForLinker
bool isDeclarationForLinker() const
Definition: GlobalValue.h:582
llvm::PHINode
Definition: Instructions.h:2651
llvm::BasicBlock::getTerminator
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:119
llvm::MipsISD::Ins
@ Ins
Definition: MipsISelLowering.h:160
llvm::SmallVectorImpl< Value * >
llvm::StructType::getElementType
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:328
llvm::GlobalValue::getType
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:276
DerivedTypes.h
llvm::SmallSetVector
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:307
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::GlobalValue::getValueType
Type * getValueType() const
Definition: GlobalValue.h:278
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1461
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
GEP
Hexagon Common GEP
Definition: HexagonCommonGEP.cpp:171
llvm::Value::replaceUsesWithIf
void replaceUsesWithIf(Value *New, llvm::function_ref< bool(Use &U)> ShouldReplace)
Go through the uses list for this definition and make each use point to "V" if the callback ShouldRep...
Definition: Value.cpp:537
LLVMContext.h
llvm::GlobalAlias::eraseFromParent
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:535
llvm::Value::takeName
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:378
llvm::SplitBlockAndInsertIfThen
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights, DominatorTree *DT, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
Definition: BasicBlockUtils.cpp:1446
llvm::appendToGlobalCtors
void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
Definition: ModuleUtils.cpp:65
llvm::User::getOperand
Value * getOperand(unsigned i) const
Definition: User.h:169
llvm::cl::desc
Definition: CommandLine.h:405
llvm::M1
unsigned M1(unsigned Val)
Definition: VE.h:370
llvm::GlobalValue::ExternalWeakLinkage
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition: GlobalValue.h:57
llvm::BranchInst
Conditional or Unconditional Branch instruction.
Definition: Instructions.h:3086
raw_ostream.h
llvm::TypeTestResolution::ByteArray
@ ByteArray
Test a byte array (first example)
Definition: ModuleSummaryIndex.h:972
llvm::ConstantAggregateZero::get
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1656
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition: SmallVector.h:644
llvm::GlobalValue::isDSOLocal
bool isDSOLocal() const
Definition: GlobalValue.h:287
BasicBlockUtils.h
Value.h
llvm::Triple::wasm64
@ wasm64
Definition: Triple.h:104
llvm::lowertypetests::isJumpTableCanonical
bool isJumpTableCanonical(Function *F)
Definition: LowerTypeTests.cpp:240
kARMJumpTableEntrySize
static const unsigned kARMJumpTableEntrySize
Definition: LowerTypeTests.cpp:1224
llvm::GlobalValue::setVisibility
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:240
InitializePasses.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::MDOperand::get
Metadata * get() const
Definition: Metadata.h:795
Debug.h
llvm::Value::users
iterator_range< user_iterator > users()
Definition: Value.h:421
llvm::Triple::aarch64
@ aarch64
Definition: Triple.h:51
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:153
llvm::TypeTestResolution::SizeM1BitWidth
unsigned SizeM1BitWidth
Range of size-1 expressed as a bit width.
Definition: ModuleSummaryIndex.h:983
SetVector.h
llvm::sampleprof::Base
@ Base
Definition: Discriminator.h:58
llvm::GlobalVariable::setInitializer
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:455
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38
createMaskedBitTest
static Value * createMaskedBitTest(IRBuilder<> &B, Value *Bits, Value *BitOffset)
Build a test that bit BitOffset mod sizeof(Bits)*8 is set in Bits.
Definition: LowerTypeTests.cpp:601