1 //===-- Verifier.cpp - Implement the Module Verifier -----------------------==//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the function verifier interface, that can be used for some
10 // basic correctness checking of input to the system.
12 // Note that this does not provide full `Java style' security and verifications,
13 // instead it just tries to ensure that code is well-formed.
15 // * Both of a binary operator's parameters are of the same type
16 // * Verify that the indices of mem access instructions match other operands
17 // * Verify that arithmetic and other things are only performed on first-class
18 // types. Verify that shifts & logicals only happen on integrals f.e.
19 // * All of the constants in a switch statement are of the correct type
20 // * The code is in valid SSA form
21 // * It should be illegal to put a label into any other type (like a structure)
22 // or to return one. [except constant arrays!]
23 // * Only phi nodes can be self referential: 'add i32 %0, %0 ; <int>:0' is bad
24 // * PHI nodes must have an entry for each predecessor, with no extras.
25 // * PHI nodes must be the first thing in a basic block, all grouped together
26 // * All basic blocks should only end with terminator insts, not contain them
27 // * The entry node to a function must not have predecessors
28 // * All Instructions must be embedded into a basic block
29 // * Functions cannot take a void-typed parameter
30 // * Verify that a function's argument list agrees with it's declared type.
31 // * It is illegal to specify a name for a void value.
32 // * It is illegal to have a internal global value with no initializer
33 // * It is illegal to have a ret instruction that returns a value that does not
34 // agree with the function return value type.
35 // * Function call argument types match the function prototype
36 // * A landing pad is defined by a landingpad instruction, and can be jumped to
37 // only by the unwind edge of an invoke instruction.
38 // * A landingpad instruction must be the first non-PHI instruction in the
40 // * Landingpad instructions must be in a function with a personality function.
41 // * Convergence control intrinsics are introduced in ConvergentOperations.rst.
42 // The applied restrictions are too numerous to list here.
43 // * The convergence entry intrinsic and the loop heart must be the first
44 // non-PHI instruction in their respective block. This does not conflict with
45 // the landing pads, since these two kinds cannot occur in the same block.
46 // * All other things that are tested by asserts spread about the code...
48 //===----------------------------------------------------------------------===//
50 #include "llvm/IR/Verifier.h"
51 #include "llvm/ADT/APFloat.h"
52 #include "llvm/ADT/APInt.h"
53 #include "llvm/ADT/ArrayRef.h"
54 #include "llvm/ADT/DenseMap.h"
55 #include "llvm/ADT/MapVector.h"
56 #include "llvm/ADT/STLExtras.h"
57 #include "llvm/ADT/SmallPtrSet.h"
58 #include "llvm/ADT/SmallSet.h"
59 #include "llvm/ADT/SmallVector.h"
60 #include "llvm/ADT/StringExtras.h"
61 #include "llvm/ADT/StringRef.h"
62 #include "llvm/ADT/Twine.h"
63 #include "llvm/BinaryFormat/Dwarf.h"
64 #include "llvm/IR/Argument.h"
65 #include "llvm/IR/AttributeMask.h"
66 #include "llvm/IR/Attributes.h"
67 #include "llvm/IR/BasicBlock.h"
68 #include "llvm/IR/CFG.h"
69 #include "llvm/IR/CallingConv.h"
70 #include "llvm/IR/Comdat.h"
71 #include "llvm/IR/Constant.h"
72 #include "llvm/IR/ConstantRange.h"
73 #include "llvm/IR/ConstantRangeList.h"
74 #include "llvm/IR/Constants.h"
75 #include "llvm/IR/ConvergenceVerifier.h"
76 #include "llvm/IR/DataLayout.h"
77 #include "llvm/IR/DebugInfo.h"
78 #include "llvm/IR/DebugInfoMetadata.h"
79 #include "llvm/IR/DebugLoc.h"
80 #include "llvm/IR/DerivedTypes.h"
81 #include "llvm/IR/Dominators.h"
82 #include "llvm/IR/EHPersonalities.h"
83 #include "llvm/IR/Function.h"
84 #include "llvm/IR/GCStrategy.h"
85 #include "llvm/IR/GlobalAlias.h"
86 #include "llvm/IR/GlobalValue.h"
87 #include "llvm/IR/GlobalVariable.h"
88 #include "llvm/IR/InlineAsm.h"
89 #include "llvm/IR/InstVisitor.h"
90 #include "llvm/IR/InstrTypes.h"
91 #include "llvm/IR/Instruction.h"
92 #include "llvm/IR/Instructions.h"
93 #include "llvm/IR/IntrinsicInst.h"
94 #include "llvm/IR/Intrinsics.h"
95 #include "llvm/IR/IntrinsicsAArch64.h"
96 #include "llvm/IR/IntrinsicsAMDGPU.h"
97 #include "llvm/IR/IntrinsicsARM.h"
98 #include "llvm/IR/IntrinsicsNVPTX.h"
99 #include "llvm/IR/IntrinsicsWebAssembly.h"
100 #include "llvm/IR/LLVMContext.h"
101 #include "llvm/IR/MemoryModelRelaxationAnnotations.h"
102 #include "llvm/IR/Metadata.h"
103 #include "llvm/IR/Module.h"
104 #include "llvm/IR/ModuleSlotTracker.h"
105 #include "llvm/IR/PassManager.h"
106 #include "llvm/IR/ProfDataUtils.h"
107 #include "llvm/IR/Statepoint.h"
108 #include "llvm/IR/Type.h"
109 #include "llvm/IR/Use.h"
110 #include "llvm/IR/User.h"
111 #include "llvm/IR/VFABIDemangler.h"
112 #include "llvm/IR/Value.h"
113 #include "llvm/InitializePasses.h"
114 #include "llvm/Pass.h"
115 #include "llvm/Support/AMDGPUAddrSpace.h"
116 #include "llvm/Support/AtomicOrdering.h"
117 #include "llvm/Support/Casting.h"
118 #include "llvm/Support/CommandLine.h"
119 #include "llvm/Support/ErrorHandling.h"
120 #include "llvm/Support/MathExtras.h"
121 #include "llvm/Support/ModRef.h"
122 #include "llvm/Support/raw_ostream.h"
131 using namespace llvm
;
133 static cl::opt
<bool> VerifyNoAliasScopeDomination(
134 "verify-noalias-scope-decl-dom", cl::Hidden
, cl::init(false),
135 cl::desc("Ensure that llvm.experimental.noalias.scope.decl for identical "
136 "scopes are not dominating"));
140 struct VerifierSupport
{
143 ModuleSlotTracker MST
;
145 const DataLayout
&DL
;
146 LLVMContext
&Context
;
148 /// Track the brokenness of the module while recursively visiting.
150 /// Broken debug info can be "recovered" from by stripping the debug info.
151 bool BrokenDebugInfo
= false;
152 /// Whether to treat broken debug info as an error.
153 bool TreatBrokenDebugInfoAsError
= true;
155 explicit VerifierSupport(raw_ostream
*OS
, const Module
&M
)
156 : OS(OS
), M(M
), MST(&M
), TT(Triple::normalize(M
.getTargetTriple())),
157 DL(M
.getDataLayout()), Context(M
.getContext()) {}
160 void Write(const Module
*M
) {
161 *OS
<< "; ModuleID = '" << M
->getModuleIdentifier() << "'\n";
164 void Write(const Value
*V
) {
169 void Write(const Value
&V
) {
170 if (isa
<Instruction
>(V
)) {
174 V
.printAsOperand(*OS
, true, MST
);
179 void Write(const DbgRecord
*DR
) {
181 DR
->print(*OS
, MST
, false);
186 void Write(DbgVariableRecord::LocationType Type
) {
188 case DbgVariableRecord::LocationType::Value
:
191 case DbgVariableRecord::LocationType::Declare
:
194 case DbgVariableRecord::LocationType::Assign
:
197 case DbgVariableRecord::LocationType::End
:
200 case DbgVariableRecord::LocationType::Any
:
206 void Write(const Metadata
*MD
) {
209 MD
->print(*OS
, MST
, &M
);
213 template <class T
> void Write(const MDTupleTypedArrayWrapper
<T
> &MD
) {
217 void Write(const NamedMDNode
*NMD
) {
220 NMD
->print(*OS
, MST
);
224 void Write(Type
*T
) {
230 void Write(const Comdat
*C
) {
236 void Write(const APInt
*AI
) {
242 void Write(const unsigned i
) { *OS
<< i
<< '\n'; }
244 // NOLINTNEXTLINE(readability-identifier-naming)
245 void Write(const Attribute
*A
) {
248 *OS
<< A
->getAsString() << '\n';
251 // NOLINTNEXTLINE(readability-identifier-naming)
252 void Write(const AttributeSet
*AS
) {
255 *OS
<< AS
->getAsString() << '\n';
258 // NOLINTNEXTLINE(readability-identifier-naming)
259 void Write(const AttributeList
*AL
) {
265 void Write(Printable P
) { *OS
<< P
<< '\n'; }
267 template <typename T
> void Write(ArrayRef
<T
> Vs
) {
268 for (const T
&V
: Vs
)
272 template <typename T1
, typename
... Ts
>
273 void WriteTs(const T1
&V1
, const Ts
&... Vs
) {
278 template <typename
... Ts
> void WriteTs() {}
281 /// A check failed, so printout out the condition and the message.
283 /// This provides a nice place to put a breakpoint if you want to see why
284 /// something is not correct.
285 void CheckFailed(const Twine
&Message
) {
287 *OS
<< Message
<< '\n';
291 /// A check failed (with values to print).
293 /// This calls the Message-only version so that the above is easier to set a
295 template <typename T1
, typename
... Ts
>
296 void CheckFailed(const Twine
&Message
, const T1
&V1
, const Ts
&... Vs
) {
297 CheckFailed(Message
);
302 /// A debug info check failed.
303 void DebugInfoCheckFailed(const Twine
&Message
) {
305 *OS
<< Message
<< '\n';
306 Broken
|= TreatBrokenDebugInfoAsError
;
307 BrokenDebugInfo
= true;
310 /// A debug info check failed (with values to print).
311 template <typename T1
, typename
... Ts
>
312 void DebugInfoCheckFailed(const Twine
&Message
, const T1
&V1
,
314 DebugInfoCheckFailed(Message
);
324 class Verifier
: public InstVisitor
<Verifier
>, VerifierSupport
{
325 friend class InstVisitor
<Verifier
>;
328 /// When verifying a basic block, keep track of all of the
329 /// instructions we have seen so far.
331 /// This allows us to do efficient dominance checks for the case when an
332 /// instruction has an operand that is an instruction in the same block.
333 SmallPtrSet
<Instruction
*, 16> InstsInThisBlock
;
335 /// Keep track of the metadata nodes that have been checked already.
336 SmallPtrSet
<const Metadata
*, 32> MDNodes
;
338 /// Keep track which DISubprogram is attached to which function.
339 DenseMap
<const DISubprogram
*, const Function
*> DISubprogramAttachments
;
341 /// Track all DICompileUnits visited.
342 SmallPtrSet
<const Metadata
*, 2> CUVisited
;
344 /// The result type for a landingpad.
345 Type
*LandingPadResultTy
;
347 /// Whether we've seen a call to @llvm.localescape in this function
351 /// Whether the current function has a DISubprogram attached to it.
352 bool HasDebugInfo
= false;
354 /// Stores the count of how many objects were passed to llvm.localescape for a
355 /// given function and the largest index passed to llvm.localrecover.
356 DenseMap
<Function
*, std::pair
<unsigned, unsigned>> FrameEscapeInfo
;
358 // Maps catchswitches and cleanuppads that unwind to siblings to the
359 // terminators that indicate the unwind, used to detect cycles therein.
360 MapVector
<Instruction
*, Instruction
*> SiblingFuncletInfo
;
362 /// Cache which blocks are in which funclet, if an EH funclet personality is
363 /// in use. Otherwise empty.
364 DenseMap
<BasicBlock
*, ColorVector
> BlockEHFuncletColors
;
366 /// Cache of constants visited in search of ConstantExprs.
367 SmallPtrSet
<const Constant
*, 32> ConstantExprVisited
;
369 /// Cache of declarations of the llvm.experimental.deoptimize.<ty> intrinsic.
370 SmallVector
<const Function
*, 4> DeoptimizeDeclarations
;
372 /// Cache of attribute lists verified.
373 SmallPtrSet
<const void *, 32> AttributeListsVisited
;
375 // Verify that this GlobalValue is only used in this module.
376 // This map is used to avoid visiting uses twice. We can arrive at a user
377 // twice, if they have multiple operands. In particular for very large
378 // constant expressions, we can arrive at a particular user many times.
379 SmallPtrSet
<const Value
*, 32> GlobalValueVisited
;
381 // Keeps track of duplicate function argument debug info.
382 SmallVector
<const DILocalVariable
*, 16> DebugFnArgs
;
384 TBAAVerifier TBAAVerifyHelper
;
385 ConvergenceVerifier ConvergenceVerifyHelper
;
387 SmallVector
<IntrinsicInst
*, 4> NoAliasScopeDecls
;
389 void checkAtomicMemAccessSize(Type
*Ty
, const Instruction
*I
);
392 explicit Verifier(raw_ostream
*OS
, bool ShouldTreatBrokenDebugInfoAsError
,
394 : VerifierSupport(OS
, M
), LandingPadResultTy(nullptr),
395 SawFrameEscape(false), TBAAVerifyHelper(this) {
396 TreatBrokenDebugInfoAsError
= ShouldTreatBrokenDebugInfoAsError
;
399 bool hasBrokenDebugInfo() const { return BrokenDebugInfo
; }
401 bool verify(const Function
&F
) {
402 assert(F
.getParent() == &M
&&
403 "An instance of this class only works with a specific module!");
405 // First ensure the function is well-enough formed to compute dominance
406 // information, and directly compute a dominance tree. We don't rely on the
407 // pass manager to provide this as it isolates us from a potentially
408 // out-of-date dominator tree and makes it significantly more complex to run
409 // this code outside of a pass manager.
410 // FIXME: It's really gross that we have to cast away constness here.
412 DT
.recalculate(const_cast<Function
&>(F
));
414 for (const BasicBlock
&BB
: F
) {
415 if (!BB
.empty() && BB
.back().isTerminator())
419 *OS
<< "Basic Block in function '" << F
.getName()
420 << "' does not have terminator!\n";
421 BB
.printAsOperand(*OS
, true, MST
);
427 auto FailureCB
= [this](const Twine
&Message
) {
428 this->CheckFailed(Message
);
430 ConvergenceVerifyHelper
.initialize(OS
, FailureCB
, F
);
433 // FIXME: We strip const here because the inst visitor strips const.
434 visit(const_cast<Function
&>(F
));
435 verifySiblingFuncletUnwinds();
437 if (ConvergenceVerifyHelper
.sawTokens())
438 ConvergenceVerifyHelper
.verify(DT
);
440 InstsInThisBlock
.clear();
442 LandingPadResultTy
= nullptr;
443 SawFrameEscape
= false;
444 SiblingFuncletInfo
.clear();
445 verifyNoAliasScopeDecl();
446 NoAliasScopeDecls
.clear();
451 /// Verify the module that this instance of \c Verifier was initialized with.
455 // Collect all declarations of the llvm.experimental.deoptimize intrinsic.
456 for (const Function
&F
: M
)
457 if (F
.getIntrinsicID() == Intrinsic::experimental_deoptimize
)
458 DeoptimizeDeclarations
.push_back(&F
);
460 // Now that we've visited every function, verify that we never asked to
461 // recover a frame index that wasn't escaped.
462 verifyFrameRecoverIndices();
463 for (const GlobalVariable
&GV
: M
.globals())
464 visitGlobalVariable(GV
);
466 for (const GlobalAlias
&GA
: M
.aliases())
467 visitGlobalAlias(GA
);
469 for (const GlobalIFunc
&GI
: M
.ifuncs())
470 visitGlobalIFunc(GI
);
472 for (const NamedMDNode
&NMD
: M
.named_metadata())
473 visitNamedMDNode(NMD
);
475 for (const StringMapEntry
<Comdat
> &SMEC
: M
.getComdatSymbolTable())
476 visitComdat(SMEC
.getValue());
480 visitModuleCommandLines();
482 verifyCompileUnits();
484 verifyDeoptimizeCallingConvs();
485 DISubprogramAttachments
.clear();
490 /// Whether a metadata node is allowed to be, or contain, a DILocation.
491 enum class AreDebugLocsAllowed
{ No
, Yes
};
493 /// Metadata that should be treated as a range, with slightly different
495 enum class RangeLikeMetadataKind
{
497 AbsoluteSymbol
, // MD_absolute_symbol
498 NoaliasAddrspace
// MD_noalias_addrspace
501 // Verification methods...
502 void visitGlobalValue(const GlobalValue
&GV
);
503 void visitGlobalVariable(const GlobalVariable
&GV
);
504 void visitGlobalAlias(const GlobalAlias
&GA
);
505 void visitGlobalIFunc(const GlobalIFunc
&GI
);
506 void visitAliaseeSubExpr(const GlobalAlias
&A
, const Constant
&C
);
507 void visitAliaseeSubExpr(SmallPtrSetImpl
<const GlobalAlias
*> &Visited
,
508 const GlobalAlias
&A
, const Constant
&C
);
509 void visitNamedMDNode(const NamedMDNode
&NMD
);
510 void visitMDNode(const MDNode
&MD
, AreDebugLocsAllowed AllowLocs
);
511 void visitMetadataAsValue(const MetadataAsValue
&MD
, Function
*F
);
512 void visitValueAsMetadata(const ValueAsMetadata
&MD
, Function
*F
);
513 void visitDIArgList(const DIArgList
&AL
, Function
*F
);
514 void visitComdat(const Comdat
&C
);
515 void visitModuleIdents();
516 void visitModuleCommandLines();
517 void visitModuleFlags();
518 void visitModuleFlag(const MDNode
*Op
,
519 DenseMap
<const MDString
*, const MDNode
*> &SeenIDs
,
520 SmallVectorImpl
<const MDNode
*> &Requirements
);
521 void visitModuleFlagCGProfileEntry(const MDOperand
&MDO
);
522 void visitFunction(const Function
&F
);
523 void visitBasicBlock(BasicBlock
&BB
);
524 void verifyRangeLikeMetadata(const Value
&V
, const MDNode
*Range
, Type
*Ty
,
525 RangeLikeMetadataKind Kind
);
526 void visitRangeMetadata(Instruction
&I
, MDNode
*Range
, Type
*Ty
);
527 void visitNoaliasAddrspaceMetadata(Instruction
&I
, MDNode
*Range
, Type
*Ty
);
528 void visitDereferenceableMetadata(Instruction
&I
, MDNode
*MD
);
529 void visitProfMetadata(Instruction
&I
, MDNode
*MD
);
530 void visitCallStackMetadata(MDNode
*MD
);
531 void visitMemProfMetadata(Instruction
&I
, MDNode
*MD
);
532 void visitCallsiteMetadata(Instruction
&I
, MDNode
*MD
);
533 void visitDIAssignIDMetadata(Instruction
&I
, MDNode
*MD
);
534 void visitMMRAMetadata(Instruction
&I
, MDNode
*MD
);
535 void visitAnnotationMetadata(MDNode
*Annotation
);
536 void visitAliasScopeMetadata(const MDNode
*MD
);
537 void visitAliasScopeListMetadata(const MDNode
*MD
);
538 void visitAccessGroupMetadata(const MDNode
*MD
);
540 template <class Ty
> bool isValidMetadataArray(const MDTuple
&N
);
541 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N);
542 #include "llvm/IR/Metadata.def"
543 void visitDIScope(const DIScope
&N
);
544 void visitDIVariable(const DIVariable
&N
);
545 void visitDILexicalBlockBase(const DILexicalBlockBase
&N
);
546 void visitDITemplateParameter(const DITemplateParameter
&N
);
548 void visitTemplateParams(const MDNode
&N
, const Metadata
&RawParams
);
550 void visit(DbgLabelRecord
&DLR
);
551 void visit(DbgVariableRecord
&DVR
);
552 // InstVisitor overrides...
553 using InstVisitor
<Verifier
>::visit
;
554 void visitDbgRecords(Instruction
&I
);
555 void visit(Instruction
&I
);
557 void visitTruncInst(TruncInst
&I
);
558 void visitZExtInst(ZExtInst
&I
);
559 void visitSExtInst(SExtInst
&I
);
560 void visitFPTruncInst(FPTruncInst
&I
);
561 void visitFPExtInst(FPExtInst
&I
);
562 void visitFPToUIInst(FPToUIInst
&I
);
563 void visitFPToSIInst(FPToSIInst
&I
);
564 void visitUIToFPInst(UIToFPInst
&I
);
565 void visitSIToFPInst(SIToFPInst
&I
);
566 void visitIntToPtrInst(IntToPtrInst
&I
);
567 void visitPtrToIntInst(PtrToIntInst
&I
);
568 void visitBitCastInst(BitCastInst
&I
);
569 void visitAddrSpaceCastInst(AddrSpaceCastInst
&I
);
570 void visitPHINode(PHINode
&PN
);
571 void visitCallBase(CallBase
&Call
);
572 void visitUnaryOperator(UnaryOperator
&U
);
573 void visitBinaryOperator(BinaryOperator
&B
);
574 void visitICmpInst(ICmpInst
&IC
);
575 void visitFCmpInst(FCmpInst
&FC
);
576 void visitExtractElementInst(ExtractElementInst
&EI
);
577 void visitInsertElementInst(InsertElementInst
&EI
);
578 void visitShuffleVectorInst(ShuffleVectorInst
&EI
);
579 void visitVAArgInst(VAArgInst
&VAA
) { visitInstruction(VAA
); }
580 void visitCallInst(CallInst
&CI
);
581 void visitInvokeInst(InvokeInst
&II
);
582 void visitGetElementPtrInst(GetElementPtrInst
&GEP
);
583 void visitLoadInst(LoadInst
&LI
);
584 void visitStoreInst(StoreInst
&SI
);
585 void verifyDominatesUse(Instruction
&I
, unsigned i
);
586 void visitInstruction(Instruction
&I
);
587 void visitTerminator(Instruction
&I
);
588 void visitBranchInst(BranchInst
&BI
);
589 void visitReturnInst(ReturnInst
&RI
);
590 void visitSwitchInst(SwitchInst
&SI
);
591 void visitIndirectBrInst(IndirectBrInst
&BI
);
592 void visitCallBrInst(CallBrInst
&CBI
);
593 void visitSelectInst(SelectInst
&SI
);
594 void visitUserOp1(Instruction
&I
);
595 void visitUserOp2(Instruction
&I
) { visitUserOp1(I
); }
596 void visitIntrinsicCall(Intrinsic::ID ID
, CallBase
&Call
);
597 void visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic
&FPI
);
598 void visitVPIntrinsic(VPIntrinsic
&VPI
);
599 void visitDbgIntrinsic(StringRef Kind
, DbgVariableIntrinsic
&DII
);
600 void visitDbgLabelIntrinsic(StringRef Kind
, DbgLabelInst
&DLI
);
601 void visitAtomicCmpXchgInst(AtomicCmpXchgInst
&CXI
);
602 void visitAtomicRMWInst(AtomicRMWInst
&RMWI
);
603 void visitFenceInst(FenceInst
&FI
);
604 void visitAllocaInst(AllocaInst
&AI
);
605 void visitExtractValueInst(ExtractValueInst
&EVI
);
606 void visitInsertValueInst(InsertValueInst
&IVI
);
607 void visitEHPadPredecessors(Instruction
&I
);
608 void visitLandingPadInst(LandingPadInst
&LPI
);
609 void visitResumeInst(ResumeInst
&RI
);
610 void visitCatchPadInst(CatchPadInst
&CPI
);
611 void visitCatchReturnInst(CatchReturnInst
&CatchReturn
);
612 void visitCleanupPadInst(CleanupPadInst
&CPI
);
613 void visitFuncletPadInst(FuncletPadInst
&FPI
);
614 void visitCatchSwitchInst(CatchSwitchInst
&CatchSwitch
);
615 void visitCleanupReturnInst(CleanupReturnInst
&CRI
);
617 void verifySwiftErrorCall(CallBase
&Call
, const Value
*SwiftErrorVal
);
618 void verifySwiftErrorValue(const Value
*SwiftErrorVal
);
619 void verifyTailCCMustTailAttrs(const AttrBuilder
&Attrs
, StringRef Context
);
620 void verifyMustTailCall(CallInst
&CI
);
621 bool verifyAttributeCount(AttributeList Attrs
, unsigned Params
);
622 void verifyAttributeTypes(AttributeSet Attrs
, const Value
*V
);
623 void verifyParameterAttrs(AttributeSet Attrs
, Type
*Ty
, const Value
*V
);
624 void checkUnsignedBaseTenFuncAttr(AttributeList Attrs
, StringRef Attr
,
626 void verifyFunctionAttrs(FunctionType
*FT
, AttributeList Attrs
,
627 const Value
*V
, bool IsIntrinsic
, bool IsInlineAsm
);
628 void verifyFunctionMetadata(ArrayRef
<std::pair
<unsigned, MDNode
*>> MDs
);
630 void visitConstantExprsRecursively(const Constant
*EntryC
);
631 void visitConstantExpr(const ConstantExpr
*CE
);
632 void visitConstantPtrAuth(const ConstantPtrAuth
*CPA
);
633 void verifyInlineAsmCall(const CallBase
&Call
);
634 void verifyStatepoint(const CallBase
&Call
);
635 void verifyFrameRecoverIndices();
636 void verifySiblingFuncletUnwinds();
638 void verifyFragmentExpression(const DbgVariableIntrinsic
&I
);
639 void verifyFragmentExpression(const DbgVariableRecord
&I
);
640 template <typename ValueOrMetadata
>
641 void verifyFragmentExpression(const DIVariable
&V
,
642 DIExpression::FragmentInfo Fragment
,
643 ValueOrMetadata
*Desc
);
644 void verifyFnArgs(const DbgVariableIntrinsic
&I
);
645 void verifyFnArgs(const DbgVariableRecord
&DVR
);
646 void verifyNotEntryValue(const DbgVariableIntrinsic
&I
);
647 void verifyNotEntryValue(const DbgVariableRecord
&I
);
649 /// Module-level debug info verification...
650 void verifyCompileUnits();
652 /// Module-level verification that all @llvm.experimental.deoptimize
653 /// declarations share the same calling convention.
654 void verifyDeoptimizeCallingConvs();
656 void verifyAttachedCallBundle(const CallBase
&Call
,
657 const OperandBundleUse
&BU
);
659 /// Verify the llvm.experimental.noalias.scope.decl declarations
660 void verifyNoAliasScopeDecl();
663 } // end anonymous namespace
665 /// We know that cond should be true, if not print an error message.
666 #define Check(C, ...) \
669 CheckFailed(__VA_ARGS__); \
674 /// We know that a debug info condition should be true, if not print
675 /// an error message.
676 #define CheckDI(C, ...) \
679 DebugInfoCheckFailed(__VA_ARGS__); \
684 void Verifier::visitDbgRecords(Instruction
&I
) {
687 CheckDI(I
.DebugMarker
->MarkedInstr
== &I
,
688 "Instruction has invalid DebugMarker", &I
);
689 CheckDI(!isa
<PHINode
>(&I
) || !I
.hasDbgRecords(),
690 "PHI Node must not have any attached DbgRecords", &I
);
691 for (DbgRecord
&DR
: I
.getDbgRecordRange()) {
692 CheckDI(DR
.getMarker() == I
.DebugMarker
,
693 "DbgRecord had invalid DebugMarker", &I
, &DR
);
695 dyn_cast_or_null
<DILocation
>(DR
.getDebugLoc().getAsMDNode()))
696 visitMDNode(*Loc
, AreDebugLocsAllowed::Yes
);
697 if (auto *DVR
= dyn_cast
<DbgVariableRecord
>(&DR
)) {
699 // These have to appear after `visit` for consistency with existing
700 // intrinsic behaviour.
701 verifyFragmentExpression(*DVR
);
702 verifyNotEntryValue(*DVR
);
703 } else if (auto *DLR
= dyn_cast
<DbgLabelRecord
>(&DR
)) {
709 void Verifier::visit(Instruction
&I
) {
711 for (unsigned i
= 0, e
= I
.getNumOperands(); i
!= e
; ++i
)
712 Check(I
.getOperand(i
) != nullptr, "Operand is null", &I
);
713 InstVisitor
<Verifier
>::visit(I
);
716 // Helper to iterate over indirect users. By returning false, the callback can ask to stop traversing further.
717 static void forEachUser(const Value
*User
,
718 SmallPtrSet
<const Value
*, 32> &Visited
,
719 llvm::function_ref
<bool(const Value
*)> Callback
) {
720 if (!Visited
.insert(User
).second
)
723 SmallVector
<const Value
*> WorkList
;
724 append_range(WorkList
, User
->materialized_users());
725 while (!WorkList
.empty()) {
726 const Value
*Cur
= WorkList
.pop_back_val();
727 if (!Visited
.insert(Cur
).second
)
730 append_range(WorkList
, Cur
->materialized_users());
734 void Verifier::visitGlobalValue(const GlobalValue
&GV
) {
735 Check(!GV
.isDeclaration() || GV
.hasValidDeclarationLinkage(),
736 "Global is external, but doesn't have external or weak linkage!", &GV
);
738 if (const GlobalObject
*GO
= dyn_cast
<GlobalObject
>(&GV
)) {
740 if (MaybeAlign A
= GO
->getAlign()) {
741 Check(A
->value() <= Value::MaximumAlignment
,
742 "huge alignment values are unsupported", GO
);
745 if (const MDNode
*Associated
=
746 GO
->getMetadata(LLVMContext::MD_associated
)) {
747 Check(Associated
->getNumOperands() == 1,
748 "associated metadata must have one operand", &GV
, Associated
);
749 const Metadata
*Op
= Associated
->getOperand(0).get();
750 Check(Op
, "associated metadata must have a global value", GO
, Associated
);
752 const auto *VM
= dyn_cast_or_null
<ValueAsMetadata
>(Op
);
753 Check(VM
, "associated metadata must be ValueAsMetadata", GO
, Associated
);
755 Check(isa
<PointerType
>(VM
->getValue()->getType()),
756 "associated value must be pointer typed", GV
, Associated
);
758 const Value
*Stripped
= VM
->getValue()->stripPointerCastsAndAliases();
759 Check(isa
<GlobalObject
>(Stripped
) || isa
<Constant
>(Stripped
),
760 "associated metadata must point to a GlobalObject", GO
, Stripped
);
761 Check(Stripped
!= GO
,
762 "global values should not associate to themselves", GO
,
767 // FIXME: Why is getMetadata on GlobalValue protected?
768 if (const MDNode
*AbsoluteSymbol
=
769 GO
->getMetadata(LLVMContext::MD_absolute_symbol
)) {
770 verifyRangeLikeMetadata(*GO
, AbsoluteSymbol
,
771 DL
.getIntPtrType(GO
->getType()),
772 RangeLikeMetadataKind::AbsoluteSymbol
);
776 Check(!GV
.hasAppendingLinkage() || isa
<GlobalVariable
>(GV
),
777 "Only global variables can have appending linkage!", &GV
);
779 if (GV
.hasAppendingLinkage()) {
780 const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(&GV
);
781 Check(GVar
&& GVar
->getValueType()->isArrayTy(),
782 "Only global arrays can have appending linkage!", GVar
);
785 if (GV
.isDeclarationForLinker())
786 Check(!GV
.hasComdat(), "Declaration may not be in a Comdat!", &GV
);
788 if (GV
.hasDLLExportStorageClass()) {
789 Check(!GV
.hasHiddenVisibility(),
790 "dllexport GlobalValue must have default or protected visibility",
793 if (GV
.hasDLLImportStorageClass()) {
794 Check(GV
.hasDefaultVisibility(),
795 "dllimport GlobalValue must have default visibility", &GV
);
796 Check(!GV
.isDSOLocal(), "GlobalValue with DLLImport Storage is dso_local!",
799 Check((GV
.isDeclaration() &&
800 (GV
.hasExternalLinkage() || GV
.hasExternalWeakLinkage())) ||
801 GV
.hasAvailableExternallyLinkage(),
802 "Global is marked as dllimport, but not external", &GV
);
805 if (GV
.isImplicitDSOLocal())
806 Check(GV
.isDSOLocal(),
807 "GlobalValue with local linkage or non-default "
808 "visibility must be dso_local!",
812 Check(!GV
.hasSection(), "tagged GlobalValue must not be in section.", &GV
);
815 forEachUser(&GV
, GlobalValueVisited
, [&](const Value
*V
) -> bool {
816 if (const Instruction
*I
= dyn_cast
<Instruction
>(V
)) {
817 if (!I
->getParent() || !I
->getParent()->getParent())
818 CheckFailed("Global is referenced by parentless instruction!", &GV
, &M
,
820 else if (I
->getParent()->getParent()->getParent() != &M
)
821 CheckFailed("Global is referenced in a different module!", &GV
, &M
, I
,
822 I
->getParent()->getParent(),
823 I
->getParent()->getParent()->getParent());
825 } else if (const Function
*F
= dyn_cast
<Function
>(V
)) {
826 if (F
->getParent() != &M
)
827 CheckFailed("Global is used by function in a different module", &GV
, &M
,
835 void Verifier::visitGlobalVariable(const GlobalVariable
&GV
) {
836 Type
*GVType
= GV
.getValueType();
838 if (GV
.hasInitializer()) {
839 Check(GV
.getInitializer()->getType() == GVType
,
840 "Global variable initializer type does not match global "
843 // If the global has common linkage, it must have a zero initializer and
844 // cannot be constant.
845 if (GV
.hasCommonLinkage()) {
846 Check(GV
.getInitializer()->isNullValue(),
847 "'common' global must have a zero initializer!", &GV
);
848 Check(!GV
.isConstant(), "'common' global may not be marked constant!",
850 Check(!GV
.hasComdat(), "'common' global may not be in a Comdat!", &GV
);
854 if (GV
.hasName() && (GV
.getName() == "llvm.global_ctors" ||
855 GV
.getName() == "llvm.global_dtors")) {
856 Check(!GV
.hasInitializer() || GV
.hasAppendingLinkage(),
857 "invalid linkage for intrinsic global variable", &GV
);
858 Check(GV
.materialized_use_empty(),
859 "invalid uses of intrinsic global variable", &GV
);
861 // Don't worry about emitting an error for it not being an array,
862 // visitGlobalValue will complain on appending non-array.
863 if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(GVType
)) {
864 StructType
*STy
= dyn_cast
<StructType
>(ATy
->getElementType());
865 PointerType
*FuncPtrTy
=
866 PointerType::get(Context
, DL
.getProgramAddressSpace());
867 Check(STy
&& (STy
->getNumElements() == 2 || STy
->getNumElements() == 3) &&
868 STy
->getTypeAtIndex(0u)->isIntegerTy(32) &&
869 STy
->getTypeAtIndex(1) == FuncPtrTy
,
870 "wrong type for intrinsic global variable", &GV
);
871 Check(STy
->getNumElements() == 3,
872 "the third field of the element type is mandatory, "
873 "specify ptr null to migrate from the obsoleted 2-field form");
874 Type
*ETy
= STy
->getTypeAtIndex(2);
875 Check(ETy
->isPointerTy(), "wrong type for intrinsic global variable",
880 if (GV
.hasName() && (GV
.getName() == "llvm.used" ||
881 GV
.getName() == "llvm.compiler.used")) {
882 Check(!GV
.hasInitializer() || GV
.hasAppendingLinkage(),
883 "invalid linkage for intrinsic global variable", &GV
);
884 Check(GV
.materialized_use_empty(),
885 "invalid uses of intrinsic global variable", &GV
);
887 if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(GVType
)) {
888 PointerType
*PTy
= dyn_cast
<PointerType
>(ATy
->getElementType());
889 Check(PTy
, "wrong type for intrinsic global variable", &GV
);
890 if (GV
.hasInitializer()) {
891 const Constant
*Init
= GV
.getInitializer();
892 const ConstantArray
*InitArray
= dyn_cast
<ConstantArray
>(Init
);
893 Check(InitArray
, "wrong initalizer for intrinsic global variable",
895 for (Value
*Op
: InitArray
->operands()) {
896 Value
*V
= Op
->stripPointerCasts();
897 Check(isa
<GlobalVariable
>(V
) || isa
<Function
>(V
) ||
899 Twine("invalid ") + GV
.getName() + " member", V
);
901 Twine("members of ") + GV
.getName() + " must be named", V
);
907 // Visit any debug info attachments.
908 SmallVector
<MDNode
*, 1> MDs
;
909 GV
.getMetadata(LLVMContext::MD_dbg
, MDs
);
910 for (auto *MD
: MDs
) {
911 if (auto *GVE
= dyn_cast
<DIGlobalVariableExpression
>(MD
))
912 visitDIGlobalVariableExpression(*GVE
);
914 CheckDI(false, "!dbg attachment of global variable must be a "
915 "DIGlobalVariableExpression");
918 // Scalable vectors cannot be global variables, since we don't know
920 Check(!GVType
->isScalableTy(), "Globals cannot contain scalable types", &GV
);
922 // Check if it is or contains a target extension type that disallows being
924 Check(!GVType
->containsNonGlobalTargetExtType(),
925 "Global @" + GV
.getName() + " has illegal target extension type",
928 if (!GV
.hasInitializer()) {
929 visitGlobalValue(GV
);
933 // Walk any aggregate initializers looking for bitcasts between address spaces
934 visitConstantExprsRecursively(GV
.getInitializer());
936 visitGlobalValue(GV
);
939 void Verifier::visitAliaseeSubExpr(const GlobalAlias
&GA
, const Constant
&C
) {
940 SmallPtrSet
<const GlobalAlias
*, 4> Visited
;
942 visitAliaseeSubExpr(Visited
, GA
, C
);
945 void Verifier::visitAliaseeSubExpr(SmallPtrSetImpl
<const GlobalAlias
*> &Visited
,
946 const GlobalAlias
&GA
, const Constant
&C
) {
947 if (GA
.hasAvailableExternallyLinkage()) {
948 Check(isa
<GlobalValue
>(C
) &&
949 cast
<GlobalValue
>(C
).hasAvailableExternallyLinkage(),
950 "available_externally alias must point to available_externally "
954 if (const auto *GV
= dyn_cast
<GlobalValue
>(&C
)) {
955 if (!GA
.hasAvailableExternallyLinkage()) {
956 Check(!GV
->isDeclarationForLinker(), "Alias must point to a definition",
960 if (const auto *GA2
= dyn_cast
<GlobalAlias
>(GV
)) {
961 Check(Visited
.insert(GA2
).second
, "Aliases cannot form a cycle", &GA
);
963 Check(!GA2
->isInterposable(),
964 "Alias cannot point to an interposable alias", &GA
);
966 // Only continue verifying subexpressions of GlobalAliases.
967 // Do not recurse into global initializers.
972 if (const auto *CE
= dyn_cast
<ConstantExpr
>(&C
))
973 visitConstantExprsRecursively(CE
);
975 for (const Use
&U
: C
.operands()) {
977 if (const auto *GA2
= dyn_cast
<GlobalAlias
>(V
))
978 visitAliaseeSubExpr(Visited
, GA
, *GA2
->getAliasee());
979 else if (const auto *C2
= dyn_cast
<Constant
>(V
))
980 visitAliaseeSubExpr(Visited
, GA
, *C2
);
984 void Verifier::visitGlobalAlias(const GlobalAlias
&GA
) {
985 Check(GlobalAlias::isValidLinkage(GA
.getLinkage()),
986 "Alias should have private, internal, linkonce, weak, linkonce_odr, "
987 "weak_odr, external, or available_externally linkage!",
989 const Constant
*Aliasee
= GA
.getAliasee();
990 Check(Aliasee
, "Aliasee cannot be NULL!", &GA
);
991 Check(GA
.getType() == Aliasee
->getType(),
992 "Alias and aliasee types should match!", &GA
);
994 Check(isa
<GlobalValue
>(Aliasee
) || isa
<ConstantExpr
>(Aliasee
),
995 "Aliasee should be either GlobalValue or ConstantExpr", &GA
);
997 visitAliaseeSubExpr(GA
, *Aliasee
);
999 visitGlobalValue(GA
);
1002 void Verifier::visitGlobalIFunc(const GlobalIFunc
&GI
) {
1003 Check(GlobalIFunc::isValidLinkage(GI
.getLinkage()),
1004 "IFunc should have private, internal, linkonce, weak, linkonce_odr, "
1005 "weak_odr, or external linkage!",
1007 // Pierce through ConstantExprs and GlobalAliases and check that the resolver
1008 // is a Function definition.
1009 const Function
*Resolver
= GI
.getResolverFunction();
1010 Check(Resolver
, "IFunc must have a Function resolver", &GI
);
1011 Check(!Resolver
->isDeclarationForLinker(),
1012 "IFunc resolver must be a definition", &GI
);
1014 // Check that the immediate resolver operand (prior to any bitcasts) has the
1016 const Type
*ResolverTy
= GI
.getResolver()->getType();
1018 Check(isa
<PointerType
>(Resolver
->getFunctionType()->getReturnType()),
1019 "IFunc resolver must return a pointer", &GI
);
1021 Check(ResolverTy
== PointerType::get(Context
, GI
.getAddressSpace()),
1022 "IFunc resolver has incorrect type", &GI
);
1025 void Verifier::visitNamedMDNode(const NamedMDNode
&NMD
) {
1026 // There used to be various other llvm.dbg.* nodes, but we don't support
1027 // upgrading them and we want to reserve the namespace for future uses.
1028 if (NMD
.getName().starts_with("llvm.dbg."))
1029 CheckDI(NMD
.getName() == "llvm.dbg.cu",
1030 "unrecognized named metadata node in the llvm.dbg namespace", &NMD
);
1031 for (const MDNode
*MD
: NMD
.operands()) {
1032 if (NMD
.getName() == "llvm.dbg.cu")
1033 CheckDI(MD
&& isa
<DICompileUnit
>(MD
), "invalid compile unit", &NMD
, MD
);
1038 visitMDNode(*MD
, AreDebugLocsAllowed::Yes
);
1042 void Verifier::visitMDNode(const MDNode
&MD
, AreDebugLocsAllowed AllowLocs
) {
1043 // Only visit each node once. Metadata can be mutually recursive, so this
1044 // avoids infinite recursion here, as well as being an optimization.
1045 if (!MDNodes
.insert(&MD
).second
)
1048 Check(&MD
.getContext() == &Context
,
1049 "MDNode context does not match Module context!", &MD
);
1051 switch (MD
.getMetadataID()) {
1053 llvm_unreachable("Invalid MDNode subclass");
1054 case Metadata::MDTupleKind
:
1056 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
1057 case Metadata::CLASS##Kind: \
1058 visit##CLASS(cast<CLASS>(MD)); \
1060 #include "llvm/IR/Metadata.def"
1063 for (const Metadata
*Op
: MD
.operands()) {
1066 Check(!isa
<LocalAsMetadata
>(Op
), "Invalid operand for global metadata!",
1068 CheckDI(!isa
<DILocation
>(Op
) || AllowLocs
== AreDebugLocsAllowed::Yes
,
1069 "DILocation not allowed within this metadata node", &MD
, Op
);
1070 if (auto *N
= dyn_cast
<MDNode
>(Op
)) {
1071 visitMDNode(*N
, AllowLocs
);
1074 if (auto *V
= dyn_cast
<ValueAsMetadata
>(Op
)) {
1075 visitValueAsMetadata(*V
, nullptr);
1080 // Check these last, so we diagnose problems in operands first.
1081 Check(!MD
.isTemporary(), "Expected no forward declarations!", &MD
);
1082 Check(MD
.isResolved(), "All nodes should be resolved!", &MD
);
1085 void Verifier::visitValueAsMetadata(const ValueAsMetadata
&MD
, Function
*F
) {
1086 Check(MD
.getValue(), "Expected valid value", &MD
);
1087 Check(!MD
.getValue()->getType()->isMetadataTy(),
1088 "Unexpected metadata round-trip through values", &MD
, MD
.getValue());
1090 auto *L
= dyn_cast
<LocalAsMetadata
>(&MD
);
1094 Check(F
, "function-local metadata used outside a function", L
);
1096 // If this was an instruction, bb, or argument, verify that it is in the
1097 // function that we expect.
1098 Function
*ActualF
= nullptr;
1099 if (Instruction
*I
= dyn_cast
<Instruction
>(L
->getValue())) {
1100 Check(I
->getParent(), "function-local metadata not in basic block", L
, I
);
1101 ActualF
= I
->getParent()->getParent();
1102 } else if (BasicBlock
*BB
= dyn_cast
<BasicBlock
>(L
->getValue()))
1103 ActualF
= BB
->getParent();
1104 else if (Argument
*A
= dyn_cast
<Argument
>(L
->getValue()))
1105 ActualF
= A
->getParent();
1106 assert(ActualF
&& "Unimplemented function local metadata case!");
1108 Check(ActualF
== F
, "function-local metadata used in wrong function", L
);
1111 void Verifier::visitDIArgList(const DIArgList
&AL
, Function
*F
) {
1112 for (const ValueAsMetadata
*VAM
: AL
.getArgs())
1113 visitValueAsMetadata(*VAM
, F
);
1116 void Verifier::visitMetadataAsValue(const MetadataAsValue
&MDV
, Function
*F
) {
1117 Metadata
*MD
= MDV
.getMetadata();
1118 if (auto *N
= dyn_cast
<MDNode
>(MD
)) {
1119 visitMDNode(*N
, AreDebugLocsAllowed::No
);
1123 // Only visit each node once. Metadata can be mutually recursive, so this
1124 // avoids infinite recursion here, as well as being an optimization.
1125 if (!MDNodes
.insert(MD
).second
)
1128 if (auto *V
= dyn_cast
<ValueAsMetadata
>(MD
))
1129 visitValueAsMetadata(*V
, F
);
1131 if (auto *AL
= dyn_cast
<DIArgList
>(MD
))
1132 visitDIArgList(*AL
, F
);
1135 static bool isType(const Metadata
*MD
) { return !MD
|| isa
<DIType
>(MD
); }
1136 static bool isScope(const Metadata
*MD
) { return !MD
|| isa
<DIScope
>(MD
); }
1137 static bool isDINode(const Metadata
*MD
) { return !MD
|| isa
<DINode
>(MD
); }
1139 void Verifier::visitDILocation(const DILocation
&N
) {
1140 CheckDI(N
.getRawScope() && isa
<DILocalScope
>(N
.getRawScope()),
1141 "location requires a valid scope", &N
, N
.getRawScope());
1142 if (auto *IA
= N
.getRawInlinedAt())
1143 CheckDI(isa
<DILocation
>(IA
), "inlined-at should be a location", &N
, IA
);
1144 if (auto *SP
= dyn_cast
<DISubprogram
>(N
.getRawScope()))
1145 CheckDI(SP
->isDefinition(), "scope points into the type hierarchy", &N
);
1148 void Verifier::visitGenericDINode(const GenericDINode
&N
) {
1149 CheckDI(N
.getTag(), "invalid tag", &N
);
1152 void Verifier::visitDIScope(const DIScope
&N
) {
1153 if (auto *F
= N
.getRawFile())
1154 CheckDI(isa
<DIFile
>(F
), "invalid file", &N
, F
);
1157 void Verifier::visitDISubrange(const DISubrange
&N
) {
1158 CheckDI(N
.getTag() == dwarf::DW_TAG_subrange_type
, "invalid tag", &N
);
1159 CheckDI(!N
.getRawCountNode() || !N
.getRawUpperBound(),
1160 "Subrange can have any one of count or upperBound", &N
);
1161 auto *CBound
= N
.getRawCountNode();
1162 CheckDI(!CBound
|| isa
<ConstantAsMetadata
>(CBound
) ||
1163 isa
<DIVariable
>(CBound
) || isa
<DIExpression
>(CBound
),
1164 "Count must be signed constant or DIVariable or DIExpression", &N
);
1165 auto Count
= N
.getCount();
1166 CheckDI(!Count
|| !isa
<ConstantInt
*>(Count
) ||
1167 cast
<ConstantInt
*>(Count
)->getSExtValue() >= -1,
1168 "invalid subrange count", &N
);
1169 auto *LBound
= N
.getRawLowerBound();
1170 CheckDI(!LBound
|| isa
<ConstantAsMetadata
>(LBound
) ||
1171 isa
<DIVariable
>(LBound
) || isa
<DIExpression
>(LBound
),
1172 "LowerBound must be signed constant or DIVariable or DIExpression",
1174 auto *UBound
= N
.getRawUpperBound();
1175 CheckDI(!UBound
|| isa
<ConstantAsMetadata
>(UBound
) ||
1176 isa
<DIVariable
>(UBound
) || isa
<DIExpression
>(UBound
),
1177 "UpperBound must be signed constant or DIVariable or DIExpression",
1179 auto *Stride
= N
.getRawStride();
1180 CheckDI(!Stride
|| isa
<ConstantAsMetadata
>(Stride
) ||
1181 isa
<DIVariable
>(Stride
) || isa
<DIExpression
>(Stride
),
1182 "Stride must be signed constant or DIVariable or DIExpression", &N
);
1185 void Verifier::visitDIGenericSubrange(const DIGenericSubrange
&N
) {
1186 CheckDI(N
.getTag() == dwarf::DW_TAG_generic_subrange
, "invalid tag", &N
);
1187 CheckDI(!N
.getRawCountNode() || !N
.getRawUpperBound(),
1188 "GenericSubrange can have any one of count or upperBound", &N
);
1189 auto *CBound
= N
.getRawCountNode();
1190 CheckDI(!CBound
|| isa
<DIVariable
>(CBound
) || isa
<DIExpression
>(CBound
),
1191 "Count must be signed constant or DIVariable or DIExpression", &N
);
1192 auto *LBound
= N
.getRawLowerBound();
1193 CheckDI(LBound
, "GenericSubrange must contain lowerBound", &N
);
1194 CheckDI(isa
<DIVariable
>(LBound
) || isa
<DIExpression
>(LBound
),
1195 "LowerBound must be signed constant or DIVariable or DIExpression",
1197 auto *UBound
= N
.getRawUpperBound();
1198 CheckDI(!UBound
|| isa
<DIVariable
>(UBound
) || isa
<DIExpression
>(UBound
),
1199 "UpperBound must be signed constant or DIVariable or DIExpression",
1201 auto *Stride
= N
.getRawStride();
1202 CheckDI(Stride
, "GenericSubrange must contain stride", &N
);
1203 CheckDI(isa
<DIVariable
>(Stride
) || isa
<DIExpression
>(Stride
),
1204 "Stride must be signed constant or DIVariable or DIExpression", &N
);
1207 void Verifier::visitDIEnumerator(const DIEnumerator
&N
) {
1208 CheckDI(N
.getTag() == dwarf::DW_TAG_enumerator
, "invalid tag", &N
);
1211 void Verifier::visitDIBasicType(const DIBasicType
&N
) {
1212 CheckDI(N
.getTag() == dwarf::DW_TAG_base_type
||
1213 N
.getTag() == dwarf::DW_TAG_unspecified_type
||
1214 N
.getTag() == dwarf::DW_TAG_string_type
,
1218 void Verifier::visitDIStringType(const DIStringType
&N
) {
1219 CheckDI(N
.getTag() == dwarf::DW_TAG_string_type
, "invalid tag", &N
);
1220 CheckDI(!(N
.isBigEndian() && N
.isLittleEndian()), "has conflicting flags",
1224 void Verifier::visitDIDerivedType(const DIDerivedType
&N
) {
1225 // Common scope checks.
1228 CheckDI(N
.getTag() == dwarf::DW_TAG_typedef
||
1229 N
.getTag() == dwarf::DW_TAG_pointer_type
||
1230 N
.getTag() == dwarf::DW_TAG_ptr_to_member_type
||
1231 N
.getTag() == dwarf::DW_TAG_reference_type
||
1232 N
.getTag() == dwarf::DW_TAG_rvalue_reference_type
||
1233 N
.getTag() == dwarf::DW_TAG_const_type
||
1234 N
.getTag() == dwarf::DW_TAG_immutable_type
||
1235 N
.getTag() == dwarf::DW_TAG_volatile_type
||
1236 N
.getTag() == dwarf::DW_TAG_restrict_type
||
1237 N
.getTag() == dwarf::DW_TAG_atomic_type
||
1238 N
.getTag() == dwarf::DW_TAG_LLVM_ptrauth_type
||
1239 N
.getTag() == dwarf::DW_TAG_member
||
1240 (N
.getTag() == dwarf::DW_TAG_variable
&& N
.isStaticMember()) ||
1241 N
.getTag() == dwarf::DW_TAG_inheritance
||
1242 N
.getTag() == dwarf::DW_TAG_friend
||
1243 N
.getTag() == dwarf::DW_TAG_set_type
||
1244 N
.getTag() == dwarf::DW_TAG_template_alias
,
1246 if (N
.getTag() == dwarf::DW_TAG_ptr_to_member_type
) {
1247 CheckDI(isType(N
.getRawExtraData()), "invalid pointer to member type", &N
,
1248 N
.getRawExtraData());
1251 if (N
.getTag() == dwarf::DW_TAG_set_type
) {
1252 if (auto *T
= N
.getRawBaseType()) {
1253 auto *Enum
= dyn_cast_or_null
<DICompositeType
>(T
);
1254 auto *Basic
= dyn_cast_or_null
<DIBasicType
>(T
);
1256 (Enum
&& Enum
->getTag() == dwarf::DW_TAG_enumeration_type
) ||
1257 (Basic
&& (Basic
->getEncoding() == dwarf::DW_ATE_unsigned
||
1258 Basic
->getEncoding() == dwarf::DW_ATE_signed
||
1259 Basic
->getEncoding() == dwarf::DW_ATE_unsigned_char
||
1260 Basic
->getEncoding() == dwarf::DW_ATE_signed_char
||
1261 Basic
->getEncoding() == dwarf::DW_ATE_boolean
)),
1262 "invalid set base type", &N
, T
);
1266 CheckDI(isScope(N
.getRawScope()), "invalid scope", &N
, N
.getRawScope());
1267 CheckDI(isType(N
.getRawBaseType()), "invalid base type", &N
,
1268 N
.getRawBaseType());
1270 if (N
.getDWARFAddressSpace()) {
1271 CheckDI(N
.getTag() == dwarf::DW_TAG_pointer_type
||
1272 N
.getTag() == dwarf::DW_TAG_reference_type
||
1273 N
.getTag() == dwarf::DW_TAG_rvalue_reference_type
,
1274 "DWARF address space only applies to pointer or reference types",
1279 /// Detect mutually exclusive flags.
1280 static bool hasConflictingReferenceFlags(unsigned Flags
) {
1281 return ((Flags
& DINode::FlagLValueReference
) &&
1282 (Flags
& DINode::FlagRValueReference
)) ||
1283 ((Flags
& DINode::FlagTypePassByValue
) &&
1284 (Flags
& DINode::FlagTypePassByReference
));
1287 void Verifier::visitTemplateParams(const MDNode
&N
, const Metadata
&RawParams
) {
1288 auto *Params
= dyn_cast
<MDTuple
>(&RawParams
);
1289 CheckDI(Params
, "invalid template params", &N
, &RawParams
);
1290 for (Metadata
*Op
: Params
->operands()) {
1291 CheckDI(Op
&& isa
<DITemplateParameter
>(Op
), "invalid template parameter",
1296 void Verifier::visitDICompositeType(const DICompositeType
&N
) {
1297 // Common scope checks.
1300 CheckDI(N
.getTag() == dwarf::DW_TAG_array_type
||
1301 N
.getTag() == dwarf::DW_TAG_structure_type
||
1302 N
.getTag() == dwarf::DW_TAG_union_type
||
1303 N
.getTag() == dwarf::DW_TAG_enumeration_type
||
1304 N
.getTag() == dwarf::DW_TAG_class_type
||
1305 N
.getTag() == dwarf::DW_TAG_variant_part
||
1306 N
.getTag() == dwarf::DW_TAG_namelist
,
1309 CheckDI(isScope(N
.getRawScope()), "invalid scope", &N
, N
.getRawScope());
1310 CheckDI(isType(N
.getRawBaseType()), "invalid base type", &N
,
1311 N
.getRawBaseType());
1313 CheckDI(!N
.getRawElements() || isa
<MDTuple
>(N
.getRawElements()),
1314 "invalid composite elements", &N
, N
.getRawElements());
1315 CheckDI(isType(N
.getRawVTableHolder()), "invalid vtable holder", &N
,
1316 N
.getRawVTableHolder());
1317 CheckDI(!hasConflictingReferenceFlags(N
.getFlags()),
1318 "invalid reference flags", &N
);
1319 unsigned DIBlockByRefStruct
= 1 << 4;
1320 CheckDI((N
.getFlags() & DIBlockByRefStruct
) == 0,
1321 "DIBlockByRefStruct on DICompositeType is no longer supported", &N
);
1324 const DINodeArray Elements
= N
.getElements();
1325 CheckDI(Elements
.size() == 1 &&
1326 Elements
[0]->getTag() == dwarf::DW_TAG_subrange_type
,
1327 "invalid vector, expected one element of type subrange", &N
);
1330 if (auto *Params
= N
.getRawTemplateParams())
1331 visitTemplateParams(N
, *Params
);
1333 if (auto *D
= N
.getRawDiscriminator()) {
1334 CheckDI(isa
<DIDerivedType
>(D
) && N
.getTag() == dwarf::DW_TAG_variant_part
,
1335 "discriminator can only appear on variant part");
1338 if (N
.getRawDataLocation()) {
1339 CheckDI(N
.getTag() == dwarf::DW_TAG_array_type
,
1340 "dataLocation can only appear in array type");
1343 if (N
.getRawAssociated()) {
1344 CheckDI(N
.getTag() == dwarf::DW_TAG_array_type
,
1345 "associated can only appear in array type");
1348 if (N
.getRawAllocated()) {
1349 CheckDI(N
.getTag() == dwarf::DW_TAG_array_type
,
1350 "allocated can only appear in array type");
1353 if (N
.getRawRank()) {
1354 CheckDI(N
.getTag() == dwarf::DW_TAG_array_type
,
1355 "rank can only appear in array type");
1358 if (N
.getTag() == dwarf::DW_TAG_array_type
) {
1359 CheckDI(N
.getRawBaseType(), "array types must have a base type", &N
);
1363 void Verifier::visitDISubroutineType(const DISubroutineType
&N
) {
1364 CheckDI(N
.getTag() == dwarf::DW_TAG_subroutine_type
, "invalid tag", &N
);
1365 if (auto *Types
= N
.getRawTypeArray()) {
1366 CheckDI(isa
<MDTuple
>(Types
), "invalid composite elements", &N
, Types
);
1367 for (Metadata
*Ty
: N
.getTypeArray()->operands()) {
1368 CheckDI(isType(Ty
), "invalid subroutine type ref", &N
, Types
, Ty
);
1371 CheckDI(!hasConflictingReferenceFlags(N
.getFlags()),
1372 "invalid reference flags", &N
);
1375 void Verifier::visitDIFile(const DIFile
&N
) {
1376 CheckDI(N
.getTag() == dwarf::DW_TAG_file_type
, "invalid tag", &N
);
1377 std::optional
<DIFile::ChecksumInfo
<StringRef
>> Checksum
= N
.getChecksum();
1379 CheckDI(Checksum
->Kind
<= DIFile::ChecksumKind::CSK_Last
,
1380 "invalid checksum kind", &N
);
1382 switch (Checksum
->Kind
) {
1383 case DIFile::CSK_MD5
:
1386 case DIFile::CSK_SHA1
:
1389 case DIFile::CSK_SHA256
:
1393 CheckDI(Checksum
->Value
.size() == Size
, "invalid checksum length", &N
);
1394 CheckDI(Checksum
->Value
.find_if_not(llvm::isHexDigit
) == StringRef::npos
,
1395 "invalid checksum", &N
);
1399 void Verifier::visitDICompileUnit(const DICompileUnit
&N
) {
1400 CheckDI(N
.isDistinct(), "compile units must be distinct", &N
);
1401 CheckDI(N
.getTag() == dwarf::DW_TAG_compile_unit
, "invalid tag", &N
);
1403 // Don't bother verifying the compilation directory or producer string
1404 // as those could be empty.
1405 CheckDI(N
.getRawFile() && isa
<DIFile
>(N
.getRawFile()), "invalid file", &N
,
1407 CheckDI(!N
.getFile()->getFilename().empty(), "invalid filename", &N
,
1410 CheckDI((N
.getEmissionKind() <= DICompileUnit::LastEmissionKind
),
1411 "invalid emission kind", &N
);
1413 if (auto *Array
= N
.getRawEnumTypes()) {
1414 CheckDI(isa
<MDTuple
>(Array
), "invalid enum list", &N
, Array
);
1415 for (Metadata
*Op
: N
.getEnumTypes()->operands()) {
1416 auto *Enum
= dyn_cast_or_null
<DICompositeType
>(Op
);
1417 CheckDI(Enum
&& Enum
->getTag() == dwarf::DW_TAG_enumeration_type
,
1418 "invalid enum type", &N
, N
.getEnumTypes(), Op
);
1421 if (auto *Array
= N
.getRawRetainedTypes()) {
1422 CheckDI(isa
<MDTuple
>(Array
), "invalid retained type list", &N
, Array
);
1423 for (Metadata
*Op
: N
.getRetainedTypes()->operands()) {
1425 Op
&& (isa
<DIType
>(Op
) || (isa
<DISubprogram
>(Op
) &&
1426 !cast
<DISubprogram
>(Op
)->isDefinition())),
1427 "invalid retained type", &N
, Op
);
1430 if (auto *Array
= N
.getRawGlobalVariables()) {
1431 CheckDI(isa
<MDTuple
>(Array
), "invalid global variable list", &N
, Array
);
1432 for (Metadata
*Op
: N
.getGlobalVariables()->operands()) {
1433 CheckDI(Op
&& (isa
<DIGlobalVariableExpression
>(Op
)),
1434 "invalid global variable ref", &N
, Op
);
1437 if (auto *Array
= N
.getRawImportedEntities()) {
1438 CheckDI(isa
<MDTuple
>(Array
), "invalid imported entity list", &N
, Array
);
1439 for (Metadata
*Op
: N
.getImportedEntities()->operands()) {
1440 CheckDI(Op
&& isa
<DIImportedEntity
>(Op
), "invalid imported entity ref",
1444 if (auto *Array
= N
.getRawMacros()) {
1445 CheckDI(isa
<MDTuple
>(Array
), "invalid macro list", &N
, Array
);
1446 for (Metadata
*Op
: N
.getMacros()->operands()) {
1447 CheckDI(Op
&& isa
<DIMacroNode
>(Op
), "invalid macro ref", &N
, Op
);
1450 CUVisited
.insert(&N
);
1453 void Verifier::visitDISubprogram(const DISubprogram
&N
) {
1454 CheckDI(N
.getTag() == dwarf::DW_TAG_subprogram
, "invalid tag", &N
);
1455 CheckDI(isScope(N
.getRawScope()), "invalid scope", &N
, N
.getRawScope());
1456 if (auto *F
= N
.getRawFile())
1457 CheckDI(isa
<DIFile
>(F
), "invalid file", &N
, F
);
1459 CheckDI(N
.getLine() == 0, "line specified with no file", &N
, N
.getLine());
1460 if (auto *T
= N
.getRawType())
1461 CheckDI(isa
<DISubroutineType
>(T
), "invalid subroutine type", &N
, T
);
1462 CheckDI(isType(N
.getRawContainingType()), "invalid containing type", &N
,
1463 N
.getRawContainingType());
1464 if (auto *Params
= N
.getRawTemplateParams())
1465 visitTemplateParams(N
, *Params
);
1466 if (auto *S
= N
.getRawDeclaration())
1467 CheckDI(isa
<DISubprogram
>(S
) && !cast
<DISubprogram
>(S
)->isDefinition(),
1468 "invalid subprogram declaration", &N
, S
);
1469 if (auto *RawNode
= N
.getRawRetainedNodes()) {
1470 auto *Node
= dyn_cast
<MDTuple
>(RawNode
);
1471 CheckDI(Node
, "invalid retained nodes list", &N
, RawNode
);
1472 for (Metadata
*Op
: Node
->operands()) {
1473 CheckDI(Op
&& (isa
<DILocalVariable
>(Op
) || isa
<DILabel
>(Op
) ||
1474 isa
<DIImportedEntity
>(Op
)),
1475 "invalid retained nodes, expected DILocalVariable, DILabel or "
1480 CheckDI(!hasConflictingReferenceFlags(N
.getFlags()),
1481 "invalid reference flags", &N
);
1483 auto *Unit
= N
.getRawUnit();
1484 if (N
.isDefinition()) {
1485 // Subprogram definitions (not part of the type hierarchy).
1486 CheckDI(N
.isDistinct(), "subprogram definitions must be distinct", &N
);
1487 CheckDI(Unit
, "subprogram definitions must have a compile unit", &N
);
1488 CheckDI(isa
<DICompileUnit
>(Unit
), "invalid unit type", &N
, Unit
);
1489 // There's no good way to cross the CU boundary to insert a nested
1490 // DISubprogram definition in one CU into a type defined in another CU.
1491 auto *CT
= dyn_cast_or_null
<DICompositeType
>(N
.getRawScope());
1492 if (CT
&& CT
->getRawIdentifier() &&
1493 M
.getContext().isODRUniquingDebugTypes())
1494 CheckDI(N
.getDeclaration(),
1495 "definition subprograms cannot be nested within DICompositeType "
1496 "when enabling ODR",
1499 // Subprogram declarations (part of the type hierarchy).
1500 CheckDI(!Unit
, "subprogram declarations must not have a compile unit", &N
);
1501 CheckDI(!N
.getRawDeclaration(),
1502 "subprogram declaration must not have a declaration field");
1505 if (auto *RawThrownTypes
= N
.getRawThrownTypes()) {
1506 auto *ThrownTypes
= dyn_cast
<MDTuple
>(RawThrownTypes
);
1507 CheckDI(ThrownTypes
, "invalid thrown types list", &N
, RawThrownTypes
);
1508 for (Metadata
*Op
: ThrownTypes
->operands())
1509 CheckDI(Op
&& isa
<DIType
>(Op
), "invalid thrown type", &N
, ThrownTypes
,
1513 if (N
.areAllCallsDescribed())
1514 CheckDI(N
.isDefinition(),
1515 "DIFlagAllCallsDescribed must be attached to a definition");
1518 void Verifier::visitDILexicalBlockBase(const DILexicalBlockBase
&N
) {
1519 CheckDI(N
.getTag() == dwarf::DW_TAG_lexical_block
, "invalid tag", &N
);
1520 CheckDI(N
.getRawScope() && isa
<DILocalScope
>(N
.getRawScope()),
1521 "invalid local scope", &N
, N
.getRawScope());
1522 if (auto *SP
= dyn_cast
<DISubprogram
>(N
.getRawScope()))
1523 CheckDI(SP
->isDefinition(), "scope points into the type hierarchy", &N
);
1526 void Verifier::visitDILexicalBlock(const DILexicalBlock
&N
) {
1527 visitDILexicalBlockBase(N
);
1529 CheckDI(N
.getLine() || !N
.getColumn(),
1530 "cannot have column info without line info", &N
);
1533 void Verifier::visitDILexicalBlockFile(const DILexicalBlockFile
&N
) {
1534 visitDILexicalBlockBase(N
);
1537 void Verifier::visitDICommonBlock(const DICommonBlock
&N
) {
1538 CheckDI(N
.getTag() == dwarf::DW_TAG_common_block
, "invalid tag", &N
);
1539 if (auto *S
= N
.getRawScope())
1540 CheckDI(isa
<DIScope
>(S
), "invalid scope ref", &N
, S
);
1541 if (auto *S
= N
.getRawDecl())
1542 CheckDI(isa
<DIGlobalVariable
>(S
), "invalid declaration", &N
, S
);
1545 void Verifier::visitDINamespace(const DINamespace
&N
) {
1546 CheckDI(N
.getTag() == dwarf::DW_TAG_namespace
, "invalid tag", &N
);
1547 if (auto *S
= N
.getRawScope())
1548 CheckDI(isa
<DIScope
>(S
), "invalid scope ref", &N
, S
);
1551 void Verifier::visitDIMacro(const DIMacro
&N
) {
1552 CheckDI(N
.getMacinfoType() == dwarf::DW_MACINFO_define
||
1553 N
.getMacinfoType() == dwarf::DW_MACINFO_undef
,
1554 "invalid macinfo type", &N
);
1555 CheckDI(!N
.getName().empty(), "anonymous macro", &N
);
1556 if (!N
.getValue().empty()) {
1557 assert(N
.getValue().data()[0] != ' ' && "Macro value has a space prefix");
1561 void Verifier::visitDIMacroFile(const DIMacroFile
&N
) {
1562 CheckDI(N
.getMacinfoType() == dwarf::DW_MACINFO_start_file
,
1563 "invalid macinfo type", &N
);
1564 if (auto *F
= N
.getRawFile())
1565 CheckDI(isa
<DIFile
>(F
), "invalid file", &N
, F
);
1567 if (auto *Array
= N
.getRawElements()) {
1568 CheckDI(isa
<MDTuple
>(Array
), "invalid macro list", &N
, Array
);
1569 for (Metadata
*Op
: N
.getElements()->operands()) {
1570 CheckDI(Op
&& isa
<DIMacroNode
>(Op
), "invalid macro ref", &N
, Op
);
1575 void Verifier::visitDIModule(const DIModule
&N
) {
1576 CheckDI(N
.getTag() == dwarf::DW_TAG_module
, "invalid tag", &N
);
1577 CheckDI(!N
.getName().empty(), "anonymous module", &N
);
1580 void Verifier::visitDITemplateParameter(const DITemplateParameter
&N
) {
1581 CheckDI(isType(N
.getRawType()), "invalid type ref", &N
, N
.getRawType());
1584 void Verifier::visitDITemplateTypeParameter(const DITemplateTypeParameter
&N
) {
1585 visitDITemplateParameter(N
);
1587 CheckDI(N
.getTag() == dwarf::DW_TAG_template_type_parameter
, "invalid tag",
1591 void Verifier::visitDITemplateValueParameter(
1592 const DITemplateValueParameter
&N
) {
1593 visitDITemplateParameter(N
);
1595 CheckDI(N
.getTag() == dwarf::DW_TAG_template_value_parameter
||
1596 N
.getTag() == dwarf::DW_TAG_GNU_template_template_param
||
1597 N
.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack
,
1601 void Verifier::visitDIVariable(const DIVariable
&N
) {
1602 if (auto *S
= N
.getRawScope())
1603 CheckDI(isa
<DIScope
>(S
), "invalid scope", &N
, S
);
1604 if (auto *F
= N
.getRawFile())
1605 CheckDI(isa
<DIFile
>(F
), "invalid file", &N
, F
);
1608 void Verifier::visitDIGlobalVariable(const DIGlobalVariable
&N
) {
1609 // Checks common to all variables.
1612 CheckDI(N
.getTag() == dwarf::DW_TAG_variable
, "invalid tag", &N
);
1613 CheckDI(isType(N
.getRawType()), "invalid type ref", &N
, N
.getRawType());
1614 // Check only if the global variable is not an extern
1615 if (N
.isDefinition())
1616 CheckDI(N
.getType(), "missing global variable type", &N
);
1617 if (auto *Member
= N
.getRawStaticDataMemberDeclaration()) {
1618 CheckDI(isa
<DIDerivedType
>(Member
),
1619 "invalid static data member declaration", &N
, Member
);
1623 void Verifier::visitDILocalVariable(const DILocalVariable
&N
) {
1624 // Checks common to all variables.
1627 CheckDI(isType(N
.getRawType()), "invalid type ref", &N
, N
.getRawType());
1628 CheckDI(N
.getTag() == dwarf::DW_TAG_variable
, "invalid tag", &N
);
1629 CheckDI(N
.getRawScope() && isa
<DILocalScope
>(N
.getRawScope()),
1630 "local variable requires a valid scope", &N
, N
.getRawScope());
1631 if (auto Ty
= N
.getType())
1632 CheckDI(!isa
<DISubroutineType
>(Ty
), "invalid type", &N
, N
.getType());
1635 void Verifier::visitDIAssignID(const DIAssignID
&N
) {
1636 CheckDI(!N
.getNumOperands(), "DIAssignID has no arguments", &N
);
1637 CheckDI(N
.isDistinct(), "DIAssignID must be distinct", &N
);
1640 void Verifier::visitDILabel(const DILabel
&N
) {
1641 if (auto *S
= N
.getRawScope())
1642 CheckDI(isa
<DIScope
>(S
), "invalid scope", &N
, S
);
1643 if (auto *F
= N
.getRawFile())
1644 CheckDI(isa
<DIFile
>(F
), "invalid file", &N
, F
);
1646 CheckDI(N
.getTag() == dwarf::DW_TAG_label
, "invalid tag", &N
);
1647 CheckDI(N
.getRawScope() && isa
<DILocalScope
>(N
.getRawScope()),
1648 "label requires a valid scope", &N
, N
.getRawScope());
1651 void Verifier::visitDIExpression(const DIExpression
&N
) {
1652 CheckDI(N
.isValid(), "invalid expression", &N
);
1655 void Verifier::visitDIGlobalVariableExpression(
1656 const DIGlobalVariableExpression
&GVE
) {
1657 CheckDI(GVE
.getVariable(), "missing variable");
1658 if (auto *Var
= GVE
.getVariable())
1659 visitDIGlobalVariable(*Var
);
1660 if (auto *Expr
= GVE
.getExpression()) {
1661 visitDIExpression(*Expr
);
1662 if (auto Fragment
= Expr
->getFragmentInfo())
1663 verifyFragmentExpression(*GVE
.getVariable(), *Fragment
, &GVE
);
1667 void Verifier::visitDIObjCProperty(const DIObjCProperty
&N
) {
1668 CheckDI(N
.getTag() == dwarf::DW_TAG_APPLE_property
, "invalid tag", &N
);
1669 if (auto *T
= N
.getRawType())
1670 CheckDI(isType(T
), "invalid type ref", &N
, T
);
1671 if (auto *F
= N
.getRawFile())
1672 CheckDI(isa
<DIFile
>(F
), "invalid file", &N
, F
);
1675 void Verifier::visitDIImportedEntity(const DIImportedEntity
&N
) {
1676 CheckDI(N
.getTag() == dwarf::DW_TAG_imported_module
||
1677 N
.getTag() == dwarf::DW_TAG_imported_declaration
,
1679 if (auto *S
= N
.getRawScope())
1680 CheckDI(isa
<DIScope
>(S
), "invalid scope for imported entity", &N
, S
);
1681 CheckDI(isDINode(N
.getRawEntity()), "invalid imported entity", &N
,
1685 void Verifier::visitComdat(const Comdat
&C
) {
1686 // In COFF the Module is invalid if the GlobalValue has private linkage.
1687 // Entities with private linkage don't have entries in the symbol table.
1688 if (TT
.isOSBinFormatCOFF())
1689 if (const GlobalValue
*GV
= M
.getNamedValue(C
.getName()))
1690 Check(!GV
->hasPrivateLinkage(), "comdat global value has private linkage",
1694 void Verifier::visitModuleIdents() {
1695 const NamedMDNode
*Idents
= M
.getNamedMetadata("llvm.ident");
1699 // llvm.ident takes a list of metadata entry. Each entry has only one string.
1700 // Scan each llvm.ident entry and make sure that this requirement is met.
1701 for (const MDNode
*N
: Idents
->operands()) {
1702 Check(N
->getNumOperands() == 1,
1703 "incorrect number of operands in llvm.ident metadata", N
);
1704 Check(dyn_cast_or_null
<MDString
>(N
->getOperand(0)),
1705 ("invalid value for llvm.ident metadata entry operand"
1706 "(the operand should be a string)"),
1711 void Verifier::visitModuleCommandLines() {
1712 const NamedMDNode
*CommandLines
= M
.getNamedMetadata("llvm.commandline");
1716 // llvm.commandline takes a list of metadata entry. Each entry has only one
1717 // string. Scan each llvm.commandline entry and make sure that this
1718 // requirement is met.
1719 for (const MDNode
*N
: CommandLines
->operands()) {
1720 Check(N
->getNumOperands() == 1,
1721 "incorrect number of operands in llvm.commandline metadata", N
);
1722 Check(dyn_cast_or_null
<MDString
>(N
->getOperand(0)),
1723 ("invalid value for llvm.commandline metadata entry operand"
1724 "(the operand should be a string)"),
1729 void Verifier::visitModuleFlags() {
1730 const NamedMDNode
*Flags
= M
.getModuleFlagsMetadata();
1733 // Scan each flag, and track the flags and requirements.
1734 DenseMap
<const MDString
*, const MDNode
*> SeenIDs
;
1735 SmallVector
<const MDNode
*, 16> Requirements
;
1736 uint64_t PAuthABIPlatform
= -1;
1737 uint64_t PAuthABIVersion
= -1;
1738 for (const MDNode
*MDN
: Flags
->operands()) {
1739 visitModuleFlag(MDN
, SeenIDs
, Requirements
);
1740 if (MDN
->getNumOperands() != 3)
1742 if (const auto *FlagName
= dyn_cast_or_null
<MDString
>(MDN
->getOperand(1))) {
1743 if (FlagName
->getString() == "aarch64-elf-pauthabi-platform") {
1744 if (const auto *PAP
=
1745 mdconst::dyn_extract_or_null
<ConstantInt
>(MDN
->getOperand(2)))
1746 PAuthABIPlatform
= PAP
->getZExtValue();
1747 } else if (FlagName
->getString() == "aarch64-elf-pauthabi-version") {
1748 if (const auto *PAV
=
1749 mdconst::dyn_extract_or_null
<ConstantInt
>(MDN
->getOperand(2)))
1750 PAuthABIVersion
= PAV
->getZExtValue();
1755 if ((PAuthABIPlatform
== uint64_t(-1)) != (PAuthABIVersion
== uint64_t(-1)))
1756 CheckFailed("either both or no 'aarch64-elf-pauthabi-platform' and "
1757 "'aarch64-elf-pauthabi-version' module flags must be present");
1759 // Validate that the requirements in the module are valid.
1760 for (const MDNode
*Requirement
: Requirements
) {
1761 const MDString
*Flag
= cast
<MDString
>(Requirement
->getOperand(0));
1762 const Metadata
*ReqValue
= Requirement
->getOperand(1);
1764 const MDNode
*Op
= SeenIDs
.lookup(Flag
);
1766 CheckFailed("invalid requirement on flag, flag is not present in module",
1771 if (Op
->getOperand(2) != ReqValue
) {
1772 CheckFailed(("invalid requirement on flag, "
1773 "flag does not have the required value"),
1781 Verifier::visitModuleFlag(const MDNode
*Op
,
1782 DenseMap
<const MDString
*, const MDNode
*> &SeenIDs
,
1783 SmallVectorImpl
<const MDNode
*> &Requirements
) {
1784 // Each module flag should have three arguments, the merge behavior (a
1785 // constant int), the flag ID (an MDString), and the value.
1786 Check(Op
->getNumOperands() == 3,
1787 "incorrect number of operands in module flag", Op
);
1788 Module::ModFlagBehavior MFB
;
1789 if (!Module::isValidModFlagBehavior(Op
->getOperand(0), MFB
)) {
1790 Check(mdconst::dyn_extract_or_null
<ConstantInt
>(Op
->getOperand(0)),
1791 "invalid behavior operand in module flag (expected constant integer)",
1794 "invalid behavior operand in module flag (unexpected constant)",
1797 MDString
*ID
= dyn_cast_or_null
<MDString
>(Op
->getOperand(1));
1798 Check(ID
, "invalid ID operand in module flag (expected metadata string)",
1801 // Check the values for behaviors with additional requirements.
1804 case Module::Warning
:
1805 case Module::Override
:
1806 // These behavior types accept any value.
1810 auto *V
= mdconst::dyn_extract_or_null
<ConstantInt
>(Op
->getOperand(2));
1811 Check(V
&& V
->getValue().isNonNegative(),
1812 "invalid value for 'min' module flag (expected constant non-negative "
1819 Check(mdconst::dyn_extract_or_null
<ConstantInt
>(Op
->getOperand(2)),
1820 "invalid value for 'max' module flag (expected constant integer)",
1825 case Module::Require
: {
1826 // The value should itself be an MDNode with two operands, a flag ID (an
1827 // MDString), and a value.
1828 MDNode
*Value
= dyn_cast
<MDNode
>(Op
->getOperand(2));
1829 Check(Value
&& Value
->getNumOperands() == 2,
1830 "invalid value for 'require' module flag (expected metadata pair)",
1832 Check(isa
<MDString
>(Value
->getOperand(0)),
1833 ("invalid value for 'require' module flag "
1834 "(first value operand should be a string)"),
1835 Value
->getOperand(0));
1837 // Append it to the list of requirements, to check once all module flags are
1839 Requirements
.push_back(Value
);
1843 case Module::Append
:
1844 case Module::AppendUnique
: {
1845 // These behavior types require the operand be an MDNode.
1846 Check(isa
<MDNode
>(Op
->getOperand(2)),
1847 "invalid value for 'append'-type module flag "
1848 "(expected a metadata node)",
1854 // Unless this is a "requires" flag, check the ID is unique.
1855 if (MFB
!= Module::Require
) {
1856 bool Inserted
= SeenIDs
.insert(std::make_pair(ID
, Op
)).second
;
1858 "module flag identifiers must be unique (or of 'require' type)", ID
);
1861 if (ID
->getString() == "wchar_size") {
1863 = mdconst::dyn_extract_or_null
<ConstantInt
>(Op
->getOperand(2));
1864 Check(Value
, "wchar_size metadata requires constant integer argument");
1867 if (ID
->getString() == "Linker Options") {
1868 // If the llvm.linker.options named metadata exists, we assume that the
1869 // bitcode reader has upgraded the module flag. Otherwise the flag might
1870 // have been created by a client directly.
1871 Check(M
.getNamedMetadata("llvm.linker.options"),
1872 "'Linker Options' named metadata no longer supported");
1875 if (ID
->getString() == "SemanticInterposition") {
1876 ConstantInt
*Value
=
1877 mdconst::dyn_extract_or_null
<ConstantInt
>(Op
->getOperand(2));
1879 "SemanticInterposition metadata requires constant integer argument");
1882 if (ID
->getString() == "CG Profile") {
1883 for (const MDOperand
&MDO
: cast
<MDNode
>(Op
->getOperand(2))->operands())
1884 visitModuleFlagCGProfileEntry(MDO
);
1888 void Verifier::visitModuleFlagCGProfileEntry(const MDOperand
&MDO
) {
1889 auto CheckFunction
= [&](const MDOperand
&FuncMDO
) {
1892 auto F
= dyn_cast
<ValueAsMetadata
>(FuncMDO
);
1893 Check(F
&& isa
<Function
>(F
->getValue()->stripPointerCasts()),
1894 "expected a Function or null", FuncMDO
);
1896 auto Node
= dyn_cast_or_null
<MDNode
>(MDO
);
1897 Check(Node
&& Node
->getNumOperands() == 3, "expected a MDNode triple", MDO
);
1898 CheckFunction(Node
->getOperand(0));
1899 CheckFunction(Node
->getOperand(1));
1900 auto Count
= dyn_cast_or_null
<ConstantAsMetadata
>(Node
->getOperand(2));
1901 Check(Count
&& Count
->getType()->isIntegerTy(),
1902 "expected an integer constant", Node
->getOperand(2));
1905 void Verifier::verifyAttributeTypes(AttributeSet Attrs
, const Value
*V
) {
1906 for (Attribute A
: Attrs
) {
1908 if (A
.isStringAttribute()) {
1909 #define GET_ATTR_NAMES
1910 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME)
1911 #define ATTRIBUTE_STRBOOL(ENUM_NAME, DISPLAY_NAME) \
1912 if (A.getKindAsString() == #DISPLAY_NAME) { \
1913 auto V = A.getValueAsString(); \
1914 if (!(V.empty() || V == "true" || V == "false")) \
1915 CheckFailed("invalid value for '" #DISPLAY_NAME "' attribute: " + V + \
1919 #include "llvm/IR/Attributes.inc"
1923 if (A
.isIntAttribute() != Attribute::isIntAttrKind(A
.getKindAsEnum())) {
1924 CheckFailed("Attribute '" + A
.getAsString() + "' should have an Argument",
1931 // VerifyParameterAttrs - Check the given attributes for an argument or return
1932 // value of the specified type. The value V is printed in error messages.
1933 void Verifier::verifyParameterAttrs(AttributeSet Attrs
, Type
*Ty
,
1935 if (!Attrs
.hasAttributes())
1938 verifyAttributeTypes(Attrs
, V
);
1940 for (Attribute Attr
: Attrs
)
1941 Check(Attr
.isStringAttribute() ||
1942 Attribute::canUseAsParamAttr(Attr
.getKindAsEnum()),
1943 "Attribute '" + Attr
.getAsString() + "' does not apply to parameters",
1946 if (Attrs
.hasAttribute(Attribute::ImmArg
)) {
1947 Check(Attrs
.getNumAttributes() == 1,
1948 "Attribute 'immarg' is incompatible with other attributes", V
);
1951 // Check for mutually incompatible attributes. Only inreg is compatible with
1953 unsigned AttrCount
= 0;
1954 AttrCount
+= Attrs
.hasAttribute(Attribute::ByVal
);
1955 AttrCount
+= Attrs
.hasAttribute(Attribute::InAlloca
);
1956 AttrCount
+= Attrs
.hasAttribute(Attribute::Preallocated
);
1957 AttrCount
+= Attrs
.hasAttribute(Attribute::StructRet
) ||
1958 Attrs
.hasAttribute(Attribute::InReg
);
1959 AttrCount
+= Attrs
.hasAttribute(Attribute::Nest
);
1960 AttrCount
+= Attrs
.hasAttribute(Attribute::ByRef
);
1961 Check(AttrCount
<= 1,
1962 "Attributes 'byval', 'inalloca', 'preallocated', 'inreg', 'nest', "
1963 "'byref', and 'sret' are incompatible!",
1966 Check(!(Attrs
.hasAttribute(Attribute::InAlloca
) &&
1967 Attrs
.hasAttribute(Attribute::ReadOnly
)),
1969 "'inalloca and readonly' are incompatible!",
1972 Check(!(Attrs
.hasAttribute(Attribute::StructRet
) &&
1973 Attrs
.hasAttribute(Attribute::Returned
)),
1975 "'sret and returned' are incompatible!",
1978 Check(!(Attrs
.hasAttribute(Attribute::ZExt
) &&
1979 Attrs
.hasAttribute(Attribute::SExt
)),
1981 "'zeroext and signext' are incompatible!",
1984 Check(!(Attrs
.hasAttribute(Attribute::ReadNone
) &&
1985 Attrs
.hasAttribute(Attribute::ReadOnly
)),
1987 "'readnone and readonly' are incompatible!",
1990 Check(!(Attrs
.hasAttribute(Attribute::ReadNone
) &&
1991 Attrs
.hasAttribute(Attribute::WriteOnly
)),
1993 "'readnone and writeonly' are incompatible!",
1996 Check(!(Attrs
.hasAttribute(Attribute::ReadOnly
) &&
1997 Attrs
.hasAttribute(Attribute::WriteOnly
)),
1999 "'readonly and writeonly' are incompatible!",
2002 Check(!(Attrs
.hasAttribute(Attribute::NoInline
) &&
2003 Attrs
.hasAttribute(Attribute::AlwaysInline
)),
2005 "'noinline and alwaysinline' are incompatible!",
2008 Check(!(Attrs
.hasAttribute(Attribute::Writable
) &&
2009 Attrs
.hasAttribute(Attribute::ReadNone
)),
2010 "Attributes writable and readnone are incompatible!", V
);
2012 Check(!(Attrs
.hasAttribute(Attribute::Writable
) &&
2013 Attrs
.hasAttribute(Attribute::ReadOnly
)),
2014 "Attributes writable and readonly are incompatible!", V
);
2016 AttributeMask IncompatibleAttrs
= AttributeFuncs::typeIncompatible(Ty
, Attrs
);
2017 for (Attribute Attr
: Attrs
) {
2018 if (!Attr
.isStringAttribute() &&
2019 IncompatibleAttrs
.contains(Attr
.getKindAsEnum())) {
2020 CheckFailed("Attribute '" + Attr
.getAsString() +
2021 "' applied to incompatible type!", V
);
2026 if (isa
<PointerType
>(Ty
)) {
2027 if (Attrs
.hasAttribute(Attribute::Alignment
)) {
2028 Align AttrAlign
= Attrs
.getAlignment().valueOrOne();
2029 Check(AttrAlign
.value() <= Value::MaximumAlignment
,
2030 "huge alignment values are unsupported", V
);
2032 if (Attrs
.hasAttribute(Attribute::ByVal
)) {
2033 Type
*ByValTy
= Attrs
.getByValType();
2034 SmallPtrSet
<Type
*, 4> Visited
;
2035 Check(ByValTy
->isSized(&Visited
),
2036 "Attribute 'byval' does not support unsized types!", V
);
2037 // Check if it is or contains a target extension type that disallows being
2038 // used on the stack.
2039 Check(!ByValTy
->containsNonLocalTargetExtType(),
2040 "'byval' argument has illegal target extension type", V
);
2041 Check(DL
.getTypeAllocSize(ByValTy
).getKnownMinValue() < (1ULL << 32),
2042 "huge 'byval' arguments are unsupported", V
);
2044 if (Attrs
.hasAttribute(Attribute::ByRef
)) {
2045 SmallPtrSet
<Type
*, 4> Visited
;
2046 Check(Attrs
.getByRefType()->isSized(&Visited
),
2047 "Attribute 'byref' does not support unsized types!", V
);
2048 Check(DL
.getTypeAllocSize(Attrs
.getByRefType()).getKnownMinValue() <
2050 "huge 'byref' arguments are unsupported", V
);
2052 if (Attrs
.hasAttribute(Attribute::InAlloca
)) {
2053 SmallPtrSet
<Type
*, 4> Visited
;
2054 Check(Attrs
.getInAllocaType()->isSized(&Visited
),
2055 "Attribute 'inalloca' does not support unsized types!", V
);
2056 Check(DL
.getTypeAllocSize(Attrs
.getInAllocaType()).getKnownMinValue() <
2058 "huge 'inalloca' arguments are unsupported", V
);
2060 if (Attrs
.hasAttribute(Attribute::Preallocated
)) {
2061 SmallPtrSet
<Type
*, 4> Visited
;
2062 Check(Attrs
.getPreallocatedType()->isSized(&Visited
),
2063 "Attribute 'preallocated' does not support unsized types!", V
);
2065 DL
.getTypeAllocSize(Attrs
.getPreallocatedType()).getKnownMinValue() <
2067 "huge 'preallocated' arguments are unsupported", V
);
2071 if (Attrs
.hasAttribute(Attribute::Initializes
)) {
2072 auto Inits
= Attrs
.getAttribute(Attribute::Initializes
).getInitializes();
2073 Check(!Inits
.empty(), "Attribute 'initializes' does not support empty list",
2075 Check(ConstantRangeList::isOrderedRanges(Inits
),
2076 "Attribute 'initializes' does not support unordered ranges", V
);
2079 if (Attrs
.hasAttribute(Attribute::NoFPClass
)) {
2080 uint64_t Val
= Attrs
.getAttribute(Attribute::NoFPClass
).getValueAsInt();
2081 Check(Val
!= 0, "Attribute 'nofpclass' must have at least one test bit set",
2083 Check((Val
& ~static_cast<unsigned>(fcAllFlags
)) == 0,
2084 "Invalid value for 'nofpclass' test mask", V
);
2086 if (Attrs
.hasAttribute(Attribute::Range
)) {
2087 const ConstantRange
&CR
=
2088 Attrs
.getAttribute(Attribute::Range
).getValueAsConstantRange();
2089 Check(Ty
->isIntOrIntVectorTy(CR
.getBitWidth()),
2090 "Range bit width must match type bit width!", V
);
2094 void Verifier::checkUnsignedBaseTenFuncAttr(AttributeList Attrs
, StringRef Attr
,
2096 if (Attrs
.hasFnAttr(Attr
)) {
2097 StringRef S
= Attrs
.getFnAttr(Attr
).getValueAsString();
2099 if (S
.getAsInteger(10, N
))
2100 CheckFailed("\"" + Attr
+ "\" takes an unsigned integer: " + S
, V
);
2104 // Check parameter attributes against a function type.
2105 // The value V is printed in error messages.
2106 void Verifier::verifyFunctionAttrs(FunctionType
*FT
, AttributeList Attrs
,
2107 const Value
*V
, bool IsIntrinsic
,
2109 if (Attrs
.isEmpty())
2112 if (AttributeListsVisited
.insert(Attrs
.getRawPointer()).second
) {
2113 Check(Attrs
.hasParentContext(Context
),
2114 "Attribute list does not match Module context!", &Attrs
, V
);
2115 for (const auto &AttrSet
: Attrs
) {
2116 Check(!AttrSet
.hasAttributes() || AttrSet
.hasParentContext(Context
),
2117 "Attribute set does not match Module context!", &AttrSet
, V
);
2118 for (const auto &A
: AttrSet
) {
2119 Check(A
.hasParentContext(Context
),
2120 "Attribute does not match Module context!", &A
, V
);
2125 bool SawNest
= false;
2126 bool SawReturned
= false;
2127 bool SawSRet
= false;
2128 bool SawSwiftSelf
= false;
2129 bool SawSwiftAsync
= false;
2130 bool SawSwiftError
= false;
2132 // Verify return value attributes.
2133 AttributeSet RetAttrs
= Attrs
.getRetAttrs();
2134 for (Attribute RetAttr
: RetAttrs
)
2135 Check(RetAttr
.isStringAttribute() ||
2136 Attribute::canUseAsRetAttr(RetAttr
.getKindAsEnum()),
2137 "Attribute '" + RetAttr
.getAsString() +
2138 "' does not apply to function return values",
2141 unsigned MaxParameterWidth
= 0;
2142 auto GetMaxParameterWidth
= [&MaxParameterWidth
](Type
*Ty
) {
2143 if (Ty
->isVectorTy()) {
2144 if (auto *VT
= dyn_cast
<FixedVectorType
>(Ty
)) {
2145 unsigned Size
= VT
->getPrimitiveSizeInBits().getFixedValue();
2146 if (Size
> MaxParameterWidth
)
2147 MaxParameterWidth
= Size
;
2151 GetMaxParameterWidth(FT
->getReturnType());
2152 verifyParameterAttrs(RetAttrs
, FT
->getReturnType(), V
);
2154 // Verify parameter attributes.
2155 for (unsigned i
= 0, e
= FT
->getNumParams(); i
!= e
; ++i
) {
2156 Type
*Ty
= FT
->getParamType(i
);
2157 AttributeSet ArgAttrs
= Attrs
.getParamAttrs(i
);
2160 Check(!ArgAttrs
.hasAttribute(Attribute::ImmArg
),
2161 "immarg attribute only applies to intrinsics", V
);
2163 Check(!ArgAttrs
.hasAttribute(Attribute::ElementType
),
2164 "Attribute 'elementtype' can only be applied to intrinsics"
2169 verifyParameterAttrs(ArgAttrs
, Ty
, V
);
2170 GetMaxParameterWidth(Ty
);
2172 if (ArgAttrs
.hasAttribute(Attribute::Nest
)) {
2173 Check(!SawNest
, "More than one parameter has attribute nest!", V
);
2177 if (ArgAttrs
.hasAttribute(Attribute::Returned
)) {
2178 Check(!SawReturned
, "More than one parameter has attribute returned!", V
);
2179 Check(Ty
->canLosslesslyBitCastTo(FT
->getReturnType()),
2180 "Incompatible argument and return types for 'returned' attribute",
2185 if (ArgAttrs
.hasAttribute(Attribute::StructRet
)) {
2186 Check(!SawSRet
, "Cannot have multiple 'sret' parameters!", V
);
2187 Check(i
== 0 || i
== 1,
2188 "Attribute 'sret' is not on first or second parameter!", V
);
2192 if (ArgAttrs
.hasAttribute(Attribute::SwiftSelf
)) {
2193 Check(!SawSwiftSelf
, "Cannot have multiple 'swiftself' parameters!", V
);
2194 SawSwiftSelf
= true;
2197 if (ArgAttrs
.hasAttribute(Attribute::SwiftAsync
)) {
2198 Check(!SawSwiftAsync
, "Cannot have multiple 'swiftasync' parameters!", V
);
2199 SawSwiftAsync
= true;
2202 if (ArgAttrs
.hasAttribute(Attribute::SwiftError
)) {
2203 Check(!SawSwiftError
, "Cannot have multiple 'swifterror' parameters!", V
);
2204 SawSwiftError
= true;
2207 if (ArgAttrs
.hasAttribute(Attribute::InAlloca
)) {
2208 Check(i
== FT
->getNumParams() - 1,
2209 "inalloca isn't on the last parameter!", V
);
2213 if (!Attrs
.hasFnAttrs())
2216 verifyAttributeTypes(Attrs
.getFnAttrs(), V
);
2217 for (Attribute FnAttr
: Attrs
.getFnAttrs())
2218 Check(FnAttr
.isStringAttribute() ||
2219 Attribute::canUseAsFnAttr(FnAttr
.getKindAsEnum()),
2220 "Attribute '" + FnAttr
.getAsString() +
2221 "' does not apply to functions!",
2224 Check(!(Attrs
.hasFnAttr(Attribute::NoInline
) &&
2225 Attrs
.hasFnAttr(Attribute::AlwaysInline
)),
2226 "Attributes 'noinline and alwaysinline' are incompatible!", V
);
2228 if (Attrs
.hasFnAttr(Attribute::OptimizeNone
)) {
2229 Check(Attrs
.hasFnAttr(Attribute::NoInline
),
2230 "Attribute 'optnone' requires 'noinline'!", V
);
2232 Check(!Attrs
.hasFnAttr(Attribute::OptimizeForSize
),
2233 "Attributes 'optsize and optnone' are incompatible!", V
);
2235 Check(!Attrs
.hasFnAttr(Attribute::MinSize
),
2236 "Attributes 'minsize and optnone' are incompatible!", V
);
2238 Check(!Attrs
.hasFnAttr(Attribute::OptimizeForDebugging
),
2239 "Attributes 'optdebug and optnone' are incompatible!", V
);
2242 Check(!(Attrs
.hasFnAttr(Attribute::SanitizeRealtime
) &&
2243 Attrs
.hasFnAttr(Attribute::SanitizeRealtimeBlocking
)),
2245 "'sanitize_realtime and sanitize_realtime_blocking' are incompatible!",
2248 if (Attrs
.hasFnAttr(Attribute::OptimizeForDebugging
)) {
2249 Check(!Attrs
.hasFnAttr(Attribute::OptimizeForSize
),
2250 "Attributes 'optsize and optdebug' are incompatible!", V
);
2252 Check(!Attrs
.hasFnAttr(Attribute::MinSize
),
2253 "Attributes 'minsize and optdebug' are incompatible!", V
);
2256 Check(!Attrs
.hasAttrSomewhere(Attribute::Writable
) ||
2257 isModSet(Attrs
.getMemoryEffects().getModRef(IRMemLocation::ArgMem
)),
2258 "Attribute writable and memory without argmem: write are incompatible!",
2261 if (Attrs
.hasFnAttr("aarch64_pstate_sm_enabled")) {
2262 Check(!Attrs
.hasFnAttr("aarch64_pstate_sm_compatible"),
2263 "Attributes 'aarch64_pstate_sm_enabled and "
2264 "aarch64_pstate_sm_compatible' are incompatible!",
2268 Check((Attrs
.hasFnAttr("aarch64_new_za") + Attrs
.hasFnAttr("aarch64_in_za") +
2269 Attrs
.hasFnAttr("aarch64_inout_za") +
2270 Attrs
.hasFnAttr("aarch64_out_za") +
2271 Attrs
.hasFnAttr("aarch64_preserves_za") +
2272 Attrs
.hasFnAttr("aarch64_za_state_agnostic")) <= 1,
2273 "Attributes 'aarch64_new_za', 'aarch64_in_za', 'aarch64_out_za', "
2274 "'aarch64_inout_za', 'aarch64_preserves_za' and "
2275 "'aarch64_za_state_agnostic' are mutually exclusive",
2278 Check((Attrs
.hasFnAttr("aarch64_new_zt0") +
2279 Attrs
.hasFnAttr("aarch64_in_zt0") +
2280 Attrs
.hasFnAttr("aarch64_inout_zt0") +
2281 Attrs
.hasFnAttr("aarch64_out_zt0") +
2282 Attrs
.hasFnAttr("aarch64_preserves_zt0") +
2283 Attrs
.hasFnAttr("aarch64_za_state_agnostic")) <= 1,
2284 "Attributes 'aarch64_new_zt0', 'aarch64_in_zt0', 'aarch64_out_zt0', "
2285 "'aarch64_inout_zt0', 'aarch64_preserves_zt0' and "
2286 "'aarch64_za_state_agnostic' are mutually exclusive",
2289 if (Attrs
.hasFnAttr(Attribute::JumpTable
)) {
2290 const GlobalValue
*GV
= cast
<GlobalValue
>(V
);
2291 Check(GV
->hasGlobalUnnamedAddr(),
2292 "Attribute 'jumptable' requires 'unnamed_addr'", V
);
2295 if (auto Args
= Attrs
.getFnAttrs().getAllocSizeArgs()) {
2296 auto CheckParam
= [&](StringRef Name
, unsigned ParamNo
) {
2297 if (ParamNo
>= FT
->getNumParams()) {
2298 CheckFailed("'allocsize' " + Name
+ " argument is out of bounds", V
);
2302 if (!FT
->getParamType(ParamNo
)->isIntegerTy()) {
2303 CheckFailed("'allocsize' " + Name
+
2304 " argument must refer to an integer parameter",
2312 if (!CheckParam("element size", Args
->first
))
2315 if (Args
->second
&& !CheckParam("number of elements", *Args
->second
))
2319 if (Attrs
.hasFnAttr(Attribute::AllocKind
)) {
2320 AllocFnKind K
= Attrs
.getAllocKind();
2322 K
& (AllocFnKind::Alloc
| AllocFnKind::Realloc
| AllocFnKind::Free
);
2324 {AllocFnKind::Alloc
, AllocFnKind::Realloc
, AllocFnKind::Free
},
2327 "'allockind()' requires exactly one of alloc, realloc, and free");
2328 if ((Type
== AllocFnKind::Free
) &&
2329 ((K
& (AllocFnKind::Uninitialized
| AllocFnKind::Zeroed
|
2330 AllocFnKind::Aligned
)) != AllocFnKind::Unknown
))
2331 CheckFailed("'allockind(\"free\")' doesn't allow uninitialized, zeroed, "
2332 "or aligned modifiers.");
2333 AllocFnKind ZeroedUninit
= AllocFnKind::Uninitialized
| AllocFnKind::Zeroed
;
2334 if ((K
& ZeroedUninit
) == ZeroedUninit
)
2335 CheckFailed("'allockind()' can't be both zeroed and uninitialized");
2338 if (Attrs
.hasFnAttr(Attribute::VScaleRange
)) {
2339 unsigned VScaleMin
= Attrs
.getFnAttrs().getVScaleRangeMin();
2341 CheckFailed("'vscale_range' minimum must be greater than 0", V
);
2342 else if (!isPowerOf2_32(VScaleMin
))
2343 CheckFailed("'vscale_range' minimum must be power-of-two value", V
);
2344 std::optional
<unsigned> VScaleMax
= Attrs
.getFnAttrs().getVScaleRangeMax();
2345 if (VScaleMax
&& VScaleMin
> VScaleMax
)
2346 CheckFailed("'vscale_range' minimum cannot be greater than maximum", V
);
2347 else if (VScaleMax
&& !isPowerOf2_32(*VScaleMax
))
2348 CheckFailed("'vscale_range' maximum must be power-of-two value", V
);
2351 if (Attrs
.hasFnAttr("frame-pointer")) {
2352 StringRef FP
= Attrs
.getFnAttr("frame-pointer").getValueAsString();
2353 if (FP
!= "all" && FP
!= "non-leaf" && FP
!= "none" && FP
!= "reserved")
2354 CheckFailed("invalid value for 'frame-pointer' attribute: " + FP
, V
);
2357 // Check EVEX512 feature.
2358 if (MaxParameterWidth
>= 512 && Attrs
.hasFnAttr("target-features") &&
2360 StringRef TF
= Attrs
.getFnAttr("target-features").getValueAsString();
2361 Check(!TF
.contains("+avx512f") || !TF
.contains("-evex512"),
2362 "512-bit vector arguments require 'evex512' for AVX512", V
);
2365 checkUnsignedBaseTenFuncAttr(Attrs
, "patchable-function-prefix", V
);
2366 checkUnsignedBaseTenFuncAttr(Attrs
, "patchable-function-entry", V
);
2367 checkUnsignedBaseTenFuncAttr(Attrs
, "warn-stack-size", V
);
2369 if (auto A
= Attrs
.getFnAttr("sign-return-address"); A
.isValid()) {
2370 StringRef S
= A
.getValueAsString();
2371 if (S
!= "none" && S
!= "all" && S
!= "non-leaf")
2372 CheckFailed("invalid value for 'sign-return-address' attribute: " + S
, V
);
2375 if (auto A
= Attrs
.getFnAttr("sign-return-address-key"); A
.isValid()) {
2376 StringRef S
= A
.getValueAsString();
2377 if (S
!= "a_key" && S
!= "b_key")
2378 CheckFailed("invalid value for 'sign-return-address-key' attribute: " + S
,
2380 if (auto AA
= Attrs
.getFnAttr("sign-return-address"); !AA
.isValid()) {
2382 "'sign-return-address-key' present without `sign-return-address`");
2386 if (auto A
= Attrs
.getFnAttr("branch-target-enforcement"); A
.isValid()) {
2387 StringRef S
= A
.getValueAsString();
2388 if (S
!= "" && S
!= "true" && S
!= "false")
2390 "invalid value for 'branch-target-enforcement' attribute: " + S
, V
);
2393 if (auto A
= Attrs
.getFnAttr("branch-protection-pauth-lr"); A
.isValid()) {
2394 StringRef S
= A
.getValueAsString();
2395 if (S
!= "" && S
!= "true" && S
!= "false")
2397 "invalid value for 'branch-protection-pauth-lr' attribute: " + S
, V
);
2400 if (auto A
= Attrs
.getFnAttr("guarded-control-stack"); A
.isValid()) {
2401 StringRef S
= A
.getValueAsString();
2402 if (S
!= "" && S
!= "true" && S
!= "false")
2403 CheckFailed("invalid value for 'guarded-control-stack' attribute: " + S
,
2407 if (auto A
= Attrs
.getFnAttr("vector-function-abi-variant"); A
.isValid()) {
2408 StringRef S
= A
.getValueAsString();
2409 const std::optional
<VFInfo
> Info
= VFABI::tryDemangleForVFABI(S
, FT
);
2411 CheckFailed("invalid name for a VFABI variant: " + S
, V
);
2414 if (auto A
= Attrs
.getFnAttr("denormal-fp-math"); A
.isValid()) {
2415 StringRef S
= A
.getValueAsString();
2416 if (!parseDenormalFPAttribute(S
).isValid())
2417 CheckFailed("invalid value for 'denormal-fp-math' attribute: " + S
, V
);
2420 if (auto A
= Attrs
.getFnAttr("denormal-fp-math-f32"); A
.isValid()) {
2421 StringRef S
= A
.getValueAsString();
2422 if (!parseDenormalFPAttribute(S
).isValid())
2423 CheckFailed("invalid value for 'denormal-fp-math-f32' attribute: " + S
,
2428 void Verifier::verifyFunctionMetadata(
2429 ArrayRef
<std::pair
<unsigned, MDNode
*>> MDs
) {
2430 for (const auto &Pair
: MDs
) {
2431 if (Pair
.first
== LLVMContext::MD_prof
) {
2432 MDNode
*MD
= Pair
.second
;
2433 Check(MD
->getNumOperands() >= 2,
2434 "!prof annotations should have no less than 2 operands", MD
);
2436 // Check first operand.
2437 Check(MD
->getOperand(0) != nullptr, "first operand should not be null",
2439 Check(isa
<MDString
>(MD
->getOperand(0)),
2440 "expected string with name of the !prof annotation", MD
);
2441 MDString
*MDS
= cast
<MDString
>(MD
->getOperand(0));
2442 StringRef ProfName
= MDS
->getString();
2443 Check(ProfName
== "function_entry_count" ||
2444 ProfName
== "synthetic_function_entry_count",
2445 "first operand should be 'function_entry_count'"
2446 " or 'synthetic_function_entry_count'",
2449 // Check second operand.
2450 Check(MD
->getOperand(1) != nullptr, "second operand should not be null",
2452 Check(isa
<ConstantAsMetadata
>(MD
->getOperand(1)),
2453 "expected integer argument to function_entry_count", MD
);
2454 } else if (Pair
.first
== LLVMContext::MD_kcfi_type
) {
2455 MDNode
*MD
= Pair
.second
;
2456 Check(MD
->getNumOperands() == 1,
2457 "!kcfi_type must have exactly one operand", MD
);
2458 Check(MD
->getOperand(0) != nullptr, "!kcfi_type operand must not be null",
2460 Check(isa
<ConstantAsMetadata
>(MD
->getOperand(0)),
2461 "expected a constant operand for !kcfi_type", MD
);
2462 Constant
*C
= cast
<ConstantAsMetadata
>(MD
->getOperand(0))->getValue();
2463 Check(isa
<ConstantInt
>(C
) && isa
<IntegerType
>(C
->getType()),
2464 "expected a constant integer operand for !kcfi_type", MD
);
2465 Check(cast
<ConstantInt
>(C
)->getBitWidth() == 32,
2466 "expected a 32-bit integer constant operand for !kcfi_type", MD
);
2471 void Verifier::visitConstantExprsRecursively(const Constant
*EntryC
) {
2472 if (!ConstantExprVisited
.insert(EntryC
).second
)
2475 SmallVector
<const Constant
*, 16> Stack
;
2476 Stack
.push_back(EntryC
);
2478 while (!Stack
.empty()) {
2479 const Constant
*C
= Stack
.pop_back_val();
2481 // Check this constant expression.
2482 if (const auto *CE
= dyn_cast
<ConstantExpr
>(C
))
2483 visitConstantExpr(CE
);
2485 if (const auto *CPA
= dyn_cast
<ConstantPtrAuth
>(C
))
2486 visitConstantPtrAuth(CPA
);
2488 if (const auto *GV
= dyn_cast
<GlobalValue
>(C
)) {
2489 // Global Values get visited separately, but we do need to make sure
2490 // that the global value is in the correct module
2491 Check(GV
->getParent() == &M
, "Referencing global in another module!",
2492 EntryC
, &M
, GV
, GV
->getParent());
2496 // Visit all sub-expressions.
2497 for (const Use
&U
: C
->operands()) {
2498 const auto *OpC
= dyn_cast
<Constant
>(U
);
2501 if (!ConstantExprVisited
.insert(OpC
).second
)
2503 Stack
.push_back(OpC
);
2508 void Verifier::visitConstantExpr(const ConstantExpr
*CE
) {
2509 if (CE
->getOpcode() == Instruction::BitCast
)
2510 Check(CastInst::castIsValid(Instruction::BitCast
, CE
->getOperand(0),
2512 "Invalid bitcast", CE
);
2515 void Verifier::visitConstantPtrAuth(const ConstantPtrAuth
*CPA
) {
2516 Check(CPA
->getPointer()->getType()->isPointerTy(),
2517 "signed ptrauth constant base pointer must have pointer type");
2519 Check(CPA
->getType() == CPA
->getPointer()->getType(),
2520 "signed ptrauth constant must have same type as its base pointer");
2522 Check(CPA
->getKey()->getBitWidth() == 32,
2523 "signed ptrauth constant key must be i32 constant integer");
2525 Check(CPA
->getAddrDiscriminator()->getType()->isPointerTy(),
2526 "signed ptrauth constant address discriminator must be a pointer");
2528 Check(CPA
->getDiscriminator()->getBitWidth() == 64,
2529 "signed ptrauth constant discriminator must be i64 constant integer");
2532 bool Verifier::verifyAttributeCount(AttributeList Attrs
, unsigned Params
) {
2533 // There shouldn't be more attribute sets than there are parameters plus the
2534 // function and return value.
2535 return Attrs
.getNumAttrSets() <= Params
+ 2;
2538 void Verifier::verifyInlineAsmCall(const CallBase
&Call
) {
2539 const InlineAsm
*IA
= cast
<InlineAsm
>(Call
.getCalledOperand());
2541 unsigned LabelNo
= 0;
2542 for (const InlineAsm::ConstraintInfo
&CI
: IA
->ParseConstraints()) {
2543 if (CI
.Type
== InlineAsm::isLabel
) {
2548 // Only deal with constraints that correspond to call arguments.
2552 if (CI
.isIndirect
) {
2553 const Value
*Arg
= Call
.getArgOperand(ArgNo
);
2554 Check(Arg
->getType()->isPointerTy(),
2555 "Operand for indirect constraint must have pointer type", &Call
);
2557 Check(Call
.getParamElementType(ArgNo
),
2558 "Operand for indirect constraint must have elementtype attribute",
2561 Check(!Call
.paramHasAttr(ArgNo
, Attribute::ElementType
),
2562 "Elementtype attribute can only be applied for indirect "
2570 if (auto *CallBr
= dyn_cast
<CallBrInst
>(&Call
)) {
2571 Check(LabelNo
== CallBr
->getNumIndirectDests(),
2572 "Number of label constraints does not match number of callbr dests",
2575 Check(LabelNo
== 0, "Label constraints can only be used with callbr",
2580 /// Verify that statepoint intrinsic is well formed.
2581 void Verifier::verifyStatepoint(const CallBase
&Call
) {
2582 assert(Call
.getCalledFunction() &&
2583 Call
.getCalledFunction()->getIntrinsicID() ==
2584 Intrinsic::experimental_gc_statepoint
);
2586 Check(!Call
.doesNotAccessMemory() && !Call
.onlyReadsMemory() &&
2587 !Call
.onlyAccessesArgMemory(),
2588 "gc.statepoint must read and write all memory to preserve "
2589 "reordering restrictions required by safepoint semantics",
2592 const int64_t NumPatchBytes
=
2593 cast
<ConstantInt
>(Call
.getArgOperand(1))->getSExtValue();
2594 assert(isInt
<32>(NumPatchBytes
) && "NumPatchBytesV is an i32!");
2595 Check(NumPatchBytes
>= 0,
2596 "gc.statepoint number of patchable bytes must be "
2600 Type
*TargetElemType
= Call
.getParamElementType(2);
2601 Check(TargetElemType
,
2602 "gc.statepoint callee argument must have elementtype attribute", Call
);
2603 FunctionType
*TargetFuncType
= dyn_cast
<FunctionType
>(TargetElemType
);
2604 Check(TargetFuncType
,
2605 "gc.statepoint callee elementtype must be function type", Call
);
2607 const int NumCallArgs
= cast
<ConstantInt
>(Call
.getArgOperand(3))->getZExtValue();
2608 Check(NumCallArgs
>= 0,
2609 "gc.statepoint number of arguments to underlying call "
2612 const int NumParams
= (int)TargetFuncType
->getNumParams();
2613 if (TargetFuncType
->isVarArg()) {
2614 Check(NumCallArgs
>= NumParams
,
2615 "gc.statepoint mismatch in number of vararg call args", Call
);
2617 // TODO: Remove this limitation
2618 Check(TargetFuncType
->getReturnType()->isVoidTy(),
2619 "gc.statepoint doesn't support wrapping non-void "
2620 "vararg functions yet",
2623 Check(NumCallArgs
== NumParams
,
2624 "gc.statepoint mismatch in number of call args", Call
);
2626 const uint64_t Flags
2627 = cast
<ConstantInt
>(Call
.getArgOperand(4))->getZExtValue();
2628 Check((Flags
& ~(uint64_t)StatepointFlags::MaskAll
) == 0,
2629 "unknown flag used in gc.statepoint flags argument", Call
);
2631 // Verify that the types of the call parameter arguments match
2632 // the type of the wrapped callee.
2633 AttributeList Attrs
= Call
.getAttributes();
2634 for (int i
= 0; i
< NumParams
; i
++) {
2635 Type
*ParamType
= TargetFuncType
->getParamType(i
);
2636 Type
*ArgType
= Call
.getArgOperand(5 + i
)->getType();
2637 Check(ArgType
== ParamType
,
2638 "gc.statepoint call argument does not match wrapped "
2642 if (TargetFuncType
->isVarArg()) {
2643 AttributeSet ArgAttrs
= Attrs
.getParamAttrs(5 + i
);
2644 Check(!ArgAttrs
.hasAttribute(Attribute::StructRet
),
2645 "Attribute 'sret' cannot be used for vararg call arguments!", Call
);
2649 const int EndCallArgsInx
= 4 + NumCallArgs
;
2651 const Value
*NumTransitionArgsV
= Call
.getArgOperand(EndCallArgsInx
+ 1);
2652 Check(isa
<ConstantInt
>(NumTransitionArgsV
),
2653 "gc.statepoint number of transition arguments "
2654 "must be constant integer",
2656 const int NumTransitionArgs
=
2657 cast
<ConstantInt
>(NumTransitionArgsV
)->getZExtValue();
2658 Check(NumTransitionArgs
== 0,
2659 "gc.statepoint w/inline transition bundle is deprecated", Call
);
2660 const int EndTransitionArgsInx
= EndCallArgsInx
+ 1 + NumTransitionArgs
;
2662 const Value
*NumDeoptArgsV
= Call
.getArgOperand(EndTransitionArgsInx
+ 1);
2663 Check(isa
<ConstantInt
>(NumDeoptArgsV
),
2664 "gc.statepoint number of deoptimization arguments "
2665 "must be constant integer",
2667 const int NumDeoptArgs
= cast
<ConstantInt
>(NumDeoptArgsV
)->getZExtValue();
2668 Check(NumDeoptArgs
== 0,
2669 "gc.statepoint w/inline deopt operands is deprecated", Call
);
2671 const int ExpectedNumArgs
= 7 + NumCallArgs
;
2672 Check(ExpectedNumArgs
== (int)Call
.arg_size(),
2673 "gc.statepoint too many arguments", Call
);
2675 // Check that the only uses of this gc.statepoint are gc.result or
2676 // gc.relocate calls which are tied to this statepoint and thus part
2677 // of the same statepoint sequence
2678 for (const User
*U
: Call
.users()) {
2679 const CallInst
*UserCall
= dyn_cast
<const CallInst
>(U
);
2680 Check(UserCall
, "illegal use of statepoint token", Call
, U
);
2683 Check(isa
<GCRelocateInst
>(UserCall
) || isa
<GCResultInst
>(UserCall
),
2684 "gc.result or gc.relocate are the only value uses "
2685 "of a gc.statepoint",
2687 if (isa
<GCResultInst
>(UserCall
)) {
2688 Check(UserCall
->getArgOperand(0) == &Call
,
2689 "gc.result connected to wrong gc.statepoint", Call
, UserCall
);
2690 } else if (isa
<GCRelocateInst
>(Call
)) {
2691 Check(UserCall
->getArgOperand(0) == &Call
,
2692 "gc.relocate connected to wrong gc.statepoint", Call
, UserCall
);
2696 // Note: It is legal for a single derived pointer to be listed multiple
2697 // times. It's non-optimal, but it is legal. It can also happen after
2698 // insertion if we strip a bitcast away.
2699 // Note: It is really tempting to check that each base is relocated and
2700 // that a derived pointer is never reused as a base pointer. This turns
2701 // out to be problematic since optimizations run after safepoint insertion
2702 // can recognize equality properties that the insertion logic doesn't know
2703 // about. See example statepoint.ll in the verifier subdirectory
2706 void Verifier::verifyFrameRecoverIndices() {
2707 for (auto &Counts
: FrameEscapeInfo
) {
2708 Function
*F
= Counts
.first
;
2709 unsigned EscapedObjectCount
= Counts
.second
.first
;
2710 unsigned MaxRecoveredIndex
= Counts
.second
.second
;
2711 Check(MaxRecoveredIndex
<= EscapedObjectCount
,
2712 "all indices passed to llvm.localrecover must be less than the "
2713 "number of arguments passed to llvm.localescape in the parent "
2719 static Instruction
*getSuccPad(Instruction
*Terminator
) {
2720 BasicBlock
*UnwindDest
;
2721 if (auto *II
= dyn_cast
<InvokeInst
>(Terminator
))
2722 UnwindDest
= II
->getUnwindDest();
2723 else if (auto *CSI
= dyn_cast
<CatchSwitchInst
>(Terminator
))
2724 UnwindDest
= CSI
->getUnwindDest();
2726 UnwindDest
= cast
<CleanupReturnInst
>(Terminator
)->getUnwindDest();
2727 return UnwindDest
->getFirstNonPHI();
2730 void Verifier::verifySiblingFuncletUnwinds() {
2731 SmallPtrSet
<Instruction
*, 8> Visited
;
2732 SmallPtrSet
<Instruction
*, 8> Active
;
2733 for (const auto &Pair
: SiblingFuncletInfo
) {
2734 Instruction
*PredPad
= Pair
.first
;
2735 if (Visited
.count(PredPad
))
2737 Active
.insert(PredPad
);
2738 Instruction
*Terminator
= Pair
.second
;
2740 Instruction
*SuccPad
= getSuccPad(Terminator
);
2741 if (Active
.count(SuccPad
)) {
2742 // Found a cycle; report error
2743 Instruction
*CyclePad
= SuccPad
;
2744 SmallVector
<Instruction
*, 8> CycleNodes
;
2746 CycleNodes
.push_back(CyclePad
);
2747 Instruction
*CycleTerminator
= SiblingFuncletInfo
[CyclePad
];
2748 if (CycleTerminator
!= CyclePad
)
2749 CycleNodes
.push_back(CycleTerminator
);
2750 CyclePad
= getSuccPad(CycleTerminator
);
2751 } while (CyclePad
!= SuccPad
);
2752 Check(false, "EH pads can't handle each other's exceptions",
2753 ArrayRef
<Instruction
*>(CycleNodes
));
2755 // Don't re-walk a node we've already checked
2756 if (!Visited
.insert(SuccPad
).second
)
2758 // Walk to this successor if it has a map entry.
2760 auto TermI
= SiblingFuncletInfo
.find(PredPad
);
2761 if (TermI
== SiblingFuncletInfo
.end())
2763 Terminator
= TermI
->second
;
2764 Active
.insert(PredPad
);
2766 // Each node only has one successor, so we've walked all the active
2767 // nodes' successors.
2772 // visitFunction - Verify that a function is ok.
2774 void Verifier::visitFunction(const Function
&F
) {
2775 visitGlobalValue(F
);
2777 // Check function arguments.
2778 FunctionType
*FT
= F
.getFunctionType();
2779 unsigned NumArgs
= F
.arg_size();
2781 Check(&Context
== &F
.getContext(),
2782 "Function context does not match Module context!", &F
);
2784 Check(!F
.hasCommonLinkage(), "Functions may not have common linkage", &F
);
2785 Check(FT
->getNumParams() == NumArgs
,
2786 "# formal arguments must match # of arguments for function type!", &F
,
2788 Check(F
.getReturnType()->isFirstClassType() ||
2789 F
.getReturnType()->isVoidTy() || F
.getReturnType()->isStructTy(),
2790 "Functions cannot return aggregate values!", &F
);
2792 Check(!F
.hasStructRetAttr() || F
.getReturnType()->isVoidTy(),
2793 "Invalid struct return type!", &F
);
2795 AttributeList Attrs
= F
.getAttributes();
2797 Check(verifyAttributeCount(Attrs
, FT
->getNumParams()),
2798 "Attribute after last parameter!", &F
);
2800 CheckDI(F
.IsNewDbgInfoFormat
== F
.getParent()->IsNewDbgInfoFormat
,
2801 "Function debug format should match parent module", &F
,
2802 F
.IsNewDbgInfoFormat
, F
.getParent(),
2803 F
.getParent()->IsNewDbgInfoFormat
);
2805 bool IsIntrinsic
= F
.isIntrinsic();
2807 // Check function attributes.
2808 verifyFunctionAttrs(FT
, Attrs
, &F
, IsIntrinsic
, /* IsInlineAsm */ false);
2810 // On function declarations/definitions, we do not support the builtin
2811 // attribute. We do not check this in VerifyFunctionAttrs since that is
2812 // checking for Attributes that can/can not ever be on functions.
2813 Check(!Attrs
.hasFnAttr(Attribute::Builtin
),
2814 "Attribute 'builtin' can only be applied to a callsite.", &F
);
2816 Check(!Attrs
.hasAttrSomewhere(Attribute::ElementType
),
2817 "Attribute 'elementtype' can only be applied to a callsite.", &F
);
2819 if (Attrs
.hasFnAttr(Attribute::Naked
))
2820 for (const Argument
&Arg
: F
.args())
2821 Check(Arg
.use_empty(), "cannot use argument of naked function", &Arg
);
2823 // Check that this function meets the restrictions on this calling convention.
2824 // Sometimes varargs is used for perfectly forwarding thunks, so some of these
2825 // restrictions can be lifted.
2826 switch (F
.getCallingConv()) {
2828 case CallingConv::C
:
2830 case CallingConv::X86_INTR
: {
2831 Check(F
.arg_empty() || Attrs
.hasParamAttr(0, Attribute::ByVal
),
2832 "Calling convention parameter requires byval", &F
);
2835 case CallingConv::AMDGPU_KERNEL
:
2836 case CallingConv::SPIR_KERNEL
:
2837 case CallingConv::AMDGPU_CS_Chain
:
2838 case CallingConv::AMDGPU_CS_ChainPreserve
:
2839 Check(F
.getReturnType()->isVoidTy(),
2840 "Calling convention requires void return type", &F
);
2842 case CallingConv::AMDGPU_VS
:
2843 case CallingConv::AMDGPU_HS
:
2844 case CallingConv::AMDGPU_GS
:
2845 case CallingConv::AMDGPU_PS
:
2846 case CallingConv::AMDGPU_CS
:
2847 Check(!F
.hasStructRetAttr(), "Calling convention does not allow sret", &F
);
2848 if (F
.getCallingConv() != CallingConv::SPIR_KERNEL
) {
2849 const unsigned StackAS
= DL
.getAllocaAddrSpace();
2851 for (const Argument
&Arg
: F
.args()) {
2852 Check(!Attrs
.hasParamAttr(i
, Attribute::ByVal
),
2853 "Calling convention disallows byval", &F
);
2854 Check(!Attrs
.hasParamAttr(i
, Attribute::Preallocated
),
2855 "Calling convention disallows preallocated", &F
);
2856 Check(!Attrs
.hasParamAttr(i
, Attribute::InAlloca
),
2857 "Calling convention disallows inalloca", &F
);
2859 if (Attrs
.hasParamAttr(i
, Attribute::ByRef
)) {
2860 // FIXME: Should also disallow LDS and GDS, but we don't have the enum
2862 Check(Arg
.getType()->getPointerAddressSpace() != StackAS
,
2863 "Calling convention disallows stack byref", &F
);
2871 case CallingConv::Fast
:
2872 case CallingConv::Cold
:
2873 case CallingConv::Intel_OCL_BI
:
2874 case CallingConv::PTX_Kernel
:
2875 case CallingConv::PTX_Device
:
2876 Check(!F
.isVarArg(),
2877 "Calling convention does not support varargs or "
2878 "perfect forwarding!",
2883 // Check that the argument values match the function type for this function...
2885 for (const Argument
&Arg
: F
.args()) {
2886 Check(Arg
.getType() == FT
->getParamType(i
),
2887 "Argument value does not match function argument type!", &Arg
,
2888 FT
->getParamType(i
));
2889 Check(Arg
.getType()->isFirstClassType(),
2890 "Function arguments must have first-class types!", &Arg
);
2892 Check(!Arg
.getType()->isMetadataTy(),
2893 "Function takes metadata but isn't an intrinsic", &Arg
, &F
);
2894 Check(!Arg
.getType()->isTokenTy(),
2895 "Function takes token but isn't an intrinsic", &Arg
, &F
);
2896 Check(!Arg
.getType()->isX86_AMXTy(),
2897 "Function takes x86_amx but isn't an intrinsic", &Arg
, &F
);
2900 // Check that swifterror argument is only used by loads and stores.
2901 if (Attrs
.hasParamAttr(i
, Attribute::SwiftError
)) {
2902 verifySwiftErrorValue(&Arg
);
2908 Check(!F
.getReturnType()->isTokenTy(),
2909 "Function returns a token but isn't an intrinsic", &F
);
2910 Check(!F
.getReturnType()->isX86_AMXTy(),
2911 "Function returns a x86_amx but isn't an intrinsic", &F
);
2914 // Get the function metadata attachments.
2915 SmallVector
<std::pair
<unsigned, MDNode
*>, 4> MDs
;
2916 F
.getAllMetadata(MDs
);
2917 assert(F
.hasMetadata() != MDs
.empty() && "Bit out-of-sync");
2918 verifyFunctionMetadata(MDs
);
2920 // Check validity of the personality function
2921 if (F
.hasPersonalityFn()) {
2922 auto *Per
= dyn_cast
<Function
>(F
.getPersonalityFn()->stripPointerCasts());
2924 Check(Per
->getParent() == F
.getParent(),
2925 "Referencing personality function in another module!", &F
,
2926 F
.getParent(), Per
, Per
->getParent());
2929 // EH funclet coloring can be expensive, recompute on-demand
2930 BlockEHFuncletColors
.clear();
2932 if (F
.isMaterializable()) {
2933 // Function has a body somewhere we can't see.
2934 Check(MDs
.empty(), "unmaterialized function cannot have metadata", &F
,
2935 MDs
.empty() ? nullptr : MDs
.front().second
);
2936 } else if (F
.isDeclaration()) {
2937 for (const auto &I
: MDs
) {
2938 // This is used for call site debug information.
2939 CheckDI(I
.first
!= LLVMContext::MD_dbg
||
2940 !cast
<DISubprogram
>(I
.second
)->isDistinct(),
2941 "function declaration may only have a unique !dbg attachment",
2943 Check(I
.first
!= LLVMContext::MD_prof
,
2944 "function declaration may not have a !prof attachment", &F
);
2946 // Verify the metadata itself.
2947 visitMDNode(*I
.second
, AreDebugLocsAllowed::Yes
);
2949 Check(!F
.hasPersonalityFn(),
2950 "Function declaration shouldn't have a personality routine", &F
);
2952 // Verify that this function (which has a body) is not named "llvm.*". It
2953 // is not legal to define intrinsics.
2954 Check(!IsIntrinsic
, "llvm intrinsics cannot be defined!", &F
);
2956 // Check the entry node
2957 const BasicBlock
*Entry
= &F
.getEntryBlock();
2958 Check(pred_empty(Entry
),
2959 "Entry block to function must not have predecessors!", Entry
);
2961 // The address of the entry block cannot be taken, unless it is dead.
2962 if (Entry
->hasAddressTaken()) {
2963 Check(!BlockAddress::lookup(Entry
)->isConstantUsed(),
2964 "blockaddress may not be used with the entry block!", Entry
);
2967 unsigned NumDebugAttachments
= 0, NumProfAttachments
= 0,
2968 NumKCFIAttachments
= 0;
2969 // Visit metadata attachments.
2970 for (const auto &I
: MDs
) {
2971 // Verify that the attachment is legal.
2972 auto AllowLocs
= AreDebugLocsAllowed::No
;
2976 case LLVMContext::MD_dbg
: {
2977 ++NumDebugAttachments
;
2978 CheckDI(NumDebugAttachments
== 1,
2979 "function must have a single !dbg attachment", &F
, I
.second
);
2980 CheckDI(isa
<DISubprogram
>(I
.second
),
2981 "function !dbg attachment must be a subprogram", &F
, I
.second
);
2982 CheckDI(cast
<DISubprogram
>(I
.second
)->isDistinct(),
2983 "function definition may only have a distinct !dbg attachment",
2986 auto *SP
= cast
<DISubprogram
>(I
.second
);
2987 const Function
*&AttachedTo
= DISubprogramAttachments
[SP
];
2988 CheckDI(!AttachedTo
|| AttachedTo
== &F
,
2989 "DISubprogram attached to more than one function", SP
, &F
);
2991 AllowLocs
= AreDebugLocsAllowed::Yes
;
2994 case LLVMContext::MD_prof
:
2995 ++NumProfAttachments
;
2996 Check(NumProfAttachments
== 1,
2997 "function must have a single !prof attachment", &F
, I
.second
);
2999 case LLVMContext::MD_kcfi_type
:
3000 ++NumKCFIAttachments
;
3001 Check(NumKCFIAttachments
== 1,
3002 "function must have a single !kcfi_type attachment", &F
,
3007 // Verify the metadata itself.
3008 visitMDNode(*I
.second
, AllowLocs
);
3012 // If this function is actually an intrinsic, verify that it is only used in
3013 // direct call/invokes, never having its "address taken".
3014 // Only do this if the module is materialized, otherwise we don't have all the
3016 if (F
.isIntrinsic() && F
.getParent()->isMaterialized()) {
3018 if (F
.hasAddressTaken(&U
, false, true, false,
3019 /*IgnoreARCAttachedCall=*/true))
3020 Check(false, "Invalid user of intrinsic instruction!", U
);
3023 // Check intrinsics' signatures.
3024 switch (F
.getIntrinsicID()) {
3025 case Intrinsic::experimental_gc_get_pointer_base
: {
3026 FunctionType
*FT
= F
.getFunctionType();
3027 Check(FT
->getNumParams() == 1, "wrong number of parameters", F
);
3028 Check(isa
<PointerType
>(F
.getReturnType()),
3029 "gc.get.pointer.base must return a pointer", F
);
3030 Check(FT
->getParamType(0) == F
.getReturnType(),
3031 "gc.get.pointer.base operand and result must be of the same type", F
);
3034 case Intrinsic::experimental_gc_get_pointer_offset
: {
3035 FunctionType
*FT
= F
.getFunctionType();
3036 Check(FT
->getNumParams() == 1, "wrong number of parameters", F
);
3037 Check(isa
<PointerType
>(FT
->getParamType(0)),
3038 "gc.get.pointer.offset operand must be a pointer", F
);
3039 Check(F
.getReturnType()->isIntegerTy(),
3040 "gc.get.pointer.offset must return integer", F
);
3045 auto *N
= F
.getSubprogram();
3046 HasDebugInfo
= (N
!= nullptr);
3050 // Check that all !dbg attachments lead to back to N.
3052 // FIXME: Check this incrementally while visiting !dbg attachments.
3053 // FIXME: Only check when N is the canonical subprogram for F.
3054 SmallPtrSet
<const MDNode
*, 32> Seen
;
3055 auto VisitDebugLoc
= [&](const Instruction
&I
, const MDNode
*Node
) {
3056 // Be careful about using DILocation here since we might be dealing with
3057 // broken code (this is the Verifier after all).
3058 const DILocation
*DL
= dyn_cast_or_null
<DILocation
>(Node
);
3061 if (!Seen
.insert(DL
).second
)
3064 Metadata
*Parent
= DL
->getRawScope();
3065 CheckDI(Parent
&& isa
<DILocalScope
>(Parent
),
3066 "DILocation's scope must be a DILocalScope", N
, &F
, &I
, DL
, Parent
);
3068 DILocalScope
*Scope
= DL
->getInlinedAtScope();
3069 Check(Scope
, "Failed to find DILocalScope", DL
);
3071 if (!Seen
.insert(Scope
).second
)
3074 DISubprogram
*SP
= Scope
->getSubprogram();
3076 // Scope and SP could be the same MDNode and we don't want to skip
3077 // validation in that case
3078 if (SP
&& ((Scope
!= SP
) && !Seen
.insert(SP
).second
))
3081 CheckDI(SP
->describes(&F
),
3082 "!dbg attachment points at wrong subprogram for function", N
, &F
,
3086 for (auto &I
: BB
) {
3087 VisitDebugLoc(I
, I
.getDebugLoc().getAsMDNode());
3088 // The llvm.loop annotations also contain two DILocations.
3089 if (auto MD
= I
.getMetadata(LLVMContext::MD_loop
))
3090 for (unsigned i
= 1; i
< MD
->getNumOperands(); ++i
)
3091 VisitDebugLoc(I
, dyn_cast_or_null
<MDNode
>(MD
->getOperand(i
)));
3092 if (BrokenDebugInfo
)
3097 // verifyBasicBlock - Verify that a basic block is well formed...
3099 void Verifier::visitBasicBlock(BasicBlock
&BB
) {
3100 InstsInThisBlock
.clear();
3101 ConvergenceVerifyHelper
.visit(BB
);
3103 // Ensure that basic blocks have terminators!
3104 Check(BB
.getTerminator(), "Basic Block does not have terminator!", &BB
);
3106 // Check constraints that this basic block imposes on all of the PHI nodes in
3108 if (isa
<PHINode
>(BB
.front())) {
3109 SmallVector
<BasicBlock
*, 8> Preds(predecessors(&BB
));
3110 SmallVector
<std::pair
<BasicBlock
*, Value
*>, 8> Values
;
3112 for (const PHINode
&PN
: BB
.phis()) {
3113 Check(PN
.getNumIncomingValues() == Preds
.size(),
3114 "PHINode should have one entry for each predecessor of its "
3115 "parent basic block!",
3118 // Get and sort all incoming values in the PHI node...
3120 Values
.reserve(PN
.getNumIncomingValues());
3121 for (unsigned i
= 0, e
= PN
.getNumIncomingValues(); i
!= e
; ++i
)
3123 std::make_pair(PN
.getIncomingBlock(i
), PN
.getIncomingValue(i
)));
3126 for (unsigned i
= 0, e
= Values
.size(); i
!= e
; ++i
) {
3127 // Check to make sure that if there is more than one entry for a
3128 // particular basic block in this PHI node, that the incoming values are
3131 Check(i
== 0 || Values
[i
].first
!= Values
[i
- 1].first
||
3132 Values
[i
].second
== Values
[i
- 1].second
,
3133 "PHI node has multiple entries for the same basic block with "
3134 "different incoming values!",
3135 &PN
, Values
[i
].first
, Values
[i
].second
, Values
[i
- 1].second
);
3137 // Check to make sure that the predecessors and PHI node entries are
3139 Check(Values
[i
].first
== Preds
[i
],
3140 "PHI node entries do not match predecessors!", &PN
,
3141 Values
[i
].first
, Preds
[i
]);
3146 // Check that all instructions have their parent pointers set up correctly.
3149 Check(I
.getParent() == &BB
, "Instruction has bogus parent pointer!");
3152 CheckDI(BB
.IsNewDbgInfoFormat
== BB
.getParent()->IsNewDbgInfoFormat
,
3153 "BB debug format should match parent function", &BB
,
3154 BB
.IsNewDbgInfoFormat
, BB
.getParent(),
3155 BB
.getParent()->IsNewDbgInfoFormat
);
3157 // Confirm that no issues arise from the debug program.
3158 if (BB
.IsNewDbgInfoFormat
)
3159 CheckDI(!BB
.getTrailingDbgRecords(), "Basic Block has trailing DbgRecords!",
3163 void Verifier::visitTerminator(Instruction
&I
) {
3164 // Ensure that terminators only exist at the end of the basic block.
3165 Check(&I
== I
.getParent()->getTerminator(),
3166 "Terminator found in the middle of a basic block!", I
.getParent());
3167 visitInstruction(I
);
3170 void Verifier::visitBranchInst(BranchInst
&BI
) {
3171 if (BI
.isConditional()) {
3172 Check(BI
.getCondition()->getType()->isIntegerTy(1),
3173 "Branch condition is not 'i1' type!", &BI
, BI
.getCondition());
3175 visitTerminator(BI
);
3178 void Verifier::visitReturnInst(ReturnInst
&RI
) {
3179 Function
*F
= RI
.getParent()->getParent();
3180 unsigned N
= RI
.getNumOperands();
3181 if (F
->getReturnType()->isVoidTy())
3183 "Found return instr that returns non-void in Function of void "
3185 &RI
, F
->getReturnType());
3187 Check(N
== 1 && F
->getReturnType() == RI
.getOperand(0)->getType(),
3188 "Function return type does not match operand "
3189 "type of return inst!",
3190 &RI
, F
->getReturnType());
3192 // Check to make sure that the return value has necessary properties for
3194 visitTerminator(RI
);
3197 void Verifier::visitSwitchInst(SwitchInst
&SI
) {
3198 Check(SI
.getType()->isVoidTy(), "Switch must have void result type!", &SI
);
3199 // Check to make sure that all of the constants in the switch instruction
3200 // have the same type as the switched-on value.
3201 Type
*SwitchTy
= SI
.getCondition()->getType();
3202 SmallPtrSet
<ConstantInt
*, 32> Constants
;
3203 for (auto &Case
: SI
.cases()) {
3204 Check(isa
<ConstantInt
>(SI
.getOperand(Case
.getCaseIndex() * 2 + 2)),
3205 "Case value is not a constant integer.", &SI
);
3206 Check(Case
.getCaseValue()->getType() == SwitchTy
,
3207 "Switch constants must all be same type as switch value!", &SI
);
3208 Check(Constants
.insert(Case
.getCaseValue()).second
,
3209 "Duplicate integer as switch case", &SI
, Case
.getCaseValue());
3212 visitTerminator(SI
);
3215 void Verifier::visitIndirectBrInst(IndirectBrInst
&BI
) {
3216 Check(BI
.getAddress()->getType()->isPointerTy(),
3217 "Indirectbr operand must have pointer type!", &BI
);
3218 for (unsigned i
= 0, e
= BI
.getNumDestinations(); i
!= e
; ++i
)
3219 Check(BI
.getDestination(i
)->getType()->isLabelTy(),
3220 "Indirectbr destinations must all have pointer type!", &BI
);
3222 visitTerminator(BI
);
3225 void Verifier::visitCallBrInst(CallBrInst
&CBI
) {
3226 Check(CBI
.isInlineAsm(), "Callbr is currently only used for asm-goto!", &CBI
);
3227 const InlineAsm
*IA
= cast
<InlineAsm
>(CBI
.getCalledOperand());
3228 Check(!IA
->canThrow(), "Unwinding from Callbr is not allowed");
3230 verifyInlineAsmCall(CBI
);
3231 visitTerminator(CBI
);
3234 void Verifier::visitSelectInst(SelectInst
&SI
) {
3235 Check(!SelectInst::areInvalidOperands(SI
.getOperand(0), SI
.getOperand(1),
3237 "Invalid operands for select instruction!", &SI
);
3239 Check(SI
.getTrueValue()->getType() == SI
.getType(),
3240 "Select values must have same type as select instruction!", &SI
);
3241 visitInstruction(SI
);
3244 /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
3245 /// a pass, if any exist, it's an error.
3247 void Verifier::visitUserOp1(Instruction
&I
) {
3248 Check(false, "User-defined operators should not live outside of a pass!", &I
);
3251 void Verifier::visitTruncInst(TruncInst
&I
) {
3252 // Get the source and destination types
3253 Type
*SrcTy
= I
.getOperand(0)->getType();
3254 Type
*DestTy
= I
.getType();
3256 // Get the size of the types in bits, we'll need this later
3257 unsigned SrcBitSize
= SrcTy
->getScalarSizeInBits();
3258 unsigned DestBitSize
= DestTy
->getScalarSizeInBits();
3260 Check(SrcTy
->isIntOrIntVectorTy(), "Trunc only operates on integer", &I
);
3261 Check(DestTy
->isIntOrIntVectorTy(), "Trunc only produces integer", &I
);
3262 Check(SrcTy
->isVectorTy() == DestTy
->isVectorTy(),
3263 "trunc source and destination must both be a vector or neither", &I
);
3264 Check(SrcBitSize
> DestBitSize
, "DestTy too big for Trunc", &I
);
3266 visitInstruction(I
);
3269 void Verifier::visitZExtInst(ZExtInst
&I
) {
3270 // Get the source and destination types
3271 Type
*SrcTy
= I
.getOperand(0)->getType();
3272 Type
*DestTy
= I
.getType();
3274 // Get the size of the types in bits, we'll need this later
3275 Check(SrcTy
->isIntOrIntVectorTy(), "ZExt only operates on integer", &I
);
3276 Check(DestTy
->isIntOrIntVectorTy(), "ZExt only produces an integer", &I
);
3277 Check(SrcTy
->isVectorTy() == DestTy
->isVectorTy(),
3278 "zext source and destination must both be a vector or neither", &I
);
3279 unsigned SrcBitSize
= SrcTy
->getScalarSizeInBits();
3280 unsigned DestBitSize
= DestTy
->getScalarSizeInBits();
3282 Check(SrcBitSize
< DestBitSize
, "Type too small for ZExt", &I
);
3284 visitInstruction(I
);
3287 void Verifier::visitSExtInst(SExtInst
&I
) {
3288 // Get the source and destination types
3289 Type
*SrcTy
= I
.getOperand(0)->getType();
3290 Type
*DestTy
= I
.getType();
3292 // Get the size of the types in bits, we'll need this later
3293 unsigned SrcBitSize
= SrcTy
->getScalarSizeInBits();
3294 unsigned DestBitSize
= DestTy
->getScalarSizeInBits();
3296 Check(SrcTy
->isIntOrIntVectorTy(), "SExt only operates on integer", &I
);
3297 Check(DestTy
->isIntOrIntVectorTy(), "SExt only produces an integer", &I
);
3298 Check(SrcTy
->isVectorTy() == DestTy
->isVectorTy(),
3299 "sext source and destination must both be a vector or neither", &I
);
3300 Check(SrcBitSize
< DestBitSize
, "Type too small for SExt", &I
);
3302 visitInstruction(I
);
3305 void Verifier::visitFPTruncInst(FPTruncInst
&I
) {
3306 // Get the source and destination types
3307 Type
*SrcTy
= I
.getOperand(0)->getType();
3308 Type
*DestTy
= I
.getType();
3309 // Get the size of the types in bits, we'll need this later
3310 unsigned SrcBitSize
= SrcTy
->getScalarSizeInBits();
3311 unsigned DestBitSize
= DestTy
->getScalarSizeInBits();
3313 Check(SrcTy
->isFPOrFPVectorTy(), "FPTrunc only operates on FP", &I
);
3314 Check(DestTy
->isFPOrFPVectorTy(), "FPTrunc only produces an FP", &I
);
3315 Check(SrcTy
->isVectorTy() == DestTy
->isVectorTy(),
3316 "fptrunc source and destination must both be a vector or neither", &I
);
3317 Check(SrcBitSize
> DestBitSize
, "DestTy too big for FPTrunc", &I
);
3319 visitInstruction(I
);
3322 void Verifier::visitFPExtInst(FPExtInst
&I
) {
3323 // Get the source and destination types
3324 Type
*SrcTy
= I
.getOperand(0)->getType();
3325 Type
*DestTy
= I
.getType();
3327 // Get the size of the types in bits, we'll need this later
3328 unsigned SrcBitSize
= SrcTy
->getScalarSizeInBits();
3329 unsigned DestBitSize
= DestTy
->getScalarSizeInBits();
3331 Check(SrcTy
->isFPOrFPVectorTy(), "FPExt only operates on FP", &I
);
3332 Check(DestTy
->isFPOrFPVectorTy(), "FPExt only produces an FP", &I
);
3333 Check(SrcTy
->isVectorTy() == DestTy
->isVectorTy(),
3334 "fpext source and destination must both be a vector or neither", &I
);
3335 Check(SrcBitSize
< DestBitSize
, "DestTy too small for FPExt", &I
);
3337 visitInstruction(I
);
3340 void Verifier::visitUIToFPInst(UIToFPInst
&I
) {
3341 // Get the source and destination types
3342 Type
*SrcTy
= I
.getOperand(0)->getType();
3343 Type
*DestTy
= I
.getType();
3345 bool SrcVec
= SrcTy
->isVectorTy();
3346 bool DstVec
= DestTy
->isVectorTy();
3348 Check(SrcVec
== DstVec
,
3349 "UIToFP source and dest must both be vector or scalar", &I
);
3350 Check(SrcTy
->isIntOrIntVectorTy(),
3351 "UIToFP source must be integer or integer vector", &I
);
3352 Check(DestTy
->isFPOrFPVectorTy(), "UIToFP result must be FP or FP vector",
3355 if (SrcVec
&& DstVec
)
3356 Check(cast
<VectorType
>(SrcTy
)->getElementCount() ==
3357 cast
<VectorType
>(DestTy
)->getElementCount(),
3358 "UIToFP source and dest vector length mismatch", &I
);
3360 visitInstruction(I
);
3363 void Verifier::visitSIToFPInst(SIToFPInst
&I
) {
3364 // Get the source and destination types
3365 Type
*SrcTy
= I
.getOperand(0)->getType();
3366 Type
*DestTy
= I
.getType();
3368 bool SrcVec
= SrcTy
->isVectorTy();
3369 bool DstVec
= DestTy
->isVectorTy();
3371 Check(SrcVec
== DstVec
,
3372 "SIToFP source and dest must both be vector or scalar", &I
);
3373 Check(SrcTy
->isIntOrIntVectorTy(),
3374 "SIToFP source must be integer or integer vector", &I
);
3375 Check(DestTy
->isFPOrFPVectorTy(), "SIToFP result must be FP or FP vector",
3378 if (SrcVec
&& DstVec
)
3379 Check(cast
<VectorType
>(SrcTy
)->getElementCount() ==
3380 cast
<VectorType
>(DestTy
)->getElementCount(),
3381 "SIToFP source and dest vector length mismatch", &I
);
3383 visitInstruction(I
);
3386 void Verifier::visitFPToUIInst(FPToUIInst
&I
) {
3387 // Get the source and destination types
3388 Type
*SrcTy
= I
.getOperand(0)->getType();
3389 Type
*DestTy
= I
.getType();
3391 bool SrcVec
= SrcTy
->isVectorTy();
3392 bool DstVec
= DestTy
->isVectorTy();
3394 Check(SrcVec
== DstVec
,
3395 "FPToUI source and dest must both be vector or scalar", &I
);
3396 Check(SrcTy
->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector", &I
);
3397 Check(DestTy
->isIntOrIntVectorTy(),
3398 "FPToUI result must be integer or integer vector", &I
);
3400 if (SrcVec
&& DstVec
)
3401 Check(cast
<VectorType
>(SrcTy
)->getElementCount() ==
3402 cast
<VectorType
>(DestTy
)->getElementCount(),
3403 "FPToUI source and dest vector length mismatch", &I
);
3405 visitInstruction(I
);
3408 void Verifier::visitFPToSIInst(FPToSIInst
&I
) {
3409 // Get the source and destination types
3410 Type
*SrcTy
= I
.getOperand(0)->getType();
3411 Type
*DestTy
= I
.getType();
3413 bool SrcVec
= SrcTy
->isVectorTy();
3414 bool DstVec
= DestTy
->isVectorTy();
3416 Check(SrcVec
== DstVec
,
3417 "FPToSI source and dest must both be vector or scalar", &I
);
3418 Check(SrcTy
->isFPOrFPVectorTy(), "FPToSI source must be FP or FP vector", &I
);
3419 Check(DestTy
->isIntOrIntVectorTy(),
3420 "FPToSI result must be integer or integer vector", &I
);
3422 if (SrcVec
&& DstVec
)
3423 Check(cast
<VectorType
>(SrcTy
)->getElementCount() ==
3424 cast
<VectorType
>(DestTy
)->getElementCount(),
3425 "FPToSI source and dest vector length mismatch", &I
);
3427 visitInstruction(I
);
3430 void Verifier::visitPtrToIntInst(PtrToIntInst
&I
) {
3431 // Get the source and destination types
3432 Type
*SrcTy
= I
.getOperand(0)->getType();
3433 Type
*DestTy
= I
.getType();
3435 Check(SrcTy
->isPtrOrPtrVectorTy(), "PtrToInt source must be pointer", &I
);
3437 Check(DestTy
->isIntOrIntVectorTy(), "PtrToInt result must be integral", &I
);
3438 Check(SrcTy
->isVectorTy() == DestTy
->isVectorTy(), "PtrToInt type mismatch",
3441 if (SrcTy
->isVectorTy()) {
3442 auto *VSrc
= cast
<VectorType
>(SrcTy
);
3443 auto *VDest
= cast
<VectorType
>(DestTy
);
3444 Check(VSrc
->getElementCount() == VDest
->getElementCount(),
3445 "PtrToInt Vector width mismatch", &I
);
3448 visitInstruction(I
);
3451 void Verifier::visitIntToPtrInst(IntToPtrInst
&I
) {
3452 // Get the source and destination types
3453 Type
*SrcTy
= I
.getOperand(0)->getType();
3454 Type
*DestTy
= I
.getType();
3456 Check(SrcTy
->isIntOrIntVectorTy(), "IntToPtr source must be an integral", &I
);
3457 Check(DestTy
->isPtrOrPtrVectorTy(), "IntToPtr result must be a pointer", &I
);
3459 Check(SrcTy
->isVectorTy() == DestTy
->isVectorTy(), "IntToPtr type mismatch",
3461 if (SrcTy
->isVectorTy()) {
3462 auto *VSrc
= cast
<VectorType
>(SrcTy
);
3463 auto *VDest
= cast
<VectorType
>(DestTy
);
3464 Check(VSrc
->getElementCount() == VDest
->getElementCount(),
3465 "IntToPtr Vector width mismatch", &I
);
3467 visitInstruction(I
);
3470 void Verifier::visitBitCastInst(BitCastInst
&I
) {
3472 CastInst::castIsValid(Instruction::BitCast
, I
.getOperand(0), I
.getType()),
3473 "Invalid bitcast", &I
);
3474 visitInstruction(I
);
3477 void Verifier::visitAddrSpaceCastInst(AddrSpaceCastInst
&I
) {
3478 Type
*SrcTy
= I
.getOperand(0)->getType();
3479 Type
*DestTy
= I
.getType();
3481 Check(SrcTy
->isPtrOrPtrVectorTy(), "AddrSpaceCast source must be a pointer",
3483 Check(DestTy
->isPtrOrPtrVectorTy(), "AddrSpaceCast result must be a pointer",
3485 Check(SrcTy
->getPointerAddressSpace() != DestTy
->getPointerAddressSpace(),
3486 "AddrSpaceCast must be between different address spaces", &I
);
3487 if (auto *SrcVTy
= dyn_cast
<VectorType
>(SrcTy
))
3488 Check(SrcVTy
->getElementCount() ==
3489 cast
<VectorType
>(DestTy
)->getElementCount(),
3490 "AddrSpaceCast vector pointer number of elements mismatch", &I
);
3491 visitInstruction(I
);
3494 /// visitPHINode - Ensure that a PHI node is well formed.
3496 void Verifier::visitPHINode(PHINode
&PN
) {
3497 // Ensure that the PHI nodes are all grouped together at the top of the block.
3498 // This can be tested by checking whether the instruction before this is
3499 // either nonexistent (because this is begin()) or is a PHI node. If not,
3500 // then there is some other instruction before a PHI.
3501 Check(&PN
== &PN
.getParent()->front() ||
3502 isa
<PHINode
>(--BasicBlock::iterator(&PN
)),
3503 "PHI nodes not grouped at top of basic block!", &PN
, PN
.getParent());
3505 // Check that a PHI doesn't yield a Token.
3506 Check(!PN
.getType()->isTokenTy(), "PHI nodes cannot have token type!");
3508 // Check that all of the values of the PHI node have the same type as the
3510 for (Value
*IncValue
: PN
.incoming_values()) {
3511 Check(PN
.getType() == IncValue
->getType(),
3512 "PHI node operands are not the same type as the result!", &PN
);
3515 // All other PHI node constraints are checked in the visitBasicBlock method.
3517 visitInstruction(PN
);
3520 void Verifier::visitCallBase(CallBase
&Call
) {
3521 Check(Call
.getCalledOperand()->getType()->isPointerTy(),
3522 "Called function must be a pointer!", Call
);
3523 FunctionType
*FTy
= Call
.getFunctionType();
3525 // Verify that the correct number of arguments are being passed
3526 if (FTy
->isVarArg())
3527 Check(Call
.arg_size() >= FTy
->getNumParams(),
3528 "Called function requires more parameters than were provided!", Call
);
3530 Check(Call
.arg_size() == FTy
->getNumParams(),
3531 "Incorrect number of arguments passed to called function!", Call
);
3533 // Verify that all arguments to the call match the function type.
3534 for (unsigned i
= 0, e
= FTy
->getNumParams(); i
!= e
; ++i
)
3535 Check(Call
.getArgOperand(i
)->getType() == FTy
->getParamType(i
),
3536 "Call parameter type does not match function signature!",
3537 Call
.getArgOperand(i
), FTy
->getParamType(i
), Call
);
3539 AttributeList Attrs
= Call
.getAttributes();
3541 Check(verifyAttributeCount(Attrs
, Call
.arg_size()),
3542 "Attribute after last parameter!", Call
);
3545 dyn_cast
<Function
>(Call
.getCalledOperand()->stripPointerCasts());
3546 bool IsIntrinsic
= Callee
&& Callee
->isIntrinsic();
3548 Check(Callee
->getValueType() == FTy
,
3549 "Intrinsic called with incompatible signature", Call
);
3551 // Disallow calls to functions with the amdgpu_cs_chain[_preserve] calling
3553 auto CC
= Call
.getCallingConv();
3554 Check(CC
!= CallingConv::AMDGPU_CS_Chain
&&
3555 CC
!= CallingConv::AMDGPU_CS_ChainPreserve
,
3556 "Direct calls to amdgpu_cs_chain/amdgpu_cs_chain_preserve functions "
3557 "not allowed. Please use the @llvm.amdgpu.cs.chain intrinsic instead.",
3560 // Disallow passing/returning values with alignment higher than we can
3562 // FIXME: Consider making DataLayout cap the alignment, so this isn't
3564 auto VerifyTypeAlign
= [&](Type
*Ty
, const Twine
&Message
) {
3567 Align ABIAlign
= DL
.getABITypeAlign(Ty
);
3568 Check(ABIAlign
.value() <= Value::MaximumAlignment
,
3569 "Incorrect alignment of " + Message
+ " to called function!", Call
);
3573 VerifyTypeAlign(FTy
->getReturnType(), "return type");
3574 for (unsigned i
= 0, e
= FTy
->getNumParams(); i
!= e
; ++i
) {
3575 Type
*Ty
= FTy
->getParamType(i
);
3576 VerifyTypeAlign(Ty
, "argument passed");
3580 if (Attrs
.hasFnAttr(Attribute::Speculatable
)) {
3581 // Don't allow speculatable on call sites, unless the underlying function
3582 // declaration is also speculatable.
3583 Check(Callee
&& Callee
->isSpeculatable(),
3584 "speculatable attribute may not apply to call sites", Call
);
3587 if (Attrs
.hasFnAttr(Attribute::Preallocated
)) {
3588 Check(Call
.getCalledFunction()->getIntrinsicID() ==
3589 Intrinsic::call_preallocated_arg
,
3590 "preallocated as a call site attribute can only be on "
3591 "llvm.call.preallocated.arg");
3594 // Verify call attributes.
3595 verifyFunctionAttrs(FTy
, Attrs
, &Call
, IsIntrinsic
, Call
.isInlineAsm());
3597 // Conservatively check the inalloca argument.
3598 // We have a bug if we can find that there is an underlying alloca without
3600 if (Call
.hasInAllocaArgument()) {
3601 Value
*InAllocaArg
= Call
.getArgOperand(FTy
->getNumParams() - 1);
3602 if (auto AI
= dyn_cast
<AllocaInst
>(InAllocaArg
->stripInBoundsOffsets()))
3603 Check(AI
->isUsedWithInAlloca(),
3604 "inalloca argument for call has mismatched alloca", AI
, Call
);
3607 // For each argument of the callsite, if it has the swifterror argument,
3608 // make sure the underlying alloca/parameter it comes from has a swifterror as
3610 for (unsigned i
= 0, e
= FTy
->getNumParams(); i
!= e
; ++i
) {
3611 if (Call
.paramHasAttr(i
, Attribute::SwiftError
)) {
3612 Value
*SwiftErrorArg
= Call
.getArgOperand(i
);
3613 if (auto AI
= dyn_cast
<AllocaInst
>(SwiftErrorArg
->stripInBoundsOffsets())) {
3614 Check(AI
->isSwiftError(),
3615 "swifterror argument for call has mismatched alloca", AI
, Call
);
3618 auto ArgI
= dyn_cast
<Argument
>(SwiftErrorArg
);
3619 Check(ArgI
, "swifterror argument should come from an alloca or parameter",
3620 SwiftErrorArg
, Call
);
3621 Check(ArgI
->hasSwiftErrorAttr(),
3622 "swifterror argument for call has mismatched parameter", ArgI
,
3626 if (Attrs
.hasParamAttr(i
, Attribute::ImmArg
)) {
3627 // Don't allow immarg on call sites, unless the underlying declaration
3628 // also has the matching immarg.
3629 Check(Callee
&& Callee
->hasParamAttribute(i
, Attribute::ImmArg
),
3630 "immarg may not apply only to call sites", Call
.getArgOperand(i
),
3634 if (Call
.paramHasAttr(i
, Attribute::ImmArg
)) {
3635 Value
*ArgVal
= Call
.getArgOperand(i
);
3636 Check(isa
<ConstantInt
>(ArgVal
) || isa
<ConstantFP
>(ArgVal
),
3637 "immarg operand has non-immediate parameter", ArgVal
, Call
);
3640 if (Call
.paramHasAttr(i
, Attribute::Preallocated
)) {
3641 Value
*ArgVal
= Call
.getArgOperand(i
);
3643 Call
.countOperandBundlesOfType(LLVMContext::OB_preallocated
) != 0;
3644 bool isMustTail
= Call
.isMustTailCall();
3645 Check(hasOB
!= isMustTail
,
3646 "preallocated operand either requires a preallocated bundle or "
3647 "the call to be musttail (but not both)",
3652 if (FTy
->isVarArg()) {
3653 // FIXME? is 'nest' even legal here?
3654 bool SawNest
= false;
3655 bool SawReturned
= false;
3657 for (unsigned Idx
= 0; Idx
< FTy
->getNumParams(); ++Idx
) {
3658 if (Attrs
.hasParamAttr(Idx
, Attribute::Nest
))
3660 if (Attrs
.hasParamAttr(Idx
, Attribute::Returned
))
3664 // Check attributes on the varargs part.
3665 for (unsigned Idx
= FTy
->getNumParams(); Idx
< Call
.arg_size(); ++Idx
) {
3666 Type
*Ty
= Call
.getArgOperand(Idx
)->getType();
3667 AttributeSet ArgAttrs
= Attrs
.getParamAttrs(Idx
);
3668 verifyParameterAttrs(ArgAttrs
, Ty
, &Call
);
3670 if (ArgAttrs
.hasAttribute(Attribute::Nest
)) {
3671 Check(!SawNest
, "More than one parameter has attribute nest!", Call
);
3675 if (ArgAttrs
.hasAttribute(Attribute::Returned
)) {
3676 Check(!SawReturned
, "More than one parameter has attribute returned!",
3678 Check(Ty
->canLosslesslyBitCastTo(FTy
->getReturnType()),
3679 "Incompatible argument and return types for 'returned' "
3685 // Statepoint intrinsic is vararg but the wrapped function may be not.
3686 // Allow sret here and check the wrapped function in verifyStatepoint.
3687 if (!Call
.getCalledFunction() ||
3688 Call
.getCalledFunction()->getIntrinsicID() !=
3689 Intrinsic::experimental_gc_statepoint
)
3690 Check(!ArgAttrs
.hasAttribute(Attribute::StructRet
),
3691 "Attribute 'sret' cannot be used for vararg call arguments!",
3694 if (ArgAttrs
.hasAttribute(Attribute::InAlloca
))
3695 Check(Idx
== Call
.arg_size() - 1,
3696 "inalloca isn't on the last argument!", Call
);
3700 // Verify that there's no metadata unless it's a direct call to an intrinsic.
3702 for (Type
*ParamTy
: FTy
->params()) {
3703 Check(!ParamTy
->isMetadataTy(),
3704 "Function has metadata parameter but isn't an intrinsic", Call
);
3705 Check(!ParamTy
->isTokenTy(),
3706 "Function has token parameter but isn't an intrinsic", Call
);
3710 // Verify that indirect calls don't return tokens.
3711 if (!Call
.getCalledFunction()) {
3712 Check(!FTy
->getReturnType()->isTokenTy(),
3713 "Return type cannot be token for indirect call!");
3714 Check(!FTy
->getReturnType()->isX86_AMXTy(),
3715 "Return type cannot be x86_amx for indirect call!");
3718 if (Function
*F
= Call
.getCalledFunction())
3719 if (Intrinsic::ID ID
= (Intrinsic::ID
)F
->getIntrinsicID())
3720 visitIntrinsicCall(ID
, Call
);
3722 // Verify that a callsite has at most one "deopt", at most one "funclet", at
3723 // most one "gc-transition", at most one "cfguardtarget", at most one
3724 // "preallocated" operand bundle, and at most one "ptrauth" operand bundle.
3725 bool FoundDeoptBundle
= false, FoundFuncletBundle
= false,
3726 FoundGCTransitionBundle
= false, FoundCFGuardTargetBundle
= false,
3727 FoundPreallocatedBundle
= false, FoundGCLiveBundle
= false,
3728 FoundPtrauthBundle
= false, FoundKCFIBundle
= false,
3729 FoundAttachedCallBundle
= false;
3730 for (unsigned i
= 0, e
= Call
.getNumOperandBundles(); i
< e
; ++i
) {
3731 OperandBundleUse BU
= Call
.getOperandBundleAt(i
);
3732 uint32_t Tag
= BU
.getTagID();
3733 if (Tag
== LLVMContext::OB_deopt
) {
3734 Check(!FoundDeoptBundle
, "Multiple deopt operand bundles", Call
);
3735 FoundDeoptBundle
= true;
3736 } else if (Tag
== LLVMContext::OB_gc_transition
) {
3737 Check(!FoundGCTransitionBundle
, "Multiple gc-transition operand bundles",
3739 FoundGCTransitionBundle
= true;
3740 } else if (Tag
== LLVMContext::OB_funclet
) {
3741 Check(!FoundFuncletBundle
, "Multiple funclet operand bundles", Call
);
3742 FoundFuncletBundle
= true;
3743 Check(BU
.Inputs
.size() == 1,
3744 "Expected exactly one funclet bundle operand", Call
);
3745 Check(isa
<FuncletPadInst
>(BU
.Inputs
.front()),
3746 "Funclet bundle operands should correspond to a FuncletPadInst",
3748 } else if (Tag
== LLVMContext::OB_cfguardtarget
) {
3749 Check(!FoundCFGuardTargetBundle
, "Multiple CFGuardTarget operand bundles",
3751 FoundCFGuardTargetBundle
= true;
3752 Check(BU
.Inputs
.size() == 1,
3753 "Expected exactly one cfguardtarget bundle operand", Call
);
3754 } else if (Tag
== LLVMContext::OB_ptrauth
) {
3755 Check(!FoundPtrauthBundle
, "Multiple ptrauth operand bundles", Call
);
3756 FoundPtrauthBundle
= true;
3757 Check(BU
.Inputs
.size() == 2,
3758 "Expected exactly two ptrauth bundle operands", Call
);
3759 Check(isa
<ConstantInt
>(BU
.Inputs
[0]) &&
3760 BU
.Inputs
[0]->getType()->isIntegerTy(32),
3761 "Ptrauth bundle key operand must be an i32 constant", Call
);
3762 Check(BU
.Inputs
[1]->getType()->isIntegerTy(64),
3763 "Ptrauth bundle discriminator operand must be an i64", Call
);
3764 } else if (Tag
== LLVMContext::OB_kcfi
) {
3765 Check(!FoundKCFIBundle
, "Multiple kcfi operand bundles", Call
);
3766 FoundKCFIBundle
= true;
3767 Check(BU
.Inputs
.size() == 1, "Expected exactly one kcfi bundle operand",
3769 Check(isa
<ConstantInt
>(BU
.Inputs
[0]) &&
3770 BU
.Inputs
[0]->getType()->isIntegerTy(32),
3771 "Kcfi bundle operand must be an i32 constant", Call
);
3772 } else if (Tag
== LLVMContext::OB_preallocated
) {
3773 Check(!FoundPreallocatedBundle
, "Multiple preallocated operand bundles",
3775 FoundPreallocatedBundle
= true;
3776 Check(BU
.Inputs
.size() == 1,
3777 "Expected exactly one preallocated bundle operand", Call
);
3778 auto Input
= dyn_cast
<IntrinsicInst
>(BU
.Inputs
.front());
3780 Input
->getIntrinsicID() == Intrinsic::call_preallocated_setup
,
3781 "\"preallocated\" argument must be a token from "
3782 "llvm.call.preallocated.setup",
3784 } else if (Tag
== LLVMContext::OB_gc_live
) {
3785 Check(!FoundGCLiveBundle
, "Multiple gc-live operand bundles", Call
);
3786 FoundGCLiveBundle
= true;
3787 } else if (Tag
== LLVMContext::OB_clang_arc_attachedcall
) {
3788 Check(!FoundAttachedCallBundle
,
3789 "Multiple \"clang.arc.attachedcall\" operand bundles", Call
);
3790 FoundAttachedCallBundle
= true;
3791 verifyAttachedCallBundle(Call
, BU
);
3795 // Verify that callee and callsite agree on whether to use pointer auth.
3796 Check(!(Call
.getCalledFunction() && FoundPtrauthBundle
),
3797 "Direct call cannot have a ptrauth bundle", Call
);
3799 // Verify that each inlinable callsite of a debug-info-bearing function in a
3800 // debug-info-bearing function has a debug location attached to it. Failure to
3801 // do so causes assertion failures when the inliner sets up inline scope info
3802 // (Interposable functions are not inlinable, neither are functions without
3804 if (Call
.getFunction()->getSubprogram() && Call
.getCalledFunction() &&
3805 !Call
.getCalledFunction()->isInterposable() &&
3806 !Call
.getCalledFunction()->isDeclaration() &&
3807 Call
.getCalledFunction()->getSubprogram())
3808 CheckDI(Call
.getDebugLoc(),
3809 "inlinable function call in a function with "
3810 "debug info must have a !dbg location",
3813 if (Call
.isInlineAsm())
3814 verifyInlineAsmCall(Call
);
3816 ConvergenceVerifyHelper
.visit(Call
);
3818 visitInstruction(Call
);
3821 void Verifier::verifyTailCCMustTailAttrs(const AttrBuilder
&Attrs
,
3822 StringRef Context
) {
3823 Check(!Attrs
.contains(Attribute::InAlloca
),
3824 Twine("inalloca attribute not allowed in ") + Context
);
3825 Check(!Attrs
.contains(Attribute::InReg
),
3826 Twine("inreg attribute not allowed in ") + Context
);
3827 Check(!Attrs
.contains(Attribute::SwiftError
),
3828 Twine("swifterror attribute not allowed in ") + Context
);
3829 Check(!Attrs
.contains(Attribute::Preallocated
),
3830 Twine("preallocated attribute not allowed in ") + Context
);
3831 Check(!Attrs
.contains(Attribute::ByRef
),
3832 Twine("byref attribute not allowed in ") + Context
);
3835 /// Two types are "congruent" if they are identical, or if they are both pointer
3836 /// types with different pointee types and the same address space.
3837 static bool isTypeCongruent(Type
*L
, Type
*R
) {
3840 PointerType
*PL
= dyn_cast
<PointerType
>(L
);
3841 PointerType
*PR
= dyn_cast
<PointerType
>(R
);
3844 return PL
->getAddressSpace() == PR
->getAddressSpace();
3847 static AttrBuilder
getParameterABIAttributes(LLVMContext
& C
, unsigned I
, AttributeList Attrs
) {
3848 static const Attribute::AttrKind ABIAttrs
[] = {
3849 Attribute::StructRet
, Attribute::ByVal
, Attribute::InAlloca
,
3850 Attribute::InReg
, Attribute::StackAlignment
, Attribute::SwiftSelf
,
3851 Attribute::SwiftAsync
, Attribute::SwiftError
, Attribute::Preallocated
,
3853 AttrBuilder
Copy(C
);
3854 for (auto AK
: ABIAttrs
) {
3855 Attribute Attr
= Attrs
.getParamAttrs(I
).getAttribute(AK
);
3857 Copy
.addAttribute(Attr
);
3860 // `align` is ABI-affecting only in combination with `byval` or `byref`.
3861 if (Attrs
.hasParamAttr(I
, Attribute::Alignment
) &&
3862 (Attrs
.hasParamAttr(I
, Attribute::ByVal
) ||
3863 Attrs
.hasParamAttr(I
, Attribute::ByRef
)))
3864 Copy
.addAlignmentAttr(Attrs
.getParamAlignment(I
));
3868 void Verifier::verifyMustTailCall(CallInst
&CI
) {
3869 Check(!CI
.isInlineAsm(), "cannot use musttail call with inline asm", &CI
);
3871 Function
*F
= CI
.getParent()->getParent();
3872 FunctionType
*CallerTy
= F
->getFunctionType();
3873 FunctionType
*CalleeTy
= CI
.getFunctionType();
3874 Check(CallerTy
->isVarArg() == CalleeTy
->isVarArg(),
3875 "cannot guarantee tail call due to mismatched varargs", &CI
);
3876 Check(isTypeCongruent(CallerTy
->getReturnType(), CalleeTy
->getReturnType()),
3877 "cannot guarantee tail call due to mismatched return types", &CI
);
3879 // - The calling conventions of the caller and callee must match.
3880 Check(F
->getCallingConv() == CI
.getCallingConv(),
3881 "cannot guarantee tail call due to mismatched calling conv", &CI
);
3883 // - The call must immediately precede a :ref:`ret <i_ret>` instruction,
3884 // or a pointer bitcast followed by a ret instruction.
3885 // - The ret instruction must return the (possibly bitcasted) value
3886 // produced by the call or void.
3887 Value
*RetVal
= &CI
;
3888 Instruction
*Next
= CI
.getNextNode();
3890 // Handle the optional bitcast.
3891 if (BitCastInst
*BI
= dyn_cast_or_null
<BitCastInst
>(Next
)) {
3892 Check(BI
->getOperand(0) == RetVal
,
3893 "bitcast following musttail call must use the call", BI
);
3895 Next
= BI
->getNextNode();
3898 // Check the return.
3899 ReturnInst
*Ret
= dyn_cast_or_null
<ReturnInst
>(Next
);
3900 Check(Ret
, "musttail call must precede a ret with an optional bitcast", &CI
);
3901 Check(!Ret
->getReturnValue() || Ret
->getReturnValue() == RetVal
||
3902 isa
<UndefValue
>(Ret
->getReturnValue()),
3903 "musttail call result must be returned", Ret
);
3905 AttributeList CallerAttrs
= F
->getAttributes();
3906 AttributeList CalleeAttrs
= CI
.getAttributes();
3907 if (CI
.getCallingConv() == CallingConv::SwiftTail
||
3908 CI
.getCallingConv() == CallingConv::Tail
) {
3910 CI
.getCallingConv() == CallingConv::Tail
? "tailcc" : "swifttailcc";
3912 // - Only sret, byval, swiftself, and swiftasync ABI-impacting attributes
3913 // are allowed in swifttailcc call
3914 for (unsigned I
= 0, E
= CallerTy
->getNumParams(); I
!= E
; ++I
) {
3915 AttrBuilder ABIAttrs
= getParameterABIAttributes(F
->getContext(), I
, CallerAttrs
);
3916 SmallString
<32> Context
{CCName
, StringRef(" musttail caller")};
3917 verifyTailCCMustTailAttrs(ABIAttrs
, Context
);
3919 for (unsigned I
= 0, E
= CalleeTy
->getNumParams(); I
!= E
; ++I
) {
3920 AttrBuilder ABIAttrs
= getParameterABIAttributes(F
->getContext(), I
, CalleeAttrs
);
3921 SmallString
<32> Context
{CCName
, StringRef(" musttail callee")};
3922 verifyTailCCMustTailAttrs(ABIAttrs
, Context
);
3924 // - Varargs functions are not allowed
3925 Check(!CallerTy
->isVarArg(), Twine("cannot guarantee ") + CCName
+
3926 " tail call for varargs function");
3930 // - The caller and callee prototypes must match. Pointer types of
3931 // parameters or return types may differ in pointee type, but not
3933 if (!CI
.getCalledFunction() || !CI
.getCalledFunction()->isIntrinsic()) {
3934 Check(CallerTy
->getNumParams() == CalleeTy
->getNumParams(),
3935 "cannot guarantee tail call due to mismatched parameter counts", &CI
);
3936 for (unsigned I
= 0, E
= CallerTy
->getNumParams(); I
!= E
; ++I
) {
3938 isTypeCongruent(CallerTy
->getParamType(I
), CalleeTy
->getParamType(I
)),
3939 "cannot guarantee tail call due to mismatched parameter types", &CI
);
3943 // - All ABI-impacting function attributes, such as sret, byval, inreg,
3944 // returned, preallocated, and inalloca, must match.
3945 for (unsigned I
= 0, E
= CallerTy
->getNumParams(); I
!= E
; ++I
) {
3946 AttrBuilder CallerABIAttrs
= getParameterABIAttributes(F
->getContext(), I
, CallerAttrs
);
3947 AttrBuilder CalleeABIAttrs
= getParameterABIAttributes(F
->getContext(), I
, CalleeAttrs
);
3948 Check(CallerABIAttrs
== CalleeABIAttrs
,
3949 "cannot guarantee tail call due to mismatched ABI impacting "
3950 "function attributes",
3951 &CI
, CI
.getOperand(I
));
3955 void Verifier::visitCallInst(CallInst
&CI
) {
3958 if (CI
.isMustTailCall())
3959 verifyMustTailCall(CI
);
3962 void Verifier::visitInvokeInst(InvokeInst
&II
) {
3965 // Verify that the first non-PHI instruction of the unwind destination is an
3966 // exception handling instruction.
3968 II
.getUnwindDest()->isEHPad(),
3969 "The unwind destination does not have an exception handling instruction!",
3972 visitTerminator(II
);
3975 /// visitUnaryOperator - Check the argument to the unary operator.
3977 void Verifier::visitUnaryOperator(UnaryOperator
&U
) {
3978 Check(U
.getType() == U
.getOperand(0)->getType(),
3979 "Unary operators must have same type for"
3980 "operands and result!",
3983 switch (U
.getOpcode()) {
3984 // Check that floating-point arithmetic operators are only used with
3985 // floating-point operands.
3986 case Instruction::FNeg
:
3987 Check(U
.getType()->isFPOrFPVectorTy(),
3988 "FNeg operator only works with float types!", &U
);
3991 llvm_unreachable("Unknown UnaryOperator opcode!");
3994 visitInstruction(U
);
3997 /// visitBinaryOperator - Check that both arguments to the binary operator are
3998 /// of the same type!
4000 void Verifier::visitBinaryOperator(BinaryOperator
&B
) {
4001 Check(B
.getOperand(0)->getType() == B
.getOperand(1)->getType(),
4002 "Both operands to a binary operator are not of the same type!", &B
);
4004 switch (B
.getOpcode()) {
4005 // Check that integer arithmetic operators are only used with
4006 // integral operands.
4007 case Instruction::Add
:
4008 case Instruction::Sub
:
4009 case Instruction::Mul
:
4010 case Instruction::SDiv
:
4011 case Instruction::UDiv
:
4012 case Instruction::SRem
:
4013 case Instruction::URem
:
4014 Check(B
.getType()->isIntOrIntVectorTy(),
4015 "Integer arithmetic operators only work with integral types!", &B
);
4016 Check(B
.getType() == B
.getOperand(0)->getType(),
4017 "Integer arithmetic operators must have same type "
4018 "for operands and result!",
4021 // Check that floating-point arithmetic operators are only used with
4022 // floating-point operands.
4023 case Instruction::FAdd
:
4024 case Instruction::FSub
:
4025 case Instruction::FMul
:
4026 case Instruction::FDiv
:
4027 case Instruction::FRem
:
4028 Check(B
.getType()->isFPOrFPVectorTy(),
4029 "Floating-point arithmetic operators only work with "
4030 "floating-point types!",
4032 Check(B
.getType() == B
.getOperand(0)->getType(),
4033 "Floating-point arithmetic operators must have same type "
4034 "for operands and result!",
4037 // Check that logical operators are only used with integral operands.
4038 case Instruction::And
:
4039 case Instruction::Or
:
4040 case Instruction::Xor
:
4041 Check(B
.getType()->isIntOrIntVectorTy(),
4042 "Logical operators only work with integral types!", &B
);
4043 Check(B
.getType() == B
.getOperand(0)->getType(),
4044 "Logical operators must have same type for operands and result!", &B
);
4046 case Instruction::Shl
:
4047 case Instruction::LShr
:
4048 case Instruction::AShr
:
4049 Check(B
.getType()->isIntOrIntVectorTy(),
4050 "Shifts only work with integral types!", &B
);
4051 Check(B
.getType() == B
.getOperand(0)->getType(),
4052 "Shift return type must be same as operands!", &B
);
4055 llvm_unreachable("Unknown BinaryOperator opcode!");
4058 visitInstruction(B
);
4061 void Verifier::visitICmpInst(ICmpInst
&IC
) {
4062 // Check that the operands are the same type
4063 Type
*Op0Ty
= IC
.getOperand(0)->getType();
4064 Type
*Op1Ty
= IC
.getOperand(1)->getType();
4065 Check(Op0Ty
== Op1Ty
,
4066 "Both operands to ICmp instruction are not of the same type!", &IC
);
4067 // Check that the operands are the right type
4068 Check(Op0Ty
->isIntOrIntVectorTy() || Op0Ty
->isPtrOrPtrVectorTy(),
4069 "Invalid operand types for ICmp instruction", &IC
);
4070 // Check that the predicate is valid.
4071 Check(IC
.isIntPredicate(), "Invalid predicate in ICmp instruction!", &IC
);
4073 visitInstruction(IC
);
4076 void Verifier::visitFCmpInst(FCmpInst
&FC
) {
4077 // Check that the operands are the same type
4078 Type
*Op0Ty
= FC
.getOperand(0)->getType();
4079 Type
*Op1Ty
= FC
.getOperand(1)->getType();
4080 Check(Op0Ty
== Op1Ty
,
4081 "Both operands to FCmp instruction are not of the same type!", &FC
);
4082 // Check that the operands are the right type
4083 Check(Op0Ty
->isFPOrFPVectorTy(), "Invalid operand types for FCmp instruction",
4085 // Check that the predicate is valid.
4086 Check(FC
.isFPPredicate(), "Invalid predicate in FCmp instruction!", &FC
);
4088 visitInstruction(FC
);
4091 void Verifier::visitExtractElementInst(ExtractElementInst
&EI
) {
4092 Check(ExtractElementInst::isValidOperands(EI
.getOperand(0), EI
.getOperand(1)),
4093 "Invalid extractelement operands!", &EI
);
4094 visitInstruction(EI
);
4097 void Verifier::visitInsertElementInst(InsertElementInst
&IE
) {
4098 Check(InsertElementInst::isValidOperands(IE
.getOperand(0), IE
.getOperand(1),
4100 "Invalid insertelement operands!", &IE
);
4101 visitInstruction(IE
);
4104 void Verifier::visitShuffleVectorInst(ShuffleVectorInst
&SV
) {
4105 Check(ShuffleVectorInst::isValidOperands(SV
.getOperand(0), SV
.getOperand(1),
4106 SV
.getShuffleMask()),
4107 "Invalid shufflevector operands!", &SV
);
4108 visitInstruction(SV
);
4111 void Verifier::visitGetElementPtrInst(GetElementPtrInst
&GEP
) {
4112 Type
*TargetTy
= GEP
.getPointerOperandType()->getScalarType();
4114 Check(isa
<PointerType
>(TargetTy
),
4115 "GEP base pointer is not a vector or a vector of pointers", &GEP
);
4116 Check(GEP
.getSourceElementType()->isSized(), "GEP into unsized type!", &GEP
);
4118 if (auto *STy
= dyn_cast
<StructType
>(GEP
.getSourceElementType())) {
4119 Check(!STy
->isScalableTy(),
4120 "getelementptr cannot target structure that contains scalable vector"
4125 SmallVector
<Value
*, 16> Idxs(GEP
.indices());
4127 all_of(Idxs
, [](Value
*V
) { return V
->getType()->isIntOrIntVectorTy(); }),
4128 "GEP indexes must be integers", &GEP
);
4130 GetElementPtrInst::getIndexedType(GEP
.getSourceElementType(), Idxs
);
4131 Check(ElTy
, "Invalid indices for GEP pointer type!", &GEP
);
4133 PointerType
*PtrTy
= dyn_cast
<PointerType
>(GEP
.getType()->getScalarType());
4135 Check(PtrTy
&& GEP
.getResultElementType() == ElTy
,
4136 "GEP is not of right type for indices!", &GEP
, ElTy
);
4138 if (auto *GEPVTy
= dyn_cast
<VectorType
>(GEP
.getType())) {
4139 // Additional checks for vector GEPs.
4140 ElementCount GEPWidth
= GEPVTy
->getElementCount();
4141 if (GEP
.getPointerOperandType()->isVectorTy())
4144 cast
<VectorType
>(GEP
.getPointerOperandType())->getElementCount(),
4145 "Vector GEP result width doesn't match operand's", &GEP
);
4146 for (Value
*Idx
: Idxs
) {
4147 Type
*IndexTy
= Idx
->getType();
4148 if (auto *IndexVTy
= dyn_cast
<VectorType
>(IndexTy
)) {
4149 ElementCount IndexWidth
= IndexVTy
->getElementCount();
4150 Check(IndexWidth
== GEPWidth
, "Invalid GEP index vector width", &GEP
);
4152 Check(IndexTy
->isIntOrIntVectorTy(),
4153 "All GEP indices should be of integer type");
4157 Check(GEP
.getAddressSpace() == PtrTy
->getAddressSpace(),
4158 "GEP address space doesn't match type", &GEP
);
4160 visitInstruction(GEP
);
4163 static bool isContiguous(const ConstantRange
&A
, const ConstantRange
&B
) {
4164 return A
.getUpper() == B
.getLower() || A
.getLower() == B
.getUpper();
4167 /// Verify !range and !absolute_symbol metadata. These have the same
4168 /// restrictions, except !absolute_symbol allows the full set.
4169 void Verifier::verifyRangeLikeMetadata(const Value
&I
, const MDNode
*Range
,
4170 Type
*Ty
, RangeLikeMetadataKind Kind
) {
4171 unsigned NumOperands
= Range
->getNumOperands();
4172 Check(NumOperands
% 2 == 0, "Unfinished range!", Range
);
4173 unsigned NumRanges
= NumOperands
/ 2;
4174 Check(NumRanges
>= 1, "It should have at least one range!", Range
);
4176 ConstantRange
LastRange(1, true); // Dummy initial value
4177 for (unsigned i
= 0; i
< NumRanges
; ++i
) {
4179 mdconst::dyn_extract
<ConstantInt
>(Range
->getOperand(2 * i
));
4180 Check(Low
, "The lower limit must be an integer!", Low
);
4182 mdconst::dyn_extract
<ConstantInt
>(Range
->getOperand(2 * i
+ 1));
4183 Check(High
, "The upper limit must be an integer!", High
);
4185 Check(High
->getType() == Low
->getType(), "Range pair types must match!",
4188 if (Kind
== RangeLikeMetadataKind::NoaliasAddrspace
) {
4189 Check(High
->getType()->isIntegerTy(32),
4190 "noalias.addrspace type must be i32!", &I
);
4192 Check(High
->getType() == Ty
->getScalarType(),
4193 "Range types must match instruction type!", &I
);
4196 APInt HighV
= High
->getValue();
4197 APInt LowV
= Low
->getValue();
4199 // ConstantRange asserts if the ranges are the same except for the min/max
4200 // value. Leave the cases it tolerates for the empty range error below.
4201 Check(LowV
!= HighV
|| LowV
.isMaxValue() || LowV
.isMinValue(),
4202 "The upper and lower limits cannot be the same value", &I
);
4204 ConstantRange
CurRange(LowV
, HighV
);
4205 Check(!CurRange
.isEmptySet() &&
4206 (Kind
== RangeLikeMetadataKind::AbsoluteSymbol
||
4207 !CurRange
.isFullSet()),
4208 "Range must not be empty!", Range
);
4210 Check(CurRange
.intersectWith(LastRange
).isEmptySet(),
4211 "Intervals are overlapping", Range
);
4212 Check(LowV
.sgt(LastRange
.getLower()), "Intervals are not in order",
4214 Check(!isContiguous(CurRange
, LastRange
), "Intervals are contiguous",
4217 LastRange
= ConstantRange(LowV
, HighV
);
4219 if (NumRanges
> 2) {
4221 mdconst::dyn_extract
<ConstantInt
>(Range
->getOperand(0))->getValue();
4223 mdconst::dyn_extract
<ConstantInt
>(Range
->getOperand(1))->getValue();
4224 ConstantRange
FirstRange(FirstLow
, FirstHigh
);
4225 Check(FirstRange
.intersectWith(LastRange
).isEmptySet(),
4226 "Intervals are overlapping", Range
);
4227 Check(!isContiguous(FirstRange
, LastRange
), "Intervals are contiguous",
4232 void Verifier::visitRangeMetadata(Instruction
&I
, MDNode
*Range
, Type
*Ty
) {
4233 assert(Range
&& Range
== I
.getMetadata(LLVMContext::MD_range
) &&
4234 "precondition violation");
4235 verifyRangeLikeMetadata(I
, Range
, Ty
, RangeLikeMetadataKind::Range
);
4238 void Verifier::visitNoaliasAddrspaceMetadata(Instruction
&I
, MDNode
*Range
,
4240 assert(Range
&& Range
== I
.getMetadata(LLVMContext::MD_noalias_addrspace
) &&
4241 "precondition violation");
4242 verifyRangeLikeMetadata(I
, Range
, Ty
,
4243 RangeLikeMetadataKind::NoaliasAddrspace
);
4246 void Verifier::checkAtomicMemAccessSize(Type
*Ty
, const Instruction
*I
) {
4247 unsigned Size
= DL
.getTypeSizeInBits(Ty
);
4248 Check(Size
>= 8, "atomic memory access' size must be byte-sized", Ty
, I
);
4249 Check(!(Size
& (Size
- 1)),
4250 "atomic memory access' operand must have a power-of-two size", Ty
, I
);
4253 void Verifier::visitLoadInst(LoadInst
&LI
) {
4254 PointerType
*PTy
= dyn_cast
<PointerType
>(LI
.getOperand(0)->getType());
4255 Check(PTy
, "Load operand must be a pointer.", &LI
);
4256 Type
*ElTy
= LI
.getType();
4257 if (MaybeAlign A
= LI
.getAlign()) {
4258 Check(A
->value() <= Value::MaximumAlignment
,
4259 "huge alignment values are unsupported", &LI
);
4261 Check(ElTy
->isSized(), "loading unsized types is not allowed", &LI
);
4262 if (LI
.isAtomic()) {
4263 Check(LI
.getOrdering() != AtomicOrdering::Release
&&
4264 LI
.getOrdering() != AtomicOrdering::AcquireRelease
,
4265 "Load cannot have Release ordering", &LI
);
4266 Check(ElTy
->isIntOrPtrTy() || ElTy
->isFloatingPointTy(),
4267 "atomic load operand must have integer, pointer, or floating point "
4270 checkAtomicMemAccessSize(ElTy
, &LI
);
4272 Check(LI
.getSyncScopeID() == SyncScope::System
,
4273 "Non-atomic load cannot have SynchronizationScope specified", &LI
);
4276 visitInstruction(LI
);
4279 void Verifier::visitStoreInst(StoreInst
&SI
) {
4280 PointerType
*PTy
= dyn_cast
<PointerType
>(SI
.getOperand(1)->getType());
4281 Check(PTy
, "Store operand must be a pointer.", &SI
);
4282 Type
*ElTy
= SI
.getOperand(0)->getType();
4283 if (MaybeAlign A
= SI
.getAlign()) {
4284 Check(A
->value() <= Value::MaximumAlignment
,
4285 "huge alignment values are unsupported", &SI
);
4287 Check(ElTy
->isSized(), "storing unsized types is not allowed", &SI
);
4288 if (SI
.isAtomic()) {
4289 Check(SI
.getOrdering() != AtomicOrdering::Acquire
&&
4290 SI
.getOrdering() != AtomicOrdering::AcquireRelease
,
4291 "Store cannot have Acquire ordering", &SI
);
4292 Check(ElTy
->isIntOrPtrTy() || ElTy
->isFloatingPointTy(),
4293 "atomic store operand must have integer, pointer, or floating point "
4296 checkAtomicMemAccessSize(ElTy
, &SI
);
4298 Check(SI
.getSyncScopeID() == SyncScope::System
,
4299 "Non-atomic store cannot have SynchronizationScope specified", &SI
);
4301 visitInstruction(SI
);
4304 /// Check that SwiftErrorVal is used as a swifterror argument in CS.
4305 void Verifier::verifySwiftErrorCall(CallBase
&Call
,
4306 const Value
*SwiftErrorVal
) {
4307 for (const auto &I
: llvm::enumerate(Call
.args())) {
4308 if (I
.value() == SwiftErrorVal
) {
4309 Check(Call
.paramHasAttr(I
.index(), Attribute::SwiftError
),
4310 "swifterror value when used in a callsite should be marked "
4311 "with swifterror attribute",
4312 SwiftErrorVal
, Call
);
4317 void Verifier::verifySwiftErrorValue(const Value
*SwiftErrorVal
) {
4318 // Check that swifterror value is only used by loads, stores, or as
4319 // a swifterror argument.
4320 for (const User
*U
: SwiftErrorVal
->users()) {
4321 Check(isa
<LoadInst
>(U
) || isa
<StoreInst
>(U
) || isa
<CallInst
>(U
) ||
4323 "swifterror value can only be loaded and stored from, or "
4324 "as a swifterror argument!",
4326 // If it is used by a store, check it is the second operand.
4327 if (auto StoreI
= dyn_cast
<StoreInst
>(U
))
4328 Check(StoreI
->getOperand(1) == SwiftErrorVal
,
4329 "swifterror value should be the second operand when used "
4332 if (auto *Call
= dyn_cast
<CallBase
>(U
))
4333 verifySwiftErrorCall(*const_cast<CallBase
*>(Call
), SwiftErrorVal
);
4337 void Verifier::visitAllocaInst(AllocaInst
&AI
) {
4338 Type
*Ty
= AI
.getAllocatedType();
4339 SmallPtrSet
<Type
*, 4> Visited
;
4340 Check(Ty
->isSized(&Visited
), "Cannot allocate unsized type", &AI
);
4341 // Check if it's a target extension type that disallows being used on the
4343 Check(!Ty
->containsNonLocalTargetExtType(),
4344 "Alloca has illegal target extension type", &AI
);
4345 Check(AI
.getArraySize()->getType()->isIntegerTy(),
4346 "Alloca array size must have integer type", &AI
);
4347 if (MaybeAlign A
= AI
.getAlign()) {
4348 Check(A
->value() <= Value::MaximumAlignment
,
4349 "huge alignment values are unsupported", &AI
);
4352 if (AI
.isSwiftError()) {
4353 Check(Ty
->isPointerTy(), "swifterror alloca must have pointer type", &AI
);
4354 Check(!AI
.isArrayAllocation(),
4355 "swifterror alloca must not be array allocation", &AI
);
4356 verifySwiftErrorValue(&AI
);
4359 visitInstruction(AI
);
4362 void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst
&CXI
) {
4363 Type
*ElTy
= CXI
.getOperand(1)->getType();
4364 Check(ElTy
->isIntOrPtrTy(),
4365 "cmpxchg operand must have integer or pointer type", ElTy
, &CXI
);
4366 checkAtomicMemAccessSize(ElTy
, &CXI
);
4367 visitInstruction(CXI
);
4370 void Verifier::visitAtomicRMWInst(AtomicRMWInst
&RMWI
) {
4371 Check(RMWI
.getOrdering() != AtomicOrdering::Unordered
,
4372 "atomicrmw instructions cannot be unordered.", &RMWI
);
4373 auto Op
= RMWI
.getOperation();
4374 Type
*ElTy
= RMWI
.getOperand(1)->getType();
4375 if (Op
== AtomicRMWInst::Xchg
) {
4376 Check(ElTy
->isIntegerTy() || ElTy
->isFloatingPointTy() ||
4377 ElTy
->isPointerTy(),
4378 "atomicrmw " + AtomicRMWInst::getOperationName(Op
) +
4379 " operand must have integer or floating point type!",
4381 } else if (AtomicRMWInst::isFPOperation(Op
)) {
4382 Check(ElTy
->isFPOrFPVectorTy() && !isa
<ScalableVectorType
>(ElTy
),
4383 "atomicrmw " + AtomicRMWInst::getOperationName(Op
) +
4384 " operand must have floating-point or fixed vector of floating-point "
4388 Check(ElTy
->isIntegerTy(),
4389 "atomicrmw " + AtomicRMWInst::getOperationName(Op
) +
4390 " operand must have integer type!",
4393 checkAtomicMemAccessSize(ElTy
, &RMWI
);
4394 Check(AtomicRMWInst::FIRST_BINOP
<= Op
&& Op
<= AtomicRMWInst::LAST_BINOP
,
4395 "Invalid binary operation!", &RMWI
);
4396 visitInstruction(RMWI
);
4399 void Verifier::visitFenceInst(FenceInst
&FI
) {
4400 const AtomicOrdering Ordering
= FI
.getOrdering();
4401 Check(Ordering
== AtomicOrdering::Acquire
||
4402 Ordering
== AtomicOrdering::Release
||
4403 Ordering
== AtomicOrdering::AcquireRelease
||
4404 Ordering
== AtomicOrdering::SequentiallyConsistent
,
4405 "fence instructions may only have acquire, release, acq_rel, or "
4406 "seq_cst ordering.",
4408 visitInstruction(FI
);
4411 void Verifier::visitExtractValueInst(ExtractValueInst
&EVI
) {
4412 Check(ExtractValueInst::getIndexedType(EVI
.getAggregateOperand()->getType(),
4413 EVI
.getIndices()) == EVI
.getType(),
4414 "Invalid ExtractValueInst operands!", &EVI
);
4416 visitInstruction(EVI
);
4419 void Verifier::visitInsertValueInst(InsertValueInst
&IVI
) {
4420 Check(ExtractValueInst::getIndexedType(IVI
.getAggregateOperand()->getType(),
4421 IVI
.getIndices()) ==
4422 IVI
.getOperand(1)->getType(),
4423 "Invalid InsertValueInst operands!", &IVI
);
4425 visitInstruction(IVI
);
4428 static Value
*getParentPad(Value
*EHPad
) {
4429 if (auto *FPI
= dyn_cast
<FuncletPadInst
>(EHPad
))
4430 return FPI
->getParentPad();
4432 return cast
<CatchSwitchInst
>(EHPad
)->getParentPad();
4435 void Verifier::visitEHPadPredecessors(Instruction
&I
) {
4436 assert(I
.isEHPad());
4438 BasicBlock
*BB
= I
.getParent();
4439 Function
*F
= BB
->getParent();
4441 Check(BB
!= &F
->getEntryBlock(), "EH pad cannot be in entry block.", &I
);
4443 if (auto *LPI
= dyn_cast
<LandingPadInst
>(&I
)) {
4444 // The landingpad instruction defines its parent as a landing pad block. The
4445 // landing pad block may be branched to only by the unwind edge of an
4447 for (BasicBlock
*PredBB
: predecessors(BB
)) {
4448 const auto *II
= dyn_cast
<InvokeInst
>(PredBB
->getTerminator());
4449 Check(II
&& II
->getUnwindDest() == BB
&& II
->getNormalDest() != BB
,
4450 "Block containing LandingPadInst must be jumped to "
4451 "only by the unwind edge of an invoke.",
4456 if (auto *CPI
= dyn_cast
<CatchPadInst
>(&I
)) {
4457 if (!pred_empty(BB
))
4458 Check(BB
->getUniquePredecessor() == CPI
->getCatchSwitch()->getParent(),
4459 "Block containg CatchPadInst must be jumped to "
4460 "only by its catchswitch.",
4462 Check(BB
!= CPI
->getCatchSwitch()->getUnwindDest(),
4463 "Catchswitch cannot unwind to one of its catchpads",
4464 CPI
->getCatchSwitch(), CPI
);
4468 // Verify that each pred has a legal terminator with a legal to/from EH
4469 // pad relationship.
4470 Instruction
*ToPad
= &I
;
4471 Value
*ToPadParent
= getParentPad(ToPad
);
4472 for (BasicBlock
*PredBB
: predecessors(BB
)) {
4473 Instruction
*TI
= PredBB
->getTerminator();
4475 if (auto *II
= dyn_cast
<InvokeInst
>(TI
)) {
4476 Check(II
->getUnwindDest() == BB
&& II
->getNormalDest() != BB
,
4477 "EH pad must be jumped to via an unwind edge", ToPad
, II
);
4479 dyn_cast
<Function
>(II
->getCalledOperand()->stripPointerCasts());
4480 if (CalledFn
&& CalledFn
->isIntrinsic() && II
->doesNotThrow() &&
4481 !IntrinsicInst::mayLowerToFunctionCall(CalledFn
->getIntrinsicID()))
4483 if (auto Bundle
= II
->getOperandBundle(LLVMContext::OB_funclet
))
4484 FromPad
= Bundle
->Inputs
[0];
4486 FromPad
= ConstantTokenNone::get(II
->getContext());
4487 } else if (auto *CRI
= dyn_cast
<CleanupReturnInst
>(TI
)) {
4488 FromPad
= CRI
->getOperand(0);
4489 Check(FromPad
!= ToPadParent
, "A cleanupret must exit its cleanup", CRI
);
4490 } else if (auto *CSI
= dyn_cast
<CatchSwitchInst
>(TI
)) {
4493 Check(false, "EH pad must be jumped to via an unwind edge", ToPad
, TI
);
4496 // The edge may exit from zero or more nested pads.
4497 SmallSet
<Value
*, 8> Seen
;
4498 for (;; FromPad
= getParentPad(FromPad
)) {
4499 Check(FromPad
!= ToPad
,
4500 "EH pad cannot handle exceptions raised within it", FromPad
, TI
);
4501 if (FromPad
== ToPadParent
) {
4502 // This is a legal unwind edge.
4505 Check(!isa
<ConstantTokenNone
>(FromPad
),
4506 "A single unwind edge may only enter one EH pad", TI
);
4507 Check(Seen
.insert(FromPad
).second
, "EH pad jumps through a cycle of pads",
4510 // This will be diagnosed on the corresponding instruction already. We
4511 // need the extra check here to make sure getParentPad() works.
4512 Check(isa
<FuncletPadInst
>(FromPad
) || isa
<CatchSwitchInst
>(FromPad
),
4513 "Parent pad must be catchpad/cleanuppad/catchswitch", TI
);
4518 void Verifier::visitLandingPadInst(LandingPadInst
&LPI
) {
4519 // The landingpad instruction is ill-formed if it doesn't have any clauses and
4521 Check(LPI
.getNumClauses() > 0 || LPI
.isCleanup(),
4522 "LandingPadInst needs at least one clause or to be a cleanup.", &LPI
);
4524 visitEHPadPredecessors(LPI
);
4526 if (!LandingPadResultTy
)
4527 LandingPadResultTy
= LPI
.getType();
4529 Check(LandingPadResultTy
== LPI
.getType(),
4530 "The landingpad instruction should have a consistent result type "
4531 "inside a function.",
4534 Function
*F
= LPI
.getParent()->getParent();
4535 Check(F
->hasPersonalityFn(),
4536 "LandingPadInst needs to be in a function with a personality.", &LPI
);
4538 // The landingpad instruction must be the first non-PHI instruction in the
4540 Check(LPI
.getParent()->getLandingPadInst() == &LPI
,
4541 "LandingPadInst not the first non-PHI instruction in the block.", &LPI
);
4543 for (unsigned i
= 0, e
= LPI
.getNumClauses(); i
< e
; ++i
) {
4544 Constant
*Clause
= LPI
.getClause(i
);
4545 if (LPI
.isCatch(i
)) {
4546 Check(isa
<PointerType
>(Clause
->getType()),
4547 "Catch operand does not have pointer type!", &LPI
);
4549 Check(LPI
.isFilter(i
), "Clause is neither catch nor filter!", &LPI
);
4550 Check(isa
<ConstantArray
>(Clause
) || isa
<ConstantAggregateZero
>(Clause
),
4551 "Filter operand is not an array of constants!", &LPI
);
4555 visitInstruction(LPI
);
4558 void Verifier::visitResumeInst(ResumeInst
&RI
) {
4559 Check(RI
.getFunction()->hasPersonalityFn(),
4560 "ResumeInst needs to be in a function with a personality.", &RI
);
4562 if (!LandingPadResultTy
)
4563 LandingPadResultTy
= RI
.getValue()->getType();
4565 Check(LandingPadResultTy
== RI
.getValue()->getType(),
4566 "The resume instruction should have a consistent result type "
4567 "inside a function.",
4570 visitTerminator(RI
);
4573 void Verifier::visitCatchPadInst(CatchPadInst
&CPI
) {
4574 BasicBlock
*BB
= CPI
.getParent();
4576 Function
*F
= BB
->getParent();
4577 Check(F
->hasPersonalityFn(),
4578 "CatchPadInst needs to be in a function with a personality.", &CPI
);
4580 Check(isa
<CatchSwitchInst
>(CPI
.getParentPad()),
4581 "CatchPadInst needs to be directly nested in a CatchSwitchInst.",
4582 CPI
.getParentPad());
4584 // The catchpad instruction must be the first non-PHI instruction in the
4586 Check(BB
->getFirstNonPHI() == &CPI
,
4587 "CatchPadInst not the first non-PHI instruction in the block.", &CPI
);
4589 visitEHPadPredecessors(CPI
);
4590 visitFuncletPadInst(CPI
);
4593 void Verifier::visitCatchReturnInst(CatchReturnInst
&CatchReturn
) {
4594 Check(isa
<CatchPadInst
>(CatchReturn
.getOperand(0)),
4595 "CatchReturnInst needs to be provided a CatchPad", &CatchReturn
,
4596 CatchReturn
.getOperand(0));
4598 visitTerminator(CatchReturn
);
4601 void Verifier::visitCleanupPadInst(CleanupPadInst
&CPI
) {
4602 BasicBlock
*BB
= CPI
.getParent();
4604 Function
*F
= BB
->getParent();
4605 Check(F
->hasPersonalityFn(),
4606 "CleanupPadInst needs to be in a function with a personality.", &CPI
);
4608 // The cleanuppad instruction must be the first non-PHI instruction in the
4610 Check(BB
->getFirstNonPHI() == &CPI
,
4611 "CleanupPadInst not the first non-PHI instruction in the block.", &CPI
);
4613 auto *ParentPad
= CPI
.getParentPad();
4614 Check(isa
<ConstantTokenNone
>(ParentPad
) || isa
<FuncletPadInst
>(ParentPad
),
4615 "CleanupPadInst has an invalid parent.", &CPI
);
4617 visitEHPadPredecessors(CPI
);
4618 visitFuncletPadInst(CPI
);
4621 void Verifier::visitFuncletPadInst(FuncletPadInst
&FPI
) {
4622 User
*FirstUser
= nullptr;
4623 Value
*FirstUnwindPad
= nullptr;
4624 SmallVector
<FuncletPadInst
*, 8> Worklist({&FPI
});
4625 SmallSet
<FuncletPadInst
*, 8> Seen
;
4627 while (!Worklist
.empty()) {
4628 FuncletPadInst
*CurrentPad
= Worklist
.pop_back_val();
4629 Check(Seen
.insert(CurrentPad
).second
,
4630 "FuncletPadInst must not be nested within itself", CurrentPad
);
4631 Value
*UnresolvedAncestorPad
= nullptr;
4632 for (User
*U
: CurrentPad
->users()) {
4633 BasicBlock
*UnwindDest
;
4634 if (auto *CRI
= dyn_cast
<CleanupReturnInst
>(U
)) {
4635 UnwindDest
= CRI
->getUnwindDest();
4636 } else if (auto *CSI
= dyn_cast
<CatchSwitchInst
>(U
)) {
4637 // We allow catchswitch unwind to caller to nest
4638 // within an outer pad that unwinds somewhere else,
4639 // because catchswitch doesn't have a nounwind variant.
4640 // See e.g. SimplifyCFGOpt::SimplifyUnreachable.
4641 if (CSI
->unwindsToCaller())
4643 UnwindDest
= CSI
->getUnwindDest();
4644 } else if (auto *II
= dyn_cast
<InvokeInst
>(U
)) {
4645 UnwindDest
= II
->getUnwindDest();
4646 } else if (isa
<CallInst
>(U
)) {
4647 // Calls which don't unwind may be found inside funclet
4648 // pads that unwind somewhere else. We don't *require*
4649 // such calls to be annotated nounwind.
4651 } else if (auto *CPI
= dyn_cast
<CleanupPadInst
>(U
)) {
4652 // The unwind dest for a cleanup can only be found by
4653 // recursive search. Add it to the worklist, and we'll
4654 // search for its first use that determines where it unwinds.
4655 Worklist
.push_back(CPI
);
4658 Check(isa
<CatchReturnInst
>(U
), "Bogus funclet pad use", U
);
4665 UnwindPad
= UnwindDest
->getFirstNonPHI();
4666 if (!cast
<Instruction
>(UnwindPad
)->isEHPad())
4668 Value
*UnwindParent
= getParentPad(UnwindPad
);
4669 // Ignore unwind edges that don't exit CurrentPad.
4670 if (UnwindParent
== CurrentPad
)
4672 // Determine whether the original funclet pad is exited,
4673 // and if we are scanning nested pads determine how many
4674 // of them are exited so we can stop searching their
4676 Value
*ExitedPad
= CurrentPad
;
4679 if (ExitedPad
== &FPI
) {
4681 // Now we can resolve any ancestors of CurrentPad up to
4682 // FPI, but not including FPI since we need to make sure
4683 // to check all direct users of FPI for consistency.
4684 UnresolvedAncestorPad
= &FPI
;
4687 Value
*ExitedParent
= getParentPad(ExitedPad
);
4688 if (ExitedParent
== UnwindParent
) {
4689 // ExitedPad is the ancestor-most pad which this unwind
4690 // edge exits, so we can resolve up to it, meaning that
4691 // ExitedParent is the first ancestor still unresolved.
4692 UnresolvedAncestorPad
= ExitedParent
;
4695 ExitedPad
= ExitedParent
;
4696 } while (!isa
<ConstantTokenNone
>(ExitedPad
));
4698 // Unwinding to caller exits all pads.
4699 UnwindPad
= ConstantTokenNone::get(FPI
.getContext());
4701 UnresolvedAncestorPad
= &FPI
;
4705 // This unwind edge exits FPI. Make sure it agrees with other
4708 Check(UnwindPad
== FirstUnwindPad
,
4709 "Unwind edges out of a funclet "
4710 "pad must have the same unwind "
4712 &FPI
, U
, FirstUser
);
4715 FirstUnwindPad
= UnwindPad
;
4716 // Record cleanup sibling unwinds for verifySiblingFuncletUnwinds
4717 if (isa
<CleanupPadInst
>(&FPI
) && !isa
<ConstantTokenNone
>(UnwindPad
) &&
4718 getParentPad(UnwindPad
) == getParentPad(&FPI
))
4719 SiblingFuncletInfo
[&FPI
] = cast
<Instruction
>(U
);
4722 // Make sure we visit all uses of FPI, but for nested pads stop as
4723 // soon as we know where they unwind to.
4724 if (CurrentPad
!= &FPI
)
4727 if (UnresolvedAncestorPad
) {
4728 if (CurrentPad
== UnresolvedAncestorPad
) {
4729 // When CurrentPad is FPI itself, we don't mark it as resolved even if
4730 // we've found an unwind edge that exits it, because we need to verify
4731 // all direct uses of FPI.
4732 assert(CurrentPad
== &FPI
);
4735 // Pop off the worklist any nested pads that we've found an unwind
4736 // destination for. The pads on the worklist are the uncles,
4737 // great-uncles, etc. of CurrentPad. We've found an unwind destination
4738 // for all ancestors of CurrentPad up to but not including
4739 // UnresolvedAncestorPad.
4740 Value
*ResolvedPad
= CurrentPad
;
4741 while (!Worklist
.empty()) {
4742 Value
*UnclePad
= Worklist
.back();
4743 Value
*AncestorPad
= getParentPad(UnclePad
);
4744 // Walk ResolvedPad up the ancestor list until we either find the
4745 // uncle's parent or the last resolved ancestor.
4746 while (ResolvedPad
!= AncestorPad
) {
4747 Value
*ResolvedParent
= getParentPad(ResolvedPad
);
4748 if (ResolvedParent
== UnresolvedAncestorPad
) {
4751 ResolvedPad
= ResolvedParent
;
4753 // If the resolved ancestor search didn't find the uncle's parent,
4754 // then the uncle is not yet resolved.
4755 if (ResolvedPad
!= AncestorPad
)
4757 // This uncle is resolved, so pop it from the worklist.
4758 Worklist
.pop_back();
4763 if (FirstUnwindPad
) {
4764 if (auto *CatchSwitch
= dyn_cast
<CatchSwitchInst
>(FPI
.getParentPad())) {
4765 BasicBlock
*SwitchUnwindDest
= CatchSwitch
->getUnwindDest();
4766 Value
*SwitchUnwindPad
;
4767 if (SwitchUnwindDest
)
4768 SwitchUnwindPad
= SwitchUnwindDest
->getFirstNonPHI();
4770 SwitchUnwindPad
= ConstantTokenNone::get(FPI
.getContext());
4771 Check(SwitchUnwindPad
== FirstUnwindPad
,
4772 "Unwind edges out of a catch must have the same unwind dest as "
4773 "the parent catchswitch",
4774 &FPI
, FirstUser
, CatchSwitch
);
4778 visitInstruction(FPI
);
4781 void Verifier::visitCatchSwitchInst(CatchSwitchInst
&CatchSwitch
) {
4782 BasicBlock
*BB
= CatchSwitch
.getParent();
4784 Function
*F
= BB
->getParent();
4785 Check(F
->hasPersonalityFn(),
4786 "CatchSwitchInst needs to be in a function with a personality.",
4789 // The catchswitch instruction must be the first non-PHI instruction in the
4791 Check(BB
->getFirstNonPHI() == &CatchSwitch
,
4792 "CatchSwitchInst not the first non-PHI instruction in the block.",
4795 auto *ParentPad
= CatchSwitch
.getParentPad();
4796 Check(isa
<ConstantTokenNone
>(ParentPad
) || isa
<FuncletPadInst
>(ParentPad
),
4797 "CatchSwitchInst has an invalid parent.", ParentPad
);
4799 if (BasicBlock
*UnwindDest
= CatchSwitch
.getUnwindDest()) {
4800 Instruction
*I
= UnwindDest
->getFirstNonPHI();
4801 Check(I
->isEHPad() && !isa
<LandingPadInst
>(I
),
4802 "CatchSwitchInst must unwind to an EH block which is not a "
4806 // Record catchswitch sibling unwinds for verifySiblingFuncletUnwinds
4807 if (getParentPad(I
) == ParentPad
)
4808 SiblingFuncletInfo
[&CatchSwitch
] = &CatchSwitch
;
4811 Check(CatchSwitch
.getNumHandlers() != 0,
4812 "CatchSwitchInst cannot have empty handler list", &CatchSwitch
);
4814 for (BasicBlock
*Handler
: CatchSwitch
.handlers()) {
4815 Check(isa
<CatchPadInst
>(Handler
->getFirstNonPHI()),
4816 "CatchSwitchInst handlers must be catchpads", &CatchSwitch
, Handler
);
4819 visitEHPadPredecessors(CatchSwitch
);
4820 visitTerminator(CatchSwitch
);
4823 void Verifier::visitCleanupReturnInst(CleanupReturnInst
&CRI
) {
4824 Check(isa
<CleanupPadInst
>(CRI
.getOperand(0)),
4825 "CleanupReturnInst needs to be provided a CleanupPad", &CRI
,
4828 if (BasicBlock
*UnwindDest
= CRI
.getUnwindDest()) {
4829 Instruction
*I
= UnwindDest
->getFirstNonPHI();
4830 Check(I
->isEHPad() && !isa
<LandingPadInst
>(I
),
4831 "CleanupReturnInst must unwind to an EH block which is not a "
4836 visitTerminator(CRI
);
4839 void Verifier::verifyDominatesUse(Instruction
&I
, unsigned i
) {
4840 Instruction
*Op
= cast
<Instruction
>(I
.getOperand(i
));
4841 // If the we have an invalid invoke, don't try to compute the dominance.
4842 // We already reject it in the invoke specific checks and the dominance
4843 // computation doesn't handle multiple edges.
4844 if (InvokeInst
*II
= dyn_cast
<InvokeInst
>(Op
)) {
4845 if (II
->getNormalDest() == II
->getUnwindDest())
4849 // Quick check whether the def has already been encountered in the same block.
4850 // PHI nodes are not checked to prevent accepting preceding PHIs, because PHI
4851 // uses are defined to happen on the incoming edge, not at the instruction.
4853 // FIXME: If this operand is a MetadataAsValue (wrapping a LocalAsMetadata)
4854 // wrapping an SSA value, assert that we've already encountered it. See
4855 // related FIXME in Mapper::mapLocalAsMetadata in ValueMapper.cpp.
4856 if (!isa
<PHINode
>(I
) && InstsInThisBlock
.count(Op
))
4859 const Use
&U
= I
.getOperandUse(i
);
4860 Check(DT
.dominates(Op
, U
), "Instruction does not dominate all uses!", Op
, &I
);
4863 void Verifier::visitDereferenceableMetadata(Instruction
& I
, MDNode
* MD
) {
4864 Check(I
.getType()->isPointerTy(),
4865 "dereferenceable, dereferenceable_or_null "
4866 "apply only to pointer types",
4868 Check((isa
<LoadInst
>(I
) || isa
<IntToPtrInst
>(I
)),
4869 "dereferenceable, dereferenceable_or_null apply only to load"
4870 " and inttoptr instructions, use attributes for calls or invokes",
4872 Check(MD
->getNumOperands() == 1,
4873 "dereferenceable, dereferenceable_or_null "
4874 "take one operand!",
4876 ConstantInt
*CI
= mdconst::dyn_extract
<ConstantInt
>(MD
->getOperand(0));
4877 Check(CI
&& CI
->getType()->isIntegerTy(64),
4879 "dereferenceable_or_null metadata value must be an i64!",
4883 void Verifier::visitProfMetadata(Instruction
&I
, MDNode
*MD
) {
4884 Check(MD
->getNumOperands() >= 2,
4885 "!prof annotations should have no less than 2 operands", MD
);
4887 // Check first operand.
4888 Check(MD
->getOperand(0) != nullptr, "first operand should not be null", MD
);
4889 Check(isa
<MDString
>(MD
->getOperand(0)),
4890 "expected string with name of the !prof annotation", MD
);
4891 MDString
*MDS
= cast
<MDString
>(MD
->getOperand(0));
4892 StringRef ProfName
= MDS
->getString();
4894 // Check consistency of !prof branch_weights metadata.
4895 if (ProfName
== "branch_weights") {
4896 unsigned NumBranchWeights
= getNumBranchWeights(*MD
);
4897 if (isa
<InvokeInst
>(&I
)) {
4898 Check(NumBranchWeights
== 1 || NumBranchWeights
== 2,
4899 "Wrong number of InvokeInst branch_weights operands", MD
);
4901 unsigned ExpectedNumOperands
= 0;
4902 if (BranchInst
*BI
= dyn_cast
<BranchInst
>(&I
))
4903 ExpectedNumOperands
= BI
->getNumSuccessors();
4904 else if (SwitchInst
*SI
= dyn_cast
<SwitchInst
>(&I
))
4905 ExpectedNumOperands
= SI
->getNumSuccessors();
4906 else if (isa
<CallInst
>(&I
))
4907 ExpectedNumOperands
= 1;
4908 else if (IndirectBrInst
*IBI
= dyn_cast
<IndirectBrInst
>(&I
))
4909 ExpectedNumOperands
= IBI
->getNumDestinations();
4910 else if (isa
<SelectInst
>(&I
))
4911 ExpectedNumOperands
= 2;
4912 else if (CallBrInst
*CI
= dyn_cast
<CallBrInst
>(&I
))
4913 ExpectedNumOperands
= CI
->getNumSuccessors();
4915 CheckFailed("!prof branch_weights are not allowed for this instruction",
4918 Check(NumBranchWeights
== ExpectedNumOperands
, "Wrong number of operands",
4921 for (unsigned i
= getBranchWeightOffset(MD
); i
< MD
->getNumOperands();
4923 auto &MDO
= MD
->getOperand(i
);
4924 Check(MDO
, "second operand should not be null", MD
);
4925 Check(mdconst::dyn_extract
<ConstantInt
>(MDO
),
4926 "!prof brunch_weights operand is not a const int");
4931 void Verifier::visitDIAssignIDMetadata(Instruction
&I
, MDNode
*MD
) {
4932 assert(I
.hasMetadata(LLVMContext::MD_DIAssignID
));
4933 bool ExpectedInstTy
=
4934 isa
<AllocaInst
>(I
) || isa
<StoreInst
>(I
) || isa
<MemIntrinsic
>(I
);
4935 CheckDI(ExpectedInstTy
, "!DIAssignID attached to unexpected instruction kind",
4937 // Iterate over the MetadataAsValue uses of the DIAssignID - these should
4938 // only be found as DbgAssignIntrinsic operands.
4939 if (auto *AsValue
= MetadataAsValue::getIfExists(Context
, MD
)) {
4940 for (auto *User
: AsValue
->users()) {
4941 CheckDI(isa
<DbgAssignIntrinsic
>(User
),
4942 "!DIAssignID should only be used by llvm.dbg.assign intrinsics",
4944 // All of the dbg.assign intrinsics should be in the same function as I.
4945 if (auto *DAI
= dyn_cast
<DbgAssignIntrinsic
>(User
))
4946 CheckDI(DAI
->getFunction() == I
.getFunction(),
4947 "dbg.assign not in same function as inst", DAI
, &I
);
4950 for (DbgVariableRecord
*DVR
:
4951 cast
<DIAssignID
>(MD
)->getAllDbgVariableRecordUsers()) {
4952 CheckDI(DVR
->isDbgAssign(),
4953 "!DIAssignID should only be used by Assign DVRs.", MD
, DVR
);
4954 CheckDI(DVR
->getFunction() == I
.getFunction(),
4955 "DVRAssign not in same function as inst", DVR
, &I
);
4959 void Verifier::visitMMRAMetadata(Instruction
&I
, MDNode
*MD
) {
4960 Check(canInstructionHaveMMRAs(I
),
4961 "!mmra metadata attached to unexpected instruction kind", I
, MD
);
4963 // MMRA Metadata should either be a tag, e.g. !{!"foo", !"bar"}, or a
4964 // list of tags such as !2 in the following example:
4965 // !0 = !{!"a", !"b"}
4966 // !1 = !{!"c", !"d"}
4968 if (MMRAMetadata::isTagMD(MD
))
4971 Check(isa
<MDTuple
>(MD
), "!mmra expected to be a metadata tuple", I
, MD
);
4972 for (const MDOperand
&MDOp
: MD
->operands())
4973 Check(MMRAMetadata::isTagMD(MDOp
.get()),
4974 "!mmra metadata tuple operand is not an MMRA tag", I
, MDOp
.get());
4977 void Verifier::visitCallStackMetadata(MDNode
*MD
) {
4978 // Call stack metadata should consist of a list of at least 1 constant int
4979 // (representing a hash of the location).
4980 Check(MD
->getNumOperands() >= 1,
4981 "call stack metadata should have at least 1 operand", MD
);
4983 for (const auto &Op
: MD
->operands())
4984 Check(mdconst::dyn_extract_or_null
<ConstantInt
>(Op
),
4985 "call stack metadata operand should be constant integer", Op
);
4988 void Verifier::visitMemProfMetadata(Instruction
&I
, MDNode
*MD
) {
4989 Check(isa
<CallBase
>(I
), "!memprof metadata should only exist on calls", &I
);
4990 Check(MD
->getNumOperands() >= 1,
4991 "!memprof annotations should have at least 1 metadata operand "
4996 for (auto &MIBOp
: MD
->operands()) {
4997 MDNode
*MIB
= dyn_cast
<MDNode
>(MIBOp
);
4998 // The first operand of an MIB should be the call stack metadata.
4999 // There rest of the operands should be MDString tags, and there should be
5001 Check(MIB
->getNumOperands() >= 2,
5002 "Each !memprof MemInfoBlock should have at least 2 operands", MIB
);
5004 // Check call stack metadata (first operand).
5005 Check(MIB
->getOperand(0) != nullptr,
5006 "!memprof MemInfoBlock first operand should not be null", MIB
);
5007 Check(isa
<MDNode
>(MIB
->getOperand(0)),
5008 "!memprof MemInfoBlock first operand should be an MDNode", MIB
);
5009 MDNode
*StackMD
= dyn_cast
<MDNode
>(MIB
->getOperand(0));
5010 visitCallStackMetadata(StackMD
);
5012 // The next set of 1 or more operands should be MDString.
5014 for (; I
< MIB
->getNumOperands(); ++I
) {
5015 if (!isa
<MDString
>(MIB
->getOperand(I
))) {
5017 "!memprof MemInfoBlock second operand should be an MDString",
5023 // Any remaining should be MDNode that are pairs of integers
5024 for (; I
< MIB
->getNumOperands(); ++I
) {
5025 MDNode
*OpNode
= dyn_cast
<MDNode
>(MIB
->getOperand(I
));
5026 Check(OpNode
, "Not all !memprof MemInfoBlock operands 2 to N are MDNode",
5028 Check(OpNode
->getNumOperands() == 2,
5029 "Not all !memprof MemInfoBlock operands 2 to N are MDNode with 2 "
5032 // Check that all of Op's operands are ConstantInt.
5033 Check(llvm::all_of(OpNode
->operands(),
5034 [](const MDOperand
&Op
) {
5035 return mdconst::hasa
<ConstantInt
>(Op
);
5037 "Not all !memprof MemInfoBlock operands 2 to N are MDNode with "
5038 "ConstantInt operands",
5044 void Verifier::visitCallsiteMetadata(Instruction
&I
, MDNode
*MD
) {
5045 Check(isa
<CallBase
>(I
), "!callsite metadata should only exist on calls", &I
);
5046 // Verify the partial callstack annotated from memprof profiles. This callsite
5047 // is a part of a profiled allocation callstack.
5048 visitCallStackMetadata(MD
);
5051 void Verifier::visitAnnotationMetadata(MDNode
*Annotation
) {
5052 Check(isa
<MDTuple
>(Annotation
), "annotation must be a tuple");
5053 Check(Annotation
->getNumOperands() >= 1,
5054 "annotation must have at least one operand");
5055 for (const MDOperand
&Op
: Annotation
->operands()) {
5056 bool TupleOfStrings
=
5057 isa
<MDTuple
>(Op
.get()) &&
5058 all_of(cast
<MDTuple
>(Op
)->operands(), [](auto &Annotation
) {
5059 return isa
<MDString
>(Annotation
.get());
5061 Check(isa
<MDString
>(Op
.get()) || TupleOfStrings
,
5062 "operands must be a string or a tuple of strings");
5066 void Verifier::visitAliasScopeMetadata(const MDNode
*MD
) {
5067 unsigned NumOps
= MD
->getNumOperands();
5068 Check(NumOps
>= 2 && NumOps
<= 3, "scope must have two or three operands",
5070 Check(MD
->getOperand(0).get() == MD
|| isa
<MDString
>(MD
->getOperand(0)),
5071 "first scope operand must be self-referential or string", MD
);
5073 Check(isa
<MDString
>(MD
->getOperand(2)),
5074 "third scope operand must be string (if used)", MD
);
5076 MDNode
*Domain
= dyn_cast
<MDNode
>(MD
->getOperand(1));
5077 Check(Domain
!= nullptr, "second scope operand must be MDNode", MD
);
5079 unsigned NumDomainOps
= Domain
->getNumOperands();
5080 Check(NumDomainOps
>= 1 && NumDomainOps
<= 2,
5081 "domain must have one or two operands", Domain
);
5082 Check(Domain
->getOperand(0).get() == Domain
||
5083 isa
<MDString
>(Domain
->getOperand(0)),
5084 "first domain operand must be self-referential or string", Domain
);
5085 if (NumDomainOps
== 2)
5086 Check(isa
<MDString
>(Domain
->getOperand(1)),
5087 "second domain operand must be string (if used)", Domain
);
5090 void Verifier::visitAliasScopeListMetadata(const MDNode
*MD
) {
5091 for (const MDOperand
&Op
: MD
->operands()) {
5092 const MDNode
*OpMD
= dyn_cast
<MDNode
>(Op
);
5093 Check(OpMD
!= nullptr, "scope list must consist of MDNodes", MD
);
5094 visitAliasScopeMetadata(OpMD
);
5098 void Verifier::visitAccessGroupMetadata(const MDNode
*MD
) {
5099 auto IsValidAccessScope
= [](const MDNode
*MD
) {
5100 return MD
->getNumOperands() == 0 && MD
->isDistinct();
5103 // It must be either an access scope itself...
5104 if (IsValidAccessScope(MD
))
5107 // ...or a list of access scopes.
5108 for (const MDOperand
&Op
: MD
->operands()) {
5109 const MDNode
*OpMD
= dyn_cast
<MDNode
>(Op
);
5110 Check(OpMD
!= nullptr, "Access scope list must consist of MDNodes", MD
);
5111 Check(IsValidAccessScope(OpMD
),
5112 "Access scope list contains invalid access scope", MD
);
5116 /// verifyInstruction - Verify that an instruction is well formed.
5118 void Verifier::visitInstruction(Instruction
&I
) {
5119 BasicBlock
*BB
= I
.getParent();
5120 Check(BB
, "Instruction not embedded in basic block!", &I
);
5122 if (!isa
<PHINode
>(I
)) { // Check that non-phi nodes are not self referential
5123 for (User
*U
: I
.users()) {
5124 Check(U
!= (User
*)&I
|| !DT
.isReachableFromEntry(BB
),
5125 "Only PHI nodes may reference their own value!", &I
);
5129 // Check that void typed values don't have names
5130 Check(!I
.getType()->isVoidTy() || !I
.hasName(),
5131 "Instruction has a name, but provides a void value!", &I
);
5133 // Check that the return value of the instruction is either void or a legal
5135 Check(I
.getType()->isVoidTy() || I
.getType()->isFirstClassType(),
5136 "Instruction returns a non-scalar type!", &I
);
5138 // Check that the instruction doesn't produce metadata. Calls are already
5139 // checked against the callee type.
5140 Check(!I
.getType()->isMetadataTy() || isa
<CallInst
>(I
) || isa
<InvokeInst
>(I
),
5141 "Invalid use of metadata!", &I
);
5143 // Check that all uses of the instruction, if they are instructions
5144 // themselves, actually have parent basic blocks. If the use is not an
5145 // instruction, it is an error!
5146 for (Use
&U
: I
.uses()) {
5147 if (Instruction
*Used
= dyn_cast
<Instruction
>(U
.getUser()))
5148 Check(Used
->getParent() != nullptr,
5149 "Instruction referencing"
5150 " instruction not embedded in a basic block!",
5153 CheckFailed("Use of instruction is not an instruction!", U
);
5158 // Get a pointer to the call base of the instruction if it is some form of
5160 const CallBase
*CBI
= dyn_cast
<CallBase
>(&I
);
5162 for (unsigned i
= 0, e
= I
.getNumOperands(); i
!= e
; ++i
) {
5163 Check(I
.getOperand(i
) != nullptr, "Instruction has null operand!", &I
);
5165 // Check to make sure that only first-class-values are operands to
5167 if (!I
.getOperand(i
)->getType()->isFirstClassType()) {
5168 Check(false, "Instruction operands must be first-class values!", &I
);
5171 if (Function
*F
= dyn_cast
<Function
>(I
.getOperand(i
))) {
5172 // This code checks whether the function is used as the operand of a
5173 // clang_arc_attachedcall operand bundle.
5174 auto IsAttachedCallOperand
= [](Function
*F
, const CallBase
*CBI
,
5176 return CBI
&& CBI
->isOperandBundleOfType(
5177 LLVMContext::OB_clang_arc_attachedcall
, Idx
);
5180 // Check to make sure that the "address of" an intrinsic function is never
5181 // taken. Ignore cases where the address of the intrinsic function is used
5182 // as the argument of operand bundle "clang.arc.attachedcall" as those
5183 // cases are handled in verifyAttachedCallBundle.
5184 Check((!F
->isIntrinsic() ||
5185 (CBI
&& &CBI
->getCalledOperandUse() == &I
.getOperandUse(i
)) ||
5186 IsAttachedCallOperand(F
, CBI
, i
)),
5187 "Cannot take the address of an intrinsic!", &I
);
5188 Check(!F
->isIntrinsic() || isa
<CallInst
>(I
) ||
5189 F
->getIntrinsicID() == Intrinsic::donothing
||
5190 F
->getIntrinsicID() == Intrinsic::seh_try_begin
||
5191 F
->getIntrinsicID() == Intrinsic::seh_try_end
||
5192 F
->getIntrinsicID() == Intrinsic::seh_scope_begin
||
5193 F
->getIntrinsicID() == Intrinsic::seh_scope_end
||
5194 F
->getIntrinsicID() == Intrinsic::coro_resume
||
5195 F
->getIntrinsicID() == Intrinsic::coro_destroy
||
5196 F
->getIntrinsicID() == Intrinsic::coro_await_suspend_void
||
5197 F
->getIntrinsicID() == Intrinsic::coro_await_suspend_bool
||
5198 F
->getIntrinsicID() == Intrinsic::coro_await_suspend_handle
||
5199 F
->getIntrinsicID() ==
5200 Intrinsic::experimental_patchpoint_void
||
5201 F
->getIntrinsicID() == Intrinsic::experimental_patchpoint
||
5202 F
->getIntrinsicID() == Intrinsic::fake_use
||
5203 F
->getIntrinsicID() == Intrinsic::experimental_gc_statepoint
||
5204 F
->getIntrinsicID() == Intrinsic::wasm_rethrow
||
5205 IsAttachedCallOperand(F
, CBI
, i
),
5206 "Cannot invoke an intrinsic other than donothing, patchpoint, "
5207 "statepoint, coro_resume, coro_destroy or clang.arc.attachedcall",
5209 Check(F
->getParent() == &M
, "Referencing function in another module!", &I
,
5210 &M
, F
, F
->getParent());
5211 } else if (BasicBlock
*OpBB
= dyn_cast
<BasicBlock
>(I
.getOperand(i
))) {
5212 Check(OpBB
->getParent() == BB
->getParent(),
5213 "Referring to a basic block in another function!", &I
);
5214 } else if (Argument
*OpArg
= dyn_cast
<Argument
>(I
.getOperand(i
))) {
5215 Check(OpArg
->getParent() == BB
->getParent(),
5216 "Referring to an argument in another function!", &I
);
5217 } else if (GlobalValue
*GV
= dyn_cast
<GlobalValue
>(I
.getOperand(i
))) {
5218 Check(GV
->getParent() == &M
, "Referencing global in another module!", &I
,
5219 &M
, GV
, GV
->getParent());
5220 } else if (Instruction
*OpInst
= dyn_cast
<Instruction
>(I
.getOperand(i
))) {
5221 Check(OpInst
->getFunction() == BB
->getParent(),
5222 "Referring to an instruction in another function!", &I
);
5223 verifyDominatesUse(I
, i
);
5224 } else if (isa
<InlineAsm
>(I
.getOperand(i
))) {
5225 Check(CBI
&& &CBI
->getCalledOperandUse() == &I
.getOperandUse(i
),
5226 "Cannot take the address of an inline asm!", &I
);
5227 } else if (auto *CPA
= dyn_cast
<ConstantPtrAuth
>(I
.getOperand(i
))) {
5228 visitConstantExprsRecursively(CPA
);
5229 } else if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(I
.getOperand(i
))) {
5230 if (CE
->getType()->isPtrOrPtrVectorTy()) {
5231 // If we have a ConstantExpr pointer, we need to see if it came from an
5233 visitConstantExprsRecursively(CE
);
5238 if (MDNode
*MD
= I
.getMetadata(LLVMContext::MD_fpmath
)) {
5239 Check(I
.getType()->isFPOrFPVectorTy(),
5240 "fpmath requires a floating point result!", &I
);
5241 Check(MD
->getNumOperands() == 1, "fpmath takes one operand!", &I
);
5242 if (ConstantFP
*CFP0
=
5243 mdconst::dyn_extract_or_null
<ConstantFP
>(MD
->getOperand(0))) {
5244 const APFloat
&Accuracy
= CFP0
->getValueAPF();
5245 Check(&Accuracy
.getSemantics() == &APFloat::IEEEsingle(),
5246 "fpmath accuracy must have float type", &I
);
5247 Check(Accuracy
.isFiniteNonZero() && !Accuracy
.isNegative(),
5248 "fpmath accuracy not a positive number!", &I
);
5250 Check(false, "invalid fpmath accuracy!", &I
);
5254 if (MDNode
*Range
= I
.getMetadata(LLVMContext::MD_range
)) {
5255 Check(isa
<LoadInst
>(I
) || isa
<CallInst
>(I
) || isa
<InvokeInst
>(I
),
5256 "Ranges are only for loads, calls and invokes!", &I
);
5257 visitRangeMetadata(I
, Range
, I
.getType());
5260 if (MDNode
*Range
= I
.getMetadata(LLVMContext::MD_noalias_addrspace
)) {
5261 Check(isa
<LoadInst
>(I
) || isa
<StoreInst
>(I
) || isa
<AtomicRMWInst
>(I
) ||
5262 isa
<AtomicCmpXchgInst
>(I
) || isa
<CallInst
>(I
),
5263 "noalias.addrspace are only for memory operations!", &I
);
5264 visitNoaliasAddrspaceMetadata(I
, Range
, I
.getType());
5267 if (I
.hasMetadata(LLVMContext::MD_invariant_group
)) {
5268 Check(isa
<LoadInst
>(I
) || isa
<StoreInst
>(I
),
5269 "invariant.group metadata is only for loads and stores", &I
);
5272 if (MDNode
*MD
= I
.getMetadata(LLVMContext::MD_nonnull
)) {
5273 Check(I
.getType()->isPointerTy(), "nonnull applies only to pointer types",
5275 Check(isa
<LoadInst
>(I
),
5276 "nonnull applies only to load instructions, use attributes"
5277 " for calls or invokes",
5279 Check(MD
->getNumOperands() == 0, "nonnull metadata must be empty", &I
);
5282 if (MDNode
*MD
= I
.getMetadata(LLVMContext::MD_dereferenceable
))
5283 visitDereferenceableMetadata(I
, MD
);
5285 if (MDNode
*MD
= I
.getMetadata(LLVMContext::MD_dereferenceable_or_null
))
5286 visitDereferenceableMetadata(I
, MD
);
5288 if (MDNode
*TBAA
= I
.getMetadata(LLVMContext::MD_tbaa
))
5289 TBAAVerifyHelper
.visitTBAAMetadata(I
, TBAA
);
5291 if (MDNode
*MD
= I
.getMetadata(LLVMContext::MD_noalias
))
5292 visitAliasScopeListMetadata(MD
);
5293 if (MDNode
*MD
= I
.getMetadata(LLVMContext::MD_alias_scope
))
5294 visitAliasScopeListMetadata(MD
);
5296 if (MDNode
*MD
= I
.getMetadata(LLVMContext::MD_access_group
))
5297 visitAccessGroupMetadata(MD
);
5299 if (MDNode
*AlignMD
= I
.getMetadata(LLVMContext::MD_align
)) {
5300 Check(I
.getType()->isPointerTy(), "align applies only to pointer types",
5302 Check(isa
<LoadInst
>(I
),
5303 "align applies only to load instructions, "
5304 "use attributes for calls or invokes",
5306 Check(AlignMD
->getNumOperands() == 1, "align takes one operand!", &I
);
5307 ConstantInt
*CI
= mdconst::dyn_extract
<ConstantInt
>(AlignMD
->getOperand(0));
5308 Check(CI
&& CI
->getType()->isIntegerTy(64),
5309 "align metadata value must be an i64!", &I
);
5310 uint64_t Align
= CI
->getZExtValue();
5311 Check(isPowerOf2_64(Align
), "align metadata value must be a power of 2!",
5313 Check(Align
<= Value::MaximumAlignment
,
5314 "alignment is larger that implementation defined limit", &I
);
5317 if (MDNode
*MD
= I
.getMetadata(LLVMContext::MD_prof
))
5318 visitProfMetadata(I
, MD
);
5320 if (MDNode
*MD
= I
.getMetadata(LLVMContext::MD_memprof
))
5321 visitMemProfMetadata(I
, MD
);
5323 if (MDNode
*MD
= I
.getMetadata(LLVMContext::MD_callsite
))
5324 visitCallsiteMetadata(I
, MD
);
5326 if (MDNode
*MD
= I
.getMetadata(LLVMContext::MD_DIAssignID
))
5327 visitDIAssignIDMetadata(I
, MD
);
5329 if (MDNode
*MMRA
= I
.getMetadata(LLVMContext::MD_mmra
))
5330 visitMMRAMetadata(I
, MMRA
);
5332 if (MDNode
*Annotation
= I
.getMetadata(LLVMContext::MD_annotation
))
5333 visitAnnotationMetadata(Annotation
);
5335 if (MDNode
*N
= I
.getDebugLoc().getAsMDNode()) {
5336 CheckDI(isa
<DILocation
>(N
), "invalid !dbg metadata attachment", &I
, N
);
5337 visitMDNode(*N
, AreDebugLocsAllowed::Yes
);
5340 if (auto *DII
= dyn_cast
<DbgVariableIntrinsic
>(&I
)) {
5341 verifyFragmentExpression(*DII
);
5342 verifyNotEntryValue(*DII
);
5345 SmallVector
<std::pair
<unsigned, MDNode
*>, 4> MDs
;
5346 I
.getAllMetadata(MDs
);
5347 for (auto Attachment
: MDs
) {
5348 unsigned Kind
= Attachment
.first
;
5350 (Kind
== LLVMContext::MD_dbg
|| Kind
== LLVMContext::MD_loop
)
5351 ? AreDebugLocsAllowed::Yes
5352 : AreDebugLocsAllowed::No
;
5353 visitMDNode(*Attachment
.second
, AllowLocs
);
5356 InstsInThisBlock
.insert(&I
);
5359 /// Allow intrinsics to be verified in different ways.
5360 void Verifier::visitIntrinsicCall(Intrinsic::ID ID
, CallBase
&Call
) {
5361 Function
*IF
= Call
.getCalledFunction();
5362 Check(IF
->isDeclaration(), "Intrinsic functions should never be defined!",
5365 // Verify that the intrinsic prototype lines up with what the .td files
5367 FunctionType
*IFTy
= IF
->getFunctionType();
5368 bool IsVarArg
= IFTy
->isVarArg();
5370 SmallVector
<Intrinsic::IITDescriptor
, 8> Table
;
5371 getIntrinsicInfoTableEntries(ID
, Table
);
5372 ArrayRef
<Intrinsic::IITDescriptor
> TableRef
= Table
;
5374 // Walk the descriptors to extract overloaded types.
5375 SmallVector
<Type
*, 4> ArgTys
;
5376 Intrinsic::MatchIntrinsicTypesResult Res
=
5377 Intrinsic::matchIntrinsicSignature(IFTy
, TableRef
, ArgTys
);
5378 Check(Res
!= Intrinsic::MatchIntrinsicTypes_NoMatchRet
,
5379 "Intrinsic has incorrect return type!", IF
);
5380 Check(Res
!= Intrinsic::MatchIntrinsicTypes_NoMatchArg
,
5381 "Intrinsic has incorrect argument type!", IF
);
5383 // Verify if the intrinsic call matches the vararg property.
5385 Check(!Intrinsic::matchIntrinsicVarArg(IsVarArg
, TableRef
),
5386 "Intrinsic was not defined with variable arguments!", IF
);
5388 Check(!Intrinsic::matchIntrinsicVarArg(IsVarArg
, TableRef
),
5389 "Callsite was not defined with variable arguments!", IF
);
5391 // All descriptors should be absorbed by now.
5392 Check(TableRef
.empty(), "Intrinsic has too few arguments!", IF
);
5394 // Now that we have the intrinsic ID and the actual argument types (and we
5395 // know they are legal for the intrinsic!) get the intrinsic name through the
5396 // usual means. This allows us to verify the mangling of argument types into
5398 const std::string ExpectedName
=
5399 Intrinsic::getName(ID
, ArgTys
, IF
->getParent(), IFTy
);
5400 Check(ExpectedName
== IF
->getName(),
5401 "Intrinsic name not mangled correctly for type arguments! "
5406 // If the intrinsic takes MDNode arguments, verify that they are either global
5407 // or are local to *this* function.
5408 for (Value
*V
: Call
.args()) {
5409 if (auto *MD
= dyn_cast
<MetadataAsValue
>(V
))
5410 visitMetadataAsValue(*MD
, Call
.getCaller());
5411 if (auto *Const
= dyn_cast
<Constant
>(V
))
5412 Check(!Const
->getType()->isX86_AMXTy(),
5413 "const x86_amx is not allowed in argument!");
5419 case Intrinsic::assume
: {
5420 for (auto &Elem
: Call
.bundle_op_infos()) {
5421 unsigned ArgCount
= Elem
.End
- Elem
.Begin
;
5422 // Separate storage assumptions are special insofar as they're the only
5423 // operand bundles allowed on assumes that aren't parameter attributes.
5424 if (Elem
.Tag
->getKey() == "separate_storage") {
5425 Check(ArgCount
== 2,
5426 "separate_storage assumptions should have 2 arguments", Call
);
5427 Check(Call
.getOperand(Elem
.Begin
)->getType()->isPointerTy() &&
5428 Call
.getOperand(Elem
.Begin
+ 1)->getType()->isPointerTy(),
5429 "arguments to separate_storage assumptions should be pointers",
5433 Check(Elem
.Tag
->getKey() == "ignore" ||
5434 Attribute::isExistingAttribute(Elem
.Tag
->getKey()),
5435 "tags must be valid attribute names", Call
);
5436 Attribute::AttrKind Kind
=
5437 Attribute::getAttrKindFromName(Elem
.Tag
->getKey());
5438 if (Kind
== Attribute::Alignment
) {
5439 Check(ArgCount
<= 3 && ArgCount
>= 2,
5440 "alignment assumptions should have 2 or 3 arguments", Call
);
5441 Check(Call
.getOperand(Elem
.Begin
)->getType()->isPointerTy(),
5442 "first argument should be a pointer", Call
);
5443 Check(Call
.getOperand(Elem
.Begin
+ 1)->getType()->isIntegerTy(),
5444 "second argument should be an integer", Call
);
5446 Check(Call
.getOperand(Elem
.Begin
+ 2)->getType()->isIntegerTy(),
5447 "third argument should be an integer if present", Call
);
5450 Check(ArgCount
<= 2, "too many arguments", Call
);
5451 if (Kind
== Attribute::None
)
5453 if (Attribute::isIntAttrKind(Kind
)) {
5454 Check(ArgCount
== 2, "this attribute should have 2 arguments", Call
);
5455 Check(isa
<ConstantInt
>(Call
.getOperand(Elem
.Begin
+ 1)),
5456 "the second argument should be a constant integral value", Call
);
5457 } else if (Attribute::canUseAsParamAttr(Kind
)) {
5458 Check((ArgCount
) == 1, "this attribute should have one argument", Call
);
5459 } else if (Attribute::canUseAsFnAttr(Kind
)) {
5460 Check((ArgCount
) == 0, "this attribute has no argument", Call
);
5465 case Intrinsic::ucmp
:
5466 case Intrinsic::scmp
: {
5467 Type
*SrcTy
= Call
.getOperand(0)->getType();
5468 Type
*DestTy
= Call
.getType();
5470 Check(DestTy
->getScalarSizeInBits() >= 2,
5471 "result type must be at least 2 bits wide", Call
);
5473 bool IsDestTypeVector
= DestTy
->isVectorTy();
5474 Check(SrcTy
->isVectorTy() == IsDestTypeVector
,
5475 "ucmp/scmp argument and result types must both be either vector or "
5478 if (IsDestTypeVector
) {
5479 auto SrcVecLen
= cast
<VectorType
>(SrcTy
)->getElementCount();
5480 auto DestVecLen
= cast
<VectorType
>(DestTy
)->getElementCount();
5481 Check(SrcVecLen
== DestVecLen
,
5482 "return type and arguments must have the same number of "
5488 case Intrinsic::coro_id
: {
5489 auto *InfoArg
= Call
.getArgOperand(3)->stripPointerCasts();
5490 if (isa
<ConstantPointerNull
>(InfoArg
))
5492 auto *GV
= dyn_cast
<GlobalVariable
>(InfoArg
);
5493 Check(GV
&& GV
->isConstant() && GV
->hasDefinitiveInitializer(),
5494 "info argument of llvm.coro.id must refer to an initialized "
5496 Constant
*Init
= GV
->getInitializer();
5497 Check(isa
<ConstantStruct
>(Init
) || isa
<ConstantArray
>(Init
),
5498 "info argument of llvm.coro.id must refer to either a struct or "
5502 case Intrinsic::is_fpclass
: {
5503 const ConstantInt
*TestMask
= cast
<ConstantInt
>(Call
.getOperand(1));
5504 Check((TestMask
->getZExtValue() & ~static_cast<unsigned>(fcAllFlags
)) == 0,
5505 "unsupported bits for llvm.is.fpclass test mask");
5508 case Intrinsic::fptrunc_round
: {
5509 // Check the rounding mode
5510 Metadata
*MD
= nullptr;
5511 auto *MAV
= dyn_cast
<MetadataAsValue
>(Call
.getOperand(1));
5513 MD
= MAV
->getMetadata();
5515 Check(MD
!= nullptr, "missing rounding mode argument", Call
);
5517 Check(isa
<MDString
>(MD
),
5518 ("invalid value for llvm.fptrunc.round metadata operand"
5519 " (the operand should be a string)"),
5522 std::optional
<RoundingMode
> RoundMode
=
5523 convertStrToRoundingMode(cast
<MDString
>(MD
)->getString());
5524 Check(RoundMode
&& *RoundMode
!= RoundingMode::Dynamic
,
5525 "unsupported rounding mode argument", Call
);
5528 #define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
5529 #include "llvm/IR/VPIntrinsics.def"
5530 #undef BEGIN_REGISTER_VP_INTRINSIC
5531 visitVPIntrinsic(cast
<VPIntrinsic
>(Call
));
5533 #define INSTRUCTION(NAME, NARGS, ROUND_MODE, INTRINSIC) \
5534 case Intrinsic::INTRINSIC:
5535 #include "llvm/IR/ConstrainedOps.def"
5537 visitConstrainedFPIntrinsic(cast
<ConstrainedFPIntrinsic
>(Call
));
5539 case Intrinsic::dbg_declare
: // llvm.dbg.declare
5540 Check(isa
<MetadataAsValue
>(Call
.getArgOperand(0)),
5541 "invalid llvm.dbg.declare intrinsic call 1", Call
);
5542 visitDbgIntrinsic("declare", cast
<DbgVariableIntrinsic
>(Call
));
5544 case Intrinsic::dbg_value
: // llvm.dbg.value
5545 visitDbgIntrinsic("value", cast
<DbgVariableIntrinsic
>(Call
));
5547 case Intrinsic::dbg_assign
: // llvm.dbg.assign
5548 visitDbgIntrinsic("assign", cast
<DbgVariableIntrinsic
>(Call
));
5550 case Intrinsic::dbg_label
: // llvm.dbg.label
5551 visitDbgLabelIntrinsic("label", cast
<DbgLabelInst
>(Call
));
5553 case Intrinsic::memcpy
:
5554 case Intrinsic::memcpy_inline
:
5555 case Intrinsic::memmove
:
5556 case Intrinsic::memset
:
5557 case Intrinsic::memset_inline
:
5558 case Intrinsic::experimental_memset_pattern
: {
5561 case Intrinsic::memcpy_element_unordered_atomic
:
5562 case Intrinsic::memmove_element_unordered_atomic
:
5563 case Intrinsic::memset_element_unordered_atomic
: {
5564 const auto *AMI
= cast
<AtomicMemIntrinsic
>(&Call
);
5566 ConstantInt
*ElementSizeCI
=
5567 cast
<ConstantInt
>(AMI
->getRawElementSizeInBytes());
5568 const APInt
&ElementSizeVal
= ElementSizeCI
->getValue();
5569 Check(ElementSizeVal
.isPowerOf2(),
5570 "element size of the element-wise atomic memory intrinsic "
5571 "must be a power of 2",
5574 auto IsValidAlignment
= [&](MaybeAlign Alignment
) {
5575 return Alignment
&& ElementSizeVal
.ule(Alignment
->value());
5577 Check(IsValidAlignment(AMI
->getDestAlign()),
5578 "incorrect alignment of the destination argument", Call
);
5579 if (const auto *AMT
= dyn_cast
<AtomicMemTransferInst
>(AMI
)) {
5580 Check(IsValidAlignment(AMT
->getSourceAlign()),
5581 "incorrect alignment of the source argument", Call
);
5585 case Intrinsic::call_preallocated_setup
: {
5586 auto *NumArgs
= dyn_cast
<ConstantInt
>(Call
.getArgOperand(0));
5587 Check(NumArgs
!= nullptr,
5588 "llvm.call.preallocated.setup argument must be a constant");
5589 bool FoundCall
= false;
5590 for (User
*U
: Call
.users()) {
5591 auto *UseCall
= dyn_cast
<CallBase
>(U
);
5592 Check(UseCall
!= nullptr,
5593 "Uses of llvm.call.preallocated.setup must be calls");
5594 const Function
*Fn
= UseCall
->getCalledFunction();
5595 if (Fn
&& Fn
->getIntrinsicID() == Intrinsic::call_preallocated_arg
) {
5596 auto *AllocArgIndex
= dyn_cast
<ConstantInt
>(UseCall
->getArgOperand(1));
5597 Check(AllocArgIndex
!= nullptr,
5598 "llvm.call.preallocated.alloc arg index must be a constant");
5599 auto AllocArgIndexInt
= AllocArgIndex
->getValue();
5600 Check(AllocArgIndexInt
.sge(0) &&
5601 AllocArgIndexInt
.slt(NumArgs
->getValue()),
5602 "llvm.call.preallocated.alloc arg index must be between 0 and "
5604 "llvm.call.preallocated.setup's argument count");
5605 } else if (Fn
&& Fn
->getIntrinsicID() ==
5606 Intrinsic::call_preallocated_teardown
) {
5609 Check(!FoundCall
, "Can have at most one call corresponding to a "
5610 "llvm.call.preallocated.setup");
5612 size_t NumPreallocatedArgs
= 0;
5613 for (unsigned i
= 0; i
< UseCall
->arg_size(); i
++) {
5614 if (UseCall
->paramHasAttr(i
, Attribute::Preallocated
)) {
5615 ++NumPreallocatedArgs
;
5618 Check(NumPreallocatedArgs
!= 0,
5619 "cannot use preallocated intrinsics on a call without "
5620 "preallocated arguments");
5621 Check(NumArgs
->equalsInt(NumPreallocatedArgs
),
5622 "llvm.call.preallocated.setup arg size must be equal to number "
5623 "of preallocated arguments "
5626 // getOperandBundle() cannot be called if more than one of the operand
5627 // bundle exists. There is already a check elsewhere for this, so skip
5628 // here if we see more than one.
5629 if (UseCall
->countOperandBundlesOfType(LLVMContext::OB_preallocated
) >
5633 auto PreallocatedBundle
=
5634 UseCall
->getOperandBundle(LLVMContext::OB_preallocated
);
5635 Check(PreallocatedBundle
,
5636 "Use of llvm.call.preallocated.setup outside intrinsics "
5637 "must be in \"preallocated\" operand bundle");
5638 Check(PreallocatedBundle
->Inputs
.front().get() == &Call
,
5639 "preallocated bundle must have token from corresponding "
5640 "llvm.call.preallocated.setup");
5645 case Intrinsic::call_preallocated_arg
: {
5646 auto *Token
= dyn_cast
<CallBase
>(Call
.getArgOperand(0));
5647 Check(Token
&& Token
->getCalledFunction()->getIntrinsicID() ==
5648 Intrinsic::call_preallocated_setup
,
5649 "llvm.call.preallocated.arg token argument must be a "
5650 "llvm.call.preallocated.setup");
5651 Check(Call
.hasFnAttr(Attribute::Preallocated
),
5652 "llvm.call.preallocated.arg must be called with a \"preallocated\" "
5653 "call site attribute");
5656 case Intrinsic::call_preallocated_teardown
: {
5657 auto *Token
= dyn_cast
<CallBase
>(Call
.getArgOperand(0));
5658 Check(Token
&& Token
->getCalledFunction()->getIntrinsicID() ==
5659 Intrinsic::call_preallocated_setup
,
5660 "llvm.call.preallocated.teardown token argument must be a "
5661 "llvm.call.preallocated.setup");
5664 case Intrinsic::gcroot
:
5665 case Intrinsic::gcwrite
:
5666 case Intrinsic::gcread
:
5667 if (ID
== Intrinsic::gcroot
) {
5669 dyn_cast
<AllocaInst
>(Call
.getArgOperand(0)->stripPointerCasts());
5670 Check(AI
, "llvm.gcroot parameter #1 must be an alloca.", Call
);
5671 Check(isa
<Constant
>(Call
.getArgOperand(1)),
5672 "llvm.gcroot parameter #2 must be a constant.", Call
);
5673 if (!AI
->getAllocatedType()->isPointerTy()) {
5674 Check(!isa
<ConstantPointerNull
>(Call
.getArgOperand(1)),
5675 "llvm.gcroot parameter #1 must either be a pointer alloca, "
5676 "or argument #2 must be a non-null constant.",
5681 Check(Call
.getParent()->getParent()->hasGC(),
5682 "Enclosing function does not use GC.", Call
);
5684 case Intrinsic::init_trampoline
:
5685 Check(isa
<Function
>(Call
.getArgOperand(1)->stripPointerCasts()),
5686 "llvm.init_trampoline parameter #2 must resolve to a function.",
5689 case Intrinsic::prefetch
:
5690 Check(cast
<ConstantInt
>(Call
.getArgOperand(1))->getZExtValue() < 2,
5691 "rw argument to llvm.prefetch must be 0-1", Call
);
5692 Check(cast
<ConstantInt
>(Call
.getArgOperand(2))->getZExtValue() < 4,
5693 "locality argument to llvm.prefetch must be 0-3", Call
);
5694 Check(cast
<ConstantInt
>(Call
.getArgOperand(3))->getZExtValue() < 2,
5695 "cache type argument to llvm.prefetch must be 0-1", Call
);
5697 case Intrinsic::stackprotector
:
5698 Check(isa
<AllocaInst
>(Call
.getArgOperand(1)->stripPointerCasts()),
5699 "llvm.stackprotector parameter #2 must resolve to an alloca.", Call
);
5701 case Intrinsic::localescape
: {
5702 BasicBlock
*BB
= Call
.getParent();
5703 Check(BB
->isEntryBlock(), "llvm.localescape used outside of entry block",
5705 Check(!SawFrameEscape
, "multiple calls to llvm.localescape in one function",
5707 for (Value
*Arg
: Call
.args()) {
5708 if (isa
<ConstantPointerNull
>(Arg
))
5709 continue; // Null values are allowed as placeholders.
5710 auto *AI
= dyn_cast
<AllocaInst
>(Arg
->stripPointerCasts());
5711 Check(AI
&& AI
->isStaticAlloca(),
5712 "llvm.localescape only accepts static allocas", Call
);
5714 FrameEscapeInfo
[BB
->getParent()].first
= Call
.arg_size();
5715 SawFrameEscape
= true;
5718 case Intrinsic::localrecover
: {
5719 Value
*FnArg
= Call
.getArgOperand(0)->stripPointerCasts();
5720 Function
*Fn
= dyn_cast
<Function
>(FnArg
);
5721 Check(Fn
&& !Fn
->isDeclaration(),
5722 "llvm.localrecover first "
5723 "argument must be function defined in this module",
5725 auto *IdxArg
= cast
<ConstantInt
>(Call
.getArgOperand(2));
5726 auto &Entry
= FrameEscapeInfo
[Fn
];
5727 Entry
.second
= unsigned(
5728 std::max(uint64_t(Entry
.second
), IdxArg
->getLimitedValue(~0U) + 1));
5732 case Intrinsic::experimental_gc_statepoint
:
5733 if (auto *CI
= dyn_cast
<CallInst
>(&Call
))
5734 Check(!CI
->isInlineAsm(),
5735 "gc.statepoint support for inline assembly unimplemented", CI
);
5736 Check(Call
.getParent()->getParent()->hasGC(),
5737 "Enclosing function does not use GC.", Call
);
5739 verifyStatepoint(Call
);
5741 case Intrinsic::experimental_gc_result
: {
5742 Check(Call
.getParent()->getParent()->hasGC(),
5743 "Enclosing function does not use GC.", Call
);
5745 auto *Statepoint
= Call
.getArgOperand(0);
5746 if (isa
<UndefValue
>(Statepoint
))
5749 // Are we tied to a statepoint properly?
5750 const auto *StatepointCall
= dyn_cast
<CallBase
>(Statepoint
);
5751 const Function
*StatepointFn
=
5752 StatepointCall
? StatepointCall
->getCalledFunction() : nullptr;
5753 Check(StatepointFn
&& StatepointFn
->isDeclaration() &&
5754 StatepointFn
->getIntrinsicID() ==
5755 Intrinsic::experimental_gc_statepoint
,
5756 "gc.result operand #1 must be from a statepoint", Call
,
5757 Call
.getArgOperand(0));
5759 // Check that result type matches wrapped callee.
5760 auto *TargetFuncType
=
5761 cast
<FunctionType
>(StatepointCall
->getParamElementType(2));
5762 Check(Call
.getType() == TargetFuncType
->getReturnType(),
5763 "gc.result result type does not match wrapped callee", Call
);
5766 case Intrinsic::experimental_gc_relocate
: {
5767 Check(Call
.arg_size() == 3, "wrong number of arguments", Call
);
5769 Check(isa
<PointerType
>(Call
.getType()->getScalarType()),
5770 "gc.relocate must return a pointer or a vector of pointers", Call
);
5772 // Check that this relocate is correctly tied to the statepoint
5774 // This is case for relocate on the unwinding path of an invoke statepoint
5775 if (LandingPadInst
*LandingPad
=
5776 dyn_cast
<LandingPadInst
>(Call
.getArgOperand(0))) {
5778 const BasicBlock
*InvokeBB
=
5779 LandingPad
->getParent()->getUniquePredecessor();
5781 // Landingpad relocates should have only one predecessor with invoke
5782 // statepoint terminator
5783 Check(InvokeBB
, "safepoints should have unique landingpads",
5784 LandingPad
->getParent());
5785 Check(InvokeBB
->getTerminator(), "safepoint block should be well formed",
5787 Check(isa
<GCStatepointInst
>(InvokeBB
->getTerminator()),
5788 "gc relocate should be linked to a statepoint", InvokeBB
);
5790 // In all other cases relocate should be tied to the statepoint directly.
5791 // This covers relocates on a normal return path of invoke statepoint and
5792 // relocates of a call statepoint.
5793 auto *Token
= Call
.getArgOperand(0);
5794 Check(isa
<GCStatepointInst
>(Token
) || isa
<UndefValue
>(Token
),
5795 "gc relocate is incorrectly tied to the statepoint", Call
, Token
);
5798 // Verify rest of the relocate arguments.
5799 const Value
&StatepointCall
= *cast
<GCRelocateInst
>(Call
).getStatepoint();
5801 // Both the base and derived must be piped through the safepoint.
5802 Value
*Base
= Call
.getArgOperand(1);
5803 Check(isa
<ConstantInt
>(Base
),
5804 "gc.relocate operand #2 must be integer offset", Call
);
5806 Value
*Derived
= Call
.getArgOperand(2);
5807 Check(isa
<ConstantInt
>(Derived
),
5808 "gc.relocate operand #3 must be integer offset", Call
);
5810 const uint64_t BaseIndex
= cast
<ConstantInt
>(Base
)->getZExtValue();
5811 const uint64_t DerivedIndex
= cast
<ConstantInt
>(Derived
)->getZExtValue();
5814 if (isa
<UndefValue
>(StatepointCall
))
5816 if (auto Opt
= cast
<GCStatepointInst
>(StatepointCall
)
5817 .getOperandBundle(LLVMContext::OB_gc_live
)) {
5818 Check(BaseIndex
< Opt
->Inputs
.size(),
5819 "gc.relocate: statepoint base index out of bounds", Call
);
5820 Check(DerivedIndex
< Opt
->Inputs
.size(),
5821 "gc.relocate: statepoint derived index out of bounds", Call
);
5824 // Relocated value must be either a pointer type or vector-of-pointer type,
5825 // but gc_relocate does not need to return the same pointer type as the
5826 // relocated pointer. It can be casted to the correct type later if it's
5827 // desired. However, they must have the same address space and 'vectorness'
5828 GCRelocateInst
&Relocate
= cast
<GCRelocateInst
>(Call
);
5829 auto *ResultType
= Call
.getType();
5830 auto *DerivedType
= Relocate
.getDerivedPtr()->getType();
5831 auto *BaseType
= Relocate
.getBasePtr()->getType();
5833 Check(BaseType
->isPtrOrPtrVectorTy(),
5834 "gc.relocate: relocated value must be a pointer", Call
);
5835 Check(DerivedType
->isPtrOrPtrVectorTy(),
5836 "gc.relocate: relocated value must be a pointer", Call
);
5838 Check(ResultType
->isVectorTy() == DerivedType
->isVectorTy(),
5839 "gc.relocate: vector relocates to vector and pointer to pointer",
5842 ResultType
->getPointerAddressSpace() ==
5843 DerivedType
->getPointerAddressSpace(),
5844 "gc.relocate: relocating a pointer shouldn't change its address space",
5847 auto GC
= llvm::getGCStrategy(Relocate
.getFunction()->getGC());
5848 Check(GC
, "gc.relocate: calling function must have GCStrategy",
5849 Call
.getFunction());
5851 auto isGCPtr
= [&GC
](Type
*PTy
) {
5852 return GC
->isGCManagedPointer(PTy
->getScalarType()).value_or(true);
5854 Check(isGCPtr(ResultType
), "gc.relocate: must return gc pointer", Call
);
5855 Check(isGCPtr(BaseType
),
5856 "gc.relocate: relocated value must be a gc pointer", Call
);
5857 Check(isGCPtr(DerivedType
),
5858 "gc.relocate: relocated value must be a gc pointer", Call
);
5862 case Intrinsic::experimental_patchpoint
: {
5863 if (Call
.getCallingConv() == CallingConv::AnyReg
) {
5864 Check(Call
.getType()->isSingleValueType(),
5865 "patchpoint: invalid return type used with anyregcc", Call
);
5869 case Intrinsic::eh_exceptioncode
:
5870 case Intrinsic::eh_exceptionpointer
: {
5871 Check(isa
<CatchPadInst
>(Call
.getArgOperand(0)),
5872 "eh.exceptionpointer argument must be a catchpad", Call
);
5875 case Intrinsic::get_active_lane_mask
: {
5876 Check(Call
.getType()->isVectorTy(),
5877 "get_active_lane_mask: must return a "
5880 auto *ElemTy
= Call
.getType()->getScalarType();
5881 Check(ElemTy
->isIntegerTy(1),
5882 "get_active_lane_mask: element type is not "
5887 case Intrinsic::experimental_get_vector_length
: {
5888 ConstantInt
*VF
= cast
<ConstantInt
>(Call
.getArgOperand(1));
5889 Check(!VF
->isNegative() && !VF
->isZero(),
5890 "get_vector_length: VF must be positive", Call
);
5893 case Intrinsic::masked_load
: {
5894 Check(Call
.getType()->isVectorTy(), "masked_load: must return a vector",
5897 ConstantInt
*Alignment
= cast
<ConstantInt
>(Call
.getArgOperand(1));
5898 Value
*Mask
= Call
.getArgOperand(2);
5899 Value
*PassThru
= Call
.getArgOperand(3);
5900 Check(Mask
->getType()->isVectorTy(), "masked_load: mask must be vector",
5902 Check(Alignment
->getValue().isPowerOf2(),
5903 "masked_load: alignment must be a power of 2", Call
);
5904 Check(PassThru
->getType() == Call
.getType(),
5905 "masked_load: pass through and return type must match", Call
);
5906 Check(cast
<VectorType
>(Mask
->getType())->getElementCount() ==
5907 cast
<VectorType
>(Call
.getType())->getElementCount(),
5908 "masked_load: vector mask must be same length as return", Call
);
5911 case Intrinsic::masked_store
: {
5912 Value
*Val
= Call
.getArgOperand(0);
5913 ConstantInt
*Alignment
= cast
<ConstantInt
>(Call
.getArgOperand(2));
5914 Value
*Mask
= Call
.getArgOperand(3);
5915 Check(Mask
->getType()->isVectorTy(), "masked_store: mask must be vector",
5917 Check(Alignment
->getValue().isPowerOf2(),
5918 "masked_store: alignment must be a power of 2", Call
);
5919 Check(cast
<VectorType
>(Mask
->getType())->getElementCount() ==
5920 cast
<VectorType
>(Val
->getType())->getElementCount(),
5921 "masked_store: vector mask must be same length as value", Call
);
5925 case Intrinsic::masked_gather
: {
5926 const APInt
&Alignment
=
5927 cast
<ConstantInt
>(Call
.getArgOperand(1))->getValue();
5928 Check(Alignment
.isZero() || Alignment
.isPowerOf2(),
5929 "masked_gather: alignment must be 0 or a power of 2", Call
);
5932 case Intrinsic::masked_scatter
: {
5933 const APInt
&Alignment
=
5934 cast
<ConstantInt
>(Call
.getArgOperand(2))->getValue();
5935 Check(Alignment
.isZero() || Alignment
.isPowerOf2(),
5936 "masked_scatter: alignment must be 0 or a power of 2", Call
);
5940 case Intrinsic::experimental_guard
: {
5941 Check(isa
<CallInst
>(Call
), "experimental_guard cannot be invoked", Call
);
5942 Check(Call
.countOperandBundlesOfType(LLVMContext::OB_deopt
) == 1,
5943 "experimental_guard must have exactly one "
5944 "\"deopt\" operand bundle");
5948 case Intrinsic::experimental_deoptimize
: {
5949 Check(isa
<CallInst
>(Call
), "experimental_deoptimize cannot be invoked",
5951 Check(Call
.countOperandBundlesOfType(LLVMContext::OB_deopt
) == 1,
5952 "experimental_deoptimize must have exactly one "
5953 "\"deopt\" operand bundle");
5954 Check(Call
.getType() == Call
.getFunction()->getReturnType(),
5955 "experimental_deoptimize return type must match caller return type");
5957 if (isa
<CallInst
>(Call
)) {
5958 auto *RI
= dyn_cast
<ReturnInst
>(Call
.getNextNode());
5960 "calls to experimental_deoptimize must be followed by a return");
5962 if (!Call
.getType()->isVoidTy() && RI
)
5963 Check(RI
->getReturnValue() == &Call
,
5964 "calls to experimental_deoptimize must be followed by a return "
5965 "of the value computed by experimental_deoptimize");
5970 case Intrinsic::vastart
: {
5971 Check(Call
.getFunction()->isVarArg(),
5972 "va_start called in a non-varargs function");
5975 case Intrinsic::vector_reduce_and
:
5976 case Intrinsic::vector_reduce_or
:
5977 case Intrinsic::vector_reduce_xor
:
5978 case Intrinsic::vector_reduce_add
:
5979 case Intrinsic::vector_reduce_mul
:
5980 case Intrinsic::vector_reduce_smax
:
5981 case Intrinsic::vector_reduce_smin
:
5982 case Intrinsic::vector_reduce_umax
:
5983 case Intrinsic::vector_reduce_umin
: {
5984 Type
*ArgTy
= Call
.getArgOperand(0)->getType();
5985 Check(ArgTy
->isIntOrIntVectorTy() && ArgTy
->isVectorTy(),
5986 "Intrinsic has incorrect argument type!");
5989 case Intrinsic::vector_reduce_fmax
:
5990 case Intrinsic::vector_reduce_fmin
: {
5991 Type
*ArgTy
= Call
.getArgOperand(0)->getType();
5992 Check(ArgTy
->isFPOrFPVectorTy() && ArgTy
->isVectorTy(),
5993 "Intrinsic has incorrect argument type!");
5996 case Intrinsic::vector_reduce_fadd
:
5997 case Intrinsic::vector_reduce_fmul
: {
5998 // Unlike the other reductions, the first argument is a start value. The
5999 // second argument is the vector to be reduced.
6000 Type
*ArgTy
= Call
.getArgOperand(1)->getType();
6001 Check(ArgTy
->isFPOrFPVectorTy() && ArgTy
->isVectorTy(),
6002 "Intrinsic has incorrect argument type!");
6005 case Intrinsic::smul_fix
:
6006 case Intrinsic::smul_fix_sat
:
6007 case Intrinsic::umul_fix
:
6008 case Intrinsic::umul_fix_sat
:
6009 case Intrinsic::sdiv_fix
:
6010 case Intrinsic::sdiv_fix_sat
:
6011 case Intrinsic::udiv_fix
:
6012 case Intrinsic::udiv_fix_sat
: {
6013 Value
*Op1
= Call
.getArgOperand(0);
6014 Value
*Op2
= Call
.getArgOperand(1);
6015 Check(Op1
->getType()->isIntOrIntVectorTy(),
6016 "first operand of [us][mul|div]_fix[_sat] must be an int type or "
6018 Check(Op2
->getType()->isIntOrIntVectorTy(),
6019 "second operand of [us][mul|div]_fix[_sat] must be an int type or "
6022 auto *Op3
= cast
<ConstantInt
>(Call
.getArgOperand(2));
6023 Check(Op3
->getType()->isIntegerTy(),
6024 "third operand of [us][mul|div]_fix[_sat] must be an int type");
6025 Check(Op3
->getBitWidth() <= 32,
6026 "third operand of [us][mul|div]_fix[_sat] must fit within 32 bits");
6028 if (ID
== Intrinsic::smul_fix
|| ID
== Intrinsic::smul_fix_sat
||
6029 ID
== Intrinsic::sdiv_fix
|| ID
== Intrinsic::sdiv_fix_sat
) {
6030 Check(Op3
->getZExtValue() < Op1
->getType()->getScalarSizeInBits(),
6031 "the scale of s[mul|div]_fix[_sat] must be less than the width of "
6034 Check(Op3
->getZExtValue() <= Op1
->getType()->getScalarSizeInBits(),
6035 "the scale of u[mul|div]_fix[_sat] must be less than or equal "
6036 "to the width of the operands");
6040 case Intrinsic::lrint
:
6041 case Intrinsic::llrint
:
6042 case Intrinsic::lround
:
6043 case Intrinsic::llround
: {
6044 Type
*ValTy
= Call
.getArgOperand(0)->getType();
6045 Type
*ResultTy
= Call
.getType();
6046 auto *VTy
= dyn_cast
<VectorType
>(ValTy
);
6047 auto *RTy
= dyn_cast
<VectorType
>(ResultTy
);
6048 Check(ValTy
->isFPOrFPVectorTy() && ResultTy
->isIntOrIntVectorTy(),
6049 ExpectedName
+ ": argument must be floating-point or vector "
6050 "of floating-points, and result must be integer or "
6051 "vector of integers",
6053 Check(ValTy
->isVectorTy() == ResultTy
->isVectorTy(),
6054 ExpectedName
+ ": argument and result disagree on vector use", &Call
);
6056 Check(VTy
->getElementCount() == RTy
->getElementCount(),
6057 ExpectedName
+ ": argument must be same length as result", &Call
);
6061 case Intrinsic::bswap
: {
6062 Type
*Ty
= Call
.getType();
6063 unsigned Size
= Ty
->getScalarSizeInBits();
6064 Check(Size
% 16 == 0, "bswap must be an even number of bytes", &Call
);
6067 case Intrinsic::invariant_start
: {
6068 ConstantInt
*InvariantSize
= dyn_cast
<ConstantInt
>(Call
.getArgOperand(0));
6069 Check(InvariantSize
&&
6070 (!InvariantSize
->isNegative() || InvariantSize
->isMinusOne()),
6071 "invariant_start parameter must be -1, 0 or a positive number",
6075 case Intrinsic::matrix_multiply
:
6076 case Intrinsic::matrix_transpose
:
6077 case Intrinsic::matrix_column_major_load
:
6078 case Intrinsic::matrix_column_major_store
: {
6079 Function
*IF
= Call
.getCalledFunction();
6080 ConstantInt
*Stride
= nullptr;
6081 ConstantInt
*NumRows
;
6082 ConstantInt
*NumColumns
;
6083 VectorType
*ResultTy
;
6084 Type
*Op0ElemTy
= nullptr;
6085 Type
*Op1ElemTy
= nullptr;
6087 case Intrinsic::matrix_multiply
: {
6088 NumRows
= cast
<ConstantInt
>(Call
.getArgOperand(2));
6089 ConstantInt
*N
= cast
<ConstantInt
>(Call
.getArgOperand(3));
6090 NumColumns
= cast
<ConstantInt
>(Call
.getArgOperand(4));
6091 Check(cast
<FixedVectorType
>(Call
.getArgOperand(0)->getType())
6092 ->getNumElements() ==
6093 NumRows
->getZExtValue() * N
->getZExtValue(),
6094 "First argument of a matrix operation does not match specified "
6096 Check(cast
<FixedVectorType
>(Call
.getArgOperand(1)->getType())
6097 ->getNumElements() ==
6098 N
->getZExtValue() * NumColumns
->getZExtValue(),
6099 "Second argument of a matrix operation does not match specified "
6102 ResultTy
= cast
<VectorType
>(Call
.getType());
6104 cast
<VectorType
>(Call
.getArgOperand(0)->getType())->getElementType();
6106 cast
<VectorType
>(Call
.getArgOperand(1)->getType())->getElementType();
6109 case Intrinsic::matrix_transpose
:
6110 NumRows
= cast
<ConstantInt
>(Call
.getArgOperand(1));
6111 NumColumns
= cast
<ConstantInt
>(Call
.getArgOperand(2));
6112 ResultTy
= cast
<VectorType
>(Call
.getType());
6114 cast
<VectorType
>(Call
.getArgOperand(0)->getType())->getElementType();
6116 case Intrinsic::matrix_column_major_load
: {
6117 Stride
= dyn_cast
<ConstantInt
>(Call
.getArgOperand(1));
6118 NumRows
= cast
<ConstantInt
>(Call
.getArgOperand(3));
6119 NumColumns
= cast
<ConstantInt
>(Call
.getArgOperand(4));
6120 ResultTy
= cast
<VectorType
>(Call
.getType());
6123 case Intrinsic::matrix_column_major_store
: {
6124 Stride
= dyn_cast
<ConstantInt
>(Call
.getArgOperand(2));
6125 NumRows
= cast
<ConstantInt
>(Call
.getArgOperand(4));
6126 NumColumns
= cast
<ConstantInt
>(Call
.getArgOperand(5));
6127 ResultTy
= cast
<VectorType
>(Call
.getArgOperand(0)->getType());
6129 cast
<VectorType
>(Call
.getArgOperand(0)->getType())->getElementType();
6133 llvm_unreachable("unexpected intrinsic");
6136 Check(ResultTy
->getElementType()->isIntegerTy() ||
6137 ResultTy
->getElementType()->isFloatingPointTy(),
6138 "Result type must be an integer or floating-point type!", IF
);
6141 Check(ResultTy
->getElementType() == Op0ElemTy
,
6142 "Vector element type mismatch of the result and first operand "
6147 Check(ResultTy
->getElementType() == Op1ElemTy
,
6148 "Vector element type mismatch of the result and second operand "
6152 Check(cast
<FixedVectorType
>(ResultTy
)->getNumElements() ==
6153 NumRows
->getZExtValue() * NumColumns
->getZExtValue(),
6154 "Result of a matrix operation does not fit in the returned vector!");
6157 Check(Stride
->getZExtValue() >= NumRows
->getZExtValue(),
6158 "Stride must be greater or equal than the number of rows!", IF
);
6162 case Intrinsic::vector_splice
: {
6163 VectorType
*VecTy
= cast
<VectorType
>(Call
.getType());
6164 int64_t Idx
= cast
<ConstantInt
>(Call
.getArgOperand(2))->getSExtValue();
6165 int64_t KnownMinNumElements
= VecTy
->getElementCount().getKnownMinValue();
6166 if (Call
.getParent() && Call
.getParent()->getParent()) {
6167 AttributeList Attrs
= Call
.getParent()->getParent()->getAttributes();
6168 if (Attrs
.hasFnAttr(Attribute::VScaleRange
))
6169 KnownMinNumElements
*= Attrs
.getFnAttrs().getVScaleRangeMin();
6171 Check((Idx
< 0 && std::abs(Idx
) <= KnownMinNumElements
) ||
6172 (Idx
>= 0 && Idx
< KnownMinNumElements
),
6173 "The splice index exceeds the range [-VL, VL-1] where VL is the "
6174 "known minimum number of elements in the vector. For scalable "
6175 "vectors the minimum number of elements is determined from "
6180 case Intrinsic::stepvector
: {
6181 VectorType
*VecTy
= dyn_cast
<VectorType
>(Call
.getType());
6182 Check(VecTy
&& VecTy
->getScalarType()->isIntegerTy() &&
6183 VecTy
->getScalarSizeInBits() >= 8,
6184 "stepvector only supported for vectors of integers "
6185 "with a bitwidth of at least 8.",
6189 case Intrinsic::experimental_vector_match
: {
6190 Value
*Op1
= Call
.getArgOperand(0);
6191 Value
*Op2
= Call
.getArgOperand(1);
6192 Value
*Mask
= Call
.getArgOperand(2);
6194 VectorType
*Op1Ty
= dyn_cast
<VectorType
>(Op1
->getType());
6195 VectorType
*Op2Ty
= dyn_cast
<VectorType
>(Op2
->getType());
6196 VectorType
*MaskTy
= dyn_cast
<VectorType
>(Mask
->getType());
6198 Check(Op1Ty
&& Op2Ty
&& MaskTy
, "Operands must be vectors.", &Call
);
6199 Check(isa
<FixedVectorType
>(Op2Ty
),
6200 "Second operand must be a fixed length vector.", &Call
);
6201 Check(Op1Ty
->getElementType()->isIntegerTy(),
6202 "First operand must be a vector of integers.", &Call
);
6203 Check(Op1Ty
->getElementType() == Op2Ty
->getElementType(),
6204 "First two operands must have the same element type.", &Call
);
6205 Check(Op1Ty
->getElementCount() == MaskTy
->getElementCount(),
6206 "First operand and mask must have the same number of elements.",
6208 Check(MaskTy
->getElementType()->isIntegerTy(1),
6209 "Mask must be a vector of i1's.", &Call
);
6210 Check(Call
.getType() == MaskTy
, "Return type must match the mask type.",
6214 case Intrinsic::vector_insert
: {
6215 Value
*Vec
= Call
.getArgOperand(0);
6216 Value
*SubVec
= Call
.getArgOperand(1);
6217 Value
*Idx
= Call
.getArgOperand(2);
6218 unsigned IdxN
= cast
<ConstantInt
>(Idx
)->getZExtValue();
6220 VectorType
*VecTy
= cast
<VectorType
>(Vec
->getType());
6221 VectorType
*SubVecTy
= cast
<VectorType
>(SubVec
->getType());
6223 ElementCount VecEC
= VecTy
->getElementCount();
6224 ElementCount SubVecEC
= SubVecTy
->getElementCount();
6225 Check(VecTy
->getElementType() == SubVecTy
->getElementType(),
6226 "vector_insert parameters must have the same element "
6229 Check(IdxN
% SubVecEC
.getKnownMinValue() == 0,
6230 "vector_insert index must be a constant multiple of "
6231 "the subvector's known minimum vector length.");
6233 // If this insertion is not the 'mixed' case where a fixed vector is
6234 // inserted into a scalable vector, ensure that the insertion of the
6235 // subvector does not overrun the parent vector.
6236 if (VecEC
.isScalable() == SubVecEC
.isScalable()) {
6237 Check(IdxN
< VecEC
.getKnownMinValue() &&
6238 IdxN
+ SubVecEC
.getKnownMinValue() <= VecEC
.getKnownMinValue(),
6239 "subvector operand of vector_insert would overrun the "
6240 "vector being inserted into.");
6244 case Intrinsic::vector_extract
: {
6245 Value
*Vec
= Call
.getArgOperand(0);
6246 Value
*Idx
= Call
.getArgOperand(1);
6247 unsigned IdxN
= cast
<ConstantInt
>(Idx
)->getZExtValue();
6249 VectorType
*ResultTy
= cast
<VectorType
>(Call
.getType());
6250 VectorType
*VecTy
= cast
<VectorType
>(Vec
->getType());
6252 ElementCount VecEC
= VecTy
->getElementCount();
6253 ElementCount ResultEC
= ResultTy
->getElementCount();
6255 Check(ResultTy
->getElementType() == VecTy
->getElementType(),
6256 "vector_extract result must have the same element "
6257 "type as the input vector.",
6259 Check(IdxN
% ResultEC
.getKnownMinValue() == 0,
6260 "vector_extract index must be a constant multiple of "
6261 "the result type's known minimum vector length.");
6263 // If this extraction is not the 'mixed' case where a fixed vector is
6264 // extracted from a scalable vector, ensure that the extraction does not
6265 // overrun the parent vector.
6266 if (VecEC
.isScalable() == ResultEC
.isScalable()) {
6267 Check(IdxN
< VecEC
.getKnownMinValue() &&
6268 IdxN
+ ResultEC
.getKnownMinValue() <= VecEC
.getKnownMinValue(),
6269 "vector_extract would overrun.");
6273 case Intrinsic::experimental_vector_partial_reduce_add
: {
6274 VectorType
*AccTy
= cast
<VectorType
>(Call
.getArgOperand(0)->getType());
6275 VectorType
*VecTy
= cast
<VectorType
>(Call
.getArgOperand(1)->getType());
6277 unsigned VecWidth
= VecTy
->getElementCount().getKnownMinValue();
6278 unsigned AccWidth
= AccTy
->getElementCount().getKnownMinValue();
6280 Check((VecWidth
% AccWidth
) == 0,
6281 "Invalid vector widths for partial "
6282 "reduction. The width of the input vector "
6283 "must be a positive integer multiple of "
6284 "the width of the accumulator vector.");
6287 case Intrinsic::experimental_noalias_scope_decl
: {
6288 NoAliasScopeDecls
.push_back(cast
<IntrinsicInst
>(&Call
));
6291 case Intrinsic::preserve_array_access_index
:
6292 case Intrinsic::preserve_struct_access_index
:
6293 case Intrinsic::aarch64_ldaxr
:
6294 case Intrinsic::aarch64_ldxr
:
6295 case Intrinsic::arm_ldaex
:
6296 case Intrinsic::arm_ldrex
: {
6297 Type
*ElemTy
= Call
.getParamElementType(0);
6298 Check(ElemTy
, "Intrinsic requires elementtype attribute on first argument.",
6302 case Intrinsic::aarch64_stlxr
:
6303 case Intrinsic::aarch64_stxr
:
6304 case Intrinsic::arm_stlex
:
6305 case Intrinsic::arm_strex
: {
6306 Type
*ElemTy
= Call
.getAttributes().getParamElementType(1);
6308 "Intrinsic requires elementtype attribute on second argument.",
6312 case Intrinsic::aarch64_prefetch
: {
6313 Check(cast
<ConstantInt
>(Call
.getArgOperand(1))->getZExtValue() < 2,
6314 "write argument to llvm.aarch64.prefetch must be 0 or 1", Call
);
6315 Check(cast
<ConstantInt
>(Call
.getArgOperand(2))->getZExtValue() < 4,
6316 "target argument to llvm.aarch64.prefetch must be 0-3", Call
);
6317 Check(cast
<ConstantInt
>(Call
.getArgOperand(3))->getZExtValue() < 2,
6318 "stream argument to llvm.aarch64.prefetch must be 0 or 1", Call
);
6319 Check(cast
<ConstantInt
>(Call
.getArgOperand(4))->getZExtValue() < 2,
6320 "isdata argument to llvm.aarch64.prefetch must be 0 or 1", Call
);
6323 case Intrinsic::callbr_landingpad
: {
6324 const auto *CBR
= dyn_cast
<CallBrInst
>(Call
.getOperand(0));
6325 Check(CBR
, "intrinstic requires callbr operand", &Call
);
6329 const BasicBlock
*LandingPadBB
= Call
.getParent();
6330 const BasicBlock
*PredBB
= LandingPadBB
->getUniquePredecessor();
6332 CheckFailed("Intrinsic in block must have 1 unique predecessor", &Call
);
6335 if (!isa
<CallBrInst
>(PredBB
->getTerminator())) {
6336 CheckFailed("Intrinsic must have corresponding callbr in predecessor",
6340 Check(llvm::is_contained(CBR
->getIndirectDests(), LandingPadBB
),
6341 "Intrinsic's corresponding callbr must have intrinsic's parent basic "
6342 "block in indirect destination list",
6344 const Instruction
&First
= *LandingPadBB
->begin();
6345 Check(&First
== &Call
, "No other instructions may proceed intrinsic",
6349 case Intrinsic::amdgcn_cs_chain
: {
6350 auto CallerCC
= Call
.getCaller()->getCallingConv();
6352 case CallingConv::AMDGPU_CS
:
6353 case CallingConv::AMDGPU_CS_Chain
:
6354 case CallingConv::AMDGPU_CS_ChainPreserve
:
6357 CheckFailed("Intrinsic can only be used from functions with the "
6358 "amdgpu_cs, amdgpu_cs_chain or amdgpu_cs_chain_preserve "
6359 "calling conventions",
6364 Check(Call
.paramHasAttr(2, Attribute::InReg
),
6365 "SGPR arguments must have the `inreg` attribute", &Call
);
6366 Check(!Call
.paramHasAttr(3, Attribute::InReg
),
6367 "VGPR arguments must not have the `inreg` attribute", &Call
);
6370 case Intrinsic::amdgcn_set_inactive_chain_arg
: {
6371 auto CallerCC
= Call
.getCaller()->getCallingConv();
6373 case CallingConv::AMDGPU_CS_Chain
:
6374 case CallingConv::AMDGPU_CS_ChainPreserve
:
6377 CheckFailed("Intrinsic can only be used from functions with the "
6378 "amdgpu_cs_chain or amdgpu_cs_chain_preserve "
6379 "calling conventions",
6384 unsigned InactiveIdx
= 1;
6385 Check(!Call
.paramHasAttr(InactiveIdx
, Attribute::InReg
),
6386 "Value for inactive lanes must not have the `inreg` attribute",
6388 Check(isa
<Argument
>(Call
.getArgOperand(InactiveIdx
)),
6389 "Value for inactive lanes must be a function argument", &Call
);
6390 Check(!cast
<Argument
>(Call
.getArgOperand(InactiveIdx
))->hasInRegAttr(),
6391 "Value for inactive lanes must be a VGPR function argument", &Call
);
6394 case Intrinsic::amdgcn_s_prefetch_data
: {
6396 AMDGPU::isFlatGlobalAddrSpace(
6397 Call
.getArgOperand(0)->getType()->getPointerAddressSpace()),
6398 "llvm.amdgcn.s.prefetch.data only supports global or constant memory");
6401 case Intrinsic::amdgcn_mfma_scale_f32_16x16x128_f8f6f4
:
6402 case Intrinsic::amdgcn_mfma_scale_f32_32x32x64_f8f6f4
: {
6403 Value
*Src0
= Call
.getArgOperand(0);
6404 Value
*Src1
= Call
.getArgOperand(1);
6406 uint64_t CBSZ
= cast
<ConstantInt
>(Call
.getArgOperand(3))->getZExtValue();
6407 uint64_t BLGP
= cast
<ConstantInt
>(Call
.getArgOperand(4))->getZExtValue();
6408 Check(CBSZ
<= 4, "invalid value for cbsz format", Call
,
6409 Call
.getArgOperand(3));
6410 Check(BLGP
<= 4, "invalid value for blgp format", Call
,
6411 Call
.getArgOperand(4));
6413 // AMDGPU::MFMAScaleFormats values
6414 auto getFormatNumRegs
= [](unsigned FormatVal
) {
6415 switch (FormatVal
) {
6425 llvm_unreachable("invalid format value");
6429 auto isValidSrcASrcBVector
= [](FixedVectorType
*Ty
) {
6430 if (!Ty
|| !Ty
->getElementType()->isIntegerTy(32))
6432 unsigned NumElts
= Ty
->getNumElements();
6433 return NumElts
== 4 || NumElts
== 6 || NumElts
== 8;
6436 auto *Src0Ty
= dyn_cast
<FixedVectorType
>(Src0
->getType());
6437 auto *Src1Ty
= dyn_cast
<FixedVectorType
>(Src1
->getType());
6438 Check(isValidSrcASrcBVector(Src0Ty
),
6439 "operand 0 must be 4, 6 or 8 element i32 vector", &Call
, Src0
);
6440 Check(isValidSrcASrcBVector(Src1Ty
),
6441 "operand 1 must be 4, 6 or 8 element i32 vector", &Call
, Src1
);
6443 // Permit excess registers for the format.
6444 Check(Src0Ty
->getNumElements() >= getFormatNumRegs(CBSZ
),
6445 "invalid vector type for format", &Call
, Src0
, Call
.getArgOperand(3));
6446 Check(Src1Ty
->getNumElements() >= getFormatNumRegs(BLGP
),
6447 "invalid vector type for format", &Call
, Src1
, Call
.getArgOperand(5));
6450 case Intrinsic::nvvm_setmaxnreg_inc_sync_aligned_u32
:
6451 case Intrinsic::nvvm_setmaxnreg_dec_sync_aligned_u32
: {
6452 Value
*V
= Call
.getArgOperand(0);
6453 unsigned RegCount
= cast
<ConstantInt
>(V
)->getZExtValue();
6454 Check(RegCount
% 8 == 0,
6455 "reg_count argument to nvvm.setmaxnreg must be in multiples of 8");
6456 Check((RegCount
>= 24 && RegCount
<= 256),
6457 "reg_count argument to nvvm.setmaxnreg must be within [24, 256]");
6460 case Intrinsic::experimental_convergence_entry
:
6461 case Intrinsic::experimental_convergence_anchor
:
6463 case Intrinsic::experimental_convergence_loop
:
6465 case Intrinsic::ptrmask
: {
6466 Type
*Ty0
= Call
.getArgOperand(0)->getType();
6467 Type
*Ty1
= Call
.getArgOperand(1)->getType();
6468 Check(Ty0
->isPtrOrPtrVectorTy(),
6469 "llvm.ptrmask intrinsic first argument must be pointer or vector "
6473 Ty0
->isVectorTy() == Ty1
->isVectorTy(),
6474 "llvm.ptrmask intrinsic arguments must be both scalars or both vectors",
6476 if (Ty0
->isVectorTy())
6477 Check(cast
<VectorType
>(Ty0
)->getElementCount() ==
6478 cast
<VectorType
>(Ty1
)->getElementCount(),
6479 "llvm.ptrmask intrinsic arguments must have the same number of "
6482 Check(DL
.getIndexTypeSizeInBits(Ty0
) == Ty1
->getScalarSizeInBits(),
6483 "llvm.ptrmask intrinsic second argument bitwidth must match "
6484 "pointer index type size of first argument",
6488 case Intrinsic::threadlocal_address
: {
6489 const Value
&Arg0
= *Call
.getArgOperand(0);
6490 Check(isa
<GlobalValue
>(Arg0
),
6491 "llvm.threadlocal.address first argument must be a GlobalValue");
6492 Check(cast
<GlobalValue
>(Arg0
).isThreadLocal(),
6493 "llvm.threadlocal.address operand isThreadLocal() must be true");
6496 case Intrinsic::nvvm_fence_proxy_tensormap_generic_acquire_cta
:
6497 case Intrinsic::nvvm_fence_proxy_tensormap_generic_acquire_cluster
:
6498 case Intrinsic::nvvm_fence_proxy_tensormap_generic_acquire_gpu
:
6499 case Intrinsic::nvvm_fence_proxy_tensormap_generic_acquire_sys
: {
6500 unsigned size
= cast
<ConstantInt
>(Call
.getArgOperand(1))->getZExtValue();
6501 Check(size
== 128, " The only supported value for size operand is 128");
6506 // Verify that there aren't any unmediated control transfers between funclets.
6507 if (IntrinsicInst::mayLowerToFunctionCall(ID
)) {
6508 Function
*F
= Call
.getParent()->getParent();
6509 if (F
->hasPersonalityFn() &&
6510 isScopedEHPersonality(classifyEHPersonality(F
->getPersonalityFn()))) {
6511 // Run EH funclet coloring on-demand and cache results for other intrinsic
6512 // calls in this function
6513 if (BlockEHFuncletColors
.empty())
6514 BlockEHFuncletColors
= colorEHFunclets(*F
);
6516 // Check for catch-/cleanup-pad in first funclet block
6517 bool InEHFunclet
= false;
6518 BasicBlock
*CallBB
= Call
.getParent();
6519 const ColorVector
&CV
= BlockEHFuncletColors
.find(CallBB
)->second
;
6520 assert(CV
.size() > 0 && "Uncolored block");
6521 for (BasicBlock
*ColorFirstBB
: CV
)
6522 if (dyn_cast_or_null
<FuncletPadInst
>(ColorFirstBB
->getFirstNonPHI()))
6525 // Check for funclet operand bundle
6526 bool HasToken
= false;
6527 for (unsigned I
= 0, E
= Call
.getNumOperandBundles(); I
!= E
; ++I
)
6528 if (Call
.getOperandBundleAt(I
).getTagID() == LLVMContext::OB_funclet
)
6531 // This would cause silent code truncation in WinEHPrepare
6533 Check(HasToken
, "Missing funclet token on intrinsic call", &Call
);
6538 /// Carefully grab the subprogram from a local scope.
6540 /// This carefully grabs the subprogram from a local scope, avoiding the
6541 /// built-in assertions that would typically fire.
6542 static DISubprogram
*getSubprogram(Metadata
*LocalScope
) {
6546 if (auto *SP
= dyn_cast
<DISubprogram
>(LocalScope
))
6549 if (auto *LB
= dyn_cast
<DILexicalBlockBase
>(LocalScope
))
6550 return getSubprogram(LB
->getRawScope());
6552 // Just return null; broken scope chains are checked elsewhere.
6553 assert(!isa
<DILocalScope
>(LocalScope
) && "Unknown type of local scope");
6557 void Verifier::visit(DbgLabelRecord
&DLR
) {
6558 CheckDI(isa
<DILabel
>(DLR
.getRawLabel()),
6559 "invalid #dbg_label intrinsic variable", &DLR
, DLR
.getRawLabel());
6561 // Ignore broken !dbg attachments; they're checked elsewhere.
6562 if (MDNode
*N
= DLR
.getDebugLoc().getAsMDNode())
6563 if (!isa
<DILocation
>(N
))
6566 BasicBlock
*BB
= DLR
.getParent();
6567 Function
*F
= BB
? BB
->getParent() : nullptr;
6569 // The scopes for variables and !dbg attachments must agree.
6570 DILabel
*Label
= DLR
.getLabel();
6571 DILocation
*Loc
= DLR
.getDebugLoc();
6572 CheckDI(Loc
, "#dbg_label record requires a !dbg attachment", &DLR
, BB
, F
);
6574 DISubprogram
*LabelSP
= getSubprogram(Label
->getRawScope());
6575 DISubprogram
*LocSP
= getSubprogram(Loc
->getRawScope());
6576 if (!LabelSP
|| !LocSP
)
6579 CheckDI(LabelSP
== LocSP
,
6580 "mismatched subprogram between #dbg_label label and !dbg attachment",
6581 &DLR
, BB
, F
, Label
, Label
->getScope()->getSubprogram(), Loc
,
6582 Loc
->getScope()->getSubprogram());
6585 void Verifier::visit(DbgVariableRecord
&DVR
) {
6586 BasicBlock
*BB
= DVR
.getParent();
6587 Function
*F
= BB
->getParent();
6589 CheckDI(DVR
.getType() == DbgVariableRecord::LocationType::Value
||
6590 DVR
.getType() == DbgVariableRecord::LocationType::Declare
||
6591 DVR
.getType() == DbgVariableRecord::LocationType::Assign
,
6592 "invalid #dbg record type", &DVR
, DVR
.getType());
6594 // The location for a DbgVariableRecord must be either a ValueAsMetadata,
6595 // DIArgList, or an empty MDNode (which is a legacy representation for an
6596 // "undef" location).
6597 auto *MD
= DVR
.getRawLocation();
6598 CheckDI(MD
&& (isa
<ValueAsMetadata
>(MD
) || isa
<DIArgList
>(MD
) ||
6599 (isa
<MDNode
>(MD
) && !cast
<MDNode
>(MD
)->getNumOperands())),
6600 "invalid #dbg record address/value", &DVR
, MD
);
6601 if (auto *VAM
= dyn_cast
<ValueAsMetadata
>(MD
))
6602 visitValueAsMetadata(*VAM
, F
);
6603 else if (auto *AL
= dyn_cast
<DIArgList
>(MD
))
6604 visitDIArgList(*AL
, F
);
6606 CheckDI(isa_and_nonnull
<DILocalVariable
>(DVR
.getRawVariable()),
6607 "invalid #dbg record variable", &DVR
, DVR
.getRawVariable());
6608 visitMDNode(*DVR
.getRawVariable(), AreDebugLocsAllowed::No
);
6610 CheckDI(isa_and_nonnull
<DIExpression
>(DVR
.getRawExpression()),
6611 "invalid #dbg record expression", &DVR
, DVR
.getRawExpression());
6612 visitMDNode(*DVR
.getExpression(), AreDebugLocsAllowed::No
);
6614 if (DVR
.isDbgAssign()) {
6615 CheckDI(isa_and_nonnull
<DIAssignID
>(DVR
.getRawAssignID()),
6616 "invalid #dbg_assign DIAssignID", &DVR
, DVR
.getRawAssignID());
6617 visitMDNode(*cast
<DIAssignID
>(DVR
.getRawAssignID()),
6618 AreDebugLocsAllowed::No
);
6620 const auto *RawAddr
= DVR
.getRawAddress();
6621 // Similarly to the location above, the address for an assign
6622 // DbgVariableRecord must be a ValueAsMetadata or an empty MDNode, which
6623 // represents an undef address.
6625 isa
<ValueAsMetadata
>(RawAddr
) ||
6626 (isa
<MDNode
>(RawAddr
) && !cast
<MDNode
>(RawAddr
)->getNumOperands()),
6627 "invalid #dbg_assign address", &DVR
, DVR
.getRawAddress());
6628 if (auto *VAM
= dyn_cast
<ValueAsMetadata
>(RawAddr
))
6629 visitValueAsMetadata(*VAM
, F
);
6631 CheckDI(isa_and_nonnull
<DIExpression
>(DVR
.getRawAddressExpression()),
6632 "invalid #dbg_assign address expression", &DVR
,
6633 DVR
.getRawAddressExpression());
6634 visitMDNode(*DVR
.getAddressExpression(), AreDebugLocsAllowed::No
);
6636 // All of the linked instructions should be in the same function as DVR.
6637 for (Instruction
*I
: at::getAssignmentInsts(&DVR
))
6638 CheckDI(DVR
.getFunction() == I
->getFunction(),
6639 "inst not in same function as #dbg_assign", I
, &DVR
);
6642 // This check is redundant with one in visitLocalVariable().
6643 DILocalVariable
*Var
= DVR
.getVariable();
6644 CheckDI(isType(Var
->getRawType()), "invalid type ref", Var
,
6647 auto *DLNode
= DVR
.getDebugLoc().getAsMDNode();
6648 CheckDI(isa_and_nonnull
<DILocation
>(DLNode
), "invalid #dbg record DILocation",
6650 DILocation
*Loc
= DVR
.getDebugLoc();
6652 // The scopes for variables and !dbg attachments must agree.
6653 DISubprogram
*VarSP
= getSubprogram(Var
->getRawScope());
6654 DISubprogram
*LocSP
= getSubprogram(Loc
->getRawScope());
6655 if (!VarSP
|| !LocSP
)
6656 return; // Broken scope chains are checked elsewhere.
6658 CheckDI(VarSP
== LocSP
,
6659 "mismatched subprogram between #dbg record variable and DILocation",
6660 &DVR
, BB
, F
, Var
, Var
->getScope()->getSubprogram(), Loc
,
6661 Loc
->getScope()->getSubprogram());
6666 void Verifier::visitVPIntrinsic(VPIntrinsic
&VPI
) {
6667 if (auto *VPCast
= dyn_cast
<VPCastIntrinsic
>(&VPI
)) {
6668 auto *RetTy
= cast
<VectorType
>(VPCast
->getType());
6669 auto *ValTy
= cast
<VectorType
>(VPCast
->getOperand(0)->getType());
6670 Check(RetTy
->getElementCount() == ValTy
->getElementCount(),
6671 "VP cast intrinsic first argument and result vector lengths must be "
6675 switch (VPCast
->getIntrinsicID()) {
6677 llvm_unreachable("Unknown VP cast intrinsic");
6678 case Intrinsic::vp_trunc
:
6679 Check(RetTy
->isIntOrIntVectorTy() && ValTy
->isIntOrIntVectorTy(),
6680 "llvm.vp.trunc intrinsic first argument and result element type "
6683 Check(RetTy
->getScalarSizeInBits() < ValTy
->getScalarSizeInBits(),
6684 "llvm.vp.trunc intrinsic the bit size of first argument must be "
6685 "larger than the bit size of the return type",
6688 case Intrinsic::vp_zext
:
6689 case Intrinsic::vp_sext
:
6690 Check(RetTy
->isIntOrIntVectorTy() && ValTy
->isIntOrIntVectorTy(),
6691 "llvm.vp.zext or llvm.vp.sext intrinsic first argument and result "
6692 "element type must be integer",
6694 Check(RetTy
->getScalarSizeInBits() > ValTy
->getScalarSizeInBits(),
6695 "llvm.vp.zext or llvm.vp.sext intrinsic the bit size of first "
6696 "argument must be smaller than the bit size of the return type",
6699 case Intrinsic::vp_fptoui
:
6700 case Intrinsic::vp_fptosi
:
6701 case Intrinsic::vp_lrint
:
6702 case Intrinsic::vp_llrint
:
6704 RetTy
->isIntOrIntVectorTy() && ValTy
->isFPOrFPVectorTy(),
6705 "llvm.vp.fptoui, llvm.vp.fptosi, llvm.vp.lrint or llvm.vp.llrint" "intrinsic first argument element "
6706 "type must be floating-point and result element type must be integer",
6709 case Intrinsic::vp_uitofp
:
6710 case Intrinsic::vp_sitofp
:
6712 RetTy
->isFPOrFPVectorTy() && ValTy
->isIntOrIntVectorTy(),
6713 "llvm.vp.uitofp or llvm.vp.sitofp intrinsic first argument element "
6714 "type must be integer and result element type must be floating-point",
6717 case Intrinsic::vp_fptrunc
:
6718 Check(RetTy
->isFPOrFPVectorTy() && ValTy
->isFPOrFPVectorTy(),
6719 "llvm.vp.fptrunc intrinsic first argument and result element type "
6720 "must be floating-point",
6722 Check(RetTy
->getScalarSizeInBits() < ValTy
->getScalarSizeInBits(),
6723 "llvm.vp.fptrunc intrinsic the bit size of first argument must be "
6724 "larger than the bit size of the return type",
6727 case Intrinsic::vp_fpext
:
6728 Check(RetTy
->isFPOrFPVectorTy() && ValTy
->isFPOrFPVectorTy(),
6729 "llvm.vp.fpext intrinsic first argument and result element type "
6730 "must be floating-point",
6732 Check(RetTy
->getScalarSizeInBits() > ValTy
->getScalarSizeInBits(),
6733 "llvm.vp.fpext intrinsic the bit size of first argument must be "
6734 "smaller than the bit size of the return type",
6737 case Intrinsic::vp_ptrtoint
:
6738 Check(RetTy
->isIntOrIntVectorTy() && ValTy
->isPtrOrPtrVectorTy(),
6739 "llvm.vp.ptrtoint intrinsic first argument element type must be "
6740 "pointer and result element type must be integer",
6743 case Intrinsic::vp_inttoptr
:
6744 Check(RetTy
->isPtrOrPtrVectorTy() && ValTy
->isIntOrIntVectorTy(),
6745 "llvm.vp.inttoptr intrinsic first argument element type must be "
6746 "integer and result element type must be pointer",
6751 if (VPI
.getIntrinsicID() == Intrinsic::vp_fcmp
) {
6752 auto Pred
= cast
<VPCmpIntrinsic
>(&VPI
)->getPredicate();
6753 Check(CmpInst::isFPPredicate(Pred
),
6754 "invalid predicate for VP FP comparison intrinsic", &VPI
);
6756 if (VPI
.getIntrinsicID() == Intrinsic::vp_icmp
) {
6757 auto Pred
= cast
<VPCmpIntrinsic
>(&VPI
)->getPredicate();
6758 Check(CmpInst::isIntPredicate(Pred
),
6759 "invalid predicate for VP integer comparison intrinsic", &VPI
);
6761 if (VPI
.getIntrinsicID() == Intrinsic::vp_is_fpclass
) {
6762 auto TestMask
= cast
<ConstantInt
>(VPI
.getOperand(1));
6763 Check((TestMask
->getZExtValue() & ~static_cast<unsigned>(fcAllFlags
)) == 0,
6764 "unsupported bits for llvm.vp.is.fpclass test mask");
6768 void Verifier::visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic
&FPI
) {
6769 unsigned NumOperands
= FPI
.getNonMetadataArgCount();
6770 bool HasRoundingMD
=
6771 Intrinsic::hasConstrainedFPRoundingModeOperand(FPI
.getIntrinsicID());
6773 // Add the expected number of metadata operands.
6774 NumOperands
+= (1 + HasRoundingMD
);
6776 // Compare intrinsics carry an extra predicate metadata operand.
6777 if (isa
<ConstrainedFPCmpIntrinsic
>(FPI
))
6779 Check((FPI
.arg_size() == NumOperands
),
6780 "invalid arguments for constrained FP intrinsic", &FPI
);
6782 switch (FPI
.getIntrinsicID()) {
6783 case Intrinsic::experimental_constrained_lrint
:
6784 case Intrinsic::experimental_constrained_llrint
: {
6785 Type
*ValTy
= FPI
.getArgOperand(0)->getType();
6786 Type
*ResultTy
= FPI
.getType();
6787 Check(!ValTy
->isVectorTy() && !ResultTy
->isVectorTy(),
6788 "Intrinsic does not support vectors", &FPI
);
6792 case Intrinsic::experimental_constrained_lround
:
6793 case Intrinsic::experimental_constrained_llround
: {
6794 Type
*ValTy
= FPI
.getArgOperand(0)->getType();
6795 Type
*ResultTy
= FPI
.getType();
6796 Check(!ValTy
->isVectorTy() && !ResultTy
->isVectorTy(),
6797 "Intrinsic does not support vectors", &FPI
);
6801 case Intrinsic::experimental_constrained_fcmp
:
6802 case Intrinsic::experimental_constrained_fcmps
: {
6803 auto Pred
= cast
<ConstrainedFPCmpIntrinsic
>(&FPI
)->getPredicate();
6804 Check(CmpInst::isFPPredicate(Pred
),
6805 "invalid predicate for constrained FP comparison intrinsic", &FPI
);
6809 case Intrinsic::experimental_constrained_fptosi
:
6810 case Intrinsic::experimental_constrained_fptoui
: {
6811 Value
*Operand
= FPI
.getArgOperand(0);
6813 Check(Operand
->getType()->isFPOrFPVectorTy(),
6814 "Intrinsic first argument must be floating point", &FPI
);
6815 if (auto *OperandT
= dyn_cast
<VectorType
>(Operand
->getType())) {
6816 SrcEC
= cast
<VectorType
>(OperandT
)->getElementCount();
6820 Check(SrcEC
.isNonZero() == Operand
->getType()->isVectorTy(),
6821 "Intrinsic first argument and result disagree on vector use", &FPI
);
6822 Check(Operand
->getType()->isIntOrIntVectorTy(),
6823 "Intrinsic result must be an integer", &FPI
);
6824 if (auto *OperandT
= dyn_cast
<VectorType
>(Operand
->getType())) {
6825 Check(SrcEC
== cast
<VectorType
>(OperandT
)->getElementCount(),
6826 "Intrinsic first argument and result vector lengths must be equal",
6832 case Intrinsic::experimental_constrained_sitofp
:
6833 case Intrinsic::experimental_constrained_uitofp
: {
6834 Value
*Operand
= FPI
.getArgOperand(0);
6836 Check(Operand
->getType()->isIntOrIntVectorTy(),
6837 "Intrinsic first argument must be integer", &FPI
);
6838 if (auto *OperandT
= dyn_cast
<VectorType
>(Operand
->getType())) {
6839 SrcEC
= cast
<VectorType
>(OperandT
)->getElementCount();
6843 Check(SrcEC
.isNonZero() == Operand
->getType()->isVectorTy(),
6844 "Intrinsic first argument and result disagree on vector use", &FPI
);
6845 Check(Operand
->getType()->isFPOrFPVectorTy(),
6846 "Intrinsic result must be a floating point", &FPI
);
6847 if (auto *OperandT
= dyn_cast
<VectorType
>(Operand
->getType())) {
6848 Check(SrcEC
== cast
<VectorType
>(OperandT
)->getElementCount(),
6849 "Intrinsic first argument and result vector lengths must be equal",
6855 case Intrinsic::experimental_constrained_fptrunc
:
6856 case Intrinsic::experimental_constrained_fpext
: {
6857 Value
*Operand
= FPI
.getArgOperand(0);
6858 Type
*OperandTy
= Operand
->getType();
6859 Value
*Result
= &FPI
;
6860 Type
*ResultTy
= Result
->getType();
6861 Check(OperandTy
->isFPOrFPVectorTy(),
6862 "Intrinsic first argument must be FP or FP vector", &FPI
);
6863 Check(ResultTy
->isFPOrFPVectorTy(),
6864 "Intrinsic result must be FP or FP vector", &FPI
);
6865 Check(OperandTy
->isVectorTy() == ResultTy
->isVectorTy(),
6866 "Intrinsic first argument and result disagree on vector use", &FPI
);
6867 if (OperandTy
->isVectorTy()) {
6868 Check(cast
<VectorType
>(OperandTy
)->getElementCount() ==
6869 cast
<VectorType
>(ResultTy
)->getElementCount(),
6870 "Intrinsic first argument and result vector lengths must be equal",
6873 if (FPI
.getIntrinsicID() == Intrinsic::experimental_constrained_fptrunc
) {
6874 Check(OperandTy
->getScalarSizeInBits() > ResultTy
->getScalarSizeInBits(),
6875 "Intrinsic first argument's type must be larger than result type",
6878 Check(OperandTy
->getScalarSizeInBits() < ResultTy
->getScalarSizeInBits(),
6879 "Intrinsic first argument's type must be smaller than result type",
6889 // If a non-metadata argument is passed in a metadata slot then the
6890 // error will be caught earlier when the incorrect argument doesn't
6891 // match the specification in the intrinsic call table. Thus, no
6892 // argument type check is needed here.
6894 Check(FPI
.getExceptionBehavior().has_value(),
6895 "invalid exception behavior argument", &FPI
);
6896 if (HasRoundingMD
) {
6897 Check(FPI
.getRoundingMode().has_value(), "invalid rounding mode argument",
6902 void Verifier::visitDbgIntrinsic(StringRef Kind
, DbgVariableIntrinsic
&DII
) {
6903 auto *MD
= DII
.getRawLocation();
6904 CheckDI(isa
<ValueAsMetadata
>(MD
) || isa
<DIArgList
>(MD
) ||
6905 (isa
<MDNode
>(MD
) && !cast
<MDNode
>(MD
)->getNumOperands()),
6906 "invalid llvm.dbg." + Kind
+ " intrinsic address/value", &DII
, MD
);
6907 CheckDI(isa
<DILocalVariable
>(DII
.getRawVariable()),
6908 "invalid llvm.dbg." + Kind
+ " intrinsic variable", &DII
,
6909 DII
.getRawVariable());
6910 CheckDI(isa
<DIExpression
>(DII
.getRawExpression()),
6911 "invalid llvm.dbg." + Kind
+ " intrinsic expression", &DII
,
6912 DII
.getRawExpression());
6914 if (auto *DAI
= dyn_cast
<DbgAssignIntrinsic
>(&DII
)) {
6915 CheckDI(isa
<DIAssignID
>(DAI
->getRawAssignID()),
6916 "invalid llvm.dbg.assign intrinsic DIAssignID", &DII
,
6917 DAI
->getRawAssignID());
6918 const auto *RawAddr
= DAI
->getRawAddress();
6920 isa
<ValueAsMetadata
>(RawAddr
) ||
6921 (isa
<MDNode
>(RawAddr
) && !cast
<MDNode
>(RawAddr
)->getNumOperands()),
6922 "invalid llvm.dbg.assign intrinsic address", &DII
,
6923 DAI
->getRawAddress());
6924 CheckDI(isa
<DIExpression
>(DAI
->getRawAddressExpression()),
6925 "invalid llvm.dbg.assign intrinsic address expression", &DII
,
6926 DAI
->getRawAddressExpression());
6927 // All of the linked instructions should be in the same function as DII.
6928 for (Instruction
*I
: at::getAssignmentInsts(DAI
))
6929 CheckDI(DAI
->getFunction() == I
->getFunction(),
6930 "inst not in same function as dbg.assign", I
, DAI
);
6933 // Ignore broken !dbg attachments; they're checked elsewhere.
6934 if (MDNode
*N
= DII
.getDebugLoc().getAsMDNode())
6935 if (!isa
<DILocation
>(N
))
6938 BasicBlock
*BB
= DII
.getParent();
6939 Function
*F
= BB
? BB
->getParent() : nullptr;
6941 // The scopes for variables and !dbg attachments must agree.
6942 DILocalVariable
*Var
= DII
.getVariable();
6943 DILocation
*Loc
= DII
.getDebugLoc();
6944 CheckDI(Loc
, "llvm.dbg." + Kind
+ " intrinsic requires a !dbg attachment",
6947 DISubprogram
*VarSP
= getSubprogram(Var
->getRawScope());
6948 DISubprogram
*LocSP
= getSubprogram(Loc
->getRawScope());
6949 if (!VarSP
|| !LocSP
)
6950 return; // Broken scope chains are checked elsewhere.
6952 CheckDI(VarSP
== LocSP
,
6953 "mismatched subprogram between llvm.dbg." + Kind
+
6954 " variable and !dbg attachment",
6955 &DII
, BB
, F
, Var
, Var
->getScope()->getSubprogram(), Loc
,
6956 Loc
->getScope()->getSubprogram());
6958 // This check is redundant with one in visitLocalVariable().
6959 CheckDI(isType(Var
->getRawType()), "invalid type ref", Var
,
6964 void Verifier::visitDbgLabelIntrinsic(StringRef Kind
, DbgLabelInst
&DLI
) {
6965 CheckDI(isa
<DILabel
>(DLI
.getRawLabel()),
6966 "invalid llvm.dbg." + Kind
+ " intrinsic variable", &DLI
,
6969 // Ignore broken !dbg attachments; they're checked elsewhere.
6970 if (MDNode
*N
= DLI
.getDebugLoc().getAsMDNode())
6971 if (!isa
<DILocation
>(N
))
6974 BasicBlock
*BB
= DLI
.getParent();
6975 Function
*F
= BB
? BB
->getParent() : nullptr;
6977 // The scopes for variables and !dbg attachments must agree.
6978 DILabel
*Label
= DLI
.getLabel();
6979 DILocation
*Loc
= DLI
.getDebugLoc();
6980 Check(Loc
, "llvm.dbg." + Kind
+ " intrinsic requires a !dbg attachment", &DLI
,
6983 DISubprogram
*LabelSP
= getSubprogram(Label
->getRawScope());
6984 DISubprogram
*LocSP
= getSubprogram(Loc
->getRawScope());
6985 if (!LabelSP
|| !LocSP
)
6988 CheckDI(LabelSP
== LocSP
,
6989 "mismatched subprogram between llvm.dbg." + Kind
+
6990 " label and !dbg attachment",
6991 &DLI
, BB
, F
, Label
, Label
->getScope()->getSubprogram(), Loc
,
6992 Loc
->getScope()->getSubprogram());
6995 void Verifier::verifyFragmentExpression(const DbgVariableIntrinsic
&I
) {
6996 DILocalVariable
*V
= dyn_cast_or_null
<DILocalVariable
>(I
.getRawVariable());
6997 DIExpression
*E
= dyn_cast_or_null
<DIExpression
>(I
.getRawExpression());
6999 // We don't know whether this intrinsic verified correctly.
7000 if (!V
|| !E
|| !E
->isValid())
7003 // Nothing to do if this isn't a DW_OP_LLVM_fragment expression.
7004 auto Fragment
= E
->getFragmentInfo();
7008 // The frontend helps out GDB by emitting the members of local anonymous
7009 // unions as artificial local variables with shared storage. When SROA splits
7010 // the storage for artificial local variables that are smaller than the entire
7011 // union, the overhang piece will be outside of the allotted space for the
7012 // variable and this check fails.
7013 // FIXME: Remove this check as soon as clang stops doing this; it hides bugs.
7014 if (V
->isArtificial())
7017 verifyFragmentExpression(*V
, *Fragment
, &I
);
7019 void Verifier::verifyFragmentExpression(const DbgVariableRecord
&DVR
) {
7020 DILocalVariable
*V
= dyn_cast_or_null
<DILocalVariable
>(DVR
.getRawVariable());
7021 DIExpression
*E
= dyn_cast_or_null
<DIExpression
>(DVR
.getRawExpression());
7023 // We don't know whether this intrinsic verified correctly.
7024 if (!V
|| !E
|| !E
->isValid())
7027 // Nothing to do if this isn't a DW_OP_LLVM_fragment expression.
7028 auto Fragment
= E
->getFragmentInfo();
7032 // The frontend helps out GDB by emitting the members of local anonymous
7033 // unions as artificial local variables with shared storage. When SROA splits
7034 // the storage for artificial local variables that are smaller than the entire
7035 // union, the overhang piece will be outside of the allotted space for the
7036 // variable and this check fails.
7037 // FIXME: Remove this check as soon as clang stops doing this; it hides bugs.
7038 if (V
->isArtificial())
7041 verifyFragmentExpression(*V
, *Fragment
, &DVR
);
7044 template <typename ValueOrMetadata
>
7045 void Verifier::verifyFragmentExpression(const DIVariable
&V
,
7046 DIExpression::FragmentInfo Fragment
,
7047 ValueOrMetadata
*Desc
) {
7048 // If there's no size, the type is broken, but that should be checked
7050 auto VarSize
= V
.getSizeInBits();
7054 unsigned FragSize
= Fragment
.SizeInBits
;
7055 unsigned FragOffset
= Fragment
.OffsetInBits
;
7056 CheckDI(FragSize
+ FragOffset
<= *VarSize
,
7057 "fragment is larger than or outside of variable", Desc
, &V
);
7058 CheckDI(FragSize
!= *VarSize
, "fragment covers entire variable", Desc
, &V
);
7061 void Verifier::verifyFnArgs(const DbgVariableIntrinsic
&I
) {
7062 // This function does not take the scope of noninlined function arguments into
7063 // account. Don't run it if current function is nodebug, because it may
7064 // contain inlined debug intrinsics.
7068 // For performance reasons only check non-inlined ones.
7069 if (I
.getDebugLoc()->getInlinedAt())
7072 DILocalVariable
*Var
= I
.getVariable();
7073 CheckDI(Var
, "dbg intrinsic without variable");
7075 unsigned ArgNo
= Var
->getArg();
7079 // Verify there are no duplicate function argument debug info entries.
7080 // These will cause hard-to-debug assertions in the DWARF backend.
7081 if (DebugFnArgs
.size() < ArgNo
)
7082 DebugFnArgs
.resize(ArgNo
, nullptr);
7084 auto *Prev
= DebugFnArgs
[ArgNo
- 1];
7085 DebugFnArgs
[ArgNo
- 1] = Var
;
7086 CheckDI(!Prev
|| (Prev
== Var
), "conflicting debug info for argument", &I
,
7089 void Verifier::verifyFnArgs(const DbgVariableRecord
&DVR
) {
7090 // This function does not take the scope of noninlined function arguments into
7091 // account. Don't run it if current function is nodebug, because it may
7092 // contain inlined debug intrinsics.
7096 // For performance reasons only check non-inlined ones.
7097 if (DVR
.getDebugLoc()->getInlinedAt())
7100 DILocalVariable
*Var
= DVR
.getVariable();
7101 CheckDI(Var
, "#dbg record without variable");
7103 unsigned ArgNo
= Var
->getArg();
7107 // Verify there are no duplicate function argument debug info entries.
7108 // These will cause hard-to-debug assertions in the DWARF backend.
7109 if (DebugFnArgs
.size() < ArgNo
)
7110 DebugFnArgs
.resize(ArgNo
, nullptr);
7112 auto *Prev
= DebugFnArgs
[ArgNo
- 1];
7113 DebugFnArgs
[ArgNo
- 1] = Var
;
7114 CheckDI(!Prev
|| (Prev
== Var
), "conflicting debug info for argument", &DVR
,
7118 void Verifier::verifyNotEntryValue(const DbgVariableIntrinsic
&I
) {
7119 DIExpression
*E
= dyn_cast_or_null
<DIExpression
>(I
.getRawExpression());
7121 // We don't know whether this intrinsic verified correctly.
7122 if (!E
|| !E
->isValid())
7125 if (isa
<ValueAsMetadata
>(I
.getRawLocation())) {
7126 Value
*VarValue
= I
.getVariableLocationOp(0);
7127 if (isa
<UndefValue
>(VarValue
) || isa
<PoisonValue
>(VarValue
))
7129 // We allow EntryValues for swift async arguments, as they have an
7130 // ABI-guarantee to be turned into a specific register.
7131 if (auto *ArgLoc
= dyn_cast_or_null
<Argument
>(VarValue
);
7132 ArgLoc
&& ArgLoc
->hasAttribute(Attribute::SwiftAsync
))
7136 CheckDI(!E
->isEntryValue(),
7137 "Entry values are only allowed in MIR unless they target a "
7138 "swiftasync Argument",
7141 void Verifier::verifyNotEntryValue(const DbgVariableRecord
&DVR
) {
7142 DIExpression
*E
= dyn_cast_or_null
<DIExpression
>(DVR
.getRawExpression());
7144 // We don't know whether this intrinsic verified correctly.
7145 if (!E
|| !E
->isValid())
7148 if (isa
<ValueAsMetadata
>(DVR
.getRawLocation())) {
7149 Value
*VarValue
= DVR
.getVariableLocationOp(0);
7150 if (isa
<UndefValue
>(VarValue
) || isa
<PoisonValue
>(VarValue
))
7152 // We allow EntryValues for swift async arguments, as they have an
7153 // ABI-guarantee to be turned into a specific register.
7154 if (auto *ArgLoc
= dyn_cast_or_null
<Argument
>(VarValue
);
7155 ArgLoc
&& ArgLoc
->hasAttribute(Attribute::SwiftAsync
))
7159 CheckDI(!E
->isEntryValue(),
7160 "Entry values are only allowed in MIR unless they target a "
7161 "swiftasync Argument",
7165 void Verifier::verifyCompileUnits() {
7166 // When more than one Module is imported into the same context, such as during
7167 // an LTO build before linking the modules, ODR type uniquing may cause types
7168 // to point to a different CU. This check does not make sense in this case.
7169 if (M
.getContext().isODRUniquingDebugTypes())
7171 auto *CUs
= M
.getNamedMetadata("llvm.dbg.cu");
7172 SmallPtrSet
<const Metadata
*, 2> Listed
;
7174 Listed
.insert(CUs
->op_begin(), CUs
->op_end());
7175 for (const auto *CU
: CUVisited
)
7176 CheckDI(Listed
.count(CU
), "DICompileUnit not listed in llvm.dbg.cu", CU
);
7180 void Verifier::verifyDeoptimizeCallingConvs() {
7181 if (DeoptimizeDeclarations
.empty())
7184 const Function
*First
= DeoptimizeDeclarations
[0];
7185 for (const auto *F
: ArrayRef(DeoptimizeDeclarations
).slice(1)) {
7186 Check(First
->getCallingConv() == F
->getCallingConv(),
7187 "All llvm.experimental.deoptimize declarations must have the same "
7188 "calling convention",
7193 void Verifier::verifyAttachedCallBundle(const CallBase
&Call
,
7194 const OperandBundleUse
&BU
) {
7195 FunctionType
*FTy
= Call
.getFunctionType();
7197 Check((FTy
->getReturnType()->isPointerTy() ||
7198 (Call
.doesNotReturn() && FTy
->getReturnType()->isVoidTy())),
7199 "a call with operand bundle \"clang.arc.attachedcall\" must call a "
7200 "function returning a pointer or a non-returning function that has a "
7204 Check(BU
.Inputs
.size() == 1 && isa
<Function
>(BU
.Inputs
.front()),
7205 "operand bundle \"clang.arc.attachedcall\" requires one function as "
7209 auto *Fn
= cast
<Function
>(BU
.Inputs
.front());
7210 Intrinsic::ID IID
= Fn
->getIntrinsicID();
7213 Check((IID
== Intrinsic::objc_retainAutoreleasedReturnValue
||
7214 IID
== Intrinsic::objc_unsafeClaimAutoreleasedReturnValue
),
7215 "invalid function argument", Call
);
7217 StringRef FnName
= Fn
->getName();
7218 Check((FnName
== "objc_retainAutoreleasedReturnValue" ||
7219 FnName
== "objc_unsafeClaimAutoreleasedReturnValue"),
7220 "invalid function argument", Call
);
7224 void Verifier::verifyNoAliasScopeDecl() {
7225 if (NoAliasScopeDecls
.empty())
7228 // only a single scope must be declared at a time.
7229 for (auto *II
: NoAliasScopeDecls
) {
7230 assert(II
->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl
&&
7231 "Not a llvm.experimental.noalias.scope.decl ?");
7232 const auto *ScopeListMV
= dyn_cast
<MetadataAsValue
>(
7233 II
->getOperand(Intrinsic::NoAliasScopeDeclScopeArg
));
7234 Check(ScopeListMV
!= nullptr,
7235 "llvm.experimental.noalias.scope.decl must have a MetadataAsValue "
7239 const auto *ScopeListMD
= dyn_cast
<MDNode
>(ScopeListMV
->getMetadata());
7240 Check(ScopeListMD
!= nullptr, "!id.scope.list must point to an MDNode", II
);
7241 Check(ScopeListMD
->getNumOperands() == 1,
7242 "!id.scope.list must point to a list with a single scope", II
);
7243 visitAliasScopeListMetadata(ScopeListMD
);
7246 // Only check the domination rule when requested. Once all passes have been
7247 // adapted this option can go away.
7248 if (!VerifyNoAliasScopeDomination
)
7251 // Now sort the intrinsics based on the scope MDNode so that declarations of
7252 // the same scopes are next to each other.
7253 auto GetScope
= [](IntrinsicInst
*II
) {
7254 const auto *ScopeListMV
= cast
<MetadataAsValue
>(
7255 II
->getOperand(Intrinsic::NoAliasScopeDeclScopeArg
));
7256 return &cast
<MDNode
>(ScopeListMV
->getMetadata())->getOperand(0);
7259 // We are sorting on MDNode pointers here. For valid input IR this is ok.
7260 // TODO: Sort on Metadata ID to avoid non-deterministic error messages.
7261 auto Compare
= [GetScope
](IntrinsicInst
*Lhs
, IntrinsicInst
*Rhs
) {
7262 return GetScope(Lhs
) < GetScope(Rhs
);
7265 llvm::sort(NoAliasScopeDecls
, Compare
);
7267 // Go over the intrinsics and check that for the same scope, they are not
7268 // dominating each other.
7269 auto ItCurrent
= NoAliasScopeDecls
.begin();
7270 while (ItCurrent
!= NoAliasScopeDecls
.end()) {
7271 auto CurScope
= GetScope(*ItCurrent
);
7272 auto ItNext
= ItCurrent
;
7275 } while (ItNext
!= NoAliasScopeDecls
.end() &&
7276 GetScope(*ItNext
) == CurScope
);
7278 // [ItCurrent, ItNext) represents the declarations for the same scope.
7279 // Ensure they are not dominating each other.. but only if it is not too
7281 if (ItNext
- ItCurrent
< 32)
7282 for (auto *I
: llvm::make_range(ItCurrent
, ItNext
))
7283 for (auto *J
: llvm::make_range(ItCurrent
, ItNext
))
7285 Check(!DT
.dominates(I
, J
),
7286 "llvm.experimental.noalias.scope.decl dominates another one "
7287 "with the same scope",
7293 //===----------------------------------------------------------------------===//
7294 // Implement the public interfaces to this file...
7295 //===----------------------------------------------------------------------===//
7297 bool llvm::verifyFunction(const Function
&f
, raw_ostream
*OS
) {
7298 Function
&F
= const_cast<Function
&>(f
);
7300 // Don't use a raw_null_ostream. Printing IR is expensive.
7301 Verifier
V(OS
, /*ShouldTreatBrokenDebugInfoAsError=*/true, *f
.getParent());
7303 // Note that this function's return value is inverted from what you would
7304 // expect of a function called "verify".
7305 return !V
.verify(F
);
7308 bool llvm::verifyModule(const Module
&M
, raw_ostream
*OS
,
7309 bool *BrokenDebugInfo
) {
7310 // Don't use a raw_null_ostream. Printing IR is expensive.
7311 Verifier
V(OS
, /*ShouldTreatBrokenDebugInfoAsError=*/!BrokenDebugInfo
, M
);
7313 bool Broken
= false;
7314 for (const Function
&F
: M
)
7315 Broken
|= !V
.verify(F
);
7317 Broken
|= !V
.verify();
7318 if (BrokenDebugInfo
)
7319 *BrokenDebugInfo
= V
.hasBrokenDebugInfo();
7320 // Note that this function's return value is inverted from what you would
7321 // expect of a function called "verify".
7327 struct VerifierLegacyPass
: public FunctionPass
{
7330 std::unique_ptr
<Verifier
> V
;
7331 bool FatalErrors
= true;
7333 VerifierLegacyPass() : FunctionPass(ID
) {
7334 initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
7336 explicit VerifierLegacyPass(bool FatalErrors
)
7338 FatalErrors(FatalErrors
) {
7339 initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
7342 bool doInitialization(Module
&M
) override
{
7343 V
= std::make_unique
<Verifier
>(
7344 &dbgs(), /*ShouldTreatBrokenDebugInfoAsError=*/false, M
);
7348 bool runOnFunction(Function
&F
) override
{
7349 if (!V
->verify(F
) && FatalErrors
) {
7350 errs() << "in function " << F
.getName() << '\n';
7351 report_fatal_error("Broken function found, compilation aborted!");
7356 bool doFinalization(Module
&M
) override
{
7357 bool HasErrors
= false;
7358 for (Function
&F
: M
)
7359 if (F
.isDeclaration())
7360 HasErrors
|= !V
->verify(F
);
7362 HasErrors
|= !V
->verify();
7363 if (FatalErrors
&& (HasErrors
|| V
->hasBrokenDebugInfo()))
7364 report_fatal_error("Broken module found, compilation aborted!");
7368 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
7369 AU
.setPreservesAll();
7373 } // end anonymous namespace
7375 /// Helper to issue failure from the TBAA verification
7376 template <typename
... Tys
> void TBAAVerifier::CheckFailed(Tys
&&... Args
) {
7378 return Diagnostic
->CheckFailed(Args
...);
7381 #define CheckTBAA(C, ...) \
7384 CheckFailed(__VA_ARGS__); \
7389 /// Verify that \p BaseNode can be used as the "base type" in the struct-path
7390 /// TBAA scheme. This means \p BaseNode is either a scalar node, or a
7391 /// struct-type node describing an aggregate data structure (like a struct).
7392 TBAAVerifier::TBAABaseNodeSummary
7393 TBAAVerifier::verifyTBAABaseNode(Instruction
&I
, const MDNode
*BaseNode
,
7395 if (BaseNode
->getNumOperands() < 2) {
7396 CheckFailed("Base nodes must have at least two operands", &I
, BaseNode
);
7400 auto Itr
= TBAABaseNodes
.find(BaseNode
);
7401 if (Itr
!= TBAABaseNodes
.end())
7404 auto Result
= verifyTBAABaseNodeImpl(I
, BaseNode
, IsNewFormat
);
7405 auto InsertResult
= TBAABaseNodes
.insert({BaseNode
, Result
});
7407 assert(InsertResult
.second
&& "We just checked!");
7411 TBAAVerifier::TBAABaseNodeSummary
7412 TBAAVerifier::verifyTBAABaseNodeImpl(Instruction
&I
, const MDNode
*BaseNode
,
7414 const TBAAVerifier::TBAABaseNodeSummary InvalidNode
= {true, ~0u};
7416 if (BaseNode
->getNumOperands() == 2) {
7417 // Scalar nodes can only be accessed at offset 0.
7418 return isValidScalarTBAANode(BaseNode
)
7419 ? TBAAVerifier::TBAABaseNodeSummary({false, 0})
7424 if (BaseNode
->getNumOperands() % 3 != 0) {
7425 CheckFailed("Access tag nodes must have the number of operands that is a "
7426 "multiple of 3!", BaseNode
);
7430 if (BaseNode
->getNumOperands() % 2 != 1) {
7431 CheckFailed("Struct tag nodes must have an odd number of operands!",
7437 // Check the type size field.
7439 auto *TypeSizeNode
= mdconst::dyn_extract_or_null
<ConstantInt
>(
7440 BaseNode
->getOperand(1));
7441 if (!TypeSizeNode
) {
7442 CheckFailed("Type size nodes must be constants!", &I
, BaseNode
);
7447 // Check the type name field. In the new format it can be anything.
7448 if (!IsNewFormat
&& !isa
<MDString
>(BaseNode
->getOperand(0))) {
7449 CheckFailed("Struct tag nodes have a string as their first operand",
7454 bool Failed
= false;
7456 std::optional
<APInt
> PrevOffset
;
7457 unsigned BitWidth
= ~0u;
7459 // We've already checked that BaseNode is not a degenerate root node with one
7460 // operand in \c verifyTBAABaseNode, so this loop should run at least once.
7461 unsigned FirstFieldOpNo
= IsNewFormat
? 3 : 1;
7462 unsigned NumOpsPerField
= IsNewFormat
? 3 : 2;
7463 for (unsigned Idx
= FirstFieldOpNo
; Idx
< BaseNode
->getNumOperands();
7464 Idx
+= NumOpsPerField
) {
7465 const MDOperand
&FieldTy
= BaseNode
->getOperand(Idx
);
7466 const MDOperand
&FieldOffset
= BaseNode
->getOperand(Idx
+ 1);
7467 if (!isa
<MDNode
>(FieldTy
)) {
7468 CheckFailed("Incorrect field entry in struct type node!", &I
, BaseNode
);
7473 auto *OffsetEntryCI
=
7474 mdconst::dyn_extract_or_null
<ConstantInt
>(FieldOffset
);
7475 if (!OffsetEntryCI
) {
7476 CheckFailed("Offset entries must be constants!", &I
, BaseNode
);
7481 if (BitWidth
== ~0u)
7482 BitWidth
= OffsetEntryCI
->getBitWidth();
7484 if (OffsetEntryCI
->getBitWidth() != BitWidth
) {
7486 "Bitwidth between the offsets and struct type entries must match", &I
,
7492 // NB! As far as I can tell, we generate a non-strictly increasing offset
7493 // sequence only from structs that have zero size bit fields. When
7494 // recursing into a contained struct in \c getFieldNodeFromTBAABaseNode we
7495 // pick the field lexically the latest in struct type metadata node. This
7496 // mirrors the actual behavior of the alias analysis implementation.
7498 !PrevOffset
|| PrevOffset
->ule(OffsetEntryCI
->getValue());
7501 CheckFailed("Offsets must be increasing!", &I
, BaseNode
);
7505 PrevOffset
= OffsetEntryCI
->getValue();
7508 auto *MemberSizeNode
= mdconst::dyn_extract_or_null
<ConstantInt
>(
7509 BaseNode
->getOperand(Idx
+ 2));
7510 if (!MemberSizeNode
) {
7511 CheckFailed("Member size entries must be constants!", &I
, BaseNode
);
7518 return Failed
? InvalidNode
7519 : TBAAVerifier::TBAABaseNodeSummary(false, BitWidth
);
7522 static bool IsRootTBAANode(const MDNode
*MD
) {
7523 return MD
->getNumOperands() < 2;
7526 static bool IsScalarTBAANodeImpl(const MDNode
*MD
,
7527 SmallPtrSetImpl
<const MDNode
*> &Visited
) {
7528 if (MD
->getNumOperands() != 2 && MD
->getNumOperands() != 3)
7531 if (!isa
<MDString
>(MD
->getOperand(0)))
7534 if (MD
->getNumOperands() == 3) {
7535 auto *Offset
= mdconst::dyn_extract
<ConstantInt
>(MD
->getOperand(2));
7536 if (!(Offset
&& Offset
->isZero() && isa
<MDString
>(MD
->getOperand(0))))
7540 auto *Parent
= dyn_cast_or_null
<MDNode
>(MD
->getOperand(1));
7541 return Parent
&& Visited
.insert(Parent
).second
&&
7542 (IsRootTBAANode(Parent
) || IsScalarTBAANodeImpl(Parent
, Visited
));
7545 bool TBAAVerifier::isValidScalarTBAANode(const MDNode
*MD
) {
7546 auto ResultIt
= TBAAScalarNodes
.find(MD
);
7547 if (ResultIt
!= TBAAScalarNodes
.end())
7548 return ResultIt
->second
;
7550 SmallPtrSet
<const MDNode
*, 4> Visited
;
7551 bool Result
= IsScalarTBAANodeImpl(MD
, Visited
);
7552 auto InsertResult
= TBAAScalarNodes
.insert({MD
, Result
});
7554 assert(InsertResult
.second
&& "Just checked!");
7559 /// Returns the field node at the offset \p Offset in \p BaseNode. Update \p
7560 /// Offset in place to be the offset within the field node returned.
7562 /// We assume we've okayed \p BaseNode via \c verifyTBAABaseNode.
7563 MDNode
*TBAAVerifier::getFieldNodeFromTBAABaseNode(Instruction
&I
,
7564 const MDNode
*BaseNode
,
7567 assert(BaseNode
->getNumOperands() >= 2 && "Invalid base node!");
7569 // Scalar nodes have only one possible "field" -- their parent in the access
7570 // hierarchy. Offset must be zero at this point, but our caller is supposed
7572 if (BaseNode
->getNumOperands() == 2)
7573 return cast
<MDNode
>(BaseNode
->getOperand(1));
7575 unsigned FirstFieldOpNo
= IsNewFormat
? 3 : 1;
7576 unsigned NumOpsPerField
= IsNewFormat
? 3 : 2;
7577 for (unsigned Idx
= FirstFieldOpNo
; Idx
< BaseNode
->getNumOperands();
7578 Idx
+= NumOpsPerField
) {
7579 auto *OffsetEntryCI
=
7580 mdconst::extract
<ConstantInt
>(BaseNode
->getOperand(Idx
+ 1));
7581 if (OffsetEntryCI
->getValue().ugt(Offset
)) {
7582 if (Idx
== FirstFieldOpNo
) {
7583 CheckFailed("Could not find TBAA parent in struct type node", &I
,
7588 unsigned PrevIdx
= Idx
- NumOpsPerField
;
7589 auto *PrevOffsetEntryCI
=
7590 mdconst::extract
<ConstantInt
>(BaseNode
->getOperand(PrevIdx
+ 1));
7591 Offset
-= PrevOffsetEntryCI
->getValue();
7592 return cast
<MDNode
>(BaseNode
->getOperand(PrevIdx
));
7596 unsigned LastIdx
= BaseNode
->getNumOperands() - NumOpsPerField
;
7597 auto *LastOffsetEntryCI
= mdconst::extract
<ConstantInt
>(
7598 BaseNode
->getOperand(LastIdx
+ 1));
7599 Offset
-= LastOffsetEntryCI
->getValue();
7600 return cast
<MDNode
>(BaseNode
->getOperand(LastIdx
));
7603 static bool isNewFormatTBAATypeNode(llvm::MDNode
*Type
) {
7604 if (!Type
|| Type
->getNumOperands() < 3)
7607 // In the new format type nodes shall have a reference to the parent type as
7608 // its first operand.
7609 return isa_and_nonnull
<MDNode
>(Type
->getOperand(0));
7612 bool TBAAVerifier::visitTBAAMetadata(Instruction
&I
, const MDNode
*MD
) {
7613 CheckTBAA(MD
->getNumOperands() > 0, "TBAA metadata cannot have 0 operands",
7616 CheckTBAA(isa
<LoadInst
>(I
) || isa
<StoreInst
>(I
) || isa
<CallInst
>(I
) ||
7617 isa
<VAArgInst
>(I
) || isa
<AtomicRMWInst
>(I
) ||
7618 isa
<AtomicCmpXchgInst
>(I
),
7619 "This instruction shall not have a TBAA access tag!", &I
);
7621 bool IsStructPathTBAA
=
7622 isa
<MDNode
>(MD
->getOperand(0)) && MD
->getNumOperands() >= 3;
7624 CheckTBAA(IsStructPathTBAA
,
7625 "Old-style TBAA is no longer allowed, use struct-path TBAA instead",
7628 MDNode
*BaseNode
= dyn_cast_or_null
<MDNode
>(MD
->getOperand(0));
7629 MDNode
*AccessType
= dyn_cast_or_null
<MDNode
>(MD
->getOperand(1));
7631 bool IsNewFormat
= isNewFormatTBAATypeNode(AccessType
);
7634 CheckTBAA(MD
->getNumOperands() == 4 || MD
->getNumOperands() == 5,
7635 "Access tag metadata must have either 4 or 5 operands", &I
, MD
);
7637 CheckTBAA(MD
->getNumOperands() < 5,
7638 "Struct tag metadata must have either 3 or 4 operands", &I
, MD
);
7641 // Check the access size field.
7643 auto *AccessSizeNode
= mdconst::dyn_extract_or_null
<ConstantInt
>(
7645 CheckTBAA(AccessSizeNode
, "Access size field must be a constant", &I
, MD
);
7648 // Check the immutability flag.
7649 unsigned ImmutabilityFlagOpNo
= IsNewFormat
? 4 : 3;
7650 if (MD
->getNumOperands() == ImmutabilityFlagOpNo
+ 1) {
7651 auto *IsImmutableCI
= mdconst::dyn_extract_or_null
<ConstantInt
>(
7652 MD
->getOperand(ImmutabilityFlagOpNo
));
7653 CheckTBAA(IsImmutableCI
,
7654 "Immutability tag on struct tag metadata must be a constant", &I
,
7657 IsImmutableCI
->isZero() || IsImmutableCI
->isOne(),
7658 "Immutability part of the struct tag metadata must be either 0 or 1",
7662 CheckTBAA(BaseNode
&& AccessType
,
7663 "Malformed struct tag metadata: base and access-type "
7664 "should be non-null and point to Metadata nodes",
7665 &I
, MD
, BaseNode
, AccessType
);
7668 CheckTBAA(isValidScalarTBAANode(AccessType
),
7669 "Access type node must be a valid scalar type", &I
, MD
,
7673 auto *OffsetCI
= mdconst::dyn_extract_or_null
<ConstantInt
>(MD
->getOperand(2));
7674 CheckTBAA(OffsetCI
, "Offset must be constant integer", &I
, MD
);
7676 APInt Offset
= OffsetCI
->getValue();
7677 bool SeenAccessTypeInPath
= false;
7679 SmallPtrSet
<MDNode
*, 4> StructPath
;
7681 for (/* empty */; BaseNode
&& !IsRootTBAANode(BaseNode
);
7682 BaseNode
= getFieldNodeFromTBAABaseNode(I
, BaseNode
, Offset
,
7684 if (!StructPath
.insert(BaseNode
).second
) {
7685 CheckFailed("Cycle detected in struct path", &I
, MD
);
7690 unsigned BaseNodeBitWidth
;
7691 std::tie(Invalid
, BaseNodeBitWidth
) = verifyTBAABaseNode(I
, BaseNode
,
7694 // If the base node is invalid in itself, then we've already printed all the
7695 // errors we wanted to print.
7699 SeenAccessTypeInPath
|= BaseNode
== AccessType
;
7701 if (isValidScalarTBAANode(BaseNode
) || BaseNode
== AccessType
)
7702 CheckTBAA(Offset
== 0, "Offset not zero at the point of scalar access",
7705 CheckTBAA(BaseNodeBitWidth
== Offset
.getBitWidth() ||
7706 (BaseNodeBitWidth
== 0 && Offset
== 0) ||
7707 (IsNewFormat
&& BaseNodeBitWidth
== ~0u),
7708 "Access bit-width not the same as description bit-width", &I
, MD
,
7709 BaseNodeBitWidth
, Offset
.getBitWidth());
7711 if (IsNewFormat
&& SeenAccessTypeInPath
)
7715 CheckTBAA(SeenAccessTypeInPath
, "Did not see access type in access path!", &I
,
7720 char VerifierLegacyPass::ID
= 0;
7721 INITIALIZE_PASS(VerifierLegacyPass
, "verify", "Module Verifier", false, false)
7723 FunctionPass
*llvm::createVerifierPass(bool FatalErrors
) {
7724 return new VerifierLegacyPass(FatalErrors
);
7727 AnalysisKey
VerifierAnalysis::Key
;
7728 VerifierAnalysis::Result
VerifierAnalysis::run(Module
&M
,
7729 ModuleAnalysisManager
&) {
7731 Res
.IRBroken
= llvm::verifyModule(M
, &dbgs(), &Res
.DebugInfoBroken
);
7735 VerifierAnalysis::Result
VerifierAnalysis::run(Function
&F
,
7736 FunctionAnalysisManager
&) {
7737 return { llvm::verifyFunction(F
, &dbgs()), false };
7740 PreservedAnalyses
VerifierPass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
7741 auto Res
= AM
.getResult
<VerifierAnalysis
>(M
);
7742 if (FatalErrors
&& (Res
.IRBroken
|| Res
.DebugInfoBroken
))
7743 report_fatal_error("Broken module found, compilation aborted!");
7745 return PreservedAnalyses::all();
7748 PreservedAnalyses
VerifierPass::run(Function
&F
, FunctionAnalysisManager
&AM
) {
7749 auto res
= AM
.getResult
<VerifierAnalysis
>(F
);
7750 if (res
.IRBroken
&& FatalErrors
)
7751 report_fatal_error("Broken function found, compilation aborted!");
7753 return PreservedAnalyses::all();