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