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