LLVM 23.0.0git
LowerTypeTests.cpp
Go to the documentation of this file.
1//===- LowerTypeTests.cpp - type metadata lowering pass -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass lowers type metadata and calls to the llvm.type.test intrinsic.
10// It also ensures that globals are properly laid out for the
11// llvm.icall.branch.funnel intrinsic.
12// See http://llvm.org/docs/TypeMetadata.html for more information.
13//
14//===----------------------------------------------------------------------===//
15
17#include "llvm/ADT/APInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/STLExtras.h"
24#include "llvm/ADT/SetVector.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/StringRef.h"
34#include "llvm/IR/Attributes.h"
35#include "llvm/IR/BasicBlock.h"
36#include "llvm/IR/Constant.h"
37#include "llvm/IR/Constants.h"
38#include "llvm/IR/DataLayout.h"
40#include "llvm/IR/Function.h"
41#include "llvm/IR/GlobalAlias.h"
43#include "llvm/IR/GlobalValue.h"
45#include "llvm/IR/IRBuilder.h"
46#include "llvm/IR/InlineAsm.h"
47#include "llvm/IR/Instruction.h"
50#include "llvm/IR/Intrinsics.h"
51#include "llvm/IR/LLVMContext.h"
52#include "llvm/IR/MDBuilder.h"
53#include "llvm/IR/Metadata.h"
54#include "llvm/IR/Module.h"
57#include "llvm/IR/Operator.h"
58#include "llvm/IR/PassManager.h"
61#include "llvm/IR/Type.h"
62#include "llvm/IR/Use.h"
63#include "llvm/IR/User.h"
64#include "llvm/IR/Value.h"
68#include "llvm/Support/Debug.h"
69#include "llvm/Support/Error.h"
78#include "llvm/Transforms/IPO.h"
81#include <algorithm>
82#include <cassert>
83#include <cstdint>
84#include <set>
85#include <string>
86#include <system_error>
87#include <utility>
88#include <vector>
89
90using namespace llvm;
91using namespace lowertypetests;
92
93#define DEBUG_TYPE "lowertypetests"
94
95STATISTIC(ByteArraySizeBits, "Byte array size in bits");
96STATISTIC(ByteArraySizeBytes, "Byte array size in bytes");
97STATISTIC(NumByteArraysCreated, "Number of byte arrays created");
98STATISTIC(NumTypeTestCallsLowered, "Number of type test calls lowered");
99STATISTIC(NumTypeIdDisjointSets, "Number of disjoint sets of type identifiers");
100
102 "lowertypetests-avoid-reuse",
103 cl::desc("Try to avoid reuse of byte array addresses using aliases"),
104 cl::Hidden, cl::init(true));
105
107 "lowertypetests-summary-action",
108 cl::desc("What to do with the summary when running this pass"),
109 cl::values(clEnumValN(PassSummaryAction::None, "none", "Do nothing"),
111 "Import typeid resolutions from summary and globals"),
113 "Export typeid resolutions to summary and globals")),
114 cl::Hidden);
115
117 "lowertypetests-read-summary",
118 cl::desc("Read summary from given YAML file before running pass"),
119 cl::Hidden);
120
122 "lowertypetests-write-summary",
123 cl::desc("Write summary to given YAML file after running pass"),
124 cl::Hidden);
125
127 if (Offset < ByteOffset)
128 return false;
129
130 if ((Offset - ByteOffset) % (uint64_t(1) << AlignLog2) != 0)
131 return false;
132
133 uint64_t BitOffset = (Offset - ByteOffset) >> AlignLog2;
134 if (BitOffset >= BitSize)
135 return false;
136
137 return Bits.count(BitSize - 1 - BitOffset);
138}
139
141 OS << "offset " << ByteOffset << " size " << BitSize << " align "
142 << (1 << AlignLog2);
143
144 if (isAllOnes()) {
145 OS << " all-ones\n";
146 return;
147 }
148
149 OS << " { ";
150 for (uint64_t B : Bits)
151 OS << B << ' ';
152 OS << "}\n";
153}
154
156 if (Min > Max)
157 Min = 0;
158
159 // Normalize each offset against the minimum observed offset, and compute
160 // the bitwise OR of each of the offsets. The number of trailing zeros
161 // in the mask gives us the log2 of the alignment of all offsets, which
162 // allows us to compress the bitset by only storing one bit per aligned
163 // address.
164 uint64_t Mask = 0;
165 for (uint64_t &Offset : Offsets) {
166 Offset -= Min;
167 Mask |= Offset;
168 }
169
170 BitSetInfo BSI;
171 BSI.ByteOffset = Min;
172
173 BSI.AlignLog2 = 0;
174 if (Mask != 0)
175 BSI.AlignLog2 = llvm::countr_zero(Mask);
176
177 // Build the compressed bitset while normalizing the offsets against the
178 // computed alignment.
179 BSI.BitSize = ((Max - Min) >> BSI.AlignLog2) + 1;
180 for (uint64_t Offset : Offsets) {
181 Offset >>= BSI.AlignLog2;
182 // We invert the order of bits when adding them to the bitset. This is
183 // because the offset that we test against is computed by subtracting the
184 // address that we are testing from the global's address, which means that
185 // the offset increases as the tested address decreases.
186 BSI.Bits.insert(BSI.BitSize - 1 - Offset);
187 }
188
189 return BSI;
190}
191
192void GlobalLayoutBuilder::addFragment(const std::set<uint64_t> &F) {
193 // Create a new fragment to hold the layout for F.
194 Fragments.emplace_back();
195 std::vector<uint64_t> &Fragment = Fragments.back();
196 uint64_t FragmentIndex = Fragments.size() - 1;
197
198 for (auto ObjIndex : F) {
199 uint64_t OldFragmentIndex = FragmentMap[ObjIndex];
200 if (OldFragmentIndex == 0) {
201 // We haven't seen this object index before, so just add it to the current
202 // fragment.
203 Fragment.push_back(ObjIndex);
204 } else {
205 // This index belongs to an existing fragment. Copy the elements of the
206 // old fragment into this one and clear the old fragment. We don't update
207 // the fragment map just yet, this ensures that any further references to
208 // indices from the old fragment in this fragment do not insert any more
209 // indices.
210 std::vector<uint64_t> &OldFragment = Fragments[OldFragmentIndex];
211 llvm::append_range(Fragment, OldFragment);
212 OldFragment.clear();
213 }
214 }
215
216 // Update the fragment map to point our object indices to this fragment.
217 for (uint64_t ObjIndex : Fragment)
218 FragmentMap[ObjIndex] = FragmentIndex;
219}
220
221void ByteArrayBuilder::allocate(const std::set<uint64_t> &Bits,
222 uint64_t BitSize, uint64_t &AllocByteOffset,
223 uint8_t &AllocMask) {
224 // Find the smallest current allocation.
225 unsigned Bit = 0;
226 for (unsigned I = 1; I != BitsPerByte; ++I)
227 if (BitAllocs[I] < BitAllocs[Bit])
228 Bit = I;
229
230 AllocByteOffset = BitAllocs[Bit];
231
232 // Add our size to it.
233 unsigned ReqSize = AllocByteOffset + BitSize;
234 BitAllocs[Bit] = ReqSize;
235 if (Bytes.size() < ReqSize)
236 Bytes.resize(ReqSize);
237
238 // Set our bits.
239 AllocMask = 1 << Bit;
240 for (uint64_t B : Bits)
241 Bytes[AllocByteOffset + B] |= AllocMask;
242}
243
245 if (F->isDeclarationForLinker())
246 return false;
248 F->getParent()->getModuleFlag("CFI Canonical Jump Tables"));
249 if (!CI || !CI->isZero())
250 return true;
251 return F->hasFnAttribute("cfi-canonical-jump-table");
252}
253
254namespace {
255
256struct ByteArrayInfo {
257 std::set<uint64_t> Bits;
258 uint64_t BitSize;
259 GlobalVariable *ByteArray;
260 GlobalVariable *MaskGlobal;
261 uint8_t *MaskPtr = nullptr;
262};
263
264/// A POD-like structure that we use to store a global reference together with
265/// its metadata types. In this pass we frequently need to query the set of
266/// metadata types referenced by a global, which at the IR level is an expensive
267/// operation involving a map lookup; this data structure helps to reduce the
268/// number of times we need to do this lookup.
269class GlobalTypeMember final : TrailingObjects<GlobalTypeMember, MDNode *> {
270 friend TrailingObjects;
271
272 GlobalObject *GO;
273 size_t NTypes;
274
275 // For functions: true if the jump table is canonical. This essentially means
276 // whether the canonical address (i.e. the symbol table entry) of the function
277 // is provided by the local jump table. This is normally the same as whether
278 // the function is defined locally, but if canonical jump tables are disabled
279 // by the user then the jump table never provides a canonical definition.
280 bool IsJumpTableCanonical;
281
282 // For functions: true if this function is either defined or used in a thinlto
283 // module and its jumptable entry needs to be exported to thinlto backends.
284 bool IsExported;
285
286public:
287 static GlobalTypeMember *create(BumpPtrAllocator &Alloc, GlobalObject *GO,
288 bool IsJumpTableCanonical, bool IsExported,
289 ArrayRef<MDNode *> Types) {
290 auto *GTM = static_cast<GlobalTypeMember *>(Alloc.Allocate(
291 totalSizeToAlloc<MDNode *>(Types.size()), alignof(GlobalTypeMember)));
292 GTM->GO = GO;
293 GTM->NTypes = Types.size();
294 GTM->IsJumpTableCanonical = IsJumpTableCanonical;
295 GTM->IsExported = IsExported;
296 llvm::copy(Types, GTM->getTrailingObjects());
297 return GTM;
298 }
299
300 GlobalObject *getGlobal() const {
301 return GO;
302 }
303
304 bool isJumpTableCanonical() const {
305 return IsJumpTableCanonical;
306 }
307
308 bool isExported() const {
309 return IsExported;
310 }
311
312 ArrayRef<MDNode *> types() const { return getTrailingObjects(NTypes); }
313};
314
315struct ICallBranchFunnel final
316 : TrailingObjects<ICallBranchFunnel, GlobalTypeMember *> {
317 static ICallBranchFunnel *create(BumpPtrAllocator &Alloc, CallInst *CI,
319 unsigned UniqueId) {
320 auto *Call = static_cast<ICallBranchFunnel *>(
321 Alloc.Allocate(totalSizeToAlloc<GlobalTypeMember *>(Targets.size()),
322 alignof(ICallBranchFunnel)));
323 Call->CI = CI;
324 Call->UniqueId = UniqueId;
325 Call->NTargets = Targets.size();
326 llvm::copy(Targets, Call->getTrailingObjects());
327 return Call;
328 }
329
330 CallInst *CI;
331 ArrayRef<GlobalTypeMember *> targets() const {
332 return getTrailingObjects(NTargets);
333 }
334
335 unsigned UniqueId;
336
337private:
338 size_t NTargets;
339};
340
341struct ScopedSaveAliaseesAndUsed {
342 Module &M;
344 std::vector<std::pair<GlobalAlias *, Function *>> FunctionAliases;
345 std::vector<std::pair<GlobalIFunc *, Function *>> ResolverIFuncs;
346
347 // This function only removes functions from llvm.used and llvm.compiler.used.
348 // We cannot remove global variables because they need to follow RAUW, as
349 // they may be deleted by buildBitSetsFromGlobalVariables.
350 void collectAndEraseUsedFunctions(Module &M,
351 SmallVectorImpl<GlobalValue *> &Vec,
352 bool CompilerUsed) {
353 auto *GV = collectUsedGlobalVariables(M, Vec, CompilerUsed);
354 if (!GV)
355 return;
356 // There's no API to only remove certain array elements from
357 // llvm.used/llvm.compiler.used, so we remove all of them and add back only
358 // the non-functions.
359 GV->eraseFromParent();
360 auto NonFuncBegin =
361 std::stable_partition(Vec.begin(), Vec.end(), [](GlobalValue *GV) {
362 return isa<Function>(GV);
363 });
364 if (CompilerUsed)
365 appendToCompilerUsed(M, {NonFuncBegin, Vec.end()});
366 else
367 appendToUsed(M, {NonFuncBegin, Vec.end()});
368 Vec.resize(NonFuncBegin - Vec.begin());
369 }
370
371 ScopedSaveAliaseesAndUsed(Module &M) : M(M) {
372 // The users of this class want to replace all function references except
373 // for aliases and llvm.used/llvm.compiler.used with references to a jump
374 // table. We avoid replacing aliases in order to avoid introducing a double
375 // indirection (or an alias pointing to a declaration in ThinLTO mode), and
376 // we avoid replacing llvm.used/llvm.compiler.used because these global
377 // variables describe properties of the global, not the jump table (besides,
378 // offseted references to the jump table in llvm.used are invalid).
379 // Unfortunately, LLVM doesn't have a "RAUW except for these (possibly
380 // indirect) users", so what we do is save the list of globals referenced by
381 // llvm.used/llvm.compiler.used and aliases, erase the used lists, let RAUW
382 // replace the aliasees and then set them back to their original values at
383 // the end.
384 collectAndEraseUsedFunctions(M, Used, false);
385 collectAndEraseUsedFunctions(M, CompilerUsed, true);
386
387 for (auto &GA : M.aliases()) {
388 // FIXME: This should look past all aliases not just interposable ones,
389 // see discussion on D65118.
390 if (auto *F = dyn_cast<Function>(GA.getAliasee()->stripPointerCasts()))
391 FunctionAliases.push_back({&GA, F});
392 }
393
394 for (auto &GI : M.ifuncs())
395 if (auto *F = dyn_cast<Function>(GI.getResolver()->stripPointerCasts()))
396 ResolverIFuncs.push_back({&GI, F});
397 }
398
399 ~ScopedSaveAliaseesAndUsed() {
400 appendToUsed(M, Used);
401 appendToCompilerUsed(M, CompilerUsed);
402
403 for (auto P : FunctionAliases)
404 P.first->setAliasee(P.second);
405
406 for (auto P : ResolverIFuncs) {
407 // This does not preserve pointer casts that may have been stripped by the
408 // constructor, but the resolver's type is different from that of the
409 // ifunc anyway.
410 P.first->setResolver(P.second);
411 }
412 }
413};
414
415class LowerTypeTestsModule {
416 Module &M;
417
418 ModuleSummaryIndex *ExportSummary;
419 const ModuleSummaryIndex *ImportSummary;
420
421 Triple::ArchType Arch;
423 Triple::ObjectFormatType ObjectFormat;
424
425 // Determines which kind of Thumb jump table we generate. If arch is
426 // either 'arm' or 'thumb' we need to find this out, because
427 // selectJumpTableArmEncoding may decide to use Thumb in either case.
428 bool CanUseArmJumpTable = false, CanUseThumbBWJumpTable = false;
429
430 // Cache variable used by hasBranchTargetEnforcement().
431 int HasBranchTargetEnforcement = -1;
432
433 IntegerType *Int1Ty = Type::getInt1Ty(M.getContext());
434 IntegerType *Int8Ty = Type::getInt8Ty(M.getContext());
435 PointerType *PtrTy = PointerType::getUnqual(M.getContext());
436 ArrayType *Int8Arr0Ty = ArrayType::get(Type::getInt8Ty(M.getContext()), 0);
437 IntegerType *Int32Ty = Type::getInt32Ty(M.getContext());
438 IntegerType *Int64Ty = Type::getInt64Ty(M.getContext());
439 IntegerType *IntPtrTy = M.getDataLayout().getIntPtrType(M.getContext(), 0);
440
441 // Indirect function call index assignment counter for WebAssembly
442 uint64_t IndirectIndex = 1;
443
444 // Mapping from type identifiers to the call sites that test them, as well as
445 // whether the type identifier needs to be exported to ThinLTO backends as
446 // part of the regular LTO phase of the ThinLTO pipeline (see exportTypeId).
447 struct TypeIdUserInfo {
448 std::vector<CallInst *> CallSites;
449 bool IsExported = false;
450 };
451 DenseMap<Metadata *, TypeIdUserInfo> TypeIdUsers;
452
453 /// This structure describes how to lower type tests for a particular type
454 /// identifier. It is either built directly from the global analysis (during
455 /// regular LTO or the regular LTO phase of ThinLTO), or indirectly using type
456 /// identifier summaries and external symbol references (in ThinLTO backends).
457 struct TypeIdLowering {
459
460 /// All except Unsat: the address of the last element within the combined
461 /// global.
462 Constant *OffsetedGlobal;
463
464 /// ByteArray, Inline, AllOnes: log2 of the required global alignment
465 /// relative to the start address.
466 Constant *AlignLog2;
467
468 /// ByteArray, Inline, AllOnes: one less than the size of the memory region
469 /// covering members of this type identifier as a multiple of 2^AlignLog2.
470 Constant *SizeM1;
471
472 /// ByteArray: the byte array to test the address against.
473 Constant *TheByteArray;
474
475 /// ByteArray: the bit mask to apply to bytes loaded from the byte array.
476 Constant *BitMask;
477
478 /// Inline: the bit mask to test the address against.
479 Constant *InlineBits;
480 };
481
482 std::vector<ByteArrayInfo> ByteArrayInfos;
483
484 Function *WeakInitializerFn = nullptr;
485
486 GlobalVariable *GlobalAnnotation;
487 DenseSet<Value *> FunctionAnnotations;
488
489 bool shouldExportConstantsAsAbsoluteSymbols();
490 uint8_t *exportTypeId(StringRef TypeId, const TypeIdLowering &TIL);
491 TypeIdLowering importTypeId(StringRef TypeId);
492 void importTypeTest(CallInst *CI);
493 void importFunction(Function *F, bool isJumpTableCanonical);
494
495 ByteArrayInfo *createByteArray(const BitSetInfo &BSI);
496 void allocateByteArrays();
497 Value *createBitSetTest(IRBuilder<> &B, const TypeIdLowering &TIL,
498 Value *BitOffset);
499 void lowerTypeTestCalls(
500 ArrayRef<Metadata *> TypeIds, Constant *CombinedGlobalAddr,
501 const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout);
502 Value *lowerTypeTestCall(Metadata *TypeId, CallInst *CI,
503 const TypeIdLowering &TIL);
504
505 void buildBitSetsFromGlobalVariables(ArrayRef<Metadata *> TypeIds,
508 selectJumpTableArmEncoding(ArrayRef<GlobalTypeMember *> Functions);
509 bool hasBranchTargetEnforcement();
510 unsigned getJumpTableEntrySize(Triple::ArchType JumpTableArch);
511 InlineAsm *createJumpTableEntryAsm(Triple::ArchType JumpTableArch);
512 void verifyTypeMDNode(GlobalObject *GO, MDNode *Type);
513 void buildBitSetsFromFunctions(ArrayRef<Metadata *> TypeIds,
515 void buildBitSetsFromFunctionsNative(ArrayRef<Metadata *> TypeIds,
517 void buildBitSetsFromFunctionsWASM(ArrayRef<Metadata *> TypeIds,
519 void
520 buildBitSetsFromDisjointSet(ArrayRef<Metadata *> TypeIds,
522 ArrayRef<ICallBranchFunnel *> ICallBranchFunnels);
523
524 void replaceWeakDeclarationWithJumpTablePtr(Function *F, Constant *JT,
525 bool IsJumpTableCanonical);
526 void moveInitializerToModuleConstructor(GlobalVariable *GV);
527 void findGlobalVariableUsersOf(Constant *C,
528 SmallSetVector<GlobalVariable *, 8> &Out);
529
530 void createJumpTable(Function *F, ArrayRef<GlobalTypeMember *> Functions,
531 Triple::ArchType JumpTableArch);
532
533 /// replaceCfiUses - Go through the uses list for this definition
534 /// and make each use point to "V" instead of "this" when the use is outside
535 /// the block. 'This's use list is expected to have at least one element.
536 /// Unlike replaceAllUsesWith this function skips blockaddr and direct call
537 /// uses.
538 void replaceCfiUses(Function *Old, Value *New, bool IsJumpTableCanonical);
539
540 /// replaceDirectCalls - Go through the uses list for this definition and
541 /// replace each use, which is a direct function call.
542 void replaceDirectCalls(Value *Old, Value *New);
543
544 bool isFunctionAnnotation(Value *V) const {
545 return FunctionAnnotations.contains(V);
546 }
547
548 void maybeReplaceComdat(Function *F, StringRef OriginalName);
549
550public:
551 LowerTypeTestsModule(Module &M, ModuleAnalysisManager &AM,
552 ModuleSummaryIndex *ExportSummary,
553 const ModuleSummaryIndex *ImportSummary);
554
555 bool lower();
556
557 // Lower the module using the action and summary passed as command line
558 // arguments. For testing purposes only.
559 static bool runForTesting(Module &M, ModuleAnalysisManager &AM);
560};
561} // end anonymous namespace
562
563/// Build a bit set for list of offsets.
565 // Compute the byte offset of each address associated with this type
566 // identifier.
567 return BitSetBuilder(Offsets).build();
568}
569
570/// Build a test that bit BitOffset mod sizeof(Bits)*8 is set in
571/// Bits. This pattern matches to the bt instruction on x86.
573 Value *BitOffset) {
574 auto BitsType = cast<IntegerType>(Bits->getType());
575 unsigned BitWidth = BitsType->getBitWidth();
576
577 BitOffset = B.CreateZExtOrTrunc(BitOffset, BitsType);
578 Value *BitIndex =
579 B.CreateAnd(BitOffset, ConstantInt::get(BitsType, BitWidth - 1));
580 Value *BitMask = B.CreateShl(ConstantInt::get(BitsType, 1), BitIndex);
581 Value *MaskedBits = B.CreateAnd(Bits, BitMask);
582 return B.CreateICmpNE(MaskedBits, ConstantInt::get(BitsType, 0));
583}
584
585ByteArrayInfo *LowerTypeTestsModule::createByteArray(const BitSetInfo &BSI) {
586 // Create globals to stand in for byte arrays and masks. These never actually
587 // get initialized, we RAUW and erase them later in allocateByteArrays() once
588 // we know the offset and mask to use.
589 auto ByteArrayGlobal = new GlobalVariable(
590 M, Int8Ty, /*isConstant=*/true, GlobalValue::PrivateLinkage, nullptr);
591 auto MaskGlobal = new GlobalVariable(M, Int8Ty, /*isConstant=*/true,
593
594 ByteArrayInfos.emplace_back();
595 ByteArrayInfo *BAI = &ByteArrayInfos.back();
596
597 BAI->Bits = BSI.Bits;
598 BAI->BitSize = BSI.BitSize;
599 BAI->ByteArray = ByteArrayGlobal;
600 BAI->MaskGlobal = MaskGlobal;
601 return BAI;
602}
603
604void LowerTypeTestsModule::allocateByteArrays() {
605 llvm::stable_sort(ByteArrayInfos,
606 [](const ByteArrayInfo &BAI1, const ByteArrayInfo &BAI2) {
607 return BAI1.BitSize > BAI2.BitSize;
608 });
609
610 std::vector<uint64_t> ByteArrayOffsets(ByteArrayInfos.size());
611
613 for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) {
614 ByteArrayInfo *BAI = &ByteArrayInfos[I];
615
616 uint8_t Mask;
617 BAB.allocate(BAI->Bits, BAI->BitSize, ByteArrayOffsets[I], Mask);
618
619 BAI->MaskGlobal->replaceAllUsesWith(
620 ConstantExpr::getIntToPtr(ConstantInt::get(Int8Ty, Mask), PtrTy));
621 BAI->MaskGlobal->eraseFromParent();
622 if (BAI->MaskPtr)
623 *BAI->MaskPtr = Mask;
624 }
625
626 Constant *ByteArrayConst = ConstantDataArray::get(M.getContext(), BAB.Bytes);
627 auto ByteArray =
628 new GlobalVariable(M, ByteArrayConst->getType(), /*isConstant=*/true,
629 GlobalValue::PrivateLinkage, ByteArrayConst);
630
631 for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) {
632 ByteArrayInfo *BAI = &ByteArrayInfos[I];
634 ByteArray, ConstantInt::get(IntPtrTy, ByteArrayOffsets[I]));
635
636 // Create an alias instead of RAUW'ing the gep directly. On x86 this ensures
637 // that the pc-relative displacement is folded into the lea instead of the
638 // test instruction getting another displacement.
639 GlobalAlias *Alias = GlobalAlias::create(
640 Int8Ty, 0, GlobalValue::PrivateLinkage, "bits", GEP, &M);
641 BAI->ByteArray->replaceAllUsesWith(Alias);
642 BAI->ByteArray->eraseFromParent();
643 }
644
645 ByteArraySizeBits = BAB.BitAllocs[0] + BAB.BitAllocs[1] + BAB.BitAllocs[2] +
646 BAB.BitAllocs[3] + BAB.BitAllocs[4] + BAB.BitAllocs[5] +
647 BAB.BitAllocs[6] + BAB.BitAllocs[7];
648 ByteArraySizeBytes = BAB.Bytes.size();
649}
650
651/// Build a test that bit BitOffset is set in the type identifier that was
652/// lowered to TIL, which must be either an Inline or a ByteArray.
653Value *LowerTypeTestsModule::createBitSetTest(IRBuilder<> &B,
654 const TypeIdLowering &TIL,
655 Value *BitOffset) {
656 if (TIL.TheKind == TypeTestResolution::Inline) {
657 // If the bit set is sufficiently small, we can avoid a load by bit testing
658 // a constant.
659 return createMaskedBitTest(B, TIL.InlineBits, BitOffset);
660 } else {
661 Constant *ByteArray = TIL.TheByteArray;
662 if (AvoidReuse && !ImportSummary) {
663 // Each use of the byte array uses a different alias. This makes the
664 // backend less likely to reuse previously computed byte array addresses,
665 // improving the security of the CFI mechanism based on this pass.
666 // This won't work when importing because TheByteArray is external.
668 "bits_use", ByteArray, &M);
669 }
670
671 Value *ByteAddr = B.CreateGEP(Int8Ty, ByteArray, BitOffset);
672 Value *Byte = B.CreateLoad(Int8Ty, ByteAddr);
673
674 Value *ByteAndMask =
675 B.CreateAnd(Byte, ConstantExpr::getPtrToInt(TIL.BitMask, Int8Ty));
676 return B.CreateICmpNE(ByteAndMask, ConstantInt::get(Int8Ty, 0));
677 }
678}
679
680static bool isKnownTypeIdMember(Metadata *TypeId, const DataLayout &DL,
681 Value *V, uint64_t COffset) {
682 if (auto GV = dyn_cast<GlobalObject>(V)) {
684 GV->getMetadata(LLVMContext::MD_type, Types);
685 for (MDNode *Type : Types) {
686 if (Type->getOperand(1) != TypeId)
687 continue;
690 cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
691 ->getZExtValue();
692 if (COffset == Offset)
693 return true;
694 }
695 return false;
696 }
697
698 if (auto GEP = dyn_cast<GEPOperator>(V)) {
699 APInt APOffset(DL.getIndexSizeInBits(0), 0);
700 bool Result = GEP->accumulateConstantOffset(DL, APOffset);
701 if (!Result)
702 return false;
703 COffset += APOffset.getZExtValue();
704 return isKnownTypeIdMember(TypeId, DL, GEP->getPointerOperand(), COffset);
705 }
706
707 if (auto Op = dyn_cast<Operator>(V)) {
708 if (Op->getOpcode() == Instruction::BitCast)
709 return isKnownTypeIdMember(TypeId, DL, Op->getOperand(0), COffset);
710
711 if (Op->getOpcode() == Instruction::Select)
712 return isKnownTypeIdMember(TypeId, DL, Op->getOperand(1), COffset) &&
713 isKnownTypeIdMember(TypeId, DL, Op->getOperand(2), COffset);
714 }
715
716 return false;
717}
718
719/// Lower a llvm.type.test call to its implementation. Returns the value to
720/// replace the call with.
721Value *LowerTypeTestsModule::lowerTypeTestCall(Metadata *TypeId, CallInst *CI,
722 const TypeIdLowering &TIL) {
723 // Delay lowering if the resolution is currently unknown.
724 if (TIL.TheKind == TypeTestResolution::Unknown)
725 return nullptr;
726 if (TIL.TheKind == TypeTestResolution::Unsat)
727 return ConstantInt::getFalse(M.getContext());
728
729 Value *Ptr = CI->getArgOperand(0);
730 const DataLayout &DL = M.getDataLayout();
731 if (isKnownTypeIdMember(TypeId, DL, Ptr, 0))
732 return ConstantInt::getTrue(M.getContext());
733
734 BasicBlock *InitialBB = CI->getParent();
735
736 IRBuilder<> B(CI);
737
738 Value *PtrAsInt = B.CreatePtrToInt(Ptr, IntPtrTy);
739
740 Constant *OffsetedGlobalAsInt =
741 ConstantExpr::getPtrToInt(TIL.OffsetedGlobal, IntPtrTy);
742 if (TIL.TheKind == TypeTestResolution::Single)
743 return B.CreateICmpEQ(PtrAsInt, OffsetedGlobalAsInt);
744
745 // Here we compute `last element - address`. The reason why we do this instead
746 // of computing `address - first element` is that it leads to a slightly
747 // shorter instruction sequence on x86. Because it doesn't matter how we do
748 // the subtraction on other architectures, we do so unconditionally.
749 Value *PtrOffset = B.CreateSub(OffsetedGlobalAsInt, PtrAsInt);
750
751 // We need to check that the offset both falls within our range and is
752 // suitably aligned. We can check both properties at the same time by
753 // performing a right rotate by log2(alignment) followed by an integer
754 // comparison against the bitset size. The rotate will move the lower
755 // order bits that need to be zero into the higher order bits of the
756 // result, causing the comparison to fail if they are nonzero. The rotate
757 // also conveniently gives us a bit offset to use during the load from
758 // the bitset.
759 Value *BitOffset = B.CreateIntrinsic(IntPtrTy, Intrinsic::fshr,
760 {PtrOffset, PtrOffset, TIL.AlignLog2});
761
762 Value *OffsetInRange = B.CreateICmpULE(BitOffset, TIL.SizeM1);
763
764 // If the bit set is all ones, testing against it is unnecessary.
765 if (TIL.TheKind == TypeTestResolution::AllOnes)
766 return OffsetInRange;
767
768 // See if the intrinsic is used in the following common pattern:
769 // br(llvm.type.test(...), thenbb, elsebb)
770 // where nothing happens between the type test and the br.
771 // If so, create slightly simpler IR.
772 if (CI->hasOneUse())
773 if (auto *Br = dyn_cast<CondBrInst>(*CI->user_begin()))
774 if (CI->getNextNode() == Br) {
775 BasicBlock *Then = InitialBB->splitBasicBlock(CI->getIterator());
776 BasicBlock *Else = Br->getSuccessor(1);
777 CondBrInst *NewBr = CondBrInst::Create(OffsetInRange, Then, Else);
778 NewBr->setMetadata(LLVMContext::MD_prof,
779 Br->getMetadata(LLVMContext::MD_prof));
780 ReplaceInstWithInst(InitialBB->getTerminator(), NewBr);
781
782 // Update phis in Else resulting from InitialBB being split
783 for (auto &Phi : Else->phis())
784 Phi.addIncoming(Phi.getIncomingValueForBlock(Then), InitialBB);
785
786 IRBuilder<> ThenB(CI);
787 return createBitSetTest(ThenB, TIL, BitOffset);
788 }
789
790 MDBuilder MDB(M.getContext());
791 IRBuilder<> ThenB(SplitBlockAndInsertIfThen(OffsetInRange, CI, false,
792 MDB.createLikelyBranchWeights()));
793
794 // Now that we know that the offset is in range and aligned, load the
795 // appropriate bit from the bitset.
796 Value *Bit = createBitSetTest(ThenB, TIL, BitOffset);
797
798 // The value we want is 0 if we came directly from the initial block
799 // (having failed the range or alignment checks), or the loaded bit if
800 // we came from the block in which we loaded it.
801 B.SetInsertPoint(CI);
802 PHINode *P = B.CreatePHI(Int1Ty, 2);
803 P->addIncoming(ConstantInt::get(Int1Ty, 0), InitialBB);
804 P->addIncoming(Bit, ThenB.GetInsertBlock());
805 return P;
806}
807
808/// Given a disjoint set of type identifiers and globals, lay out the globals,
809/// build the bit sets and lower the llvm.type.test calls.
810void LowerTypeTestsModule::buildBitSetsFromGlobalVariables(
812 // Build a new global with the combined contents of the referenced globals.
813 // This global is a struct whose even-indexed elements contain the original
814 // contents of the referenced globals and whose odd-indexed elements contain
815 // any padding required to align the next element to the next power of 2 plus
816 // any additional padding required to meet its alignment requirements.
817 std::vector<Constant *> GlobalInits;
818 const DataLayout &DL = M.getDataLayout();
819 DenseMap<GlobalTypeMember *, uint64_t> GlobalLayout;
820 Align MaxAlign;
821 uint64_t CurOffset = 0;
822 uint64_t DesiredPadding = 0;
823 for (GlobalTypeMember *G : Globals) {
824 auto *GV = cast<GlobalVariable>(G->getGlobal());
825 Align Alignment =
826 DL.getValueOrABITypeAlignment(GV->getAlign(), GV->getValueType());
827 MaxAlign = std::max(MaxAlign, Alignment);
828 uint64_t GVOffset = alignTo(CurOffset + DesiredPadding, Alignment);
829 GlobalLayout[G] = GVOffset;
830 if (GVOffset != 0) {
831 uint64_t Padding = GVOffset - CurOffset;
832 GlobalInits.push_back(
834 }
835
836 GlobalInits.push_back(GV->getInitializer());
837 uint64_t InitSize = GV->getGlobalSize(DL);
838 CurOffset = GVOffset + InitSize;
839
840 // Compute the amount of padding that we'd like for the next element.
841 DesiredPadding = NextPowerOf2(InitSize - 1) - InitSize;
842
843 // Experiments of different caps with Chromium on both x64 and ARM64
844 // have shown that the 32-byte cap generates the smallest binary on
845 // both platforms while different caps yield similar performance.
846 // (see https://lists.llvm.org/pipermail/llvm-dev/2018-July/124694.html)
847 if (DesiredPadding > 32)
848 DesiredPadding = alignTo(InitSize, 32) - InitSize;
849 }
850
851 Constant *NewInit = ConstantStruct::getAnon(M.getContext(), GlobalInits);
852 auto *CombinedGlobal =
853 new GlobalVariable(M, NewInit->getType(), /*isConstant=*/true,
855 CombinedGlobal->setAlignment(MaxAlign);
856
857 StructType *NewTy = cast<StructType>(NewInit->getType());
858 lowerTypeTestCalls(TypeIds, CombinedGlobal, GlobalLayout);
859
860 // Build aliases pointing to offsets into the combined global for each
861 // global from which we built the combined global, and replace references
862 // to the original globals with references to the aliases.
863 for (unsigned I = 0; I != Globals.size(); ++I) {
864 GlobalVariable *GV = cast<GlobalVariable>(Globals[I]->getGlobal());
865
866 // Multiply by 2 to account for padding elements.
867 Constant *CombinedGlobalIdxs[] = {ConstantInt::get(Int32Ty, 0),
868 ConstantInt::get(Int32Ty, I * 2)};
869 Constant *CombinedGlobalElemPtr = ConstantExpr::getInBoundsGetElementPtr(
870 NewInit->getType(), CombinedGlobal, CombinedGlobalIdxs);
871 assert(GV->getType()->getAddressSpace() == 0);
872 GlobalAlias *GAlias =
873 GlobalAlias::create(NewTy->getElementType(I * 2), 0, GV->getLinkage(),
874 "", CombinedGlobalElemPtr, &M);
875 GAlias->setVisibility(GV->getVisibility());
876 GAlias->takeName(GV);
877 GV->replaceAllUsesWith(GAlias);
878 GV->eraseFromParent();
879 }
880}
881
882bool LowerTypeTestsModule::shouldExportConstantsAsAbsoluteSymbols() {
883 return (Arch == Triple::x86 || Arch == Triple::x86_64) &&
884 ObjectFormat == Triple::ELF;
885}
886
887/// Export the given type identifier so that ThinLTO backends may import it.
888/// Type identifiers are exported by adding coarse-grained information about how
889/// to test the type identifier to the summary, and creating symbols in the
890/// object file (aliases and absolute symbols) containing fine-grained
891/// information about the type identifier.
892///
893/// Returns a pointer to the location in which to store the bitmask, if
894/// applicable.
895uint8_t *LowerTypeTestsModule::exportTypeId(StringRef TypeId,
896 const TypeIdLowering &TIL) {
897 TypeTestResolution &TTRes =
898 ExportSummary->getOrInsertTypeIdSummary(TypeId).TTRes;
899 TTRes.TheKind = TIL.TheKind;
900
901 auto ExportGlobal = [&](StringRef Name, Constant *C) {
902 GlobalAlias *GA =
904 "__typeid_" + TypeId + "_" + Name, C, &M);
906 };
907
908 auto ExportConstant = [&](StringRef Name, uint64_t &Storage, Constant *C) {
909 if (shouldExportConstantsAsAbsoluteSymbols())
910 ExportGlobal(Name, ConstantExpr::getIntToPtr(C, PtrTy));
911 else
912 Storage = cast<ConstantInt>(C)->getZExtValue();
913 };
914
915 if (TIL.TheKind != TypeTestResolution::Unsat)
916 ExportGlobal("global_addr", TIL.OffsetedGlobal);
917
918 if (TIL.TheKind == TypeTestResolution::ByteArray ||
919 TIL.TheKind == TypeTestResolution::Inline ||
920 TIL.TheKind == TypeTestResolution::AllOnes) {
921 ExportConstant("align", TTRes.AlignLog2, TIL.AlignLog2);
922 ExportConstant("size_m1", TTRes.SizeM1, TIL.SizeM1);
923
924 uint64_t BitSize = cast<ConstantInt>(TIL.SizeM1)->getZExtValue() + 1;
925 if (TIL.TheKind == TypeTestResolution::Inline)
926 TTRes.SizeM1BitWidth = (BitSize <= 32) ? 5 : 6;
927 else
928 TTRes.SizeM1BitWidth = (BitSize <= 128) ? 7 : 32;
929 }
930
931 if (TIL.TheKind == TypeTestResolution::ByteArray) {
932 ExportGlobal("byte_array", TIL.TheByteArray);
933 if (shouldExportConstantsAsAbsoluteSymbols())
934 ExportGlobal("bit_mask", TIL.BitMask);
935 else
936 return &TTRes.BitMask;
937 }
938
939 if (TIL.TheKind == TypeTestResolution::Inline)
940 ExportConstant("inline_bits", TTRes.InlineBits, TIL.InlineBits);
941
942 return nullptr;
943}
944
945LowerTypeTestsModule::TypeIdLowering
946LowerTypeTestsModule::importTypeId(StringRef TypeId) {
947 const TypeIdSummary *TidSummary = ImportSummary->getTypeIdSummary(TypeId);
948 if (!TidSummary)
949 return {}; // Unsat: no globals match this type id.
950 const TypeTestResolution &TTRes = TidSummary->TTRes;
951
952 TypeIdLowering TIL;
953 TIL.TheKind = TTRes.TheKind;
954
955 auto ImportGlobal = [&](StringRef Name) {
956 // Give the global a type of length 0 so that it is not assumed not to alias
957 // with any other global.
958 GlobalVariable *GV = M.getOrInsertGlobal(
959 ("__typeid_" + TypeId + "_" + Name).str(), Int8Arr0Ty);
961 return GV;
962 };
963
964 auto ImportConstant = [&](StringRef Name, uint64_t Const, unsigned AbsWidth,
965 Type *Ty) {
966 if (!shouldExportConstantsAsAbsoluteSymbols()) {
967 Constant *C =
968 ConstantInt::get(isa<IntegerType>(Ty) ? Ty : Int64Ty, Const);
969 if (!isa<IntegerType>(Ty))
971 return C;
972 }
973
974 Constant *C = ImportGlobal(Name);
975 auto *GV = cast<GlobalVariable>(C->stripPointerCasts());
976 if (isa<IntegerType>(Ty))
978 if (GV->getMetadata(LLVMContext::MD_absolute_symbol))
979 return C;
980
981 auto SetAbsRange = [&](uint64_t Min, uint64_t Max) {
982 auto *MinC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Min));
983 auto *MaxC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Max));
984 GV->setMetadata(LLVMContext::MD_absolute_symbol,
985 MDNode::get(M.getContext(), {MinC, MaxC}));
986 };
987 if (AbsWidth == IntPtrTy->getBitWidth()) {
988 uint64_t AllOnes = IntPtrTy->getBitMask();
989 SetAbsRange(AllOnes, AllOnes); // Full set.
990 } else {
991 SetAbsRange(0, 1ull << AbsWidth);
992 }
993 return C;
994 };
995
996 if (TIL.TheKind != TypeTestResolution::Unsat) {
997 auto *GV = ImportGlobal("global_addr");
998 // This is either a vtable (in .data.rel.ro) or a jump table (in .text).
999 // Either way it's expected to be in the low 2 GiB, so set the small code
1000 // model.
1001 //
1002 // For .data.rel.ro, we currently place all such sections in the low 2 GiB
1003 // [1], and for .text the sections are expected to be in the low 2 GiB under
1004 // the small and medium code models [2] and this pass only supports those
1005 // code models (e.g. jump tables use jmp instead of movabs/jmp).
1006 //
1007 // [1]https://github.com/llvm/llvm-project/pull/137742
1008 // [2]https://maskray.me/blog/2023-05-14-relocation-overflow-and-code-models
1010 TIL.OffsetedGlobal = GV;
1011 }
1012
1013 if (TIL.TheKind == TypeTestResolution::ByteArray ||
1014 TIL.TheKind == TypeTestResolution::Inline ||
1015 TIL.TheKind == TypeTestResolution::AllOnes) {
1016 TIL.AlignLog2 = ImportConstant("align", TTRes.AlignLog2, 8, IntPtrTy);
1017 TIL.SizeM1 =
1018 ImportConstant("size_m1", TTRes.SizeM1, TTRes.SizeM1BitWidth, IntPtrTy);
1019 }
1020
1021 if (TIL.TheKind == TypeTestResolution::ByteArray) {
1022 TIL.TheByteArray = ImportGlobal("byte_array");
1023 TIL.BitMask = ImportConstant("bit_mask", TTRes.BitMask, 8, PtrTy);
1024 }
1025
1026 if (TIL.TheKind == TypeTestResolution::Inline)
1027 TIL.InlineBits = ImportConstant(
1028 "inline_bits", TTRes.InlineBits, 1 << TTRes.SizeM1BitWidth,
1029 TTRes.SizeM1BitWidth <= 5 ? Int32Ty : Int64Ty);
1030
1031 return TIL;
1032}
1033
1034void LowerTypeTestsModule::importTypeTest(CallInst *CI) {
1035 auto TypeIdMDVal = dyn_cast<MetadataAsValue>(CI->getArgOperand(1));
1036 if (!TypeIdMDVal)
1037 report_fatal_error("Second argument of llvm.type.test must be metadata");
1038
1039 auto TypeIdStr = dyn_cast<MDString>(TypeIdMDVal->getMetadata());
1040 // If this is a local unpromoted type, which doesn't have a metadata string,
1041 // treat as Unknown and delay lowering, so that we can still utilize it for
1042 // later optimizations.
1043 if (!TypeIdStr)
1044 return;
1045
1046 TypeIdLowering TIL = importTypeId(TypeIdStr->getString());
1047 Value *Lowered = lowerTypeTestCall(TypeIdStr, CI, TIL);
1048 if (Lowered) {
1049 CI->replaceAllUsesWith(Lowered);
1050 CI->eraseFromParent();
1051 }
1052}
1053
1054void LowerTypeTestsModule::maybeReplaceComdat(Function *F,
1055 StringRef OriginalName) {
1056 // For COFF we should also rename the comdat if this function also
1057 // happens to be the key function. Even if the comdat name changes, this
1058 // should still be fine since comdat and symbol resolution happens
1059 // before LTO, so all symbols which would prevail have been selected.
1060 if (F->hasComdat() && ObjectFormat == Triple::COFF &&
1061 F->getComdat()->getName() == OriginalName) {
1062 Comdat *OldComdat = F->getComdat();
1063 Comdat *NewComdat = M.getOrInsertComdat(F->getName());
1064 for (GlobalObject &GO : M.global_objects()) {
1065 if (GO.getComdat() == OldComdat)
1066 GO.setComdat(NewComdat);
1067 }
1068 }
1069}
1070
1071// ThinLTO backend: the function F has a jump table entry; update this module
1072// accordingly. isJumpTableCanonical describes the type of the jump table entry.
1073void LowerTypeTestsModule::importFunction(Function *F,
1074 bool isJumpTableCanonical) {
1075 assert(F->getType()->getAddressSpace() == 0);
1076
1077 GlobalValue::VisibilityTypes Visibility = F->getVisibility();
1078 std::string Name = std::string(F->getName());
1079
1080 if (F->isDeclarationForLinker() && isJumpTableCanonical) {
1081 // Non-dso_local functions may be overriden at run time,
1082 // don't short curcuit them
1083 if (F->isDSOLocal()) {
1084 Function *RealF = Function::Create(F->getFunctionType(),
1086 F->getAddressSpace(),
1087 Name + ".cfi", &M);
1089 replaceDirectCalls(F, RealF);
1090 }
1091 return;
1092 }
1093
1094 Function *FDecl;
1095 if (!isJumpTableCanonical) {
1096 // Either a declaration of an external function or a reference to a locally
1097 // defined jump table.
1098 FDecl = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage,
1099 F->getAddressSpace(), Name + ".cfi_jt", &M);
1101 } else {
1102 F->setName(Name + ".cfi");
1103 maybeReplaceComdat(F, Name);
1104 FDecl = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage,
1105 F->getAddressSpace(), Name, &M);
1106 FDecl->setVisibility(Visibility);
1107 Visibility = GlobalValue::HiddenVisibility;
1108
1109 // Update aliases pointing to this function to also include the ".cfi" suffix,
1110 // We expect the jump table entry to either point to the real function or an
1111 // alias. Redirect all other users to the jump table entry.
1112 for (auto &U : F->uses()) {
1113 if (auto *A = dyn_cast<GlobalAlias>(U.getUser())) {
1114 std::string AliasName = A->getName().str() + ".cfi";
1115 Function *AliasDecl = Function::Create(
1116 F->getFunctionType(), GlobalValue::ExternalLinkage,
1117 F->getAddressSpace(), "", &M);
1118 AliasDecl->takeName(A);
1119 A->replaceAllUsesWith(AliasDecl);
1120 A->setName(AliasName);
1121 }
1122 }
1123 }
1124
1125 if (F->hasExternalWeakLinkage())
1126 replaceWeakDeclarationWithJumpTablePtr(F, FDecl, isJumpTableCanonical);
1127 else
1128 replaceCfiUses(F, FDecl, isJumpTableCanonical);
1129
1130 // Set visibility late because it's used in replaceCfiUses() to determine
1131 // whether uses need to be replaced.
1132 F->setVisibility(Visibility);
1133}
1134
1135static auto
1137 const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout) {
1139 // Pre-populate the map with interesting type identifiers.
1140 for (Metadata *TypeId : TypeIds)
1141 OffsetsByTypeID[TypeId];
1142 for (const auto &[Mem, MemOff] : GlobalLayout) {
1143 for (MDNode *Type : Mem->types()) {
1144 auto It = OffsetsByTypeID.find(Type->getOperand(1));
1145 if (It == OffsetsByTypeID.end())
1146 continue;
1149 cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
1150 ->getZExtValue();
1151 It->second.push_back(MemOff + Offset);
1152 }
1153 }
1154
1156 BitSets.reserve(TypeIds.size());
1157 for (Metadata *TypeId : TypeIds) {
1158 BitSets.emplace_back(TypeId, buildBitSet(OffsetsByTypeID[TypeId]));
1159 LLVM_DEBUG({
1160 if (auto MDS = dyn_cast<MDString>(TypeId))
1161 dbgs() << MDS->getString() << ": ";
1162 else
1163 dbgs() << "<unnamed>: ";
1164 BitSets.back().second.print(dbgs());
1165 });
1166 }
1167
1168 return BitSets;
1169}
1170
1171void LowerTypeTestsModule::lowerTypeTestCalls(
1172 ArrayRef<Metadata *> TypeIds, Constant *CombinedGlobalAddr,
1173 const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout) {
1174 // For each type identifier in this disjoint set...
1175 for (const auto &[TypeId, BSI] : buildBitSets(TypeIds, GlobalLayout)) {
1176 ByteArrayInfo *BAI = nullptr;
1177 TypeIdLowering TIL;
1178
1179 uint64_t GlobalOffset =
1180 BSI.ByteOffset + ((BSI.BitSize - 1) << BSI.AlignLog2);
1181 TIL.OffsetedGlobal = ConstantExpr::getPtrAdd(
1182 CombinedGlobalAddr, ConstantInt::get(IntPtrTy, GlobalOffset)),
1183 TIL.AlignLog2 = ConstantInt::get(IntPtrTy, BSI.AlignLog2);
1184 TIL.SizeM1 = ConstantInt::get(IntPtrTy, BSI.BitSize - 1);
1185 if (BSI.isAllOnes()) {
1186 TIL.TheKind = (BSI.BitSize == 1) ? TypeTestResolution::Single
1187 : TypeTestResolution::AllOnes;
1188 } else if (BSI.BitSize <= IntPtrTy->getBitWidth()) {
1189 TIL.TheKind = TypeTestResolution::Inline;
1190 uint64_t InlineBits = 0;
1191 for (auto Bit : BSI.Bits)
1192 InlineBits |= uint64_t(1) << Bit;
1193 if (InlineBits == 0)
1194 TIL.TheKind = TypeTestResolution::Unsat;
1195 else
1196 TIL.InlineBits = ConstantInt::get(
1197 (BSI.BitSize <= 32) ? Int32Ty : Int64Ty, InlineBits);
1198 } else {
1199 TIL.TheKind = TypeTestResolution::ByteArray;
1200 ++NumByteArraysCreated;
1201 BAI = createByteArray(BSI);
1202 TIL.TheByteArray = BAI->ByteArray;
1203 TIL.BitMask = BAI->MaskGlobal;
1204 }
1205
1206 TypeIdUserInfo &TIUI = TypeIdUsers[TypeId];
1207
1208 if (TIUI.IsExported) {
1209 uint8_t *MaskPtr = exportTypeId(cast<MDString>(TypeId)->getString(), TIL);
1210 if (BAI)
1211 BAI->MaskPtr = MaskPtr;
1212 }
1213
1214 // Lower each call to llvm.type.test for this type identifier.
1215 for (CallInst *CI : TIUI.CallSites) {
1216 ++NumTypeTestCallsLowered;
1217 Value *Lowered = lowerTypeTestCall(TypeId, CI, TIL);
1218 if (Lowered) {
1219 CI->replaceAllUsesWith(Lowered);
1220 CI->eraseFromParent();
1221 }
1222 }
1223 }
1224}
1225
1226void LowerTypeTestsModule::verifyTypeMDNode(GlobalObject *GO, MDNode *Type) {
1227 if (Type->getNumOperands() != 2)
1228 report_fatal_error("All operands of type metadata must have 2 elements");
1229
1230 if (GO->isThreadLocal())
1231 report_fatal_error("Bit set element may not be thread-local");
1232 if (isa<GlobalVariable>(GO) && GO->hasSection())
1234 "A member of a type identifier may not have an explicit section");
1235
1236 // FIXME: We previously checked that global var member of a type identifier
1237 // must be a definition, but the IR linker may leave type metadata on
1238 // declarations. We should restore this check after fixing PR31759.
1239
1240 auto OffsetConstMD = dyn_cast<ConstantAsMetadata>(Type->getOperand(0));
1241 if (!OffsetConstMD)
1242 report_fatal_error("Type offset must be a constant");
1243 auto OffsetInt = dyn_cast<ConstantInt>(OffsetConstMD->getValue());
1244 if (!OffsetInt)
1245 report_fatal_error("Type offset must be an integer constant");
1246}
1247
1248static const unsigned kX86JumpTableEntrySize = 8;
1249static const unsigned kX86IBTJumpTableEntrySize = 16;
1250static const unsigned kARMJumpTableEntrySize = 4;
1251static const unsigned kARMBTIJumpTableEntrySize = 8;
1252static const unsigned kARMv6MJumpTableEntrySize = 16;
1253static const unsigned kRISCVJumpTableEntrySize = 8;
1254static const unsigned kLOONGARCH64JumpTableEntrySize = 8;
1255static const unsigned kHexagonJumpTableEntrySize = 4;
1256
1257bool LowerTypeTestsModule::hasBranchTargetEnforcement() {
1258 if (HasBranchTargetEnforcement == -1) {
1259 // First time this query has been called. Find out the answer by checking
1260 // the module flags.
1261 if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
1262 M.getModuleFlag("branch-target-enforcement")))
1263 HasBranchTargetEnforcement = !BTE->isZero();
1264 else
1265 HasBranchTargetEnforcement = 0;
1266 }
1267 return HasBranchTargetEnforcement;
1268}
1269
1270unsigned
1271LowerTypeTestsModule::getJumpTableEntrySize(Triple::ArchType JumpTableArch) {
1272 switch (JumpTableArch) {
1273 case Triple::x86:
1274 case Triple::x86_64:
1275 if (const auto *MD = mdconst::extract_or_null<ConstantInt>(
1276 M.getModuleFlag("cf-protection-branch")))
1277 if (MD->getZExtValue())
1280 case Triple::arm:
1282 case Triple::thumb:
1283 if (CanUseThumbBWJumpTable) {
1284 if (hasBranchTargetEnforcement())
1287 } else {
1289 }
1290 case Triple::aarch64:
1291 if (hasBranchTargetEnforcement())
1294 case Triple::riscv32:
1295 case Triple::riscv64:
1299 case Triple::hexagon:
1301 default:
1302 report_fatal_error("Unsupported architecture for jump tables");
1303 }
1304}
1305
1306// Create an inline asm constant representing a jump table entry for the target.
1307// This consists of an instruction sequence containing a relative branch to
1308// Dest.
1309InlineAsm *
1310LowerTypeTestsModule::createJumpTableEntryAsm(Triple::ArchType JumpTableArch) {
1311 std::string Asm;
1312 raw_string_ostream AsmOS(Asm);
1313
1314 if (JumpTableArch == Triple::x86 || JumpTableArch == Triple::x86_64) {
1315 bool Endbr = false;
1316 if (const auto *MD = mdconst::extract_or_null<ConstantInt>(
1317 M.getModuleFlag("cf-protection-branch")))
1318 Endbr = !MD->isZero();
1319 if (Endbr)
1320 AsmOS << (JumpTableArch == Triple::x86 ? "endbr32\n" : "endbr64\n");
1321 AsmOS << "jmp ${0:c}@plt\n";
1322 if (Endbr)
1323 AsmOS << ".balign 16, 0xcc\n";
1324 else
1325 AsmOS << "int3\nint3\nint3\n";
1326 } else if (JumpTableArch == Triple::arm) {
1327 AsmOS << "b $0\n";
1328 } else if (JumpTableArch == Triple::aarch64) {
1329 if (hasBranchTargetEnforcement())
1330 AsmOS << "bti c\n";
1331 AsmOS << "b $0\n";
1332 } else if (JumpTableArch == Triple::thumb) {
1333 if (!CanUseThumbBWJumpTable) {
1334 // In Armv6-M, this sequence will generate a branch without corrupting
1335 // any registers. We use two stack words; in the second, we construct the
1336 // address we'll pop into pc, and the first is used to save and restore
1337 // r0 which we use as a temporary register.
1338 //
1339 // To support position-independent use cases, the offset of the target
1340 // function is stored as a relative offset (which will expand into an
1341 // R_ARM_REL32 relocation in ELF, and presumably the equivalent in other
1342 // object file types), and added to pc after we load it. (The alternative
1343 // B.W is automatically pc-relative.)
1344 //
1345 // There are five 16-bit Thumb instructions here, so the .balign 4 adds a
1346 // sixth halfword of padding, and then the offset consumes a further 4
1347 // bytes, for a total of 16, which is very convenient since entries in
1348 // this jump table need to have power-of-two size.
1349 AsmOS << "push {r0,r1}\n"
1350 << "ldr r0, 1f\n"
1351 << "0: add r0, r0, pc\n"
1352 << "str r0, [sp, #4]\n"
1353 << "pop {r0,pc}\n"
1354 << ".balign 4\n"
1355 << "1: .word $0 - (0b + 4)\n";
1356 } else {
1357 if (hasBranchTargetEnforcement())
1358 AsmOS << "bti\n";
1359 AsmOS << "b.w $0\n";
1360 }
1361 } else if (JumpTableArch == Triple::riscv32 ||
1362 JumpTableArch == Triple::riscv64) {
1363 AsmOS << "tail $0@plt\n";
1364 } else if (JumpTableArch == Triple::loongarch64) {
1365 AsmOS << "pcalau12i $$t0, %pc_hi20($0)\n"
1366 << "jirl $$r0, $$t0, %pc_lo12($0)\n";
1367 } else if (JumpTableArch == Triple::hexagon) {
1368 AsmOS << "jump $0\n";
1369 } else {
1370 report_fatal_error("Unsupported architecture for jump tables");
1371 }
1372
1373 return InlineAsm::get(
1374 FunctionType::get(Type::getVoidTy(M.getContext()), PtrTy, false),
1375 AsmOS.str(), "s",
1376 /*hasSideEffects=*/true);
1377}
1378
1379/// Given a disjoint set of type identifiers and functions, build the bit sets
1380/// and lower the llvm.type.test calls, architecture dependently.
1381void LowerTypeTestsModule::buildBitSetsFromFunctions(
1383 if (Arch == Triple::x86 || Arch == Triple::x86_64 || Arch == Triple::arm ||
1384 Arch == Triple::thumb || Arch == Triple::aarch64 ||
1385 Arch == Triple::riscv32 || Arch == Triple::riscv64 ||
1386 Arch == Triple::loongarch64 || Arch == Triple::hexagon)
1387 buildBitSetsFromFunctionsNative(TypeIds, Functions);
1388 else if (Arch == Triple::wasm32 || Arch == Triple::wasm64)
1389 buildBitSetsFromFunctionsWASM(TypeIds, Functions);
1390 else
1391 report_fatal_error("Unsupported architecture for jump tables");
1392}
1393
1394void LowerTypeTestsModule::moveInitializerToModuleConstructor(
1395 GlobalVariable *GV) {
1396 if (WeakInitializerFn == nullptr) {
1397 WeakInitializerFn = Function::Create(
1398 FunctionType::get(Type::getVoidTy(M.getContext()),
1399 /* IsVarArg */ false),
1401 M.getDataLayout().getProgramAddressSpace(),
1402 "__cfi_global_var_init", &M);
1403 BasicBlock *BB =
1404 BasicBlock::Create(M.getContext(), "entry", WeakInitializerFn);
1405 ReturnInst::Create(M.getContext(), BB);
1406 WeakInitializerFn->setSection(
1407 ObjectFormat == Triple::MachO
1408 ? "__TEXT,__StaticInit,regular,pure_instructions"
1409 : ".text.startup");
1410 // This code is equivalent to relocation application, and should run at the
1411 // earliest possible time (i.e. with the highest priority).
1412 appendToGlobalCtors(M, WeakInitializerFn, /* Priority */ 0);
1413 }
1414
1415 IRBuilder<> IRB(WeakInitializerFn->getEntryBlock().getTerminator());
1416 GV->setConstant(false);
1417 IRB.CreateAlignedStore(GV->getInitializer(), GV, GV->getAlign());
1419}
1420
1421void LowerTypeTestsModule::findGlobalVariableUsersOf(
1422 Constant *C, SmallSetVector<GlobalVariable *, 8> &Out) {
1423 for (auto *U : C->users()){
1424 if (auto *GV = dyn_cast<GlobalVariable>(U))
1425 Out.insert(GV);
1426 else if (auto *C2 = dyn_cast<Constant>(U))
1427 findGlobalVariableUsersOf(C2, Out);
1428 }
1429}
1430
1431// Replace all uses of F with (F ? JT : 0).
1432void LowerTypeTestsModule::replaceWeakDeclarationWithJumpTablePtr(
1433 Function *F, Constant *JT, bool IsJumpTableCanonical) {
1434 // The target expression can not appear in a constant initializer on most
1435 // (all?) targets. Switch to a runtime initializer.
1436 SmallSetVector<GlobalVariable *, 8> GlobalVarUsers;
1437 findGlobalVariableUsersOf(F, GlobalVarUsers);
1438 for (auto *GV : GlobalVarUsers) {
1439 if (GV == GlobalAnnotation)
1440 continue;
1441 moveInitializerToModuleConstructor(GV);
1442 }
1443
1444 // Can not RAUW F with an expression that uses F. Replace with a temporary
1445 // placeholder first.
1446 Function *PlaceholderFn =
1448 F->getAddressSpace(), "", &M);
1449 replaceCfiUses(F, PlaceholderFn, IsJumpTableCanonical);
1450
1452 // Don't use range based loop, because use list will be modified.
1453 while (!PlaceholderFn->use_empty()) {
1454 Use &U = *PlaceholderFn->use_begin();
1455 auto *InsertPt = dyn_cast<Instruction>(U.getUser());
1456 assert(InsertPt && "Non-instruction users should have been eliminated");
1457 auto *PN = dyn_cast<PHINode>(InsertPt);
1458 if (PN)
1459 InsertPt = PN->getIncomingBlock(U)->getTerminator();
1460 IRBuilder Builder(InsertPt);
1461 Value *ICmp = Builder.CreateICmp(CmpInst::ICMP_NE, F,
1462 Constant::getNullValue(F->getType()));
1463 Value *Select = Builder.CreateSelect(ICmp, JT,
1464 Constant::getNullValue(F->getType()));
1465
1466 if (auto *SI = dyn_cast<SelectInst>(Select))
1468 // For phi nodes, we need to update the incoming value for all operands
1469 // with the same predecessor.
1470 if (PN)
1471 PN->setIncomingValueForBlock(InsertPt->getParent(), Select);
1472 else
1473 U.set(Select);
1474 }
1475 PlaceholderFn->eraseFromParent();
1476}
1477
1478static bool isThumbFunction(Function *F, Triple::ArchType ModuleArch) {
1479 Attribute TFAttr = F->getFnAttribute("target-features");
1480 if (TFAttr.isValid()) {
1482 TFAttr.getValueAsString().split(Features, ',');
1483 for (StringRef Feature : Features) {
1484 if (Feature == "-thumb-mode")
1485 return false;
1486 else if (Feature == "+thumb-mode")
1487 return true;
1488 }
1489 }
1490
1491 return ModuleArch == Triple::thumb;
1492}
1493
1494// Each jump table must be either ARM or Thumb as a whole for the bit-test math
1495// to work. Pick one that matches the majority of members to minimize interop
1496// veneers inserted by the linker.
1497Triple::ArchType LowerTypeTestsModule::selectJumpTableArmEncoding(
1498 ArrayRef<GlobalTypeMember *> Functions) {
1499 if (Arch != Triple::arm && Arch != Triple::thumb)
1500 return Arch;
1501
1502 if (!CanUseThumbBWJumpTable && CanUseArmJumpTable) {
1503 // In architectures that provide Arm and Thumb-1 but not Thumb-2,
1504 // we should always prefer the Arm jump table format, because the
1505 // Thumb-1 one is larger and slower.
1506 return Triple::arm;
1507 }
1508
1509 // Otherwise, go with majority vote.
1510 unsigned ArmCount = 0, ThumbCount = 0;
1511 for (const auto GTM : Functions) {
1512 if (!GTM->isJumpTableCanonical()) {
1513 // PLT stubs are always ARM.
1514 // FIXME: This is the wrong heuristic for non-canonical jump tables.
1515 ++ArmCount;
1516 continue;
1517 }
1518
1519 Function *F = cast<Function>(GTM->getGlobal());
1520 ++(isThumbFunction(F, Arch) ? ThumbCount : ArmCount);
1521 }
1522
1523 return ArmCount > ThumbCount ? Triple::arm : Triple::thumb;
1524}
1525
1526void LowerTypeTestsModule::createJumpTable(
1527 Function *F, ArrayRef<GlobalTypeMember *> Functions,
1528 Triple::ArchType JumpTableArch) {
1529 BasicBlock *BB = BasicBlock::Create(M.getContext(), "entry", F);
1530 IRBuilder<> IRB(BB);
1531
1532 InlineAsm *JumpTableAsm = createJumpTableEntryAsm(JumpTableArch);
1533
1534 // Check if all entries have the NoUnwind attribute.
1535 // If all entries have it, we can safely mark the
1536 // cfi.jumptable as NoUnwind, otherwise, direct calls
1537 // to the jump table will not handle exceptions properly
1538 bool areAllEntriesNounwind = true;
1539 for (GlobalTypeMember *GTM : Functions) {
1540 if (!llvm::cast<llvm::Function>(GTM->getGlobal())
1541 ->hasFnAttribute(llvm::Attribute::NoUnwind)) {
1542 areAllEntriesNounwind = false;
1543 }
1544 IRB.CreateCall(JumpTableAsm, GTM->getGlobal());
1545 }
1546 IRB.CreateUnreachable();
1547
1548 // Align the whole table by entry size.
1549 F->setAlignment(Align(getJumpTableEntrySize(JumpTableArch)));
1550 F->addFnAttr(Attribute::Naked);
1551 if (JumpTableArch == Triple::arm)
1552 F->addFnAttr("target-features", "-thumb-mode");
1553 if (JumpTableArch == Triple::thumb) {
1554 if (hasBranchTargetEnforcement()) {
1555 // If we're generating a Thumb jump table with BTI, add a target-features
1556 // setting to ensure BTI can be assembled.
1557 F->addFnAttr("target-features", "+thumb-mode,+pacbti");
1558 } else {
1559 F->addFnAttr("target-features", "+thumb-mode");
1560 if (CanUseThumbBWJumpTable) {
1561 // Thumb jump table assembly needs Thumb2. The following attribute is
1562 // added by Clang for -march=armv7.
1563 F->addFnAttr("target-cpu", "cortex-a8");
1564 }
1565 }
1566 }
1567 // When -mbranch-protection= is used, the inline asm adds a BTI. Suppress BTI
1568 // for the function to avoid double BTI. This is a no-op without
1569 // -mbranch-protection=.
1570 if (JumpTableArch == Triple::aarch64 || JumpTableArch == Triple::thumb) {
1571 if (F->hasFnAttribute("branch-target-enforcement"))
1572 F->removeFnAttr("branch-target-enforcement");
1573 if (F->hasFnAttribute("sign-return-address"))
1574 F->removeFnAttr("sign-return-address");
1575 }
1576 if (JumpTableArch == Triple::riscv32 || JumpTableArch == Triple::riscv64) {
1577 // Make sure the jump table assembly is not modified by the assembler or
1578 // the linker.
1579 F->addFnAttr("target-features", "-c,-relax");
1580 }
1581 // When -fcf-protection= is used, the inline asm adds an ENDBR. Suppress ENDBR
1582 // for the function to avoid double ENDBR. This is a no-op without
1583 // -fcf-protection=.
1584 if (JumpTableArch == Triple::x86 || JumpTableArch == Triple::x86_64)
1585 F->addFnAttr(Attribute::NoCfCheck);
1586
1587 // Make sure we don't emit .eh_frame for this function if it isn't needed.
1588 if (areAllEntriesNounwind)
1589 F->addFnAttr(Attribute::NoUnwind);
1590
1591 // Make sure we do not inline any calls to the cfi.jumptable.
1592 F->addFnAttr(Attribute::NoInline);
1593}
1594
1595/// Given a disjoint set of type identifiers and functions, build a jump table
1596/// for the functions, build the bit sets and lower the llvm.type.test calls.
1597void LowerTypeTestsModule::buildBitSetsFromFunctionsNative(
1599 // Unlike the global bitset builder, the function bitset builder cannot
1600 // re-arrange functions in a particular order and base its calculations on the
1601 // layout of the functions' entry points, as we have no idea how large a
1602 // particular function will end up being (the size could even depend on what
1603 // this pass does!) Instead, we build a jump table, which is a block of code
1604 // consisting of one branch instruction for each of the functions in the bit
1605 // set that branches to the target function, and redirect any taken function
1606 // addresses to the corresponding jump table entry. In the object file's
1607 // symbol table, the symbols for the target functions also refer to the jump
1608 // table entries, so that addresses taken outside the module will pass any
1609 // verification done inside the module.
1610 //
1611 // In more concrete terms, suppose we have three functions f, g, h which are
1612 // of the same type, and a function foo that returns their addresses:
1613 //
1614 // f:
1615 // mov 0, %eax
1616 // ret
1617 //
1618 // g:
1619 // mov 1, %eax
1620 // ret
1621 //
1622 // h:
1623 // mov 2, %eax
1624 // ret
1625 //
1626 // foo:
1627 // mov f, %eax
1628 // mov g, %edx
1629 // mov h, %ecx
1630 // ret
1631 //
1632 // We output the jump table as module-level inline asm string. The end result
1633 // will (conceptually) look like this:
1634 //
1635 // f = .cfi.jumptable
1636 // g = .cfi.jumptable + 4
1637 // h = .cfi.jumptable + 8
1638 // .cfi.jumptable:
1639 // jmp f.cfi ; 5 bytes
1640 // int3 ; 1 byte
1641 // int3 ; 1 byte
1642 // int3 ; 1 byte
1643 // jmp g.cfi ; 5 bytes
1644 // int3 ; 1 byte
1645 // int3 ; 1 byte
1646 // int3 ; 1 byte
1647 // jmp h.cfi ; 5 bytes
1648 // int3 ; 1 byte
1649 // int3 ; 1 byte
1650 // int3 ; 1 byte
1651 //
1652 // f.cfi:
1653 // mov 0, %eax
1654 // ret
1655 //
1656 // g.cfi:
1657 // mov 1, %eax
1658 // ret
1659 //
1660 // h.cfi:
1661 // mov 2, %eax
1662 // ret
1663 //
1664 // foo:
1665 // mov f, %eax
1666 // mov g, %edx
1667 // mov h, %ecx
1668 // ret
1669 //
1670 // Because the addresses of f, g, h are evenly spaced at a power of 2, in the
1671 // normal case the check can be carried out using the same kind of simple
1672 // arithmetic that we normally use for globals.
1673
1674 // FIXME: find a better way to represent the jumptable in the IR.
1675 assert(!Functions.empty());
1676
1677 // Decide on the jump table encoding, so that we know how big the
1678 // entries will be.
1679 Triple::ArchType JumpTableArch = selectJumpTableArmEncoding(Functions);
1680
1681 // Build a simple layout based on the regular layout of jump tables.
1682 DenseMap<GlobalTypeMember *, uint64_t> GlobalLayout;
1683 unsigned EntrySize = getJumpTableEntrySize(JumpTableArch);
1684 for (unsigned I = 0; I != Functions.size(); ++I)
1685 GlobalLayout[Functions[I]] = I * EntrySize;
1686
1687 Function *JumpTableFn =
1689 /* IsVarArg */ false),
1691 M.getDataLayout().getProgramAddressSpace(),
1692 ".cfi.jumptable", &M);
1693 ArrayType *JumpTableEntryType = ArrayType::get(Int8Ty, EntrySize);
1695 ArrayType::get(JumpTableEntryType, Functions.size());
1697 JumpTableFn, PointerType::getUnqual(M.getContext()));
1698
1699 lowerTypeTestCalls(TypeIds, JumpTable, GlobalLayout);
1700
1701 // Build aliases pointing to offsets into the jump table, and replace
1702 // references to the original functions with references to the aliases.
1703 for (unsigned I = 0; I != Functions.size(); ++I) {
1704 Function *F = cast<Function>(Functions[I]->getGlobal());
1705 bool IsJumpTableCanonical = Functions[I]->isJumpTableCanonical();
1706
1707 Constant *CombinedGlobalElemPtr = ConstantExpr::getInBoundsGetElementPtr(
1708 JumpTableType, JumpTable,
1709 ArrayRef<Constant *>{ConstantInt::get(IntPtrTy, 0),
1710 ConstantInt::get(IntPtrTy, I)});
1711
1712 const bool IsExported = Functions[I]->isExported();
1713 if (!IsJumpTableCanonical) {
1716 GlobalAlias *JtAlias = GlobalAlias::create(JumpTableEntryType, 0, LT,
1717 F->getName() + ".cfi_jt",
1718 CombinedGlobalElemPtr, &M);
1719 if (IsExported)
1721 else
1722 appendToUsed(M, {JtAlias});
1723 }
1724
1725 if (IsExported) {
1726 if (IsJumpTableCanonical)
1727 ExportSummary->cfiFunctionDefs().emplace(F->getName());
1728 else
1729 ExportSummary->cfiFunctionDecls().emplace(F->getName());
1730 }
1731
1732 if (!IsJumpTableCanonical) {
1733 if (F->hasExternalWeakLinkage())
1734 replaceWeakDeclarationWithJumpTablePtr(F, CombinedGlobalElemPtr,
1735 IsJumpTableCanonical);
1736 else
1737 replaceCfiUses(F, CombinedGlobalElemPtr, IsJumpTableCanonical);
1738 } else {
1739 assert(F->getType()->getAddressSpace() == 0);
1740
1741 GlobalAlias *FAlias =
1742 GlobalAlias::create(JumpTableEntryType, 0, F->getLinkage(), "",
1743 CombinedGlobalElemPtr, &M);
1744 FAlias->setVisibility(F->getVisibility());
1745 FAlias->takeName(F);
1746 if (FAlias->hasName()) {
1747 F->setName(FAlias->getName() + ".cfi");
1748 maybeReplaceComdat(F, FAlias->getName());
1749 }
1750 replaceCfiUses(F, FAlias, IsJumpTableCanonical);
1751 if (!F->hasLocalLinkage())
1752 F->setVisibility(GlobalVariable::HiddenVisibility);
1753 }
1754 }
1755
1756 createJumpTable(JumpTableFn, Functions, JumpTableArch);
1757}
1758
1759/// Assign a dummy layout using an incrementing counter, tag each function
1760/// with its index represented as metadata, and lower each type test to an
1761/// integer range comparison. During generation of the indirect function call
1762/// table in the backend, it will assign the given indexes.
1763/// Note: Dynamic linking is not supported, as the WebAssembly ABI has not yet
1764/// been finalized.
1765void LowerTypeTestsModule::buildBitSetsFromFunctionsWASM(
1767 assert(!Functions.empty());
1768
1769 // Build consecutive monotonic integer ranges for each call target set
1770 DenseMap<GlobalTypeMember *, uint64_t> GlobalLayout;
1771
1772 for (GlobalTypeMember *GTM : Functions) {
1773 Function *F = cast<Function>(GTM->getGlobal());
1774
1775 // Skip functions that are not address taken, to avoid bloating the table
1776 if (!F->hasAddressTaken())
1777 continue;
1778
1779 // Store metadata with the index for each function
1780 MDNode *MD = MDNode::get(F->getContext(),
1782 ConstantInt::get(Int64Ty, IndirectIndex))));
1783 F->setMetadata("wasm.index", MD);
1784
1785 // Assign the counter value
1786 GlobalLayout[GTM] = IndirectIndex++;
1787 }
1788
1789 // The indirect function table index space starts at zero, so pass a NULL
1790 // pointer as the subtracted "jump table" offset.
1791 lowerTypeTestCalls(TypeIds, ConstantPointerNull::get(PtrTy),
1792 GlobalLayout);
1793}
1794
1795void LowerTypeTestsModule::buildBitSetsFromDisjointSet(
1797 ArrayRef<ICallBranchFunnel *> ICallBranchFunnels) {
1798 DenseMap<Metadata *, uint64_t> TypeIdIndices;
1799 for (unsigned I = 0; I != TypeIds.size(); ++I)
1800 TypeIdIndices[TypeIds[I]] = I;
1801
1802 // For each type identifier, build a set of indices that refer to members of
1803 // the type identifier.
1804 std::vector<std::set<uint64_t>> TypeMembers(TypeIds.size());
1805 unsigned GlobalIndex = 0;
1806 DenseMap<GlobalTypeMember *, uint64_t> GlobalIndices;
1807 for (GlobalTypeMember *GTM : Globals) {
1808 for (MDNode *Type : GTM->types()) {
1809 // Type = { offset, type identifier }
1810 auto I = TypeIdIndices.find(Type->getOperand(1));
1811 if (I != TypeIdIndices.end())
1812 TypeMembers[I->second].insert(GlobalIndex);
1813 }
1814 GlobalIndices[GTM] = GlobalIndex;
1815 GlobalIndex++;
1816 }
1817
1818 for (ICallBranchFunnel *JT : ICallBranchFunnels) {
1819 TypeMembers.emplace_back();
1820 std::set<uint64_t> &TMSet = TypeMembers.back();
1821 for (GlobalTypeMember *T : JT->targets())
1822 TMSet.insert(GlobalIndices[T]);
1823 }
1824
1825 // Order the sets of indices by size. The GlobalLayoutBuilder works best
1826 // when given small index sets first.
1827 llvm::stable_sort(TypeMembers, [](const std::set<uint64_t> &O1,
1828 const std::set<uint64_t> &O2) {
1829 return O1.size() < O2.size();
1830 });
1831
1832 // Create a GlobalLayoutBuilder and provide it with index sets as layout
1833 // fragments. The GlobalLayoutBuilder tries to lay out members of fragments as
1834 // close together as possible.
1835 GlobalLayoutBuilder GLB(Globals.size());
1836 for (auto &&MemSet : TypeMembers)
1837 GLB.addFragment(MemSet);
1838
1839 // Build a vector of globals with the computed layout.
1840 bool IsGlobalSet =
1841 Globals.empty() || isa<GlobalVariable>(Globals[0]->getGlobal());
1842 std::vector<GlobalTypeMember *> OrderedGTMs(Globals.size());
1843 auto OGTMI = OrderedGTMs.begin();
1844 for (auto &&F : GLB.Fragments) {
1845 for (auto &&Offset : F) {
1846 if (IsGlobalSet != isa<GlobalVariable>(Globals[Offset]->getGlobal()))
1847 report_fatal_error("Type identifier may not contain both global "
1848 "variables and functions");
1849 *OGTMI++ = Globals[Offset];
1850 }
1851 }
1852
1853 // Build the bitsets from this disjoint set.
1854 if (IsGlobalSet)
1855 buildBitSetsFromGlobalVariables(TypeIds, OrderedGTMs);
1856 else
1857 buildBitSetsFromFunctions(TypeIds, OrderedGTMs);
1858}
1859
1860/// Lower all type tests in this module.
1861LowerTypeTestsModule::LowerTypeTestsModule(
1862 Module &M, ModuleAnalysisManager &AM, ModuleSummaryIndex *ExportSummary,
1863 const ModuleSummaryIndex *ImportSummary)
1864 : M(M), ExportSummary(ExportSummary), ImportSummary(ImportSummary) {
1865 assert(!(ExportSummary && ImportSummary));
1866 Triple TargetTriple(M.getTargetTriple());
1867 Arch = TargetTriple.getArch();
1868 if (Arch == Triple::arm)
1869 CanUseArmJumpTable = true;
1870 if (Arch == Triple::arm || Arch == Triple::thumb) {
1871 auto &FAM =
1873 for (Function &F : M) {
1874 // Skip declarations since we should not query the TTI for them.
1875 if (F.isDeclaration())
1876 continue;
1877 auto &TTI = FAM.getResult<TargetIRAnalysis>(F);
1878 if (TTI.hasArmWideBranch(false))
1879 CanUseArmJumpTable = true;
1880 if (TTI.hasArmWideBranch(true))
1881 CanUseThumbBWJumpTable = true;
1882 }
1883 }
1884 OS = TargetTriple.getOS();
1885 ObjectFormat = TargetTriple.getObjectFormat();
1886
1887 // Function annotation describes or applies to function itself, and
1888 // shouldn't be associated with jump table thunk generated for CFI.
1889 GlobalAnnotation = M.getGlobalVariable("llvm.global.annotations");
1890 if (GlobalAnnotation && GlobalAnnotation->hasInitializer()) {
1891 const ConstantArray *CA =
1892 cast<ConstantArray>(GlobalAnnotation->getInitializer());
1893 FunctionAnnotations.insert_range(CA->operands());
1894 }
1895}
1896
1897bool LowerTypeTestsModule::runForTesting(Module &M, ModuleAnalysisManager &AM) {
1898 ModuleSummaryIndex Summary(/*HaveGVs=*/false);
1899
1900 // Handle the command-line summary arguments. This code is for testing
1901 // purposes only, so we handle errors directly.
1902 if (!ClReadSummary.empty()) {
1903 ExitOnError ExitOnErr("-lowertypetests-read-summary: " + ClReadSummary +
1904 ": ");
1905 auto ReadSummaryFile = ExitOnErr(errorOrToExpected(
1906 MemoryBuffer::getFile(ClReadSummary, /*IsText=*/true)));
1907
1908 yaml::Input In(ReadSummaryFile->getBuffer());
1909 In >> Summary;
1910 ExitOnErr(errorCodeToError(In.error()));
1911 }
1912
1913 bool Changed =
1914 LowerTypeTestsModule(
1915 M, AM,
1916 ClSummaryAction == PassSummaryAction::Export ? &Summary : nullptr,
1917 ClSummaryAction == PassSummaryAction::Import ? &Summary : nullptr)
1918 .lower();
1919
1920 if (!ClWriteSummary.empty()) {
1921 ExitOnError ExitOnErr("-lowertypetests-write-summary: " + ClWriteSummary +
1922 ": ");
1923 std::error_code EC;
1924 raw_fd_ostream OS(ClWriteSummary, EC, sys::fs::OF_TextWithCRLF);
1925 ExitOnErr(errorCodeToError(EC));
1926
1927 yaml::Output Out(OS);
1928 Out << Summary;
1929 }
1930
1931 return Changed;
1932}
1933
1934static bool isDirectCall(Use& U) {
1935 auto *Usr = dyn_cast<CallInst>(U.getUser());
1936 return Usr && Usr->isCallee(&U);
1937}
1938
1939void LowerTypeTestsModule::replaceCfiUses(Function *Old, Value *New,
1940 bool IsJumpTableCanonical) {
1941 SmallSetVector<Constant *, 4> Constants;
1942 for (Use &U : llvm::make_early_inc_range(Old->uses())) {
1943 // Skip no_cfi values, which refer to the function body instead of the jump
1944 // table.
1945 if (isa<NoCFIValue>(U.getUser()))
1946 continue;
1947
1948 // Skip direct calls to externally defined or non-dso_local functions.
1949 if (isDirectCall(U) && (Old->isDSOLocal() || !IsJumpTableCanonical))
1950 continue;
1951
1952 // Skip function annotation.
1953 if (isFunctionAnnotation(U.getUser()))
1954 continue;
1955
1956 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
1957 // constant because they are uniqued.
1958 if (auto *C = dyn_cast<Constant>(U.getUser())) {
1959 if (!isa<GlobalValue>(C)) {
1960 // Save unique users to avoid processing operand replacement
1961 // more than once.
1962 Constants.insert(C);
1963 continue;
1964 }
1965 }
1966
1967 U.set(New);
1968 }
1969
1970 // Process operand replacement of saved constants.
1971 for (auto *C : Constants)
1972 C->handleOperandChange(Old, New);
1973}
1974
1975void LowerTypeTestsModule::replaceDirectCalls(Value *Old, Value *New) {
1977}
1978
1979static void dropTypeTests(Module &M, Function &TypeTestFunc,
1980 bool ShouldDropAll) {
1981 for (Use &U : llvm::make_early_inc_range(TypeTestFunc.uses())) {
1982 auto *CI = cast<CallInst>(U.getUser());
1983 // Find and erase llvm.assume intrinsics for this llvm.type.test call.
1984 for (Use &CIU : llvm::make_early_inc_range(CI->uses()))
1985 if (auto *Assume = dyn_cast<AssumeInst>(CIU.getUser()))
1986 Assume->eraseFromParent();
1987 // If the assume was merged with another assume, we might have a use on a
1988 // phi or select (which will feed the assume). Simply replace the use on
1989 // the phi/select with "true" and leave the merged assume.
1990 //
1991 // If ShouldDropAll is set, then we we need to update any remaining uses,
1992 // regardless of the instruction type.
1993 if (!CI->use_empty()) {
1994 assert(ShouldDropAll || all_of(CI->users(), [](User *U) -> bool {
1995 return isa<PHINode>(U) || isa<SelectInst>(U);
1996 }));
1997 CI->replaceAllUsesWith(ConstantInt::getTrue(M.getContext()));
1998 }
1999 CI->eraseFromParent();
2000 }
2001}
2002
2003static bool dropTypeTests(Module &M, bool ShouldDropAll) {
2004 Function *TypeTestFunc =
2005 Intrinsic::getDeclarationIfExists(&M, Intrinsic::type_test);
2006 if (TypeTestFunc)
2007 dropTypeTests(M, *TypeTestFunc, ShouldDropAll);
2008 // Normally we'd have already removed all @llvm.public.type.test calls,
2009 // except for in the case where we originally were performing ThinLTO but
2010 // decided not to in the backend.
2011 Function *PublicTypeTestFunc =
2012 Intrinsic::getDeclarationIfExists(&M, Intrinsic::public_type_test);
2013 if (PublicTypeTestFunc)
2014 dropTypeTests(M, *PublicTypeTestFunc, ShouldDropAll);
2015 if (TypeTestFunc || PublicTypeTestFunc) {
2016 // We have deleted the type intrinsics, so we no longer have enough
2017 // information to reason about the liveness of virtual function pointers
2018 // in GlobalDCE.
2019 for (GlobalVariable &GV : M.globals())
2020 GV.eraseMetadata(LLVMContext::MD_vcall_visibility);
2021 return true;
2022 }
2023 return false;
2024}
2025
2026bool LowerTypeTestsModule::lower() {
2027 Function *TypeTestFunc =
2028 Intrinsic::getDeclarationIfExists(&M, Intrinsic::type_test);
2029
2030 // If only some of the modules were split, we cannot correctly perform
2031 // this transformation. We already checked for the presense of type tests
2032 // with partially split modules during the thin link, and would have emitted
2033 // an error if any were found, so here we can simply return.
2034 if ((ExportSummary && ExportSummary->partiallySplitLTOUnits()) ||
2035 (ImportSummary && ImportSummary->partiallySplitLTOUnits()))
2036 return false;
2037
2038 Function *ICallBranchFunnelFunc =
2039 Intrinsic::getDeclarationIfExists(&M, Intrinsic::icall_branch_funnel);
2040 if ((!TypeTestFunc || TypeTestFunc->use_empty()) &&
2041 (!ICallBranchFunnelFunc || ICallBranchFunnelFunc->use_empty()) &&
2042 !ExportSummary && !ImportSummary)
2043 return false;
2044
2045 if (ImportSummary) {
2046 if (TypeTestFunc)
2047 for (Use &U : llvm::make_early_inc_range(TypeTestFunc->uses()))
2048 importTypeTest(cast<CallInst>(U.getUser()));
2049
2050 if (ICallBranchFunnelFunc && !ICallBranchFunnelFunc->use_empty())
2052 "unexpected call to llvm.icall.branch.funnel during import phase");
2053
2056 for (auto &F : M) {
2057 // CFI functions are either external, or promoted. A local function may
2058 // have the same name, but it's not the one we are looking for.
2059 if (F.hasLocalLinkage())
2060 continue;
2061 if (ImportSummary->cfiFunctionDefs().count(F.getName()))
2062 Defs.push_back(&F);
2063 else if (ImportSummary->cfiFunctionDecls().count(F.getName()))
2064 Decls.push_back(&F);
2065 }
2066
2067 {
2068 ScopedSaveAliaseesAndUsed S(M);
2069 for (auto *F : Defs)
2070 importFunction(F, /*isJumpTableCanonical*/ true);
2071 for (auto *F : Decls)
2072 importFunction(F, /*isJumpTableCanonical*/ false);
2073 }
2074
2075 return true;
2076 }
2077
2078 // Equivalence class set containing type identifiers and the globals that
2079 // reference them. This is used to partition the set of type identifiers in
2080 // the module into disjoint sets.
2081 using GlobalClassesTy = EquivalenceClasses<
2082 PointerUnion<GlobalTypeMember *, Metadata *, ICallBranchFunnel *>>;
2083 GlobalClassesTy GlobalClasses;
2084
2085 // Verify the type metadata and build a few data structures to let us
2086 // efficiently enumerate the type identifiers associated with a global:
2087 // a list of GlobalTypeMembers (a GlobalObject stored alongside a vector
2088 // of associated type metadata) and a mapping from type identifiers to their
2089 // list of GlobalTypeMembers and last observed index in the list of globals.
2090 // The indices will be used later to deterministically order the list of type
2091 // identifiers.
2093 struct TIInfo {
2094 unsigned UniqueId;
2095 std::vector<GlobalTypeMember *> RefGlobals;
2096 };
2097 DenseMap<Metadata *, TIInfo> TypeIdInfo;
2098 unsigned CurUniqueId = 0;
2100
2101 // Cross-DSO CFI emits jumptable entries for exported functions as well as
2102 // address taken functions in case they are address taken in other modules.
2103 const bool CrossDsoCfi = M.getModuleFlag("Cross-DSO CFI") != nullptr;
2104
2105 struct ExportedFunctionInfo {
2107 MDNode *FuncMD; // {name, linkage, type[, type...]}
2108 };
2109 MapVector<StringRef, ExportedFunctionInfo> ExportedFunctions;
2110 if (ExportSummary) {
2111 NamedMDNode *CfiFunctionsMD = M.getNamedMetadata("cfi.functions");
2112 if (CfiFunctionsMD) {
2113 // A set of all functions that are address taken by a live global object.
2114 DenseSet<GlobalValue::GUID> AddressTaken;
2115 for (auto &I : *ExportSummary)
2116 for (auto &GVS : I.second.getSummaryList())
2117 if (GVS->isLive())
2118 for (const auto &Ref : GVS->refs()) {
2119 AddressTaken.insert(Ref.getGUID());
2120 for (auto &RefGVS : Ref.getSummaryList())
2121 if (auto Alias = dyn_cast<AliasSummary>(RefGVS.get()))
2122 AddressTaken.insert(Alias->getAliaseeGUID());
2123 }
2125 if (AddressTaken.count(GUID))
2126 return true;
2127 auto VI = ExportSummary->getValueInfo(GUID);
2128 if (!VI)
2129 return false;
2130 for (auto &I : VI.getSummaryList())
2131 if (auto Alias = dyn_cast<AliasSummary>(I.get()))
2132 if (AddressTaken.count(Alias->getAliaseeGUID()))
2133 return true;
2134 return false;
2135 };
2136 for (auto *FuncMD : CfiFunctionsMD->operands()) {
2137 assert(FuncMD->getNumOperands() >= 2);
2138 StringRef FunctionName =
2139 cast<MDString>(FuncMD->getOperand(0))->getString();
2141 cast<ConstantAsMetadata>(FuncMD->getOperand(1))
2142 ->getValue()
2143 ->getUniqueInteger()
2144 .getZExtValue());
2145 const GlobalValue::GUID GUID =
2148 // Do not emit jumptable entries for functions that are not-live and
2149 // have no live references (and are not exported with cross-DSO CFI.)
2150 if (!ExportSummary->isGUIDLive(GUID))
2151 continue;
2152 if (!IsAddressTaken(GUID)) {
2153 if (!CrossDsoCfi || Linkage != CFL_Definition)
2154 continue;
2155
2156 bool Exported = false;
2157 if (auto VI = ExportSummary->getValueInfo(GUID))
2158 for (const auto &GVS : VI.getSummaryList())
2159 if (GVS->isLive() && !GlobalValue::isLocalLinkage(GVS->linkage()))
2160 Exported = true;
2161
2162 if (!Exported)
2163 continue;
2164 }
2165 auto P = ExportedFunctions.insert({FunctionName, {Linkage, FuncMD}});
2166 if (!P.second && P.first->second.Linkage != CFL_Definition)
2167 P.first->second = {Linkage, FuncMD};
2168 }
2169
2170 for (const auto &P : ExportedFunctions) {
2171 StringRef FunctionName = P.first;
2172 CfiFunctionLinkage Linkage = P.second.Linkage;
2173 MDNode *FuncMD = P.second.FuncMD;
2174 Function *F = M.getFunction(FunctionName);
2175 if (F && F->hasLocalLinkage()) {
2176 // Locally defined function that happens to have the same name as a
2177 // function defined in a ThinLTO module. Rename it to move it out of
2178 // the way of the external reference that we're about to create.
2179 // Note that setName will find a unique name for the function, so even
2180 // if there is an existing function with the suffix there won't be a
2181 // name collision.
2182 F->setName(F->getName() + ".1");
2183 F = nullptr;
2184 }
2185
2186 if (!F)
2188 FunctionType::get(Type::getVoidTy(M.getContext()), false),
2189 GlobalVariable::ExternalLinkage,
2190 M.getDataLayout().getProgramAddressSpace(), FunctionName, &M);
2191
2192 // If the function is available_externally, remove its definition so
2193 // that it is handled the same way as a declaration. Later we will try
2194 // to create an alias using this function's linkage, which will fail if
2195 // the linkage is available_externally. This will also result in us
2196 // following the code path below to replace the type metadata.
2197 if (F->hasAvailableExternallyLinkage()) {
2198 F->setLinkage(GlobalValue::ExternalLinkage);
2199 F->deleteBody();
2200 F->setComdat(nullptr);
2201 F->clearMetadata();
2202 }
2203
2204 // Update the linkage for extern_weak declarations when a definition
2205 // exists.
2206 if (Linkage == CFL_Definition && F->hasExternalWeakLinkage())
2207 F->setLinkage(GlobalValue::ExternalLinkage);
2208
2209 // If the function in the full LTO module is a declaration, replace its
2210 // type metadata with the type metadata we found in cfi.functions. That
2211 // metadata is presumed to be more accurate than the metadata attached
2212 // to the declaration.
2213 if (F->isDeclaration()) {
2216
2217 F->eraseMetadata(LLVMContext::MD_type);
2218 for (unsigned I = 2; I < FuncMD->getNumOperands(); ++I)
2219 F->addMetadata(LLVMContext::MD_type,
2220 *cast<MDNode>(FuncMD->getOperand(I).get()));
2221 }
2222 }
2223 }
2224 }
2225
2226 struct AliasToCreate {
2227 Function *Alias;
2228 std::string TargetName;
2229 };
2230 std::vector<AliasToCreate> AliasesToCreate;
2231
2232 // Parse alias data to replace stand-in function declarations for aliases
2233 // with an alias to the intended target.
2234 if (ExportSummary) {
2235 if (NamedMDNode *AliasesMD = M.getNamedMetadata("aliases")) {
2236 for (auto *AliasMD : AliasesMD->operands()) {
2238 for (Metadata *MD : AliasMD->operands()) {
2239 auto *MDS = dyn_cast<MDString>(MD);
2240 if (!MDS)
2241 continue;
2242 StringRef AliasName = MDS->getString();
2243 if (!ExportedFunctions.count(AliasName))
2244 continue;
2245 auto *AliasF = M.getFunction(AliasName);
2246 if (AliasF)
2247 Aliases.push_back(AliasF);
2248 }
2249
2250 if (Aliases.empty())
2251 continue;
2252
2253 for (unsigned I = 1; I != Aliases.size(); ++I) {
2254 auto *AliasF = Aliases[I];
2255 ExportedFunctions.erase(AliasF->getName());
2256 AliasesToCreate.push_back(
2257 {AliasF, std::string(Aliases[0]->getName())});
2258 }
2259 }
2260 }
2261 }
2262
2263 DenseMap<GlobalObject *, GlobalTypeMember *> GlobalTypeMembers;
2264 for (GlobalObject &GO : M.global_objects()) {
2266 continue;
2267
2268 Types.clear();
2269 GO.getMetadata(LLVMContext::MD_type, Types);
2270
2271 bool IsJumpTableCanonical = false;
2272 bool IsExported = false;
2273 if (Function *F = dyn_cast<Function>(&GO)) {
2274 IsJumpTableCanonical = isJumpTableCanonical(F);
2275 if (auto It = ExportedFunctions.find(F->getName());
2276 It != ExportedFunctions.end()) {
2277 IsJumpTableCanonical |= It->second.Linkage == CFL_Definition;
2278 IsExported = true;
2279 // TODO: The logic here checks only that the function is address taken,
2280 // not that the address takers are live. This can be updated to check
2281 // their liveness and emit fewer jumptable entries once monolithic LTO
2282 // builds also emit summaries.
2283 } else if (!F->hasAddressTaken()) {
2284 if (!CrossDsoCfi || !IsJumpTableCanonical || F->hasLocalLinkage())
2285 continue;
2286 }
2287 }
2288
2289 auto *GTM = GlobalTypeMember::create(Alloc, &GO, IsJumpTableCanonical,
2290 IsExported, Types);
2291 GlobalTypeMembers[&GO] = GTM;
2292 for (MDNode *Type : Types) {
2293 verifyTypeMDNode(&GO, Type);
2294 auto &Info = TypeIdInfo[Type->getOperand(1)];
2295 Info.UniqueId = ++CurUniqueId;
2296 Info.RefGlobals.push_back(GTM);
2297 }
2298 }
2299
2300 auto AddTypeIdUse = [&](Metadata *TypeId) -> TypeIdUserInfo & {
2301 // Add the call site to the list of call sites for this type identifier. We
2302 // also use TypeIdUsers to keep track of whether we have seen this type
2303 // identifier before. If we have, we don't need to re-add the referenced
2304 // globals to the equivalence class.
2305 auto Ins = TypeIdUsers.insert({TypeId, {}});
2306 if (Ins.second) {
2307 // Add the type identifier to the equivalence class.
2308 auto &GCI = GlobalClasses.insert(TypeId);
2309 GlobalClassesTy::member_iterator CurSet = GlobalClasses.findLeader(GCI);
2310
2311 // Add the referenced globals to the type identifier's equivalence class.
2312 for (GlobalTypeMember *GTM : TypeIdInfo[TypeId].RefGlobals)
2313 CurSet = GlobalClasses.unionSets(
2314 CurSet, GlobalClasses.findLeader(GlobalClasses.insert(GTM)));
2315 }
2316
2317 return Ins.first->second;
2318 };
2319
2320 if (TypeTestFunc) {
2321 for (const Use &U : TypeTestFunc->uses()) {
2322 auto CI = cast<CallInst>(U.getUser());
2323 // If this type test is only used by llvm.assume instructions, it
2324 // was used for whole program devirtualization, and is being kept
2325 // for use by other optimization passes. We do not need or want to
2326 // lower it here. We also don't want to rewrite any associated globals
2327 // unnecessarily. These will be removed by a subsequent LTT invocation
2328 // with the DropTypeTests flag set.
2329 bool OnlyAssumeUses = !CI->use_empty();
2330 for (const Use &CIU : CI->uses()) {
2331 if (isa<AssumeInst>(CIU.getUser()))
2332 continue;
2333 OnlyAssumeUses = false;
2334 break;
2335 }
2336 if (OnlyAssumeUses)
2337 continue;
2338
2339 auto TypeIdMDVal = dyn_cast<MetadataAsValue>(CI->getArgOperand(1));
2340 if (!TypeIdMDVal)
2341 report_fatal_error("Second argument of llvm.type.test must be metadata");
2342 auto TypeId = TypeIdMDVal->getMetadata();
2343 AddTypeIdUse(TypeId).CallSites.push_back(CI);
2344 }
2345 }
2346
2347 if (ICallBranchFunnelFunc) {
2348 for (const Use &U : ICallBranchFunnelFunc->uses()) {
2349 if (Arch != Triple::x86_64)
2351 "llvm.icall.branch.funnel not supported on this target");
2352
2353 auto CI = cast<CallInst>(U.getUser());
2354
2355 std::vector<GlobalTypeMember *> Targets;
2356 if (CI->arg_size() % 2 != 1)
2357 report_fatal_error("number of arguments should be odd");
2358
2359 GlobalClassesTy::member_iterator CurSet;
2360 for (unsigned I = 1; I != CI->arg_size(); I += 2) {
2361 int64_t Offset;
2363 CI->getOperand(I), Offset, M.getDataLayout()));
2364 if (!Base)
2366 "Expected branch funnel operand to be global value");
2367
2368 GlobalTypeMember *GTM = GlobalTypeMembers[Base];
2369 Targets.push_back(GTM);
2370 GlobalClassesTy::member_iterator NewSet =
2371 GlobalClasses.findLeader(GlobalClasses.insert(GTM));
2372 if (I == 1)
2373 CurSet = NewSet;
2374 else
2375 CurSet = GlobalClasses.unionSets(CurSet, NewSet);
2376 }
2377
2378 GlobalClasses.unionSets(
2379 CurSet, GlobalClasses.findLeader(
2380 GlobalClasses.insert(ICallBranchFunnel::create(
2381 Alloc, CI, Targets, ++CurUniqueId))));
2382 }
2383 }
2384
2385 if (ExportSummary) {
2386 DenseMap<GlobalValue::GUID, TinyPtrVector<Metadata *>> MetadataByGUID;
2387 for (auto &P : TypeIdInfo) {
2388 if (auto *TypeId = dyn_cast<MDString>(P.first))
2390 TypeId->getString())]
2391 .push_back(TypeId);
2392 }
2393
2394 for (auto &P : *ExportSummary) {
2395 for (auto &S : P.second.getSummaryList()) {
2396 if (!ExportSummary->isGlobalValueLive(S.get()))
2397 continue;
2398 if (auto *FS = dyn_cast<FunctionSummary>(S->getBaseObject()))
2399 for (GlobalValue::GUID G : FS->type_tests())
2400 for (Metadata *MD : MetadataByGUID[G])
2401 AddTypeIdUse(MD).IsExported = true;
2402 }
2403 }
2404 }
2405
2406 if (GlobalClasses.empty())
2407 return false;
2408
2409 {
2410 ScopedSaveAliaseesAndUsed S(M);
2411 // For each disjoint set we found...
2412 for (const auto &C : GlobalClasses) {
2413 if (!C->isLeader())
2414 continue;
2415
2416 ++NumTypeIdDisjointSets;
2417 // Build the list of type identifiers in this disjoint set.
2418 std::vector<Metadata *> TypeIds;
2419 std::vector<GlobalTypeMember *> Globals;
2420 std::vector<ICallBranchFunnel *> ICallBranchFunnels;
2421 for (auto M : GlobalClasses.members(*C)) {
2422 if (isa<Metadata *>(M))
2423 TypeIds.push_back(cast<Metadata *>(M));
2424 else if (isa<GlobalTypeMember *>(M))
2425 Globals.push_back(cast<GlobalTypeMember *>(M));
2426 else
2427 ICallBranchFunnels.push_back(cast<ICallBranchFunnel *>(M));
2428 }
2429
2430 // Order type identifiers by unique ID for determinism. This ordering is
2431 // stable as there is a one-to-one mapping between metadata and unique
2432 // IDs.
2433 llvm::sort(TypeIds, [&](Metadata *M1, Metadata *M2) {
2434 return TypeIdInfo[M1].UniqueId < TypeIdInfo[M2].UniqueId;
2435 });
2436
2437 // Same for the branch funnels.
2438 llvm::sort(ICallBranchFunnels,
2439 [&](ICallBranchFunnel *F1, ICallBranchFunnel *F2) {
2440 return F1->UniqueId < F2->UniqueId;
2441 });
2442
2443 // Build bitsets for this disjoint set.
2444 buildBitSetsFromDisjointSet(TypeIds, Globals, ICallBranchFunnels);
2445 }
2446 }
2447
2448 allocateByteArrays();
2449
2450 for (auto A : AliasesToCreate) {
2451 auto *Target = M.getNamedValue(A.TargetName);
2452 if (!isa<GlobalAlias>(Target))
2453 continue;
2454 auto *AliasGA = GlobalAlias::create("", Target);
2455 AliasGA->setVisibility(A.Alias->getVisibility());
2456 AliasGA->setLinkage(A.Alias->getLinkage());
2457 AliasGA->takeName(A.Alias);
2458 A.Alias->replaceAllUsesWith(AliasGA);
2459 A.Alias->eraseFromParent();
2460 }
2461
2462 // Emit .symver directives for exported functions, if they exist.
2463 if (ExportSummary) {
2464 if (NamedMDNode *SymversMD = M.getNamedMetadata("symvers")) {
2465 for (auto *Symver : SymversMD->operands()) {
2466 assert(Symver->getNumOperands() >= 2);
2467 StringRef SymbolName =
2468 cast<MDString>(Symver->getOperand(0))->getString();
2469 StringRef Alias = cast<MDString>(Symver->getOperand(1))->getString();
2470
2471 if (!ExportedFunctions.count(SymbolName))
2472 continue;
2473
2474 M.appendModuleInlineAsm(
2475 (llvm::Twine(".symver ") + SymbolName + ", " + Alias).str());
2476 }
2477 }
2478 }
2479
2480 return true;
2481}
2482
2485 bool Changed;
2486 if (UseCommandLine)
2487 Changed = LowerTypeTestsModule::runForTesting(M, AM);
2488 else
2489 Changed = LowerTypeTestsModule(M, AM, ExportSummary, ImportSummary).lower();
2490 if (!Changed)
2491 return PreservedAnalyses::all();
2492 return PreservedAnalyses::none();
2493}
2494
2496 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
2497 static_cast<PassInfoMixin<DropTypeTestsPass> *>(this)->printPipeline(
2498 OS, MapClassName2PassName);
2499 OS << '<';
2500 switch (Kind) {
2501 case DropTestKind::Assume:
2502 OS << "assume";
2503 break;
2504 case DropTestKind::All:
2505 OS << "all";
2506 break;
2507 }
2508 OS << '>';
2509}
2510
2515
2518 bool Changed = false;
2519 // Figure out whether inlining has exposed a constant address to a lowered
2520 // type test, and remove the test if so and the address is known to pass the
2521 // test. Unfortunately this pass ends up needing to reverse engineer what
2522 // LowerTypeTests did; this is currently inherent to the design of ThinLTO
2523 // importing where LowerTypeTests needs to run at the start.
2524 //
2525 // We look for things like:
2526 //
2527 // sub (i64 ptrtoint (ptr @_Z2fpv to i64), i64 ptrtoint (ptr
2528 // @__typeid__ZTSFvvE_global_addr to i64))
2529 //
2530 // which gets replaced with 0 if _Z2fpv (more specifically _Z2fpv.cfi, the
2531 // function referred to by the jump table) is a member of the type _ZTSFvv, as
2532 // well as things like
2533 //
2534 // icmp eq ptr @_Z2fpv, @__typeid__ZTSFvvE_global_addr
2535 //
2536 // which gets replaced with true if _Z2fpv is a member.
2537 for (auto &GV : M.globals()) {
2538 if (!GV.getName().starts_with("__typeid_") ||
2539 !GV.getName().ends_with("_global_addr"))
2540 continue;
2541 // __typeid_foo_global_addr -> foo
2542 auto *MD = MDString::get(M.getContext(),
2543 GV.getName().substr(9, GV.getName().size() - 21));
2544 auto MaySimplifyPtr = [&](Value *Ptr) {
2545 if (auto *GV = dyn_cast<GlobalValue>(Ptr))
2546 if (auto *CFIGV = M.getNamedValue((GV->getName() + ".cfi").str()))
2547 Ptr = CFIGV;
2548 return isKnownTypeIdMember(MD, M.getDataLayout(), Ptr, 0);
2549 };
2550 auto MaySimplifyInt = [&](Value *Op) {
2551 auto *PtrAsInt = dyn_cast<ConstantExpr>(Op);
2552 if (!PtrAsInt || PtrAsInt->getOpcode() != Instruction::PtrToInt)
2553 return false;
2554 return MaySimplifyPtr(PtrAsInt->getOperand(0));
2555 };
2556 for (User *U : make_early_inc_range(GV.users())) {
2557 if (auto *CI = dyn_cast<ICmpInst>(U)) {
2558 if (CI->getPredicate() == CmpInst::ICMP_EQ &&
2559 MaySimplifyPtr(CI->getOperand(0))) {
2560 // This is an equality comparison (TypeTestResolution::Single case in
2561 // lowerTypeTestCall). In this case we just replace the comparison
2562 // with true.
2563 CI->replaceAllUsesWith(ConstantInt::getTrue(M.getContext()));
2564 CI->eraseFromParent();
2565 Changed = true;
2566 continue;
2567 }
2568 }
2569 auto *CE = dyn_cast<ConstantExpr>(U);
2570 if (!CE || CE->getOpcode() != Instruction::PtrToInt)
2571 continue;
2572 for (Use &U : make_early_inc_range(CE->uses())) {
2573 auto *CE = dyn_cast<ConstantExpr>(U.getUser());
2574 if (U.getOperandNo() == 0 && CE &&
2575 CE->getOpcode() == Instruction::Sub &&
2576 MaySimplifyInt(CE->getOperand(1))) {
2577 // This is a computation of PtrOffset as generated by
2578 // LowerTypeTestsModule::lowerTypeTestCall above. If
2579 // isKnownTypeIdMember passes we just pretend it evaluated to 0. This
2580 // should cause later passes to remove the range and alignment checks.
2581 // The bitset checks won't be removed but those are uncommon.
2582 CE->replaceAllUsesWith(ConstantInt::get(CE->getType(), 0));
2583 Changed = true;
2584 }
2585 auto *CI = dyn_cast<ICmpInst>(U.getUser());
2586 if (U.getOperandNo() == 1 && CI &&
2587 CI->getPredicate() == CmpInst::ICMP_EQ &&
2588 MaySimplifyInt(CI->getOperand(0))) {
2589 // This is an equality comparison. Unlike in the case above it
2590 // remained as an integer compare.
2591 CI->replaceAllUsesWith(ConstantInt::getTrue(M.getContext()));
2592 CI->eraseFromParent();
2593 Changed = true;
2594 }
2595 }
2596 }
2597 }
2598
2599 if (!Changed)
2600 return PreservedAnalyses::all();
2604 PA.preserve<LoopAnalysis>();
2605 return PA;
2606}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file defines the BumpPtrAllocator interface.
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DXIL Finalize Linkage
dxil translate DXIL Translate Metadata
This file defines the DenseMap class.
Generic implementation of equivalence classes through the use Tarjan's efficient union-find algorithm...
#define DEBUG_TYPE
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
This defines the Use class.
static const unsigned kARMJumpTableEntrySize
static const unsigned kLOONGARCH64JumpTableEntrySize
static bool isKnownTypeIdMember(Metadata *TypeId, const DataLayout &DL, Value *V, uint64_t COffset)
static const unsigned kX86IBTJumpTableEntrySize
static cl::opt< std::string > ClReadSummary("lowertypetests-read-summary", cl::desc("Read summary from given YAML file before running pass"), cl::Hidden)
static const unsigned kRISCVJumpTableEntrySize
static auto buildBitSets(ArrayRef< Metadata * > TypeIds, const DenseMap< GlobalTypeMember *, uint64_t > &GlobalLayout)
static void dropTypeTests(Module &M, Function &TypeTestFunc, bool ShouldDropAll)
static Value * createMaskedBitTest(IRBuilder<> &B, Value *Bits, Value *BitOffset)
Build a test that bit BitOffset mod sizeof(Bits)*8 is set in Bits.
static bool isThumbFunction(Function *F, Triple::ArchType ModuleArch)
static const unsigned kX86JumpTableEntrySize
static cl::opt< bool > AvoidReuse("lowertypetests-avoid-reuse", cl::desc("Try to avoid reuse of byte array addresses using aliases"), cl::Hidden, cl::init(true))
static cl::opt< PassSummaryAction > ClSummaryAction("lowertypetests-summary-action", cl::desc("What to do with the summary when running this pass"), cl::values(clEnumValN(PassSummaryAction::None, "none", "Do nothing"), clEnumValN(PassSummaryAction::Import, "import", "Import typeid resolutions from summary and globals"), clEnumValN(PassSummaryAction::Export, "export", "Export typeid resolutions to summary and globals")), cl::Hidden)
static const unsigned kARMBTIJumpTableEntrySize
static cl::opt< std::string > ClWriteSummary("lowertypetests-write-summary", cl::desc("Write summary to given YAML file after running pass"), cl::Hidden)
static BitSetInfo buildBitSet(ArrayRef< uint64_t > Offsets)
Build a bit set for list of offsets.
static bool isDirectCall(Use &U)
static const unsigned kARMv6MJumpTableEntrySize
static const unsigned kHexagonJumpTableEntrySize
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
Machine Check Debug Module
This file contains the declarations for metadata subclasses.
#define T
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
#define P(N)
FunctionAnalysisManager FAM
This file defines the PointerUnion class, which is a discriminated union of pointer types.
This file contains the declarations for profiling metadata utility functions.
static StringRef getName(Value *V)
This file contains some templates that are useful if you are working with the STL at all.
This file contains library features backported from future STL versions.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
This pass exposes codegen information to IR-level passes.
This header defines support for implementing classes that have some trailing object (or arrays of obj...
Class for arbitrary precision integers.
Definition APInt.h:78
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1563
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:105
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:261
LLVM_ABI BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
Value * getArgOperand(unsigned i) const
unsigned arg_size() const
size_t count(StringRef S) const
@ ICMP_NE
not equal
Definition InstrTypes.h:698
static CondBrInst * Create(Value *Cond, BasicBlock *IfTrue, BasicBlock *IfFalse, InsertPosition InsertBefore=nullptr)
static LLVM_ABI ConstantAggregateZero * get(Type *Ty)
ConstantArray - Constant Array Declarations.
Definition Constants.h:576
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:537
static Constant * get(LLVMContext &Context, ArrayRef< ElementTy > Elts)
get() constructor - Return a constant with array type with an element count and element type matching...
Definition Constants.h:859
static LLVM_ABI Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList)
Create an "inbounds" getelementptr.
Definition Constants.h:1482
static LLVM_ABI Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
static Constant * getPtrAdd(Constant *Ptr, Constant *Offset, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReduced=nullptr)
Create a getelementptr i8, ptr, offset constant expression.
Definition Constants.h:1472
static LLVM_ABI Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getInBoundsPtrAdd(Constant *Ptr, Constant *Offset)
Create a getelementptr inbounds i8, ptr, offset constant expression.
Definition Constants.h:1499
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
static Constant * getAnon(ArrayRef< Constant * > V, bool Packed=false)
Return an anonymous struct that has the specified elements.
Definition Constants.h:629
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
iterator end()
Definition DenseMap.h:81
Analysis pass which computes a DominatorTree.
Definition Dominators.h:278
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition Function.h:168
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition Function.cpp:449
static LLVM_ABI GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition Globals.cpp:621
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
LLVM_ABI void setComdat(Comdat *C)
Definition Globals.cpp:223
const Comdat * getComdat() const
LLVM_ABI bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this GlobalObject.
bool hasSection() const
Check if this global has a custom object file section.
static LLVM_ABI GUID getGUIDAssumingExternalLinkage(StringRef GlobalName)
Return a 64-bit global unique ID constructed from the name of a global symbol.
Definition Globals.cpp:80
bool isDSOLocal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
VisibilityTypes getVisibility() const
static bool isLocalLinkage(LinkageTypes Linkage)
LinkageTypes getLinkage() const
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
bool isDeclarationForLinker() const
PointerType * getType() const
Global values are always pointers.
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition GlobalValue.h:67
@ HiddenVisibility
The GV is hidden.
Definition GlobalValue.h:69
void setVisibility(VisibilityTypes V)
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition GlobalValue.h:52
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition GlobalValue.h:61
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition GlobalValue.h:62
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
LLVM_ABI void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition Globals.cpp:542
MaybeAlign getAlign() const
Returns the alignment of the given variable.
void setConstant(bool Val)
LLVM_ABI void setCodeModel(CodeModel::Model CM)
Change the code model for this global.
Definition Globals.cpp:589
LLVM_ABI void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition Globals.cpp:538
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2822
static LLVM_ABI InlineAsm * get(FunctionType *Ty, StringRef AsmString, StringRef Constraints, bool hasSideEffects, bool isAlignStack=false, AsmDialect asmDialect=AD_ATT, bool canThrow=false)
InlineAsm::get - Return the specified uniqued inline asm string.
Definition InlineAsm.cpp:43
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
uint64_t getBitMask() const
Return a bitmask with ones set for all of the bits that can be set by an unsigned version of this typ...
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:569
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Metadata node.
Definition Metadata.h:1080
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1444
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1572
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1450
Metadata * get() const
Definition Metadata.h:931
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:614
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition MapVector.h:124
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Root of the metadata hierarchy.
Definition Metadata.h:64
TypeIdSummary & getOrInsertTypeIdSummary(StringRef TypeId)
Return an existing or new TypeIdSummary entry for TypeId.
const TypeIdSummary * getTypeIdSummary(StringRef TypeId) const
This returns either a pointer to the type id summary (if present in the summary map) or null (if not ...
CfiFunctionIndex & cfiFunctionDecls()
CfiFunctionIndex & cfiFunctionDefs()
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
iterator_range< op_iterator > operands()
Definition Metadata.h:1856
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Analysis pass which computes a PostDominatorTree.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:730
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:591
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:143
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition StringRef.h:270
Type * getElementType(unsigned N) const
Analysis pass providing the TargetTransformInfo.
See the file comment for details on the usage of the TrailingObjects type.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
@ loongarch64
Definition Triple.h:65
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:286
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_range operands()
Definition User.h:267
Value * getOperand(unsigned i) const
Definition User.h:207
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
user_iterator user_begin()
Definition Value.h:402
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:549
iterator_range< user_iterator > users()
Definition Value.h:426
use_iterator use_begin()
Definition Value.h:364
bool use_empty() const
Definition Value.h:346
LLVM_ABI bool replaceUsesWithIf(Value *New, llvm::function_ref< bool(Use &U)> ShouldReplace)
Go through the uses list for this definition and make each use point to "V" if the callback ShouldRep...
Definition Value.cpp:557
iterator_range< use_iterator > uses()
Definition Value.h:380
bool hasName() const
Definition Value.h:261
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:318
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:399
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:180
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
CallInst * Call
Changed
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
LLVM_ABI Function * getDeclarationIfExists(const Module *M, ID id)
Look up the Function declaration of the intrinsic id in the Module M and return it if it exists.
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
LLVM_ABI bool isJumpTableCanonical(Function *F)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
Definition Metadata.h:683
SmallVector< unsigned char, 0 > ByteArray
Definition PropertySet.h:25
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition FileSystem.h:786
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void ReplaceInstWithInst(BasicBlock *BB, BasicBlock::iterator &BI, Instruction *I)
Replace the instruction specified by BI with the instruction specified by I.
@ Offset
Definition DWP.cpp:532
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
void stable_sort(R &&Range)
Definition STLExtras.h:2116
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
LLVM_ABI void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I, StringRef PassName, const Function *F=nullptr)
Like setExplicitlyUnknownBranchWeights(...), but only sets unknown branch weights in the new instruct...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
FunctionAddr VTableAddr uintptr_t uintptr_t Int32Ty
Definition InstrProf.h:328
@ Export
Export information to summary.
Definition IPO.h:57
@ None
Do nothing.
Definition IPO.h:55
@ Import
Import information from summary.
Definition IPO.h:56
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:634
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
unsigned M1(unsigned Val)
Definition VE.h:377
LLVM_ABI bool convertUsersOfConstantsToInstructions(ArrayRef< Constant * > Consts, Function *RestrictToFunc=nullptr, bool RemoveDeadConstants=true, bool IncludeSelf=false)
Replace constant expressions users of the given constants with instructions.
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
TargetTransformInfo TTI
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
LLVM_ABI void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.
DWARFExpression::Operation Op
Expected< T > errorOrToExpected(ErrorOr< T > &&EO)
Convert an ErrorOr<T> to an Expected<T>.
Definition Error.h:1261
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1885
constexpr unsigned BitWidth
LLVM_ABI void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition Error.cpp:107
LLVM_ABI Instruction * SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
LLVM_ABI void appendToUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.used list.
CfiFunctionLinkage
The type of CFI jumptable needed for a function.
@ CFL_WeakDeclaration
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition MathExtras.h:373
LLVM_ABI GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition Module.cpp:875
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:70
TypeTestResolution TTRes
Kind
Specifies which kind of type check we should emit for this byte array.
@ Unknown
Unknown (analysis not performed, don't lower)
@ Single
Single element (last example in "Short Inline Bit Vectors")
@ Inline
Inlined bit vector ("Short Inline Bit Vectors")
@ Unsat
Unsatisfiable type (i.e. no global has this type metadata)
@ AllOnes
All-ones bit vector ("Eliminating Bit Vector Checks for All-Ones Bit Vectors")
@ ByteArray
Test a byte array (first example)
unsigned SizeM1BitWidth
Range of size-1 expressed as a bit width.
enum llvm::TypeTestResolution::Kind TheKind
SmallVector< uint64_t, 16 > Offsets
LLVM_ABI bool containsGlobalOffset(uint64_t Offset) const
LLVM_ABI void print(raw_ostream &OS) const
This class is used to build a byte array containing overlapping bit sets.
uint64_t BitAllocs[BitsPerByte]
The number of bytes allocated so far for each of the bits.
std::vector< uint8_t > Bytes
The byte array built so far.
LLVM_ABI void allocate(const std::set< uint64_t > &Bits, uint64_t BitSize, uint64_t &AllocByteOffset, uint8_t &AllocMask)
Allocate BitSize bits in the byte array where Bits contains the bits to set.
This class implements a layout algorithm for globals referenced by bit sets that tries to keep member...
std::vector< std::vector< uint64_t > > Fragments
The computed layout.
LLVM_ABI void addFragment(const std::set< uint64_t > &F)
Add F to the layout while trying to keep its indices contiguous.
std::vector< uint64_t > FragmentMap
Mapping from object index to fragment index.