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 // See http://llvm.org/docs/TypeMetadata.html for more information.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/IPO/LowerTypeTests.h"
16 #include "llvm/ADT/APInt.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/EquivalenceClasses.h"
20 #include "llvm/ADT/PointerUnion.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/ADT/TinyPtrVector.h"
26 #include "llvm/ADT/Triple.h"
27 #include "llvm/Analysis/TypeMetadataUtils.h"
28 #include "llvm/IR/Attributes.h"
29 #include "llvm/IR/BasicBlock.h"
30 #include "llvm/IR/Constant.h"
31 #include "llvm/IR/Constants.h"
32 #include "llvm/IR/DataLayout.h"
33 #include "llvm/IR/DerivedTypes.h"
34 #include "llvm/IR/Function.h"
35 #include "llvm/IR/GlobalAlias.h"
36 #include "llvm/IR/GlobalObject.h"
37 #include "llvm/IR/GlobalValue.h"
38 #include "llvm/IR/GlobalVariable.h"
39 #include "llvm/IR/IRBuilder.h"
40 #include "llvm/IR/InlineAsm.h"
41 #include "llvm/IR/Instruction.h"
42 #include "llvm/IR/Instructions.h"
43 #include "llvm/IR/Intrinsics.h"
44 #include "llvm/IR/LLVMContext.h"
45 #include "llvm/IR/Metadata.h"
46 #include "llvm/IR/Module.h"
47 #include "llvm/IR/ModuleSummaryIndex.h"
48 #include "llvm/IR/ModuleSummaryIndexYAML.h"
49 #include "llvm/IR/Operator.h"
50 #include "llvm/IR/PassManager.h"
51 #include "llvm/IR/Type.h"
52 #include "llvm/IR/Use.h"
53 #include "llvm/IR/User.h"
54 #include "llvm/IR/Value.h"
55 #include "llvm/Pass.h"
56 #include "llvm/Support/Allocator.h"
57 #include "llvm/Support/Casting.h"
58 #include "llvm/Support/CommandLine.h"
59 #include "llvm/Support/Debug.h"
60 #include "llvm/Support/Error.h"
61 #include "llvm/Support/ErrorHandling.h"
62 #include "llvm/Support/FileSystem.h"
63 #include "llvm/Support/MathExtras.h"
64 #include "llvm/Support/MemoryBuffer.h"
65 #include "llvm/Support/TrailingObjects.h"
66 #include "llvm/Support/YAMLTraits.h"
67 #include "llvm/Support/raw_ostream.h"
68 #include "llvm/Transforms/IPO.h"
69 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
70 #include "llvm/Transforms/Utils/ModuleUtils.h"
77 #include <system_error>
82 using namespace lowertypetests;
84 #define DEBUG_TYPE "lowertypetests"
86 STATISTIC(ByteArraySizeBits, "Byte array size in bits");
87 STATISTIC(ByteArraySizeBytes, "Byte array size in bytes");
88 STATISTIC(NumByteArraysCreated, "Number of byte arrays created");
89 STATISTIC(NumTypeTestCallsLowered, "Number of type test calls lowered");
90 STATISTIC(NumTypeIdDisjointSets, "Number of disjoint sets of type identifiers");
92 static cl::opt<bool> AvoidReuse(
93 "lowertypetests-avoid-reuse",
94 cl::desc("Try to avoid reuse of byte array addresses using aliases"),
95 cl::Hidden, cl::init(true));
97 static cl::opt<PassSummaryAction> ClSummaryAction(
98 "lowertypetests-summary-action",
99 cl::desc("What to do with the summary when running this pass"),
100 cl::values(clEnumValN(PassSummaryAction::None, "none", "Do nothing"),
101 clEnumValN(PassSummaryAction::Import, "import",
102 "Import typeid resolutions from summary and globals"),
103 clEnumValN(PassSummaryAction::Export, "export",
104 "Export typeid resolutions to summary and globals")),
107 static cl::opt<std::string> ClReadSummary(
108 "lowertypetests-read-summary",
109 cl::desc("Read summary from given YAML file before running pass"),
112 static cl::opt<std::string> ClWriteSummary(
113 "lowertypetests-write-summary",
114 cl::desc("Write summary to given YAML file after running pass"),
117 bool BitSetInfo::containsGlobalOffset(uint64_t Offset) const {
118 if (Offset < ByteOffset)
121 if ((Offset - ByteOffset) % (uint64_t(1) << AlignLog2) != 0)
124 uint64_t BitOffset = (Offset - ByteOffset) >> AlignLog2;
125 if (BitOffset >= BitSize)
128 return Bits.count(BitOffset);
131 void BitSetInfo::print(raw_ostream &OS) const {
132 OS << "offset " << ByteOffset << " size " << BitSize << " align "
141 for (uint64_t B : Bits)
146 BitSetInfo BitSetBuilder::build() {
150 // Normalize each offset against the minimum observed offset, and compute
151 // the bitwise OR of each of the offsets. The number of trailing zeros
152 // in the mask gives us the log2 of the alignment of all offsets, which
153 // allows us to compress the bitset by only storing one bit per aligned
156 for (uint64_t &Offset : Offsets) {
162 BSI.ByteOffset = Min;
166 BSI.AlignLog2 = countTrailingZeros(Mask, ZB_Undefined);
168 // Build the compressed bitset while normalizing the offsets against the
169 // computed alignment.
170 BSI.BitSize = ((Max - Min) >> BSI.AlignLog2) + 1;
171 for (uint64_t Offset : Offsets) {
172 Offset >>= BSI.AlignLog2;
173 BSI.Bits.insert(Offset);
179 void GlobalLayoutBuilder::addFragment(const std::set<uint64_t> &F) {
180 // Create a new fragment to hold the layout for F.
181 Fragments.emplace_back();
182 std::vector<uint64_t> &Fragment = Fragments.back();
183 uint64_t FragmentIndex = Fragments.size() - 1;
185 for (auto ObjIndex : F) {
186 uint64_t OldFragmentIndex = FragmentMap[ObjIndex];
187 if (OldFragmentIndex == 0) {
188 // We haven't seen this object index before, so just add it to the current
190 Fragment.push_back(ObjIndex);
192 // This index belongs to an existing fragment. Copy the elements of the
193 // old fragment into this one and clear the old fragment. We don't update
194 // the fragment map just yet, this ensures that any further references to
195 // indices from the old fragment in this fragment do not insert any more
197 std::vector<uint64_t> &OldFragment = Fragments[OldFragmentIndex];
198 Fragment.insert(Fragment.end(), OldFragment.begin(), OldFragment.end());
203 // Update the fragment map to point our object indices to this fragment.
204 for (uint64_t ObjIndex : Fragment)
205 FragmentMap[ObjIndex] = FragmentIndex;
208 void ByteArrayBuilder::allocate(const std::set<uint64_t> &Bits,
209 uint64_t BitSize, uint64_t &AllocByteOffset,
210 uint8_t &AllocMask) {
211 // Find the smallest current allocation.
213 for (unsigned I = 1; I != BitsPerByte; ++I)
214 if (BitAllocs[I] < BitAllocs[Bit])
217 AllocByteOffset = BitAllocs[Bit];
219 // Add our size to it.
220 unsigned ReqSize = AllocByteOffset + BitSize;
221 BitAllocs[Bit] = ReqSize;
222 if (Bytes.size() < ReqSize)
223 Bytes.resize(ReqSize);
226 AllocMask = 1 << Bit;
227 for (uint64_t B : Bits)
228 Bytes[AllocByteOffset + B] |= AllocMask;
233 struct ByteArrayInfo {
234 std::set<uint64_t> Bits;
236 GlobalVariable *ByteArray;
237 GlobalVariable *MaskGlobal;
238 uint8_t *MaskPtr = nullptr;
241 /// A POD-like structure that we use to store a global reference together with
242 /// its metadata types. In this pass we frequently need to query the set of
243 /// metadata types referenced by a global, which at the IR level is an expensive
244 /// operation involving a map lookup; this data structure helps to reduce the
245 /// number of times we need to do this lookup.
246 class GlobalTypeMember final : TrailingObjects<GlobalTypeMember, MDNode *> {
247 friend TrailingObjects;
252 // For functions: true if this is a definition (either in the merged module or
253 // in one of the thinlto modules).
256 // For functions: true if this function is either defined or used in a thinlto
257 // module and its jumptable entry needs to be exported to thinlto backends.
260 size_t numTrailingObjects(OverloadToken<MDNode *>) const { return NTypes; }
263 static GlobalTypeMember *create(BumpPtrAllocator &Alloc, GlobalObject *GO,
264 bool IsDefinition, bool IsExported,
265 ArrayRef<MDNode *> Types) {
266 auto *GTM = static_cast<GlobalTypeMember *>(Alloc.Allocate(
267 totalSizeToAlloc<MDNode *>(Types.size()), alignof(GlobalTypeMember)));
269 GTM->NTypes = Types.size();
270 GTM->IsDefinition = IsDefinition;
271 GTM->IsExported = IsExported;
272 std::uninitialized_copy(Types.begin(), Types.end(),
273 GTM->getTrailingObjects<MDNode *>());
277 GlobalObject *getGlobal() const {
281 bool isDefinition() const {
285 bool isExported() const {
289 ArrayRef<MDNode *> types() const {
290 return makeArrayRef(getTrailingObjects<MDNode *>(), NTypes);
294 class LowerTypeTestsModule {
297 ModuleSummaryIndex *ExportSummary;
298 const ModuleSummaryIndex *ImportSummary;
300 Triple::ArchType Arch;
302 Triple::ObjectFormatType ObjectFormat;
304 IntegerType *Int1Ty = Type::getInt1Ty(M.getContext());
305 IntegerType *Int8Ty = Type::getInt8Ty(M.getContext());
306 PointerType *Int8PtrTy = Type::getInt8PtrTy(M.getContext());
307 ArrayType *Int8Arr0Ty = ArrayType::get(Type::getInt8Ty(M.getContext()), 0);
308 IntegerType *Int32Ty = Type::getInt32Ty(M.getContext());
309 PointerType *Int32PtrTy = PointerType::getUnqual(Int32Ty);
310 IntegerType *Int64Ty = Type::getInt64Ty(M.getContext());
311 IntegerType *IntPtrTy = M.getDataLayout().getIntPtrType(M.getContext(), 0);
313 // Indirect function call index assignment counter for WebAssembly
314 uint64_t IndirectIndex = 1;
316 // Mapping from type identifiers to the call sites that test them, as well as
317 // whether the type identifier needs to be exported to ThinLTO backends as
318 // part of the regular LTO phase of the ThinLTO pipeline (see exportTypeId).
319 struct TypeIdUserInfo {
320 std::vector<CallInst *> CallSites;
321 bool IsExported = false;
323 DenseMap<Metadata *, TypeIdUserInfo> TypeIdUsers;
325 /// This structure describes how to lower type tests for a particular type
326 /// identifier. It is either built directly from the global analysis (during
327 /// regular LTO or the regular LTO phase of ThinLTO), or indirectly using type
328 /// identifier summaries and external symbol references (in ThinLTO backends).
329 struct TypeIdLowering {
330 TypeTestResolution::Kind TheKind = TypeTestResolution::Unsat;
332 /// All except Unsat: the start address within the combined global.
333 Constant *OffsetedGlobal;
335 /// ByteArray, Inline, AllOnes: log2 of the required global alignment
336 /// relative to the start address.
339 /// ByteArray, Inline, AllOnes: one less than the size of the memory region
340 /// covering members of this type identifier as a multiple of 2^AlignLog2.
343 /// ByteArray: the byte array to test the address against.
344 Constant *TheByteArray;
346 /// ByteArray: the bit mask to apply to bytes loaded from the byte array.
349 /// Inline: the bit mask to test the address against.
350 Constant *InlineBits;
353 std::vector<ByteArrayInfo> ByteArrayInfos;
355 Function *WeakInitializerFn = nullptr;
357 bool shouldExportConstantsAsAbsoluteSymbols();
358 uint8_t *exportTypeId(StringRef TypeId, const TypeIdLowering &TIL);
359 TypeIdLowering importTypeId(StringRef TypeId);
360 void importTypeTest(CallInst *CI);
361 void importFunction(Function *F, bool isDefinition);
364 buildBitSet(Metadata *TypeId,
365 const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout);
366 ByteArrayInfo *createByteArray(BitSetInfo &BSI);
367 void allocateByteArrays();
368 Value *createBitSetTest(IRBuilder<> &B, const TypeIdLowering &TIL,
370 void lowerTypeTestCalls(
371 ArrayRef<Metadata *> TypeIds, Constant *CombinedGlobalAddr,
372 const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout);
373 Value *lowerTypeTestCall(Metadata *TypeId, CallInst *CI,
374 const TypeIdLowering &TIL);
375 void buildBitSetsFromGlobalVariables(ArrayRef<Metadata *> TypeIds,
376 ArrayRef<GlobalTypeMember *> Globals);
377 unsigned getJumpTableEntrySize();
378 Type *getJumpTableEntryType();
379 void createJumpTableEntry(raw_ostream &AsmOS, raw_ostream &ConstraintOS,
380 Triple::ArchType JumpTableArch,
381 SmallVectorImpl<Value *> &AsmArgs, Function *Dest);
382 void verifyTypeMDNode(GlobalObject *GO, MDNode *Type);
383 void buildBitSetsFromFunctions(ArrayRef<Metadata *> TypeIds,
384 ArrayRef<GlobalTypeMember *> Functions);
385 void buildBitSetsFromFunctionsNative(ArrayRef<Metadata *> TypeIds,
386 ArrayRef<GlobalTypeMember *> Functions);
387 void buildBitSetsFromFunctionsWASM(ArrayRef<Metadata *> TypeIds,
388 ArrayRef<GlobalTypeMember *> Functions);
389 void buildBitSetsFromDisjointSet(ArrayRef<Metadata *> TypeIds,
390 ArrayRef<GlobalTypeMember *> Globals);
392 void replaceWeakDeclarationWithJumpTablePtr(Function *F, Constant *JT);
393 void moveInitializerToModuleConstructor(GlobalVariable *GV);
394 void findGlobalVariableUsersOf(Constant *C,
395 SmallSetVector<GlobalVariable *, 8> &Out);
397 void createJumpTable(Function *F, ArrayRef<GlobalTypeMember *> Functions);
400 LowerTypeTestsModule(Module &M, ModuleSummaryIndex *ExportSummary,
401 const ModuleSummaryIndex *ImportSummary);
405 // Lower the module using the action and summary passed as command line
406 // arguments. For testing purposes only.
407 static bool runForTesting(Module &M);
410 struct LowerTypeTests : public ModulePass {
413 bool UseCommandLine = false;
415 ModuleSummaryIndex *ExportSummary;
416 const ModuleSummaryIndex *ImportSummary;
418 LowerTypeTests() : ModulePass(ID), UseCommandLine(true) {
419 initializeLowerTypeTestsPass(*PassRegistry::getPassRegistry());
422 LowerTypeTests(ModuleSummaryIndex *ExportSummary,
423 const ModuleSummaryIndex *ImportSummary)
424 : ModulePass(ID), ExportSummary(ExportSummary),
425 ImportSummary(ImportSummary) {
426 initializeLowerTypeTestsPass(*PassRegistry::getPassRegistry());
429 bool runOnModule(Module &M) override {
433 return LowerTypeTestsModule::runForTesting(M);
434 return LowerTypeTestsModule(M, ExportSummary, ImportSummary).lower();
438 } // end anonymous namespace
440 char LowerTypeTests::ID = 0;
442 INITIALIZE_PASS(LowerTypeTests, "lowertypetests", "Lower type metadata", false,
446 llvm::createLowerTypeTestsPass(ModuleSummaryIndex *ExportSummary,
447 const ModuleSummaryIndex *ImportSummary) {
448 return new LowerTypeTests(ExportSummary, ImportSummary);
451 /// Build a bit set for TypeId using the object layouts in
453 BitSetInfo LowerTypeTestsModule::buildBitSet(
455 const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout) {
458 // Compute the byte offset of each address associated with this type
460 for (auto &GlobalAndOffset : GlobalLayout) {
461 for (MDNode *Type : GlobalAndOffset.first->types()) {
462 if (Type->getOperand(1) != TypeId)
466 cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
468 BSB.addOffset(GlobalAndOffset.second + Offset);
475 /// Build a test that bit BitOffset mod sizeof(Bits)*8 is set in
476 /// Bits. This pattern matches to the bt instruction on x86.
477 static Value *createMaskedBitTest(IRBuilder<> &B, Value *Bits,
479 auto BitsType = cast<IntegerType>(Bits->getType());
480 unsigned BitWidth = BitsType->getBitWidth();
482 BitOffset = B.CreateZExtOrTrunc(BitOffset, BitsType);
484 B.CreateAnd(BitOffset, ConstantInt::get(BitsType, BitWidth - 1));
485 Value *BitMask = B.CreateShl(ConstantInt::get(BitsType, 1), BitIndex);
486 Value *MaskedBits = B.CreateAnd(Bits, BitMask);
487 return B.CreateICmpNE(MaskedBits, ConstantInt::get(BitsType, 0));
490 ByteArrayInfo *LowerTypeTestsModule::createByteArray(BitSetInfo &BSI) {
491 // Create globals to stand in for byte arrays and masks. These never actually
492 // get initialized, we RAUW and erase them later in allocateByteArrays() once
493 // we know the offset and mask to use.
494 auto ByteArrayGlobal = new GlobalVariable(
495 M, Int8Ty, /*isConstant=*/true, GlobalValue::PrivateLinkage, nullptr);
496 auto MaskGlobal = new GlobalVariable(M, Int8Ty, /*isConstant=*/true,
497 GlobalValue::PrivateLinkage, nullptr);
499 ByteArrayInfos.emplace_back();
500 ByteArrayInfo *BAI = &ByteArrayInfos.back();
502 BAI->Bits = BSI.Bits;
503 BAI->BitSize = BSI.BitSize;
504 BAI->ByteArray = ByteArrayGlobal;
505 BAI->MaskGlobal = MaskGlobal;
509 void LowerTypeTestsModule::allocateByteArrays() {
510 std::stable_sort(ByteArrayInfos.begin(), ByteArrayInfos.end(),
511 [](const ByteArrayInfo &BAI1, const ByteArrayInfo &BAI2) {
512 return BAI1.BitSize > BAI2.BitSize;
515 std::vector<uint64_t> ByteArrayOffsets(ByteArrayInfos.size());
517 ByteArrayBuilder BAB;
518 for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) {
519 ByteArrayInfo *BAI = &ByteArrayInfos[I];
522 BAB.allocate(BAI->Bits, BAI->BitSize, ByteArrayOffsets[I], Mask);
524 BAI->MaskGlobal->replaceAllUsesWith(
525 ConstantExpr::getIntToPtr(ConstantInt::get(Int8Ty, Mask), Int8PtrTy));
526 BAI->MaskGlobal->eraseFromParent();
528 *BAI->MaskPtr = Mask;
531 Constant *ByteArrayConst = ConstantDataArray::get(M.getContext(), BAB.Bytes);
533 new GlobalVariable(M, ByteArrayConst->getType(), /*isConstant=*/true,
534 GlobalValue::PrivateLinkage, ByteArrayConst);
536 for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) {
537 ByteArrayInfo *BAI = &ByteArrayInfos[I];
539 Constant *Idxs[] = {ConstantInt::get(IntPtrTy, 0),
540 ConstantInt::get(IntPtrTy, ByteArrayOffsets[I])};
541 Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(
542 ByteArrayConst->getType(), ByteArray, Idxs);
544 // Create an alias instead of RAUW'ing the gep directly. On x86 this ensures
545 // that the pc-relative displacement is folded into the lea instead of the
546 // test instruction getting another displacement.
547 GlobalAlias *Alias = GlobalAlias::create(
548 Int8Ty, 0, GlobalValue::PrivateLinkage, "bits", GEP, &M);
549 BAI->ByteArray->replaceAllUsesWith(Alias);
550 BAI->ByteArray->eraseFromParent();
553 ByteArraySizeBits = BAB.BitAllocs[0] + BAB.BitAllocs[1] + BAB.BitAllocs[2] +
554 BAB.BitAllocs[3] + BAB.BitAllocs[4] + BAB.BitAllocs[5] +
555 BAB.BitAllocs[6] + BAB.BitAllocs[7];
556 ByteArraySizeBytes = BAB.Bytes.size();
559 /// Build a test that bit BitOffset is set in the type identifier that was
560 /// lowered to TIL, which must be either an Inline or a ByteArray.
561 Value *LowerTypeTestsModule::createBitSetTest(IRBuilder<> &B,
562 const TypeIdLowering &TIL,
564 if (TIL.TheKind == TypeTestResolution::Inline) {
565 // If the bit set is sufficiently small, we can avoid a load by bit testing
567 return createMaskedBitTest(B, TIL.InlineBits, BitOffset);
569 Constant *ByteArray = TIL.TheByteArray;
570 if (AvoidReuse && !ImportSummary) {
571 // Each use of the byte array uses a different alias. This makes the
572 // backend less likely to reuse previously computed byte array addresses,
573 // improving the security of the CFI mechanism based on this pass.
574 // This won't work when importing because TheByteArray is external.
575 ByteArray = GlobalAlias::create(Int8Ty, 0, GlobalValue::PrivateLinkage,
576 "bits_use", ByteArray, &M);
579 Value *ByteAddr = B.CreateGEP(Int8Ty, ByteArray, BitOffset);
580 Value *Byte = B.CreateLoad(ByteAddr);
583 B.CreateAnd(Byte, ConstantExpr::getPtrToInt(TIL.BitMask, Int8Ty));
584 return B.CreateICmpNE(ByteAndMask, ConstantInt::get(Int8Ty, 0));
588 static bool isKnownTypeIdMember(Metadata *TypeId, const DataLayout &DL,
589 Value *V, uint64_t COffset) {
590 if (auto GV = dyn_cast<GlobalObject>(V)) {
591 SmallVector<MDNode *, 2> Types;
592 GV->getMetadata(LLVMContext::MD_type, Types);
593 for (MDNode *Type : Types) {
594 if (Type->getOperand(1) != TypeId)
598 cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
600 if (COffset == Offset)
606 if (auto GEP = dyn_cast<GEPOperator>(V)) {
607 APInt APOffset(DL.getPointerSizeInBits(0), 0);
608 bool Result = GEP->accumulateConstantOffset(DL, APOffset);
611 COffset += APOffset.getZExtValue();
612 return isKnownTypeIdMember(TypeId, DL, GEP->getPointerOperand(), COffset);
615 if (auto Op = dyn_cast<Operator>(V)) {
616 if (Op->getOpcode() == Instruction::BitCast)
617 return isKnownTypeIdMember(TypeId, DL, Op->getOperand(0), COffset);
619 if (Op->getOpcode() == Instruction::Select)
620 return isKnownTypeIdMember(TypeId, DL, Op->getOperand(1), COffset) &&
621 isKnownTypeIdMember(TypeId, DL, Op->getOperand(2), COffset);
627 /// Lower a llvm.type.test call to its implementation. Returns the value to
628 /// replace the call with.
629 Value *LowerTypeTestsModule::lowerTypeTestCall(Metadata *TypeId, CallInst *CI,
630 const TypeIdLowering &TIL) {
631 if (TIL.TheKind == TypeTestResolution::Unsat)
632 return ConstantInt::getFalse(M.getContext());
634 Value *Ptr = CI->getArgOperand(0);
635 const DataLayout &DL = M.getDataLayout();
636 if (isKnownTypeIdMember(TypeId, DL, Ptr, 0))
637 return ConstantInt::getTrue(M.getContext());
639 BasicBlock *InitialBB = CI->getParent();
643 Value *PtrAsInt = B.CreatePtrToInt(Ptr, IntPtrTy);
645 Constant *OffsetedGlobalAsInt =
646 ConstantExpr::getPtrToInt(TIL.OffsetedGlobal, IntPtrTy);
647 if (TIL.TheKind == TypeTestResolution::Single)
648 return B.CreateICmpEQ(PtrAsInt, OffsetedGlobalAsInt);
650 Value *PtrOffset = B.CreateSub(PtrAsInt, OffsetedGlobalAsInt);
652 // We need to check that the offset both falls within our range and is
653 // suitably aligned. We can check both properties at the same time by
654 // performing a right rotate by log2(alignment) followed by an integer
655 // comparison against the bitset size. The rotate will move the lower
656 // order bits that need to be zero into the higher order bits of the
657 // result, causing the comparison to fail if they are nonzero. The rotate
658 // also conveniently gives us a bit offset to use during the load from
661 B.CreateLShr(PtrOffset, ConstantExpr::getZExt(TIL.AlignLog2, IntPtrTy));
662 Value *OffsetSHL = B.CreateShl(
663 PtrOffset, ConstantExpr::getZExt(
664 ConstantExpr::getSub(
665 ConstantInt::get(Int8Ty, DL.getPointerSizeInBits(0)),
668 Value *BitOffset = B.CreateOr(OffsetSHR, OffsetSHL);
670 Value *OffsetInRange = B.CreateICmpULE(BitOffset, TIL.SizeM1);
672 // If the bit set is all ones, testing against it is unnecessary.
673 if (TIL.TheKind == TypeTestResolution::AllOnes)
674 return OffsetInRange;
676 // See if the intrinsic is used in the following common pattern:
677 // br(llvm.type.test(...), thenbb, elsebb)
678 // where nothing happens between the type test and the br.
679 // If so, create slightly simpler IR.
681 if (auto *Br = dyn_cast<BranchInst>(*CI->user_begin()))
682 if (CI->getNextNode() == Br) {
683 BasicBlock *Then = InitialBB->splitBasicBlock(CI->getIterator());
684 BasicBlock *Else = Br->getSuccessor(1);
685 BranchInst *NewBr = BranchInst::Create(Then, Else, OffsetInRange);
686 NewBr->setMetadata(LLVMContext::MD_prof,
687 Br->getMetadata(LLVMContext::MD_prof));
688 ReplaceInstWithInst(InitialBB->getTerminator(), NewBr);
690 // Update phis in Else resulting from InitialBB being split
691 for (auto &Phi : Else->phis())
692 Phi.addIncoming(Phi.getIncomingValueForBlock(Then), InitialBB);
694 IRBuilder<> ThenB(CI);
695 return createBitSetTest(ThenB, TIL, BitOffset);
698 IRBuilder<> ThenB(SplitBlockAndInsertIfThen(OffsetInRange, CI, false));
700 // Now that we know that the offset is in range and aligned, load the
701 // appropriate bit from the bitset.
702 Value *Bit = createBitSetTest(ThenB, TIL, BitOffset);
704 // The value we want is 0 if we came directly from the initial block
705 // (having failed the range or alignment checks), or the loaded bit if
706 // we came from the block in which we loaded it.
707 B.SetInsertPoint(CI);
708 PHINode *P = B.CreatePHI(Int1Ty, 2);
709 P->addIncoming(ConstantInt::get(Int1Ty, 0), InitialBB);
710 P->addIncoming(Bit, ThenB.GetInsertBlock());
714 /// Given a disjoint set of type identifiers and globals, lay out the globals,
715 /// build the bit sets and lower the llvm.type.test calls.
716 void LowerTypeTestsModule::buildBitSetsFromGlobalVariables(
717 ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Globals) {
718 // Build a new global with the combined contents of the referenced globals.
719 // This global is a struct whose even-indexed elements contain the original
720 // contents of the referenced globals and whose odd-indexed elements contain
721 // any padding required to align the next element to the next power of 2.
722 std::vector<Constant *> GlobalInits;
723 const DataLayout &DL = M.getDataLayout();
724 for (GlobalTypeMember *G : Globals) {
725 GlobalVariable *GV = cast<GlobalVariable>(G->getGlobal());
726 GlobalInits.push_back(GV->getInitializer());
727 uint64_t InitSize = DL.getTypeAllocSize(GV->getValueType());
729 // Compute the amount of padding required.
730 uint64_t Padding = NextPowerOf2(InitSize - 1) - InitSize;
732 // Cap at 128 was found experimentally to have a good data/instruction
733 // overhead tradeoff.
735 Padding = alignTo(InitSize, 128) - InitSize;
737 GlobalInits.push_back(
738 ConstantAggregateZero::get(ArrayType::get(Int8Ty, Padding)));
740 if (!GlobalInits.empty())
741 GlobalInits.pop_back();
742 Constant *NewInit = ConstantStruct::getAnon(M.getContext(), GlobalInits);
743 auto *CombinedGlobal =
744 new GlobalVariable(M, NewInit->getType(), /*isConstant=*/true,
745 GlobalValue::PrivateLinkage, NewInit);
747 StructType *NewTy = cast<StructType>(NewInit->getType());
748 const StructLayout *CombinedGlobalLayout = DL.getStructLayout(NewTy);
750 // Compute the offsets of the original globals within the new global.
751 DenseMap<GlobalTypeMember *, uint64_t> GlobalLayout;
752 for (unsigned I = 0; I != Globals.size(); ++I)
753 // Multiply by 2 to account for padding elements.
754 GlobalLayout[Globals[I]] = CombinedGlobalLayout->getElementOffset(I * 2);
756 lowerTypeTestCalls(TypeIds, CombinedGlobal, GlobalLayout);
758 // Build aliases pointing to offsets into the combined global for each
759 // global from which we built the combined global, and replace references
760 // to the original globals with references to the aliases.
761 for (unsigned I = 0; I != Globals.size(); ++I) {
762 GlobalVariable *GV = cast<GlobalVariable>(Globals[I]->getGlobal());
764 // Multiply by 2 to account for padding elements.
765 Constant *CombinedGlobalIdxs[] = {ConstantInt::get(Int32Ty, 0),
766 ConstantInt::get(Int32Ty, I * 2)};
767 Constant *CombinedGlobalElemPtr = ConstantExpr::getGetElementPtr(
768 NewInit->getType(), CombinedGlobal, CombinedGlobalIdxs);
769 assert(GV->getType()->getAddressSpace() == 0);
770 GlobalAlias *GAlias =
771 GlobalAlias::create(NewTy->getElementType(I * 2), 0, GV->getLinkage(),
772 "", CombinedGlobalElemPtr, &M);
773 GAlias->setVisibility(GV->getVisibility());
774 GAlias->takeName(GV);
775 GV->replaceAllUsesWith(GAlias);
776 GV->eraseFromParent();
780 bool LowerTypeTestsModule::shouldExportConstantsAsAbsoluteSymbols() {
781 return (Arch == Triple::x86 || Arch == Triple::x86_64) &&
782 ObjectFormat == Triple::ELF;
785 /// Export the given type identifier so that ThinLTO backends may import it.
786 /// Type identifiers are exported by adding coarse-grained information about how
787 /// to test the type identifier to the summary, and creating symbols in the
788 /// object file (aliases and absolute symbols) containing fine-grained
789 /// information about the type identifier.
791 /// Returns a pointer to the location in which to store the bitmask, if
793 uint8_t *LowerTypeTestsModule::exportTypeId(StringRef TypeId,
794 const TypeIdLowering &TIL) {
795 TypeTestResolution &TTRes =
796 ExportSummary->getOrInsertTypeIdSummary(TypeId).TTRes;
797 TTRes.TheKind = TIL.TheKind;
799 auto ExportGlobal = [&](StringRef Name, Constant *C) {
801 GlobalAlias::create(Int8Ty, 0, GlobalValue::ExternalLinkage,
802 "__typeid_" + TypeId + "_" + Name, C, &M);
803 GA->setVisibility(GlobalValue::HiddenVisibility);
806 auto ExportConstant = [&](StringRef Name, uint64_t &Storage, Constant *C) {
807 if (shouldExportConstantsAsAbsoluteSymbols())
808 ExportGlobal(Name, ConstantExpr::getIntToPtr(C, Int8PtrTy));
810 Storage = cast<ConstantInt>(C)->getZExtValue();
813 if (TIL.TheKind != TypeTestResolution::Unsat)
814 ExportGlobal("global_addr", TIL.OffsetedGlobal);
816 if (TIL.TheKind == TypeTestResolution::ByteArray ||
817 TIL.TheKind == TypeTestResolution::Inline ||
818 TIL.TheKind == TypeTestResolution::AllOnes) {
819 ExportConstant("align", TTRes.AlignLog2, TIL.AlignLog2);
820 ExportConstant("size_m1", TTRes.SizeM1, TIL.SizeM1);
822 uint64_t BitSize = cast<ConstantInt>(TIL.SizeM1)->getZExtValue() + 1;
823 if (TIL.TheKind == TypeTestResolution::Inline)
824 TTRes.SizeM1BitWidth = (BitSize <= 32) ? 5 : 6;
826 TTRes.SizeM1BitWidth = (BitSize <= 128) ? 7 : 32;
829 if (TIL.TheKind == TypeTestResolution::ByteArray) {
830 ExportGlobal("byte_array", TIL.TheByteArray);
831 if (shouldExportConstantsAsAbsoluteSymbols())
832 ExportGlobal("bit_mask", TIL.BitMask);
834 return &TTRes.BitMask;
837 if (TIL.TheKind == TypeTestResolution::Inline)
838 ExportConstant("inline_bits", TTRes.InlineBits, TIL.InlineBits);
843 LowerTypeTestsModule::TypeIdLowering
844 LowerTypeTestsModule::importTypeId(StringRef TypeId) {
845 const TypeIdSummary *TidSummary = ImportSummary->getTypeIdSummary(TypeId);
847 return {}; // Unsat: no globals match this type id.
848 const TypeTestResolution &TTRes = TidSummary->TTRes;
851 TIL.TheKind = TTRes.TheKind;
853 auto ImportGlobal = [&](StringRef Name) {
854 // Give the global a type of length 0 so that it is not assumed not to alias
855 // with any other global.
856 Constant *C = M.getOrInsertGlobal(("__typeid_" + TypeId + "_" + Name).str(),
858 if (auto *GV = dyn_cast<GlobalVariable>(C))
859 GV->setVisibility(GlobalValue::HiddenVisibility);
860 C = ConstantExpr::getBitCast(C, Int8PtrTy);
864 auto ImportConstant = [&](StringRef Name, uint64_t Const, unsigned AbsWidth,
866 if (!shouldExportConstantsAsAbsoluteSymbols()) {
868 ConstantInt::get(isa<IntegerType>(Ty) ? Ty : Int64Ty, Const);
869 if (!isa<IntegerType>(Ty))
870 C = ConstantExpr::getIntToPtr(C, Ty);
874 Constant *C = ImportGlobal(Name);
875 auto *GV = cast<GlobalVariable>(C->stripPointerCasts());
876 if (isa<IntegerType>(Ty))
877 C = ConstantExpr::getPtrToInt(C, Ty);
878 if (GV->getMetadata(LLVMContext::MD_absolute_symbol))
881 auto SetAbsRange = [&](uint64_t Min, uint64_t Max) {
882 auto *MinC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Min));
883 auto *MaxC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Max));
884 GV->setMetadata(LLVMContext::MD_absolute_symbol,
885 MDNode::get(M.getContext(), {MinC, MaxC}));
887 if (AbsWidth == IntPtrTy->getBitWidth())
888 SetAbsRange(~0ull, ~0ull); // Full set.
890 SetAbsRange(0, 1ull << AbsWidth);
894 if (TIL.TheKind != TypeTestResolution::Unsat)
895 TIL.OffsetedGlobal = ImportGlobal("global_addr");
897 if (TIL.TheKind == TypeTestResolution::ByteArray ||
898 TIL.TheKind == TypeTestResolution::Inline ||
899 TIL.TheKind == TypeTestResolution::AllOnes) {
900 TIL.AlignLog2 = ImportConstant("align", TTRes.AlignLog2, 8, Int8Ty);
902 ImportConstant("size_m1", TTRes.SizeM1, TTRes.SizeM1BitWidth, IntPtrTy);
905 if (TIL.TheKind == TypeTestResolution::ByteArray) {
906 TIL.TheByteArray = ImportGlobal("byte_array");
907 TIL.BitMask = ImportConstant("bit_mask", TTRes.BitMask, 8, Int8PtrTy);
910 if (TIL.TheKind == TypeTestResolution::Inline)
911 TIL.InlineBits = ImportConstant(
912 "inline_bits", TTRes.InlineBits, 1 << TTRes.SizeM1BitWidth,
913 TTRes.SizeM1BitWidth <= 5 ? Int32Ty : Int64Ty);
918 void LowerTypeTestsModule::importTypeTest(CallInst *CI) {
919 auto TypeIdMDVal = dyn_cast<MetadataAsValue>(CI->getArgOperand(1));
921 report_fatal_error("Second argument of llvm.type.test must be metadata");
923 auto TypeIdStr = dyn_cast<MDString>(TypeIdMDVal->getMetadata());
926 "Second argument of llvm.type.test must be a metadata string");
928 TypeIdLowering TIL = importTypeId(TypeIdStr->getString());
929 Value *Lowered = lowerTypeTestCall(TypeIdStr, CI, TIL);
930 CI->replaceAllUsesWith(Lowered);
931 CI->eraseFromParent();
934 // ThinLTO backend: the function F has a jump table entry; update this module
935 // accordingly. isDefinition describes the type of the jump table entry.
936 void LowerTypeTestsModule::importFunction(Function *F, bool isDefinition) {
937 assert(F->getType()->getAddressSpace() == 0);
939 // Declaration of a local function - nothing to do.
940 if (F->isDeclarationForLinker() && isDefinition)
943 GlobalValue::VisibilityTypes Visibility = F->getVisibility();
944 std::string Name = F->getName();
947 if (F->isDeclarationForLinker() && !isDefinition) {
948 // Declaration of an external function.
949 FDecl = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage,
950 Name + ".cfi_jt", &M);
951 FDecl->setVisibility(GlobalValue::HiddenVisibility);
952 } else if (isDefinition) {
953 F->setName(Name + ".cfi");
954 F->setLinkage(GlobalValue::ExternalLinkage);
955 F->setVisibility(GlobalValue::HiddenVisibility);
956 FDecl = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage,
958 FDecl->setVisibility(Visibility);
960 // Function definition without type metadata, where some other translation
961 // unit contained a declaration with type metadata. This normally happens
962 // during mixed CFI + non-CFI compilation. We do nothing with the function
963 // so that it is treated the same way as a function defined outside of the
968 if (F->isWeakForLinker())
969 replaceWeakDeclarationWithJumpTablePtr(F, FDecl);
971 F->replaceAllUsesWith(FDecl);
974 void LowerTypeTestsModule::lowerTypeTestCalls(
975 ArrayRef<Metadata *> TypeIds, Constant *CombinedGlobalAddr,
976 const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout) {
977 CombinedGlobalAddr = ConstantExpr::getBitCast(CombinedGlobalAddr, Int8PtrTy);
979 // For each type identifier in this disjoint set...
980 for (Metadata *TypeId : TypeIds) {
982 BitSetInfo BSI = buildBitSet(TypeId, GlobalLayout);
984 if (auto MDS = dyn_cast<MDString>(TypeId))
985 dbgs() << MDS->getString() << ": ";
987 dbgs() << "<unnamed>: ";
991 ByteArrayInfo *BAI = nullptr;
993 TIL.OffsetedGlobal = ConstantExpr::getGetElementPtr(
994 Int8Ty, CombinedGlobalAddr, ConstantInt::get(IntPtrTy, BSI.ByteOffset)),
995 TIL.AlignLog2 = ConstantInt::get(Int8Ty, BSI.AlignLog2);
996 TIL.SizeM1 = ConstantInt::get(IntPtrTy, BSI.BitSize - 1);
997 if (BSI.isAllOnes()) {
998 TIL.TheKind = (BSI.BitSize == 1) ? TypeTestResolution::Single
999 : TypeTestResolution::AllOnes;
1000 } else if (BSI.BitSize <= 64) {
1001 TIL.TheKind = TypeTestResolution::Inline;
1002 uint64_t InlineBits = 0;
1003 for (auto Bit : BSI.Bits)
1004 InlineBits |= uint64_t(1) << Bit;
1005 if (InlineBits == 0)
1006 TIL.TheKind = TypeTestResolution::Unsat;
1008 TIL.InlineBits = ConstantInt::get(
1009 (BSI.BitSize <= 32) ? Int32Ty : Int64Ty, InlineBits);
1011 TIL.TheKind = TypeTestResolution::ByteArray;
1012 ++NumByteArraysCreated;
1013 BAI = createByteArray(BSI);
1014 TIL.TheByteArray = BAI->ByteArray;
1015 TIL.BitMask = BAI->MaskGlobal;
1018 TypeIdUserInfo &TIUI = TypeIdUsers[TypeId];
1020 if (TIUI.IsExported) {
1021 uint8_t *MaskPtr = exportTypeId(cast<MDString>(TypeId)->getString(), TIL);
1023 BAI->MaskPtr = MaskPtr;
1026 // Lower each call to llvm.type.test for this type identifier.
1027 for (CallInst *CI : TIUI.CallSites) {
1028 ++NumTypeTestCallsLowered;
1029 Value *Lowered = lowerTypeTestCall(TypeId, CI, TIL);
1030 CI->replaceAllUsesWith(Lowered);
1031 CI->eraseFromParent();
1036 void LowerTypeTestsModule::verifyTypeMDNode(GlobalObject *GO, MDNode *Type) {
1037 if (Type->getNumOperands() != 2)
1038 report_fatal_error("All operands of type metadata must have 2 elements");
1040 if (GO->isThreadLocal())
1041 report_fatal_error("Bit set element may not be thread-local");
1042 if (isa<GlobalVariable>(GO) && GO->hasSection())
1044 "A member of a type identifier may not have an explicit section");
1046 // FIXME: We previously checked that global var member of a type identifier
1047 // must be a definition, but the IR linker may leave type metadata on
1048 // declarations. We should restore this check after fixing PR31759.
1050 auto OffsetConstMD = dyn_cast<ConstantAsMetadata>(Type->getOperand(0));
1052 report_fatal_error("Type offset must be a constant");
1053 auto OffsetInt = dyn_cast<ConstantInt>(OffsetConstMD->getValue());
1055 report_fatal_error("Type offset must be an integer constant");
1058 static const unsigned kX86JumpTableEntrySize = 8;
1059 static const unsigned kARMJumpTableEntrySize = 4;
1061 unsigned LowerTypeTestsModule::getJumpTableEntrySize() {
1064 case Triple::x86_64:
1065 return kX86JumpTableEntrySize;
1068 case Triple::aarch64:
1069 return kARMJumpTableEntrySize;
1071 report_fatal_error("Unsupported architecture for jump tables");
1075 // Create a jump table entry for the target. This consists of an instruction
1076 // sequence containing a relative branch to Dest. Appends inline asm text,
1077 // constraints and arguments to AsmOS, ConstraintOS and AsmArgs.
1078 void LowerTypeTestsModule::createJumpTableEntry(
1079 raw_ostream &AsmOS, raw_ostream &ConstraintOS,
1080 Triple::ArchType JumpTableArch, SmallVectorImpl<Value *> &AsmArgs,
1082 unsigned ArgIndex = AsmArgs.size();
1084 if (JumpTableArch == Triple::x86 || JumpTableArch == Triple::x86_64) {
1085 AsmOS << "jmp ${" << ArgIndex << ":c}@plt\n";
1086 AsmOS << "int3\nint3\nint3\n";
1087 } else if (JumpTableArch == Triple::arm || JumpTableArch == Triple::aarch64) {
1088 AsmOS << "b $" << ArgIndex << "\n";
1089 } else if (JumpTableArch == Triple::thumb) {
1090 AsmOS << "b.w $" << ArgIndex << "\n";
1092 report_fatal_error("Unsupported architecture for jump tables");
1095 ConstraintOS << (ArgIndex > 0 ? ",s" : "s");
1096 AsmArgs.push_back(Dest);
1099 Type *LowerTypeTestsModule::getJumpTableEntryType() {
1100 return ArrayType::get(Int8Ty, getJumpTableEntrySize());
1103 /// Given a disjoint set of type identifiers and functions, build the bit sets
1104 /// and lower the llvm.type.test calls, architecture dependently.
1105 void LowerTypeTestsModule::buildBitSetsFromFunctions(
1106 ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Functions) {
1107 if (Arch == Triple::x86 || Arch == Triple::x86_64 || Arch == Triple::arm ||
1108 Arch == Triple::thumb || Arch == Triple::aarch64)
1109 buildBitSetsFromFunctionsNative(TypeIds, Functions);
1110 else if (Arch == Triple::wasm32 || Arch == Triple::wasm64)
1111 buildBitSetsFromFunctionsWASM(TypeIds, Functions);
1113 report_fatal_error("Unsupported architecture for jump tables");
1116 void LowerTypeTestsModule::moveInitializerToModuleConstructor(
1117 GlobalVariable *GV) {
1118 if (WeakInitializerFn == nullptr) {
1119 WeakInitializerFn = Function::Create(
1120 FunctionType::get(Type::getVoidTy(M.getContext()),
1121 /* IsVarArg */ false),
1122 GlobalValue::InternalLinkage, "__cfi_global_var_init", &M);
1124 BasicBlock::Create(M.getContext(), "entry", WeakInitializerFn);
1125 ReturnInst::Create(M.getContext(), BB);
1126 WeakInitializerFn->setSection(
1127 ObjectFormat == Triple::MachO
1128 ? "__TEXT,__StaticInit,regular,pure_instructions"
1130 // This code is equivalent to relocation application, and should run at the
1131 // earliest possible time (i.e. with the highest priority).
1132 appendToGlobalCtors(M, WeakInitializerFn, /* Priority */ 0);
1135 IRBuilder<> IRB(WeakInitializerFn->getEntryBlock().getTerminator());
1136 GV->setConstant(false);
1137 IRB.CreateAlignedStore(GV->getInitializer(), GV, GV->getAlignment());
1138 GV->setInitializer(Constant::getNullValue(GV->getValueType()));
1141 void LowerTypeTestsModule::findGlobalVariableUsersOf(
1142 Constant *C, SmallSetVector<GlobalVariable *, 8> &Out) {
1143 for (auto *U : C->users()){
1144 if (auto *GV = dyn_cast<GlobalVariable>(U))
1146 else if (auto *C2 = dyn_cast<Constant>(U))
1147 findGlobalVariableUsersOf(C2, Out);
1151 // Replace all uses of F with (F ? JT : 0).
1152 void LowerTypeTestsModule::replaceWeakDeclarationWithJumpTablePtr(
1153 Function *F, Constant *JT) {
1154 // The target expression can not appear in a constant initializer on most
1155 // (all?) targets. Switch to a runtime initializer.
1156 SmallSetVector<GlobalVariable *, 8> GlobalVarUsers;
1157 findGlobalVariableUsersOf(F, GlobalVarUsers);
1158 for (auto GV : GlobalVarUsers)
1159 moveInitializerToModuleConstructor(GV);
1161 // Can not RAUW F with an expression that uses F. Replace with a temporary
1162 // placeholder first.
1163 Function *PlaceholderFn =
1164 Function::Create(cast<FunctionType>(F->getValueType()),
1165 GlobalValue::ExternalWeakLinkage, "", &M);
1166 F->replaceAllUsesWith(PlaceholderFn);
1168 Constant *Target = ConstantExpr::getSelect(
1169 ConstantExpr::getICmp(CmpInst::ICMP_NE, F,
1170 Constant::getNullValue(F->getType())),
1171 JT, Constant::getNullValue(F->getType()));
1172 PlaceholderFn->replaceAllUsesWith(Target);
1173 PlaceholderFn->eraseFromParent();
1176 static bool isThumbFunction(Function *F, Triple::ArchType ModuleArch) {
1177 Attribute TFAttr = F->getFnAttribute("target-features");
1178 if (!TFAttr.hasAttribute(Attribute::None)) {
1179 SmallVector<StringRef, 6> Features;
1180 TFAttr.getValueAsString().split(Features, ',');
1181 for (StringRef Feature : Features) {
1182 if (Feature == "-thumb-mode")
1184 else if (Feature == "+thumb-mode")
1189 return ModuleArch == Triple::thumb;
1192 // Each jump table must be either ARM or Thumb as a whole for the bit-test math
1193 // to work. Pick one that matches the majority of members to minimize interop
1194 // veneers inserted by the linker.
1195 static Triple::ArchType
1196 selectJumpTableArmEncoding(ArrayRef<GlobalTypeMember *> Functions,
1197 Triple::ArchType ModuleArch) {
1198 if (ModuleArch != Triple::arm && ModuleArch != Triple::thumb)
1201 unsigned ArmCount = 0, ThumbCount = 0;
1202 for (const auto GTM : Functions) {
1203 if (!GTM->isDefinition()) {
1204 // PLT stubs are always ARM.
1209 Function *F = cast<Function>(GTM->getGlobal());
1210 ++(isThumbFunction(F, ModuleArch) ? ThumbCount : ArmCount);
1213 return ArmCount > ThumbCount ? Triple::arm : Triple::thumb;
1216 void LowerTypeTestsModule::createJumpTable(
1217 Function *F, ArrayRef<GlobalTypeMember *> Functions) {
1218 std::string AsmStr, ConstraintStr;
1219 raw_string_ostream AsmOS(AsmStr), ConstraintOS(ConstraintStr);
1220 SmallVector<Value *, 16> AsmArgs;
1221 AsmArgs.reserve(Functions.size() * 2);
1223 Triple::ArchType JumpTableArch = selectJumpTableArmEncoding(Functions, Arch);
1225 for (unsigned I = 0; I != Functions.size(); ++I)
1226 createJumpTableEntry(AsmOS, ConstraintOS, JumpTableArch, AsmArgs,
1227 cast<Function>(Functions[I]->getGlobal()));
1229 // Try to emit the jump table at the end of the text segment.
1230 // Jump table must come after __cfi_check in the cross-dso mode.
1231 // FIXME: this magic section name seems to do the trick.
1232 F->setSection(ObjectFormat == Triple::MachO
1233 ? "__TEXT,__text,regular,pure_instructions"
1235 // Align the whole table by entry size.
1236 F->setAlignment(getJumpTableEntrySize());
1238 // Disabled on win32 due to https://llvm.org/bugs/show_bug.cgi?id=28641#c3.
1239 // Luckily, this function does not get any prologue even without the
1241 if (OS != Triple::Win32)
1242 F->addFnAttr(Attribute::Naked);
1243 if (JumpTableArch == Triple::arm)
1244 F->addFnAttr("target-features", "-thumb-mode");
1245 if (JumpTableArch == Triple::thumb) {
1246 F->addFnAttr("target-features", "+thumb-mode");
1247 // Thumb jump table assembly needs Thumb2. The following attribute is added
1248 // by Clang for -march=armv7.
1249 F->addFnAttr("target-cpu", "cortex-a8");
1252 BasicBlock *BB = BasicBlock::Create(M.getContext(), "entry", F);
1253 IRBuilder<> IRB(BB);
1255 SmallVector<Type *, 16> ArgTypes;
1256 ArgTypes.reserve(AsmArgs.size());
1257 for (const auto &Arg : AsmArgs)
1258 ArgTypes.push_back(Arg->getType());
1259 InlineAsm *JumpTableAsm =
1260 InlineAsm::get(FunctionType::get(IRB.getVoidTy(), ArgTypes, false),
1261 AsmOS.str(), ConstraintOS.str(),
1262 /*hasSideEffects=*/true);
1264 IRB.CreateCall(JumpTableAsm, AsmArgs);
1265 IRB.CreateUnreachable();
1268 /// Given a disjoint set of type identifiers and functions, build a jump table
1269 /// for the functions, build the bit sets and lower the llvm.type.test calls.
1270 void LowerTypeTestsModule::buildBitSetsFromFunctionsNative(
1271 ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Functions) {
1272 // Unlike the global bitset builder, the function bitset builder cannot
1273 // re-arrange functions in a particular order and base its calculations on the
1274 // layout of the functions' entry points, as we have no idea how large a
1275 // particular function will end up being (the size could even depend on what
1276 // this pass does!) Instead, we build a jump table, which is a block of code
1277 // consisting of one branch instruction for each of the functions in the bit
1278 // set that branches to the target function, and redirect any taken function
1279 // addresses to the corresponding jump table entry. In the object file's
1280 // symbol table, the symbols for the target functions also refer to the jump
1281 // table entries, so that addresses taken outside the module will pass any
1282 // verification done inside the module.
1284 // In more concrete terms, suppose we have three functions f, g, h which are
1285 // of the same type, and a function foo that returns their addresses:
1305 // We output the jump table as module-level inline asm string. The end result
1306 // will (conceptually) look like this:
1308 // f = .cfi.jumptable
1309 // g = .cfi.jumptable + 4
1310 // h = .cfi.jumptable + 8
1312 // jmp f.cfi ; 5 bytes
1316 // jmp g.cfi ; 5 bytes
1320 // jmp h.cfi ; 5 bytes
1343 // Because the addresses of f, g, h are evenly spaced at a power of 2, in the
1344 // normal case the check can be carried out using the same kind of simple
1345 // arithmetic that we normally use for globals.
1347 // FIXME: find a better way to represent the jumptable in the IR.
1348 assert(!Functions.empty());
1350 // Build a simple layout based on the regular layout of jump tables.
1351 DenseMap<GlobalTypeMember *, uint64_t> GlobalLayout;
1352 unsigned EntrySize = getJumpTableEntrySize();
1353 for (unsigned I = 0; I != Functions.size(); ++I)
1354 GlobalLayout[Functions[I]] = I * EntrySize;
1356 Function *JumpTableFn =
1357 Function::Create(FunctionType::get(Type::getVoidTy(M.getContext()),
1358 /* IsVarArg */ false),
1359 GlobalValue::PrivateLinkage, ".cfi.jumptable", &M);
1360 ArrayType *JumpTableType =
1361 ArrayType::get(getJumpTableEntryType(), Functions.size());
1363 ConstantExpr::getPointerCast(JumpTableFn, JumpTableType->getPointerTo(0));
1365 lowerTypeTestCalls(TypeIds, JumpTable, GlobalLayout);
1367 // Build aliases pointing to offsets into the jump table, and replace
1368 // references to the original functions with references to the aliases.
1369 for (unsigned I = 0; I != Functions.size(); ++I) {
1370 Function *F = cast<Function>(Functions[I]->getGlobal());
1371 bool IsDefinition = Functions[I]->isDefinition();
1373 Constant *CombinedGlobalElemPtr = ConstantExpr::getBitCast(
1374 ConstantExpr::getInBoundsGetElementPtr(
1375 JumpTableType, JumpTable,
1376 ArrayRef<Constant *>{ConstantInt::get(IntPtrTy, 0),
1377 ConstantInt::get(IntPtrTy, I)}),
1379 if (Functions[I]->isExported()) {
1381 ExportSummary->cfiFunctionDefs().insert(F->getName());
1383 GlobalAlias *JtAlias = GlobalAlias::create(
1384 F->getValueType(), 0, GlobalValue::ExternalLinkage,
1385 F->getName() + ".cfi_jt", CombinedGlobalElemPtr, &M);
1386 JtAlias->setVisibility(GlobalValue::HiddenVisibility);
1387 ExportSummary->cfiFunctionDecls().insert(F->getName());
1390 if (!IsDefinition) {
1391 if (F->isWeakForLinker())
1392 replaceWeakDeclarationWithJumpTablePtr(F, CombinedGlobalElemPtr);
1394 F->replaceAllUsesWith(CombinedGlobalElemPtr);
1396 assert(F->getType()->getAddressSpace() == 0);
1398 GlobalAlias *FAlias = GlobalAlias::create(
1399 F->getValueType(), 0, F->getLinkage(), "", CombinedGlobalElemPtr, &M);
1400 FAlias->setVisibility(F->getVisibility());
1401 FAlias->takeName(F);
1402 if (FAlias->hasName())
1403 F->setName(FAlias->getName() + ".cfi");
1404 F->replaceUsesExceptBlockAddr(FAlias);
1406 if (!F->isDeclarationForLinker())
1407 F->setLinkage(GlobalValue::InternalLinkage);
1410 createJumpTable(JumpTableFn, Functions);
1413 /// Assign a dummy layout using an incrementing counter, tag each function
1414 /// with its index represented as metadata, and lower each type test to an
1415 /// integer range comparison. During generation of the indirect function call
1416 /// table in the backend, it will assign the given indexes.
1417 /// Note: Dynamic linking is not supported, as the WebAssembly ABI has not yet
1419 void LowerTypeTestsModule::buildBitSetsFromFunctionsWASM(
1420 ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Functions) {
1421 assert(!Functions.empty());
1423 // Build consecutive monotonic integer ranges for each call target set
1424 DenseMap<GlobalTypeMember *, uint64_t> GlobalLayout;
1426 for (GlobalTypeMember *GTM : Functions) {
1427 Function *F = cast<Function>(GTM->getGlobal());
1429 // Skip functions that are not address taken, to avoid bloating the table
1430 if (!F->hasAddressTaken())
1433 // Store metadata with the index for each function
1434 MDNode *MD = MDNode::get(F->getContext(),
1435 ArrayRef<Metadata *>(ConstantAsMetadata::get(
1436 ConstantInt::get(Int64Ty, IndirectIndex))));
1437 F->setMetadata("wasm.index", MD);
1439 // Assign the counter value
1440 GlobalLayout[GTM] = IndirectIndex++;
1443 // The indirect function table index space starts at zero, so pass a NULL
1444 // pointer as the subtracted "jump table" offset.
1445 lowerTypeTestCalls(TypeIds, ConstantPointerNull::get(Int32PtrTy),
1449 void LowerTypeTestsModule::buildBitSetsFromDisjointSet(
1450 ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Globals) {
1451 DenseMap<Metadata *, uint64_t> TypeIdIndices;
1452 for (unsigned I = 0; I != TypeIds.size(); ++I)
1453 TypeIdIndices[TypeIds[I]] = I;
1455 // For each type identifier, build a set of indices that refer to members of
1456 // the type identifier.
1457 std::vector<std::set<uint64_t>> TypeMembers(TypeIds.size());
1458 unsigned GlobalIndex = 0;
1459 for (GlobalTypeMember *GTM : Globals) {
1460 for (MDNode *Type : GTM->types()) {
1461 // Type = { offset, type identifier }
1462 unsigned TypeIdIndex = TypeIdIndices[Type->getOperand(1)];
1463 TypeMembers[TypeIdIndex].insert(GlobalIndex);
1468 // Order the sets of indices by size. The GlobalLayoutBuilder works best
1469 // when given small index sets first.
1471 TypeMembers.begin(), TypeMembers.end(),
1472 [](const std::set<uint64_t> &O1, const std::set<uint64_t> &O2) {
1473 return O1.size() < O2.size();
1476 // Create a GlobalLayoutBuilder and provide it with index sets as layout
1477 // fragments. The GlobalLayoutBuilder tries to lay out members of fragments as
1478 // close together as possible.
1479 GlobalLayoutBuilder GLB(Globals.size());
1480 for (auto &&MemSet : TypeMembers)
1481 GLB.addFragment(MemSet);
1483 // Build a vector of globals with the computed layout.
1485 Globals.empty() || isa<GlobalVariable>(Globals[0]->getGlobal());
1486 std::vector<GlobalTypeMember *> OrderedGTMs(Globals.size());
1487 auto OGTMI = OrderedGTMs.begin();
1488 for (auto &&F : GLB.Fragments) {
1489 for (auto &&Offset : F) {
1490 if (IsGlobalSet != isa<GlobalVariable>(Globals[Offset]->getGlobal()))
1491 report_fatal_error("Type identifier may not contain both global "
1492 "variables and functions");
1493 *OGTMI++ = Globals[Offset];
1497 // Build the bitsets from this disjoint set.
1499 buildBitSetsFromGlobalVariables(TypeIds, OrderedGTMs);
1501 buildBitSetsFromFunctions(TypeIds, OrderedGTMs);
1504 /// Lower all type tests in this module.
1505 LowerTypeTestsModule::LowerTypeTestsModule(
1506 Module &M, ModuleSummaryIndex *ExportSummary,
1507 const ModuleSummaryIndex *ImportSummary)
1508 : M(M), ExportSummary(ExportSummary), ImportSummary(ImportSummary) {
1509 assert(!(ExportSummary && ImportSummary));
1510 Triple TargetTriple(M.getTargetTriple());
1511 Arch = TargetTriple.getArch();
1512 OS = TargetTriple.getOS();
1513 ObjectFormat = TargetTriple.getObjectFormat();
1516 bool LowerTypeTestsModule::runForTesting(Module &M) {
1517 ModuleSummaryIndex Summary;
1519 // Handle the command-line summary arguments. This code is for testing
1520 // purposes only, so we handle errors directly.
1521 if (!ClReadSummary.empty()) {
1522 ExitOnError ExitOnErr("-lowertypetests-read-summary: " + ClReadSummary +
1524 auto ReadSummaryFile =
1525 ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ClReadSummary)));
1527 yaml::Input In(ReadSummaryFile->getBuffer());
1529 ExitOnErr(errorCodeToError(In.error()));
1533 LowerTypeTestsModule(
1534 M, ClSummaryAction == PassSummaryAction::Export ? &Summary : nullptr,
1535 ClSummaryAction == PassSummaryAction::Import ? &Summary : nullptr)
1538 if (!ClWriteSummary.empty()) {
1539 ExitOnError ExitOnErr("-lowertypetests-write-summary: " + ClWriteSummary +
1542 raw_fd_ostream OS(ClWriteSummary, EC, sys::fs::F_Text);
1543 ExitOnErr(errorCodeToError(EC));
1545 yaml::Output Out(OS);
1552 bool LowerTypeTestsModule::lower() {
1553 Function *TypeTestFunc =
1554 M.getFunction(Intrinsic::getName(Intrinsic::type_test));
1555 if ((!TypeTestFunc || TypeTestFunc->use_empty()) && !ExportSummary &&
1559 if (ImportSummary) {
1561 for (auto UI = TypeTestFunc->use_begin(), UE = TypeTestFunc->use_end();
1563 auto *CI = cast<CallInst>((*UI++).getUser());
1568 SmallVector<Function *, 8> Defs;
1569 SmallVector<Function *, 8> Decls;
1571 // CFI functions are either external, or promoted. A local function may
1572 // have the same name, but it's not the one we are looking for.
1573 if (F.hasLocalLinkage())
1575 if (ImportSummary->cfiFunctionDefs().count(F.getName()))
1577 else if (ImportSummary->cfiFunctionDecls().count(F.getName()))
1578 Decls.push_back(&F);
1582 importFunction(F, /*isDefinition*/ true);
1583 for (auto F : Decls)
1584 importFunction(F, /*isDefinition*/ false);
1589 // Equivalence class set containing type identifiers and the globals that
1590 // reference them. This is used to partition the set of type identifiers in
1591 // the module into disjoint sets.
1592 using GlobalClassesTy =
1593 EquivalenceClasses<PointerUnion<GlobalTypeMember *, Metadata *>>;
1594 GlobalClassesTy GlobalClasses;
1596 // Verify the type metadata and build a few data structures to let us
1597 // efficiently enumerate the type identifiers associated with a global:
1598 // a list of GlobalTypeMembers (a GlobalObject stored alongside a vector
1599 // of associated type metadata) and a mapping from type identifiers to their
1600 // list of GlobalTypeMembers and last observed index in the list of globals.
1601 // The indices will be used later to deterministically order the list of type
1603 BumpPtrAllocator Alloc;
1606 std::vector<GlobalTypeMember *> RefGlobals;
1608 DenseMap<Metadata *, TIInfo> TypeIdInfo;
1610 SmallVector<MDNode *, 2> Types;
1612 struct ExportedFunctionInfo {
1613 CfiFunctionLinkage Linkage;
1614 MDNode *FuncMD; // {name, linkage, type[, type...]}
1616 DenseMap<StringRef, ExportedFunctionInfo> ExportedFunctions;
1617 if (ExportSummary) {
1618 NamedMDNode *CfiFunctionsMD = M.getNamedMetadata("cfi.functions");
1619 if (CfiFunctionsMD) {
1620 for (auto FuncMD : CfiFunctionsMD->operands()) {
1621 assert(FuncMD->getNumOperands() >= 2);
1622 StringRef FunctionName =
1623 cast<MDString>(FuncMD->getOperand(0))->getString();
1624 if (!ExportSummary->isGUIDLive(GlobalValue::getGUID(
1625 GlobalValue::dropLLVMManglingEscape(FunctionName))))
1627 CfiFunctionLinkage Linkage = static_cast<CfiFunctionLinkage>(
1628 cast<ConstantAsMetadata>(FuncMD->getOperand(1))
1630 ->getUniqueInteger()
1632 auto P = ExportedFunctions.insert({FunctionName, {Linkage, FuncMD}});
1633 if (!P.second && P.first->second.Linkage != CFL_Definition)
1634 P.first->second = {Linkage, FuncMD};
1637 for (const auto &P : ExportedFunctions) {
1638 StringRef FunctionName = P.first;
1639 CfiFunctionLinkage Linkage = P.second.Linkage;
1640 MDNode *FuncMD = P.second.FuncMD;
1641 Function *F = M.getFunction(FunctionName);
1643 F = Function::Create(
1644 FunctionType::get(Type::getVoidTy(M.getContext()), false),
1645 GlobalVariable::ExternalLinkage, FunctionName, &M);
1647 // If the function is available_externally, remove its definition so
1648 // that it is handled the same way as a declaration. Later we will try
1649 // to create an alias using this function's linkage, which will fail if
1650 // the linkage is available_externally. This will also result in us
1651 // following the code path below to replace the type metadata.
1652 if (F->hasAvailableExternallyLinkage()) {
1653 F->setLinkage(GlobalValue::ExternalLinkage);
1655 F->setComdat(nullptr);
1659 // If the function in the full LTO module is a declaration, replace its
1660 // type metadata with the type metadata we found in cfi.functions. That
1661 // metadata is presumed to be more accurate than the metadata attached
1662 // to the declaration.
1663 if (F->isDeclaration()) {
1664 if (Linkage == CFL_WeakDeclaration)
1665 F->setLinkage(GlobalValue::ExternalWeakLinkage);
1667 F->eraseMetadata(LLVMContext::MD_type);
1668 for (unsigned I = 2; I < FuncMD->getNumOperands(); ++I)
1669 F->addMetadata(LLVMContext::MD_type,
1670 *cast<MDNode>(FuncMD->getOperand(I).get()));
1676 for (GlobalObject &GO : M.global_objects()) {
1677 if (isa<GlobalVariable>(GO) && GO.isDeclarationForLinker())
1681 GO.getMetadata(LLVMContext::MD_type, Types);
1685 bool IsDefinition = !GO.isDeclarationForLinker();
1686 bool IsExported = false;
1687 if (isa<Function>(GO) && ExportedFunctions.count(GO.getName())) {
1688 IsDefinition |= ExportedFunctions[GO.getName()].Linkage == CFL_Definition;
1693 GlobalTypeMember::create(Alloc, &GO, IsDefinition, IsExported, Types);
1694 for (MDNode *Type : Types) {
1695 verifyTypeMDNode(&GO, Type);
1696 auto &Info = TypeIdInfo[Type->getOperand(1)];
1698 Info.RefGlobals.push_back(GTM);
1702 auto AddTypeIdUse = [&](Metadata *TypeId) -> TypeIdUserInfo & {
1703 // Add the call site to the list of call sites for this type identifier. We
1704 // also use TypeIdUsers to keep track of whether we have seen this type
1705 // identifier before. If we have, we don't need to re-add the referenced
1706 // globals to the equivalence class.
1707 auto Ins = TypeIdUsers.insert({TypeId, {}});
1709 // Add the type identifier to the equivalence class.
1710 GlobalClassesTy::iterator GCI = GlobalClasses.insert(TypeId);
1711 GlobalClassesTy::member_iterator CurSet = GlobalClasses.findLeader(GCI);
1713 // Add the referenced globals to the type identifier's equivalence class.
1714 for (GlobalTypeMember *GTM : TypeIdInfo[TypeId].RefGlobals)
1715 CurSet = GlobalClasses.unionSets(
1716 CurSet, GlobalClasses.findLeader(GlobalClasses.insert(GTM)));
1719 return Ins.first->second;
1723 for (const Use &U : TypeTestFunc->uses()) {
1724 auto CI = cast<CallInst>(U.getUser());
1726 auto TypeIdMDVal = dyn_cast<MetadataAsValue>(CI->getArgOperand(1));
1728 report_fatal_error("Second argument of llvm.type.test must be metadata");
1729 auto TypeId = TypeIdMDVal->getMetadata();
1730 AddTypeIdUse(TypeId).CallSites.push_back(CI);
1734 if (ExportSummary) {
1735 DenseMap<GlobalValue::GUID, TinyPtrVector<Metadata *>> MetadataByGUID;
1736 for (auto &P : TypeIdInfo) {
1737 if (auto *TypeId = dyn_cast<MDString>(P.first))
1738 MetadataByGUID[GlobalValue::getGUID(TypeId->getString())].push_back(
1742 for (auto &P : *ExportSummary) {
1743 for (auto &S : P.second.SummaryList) {
1744 if (!ExportSummary->isGlobalValueLive(S.get()))
1746 if (auto *FS = dyn_cast<FunctionSummary>(S->getBaseObject()))
1747 for (GlobalValue::GUID G : FS->type_tests())
1748 for (Metadata *MD : MetadataByGUID[G])
1749 AddTypeIdUse(MD).IsExported = true;
1754 if (GlobalClasses.empty())
1757 // Build a list of disjoint sets ordered by their maximum global index for
1759 std::vector<std::pair<GlobalClassesTy::iterator, unsigned>> Sets;
1760 for (GlobalClassesTy::iterator I = GlobalClasses.begin(),
1761 E = GlobalClasses.end();
1765 ++NumTypeIdDisjointSets;
1767 unsigned MaxIndex = 0;
1768 for (GlobalClassesTy::member_iterator MI = GlobalClasses.member_begin(I);
1769 MI != GlobalClasses.member_end(); ++MI) {
1770 if ((*MI).is<Metadata *>())
1771 MaxIndex = std::max(MaxIndex, TypeIdInfo[MI->get<Metadata *>()].Index);
1773 Sets.emplace_back(I, MaxIndex);
1775 std::sort(Sets.begin(), Sets.end(),
1776 [](const std::pair<GlobalClassesTy::iterator, unsigned> &S1,
1777 const std::pair<GlobalClassesTy::iterator, unsigned> &S2) {
1778 return S1.second < S2.second;
1781 // For each disjoint set we found...
1782 for (const auto &S : Sets) {
1783 // Build the list of type identifiers in this disjoint set.
1784 std::vector<Metadata *> TypeIds;
1785 std::vector<GlobalTypeMember *> Globals;
1786 for (GlobalClassesTy::member_iterator MI =
1787 GlobalClasses.member_begin(S.first);
1788 MI != GlobalClasses.member_end(); ++MI) {
1789 if ((*MI).is<Metadata *>())
1790 TypeIds.push_back(MI->get<Metadata *>());
1792 Globals.push_back(MI->get<GlobalTypeMember *>());
1795 // Order type identifiers by global index for determinism. This ordering is
1796 // stable as there is a one-to-one mapping between metadata and indices.
1797 std::sort(TypeIds.begin(), TypeIds.end(), [&](Metadata *M1, Metadata *M2) {
1798 return TypeIdInfo[M1].Index < TypeIdInfo[M2].Index;
1801 // Build bitsets for this disjoint set.
1802 buildBitSetsFromDisjointSet(TypeIds, Globals);
1805 allocateByteArrays();
1810 PreservedAnalyses LowerTypeTestsPass::run(Module &M,
1811 ModuleAnalysisManager &AM) {
1812 bool Changed = LowerTypeTestsModule(M, /*ExportSummary=*/nullptr,
1813 /*ImportSummary=*/nullptr)
1816 return PreservedAnalyses::all();
1817 return PreservedAnalyses::none();