LLVM 19.0.0git
Verifier.cpp
Go to the documentation of this file.
1//===-- Verifier.cpp - Implement the Module Verifier -----------------------==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the function verifier interface, that can be used for some
10// basic correctness checking of input to the system.
11//
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.
14//
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
39// block.
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...
47//
48//===----------------------------------------------------------------------===//
49
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"
57#include "llvm/ADT/STLExtras.h"
59#include "llvm/ADT/SmallSet.h"
62#include "llvm/ADT/StringMap.h"
63#include "llvm/ADT/StringRef.h"
64#include "llvm/ADT/Twine.h"
66#include "llvm/IR/Argument.h"
68#include "llvm/IR/Attributes.h"
69#include "llvm/IR/BasicBlock.h"
70#include "llvm/IR/CFG.h"
71#include "llvm/IR/CallingConv.h"
72#include "llvm/IR/Comdat.h"
73#include "llvm/IR/Constant.h"
75#include "llvm/IR/Constants.h"
77#include "llvm/IR/DataLayout.h"
78#include "llvm/IR/DebugInfo.h"
80#include "llvm/IR/DebugLoc.h"
82#include "llvm/IR/Dominators.h"
84#include "llvm/IR/Function.h"
85#include "llvm/IR/GCStrategy.h"
86#include "llvm/IR/GlobalAlias.h"
87#include "llvm/IR/GlobalValue.h"
89#include "llvm/IR/InlineAsm.h"
90#include "llvm/IR/InstVisitor.h"
91#include "llvm/IR/InstrTypes.h"
92#include "llvm/IR/Instruction.h"
95#include "llvm/IR/Intrinsics.h"
96#include "llvm/IR/IntrinsicsAArch64.h"
97#include "llvm/IR/IntrinsicsAMDGPU.h"
98#include "llvm/IR/IntrinsicsARM.h"
99#include "llvm/IR/IntrinsicsNVPTX.h"
100#include "llvm/IR/IntrinsicsWebAssembly.h"
101#include "llvm/IR/LLVMContext.h"
102#include "llvm/IR/Metadata.h"
103#include "llvm/IR/Module.h"
105#include "llvm/IR/PassManager.h"
106#include "llvm/IR/Statepoint.h"
107#include "llvm/IR/Type.h"
108#include "llvm/IR/Use.h"
109#include "llvm/IR/User.h"
111#include "llvm/IR/Value.h"
113#include "llvm/Pass.h"
115#include "llvm/Support/Casting.h"
120#include <algorithm>
121#include <cassert>
122#include <cstdint>
123#include <memory>
124#include <optional>
125#include <string>
126#include <utility>
127
128using namespace llvm;
129
131 "verify-noalias-scope-decl-dom", cl::Hidden, cl::init(false),
132 cl::desc("Ensure that llvm.experimental.noalias.scope.decl for identical "
133 "scopes are not dominating"));
134
135namespace llvm {
136
139 const Module &M;
144
145 /// Track the brokenness of the module while recursively visiting.
146 bool Broken = false;
147 /// Broken debug info can be "recovered" from by stripping the debug info.
148 bool BrokenDebugInfo = false;
149 /// Whether to treat broken debug info as an error.
151
153 : OS(OS), M(M), MST(&M), TT(M.getTargetTriple()), DL(M.getDataLayout()),
154 Context(M.getContext()) {}
155
156private:
157 void Write(const Module *M) {
158 *OS << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
159 }
160
161 void Write(const Value *V) {
162 if (V)
163 Write(*V);
164 }
165
166 void Write(const Value &V) {
167 if (isa<Instruction>(V)) {
168 V.print(*OS, MST);
169 *OS << '\n';
170 } else {
171 V.printAsOperand(*OS, true, MST);
172 *OS << '\n';
173 }
174 }
175
176 void Write(const DbgRecord *DR) {
177 if (DR) {
178 DR->print(*OS, MST, false);
179 *OS << '\n';
180 }
181 }
182
184 switch (Type) {
186 *OS << "value";
187 break;
189 *OS << "declare";
190 break;
192 *OS << "assign";
193 break;
195 *OS << "end";
196 break;
198 *OS << "any";
199 break;
200 };
201 }
202
203 void Write(const Metadata *MD) {
204 if (!MD)
205 return;
206 MD->print(*OS, MST, &M);
207 *OS << '\n';
208 }
209
210 template <class T> void Write(const MDTupleTypedArrayWrapper<T> &MD) {
211 Write(MD.get());
212 }
213
214 void Write(const NamedMDNode *NMD) {
215 if (!NMD)
216 return;
217 NMD->print(*OS, MST);
218 *OS << '\n';
219 }
220
221 void Write(Type *T) {
222 if (!T)
223 return;
224 *OS << ' ' << *T;
225 }
226
227 void Write(const Comdat *C) {
228 if (!C)
229 return;
230 *OS << *C;
231 }
232
233 void Write(const APInt *AI) {
234 if (!AI)
235 return;
236 *OS << *AI << '\n';
237 }
238
239 void Write(const unsigned i) { *OS << i << '\n'; }
240
241 // NOLINTNEXTLINE(readability-identifier-naming)
242 void Write(const Attribute *A) {
243 if (!A)
244 return;
245 *OS << A->getAsString() << '\n';
246 }
247
248 // NOLINTNEXTLINE(readability-identifier-naming)
249 void Write(const AttributeSet *AS) {
250 if (!AS)
251 return;
252 *OS << AS->getAsString() << '\n';
253 }
254
255 // NOLINTNEXTLINE(readability-identifier-naming)
256 void Write(const AttributeList *AL) {
257 if (!AL)
258 return;
259 AL->print(*OS);
260 }
261
262 void Write(Printable P) { *OS << P << '\n'; }
263
264 template <typename T> void Write(ArrayRef<T> Vs) {
265 for (const T &V : Vs)
266 Write(V);
267 }
268
269 template <typename T1, typename... Ts>
270 void WriteTs(const T1 &V1, const Ts &... Vs) {
271 Write(V1);
272 WriteTs(Vs...);
273 }
274
275 template <typename... Ts> void WriteTs() {}
276
277public:
278 /// A check failed, so printout out the condition and the message.
279 ///
280 /// This provides a nice place to put a breakpoint if you want to see why
281 /// something is not correct.
282 void CheckFailed(const Twine &Message) {
283 if (OS)
284 *OS << Message << '\n';
285 Broken = true;
286 }
287
288 /// A check failed (with values to print).
289 ///
290 /// This calls the Message-only version so that the above is easier to set a
291 /// breakpoint on.
292 template <typename T1, typename... Ts>
293 void CheckFailed(const Twine &Message, const T1 &V1, const Ts &... Vs) {
294 CheckFailed(Message);
295 if (OS)
296 WriteTs(V1, Vs...);
297 }
298
299 /// A debug info check failed.
300 void DebugInfoCheckFailed(const Twine &Message) {
301 if (OS)
302 *OS << Message << '\n';
304 BrokenDebugInfo = true;
305 }
306
307 /// A debug info check failed (with values to print).
308 template <typename T1, typename... Ts>
309 void DebugInfoCheckFailed(const Twine &Message, const T1 &V1,
310 const Ts &... Vs) {
311 DebugInfoCheckFailed(Message);
312 if (OS)
313 WriteTs(V1, Vs...);
314 }
315};
316
317} // namespace llvm
318
319namespace {
320
321class Verifier : public InstVisitor<Verifier>, VerifierSupport {
322 friend class InstVisitor<Verifier>;
323
324 // ISD::ArgFlagsTy::MemAlign only have 4 bits for alignment, so
325 // the alignment size should not exceed 2^15. Since encode(Align)
326 // would plus the shift value by 1, the alignment size should
327 // not exceed 2^14, otherwise it can NOT be properly lowered
328 // in backend.
329 static constexpr unsigned ParamMaxAlignment = 1 << 14;
330 DominatorTree DT;
331
332 /// When verifying a basic block, keep track of all of the
333 /// instructions we have seen so far.
334 ///
335 /// This allows us to do efficient dominance checks for the case when an
336 /// instruction has an operand that is an instruction in the same block.
337 SmallPtrSet<Instruction *, 16> InstsInThisBlock;
338
339 /// Keep track of the metadata nodes that have been checked already.
341
342 /// Keep track which DISubprogram is attached to which function.
344
345 /// Track all DICompileUnits visited.
347
348 /// The result type for a landingpad.
349 Type *LandingPadResultTy;
350
351 /// Whether we've seen a call to @llvm.localescape in this function
352 /// already.
353 bool SawFrameEscape;
354
355 /// Whether the current function has a DISubprogram attached to it.
356 bool HasDebugInfo = false;
357
358 /// The current source language.
360
361 /// Stores the count of how many objects were passed to llvm.localescape for a
362 /// given function and the largest index passed to llvm.localrecover.
364
365 // Maps catchswitches and cleanuppads that unwind to siblings to the
366 // terminators that indicate the unwind, used to detect cycles therein.
368
369 /// Cache which blocks are in which funclet, if an EH funclet personality is
370 /// in use. Otherwise empty.
371 DenseMap<BasicBlock *, ColorVector> BlockEHFuncletColors;
372
373 /// Cache of constants visited in search of ConstantExprs.
374 SmallPtrSet<const Constant *, 32> ConstantExprVisited;
375
376 /// Cache of declarations of the llvm.experimental.deoptimize.<ty> intrinsic.
377 SmallVector<const Function *, 4> DeoptimizeDeclarations;
378
379 /// Cache of attribute lists verified.
380 SmallPtrSet<const void *, 32> AttributeListsVisited;
381
382 // Verify that this GlobalValue is only used in this module.
383 // This map is used to avoid visiting uses twice. We can arrive at a user
384 // twice, if they have multiple operands. In particular for very large
385 // constant expressions, we can arrive at a particular user many times.
386 SmallPtrSet<const Value *, 32> GlobalValueVisited;
387
388 // Keeps track of duplicate function argument debug info.
390
391 TBAAVerifier TBAAVerifyHelper;
392 ConvergenceVerifier ConvergenceVerifyHelper;
393
394 SmallVector<IntrinsicInst *, 4> NoAliasScopeDecls;
395
396 void checkAtomicMemAccessSize(Type *Ty, const Instruction *I);
397
398public:
399 explicit Verifier(raw_ostream *OS, bool ShouldTreatBrokenDebugInfoAsError,
400 const Module &M)
401 : VerifierSupport(OS, M), LandingPadResultTy(nullptr),
402 SawFrameEscape(false), TBAAVerifyHelper(this) {
403 TreatBrokenDebugInfoAsError = ShouldTreatBrokenDebugInfoAsError;
404 }
405
406 bool hasBrokenDebugInfo() const { return BrokenDebugInfo; }
407
408 bool verify(const Function &F) {
409 assert(F.getParent() == &M &&
410 "An instance of this class only works with a specific module!");
411
412 // First ensure the function is well-enough formed to compute dominance
413 // information, and directly compute a dominance tree. We don't rely on the
414 // pass manager to provide this as it isolates us from a potentially
415 // out-of-date dominator tree and makes it significantly more complex to run
416 // this code outside of a pass manager.
417 // FIXME: It's really gross that we have to cast away constness here.
418 if (!F.empty())
419 DT.recalculate(const_cast<Function &>(F));
420
421 for (const BasicBlock &BB : F) {
422 if (!BB.empty() && BB.back().isTerminator())
423 continue;
424
425 if (OS) {
426 *OS << "Basic Block in function '" << F.getName()
427 << "' does not have terminator!\n";
428 BB.printAsOperand(*OS, true, MST);
429 *OS << "\n";
430 }
431 return false;
432 }
433
434 auto FailureCB = [this](const Twine &Message) {
435 this->CheckFailed(Message);
436 };
437 ConvergenceVerifyHelper.initialize(OS, FailureCB, F);
438
439 Broken = false;
440 // FIXME: We strip const here because the inst visitor strips const.
441 visit(const_cast<Function &>(F));
442 verifySiblingFuncletUnwinds();
443
444 if (ConvergenceVerifyHelper.sawTokens())
445 ConvergenceVerifyHelper.verify(DT);
446
447 InstsInThisBlock.clear();
448 DebugFnArgs.clear();
449 LandingPadResultTy = nullptr;
450 SawFrameEscape = false;
451 SiblingFuncletInfo.clear();
452 verifyNoAliasScopeDecl();
453 NoAliasScopeDecls.clear();
454
455 return !Broken;
456 }
457
458 /// Verify the module that this instance of \c Verifier was initialized with.
459 bool verify() {
460 Broken = false;
461
462 // Collect all declarations of the llvm.experimental.deoptimize intrinsic.
463 for (const Function &F : M)
464 if (F.getIntrinsicID() == Intrinsic::experimental_deoptimize)
465 DeoptimizeDeclarations.push_back(&F);
466
467 // Now that we've visited every function, verify that we never asked to
468 // recover a frame index that wasn't escaped.
469 verifyFrameRecoverIndices();
470 for (const GlobalVariable &GV : M.globals())
471 visitGlobalVariable(GV);
472
473 for (const GlobalAlias &GA : M.aliases())
474 visitGlobalAlias(GA);
475
476 for (const GlobalIFunc &GI : M.ifuncs())
477 visitGlobalIFunc(GI);
478
479 for (const NamedMDNode &NMD : M.named_metadata())
480 visitNamedMDNode(NMD);
481
482 for (const StringMapEntry<Comdat> &SMEC : M.getComdatSymbolTable())
483 visitComdat(SMEC.getValue());
484
485 visitModuleFlags();
486 visitModuleIdents();
487 visitModuleCommandLines();
488
489 verifyCompileUnits();
490
491 verifyDeoptimizeCallingConvs();
492 DISubprogramAttachments.clear();
493 return !Broken;
494 }
495
496private:
497 /// Whether a metadata node is allowed to be, or contain, a DILocation.
498 enum class AreDebugLocsAllowed { No, Yes };
499
500 // Verification methods...
501 void visitGlobalValue(const GlobalValue &GV);
502 void visitGlobalVariable(const GlobalVariable &GV);
503 void visitGlobalAlias(const GlobalAlias &GA);
504 void visitGlobalIFunc(const GlobalIFunc &GI);
505 void visitAliaseeSubExpr(const GlobalAlias &A, const Constant &C);
506 void visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias *> &Visited,
507 const GlobalAlias &A, const Constant &C);
508 void visitNamedMDNode(const NamedMDNode &NMD);
509 void visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs);
510 void visitMetadataAsValue(const MetadataAsValue &MD, Function *F);
511 void visitValueAsMetadata(const ValueAsMetadata &MD, Function *F);
512 void visitDIArgList(const DIArgList &AL, Function *F);
513 void visitComdat(const Comdat &C);
514 void visitModuleIdents();
515 void visitModuleCommandLines();
516 void visitModuleFlags();
517 void visitModuleFlag(const MDNode *Op,
519 SmallVectorImpl<const MDNode *> &Requirements);
520 void visitModuleFlagCGProfileEntry(const MDOperand &MDO);
521 void visitFunction(const Function &F);
522 void visitBasicBlock(BasicBlock &BB);
523 void verifyRangeMetadata(const Value &V, const MDNode *Range, Type *Ty,
524 bool IsAbsoluteSymbol);
525 void visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty);
526 void visitDereferenceableMetadata(Instruction &I, MDNode *MD);
527 void visitProfMetadata(Instruction &I, MDNode *MD);
528 void visitCallStackMetadata(MDNode *MD);
529 void visitMemProfMetadata(Instruction &I, MDNode *MD);
530 void visitCallsiteMetadata(Instruction &I, MDNode *MD);
531 void visitDIAssignIDMetadata(Instruction &I, MDNode *MD);
532 void visitAnnotationMetadata(MDNode *Annotation);
533 void visitAliasScopeMetadata(const MDNode *MD);
534 void visitAliasScopeListMetadata(const MDNode *MD);
535 void visitAccessGroupMetadata(const MDNode *MD);
536
537 template <class Ty> bool isValidMetadataArray(const MDTuple &N);
538#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N);
539#include "llvm/IR/Metadata.def"
540 void visitDIScope(const DIScope &N);
541 void visitDIVariable(const DIVariable &N);
542 void visitDILexicalBlockBase(const DILexicalBlockBase &N);
543 void visitDITemplateParameter(const DITemplateParameter &N);
544
545 void visitTemplateParams(const MDNode &N, const Metadata &RawParams);
546
547 void visit(DbgLabelRecord &DLR);
548 void visit(DbgVariableRecord &DVR);
549 // InstVisitor overrides...
551 void visitDbgRecords(Instruction &I);
552 void visit(Instruction &I);
553
554 void visitTruncInst(TruncInst &I);
555 void visitZExtInst(ZExtInst &I);
556 void visitSExtInst(SExtInst &I);
557 void visitFPTruncInst(FPTruncInst &I);
558 void visitFPExtInst(FPExtInst &I);
559 void visitFPToUIInst(FPToUIInst &I);
560 void visitFPToSIInst(FPToSIInst &I);
561 void visitUIToFPInst(UIToFPInst &I);
562 void visitSIToFPInst(SIToFPInst &I);
563 void visitIntToPtrInst(IntToPtrInst &I);
564 void visitPtrToIntInst(PtrToIntInst &I);
565 void visitBitCastInst(BitCastInst &I);
566 void visitAddrSpaceCastInst(AddrSpaceCastInst &I);
567 void visitPHINode(PHINode &PN);
568 void visitCallBase(CallBase &Call);
569 void visitUnaryOperator(UnaryOperator &U);
570 void visitBinaryOperator(BinaryOperator &B);
571 void visitICmpInst(ICmpInst &IC);
572 void visitFCmpInst(FCmpInst &FC);
573 void visitExtractElementInst(ExtractElementInst &EI);
574 void visitInsertElementInst(InsertElementInst &EI);
575 void visitShuffleVectorInst(ShuffleVectorInst &EI);
576 void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
577 void visitCallInst(CallInst &CI);
578 void visitInvokeInst(InvokeInst &II);
579 void visitGetElementPtrInst(GetElementPtrInst &GEP);
580 void visitLoadInst(LoadInst &LI);
581 void visitStoreInst(StoreInst &SI);
582 void verifyDominatesUse(Instruction &I, unsigned i);
583 void visitInstruction(Instruction &I);
584 void visitTerminator(Instruction &I);
585 void visitBranchInst(BranchInst &BI);
586 void visitReturnInst(ReturnInst &RI);
587 void visitSwitchInst(SwitchInst &SI);
588 void visitIndirectBrInst(IndirectBrInst &BI);
589 void visitCallBrInst(CallBrInst &CBI);
590 void visitSelectInst(SelectInst &SI);
591 void visitUserOp1(Instruction &I);
592 void visitUserOp2(Instruction &I) { visitUserOp1(I); }
593 void visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call);
594 void visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI);
595 void visitVPIntrinsic(VPIntrinsic &VPI);
596 void visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII);
597 void visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI);
598 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI);
599 void visitAtomicRMWInst(AtomicRMWInst &RMWI);
600 void visitFenceInst(FenceInst &FI);
601 void visitAllocaInst(AllocaInst &AI);
602 void visitExtractValueInst(ExtractValueInst &EVI);
603 void visitInsertValueInst(InsertValueInst &IVI);
604 void visitEHPadPredecessors(Instruction &I);
605 void visitLandingPadInst(LandingPadInst &LPI);
606 void visitResumeInst(ResumeInst &RI);
607 void visitCatchPadInst(CatchPadInst &CPI);
608 void visitCatchReturnInst(CatchReturnInst &CatchReturn);
609 void visitCleanupPadInst(CleanupPadInst &CPI);
610 void visitFuncletPadInst(FuncletPadInst &FPI);
611 void visitCatchSwitchInst(CatchSwitchInst &CatchSwitch);
612 void visitCleanupReturnInst(CleanupReturnInst &CRI);
613
614 void verifySwiftErrorCall(CallBase &Call, const Value *SwiftErrorVal);
615 void verifySwiftErrorValue(const Value *SwiftErrorVal);
616 void verifyTailCCMustTailAttrs(const AttrBuilder &Attrs, StringRef Context);
617 void verifyMustTailCall(CallInst &CI);
618 bool verifyAttributeCount(AttributeList Attrs, unsigned Params);
619 void verifyAttributeTypes(AttributeSet Attrs, const Value *V);
620 void verifyParameterAttrs(AttributeSet Attrs, Type *Ty, const Value *V);
621 void checkUnsignedBaseTenFuncAttr(AttributeList Attrs, StringRef Attr,
622 const Value *V);
623 void verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
624 const Value *V, bool IsIntrinsic, bool IsInlineAsm);
625 void verifyFunctionMetadata(ArrayRef<std::pair<unsigned, MDNode *>> MDs);
626
627 void visitConstantExprsRecursively(const Constant *EntryC);
628 void visitConstantExpr(const ConstantExpr *CE);
629 void verifyInlineAsmCall(const CallBase &Call);
630 void verifyStatepoint(const CallBase &Call);
631 void verifyFrameRecoverIndices();
632 void verifySiblingFuncletUnwinds();
633
634 void verifyFragmentExpression(const DbgVariableIntrinsic &I);
635 void verifyFragmentExpression(const DbgVariableRecord &I);
636 template <typename ValueOrMetadata>
637 void verifyFragmentExpression(const DIVariable &V,
639 ValueOrMetadata *Desc);
640 void verifyFnArgs(const DbgVariableIntrinsic &I);
641 void verifyFnArgs(const DbgVariableRecord &DVR);
642 void verifyNotEntryValue(const DbgVariableIntrinsic &I);
643 void verifyNotEntryValue(const DbgVariableRecord &I);
644
645 /// Module-level debug info verification...
646 void verifyCompileUnits();
647
648 /// Module-level verification that all @llvm.experimental.deoptimize
649 /// declarations share the same calling convention.
650 void verifyDeoptimizeCallingConvs();
651
652 void verifyAttachedCallBundle(const CallBase &Call,
653 const OperandBundleUse &BU);
654
655 /// Verify the llvm.experimental.noalias.scope.decl declarations
656 void verifyNoAliasScopeDecl();
657};
658
659} // end anonymous namespace
660
661/// We know that cond should be true, if not print an error message.
662#define Check(C, ...) \
663 do { \
664 if (!(C)) { \
665 CheckFailed(__VA_ARGS__); \
666 return; \
667 } \
668 } while (false)
669
670/// We know that a debug info condition should be true, if not print
671/// an error message.
672#define CheckDI(C, ...) \
673 do { \
674 if (!(C)) { \
675 DebugInfoCheckFailed(__VA_ARGS__); \
676 return; \
677 } \
678 } while (false)
679
680void Verifier::visitDbgRecords(Instruction &I) {
681 if (!I.DebugMarker)
682 return;
683 CheckDI(I.DebugMarker->MarkedInstr == &I,
684 "Instruction has invalid DebugMarker", &I);
685 CheckDI(!isa<PHINode>(&I) || !I.hasDbgRecords(),
686 "PHI Node must not have any attached DbgRecords", &I);
687 for (DbgRecord &DR : I.getDbgRecordRange()) {
688 CheckDI(DR.getMarker() == I.DebugMarker,
689 "DbgRecord had invalid DebugMarker", &I, &DR);
690 if (auto *Loc =
691 dyn_cast_or_null<DILocation>(DR.getDebugLoc().getAsMDNode()))
692 visitMDNode(*Loc, AreDebugLocsAllowed::Yes);
693 if (auto *DVR = dyn_cast<DbgVariableRecord>(&DR)) {
694 visit(*DVR);
695 // These have to appear after `visit` for consistency with existing
696 // intrinsic behaviour.
697 verifyFragmentExpression(*DVR);
698 verifyNotEntryValue(*DVR);
699 } else if (auto *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
700 visit(*DLR);
701 }
702 }
703}
704
705void Verifier::visit(Instruction &I) {
706 visitDbgRecords(I);
707 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
708 Check(I.getOperand(i) != nullptr, "Operand is null", &I);
710}
711
712// Helper to iterate over indirect users. By returning false, the callback can ask to stop traversing further.
713static void forEachUser(const Value *User,
715 llvm::function_ref<bool(const Value *)> Callback) {
716 if (!Visited.insert(User).second)
717 return;
718
721 while (!WorkList.empty()) {
722 const Value *Cur = WorkList.pop_back_val();
723 if (!Visited.insert(Cur).second)
724 continue;
725 if (Callback(Cur))
726 append_range(WorkList, Cur->materialized_users());
727 }
728}
729
730void Verifier::visitGlobalValue(const GlobalValue &GV) {
732 "Global is external, but doesn't have external or weak linkage!", &GV);
733
734 if (const GlobalObject *GO = dyn_cast<GlobalObject>(&GV)) {
735
736 if (MaybeAlign A = GO->getAlign()) {
737 Check(A->value() <= Value::MaximumAlignment,
738 "huge alignment values are unsupported", GO);
739 }
740
741 if (const MDNode *Associated =
742 GO->getMetadata(LLVMContext::MD_associated)) {
743 Check(Associated->getNumOperands() == 1,
744 "associated metadata must have one operand", &GV, Associated);
745 const Metadata *Op = Associated->getOperand(0).get();
746 Check(Op, "associated metadata must have a global value", GO, Associated);
747
748 const auto *VM = dyn_cast_or_null<ValueAsMetadata>(Op);
749 Check(VM, "associated metadata must be ValueAsMetadata", GO, Associated);
750 if (VM) {
751 Check(isa<PointerType>(VM->getValue()->getType()),
752 "associated value must be pointer typed", GV, Associated);
753
754 const Value *Stripped = VM->getValue()->stripPointerCastsAndAliases();
755 Check(isa<GlobalObject>(Stripped) || isa<Constant>(Stripped),
756 "associated metadata must point to a GlobalObject", GO, Stripped);
757 Check(Stripped != GO,
758 "global values should not associate to themselves", GO,
759 Associated);
760 }
761 }
762
763 // FIXME: Why is getMetadata on GlobalValue protected?
764 if (const MDNode *AbsoluteSymbol =
765 GO->getMetadata(LLVMContext::MD_absolute_symbol)) {
766 verifyRangeMetadata(*GO, AbsoluteSymbol, DL.getIntPtrType(GO->getType()),
767 true);
768 }
769 }
770
771 Check(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
772 "Only global variables can have appending linkage!", &GV);
773
774 if (GV.hasAppendingLinkage()) {
775 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV);
776 Check(GVar && GVar->getValueType()->isArrayTy(),
777 "Only global arrays can have appending linkage!", GVar);
778 }
779
780 if (GV.isDeclarationForLinker())
781 Check(!GV.hasComdat(), "Declaration may not be in a Comdat!", &GV);
782
783 if (GV.hasDLLExportStorageClass()) {
785 "dllexport GlobalValue must have default or protected visibility",
786 &GV);
787 }
788 if (GV.hasDLLImportStorageClass()) {
790 "dllimport GlobalValue must have default visibility", &GV);
791 Check(!GV.isDSOLocal(), "GlobalValue with DLLImport Storage is dso_local!",
792 &GV);
793
794 Check((GV.isDeclaration() &&
797 "Global is marked as dllimport, but not external", &GV);
798 }
799
800 if (GV.isImplicitDSOLocal())
801 Check(GV.isDSOLocal(),
802 "GlobalValue with local linkage or non-default "
803 "visibility must be dso_local!",
804 &GV);
805
806 forEachUser(&GV, GlobalValueVisited, [&](const Value *V) -> bool {
807 if (const Instruction *I = dyn_cast<Instruction>(V)) {
808 if (!I->getParent() || !I->getParent()->getParent())
809 CheckFailed("Global is referenced by parentless instruction!", &GV, &M,
810 I);
811 else if (I->getParent()->getParent()->getParent() != &M)
812 CheckFailed("Global is referenced in a different module!", &GV, &M, I,
813 I->getParent()->getParent(),
814 I->getParent()->getParent()->getParent());
815 return false;
816 } else if (const Function *F = dyn_cast<Function>(V)) {
817 if (F->getParent() != &M)
818 CheckFailed("Global is used by function in a different module", &GV, &M,
819 F, F->getParent());
820 return false;
821 }
822 return true;
823 });
824}
825
826void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
827 if (GV.hasInitializer()) {
829 "Global variable initializer type does not match global "
830 "variable type!",
831 &GV);
832 // If the global has common linkage, it must have a zero initializer and
833 // cannot be constant.
834 if (GV.hasCommonLinkage()) {
836 "'common' global must have a zero initializer!", &GV);
837 Check(!GV.isConstant(), "'common' global may not be marked constant!",
838 &GV);
839 Check(!GV.hasComdat(), "'common' global may not be in a Comdat!", &GV);
840 }
841 }
842
843 if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
844 GV.getName() == "llvm.global_dtors")) {
846 "invalid linkage for intrinsic global variable", &GV);
848 "invalid uses of intrinsic global variable", &GV);
849
850 // Don't worry about emitting an error for it not being an array,
851 // visitGlobalValue will complain on appending non-array.
852 if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getValueType())) {
853 StructType *STy = dyn_cast<StructType>(ATy->getElementType());
854 PointerType *FuncPtrTy =
855 PointerType::get(Context, DL.getProgramAddressSpace());
856 Check(STy && (STy->getNumElements() == 2 || STy->getNumElements() == 3) &&
857 STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
858 STy->getTypeAtIndex(1) == FuncPtrTy,
859 "wrong type for intrinsic global variable", &GV);
860 Check(STy->getNumElements() == 3,
861 "the third field of the element type is mandatory, "
862 "specify ptr null to migrate from the obsoleted 2-field form");
863 Type *ETy = STy->getTypeAtIndex(2);
864 Check(ETy->isPointerTy(), "wrong type for intrinsic global variable",
865 &GV);
866 }
867 }
868
869 if (GV.hasName() && (GV.getName() == "llvm.used" ||
870 GV.getName() == "llvm.compiler.used")) {
872 "invalid linkage for intrinsic global variable", &GV);
874 "invalid uses of intrinsic global variable", &GV);
875
876 Type *GVType = GV.getValueType();
877 if (ArrayType *ATy = dyn_cast<ArrayType>(GVType)) {
878 PointerType *PTy = dyn_cast<PointerType>(ATy->getElementType());
879 Check(PTy, "wrong type for intrinsic global variable", &GV);
880 if (GV.hasInitializer()) {
881 const Constant *Init = GV.getInitializer();
882 const ConstantArray *InitArray = dyn_cast<ConstantArray>(Init);
883 Check(InitArray, "wrong initalizer for intrinsic global variable",
884 Init);
885 for (Value *Op : InitArray->operands()) {
886 Value *V = Op->stripPointerCasts();
887 Check(isa<GlobalVariable>(V) || isa<Function>(V) ||
888 isa<GlobalAlias>(V),
889 Twine("invalid ") + GV.getName() + " member", V);
890 Check(V->hasName(),
891 Twine("members of ") + GV.getName() + " must be named", V);
892 }
893 }
894 }
895 }
896
897 // Visit any debug info attachments.
899 GV.getMetadata(LLVMContext::MD_dbg, MDs);
900 for (auto *MD : MDs) {
901 if (auto *GVE = dyn_cast<DIGlobalVariableExpression>(MD))
902 visitDIGlobalVariableExpression(*GVE);
903 else
904 CheckDI(false, "!dbg attachment of global variable must be a "
905 "DIGlobalVariableExpression");
906 }
907
908 // Scalable vectors cannot be global variables, since we don't know
909 // the runtime size.
911 "Globals cannot contain scalable types", &GV);
912
913 // Check if it's a target extension type that disallows being used as a
914 // global.
915 if (auto *TTy = dyn_cast<TargetExtType>(GV.getValueType()))
916 Check(TTy->hasProperty(TargetExtType::CanBeGlobal),
917 "Global @" + GV.getName() + " has illegal target extension type",
918 TTy);
919
920 if (!GV.hasInitializer()) {
921 visitGlobalValue(GV);
922 return;
923 }
924
925 // Walk any aggregate initializers looking for bitcasts between address spaces
926 visitConstantExprsRecursively(GV.getInitializer());
927
928 visitGlobalValue(GV);
929}
930
931void Verifier::visitAliaseeSubExpr(const GlobalAlias &GA, const Constant &C) {
933 Visited.insert(&GA);
934 visitAliaseeSubExpr(Visited, GA, C);
935}
936
937void Verifier::visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias*> &Visited,
938 const GlobalAlias &GA, const Constant &C) {
940 Check(isa<GlobalValue>(C) &&
941 cast<GlobalValue>(C).hasAvailableExternallyLinkage(),
942 "available_externally alias must point to available_externally "
943 "global value",
944 &GA);
945 }
946 if (const auto *GV = dyn_cast<GlobalValue>(&C)) {
948 Check(!GV->isDeclarationForLinker(), "Alias must point to a definition",
949 &GA);
950 }
951
952 if (const auto *GA2 = dyn_cast<GlobalAlias>(GV)) {
953 Check(Visited.insert(GA2).second, "Aliases cannot form a cycle", &GA);
954
955 Check(!GA2->isInterposable(),
956 "Alias cannot point to an interposable alias", &GA);
957 } else {
958 // Only continue verifying subexpressions of GlobalAliases.
959 // Do not recurse into global initializers.
960 return;
961 }
962 }
963
964 if (const auto *CE = dyn_cast<ConstantExpr>(&C))
965 visitConstantExprsRecursively(CE);
966
967 for (const Use &U : C.operands()) {
968 Value *V = &*U;
969 if (const auto *GA2 = dyn_cast<GlobalAlias>(V))
970 visitAliaseeSubExpr(Visited, GA, *GA2->getAliasee());
971 else if (const auto *C2 = dyn_cast<Constant>(V))
972 visitAliaseeSubExpr(Visited, GA, *C2);
973 }
974}
975
976void Verifier::visitGlobalAlias(const GlobalAlias &GA) {
978 "Alias should have private, internal, linkonce, weak, linkonce_odr, "
979 "weak_odr, external, or available_externally linkage!",
980 &GA);
981 const Constant *Aliasee = GA.getAliasee();
982 Check(Aliasee, "Aliasee cannot be NULL!", &GA);
983 Check(GA.getType() == Aliasee->getType(),
984 "Alias and aliasee types should match!", &GA);
985
986 Check(isa<GlobalValue>(Aliasee) || isa<ConstantExpr>(Aliasee),
987 "Aliasee should be either GlobalValue or ConstantExpr", &GA);
988
989 visitAliaseeSubExpr(GA, *Aliasee);
990
991 visitGlobalValue(GA);
992}
993
994void Verifier::visitGlobalIFunc(const GlobalIFunc &GI) {
996 "IFunc should have private, internal, linkonce, weak, linkonce_odr, "
997 "weak_odr, or external linkage!",
998 &GI);
999 // Pierce through ConstantExprs and GlobalAliases and check that the resolver
1000 // is a Function definition.
1002 Check(Resolver, "IFunc must have a Function resolver", &GI);
1003 Check(!Resolver->isDeclarationForLinker(),
1004 "IFunc resolver must be a definition", &GI);
1005
1006 // Check that the immediate resolver operand (prior to any bitcasts) has the
1007 // correct type.
1008 const Type *ResolverTy = GI.getResolver()->getType();
1009
1010 Check(isa<PointerType>(Resolver->getFunctionType()->getReturnType()),
1011 "IFunc resolver must return a pointer", &GI);
1012
1013 const Type *ResolverFuncTy =
1015 Check(ResolverTy == ResolverFuncTy->getPointerTo(GI.getAddressSpace()),
1016 "IFunc resolver has incorrect type", &GI);
1017}
1018
1019void Verifier::visitNamedMDNode(const NamedMDNode &NMD) {
1020 // There used to be various other llvm.dbg.* nodes, but we don't support
1021 // upgrading them and we want to reserve the namespace for future uses.
1022 if (NMD.getName().starts_with("llvm.dbg."))
1023 CheckDI(NMD.getName() == "llvm.dbg.cu",
1024 "unrecognized named metadata node in the llvm.dbg namespace", &NMD);
1025 for (const MDNode *MD : NMD.operands()) {
1026 if (NMD.getName() == "llvm.dbg.cu")
1027 CheckDI(MD && isa<DICompileUnit>(MD), "invalid compile unit", &NMD, MD);
1028
1029 if (!MD)
1030 continue;
1031
1032 visitMDNode(*MD, AreDebugLocsAllowed::Yes);
1033 }
1034}
1035
1036void Verifier::visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs) {
1037 // Only visit each node once. Metadata can be mutually recursive, so this
1038 // avoids infinite recursion here, as well as being an optimization.
1039 if (!MDNodes.insert(&MD).second)
1040 return;
1041
1042 Check(&MD.getContext() == &Context,
1043 "MDNode context does not match Module context!", &MD);
1044
1045 switch (MD.getMetadataID()) {
1046 default:
1047 llvm_unreachable("Invalid MDNode subclass");
1048 case Metadata::MDTupleKind:
1049 break;
1050#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
1051 case Metadata::CLASS##Kind: \
1052 visit##CLASS(cast<CLASS>(MD)); \
1053 break;
1054#include "llvm/IR/Metadata.def"
1055 }
1056
1057 for (const Metadata *Op : MD.operands()) {
1058 if (!Op)
1059 continue;
1060 Check(!isa<LocalAsMetadata>(Op), "Invalid operand for global metadata!",
1061 &MD, Op);
1062 CheckDI(!isa<DILocation>(Op) || AllowLocs == AreDebugLocsAllowed::Yes,
1063 "DILocation not allowed within this metadata node", &MD, Op);
1064 if (auto *N = dyn_cast<MDNode>(Op)) {
1065 visitMDNode(*N, AllowLocs);
1066 continue;
1067 }
1068 if (auto *V = dyn_cast<ValueAsMetadata>(Op)) {
1069 visitValueAsMetadata(*V, nullptr);
1070 continue;
1071 }
1072 }
1073
1074 // Check these last, so we diagnose problems in operands first.
1075 Check(!MD.isTemporary(), "Expected no forward declarations!", &MD);
1076 Check(MD.isResolved(), "All nodes should be resolved!", &MD);
1077}
1078
1079void Verifier::visitValueAsMetadata(const ValueAsMetadata &MD, Function *F) {
1080 Check(MD.getValue(), "Expected valid value", &MD);
1081 Check(!MD.getValue()->getType()->isMetadataTy(),
1082 "Unexpected metadata round-trip through values", &MD, MD.getValue());
1083
1084 auto *L = dyn_cast<LocalAsMetadata>(&MD);
1085 if (!L)
1086 return;
1087
1088 Check(F, "function-local metadata used outside a function", L);
1089
1090 // If this was an instruction, bb, or argument, verify that it is in the
1091 // function that we expect.
1092 Function *ActualF = nullptr;
1093 if (Instruction *I = dyn_cast<Instruction>(L->getValue())) {
1094 Check(I->getParent(), "function-local metadata not in basic block", L, I);
1095 ActualF = I->getParent()->getParent();
1096 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(L->getValue()))
1097 ActualF = BB->getParent();
1098 else if (Argument *A = dyn_cast<Argument>(L->getValue()))
1099 ActualF = A->getParent();
1100 assert(ActualF && "Unimplemented function local metadata case!");
1101
1102 Check(ActualF == F, "function-local metadata used in wrong function", L);
1103}
1104
1105void Verifier::visitDIArgList(const DIArgList &AL, Function *F) {
1106 for (const ValueAsMetadata *VAM : AL.getArgs())
1107 visitValueAsMetadata(*VAM, F);
1108}
1109
1110void Verifier::visitMetadataAsValue(const MetadataAsValue &MDV, Function *F) {
1111 Metadata *MD = MDV.getMetadata();
1112 if (auto *N = dyn_cast<MDNode>(MD)) {
1113 visitMDNode(*N, AreDebugLocsAllowed::No);
1114 return;
1115 }
1116
1117 // Only visit each node once. Metadata can be mutually recursive, so this
1118 // avoids infinite recursion here, as well as being an optimization.
1119 if (!MDNodes.insert(MD).second)
1120 return;
1121
1122 if (auto *V = dyn_cast<ValueAsMetadata>(MD))
1123 visitValueAsMetadata(*V, F);
1124
1125 if (auto *AL = dyn_cast<DIArgList>(MD))
1126 visitDIArgList(*AL, F);
1127}
1128
1129static bool isType(const Metadata *MD) { return !MD || isa<DIType>(MD); }
1130static bool isScope(const Metadata *MD) { return !MD || isa<DIScope>(MD); }
1131static bool isDINode(const Metadata *MD) { return !MD || isa<DINode>(MD); }
1132
1133void Verifier::visitDILocation(const DILocation &N) {
1134 CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1135 "location requires a valid scope", &N, N.getRawScope());
1136 if (auto *IA = N.getRawInlinedAt())
1137 CheckDI(isa<DILocation>(IA), "inlined-at should be a location", &N, IA);
1138 if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
1139 CheckDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
1140}
1141
1142void Verifier::visitGenericDINode(const GenericDINode &N) {
1143 CheckDI(N.getTag(), "invalid tag", &N);
1144}
1145
1146void Verifier::visitDIScope(const DIScope &N) {
1147 if (auto *F = N.getRawFile())
1148 CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1149}
1150
1151void Verifier::visitDISubrange(const DISubrange &N) {
1152 CheckDI(N.getTag() == dwarf::DW_TAG_subrange_type, "invalid tag", &N);
1153 bool HasAssumedSizedArraySupport = dwarf::isFortran(CurrentSourceLang);
1154 CheckDI(HasAssumedSizedArraySupport || N.getRawCountNode() ||
1155 N.getRawUpperBound(),
1156 "Subrange must contain count or upperBound", &N);
1157 CheckDI(!N.getRawCountNode() || !N.getRawUpperBound(),
1158 "Subrange can have any one of count or upperBound", &N);
1159 auto *CBound = N.getRawCountNode();
1160 CheckDI(!CBound || isa<ConstantAsMetadata>(CBound) ||
1161 isa<DIVariable>(CBound) || isa<DIExpression>(CBound),
1162 "Count must be signed constant or DIVariable or DIExpression", &N);
1163 auto Count = N.getCount();
1164 CheckDI(!Count || !isa<ConstantInt *>(Count) ||
1165 cast<ConstantInt *>(Count)->getSExtValue() >= -1,
1166 "invalid subrange count", &N);
1167 auto *LBound = N.getRawLowerBound();
1168 CheckDI(!LBound || isa<ConstantAsMetadata>(LBound) ||
1169 isa<DIVariable>(LBound) || isa<DIExpression>(LBound),
1170 "LowerBound must be signed constant or DIVariable or DIExpression",
1171 &N);
1172 auto *UBound = N.getRawUpperBound();
1173 CheckDI(!UBound || isa<ConstantAsMetadata>(UBound) ||
1174 isa<DIVariable>(UBound) || isa<DIExpression>(UBound),
1175 "UpperBound must be signed constant or DIVariable or DIExpression",
1176 &N);
1177 auto *Stride = N.getRawStride();
1178 CheckDI(!Stride || isa<ConstantAsMetadata>(Stride) ||
1179 isa<DIVariable>(Stride) || isa<DIExpression>(Stride),
1180 "Stride must be signed constant or DIVariable or DIExpression", &N);
1181}
1182
1183void Verifier::visitDIGenericSubrange(const DIGenericSubrange &N) {
1184 CheckDI(N.getTag() == dwarf::DW_TAG_generic_subrange, "invalid tag", &N);
1185 CheckDI(N.getRawCountNode() || N.getRawUpperBound(),
1186 "GenericSubrange must contain count or upperBound", &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",
1196 &N);
1197 auto *UBound = N.getRawUpperBound();
1198 CheckDI(!UBound || isa<DIVariable>(UBound) || isa<DIExpression>(UBound),
1199 "UpperBound must be signed constant or DIVariable or DIExpression",
1200 &N);
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);
1205}
1206
1207void Verifier::visitDIEnumerator(const DIEnumerator &N) {
1208 CheckDI(N.getTag() == dwarf::DW_TAG_enumerator, "invalid tag", &N);
1209}
1210
1211void 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,
1215 "invalid tag", &N);
1216}
1217
1218void 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",
1221 &N);
1222}
1223
1224void Verifier::visitDIDerivedType(const DIDerivedType &N) {
1225 // Common scope checks.
1226 visitDIScope(N);
1227
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 "invalid tag", &N);
1245 if (N.getTag() == dwarf::DW_TAG_ptr_to_member_type) {
1246 CheckDI(isType(N.getRawExtraData()), "invalid pointer to member type", &N,
1247 N.getRawExtraData());
1248 }
1249
1250 if (N.getTag() == dwarf::DW_TAG_set_type) {
1251 if (auto *T = N.getRawBaseType()) {
1252 auto *Enum = dyn_cast_or_null<DICompositeType>(T);
1253 auto *Basic = dyn_cast_or_null<DIBasicType>(T);
1254 CheckDI(
1255 (Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type) ||
1256 (Basic && (Basic->getEncoding() == dwarf::DW_ATE_unsigned ||
1257 Basic->getEncoding() == dwarf::DW_ATE_signed ||
1258 Basic->getEncoding() == dwarf::DW_ATE_unsigned_char ||
1259 Basic->getEncoding() == dwarf::DW_ATE_signed_char ||
1260 Basic->getEncoding() == dwarf::DW_ATE_boolean)),
1261 "invalid set base type", &N, T);
1262 }
1263 }
1264
1265 CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
1266 CheckDI(isType(N.getRawBaseType()), "invalid base type", &N,
1267 N.getRawBaseType());
1268
1269 if (N.getDWARFAddressSpace()) {
1270 CheckDI(N.getTag() == dwarf::DW_TAG_pointer_type ||
1271 N.getTag() == dwarf::DW_TAG_reference_type ||
1272 N.getTag() == dwarf::DW_TAG_rvalue_reference_type,
1273 "DWARF address space only applies to pointer or reference types",
1274 &N);
1275 }
1276}
1277
1278/// Detect mutually exclusive flags.
1279static bool hasConflictingReferenceFlags(unsigned Flags) {
1280 return ((Flags & DINode::FlagLValueReference) &&
1281 (Flags & DINode::FlagRValueReference)) ||
1282 ((Flags & DINode::FlagTypePassByValue) &&
1283 (Flags & DINode::FlagTypePassByReference));
1284}
1285
1286void Verifier::visitTemplateParams(const MDNode &N, const Metadata &RawParams) {
1287 auto *Params = dyn_cast<MDTuple>(&RawParams);
1288 CheckDI(Params, "invalid template params", &N, &RawParams);
1289 for (Metadata *Op : Params->operands()) {
1290 CheckDI(Op && isa<DITemplateParameter>(Op), "invalid template parameter",
1291 &N, Params, Op);
1292 }
1293}
1294
1295void Verifier::visitDICompositeType(const DICompositeType &N) {
1296 // Common scope checks.
1297 visitDIScope(N);
1298
1299 CheckDI(N.getTag() == dwarf::DW_TAG_array_type ||
1300 N.getTag() == dwarf::DW_TAG_structure_type ||
1301 N.getTag() == dwarf::DW_TAG_union_type ||
1302 N.getTag() == dwarf::DW_TAG_enumeration_type ||
1303 N.getTag() == dwarf::DW_TAG_class_type ||
1304 N.getTag() == dwarf::DW_TAG_variant_part ||
1305 N.getTag() == dwarf::DW_TAG_namelist,
1306 "invalid tag", &N);
1307
1308 CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
1309 CheckDI(isType(N.getRawBaseType()), "invalid base type", &N,
1310 N.getRawBaseType());
1311
1312 CheckDI(!N.getRawElements() || isa<MDTuple>(N.getRawElements()),
1313 "invalid composite elements", &N, N.getRawElements());
1314 CheckDI(isType(N.getRawVTableHolder()), "invalid vtable holder", &N,
1315 N.getRawVTableHolder());
1317 "invalid reference flags", &N);
1318 unsigned DIBlockByRefStruct = 1 << 4;
1319 CheckDI((N.getFlags() & DIBlockByRefStruct) == 0,
1320 "DIBlockByRefStruct on DICompositeType is no longer supported", &N);
1321
1322 if (N.isVector()) {
1323 const DINodeArray Elements = N.getElements();
1324 CheckDI(Elements.size() == 1 &&
1325 Elements[0]->getTag() == dwarf::DW_TAG_subrange_type,
1326 "invalid vector, expected one element of type subrange", &N);
1327 }
1328
1329 if (auto *Params = N.getRawTemplateParams())
1330 visitTemplateParams(N, *Params);
1331
1332 if (auto *D = N.getRawDiscriminator()) {
1333 CheckDI(isa<DIDerivedType>(D) && N.getTag() == dwarf::DW_TAG_variant_part,
1334 "discriminator can only appear on variant part");
1335 }
1336
1337 if (N.getRawDataLocation()) {
1338 CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1339 "dataLocation can only appear in array type");
1340 }
1341
1342 if (N.getRawAssociated()) {
1343 CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1344 "associated can only appear in array type");
1345 }
1346
1347 if (N.getRawAllocated()) {
1348 CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1349 "allocated can only appear in array type");
1350 }
1351
1352 if (N.getRawRank()) {
1353 CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1354 "rank can only appear in array type");
1355 }
1356
1357 if (N.getTag() == dwarf::DW_TAG_array_type) {
1358 CheckDI(N.getRawBaseType(), "array types must have a base type", &N);
1359 }
1360}
1361
1362void Verifier::visitDISubroutineType(const DISubroutineType &N) {
1363 CheckDI(N.getTag() == dwarf::DW_TAG_subroutine_type, "invalid tag", &N);
1364 if (auto *Types = N.getRawTypeArray()) {
1365 CheckDI(isa<MDTuple>(Types), "invalid composite elements", &N, Types);
1366 for (Metadata *Ty : N.getTypeArray()->operands()) {
1367 CheckDI(isType(Ty), "invalid subroutine type ref", &N, Types, Ty);
1368 }
1369 }
1371 "invalid reference flags", &N);
1372}
1373
1374void Verifier::visitDIFile(const DIFile &N) {
1375 CheckDI(N.getTag() == dwarf::DW_TAG_file_type, "invalid tag", &N);
1376 std::optional<DIFile::ChecksumInfo<StringRef>> Checksum = N.getChecksum();
1377 if (Checksum) {
1378 CheckDI(Checksum->Kind <= DIFile::ChecksumKind::CSK_Last,
1379 "invalid checksum kind", &N);
1380 size_t Size;
1381 switch (Checksum->Kind) {
1382 case DIFile::CSK_MD5:
1383 Size = 32;
1384 break;
1385 case DIFile::CSK_SHA1:
1386 Size = 40;
1387 break;
1388 case DIFile::CSK_SHA256:
1389 Size = 64;
1390 break;
1391 }
1392 CheckDI(Checksum->Value.size() == Size, "invalid checksum length", &N);
1393 CheckDI(Checksum->Value.find_if_not(llvm::isHexDigit) == StringRef::npos,
1394 "invalid checksum", &N);
1395 }
1396}
1397
1398void Verifier::visitDICompileUnit(const DICompileUnit &N) {
1399 CheckDI(N.isDistinct(), "compile units must be distinct", &N);
1400 CheckDI(N.getTag() == dwarf::DW_TAG_compile_unit, "invalid tag", &N);
1401
1402 // Don't bother verifying the compilation directory or producer string
1403 // as those could be empty.
1404 CheckDI(N.getRawFile() && isa<DIFile>(N.getRawFile()), "invalid file", &N,
1405 N.getRawFile());
1406 CheckDI(!N.getFile()->getFilename().empty(), "invalid filename", &N,
1407 N.getFile());
1408
1409 CurrentSourceLang = (dwarf::SourceLanguage)N.getSourceLanguage();
1410
1411 CheckDI((N.getEmissionKind() <= DICompileUnit::LastEmissionKind),
1412 "invalid emission kind", &N);
1413
1414 if (auto *Array = N.getRawEnumTypes()) {
1415 CheckDI(isa<MDTuple>(Array), "invalid enum list", &N, Array);
1416 for (Metadata *Op : N.getEnumTypes()->operands()) {
1417 auto *Enum = dyn_cast_or_null<DICompositeType>(Op);
1418 CheckDI(Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type,
1419 "invalid enum type", &N, N.getEnumTypes(), Op);
1420 }
1421 }
1422 if (auto *Array = N.getRawRetainedTypes()) {
1423 CheckDI(isa<MDTuple>(Array), "invalid retained type list", &N, Array);
1424 for (Metadata *Op : N.getRetainedTypes()->operands()) {
1425 CheckDI(
1426 Op && (isa<DIType>(Op) || (isa<DISubprogram>(Op) &&
1427 !cast<DISubprogram>(Op)->isDefinition())),
1428 "invalid retained type", &N, Op);
1429 }
1430 }
1431 if (auto *Array = N.getRawGlobalVariables()) {
1432 CheckDI(isa<MDTuple>(Array), "invalid global variable list", &N, Array);
1433 for (Metadata *Op : N.getGlobalVariables()->operands()) {
1434 CheckDI(Op && (isa<DIGlobalVariableExpression>(Op)),
1435 "invalid global variable ref", &N, Op);
1436 }
1437 }
1438 if (auto *Array = N.getRawImportedEntities()) {
1439 CheckDI(isa<MDTuple>(Array), "invalid imported entity list", &N, Array);
1440 for (Metadata *Op : N.getImportedEntities()->operands()) {
1441 CheckDI(Op && isa<DIImportedEntity>(Op), "invalid imported entity ref",
1442 &N, Op);
1443 }
1444 }
1445 if (auto *Array = N.getRawMacros()) {
1446 CheckDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
1447 for (Metadata *Op : N.getMacros()->operands()) {
1448 CheckDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
1449 }
1450 }
1451 CUVisited.insert(&N);
1452}
1453
1454void Verifier::visitDISubprogram(const DISubprogram &N) {
1455 CheckDI(N.getTag() == dwarf::DW_TAG_subprogram, "invalid tag", &N);
1456 CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
1457 if (auto *F = N.getRawFile())
1458 CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1459 else
1460 CheckDI(N.getLine() == 0, "line specified with no file", &N, N.getLine());
1461 if (auto *T = N.getRawType())
1462 CheckDI(isa<DISubroutineType>(T), "invalid subroutine type", &N, T);
1463 CheckDI(isType(N.getRawContainingType()), "invalid containing type", &N,
1464 N.getRawContainingType());
1465 if (auto *Params = N.getRawTemplateParams())
1466 visitTemplateParams(N, *Params);
1467 if (auto *S = N.getRawDeclaration())
1468 CheckDI(isa<DISubprogram>(S) && !cast<DISubprogram>(S)->isDefinition(),
1469 "invalid subprogram declaration", &N, S);
1470 if (auto *RawNode = N.getRawRetainedNodes()) {
1471 auto *Node = dyn_cast<MDTuple>(RawNode);
1472 CheckDI(Node, "invalid retained nodes list", &N, RawNode);
1473 for (Metadata *Op : Node->operands()) {
1474 CheckDI(Op && (isa<DILocalVariable>(Op) || isa<DILabel>(Op) ||
1475 isa<DIImportedEntity>(Op)),
1476 "invalid retained nodes, expected DILocalVariable, DILabel or "
1477 "DIImportedEntity",
1478 &N, Node, Op);
1479 }
1480 }
1482 "invalid reference flags", &N);
1483
1484 auto *Unit = N.getRawUnit();
1485 if (N.isDefinition()) {
1486 // Subprogram definitions (not part of the type hierarchy).
1487 CheckDI(N.isDistinct(), "subprogram definitions must be distinct", &N);
1488 CheckDI(Unit, "subprogram definitions must have a compile unit", &N);
1489 CheckDI(isa<DICompileUnit>(Unit), "invalid unit type", &N, Unit);
1490 // There's no good way to cross the CU boundary to insert a nested
1491 // DISubprogram definition in one CU into a type defined in another CU.
1492 auto *CT = dyn_cast_or_null<DICompositeType>(N.getRawScope());
1493 if (CT && CT->getRawIdentifier() &&
1494 M.getContext().isODRUniquingDebugTypes())
1495 CheckDI(N.getDeclaration(),
1496 "definition subprograms cannot be nested within DICompositeType "
1497 "when enabling ODR",
1498 &N);
1499 } else {
1500 // Subprogram declarations (part of the type hierarchy).
1501 CheckDI(!Unit, "subprogram declarations must not have a compile unit", &N);
1502 CheckDI(!N.getRawDeclaration(),
1503 "subprogram declaration must not have a declaration field");
1504 }
1505
1506 if (auto *RawThrownTypes = N.getRawThrownTypes()) {
1507 auto *ThrownTypes = dyn_cast<MDTuple>(RawThrownTypes);
1508 CheckDI(ThrownTypes, "invalid thrown types list", &N, RawThrownTypes);
1509 for (Metadata *Op : ThrownTypes->operands())
1510 CheckDI(Op && isa<DIType>(Op), "invalid thrown type", &N, ThrownTypes,
1511 Op);
1512 }
1513
1514 if (N.areAllCallsDescribed())
1515 CheckDI(N.isDefinition(),
1516 "DIFlagAllCallsDescribed must be attached to a definition");
1517}
1518
1519void Verifier::visitDILexicalBlockBase(const DILexicalBlockBase &N) {
1520 CheckDI(N.getTag() == dwarf::DW_TAG_lexical_block, "invalid tag", &N);
1521 CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1522 "invalid local scope", &N, N.getRawScope());
1523 if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
1524 CheckDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
1525}
1526
1527void Verifier::visitDILexicalBlock(const DILexicalBlock &N) {
1528 visitDILexicalBlockBase(N);
1529
1530 CheckDI(N.getLine() || !N.getColumn(),
1531 "cannot have column info without line info", &N);
1532}
1533
1534void Verifier::visitDILexicalBlockFile(const DILexicalBlockFile &N) {
1535 visitDILexicalBlockBase(N);
1536}
1537
1538void Verifier::visitDICommonBlock(const DICommonBlock &N) {
1539 CheckDI(N.getTag() == dwarf::DW_TAG_common_block, "invalid tag", &N);
1540 if (auto *S = N.getRawScope())
1541 CheckDI(isa<DIScope>(S), "invalid scope ref", &N, S);
1542 if (auto *S = N.getRawDecl())
1543 CheckDI(isa<DIGlobalVariable>(S), "invalid declaration", &N, S);
1544}
1545
1546void Verifier::visitDINamespace(const DINamespace &N) {
1547 CheckDI(N.getTag() == dwarf::DW_TAG_namespace, "invalid tag", &N);
1548 if (auto *S = N.getRawScope())
1549 CheckDI(isa<DIScope>(S), "invalid scope ref", &N, S);
1550}
1551
1552void Verifier::visitDIMacro(const DIMacro &N) {
1553 CheckDI(N.getMacinfoType() == dwarf::DW_MACINFO_define ||
1554 N.getMacinfoType() == dwarf::DW_MACINFO_undef,
1555 "invalid macinfo type", &N);
1556 CheckDI(!N.getName().empty(), "anonymous macro", &N);
1557 if (!N.getValue().empty()) {
1558 assert(N.getValue().data()[0] != ' ' && "Macro value has a space prefix");
1559 }
1560}
1561
1562void Verifier::visitDIMacroFile(const DIMacroFile &N) {
1563 CheckDI(N.getMacinfoType() == dwarf::DW_MACINFO_start_file,
1564 "invalid macinfo type", &N);
1565 if (auto *F = N.getRawFile())
1566 CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1567
1568 if (auto *Array = N.getRawElements()) {
1569 CheckDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
1570 for (Metadata *Op : N.getElements()->operands()) {
1571 CheckDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
1572 }
1573 }
1574}
1575
1576void Verifier::visitDIModule(const DIModule &N) {
1577 CheckDI(N.getTag() == dwarf::DW_TAG_module, "invalid tag", &N);
1578 CheckDI(!N.getName().empty(), "anonymous module", &N);
1579}
1580
1581void Verifier::visitDITemplateParameter(const DITemplateParameter &N) {
1582 CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1583}
1584
1585void Verifier::visitDITemplateTypeParameter(const DITemplateTypeParameter &N) {
1586 visitDITemplateParameter(N);
1587
1588 CheckDI(N.getTag() == dwarf::DW_TAG_template_type_parameter, "invalid tag",
1589 &N);
1590}
1591
1592void Verifier::visitDITemplateValueParameter(
1593 const DITemplateValueParameter &N) {
1594 visitDITemplateParameter(N);
1595
1596 CheckDI(N.getTag() == dwarf::DW_TAG_template_value_parameter ||
1597 N.getTag() == dwarf::DW_TAG_GNU_template_template_param ||
1598 N.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack,
1599 "invalid tag", &N);
1600}
1601
1602void Verifier::visitDIVariable(const DIVariable &N) {
1603 if (auto *S = N.getRawScope())
1604 CheckDI(isa<DIScope>(S), "invalid scope", &N, S);
1605 if (auto *F = N.getRawFile())
1606 CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1607}
1608
1609void Verifier::visitDIGlobalVariable(const DIGlobalVariable &N) {
1610 // Checks common to all variables.
1611 visitDIVariable(N);
1612
1613 CheckDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
1614 CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1615 // Check only if the global variable is not an extern
1616 if (N.isDefinition())
1617 CheckDI(N.getType(), "missing global variable type", &N);
1618 if (auto *Member = N.getRawStaticDataMemberDeclaration()) {
1619 CheckDI(isa<DIDerivedType>(Member),
1620 "invalid static data member declaration", &N, Member);
1621 }
1622}
1623
1624void Verifier::visitDILocalVariable(const DILocalVariable &N) {
1625 // Checks common to all variables.
1626 visitDIVariable(N);
1627
1628 CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1629 CheckDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
1630 CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1631 "local variable requires a valid scope", &N, N.getRawScope());
1632 if (auto Ty = N.getType())
1633 CheckDI(!isa<DISubroutineType>(Ty), "invalid type", &N, N.getType());
1634}
1635
1636void Verifier::visitDIAssignID(const DIAssignID &N) {
1637 CheckDI(!N.getNumOperands(), "DIAssignID has no arguments", &N);
1638 CheckDI(N.isDistinct(), "DIAssignID must be distinct", &N);
1639}
1640
1641void Verifier::visitDILabel(const DILabel &N) {
1642 if (auto *S = N.getRawScope())
1643 CheckDI(isa<DIScope>(S), "invalid scope", &N, S);
1644 if (auto *F = N.getRawFile())
1645 CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1646
1647 CheckDI(N.getTag() == dwarf::DW_TAG_label, "invalid tag", &N);
1648 CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1649 "label requires a valid scope", &N, N.getRawScope());
1650}
1651
1652void Verifier::visitDIExpression(const DIExpression &N) {
1653 CheckDI(N.isValid(), "invalid expression", &N);
1654}
1655
1656void Verifier::visitDIGlobalVariableExpression(
1657 const DIGlobalVariableExpression &GVE) {
1658 CheckDI(GVE.getVariable(), "missing variable");
1659 if (auto *Var = GVE.getVariable())
1660 visitDIGlobalVariable(*Var);
1661 if (auto *Expr = GVE.getExpression()) {
1662 visitDIExpression(*Expr);
1663 if (auto Fragment = Expr->getFragmentInfo())
1664 verifyFragmentExpression(*GVE.getVariable(), *Fragment, &GVE);
1665 }
1666}
1667
1668void Verifier::visitDIObjCProperty(const DIObjCProperty &N) {
1669 CheckDI(N.getTag() == dwarf::DW_TAG_APPLE_property, "invalid tag", &N);
1670 if (auto *T = N.getRawType())
1671 CheckDI(isType(T), "invalid type ref", &N, T);
1672 if (auto *F = N.getRawFile())
1673 CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1674}
1675
1676void Verifier::visitDIImportedEntity(const DIImportedEntity &N) {
1677 CheckDI(N.getTag() == dwarf::DW_TAG_imported_module ||
1678 N.getTag() == dwarf::DW_TAG_imported_declaration,
1679 "invalid tag", &N);
1680 if (auto *S = N.getRawScope())
1681 CheckDI(isa<DIScope>(S), "invalid scope for imported entity", &N, S);
1682 CheckDI(isDINode(N.getRawEntity()), "invalid imported entity", &N,
1683 N.getRawEntity());
1684}
1685
1686void Verifier::visitComdat(const Comdat &C) {
1687 // In COFF the Module is invalid if the GlobalValue has private linkage.
1688 // Entities with private linkage don't have entries in the symbol table.
1689 if (TT.isOSBinFormatCOFF())
1690 if (const GlobalValue *GV = M.getNamedValue(C.getName()))
1691 Check(!GV->hasPrivateLinkage(), "comdat global value has private linkage",
1692 GV);
1693}
1694
1695void Verifier::visitModuleIdents() {
1696 const NamedMDNode *Idents = M.getNamedMetadata("llvm.ident");
1697 if (!Idents)
1698 return;
1699
1700 // llvm.ident takes a list of metadata entry. Each entry has only one string.
1701 // Scan each llvm.ident entry and make sure that this requirement is met.
1702 for (const MDNode *N : Idents->operands()) {
1703 Check(N->getNumOperands() == 1,
1704 "incorrect number of operands in llvm.ident metadata", N);
1705 Check(dyn_cast_or_null<MDString>(N->getOperand(0)),
1706 ("invalid value for llvm.ident metadata entry operand"
1707 "(the operand should be a string)"),
1708 N->getOperand(0));
1709 }
1710}
1711
1712void Verifier::visitModuleCommandLines() {
1713 const NamedMDNode *CommandLines = M.getNamedMetadata("llvm.commandline");
1714 if (!CommandLines)
1715 return;
1716
1717 // llvm.commandline takes a list of metadata entry. Each entry has only one
1718 // string. Scan each llvm.commandline entry and make sure that this
1719 // requirement is met.
1720 for (const MDNode *N : CommandLines->operands()) {
1721 Check(N->getNumOperands() == 1,
1722 "incorrect number of operands in llvm.commandline metadata", N);
1723 Check(dyn_cast_or_null<MDString>(N->getOperand(0)),
1724 ("invalid value for llvm.commandline metadata entry operand"
1725 "(the operand should be a string)"),
1726 N->getOperand(0));
1727 }
1728}
1729
1730void Verifier::visitModuleFlags() {
1731 const NamedMDNode *Flags = M.getModuleFlagsMetadata();
1732 if (!Flags) return;
1733
1734 // Scan each flag, and track the flags and requirements.
1736 SmallVector<const MDNode*, 16> Requirements;
1737 uint64_t PAuthABIPlatform = -1;
1738 uint64_t PAuthABIVersion = -1;
1739 for (const MDNode *MDN : Flags->operands()) {
1740 visitModuleFlag(MDN, SeenIDs, Requirements);
1741 if (MDN->getNumOperands() != 3)
1742 continue;
1743 if (const auto *FlagName = dyn_cast_or_null<MDString>(MDN->getOperand(1))) {
1744 if (FlagName->getString() == "aarch64-elf-pauthabi-platform") {
1745 if (const auto *PAP =
1746 mdconst::dyn_extract_or_null<ConstantInt>(MDN->getOperand(2)))
1747 PAuthABIPlatform = PAP->getZExtValue();
1748 } else if (FlagName->getString() == "aarch64-elf-pauthabi-version") {
1749 if (const auto *PAV =
1750 mdconst::dyn_extract_or_null<ConstantInt>(MDN->getOperand(2)))
1751 PAuthABIVersion = PAV->getZExtValue();
1752 }
1753 }
1754 }
1755
1756 if ((PAuthABIPlatform == uint64_t(-1)) != (PAuthABIVersion == uint64_t(-1)))
1757 CheckFailed("either both or no 'aarch64-elf-pauthabi-platform' and "
1758 "'aarch64-elf-pauthabi-version' module flags must be present");
1759
1760 // Validate that the requirements in the module are valid.
1761 for (const MDNode *Requirement : Requirements) {
1762 const MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1763 const Metadata *ReqValue = Requirement->getOperand(1);
1764
1765 const MDNode *Op = SeenIDs.lookup(Flag);
1766 if (!Op) {
1767 CheckFailed("invalid requirement on flag, flag is not present in module",
1768 Flag);
1769 continue;
1770 }
1771
1772 if (Op->getOperand(2) != ReqValue) {
1773 CheckFailed(("invalid requirement on flag, "
1774 "flag does not have the required value"),
1775 Flag);
1776 continue;
1777 }
1778 }
1779}
1780
1781void
1782Verifier::visitModuleFlag(const MDNode *Op,
1784 SmallVectorImpl<const MDNode *> &Requirements) {
1785 // Each module flag should have three arguments, the merge behavior (a
1786 // constant int), the flag ID (an MDString), and the value.
1787 Check(Op->getNumOperands() == 3,
1788 "incorrect number of operands in module flag", Op);
1790 if (!Module::isValidModFlagBehavior(Op->getOperand(0), MFB)) {
1791 Check(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0)),
1792 "invalid behavior operand in module flag (expected constant integer)",
1793 Op->getOperand(0));
1794 Check(false,
1795 "invalid behavior operand in module flag (unexpected constant)",
1796 Op->getOperand(0));
1797 }
1798 MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
1799 Check(ID, "invalid ID operand in module flag (expected metadata string)",
1800 Op->getOperand(1));
1801
1802 // Check the values for behaviors with additional requirements.
1803 switch (MFB) {
1804 case Module::Error:
1805 case Module::Warning:
1806 case Module::Override:
1807 // These behavior types accept any value.
1808 break;
1809
1810 case Module::Min: {
1811 auto *V = mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1812 Check(V && V->getValue().isNonNegative(),
1813 "invalid value for 'min' module flag (expected constant non-negative "
1814 "integer)",
1815 Op->getOperand(2));
1816 break;
1817 }
1818
1819 case Module::Max: {
1820 Check(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2)),
1821 "invalid value for 'max' module flag (expected constant integer)",
1822 Op->getOperand(2));
1823 break;
1824 }
1825
1826 case Module::Require: {
1827 // The value should itself be an MDNode with two operands, a flag ID (an
1828 // MDString), and a value.
1829 MDNode *Value = dyn_cast<MDNode>(Op->getOperand(2));
1830 Check(Value && Value->getNumOperands() == 2,
1831 "invalid value for 'require' module flag (expected metadata pair)",
1832 Op->getOperand(2));
1833 Check(isa<MDString>(Value->getOperand(0)),
1834 ("invalid value for 'require' module flag "
1835 "(first value operand should be a string)"),
1836 Value->getOperand(0));
1837
1838 // Append it to the list of requirements, to check once all module flags are
1839 // scanned.
1840 Requirements.push_back(Value);
1841 break;
1842 }
1843
1844 case Module::Append:
1845 case Module::AppendUnique: {
1846 // These behavior types require the operand be an MDNode.
1847 Check(isa<MDNode>(Op->getOperand(2)),
1848 "invalid value for 'append'-type module flag "
1849 "(expected a metadata node)",
1850 Op->getOperand(2));
1851 break;
1852 }
1853 }
1854
1855 // Unless this is a "requires" flag, check the ID is unique.
1856 if (MFB != Module::Require) {
1857 bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
1858 Check(Inserted,
1859 "module flag identifiers must be unique (or of 'require' type)", ID);
1860 }
1861
1862 if (ID->getString() == "wchar_size") {
1864 = mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1865 Check(Value, "wchar_size metadata requires constant integer argument");
1866 }
1867
1868 if (ID->getString() == "Linker Options") {
1869 // If the llvm.linker.options named metadata exists, we assume that the
1870 // bitcode reader has upgraded the module flag. Otherwise the flag might
1871 // have been created by a client directly.
1872 Check(M.getNamedMetadata("llvm.linker.options"),
1873 "'Linker Options' named metadata no longer supported");
1874 }
1875
1876 if (ID->getString() == "SemanticInterposition") {
1878 mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1879 Check(Value,
1880 "SemanticInterposition metadata requires constant integer argument");
1881 }
1882
1883 if (ID->getString() == "CG Profile") {
1884 for (const MDOperand &MDO : cast<MDNode>(Op->getOperand(2))->operands())
1885 visitModuleFlagCGProfileEntry(MDO);
1886 }
1887}
1888
1889void Verifier::visitModuleFlagCGProfileEntry(const MDOperand &MDO) {
1890 auto CheckFunction = [&](const MDOperand &FuncMDO) {
1891 if (!FuncMDO)
1892 return;
1893 auto F = dyn_cast<ValueAsMetadata>(FuncMDO);
1894 Check(F && isa<Function>(F->getValue()->stripPointerCasts()),
1895 "expected a Function or null", FuncMDO);
1896 };
1897 auto Node = dyn_cast_or_null<MDNode>(MDO);
1898 Check(Node && Node->getNumOperands() == 3, "expected a MDNode triple", MDO);
1899 CheckFunction(Node->getOperand(0));
1900 CheckFunction(Node->getOperand(1));
1901 auto Count = dyn_cast_or_null<ConstantAsMetadata>(Node->getOperand(2));
1902 Check(Count && Count->getType()->isIntegerTy(),
1903 "expected an integer constant", Node->getOperand(2));
1904}
1905
1906void Verifier::verifyAttributeTypes(AttributeSet Attrs, const Value *V) {
1907 for (Attribute A : Attrs) {
1908
1909 if (A.isStringAttribute()) {
1910#define GET_ATTR_NAMES
1911#define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME)
1912#define ATTRIBUTE_STRBOOL(ENUM_NAME, DISPLAY_NAME) \
1913 if (A.getKindAsString() == #DISPLAY_NAME) { \
1914 auto V = A.getValueAsString(); \
1915 if (!(V.empty() || V == "true" || V == "false")) \
1916 CheckFailed("invalid value for '" #DISPLAY_NAME "' attribute: " + V + \
1917 ""); \
1918 }
1919
1920#include "llvm/IR/Attributes.inc"
1921 continue;
1922 }
1923
1924 if (A.isIntAttribute() != Attribute::isIntAttrKind(A.getKindAsEnum())) {
1925 CheckFailed("Attribute '" + A.getAsString() + "' should have an Argument",
1926 V);
1927 return;
1928 }
1929 }
1930}
1931
1932// VerifyParameterAttrs - Check the given attributes for an argument or return
1933// value of the specified type. The value V is printed in error messages.
1934void Verifier::verifyParameterAttrs(AttributeSet Attrs, Type *Ty,
1935 const Value *V) {
1936 if (!Attrs.hasAttributes())
1937 return;
1938
1939 verifyAttributeTypes(Attrs, V);
1940
1941 for (Attribute Attr : Attrs)
1942 Check(Attr.isStringAttribute() ||
1943 Attribute::canUseAsParamAttr(Attr.getKindAsEnum()),
1944 "Attribute '" + Attr.getAsString() + "' does not apply to parameters",
1945 V);
1946
1947 if (Attrs.hasAttribute(Attribute::ImmArg)) {
1948 Check(Attrs.getNumAttributes() == 1,
1949 "Attribute 'immarg' is incompatible with other attributes", V);
1950 }
1951
1952 // Check for mutually incompatible attributes. Only inreg is compatible with
1953 // sret.
1954 unsigned AttrCount = 0;
1955 AttrCount += Attrs.hasAttribute(Attribute::ByVal);
1956 AttrCount += Attrs.hasAttribute(Attribute::InAlloca);
1957 AttrCount += Attrs.hasAttribute(Attribute::Preallocated);
1958 AttrCount += Attrs.hasAttribute(Attribute::StructRet) ||
1959 Attrs.hasAttribute(Attribute::InReg);
1960 AttrCount += Attrs.hasAttribute(Attribute::Nest);
1961 AttrCount += Attrs.hasAttribute(Attribute::ByRef);
1962 Check(AttrCount <= 1,
1963 "Attributes 'byval', 'inalloca', 'preallocated', 'inreg', 'nest', "
1964 "'byref', and 'sret' are incompatible!",
1965 V);
1966
1967 Check(!(Attrs.hasAttribute(Attribute::InAlloca) &&
1968 Attrs.hasAttribute(Attribute::ReadOnly)),
1969 "Attributes "
1970 "'inalloca and readonly' are incompatible!",
1971 V);
1972
1973 Check(!(Attrs.hasAttribute(Attribute::StructRet) &&
1974 Attrs.hasAttribute(Attribute::Returned)),
1975 "Attributes "
1976 "'sret and returned' are incompatible!",
1977 V);
1978
1979 Check(!(Attrs.hasAttribute(Attribute::ZExt) &&
1980 Attrs.hasAttribute(Attribute::SExt)),
1981 "Attributes "
1982 "'zeroext and signext' are incompatible!",
1983 V);
1984
1985 Check(!(Attrs.hasAttribute(Attribute::ReadNone) &&
1986 Attrs.hasAttribute(Attribute::ReadOnly)),
1987 "Attributes "
1988 "'readnone and readonly' are incompatible!",
1989 V);
1990
1991 Check(!(Attrs.hasAttribute(Attribute::ReadNone) &&
1992 Attrs.hasAttribute(Attribute::WriteOnly)),
1993 "Attributes "
1994 "'readnone and writeonly' are incompatible!",
1995 V);
1996
1997 Check(!(Attrs.hasAttribute(Attribute::ReadOnly) &&
1998 Attrs.hasAttribute(Attribute::WriteOnly)),
1999 "Attributes "
2000 "'readonly and writeonly' are incompatible!",
2001 V);
2002
2003 Check(!(Attrs.hasAttribute(Attribute::NoInline) &&
2004 Attrs.hasAttribute(Attribute::AlwaysInline)),
2005 "Attributes "
2006 "'noinline and alwaysinline' are incompatible!",
2007 V);
2008
2009 Check(!(Attrs.hasAttribute(Attribute::Writable) &&
2010 Attrs.hasAttribute(Attribute::ReadNone)),
2011 "Attributes writable and readnone are incompatible!", V);
2012
2013 Check(!(Attrs.hasAttribute(Attribute::Writable) &&
2014 Attrs.hasAttribute(Attribute::ReadOnly)),
2015 "Attributes writable and readonly are incompatible!", V);
2016
2017 AttributeMask IncompatibleAttrs = AttributeFuncs::typeIncompatible(Ty);
2018 for (Attribute Attr : Attrs) {
2019 if (!Attr.isStringAttribute() &&
2020 IncompatibleAttrs.contains(Attr.getKindAsEnum())) {
2021 CheckFailed("Attribute '" + Attr.getAsString() +
2022 "' applied to incompatible type!", V);
2023 return;
2024 }
2025 }
2026
2027 if (isa<PointerType>(Ty)) {
2028 if (Attrs.hasAttribute(Attribute::ByVal)) {
2029 if (Attrs.hasAttribute(Attribute::Alignment)) {
2030 Align AttrAlign = Attrs.getAlignment().valueOrOne();
2031 Align MaxAlign(ParamMaxAlignment);
2032 Check(AttrAlign <= MaxAlign,
2033 "Attribute 'align' exceed the max size 2^14", V);
2034 }
2035 SmallPtrSet<Type *, 4> Visited;
2036 Check(Attrs.getByValType()->isSized(&Visited),
2037 "Attribute 'byval' does not support unsized types!", V);
2038 }
2039 if (Attrs.hasAttribute(Attribute::ByRef)) {
2040 SmallPtrSet<Type *, 4> Visited;
2041 Check(Attrs.getByRefType()->isSized(&Visited),
2042 "Attribute 'byref' does not support unsized types!", V);
2043 }
2044 if (Attrs.hasAttribute(Attribute::InAlloca)) {
2045 SmallPtrSet<Type *, 4> Visited;
2046 Check(Attrs.getInAllocaType()->isSized(&Visited),
2047 "Attribute 'inalloca' does not support unsized types!", V);
2048 }
2049 if (Attrs.hasAttribute(Attribute::Preallocated)) {
2050 SmallPtrSet<Type *, 4> Visited;
2051 Check(Attrs.getPreallocatedType()->isSized(&Visited),
2052 "Attribute 'preallocated' does not support unsized types!", V);
2053 }
2054 }
2055
2056 if (Attrs.hasAttribute(Attribute::NoFPClass)) {
2057 uint64_t Val = Attrs.getAttribute(Attribute::NoFPClass).getValueAsInt();
2058 Check(Val != 0, "Attribute 'nofpclass' must have at least one test bit set",
2059 V);
2060 Check((Val & ~static_cast<unsigned>(fcAllFlags)) == 0,
2061 "Invalid value for 'nofpclass' test mask", V);
2062 }
2063 if (Attrs.hasAttribute(Attribute::Range)) {
2064 auto CR = Attrs.getAttribute(Attribute::Range).getValueAsConstantRange();
2065 Check(Ty->isIntOrIntVectorTy(CR.getBitWidth()),
2066 "Range bit width must match type bit width!", V);
2067 }
2068}
2069
2070void Verifier::checkUnsignedBaseTenFuncAttr(AttributeList Attrs, StringRef Attr,
2071 const Value *V) {
2072 if (Attrs.hasFnAttr(Attr)) {
2073 StringRef S = Attrs.getFnAttr(Attr).getValueAsString();
2074 unsigned N;
2075 if (S.getAsInteger(10, N))
2076 CheckFailed("\"" + Attr + "\" takes an unsigned integer: " + S, V);
2077 }
2078}
2079
2080// Check parameter attributes against a function type.
2081// The value V is printed in error messages.
2082void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
2083 const Value *V, bool IsIntrinsic,
2084 bool IsInlineAsm) {
2085 if (Attrs.isEmpty())
2086 return;
2087
2088 if (AttributeListsVisited.insert(Attrs.getRawPointer()).second) {
2089 Check(Attrs.hasParentContext(Context),
2090 "Attribute list does not match Module context!", &Attrs, V);
2091 for (const auto &AttrSet : Attrs) {
2092 Check(!AttrSet.hasAttributes() || AttrSet.hasParentContext(Context),
2093 "Attribute set does not match Module context!", &AttrSet, V);
2094 for (const auto &A : AttrSet) {
2095 Check(A.hasParentContext(Context),
2096 "Attribute does not match Module context!", &A, V);
2097 }
2098 }
2099 }
2100
2101 bool SawNest = false;
2102 bool SawReturned = false;
2103 bool SawSRet = false;
2104 bool SawSwiftSelf = false;
2105 bool SawSwiftAsync = false;
2106 bool SawSwiftError = false;
2107
2108 // Verify return value attributes.
2109 AttributeSet RetAttrs = Attrs.getRetAttrs();
2110 for (Attribute RetAttr : RetAttrs)
2111 Check(RetAttr.isStringAttribute() ||
2112 Attribute::canUseAsRetAttr(RetAttr.getKindAsEnum()),
2113 "Attribute '" + RetAttr.getAsString() +
2114 "' does not apply to function return values",
2115 V);
2116
2117 unsigned MaxParameterWidth = 0;
2118 auto GetMaxParameterWidth = [&MaxParameterWidth](Type *Ty) {
2119 if (Ty->isVectorTy()) {
2120 if (auto *VT = dyn_cast<FixedVectorType>(Ty)) {
2121 unsigned Size = VT->getPrimitiveSizeInBits().getFixedValue();
2122 if (Size > MaxParameterWidth)
2123 MaxParameterWidth = Size;
2124 }
2125 }
2126 };
2127 GetMaxParameterWidth(FT->getReturnType());
2128 verifyParameterAttrs(RetAttrs, FT->getReturnType(), V);
2129
2130 // Verify parameter attributes.
2131 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
2132 Type *Ty = FT->getParamType(i);
2133 AttributeSet ArgAttrs = Attrs.getParamAttrs(i);
2134
2135 if (!IsIntrinsic) {
2136 Check(!ArgAttrs.hasAttribute(Attribute::ImmArg),
2137 "immarg attribute only applies to intrinsics", V);
2138 if (!IsInlineAsm)
2139 Check(!ArgAttrs.hasAttribute(Attribute::ElementType),
2140 "Attribute 'elementtype' can only be applied to intrinsics"
2141 " and inline asm.",
2142 V);
2143 }
2144
2145 verifyParameterAttrs(ArgAttrs, Ty, V);
2146 GetMaxParameterWidth(Ty);
2147
2148 if (ArgAttrs.hasAttribute(Attribute::Nest)) {
2149 Check(!SawNest, "More than one parameter has attribute nest!", V);
2150 SawNest = true;
2151 }
2152
2153 if (ArgAttrs.hasAttribute(Attribute::Returned)) {
2154 Check(!SawReturned, "More than one parameter has attribute returned!", V);
2155 Check(Ty->canLosslesslyBitCastTo(FT->getReturnType()),
2156 "Incompatible argument and return types for 'returned' attribute",
2157 V);
2158 SawReturned = true;
2159 }
2160
2161 if (ArgAttrs.hasAttribute(Attribute::StructRet)) {
2162 Check(!SawSRet, "Cannot have multiple 'sret' parameters!", V);
2163 Check(i == 0 || i == 1,
2164 "Attribute 'sret' is not on first or second parameter!", V);
2165 SawSRet = true;
2166 }
2167
2168 if (ArgAttrs.hasAttribute(Attribute::SwiftSelf)) {
2169 Check(!SawSwiftSelf, "Cannot have multiple 'swiftself' parameters!", V);
2170 SawSwiftSelf = true;
2171 }
2172
2173 if (ArgAttrs.hasAttribute(Attribute::SwiftAsync)) {
2174 Check(!SawSwiftAsync, "Cannot have multiple 'swiftasync' parameters!", V);
2175 SawSwiftAsync = true;
2176 }
2177
2178 if (ArgAttrs.hasAttribute(Attribute::SwiftError)) {
2179 Check(!SawSwiftError, "Cannot have multiple 'swifterror' parameters!", V);
2180 SawSwiftError = true;
2181 }
2182
2183 if (ArgAttrs.hasAttribute(Attribute::InAlloca)) {
2184 Check(i == FT->getNumParams() - 1,
2185 "inalloca isn't on the last parameter!", V);
2186 }
2187 }
2188
2189 if (!Attrs.hasFnAttrs())
2190 return;
2191
2192 verifyAttributeTypes(Attrs.getFnAttrs(), V);
2193 for (Attribute FnAttr : Attrs.getFnAttrs())
2194 Check(FnAttr.isStringAttribute() ||
2195 Attribute::canUseAsFnAttr(FnAttr.getKindAsEnum()),
2196 "Attribute '" + FnAttr.getAsString() +
2197 "' does not apply to functions!",
2198 V);
2199
2200 Check(!(Attrs.hasFnAttr(Attribute::NoInline) &&
2201 Attrs.hasFnAttr(Attribute::AlwaysInline)),
2202 "Attributes 'noinline and alwaysinline' are incompatible!", V);
2203
2204 if (Attrs.hasFnAttr(Attribute::OptimizeNone)) {
2205 Check(Attrs.hasFnAttr(Attribute::NoInline),
2206 "Attribute 'optnone' requires 'noinline'!", V);
2207
2208 Check(!Attrs.hasFnAttr(Attribute::OptimizeForSize),
2209 "Attributes 'optsize and optnone' are incompatible!", V);
2210
2211 Check(!Attrs.hasFnAttr(Attribute::MinSize),
2212 "Attributes 'minsize and optnone' are incompatible!", V);
2213
2214 Check(!Attrs.hasFnAttr(Attribute::OptimizeForDebugging),
2215 "Attributes 'optdebug and optnone' are incompatible!", V);
2216 }
2217
2218 if (Attrs.hasFnAttr(Attribute::OptimizeForDebugging)) {
2219 Check(!Attrs.hasFnAttr(Attribute::OptimizeForSize),
2220 "Attributes 'optsize and optdebug' are incompatible!", V);
2221
2222 Check(!Attrs.hasFnAttr(Attribute::MinSize),
2223 "Attributes 'minsize and optdebug' are incompatible!", V);
2224 }
2225
2226 Check(!Attrs.hasAttrSomewhere(Attribute::Writable) ||
2227 isModSet(Attrs.getMemoryEffects().getModRef(IRMemLocation::ArgMem)),
2228 "Attribute writable and memory without argmem: write are incompatible!",
2229 V);
2230
2231 if (Attrs.hasFnAttr("aarch64_pstate_sm_enabled")) {
2232 Check(!Attrs.hasFnAttr("aarch64_pstate_sm_compatible"),
2233 "Attributes 'aarch64_pstate_sm_enabled and "
2234 "aarch64_pstate_sm_compatible' are incompatible!",
2235 V);
2236 }
2237
2238 Check((Attrs.hasFnAttr("aarch64_new_za") + Attrs.hasFnAttr("aarch64_in_za") +
2239 Attrs.hasFnAttr("aarch64_inout_za") +
2240 Attrs.hasFnAttr("aarch64_out_za") +
2241 Attrs.hasFnAttr("aarch64_preserves_za")) <= 1,
2242 "Attributes 'aarch64_new_za', 'aarch64_in_za', 'aarch64_out_za', "
2243 "'aarch64_inout_za' and 'aarch64_preserves_za' are mutually exclusive",
2244 V);
2245
2246 Check(
2247 (Attrs.hasFnAttr("aarch64_new_zt0") + Attrs.hasFnAttr("aarch64_in_zt0") +
2248 Attrs.hasFnAttr("aarch64_inout_zt0") +
2249 Attrs.hasFnAttr("aarch64_out_zt0") +
2250 Attrs.hasFnAttr("aarch64_preserves_zt0")) <= 1,
2251 "Attributes 'aarch64_new_zt0', 'aarch64_in_zt0', 'aarch64_out_zt0', "
2252 "'aarch64_inout_zt0' and 'aarch64_preserves_zt0' are mutually exclusive",
2253 V);
2254
2255 if (Attrs.hasFnAttr(Attribute::JumpTable)) {
2256 const GlobalValue *GV = cast<GlobalValue>(V);
2258 "Attribute 'jumptable' requires 'unnamed_addr'", V);
2259 }
2260
2261 if (auto Args = Attrs.getFnAttrs().getAllocSizeArgs()) {
2262 auto CheckParam = [&](StringRef Name, unsigned ParamNo) {
2263 if (ParamNo >= FT->getNumParams()) {
2264 CheckFailed("'allocsize' " + Name + " argument is out of bounds", V);
2265 return false;
2266 }
2267
2268 if (!FT->getParamType(ParamNo)->isIntegerTy()) {
2269 CheckFailed("'allocsize' " + Name +
2270 " argument must refer to an integer parameter",
2271 V);
2272 return false;
2273 }
2274
2275 return true;
2276 };
2277
2278 if (!CheckParam("element size", Args->first))
2279 return;
2280
2281 if (Args->second && !CheckParam("number of elements", *Args->second))
2282 return;
2283 }
2284
2285 if (Attrs.hasFnAttr(Attribute::AllocKind)) {
2286 AllocFnKind K = Attrs.getAllocKind();
2289 if (!is_contained(
2291 Type))
2292 CheckFailed(
2293 "'allockind()' requires exactly one of alloc, realloc, and free");
2294 if ((Type == AllocFnKind::Free) &&
2297 CheckFailed("'allockind(\"free\")' doesn't allow uninitialized, zeroed, "
2298 "or aligned modifiers.");
2300 if ((K & ZeroedUninit) == ZeroedUninit)
2301 CheckFailed("'allockind()' can't be both zeroed and uninitialized");
2302 }
2303
2304 if (Attrs.hasFnAttr(Attribute::VScaleRange)) {
2305 unsigned VScaleMin = Attrs.getFnAttrs().getVScaleRangeMin();
2306 if (VScaleMin == 0)
2307 CheckFailed("'vscale_range' minimum must be greater than 0", V);
2308 else if (!isPowerOf2_32(VScaleMin))
2309 CheckFailed("'vscale_range' minimum must be power-of-two value", V);
2310 std::optional<unsigned> VScaleMax = Attrs.getFnAttrs().getVScaleRangeMax();
2311 if (VScaleMax && VScaleMin > VScaleMax)
2312 CheckFailed("'vscale_range' minimum cannot be greater than maximum", V);
2313 else if (VScaleMax && !isPowerOf2_32(*VScaleMax))
2314 CheckFailed("'vscale_range' maximum must be power-of-two value", V);
2315 }
2316
2317 if (Attrs.hasFnAttr("frame-pointer")) {
2318 StringRef FP = Attrs.getFnAttr("frame-pointer").getValueAsString();
2319 if (FP != "all" && FP != "non-leaf" && FP != "none")
2320 CheckFailed("invalid value for 'frame-pointer' attribute: " + FP, V);
2321 }
2322
2323 // Check EVEX512 feature.
2324 if (MaxParameterWidth >= 512 && Attrs.hasFnAttr("target-features") &&
2325 TT.isX86()) {
2326 StringRef TF = Attrs.getFnAttr("target-features").getValueAsString();
2327 Check(!TF.contains("+avx512f") || !TF.contains("-evex512"),
2328 "512-bit vector arguments require 'evex512' for AVX512", V);
2329 }
2330
2331 checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-prefix", V);
2332 checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-entry", V);
2333 checkUnsignedBaseTenFuncAttr(Attrs, "warn-stack-size", V);
2334
2335 if (auto A = Attrs.getFnAttr("sign-return-address"); A.isValid()) {
2336 StringRef S = A.getValueAsString();
2337 if (S != "none" && S != "all" && S != "non-leaf")
2338 CheckFailed("invalid value for 'sign-return-address' attribute: " + S, V);
2339 }
2340
2341 if (auto A = Attrs.getFnAttr("sign-return-address-key"); A.isValid()) {
2342 StringRef S = A.getValueAsString();
2343 if (S != "a_key" && S != "b_key")
2344 CheckFailed("invalid value for 'sign-return-address-key' attribute: " + S,
2345 V);
2346 }
2347
2348 if (auto A = Attrs.getFnAttr("branch-target-enforcement"); A.isValid()) {
2349 StringRef S = A.getValueAsString();
2350 if (S != "true" && S != "false")
2351 CheckFailed(
2352 "invalid value for 'branch-target-enforcement' attribute: " + S, V);
2353 }
2354
2355 if (auto A = Attrs.getFnAttr("vector-function-abi-variant"); A.isValid()) {
2356 StringRef S = A.getValueAsString();
2357 const std::optional<VFInfo> Info = VFABI::tryDemangleForVFABI(S, FT);
2358 if (!Info)
2359 CheckFailed("invalid name for a VFABI variant: " + S, V);
2360 }
2361}
2362
2363void Verifier::verifyFunctionMetadata(
2364 ArrayRef<std::pair<unsigned, MDNode *>> MDs) {
2365 for (const auto &Pair : MDs) {
2366 if (Pair.first == LLVMContext::MD_prof) {
2367 MDNode *MD = Pair.second;
2368 Check(MD->getNumOperands() >= 2,
2369 "!prof annotations should have no less than 2 operands", MD);
2370
2371 // Check first operand.
2372 Check(MD->getOperand(0) != nullptr, "first operand should not be null",
2373 MD);
2374 Check(isa<MDString>(MD->getOperand(0)),
2375 "expected string with name of the !prof annotation", MD);
2376 MDString *MDS = cast<MDString>(MD->getOperand(0));
2377 StringRef ProfName = MDS->getString();
2378 Check(ProfName.equals("function_entry_count") ||
2379 ProfName.equals("synthetic_function_entry_count"),
2380 "first operand should be 'function_entry_count'"
2381 " or 'synthetic_function_entry_count'",
2382 MD);
2383
2384 // Check second operand.
2385 Check(MD->getOperand(1) != nullptr, "second operand should not be null",
2386 MD);
2387 Check(isa<ConstantAsMetadata>(MD->getOperand(1)),
2388 "expected integer argument to function_entry_count", MD);
2389 } else if (Pair.first == LLVMContext::MD_kcfi_type) {
2390 MDNode *MD = Pair.second;
2391 Check(MD->getNumOperands() == 1,
2392 "!kcfi_type must have exactly one operand", MD);
2393 Check(MD->getOperand(0) != nullptr, "!kcfi_type operand must not be null",
2394 MD);
2395 Check(isa<ConstantAsMetadata>(MD->getOperand(0)),
2396 "expected a constant operand for !kcfi_type", MD);
2397 Constant *C = cast<ConstantAsMetadata>(MD->getOperand(0))->getValue();
2398 Check(isa<ConstantInt>(C) && isa<IntegerType>(C->getType()),
2399 "expected a constant integer operand for !kcfi_type", MD);
2400 Check(cast<ConstantInt>(C)->getBitWidth() == 32,
2401 "expected a 32-bit integer constant operand for !kcfi_type", MD);
2402 }
2403 }
2404}
2405
2406void Verifier::visitConstantExprsRecursively(const Constant *EntryC) {
2407 if (!ConstantExprVisited.insert(EntryC).second)
2408 return;
2409
2411 Stack.push_back(EntryC);
2412
2413 while (!Stack.empty()) {
2414 const Constant *C = Stack.pop_back_val();
2415
2416 // Check this constant expression.
2417 if (const auto *CE = dyn_cast<ConstantExpr>(C))
2418 visitConstantExpr(CE);
2419
2420 if (const auto *GV = dyn_cast<GlobalValue>(C)) {
2421 // Global Values get visited separately, but we do need to make sure
2422 // that the global value is in the correct module
2423 Check(GV->getParent() == &M, "Referencing global in another module!",
2424 EntryC, &M, GV, GV->getParent());
2425 continue;
2426 }
2427
2428 // Visit all sub-expressions.
2429 for (const Use &U : C->operands()) {
2430 const auto *OpC = dyn_cast<Constant>(U);
2431 if (!OpC)
2432 continue;
2433 if (!ConstantExprVisited.insert(OpC).second)
2434 continue;
2435 Stack.push_back(OpC);
2436 }
2437 }
2438}
2439
2440void Verifier::visitConstantExpr(const ConstantExpr *CE) {
2441 if (CE->getOpcode() == Instruction::BitCast)
2442 Check(CastInst::castIsValid(Instruction::BitCast, CE->getOperand(0),
2443 CE->getType()),
2444 "Invalid bitcast", CE);
2445}
2446
2447bool Verifier::verifyAttributeCount(AttributeList Attrs, unsigned Params) {
2448 // There shouldn't be more attribute sets than there are parameters plus the
2449 // function and return value.
2450 return Attrs.getNumAttrSets() <= Params + 2;
2451}
2452
2453void Verifier::verifyInlineAsmCall(const CallBase &Call) {
2454 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
2455 unsigned ArgNo = 0;
2456 unsigned LabelNo = 0;
2457 for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) {
2458 if (CI.Type == InlineAsm::isLabel) {
2459 ++LabelNo;
2460 continue;
2461 }
2462
2463 // Only deal with constraints that correspond to call arguments.
2464 if (!CI.hasArg())
2465 continue;
2466
2467 if (CI.isIndirect) {
2468 const Value *Arg = Call.getArgOperand(ArgNo);
2469 Check(Arg->getType()->isPointerTy(),
2470 "Operand for indirect constraint must have pointer type", &Call);
2471
2472 Check(Call.getParamElementType(ArgNo),
2473 "Operand for indirect constraint must have elementtype attribute",
2474 &Call);
2475 } else {
2476 Check(!Call.paramHasAttr(ArgNo, Attribute::ElementType),
2477 "Elementtype attribute can only be applied for indirect "
2478 "constraints",
2479 &Call);
2480 }
2481
2482 ArgNo++;
2483 }
2484
2485 if (auto *CallBr = dyn_cast<CallBrInst>(&Call)) {
2486 Check(LabelNo == CallBr->getNumIndirectDests(),
2487 "Number of label constraints does not match number of callbr dests",
2488 &Call);
2489 } else {
2490 Check(LabelNo == 0, "Label constraints can only be used with callbr",
2491 &Call);
2492 }
2493}
2494
2495/// Verify that statepoint intrinsic is well formed.
2496void Verifier::verifyStatepoint(const CallBase &Call) {
2497 assert(Call.getCalledFunction() &&
2498 Call.getCalledFunction()->getIntrinsicID() ==
2499 Intrinsic::experimental_gc_statepoint);
2500
2501 Check(!Call.doesNotAccessMemory() && !Call.onlyReadsMemory() &&
2502 !Call.onlyAccessesArgMemory(),
2503 "gc.statepoint must read and write all memory to preserve "
2504 "reordering restrictions required by safepoint semantics",
2505 Call);
2506
2507 const int64_t NumPatchBytes =
2508 cast<ConstantInt>(Call.getArgOperand(1))->getSExtValue();
2509 assert(isInt<32>(NumPatchBytes) && "NumPatchBytesV is an i32!");
2510 Check(NumPatchBytes >= 0,
2511 "gc.statepoint number of patchable bytes must be "
2512 "positive",
2513 Call);
2514
2515 Type *TargetElemType = Call.getParamElementType(2);
2516 Check(TargetElemType,
2517 "gc.statepoint callee argument must have elementtype attribute", Call);
2518 FunctionType *TargetFuncType = dyn_cast<FunctionType>(TargetElemType);
2519 Check(TargetFuncType,
2520 "gc.statepoint callee elementtype must be function type", Call);
2521
2522 const int NumCallArgs = cast<ConstantInt>(Call.getArgOperand(3))->getZExtValue();
2523 Check(NumCallArgs >= 0,
2524 "gc.statepoint number of arguments to underlying call "
2525 "must be positive",
2526 Call);
2527 const int NumParams = (int)TargetFuncType->getNumParams();
2528 if (TargetFuncType->isVarArg()) {
2529 Check(NumCallArgs >= NumParams,
2530 "gc.statepoint mismatch in number of vararg call args", Call);
2531
2532 // TODO: Remove this limitation
2533 Check(TargetFuncType->getReturnType()->isVoidTy(),
2534 "gc.statepoint doesn't support wrapping non-void "
2535 "vararg functions yet",
2536 Call);
2537 } else
2538 Check(NumCallArgs == NumParams,
2539 "gc.statepoint mismatch in number of call args", Call);
2540
2541 const uint64_t Flags
2542 = cast<ConstantInt>(Call.getArgOperand(4))->getZExtValue();
2543 Check((Flags & ~(uint64_t)StatepointFlags::MaskAll) == 0,
2544 "unknown flag used in gc.statepoint flags argument", Call);
2545
2546 // Verify that the types of the call parameter arguments match
2547 // the type of the wrapped callee.
2548 AttributeList Attrs = Call.getAttributes();
2549 for (int i = 0; i < NumParams; i++) {
2550 Type *ParamType = TargetFuncType->getParamType(i);
2551 Type *ArgType = Call.getArgOperand(5 + i)->getType();
2552 Check(ArgType == ParamType,
2553 "gc.statepoint call argument does not match wrapped "
2554 "function type",
2555 Call);
2556
2557 if (TargetFuncType->isVarArg()) {
2558 AttributeSet ArgAttrs = Attrs.getParamAttrs(5 + i);
2559 Check(!ArgAttrs.hasAttribute(Attribute::StructRet),
2560 "Attribute 'sret' cannot be used for vararg call arguments!", Call);
2561 }
2562 }
2563
2564 const int EndCallArgsInx = 4 + NumCallArgs;
2565
2566 const Value *NumTransitionArgsV = Call.getArgOperand(EndCallArgsInx + 1);
2567 Check(isa<ConstantInt>(NumTransitionArgsV),
2568 "gc.statepoint number of transition arguments "
2569 "must be constant integer",
2570 Call);
2571 const int NumTransitionArgs =
2572 cast<ConstantInt>(NumTransitionArgsV)->getZExtValue();
2573 Check(NumTransitionArgs == 0,
2574 "gc.statepoint w/inline transition bundle is deprecated", Call);
2575 const int EndTransitionArgsInx = EndCallArgsInx + 1 + NumTransitionArgs;
2576
2577 const Value *NumDeoptArgsV = Call.getArgOperand(EndTransitionArgsInx + 1);
2578 Check(isa<ConstantInt>(NumDeoptArgsV),
2579 "gc.statepoint number of deoptimization arguments "
2580 "must be constant integer",
2581 Call);
2582 const int NumDeoptArgs = cast<ConstantInt>(NumDeoptArgsV)->getZExtValue();
2583 Check(NumDeoptArgs == 0,
2584 "gc.statepoint w/inline deopt operands is deprecated", Call);
2585
2586 const int ExpectedNumArgs = 7 + NumCallArgs;
2587 Check(ExpectedNumArgs == (int)Call.arg_size(),
2588 "gc.statepoint too many arguments", Call);
2589
2590 // Check that the only uses of this gc.statepoint are gc.result or
2591 // gc.relocate calls which are tied to this statepoint and thus part
2592 // of the same statepoint sequence
2593 for (const User *U : Call.users()) {
2594 const CallInst *UserCall = dyn_cast<const CallInst>(U);
2595 Check(UserCall, "illegal use of statepoint token", Call, U);
2596 if (!UserCall)
2597 continue;
2598 Check(isa<GCRelocateInst>(UserCall) || isa<GCResultInst>(UserCall),
2599 "gc.result or gc.relocate are the only value uses "
2600 "of a gc.statepoint",
2601 Call, U);
2602 if (isa<GCResultInst>(UserCall)) {
2603 Check(UserCall->getArgOperand(0) == &Call,
2604 "gc.result connected to wrong gc.statepoint", Call, UserCall);
2605 } else if (isa<GCRelocateInst>(Call)) {
2606 Check(UserCall->getArgOperand(0) == &Call,
2607 "gc.relocate connected to wrong gc.statepoint", Call, UserCall);
2608 }
2609 }
2610
2611 // Note: It is legal for a single derived pointer to be listed multiple
2612 // times. It's non-optimal, but it is legal. It can also happen after
2613 // insertion if we strip a bitcast away.
2614 // Note: It is really tempting to check that each base is relocated and
2615 // that a derived pointer is never reused as a base pointer. This turns
2616 // out to be problematic since optimizations run after safepoint insertion
2617 // can recognize equality properties that the insertion logic doesn't know
2618 // about. See example statepoint.ll in the verifier subdirectory
2619}
2620
2621void Verifier::verifyFrameRecoverIndices() {
2622 for (auto &Counts : FrameEscapeInfo) {
2623 Function *F = Counts.first;
2624 unsigned EscapedObjectCount = Counts.second.first;
2625 unsigned MaxRecoveredIndex = Counts.second.second;
2626 Check(MaxRecoveredIndex <= EscapedObjectCount,
2627 "all indices passed to llvm.localrecover must be less than the "
2628 "number of arguments passed to llvm.localescape in the parent "
2629 "function",
2630 F);
2631 }
2632}
2633
2634static Instruction *getSuccPad(Instruction *Terminator) {
2635 BasicBlock *UnwindDest;
2636 if (auto *II = dyn_cast<InvokeInst>(Terminator))
2637 UnwindDest = II->getUnwindDest();
2638 else if (auto *CSI = dyn_cast<CatchSwitchInst>(Terminator))
2639 UnwindDest = CSI->getUnwindDest();
2640 else
2641 UnwindDest = cast<CleanupReturnInst>(Terminator)->getUnwindDest();
2642 return UnwindDest->getFirstNonPHI();
2643}
2644
2645void Verifier::verifySiblingFuncletUnwinds() {
2648 for (const auto &Pair : SiblingFuncletInfo) {
2649 Instruction *PredPad = Pair.first;
2650 if (Visited.count(PredPad))
2651 continue;
2652 Active.insert(PredPad);
2653 Instruction *Terminator = Pair.second;
2654 do {
2655 Instruction *SuccPad = getSuccPad(Terminator);
2656 if (Active.count(SuccPad)) {
2657 // Found a cycle; report error
2658 Instruction *CyclePad = SuccPad;
2660 do {
2661 CycleNodes.push_back(CyclePad);
2662 Instruction *CycleTerminator = SiblingFuncletInfo[CyclePad];
2663 if (CycleTerminator != CyclePad)
2664 CycleNodes.push_back(CycleTerminator);
2665 CyclePad = getSuccPad(CycleTerminator);
2666 } while (CyclePad != SuccPad);
2667 Check(false, "EH pads can't handle each other's exceptions",
2668 ArrayRef<Instruction *>(CycleNodes));
2669 }
2670 // Don't re-walk a node we've already checked
2671 if (!Visited.insert(SuccPad).second)
2672 break;
2673 // Walk to this successor if it has a map entry.
2674 PredPad = SuccPad;
2675 auto TermI = SiblingFuncletInfo.find(PredPad);
2676 if (TermI == SiblingFuncletInfo.end())
2677 break;
2678 Terminator = TermI->second;
2679 Active.insert(PredPad);
2680 } while (true);
2681 // Each node only has one successor, so we've walked all the active
2682 // nodes' successors.
2683 Active.clear();
2684 }
2685}
2686
2687// visitFunction - Verify that a function is ok.
2688//
2689void Verifier::visitFunction(const Function &F) {
2690 visitGlobalValue(F);
2691
2692 // Check function arguments.
2693 FunctionType *FT = F.getFunctionType();
2694 unsigned NumArgs = F.arg_size();
2695
2696 Check(&Context == &F.getContext(),
2697 "Function context does not match Module context!", &F);
2698
2699 Check(!F.hasCommonLinkage(), "Functions may not have common linkage", &F);
2700 Check(FT->getNumParams() == NumArgs,
2701 "# formal arguments must match # of arguments for function type!", &F,
2702 FT);
2703 Check(F.getReturnType()->isFirstClassType() ||
2704 F.getReturnType()->isVoidTy() || F.getReturnType()->isStructTy(),
2705 "Functions cannot return aggregate values!", &F);
2706
2707 Check(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
2708 "Invalid struct return type!", &F);
2709
2710 AttributeList Attrs = F.getAttributes();
2711
2712 Check(verifyAttributeCount(Attrs, FT->getNumParams()),
2713 "Attribute after last parameter!", &F);
2714
2715 CheckDI(F.IsNewDbgInfoFormat == F.getParent()->IsNewDbgInfoFormat,
2716 "Function debug format should match parent module", &F,
2717 F.IsNewDbgInfoFormat, F.getParent(),
2718 F.getParent()->IsNewDbgInfoFormat);
2719
2720 bool IsIntrinsic = F.isIntrinsic();
2721
2722 // Check function attributes.
2723 verifyFunctionAttrs(FT, Attrs, &F, IsIntrinsic, /* IsInlineAsm */ false);
2724
2725 // On function declarations/definitions, we do not support the builtin
2726 // attribute. We do not check this in VerifyFunctionAttrs since that is
2727 // checking for Attributes that can/can not ever be on functions.
2728 Check(!Attrs.hasFnAttr(Attribute::Builtin),
2729 "Attribute 'builtin' can only be applied to a callsite.", &F);
2730
2731 Check(!Attrs.hasAttrSomewhere(Attribute::ElementType),
2732 "Attribute 'elementtype' can only be applied to a callsite.", &F);
2733
2734 // Check that this function meets the restrictions on this calling convention.
2735 // Sometimes varargs is used for perfectly forwarding thunks, so some of these
2736 // restrictions can be lifted.
2737 switch (F.getCallingConv()) {
2738 default:
2739 case CallingConv::C:
2740 break;
2741 case CallingConv::X86_INTR: {
2742 Check(F.arg_empty() || Attrs.hasParamAttr(0, Attribute::ByVal),
2743 "Calling convention parameter requires byval", &F);
2744 break;
2745 }
2750 Check(F.getReturnType()->isVoidTy(),
2751 "Calling convention requires void return type", &F);
2752 [[fallthrough]];
2758 Check(!F.hasStructRetAttr(), "Calling convention does not allow sret", &F);
2759 if (F.getCallingConv() != CallingConv::SPIR_KERNEL) {
2760 const unsigned StackAS = DL.getAllocaAddrSpace();
2761 unsigned i = 0;
2762 for (const Argument &Arg : F.args()) {
2763 Check(!Attrs.hasParamAttr(i, Attribute::ByVal),
2764 "Calling convention disallows byval", &F);
2765 Check(!Attrs.hasParamAttr(i, Attribute::Preallocated),
2766 "Calling convention disallows preallocated", &F);
2767 Check(!Attrs.hasParamAttr(i, Attribute::InAlloca),
2768 "Calling convention disallows inalloca", &F);
2769
2770 if (Attrs.hasParamAttr(i, Attribute::ByRef)) {
2771 // FIXME: Should also disallow LDS and GDS, but we don't have the enum
2772 // value here.
2773 Check(Arg.getType()->getPointerAddressSpace() != StackAS,
2774 "Calling convention disallows stack byref", &F);
2775 }
2776
2777 ++i;
2778 }
2779 }
2780
2781 [[fallthrough]];
2782 case CallingConv::Fast:
2783 case CallingConv::Cold:
2787 Check(!F.isVarArg(),
2788 "Calling convention does not support varargs or "
2789 "perfect forwarding!",
2790 &F);
2791 break;
2792 }
2793
2794 // Check that the argument values match the function type for this function...
2795 unsigned i = 0;
2796 for (const Argument &Arg : F.args()) {
2797 Check(Arg.getType() == FT->getParamType(i),
2798 "Argument value does not match function argument type!", &Arg,
2799 FT->getParamType(i));
2800 Check(Arg.getType()->isFirstClassType(),
2801 "Function arguments must have first-class types!", &Arg);
2802 if (!IsIntrinsic) {
2803 Check(!Arg.getType()->isMetadataTy(),
2804 "Function takes metadata but isn't an intrinsic", &Arg, &F);
2805 Check(!Arg.getType()->isTokenTy(),
2806 "Function takes token but isn't an intrinsic", &Arg, &F);
2807 Check(!Arg.getType()->isX86_AMXTy(),
2808 "Function takes x86_amx but isn't an intrinsic", &Arg, &F);
2809 }
2810
2811 // Check that swifterror argument is only used by loads and stores.
2812 if (Attrs.hasParamAttr(i, Attribute::SwiftError)) {
2813 verifySwiftErrorValue(&Arg);
2814 }
2815 ++i;
2816 }
2817
2818 if (!IsIntrinsic) {
2819 Check(!F.getReturnType()->isTokenTy(),
2820 "Function returns a token but isn't an intrinsic", &F);
2821 Check(!F.getReturnType()->isX86_AMXTy(),
2822 "Function returns a x86_amx but isn't an intrinsic", &F);
2823 }
2824
2825 // Get the function metadata attachments.
2827 F.getAllMetadata(MDs);
2828 assert(F.hasMetadata() != MDs.empty() && "Bit out-of-sync");
2829 verifyFunctionMetadata(MDs);
2830
2831 // Check validity of the personality function
2832 if (F.hasPersonalityFn()) {
2833 auto *Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
2834 if (Per)
2835 Check(Per->getParent() == F.getParent(),
2836 "Referencing personality function in another module!", &F,
2837 F.getParent(), Per, Per->getParent());
2838 }
2839
2840 // EH funclet coloring can be expensive, recompute on-demand
2841 BlockEHFuncletColors.clear();
2842
2843 if (F.isMaterializable()) {
2844 // Function has a body somewhere we can't see.
2845 Check(MDs.empty(), "unmaterialized function cannot have metadata", &F,
2846 MDs.empty() ? nullptr : MDs.front().second);
2847 } else if (F.isDeclaration()) {
2848 for (const auto &I : MDs) {
2849 // This is used for call site debug information.
2850 CheckDI(I.first != LLVMContext::MD_dbg ||
2851 !cast<DISubprogram>(I.second)->isDistinct(),
2852 "function declaration may only have a unique !dbg attachment",
2853 &F);
2854 Check(I.first != LLVMContext::MD_prof,
2855 "function declaration may not have a !prof attachment", &F);
2856
2857 // Verify the metadata itself.
2858 visitMDNode(*I.second, AreDebugLocsAllowed::Yes);
2859 }
2860 Check(!F.hasPersonalityFn(),
2861 "Function declaration shouldn't have a personality routine", &F);
2862 } else {
2863 // Verify that this function (which has a body) is not named "llvm.*". It
2864 // is not legal to define intrinsics.
2865 Check(!IsIntrinsic, "llvm intrinsics cannot be defined!", &F);
2866
2867 // Check the entry node
2868 const BasicBlock *Entry = &F.getEntryBlock();
2869 Check(pred_empty(Entry),
2870 "Entry block to function must not have predecessors!", Entry);
2871
2872 // The address of the entry block cannot be taken, unless it is dead.
2873 if (Entry->hasAddressTaken()) {
2874 Check(!BlockAddress::lookup(Entry)->isConstantUsed(),
2875 "blockaddress may not be used with the entry block!", Entry);
2876 }
2877
2878 unsigned NumDebugAttachments = 0, NumProfAttachments = 0,
2879 NumKCFIAttachments = 0;
2880 // Visit metadata attachments.
2881 for (const auto &I : MDs) {
2882 // Verify that the attachment is legal.
2883 auto AllowLocs = AreDebugLocsAllowed::No;
2884 switch (I.first) {
2885 default:
2886 break;
2887 case LLVMContext::MD_dbg: {
2888 ++NumDebugAttachments;
2889 CheckDI(NumDebugAttachments == 1,
2890 "function must have a single !dbg attachment", &F, I.second);
2891 CheckDI(isa<DISubprogram>(I.second),
2892 "function !dbg attachment must be a subprogram", &F, I.second);
2893 CheckDI(cast<DISubprogram>(I.second)->isDistinct(),
2894 "function definition may only have a distinct !dbg attachment",
2895 &F);
2896
2897 auto *SP = cast<DISubprogram>(I.second);
2898 const Function *&AttachedTo = DISubprogramAttachments[SP];
2899 CheckDI(!AttachedTo || AttachedTo == &F,
2900 "DISubprogram attached to more than one function", SP, &F);
2901 AttachedTo = &F;
2902 AllowLocs = AreDebugLocsAllowed::Yes;
2903 break;
2904 }
2905 case LLVMContext::MD_prof:
2906 ++NumProfAttachments;
2907 Check(NumProfAttachments == 1,
2908 "function must have a single !prof attachment", &F, I.second);
2909 break;
2910 case LLVMContext::MD_kcfi_type:
2911 ++NumKCFIAttachments;
2912 Check(NumKCFIAttachments == 1,
2913 "function must have a single !kcfi_type attachment", &F,
2914 I.second);
2915 break;
2916 }
2917
2918 // Verify the metadata itself.
2919 visitMDNode(*I.second, AllowLocs);
2920 }
2921 }
2922
2923 // If this function is actually an intrinsic, verify that it is only used in
2924 // direct call/invokes, never having its "address taken".
2925 // Only do this if the module is materialized, otherwise we don't have all the
2926 // uses.
2927 if (F.isIntrinsic() && F.getParent()->isMaterialized()) {
2928 const User *U;
2929 if (F.hasAddressTaken(&U, false, true, false,
2930 /*IgnoreARCAttachedCall=*/true))
2931 Check(false, "Invalid user of intrinsic instruction!", U);
2932 }
2933
2934 // Check intrinsics' signatures.
2935 switch (F.getIntrinsicID()) {
2936 case Intrinsic::experimental_gc_get_pointer_base: {
2937 FunctionType *FT = F.getFunctionType();
2938 Check(FT->getNumParams() == 1, "wrong number of parameters", F);
2939 Check(isa<PointerType>(F.getReturnType()),
2940 "gc.get.pointer.base must return a pointer", F);
2941 Check(FT->getParamType(0) == F.getReturnType(),
2942 "gc.get.pointer.base operand and result must be of the same type", F);
2943 break;
2944 }
2945 case Intrinsic::experimental_gc_get_pointer_offset: {
2946 FunctionType *FT = F.getFunctionType();
2947 Check(FT->getNumParams() == 1, "wrong number of parameters", F);
2948 Check(isa<PointerType>(FT->getParamType(0)),
2949 "gc.get.pointer.offset operand must be a pointer", F);
2950 Check(F.getReturnType()->isIntegerTy(),
2951 "gc.get.pointer.offset must return integer", F);
2952 break;
2953 }
2954 }
2955
2956 auto *N = F.getSubprogram();
2957 HasDebugInfo = (N != nullptr);
2958 if (!HasDebugInfo)
2959 return;
2960
2961 // Check that all !dbg attachments lead to back to N.
2962 //
2963 // FIXME: Check this incrementally while visiting !dbg attachments.
2964 // FIXME: Only check when N is the canonical subprogram for F.
2966 auto VisitDebugLoc = [&](const Instruction &I, const MDNode *Node) {
2967 // Be careful about using DILocation here since we might be dealing with
2968 // broken code (this is the Verifier after all).
2969 const DILocation *DL = dyn_cast_or_null<DILocation>(Node);
2970 if (!DL)
2971 return;
2972 if (!Seen.insert(DL).second)
2973 return;
2974
2975 Metadata *Parent = DL->getRawScope();
2976 CheckDI(Parent && isa<DILocalScope>(Parent),
2977 "DILocation's scope must be a DILocalScope", N, &F, &I, DL, Parent);
2978
2979 DILocalScope *Scope = DL->getInlinedAtScope();
2980 Check(Scope, "Failed to find DILocalScope", DL);
2981
2982 if (!Seen.insert(Scope).second)
2983 return;
2984
2985 DISubprogram *SP = Scope->getSubprogram();
2986
2987 // Scope and SP could be the same MDNode and we don't want to skip
2988 // validation in that case
2989 if (SP && ((Scope != SP) && !Seen.insert(SP).second))
2990 return;
2991
2992 CheckDI(SP->describes(&F),
2993 "!dbg attachment points at wrong subprogram for function", N, &F,
2994 &I, DL, Scope, SP);
2995 };
2996 for (auto &BB : F)
2997 for (auto &I : BB) {
2998 VisitDebugLoc(I, I.getDebugLoc().getAsMDNode());
2999 // The llvm.loop annotations also contain two DILocations.
3000 if (auto MD = I.getMetadata(LLVMContext::MD_loop))
3001 for (unsigned i = 1; i < MD->getNumOperands(); ++i)
3002 VisitDebugLoc(I, dyn_cast_or_null<MDNode>(MD->getOperand(i)));
3003 if (BrokenDebugInfo)
3004 return;
3005 }
3006}
3007
3008// verifyBasicBlock - Verify that a basic block is well formed...
3009//
3010void Verifier::visitBasicBlock(BasicBlock &BB) {
3011 InstsInThisBlock.clear();
3012 ConvergenceVerifyHelper.visit(BB);
3013
3014 // Ensure that basic blocks have terminators!
3015 Check(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
3016
3017 // Check constraints that this basic block imposes on all of the PHI nodes in
3018 // it.
3019 if (isa<PHINode>(BB.front())) {
3022 llvm::sort(Preds);
3023 for (const PHINode &PN : BB.phis()) {
3024 Check(PN.getNumIncomingValues() == Preds.size(),
3025 "PHINode should have one entry for each predecessor of its "
3026 "parent basic block!",
3027 &PN);
3028
3029 // Get and sort all incoming values in the PHI node...
3030 Values.clear();
3031 Values.reserve(PN.getNumIncomingValues());
3032 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
3033 Values.push_back(
3034 std::make_pair(PN.getIncomingBlock(i), PN.getIncomingValue(i)));
3035 llvm::sort(Values);
3036
3037 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
3038 // Check to make sure that if there is more than one entry for a
3039 // particular basic block in this PHI node, that the incoming values are
3040 // all identical.
3041 //
3042 Check(i == 0 || Values[i].first != Values[i - 1].first ||
3043 Values[i].second == Values[i - 1].second,
3044 "PHI node has multiple entries for the same basic block with "
3045 "different incoming values!",
3046 &PN, Values[i].first, Values[i].second, Values[i - 1].second);
3047
3048 // Check to make sure that the predecessors and PHI node entries are
3049 // matched up.
3050 Check(Values[i].first == Preds[i],
3051 "PHI node entries do not match predecessors!", &PN,
3052 Values[i].first, Preds[i]);
3053 }
3054 }
3055 }
3056
3057 // Check that all instructions have their parent pointers set up correctly.
3058 for (auto &I : BB)
3059 {
3060 Check(I.getParent() == &BB, "Instruction has bogus parent pointer!");
3061 }
3062
3063 CheckDI(BB.IsNewDbgInfoFormat == BB.getParent()->IsNewDbgInfoFormat,
3064 "BB debug format should match parent function", &BB,
3065 BB.IsNewDbgInfoFormat, BB.getParent(),
3066 BB.getParent()->IsNewDbgInfoFormat);
3067
3068 // Confirm that no issues arise from the debug program.
3069 if (BB.IsNewDbgInfoFormat)
3070 CheckDI(!BB.getTrailingDbgRecords(), "Basic Block has trailing DbgRecords!",
3071 &BB);
3072}
3073
3074void Verifier::visitTerminator(Instruction &I) {
3075 // Ensure that terminators only exist at the end of the basic block.
3076 Check(&I == I.getParent()->getTerminator(),
3077 "Terminator found in the middle of a basic block!", I.getParent());
3079}
3080
3081void Verifier::visitBranchInst(BranchInst &BI) {
3082 if (BI.isConditional()) {
3084 "Branch condition is not 'i1' type!", &BI, BI.getCondition());
3085 }
3086 visitTerminator(BI);
3087}
3088
3089void Verifier::visitReturnInst(ReturnInst &RI) {
3090 Function *F = RI.getParent()->getParent();
3091 unsigned N = RI.getNumOperands();
3092 if (F->getReturnType()->isVoidTy())
3093 Check(N == 0,
3094 "Found return instr that returns non-void in Function of void "
3095 "return type!",
3096 &RI, F->getReturnType());
3097 else
3098 Check(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(),
3099 "Function return type does not match operand "
3100 "type of return inst!",
3101 &RI, F->getReturnType());
3102
3103 // Check to make sure that the return value has necessary properties for
3104 // terminators...
3105 visitTerminator(RI);
3106}
3107
3108void Verifier::visitSwitchInst(SwitchInst &SI) {
3109 Check(SI.getType()->isVoidTy(), "Switch must have void result type!", &SI);
3110 // Check to make sure that all of the constants in the switch instruction
3111 // have the same type as the switched-on value.
3112 Type *SwitchTy = SI.getCondition()->getType();
3114 for (auto &Case : SI.cases()) {
3115 Check(isa<ConstantInt>(SI.getOperand(Case.getCaseIndex() * 2 + 2)),
3116 "Case value is not a constant integer.", &SI);
3117 Check(Case.getCaseValue()->getType() == SwitchTy,
3118 "Switch constants must all be same type as switch value!", &SI);
3119 Check(Constants.insert(Case.getCaseValue()).second,
3120 "Duplicate integer as switch case", &SI, Case.getCaseValue());
3121 }
3122
3123 visitTerminator(SI);
3124}
3125
3126void Verifier::visitIndirectBrInst(IndirectBrInst &BI) {
3128 "Indirectbr operand must have pointer type!", &BI);
3129 for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i)
3131 "Indirectbr destinations must all have pointer type!", &BI);
3132
3133 visitTerminator(BI);
3134}
3135
3136void Verifier::visitCallBrInst(CallBrInst &CBI) {
3137 Check(CBI.isInlineAsm(), "Callbr is currently only used for asm-goto!", &CBI);
3138 const InlineAsm *IA = cast<InlineAsm>(CBI.getCalledOperand());
3139 Check(!IA->canThrow(), "Unwinding from Callbr is not allowed");
3140
3141 verifyInlineAsmCall(CBI);
3142 visitTerminator(CBI);
3143}
3144
3145void Verifier::visitSelectInst(SelectInst &SI) {
3146 Check(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1),
3147 SI.getOperand(2)),
3148 "Invalid operands for select instruction!", &SI);
3149
3150 Check(SI.getTrueValue()->getType() == SI.getType(),
3151 "Select values must have same type as select instruction!", &SI);
3152 visitInstruction(SI);
3153}
3154
3155/// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
3156/// a pass, if any exist, it's an error.
3157///
3158void Verifier::visitUserOp1(Instruction &I) {
3159 Check(false, "User-defined operators should not live outside of a pass!", &I);
3160}
3161
3162void Verifier::visitTruncInst(TruncInst &I) {
3163 // Get the source and destination types
3164 Type *SrcTy = I.getOperand(0)->getType();
3165 Type *DestTy = I.getType();
3166
3167 // Get the size of the types in bits, we'll need this later
3168 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3169 unsigned DestBitSize = DestTy->getScalarSizeInBits();
3170
3171 Check(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I);
3172 Check(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I);
3173 Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
3174 "trunc source and destination must both be a vector or neither", &I);
3175 Check(SrcBitSize > DestBitSize, "DestTy too big for Trunc", &I);
3176
3178}
3179
3180void Verifier::visitZExtInst(ZExtInst &I) {
3181 // Get the source and destination types
3182 Type *SrcTy = I.getOperand(0)->getType();
3183 Type *DestTy = I.getType();
3184
3185 // Get the size of the types in bits, we'll need this later
3186 Check(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I);
3187 Check(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I);
3188 Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
3189 "zext source and destination must both be a vector or neither", &I);
3190 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3191 unsigned DestBitSize = DestTy->getScalarSizeInBits();
3192
3193 Check(SrcBitSize < DestBitSize, "Type too small for ZExt", &I);
3194
3196}
3197
3198void Verifier::visitSExtInst(SExtInst &I) {
3199 // Get the source and destination types
3200 Type *SrcTy = I.getOperand(0)->getType();
3201 Type *DestTy = I.getType();
3202
3203 // Get the size of the types in bits, we'll need this later
3204 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3205 unsigned DestBitSize = DestTy->getScalarSizeInBits();
3206
3207 Check(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I);
3208 Check(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I);
3209 Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
3210 "sext source and destination must both be a vector or neither", &I);
3211 Check(SrcBitSize < DestBitSize, "Type too small for SExt", &I);
3212
3214}
3215
3216void Verifier::visitFPTruncInst(FPTruncInst &I) {
3217 // Get the source and destination types
3218 Type *SrcTy = I.getOperand(0)->getType();
3219 Type *DestTy = I.getType();
3220 // Get the size of the types in bits, we'll need this later
3221 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3222 unsigned DestBitSize = DestTy->getScalarSizeInBits();
3223
3224 Check(SrcTy->isFPOrFPVectorTy(), "FPTrunc only operates on FP", &I);
3225 Check(DestTy->isFPOrFPVectorTy(), "FPTrunc only produces an FP", &I);
3226 Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
3227 "fptrunc source and destination must both be a vector or neither", &I);
3228 Check(SrcBitSize > DestBitSize, "DestTy too big for FPTrunc", &I);
3229
3231}
3232
3233void Verifier::visitFPExtInst(FPExtInst &I) {
3234 // Get the source and destination types
3235 Type *SrcTy = I.getOperand(0)->getType();
3236 Type *DestTy = I.getType();
3237
3238 // Get the size of the types in bits, we'll need this later
3239 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3240 unsigned DestBitSize = DestTy->getScalarSizeInBits();
3241
3242 Check(SrcTy->isFPOrFPVectorTy(), "FPExt only operates on FP", &I);
3243 Check(DestTy->isFPOrFPVectorTy(), "FPExt only produces an FP", &I);
3244 Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
3245 "fpext source and destination must both be a vector or neither", &I);
3246 Check(SrcBitSize < DestBitSize, "DestTy too small for FPExt", &I);
3247
3249}
3250
3251void Verifier::visitUIToFPInst(UIToFPInst &I) {
3252 // Get the source and destination types
3253 Type *SrcTy = I.getOperand(0)->getType();
3254 Type *DestTy = I.getType();
3255
3256 bool SrcVec = SrcTy->isVectorTy();
3257 bool DstVec = DestTy->isVectorTy();
3258
3259 Check(SrcVec == DstVec,
3260 "UIToFP source and dest must both be vector or scalar", &I);
3261 Check(SrcTy->isIntOrIntVectorTy(),
3262 "UIToFP source must be integer or integer vector", &I);
3263 Check(DestTy->isFPOrFPVectorTy(), "UIToFP result must be FP or FP vector",
3264 &I);
3265
3266 if (SrcVec && DstVec)
3267 Check(cast<VectorType>(SrcTy)->getElementCount() ==
3268 cast<VectorType>(DestTy)->getElementCount(),
3269 "UIToFP source and dest vector length mismatch", &I);
3270
3272}
3273
3274void Verifier::visitSIToFPInst(SIToFPInst &I) {
3275 // Get the source and destination types
3276 Type *SrcTy = I.getOperand(0)->getType();
3277 Type *DestTy = I.getType();
3278
3279 bool SrcVec = SrcTy->isVectorTy();
3280 bool DstVec = DestTy->isVectorTy();
3281
3282 Check(SrcVec == DstVec,
3283 "SIToFP source and dest must both be vector or scalar", &I);
3284 Check(SrcTy->isIntOrIntVectorTy(),
3285 "SIToFP source must be integer or integer vector", &I);
3286 Check(DestTy->isFPOrFPVectorTy(), "SIToFP result must be FP or FP vector",
3287 &I);
3288
3289 if (SrcVec && DstVec)
3290 Check(cast<VectorType>(SrcTy)->getElementCount() ==
3291 cast<VectorType>(DestTy)->getElementCount(),
3292 "SIToFP source and dest vector length mismatch", &I);
3293
3295}
3296
3297void Verifier::visitFPToUIInst(FPToUIInst &I) {
3298 // Get the source and destination types
3299 Type *SrcTy = I.getOperand(0)->getType();
3300 Type *DestTy = I.getType();
3301
3302 bool SrcVec = SrcTy->isVectorTy();
3303 bool DstVec = DestTy->isVectorTy();
3304
3305 Check(SrcVec == DstVec,
3306 "FPToUI source and dest must both be vector or scalar", &I);
3307 Check(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector", &I);
3308 Check(DestTy->isIntOrIntVectorTy(),
3309 "FPToUI result must be integer or integer vector", &I);
3310
3311 if (SrcVec && DstVec)
3312 Check(cast<VectorType>(SrcTy)->getElementCount() ==
3313 cast<VectorType>(DestTy)->getElementCount(),
3314 "FPToUI source and dest vector length mismatch", &I);
3315
3317}
3318
3319void Verifier::visitFPToSIInst(FPToSIInst &I) {
3320 // Get the source and destination types
3321 Type *SrcTy = I.getOperand(0)->getType();
3322 Type *DestTy = I.getType();
3323
3324 bool SrcVec = SrcTy->isVectorTy();
3325 bool DstVec = DestTy->isVectorTy();
3326
3327 Check(SrcVec == DstVec,
3328 "FPToSI source and dest must both be vector or scalar", &I);
3329 Check(SrcTy->isFPOrFPVectorTy(), "FPToSI source must be FP or FP vector", &I);
3330 Check(DestTy->isIntOrIntVectorTy(),
3331 "FPToSI result must be integer or integer vector", &I);
3332
3333 if (SrcVec && DstVec)
3334 Check(cast<VectorType>(SrcTy)->getElementCount() ==
3335 cast<VectorType>(DestTy)->getElementCount(),
3336 "FPToSI source and dest vector length mismatch", &I);
3337
3339}
3340
3341void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
3342 // Get the source and destination types
3343 Type *SrcTy = I.getOperand(0)->getType();
3344 Type *DestTy = I.getType();
3345
3346 Check(SrcTy->isPtrOrPtrVectorTy(), "PtrToInt source must be pointer", &I);
3347
3348 Check(DestTy->isIntOrIntVectorTy(), "PtrToInt result must be integral", &I);
3349 Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), "PtrToInt type mismatch",
3350 &I);
3351
3352 if (SrcTy->isVectorTy()) {
3353 auto *VSrc = cast<VectorType>(SrcTy);
3354 auto *VDest = cast<VectorType>(DestTy);
3355 Check(VSrc->getElementCount() == VDest->getElementCount(),
3356 "PtrToInt Vector width mismatch", &I);
3357 }
3358
3360}
3361
3362void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
3363 // Get the source and destination types
3364 Type *SrcTy = I.getOperand(0)->getType();
3365 Type *DestTy = I.getType();
3366
3367 Check(SrcTy->isIntOrIntVectorTy(), "IntToPtr source must be an integral", &I);
3368 Check(DestTy->isPtrOrPtrVectorTy(), "IntToPtr result must be a pointer", &I);
3369
3370 Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), "IntToPtr type mismatch",
3371 &I);
3372 if (SrcTy->isVectorTy()) {
3373 auto *VSrc = cast<VectorType>(SrcTy);
3374 auto *VDest = cast<VectorType>(DestTy);
3375 Check(VSrc->getElementCount() == VDest->getElementCount(),
3376 "IntToPtr Vector width mismatch", &I);
3377 }
3379}
3380
3381void Verifier::visitBitCastInst(BitCastInst &I) {
3382 Check(
3383 CastInst::castIsValid(Instruction::BitCast, I.getOperand(0), I.getType()),
3384 "Invalid bitcast", &I);
3386}
3387
3388void Verifier::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
3389 Type *SrcTy = I.getOperand(0)->getType();
3390 Type *DestTy = I.getType();
3391
3392 Check(SrcTy->isPtrOrPtrVectorTy(), "AddrSpaceCast source must be a pointer",
3393 &I);
3394 Check(DestTy->isPtrOrPtrVectorTy(), "AddrSpaceCast result must be a pointer",
3395 &I);
3397 "AddrSpaceCast must be between different address spaces", &I);
3398 if (auto *SrcVTy = dyn_cast<VectorType>(SrcTy))
3399 Check(SrcVTy->getElementCount() ==
3400 cast<VectorType>(DestTy)->getElementCount(),
3401 "AddrSpaceCast vector pointer number of elements mismatch", &I);
3403}
3404
3405/// visitPHINode - Ensure that a PHI node is well formed.
3406///
3407void Verifier::visitPHINode(PHINode &PN) {
3408 // Ensure that the PHI nodes are all grouped together at the top of the block.
3409 // This can be tested by checking whether the instruction before this is
3410 // either nonexistent (because this is begin()) or is a PHI node. If not,
3411 // then there is some other instruction before a PHI.
3412 Check(&PN == &PN.getParent()->front() ||
3413 isa<PHINode>(--BasicBlock::iterator(&PN)),
3414 "PHI nodes not grouped at top of basic block!", &PN, PN.getParent());
3415
3416 // Check that a PHI doesn't yield a Token.
3417 Check(!PN.getType()->isTokenTy(), "PHI nodes cannot have token type!");
3418
3419 // Check that all of the values of the PHI node have the same type as the
3420 // result.
3421 for (Value *IncValue : PN.incoming_values()) {
3422 Check(PN.getType() == IncValue->getType(),
3423 "PHI node operands are not the same type as the result!", &PN);
3424 }
3425
3426 // All other PHI node constraints are checked in the visitBasicBlock method.
3427
3428 visitInstruction(PN);
3429}
3430
3431void Verifier::visitCallBase(CallBase &Call) {
3432 Check(Call.getCalledOperand()->getType()->isPointerTy(),
3433 "Called function must be a pointer!", Call);
3434 FunctionType *FTy = Call.getFunctionType();
3435
3436 // Verify that the correct number of arguments are being passed
3437 if (FTy->isVarArg())
3438 Check(Call.arg_size() >= FTy->getNumParams(),
3439 "Called function requires more parameters than were provided!", Call);
3440 else
3441 Check(Call.arg_size() == FTy->getNumParams(),
3442 "Incorrect number of arguments passed to called function!", Call);
3443
3444 // Verify that all arguments to the call match the function type.
3445 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3446 Check(Call.getArgOperand(i)->getType() == FTy->getParamType(i),
3447 "Call parameter type does not match function signature!",
3448 Call.getArgOperand(i), FTy->getParamType(i), Call);
3449
3450 AttributeList Attrs = Call.getAttributes();
3451
3452 Check(verifyAttributeCount(Attrs, Call.arg_size()),
3453 "Attribute after last parameter!", Call);
3454
3455 Function *Callee =
3456 dyn_cast<Function>(Call.getCalledOperand()->stripPointerCasts());
3457 bool IsIntrinsic = Callee && Callee->isIntrinsic();
3458 if (IsIntrinsic)
3459 Check(Callee->getValueType() == FTy,
3460 "Intrinsic called with incompatible signature", Call);
3461
3462 // Disallow calls to functions with the amdgpu_cs_chain[_preserve] calling
3463 // convention.
3464 auto CC = Call.getCallingConv();
3467 "Direct calls to amdgpu_cs_chain/amdgpu_cs_chain_preserve functions "
3468 "not allowed. Please use the @llvm.amdgpu.cs.chain intrinsic instead.",
3469 Call);
3470
3471 auto VerifyTypeAlign = [&](Type *Ty, const Twine &Message) {
3472 if (!Ty->isSized())
3473 return;
3474 Align ABIAlign = DL.getABITypeAlign(Ty);
3475 Align MaxAlign(ParamMaxAlignment);
3476 Check(ABIAlign <= MaxAlign,
3477 "Incorrect alignment of " + Message + " to called function!", Call);
3478 };
3479
3480 if (!IsIntrinsic) {
3481 VerifyTypeAlign(FTy->getReturnType(), "return type");
3482 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
3483 Type *Ty = FTy->getParamType(i);
3484 VerifyTypeAlign(Ty, "argument passed");
3485 }
3486 }
3487
3488 if (Attrs.hasFnAttr(Attribute::Speculatable)) {
3489 // Don't allow speculatable on call sites, unless the underlying function
3490 // declaration is also speculatable.
3491 Check(Callee && Callee->isSpeculatable(),
3492 "speculatable attribute may not apply to call sites", Call);
3493 }
3494
3495 if (Attrs.hasFnAttr(Attribute::Preallocated)) {
3496 Check(Call.getCalledFunction()->getIntrinsicID() ==
3497 Intrinsic::call_preallocated_arg,
3498 "preallocated as a call site attribute can only be on "
3499 "llvm.call.preallocated.arg");
3500 }
3501
3502 // Verify call attributes.
3503 verifyFunctionAttrs(FTy, Attrs, &Call, IsIntrinsic, Call.isInlineAsm());
3504
3505 // Conservatively check the inalloca argument.
3506 // We have a bug if we can find that there is an underlying alloca without
3507 // inalloca.
3508 if (Call.hasInAllocaArgument()) {
3509 Value *InAllocaArg = Call.getArgOperand(FTy->getNumParams() - 1);
3510 if (auto AI = dyn_cast<AllocaInst>(InAllocaArg->stripInBoundsOffsets()))
3511 Check(AI->isUsedWithInAlloca(),
3512 "inalloca argument for call has mismatched alloca", AI, Call);
3513 }
3514
3515 // For each argument of the callsite, if it has the swifterror argument,
3516 // make sure the underlying alloca/parameter it comes from has a swifterror as
3517 // well.
3518 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
3519 if (Call.paramHasAttr(i, Attribute::SwiftError)) {
3520 Value *SwiftErrorArg = Call.getArgOperand(i);
3521 if (auto AI = dyn_cast<AllocaInst>(SwiftErrorArg->stripInBoundsOffsets())) {
3522 Check(AI->isSwiftError(),
3523 "swifterror argument for call has mismatched alloca", AI, Call);
3524 continue;
3525 }
3526 auto ArgI = dyn_cast<Argument>(SwiftErrorArg);
3527 Check(ArgI, "swifterror argument should come from an alloca or parameter",
3528 SwiftErrorArg, Call);
3529 Check(ArgI->hasSwiftErrorAttr(),
3530 "swifterror argument for call has mismatched parameter", ArgI,
3531 Call);
3532 }
3533
3534 if (Attrs.hasParamAttr(i, Attribute::ImmArg)) {
3535 // Don't allow immarg on call sites, unless the underlying declaration
3536 // also has the matching immarg.
3537 Check(Callee && Callee->hasParamAttribute(i, Attribute::ImmArg),
3538 "immarg may not apply only to call sites", Call.getArgOperand(i),
3539 Call);
3540 }
3541
3542 if (Call.paramHasAttr(i, Attribute::ImmArg)) {
3543 Value *ArgVal = Call.getArgOperand(i);
3544 Check(isa<ConstantInt>(ArgVal) || isa<ConstantFP>(ArgVal),
3545 "immarg operand has non-immediate parameter", ArgVal, Call);
3546 }
3547
3548 if (Call.paramHasAttr(i, Attribute::Preallocated)) {
3549 Value *ArgVal = Call.getArgOperand(i);
3550 bool hasOB =
3551 Call.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0;
3552 bool isMustTail = Call.isMustTailCall();
3553 Check(hasOB != isMustTail,
3554 "preallocated operand either requires a preallocated bundle or "
3555 "the call to be musttail (but not both)",
3556 ArgVal, Call);
3557 }
3558 }
3559
3560 if (FTy->isVarArg()) {
3561 // FIXME? is 'nest' even legal here?
3562 bool SawNest = false;
3563 bool SawReturned = false;
3564
3565 for (unsigned Idx = 0; Idx < FTy->getNumParams(); ++Idx) {
3566 if (Attrs.hasParamAttr(Idx, Attribute::Nest))
3567 SawNest = true;
3568 if (Attrs.hasParamAttr(Idx, Attribute::Returned))
3569 SawReturned = true;
3570 }
3571
3572 // Check attributes on the varargs part.
3573 for (unsigned Idx = FTy->getNumParams(); Idx < Call.arg_size(); ++Idx) {
3574 Type *Ty = Call.getArgOperand(Idx)->getType();
3575 AttributeSet ArgAttrs = Attrs.getParamAttrs(Idx);
3576 verifyParameterAttrs(ArgAttrs, Ty, &Call);
3577
3578 if (ArgAttrs.hasAttribute(Attribute::Nest)) {
3579 Check(!SawNest, "More than one parameter has attribute nest!", Call);
3580 SawNest = true;
3581 }
3582
3583 if (ArgAttrs.hasAttribute(Attribute::Returned)) {
3584 Check(!SawReturned, "More than one parameter has attribute returned!",
3585 Call);
3586 Check(Ty->canLosslesslyBitCastTo(FTy->getReturnType()),
3587 "Incompatible argument and return types for 'returned' "
3588 "attribute",
3589 Call);
3590 SawReturned = true;
3591 }
3592
3593 // Statepoint intrinsic is vararg but the wrapped function may be not.
3594 // Allow sret here and check the wrapped function in verifyStatepoint.
3595 if (!Call.getCalledFunction() ||
3596 Call.getCalledFunction()->getIntrinsicID() !=
3597 Intrinsic::experimental_gc_statepoint)
3598 Check(!ArgAttrs.hasAttribute(Attribute::StructRet),
3599 "Attribute 'sret' cannot be used for vararg call arguments!",
3600 Call);
3601
3602 if (ArgAttrs.hasAttribute(Attribute::InAlloca))
3603 Check(Idx == Call.arg_size() - 1,
3604 "inalloca isn't on the last argument!", Call);
3605 }
3606 }
3607
3608 // Verify that there's no metadata unless it's a direct call to an intrinsic.
3609 if (!IsIntrinsic) {
3610 for (Type *ParamTy : FTy->params()) {
3611 Check(!ParamTy->isMetadataTy(),
3612 "Function has metadata parameter but isn't an intrinsic", Call);
3613 Check(!ParamTy->isTokenTy(),
3614 "Function has token parameter but isn't an intrinsic", Call);
3615 }
3616 }
3617
3618 // Verify that indirect calls don't return tokens.
3619 if (!Call.getCalledFunction()) {
3620 Check(!FTy->getReturnType()->isTokenTy(),
3621 "Return type cannot be token for indirect call!");
3622 Check(!FTy->getReturnType()->isX86_AMXTy(),
3623 "Return type cannot be x86_amx for indirect call!");
3624 }
3625
3626 if (Function *F = Call.getCalledFunction())
3627 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
3628 visitIntrinsicCall(ID, Call);
3629
3630 // Verify that a callsite has at most one "deopt", at most one "funclet", at
3631 // most one "gc-transition", at most one "cfguardtarget", at most one
3632 // "preallocated" operand bundle, and at most one "ptrauth" operand bundle.
3633 bool FoundDeoptBundle = false, FoundFuncletBundle = false,
3634 FoundGCTransitionBundle = false, FoundCFGuardTargetBundle = false,
3635 FoundPreallocatedBundle = false, FoundGCLiveBundle = false,
3636 FoundPtrauthBundle = false, FoundKCFIBundle = false,
3637 FoundAttachedCallBundle = false;
3638 for (unsigned i = 0, e = Call.getNumOperandBundles(); i < e; ++i) {
3639 OperandBundleUse BU = Call.getOperandBundleAt(i);
3640 uint32_t Tag = BU.getTagID();
3641 if (Tag == LLVMContext::OB_deopt) {
3642 Check(!FoundDeoptBundle, "Multiple deopt operand bundles", Call);
3643 FoundDeoptBundle = true;
3644 } else if (Tag == LLVMContext::OB_gc_transition) {
3645 Check(!FoundGCTransitionBundle, "Multiple gc-transition operand bundles",
3646 Call);
3647 FoundGCTransitionBundle = true;
3648 } else if (Tag == LLVMContext::OB_funclet) {
3649 Check(!FoundFuncletBundle, "Multiple funclet operand bundles", Call);
3650 FoundFuncletBundle = true;
3651 Check(BU.Inputs.size() == 1,
3652 "Expected exactly one funclet bundle operand", Call);
3653 Check(isa<FuncletPadInst>(BU.Inputs.front()),
3654 "Funclet bundle operands should correspond to a FuncletPadInst",
3655 Call);
3656 } else if (Tag == LLVMContext::OB_cfguardtarget) {
3657 Check(!FoundCFGuardTargetBundle, "Multiple CFGuardTarget operand bundles",
3658 Call);
3659 FoundCFGuardTargetBundle = true;
3660 Check(BU.Inputs.size() == 1,
3661 "Expected exactly one cfguardtarget bundle operand", Call);
3662 } else if (Tag == LLVMContext::OB_ptrauth) {
3663 Check(!FoundPtrauthBundle, "Multiple ptrauth operand bundles", Call);
3664 FoundPtrauthBundle = true;
3665 Check(BU.Inputs.size() == 2,
3666 "Expected exactly two ptrauth bundle operands", Call);
3667 Check(isa<ConstantInt>(BU.Inputs[0]) &&
3668 BU.Inputs[0]->getType()->isIntegerTy(32),
3669 "Ptrauth bundle key operand must be an i32 constant", Call);
3670 Check(BU.Inputs[1]->getType()->isIntegerTy(64),
3671 "Ptrauth bundle discriminator operand must be an i64", Call);
3672 } else if (Tag == LLVMContext::OB_kcfi) {
3673 Check(!FoundKCFIBundle, "Multiple kcfi operand bundles", Call);
3674 FoundKCFIBundle = true;
3675 Check(BU.Inputs.size() == 1, "Expected exactly one kcfi bundle operand",
3676 Call);
3677 Check(isa<ConstantInt>(BU.Inputs[0]) &&
3678 BU.Inputs[0]->getType()->isIntegerTy(32),
3679 "Kcfi bundle operand must be an i32 constant", Call);
3680 } else if (Tag == LLVMContext::OB_preallocated) {
3681 Check(!FoundPreallocatedBundle, "Multiple preallocated operand bundles",
3682 Call);
3683 FoundPreallocatedBundle = true;
3684 Check(BU.Inputs.size() == 1,
3685 "Expected exactly one preallocated bundle operand", Call);
3686 auto Input = dyn_cast<IntrinsicInst>(BU.Inputs.front());
3687 Check(Input &&
3688 Input->getIntrinsicID() == Intrinsic::call_preallocated_setup,
3689 "\"preallocated\" argument must be a token from "
3690 "llvm.call.preallocated.setup",
3691 Call);
3692 } else if (Tag == LLVMContext::OB_gc_live) {
3693 Check(!FoundGCLiveBundle, "Multiple gc-live operand bundles", Call);
3694 FoundGCLiveBundle = true;
3696 Check(!FoundAttachedCallBundle,
3697 "Multiple \"clang.arc.attachedcall\" operand bundles", Call);
3698 FoundAttachedCallBundle = true;
3699 verifyAttachedCallBundle(Call, BU);
3700 }
3701 }
3702
3703 // Verify that callee and callsite agree on whether to use pointer auth.
3704 Check(!(Call.getCalledFunction() && FoundPtrauthBundle),
3705 "Direct call cannot have a ptrauth bundle", Call);
3706
3707 // Verify that each inlinable callsite of a debug-info-bearing function in a
3708 // debug-info-bearing function has a debug location attached to it. Failure to
3709 // do so causes assertion failures when the inliner sets up inline scope info
3710 // (Interposable functions are not inlinable, neither are functions without
3711 // definitions.)
3712 if (Call.getFunction()->getSubprogram() && Call.getCalledFunction() &&
3713 !Call.getCalledFunction()->isInterposable() &&
3714 !Call.getCalledFunction()->isDeclaration() &&
3715 Call.getCalledFunction()->getSubprogram())
3716 CheckDI(Call.getDebugLoc(),
3717 "inlinable function call in a function with "
3718 "debug info must have a !dbg location",
3719 Call);
3720
3721 if (Call.isInlineAsm())
3722 verifyInlineAsmCall(Call);
3723
3724 ConvergenceVerifyHelper.visit(Call);
3725
3726 visitInstruction(Call);
3727}
3728
3729void Verifier::verifyTailCCMustTailAttrs(const AttrBuilder &Attrs,
3730 StringRef Context) {
3731 Check(!Attrs.contains(Attribute::InAlloca),
3732 Twine("inalloca attribute not allowed in ") + Context);
3733 Check(!Attrs.contains(Attribute::InReg),
3734 Twine("inreg attribute not allowed in ") + Context);
3735 Check(!Attrs.contains(Attribute::SwiftError),
3736 Twine("swifterror attribute not allowed in ") + Context);
3737 Check(!Attrs.contains(Attribute::Preallocated),
3738 Twine("preallocated attribute not allowed in ") + Context);
3739 Check(!Attrs.contains(Attribute::ByRef),
3740 Twine("byref attribute not allowed in ") + Context);
3741}
3742
3743/// Two types are "congruent" if they are identical, or if they are both pointer
3744/// types with different pointee types and the same address space.
3745static bool isTypeCongruent(Type *L, Type *R) {
3746 if (L == R)
3747 return true;
3748 PointerType *PL = dyn_cast<PointerType>(L);
3749 PointerType *PR = dyn_cast<PointerType>(R);
3750 if (!PL || !PR)
3751 return false;
3752 return PL->getAddressSpace() == PR->getAddressSpace();
3753}
3754
3756 static const Attribute::AttrKind ABIAttrs[] = {
3757 Attribute::StructRet, Attribute::ByVal, Attribute::InAlloca,
3758 Attribute::InReg, Attribute::StackAlignment, Attribute::SwiftSelf,
3759 Attribute::SwiftAsync, Attribute::SwiftError, Attribute::Preallocated,
3760 Attribute::ByRef};
3761 AttrBuilder Copy(C);
3762 for (auto AK : ABIAttrs) {
3763 Attribute Attr = Attrs.getParamAttrs(I).getAttribute(AK);
3764 if (Attr.isValid())
3765 Copy.addAttribute(Attr);
3766 }
3767
3768 // `align` is ABI-affecting only in combination with `byval` or `byref`.
3769 if (Attrs.hasParamAttr(I, Attribute::Alignment) &&
3770 (Attrs.hasParamAttr(I, Attribute::ByVal) ||
3771 Attrs.hasParamAttr(I, Attribute::ByRef)))
3772 Copy.addAlignmentAttr(Attrs.getParamAlignment(I));
3773 return Copy;
3774}
3775
3776void Verifier::verifyMustTailCall(CallInst &CI) {
3777 Check(!CI.isInlineAsm(), "cannot use musttail call with inline asm", &CI);
3778
3779 Function *F = CI.getParent()->getParent();
3780 FunctionType *CallerTy = F->getFunctionType();
3781 FunctionType *CalleeTy = CI.getFunctionType();
3782 Check(CallerTy->isVarArg() == CalleeTy->isVarArg(),
3783 "cannot guarantee tail call due to mismatched varargs", &CI);
3784 Check(isTypeCongruent(CallerTy->getReturnType(), CalleeTy->getReturnType()),
3785 "cannot guarantee tail call due to mismatched return types", &CI);
3786
3787 // - The calling conventions of the caller and callee must match.
3788 Check(F->getCallingConv() == CI.getCallingConv(),
3789 "cannot guarantee tail call due to mismatched calling conv", &CI);
3790
3791 // - The call must immediately precede a :ref:`ret <i_ret>` instruction,
3792 // or a pointer bitcast followed by a ret instruction.
3793 // - The ret instruction must return the (possibly bitcasted) value
3794 // produced by the call or void.
3795 Value *RetVal = &CI;
3796 Instruction *Next = CI.getNextNode();
3797
3798 // Handle the optional bitcast.
3799 if (BitCastInst *BI = dyn_cast_or_null<BitCastInst>(Next)) {
3800 Check(BI->getOperand(0) == RetVal,
3801 "bitcast following musttail call must use the call", BI);
3802 RetVal = BI;
3803 Next = BI->getNextNode();
3804 }
3805
3806 // Check the return.
3807 ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next);
3808 Check(Ret, "musttail call must precede a ret with an optional bitcast", &CI);
3809 Check(!Ret->getReturnValue() || Ret->getReturnValue() == RetVal ||
3810 isa<UndefValue>(Ret->getReturnValue()),
3811 "musttail call result must be returned", Ret);
3812
3813 AttributeList CallerAttrs = F->getAttributes();
3814 AttributeList CalleeAttrs = CI.getAttributes();
3817 StringRef CCName =
3818 CI.getCallingConv() == CallingConv::Tail ? "tailcc" : "swifttailcc";
3819
3820 // - Only sret, byval, swiftself, and swiftasync ABI-impacting attributes
3821 // are allowed in swifttailcc call
3822 for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3823 AttrBuilder ABIAttrs = getParameterABIAttributes(F->getContext(), I, CallerAttrs);
3824 SmallString<32> Context{CCName, StringRef(" musttail caller")};
3825 verifyTailCCMustTailAttrs(ABIAttrs, Context);
3826 }
3827 for (unsigned I = 0, E = CalleeTy->getNumParams(); I != E; ++I) {
3828 AttrBuilder ABIAttrs = getParameterABIAttributes(F->getContext(), I, CalleeAttrs);
3829 SmallString<32> Context{CCName, StringRef(" musttail callee")};
3830 verifyTailCCMustTailAttrs(ABIAttrs, Context);
3831 }
3832 // - Varargs functions are not allowed
3833 Check(!CallerTy->isVarArg(), Twine("cannot guarantee ") + CCName +
3834 " tail call for varargs function");
3835 return;
3836 }
3837
3838 // - The caller and callee prototypes must match. Pointer types of
3839 // parameters or return types may differ in pointee type, but not
3840 // address space.
3841 if (!CI.getCalledFunction() || !CI.getCalledFunction()->isIntrinsic()) {
3842 Check(CallerTy->getNumParams() == CalleeTy->getNumParams(),
3843 "cannot guarantee tail call due to mismatched parameter counts", &CI);
3844 for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3845 Check(
3846 isTypeCongruent(CallerTy->getParamType(I), CalleeTy->getParamType(I)),
3847 "cannot guarantee tail call due to mismatched parameter types", &CI);
3848 }
3849 }
3850
3851 // - All ABI-impacting function attributes, such as sret, byval, inreg,
3852 // returned, preallocated, and inalloca, must match.
3853 for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3854 AttrBuilder CallerABIAttrs = getParameterABIAttributes(F->getContext(), I, CallerAttrs);
3855 AttrBuilder CalleeABIAttrs = getParameterABIAttributes(F->getContext(), I, CalleeAttrs);
3856 Check(CallerABIAttrs == CalleeABIAttrs,
3857 "cannot guarantee tail call due to mismatched ABI impacting "
3858 "function attributes",
3859 &CI, CI.getOperand(I));
3860 }
3861}
3862
3863void Verifier::visitCallInst(CallInst &CI) {
3864 visitCallBase(CI);
3865
3866 if (CI.isMustTailCall())
3867 verifyMustTailCall(CI);
3868}
3869
3870void Verifier::visitInvokeInst(InvokeInst &II) {
3871 visitCallBase(II);
3872
3873 // Verify that the first non-PHI instruction of the unwind destination is an
3874 // exception handling instruction.
3875 Check(
3876 II.getUnwindDest()->isEHPad(),
3877 "The unwind destination does not have an exception handling instruction!",
3878 &II);
3879
3880 visitTerminator(II);
3881}
3882
3883/// visitUnaryOperator - Check the argument to the unary operator.
3884///
3885void Verifier::visitUnaryOperator(UnaryOperator &U) {
3886 Check(U.getType() == U.getOperand(0)->getType(),
3887 "Unary operators must have same type for"
3888 "operands and result!",
3889 &U);
3890
3891 switch (U.getOpcode()) {
3892 // Check that floating-point arithmetic operators are only used with
3893 // floating-point operands.
3894 case Instruction::FNeg:
3895 Check(U.getType()->isFPOrFPVectorTy(),
3896 "FNeg operator only works with float types!", &U);
3897 break;
3898 default:
3899 llvm_unreachable("Unknown UnaryOperator opcode!");
3900 }
3901
3903}
3904
3905/// visitBinaryOperator - Check that both arguments to the binary operator are
3906/// of the same type!
3907///
3908void Verifier::visitBinaryOperator(BinaryOperator &B) {
3909 Check(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
3910 "Both operands to a binary operator are not of the same type!", &B);
3911
3912 switch (B.getOpcode()) {
3913 // Check that integer arithmetic operators are only used with
3914 // integral operands.
3915 case Instruction::Add:
3916 case Instruction::Sub:
3917 case Instruction::Mul:
3918 case Instruction::SDiv:
3919 case Instruction::UDiv:
3920 case Instruction::SRem:
3921 case Instruction::URem:
3922 Check(B.getType()->isIntOrIntVectorTy(),
3923 "Integer arithmetic operators only work with integral types!", &B);
3924 Check(B.getType() == B.getOperand(0)->getType(),
3925 "Integer arithmetic operators must have same type "
3926 "for operands and result!",
3927 &B);
3928 break;
3929 // Check that floating-point arithmetic operators are only used with
3930 // floating-point operands.
3931 case Instruction::FAdd:
3932 case Instruction::FSub:
3933 case Instruction::FMul:
3934 case Instruction::FDiv:
3935 case Instruction::FRem:
3936 Check(B.getType()->isFPOrFPVectorTy(),
3937 "Floating-point arithmetic operators only work with "
3938 "floating-point types!",
3939 &B);
3940 Check(B.getType() == B.getOperand(0)->getType(),
3941 "Floating-point arithmetic operators must have same type "
3942 "for operands and result!",
3943 &B);
3944 break;
3945 // Check that logical operators are only used with integral operands.
3946 case Instruction::And:
3947 case Instruction::Or:
3948 case Instruction::Xor:
3949 Check(B.getType()->isIntOrIntVectorTy(),
3950 "Logical operators only work with integral types!", &B);
3951 Check(B.getType() == B.getOperand(0)->getType(),
3952 "Logical operators must have same type for operands and result!", &B);
3953 break;
3954 case Instruction::Shl:
3955 case Instruction::LShr:
3956 case Instruction::AShr:
3957 Check(B.getType()->isIntOrIntVectorTy(),
3958 "Shifts only work with integral types!", &B);
3959 Check(B.getType() == B.getOperand(0)->getType(),
3960 "Shift return type must be same as operands!", &B);
3961 break;
3962 default:
3963 llvm_unreachable("Unknown BinaryOperator opcode!");
3964 }
3965
3967}
3968
3969void Verifier::visitICmpInst(ICmpInst &IC) {
3970 // Check that the operands are the same type
3971 Type *Op0Ty = IC.getOperand(0)->getType();
3972 Type *Op1Ty = IC.getOperand(1)->getType();
3973 Check(Op0Ty == Op1Ty,
3974 "Both operands to ICmp instruction are not of the same type!", &IC);
3975 // Check that the operands are the right type
3976 Check(Op0Ty->isIntOrIntVectorTy() || Op0Ty->isPtrOrPtrVectorTy(),
3977 "Invalid operand types for ICmp instruction", &IC);
3978 // Check that the predicate is valid.
3979 Check(IC.isIntPredicate(), "Invalid predicate in ICmp instruction!", &IC);
3980
3981 visitInstruction(IC);
3982}
3983
3984void Verifier::visitFCmpInst(FCmpInst &FC) {
3985 // Check that the operands are the same type
3986 Type *Op0Ty = FC.getOperand(0)->getType();
3987 Type *Op1Ty = FC.getOperand(1)->getType();
3988 Check(Op0Ty == Op1Ty,
3989 "Both operands to FCmp instruction are not of the same type!", &FC);
3990 // Check that the operands are the right type
3991 Check(Op0Ty->isFPOrFPVectorTy(), "Invalid operand types for FCmp instruction",
3992 &FC);
3993 // Check that the predicate is valid.
3994 Check(FC.isFPPredicate(), "Invalid predicate in FCmp instruction!", &FC);
3995
3996 visitInstruction(FC);
3997}
3998
3999void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
4001 "Invalid extractelement operands!", &EI);
4002 visitInstruction(EI);
4003}
4004
4005void Verifier::visitInsertElementInst(InsertElementInst &IE) {
4006 Check(InsertElementInst::isValidOperands(IE.getOperand(0), IE.getOperand(1),
4007 IE.getOperand(2)),
4008 "Invalid insertelement operands!", &IE);
4009 visitInstruction(IE);
4010}
4011
4012void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
4014 SV.getShuffleMask()),
4015 "Invalid shufflevector operands!", &SV);
4016 visitInstruction(SV);
4017}
4018
4019void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
4020 Type *TargetTy = GEP.getPointerOperandType()->getScalarType();
4021
4022 Check(isa<PointerType>(TargetTy),
4023 "GEP base pointer is not a vector or a vector of pointers", &GEP);
4024 Check(GEP.getSourceElementType()->isSized(), "GEP into unsized type!", &GEP);
4025
4026 if (auto *STy = dyn_cast<StructType>(GEP.getSourceElementType())) {
4027 SmallPtrSet<Type *, 4> Visited;
4028 Check(!STy->containsScalableVectorType(&Visited),
4029 "getelementptr cannot target structure that contains scalable vector"
4030 "type",
4031 &GEP);
4032 }
4033
4034 SmallVector<Value *, 16> Idxs(GEP.indices());
4035 Check(
4036 all_of(Idxs, [](Value *V) { return V->getType()->isIntOrIntVectorTy(); }),
4037 "GEP indexes must be integers", &GEP);
4038 Type *ElTy =
4039 GetElementPtrInst::getIndexedType(GEP.getSourceElementType(), Idxs);
4040 Check(ElTy, "Invalid indices for GEP pointer type!", &GEP);
4041
4042 Check(GEP.getType()->isPtrOrPtrVectorTy() &&
4043 GEP.getResultElementType() == ElTy,
4044 "GEP is not of right type for indices!", &GEP, ElTy);
4045
4046 if (auto *GEPVTy = dyn_cast<VectorType>(GEP.getType())) {
4047 // Additional checks for vector GEPs.
4048 ElementCount GEPWidth = GEPVTy->getElementCount();
4049 if (GEP.getPointerOperandType()->isVectorTy())
4050 Check(
4051 GEPWidth ==
4052 cast<VectorType>(GEP.getPointerOperandType())->getElementCount(),
4053 "Vector GEP result width doesn't match operand's", &GEP);
4054 for (Value *Idx : Idxs) {
4055 Type *IndexTy = Idx->getType();
4056 if (auto *IndexVTy = dyn_cast<VectorType>(IndexTy)) {
4057 ElementCount IndexWidth = IndexVTy->getElementCount();
4058 Check(IndexWidth == GEPWidth, "Invalid GEP index vector width", &GEP);
4059 }
4060 Check(IndexTy->isIntOrIntVectorTy(),
4061 "All GEP indices should be of integer type");
4062 }
4063 }
4064
4065 if (auto *PTy = dyn_cast<PointerType>(GEP.getType())) {
4066 Check(GEP.getAddressSpace() == PTy->getAddressSpace(),
4067 "GEP address space doesn't match type", &GEP);
4068 }
4069
4071}
4072
4073static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
4074 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
4075}
4076
4077/// Verify !range and !absolute_symbol metadata. These have the same
4078/// restrictions, except !absolute_symbol allows the full set.
4079void Verifier::verifyRangeMetadata(const Value &I, const MDNode *Range,
4080 Type *Ty, bool IsAbsoluteSymbol) {
4081 unsigned NumOperands = Range->getNumOperands();
4082 Check(NumOperands % 2 == 0, "Unfinished range!", Range);
4083 unsigned NumRanges = NumOperands / 2;
4084 Check(NumRanges >= 1, "It should have at least one range!", Range);
4085
4086 ConstantRange LastRange(1, true); // Dummy initial value
4087 for (unsigned i = 0; i < NumRanges; ++i) {
4088 ConstantInt *Low =
4089 mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i));
4090 Check(Low, "The lower limit must be an integer!", Low);
4091 ConstantInt *High =
4092 mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i + 1));
4093 Check(High, "The upper limit must be an integer!", High);
4094 Check(High->getType() == Low->getType() &&
4095 High->getType() == Ty->getScalarType(),
4096 "Range types must match instruction type!", &I);
4097
4098 APInt HighV = High->getValue();
4099 APInt LowV = Low->getValue();
4100
4101 // ConstantRange asserts if the ranges are the same except for the min/max
4102 // value. Leave the cases it tolerates for the empty range error below.
4103 Check(LowV != HighV || LowV.isMaxValue() || LowV.isMinValue(),
4104 "The upper and lower limits cannot be the same value", &I);
4105
4106 ConstantRange CurRange(LowV, HighV);
4107 Check(!CurRange.isEmptySet() && (IsAbsoluteSymbol || !CurRange.isFullSet()),
4108 "Range must not be empty!", Range);
4109 if (i != 0) {
4110 Check(CurRange.intersectWith(LastRange).isEmptySet(),
4111 "Intervals are overlapping", Range);
4112 Check(LowV.sgt(LastRange.getLower()), "Intervals are not in order",
4113 Range);
4114 Check(!isContiguous(CurRange, LastRange), "Intervals are contiguous",
4115 Range);
4116 }
4117 LastRange = ConstantRange(LowV, HighV);
4118 }
4119 if (NumRanges > 2) {
4120 APInt FirstLow =
4121 mdconst::dyn_extract<ConstantInt>(Range->getOperand(0))->getValue();
4122 APInt FirstHigh =
4123 mdconst::dyn_extract<ConstantInt>(Range->getOperand(1))->getValue();
4124 ConstantRange FirstRange(FirstLow, FirstHigh);
4125 Check(FirstRange.intersectWith(LastRange).isEmptySet(),
4126 "Intervals are overlapping", Range);
4127 Check(!isContiguous(FirstRange, LastRange), "Intervals are contiguous",
4128 Range);
4129 }
4130}
4131
4132void Verifier::visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty) {
4133 assert(Range && Range == I.getMetadata(LLVMContext::MD_range) &&
4134 "precondition violation");
4135 verifyRangeMetadata(I, Range, Ty, false);
4136}
4137
4138void Verifier::checkAtomicMemAccessSize(Type *Ty, const Instruction *I) {
4139 unsigned Size = DL.getTypeSizeInBits(Ty);
4140 Check(Size >= 8, "atomic memory access' size must be byte-sized", Ty, I);
4141 Check(!(Size & (Size - 1)),
4142 "atomic memory access' operand must have a power-of-two size", Ty, I);
4143}
4144
4145void Verifier::visitLoadInst(LoadInst &LI) {
4146 PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType());
4147 Check(PTy, "Load operand must be a pointer.", &LI);
4148 Type *ElTy = LI.getType();
4149 if (MaybeAlign A = LI.getAlign()) {
4150 Check(A->value() <= Value::MaximumAlignment,
4151 "huge alignment values are unsupported", &LI);
4152 }
4153 Check(ElTy->isSized(), "loading unsized types is not allowed", &LI);
4154 if (LI.isAtomic()) {
4157 "Load cannot have Release ordering", &LI);
4158 Check(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
4159 "atomic load operand must have integer, pointer, or floating point "
4160 "type!",
4161 ElTy, &LI);
4162 checkAtomicMemAccessSize(ElTy, &LI);
4163 } else {
4165 "Non-atomic load cannot have SynchronizationScope specified", &LI);
4166 }
4167
4168 visitInstruction(LI);
4169}
4170
4171void Verifier::visitStoreInst(StoreInst &SI) {
4172 PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType());
4173 Check(PTy, "Store operand must be a pointer.", &SI);
4174 Type *ElTy = SI.getOperand(0)->getType();
4175 if (MaybeAlign A = SI.getAlign()) {
4176 Check(A->value() <= Value::MaximumAlignment,
4177 "huge alignment values are unsupported", &SI);
4178 }
4179 Check(ElTy->isSized(), "storing unsized types is not allowed", &SI);
4180 if (SI.isAtomic()) {
4181 Check(SI.getOrdering() != AtomicOrdering::Acquire &&
4182 SI.getOrdering() != AtomicOrdering::AcquireRelease,
4183 "Store cannot have Acquire ordering", &SI);
4184 Check(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
4185 "atomic store operand must have integer, pointer, or floating point "
4186 "type!",
4187 ElTy, &SI);
4188 checkAtomicMemAccessSize(ElTy, &SI);
4189 } else {
4190 Check(SI.getSyncScopeID() == SyncScope::System,
4191 "Non-atomic store cannot have SynchronizationScope specified", &SI);
4192 }
4193 visitInstruction(SI);
4194}
4195
4196/// Check that SwiftErrorVal is used as a swifterror argument in CS.
4197void Verifier::verifySwiftErrorCall(CallBase &Call,
4198 const Value *SwiftErrorVal) {
4199 for (const auto &I : llvm::enumerate(Call.args())) {
4200 if (I.value() == SwiftErrorVal) {
4201 Check(Call.paramHasAttr(I.index(), Attribute::SwiftError),
4202 "swifterror value when used in a callsite should be marked "
4203 "with swifterror attribute",
4204 SwiftErrorVal, Call);
4205 }
4206 }
4207}
4208
4209void Verifier::verifySwiftErrorValue(const Value *SwiftErrorVal) {
4210 // Check that swifterror value is only used by loads, stores, or as
4211 // a swifterror argument.
4212 for (const User *U : SwiftErrorVal->users()) {
4213 Check(isa<LoadInst>(U) || isa<StoreInst>(U) || isa<CallInst>(U) ||
4214 isa<InvokeInst>(U),
4215 "swifterror value can only be loaded and stored from, or "
4216 "as a swifterror argument!",
4217 SwiftErrorVal, U);
4218 // If it is used by a store, check it is the second operand.
4219 if (auto StoreI = dyn_cast<StoreInst>(U))
4220 Check(StoreI->getOperand(1) == SwiftErrorVal,
4221 "swifterror value should be the second operand when used "
4222 "by stores",
4223 SwiftErrorVal, U);
4224 if (auto *Call = dyn_cast<CallBase>(U))
4225 verifySwiftErrorCall(*const_cast<CallBase *>(Call), SwiftErrorVal);
4226 }
4227}
4228
4229void Verifier::visitAllocaInst(AllocaInst &AI) {
4230 SmallPtrSet<Type*, 4> Visited;
4231 Check(AI.getAllocatedType()->isSized(&Visited),
4232 "Cannot allocate unsized type", &AI);
4234 "Alloca array size must have integer type", &AI);
4235 if (MaybeAlign A = AI.getAlign()) {
4236 Check(A->value() <= Value::MaximumAlignment,
4237 "huge alignment values are unsupported", &AI);
4238 }
4239
4240 if (AI.isSwiftError()) {
4242 "swifterror alloca must have pointer type", &AI);
4244 "swifterror alloca must not be array allocation", &AI);
4245 verifySwiftErrorValue(&AI);
4246 }
4247
4248 visitInstruction(AI);
4249}
4250
4251void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI) {
4252 Type *ElTy = CXI.getOperand(1)->getType();
4253 Check(ElTy->isIntOrPtrTy(),
4254 "cmpxchg operand must have integer or pointer type", ElTy, &CXI);
4255 checkAtomicMemAccessSize(ElTy, &CXI);
4256 visitInstruction(CXI);
4257}
4258
4259void Verifier::visitAtomicRMWInst(AtomicRMWInst &RMWI) {
4261 "atomicrmw instructions cannot be unordered.", &RMWI);
4262 auto Op = RMWI.getOperation();
4263 Type *ElTy = RMWI.getOperand(1)->getType();
4264 if (Op == AtomicRMWInst::Xchg) {
4265 Check(ElTy->isIntegerTy() || ElTy->isFloatingPointTy() ||
4266 ElTy->isPointerTy(),
4267 "atomicrmw " + AtomicRMWInst::getOperationName(Op) +
4268 " operand must have integer or floating point type!",
4269 &RMWI, ElTy);
4270 } else if (AtomicRMWInst::isFPOperation(Op)) {
4271 Check(ElTy->isFPOrFPVectorTy() && !isa<ScalableVectorType>(ElTy),
4272 "atomicrmw " + AtomicRMWInst::getOperationName(Op) +
4273 " operand must have floating-point or fixed vector of floating-point "
4274 "type!",
4275 &RMWI, ElTy);
4276 } else {
4277 Check(ElTy->isIntegerTy(),
4278 "atomicrmw " + AtomicRMWInst::getOperationName(Op) +
4279 " operand must have integer type!",
4280 &RMWI, ElTy);
4281 }
4282 checkAtomicMemAccessSize(ElTy, &RMWI);
4284 "Invalid binary operation!", &RMWI);
4285 visitInstruction(RMWI);
4286}
4287
4288void Verifier::visitFenceInst(FenceInst &FI) {
4289 const AtomicOrdering Ordering = FI.getOrdering();
4290 Check(Ordering == AtomicOrdering::Acquire ||
4291 Ordering == AtomicOrdering::Release ||
4292 Ordering == AtomicOrdering::AcquireRelease ||
4294 "fence instructions may only have acquire, release, acq_rel, or "
4295 "seq_cst ordering.",
4296 &FI);
4297 visitInstruction(FI);
4298}
4299
4300void Verifier::visitExtractValueInst(ExtractValueInst &EVI) {
4302 EVI.getIndices()) == EVI.getType(),
4303 "Invalid ExtractValueInst operands!", &EVI);
4304
4305 visitInstruction(EVI);
4306}
4307
4308void Verifier::visitInsertValueInst(InsertValueInst &IVI) {
4310 IVI.getIndices()) ==
4311 IVI.getOperand(1)->getType(),
4312 "Invalid InsertValueInst operands!", &IVI);
4313
4314 visitInstruction(IVI);
4315}
4316
4317static Value *getParentPad(Value *EHPad) {
4318 if (auto *FPI = dyn_cast<FuncletPadInst>(EHPad))
4319 return FPI->getParentPad();
4320
4321 return cast<CatchSwitchInst>(EHPad)->getParentPad();
4322}
4323
4324void Verifier::visitEHPadPredecessors(Instruction &I) {
4325 assert(I.isEHPad());
4326
4327 BasicBlock *BB = I.getParent();
4328 Function *F = BB->getParent();
4329
4330 Check(BB != &F->getEntryBlock(), "EH pad cannot be in entry block.", &I);
4331
4332 if (auto *LPI = dyn_cast<LandingPadInst>(&I)) {
4333 // The landingpad instruction defines its parent as a landing pad block. The
4334 // landing pad block may be branched to only by the unwind edge of an
4335 // invoke.
4336 for (BasicBlock *PredBB : predecessors(BB)) {
4337 const auto *II = dyn_cast<InvokeInst>(PredBB->getTerminator());
4338 Check(II && II->getUnwindDest() == BB && II->getNormalDest() != BB,
4339 "Block containing LandingPadInst must be jumped to "
4340 "only by the unwind edge of an invoke.",
4341 LPI);
4342 }
4343 return;
4344 }
4345 if (auto *CPI = dyn_cast<CatchPadInst>(&I)) {
4346 if (!pred_empty(BB))
4347 Check(BB->getUniquePredecessor() == CPI->getCatchSwitch()->getParent(),
4348 "Block containg CatchPadInst must be jumped to "
4349 "only by its catchswitch.",
4350 CPI);
4351 Check(BB != CPI->getCatchSwitch()->getUnwindDest(),
4352 "Catchswitch cannot unwind to one of its catchpads",
4353 CPI->getCatchSwitch(), CPI);
4354 return;
4355 }
4356
4357 // Verify that each pred has a legal terminator with a legal to/from EH
4358 // pad relationship.
4359 Instruction *ToPad = &I;
4360 Value *ToPadParent = getParentPad(ToPad);
4361 for (BasicBlock *PredBB : predecessors(BB)) {
4362 Instruction *TI = PredBB->getTerminator();
4363 Value *FromPad;
4364 if (auto *II = dyn_cast<InvokeInst>(TI)) {
4365 Check(II->getUnwindDest() == BB && II->getNormalDest() != BB,
4366 "EH pad must be jumped to via an unwind edge", ToPad, II);
4367 auto *CalledFn =
4368 dyn_cast<Function>(II->getCalledOperand()->stripPointerCasts());
4369 if (CalledFn && CalledFn->isIntrinsic() && II->doesNotThrow() &&
4370 !IntrinsicInst::mayLowerToFunctionCall(CalledFn->getIntrinsicID()))
4371 continue;
4372 if (auto Bundle = II->getOperandBundle(LLVMContext::OB_funclet))
4373 FromPad = Bundle->Inputs[0];
4374 else
4375 FromPad = ConstantTokenNone::get(II->getContext());
4376 } else if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) {
4377 FromPad = CRI->getOperand(0);
4378 Check(FromPad != ToPadParent, "A cleanupret must exit its cleanup", CRI);
4379 } else if (auto *CSI = dyn_cast<CatchSwitchInst>(TI)) {
4380 FromPad = CSI;
4381 } else {
4382 Check(false, "EH pad must be jumped to via an unwind edge", ToPad, TI);
4383 }
4384
4385 // The edge may exit from zero or more nested pads.
4387 for (;; FromPad = getParentPad(FromPad)) {
4388 Check(FromPad != ToPad,
4389 "EH pad cannot handle exceptions raised within it", FromPad, TI);
4390 if (FromPad == ToPadParent) {
4391 // This is a legal unwind edge.
4392 break;
4393 }
4394 Check(!isa<ConstantTokenNone>(FromPad),
4395 "A single unwind edge may only enter one EH pad", TI);
4396 Check(Seen.insert(FromPad).second, "EH pad jumps through a cycle of pads",
4397 FromPad);
4398
4399 // This will be diagnosed on the corresponding instruction already. We
4400 // need the extra check here to make sure getParentPad() works.
4401 Check(isa<FuncletPadInst>(FromPad) || isa<CatchSwitchInst>(FromPad),
4402 "Parent pad must be catchpad/cleanuppad/catchswitch", TI);
4403 }
4404 }
4405}
4406
4407void Verifier::visitLandingPadInst(LandingPadInst &LPI) {
4408 // The landingpad instruction is ill-formed if it doesn't have any clauses and
4409 // isn't a cleanup.
4410 Check(LPI.getNumClauses() > 0 || LPI.isCleanup(),
4411 "LandingPadInst needs at least one clause or to be a cleanup.", &LPI);
4412
4413 visitEHPadPredecessors(LPI);
4414
4415 if (!LandingPadResultTy)
4416 LandingPadResultTy = LPI.getType();
4417 else
4418 Check(LandingPadResultTy == LPI.getType(),
4419 "The landingpad instruction should have a consistent result type "
4420 "inside a function.",
4421 &LPI);
4422
4423 Function *F = LPI.getParent()->getParent();
4424 Check(F->hasPersonalityFn(),
4425 "LandingPadInst needs to be in a function with a personality.", &LPI);
4426
4427 // The landingpad instruction must be the first non-PHI instruction in the
4428 // block.
4429 Check(LPI.getParent()->getLandingPadInst() == &LPI,
4430 "LandingPadInst not the first non-PHI instruction in the block.", &LPI);
4431
4432 for (unsigned i = 0, e = LPI.getNumClauses(); i < e; ++i) {
4433 Constant *Clause = LPI.getClause(i);
4434 if (LPI.isCatch(i)) {
4435 Check(isa<PointerType>(Clause->getType()),
4436 "Catch operand does not have pointer type!", &LPI);
4437 } else {
4438 Check(LPI.isFilter(i), "Clause is neither catch nor filter!", &LPI);
4439 Check(isa<ConstantArray>(Clause) || isa<ConstantAggregateZero>(Clause),
4440 "Filter operand is not an array of constants!", &LPI);
4441 }
4442 }
4443
4444 visitInstruction(LPI);
4445}
4446
4447void Verifier::visitResumeInst(ResumeInst &RI) {
4449 "ResumeInst needs to be in a function with a personality.", &RI);
4450
4451 if (!LandingPadResultTy)
4452 LandingPadResultTy = RI.getValue()->getType();
4453 else
4454 Check(LandingPadResultTy == RI.getValue()->getType(),
4455 "The resume instruction should have a consistent result type "
4456 "inside a function.",
4457 &RI);
4458
4459 visitTerminator(RI);
4460}
4461
4462void Verifier::visitCatchPadInst(CatchPadInst &CPI) {
4463 BasicBlock *BB = CPI.getParent();
4464
4465 Function *F = BB->getParent();
4466 Check(F->hasPersonalityFn(),
4467 "CatchPadInst needs to be in a function with a personality.", &CPI);
4468
4469 Check(isa<CatchSwitchInst>(CPI.getParentPad()),
4470 "CatchPadInst needs to be directly nested in a CatchSwitchInst.",
4471 CPI.getParentPad());
4472
4473 // The catchpad instruction must be the first non-PHI instruction in the
4474 // block.
4475 Check(BB->getFirstNonPHI() == &CPI,
4476 "CatchPadInst not the first non-PHI instruction in the block.", &CPI);
4477
4478 visitEHPadPredecessors(CPI);
4480}
4481
4482void Verifier::visitCatchReturnInst(CatchReturnInst &CatchReturn) {
4483 Check(isa<CatchPadInst>(CatchReturn.getOperand(0)),
4484 "CatchReturnInst needs to be provided a CatchPad", &CatchReturn,
4485 CatchReturn.getOperand(0));
4486
4487 visitTerminator(CatchReturn);
4488}
4489
4490void Verifier::visitCleanupPadInst(CleanupPadInst &CPI) {
4491 BasicBlock *BB = CPI.getParent();
4492
4493 Function *F = BB->getParent();
4494 Check(F->hasPersonalityFn(),
4495 "CleanupPadInst needs to be in a function with a personality.", &CPI);
4496
4497 // The cleanuppad instruction must be the first non-PHI instruction in the
4498 // block.
4499 Check(BB->getFirstNonPHI() == &CPI,
4500 "CleanupPadInst not the first non-PHI instruction in the block.", &CPI);
4501
4502 auto *ParentPad = CPI.getParentPad();
4503 Check(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
4504 "CleanupPadInst has an invalid parent.", &CPI);
4505
4506 visitEHPadPredecessors(CPI);
4508}
4509
4510void Verifier::visitFuncletPadInst(FuncletPadInst &FPI) {
4511 User *FirstUser = nullptr;
4512 Value *FirstUnwindPad = nullptr;
4513 SmallVector<FuncletPadInst *, 8> Worklist({&FPI});
4515
4516 while (!Worklist.empty()) {
4517 FuncletPadInst *CurrentPad = Worklist.pop_back_val();
4518 Check(Seen.insert(CurrentPad).second,
4519 "FuncletPadInst must not be nested within itself", CurrentPad);
4520 Value *UnresolvedAncestorPad = nullptr;
4521 for (User *U : CurrentPad->users()) {
4522 BasicBlock *UnwindDest;
4523 if (auto *CRI = dyn_cast<CleanupReturnInst>(U)) {
4524 UnwindDest = CRI->getUnwindDest();
4525 } else if (auto *CSI = dyn_cast<CatchSwitchInst>(U)) {
4526 // We allow catchswitch unwind to caller to nest
4527 // within an outer pad that unwinds somewhere else,
4528 // because catchswitch doesn't have a nounwind variant.
4529 // See e.g. SimplifyCFGOpt::SimplifyUnreachable.
4530 if (CSI->unwindsToCaller())
4531 continue;
4532 UnwindDest = CSI->getUnwindDest();
4533 } else if (auto *II = dyn_cast<InvokeInst>(U)) {
4534 UnwindDest = II->getUnwindDest();
4535 } else if (isa<CallInst>(U)) {
4536 // Calls which don't unwind may be found inside funclet
4537 // pads that unwind somewhere else. We don't *require*
4538 // such calls to be annotated nounwind.
4539 continue;
4540 } else if (auto *CPI = dyn_cast<CleanupPadInst>(U)) {
4541 // The unwind dest for a cleanup can only be found by
4542 // recursive search. Add it to the worklist, and we'll
4543 // search for its first use that determines where it unwinds.
4544 Worklist.push_back(CPI);
4545 continue;
4546 } else {
4547 Check(isa<CatchReturnInst>(U), "Bogus funclet pad use", U);
4548 continue;
4549 }
4550
4551 Value *UnwindPad;
4552 bool ExitsFPI;
4553 if (UnwindDest) {
4554 UnwindPad = UnwindDest->getFirstNonPHI();
4555 if (!cast<Instruction>(UnwindPad)->isEHPad())
4556 continue;
4557 Value *UnwindParent = getParentPad(UnwindPad);
4558 // Ignore unwind edges that don't exit CurrentPad.
4559 if (UnwindParent == CurrentPad)
4560 continue;
4561 // Determine whether the original funclet pad is exited,
4562 // and if we are scanning nested pads determine how many
4563 // of them are exited so we can stop searching their
4564 // children.
4565 Value *ExitedPad = CurrentPad;
4566 ExitsFPI = false;
4567 do {
4568 if (ExitedPad == &FPI) {
4569 ExitsFPI = true;
4570 // Now we can resolve any ancestors of CurrentPad up to
4571 // FPI, but not including FPI since we need to make sure
4572 // to check all direct users of FPI for consistency.
4573 UnresolvedAncestorPad = &FPI;
4574 break;
4575 }
4576 Value *ExitedParent = getParentPad(ExitedPad);
4577 if (ExitedParent == UnwindParent) {
4578 // ExitedPad is the ancestor-most pad which this unwind
4579 // edge exits, so we can resolve up to it, meaning that
4580 // ExitedParent is the first ancestor still unresolved.
4581 UnresolvedAncestorPad = ExitedParent;
4582 break;
4583 }
4584 ExitedPad = ExitedParent;
4585 } while (!isa<ConstantTokenNone>(ExitedPad));
4586 } else {
4587 // Unwinding to caller exits all pads.
4588 UnwindPad = ConstantTokenNone::get(FPI.getContext());
4589 ExitsFPI = true;
4590 UnresolvedAncestorPad = &FPI;
4591 }
4592
4593 if (ExitsFPI) {
4594 // This unwind edge exits FPI. Make sure it agrees with other
4595 // such edges.
4596 if (FirstUser) {
4597 Check(UnwindPad == FirstUnwindPad,
4598 "Unwind edges out of a funclet "
4599 "pad must have the same unwind "
4600 "dest",
4601 &FPI, U, FirstUser);
4602 } else {
4603 FirstUser = U;
4604 FirstUnwindPad = UnwindPad;
4605 // Record cleanup sibling unwinds for verifySiblingFuncletUnwinds
4606 if (isa<CleanupPadInst>(&FPI) && !isa<ConstantTokenNone>(UnwindPad) &&
4607 getParentPad(UnwindPad) == getParentPad(&FPI))
4608 SiblingFuncletInfo[&FPI] = cast<Instruction>(U);
4609 }
4610 }
4611 // Make sure we visit all uses of FPI, but for nested pads stop as
4612 // soon as we know where they unwind to.
4613 if (CurrentPad != &FPI)
4614 break;
4615 }
4616 if (UnresolvedAncestorPad) {
4617 if (CurrentPad == UnresolvedAncestorPad) {
4618 // When CurrentPad is FPI itself, we don't mark it as resolved even if
4619 // we've found an unwind edge that exits it, because we need to verify
4620 // all direct uses of FPI.
4621 assert(CurrentPad == &FPI);
4622 continue;
4623 }
4624 // Pop off the worklist any nested pads that we've found an unwind
4625 // destination for. The pads on the worklist are the uncles,
4626 // great-uncles, etc. of CurrentPad. We've found an unwind destination
4627 // for all ancestors of CurrentPad up to but not including
4628 // UnresolvedAncestorPad.
4629 Value *ResolvedPad = CurrentPad;
4630 while (!Worklist.empty()) {
4631 Value *UnclePad = Worklist.back();
4632 Value *AncestorPad = getParentPad(UnclePad);
4633 // Walk ResolvedPad up the ancestor list until we either find the
4634 // uncle's parent or the last resolved ancestor.
4635 while (ResolvedPad != AncestorPad) {
4636 Value *ResolvedParent = getParentPad(ResolvedPad);
4637 if (ResolvedParent == UnresolvedAncestorPad) {
4638 break;
4639 }
4640 ResolvedPad = ResolvedParent;
4641 }
4642 // If the resolved ancestor search didn't find the uncle's parent,
4643 // then the uncle is not yet resolved.
4644 if (ResolvedPad != AncestorPad)
4645 break;
4646 // This uncle is resolved, so pop it from the worklist.
4647 Worklist.pop_back();
4648 }
4649 }
4650 }
4651
4652 if (FirstUnwindPad) {
4653 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FPI.getParentPad())) {
4654 BasicBlock *SwitchUnwindDest = CatchSwitch->getUnwindDest();
4655 Value *SwitchUnwindPad;
4656 if (SwitchUnwindDest)
4657 SwitchUnwindPad = SwitchUnwindDest->getFirstNonPHI();
4658 else
4659 SwitchUnwindPad = ConstantTokenNone::get(FPI.getContext());
4660 Check(SwitchUnwindPad == FirstUnwindPad,
4661 "Unwind edges out of a catch must have the same unwind dest as "
4662 "the parent catchswitch",
4663 &FPI, FirstUser, CatchSwitch);
4664 }
4665 }
4666
4667 visitInstruction(FPI);
4668}
4669
4670void Verifier::visitCatchSwitchInst(CatchSwitchInst &CatchSwitch) {
4671 BasicBlock *BB = CatchSwitch.getParent();
4672
4673 Function *F = BB->getParent();
4674 Check(