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