1 //===- LowerTypeTests.cpp - type metadata lowering pass -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass lowers type metadata and calls to the llvm.type.test intrinsic.
11 // It also ensures that globals are properly laid out for the
12 // llvm.icall.branch.funnel intrinsic.
13 // See http://llvm.org/docs/TypeMetadata.html for more information.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/IPO/LowerTypeTests.h"
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/EquivalenceClasses.h"
22 #include "llvm/ADT/PointerUnion.h"
23 #include "llvm/ADT/SetVector.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/ADT/TinyPtrVector.h"
28 #include "llvm/ADT/Triple.h"
29 #include "llvm/Analysis/TypeMetadataUtils.h"
30 #include "llvm/Analysis/ValueTracking.h"
31 #include "llvm/IR/Attributes.h"
32 #include "llvm/IR/BasicBlock.h"
33 #include "llvm/IR/Constant.h"
34 #include "llvm/IR/Constants.h"
35 #include "llvm/IR/DataLayout.h"
36 #include "llvm/IR/DerivedTypes.h"
37 #include "llvm/IR/Function.h"
38 #include "llvm/IR/GlobalAlias.h"
39 #include "llvm/IR/GlobalObject.h"
40 #include "llvm/IR/GlobalValue.h"
41 #include "llvm/IR/GlobalVariable.h"
42 #include "llvm/IR/IRBuilder.h"
43 #include "llvm/IR/InlineAsm.h"
44 #include "llvm/IR/Instruction.h"
45 #include "llvm/IR/Instructions.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"
50 #include "llvm/IR/ModuleSummaryIndex.h"
51 #include "llvm/IR/ModuleSummaryIndexYAML.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/Pass.h"
59 #include "llvm/Support/Allocator.h"
60 #include "llvm/Support/Casting.h"
61 #include "llvm/Support/CommandLine.h"
62 #include "llvm/Support/Debug.h"
63 #include "llvm/Support/Error.h"
64 #include "llvm/Support/ErrorHandling.h"
65 #include "llvm/Support/FileSystem.h"
66 #include "llvm/Support/MathExtras.h"
67 #include "llvm/Support/MemoryBuffer.h"
68 #include "llvm/Support/TrailingObjects.h"
69 #include "llvm/Support/YAMLTraits.h"
70 #include "llvm/Support/raw_ostream.h"
71 #include "llvm/Transforms/IPO.h"
72 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
73 #include "llvm/Transforms/Utils/ModuleUtils.h"
80 #include <system_error>
85 using namespace lowertypetests;
87 #define DEBUG_TYPE "lowertypetests"
89 STATISTIC(ByteArraySizeBits, "Byte array size in bits");
90 STATISTIC(ByteArraySizeBytes, "Byte array size in bytes");
91 STATISTIC(NumByteArraysCreated, "Number of byte arrays created");
92 STATISTIC(NumTypeTestCallsLowered, "Number of type test calls lowered");
93 STATISTIC(NumTypeIdDisjointSets, "Number of disjoint sets of type identifiers");
95 static cl::opt<bool> AvoidReuse(
96 "lowertypetests-avoid-reuse",
97 cl::desc("Try to avoid reuse of byte array addresses using aliases"),
98 cl::Hidden, cl::init(true));
100 static cl::opt<PassSummaryAction> ClSummaryAction(
101 "lowertypetests-summary-action",
102 cl::desc("What to do with the summary when running this pass"),
103 cl::values(clEnumValN(PassSummaryAction::None, "none", "Do nothing"),
104 clEnumValN(PassSummaryAction::Import, "import",
105 "Import typeid resolutions from summary and globals"),
106 clEnumValN(PassSummaryAction::Export, "export",
107 "Export typeid resolutions to summary and globals")),
110 static cl::opt<std::string> ClReadSummary(
111 "lowertypetests-read-summary",
112 cl::desc("Read summary from given YAML file before running pass"),
115 static cl::opt<std::string> ClWriteSummary(
116 "lowertypetests-write-summary",
117 cl::desc("Write summary to given YAML file after running pass"),
120 bool BitSetInfo::containsGlobalOffset(uint64_t Offset) const {
121 if (Offset < ByteOffset)
124 if ((Offset - ByteOffset) % (uint64_t(1) << AlignLog2) != 0)
127 uint64_t BitOffset = (Offset - ByteOffset) >> AlignLog2;
128 if (BitOffset >= BitSize)
131 return Bits.count(BitOffset);
134 void BitSetInfo::print(raw_ostream &OS) const {
135 OS << "offset " << ByteOffset << " size " << BitSize << " align "
144 for (uint64_t B : Bits)
149 BitSetInfo BitSetBuilder::build() {
153 // Normalize each offset against the minimum observed offset, and compute
154 // the bitwise OR of each of the offsets. The number of trailing zeros
155 // in the mask gives us the log2 of the alignment of all offsets, which
156 // allows us to compress the bitset by only storing one bit per aligned
159 for (uint64_t &Offset : Offsets) {
165 BSI.ByteOffset = Min;
169 BSI.AlignLog2 = countTrailingZeros(Mask, ZB_Undefined);
171 // Build the compressed bitset while normalizing the offsets against the
172 // computed alignment.
173 BSI.BitSize = ((Max - Min) >> BSI.AlignLog2) + 1;
174 for (uint64_t Offset : Offsets) {
175 Offset >>= BSI.AlignLog2;
176 BSI.Bits.insert(Offset);
182 void GlobalLayoutBuilder::addFragment(const std::set<uint64_t> &F) {
183 // Create a new fragment to hold the layout for F.
184 Fragments.emplace_back();
185 std::vector<uint64_t> &Fragment = Fragments.back();
186 uint64_t FragmentIndex = Fragments.size() - 1;
188 for (auto ObjIndex : F) {
189 uint64_t OldFragmentIndex = FragmentMap[ObjIndex];
190 if (OldFragmentIndex == 0) {
191 // We haven't seen this object index before, so just add it to the current
193 Fragment.push_back(ObjIndex);
195 // This index belongs to an existing fragment. Copy the elements of the
196 // old fragment into this one and clear the old fragment. We don't update
197 // the fragment map just yet, this ensures that any further references to
198 // indices from the old fragment in this fragment do not insert any more
200 std::vector<uint64_t> &OldFragment = Fragments[OldFragmentIndex];
201 Fragment.insert(Fragment.end(), OldFragment.begin(), OldFragment.end());
206 // Update the fragment map to point our object indices to this fragment.
207 for (uint64_t ObjIndex : Fragment)
208 FragmentMap[ObjIndex] = FragmentIndex;
211 void ByteArrayBuilder::allocate(const std::set<uint64_t> &Bits,
212 uint64_t BitSize, uint64_t &AllocByteOffset,
213 uint8_t &AllocMask) {
214 // Find the smallest current allocation.
216 for (unsigned I = 1; I != BitsPerByte; ++I)
217 if (BitAllocs[I] < BitAllocs[Bit])
220 AllocByteOffset = BitAllocs[Bit];
222 // Add our size to it.
223 unsigned ReqSize = AllocByteOffset + BitSize;
224 BitAllocs[Bit] = ReqSize;
225 if (Bytes.size() < ReqSize)
226 Bytes.resize(ReqSize);
229 AllocMask = 1 << Bit;
230 for (uint64_t B : Bits)
231 Bytes[AllocByteOffset + B] |= AllocMask;
236 struct ByteArrayInfo {
237 std::set<uint64_t> Bits;
239 GlobalVariable *ByteArray;
240 GlobalVariable *MaskGlobal;
241 uint8_t *MaskPtr = nullptr;
244 /// A POD-like structure that we use to store a global reference together with
245 /// its metadata types. In this pass we frequently need to query the set of
246 /// metadata types referenced by a global, which at the IR level is an expensive
247 /// operation involving a map lookup; this data structure helps to reduce the
248 /// number of times we need to do this lookup.
249 class GlobalTypeMember final : TrailingObjects<GlobalTypeMember, MDNode *> {
250 friend TrailingObjects;
255 // For functions: true if this is a definition (either in the merged module or
256 // in one of the thinlto modules).
259 // For functions: true if this function is either defined or used in a thinlto
260 // module and its jumptable entry needs to be exported to thinlto backends.
263 size_t numTrailingObjects(OverloadToken<MDNode *>) const { return NTypes; }
266 static GlobalTypeMember *create(BumpPtrAllocator &Alloc, GlobalObject *GO,
267 bool IsDefinition, bool IsExported,
268 ArrayRef<MDNode *> Types) {
269 auto *GTM = static_cast<GlobalTypeMember *>(Alloc.Allocate(
270 totalSizeToAlloc<MDNode *>(Types.size()), alignof(GlobalTypeMember)));
272 GTM->NTypes = Types.size();
273 GTM->IsDefinition = IsDefinition;
274 GTM->IsExported = IsExported;
275 std::uninitialized_copy(Types.begin(), Types.end(),
276 GTM->getTrailingObjects<MDNode *>());
280 GlobalObject *getGlobal() const {
284 bool isDefinition() const {
288 bool isExported() const {
292 ArrayRef<MDNode *> types() const {
293 return makeArrayRef(getTrailingObjects<MDNode *>(), NTypes);
297 struct ICallBranchFunnel final
298 : TrailingObjects<ICallBranchFunnel, GlobalTypeMember *> {
299 static ICallBranchFunnel *create(BumpPtrAllocator &Alloc, CallInst *CI,
300 ArrayRef<GlobalTypeMember *> Targets,
302 auto *Call = static_cast<ICallBranchFunnel *>(
303 Alloc.Allocate(totalSizeToAlloc<GlobalTypeMember *>(Targets.size()),
304 alignof(ICallBranchFunnel)));
306 Call->UniqueId = UniqueId;
307 Call->NTargets = Targets.size();
308 std::uninitialized_copy(Targets.begin(), Targets.end(),
309 Call->getTrailingObjects<GlobalTypeMember *>());
314 ArrayRef<GlobalTypeMember *> targets() const {
315 return makeArrayRef(getTrailingObjects<GlobalTypeMember *>(), NTargets);
324 class LowerTypeTestsModule {
327 ModuleSummaryIndex *ExportSummary;
328 const ModuleSummaryIndex *ImportSummary;
330 Triple::ArchType Arch;
332 Triple::ObjectFormatType ObjectFormat;
334 IntegerType *Int1Ty = Type::getInt1Ty(M.getContext());
335 IntegerType *Int8Ty = Type::getInt8Ty(M.getContext());
336 PointerType *Int8PtrTy = Type::getInt8PtrTy(M.getContext());
337 ArrayType *Int8Arr0Ty = ArrayType::get(Type::getInt8Ty(M.getContext()), 0);
338 IntegerType *Int32Ty = Type::getInt32Ty(M.getContext());
339 PointerType *Int32PtrTy = PointerType::getUnqual(Int32Ty);
340 IntegerType *Int64Ty = Type::getInt64Ty(M.getContext());
341 IntegerType *IntPtrTy = M.getDataLayout().getIntPtrType(M.getContext(), 0);
343 // Indirect function call index assignment counter for WebAssembly
344 uint64_t IndirectIndex = 1;
346 // Mapping from type identifiers to the call sites that test them, as well as
347 // whether the type identifier needs to be exported to ThinLTO backends as
348 // part of the regular LTO phase of the ThinLTO pipeline (see exportTypeId).
349 struct TypeIdUserInfo {
350 std::vector<CallInst *> CallSites;
351 bool IsExported = false;
353 DenseMap<Metadata *, TypeIdUserInfo> TypeIdUsers;
355 /// This structure describes how to lower type tests for a particular type
356 /// identifier. It is either built directly from the global analysis (during
357 /// regular LTO or the regular LTO phase of ThinLTO), or indirectly using type
358 /// identifier summaries and external symbol references (in ThinLTO backends).
359 struct TypeIdLowering {
360 TypeTestResolution::Kind TheKind = TypeTestResolution::Unsat;
362 /// All except Unsat: the start address within the combined global.
363 Constant *OffsetedGlobal;
365 /// ByteArray, Inline, AllOnes: log2 of the required global alignment
366 /// relative to the start address.
369 /// ByteArray, Inline, AllOnes: one less than the size of the memory region
370 /// covering members of this type identifier as a multiple of 2^AlignLog2.
373 /// ByteArray: the byte array to test the address against.
374 Constant *TheByteArray;
376 /// ByteArray: the bit mask to apply to bytes loaded from the byte array.
379 /// Inline: the bit mask to test the address against.
380 Constant *InlineBits;
383 std::vector<ByteArrayInfo> ByteArrayInfos;
385 Function *WeakInitializerFn = nullptr;
387 bool shouldExportConstantsAsAbsoluteSymbols();
388 uint8_t *exportTypeId(StringRef TypeId, const TypeIdLowering &TIL);
389 TypeIdLowering importTypeId(StringRef TypeId);
390 void importTypeTest(CallInst *CI);
391 void importFunction(Function *F, bool isDefinition);
394 buildBitSet(Metadata *TypeId,
395 const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout);
396 ByteArrayInfo *createByteArray(BitSetInfo &BSI);
397 void allocateByteArrays();
398 Value *createBitSetTest(IRBuilder<> &B, const TypeIdLowering &TIL,
400 void lowerTypeTestCalls(
401 ArrayRef<Metadata *> TypeIds, Constant *CombinedGlobalAddr,
402 const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout);
403 Value *lowerTypeTestCall(Metadata *TypeId, CallInst *CI,
404 const TypeIdLowering &TIL);
406 void buildBitSetsFromGlobalVariables(ArrayRef<Metadata *> TypeIds,
407 ArrayRef<GlobalTypeMember *> Globals);
408 unsigned getJumpTableEntrySize();
409 Type *getJumpTableEntryType();
410 void createJumpTableEntry(raw_ostream &AsmOS, raw_ostream &ConstraintOS,
411 Triple::ArchType JumpTableArch,
412 SmallVectorImpl<Value *> &AsmArgs, Function *Dest);
413 void verifyTypeMDNode(GlobalObject *GO, MDNode *Type);
414 void buildBitSetsFromFunctions(ArrayRef<Metadata *> TypeIds,
415 ArrayRef<GlobalTypeMember *> Functions);
416 void buildBitSetsFromFunctionsNative(ArrayRef<Metadata *> TypeIds,
417 ArrayRef<GlobalTypeMember *> Functions);
418 void buildBitSetsFromFunctionsWASM(ArrayRef<Metadata *> TypeIds,
419 ArrayRef<GlobalTypeMember *> Functions);
421 buildBitSetsFromDisjointSet(ArrayRef<Metadata *> TypeIds,
422 ArrayRef<GlobalTypeMember *> Globals,
423 ArrayRef<ICallBranchFunnel *> ICallBranchFunnels);
425 void replaceWeakDeclarationWithJumpTablePtr(Function *F, Constant *JT, bool IsDefinition);
426 void moveInitializerToModuleConstructor(GlobalVariable *GV);
427 void findGlobalVariableUsersOf(Constant *C,
428 SmallSetVector<GlobalVariable *, 8> &Out);
430 void createJumpTable(Function *F, ArrayRef<GlobalTypeMember *> Functions);
432 /// replaceCfiUses - Go through the uses list for this definition
433 /// and make each use point to "V" instead of "this" when the use is outside
434 /// the block. 'This's use list is expected to have at least one element.
435 /// Unlike replaceAllUsesWith this function skips blockaddr and direct call
437 void replaceCfiUses(Function *Old, Value *New, bool IsDefinition);
439 /// replaceDirectCalls - Go through the uses list for this definition and
440 /// replace each use, which is a direct function call.
441 void replaceDirectCalls(Value *Old, Value *New);
444 LowerTypeTestsModule(Module &M, ModuleSummaryIndex *ExportSummary,
445 const ModuleSummaryIndex *ImportSummary);
449 // Lower the module using the action and summary passed as command line
450 // arguments. For testing purposes only.
451 static bool runForTesting(Module &M);
454 struct LowerTypeTests : public ModulePass {
457 bool UseCommandLine = false;
459 ModuleSummaryIndex *ExportSummary;
460 const ModuleSummaryIndex *ImportSummary;
462 LowerTypeTests() : ModulePass(ID), UseCommandLine(true) {
463 initializeLowerTypeTestsPass(*PassRegistry::getPassRegistry());
466 LowerTypeTests(ModuleSummaryIndex *ExportSummary,
467 const ModuleSummaryIndex *ImportSummary)
468 : ModulePass(ID), ExportSummary(ExportSummary),
469 ImportSummary(ImportSummary) {
470 initializeLowerTypeTestsPass(*PassRegistry::getPassRegistry());
473 bool runOnModule(Module &M) override {
475 return LowerTypeTestsModule::runForTesting(M);
476 return LowerTypeTestsModule(M, ExportSummary, ImportSummary).lower();
480 } // end anonymous namespace
482 char LowerTypeTests::ID = 0;
484 INITIALIZE_PASS(LowerTypeTests, "lowertypetests", "Lower type metadata", false,
488 llvm::createLowerTypeTestsPass(ModuleSummaryIndex *ExportSummary,
489 const ModuleSummaryIndex *ImportSummary) {
490 return new LowerTypeTests(ExportSummary, ImportSummary);
493 /// Build a bit set for TypeId using the object layouts in
495 BitSetInfo LowerTypeTestsModule::buildBitSet(
497 const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout) {
500 // Compute the byte offset of each address associated with this type
502 for (auto &GlobalAndOffset : GlobalLayout) {
503 for (MDNode *Type : GlobalAndOffset.first->types()) {
504 if (Type->getOperand(1) != TypeId)
508 cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
510 BSB.addOffset(GlobalAndOffset.second + Offset);
517 /// Build a test that bit BitOffset mod sizeof(Bits)*8 is set in
518 /// Bits. This pattern matches to the bt instruction on x86.
519 static Value *createMaskedBitTest(IRBuilder<> &B, Value *Bits,
521 auto BitsType = cast<IntegerType>(Bits->getType());
522 unsigned BitWidth = BitsType->getBitWidth();
524 BitOffset = B.CreateZExtOrTrunc(BitOffset, BitsType);
526 B.CreateAnd(BitOffset, ConstantInt::get(BitsType, BitWidth - 1));
527 Value *BitMask = B.CreateShl(ConstantInt::get(BitsType, 1), BitIndex);
528 Value *MaskedBits = B.CreateAnd(Bits, BitMask);
529 return B.CreateICmpNE(MaskedBits, ConstantInt::get(BitsType, 0));
532 ByteArrayInfo *LowerTypeTestsModule::createByteArray(BitSetInfo &BSI) {
533 // Create globals to stand in for byte arrays and masks. These never actually
534 // get initialized, we RAUW and erase them later in allocateByteArrays() once
535 // we know the offset and mask to use.
536 auto ByteArrayGlobal = new GlobalVariable(
537 M, Int8Ty, /*isConstant=*/true, GlobalValue::PrivateLinkage, nullptr);
538 auto MaskGlobal = new GlobalVariable(M, Int8Ty, /*isConstant=*/true,
539 GlobalValue::PrivateLinkage, nullptr);
541 ByteArrayInfos.emplace_back();
542 ByteArrayInfo *BAI = &ByteArrayInfos.back();
544 BAI->Bits = BSI.Bits;
545 BAI->BitSize = BSI.BitSize;
546 BAI->ByteArray = ByteArrayGlobal;
547 BAI->MaskGlobal = MaskGlobal;
551 void LowerTypeTestsModule::allocateByteArrays() {
552 std::stable_sort(ByteArrayInfos.begin(), ByteArrayInfos.end(),
553 [](const ByteArrayInfo &BAI1, const ByteArrayInfo &BAI2) {
554 return BAI1.BitSize > BAI2.BitSize;
557 std::vector<uint64_t> ByteArrayOffsets(ByteArrayInfos.size());
559 ByteArrayBuilder BAB;
560 for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) {
561 ByteArrayInfo *BAI = &ByteArrayInfos[I];
564 BAB.allocate(BAI->Bits, BAI->BitSize, ByteArrayOffsets[I], Mask);
566 BAI->MaskGlobal->replaceAllUsesWith(
567 ConstantExpr::getIntToPtr(ConstantInt::get(Int8Ty, Mask), Int8PtrTy));
568 BAI->MaskGlobal->eraseFromParent();
570 *BAI->MaskPtr = Mask;
573 Constant *ByteArrayConst = ConstantDataArray::get(M.getContext(), BAB.Bytes);
575 new GlobalVariable(M, ByteArrayConst->getType(), /*isConstant=*/true,
576 GlobalValue::PrivateLinkage, ByteArrayConst);
578 for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) {
579 ByteArrayInfo *BAI = &ByteArrayInfos[I];
581 Constant *Idxs[] = {ConstantInt::get(IntPtrTy, 0),
582 ConstantInt::get(IntPtrTy, ByteArrayOffsets[I])};
583 Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(
584 ByteArrayConst->getType(), ByteArray, Idxs);
586 // Create an alias instead of RAUW'ing the gep directly. On x86 this ensures
587 // that the pc-relative displacement is folded into the lea instead of the
588 // test instruction getting another displacement.
589 GlobalAlias *Alias = GlobalAlias::create(
590 Int8Ty, 0, GlobalValue::PrivateLinkage, "bits", GEP, &M);
591 BAI->ByteArray->replaceAllUsesWith(Alias);
592 BAI->ByteArray->eraseFromParent();
595 ByteArraySizeBits = BAB.BitAllocs[0] + BAB.BitAllocs[1] + BAB.BitAllocs[2] +
596 BAB.BitAllocs[3] + BAB.BitAllocs[4] + BAB.BitAllocs[5] +
597 BAB.BitAllocs[6] + BAB.BitAllocs[7];
598 ByteArraySizeBytes = BAB.Bytes.size();
601 /// Build a test that bit BitOffset is set in the type identifier that was
602 /// lowered to TIL, which must be either an Inline or a ByteArray.
603 Value *LowerTypeTestsModule::createBitSetTest(IRBuilder<> &B,
604 const TypeIdLowering &TIL,
606 if (TIL.TheKind == TypeTestResolution::Inline) {
607 // If the bit set is sufficiently small, we can avoid a load by bit testing
609 return createMaskedBitTest(B, TIL.InlineBits, BitOffset);
611 Constant *ByteArray = TIL.TheByteArray;
612 if (AvoidReuse && !ImportSummary) {
613 // Each use of the byte array uses a different alias. This makes the
614 // backend less likely to reuse previously computed byte array addresses,
615 // improving the security of the CFI mechanism based on this pass.
616 // This won't work when importing because TheByteArray is external.
617 ByteArray = GlobalAlias::create(Int8Ty, 0, GlobalValue::PrivateLinkage,
618 "bits_use", ByteArray, &M);
621 Value *ByteAddr = B.CreateGEP(Int8Ty, ByteArray, BitOffset);
622 Value *Byte = B.CreateLoad(ByteAddr);
625 B.CreateAnd(Byte, ConstantExpr::getPtrToInt(TIL.BitMask, Int8Ty));
626 return B.CreateICmpNE(ByteAndMask, ConstantInt::get(Int8Ty, 0));
630 static bool isKnownTypeIdMember(Metadata *TypeId, const DataLayout &DL,
631 Value *V, uint64_t COffset) {
632 if (auto GV = dyn_cast<GlobalObject>(V)) {
633 SmallVector<MDNode *, 2> Types;
634 GV->getMetadata(LLVMContext::MD_type, Types);
635 for (MDNode *Type : Types) {
636 if (Type->getOperand(1) != TypeId)
640 cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
642 if (COffset == Offset)
648 if (auto GEP = dyn_cast<GEPOperator>(V)) {
649 APInt APOffset(DL.getPointerSizeInBits(0), 0);
650 bool Result = GEP->accumulateConstantOffset(DL, APOffset);
653 COffset += APOffset.getZExtValue();
654 return isKnownTypeIdMember(TypeId, DL, GEP->getPointerOperand(), COffset);
657 if (auto Op = dyn_cast<Operator>(V)) {
658 if (Op->getOpcode() == Instruction::BitCast)
659 return isKnownTypeIdMember(TypeId, DL, Op->getOperand(0), COffset);
661 if (Op->getOpcode() == Instruction::Select)
662 return isKnownTypeIdMember(TypeId, DL, Op->getOperand(1), COffset) &&
663 isKnownTypeIdMember(TypeId, DL, Op->getOperand(2), COffset);
669 /// Lower a llvm.type.test call to its implementation. Returns the value to
670 /// replace the call with.
671 Value *LowerTypeTestsModule::lowerTypeTestCall(Metadata *TypeId, CallInst *CI,
672 const TypeIdLowering &TIL) {
673 if (TIL.TheKind == TypeTestResolution::Unsat)
674 return ConstantInt::getFalse(M.getContext());
676 Value *Ptr = CI->getArgOperand(0);
677 const DataLayout &DL = M.getDataLayout();
678 if (isKnownTypeIdMember(TypeId, DL, Ptr, 0))
679 return ConstantInt::getTrue(M.getContext());
681 BasicBlock *InitialBB = CI->getParent();
685 Value *PtrAsInt = B.CreatePtrToInt(Ptr, IntPtrTy);
687 Constant *OffsetedGlobalAsInt =
688 ConstantExpr::getPtrToInt(TIL.OffsetedGlobal, IntPtrTy);
689 if (TIL.TheKind == TypeTestResolution::Single)
690 return B.CreateICmpEQ(PtrAsInt, OffsetedGlobalAsInt);
692 Value *PtrOffset = B.CreateSub(PtrAsInt, OffsetedGlobalAsInt);
694 // We need to check that the offset both falls within our range and is
695 // suitably aligned. We can check both properties at the same time by
696 // performing a right rotate by log2(alignment) followed by an integer
697 // comparison against the bitset size. The rotate will move the lower
698 // order bits that need to be zero into the higher order bits of the
699 // result, causing the comparison to fail if they are nonzero. The rotate
700 // also conveniently gives us a bit offset to use during the load from
703 B.CreateLShr(PtrOffset, ConstantExpr::getZExt(TIL.AlignLog2, IntPtrTy));
704 Value *OffsetSHL = B.CreateShl(
705 PtrOffset, ConstantExpr::getZExt(
706 ConstantExpr::getSub(
707 ConstantInt::get(Int8Ty, DL.getPointerSizeInBits(0)),
710 Value *BitOffset = B.CreateOr(OffsetSHR, OffsetSHL);
712 Value *OffsetInRange = B.CreateICmpULE(BitOffset, TIL.SizeM1);
714 // If the bit set is all ones, testing against it is unnecessary.
715 if (TIL.TheKind == TypeTestResolution::AllOnes)
716 return OffsetInRange;
718 // See if the intrinsic is used in the following common pattern:
719 // br(llvm.type.test(...), thenbb, elsebb)
720 // where nothing happens between the type test and the br.
721 // If so, create slightly simpler IR.
723 if (auto *Br = dyn_cast<BranchInst>(*CI->user_begin()))
724 if (CI->getNextNode() == Br) {
725 BasicBlock *Then = InitialBB->splitBasicBlock(CI->getIterator());
726 BasicBlock *Else = Br->getSuccessor(1);
727 BranchInst *NewBr = BranchInst::Create(Then, Else, OffsetInRange);
728 NewBr->setMetadata(LLVMContext::MD_prof,
729 Br->getMetadata(LLVMContext::MD_prof));
730 ReplaceInstWithInst(InitialBB->getTerminator(), NewBr);
732 // Update phis in Else resulting from InitialBB being split
733 for (auto &Phi : Else->phis())
734 Phi.addIncoming(Phi.getIncomingValueForBlock(Then), InitialBB);
736 IRBuilder<> ThenB(CI);
737 return createBitSetTest(ThenB, TIL, BitOffset);
740 IRBuilder<> ThenB(SplitBlockAndInsertIfThen(OffsetInRange, CI, false));
742 // Now that we know that the offset is in range and aligned, load the
743 // appropriate bit from the bitset.
744 Value *Bit = createBitSetTest(ThenB, TIL, BitOffset);
746 // The value we want is 0 if we came directly from the initial block
747 // (having failed the range or alignment checks), or the loaded bit if
748 // we came from the block in which we loaded it.
749 B.SetInsertPoint(CI);
750 PHINode *P = B.CreatePHI(Int1Ty, 2);
751 P->addIncoming(ConstantInt::get(Int1Ty, 0), InitialBB);
752 P->addIncoming(Bit, ThenB.GetInsertBlock());
756 /// Given a disjoint set of type identifiers and globals, lay out the globals,
757 /// build the bit sets and lower the llvm.type.test calls.
758 void LowerTypeTestsModule::buildBitSetsFromGlobalVariables(
759 ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Globals) {
760 // Build a new global with the combined contents of the referenced globals.
761 // This global is a struct whose even-indexed elements contain the original
762 // contents of the referenced globals and whose odd-indexed elements contain
763 // any padding required to align the next element to the next power of 2.
764 std::vector<Constant *> GlobalInits;
765 const DataLayout &DL = M.getDataLayout();
766 for (GlobalTypeMember *G : Globals) {
767 GlobalVariable *GV = cast<GlobalVariable>(G->getGlobal());
768 GlobalInits.push_back(GV->getInitializer());
769 uint64_t InitSize = DL.getTypeAllocSize(GV->getValueType());
771 // Compute the amount of padding required.
772 uint64_t Padding = NextPowerOf2(InitSize - 1) - InitSize;
774 // Experiments of different caps with Chromium on both x64 and ARM64
775 // have shown that the 32-byte cap generates the smallest binary on
776 // both platforms while different caps yield similar performance.
777 // (see https://lists.llvm.org/pipermail/llvm-dev/2018-July/124694.html)
779 Padding = alignTo(InitSize, 32) - InitSize;
781 GlobalInits.push_back(
782 ConstantAggregateZero::get(ArrayType::get(Int8Ty, Padding)));
784 if (!GlobalInits.empty())
785 GlobalInits.pop_back();
786 Constant *NewInit = ConstantStruct::getAnon(M.getContext(), GlobalInits);
787 auto *CombinedGlobal =
788 new GlobalVariable(M, NewInit->getType(), /*isConstant=*/true,
789 GlobalValue::PrivateLinkage, NewInit);
791 StructType *NewTy = cast<StructType>(NewInit->getType());
792 const StructLayout *CombinedGlobalLayout = DL.getStructLayout(NewTy);
794 // Compute the offsets of the original globals within the new global.
795 DenseMap<GlobalTypeMember *, uint64_t> GlobalLayout;
796 for (unsigned I = 0; I != Globals.size(); ++I)
797 // Multiply by 2 to account for padding elements.
798 GlobalLayout[Globals[I]] = CombinedGlobalLayout->getElementOffset(I * 2);
800 lowerTypeTestCalls(TypeIds, CombinedGlobal, GlobalLayout);
802 // Build aliases pointing to offsets into the combined global for each
803 // global from which we built the combined global, and replace references
804 // to the original globals with references to the aliases.
805 for (unsigned I = 0; I != Globals.size(); ++I) {
806 GlobalVariable *GV = cast<GlobalVariable>(Globals[I]->getGlobal());
808 // Multiply by 2 to account for padding elements.
809 Constant *CombinedGlobalIdxs[] = {ConstantInt::get(Int32Ty, 0),
810 ConstantInt::get(Int32Ty, I * 2)};
811 Constant *CombinedGlobalElemPtr = ConstantExpr::getGetElementPtr(
812 NewInit->getType(), CombinedGlobal, CombinedGlobalIdxs);
813 assert(GV->getType()->getAddressSpace() == 0);
814 GlobalAlias *GAlias =
815 GlobalAlias::create(NewTy->getElementType(I * 2), 0, GV->getLinkage(),
816 "", CombinedGlobalElemPtr, &M);
817 GAlias->setVisibility(GV->getVisibility());
818 GAlias->takeName(GV);
819 GV->replaceAllUsesWith(GAlias);
820 GV->eraseFromParent();
824 bool LowerTypeTestsModule::shouldExportConstantsAsAbsoluteSymbols() {
825 return (Arch == Triple::x86 || Arch == Triple::x86_64) &&
826 ObjectFormat == Triple::ELF;
829 /// Export the given type identifier so that ThinLTO backends may import it.
830 /// Type identifiers are exported by adding coarse-grained information about how
831 /// to test the type identifier to the summary, and creating symbols in the
832 /// object file (aliases and absolute symbols) containing fine-grained
833 /// information about the type identifier.
835 /// Returns a pointer to the location in which to store the bitmask, if
837 uint8_t *LowerTypeTestsModule::exportTypeId(StringRef TypeId,
838 const TypeIdLowering &TIL) {
839 TypeTestResolution &TTRes =
840 ExportSummary->getOrInsertTypeIdSummary(TypeId).TTRes;
841 TTRes.TheKind = TIL.TheKind;
843 auto ExportGlobal = [&](StringRef Name, Constant *C) {
845 GlobalAlias::create(Int8Ty, 0, GlobalValue::ExternalLinkage,
846 "__typeid_" + TypeId + "_" + Name, C, &M);
847 GA->setVisibility(GlobalValue::HiddenVisibility);
850 auto ExportConstant = [&](StringRef Name, uint64_t &Storage, Constant *C) {
851 if (shouldExportConstantsAsAbsoluteSymbols())
852 ExportGlobal(Name, ConstantExpr::getIntToPtr(C, Int8PtrTy));
854 Storage = cast<ConstantInt>(C)->getZExtValue();
857 if (TIL.TheKind != TypeTestResolution::Unsat)
858 ExportGlobal("global_addr", TIL.OffsetedGlobal);
860 if (TIL.TheKind == TypeTestResolution::ByteArray ||
861 TIL.TheKind == TypeTestResolution::Inline ||
862 TIL.TheKind == TypeTestResolution::AllOnes) {
863 ExportConstant("align", TTRes.AlignLog2, TIL.AlignLog2);
864 ExportConstant("size_m1", TTRes.SizeM1, TIL.SizeM1);
866 uint64_t BitSize = cast<ConstantInt>(TIL.SizeM1)->getZExtValue() + 1;
867 if (TIL.TheKind == TypeTestResolution::Inline)
868 TTRes.SizeM1BitWidth = (BitSize <= 32) ? 5 : 6;
870 TTRes.SizeM1BitWidth = (BitSize <= 128) ? 7 : 32;
873 if (TIL.TheKind == TypeTestResolution::ByteArray) {
874 ExportGlobal("byte_array", TIL.TheByteArray);
875 if (shouldExportConstantsAsAbsoluteSymbols())
876 ExportGlobal("bit_mask", TIL.BitMask);
878 return &TTRes.BitMask;
881 if (TIL.TheKind == TypeTestResolution::Inline)
882 ExportConstant("inline_bits", TTRes.InlineBits, TIL.InlineBits);
887 LowerTypeTestsModule::TypeIdLowering
888 LowerTypeTestsModule::importTypeId(StringRef TypeId) {
889 const TypeIdSummary *TidSummary = ImportSummary->getTypeIdSummary(TypeId);
891 return {}; // Unsat: no globals match this type id.
892 const TypeTestResolution &TTRes = TidSummary->TTRes;
895 TIL.TheKind = TTRes.TheKind;
897 auto ImportGlobal = [&](StringRef Name) {
898 // Give the global a type of length 0 so that it is not assumed not to alias
899 // with any other global.
900 Constant *C = M.getOrInsertGlobal(("__typeid_" + TypeId + "_" + Name).str(),
902 if (auto *GV = dyn_cast<GlobalVariable>(C))
903 GV->setVisibility(GlobalValue::HiddenVisibility);
904 C = ConstantExpr::getBitCast(C, Int8PtrTy);
908 auto ImportConstant = [&](StringRef Name, uint64_t Const, unsigned AbsWidth,
910 if (!shouldExportConstantsAsAbsoluteSymbols()) {
912 ConstantInt::get(isa<IntegerType>(Ty) ? Ty : Int64Ty, Const);
913 if (!isa<IntegerType>(Ty))
914 C = ConstantExpr::getIntToPtr(C, Ty);
918 Constant *C = ImportGlobal(Name);
919 auto *GV = cast<GlobalVariable>(C->stripPointerCasts());
920 if (isa<IntegerType>(Ty))
921 C = ConstantExpr::getPtrToInt(C, Ty);
922 if (GV->getMetadata(LLVMContext::MD_absolute_symbol))
925 auto SetAbsRange = [&](uint64_t Min, uint64_t Max) {
926 auto *MinC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Min));
927 auto *MaxC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Max));
928 GV->setMetadata(LLVMContext::MD_absolute_symbol,
929 MDNode::get(M.getContext(), {MinC, MaxC}));
931 if (AbsWidth == IntPtrTy->getBitWidth())
932 SetAbsRange(~0ull, ~0ull); // Full set.
934 SetAbsRange(0, 1ull << AbsWidth);
938 if (TIL.TheKind != TypeTestResolution::Unsat)
939 TIL.OffsetedGlobal = ImportGlobal("global_addr");
941 if (TIL.TheKind == TypeTestResolution::ByteArray ||
942 TIL.TheKind == TypeTestResolution::Inline ||
943 TIL.TheKind == TypeTestResolution::AllOnes) {
944 TIL.AlignLog2 = ImportConstant("align", TTRes.AlignLog2, 8, Int8Ty);
946 ImportConstant("size_m1", TTRes.SizeM1, TTRes.SizeM1BitWidth, IntPtrTy);
949 if (TIL.TheKind == TypeTestResolution::ByteArray) {
950 TIL.TheByteArray = ImportGlobal("byte_array");
951 TIL.BitMask = ImportConstant("bit_mask", TTRes.BitMask, 8, Int8PtrTy);
954 if (TIL.TheKind == TypeTestResolution::Inline)
955 TIL.InlineBits = ImportConstant(
956 "inline_bits", TTRes.InlineBits, 1 << TTRes.SizeM1BitWidth,
957 TTRes.SizeM1BitWidth <= 5 ? Int32Ty : Int64Ty);
962 void LowerTypeTestsModule::importTypeTest(CallInst *CI) {
963 auto TypeIdMDVal = dyn_cast<MetadataAsValue>(CI->getArgOperand(1));
965 report_fatal_error("Second argument of llvm.type.test must be metadata");
967 auto TypeIdStr = dyn_cast<MDString>(TypeIdMDVal->getMetadata());
970 "Second argument of llvm.type.test must be a metadata string");
972 TypeIdLowering TIL = importTypeId(TypeIdStr->getString());
973 Value *Lowered = lowerTypeTestCall(TypeIdStr, CI, TIL);
974 CI->replaceAllUsesWith(Lowered);
975 CI->eraseFromParent();
978 // ThinLTO backend: the function F has a jump table entry; update this module
979 // accordingly. isDefinition describes the type of the jump table entry.
980 void LowerTypeTestsModule::importFunction(Function *F, bool isDefinition) {
981 assert(F->getType()->getAddressSpace() == 0);
983 GlobalValue::VisibilityTypes Visibility = F->getVisibility();
984 std::string Name = F->getName();
986 if (F->isDeclarationForLinker() && isDefinition) {
987 // Non-dso_local functions may be overriden at run time,
988 // don't short curcuit them
989 if (F->isDSOLocal()) {
990 Function *RealF = Function::Create(F->getFunctionType(),
991 GlobalValue::ExternalLinkage,
992 F->getAddressSpace(),
994 RealF->setVisibility(GlobalVariable::HiddenVisibility);
995 replaceDirectCalls(F, RealF);
1001 if (F->isDeclarationForLinker() && !isDefinition) {
1002 // Declaration of an external function.
1003 FDecl = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage,
1004 F->getAddressSpace(), Name + ".cfi_jt", &M);
1005 FDecl->setVisibility(GlobalValue::HiddenVisibility);
1006 } else if (isDefinition) {
1007 F->setName(Name + ".cfi");
1008 F->setLinkage(GlobalValue::ExternalLinkage);
1009 FDecl = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage,
1010 F->getAddressSpace(), Name, &M);
1011 FDecl->setVisibility(Visibility);
1012 Visibility = GlobalValue::HiddenVisibility;
1014 // Delete aliases pointing to this function, they'll be re-created in the
1016 SmallVector<GlobalAlias*, 4> ToErase;
1017 for (auto &U : F->uses()) {
1018 if (auto *A = dyn_cast<GlobalAlias>(U.getUser())) {
1019 Function *AliasDecl = Function::Create(
1020 F->getFunctionType(), GlobalValue::ExternalLinkage,
1021 F->getAddressSpace(), "", &M);
1022 AliasDecl->takeName(A);
1023 A->replaceAllUsesWith(AliasDecl);
1024 ToErase.push_back(A);
1027 for (auto *A : ToErase)
1028 A->eraseFromParent();
1030 // Function definition without type metadata, where some other translation
1031 // unit contained a declaration with type metadata. This normally happens
1032 // during mixed CFI + non-CFI compilation. We do nothing with the function
1033 // so that it is treated the same way as a function defined outside of the
1038 if (F->isWeakForLinker())
1039 replaceWeakDeclarationWithJumpTablePtr(F, FDecl, isDefinition);
1041 replaceCfiUses(F, FDecl, isDefinition);
1043 // Set visibility late because it's used in replaceCfiUses() to determine
1044 // whether uses need to to be replaced.
1045 F->setVisibility(Visibility);
1048 void LowerTypeTestsModule::lowerTypeTestCalls(
1049 ArrayRef<Metadata *> TypeIds, Constant *CombinedGlobalAddr,
1050 const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout) {
1051 CombinedGlobalAddr = ConstantExpr::getBitCast(CombinedGlobalAddr, Int8PtrTy);
1053 // For each type identifier in this disjoint set...
1054 for (Metadata *TypeId : TypeIds) {
1055 // Build the bitset.
1056 BitSetInfo BSI = buildBitSet(TypeId, GlobalLayout);
1058 if (auto MDS = dyn_cast<MDString>(TypeId))
1059 dbgs() << MDS->getString() << ": ";
1061 dbgs() << "<unnamed>: ";
1065 ByteArrayInfo *BAI = nullptr;
1067 TIL.OffsetedGlobal = ConstantExpr::getGetElementPtr(
1068 Int8Ty, CombinedGlobalAddr, ConstantInt::get(IntPtrTy, BSI.ByteOffset)),
1069 TIL.AlignLog2 = ConstantInt::get(Int8Ty, BSI.AlignLog2);
1070 TIL.SizeM1 = ConstantInt::get(IntPtrTy, BSI.BitSize - 1);
1071 if (BSI.isAllOnes()) {
1072 TIL.TheKind = (BSI.BitSize == 1) ? TypeTestResolution::Single
1073 : TypeTestResolution::AllOnes;
1074 } else if (BSI.BitSize <= 64) {
1075 TIL.TheKind = TypeTestResolution::Inline;
1076 uint64_t InlineBits = 0;
1077 for (auto Bit : BSI.Bits)
1078 InlineBits |= uint64_t(1) << Bit;
1079 if (InlineBits == 0)
1080 TIL.TheKind = TypeTestResolution::Unsat;
1082 TIL.InlineBits = ConstantInt::get(
1083 (BSI.BitSize <= 32) ? Int32Ty : Int64Ty, InlineBits);
1085 TIL.TheKind = TypeTestResolution::ByteArray;
1086 ++NumByteArraysCreated;
1087 BAI = createByteArray(BSI);
1088 TIL.TheByteArray = BAI->ByteArray;
1089 TIL.BitMask = BAI->MaskGlobal;
1092 TypeIdUserInfo &TIUI = TypeIdUsers[TypeId];
1094 if (TIUI.IsExported) {
1095 uint8_t *MaskPtr = exportTypeId(cast<MDString>(TypeId)->getString(), TIL);
1097 BAI->MaskPtr = MaskPtr;
1100 // Lower each call to llvm.type.test for this type identifier.
1101 for (CallInst *CI : TIUI.CallSites) {
1102 ++NumTypeTestCallsLowered;
1103 Value *Lowered = lowerTypeTestCall(TypeId, CI, TIL);
1104 CI->replaceAllUsesWith(Lowered);
1105 CI->eraseFromParent();
1110 void LowerTypeTestsModule::verifyTypeMDNode(GlobalObject *GO, MDNode *Type) {
1111 if (Type->getNumOperands() != 2)
1112 report_fatal_error("All operands of type metadata must have 2 elements");
1114 if (GO->isThreadLocal())
1115 report_fatal_error("Bit set element may not be thread-local");
1116 if (isa<GlobalVariable>(GO) && GO->hasSection())
1118 "A member of a type identifier may not have an explicit section");
1120 // FIXME: We previously checked that global var member of a type identifier
1121 // must be a definition, but the IR linker may leave type metadata on
1122 // declarations. We should restore this check after fixing PR31759.
1124 auto OffsetConstMD = dyn_cast<ConstantAsMetadata>(Type->getOperand(0));
1126 report_fatal_error("Type offset must be a constant");
1127 auto OffsetInt = dyn_cast<ConstantInt>(OffsetConstMD->getValue());
1129 report_fatal_error("Type offset must be an integer constant");
1132 static const unsigned kX86JumpTableEntrySize = 8;
1133 static const unsigned kARMJumpTableEntrySize = 4;
1135 unsigned LowerTypeTestsModule::getJumpTableEntrySize() {
1138 case Triple::x86_64:
1139 return kX86JumpTableEntrySize;
1142 case Triple::aarch64:
1143 return kARMJumpTableEntrySize;
1145 report_fatal_error("Unsupported architecture for jump tables");
1149 // Create a jump table entry for the target. This consists of an instruction
1150 // sequence containing a relative branch to Dest. Appends inline asm text,
1151 // constraints and arguments to AsmOS, ConstraintOS and AsmArgs.
1152 void LowerTypeTestsModule::createJumpTableEntry(
1153 raw_ostream &AsmOS, raw_ostream &ConstraintOS,
1154 Triple::ArchType JumpTableArch, SmallVectorImpl<Value *> &AsmArgs,
1156 unsigned ArgIndex = AsmArgs.size();
1158 if (JumpTableArch == Triple::x86 || JumpTableArch == Triple::x86_64) {
1159 AsmOS << "jmp ${" << ArgIndex << ":c}@plt\n";
1160 AsmOS << "int3\nint3\nint3\n";
1161 } else if (JumpTableArch == Triple::arm || JumpTableArch == Triple::aarch64) {
1162 AsmOS << "b $" << ArgIndex << "\n";
1163 } else if (JumpTableArch == Triple::thumb) {
1164 AsmOS << "b.w $" << ArgIndex << "\n";
1166 report_fatal_error("Unsupported architecture for jump tables");
1169 ConstraintOS << (ArgIndex > 0 ? ",s" : "s");
1170 AsmArgs.push_back(Dest);
1173 Type *LowerTypeTestsModule::getJumpTableEntryType() {
1174 return ArrayType::get(Int8Ty, getJumpTableEntrySize());
1177 /// Given a disjoint set of type identifiers and functions, build the bit sets
1178 /// and lower the llvm.type.test calls, architecture dependently.
1179 void LowerTypeTestsModule::buildBitSetsFromFunctions(
1180 ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Functions) {
1181 if (Arch == Triple::x86 || Arch == Triple::x86_64 || Arch == Triple::arm ||
1182 Arch == Triple::thumb || Arch == Triple::aarch64)
1183 buildBitSetsFromFunctionsNative(TypeIds, Functions);
1184 else if (Arch == Triple::wasm32 || Arch == Triple::wasm64)
1185 buildBitSetsFromFunctionsWASM(TypeIds, Functions);
1187 report_fatal_error("Unsupported architecture for jump tables");
1190 void LowerTypeTestsModule::moveInitializerToModuleConstructor(
1191 GlobalVariable *GV) {
1192 if (WeakInitializerFn == nullptr) {
1193 WeakInitializerFn = Function::Create(
1194 FunctionType::get(Type::getVoidTy(M.getContext()),
1195 /* IsVarArg */ false),
1196 GlobalValue::InternalLinkage,
1197 M.getDataLayout().getProgramAddressSpace(),
1198 "__cfi_global_var_init", &M);
1200 BasicBlock::Create(M.getContext(), "entry", WeakInitializerFn);
1201 ReturnInst::Create(M.getContext(), BB);
1202 WeakInitializerFn->setSection(
1203 ObjectFormat == Triple::MachO
1204 ? "__TEXT,__StaticInit,regular,pure_instructions"
1206 // This code is equivalent to relocation application, and should run at the
1207 // earliest possible time (i.e. with the highest priority).
1208 appendToGlobalCtors(M, WeakInitializerFn, /* Priority */ 0);
1211 IRBuilder<> IRB(WeakInitializerFn->getEntryBlock().getTerminator());
1212 GV->setConstant(false);
1213 IRB.CreateAlignedStore(GV->getInitializer(), GV, GV->getAlignment());
1214 GV->setInitializer(Constant::getNullValue(GV->getValueType()));
1217 void LowerTypeTestsModule::findGlobalVariableUsersOf(
1218 Constant *C, SmallSetVector<GlobalVariable *, 8> &Out) {
1219 for (auto *U : C->users()){
1220 if (auto *GV = dyn_cast<GlobalVariable>(U))
1222 else if (auto *C2 = dyn_cast<Constant>(U))
1223 findGlobalVariableUsersOf(C2, Out);
1227 // Replace all uses of F with (F ? JT : 0).
1228 void LowerTypeTestsModule::replaceWeakDeclarationWithJumpTablePtr(
1229 Function *F, Constant *JT, bool IsDefinition) {
1230 // The target expression can not appear in a constant initializer on most
1231 // (all?) targets. Switch to a runtime initializer.
1232 SmallSetVector<GlobalVariable *, 8> GlobalVarUsers;
1233 findGlobalVariableUsersOf(F, GlobalVarUsers);
1234 for (auto GV : GlobalVarUsers)
1235 moveInitializerToModuleConstructor(GV);
1237 // Can not RAUW F with an expression that uses F. Replace with a temporary
1238 // placeholder first.
1239 Function *PlaceholderFn =
1240 Function::Create(cast<FunctionType>(F->getValueType()),
1241 GlobalValue::ExternalWeakLinkage,
1242 F->getAddressSpace(), "", &M);
1243 replaceCfiUses(F, PlaceholderFn, IsDefinition);
1245 Constant *Target = ConstantExpr::getSelect(
1246 ConstantExpr::getICmp(CmpInst::ICMP_NE, F,
1247 Constant::getNullValue(F->getType())),
1248 JT, Constant::getNullValue(F->getType()));
1249 PlaceholderFn->replaceAllUsesWith(Target);
1250 PlaceholderFn->eraseFromParent();
1253 static bool isThumbFunction(Function *F, Triple::ArchType ModuleArch) {
1254 Attribute TFAttr = F->getFnAttribute("target-features");
1255 if (!TFAttr.hasAttribute(Attribute::None)) {
1256 SmallVector<StringRef, 6> Features;
1257 TFAttr.getValueAsString().split(Features, ',');
1258 for (StringRef Feature : Features) {
1259 if (Feature == "-thumb-mode")
1261 else if (Feature == "+thumb-mode")
1266 return ModuleArch == Triple::thumb;
1269 // Each jump table must be either ARM or Thumb as a whole for the bit-test math
1270 // to work. Pick one that matches the majority of members to minimize interop
1271 // veneers inserted by the linker.
1272 static Triple::ArchType
1273 selectJumpTableArmEncoding(ArrayRef<GlobalTypeMember *> Functions,
1274 Triple::ArchType ModuleArch) {
1275 if (ModuleArch != Triple::arm && ModuleArch != Triple::thumb)
1278 unsigned ArmCount = 0, ThumbCount = 0;
1279 for (const auto GTM : Functions) {
1280 if (!GTM->isDefinition()) {
1281 // PLT stubs are always ARM.
1286 Function *F = cast<Function>(GTM->getGlobal());
1287 ++(isThumbFunction(F, ModuleArch) ? ThumbCount : ArmCount);
1290 return ArmCount > ThumbCount ? Triple::arm : Triple::thumb;
1293 void LowerTypeTestsModule::createJumpTable(
1294 Function *F, ArrayRef<GlobalTypeMember *> Functions) {
1295 std::string AsmStr, ConstraintStr;
1296 raw_string_ostream AsmOS(AsmStr), ConstraintOS(ConstraintStr);
1297 SmallVector<Value *, 16> AsmArgs;
1298 AsmArgs.reserve(Functions.size() * 2);
1300 Triple::ArchType JumpTableArch = selectJumpTableArmEncoding(Functions, Arch);
1302 for (unsigned I = 0; I != Functions.size(); ++I)
1303 createJumpTableEntry(AsmOS, ConstraintOS, JumpTableArch, AsmArgs,
1304 cast<Function>(Functions[I]->getGlobal()));
1306 // Align the whole table by entry size.
1307 F->setAlignment(getJumpTableEntrySize());
1309 // Disabled on win32 due to https://llvm.org/bugs/show_bug.cgi?id=28641#c3.
1310 // Luckily, this function does not get any prologue even without the
1312 if (OS != Triple::Win32)
1313 F->addFnAttr(Attribute::Naked);
1314 if (JumpTableArch == Triple::arm)
1315 F->addFnAttr("target-features", "-thumb-mode");
1316 if (JumpTableArch == Triple::thumb) {
1317 F->addFnAttr("target-features", "+thumb-mode");
1318 // Thumb jump table assembly needs Thumb2. The following attribute is added
1319 // by Clang for -march=armv7.
1320 F->addFnAttr("target-cpu", "cortex-a8");
1322 // Make sure we don't emit .eh_frame for this function.
1323 F->addFnAttr(Attribute::NoUnwind);
1325 BasicBlock *BB = BasicBlock::Create(M.getContext(), "entry", F);
1326 IRBuilder<> IRB(BB);
1328 SmallVector<Type *, 16> ArgTypes;
1329 ArgTypes.reserve(AsmArgs.size());
1330 for (const auto &Arg : AsmArgs)
1331 ArgTypes.push_back(Arg->getType());
1332 InlineAsm *JumpTableAsm =
1333 InlineAsm::get(FunctionType::get(IRB.getVoidTy(), ArgTypes, false),
1334 AsmOS.str(), ConstraintOS.str(),
1335 /*hasSideEffects=*/true);
1337 IRB.CreateCall(JumpTableAsm, AsmArgs);
1338 IRB.CreateUnreachable();
1341 /// Given a disjoint set of type identifiers and functions, build a jump table
1342 /// for the functions, build the bit sets and lower the llvm.type.test calls.
1343 void LowerTypeTestsModule::buildBitSetsFromFunctionsNative(
1344 ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Functions) {
1345 // Unlike the global bitset builder, the function bitset builder cannot
1346 // re-arrange functions in a particular order and base its calculations on the
1347 // layout of the functions' entry points, as we have no idea how large a
1348 // particular function will end up being (the size could even depend on what
1349 // this pass does!) Instead, we build a jump table, which is a block of code
1350 // consisting of one branch instruction for each of the functions in the bit
1351 // set that branches to the target function, and redirect any taken function
1352 // addresses to the corresponding jump table entry. In the object file's
1353 // symbol table, the symbols for the target functions also refer to the jump
1354 // table entries, so that addresses taken outside the module will pass any
1355 // verification done inside the module.
1357 // In more concrete terms, suppose we have three functions f, g, h which are
1358 // of the same type, and a function foo that returns their addresses:
1378 // We output the jump table as module-level inline asm string. The end result
1379 // will (conceptually) look like this:
1381 // f = .cfi.jumptable
1382 // g = .cfi.jumptable + 4
1383 // h = .cfi.jumptable + 8
1385 // jmp f.cfi ; 5 bytes
1389 // jmp g.cfi ; 5 bytes
1393 // jmp h.cfi ; 5 bytes
1416 // Because the addresses of f, g, h are evenly spaced at a power of 2, in the
1417 // normal case the check can be carried out using the same kind of simple
1418 // arithmetic that we normally use for globals.
1420 // FIXME: find a better way to represent the jumptable in the IR.
1421 assert(!Functions.empty());
1423 // Build a simple layout based on the regular layout of jump tables.
1424 DenseMap<GlobalTypeMember *, uint64_t> GlobalLayout;
1425 unsigned EntrySize = getJumpTableEntrySize();
1426 for (unsigned I = 0; I != Functions.size(); ++I)
1427 GlobalLayout[Functions[I]] = I * EntrySize;
1429 Function *JumpTableFn =
1430 Function::Create(FunctionType::get(Type::getVoidTy(M.getContext()),
1431 /* IsVarArg */ false),
1432 GlobalValue::PrivateLinkage,
1433 M.getDataLayout().getProgramAddressSpace(),
1434 ".cfi.jumptable", &M);
1435 ArrayType *JumpTableType =
1436 ArrayType::get(getJumpTableEntryType(), Functions.size());
1438 ConstantExpr::getPointerCast(JumpTableFn, JumpTableType->getPointerTo(0));
1440 lowerTypeTestCalls(TypeIds, JumpTable, GlobalLayout);
1442 // Build aliases pointing to offsets into the jump table, and replace
1443 // references to the original functions with references to the aliases.
1444 for (unsigned I = 0; I != Functions.size(); ++I) {
1445 Function *F = cast<Function>(Functions[I]->getGlobal());
1446 bool IsDefinition = Functions[I]->isDefinition();
1448 Constant *CombinedGlobalElemPtr = ConstantExpr::getBitCast(
1449 ConstantExpr::getInBoundsGetElementPtr(
1450 JumpTableType, JumpTable,
1451 ArrayRef<Constant *>{ConstantInt::get(IntPtrTy, 0),
1452 ConstantInt::get(IntPtrTy, I)}),
1454 if (Functions[I]->isExported()) {
1456 ExportSummary->cfiFunctionDefs().insert(F->getName());
1458 GlobalAlias *JtAlias = GlobalAlias::create(
1459 F->getValueType(), 0, GlobalValue::ExternalLinkage,
1460 F->getName() + ".cfi_jt", CombinedGlobalElemPtr, &M);
1461 JtAlias->setVisibility(GlobalValue::HiddenVisibility);
1462 ExportSummary->cfiFunctionDecls().insert(F->getName());
1465 if (!IsDefinition) {
1466 if (F->isWeakForLinker())
1467 replaceWeakDeclarationWithJumpTablePtr(F, CombinedGlobalElemPtr, IsDefinition);
1469 replaceCfiUses(F, CombinedGlobalElemPtr, IsDefinition);
1471 assert(F->getType()->getAddressSpace() == 0);
1473 GlobalAlias *FAlias = GlobalAlias::create(
1474 F->getValueType(), 0, F->getLinkage(), "", CombinedGlobalElemPtr, &M);
1475 FAlias->setVisibility(F->getVisibility());
1476 FAlias->takeName(F);
1477 if (FAlias->hasName())
1478 F->setName(FAlias->getName() + ".cfi");
1479 replaceCfiUses(F, FAlias, IsDefinition);
1480 if (!F->hasLocalLinkage())
1481 F->setVisibility(GlobalVariable::HiddenVisibility);
1485 createJumpTable(JumpTableFn, Functions);
1488 /// Assign a dummy layout using an incrementing counter, tag each function
1489 /// with its index represented as metadata, and lower each type test to an
1490 /// integer range comparison. During generation of the indirect function call
1491 /// table in the backend, it will assign the given indexes.
1492 /// Note: Dynamic linking is not supported, as the WebAssembly ABI has not yet
1494 void LowerTypeTestsModule::buildBitSetsFromFunctionsWASM(
1495 ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Functions) {
1496 assert(!Functions.empty());
1498 // Build consecutive monotonic integer ranges for each call target set
1499 DenseMap<GlobalTypeMember *, uint64_t> GlobalLayout;
1501 for (GlobalTypeMember *GTM : Functions) {
1502 Function *F = cast<Function>(GTM->getGlobal());
1504 // Skip functions that are not address taken, to avoid bloating the table
1505 if (!F->hasAddressTaken())
1508 // Store metadata with the index for each function
1509 MDNode *MD = MDNode::get(F->getContext(),
1510 ArrayRef<Metadata *>(ConstantAsMetadata::get(
1511 ConstantInt::get(Int64Ty, IndirectIndex))));
1512 F->setMetadata("wasm.index", MD);
1514 // Assign the counter value
1515 GlobalLayout[GTM] = IndirectIndex++;
1518 // The indirect function table index space starts at zero, so pass a NULL
1519 // pointer as the subtracted "jump table" offset.
1520 lowerTypeTestCalls(TypeIds, ConstantPointerNull::get(Int32PtrTy),
1524 void LowerTypeTestsModule::buildBitSetsFromDisjointSet(
1525 ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Globals,
1526 ArrayRef<ICallBranchFunnel *> ICallBranchFunnels) {
1527 DenseMap<Metadata *, uint64_t> TypeIdIndices;
1528 for (unsigned I = 0; I != TypeIds.size(); ++I)
1529 TypeIdIndices[TypeIds[I]] = I;
1531 // For each type identifier, build a set of indices that refer to members of
1532 // the type identifier.
1533 std::vector<std::set<uint64_t>> TypeMembers(TypeIds.size());
1534 unsigned GlobalIndex = 0;
1535 DenseMap<GlobalTypeMember *, uint64_t> GlobalIndices;
1536 for (GlobalTypeMember *GTM : Globals) {
1537 for (MDNode *Type : GTM->types()) {
1538 // Type = { offset, type identifier }
1539 auto I = TypeIdIndices.find(Type->getOperand(1));
1540 if (I != TypeIdIndices.end())
1541 TypeMembers[I->second].insert(GlobalIndex);
1543 GlobalIndices[GTM] = GlobalIndex;
1547 for (ICallBranchFunnel *JT : ICallBranchFunnels) {
1548 TypeMembers.emplace_back();
1549 std::set<uint64_t> &TMSet = TypeMembers.back();
1550 for (GlobalTypeMember *T : JT->targets())
1551 TMSet.insert(GlobalIndices[T]);
1554 // Order the sets of indices by size. The GlobalLayoutBuilder works best
1555 // when given small index sets first.
1557 TypeMembers.begin(), TypeMembers.end(),
1558 [](const std::set<uint64_t> &O1, const std::set<uint64_t> &O2) {
1559 return O1.size() < O2.size();
1562 // Create a GlobalLayoutBuilder and provide it with index sets as layout
1563 // fragments. The GlobalLayoutBuilder tries to lay out members of fragments as
1564 // close together as possible.
1565 GlobalLayoutBuilder GLB(Globals.size());
1566 for (auto &&MemSet : TypeMembers)
1567 GLB.addFragment(MemSet);
1569 // Build a vector of globals with the computed layout.
1571 Globals.empty() || isa<GlobalVariable>(Globals[0]->getGlobal());
1572 std::vector<GlobalTypeMember *> OrderedGTMs(Globals.size());
1573 auto OGTMI = OrderedGTMs.begin();
1574 for (auto &&F : GLB.Fragments) {
1575 for (auto &&Offset : F) {
1576 if (IsGlobalSet != isa<GlobalVariable>(Globals[Offset]->getGlobal()))
1577 report_fatal_error("Type identifier may not contain both global "
1578 "variables and functions");
1579 *OGTMI++ = Globals[Offset];
1583 // Build the bitsets from this disjoint set.
1585 buildBitSetsFromGlobalVariables(TypeIds, OrderedGTMs);
1587 buildBitSetsFromFunctions(TypeIds, OrderedGTMs);
1590 /// Lower all type tests in this module.
1591 LowerTypeTestsModule::LowerTypeTestsModule(
1592 Module &M, ModuleSummaryIndex *ExportSummary,
1593 const ModuleSummaryIndex *ImportSummary)
1594 : M(M), ExportSummary(ExportSummary), ImportSummary(ImportSummary) {
1595 assert(!(ExportSummary && ImportSummary));
1596 Triple TargetTriple(M.getTargetTriple());
1597 Arch = TargetTriple.getArch();
1598 OS = TargetTriple.getOS();
1599 ObjectFormat = TargetTriple.getObjectFormat();
1602 bool LowerTypeTestsModule::runForTesting(Module &M) {
1603 ModuleSummaryIndex Summary(/*HaveGVs=*/false);
1605 // Handle the command-line summary arguments. This code is for testing
1606 // purposes only, so we handle errors directly.
1607 if (!ClReadSummary.empty()) {
1608 ExitOnError ExitOnErr("-lowertypetests-read-summary: " + ClReadSummary +
1610 auto ReadSummaryFile =
1611 ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ClReadSummary)));
1613 yaml::Input In(ReadSummaryFile->getBuffer());
1615 ExitOnErr(errorCodeToError(In.error()));
1619 LowerTypeTestsModule(
1620 M, ClSummaryAction == PassSummaryAction::Export ? &Summary : nullptr,
1621 ClSummaryAction == PassSummaryAction::Import ? &Summary : nullptr)
1624 if (!ClWriteSummary.empty()) {
1625 ExitOnError ExitOnErr("-lowertypetests-write-summary: " + ClWriteSummary +
1628 raw_fd_ostream OS(ClWriteSummary, EC, sys::fs::F_Text);
1629 ExitOnErr(errorCodeToError(EC));
1631 yaml::Output Out(OS);
1638 static bool isDirectCall(Use& U) {
1639 auto *Usr = dyn_cast<CallInst>(U.getUser());
1642 if (CS.isCallee(&U))
1648 void LowerTypeTestsModule::replaceCfiUses(Function *Old, Value *New, bool IsDefinition) {
1649 SmallSetVector<Constant *, 4> Constants;
1650 auto UI = Old->use_begin(), E = Old->use_end();
1655 // Skip block addresses
1656 if (isa<BlockAddress>(U.getUser()))
1659 // Skip direct calls to externally defined or non-dso_local functions
1660 if (isDirectCall(U) && (Old->isDSOLocal() || !IsDefinition))
1663 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
1664 // constant because they are uniqued.
1665 if (auto *C = dyn_cast<Constant>(U.getUser())) {
1666 if (!isa<GlobalValue>(C)) {
1667 // Save unique users to avoid processing operand replacement
1669 Constants.insert(C);
1677 // Process operand replacement of saved constants.
1678 for (auto *C : Constants)
1679 C->handleOperandChange(Old, New);
1682 void LowerTypeTestsModule::replaceDirectCalls(Value *Old, Value *New) {
1683 auto UI = Old->use_begin(), E = Old->use_end();
1688 if (!isDirectCall(U))
1695 bool LowerTypeTestsModule::lower() {
1696 Function *TypeTestFunc =
1697 M.getFunction(Intrinsic::getName(Intrinsic::type_test));
1698 Function *ICallBranchFunnelFunc =
1699 M.getFunction(Intrinsic::getName(Intrinsic::icall_branch_funnel));
1700 if ((!TypeTestFunc || TypeTestFunc->use_empty()) &&
1701 (!ICallBranchFunnelFunc || ICallBranchFunnelFunc->use_empty()) &&
1702 !ExportSummary && !ImportSummary)
1705 // If only some of the modules were split, we cannot correctly handle
1706 // code that contains type tests.
1707 if (TypeTestFunc && !TypeTestFunc->use_empty() &&
1708 ((ExportSummary && ExportSummary->partiallySplitLTOUnits()) ||
1709 (ImportSummary && ImportSummary->partiallySplitLTOUnits())))
1710 report_fatal_error("inconsistent LTO Unit splitting with llvm.type.test");
1712 if (ImportSummary) {
1714 for (auto UI = TypeTestFunc->use_begin(), UE = TypeTestFunc->use_end();
1716 auto *CI = cast<CallInst>((*UI++).getUser());
1721 if (ICallBranchFunnelFunc && !ICallBranchFunnelFunc->use_empty())
1723 "unexpected call to llvm.icall.branch.funnel during import phase");
1725 SmallVector<Function *, 8> Defs;
1726 SmallVector<Function *, 8> Decls;
1728 // CFI functions are either external, or promoted. A local function may
1729 // have the same name, but it's not the one we are looking for.
1730 if (F.hasLocalLinkage())
1732 if (ImportSummary->cfiFunctionDefs().count(F.getName()))
1734 else if (ImportSummary->cfiFunctionDecls().count(F.getName()))
1735 Decls.push_back(&F);
1739 importFunction(F, /*isDefinition*/ true);
1740 for (auto F : Decls)
1741 importFunction(F, /*isDefinition*/ false);
1746 // Equivalence class set containing type identifiers and the globals that
1747 // reference them. This is used to partition the set of type identifiers in
1748 // the module into disjoint sets.
1749 using GlobalClassesTy = EquivalenceClasses<
1750 PointerUnion3<GlobalTypeMember *, Metadata *, ICallBranchFunnel *>>;
1751 GlobalClassesTy GlobalClasses;
1753 // Verify the type metadata and build a few data structures to let us
1754 // efficiently enumerate the type identifiers associated with a global:
1755 // a list of GlobalTypeMembers (a GlobalObject stored alongside a vector
1756 // of associated type metadata) and a mapping from type identifiers to their
1757 // list of GlobalTypeMembers and last observed index in the list of globals.
1758 // The indices will be used later to deterministically order the list of type
1760 BumpPtrAllocator Alloc;
1763 std::vector<GlobalTypeMember *> RefGlobals;
1765 DenseMap<Metadata *, TIInfo> TypeIdInfo;
1766 unsigned CurUniqueId = 0;
1767 SmallVector<MDNode *, 2> Types;
1769 // Cross-DSO CFI emits jumptable entries for exported functions as well as
1770 // address taken functions in case they are address taken in other modules.
1771 const bool CrossDsoCfi = M.getModuleFlag("Cross-DSO CFI") != nullptr;
1773 struct ExportedFunctionInfo {
1774 CfiFunctionLinkage Linkage;
1775 MDNode *FuncMD; // {name, linkage, type[, type...]}
1777 DenseMap<StringRef, ExportedFunctionInfo> ExportedFunctions;
1778 if (ExportSummary) {
1779 // A set of all functions that are address taken by a live global object.
1780 DenseSet<GlobalValue::GUID> AddressTaken;
1781 for (auto &I : *ExportSummary)
1782 for (auto &GVS : I.second.SummaryList)
1784 for (auto &Ref : GVS->refs())
1785 AddressTaken.insert(Ref.getGUID());
1787 NamedMDNode *CfiFunctionsMD = M.getNamedMetadata("cfi.functions");
1788 if (CfiFunctionsMD) {
1789 for (auto FuncMD : CfiFunctionsMD->operands()) {
1790 assert(FuncMD->getNumOperands() >= 2);
1791 StringRef FunctionName =
1792 cast<MDString>(FuncMD->getOperand(0))->getString();
1793 CfiFunctionLinkage Linkage = static_cast<CfiFunctionLinkage>(
1794 cast<ConstantAsMetadata>(FuncMD->getOperand(1))
1796 ->getUniqueInteger()
1798 const GlobalValue::GUID GUID = GlobalValue::getGUID(
1799 GlobalValue::dropLLVMManglingEscape(FunctionName));
1800 // Do not emit jumptable entries for functions that are not-live and
1801 // have no live references (and are not exported with cross-DSO CFI.)
1802 if (!ExportSummary->isGUIDLive(GUID))
1804 if (!AddressTaken.count(GUID)) {
1805 if (!CrossDsoCfi || Linkage != CFL_Definition)
1808 bool Exported = false;
1809 if (auto VI = ExportSummary->getValueInfo(GUID))
1810 for (auto &GVS : VI.getSummaryList())
1811 if (GVS->isLive() && !GlobalValue::isLocalLinkage(GVS->linkage()))
1817 auto P = ExportedFunctions.insert({FunctionName, {Linkage, FuncMD}});
1818 if (!P.second && P.first->second.Linkage != CFL_Definition)
1819 P.first->second = {Linkage, FuncMD};
1822 for (const auto &P : ExportedFunctions) {
1823 StringRef FunctionName = P.first;
1824 CfiFunctionLinkage Linkage = P.second.Linkage;
1825 MDNode *FuncMD = P.second.FuncMD;
1826 Function *F = M.getFunction(FunctionName);
1828 F = Function::Create(
1829 FunctionType::get(Type::getVoidTy(M.getContext()), false),
1830 GlobalVariable::ExternalLinkage,
1831 M.getDataLayout().getProgramAddressSpace(), FunctionName, &M);
1833 // If the function is available_externally, remove its definition so
1834 // that it is handled the same way as a declaration. Later we will try
1835 // to create an alias using this function's linkage, which will fail if
1836 // the linkage is available_externally. This will also result in us
1837 // following the code path below to replace the type metadata.
1838 if (F->hasAvailableExternallyLinkage()) {
1839 F->setLinkage(GlobalValue::ExternalLinkage);
1841 F->setComdat(nullptr);
1845 // Update the linkage for extern_weak declarations when a definition
1847 if (Linkage == CFL_Definition && F->hasExternalWeakLinkage())
1848 F->setLinkage(GlobalValue::ExternalLinkage);
1850 // If the function in the full LTO module is a declaration, replace its
1851 // type metadata with the type metadata we found in cfi.functions. That
1852 // metadata is presumed to be more accurate than the metadata attached
1853 // to the declaration.
1854 if (F->isDeclaration()) {
1855 if (Linkage == CFL_WeakDeclaration)
1856 F->setLinkage(GlobalValue::ExternalWeakLinkage);
1858 F->eraseMetadata(LLVMContext::MD_type);
1859 for (unsigned I = 2; I < FuncMD->getNumOperands(); ++I)
1860 F->addMetadata(LLVMContext::MD_type,
1861 *cast<MDNode>(FuncMD->getOperand(I).get()));
1867 DenseMap<GlobalObject *, GlobalTypeMember *> GlobalTypeMembers;
1868 for (GlobalObject &GO : M.global_objects()) {
1869 if (isa<GlobalVariable>(GO) && GO.isDeclarationForLinker())
1873 GO.getMetadata(LLVMContext::MD_type, Types);
1875 bool IsDefinition = !GO.isDeclarationForLinker();
1876 bool IsExported = false;
1877 if (Function *F = dyn_cast<Function>(&GO)) {
1878 if (ExportedFunctions.count(F->getName())) {
1879 IsDefinition |= ExportedFunctions[F->getName()].Linkage == CFL_Definition;
1881 // TODO: The logic here checks only that the function is address taken,
1882 // not that the address takers are live. This can be updated to check
1883 // their liveness and emit fewer jumptable entries once monolithic LTO
1884 // builds also emit summaries.
1885 } else if (!F->hasAddressTaken()) {
1886 if (!CrossDsoCfi || !IsDefinition || F->hasLocalLinkage())
1892 GlobalTypeMember::create(Alloc, &GO, IsDefinition, IsExported, Types);
1893 GlobalTypeMembers[&GO] = GTM;
1894 for (MDNode *Type : Types) {
1895 verifyTypeMDNode(&GO, Type);
1896 auto &Info = TypeIdInfo[Type->getOperand(1)];
1897 Info.UniqueId = ++CurUniqueId;
1898 Info.RefGlobals.push_back(GTM);
1902 auto AddTypeIdUse = [&](Metadata *TypeId) -> TypeIdUserInfo & {
1903 // Add the call site to the list of call sites for this type identifier. We
1904 // also use TypeIdUsers to keep track of whether we have seen this type
1905 // identifier before. If we have, we don't need to re-add the referenced
1906 // globals to the equivalence class.
1907 auto Ins = TypeIdUsers.insert({TypeId, {}});
1909 // Add the type identifier to the equivalence class.
1910 GlobalClassesTy::iterator GCI = GlobalClasses.insert(TypeId);
1911 GlobalClassesTy::member_iterator CurSet = GlobalClasses.findLeader(GCI);
1913 // Add the referenced globals to the type identifier's equivalence class.
1914 for (GlobalTypeMember *GTM : TypeIdInfo[TypeId].RefGlobals)
1915 CurSet = GlobalClasses.unionSets(
1916 CurSet, GlobalClasses.findLeader(GlobalClasses.insert(GTM)));
1919 return Ins.first->second;
1923 for (const Use &U : TypeTestFunc->uses()) {
1924 auto CI = cast<CallInst>(U.getUser());
1926 auto TypeIdMDVal = dyn_cast<MetadataAsValue>(CI->getArgOperand(1));
1928 report_fatal_error("Second argument of llvm.type.test must be metadata");
1929 auto TypeId = TypeIdMDVal->getMetadata();
1930 AddTypeIdUse(TypeId).CallSites.push_back(CI);
1934 if (ICallBranchFunnelFunc) {
1935 for (const Use &U : ICallBranchFunnelFunc->uses()) {
1936 if (Arch != Triple::x86_64)
1938 "llvm.icall.branch.funnel not supported on this target");
1940 auto CI = cast<CallInst>(U.getUser());
1942 std::vector<GlobalTypeMember *> Targets;
1943 if (CI->getNumArgOperands() % 2 != 1)
1944 report_fatal_error("number of arguments should be odd");
1946 GlobalClassesTy::member_iterator CurSet;
1947 for (unsigned I = 1; I != CI->getNumArgOperands(); I += 2) {
1949 auto *Base = dyn_cast<GlobalObject>(GetPointerBaseWithConstantOffset(
1950 CI->getOperand(I), Offset, M.getDataLayout()));
1953 "Expected branch funnel operand to be global value");
1955 GlobalTypeMember *GTM = GlobalTypeMembers[Base];
1956 Targets.push_back(GTM);
1957 GlobalClassesTy::member_iterator NewSet =
1958 GlobalClasses.findLeader(GlobalClasses.insert(GTM));
1962 CurSet = GlobalClasses.unionSets(CurSet, NewSet);
1965 GlobalClasses.unionSets(
1966 CurSet, GlobalClasses.findLeader(
1967 GlobalClasses.insert(ICallBranchFunnel::create(
1968 Alloc, CI, Targets, ++CurUniqueId))));
1972 if (ExportSummary) {
1973 DenseMap<GlobalValue::GUID, TinyPtrVector<Metadata *>> MetadataByGUID;
1974 for (auto &P : TypeIdInfo) {
1975 if (auto *TypeId = dyn_cast<MDString>(P.first))
1976 MetadataByGUID[GlobalValue::getGUID(TypeId->getString())].push_back(
1980 for (auto &P : *ExportSummary) {
1981 for (auto &S : P.second.SummaryList) {
1982 if (!ExportSummary->isGlobalValueLive(S.get()))
1984 if (auto *FS = dyn_cast<FunctionSummary>(S->getBaseObject()))
1985 for (GlobalValue::GUID G : FS->type_tests())
1986 for (Metadata *MD : MetadataByGUID[G])
1987 AddTypeIdUse(MD).IsExported = true;
1992 if (GlobalClasses.empty())
1995 // Build a list of disjoint sets ordered by their maximum global index for
1997 std::vector<std::pair<GlobalClassesTy::iterator, unsigned>> Sets;
1998 for (GlobalClassesTy::iterator I = GlobalClasses.begin(),
1999 E = GlobalClasses.end();
2003 ++NumTypeIdDisjointSets;
2005 unsigned MaxUniqueId = 0;
2006 for (GlobalClassesTy::member_iterator MI = GlobalClasses.member_begin(I);
2007 MI != GlobalClasses.member_end(); ++MI) {
2008 if (auto *MD = MI->dyn_cast<Metadata *>())
2009 MaxUniqueId = std::max(MaxUniqueId, TypeIdInfo[MD].UniqueId);
2010 else if (auto *BF = MI->dyn_cast<ICallBranchFunnel *>())
2011 MaxUniqueId = std::max(MaxUniqueId, BF->UniqueId);
2013 Sets.emplace_back(I, MaxUniqueId);
2016 [](const std::pair<GlobalClassesTy::iterator, unsigned> &S1,
2017 const std::pair<GlobalClassesTy::iterator, unsigned> &S2) {
2018 return S1.second < S2.second;
2021 // For each disjoint set we found...
2022 for (const auto &S : Sets) {
2023 // Build the list of type identifiers in this disjoint set.
2024 std::vector<Metadata *> TypeIds;
2025 std::vector<GlobalTypeMember *> Globals;
2026 std::vector<ICallBranchFunnel *> ICallBranchFunnels;
2027 for (GlobalClassesTy::member_iterator MI =
2028 GlobalClasses.member_begin(S.first);
2029 MI != GlobalClasses.member_end(); ++MI) {
2030 if (MI->is<Metadata *>())
2031 TypeIds.push_back(MI->get<Metadata *>());
2032 else if (MI->is<GlobalTypeMember *>())
2033 Globals.push_back(MI->get<GlobalTypeMember *>());
2035 ICallBranchFunnels.push_back(MI->get<ICallBranchFunnel *>());
2038 // Order type identifiers by unique ID for determinism. This ordering is
2039 // stable as there is a one-to-one mapping between metadata and unique IDs.
2040 llvm::sort(TypeIds, [&](Metadata *M1, Metadata *M2) {
2041 return TypeIdInfo[M1].UniqueId < TypeIdInfo[M2].UniqueId;
2044 // Same for the branch funnels.
2045 llvm::sort(ICallBranchFunnels,
2046 [&](ICallBranchFunnel *F1, ICallBranchFunnel *F2) {
2047 return F1->UniqueId < F2->UniqueId;
2050 // Build bitsets for this disjoint set.
2051 buildBitSetsFromDisjointSet(TypeIds, Globals, ICallBranchFunnels);
2054 allocateByteArrays();
2056 // Parse alias data to replace stand-in function declarations for aliases
2057 // with an alias to the intended target.
2058 if (ExportSummary) {
2059 if (NamedMDNode *AliasesMD = M.getNamedMetadata("aliases")) {
2060 for (auto AliasMD : AliasesMD->operands()) {
2061 assert(AliasMD->getNumOperands() >= 4);
2062 StringRef AliasName =
2063 cast<MDString>(AliasMD->getOperand(0))->getString();
2064 StringRef Aliasee = cast<MDString>(AliasMD->getOperand(1))->getString();
2066 if (!ExportedFunctions.count(Aliasee) ||
2067 ExportedFunctions[Aliasee].Linkage != CFL_Definition ||
2068 !M.getNamedAlias(Aliasee))
2071 GlobalValue::VisibilityTypes Visibility =
2072 static_cast<GlobalValue::VisibilityTypes>(
2073 cast<ConstantAsMetadata>(AliasMD->getOperand(2))
2075 ->getUniqueInteger()
2078 static_cast<bool>(cast<ConstantAsMetadata>(AliasMD->getOperand(3))
2080 ->getUniqueInteger()
2083 auto *Alias = GlobalAlias::create("", M.getNamedAlias(Aliasee));
2084 Alias->setVisibility(Visibility);
2086 Alias->setLinkage(GlobalValue::WeakAnyLinkage);
2088 if (auto *F = M.getFunction(AliasName)) {
2090 F->replaceAllUsesWith(Alias);
2091 F->eraseFromParent();
2093 Alias->setName(AliasName);
2099 // Emit .symver directives for exported functions, if they exist.
2100 if (ExportSummary) {
2101 if (NamedMDNode *SymversMD = M.getNamedMetadata("symvers")) {
2102 for (auto Symver : SymversMD->operands()) {
2103 assert(Symver->getNumOperands() >= 2);
2104 StringRef SymbolName =
2105 cast<MDString>(Symver->getOperand(0))->getString();
2106 StringRef Alias = cast<MDString>(Symver->getOperand(1))->getString();
2108 if (!ExportedFunctions.count(SymbolName))
2111 M.appendModuleInlineAsm(
2112 (llvm::Twine(".symver ") + SymbolName + ", " + Alias).str());
2120 PreservedAnalyses LowerTypeTestsPass::run(Module &M,
2121 ModuleAnalysisManager &AM) {
2122 bool Changed = LowerTypeTestsModule(M, ExportSummary, ImportSummary).lower();
2124 return PreservedAnalyses::all();
2125 return PreservedAnalyses::none();