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 //  * PHI nodes must have at least one entry
27 //  * All basic blocks should only end with terminator insts, not contain them
28 //  * The entry node to a function must not have predecessors
29 //  * All Instructions must be embedded into a basic block
30 //  * Functions cannot take a void-typed parameter
31 //  * Verify that a function's argument list agrees with it's declared type.
32 //  * It is illegal to specify a name for a void value.
33 //  * It is illegal to have a internal global value with no initializer
34 //  * It is illegal to have a ret instruction that returns a value that does not
35 //    agree with the function return value type.
36 //  * Function call argument types match the function prototype
37 //  * A landing pad is defined by a landingpad instruction, and can be jumped to
38 //    only by the unwind edge of an invoke instruction.
39 //  * A landingpad instruction must be the first non-PHI instruction in the
40 //    block.
41 //  * Landingpad instructions must be in a function with a personality function.
42 //  * All other things that are tested by asserts spread about the code...
43 //
44 //===----------------------------------------------------------------------===//
45 
46 #include "llvm/IR/Verifier.h"
47 #include "llvm/ADT/APFloat.h"
48 #include "llvm/ADT/APInt.h"
49 #include "llvm/ADT/ArrayRef.h"
50 #include "llvm/ADT/DenseMap.h"
51 #include "llvm/ADT/MapVector.h"
52 #include "llvm/ADT/Optional.h"
53 #include "llvm/ADT/STLExtras.h"
54 #include "llvm/ADT/SmallPtrSet.h"
55 #include "llvm/ADT/SmallSet.h"
56 #include "llvm/ADT/SmallVector.h"
57 #include "llvm/ADT/StringExtras.h"
58 #include "llvm/ADT/StringMap.h"
59 #include "llvm/ADT/StringRef.h"
60 #include "llvm/ADT/Twine.h"
61 #include "llvm/BinaryFormat/Dwarf.h"
62 #include "llvm/IR/Argument.h"
63 #include "llvm/IR/Attributes.h"
64 #include "llvm/IR/BasicBlock.h"
65 #include "llvm/IR/CFG.h"
66 #include "llvm/IR/CallingConv.h"
67 #include "llvm/IR/Comdat.h"
68 #include "llvm/IR/Constant.h"
69 #include "llvm/IR/ConstantRange.h"
70 #include "llvm/IR/Constants.h"
71 #include "llvm/IR/DataLayout.h"
72 #include "llvm/IR/DebugInfoMetadata.h"
73 #include "llvm/IR/DebugLoc.h"
74 #include "llvm/IR/DerivedTypes.h"
75 #include "llvm/IR/Dominators.h"
76 #include "llvm/IR/Function.h"
77 #include "llvm/IR/GlobalAlias.h"
78 #include "llvm/IR/GlobalValue.h"
79 #include "llvm/IR/GlobalVariable.h"
80 #include "llvm/IR/InlineAsm.h"
81 #include "llvm/IR/InstVisitor.h"
82 #include "llvm/IR/InstrTypes.h"
83 #include "llvm/IR/Instruction.h"
84 #include "llvm/IR/Instructions.h"
85 #include "llvm/IR/IntrinsicInst.h"
86 #include "llvm/IR/Intrinsics.h"
87 #include "llvm/IR/IntrinsicsAArch64.h"
88 #include "llvm/IR/IntrinsicsARM.h"
89 #include "llvm/IR/IntrinsicsWebAssembly.h"
90 #include "llvm/IR/LLVMContext.h"
91 #include "llvm/IR/Metadata.h"
92 #include "llvm/IR/Module.h"
93 #include "llvm/IR/ModuleSlotTracker.h"
94 #include "llvm/IR/PassManager.h"
95 #include "llvm/IR/Statepoint.h"
96 #include "llvm/IR/Type.h"
97 #include "llvm/IR/Use.h"
98 #include "llvm/IR/User.h"
99 #include "llvm/IR/Value.h"
100 #include "llvm/InitializePasses.h"
101 #include "llvm/Pass.h"
102 #include "llvm/Support/AtomicOrdering.h"
103 #include "llvm/Support/Casting.h"
104 #include "llvm/Support/CommandLine.h"
105 #include "llvm/Support/ErrorHandling.h"
106 #include "llvm/Support/MathExtras.h"
107 #include "llvm/Support/raw_ostream.h"
108 #include <algorithm>
109 #include <cassert>
110 #include <cstdint>
111 #include <memory>
112 #include <string>
113 #include <utility>
114 
115 using namespace llvm;
116 
117 static cl::opt<bool> VerifyNoAliasScopeDomination(
118     "verify-noalias-scope-decl-dom", cl::Hidden, cl::init(false),
119     cl::desc("Ensure that llvm.experimental.noalias.scope.decl for identical "
120              "scopes are not dominating"));
121 
122 namespace llvm {
123 
124 struct VerifierSupport {
125   raw_ostream *OS;
126   const Module &M;
127   ModuleSlotTracker MST;
128   Triple TT;
129   const DataLayout &DL;
130   LLVMContext &Context;
131 
132   /// Track the brokenness of the module while recursively visiting.
133   bool Broken = false;
134   /// Broken debug info can be "recovered" from by stripping the debug info.
135   bool BrokenDebugInfo = false;
136   /// Whether to treat broken debug info as an error.
137   bool TreatBrokenDebugInfoAsError = true;
138 
139   explicit VerifierSupport(raw_ostream *OS, const Module &M)
140       : OS(OS), M(M), MST(&M), TT(M.getTargetTriple()), DL(M.getDataLayout()),
141         Context(M.getContext()) {}
142 
143 private:
144   void Write(const Module *M) {
145     *OS << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
146   }
147 
148   void Write(const Value *V) {
149     if (V)
150       Write(*V);
151   }
152 
153   void Write(const Value &V) {
154     if (isa<Instruction>(V)) {
155       V.print(*OS, MST);
156       *OS << '\n';
157     } else {
158       V.printAsOperand(*OS, true, MST);
159       *OS << '\n';
160     }
161   }
162 
163   void Write(const Metadata *MD) {
164     if (!MD)
165       return;
166     MD->print(*OS, MST, &M);
167     *OS << '\n';
168   }
169 
170   template <class T> void Write(const MDTupleTypedArrayWrapper<T> &MD) {
171     Write(MD.get());
172   }
173 
174   void Write(const NamedMDNode *NMD) {
175     if (!NMD)
176       return;
177     NMD->print(*OS, MST);
178     *OS << '\n';
179   }
180 
181   void Write(Type *T) {
182     if (!T)
183       return;
184     *OS << ' ' << *T;
185   }
186 
187   void Write(const Comdat *C) {
188     if (!C)
189       return;
190     *OS << *C;
191   }
192 
193   void Write(const APInt *AI) {
194     if (!AI)
195       return;
196     *OS << *AI << '\n';
197   }
198 
199   void Write(const unsigned i) { *OS << i << '\n'; }
200 
201   // NOLINTNEXTLINE(readability-identifier-naming)
202   void Write(const Attribute *A) {
203     if (!A)
204       return;
205     *OS << A->getAsString() << '\n';
206   }
207 
208   // NOLINTNEXTLINE(readability-identifier-naming)
209   void Write(const AttributeSet *AS) {
210     if (!AS)
211       return;
212     *OS << AS->getAsString() << '\n';
213   }
214 
215   // NOLINTNEXTLINE(readability-identifier-naming)
216   void Write(const AttributeList *AL) {
217     if (!AL)
218       return;
219     AL->print(*OS);
220   }
221 
222   template <typename T> void Write(ArrayRef<T> Vs) {
223     for (const T &V : Vs)
224       Write(V);
225   }
226 
227   template <typename T1, typename... Ts>
228   void WriteTs(const T1 &V1, const Ts &... Vs) {
229     Write(V1);
230     WriteTs(Vs...);
231   }
232 
233   template <typename... Ts> void WriteTs() {}
234 
235 public:
236   /// A check failed, so printout out the condition and the message.
237   ///
238   /// This provides a nice place to put a breakpoint if you want to see why
239   /// something is not correct.
240   void CheckFailed(const Twine &Message) {
241     if (OS)
242       *OS << Message << '\n';
243     Broken = true;
244   }
245 
246   /// A check failed (with values to print).
247   ///
248   /// This calls the Message-only version so that the above is easier to set a
249   /// breakpoint on.
250   template <typename T1, typename... Ts>
251   void CheckFailed(const Twine &Message, const T1 &V1, const Ts &... Vs) {
252     CheckFailed(Message);
253     if (OS)
254       WriteTs(V1, Vs...);
255   }
256 
257   /// A debug info check failed.
258   void DebugInfoCheckFailed(const Twine &Message) {
259     if (OS)
260       *OS << Message << '\n';
261     Broken |= TreatBrokenDebugInfoAsError;
262     BrokenDebugInfo = true;
263   }
264 
265   /// A debug info check failed (with values to print).
266   template <typename T1, typename... Ts>
267   void DebugInfoCheckFailed(const Twine &Message, const T1 &V1,
268                             const Ts &... Vs) {
269     DebugInfoCheckFailed(Message);
270     if (OS)
271       WriteTs(V1, Vs...);
272   }
273 };
274 
275 } // namespace llvm
276 
277 namespace {
278 
279 class Verifier : public InstVisitor<Verifier>, VerifierSupport {
280   friend class InstVisitor<Verifier>;
281 
282   // ISD::ArgFlagsTy::MemAlign only have 4 bits for alignment, so
283   // the alignment size should not exceed 2^15. Since encode(Align)
284   // would plus the shift value by 1, the alignment size should
285   // not exceed 2^14, otherwise it can NOT be properly lowered
286   // in backend.
287   static constexpr unsigned ParamMaxAlignment = 1 << 14;
288   DominatorTree DT;
289 
290   /// When verifying a basic block, keep track of all of the
291   /// instructions we have seen so far.
292   ///
293   /// This allows us to do efficient dominance checks for the case when an
294   /// instruction has an operand that is an instruction in the same block.
295   SmallPtrSet<Instruction *, 16> InstsInThisBlock;
296 
297   /// Keep track of the metadata nodes that have been checked already.
298   SmallPtrSet<const Metadata *, 32> MDNodes;
299 
300   /// Keep track which DISubprogram is attached to which function.
301   DenseMap<const DISubprogram *, const Function *> DISubprogramAttachments;
302 
303   /// Track all DICompileUnits visited.
304   SmallPtrSet<const Metadata *, 2> CUVisited;
305 
306   /// The result type for a landingpad.
307   Type *LandingPadResultTy;
308 
309   /// Whether we've seen a call to @llvm.localescape in this function
310   /// already.
311   bool SawFrameEscape;
312 
313   /// Whether the current function has a DISubprogram attached to it.
314   bool HasDebugInfo = false;
315 
316   /// The current source language.
317   dwarf::SourceLanguage CurrentSourceLang = dwarf::DW_LANG_lo_user;
318 
319   /// Whether source was present on the first DIFile encountered in each CU.
320   DenseMap<const DICompileUnit *, bool> HasSourceDebugInfo;
321 
322   /// Stores the count of how many objects were passed to llvm.localescape for a
323   /// given function and the largest index passed to llvm.localrecover.
324   DenseMap<Function *, std::pair<unsigned, unsigned>> FrameEscapeInfo;
325 
326   // Maps catchswitches and cleanuppads that unwind to siblings to the
327   // terminators that indicate the unwind, used to detect cycles therein.
328   MapVector<Instruction *, Instruction *> SiblingFuncletInfo;
329 
330   /// Cache of constants visited in search of ConstantExprs.
331   SmallPtrSet<const Constant *, 32> ConstantExprVisited;
332 
333   /// Cache of declarations of the llvm.experimental.deoptimize.<ty> intrinsic.
334   SmallVector<const Function *, 4> DeoptimizeDeclarations;
335 
336   /// Cache of attribute lists verified.
337   SmallPtrSet<const void *, 32> AttributeListsVisited;
338 
339   // Verify that this GlobalValue is only used in this module.
340   // This map is used to avoid visiting uses twice. We can arrive at a user
341   // twice, if they have multiple operands. In particular for very large
342   // constant expressions, we can arrive at a particular user many times.
343   SmallPtrSet<const Value *, 32> GlobalValueVisited;
344 
345   // Keeps track of duplicate function argument debug info.
346   SmallVector<const DILocalVariable *, 16> DebugFnArgs;
347 
348   TBAAVerifier TBAAVerifyHelper;
349 
350   SmallVector<IntrinsicInst *, 4> NoAliasScopeDecls;
351 
352   void checkAtomicMemAccessSize(Type *Ty, const Instruction *I);
353 
354 public:
355   explicit Verifier(raw_ostream *OS, bool ShouldTreatBrokenDebugInfoAsError,
356                     const Module &M)
357       : VerifierSupport(OS, M), LandingPadResultTy(nullptr),
358         SawFrameEscape(false), TBAAVerifyHelper(this) {
359     TreatBrokenDebugInfoAsError = ShouldTreatBrokenDebugInfoAsError;
360   }
361 
362   bool hasBrokenDebugInfo() const { return BrokenDebugInfo; }
363 
364   bool verify(const Function &F) {
365     assert(F.getParent() == &M &&
366            "An instance of this class only works with a specific module!");
367 
368     // First ensure the function is well-enough formed to compute dominance
369     // information, and directly compute a dominance tree. We don't rely on the
370     // pass manager to provide this as it isolates us from a potentially
371     // out-of-date dominator tree and makes it significantly more complex to run
372     // this code outside of a pass manager.
373     // FIXME: It's really gross that we have to cast away constness here.
374     if (!F.empty())
375       DT.recalculate(const_cast<Function &>(F));
376 
377     for (const BasicBlock &BB : F) {
378       if (!BB.empty() && BB.back().isTerminator())
379         continue;
380 
381       if (OS) {
382         *OS << "Basic Block in function '" << F.getName()
383             << "' does not have terminator!\n";
384         BB.printAsOperand(*OS, true, MST);
385         *OS << "\n";
386       }
387       return false;
388     }
389 
390     Broken = false;
391     // FIXME: We strip const here because the inst visitor strips const.
392     visit(const_cast<Function &>(F));
393     verifySiblingFuncletUnwinds();
394     InstsInThisBlock.clear();
395     DebugFnArgs.clear();
396     LandingPadResultTy = nullptr;
397     SawFrameEscape = false;
398     SiblingFuncletInfo.clear();
399     verifyNoAliasScopeDecl();
400     NoAliasScopeDecls.clear();
401 
402     return !Broken;
403   }
404 
405   /// Verify the module that this instance of \c Verifier was initialized with.
406   bool verify() {
407     Broken = false;
408 
409     // Collect all declarations of the llvm.experimental.deoptimize intrinsic.
410     for (const Function &F : M)
411       if (F.getIntrinsicID() == Intrinsic::experimental_deoptimize)
412         DeoptimizeDeclarations.push_back(&F);
413 
414     // Now that we've visited every function, verify that we never asked to
415     // recover a frame index that wasn't escaped.
416     verifyFrameRecoverIndices();
417     for (const GlobalVariable &GV : M.globals())
418       visitGlobalVariable(GV);
419 
420     for (const GlobalAlias &GA : M.aliases())
421       visitGlobalAlias(GA);
422 
423     for (const GlobalIFunc &GI : M.ifuncs())
424       visitGlobalIFunc(GI);
425 
426     for (const NamedMDNode &NMD : M.named_metadata())
427       visitNamedMDNode(NMD);
428 
429     for (const StringMapEntry<Comdat> &SMEC : M.getComdatSymbolTable())
430       visitComdat(SMEC.getValue());
431 
432     visitModuleFlags();
433     visitModuleIdents();
434     visitModuleCommandLines();
435 
436     verifyCompileUnits();
437 
438     verifyDeoptimizeCallingConvs();
439     DISubprogramAttachments.clear();
440     return !Broken;
441   }
442 
443 private:
444   /// Whether a metadata node is allowed to be, or contain, a DILocation.
445   enum class AreDebugLocsAllowed { No, Yes };
446 
447   // Verification methods...
448   void visitGlobalValue(const GlobalValue &GV);
449   void visitGlobalVariable(const GlobalVariable &GV);
450   void visitGlobalAlias(const GlobalAlias &GA);
451   void visitGlobalIFunc(const GlobalIFunc &GI);
452   void visitAliaseeSubExpr(const GlobalAlias &A, const Constant &C);
453   void visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias *> &Visited,
454                            const GlobalAlias &A, const Constant &C);
455   void visitNamedMDNode(const NamedMDNode &NMD);
456   void visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs);
457   void visitMetadataAsValue(const MetadataAsValue &MD, Function *F);
458   void visitValueAsMetadata(const ValueAsMetadata &MD, Function *F);
459   void visitComdat(const Comdat &C);
460   void visitModuleIdents();
461   void visitModuleCommandLines();
462   void visitModuleFlags();
463   void visitModuleFlag(const MDNode *Op,
464                        DenseMap<const MDString *, const MDNode *> &SeenIDs,
465                        SmallVectorImpl<const MDNode *> &Requirements);
466   void visitModuleFlagCGProfileEntry(const MDOperand &MDO);
467   void visitFunction(const Function &F);
468   void visitBasicBlock(BasicBlock &BB);
469   void visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty);
470   void visitDereferenceableMetadata(Instruction &I, MDNode *MD);
471   void visitProfMetadata(Instruction &I, MDNode *MD);
472   void visitAnnotationMetadata(MDNode *Annotation);
473   void visitAliasScopeMetadata(const MDNode *MD);
474   void visitAliasScopeListMetadata(const MDNode *MD);
475   void visitAccessGroupMetadata(const MDNode *MD);
476 
477   template <class Ty> bool isValidMetadataArray(const MDTuple &N);
478 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N);
479 #include "llvm/IR/Metadata.def"
480   void visitDIScope(const DIScope &N);
481   void visitDIVariable(const DIVariable &N);
482   void visitDILexicalBlockBase(const DILexicalBlockBase &N);
483   void visitDITemplateParameter(const DITemplateParameter &N);
484 
485   void visitTemplateParams(const MDNode &N, const Metadata &RawParams);
486 
487   // InstVisitor overrides...
488   using InstVisitor<Verifier>::visit;
489   void visit(Instruction &I);
490 
491   void visitTruncInst(TruncInst &I);
492   void visitZExtInst(ZExtInst &I);
493   void visitSExtInst(SExtInst &I);
494   void visitFPTruncInst(FPTruncInst &I);
495   void visitFPExtInst(FPExtInst &I);
496   void visitFPToUIInst(FPToUIInst &I);
497   void visitFPToSIInst(FPToSIInst &I);
498   void visitUIToFPInst(UIToFPInst &I);
499   void visitSIToFPInst(SIToFPInst &I);
500   void visitIntToPtrInst(IntToPtrInst &I);
501   void visitPtrToIntInst(PtrToIntInst &I);
502   void visitBitCastInst(BitCastInst &I);
503   void visitAddrSpaceCastInst(AddrSpaceCastInst &I);
504   void visitPHINode(PHINode &PN);
505   void visitCallBase(CallBase &Call);
506   void visitUnaryOperator(UnaryOperator &U);
507   void visitBinaryOperator(BinaryOperator &B);
508   void visitICmpInst(ICmpInst &IC);
509   void visitFCmpInst(FCmpInst &FC);
510   void visitExtractElementInst(ExtractElementInst &EI);
511   void visitInsertElementInst(InsertElementInst &EI);
512   void visitShuffleVectorInst(ShuffleVectorInst &EI);
513   void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
514   void visitCallInst(CallInst &CI);
515   void visitInvokeInst(InvokeInst &II);
516   void visitGetElementPtrInst(GetElementPtrInst &GEP);
517   void visitLoadInst(LoadInst &LI);
518   void visitStoreInst(StoreInst &SI);
519   void verifyDominatesUse(Instruction &I, unsigned i);
520   void visitInstruction(Instruction &I);
521   void visitTerminator(Instruction &I);
522   void visitBranchInst(BranchInst &BI);
523   void visitReturnInst(ReturnInst &RI);
524   void visitSwitchInst(SwitchInst &SI);
525   void visitIndirectBrInst(IndirectBrInst &BI);
526   void visitCallBrInst(CallBrInst &CBI);
527   void visitSelectInst(SelectInst &SI);
528   void visitUserOp1(Instruction &I);
529   void visitUserOp2(Instruction &I) { visitUserOp1(I); }
530   void visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call);
531   void visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI);
532   void visitVPIntrinsic(VPIntrinsic &VPI);
533   void visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII);
534   void visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI);
535   void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI);
536   void visitAtomicRMWInst(AtomicRMWInst &RMWI);
537   void visitFenceInst(FenceInst &FI);
538   void visitAllocaInst(AllocaInst &AI);
539   void visitExtractValueInst(ExtractValueInst &EVI);
540   void visitInsertValueInst(InsertValueInst &IVI);
541   void visitEHPadPredecessors(Instruction &I);
542   void visitLandingPadInst(LandingPadInst &LPI);
543   void visitResumeInst(ResumeInst &RI);
544   void visitCatchPadInst(CatchPadInst &CPI);
545   void visitCatchReturnInst(CatchReturnInst &CatchReturn);
546   void visitCleanupPadInst(CleanupPadInst &CPI);
547   void visitFuncletPadInst(FuncletPadInst &FPI);
548   void visitCatchSwitchInst(CatchSwitchInst &CatchSwitch);
549   void visitCleanupReturnInst(CleanupReturnInst &CRI);
550 
551   void verifySwiftErrorCall(CallBase &Call, const Value *SwiftErrorVal);
552   void verifySwiftErrorValue(const Value *SwiftErrorVal);
553   void verifyTailCCMustTailAttrs(const AttrBuilder &Attrs, StringRef Context);
554   void verifyMustTailCall(CallInst &CI);
555   bool verifyAttributeCount(AttributeList Attrs, unsigned Params);
556   void verifyAttributeTypes(AttributeSet Attrs, const Value *V);
557   void verifyParameterAttrs(AttributeSet Attrs, Type *Ty, const Value *V);
558   void checkUnsignedBaseTenFuncAttr(AttributeList Attrs, StringRef Attr,
559                                     const Value *V);
560   void verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
561                            const Value *V, bool IsIntrinsic, bool IsInlineAsm);
562   void verifyFunctionMetadata(ArrayRef<std::pair<unsigned, MDNode *>> MDs);
563 
564   void visitConstantExprsRecursively(const Constant *EntryC);
565   void visitConstantExpr(const ConstantExpr *CE);
566   void verifyInlineAsmCall(const CallBase &Call);
567   void verifyStatepoint(const CallBase &Call);
568   void verifyFrameRecoverIndices();
569   void verifySiblingFuncletUnwinds();
570 
571   void verifyFragmentExpression(const DbgVariableIntrinsic &I);
572   template <typename ValueOrMetadata>
573   void verifyFragmentExpression(const DIVariable &V,
574                                 DIExpression::FragmentInfo Fragment,
575                                 ValueOrMetadata *Desc);
576   void verifyFnArgs(const DbgVariableIntrinsic &I);
577   void verifyNotEntryValue(const DbgVariableIntrinsic &I);
578 
579   /// Module-level debug info verification...
580   void verifyCompileUnits();
581 
582   /// Module-level verification that all @llvm.experimental.deoptimize
583   /// declarations share the same calling convention.
584   void verifyDeoptimizeCallingConvs();
585 
586   void verifyAttachedCallBundle(const CallBase &Call,
587                                 const OperandBundleUse &BU);
588 
589   /// Verify all-or-nothing property of DIFile source attribute within a CU.
590   void verifySourceDebugInfo(const DICompileUnit &U, const DIFile &F);
591 
592   /// Verify the llvm.experimental.noalias.scope.decl declarations
593   void verifyNoAliasScopeDecl();
594 };
595 
596 } // end anonymous namespace
597 
598 /// We know that cond should be true, if not print an error message.
599 #define Check(C, ...)                                                          \
600   do {                                                                         \
601     if (!(C)) {                                                                \
602       CheckFailed(__VA_ARGS__);                                                \
603       return;                                                                  \
604     }                                                                          \
605   } while (false)
606 
607 /// We know that a debug info condition should be true, if not print
608 /// an error message.
609 #define CheckDI(C, ...)                                                        \
610   do {                                                                         \
611     if (!(C)) {                                                                \
612       DebugInfoCheckFailed(__VA_ARGS__);                                       \
613       return;                                                                  \
614     }                                                                          \
615   } while (false)
616 
617 void Verifier::visit(Instruction &I) {
618   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
619     Check(I.getOperand(i) != nullptr, "Operand is null", &I);
620   InstVisitor<Verifier>::visit(I);
621 }
622 
623 // Helper to iterate over indirect users. By returning false, the callback can ask to stop traversing further.
624 static void forEachUser(const Value *User,
625                         SmallPtrSet<const Value *, 32> &Visited,
626                         llvm::function_ref<bool(const Value *)> Callback) {
627   if (!Visited.insert(User).second)
628     return;
629 
630   SmallVector<const Value *> WorkList;
631   append_range(WorkList, User->materialized_users());
632   while (!WorkList.empty()) {
633    const Value *Cur = WorkList.pop_back_val();
634     if (!Visited.insert(Cur).second)
635       continue;
636     if (Callback(Cur))
637       append_range(WorkList, Cur->materialized_users());
638   }
639 }
640 
641 void Verifier::visitGlobalValue(const GlobalValue &GV) {
642   Check(!GV.isDeclaration() || GV.hasValidDeclarationLinkage(),
643         "Global is external, but doesn't have external or weak linkage!", &GV);
644 
645   if (const GlobalObject *GO = dyn_cast<GlobalObject>(&GV)) {
646 
647     if (MaybeAlign A = GO->getAlign()) {
648       Check(A->value() <= Value::MaximumAlignment,
649             "huge alignment values are unsupported", GO);
650     }
651   }
652   Check(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
653         "Only global variables can have appending linkage!", &GV);
654 
655   if (GV.hasAppendingLinkage()) {
656     const GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV);
657     Check(GVar && GVar->getValueType()->isArrayTy(),
658           "Only global arrays can have appending linkage!", GVar);
659   }
660 
661   if (GV.isDeclarationForLinker())
662     Check(!GV.hasComdat(), "Declaration may not be in a Comdat!", &GV);
663 
664   if (GV.hasDLLImportStorageClass()) {
665     Check(!GV.isDSOLocal(), "GlobalValue with DLLImport Storage is dso_local!",
666           &GV);
667 
668     Check((GV.isDeclaration() &&
669            (GV.hasExternalLinkage() || GV.hasExternalWeakLinkage())) ||
670               GV.hasAvailableExternallyLinkage(),
671           "Global is marked as dllimport, but not external", &GV);
672   }
673 
674   if (GV.isImplicitDSOLocal())
675     Check(GV.isDSOLocal(),
676           "GlobalValue with local linkage or non-default "
677           "visibility must be dso_local!",
678           &GV);
679 
680   forEachUser(&GV, GlobalValueVisited, [&](const Value *V) -> bool {
681     if (const Instruction *I = dyn_cast<Instruction>(V)) {
682       if (!I->getParent() || !I->getParent()->getParent())
683         CheckFailed("Global is referenced by parentless instruction!", &GV, &M,
684                     I);
685       else if (I->getParent()->getParent()->getParent() != &M)
686         CheckFailed("Global is referenced in a different module!", &GV, &M, I,
687                     I->getParent()->getParent(),
688                     I->getParent()->getParent()->getParent());
689       return false;
690     } else if (const Function *F = dyn_cast<Function>(V)) {
691       if (F->getParent() != &M)
692         CheckFailed("Global is used by function in a different module", &GV, &M,
693                     F, F->getParent());
694       return false;
695     }
696     return true;
697   });
698 }
699 
700 void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
701   if (GV.hasInitializer()) {
702     Check(GV.getInitializer()->getType() == GV.getValueType(),
703           "Global variable initializer type does not match global "
704           "variable type!",
705           &GV);
706     // If the global has common linkage, it must have a zero initializer and
707     // cannot be constant.
708     if (GV.hasCommonLinkage()) {
709       Check(GV.getInitializer()->isNullValue(),
710             "'common' global must have a zero initializer!", &GV);
711       Check(!GV.isConstant(), "'common' global may not be marked constant!",
712             &GV);
713       Check(!GV.hasComdat(), "'common' global may not be in a Comdat!", &GV);
714     }
715   }
716 
717   if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
718                        GV.getName() == "llvm.global_dtors")) {
719     Check(!GV.hasInitializer() || GV.hasAppendingLinkage(),
720           "invalid linkage for intrinsic global variable", &GV);
721     // Don't worry about emitting an error for it not being an array,
722     // visitGlobalValue will complain on appending non-array.
723     if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getValueType())) {
724       StructType *STy = dyn_cast<StructType>(ATy->getElementType());
725       PointerType *FuncPtrTy =
726           FunctionType::get(Type::getVoidTy(Context), false)->
727           getPointerTo(DL.getProgramAddressSpace());
728       Check(STy && (STy->getNumElements() == 2 || STy->getNumElements() == 3) &&
729                 STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
730                 STy->getTypeAtIndex(1) == FuncPtrTy,
731             "wrong type for intrinsic global variable", &GV);
732       Check(STy->getNumElements() == 3,
733             "the third field of the element type is mandatory, "
734             "specify i8* null to migrate from the obsoleted 2-field form");
735       Type *ETy = STy->getTypeAtIndex(2);
736       Type *Int8Ty = Type::getInt8Ty(ETy->getContext());
737       Check(ETy->isPointerTy() &&
738                 cast<PointerType>(ETy)->isOpaqueOrPointeeTypeMatches(Int8Ty),
739             "wrong type for intrinsic global variable", &GV);
740     }
741   }
742 
743   if (GV.hasName() && (GV.getName() == "llvm.used" ||
744                        GV.getName() == "llvm.compiler.used")) {
745     Check(!GV.hasInitializer() || GV.hasAppendingLinkage(),
746           "invalid linkage for intrinsic global variable", &GV);
747     Type *GVType = GV.getValueType();
748     if (ArrayType *ATy = dyn_cast<ArrayType>(GVType)) {
749       PointerType *PTy = dyn_cast<PointerType>(ATy->getElementType());
750       Check(PTy, "wrong type for intrinsic global variable", &GV);
751       if (GV.hasInitializer()) {
752         const Constant *Init = GV.getInitializer();
753         const ConstantArray *InitArray = dyn_cast<ConstantArray>(Init);
754         Check(InitArray, "wrong initalizer for intrinsic global variable",
755               Init);
756         for (Value *Op : InitArray->operands()) {
757           Value *V = Op->stripPointerCasts();
758           Check(isa<GlobalVariable>(V) || isa<Function>(V) ||
759                     isa<GlobalAlias>(V),
760                 Twine("invalid ") + GV.getName() + " member", V);
761           Check(V->hasName(),
762                 Twine("members of ") + GV.getName() + " must be named", V);
763         }
764       }
765     }
766   }
767 
768   // Visit any debug info attachments.
769   SmallVector<MDNode *, 1> MDs;
770   GV.getMetadata(LLVMContext::MD_dbg, MDs);
771   for (auto *MD : MDs) {
772     if (auto *GVE = dyn_cast<DIGlobalVariableExpression>(MD))
773       visitDIGlobalVariableExpression(*GVE);
774     else
775       CheckDI(false, "!dbg attachment of global variable must be a "
776                      "DIGlobalVariableExpression");
777   }
778 
779   // Scalable vectors cannot be global variables, since we don't know
780   // the runtime size. If the global is an array containing scalable vectors,
781   // that will be caught by the isValidElementType methods in StructType or
782   // ArrayType instead.
783   Check(!isa<ScalableVectorType>(GV.getValueType()),
784         "Globals cannot contain scalable vectors", &GV);
785 
786   if (auto *STy = dyn_cast<StructType>(GV.getValueType()))
787     Check(!STy->containsScalableVectorType(),
788           "Globals cannot contain scalable vectors", &GV);
789 
790   if (!GV.hasInitializer()) {
791     visitGlobalValue(GV);
792     return;
793   }
794 
795   // Walk any aggregate initializers looking for bitcasts between address spaces
796   visitConstantExprsRecursively(GV.getInitializer());
797 
798   visitGlobalValue(GV);
799 }
800 
801 void Verifier::visitAliaseeSubExpr(const GlobalAlias &GA, const Constant &C) {
802   SmallPtrSet<const GlobalAlias*, 4> Visited;
803   Visited.insert(&GA);
804   visitAliaseeSubExpr(Visited, GA, C);
805 }
806 
807 void Verifier::visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias*> &Visited,
808                                    const GlobalAlias &GA, const Constant &C) {
809   if (const auto *GV = dyn_cast<GlobalValue>(&C)) {
810     Check(!GV->isDeclarationForLinker(), "Alias must point to a definition",
811           &GA);
812 
813     if (const auto *GA2 = dyn_cast<GlobalAlias>(GV)) {
814       Check(Visited.insert(GA2).second, "Aliases cannot form a cycle", &GA);
815 
816       Check(!GA2->isInterposable(),
817             "Alias cannot point to an interposable alias", &GA);
818     } else {
819       // Only continue verifying subexpressions of GlobalAliases.
820       // Do not recurse into global initializers.
821       return;
822     }
823   }
824 
825   if (const auto *CE = dyn_cast<ConstantExpr>(&C))
826     visitConstantExprsRecursively(CE);
827 
828   for (const Use &U : C.operands()) {
829     Value *V = &*U;
830     if (const auto *GA2 = dyn_cast<GlobalAlias>(V))
831       visitAliaseeSubExpr(Visited, GA, *GA2->getAliasee());
832     else if (const auto *C2 = dyn_cast<Constant>(V))
833       visitAliaseeSubExpr(Visited, GA, *C2);
834   }
835 }
836 
837 void Verifier::visitGlobalAlias(const GlobalAlias &GA) {
838   Check(GlobalAlias::isValidLinkage(GA.getLinkage()),
839         "Alias should have private, internal, linkonce, weak, linkonce_odr, "
840         "weak_odr, or external linkage!",
841         &GA);
842   const Constant *Aliasee = GA.getAliasee();
843   Check(Aliasee, "Aliasee cannot be NULL!", &GA);
844   Check(GA.getType() == Aliasee->getType(),
845         "Alias and aliasee types should match!", &GA);
846 
847   Check(isa<GlobalValue>(Aliasee) || isa<ConstantExpr>(Aliasee),
848         "Aliasee should be either GlobalValue or ConstantExpr", &GA);
849 
850   visitAliaseeSubExpr(GA, *Aliasee);
851 
852   visitGlobalValue(GA);
853 }
854 
855 void Verifier::visitGlobalIFunc(const GlobalIFunc &GI) {
856   Check(GlobalIFunc::isValidLinkage(GI.getLinkage()),
857         "IFunc should have private, internal, linkonce, weak, linkonce_odr, "
858         "weak_odr, or external linkage!",
859         &GI);
860   // Pierce through ConstantExprs and GlobalAliases and check that the resolver
861   // is a Function definition.
862   const Function *Resolver = GI.getResolverFunction();
863   Check(Resolver, "IFunc must have a Function resolver", &GI);
864   Check(!Resolver->isDeclarationForLinker(),
865         "IFunc resolver must be a definition", &GI);
866 
867   // Check that the immediate resolver operand (prior to any bitcasts) has the
868   // correct type.
869   const Type *ResolverTy = GI.getResolver()->getType();
870   const Type *ResolverFuncTy =
871       GlobalIFunc::getResolverFunctionType(GI.getValueType());
872   Check(ResolverTy == ResolverFuncTy->getPointerTo(),
873         "IFunc resolver has incorrect type", &GI);
874 }
875 
876 void Verifier::visitNamedMDNode(const NamedMDNode &NMD) {
877   // There used to be various other llvm.dbg.* nodes, but we don't support
878   // upgrading them and we want to reserve the namespace for future uses.
879   if (NMD.getName().startswith("llvm.dbg."))
880     CheckDI(NMD.getName() == "llvm.dbg.cu",
881             "unrecognized named metadata node in the llvm.dbg namespace", &NMD);
882   for (const MDNode *MD : NMD.operands()) {
883     if (NMD.getName() == "llvm.dbg.cu")
884       CheckDI(MD && isa<DICompileUnit>(MD), "invalid compile unit", &NMD, MD);
885 
886     if (!MD)
887       continue;
888 
889     visitMDNode(*MD, AreDebugLocsAllowed::Yes);
890   }
891 }
892 
893 void Verifier::visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs) {
894   // Only visit each node once.  Metadata can be mutually recursive, so this
895   // avoids infinite recursion here, as well as being an optimization.
896   if (!MDNodes.insert(&MD).second)
897     return;
898 
899   Check(&MD.getContext() == &Context,
900         "MDNode context does not match Module context!", &MD);
901 
902   switch (MD.getMetadataID()) {
903   default:
904     llvm_unreachable("Invalid MDNode subclass");
905   case Metadata::MDTupleKind:
906     break;
907 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS)                                  \
908   case Metadata::CLASS##Kind:                                                  \
909     visit##CLASS(cast<CLASS>(MD));                                             \
910     break;
911 #include "llvm/IR/Metadata.def"
912   }
913 
914   for (const Metadata *Op : MD.operands()) {
915     if (!Op)
916       continue;
917     Check(!isa<LocalAsMetadata>(Op), "Invalid operand for global metadata!",
918           &MD, Op);
919     CheckDI(!isa<DILocation>(Op) || AllowLocs == AreDebugLocsAllowed::Yes,
920             "DILocation not allowed within this metadata node", &MD, Op);
921     if (auto *N = dyn_cast<MDNode>(Op)) {
922       visitMDNode(*N, AllowLocs);
923       continue;
924     }
925     if (auto *V = dyn_cast<ValueAsMetadata>(Op)) {
926       visitValueAsMetadata(*V, nullptr);
927       continue;
928     }
929   }
930 
931   // Check these last, so we diagnose problems in operands first.
932   Check(!MD.isTemporary(), "Expected no forward declarations!", &MD);
933   Check(MD.isResolved(), "All nodes should be resolved!", &MD);
934 }
935 
936 void Verifier::visitValueAsMetadata(const ValueAsMetadata &MD, Function *F) {
937   Check(MD.getValue(), "Expected valid value", &MD);
938   Check(!MD.getValue()->getType()->isMetadataTy(),
939         "Unexpected metadata round-trip through values", &MD, MD.getValue());
940 
941   auto *L = dyn_cast<LocalAsMetadata>(&MD);
942   if (!L)
943     return;
944 
945   Check(F, "function-local metadata used outside a function", L);
946 
947   // If this was an instruction, bb, or argument, verify that it is in the
948   // function that we expect.
949   Function *ActualF = nullptr;
950   if (Instruction *I = dyn_cast<Instruction>(L->getValue())) {
951     Check(I->getParent(), "function-local metadata not in basic block", L, I);
952     ActualF = I->getParent()->getParent();
953   } else if (BasicBlock *BB = dyn_cast<BasicBlock>(L->getValue()))
954     ActualF = BB->getParent();
955   else if (Argument *A = dyn_cast<Argument>(L->getValue()))
956     ActualF = A->getParent();
957   assert(ActualF && "Unimplemented function local metadata case!");
958 
959   Check(ActualF == F, "function-local metadata used in wrong function", L);
960 }
961 
962 void Verifier::visitMetadataAsValue(const MetadataAsValue &MDV, Function *F) {
963   Metadata *MD = MDV.getMetadata();
964   if (auto *N = dyn_cast<MDNode>(MD)) {
965     visitMDNode(*N, AreDebugLocsAllowed::No);
966     return;
967   }
968 
969   // Only visit each node once.  Metadata can be mutually recursive, so this
970   // avoids infinite recursion here, as well as being an optimization.
971   if (!MDNodes.insert(MD).second)
972     return;
973 
974   if (auto *V = dyn_cast<ValueAsMetadata>(MD))
975     visitValueAsMetadata(*V, F);
976 }
977 
978 static bool isType(const Metadata *MD) { return !MD || isa<DIType>(MD); }
979 static bool isScope(const Metadata *MD) { return !MD || isa<DIScope>(MD); }
980 static bool isDINode(const Metadata *MD) { return !MD || isa<DINode>(MD); }
981 
982 void Verifier::visitDILocation(const DILocation &N) {
983   CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
984           "location requires a valid scope", &N, N.getRawScope());
985   if (auto *IA = N.getRawInlinedAt())
986     CheckDI(isa<DILocation>(IA), "inlined-at should be a location", &N, IA);
987   if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
988     CheckDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
989 }
990 
991 void Verifier::visitGenericDINode(const GenericDINode &N) {
992   CheckDI(N.getTag(), "invalid tag", &N);
993 }
994 
995 void Verifier::visitDIScope(const DIScope &N) {
996   if (auto *F = N.getRawFile())
997     CheckDI(isa<DIFile>(F), "invalid file", &N, F);
998 }
999 
1000 void Verifier::visitDISubrange(const DISubrange &N) {
1001   CheckDI(N.getTag() == dwarf::DW_TAG_subrange_type, "invalid tag", &N);
1002   bool HasAssumedSizedArraySupport = dwarf::isFortran(CurrentSourceLang);
1003   CheckDI(HasAssumedSizedArraySupport || N.getRawCountNode() ||
1004               N.getRawUpperBound(),
1005           "Subrange must contain count or upperBound", &N);
1006   CheckDI(!N.getRawCountNode() || !N.getRawUpperBound(),
1007           "Subrange can have any one of count or upperBound", &N);
1008   auto *CBound = N.getRawCountNode();
1009   CheckDI(!CBound || isa<ConstantAsMetadata>(CBound) ||
1010               isa<DIVariable>(CBound) || isa<DIExpression>(CBound),
1011           "Count must be signed constant or DIVariable or DIExpression", &N);
1012   auto Count = N.getCount();
1013   CheckDI(!Count || !Count.is<ConstantInt *>() ||
1014               Count.get<ConstantInt *>()->getSExtValue() >= -1,
1015           "invalid subrange count", &N);
1016   auto *LBound = N.getRawLowerBound();
1017   CheckDI(!LBound || isa<ConstantAsMetadata>(LBound) ||
1018               isa<DIVariable>(LBound) || isa<DIExpression>(LBound),
1019           "LowerBound must be signed constant or DIVariable or DIExpression",
1020           &N);
1021   auto *UBound = N.getRawUpperBound();
1022   CheckDI(!UBound || isa<ConstantAsMetadata>(UBound) ||
1023               isa<DIVariable>(UBound) || isa<DIExpression>(UBound),
1024           "UpperBound must be signed constant or DIVariable or DIExpression",
1025           &N);
1026   auto *Stride = N.getRawStride();
1027   CheckDI(!Stride || isa<ConstantAsMetadata>(Stride) ||
1028               isa<DIVariable>(Stride) || isa<DIExpression>(Stride),
1029           "Stride must be signed constant or DIVariable or DIExpression", &N);
1030 }
1031 
1032 void Verifier::visitDIGenericSubrange(const DIGenericSubrange &N) {
1033   CheckDI(N.getTag() == dwarf::DW_TAG_generic_subrange, "invalid tag", &N);
1034   CheckDI(N.getRawCountNode() || N.getRawUpperBound(),
1035           "GenericSubrange must contain count or upperBound", &N);
1036   CheckDI(!N.getRawCountNode() || !N.getRawUpperBound(),
1037           "GenericSubrange can have any one of count or upperBound", &N);
1038   auto *CBound = N.getRawCountNode();
1039   CheckDI(!CBound || isa<DIVariable>(CBound) || isa<DIExpression>(CBound),
1040           "Count must be signed constant or DIVariable or DIExpression", &N);
1041   auto *LBound = N.getRawLowerBound();
1042   CheckDI(LBound, "GenericSubrange must contain lowerBound", &N);
1043   CheckDI(isa<DIVariable>(LBound) || isa<DIExpression>(LBound),
1044           "LowerBound must be signed constant or DIVariable or DIExpression",
1045           &N);
1046   auto *UBound = N.getRawUpperBound();
1047   CheckDI(!UBound || isa<DIVariable>(UBound) || isa<DIExpression>(UBound),
1048           "UpperBound must be signed constant or DIVariable or DIExpression",
1049           &N);
1050   auto *Stride = N.getRawStride();
1051   CheckDI(Stride, "GenericSubrange must contain stride", &N);
1052   CheckDI(isa<DIVariable>(Stride) || isa<DIExpression>(Stride),
1053           "Stride must be signed constant or DIVariable or DIExpression", &N);
1054 }
1055 
1056 void Verifier::visitDIEnumerator(const DIEnumerator &N) {
1057   CheckDI(N.getTag() == dwarf::DW_TAG_enumerator, "invalid tag", &N);
1058 }
1059 
1060 void Verifier::visitDIBasicType(const DIBasicType &N) {
1061   CheckDI(N.getTag() == dwarf::DW_TAG_base_type ||
1062               N.getTag() == dwarf::DW_TAG_unspecified_type ||
1063               N.getTag() == dwarf::DW_TAG_string_type,
1064           "invalid tag", &N);
1065 }
1066 
1067 void Verifier::visitDIStringType(const DIStringType &N) {
1068   CheckDI(N.getTag() == dwarf::DW_TAG_string_type, "invalid tag", &N);
1069   CheckDI(!(N.isBigEndian() && N.isLittleEndian()), "has conflicting flags",
1070           &N);
1071 }
1072 
1073 void Verifier::visitDIDerivedType(const DIDerivedType &N) {
1074   // Common scope checks.
1075   visitDIScope(N);
1076 
1077   CheckDI(N.getTag() == dwarf::DW_TAG_typedef ||
1078               N.getTag() == dwarf::DW_TAG_pointer_type ||
1079               N.getTag() == dwarf::DW_TAG_ptr_to_member_type ||
1080               N.getTag() == dwarf::DW_TAG_reference_type ||
1081               N.getTag() == dwarf::DW_TAG_rvalue_reference_type ||
1082               N.getTag() == dwarf::DW_TAG_const_type ||
1083               N.getTag() == dwarf::DW_TAG_immutable_type ||
1084               N.getTag() == dwarf::DW_TAG_volatile_type ||
1085               N.getTag() == dwarf::DW_TAG_restrict_type ||
1086               N.getTag() == dwarf::DW_TAG_atomic_type ||
1087               N.getTag() == dwarf::DW_TAG_member ||
1088               N.getTag() == dwarf::DW_TAG_inheritance ||
1089               N.getTag() == dwarf::DW_TAG_friend ||
1090               N.getTag() == dwarf::DW_TAG_set_type,
1091           "invalid tag", &N);
1092   if (N.getTag() == dwarf::DW_TAG_ptr_to_member_type) {
1093     CheckDI(isType(N.getRawExtraData()), "invalid pointer to member type", &N,
1094             N.getRawExtraData());
1095   }
1096 
1097   if (N.getTag() == dwarf::DW_TAG_set_type) {
1098     if (auto *T = N.getRawBaseType()) {
1099       auto *Enum = dyn_cast_or_null<DICompositeType>(T);
1100       auto *Basic = dyn_cast_or_null<DIBasicType>(T);
1101       CheckDI(
1102           (Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type) ||
1103               (Basic && (Basic->getEncoding() == dwarf::DW_ATE_unsigned ||
1104                          Basic->getEncoding() == dwarf::DW_ATE_signed ||
1105                          Basic->getEncoding() == dwarf::DW_ATE_unsigned_char ||
1106                          Basic->getEncoding() == dwarf::DW_ATE_signed_char ||
1107                          Basic->getEncoding() == dwarf::DW_ATE_boolean)),
1108           "invalid set base type", &N, T);
1109     }
1110   }
1111 
1112   CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
1113   CheckDI(isType(N.getRawBaseType()), "invalid base type", &N,
1114           N.getRawBaseType());
1115 
1116   if (N.getDWARFAddressSpace()) {
1117     CheckDI(N.getTag() == dwarf::DW_TAG_pointer_type ||
1118                 N.getTag() == dwarf::DW_TAG_reference_type ||
1119                 N.getTag() == dwarf::DW_TAG_rvalue_reference_type,
1120             "DWARF address space only applies to pointer or reference types",
1121             &N);
1122   }
1123 }
1124 
1125 /// Detect mutually exclusive flags.
1126 static bool hasConflictingReferenceFlags(unsigned Flags) {
1127   return ((Flags & DINode::FlagLValueReference) &&
1128           (Flags & DINode::FlagRValueReference)) ||
1129          ((Flags & DINode::FlagTypePassByValue) &&
1130           (Flags & DINode::FlagTypePassByReference));
1131 }
1132 
1133 void Verifier::visitTemplateParams(const MDNode &N, const Metadata &RawParams) {
1134   auto *Params = dyn_cast<MDTuple>(&RawParams);
1135   CheckDI(Params, "invalid template params", &N, &RawParams);
1136   for (Metadata *Op : Params->operands()) {
1137     CheckDI(Op && isa<DITemplateParameter>(Op), "invalid template parameter",
1138             &N, Params, Op);
1139   }
1140 }
1141 
1142 void Verifier::visitDICompositeType(const DICompositeType &N) {
1143   // Common scope checks.
1144   visitDIScope(N);
1145 
1146   CheckDI(N.getTag() == dwarf::DW_TAG_array_type ||
1147               N.getTag() == dwarf::DW_TAG_structure_type ||
1148               N.getTag() == dwarf::DW_TAG_union_type ||
1149               N.getTag() == dwarf::DW_TAG_enumeration_type ||
1150               N.getTag() == dwarf::DW_TAG_class_type ||
1151               N.getTag() == dwarf::DW_TAG_variant_part ||
1152               N.getTag() == dwarf::DW_TAG_namelist,
1153           "invalid tag", &N);
1154 
1155   CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
1156   CheckDI(isType(N.getRawBaseType()), "invalid base type", &N,
1157           N.getRawBaseType());
1158 
1159   CheckDI(!N.getRawElements() || isa<MDTuple>(N.getRawElements()),
1160           "invalid composite elements", &N, N.getRawElements());
1161   CheckDI(isType(N.getRawVTableHolder()), "invalid vtable holder", &N,
1162           N.getRawVTableHolder());
1163   CheckDI(!hasConflictingReferenceFlags(N.getFlags()),
1164           "invalid reference flags", &N);
1165   unsigned DIBlockByRefStruct = 1 << 4;
1166   CheckDI((N.getFlags() & DIBlockByRefStruct) == 0,
1167           "DIBlockByRefStruct on DICompositeType is no longer supported", &N);
1168 
1169   if (N.isVector()) {
1170     const DINodeArray Elements = N.getElements();
1171     CheckDI(Elements.size() == 1 &&
1172                 Elements[0]->getTag() == dwarf::DW_TAG_subrange_type,
1173             "invalid vector, expected one element of type subrange", &N);
1174   }
1175 
1176   if (auto *Params = N.getRawTemplateParams())
1177     visitTemplateParams(N, *Params);
1178 
1179   if (auto *D = N.getRawDiscriminator()) {
1180     CheckDI(isa<DIDerivedType>(D) && N.getTag() == dwarf::DW_TAG_variant_part,
1181             "discriminator can only appear on variant part");
1182   }
1183 
1184   if (N.getRawDataLocation()) {
1185     CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1186             "dataLocation can only appear in array type");
1187   }
1188 
1189   if (N.getRawAssociated()) {
1190     CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1191             "associated can only appear in array type");
1192   }
1193 
1194   if (N.getRawAllocated()) {
1195     CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1196             "allocated can only appear in array type");
1197   }
1198 
1199   if (N.getRawRank()) {
1200     CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1201             "rank can only appear in array type");
1202   }
1203 }
1204 
1205 void Verifier::visitDISubroutineType(const DISubroutineType &N) {
1206   CheckDI(N.getTag() == dwarf::DW_TAG_subroutine_type, "invalid tag", &N);
1207   if (auto *Types = N.getRawTypeArray()) {
1208     CheckDI(isa<MDTuple>(Types), "invalid composite elements", &N, Types);
1209     for (Metadata *Ty : N.getTypeArray()->operands()) {
1210       CheckDI(isType(Ty), "invalid subroutine type ref", &N, Types, Ty);
1211     }
1212   }
1213   CheckDI(!hasConflictingReferenceFlags(N.getFlags()),
1214           "invalid reference flags", &N);
1215 }
1216 
1217 void Verifier::visitDIFile(const DIFile &N) {
1218   CheckDI(N.getTag() == dwarf::DW_TAG_file_type, "invalid tag", &N);
1219   Optional<DIFile::ChecksumInfo<StringRef>> Checksum = N.getChecksum();
1220   if (Checksum) {
1221     CheckDI(Checksum->Kind <= DIFile::ChecksumKind::CSK_Last,
1222             "invalid checksum kind", &N);
1223     size_t Size;
1224     switch (Checksum->Kind) {
1225     case DIFile::CSK_MD5:
1226       Size = 32;
1227       break;
1228     case DIFile::CSK_SHA1:
1229       Size = 40;
1230       break;
1231     case DIFile::CSK_SHA256:
1232       Size = 64;
1233       break;
1234     }
1235     CheckDI(Checksum->Value.size() == Size, "invalid checksum length", &N);
1236     CheckDI(Checksum->Value.find_if_not(llvm::isHexDigit) == StringRef::npos,
1237             "invalid checksum", &N);
1238   }
1239 }
1240 
1241 void Verifier::visitDICompileUnit(const DICompileUnit &N) {
1242   CheckDI(N.isDistinct(), "compile units must be distinct", &N);
1243   CheckDI(N.getTag() == dwarf::DW_TAG_compile_unit, "invalid tag", &N);
1244 
1245   // Don't bother verifying the compilation directory or producer string
1246   // as those could be empty.
1247   CheckDI(N.getRawFile() && isa<DIFile>(N.getRawFile()), "invalid file", &N,
1248           N.getRawFile());
1249   CheckDI(!N.getFile()->getFilename().empty(), "invalid filename", &N,
1250           N.getFile());
1251 
1252   CurrentSourceLang = (dwarf::SourceLanguage)N.getSourceLanguage();
1253 
1254   verifySourceDebugInfo(N, *N.getFile());
1255 
1256   CheckDI((N.getEmissionKind() <= DICompileUnit::LastEmissionKind),
1257           "invalid emission kind", &N);
1258 
1259   if (auto *Array = N.getRawEnumTypes()) {
1260     CheckDI(isa<MDTuple>(Array), "invalid enum list", &N, Array);
1261     for (Metadata *Op : N.getEnumTypes()->operands()) {
1262       auto *Enum = dyn_cast_or_null<DICompositeType>(Op);
1263       CheckDI(Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type,
1264               "invalid enum type", &N, N.getEnumTypes(), Op);
1265     }
1266   }
1267   if (auto *Array = N.getRawRetainedTypes()) {
1268     CheckDI(isa<MDTuple>(Array), "invalid retained type list", &N, Array);
1269     for (Metadata *Op : N.getRetainedTypes()->operands()) {
1270       CheckDI(
1271           Op && (isa<DIType>(Op) || (isa<DISubprogram>(Op) &&
1272                                      !cast<DISubprogram>(Op)->isDefinition())),
1273           "invalid retained type", &N, Op);
1274     }
1275   }
1276   if (auto *Array = N.getRawGlobalVariables()) {
1277     CheckDI(isa<MDTuple>(Array), "invalid global variable list", &N, Array);
1278     for (Metadata *Op : N.getGlobalVariables()->operands()) {
1279       CheckDI(Op && (isa<DIGlobalVariableExpression>(Op)),
1280               "invalid global variable ref", &N, Op);
1281     }
1282   }
1283   if (auto *Array = N.getRawImportedEntities()) {
1284     CheckDI(isa<MDTuple>(Array), "invalid imported entity list", &N, Array);
1285     for (Metadata *Op : N.getImportedEntities()->operands()) {
1286       CheckDI(Op && isa<DIImportedEntity>(Op), "invalid imported entity ref",
1287               &N, Op);
1288     }
1289   }
1290   if (auto *Array = N.getRawMacros()) {
1291     CheckDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
1292     for (Metadata *Op : N.getMacros()->operands()) {
1293       CheckDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
1294     }
1295   }
1296   CUVisited.insert(&N);
1297 }
1298 
1299 void Verifier::visitDISubprogram(const DISubprogram &N) {
1300   CheckDI(N.getTag() == dwarf::DW_TAG_subprogram, "invalid tag", &N);
1301   CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
1302   if (auto *F = N.getRawFile())
1303     CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1304   else
1305     CheckDI(N.getLine() == 0, "line specified with no file", &N, N.getLine());
1306   if (auto *T = N.getRawType())
1307     CheckDI(isa<DISubroutineType>(T), "invalid subroutine type", &N, T);
1308   CheckDI(isType(N.getRawContainingType()), "invalid containing type", &N,
1309           N.getRawContainingType());
1310   if (auto *Params = N.getRawTemplateParams())
1311     visitTemplateParams(N, *Params);
1312   if (auto *S = N.getRawDeclaration())
1313     CheckDI(isa<DISubprogram>(S) && !cast<DISubprogram>(S)->isDefinition(),
1314             "invalid subprogram declaration", &N, S);
1315   if (auto *RawNode = N.getRawRetainedNodes()) {
1316     auto *Node = dyn_cast<MDTuple>(RawNode);
1317     CheckDI(Node, "invalid retained nodes list", &N, RawNode);
1318     for (Metadata *Op : Node->operands()) {
1319       CheckDI(Op && (isa<DILocalVariable>(Op) || isa<DILabel>(Op)),
1320               "invalid retained nodes, expected DILocalVariable or DILabel", &N,
1321               Node, Op);
1322     }
1323   }
1324   CheckDI(!hasConflictingReferenceFlags(N.getFlags()),
1325           "invalid reference flags", &N);
1326 
1327   auto *Unit = N.getRawUnit();
1328   if (N.isDefinition()) {
1329     // Subprogram definitions (not part of the type hierarchy).
1330     CheckDI(N.isDistinct(), "subprogram definitions must be distinct", &N);
1331     CheckDI(Unit, "subprogram definitions must have a compile unit", &N);
1332     CheckDI(isa<DICompileUnit>(Unit), "invalid unit type", &N, Unit);
1333     if (N.getFile())
1334       verifySourceDebugInfo(*N.getUnit(), *N.getFile());
1335   } else {
1336     // Subprogram declarations (part of the type hierarchy).
1337     CheckDI(!Unit, "subprogram declarations must not have a compile unit", &N);
1338   }
1339 
1340   if (auto *RawThrownTypes = N.getRawThrownTypes()) {
1341     auto *ThrownTypes = dyn_cast<MDTuple>(RawThrownTypes);
1342     CheckDI(ThrownTypes, "invalid thrown types list", &N, RawThrownTypes);
1343     for (Metadata *Op : ThrownTypes->operands())
1344       CheckDI(Op && isa<DIType>(Op), "invalid thrown type", &N, ThrownTypes,
1345               Op);
1346   }
1347 
1348   if (N.areAllCallsDescribed())
1349     CheckDI(N.isDefinition(),
1350             "DIFlagAllCallsDescribed must be attached to a definition");
1351 }
1352 
1353 void Verifier::visitDILexicalBlockBase(const DILexicalBlockBase &N) {
1354   CheckDI(N.getTag() == dwarf::DW_TAG_lexical_block, "invalid tag", &N);
1355   CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1356           "invalid local scope", &N, N.getRawScope());
1357   if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
1358     CheckDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
1359 }
1360 
1361 void Verifier::visitDILexicalBlock(const DILexicalBlock &N) {
1362   visitDILexicalBlockBase(N);
1363 
1364   CheckDI(N.getLine() || !N.getColumn(),
1365           "cannot have column info without line info", &N);
1366 }
1367 
1368 void Verifier::visitDILexicalBlockFile(const DILexicalBlockFile &N) {
1369   visitDILexicalBlockBase(N);
1370 }
1371 
1372 void Verifier::visitDICommonBlock(const DICommonBlock &N) {
1373   CheckDI(N.getTag() == dwarf::DW_TAG_common_block, "invalid tag", &N);
1374   if (auto *S = N.getRawScope())
1375     CheckDI(isa<DIScope>(S), "invalid scope ref", &N, S);
1376   if (auto *S = N.getRawDecl())
1377     CheckDI(isa<DIGlobalVariable>(S), "invalid declaration", &N, S);
1378 }
1379 
1380 void Verifier::visitDINamespace(const DINamespace &N) {
1381   CheckDI(N.getTag() == dwarf::DW_TAG_namespace, "invalid tag", &N);
1382   if (auto *S = N.getRawScope())
1383     CheckDI(isa<DIScope>(S), "invalid scope ref", &N, S);
1384 }
1385 
1386 void Verifier::visitDIMacro(const DIMacro &N) {
1387   CheckDI(N.getMacinfoType() == dwarf::DW_MACINFO_define ||
1388               N.getMacinfoType() == dwarf::DW_MACINFO_undef,
1389           "invalid macinfo type", &N);
1390   CheckDI(!N.getName().empty(), "anonymous macro", &N);
1391   if (!N.getValue().empty()) {
1392     assert(N.getValue().data()[0] != ' ' && "Macro value has a space prefix");
1393   }
1394 }
1395 
1396 void Verifier::visitDIMacroFile(const DIMacroFile &N) {
1397   CheckDI(N.getMacinfoType() == dwarf::DW_MACINFO_start_file,
1398           "invalid macinfo type", &N);
1399   if (auto *F = N.getRawFile())
1400     CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1401 
1402   if (auto *Array = N.getRawElements()) {
1403     CheckDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
1404     for (Metadata *Op : N.getElements()->operands()) {
1405       CheckDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
1406     }
1407   }
1408 }
1409 
1410 void Verifier::visitDIArgList(const DIArgList &N) {
1411   CheckDI(!N.getNumOperands(),
1412           "DIArgList should have no operands other than a list of "
1413           "ValueAsMetadata",
1414           &N);
1415 }
1416 
1417 void Verifier::visitDIModule(const DIModule &N) {
1418   CheckDI(N.getTag() == dwarf::DW_TAG_module, "invalid tag", &N);
1419   CheckDI(!N.getName().empty(), "anonymous module", &N);
1420 }
1421 
1422 void Verifier::visitDITemplateParameter(const DITemplateParameter &N) {
1423   CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1424 }
1425 
1426 void Verifier::visitDITemplateTypeParameter(const DITemplateTypeParameter &N) {
1427   visitDITemplateParameter(N);
1428 
1429   CheckDI(N.getTag() == dwarf::DW_TAG_template_type_parameter, "invalid tag",
1430           &N);
1431 }
1432 
1433 void Verifier::visitDITemplateValueParameter(
1434     const DITemplateValueParameter &N) {
1435   visitDITemplateParameter(N);
1436 
1437   CheckDI(N.getTag() == dwarf::DW_TAG_template_value_parameter ||
1438               N.getTag() == dwarf::DW_TAG_GNU_template_template_param ||
1439               N.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack,
1440           "invalid tag", &N);
1441 }
1442 
1443 void Verifier::visitDIVariable(const DIVariable &N) {
1444   if (auto *S = N.getRawScope())
1445     CheckDI(isa<DIScope>(S), "invalid scope", &N, S);
1446   if (auto *F = N.getRawFile())
1447     CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1448 }
1449 
1450 void Verifier::visitDIGlobalVariable(const DIGlobalVariable &N) {
1451   // Checks common to all variables.
1452   visitDIVariable(N);
1453 
1454   CheckDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
1455   CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1456   // Check only if the global variable is not an extern
1457   if (N.isDefinition())
1458     CheckDI(N.getType(), "missing global variable type", &N);
1459   if (auto *Member = N.getRawStaticDataMemberDeclaration()) {
1460     CheckDI(isa<DIDerivedType>(Member),
1461             "invalid static data member declaration", &N, Member);
1462   }
1463 }
1464 
1465 void Verifier::visitDILocalVariable(const DILocalVariable &N) {
1466   // Checks common to all variables.
1467   visitDIVariable(N);
1468 
1469   CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1470   CheckDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
1471   CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1472           "local variable requires a valid scope", &N, N.getRawScope());
1473   if (auto Ty = N.getType())
1474     CheckDI(!isa<DISubroutineType>(Ty), "invalid type", &N, N.getType());
1475 }
1476 
1477 void Verifier::visitDILabel(const DILabel &N) {
1478   if (auto *S = N.getRawScope())
1479     CheckDI(isa<DIScope>(S), "invalid scope", &N, S);
1480   if (auto *F = N.getRawFile())
1481     CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1482 
1483   CheckDI(N.getTag() == dwarf::DW_TAG_label, "invalid tag", &N);
1484   CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1485           "label requires a valid scope", &N, N.getRawScope());
1486 }
1487 
1488 void Verifier::visitDIExpression(const DIExpression &N) {
1489   CheckDI(N.isValid(), "invalid expression", &N);
1490 }
1491 
1492 void Verifier::visitDIGlobalVariableExpression(
1493     const DIGlobalVariableExpression &GVE) {
1494   CheckDI(GVE.getVariable(), "missing variable");
1495   if (auto *Var = GVE.getVariable())
1496     visitDIGlobalVariable(*Var);
1497   if (auto *Expr = GVE.getExpression()) {
1498     visitDIExpression(*Expr);
1499     if (auto Fragment = Expr->getFragmentInfo())
1500       verifyFragmentExpression(*GVE.getVariable(), *Fragment, &GVE);
1501   }
1502 }
1503 
1504 void Verifier::visitDIObjCProperty(const DIObjCProperty &N) {
1505   CheckDI(N.getTag() == dwarf::DW_TAG_APPLE_property, "invalid tag", &N);
1506   if (auto *T = N.getRawType())
1507     CheckDI(isType(T), "invalid type ref", &N, T);
1508   if (auto *F = N.getRawFile())
1509     CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1510 }
1511 
1512 void Verifier::visitDIImportedEntity(const DIImportedEntity &N) {
1513   CheckDI(N.getTag() == dwarf::DW_TAG_imported_module ||
1514               N.getTag() == dwarf::DW_TAG_imported_declaration,
1515           "invalid tag", &N);
1516   if (auto *S = N.getRawScope())
1517     CheckDI(isa<DIScope>(S), "invalid scope for imported entity", &N, S);
1518   CheckDI(isDINode(N.getRawEntity()), "invalid imported entity", &N,
1519           N.getRawEntity());
1520 }
1521 
1522 void Verifier::visitComdat(const Comdat &C) {
1523   // In COFF the Module is invalid if the GlobalValue has private linkage.
1524   // Entities with private linkage don't have entries in the symbol table.
1525   if (TT.isOSBinFormatCOFF())
1526     if (const GlobalValue *GV = M.getNamedValue(C.getName()))
1527       Check(!GV->hasPrivateLinkage(), "comdat global value has private linkage",
1528             GV);
1529 }
1530 
1531 void Verifier::visitModuleIdents() {
1532   const NamedMDNode *Idents = M.getNamedMetadata("llvm.ident");
1533   if (!Idents)
1534     return;
1535 
1536   // llvm.ident takes a list of metadata entry. Each entry has only one string.
1537   // Scan each llvm.ident entry and make sure that this requirement is met.
1538   for (const MDNode *N : Idents->operands()) {
1539     Check(N->getNumOperands() == 1,
1540           "incorrect number of operands in llvm.ident metadata", N);
1541     Check(dyn_cast_or_null<MDString>(N->getOperand(0)),
1542           ("invalid value for llvm.ident metadata entry operand"
1543            "(the operand should be a string)"),
1544           N->getOperand(0));
1545   }
1546 }
1547 
1548 void Verifier::visitModuleCommandLines() {
1549   const NamedMDNode *CommandLines = M.getNamedMetadata("llvm.commandline");
1550   if (!CommandLines)
1551     return;
1552 
1553   // llvm.commandline takes a list of metadata entry. Each entry has only one
1554   // string. Scan each llvm.commandline entry and make sure that this
1555   // requirement is met.
1556   for (const MDNode *N : CommandLines->operands()) {
1557     Check(N->getNumOperands() == 1,
1558           "incorrect number of operands in llvm.commandline metadata", N);
1559     Check(dyn_cast_or_null<MDString>(N->getOperand(0)),
1560           ("invalid value for llvm.commandline metadata entry operand"
1561            "(the operand should be a string)"),
1562           N->getOperand(0));
1563   }
1564 }
1565 
1566 void Verifier::visitModuleFlags() {
1567   const NamedMDNode *Flags = M.getModuleFlagsMetadata();
1568   if (!Flags) return;
1569 
1570   // Scan each flag, and track the flags and requirements.
1571   DenseMap<const MDString*, const MDNode*> SeenIDs;
1572   SmallVector<const MDNode*, 16> Requirements;
1573   for (const MDNode *MDN : Flags->operands())
1574     visitModuleFlag(MDN, SeenIDs, Requirements);
1575 
1576   // Validate that the requirements in the module are valid.
1577   for (const MDNode *Requirement : Requirements) {
1578     const MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1579     const Metadata *ReqValue = Requirement->getOperand(1);
1580 
1581     const MDNode *Op = SeenIDs.lookup(Flag);
1582     if (!Op) {
1583       CheckFailed("invalid requirement on flag, flag is not present in module",
1584                   Flag);
1585       continue;
1586     }
1587 
1588     if (Op->getOperand(2) != ReqValue) {
1589       CheckFailed(("invalid requirement on flag, "
1590                    "flag does not have the required value"),
1591                   Flag);
1592       continue;
1593     }
1594   }
1595 }
1596 
1597 void
1598 Verifier::visitModuleFlag(const MDNode *Op,
1599                           DenseMap<const MDString *, const MDNode *> &SeenIDs,
1600                           SmallVectorImpl<const MDNode *> &Requirements) {
1601   // Each module flag should have three arguments, the merge behavior (a
1602   // constant int), the flag ID (an MDString), and the value.
1603   Check(Op->getNumOperands() == 3,
1604         "incorrect number of operands in module flag", Op);
1605   Module::ModFlagBehavior MFB;
1606   if (!Module::isValidModFlagBehavior(Op->getOperand(0), MFB)) {
1607     Check(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0)),
1608           "invalid behavior operand in module flag (expected constant integer)",
1609           Op->getOperand(0));
1610     Check(false,
1611           "invalid behavior operand in module flag (unexpected constant)",
1612           Op->getOperand(0));
1613   }
1614   MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
1615   Check(ID, "invalid ID operand in module flag (expected metadata string)",
1616         Op->getOperand(1));
1617 
1618   // Check the values for behaviors with additional requirements.
1619   switch (MFB) {
1620   case Module::Error:
1621   case Module::Warning:
1622   case Module::Override:
1623     // These behavior types accept any value.
1624     break;
1625 
1626   case Module::Min: {
1627     auto *V = mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1628     Check(V && V->getValue().isNonNegative(),
1629           "invalid value for 'min' module flag (expected constant non-negative "
1630           "integer)",
1631           Op->getOperand(2));
1632     break;
1633   }
1634 
1635   case Module::Max: {
1636     Check(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2)),
1637           "invalid value for 'max' module flag (expected constant integer)",
1638           Op->getOperand(2));
1639     break;
1640   }
1641 
1642   case Module::Require: {
1643     // The value should itself be an MDNode with two operands, a flag ID (an
1644     // MDString), and a value.
1645     MDNode *Value = dyn_cast<MDNode>(Op->getOperand(2));
1646     Check(Value && Value->getNumOperands() == 2,
1647           "invalid value for 'require' module flag (expected metadata pair)",
1648           Op->getOperand(2));
1649     Check(isa<MDString>(Value->getOperand(0)),
1650           ("invalid value for 'require' module flag "
1651            "(first value operand should be a string)"),
1652           Value->getOperand(0));
1653 
1654     // Append it to the list of requirements, to check once all module flags are
1655     // scanned.
1656     Requirements.push_back(Value);
1657     break;
1658   }
1659 
1660   case Module::Append:
1661   case Module::AppendUnique: {
1662     // These behavior types require the operand be an MDNode.
1663     Check(isa<MDNode>(Op->getOperand(2)),
1664           "invalid value for 'append'-type module flag "
1665           "(expected a metadata node)",
1666           Op->getOperand(2));
1667     break;
1668   }
1669   }
1670 
1671   // Unless this is a "requires" flag, check the ID is unique.
1672   if (MFB != Module::Require) {
1673     bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
1674     Check(Inserted,
1675           "module flag identifiers must be unique (or of 'require' type)", ID);
1676   }
1677 
1678   if (ID->getString() == "wchar_size") {
1679     ConstantInt *Value
1680       = mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1681     Check(Value, "wchar_size metadata requires constant integer argument");
1682   }
1683 
1684   if (ID->getString() == "Linker Options") {
1685     // If the llvm.linker.options named metadata exists, we assume that the
1686     // bitcode reader has upgraded the module flag. Otherwise the flag might
1687     // have been created by a client directly.
1688     Check(M.getNamedMetadata("llvm.linker.options"),
1689           "'Linker Options' named metadata no longer supported");
1690   }
1691 
1692   if (ID->getString() == "SemanticInterposition") {
1693     ConstantInt *Value =
1694         mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1695     Check(Value,
1696           "SemanticInterposition metadata requires constant integer argument");
1697   }
1698 
1699   if (ID->getString() == "CG Profile") {
1700     for (const MDOperand &MDO : cast<MDNode>(Op->getOperand(2))->operands())
1701       visitModuleFlagCGProfileEntry(MDO);
1702   }
1703 }
1704 
1705 void Verifier::visitModuleFlagCGProfileEntry(const MDOperand &MDO) {
1706   auto CheckFunction = [&](const MDOperand &FuncMDO) {
1707     if (!FuncMDO)
1708       return;
1709     auto F = dyn_cast<ValueAsMetadata>(FuncMDO);
1710     Check(F && isa<Function>(F->getValue()->stripPointerCasts()),
1711           "expected a Function or null", FuncMDO);
1712   };
1713   auto Node = dyn_cast_or_null<MDNode>(MDO);
1714   Check(Node && Node->getNumOperands() == 3, "expected a MDNode triple", MDO);
1715   CheckFunction(Node->getOperand(0));
1716   CheckFunction(Node->getOperand(1));
1717   auto Count = dyn_cast_or_null<ConstantAsMetadata>(Node->getOperand(2));
1718   Check(Count && Count->getType()->isIntegerTy(),
1719         "expected an integer constant", Node->getOperand(2));
1720 }
1721 
1722 void Verifier::verifyAttributeTypes(AttributeSet Attrs, const Value *V) {
1723   for (Attribute A : Attrs) {
1724 
1725     if (A.isStringAttribute()) {
1726 #define GET_ATTR_NAMES
1727 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME)
1728 #define ATTRIBUTE_STRBOOL(ENUM_NAME, DISPLAY_NAME)                             \
1729   if (A.getKindAsString() == #DISPLAY_NAME) {                                  \
1730     auto V = A.getValueAsString();                                             \
1731     if (!(V.empty() || V == "true" || V == "false"))                           \
1732       CheckFailed("invalid value for '" #DISPLAY_NAME "' attribute: " + V +    \
1733                   "");                                                         \
1734   }
1735 
1736 #include "llvm/IR/Attributes.inc"
1737       continue;
1738     }
1739 
1740     if (A.isIntAttribute() != Attribute::isIntAttrKind(A.getKindAsEnum())) {
1741       CheckFailed("Attribute '" + A.getAsString() + "' should have an Argument",
1742                   V);
1743       return;
1744     }
1745   }
1746 }
1747 
1748 // VerifyParameterAttrs - Check the given attributes for an argument or return
1749 // value of the specified type.  The value V is printed in error messages.
1750 void Verifier::verifyParameterAttrs(AttributeSet Attrs, Type *Ty,
1751                                     const Value *V) {
1752   if (!Attrs.hasAttributes())
1753     return;
1754 
1755   verifyAttributeTypes(Attrs, V);
1756 
1757   for (Attribute Attr : Attrs)
1758     Check(Attr.isStringAttribute() ||
1759               Attribute::canUseAsParamAttr(Attr.getKindAsEnum()),
1760           "Attribute '" + Attr.getAsString() + "' does not apply to parameters",
1761           V);
1762 
1763   if (Attrs.hasAttribute(Attribute::ImmArg)) {
1764     Check(Attrs.getNumAttributes() == 1,
1765           "Attribute 'immarg' is incompatible with other attributes", V);
1766   }
1767 
1768   // Check for mutually incompatible attributes.  Only inreg is compatible with
1769   // sret.
1770   unsigned AttrCount = 0;
1771   AttrCount += Attrs.hasAttribute(Attribute::ByVal);
1772   AttrCount += Attrs.hasAttribute(Attribute::InAlloca);
1773   AttrCount += Attrs.hasAttribute(Attribute::Preallocated);
1774   AttrCount += Attrs.hasAttribute(Attribute::StructRet) ||
1775                Attrs.hasAttribute(Attribute::InReg);
1776   AttrCount += Attrs.hasAttribute(Attribute::Nest);
1777   AttrCount += Attrs.hasAttribute(Attribute::ByRef);
1778   Check(AttrCount <= 1,
1779         "Attributes 'byval', 'inalloca', 'preallocated', 'inreg', 'nest', "
1780         "'byref', and 'sret' are incompatible!",
1781         V);
1782 
1783   Check(!(Attrs.hasAttribute(Attribute::InAlloca) &&
1784           Attrs.hasAttribute(Attribute::ReadOnly)),
1785         "Attributes "
1786         "'inalloca and readonly' are incompatible!",
1787         V);
1788 
1789   Check(!(Attrs.hasAttribute(Attribute::StructRet) &&
1790           Attrs.hasAttribute(Attribute::Returned)),
1791         "Attributes "
1792         "'sret and returned' are incompatible!",
1793         V);
1794 
1795   Check(!(Attrs.hasAttribute(Attribute::ZExt) &&
1796           Attrs.hasAttribute(Attribute::SExt)),
1797         "Attributes "
1798         "'zeroext and signext' are incompatible!",
1799         V);
1800 
1801   Check(!(Attrs.hasAttribute(Attribute::ReadNone) &&
1802           Attrs.hasAttribute(Attribute::ReadOnly)),
1803         "Attributes "
1804         "'readnone and readonly' are incompatible!",
1805         V);
1806 
1807   Check(!(Attrs.hasAttribute(Attribute::ReadNone) &&
1808           Attrs.hasAttribute(Attribute::WriteOnly)),
1809         "Attributes "
1810         "'readnone and writeonly' are incompatible!",
1811         V);
1812 
1813   Check(!(Attrs.hasAttribute(Attribute::ReadOnly) &&
1814           Attrs.hasAttribute(Attribute::WriteOnly)),
1815         "Attributes "
1816         "'readonly and writeonly' are incompatible!",
1817         V);
1818 
1819   Check(!(Attrs.hasAttribute(Attribute::NoInline) &&
1820           Attrs.hasAttribute(Attribute::AlwaysInline)),
1821         "Attributes "
1822         "'noinline and alwaysinline' are incompatible!",
1823         V);
1824 
1825   AttributeMask IncompatibleAttrs = AttributeFuncs::typeIncompatible(Ty);
1826   for (Attribute Attr : Attrs) {
1827     if (!Attr.isStringAttribute() &&
1828         IncompatibleAttrs.contains(Attr.getKindAsEnum())) {
1829       CheckFailed("Attribute '" + Attr.getAsString() +
1830                   "' applied to incompatible type!", V);
1831       return;
1832     }
1833   }
1834 
1835   if (PointerType *PTy = dyn_cast<PointerType>(Ty)) {
1836     if (Attrs.hasAttribute(Attribute::ByVal)) {
1837       if (Attrs.hasAttribute(Attribute::Alignment)) {
1838         Align AttrAlign = Attrs.getAlignment().valueOrOne();
1839         Align MaxAlign(ParamMaxAlignment);
1840         Check(AttrAlign <= MaxAlign,
1841               "Attribute 'align' exceed the max size 2^14", V);
1842       }
1843       SmallPtrSet<Type *, 4> Visited;
1844       Check(Attrs.getByValType()->isSized(&Visited),
1845             "Attribute 'byval' does not support unsized types!", V);
1846     }
1847     if (Attrs.hasAttribute(Attribute::ByRef)) {
1848       SmallPtrSet<Type *, 4> Visited;
1849       Check(Attrs.getByRefType()->isSized(&Visited),
1850             "Attribute 'byref' does not support unsized types!", V);
1851     }
1852     if (Attrs.hasAttribute(Attribute::InAlloca)) {
1853       SmallPtrSet<Type *, 4> Visited;
1854       Check(Attrs.getInAllocaType()->isSized(&Visited),
1855             "Attribute 'inalloca' does not support unsized types!", V);
1856     }
1857     if (Attrs.hasAttribute(Attribute::Preallocated)) {
1858       SmallPtrSet<Type *, 4> Visited;
1859       Check(Attrs.getPreallocatedType()->isSized(&Visited),
1860             "Attribute 'preallocated' does not support unsized types!", V);
1861     }
1862     if (!PTy->isOpaque()) {
1863       if (!isa<PointerType>(PTy->getNonOpaquePointerElementType()))
1864         Check(!Attrs.hasAttribute(Attribute::SwiftError),
1865               "Attribute 'swifterror' only applies to parameters "
1866               "with pointer to pointer type!",
1867               V);
1868       if (Attrs.hasAttribute(Attribute::ByRef)) {
1869         Check(Attrs.getByRefType() == PTy->getNonOpaquePointerElementType(),
1870               "Attribute 'byref' type does not match parameter!", V);
1871       }
1872 
1873       if (Attrs.hasAttribute(Attribute::ByVal) && Attrs.getByValType()) {
1874         Check(Attrs.getByValType() == PTy->getNonOpaquePointerElementType(),
1875               "Attribute 'byval' type does not match parameter!", V);
1876       }
1877 
1878       if (Attrs.hasAttribute(Attribute::Preallocated)) {
1879         Check(Attrs.getPreallocatedType() ==
1880                   PTy->getNonOpaquePointerElementType(),
1881               "Attribute 'preallocated' type does not match parameter!", V);
1882       }
1883 
1884       if (Attrs.hasAttribute(Attribute::InAlloca)) {
1885         Check(Attrs.getInAllocaType() == PTy->getNonOpaquePointerElementType(),
1886               "Attribute 'inalloca' type does not match parameter!", V);
1887       }
1888 
1889       if (Attrs.hasAttribute(Attribute::ElementType)) {
1890         Check(Attrs.getElementType() == PTy->getNonOpaquePointerElementType(),
1891               "Attribute 'elementtype' type does not match parameter!", V);
1892       }
1893     }
1894   }
1895 }
1896 
1897 void Verifier::checkUnsignedBaseTenFuncAttr(AttributeList Attrs, StringRef Attr,
1898                                             const Value *V) {
1899   if (Attrs.hasFnAttr(Attr)) {
1900     StringRef S = Attrs.getFnAttr(Attr).getValueAsString();
1901     unsigned N;
1902     if (S.getAsInteger(10, N))
1903       CheckFailed("\"" + Attr + "\" takes an unsigned integer: " + S, V);
1904   }
1905 }
1906 
1907 // Check parameter attributes against a function type.
1908 // The value V is printed in error messages.
1909 void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
1910                                    const Value *V, bool IsIntrinsic,
1911                                    bool IsInlineAsm) {
1912   if (Attrs.isEmpty())
1913     return;
1914 
1915   if (AttributeListsVisited.insert(Attrs.getRawPointer()).second) {
1916     Check(Attrs.hasParentContext(Context),
1917           "Attribute list does not match Module context!", &Attrs, V);
1918     for (const auto &AttrSet : Attrs) {
1919       Check(!AttrSet.hasAttributes() || AttrSet.hasParentContext(Context),
1920             "Attribute set does not match Module context!", &AttrSet, V);
1921       for (const auto &A : AttrSet) {
1922         Check(A.hasParentContext(Context),
1923               "Attribute does not match Module context!", &A, V);
1924       }
1925     }
1926   }
1927 
1928   bool SawNest = false;
1929   bool SawReturned = false;
1930   bool SawSRet = false;
1931   bool SawSwiftSelf = false;
1932   bool SawSwiftAsync = false;
1933   bool SawSwiftError = false;
1934 
1935   // Verify return value attributes.
1936   AttributeSet RetAttrs = Attrs.getRetAttrs();
1937   for (Attribute RetAttr : RetAttrs)
1938     Check(RetAttr.isStringAttribute() ||
1939               Attribute::canUseAsRetAttr(RetAttr.getKindAsEnum()),
1940           "Attribute '" + RetAttr.getAsString() +
1941               "' does not apply to function return values",
1942           V);
1943 
1944   verifyParameterAttrs(RetAttrs, FT->getReturnType(), V);
1945 
1946   // Verify parameter attributes.
1947   for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1948     Type *Ty = FT->getParamType(i);
1949     AttributeSet ArgAttrs = Attrs.getParamAttrs(i);
1950 
1951     if (!IsIntrinsic) {
1952       Check(!ArgAttrs.hasAttribute(Attribute::ImmArg),
1953             "immarg attribute only applies to intrinsics", V);
1954       if (!IsInlineAsm)
1955         Check(!ArgAttrs.hasAttribute(Attribute::ElementType),
1956               "Attribute 'elementtype' can only be applied to intrinsics"
1957               " and inline asm.",
1958               V);
1959     }
1960 
1961     verifyParameterAttrs(ArgAttrs, Ty, V);
1962 
1963     if (ArgAttrs.hasAttribute(Attribute::Nest)) {
1964       Check(!SawNest, "More than one parameter has attribute nest!", V);
1965       SawNest = true;
1966     }
1967 
1968     if (ArgAttrs.hasAttribute(Attribute::Returned)) {
1969       Check(!SawReturned, "More than one parameter has attribute returned!", V);
1970       Check(Ty->canLosslesslyBitCastTo(FT->getReturnType()),
1971             "Incompatible argument and return types for 'returned' attribute",
1972             V);
1973       SawReturned = true;
1974     }
1975 
1976     if (ArgAttrs.hasAttribute(Attribute::StructRet)) {
1977       Check(!SawSRet, "Cannot have multiple 'sret' parameters!", V);
1978       Check(i == 0 || i == 1,
1979             "Attribute 'sret' is not on first or second parameter!", V);
1980       SawSRet = true;
1981     }
1982 
1983     if (ArgAttrs.hasAttribute(Attribute::SwiftSelf)) {
1984       Check(!SawSwiftSelf, "Cannot have multiple 'swiftself' parameters!", V);
1985       SawSwiftSelf = true;
1986     }
1987 
1988     if (ArgAttrs.hasAttribute(Attribute::SwiftAsync)) {
1989       Check(!SawSwiftAsync, "Cannot have multiple 'swiftasync' parameters!", V);
1990       SawSwiftAsync = true;
1991     }
1992 
1993     if (ArgAttrs.hasAttribute(Attribute::SwiftError)) {
1994       Check(!SawSwiftError, "Cannot have multiple 'swifterror' parameters!", V);
1995       SawSwiftError = true;
1996     }
1997 
1998     if (ArgAttrs.hasAttribute(Attribute::InAlloca)) {
1999       Check(i == FT->getNumParams() - 1,
2000             "inalloca isn't on the last parameter!", V);
2001     }
2002   }
2003 
2004   if (!Attrs.hasFnAttrs())
2005     return;
2006 
2007   verifyAttributeTypes(Attrs.getFnAttrs(), V);
2008   for (Attribute FnAttr : Attrs.getFnAttrs())
2009     Check(FnAttr.isStringAttribute() ||
2010               Attribute::canUseAsFnAttr(FnAttr.getKindAsEnum()),
2011           "Attribute '" + FnAttr.getAsString() +
2012               "' does not apply to functions!",
2013           V);
2014 
2015   Check(!(Attrs.hasFnAttr(Attribute::ReadNone) &&
2016           Attrs.hasFnAttr(Attribute::ReadOnly)),
2017         "Attributes 'readnone and readonly' are incompatible!", V);
2018 
2019   Check(!(Attrs.hasFnAttr(Attribute::ReadNone) &&
2020           Attrs.hasFnAttr(Attribute::WriteOnly)),
2021         "Attributes 'readnone and writeonly' are incompatible!", V);
2022 
2023   Check(!(Attrs.hasFnAttr(Attribute::ReadOnly) &&
2024           Attrs.hasFnAttr(Attribute::WriteOnly)),
2025         "Attributes 'readonly and writeonly' are incompatible!", V);
2026 
2027   Check(!(Attrs.hasFnAttr(Attribute::ReadNone) &&
2028           Attrs.hasFnAttr(Attribute::InaccessibleMemOrArgMemOnly)),
2029         "Attributes 'readnone and inaccessiblemem_or_argmemonly' are "
2030         "incompatible!",
2031         V);
2032 
2033   Check(!(Attrs.hasFnAttr(Attribute::ReadNone) &&
2034           Attrs.hasFnAttr(Attribute::InaccessibleMemOnly)),
2035         "Attributes 'readnone and inaccessiblememonly' are incompatible!", V);
2036 
2037   Check(!(Attrs.hasFnAttr(Attribute::NoInline) &&
2038           Attrs.hasFnAttr(Attribute::AlwaysInline)),
2039         "Attributes 'noinline and alwaysinline' are incompatible!", V);
2040 
2041   if (Attrs.hasFnAttr(Attribute::OptimizeNone)) {
2042     Check(Attrs.hasFnAttr(Attribute::NoInline),
2043           "Attribute 'optnone' requires 'noinline'!", V);
2044 
2045     Check(!Attrs.hasFnAttr(Attribute::OptimizeForSize),
2046           "Attributes 'optsize and optnone' are incompatible!", V);
2047 
2048     Check(!Attrs.hasFnAttr(Attribute::MinSize),
2049           "Attributes 'minsize and optnone' are incompatible!", V);
2050   }
2051 
2052   if (Attrs.hasFnAttr(Attribute::JumpTable)) {
2053     const GlobalValue *GV = cast<GlobalValue>(V);
2054     Check(GV->hasGlobalUnnamedAddr(),
2055           "Attribute 'jumptable' requires 'unnamed_addr'", V);
2056   }
2057 
2058   if (Attrs.hasFnAttr(Attribute::AllocSize)) {
2059     std::pair<unsigned, Optional<unsigned>> Args =
2060         Attrs.getFnAttrs().getAllocSizeArgs();
2061 
2062     auto CheckParam = [&](StringRef Name, unsigned ParamNo) {
2063       if (ParamNo >= FT->getNumParams()) {
2064         CheckFailed("'allocsize' " + Name + " argument is out of bounds", V);
2065         return false;
2066       }
2067 
2068       if (!FT->getParamType(ParamNo)->isIntegerTy()) {
2069         CheckFailed("'allocsize' " + Name +
2070                         " argument must refer to an integer parameter",
2071                     V);
2072         return false;
2073       }
2074 
2075       return true;
2076     };
2077 
2078     if (!CheckParam("element size", Args.first))
2079       return;
2080 
2081     if (Args.second && !CheckParam("number of elements", *Args.second))
2082       return;
2083   }
2084 
2085   if (Attrs.hasFnAttr(Attribute::AllocKind)) {
2086     AllocFnKind K = Attrs.getAllocKind();
2087     AllocFnKind Type =
2088         K & (AllocFnKind::Alloc | AllocFnKind::Realloc | AllocFnKind::Free);
2089     if (!is_contained(
2090             {AllocFnKind::Alloc, AllocFnKind::Realloc, AllocFnKind::Free},
2091             Type))
2092       CheckFailed(
2093           "'allockind()' requires exactly one of alloc, realloc, and free");
2094     if ((Type == AllocFnKind::Free) &&
2095         ((K & (AllocFnKind::Uninitialized | AllocFnKind::Zeroed |
2096                AllocFnKind::Aligned)) != AllocFnKind::Unknown))
2097       CheckFailed("'allockind(\"free\")' doesn't allow uninitialized, zeroed, "
2098                   "or aligned modifiers.");
2099     AllocFnKind ZeroedUninit = AllocFnKind::Uninitialized | AllocFnKind::Zeroed;
2100     if ((K & ZeroedUninit) == ZeroedUninit)
2101       CheckFailed("'allockind()' can't be both zeroed and uninitialized");
2102   }
2103 
2104   if (Attrs.hasFnAttr(Attribute::VScaleRange)) {
2105     unsigned VScaleMin = Attrs.getFnAttrs().getVScaleRangeMin();
2106     if (VScaleMin == 0)
2107       CheckFailed("'vscale_range' minimum must be greater than 0", V);
2108 
2109     Optional<unsigned> VScaleMax = Attrs.getFnAttrs().getVScaleRangeMax();
2110     if (VScaleMax && VScaleMin > VScaleMax)
2111       CheckFailed("'vscale_range' minimum cannot be greater than maximum", V);
2112   }
2113 
2114   if (Attrs.hasFnAttr("frame-pointer")) {
2115     StringRef FP = Attrs.getFnAttr("frame-pointer").getValueAsString();
2116     if (FP != "all" && FP != "non-leaf" && FP != "none")
2117       CheckFailed("invalid value for 'frame-pointer' attribute: " + FP, V);
2118   }
2119 
2120   checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-prefix", V);
2121   checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-entry", V);
2122   checkUnsignedBaseTenFuncAttr(Attrs, "warn-stack-size", V);
2123 }
2124 
2125 void Verifier::verifyFunctionMetadata(
2126     ArrayRef<std::pair<unsigned, MDNode *>> MDs) {
2127   for (const auto &Pair : MDs) {
2128     if (Pair.first == LLVMContext::MD_prof) {
2129       MDNode *MD = Pair.second;
2130       Check(MD->getNumOperands() >= 2,
2131             "!prof annotations should have no less than 2 operands", MD);
2132 
2133       // Check first operand.
2134       Check(MD->getOperand(0) != nullptr, "first operand should not be null",
2135             MD);
2136       Check(isa<MDString>(MD->getOperand(0)),
2137             "expected string with name of the !prof annotation", MD);
2138       MDString *MDS = cast<MDString>(MD->getOperand(0));
2139       StringRef ProfName = MDS->getString();
2140       Check(ProfName.equals("function_entry_count") ||
2141                 ProfName.equals("synthetic_function_entry_count"),
2142             "first operand should be 'function_entry_count'"
2143             " or 'synthetic_function_entry_count'",
2144             MD);
2145 
2146       // Check second operand.
2147       Check(MD->getOperand(1) != nullptr, "second operand should not be null",
2148             MD);
2149       Check(isa<ConstantAsMetadata>(MD->getOperand(1)),
2150             "expected integer argument to function_entry_count", MD);
2151     }
2152   }
2153 }
2154 
2155 void Verifier::visitConstantExprsRecursively(const Constant *EntryC) {
2156   if (!ConstantExprVisited.insert(EntryC).second)
2157     return;
2158 
2159   SmallVector<const Constant *, 16> Stack;
2160   Stack.push_back(EntryC);
2161 
2162   while (!Stack.empty()) {
2163     const Constant *C = Stack.pop_back_val();
2164 
2165     // Check this constant expression.
2166     if (const auto *CE = dyn_cast<ConstantExpr>(C))
2167       visitConstantExpr(CE);
2168 
2169     if (const auto *GV = dyn_cast<GlobalValue>(C)) {
2170       // Global Values get visited separately, but we do need to make sure
2171       // that the global value is in the correct module
2172       Check(GV->getParent() == &M, "Referencing global in another module!",
2173             EntryC, &M, GV, GV->getParent());
2174       continue;
2175     }
2176 
2177     // Visit all sub-expressions.
2178     for (const Use &U : C->operands()) {
2179       const auto *OpC = dyn_cast<Constant>(U);
2180       if (!OpC)
2181         continue;
2182       if (!ConstantExprVisited.insert(OpC).second)
2183         continue;
2184       Stack.push_back(OpC);
2185     }
2186   }
2187 }
2188 
2189 void Verifier::visitConstantExpr(const ConstantExpr *CE) {
2190   if (CE->getOpcode() == Instruction::BitCast)
2191     Check(CastInst::castIsValid(Instruction::BitCast, CE->getOperand(0),
2192                                 CE->getType()),
2193           "Invalid bitcast", CE);
2194 }
2195 
2196 bool Verifier::verifyAttributeCount(AttributeList Attrs, unsigned Params) {
2197   // There shouldn't be more attribute sets than there are parameters plus the
2198   // function and return value.
2199   return Attrs.getNumAttrSets() <= Params + 2;
2200 }
2201 
2202 void Verifier::verifyInlineAsmCall(const CallBase &Call) {
2203   const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
2204   unsigned ArgNo = 0;
2205   unsigned LabelNo = 0;
2206   for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) {
2207     if (CI.Type == InlineAsm::isLabel) {
2208       ++LabelNo;
2209       continue;
2210     }
2211 
2212     // Only deal with constraints that correspond to call arguments.
2213     if (!CI.hasArg())
2214       continue;
2215 
2216     if (CI.isIndirect) {
2217       const Value *Arg = Call.getArgOperand(ArgNo);
2218       Check(Arg->getType()->isPointerTy(),
2219             "Operand for indirect constraint must have pointer type", &Call);
2220 
2221       Check(Call.getParamElementType(ArgNo),
2222             "Operand for indirect constraint must have elementtype attribute",
2223             &Call);
2224     } else {
2225       Check(!Call.paramHasAttr(ArgNo, Attribute::ElementType),
2226             "Elementtype attribute can only be applied for indirect "
2227             "constraints",
2228             &Call);
2229     }
2230 
2231     ArgNo++;
2232   }
2233 
2234   if (auto *CallBr = dyn_cast<CallBrInst>(&Call)) {
2235     Check(LabelNo == CallBr->getNumIndirectDests(),
2236           "Number of label constraints does not match number of callbr dests",
2237           &Call);
2238   } else {
2239     Check(LabelNo == 0, "Label constraints can only be used with callbr",
2240           &Call);
2241   }
2242 }
2243 
2244 /// Verify that statepoint intrinsic is well formed.
2245 void Verifier::verifyStatepoint(const CallBase &Call) {
2246   assert(Call.getCalledFunction() &&
2247          Call.getCalledFunction()->getIntrinsicID() ==
2248              Intrinsic::experimental_gc_statepoint);
2249 
2250   Check(!Call.doesNotAccessMemory() && !Call.onlyReadsMemory() &&
2251             !Call.onlyAccessesArgMemory(),
2252         "gc.statepoint must read and write all memory to preserve "
2253         "reordering restrictions required by safepoint semantics",
2254         Call);
2255 
2256   const int64_t NumPatchBytes =
2257       cast<ConstantInt>(Call.getArgOperand(1))->getSExtValue();
2258   assert(isInt<32>(NumPatchBytes) && "NumPatchBytesV is an i32!");
2259   Check(NumPatchBytes >= 0,
2260         "gc.statepoint number of patchable bytes must be "
2261         "positive",
2262         Call);
2263 
2264   Type *TargetElemType = Call.getParamElementType(2);
2265   Check(TargetElemType,
2266         "gc.statepoint callee argument must have elementtype attribute", Call);
2267   FunctionType *TargetFuncType = dyn_cast<FunctionType>(TargetElemType);
2268   Check(TargetFuncType,
2269         "gc.statepoint callee elementtype must be function type", Call);
2270 
2271   const int NumCallArgs = cast<ConstantInt>(Call.getArgOperand(3))->getZExtValue();
2272   Check(NumCallArgs >= 0,
2273         "gc.statepoint number of arguments to underlying call "
2274         "must be positive",
2275         Call);
2276   const int NumParams = (int)TargetFuncType->getNumParams();
2277   if (TargetFuncType->isVarArg()) {
2278     Check(NumCallArgs >= NumParams,
2279           "gc.statepoint mismatch in number of vararg call args", Call);
2280 
2281     // TODO: Remove this limitation
2282     Check(TargetFuncType->getReturnType()->isVoidTy(),
2283           "gc.statepoint doesn't support wrapping non-void "
2284           "vararg functions yet",
2285           Call);
2286   } else
2287     Check(NumCallArgs == NumParams,
2288           "gc.statepoint mismatch in number of call args", Call);
2289 
2290   const uint64_t Flags
2291     = cast<ConstantInt>(Call.getArgOperand(4))->getZExtValue();
2292   Check((Flags & ~(uint64_t)StatepointFlags::MaskAll) == 0,
2293         "unknown flag used in gc.statepoint flags argument", Call);
2294 
2295   // Verify that the types of the call parameter arguments match
2296   // the type of the wrapped callee.
2297   AttributeList Attrs = Call.getAttributes();
2298   for (int i = 0; i < NumParams; i++) {
2299     Type *ParamType = TargetFuncType->getParamType(i);
2300     Type *ArgType = Call.getArgOperand(5 + i)->getType();
2301     Check(ArgType == ParamType,
2302           "gc.statepoint call argument does not match wrapped "
2303           "function type",
2304           Call);
2305 
2306     if (TargetFuncType->isVarArg()) {
2307       AttributeSet ArgAttrs = Attrs.getParamAttrs(5 + i);
2308       Check(!ArgAttrs.hasAttribute(Attribute::StructRet),
2309             "Attribute 'sret' cannot be used for vararg call arguments!", Call);
2310     }
2311   }
2312 
2313   const int EndCallArgsInx = 4 + NumCallArgs;
2314 
2315   const Value *NumTransitionArgsV = Call.getArgOperand(EndCallArgsInx + 1);
2316   Check(isa<ConstantInt>(NumTransitionArgsV),
2317         "gc.statepoint number of transition arguments "
2318         "must be constant integer",
2319         Call);
2320   const int NumTransitionArgs =
2321       cast<ConstantInt>(NumTransitionArgsV)->getZExtValue();
2322   Check(NumTransitionArgs == 0,
2323         "gc.statepoint w/inline transition bundle is deprecated", Call);
2324   const int EndTransitionArgsInx = EndCallArgsInx + 1 + NumTransitionArgs;
2325 
2326   const Value *NumDeoptArgsV = Call.getArgOperand(EndTransitionArgsInx + 1);
2327   Check(isa<ConstantInt>(NumDeoptArgsV),
2328         "gc.statepoint number of deoptimization arguments "
2329         "must be constant integer",
2330         Call);
2331   const int NumDeoptArgs = cast<ConstantInt>(NumDeoptArgsV)->getZExtValue();
2332   Check(NumDeoptArgs == 0,
2333         "gc.statepoint w/inline deopt operands is deprecated", Call);
2334 
2335   const int ExpectedNumArgs = 7 + NumCallArgs;
2336   Check(ExpectedNumArgs == (int)Call.arg_size(),
2337         "gc.statepoint too many arguments", Call);
2338 
2339   // Check that the only uses of this gc.statepoint are gc.result or
2340   // gc.relocate calls which are tied to this statepoint and thus part
2341   // of the same statepoint sequence
2342   for (const User *U : Call.users()) {
2343     const CallInst *UserCall = dyn_cast<const CallInst>(U);
2344     Check(UserCall, "illegal use of statepoint token", Call, U);
2345     if (!UserCall)
2346       continue;
2347     Check(isa<GCRelocateInst>(UserCall) || isa<GCResultInst>(UserCall),
2348           "gc.result or gc.relocate are the only value uses "
2349           "of a gc.statepoint",
2350           Call, U);
2351     if (isa<GCResultInst>(UserCall)) {
2352       Check(UserCall->getArgOperand(0) == &Call,
2353             "gc.result connected to wrong gc.statepoint", Call, UserCall);
2354     } else if (isa<GCRelocateInst>(Call)) {
2355       Check(UserCall->getArgOperand(0) == &Call,
2356             "gc.relocate connected to wrong gc.statepoint", Call, UserCall);
2357     }
2358   }
2359 
2360   // Note: It is legal for a single derived pointer to be listed multiple
2361   // times.  It's non-optimal, but it is legal.  It can also happen after
2362   // insertion if we strip a bitcast away.
2363   // Note: It is really tempting to check that each base is relocated and
2364   // that a derived pointer is never reused as a base pointer.  This turns
2365   // out to be problematic since optimizations run after safepoint insertion
2366   // can recognize equality properties that the insertion logic doesn't know
2367   // about.  See example statepoint.ll in the verifier subdirectory
2368 }
2369 
2370 void Verifier::verifyFrameRecoverIndices() {
2371   for (auto &Counts : FrameEscapeInfo) {
2372     Function *F = Counts.first;
2373     unsigned EscapedObjectCount = Counts.second.first;
2374     unsigned MaxRecoveredIndex = Counts.second.second;
2375     Check(MaxRecoveredIndex <= EscapedObjectCount,
2376           "all indices passed to llvm.localrecover must be less than the "
2377           "number of arguments passed to llvm.localescape in the parent "
2378           "function",
2379           F);
2380   }
2381 }
2382 
2383 static Instruction *getSuccPad(Instruction *Terminator) {
2384   BasicBlock *UnwindDest;
2385   if (auto *II = dyn_cast<InvokeInst>(Terminator))
2386     UnwindDest = II->getUnwindDest();
2387   else if (auto *CSI = dyn_cast<CatchSwitchInst>(Terminator))
2388     UnwindDest = CSI->getUnwindDest();
2389   else
2390     UnwindDest = cast<CleanupReturnInst>(Terminator)->getUnwindDest();
2391   return UnwindDest->getFirstNonPHI();
2392 }
2393 
2394 void Verifier::verifySiblingFuncletUnwinds() {
2395   SmallPtrSet<Instruction *, 8> Visited;
2396   SmallPtrSet<Instruction *, 8> Active;
2397   for (const auto &Pair : SiblingFuncletInfo) {
2398     Instruction *PredPad = Pair.first;
2399     if (Visited.count(PredPad))
2400       continue;
2401     Active.insert(PredPad);
2402     Instruction *Terminator = Pair.second;
2403     do {
2404       Instruction *SuccPad = getSuccPad(Terminator);
2405       if (Active.count(SuccPad)) {
2406         // Found a cycle; report error
2407         Instruction *CyclePad = SuccPad;
2408         SmallVector<Instruction *, 8> CycleNodes;
2409         do {
2410           CycleNodes.push_back(CyclePad);
2411           Instruction *CycleTerminator = SiblingFuncletInfo[CyclePad];
2412           if (CycleTerminator != CyclePad)
2413             CycleNodes.push_back(CycleTerminator);
2414           CyclePad = getSuccPad(CycleTerminator);
2415         } while (CyclePad != SuccPad);
2416         Check(false, "EH pads can't handle each other's exceptions",
2417               ArrayRef<Instruction *>(CycleNodes));
2418       }
2419       // Don't re-walk a node we've already checked
2420       if (!Visited.insert(SuccPad).second)
2421         break;
2422       // Walk to this successor if it has a map entry.
2423       PredPad = SuccPad;
2424       auto TermI = SiblingFuncletInfo.find(PredPad);
2425       if (TermI == SiblingFuncletInfo.end())
2426         break;
2427       Terminator = TermI->second;
2428       Active.insert(PredPad);
2429     } while (true);
2430     // Each node only has one successor, so we've walked all the active
2431     // nodes' successors.
2432     Active.clear();
2433   }
2434 }
2435 
2436 // visitFunction - Verify that a function is ok.
2437 //
2438 void Verifier::visitFunction(const Function &F) {
2439   visitGlobalValue(F);
2440 
2441   // Check function arguments.
2442   FunctionType *FT = F.getFunctionType();
2443   unsigned NumArgs = F.arg_size();
2444 
2445   Check(&Context == &F.getContext(),
2446         "Function context does not match Module context!", &F);
2447 
2448   Check(!F.hasCommonLinkage(), "Functions may not have common linkage", &F);
2449   Check(FT->getNumParams() == NumArgs,
2450         "# formal arguments must match # of arguments for function type!", &F,
2451         FT);
2452   Check(F.getReturnType()->isFirstClassType() ||
2453             F.getReturnType()->isVoidTy() || F.getReturnType()->isStructTy(),
2454         "Functions cannot return aggregate values!", &F);
2455 
2456   Check(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
2457         "Invalid struct return type!", &F);
2458 
2459   AttributeList Attrs = F.getAttributes();
2460 
2461   Check(verifyAttributeCount(Attrs, FT->getNumParams()),
2462         "Attribute after last parameter!", &F);
2463 
2464   bool IsIntrinsic = F.isIntrinsic();
2465 
2466   // Check function attributes.
2467   verifyFunctionAttrs(FT, Attrs, &F, IsIntrinsic, /* IsInlineAsm */ false);
2468 
2469   // On function declarations/definitions, we do not support the builtin
2470   // attribute. We do not check this in VerifyFunctionAttrs since that is
2471   // checking for Attributes that can/can not ever be on functions.
2472   Check(!Attrs.hasFnAttr(Attribute::Builtin),
2473         "Attribute 'builtin' can only be applied to a callsite.", &F);
2474 
2475   Check(!Attrs.hasAttrSomewhere(Attribute::ElementType),
2476         "Attribute 'elementtype' can only be applied to a callsite.", &F);
2477 
2478   // Check that this function meets the restrictions on this calling convention.
2479   // Sometimes varargs is used for perfectly forwarding thunks, so some of these
2480   // restrictions can be lifted.
2481   switch (F.getCallingConv()) {
2482   default:
2483   case CallingConv::C:
2484     break;
2485   case CallingConv::X86_INTR: {
2486     Check(F.arg_empty() || Attrs.hasParamAttr(0, Attribute::ByVal),
2487           "Calling convention parameter requires byval", &F);
2488     break;
2489   }
2490   case CallingConv::AMDGPU_KERNEL:
2491   case CallingConv::SPIR_KERNEL:
2492     Check(F.getReturnType()->isVoidTy(),
2493           "Calling convention requires void return type", &F);
2494     LLVM_FALLTHROUGH;
2495   case CallingConv::AMDGPU_VS:
2496   case CallingConv::AMDGPU_HS:
2497   case CallingConv::AMDGPU_GS:
2498   case CallingConv::AMDGPU_PS:
2499   case CallingConv::AMDGPU_CS:
2500     Check(!F.hasStructRetAttr(), "Calling convention does not allow sret", &F);
2501     if (F.getCallingConv() != CallingConv::SPIR_KERNEL) {
2502       const unsigned StackAS = DL.getAllocaAddrSpace();
2503       unsigned i = 0;
2504       for (const Argument &Arg : F.args()) {
2505         Check(!Attrs.hasParamAttr(i, Attribute::ByVal),
2506               "Calling convention disallows byval", &F);
2507         Check(!Attrs.hasParamAttr(i, Attribute::Preallocated),
2508               "Calling convention disallows preallocated", &F);
2509         Check(!Attrs.hasParamAttr(i, Attribute::InAlloca),
2510               "Calling convention disallows inalloca", &F);
2511 
2512         if (Attrs.hasParamAttr(i, Attribute::ByRef)) {
2513           // FIXME: Should also disallow LDS and GDS, but we don't have the enum
2514           // value here.
2515           Check(Arg.getType()->getPointerAddressSpace() != StackAS,
2516                 "Calling convention disallows stack byref", &F);
2517         }
2518 
2519         ++i;
2520       }
2521     }
2522 
2523     LLVM_FALLTHROUGH;
2524   case CallingConv::Fast:
2525   case CallingConv::Cold:
2526   case CallingConv::Intel_OCL_BI:
2527   case CallingConv::PTX_Kernel:
2528   case CallingConv::PTX_Device:
2529     Check(!F.isVarArg(),
2530           "Calling convention does not support varargs or "
2531           "perfect forwarding!",
2532           &F);
2533     break;
2534   }
2535 
2536   // Check that the argument values match the function type for this function...
2537   unsigned i = 0;
2538   for (const Argument &Arg : F.args()) {
2539     Check(Arg.getType() == FT->getParamType(i),
2540           "Argument value does not match function argument type!", &Arg,
2541           FT->getParamType(i));
2542     Check(Arg.getType()->isFirstClassType(),
2543           "Function arguments must have first-class types!", &Arg);
2544     if (!IsIntrinsic) {
2545       Check(!Arg.getType()->isMetadataTy(),
2546             "Function takes metadata but isn't an intrinsic", &Arg, &F);
2547       Check(!Arg.getType()->isTokenTy(),
2548             "Function takes token but isn't an intrinsic", &Arg, &F);
2549       Check(!Arg.getType()->isX86_AMXTy(),
2550             "Function takes x86_amx but isn't an intrinsic", &Arg, &F);
2551     }
2552 
2553     // Check that swifterror argument is only used by loads and stores.
2554     if (Attrs.hasParamAttr(i, Attribute::SwiftError)) {
2555       verifySwiftErrorValue(&Arg);
2556     }
2557     ++i;
2558   }
2559 
2560   if (!IsIntrinsic) {
2561     Check(!F.getReturnType()->isTokenTy(),
2562           "Function returns a token but isn't an intrinsic", &F);
2563     Check(!F.getReturnType()->isX86_AMXTy(),
2564           "Function returns a x86_amx but isn't an intrinsic", &F);
2565   }
2566 
2567   // Get the function metadata attachments.
2568   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2569   F.getAllMetadata(MDs);
2570   assert(F.hasMetadata() != MDs.empty() && "Bit out-of-sync");
2571   verifyFunctionMetadata(MDs);
2572 
2573   // Check validity of the personality function
2574   if (F.hasPersonalityFn()) {
2575     auto *Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
2576     if (Per)
2577       Check(Per->getParent() == F.getParent(),
2578             "Referencing personality function in another module!", &F,
2579             F.getParent(), Per, Per->getParent());
2580   }
2581 
2582   if (F.isMaterializable()) {
2583     // Function has a body somewhere we can't see.
2584     Check(MDs.empty(), "unmaterialized function cannot have metadata", &F,
2585           MDs.empty() ? nullptr : MDs.front().second);
2586   } else if (F.isDeclaration()) {
2587     for (const auto &I : MDs) {
2588       // This is used for call site debug information.
2589       CheckDI(I.first != LLVMContext::MD_dbg ||
2590                   !cast<DISubprogram>(I.second)->isDistinct(),
2591               "function declaration may only have a unique !dbg attachment",
2592               &F);
2593       Check(I.first != LLVMContext::MD_prof,
2594             "function declaration may not have a !prof attachment", &F);
2595 
2596       // Verify the metadata itself.
2597       visitMDNode(*I.second, AreDebugLocsAllowed::Yes);
2598     }
2599     Check(!F.hasPersonalityFn(),
2600           "Function declaration shouldn't have a personality routine", &F);
2601   } else {
2602     // Verify that this function (which has a body) is not named "llvm.*".  It
2603     // is not legal to define intrinsics.
2604     Check(!IsIntrinsic, "llvm intrinsics cannot be defined!", &F);
2605 
2606     // Check the entry node
2607     const BasicBlock *Entry = &F.getEntryBlock();
2608     Check(pred_empty(Entry),
2609           "Entry block to function must not have predecessors!", Entry);
2610 
2611     // The address of the entry block cannot be taken, unless it is dead.
2612     if (Entry->hasAddressTaken()) {
2613       Check(!BlockAddress::lookup(Entry)->isConstantUsed(),
2614             "blockaddress may not be used with the entry block!", Entry);
2615     }
2616 
2617     unsigned NumDebugAttachments = 0, NumProfAttachments = 0;
2618     // Visit metadata attachments.
2619     for (const auto &I : MDs) {
2620       // Verify that the attachment is legal.
2621       auto AllowLocs = AreDebugLocsAllowed::No;
2622       switch (I.first) {
2623       default:
2624         break;
2625       case LLVMContext::MD_dbg: {
2626         ++NumDebugAttachments;
2627         CheckDI(NumDebugAttachments == 1,
2628                 "function must have a single !dbg attachment", &F, I.second);
2629         CheckDI(isa<DISubprogram>(I.second),
2630                 "function !dbg attachment must be a subprogram", &F, I.second);
2631         CheckDI(cast<DISubprogram>(I.second)->isDistinct(),
2632                 "function definition may only have a distinct !dbg attachment",
2633                 &F);
2634 
2635         auto *SP = cast<DISubprogram>(I.second);
2636         const Function *&AttachedTo = DISubprogramAttachments[SP];
2637         CheckDI(!AttachedTo || AttachedTo == &F,
2638                 "DISubprogram attached to more than one function", SP, &F);
2639         AttachedTo = &F;
2640         AllowLocs = AreDebugLocsAllowed::Yes;
2641         break;
2642       }
2643       case LLVMContext::MD_prof:
2644         ++NumProfAttachments;
2645         Check(NumProfAttachments == 1,
2646               "function must have a single !prof attachment", &F, I.second);
2647         break;
2648       }
2649 
2650       // Verify the metadata itself.
2651       visitMDNode(*I.second, AllowLocs);
2652     }
2653   }
2654 
2655   // If this function is actually an intrinsic, verify that it is only used in
2656   // direct call/invokes, never having its "address taken".
2657   // Only do this if the module is materialized, otherwise we don't have all the
2658   // uses.
2659   if (F.isIntrinsic() && F.getParent()->isMaterialized()) {
2660     const User *U;
2661     if (F.hasAddressTaken(&U, false, true, false,
2662                           /*IgnoreARCAttachedCall=*/true))
2663       Check(false, "Invalid user of intrinsic instruction!", U);
2664   }
2665 
2666   // Check intrinsics' signatures.
2667   switch (F.getIntrinsicID()) {
2668   case Intrinsic::experimental_gc_get_pointer_base: {
2669     FunctionType *FT = F.getFunctionType();
2670     Check(FT->getNumParams() == 1, "wrong number of parameters", F);
2671     Check(isa<PointerType>(F.getReturnType()),
2672           "gc.get.pointer.base must return a pointer", F);
2673     Check(FT->getParamType(0) == F.getReturnType(),
2674           "gc.get.pointer.base operand and result must be of the same type", F);
2675     break;
2676   }
2677   case Intrinsic::experimental_gc_get_pointer_offset: {
2678     FunctionType *FT = F.getFunctionType();
2679     Check(FT->getNumParams() == 1, "wrong number of parameters", F);
2680     Check(isa<PointerType>(FT->getParamType(0)),
2681           "gc.get.pointer.offset operand must be a pointer", F);
2682     Check(F.getReturnType()->isIntegerTy(),
2683           "gc.get.pointer.offset must return integer", F);
2684     break;
2685   }
2686   }
2687 
2688   auto *N = F.getSubprogram();
2689   HasDebugInfo = (N != nullptr);
2690   if (!HasDebugInfo)
2691     return;
2692 
2693   // Check that all !dbg attachments lead to back to N.
2694   //
2695   // FIXME: Check this incrementally while visiting !dbg attachments.
2696   // FIXME: Only check when N is the canonical subprogram for F.
2697   SmallPtrSet<const MDNode *, 32> Seen;
2698   auto VisitDebugLoc = [&](const Instruction &I, const MDNode *Node) {
2699     // Be careful about using DILocation here since we might be dealing with
2700     // broken code (this is the Verifier after all).
2701     const DILocation *DL = dyn_cast_or_null<DILocation>(Node);
2702     if (!DL)
2703       return;
2704     if (!Seen.insert(DL).second)
2705       return;
2706 
2707     Metadata *Parent = DL->getRawScope();
2708     CheckDI(Parent && isa<DILocalScope>(Parent),
2709             "DILocation's scope must be a DILocalScope", N, &F, &I, DL, Parent);
2710 
2711     DILocalScope *Scope = DL->getInlinedAtScope();
2712     Check(Scope, "Failed to find DILocalScope", DL);
2713 
2714     if (!Seen.insert(Scope).second)
2715       return;
2716 
2717     DISubprogram *SP = Scope->getSubprogram();
2718 
2719     // Scope and SP could be the same MDNode and we don't want to skip
2720     // validation in that case
2721     if (SP && ((Scope != SP) && !Seen.insert(SP).second))
2722       return;
2723 
2724     CheckDI(SP->describes(&F),
2725             "!dbg attachment points at wrong subprogram for function", N, &F,
2726             &I, DL, Scope, SP);
2727   };
2728   for (auto &BB : F)
2729     for (auto &I : BB) {
2730       VisitDebugLoc(I, I.getDebugLoc().getAsMDNode());
2731       // The llvm.loop annotations also contain two DILocations.
2732       if (auto MD = I.getMetadata(LLVMContext::MD_loop))
2733         for (unsigned i = 1; i < MD->getNumOperands(); ++i)
2734           VisitDebugLoc(I, dyn_cast_or_null<MDNode>(MD->getOperand(i)));
2735       if (BrokenDebugInfo)
2736         return;
2737     }
2738 }
2739 
2740 // verifyBasicBlock - Verify that a basic block is well formed...
2741 //
2742 void Verifier::visitBasicBlock(BasicBlock &BB) {
2743   InstsInThisBlock.clear();
2744 
2745   // Ensure that basic blocks have terminators!
2746   Check(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
2747 
2748   // Check constraints that this basic block imposes on all of the PHI nodes in
2749   // it.
2750   if (isa<PHINode>(BB.front())) {
2751     SmallVector<BasicBlock *, 8> Preds(predecessors(&BB));
2752     SmallVector<std::pair<BasicBlock*, Value*>, 8> Values;
2753     llvm::sort(Preds);
2754     for (const PHINode &PN : BB.phis()) {
2755       Check(PN.getNumIncomingValues() == Preds.size(),
2756             "PHINode should have one entry for each predecessor of its "
2757             "parent basic block!",
2758             &PN);
2759 
2760       // Get and sort all incoming values in the PHI node...
2761       Values.clear();
2762       Values.reserve(PN.getNumIncomingValues());
2763       for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
2764         Values.push_back(
2765             std::make_pair(PN.getIncomingBlock(i), PN.getIncomingValue(i)));
2766       llvm::sort(Values);
2767 
2768       for (unsigned i = 0, e = Values.size(); i != e; ++i) {
2769         // Check to make sure that if there is more than one entry for a
2770         // particular basic block in this PHI node, that the incoming values are
2771         // all identical.
2772         //
2773         Check(i == 0 || Values[i].first != Values[i - 1].first ||
2774                   Values[i].second == Values[i - 1].second,
2775               "PHI node has multiple entries for the same basic block with "
2776               "different incoming values!",
2777               &PN, Values[i].first, Values[i].second, Values[i - 1].second);
2778 
2779         // Check to make sure that the predecessors and PHI node entries are
2780         // matched up.
2781         Check(Values[i].first == Preds[i],
2782               "PHI node entries do not match predecessors!", &PN,
2783               Values[i].first, Preds[i]);
2784       }
2785     }
2786   }
2787 
2788   // Check that all instructions have their parent pointers set up correctly.
2789   for (auto &I : BB)
2790   {
2791     Check(I.getParent() == &BB, "Instruction has bogus parent pointer!");
2792   }
2793 }
2794 
2795 void Verifier::visitTerminator(Instruction &I) {
2796   // Ensure that terminators only exist at the end of the basic block.
2797   Check(&I == I.getParent()->getTerminator(),
2798         "Terminator found in the middle of a basic block!", I.getParent());
2799   visitInstruction(I);
2800 }
2801 
2802 void Verifier::visitBranchInst(BranchInst &BI) {
2803   if (BI.isConditional()) {
2804     Check(BI.getCondition()->getType()->isIntegerTy(1),
2805           "Branch condition is not 'i1' type!", &BI, BI.getCondition());
2806   }
2807   visitTerminator(BI);
2808 }
2809 
2810 void Verifier::visitReturnInst(ReturnInst &RI) {
2811   Function *F = RI.getParent()->getParent();
2812   unsigned N = RI.getNumOperands();
2813   if (F->getReturnType()->isVoidTy())
2814     Check(N == 0,
2815           "Found return instr that returns non-void in Function of void "
2816           "return type!",
2817           &RI, F->getReturnType());
2818   else
2819     Check(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(),
2820           "Function return type does not match operand "
2821           "type of return inst!",
2822           &RI, F->getReturnType());
2823 
2824   // Check to make sure that the return value has necessary properties for
2825   // terminators...
2826   visitTerminator(RI);
2827 }
2828 
2829 void Verifier::visitSwitchInst(SwitchInst &SI) {
2830   Check(SI.getType()->isVoidTy(), "Switch must have void result type!", &SI);
2831   // Check to make sure that all of the constants in the switch instruction
2832   // have the same type as the switched-on value.
2833   Type *SwitchTy = SI.getCondition()->getType();
2834   SmallPtrSet<ConstantInt*, 32> Constants;
2835   for (auto &Case : SI.cases()) {
2836     Check(Case.getCaseValue()->getType() == SwitchTy,
2837           "Switch constants must all be same type as switch value!", &SI);
2838     Check(Constants.insert(Case.getCaseValue()).second,
2839           "Duplicate integer as switch case", &SI, Case.getCaseValue());
2840   }
2841 
2842   visitTerminator(SI);
2843 }
2844 
2845 void Verifier::visitIndirectBrInst(IndirectBrInst &BI) {
2846   Check(BI.getAddress()->getType()->isPointerTy(),
2847         "Indirectbr operand must have pointer type!", &BI);
2848   for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i)
2849     Check(BI.getDestination(i)->getType()->isLabelTy(),
2850           "Indirectbr destinations must all have pointer type!", &BI);
2851 
2852   visitTerminator(BI);
2853 }
2854 
2855 void Verifier::visitCallBrInst(CallBrInst &CBI) {
2856   Check(CBI.isInlineAsm(), "Callbr is currently only used for asm-goto!", &CBI);
2857   const InlineAsm *IA = cast<InlineAsm>(CBI.getCalledOperand());
2858   Check(!IA->canThrow(), "Unwinding from Callbr is not allowed");
2859 
2860   verifyInlineAsmCall(CBI);
2861   visitTerminator(CBI);
2862 }
2863 
2864 void Verifier::visitSelectInst(SelectInst &SI) {
2865   Check(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1),
2866                                         SI.getOperand(2)),
2867         "Invalid operands for select instruction!", &SI);
2868 
2869   Check(SI.getTrueValue()->getType() == SI.getType(),
2870         "Select values must have same type as select instruction!", &SI);
2871   visitInstruction(SI);
2872 }
2873 
2874 /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
2875 /// a pass, if any exist, it's an error.
2876 ///
2877 void Verifier::visitUserOp1(Instruction &I) {
2878   Check(false, "User-defined operators should not live outside of a pass!", &I);
2879 }
2880 
2881 void Verifier::visitTruncInst(TruncInst &I) {
2882   // Get the source and destination types
2883   Type *SrcTy = I.getOperand(0)->getType();
2884   Type *DestTy = I.getType();
2885 
2886   // Get the size of the types in bits, we'll need this later
2887   unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2888   unsigned DestBitSize = DestTy->getScalarSizeInBits();
2889 
2890   Check(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I);
2891   Check(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I);
2892   Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2893         "trunc source and destination must both be a vector or neither", &I);
2894   Check(SrcBitSize > DestBitSize, "DestTy too big for Trunc", &I);
2895 
2896   visitInstruction(I);
2897 }
2898 
2899 void Verifier::visitZExtInst(ZExtInst &I) {
2900   // Get the source and destination types
2901   Type *SrcTy = I.getOperand(0)->getType();
2902   Type *DestTy = I.getType();
2903 
2904   // Get the size of the types in bits, we'll need this later
2905   Check(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I);
2906   Check(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I);
2907   Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2908         "zext source and destination must both be a vector or neither", &I);
2909   unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2910   unsigned DestBitSize = DestTy->getScalarSizeInBits();
2911 
2912   Check(SrcBitSize < DestBitSize, "Type too small for ZExt", &I);
2913 
2914   visitInstruction(I);
2915 }
2916 
2917 void Verifier::visitSExtInst(SExtInst &I) {
2918   // Get the source and destination types
2919   Type *SrcTy = I.getOperand(0)->getType();
2920   Type *DestTy = I.getType();
2921 
2922   // Get the size of the types in bits, we'll need this later
2923   unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2924   unsigned DestBitSize = DestTy->getScalarSizeInBits();
2925 
2926   Check(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I);
2927   Check(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I);
2928   Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2929         "sext source and destination must both be a vector or neither", &I);
2930   Check(SrcBitSize < DestBitSize, "Type too small for SExt", &I);
2931 
2932   visitInstruction(I);
2933 }
2934 
2935 void Verifier::visitFPTruncInst(FPTruncInst &I) {
2936   // Get the source and destination types
2937   Type *SrcTy = I.getOperand(0)->getType();
2938   Type *DestTy = I.getType();
2939   // Get the size of the types in bits, we'll need this later
2940   unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2941   unsigned DestBitSize = DestTy->getScalarSizeInBits();
2942 
2943   Check(SrcTy->isFPOrFPVectorTy(), "FPTrunc only operates on FP", &I);
2944   Check(DestTy->isFPOrFPVectorTy(), "FPTrunc only produces an FP", &I);
2945   Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2946         "fptrunc source and destination must both be a vector or neither", &I);
2947   Check(SrcBitSize > DestBitSize, "DestTy too big for FPTrunc", &I);
2948 
2949   visitInstruction(I);
2950 }
2951 
2952 void Verifier::visitFPExtInst(FPExtInst &I) {
2953   // Get the source and destination types
2954   Type *SrcTy = I.getOperand(0)->getType();
2955   Type *DestTy = I.getType();
2956 
2957   // Get the size of the types in bits, we'll need this later
2958   unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2959   unsigned DestBitSize = DestTy->getScalarSizeInBits();
2960 
2961   Check(SrcTy->isFPOrFPVectorTy(), "FPExt only operates on FP", &I);
2962   Check(DestTy->isFPOrFPVectorTy(), "FPExt only produces an FP", &I);
2963   Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2964         "fpext source and destination must both be a vector or neither", &I);
2965   Check(SrcBitSize < DestBitSize, "DestTy too small for FPExt", &I);
2966 
2967   visitInstruction(I);
2968 }
2969 
2970 void Verifier::visitUIToFPInst(UIToFPInst &I) {
2971   // Get the source and destination types
2972   Type *SrcTy = I.getOperand(0)->getType();
2973   Type *DestTy = I.getType();
2974 
2975   bool SrcVec = SrcTy->isVectorTy();
2976   bool DstVec = DestTy->isVectorTy();
2977 
2978   Check(SrcVec == DstVec,
2979         "UIToFP source and dest must both be vector or scalar", &I);
2980   Check(SrcTy->isIntOrIntVectorTy(),
2981         "UIToFP source must be integer or integer vector", &I);
2982   Check(DestTy->isFPOrFPVectorTy(), "UIToFP result must be FP or FP vector",
2983         &I);
2984 
2985   if (SrcVec && DstVec)
2986     Check(cast<VectorType>(SrcTy)->getElementCount() ==
2987               cast<VectorType>(DestTy)->getElementCount(),
2988           "UIToFP source and dest vector length mismatch", &I);
2989 
2990   visitInstruction(I);
2991 }
2992 
2993 void Verifier::visitSIToFPInst(SIToFPInst &I) {
2994   // Get the source and destination types
2995   Type *SrcTy = I.getOperand(0)->getType();
2996   Type *DestTy = I.getType();
2997 
2998   bool SrcVec = SrcTy->isVectorTy();
2999   bool DstVec = DestTy->isVectorTy();
3000 
3001   Check(SrcVec == DstVec,
3002         "SIToFP source and dest must both be vector or scalar", &I);
3003   Check(SrcTy->isIntOrIntVectorTy(),
3004         "SIToFP source must be integer or integer vector", &I);
3005   Check(DestTy->isFPOrFPVectorTy(), "SIToFP result must be FP or FP vector",
3006         &I);
3007 
3008   if (SrcVec && DstVec)
3009     Check(cast<VectorType>(SrcTy)->getElementCount() ==
3010               cast<VectorType>(DestTy)->getElementCount(),
3011           "SIToFP source and dest vector length mismatch", &I);
3012 
3013   visitInstruction(I);
3014 }
3015 
3016 void Verifier::visitFPToUIInst(FPToUIInst &I) {
3017   // Get the source and destination types
3018   Type *SrcTy = I.getOperand(0)->getType();
3019   Type *DestTy = I.getType();
3020 
3021   bool SrcVec = SrcTy->isVectorTy();
3022   bool DstVec = DestTy->isVectorTy();
3023 
3024   Check(SrcVec == DstVec,
3025         "FPToUI source and dest must both be vector or scalar", &I);
3026   Check(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector", &I);
3027   Check(DestTy->isIntOrIntVectorTy(),
3028         "FPToUI result must be integer or integer vector", &I);
3029 
3030   if (SrcVec && DstVec)
3031     Check(cast<VectorType>(SrcTy)->getElementCount() ==
3032               cast<VectorType>(DestTy)->getElementCount(),
3033           "FPToUI source and dest vector length mismatch", &I);
3034 
3035   visitInstruction(I);
3036 }
3037 
3038 void Verifier::visitFPToSIInst(FPToSIInst &I) {
3039   // Get the source and destination types
3040   Type *SrcTy = I.getOperand(0)->getType();
3041   Type *DestTy = I.getType();
3042 
3043   bool SrcVec = SrcTy->isVectorTy();
3044   bool DstVec = DestTy->isVectorTy();
3045 
3046   Check(SrcVec == DstVec,
3047         "FPToSI source and dest must both be vector or scalar", &I);
3048   Check(SrcTy->isFPOrFPVectorTy(), "FPToSI source must be FP or FP vector", &I);
3049   Check(DestTy->isIntOrIntVectorTy(),
3050         "FPToSI result must be integer or integer vector", &I);
3051 
3052   if (SrcVec && DstVec)
3053     Check(cast<VectorType>(SrcTy)->getElementCount() ==
3054               cast<VectorType>(DestTy)->getElementCount(),
3055           "FPToSI source and dest vector length mismatch", &I);
3056 
3057   visitInstruction(I);
3058 }
3059 
3060 void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
3061   // Get the source and destination types
3062   Type *SrcTy = I.getOperand(0)->getType();
3063   Type *DestTy = I.getType();
3064 
3065   Check(SrcTy->isPtrOrPtrVectorTy(), "PtrToInt source must be pointer", &I);
3066 
3067   Check(DestTy->isIntOrIntVectorTy(), "PtrToInt result must be integral", &I);
3068   Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), "PtrToInt type mismatch",
3069         &I);
3070 
3071   if (SrcTy->isVectorTy()) {
3072     auto *VSrc = cast<VectorType>(SrcTy);
3073     auto *VDest = cast<VectorType>(DestTy);
3074     Check(VSrc->getElementCount() == VDest->getElementCount(),
3075           "PtrToInt Vector width mismatch", &I);
3076   }
3077 
3078   visitInstruction(I);
3079 }
3080 
3081 void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
3082   // Get the source and destination types
3083   Type *SrcTy = I.getOperand(0)->getType();
3084   Type *DestTy = I.getType();
3085 
3086   Check(SrcTy->isIntOrIntVectorTy(), "IntToPtr source must be an integral", &I);
3087   Check(DestTy->isPtrOrPtrVectorTy(), "IntToPtr result must be a pointer", &I);
3088 
3089   Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), "IntToPtr type mismatch",
3090         &I);
3091   if (SrcTy->isVectorTy()) {
3092     auto *VSrc = cast<VectorType>(SrcTy);
3093     auto *VDest = cast<VectorType>(DestTy);
3094     Check(VSrc->getElementCount() == VDest->getElementCount(),
3095           "IntToPtr Vector width mismatch", &I);
3096   }
3097   visitInstruction(I);
3098 }
3099 
3100 void Verifier::visitBitCastInst(BitCastInst &I) {
3101   Check(
3102       CastInst::castIsValid(Instruction::BitCast, I.getOperand(0), I.getType()),
3103       "Invalid bitcast", &I);
3104   visitInstruction(I);
3105 }
3106 
3107 void Verifier::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
3108   Type *SrcTy = I.getOperand(0)->getType();
3109   Type *DestTy = I.getType();
3110 
3111   Check(SrcTy->isPtrOrPtrVectorTy(), "AddrSpaceCast source must be a pointer",
3112         &I);
3113   Check(DestTy->isPtrOrPtrVectorTy(), "AddrSpaceCast result must be a pointer",
3114         &I);
3115   Check(SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace(),
3116         "AddrSpaceCast must be between different address spaces", &I);
3117   if (auto *SrcVTy = dyn_cast<VectorType>(SrcTy))
3118     Check(SrcVTy->getElementCount() ==
3119               cast<VectorType>(DestTy)->getElementCount(),
3120           "AddrSpaceCast vector pointer number of elements mismatch", &I);
3121   visitInstruction(I);
3122 }
3123 
3124 /// visitPHINode - Ensure that a PHI node is well formed.
3125 ///
3126 void Verifier::visitPHINode(PHINode &PN) {
3127   // Ensure that the PHI nodes are all grouped together at the top of the block.
3128   // This can be tested by checking whether the instruction before this is
3129   // either nonexistent (because this is begin()) or is a PHI node.  If not,
3130   // then there is some other instruction before a PHI.
3131   Check(&PN == &PN.getParent()->front() ||
3132             isa<PHINode>(--BasicBlock::iterator(&PN)),
3133         "PHI nodes not grouped at top of basic block!", &PN, PN.getParent());
3134 
3135   // Check that a PHI doesn't yield a Token.
3136   Check(!PN.getType()->isTokenTy(), "PHI nodes cannot have token type!");
3137 
3138   // Check that all of the values of the PHI node have the same type as the
3139   // result, and that the incoming blocks are really basic blocks.
3140   for (Value *IncValue : PN.incoming_values()) {
3141     Check(PN.getType() == IncValue->getType(),
3142           "PHI node operands are not the same type as the result!", &PN);
3143   }
3144 
3145   // All other PHI node constraints are checked in the visitBasicBlock method.
3146 
3147   visitInstruction(PN);
3148 }
3149 
3150 void Verifier::visitCallBase(CallBase &Call) {
3151   Check(Call.getCalledOperand()->getType()->isPointerTy(),
3152         "Called function must be a pointer!", Call);
3153   PointerType *FPTy = cast<PointerType>(Call.getCalledOperand()->getType());
3154 
3155   Check(FPTy->isOpaqueOrPointeeTypeMatches(Call.getFunctionType()),
3156         "Called function is not the same type as the call!", Call);
3157 
3158   FunctionType *FTy = Call.getFunctionType();
3159 
3160   // Verify that the correct number of arguments are being passed
3161   if (FTy->isVarArg())
3162     Check(Call.arg_size() >= FTy->getNumParams(),
3163           "Called function requires more parameters than were provided!", Call);
3164   else
3165     Check(Call.arg_size() == FTy->getNumParams(),
3166           "Incorrect number of arguments passed to called function!", Call);
3167 
3168   // Verify that all arguments to the call match the function type.
3169   for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3170     Check(Call.getArgOperand(i)->getType() == FTy->getParamType(i),
3171           "Call parameter type does not match function signature!",
3172           Call.getArgOperand(i), FTy->getParamType(i), Call);
3173 
3174   AttributeList Attrs = Call.getAttributes();
3175 
3176   Check(verifyAttributeCount(Attrs, Call.arg_size()),
3177         "Attribute after last parameter!", Call);
3178 
3179   auto VerifyTypeAlign = [&](Type *Ty, const Twine &Message) {
3180     if (!Ty->isSized())
3181       return;
3182     Align ABIAlign = DL.getABITypeAlign(Ty);
3183     Align MaxAlign(ParamMaxAlignment);
3184     Check(ABIAlign <= MaxAlign,
3185           "Incorrect alignment of " + Message + " to called function!", Call);
3186   };
3187 
3188   VerifyTypeAlign(FTy->getReturnType(), "return type");
3189   for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
3190     Type *Ty = FTy->getParamType(i);
3191     VerifyTypeAlign(Ty, "argument passed");
3192   }
3193 
3194   Function *Callee =
3195       dyn_cast<Function>(Call.getCalledOperand()->stripPointerCasts());
3196   bool IsIntrinsic = Callee && Callee->isIntrinsic();
3197   if (IsIntrinsic)
3198     Check(Callee->getValueType() == FTy,
3199           "Intrinsic called with incompatible signature", Call);
3200 
3201   if (Attrs.hasFnAttr(Attribute::Speculatable)) {
3202     // Don't allow speculatable on call sites, unless the underlying function
3203     // declaration is also speculatable.
3204     Check(Callee && Callee->isSpeculatable(),
3205           "speculatable attribute may not apply to call sites", Call);
3206   }
3207 
3208   if (Attrs.hasFnAttr(Attribute::Preallocated)) {
3209     Check(Call.getCalledFunction()->getIntrinsicID() ==
3210               Intrinsic::call_preallocated_arg,
3211           "preallocated as a call site attribute can only be on "
3212           "llvm.call.preallocated.arg");
3213   }
3214 
3215   // Verify call attributes.
3216   verifyFunctionAttrs(FTy, Attrs, &Call, IsIntrinsic, Call.isInlineAsm());
3217 
3218   // Conservatively check the inalloca argument.
3219   // We have a bug if we can find that there is an underlying alloca without
3220   // inalloca.
3221   if (Call.hasInAllocaArgument()) {
3222     Value *InAllocaArg = Call.getArgOperand(FTy->getNumParams() - 1);
3223     if (auto AI = dyn_cast<AllocaInst>(InAllocaArg->stripInBoundsOffsets()))
3224       Check(AI->isUsedWithInAlloca(),
3225             "inalloca argument for call has mismatched alloca", AI, Call);
3226   }
3227 
3228   // For each argument of the callsite, if it has the swifterror argument,
3229   // make sure the underlying alloca/parameter it comes from has a swifterror as
3230   // well.
3231   for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
3232     if (Call.paramHasAttr(i, Attribute::SwiftError)) {
3233       Value *SwiftErrorArg = Call.getArgOperand(i);
3234       if (auto AI = dyn_cast<AllocaInst>(SwiftErrorArg->stripInBoundsOffsets())) {
3235         Check(AI->isSwiftError(),
3236               "swifterror argument for call has mismatched alloca", AI, Call);
3237         continue;
3238       }
3239       auto ArgI = dyn_cast<Argument>(SwiftErrorArg);
3240       Check(ArgI, "swifterror argument should come from an alloca or parameter",
3241             SwiftErrorArg, Call);
3242       Check(ArgI->hasSwiftErrorAttr(),
3243             "swifterror argument for call has mismatched parameter", ArgI,
3244             Call);
3245     }
3246 
3247     if (Attrs.hasParamAttr(i, Attribute::ImmArg)) {
3248       // Don't allow immarg on call sites, unless the underlying declaration
3249       // also has the matching immarg.
3250       Check(Callee && Callee->hasParamAttribute(i, Attribute::ImmArg),
3251             "immarg may not apply only to call sites", Call.getArgOperand(i),
3252             Call);
3253     }
3254 
3255     if (Call.paramHasAttr(i, Attribute::ImmArg)) {
3256       Value *ArgVal = Call.getArgOperand(i);
3257       Check(isa<ConstantInt>(ArgVal) || isa<ConstantFP>(ArgVal),
3258             "immarg operand has non-immediate parameter", ArgVal, Call);
3259     }
3260 
3261     if (Call.paramHasAttr(i, Attribute::Preallocated)) {
3262       Value *ArgVal = Call.getArgOperand(i);
3263       bool hasOB =
3264           Call.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0;
3265       bool isMustTail = Call.isMustTailCall();
3266       Check(hasOB != isMustTail,
3267             "preallocated operand either requires a preallocated bundle or "
3268             "the call to be musttail (but not both)",
3269             ArgVal, Call);
3270     }
3271   }
3272 
3273   if (FTy->isVarArg()) {
3274     // FIXME? is 'nest' even legal here?
3275     bool SawNest = false;
3276     bool SawReturned = false;
3277 
3278     for (unsigned Idx = 0; Idx < FTy->getNumParams(); ++Idx) {
3279       if (Attrs.hasParamAttr(Idx, Attribute::Nest))
3280         SawNest = true;
3281       if (Attrs.hasParamAttr(Idx, Attribute::Returned))
3282         SawReturned = true;
3283     }
3284 
3285     // Check attributes on the varargs part.
3286     for (unsigned Idx = FTy->getNumParams(); Idx < Call.arg_size(); ++Idx) {
3287       Type *Ty = Call.getArgOperand(Idx)->getType();
3288       AttributeSet ArgAttrs = Attrs.getParamAttrs(Idx);
3289       verifyParameterAttrs(ArgAttrs, Ty, &Call);
3290 
3291       if (ArgAttrs.hasAttribute(Attribute::Nest)) {
3292         Check(!SawNest, "More than one parameter has attribute nest!", Call);
3293         SawNest = true;
3294       }
3295 
3296       if (ArgAttrs.hasAttribute(Attribute::Returned)) {
3297         Check(!SawReturned, "More than one parameter has attribute returned!",
3298               Call);
3299         Check(Ty->canLosslesslyBitCastTo(FTy->getReturnType()),
3300               "Incompatible argument and return types for 'returned' "
3301               "attribute",
3302               Call);
3303         SawReturned = true;
3304       }
3305 
3306       // Statepoint intrinsic is vararg but the wrapped function may be not.
3307       // Allow sret here and check the wrapped function in verifyStatepoint.
3308       if (!Call.getCalledFunction() ||
3309           Call.getCalledFunction()->getIntrinsicID() !=
3310               Intrinsic::experimental_gc_statepoint)
3311         Check(!ArgAttrs.hasAttribute(Attribute::StructRet),
3312               "Attribute 'sret' cannot be used for vararg call arguments!",
3313               Call);
3314 
3315       if (ArgAttrs.hasAttribute(Attribute::InAlloca))
3316         Check(Idx == Call.arg_size() - 1,
3317               "inalloca isn't on the last argument!", Call);
3318     }
3319   }
3320 
3321   // Verify that there's no metadata unless it's a direct call to an intrinsic.
3322   if (!IsIntrinsic) {
3323     for (Type *ParamTy : FTy->params()) {
3324       Check(!ParamTy->isMetadataTy(),
3325             "Function has metadata parameter but isn't an intrinsic", Call);
3326       Check(!ParamTy->isTokenTy(),
3327             "Function has token parameter but isn't an intrinsic", Call);
3328     }
3329   }
3330 
3331   // Verify that indirect calls don't return tokens.
3332   if (!Call.getCalledFunction()) {
3333     Check(!FTy->getReturnType()->isTokenTy(),
3334           "Return type cannot be token for indirect call!");
3335     Check(!FTy->getReturnType()->isX86_AMXTy(),
3336           "Return type cannot be x86_amx for indirect call!");
3337   }
3338 
3339   if (Function *F = Call.getCalledFunction())
3340     if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
3341       visitIntrinsicCall(ID, Call);
3342 
3343   // Verify that a callsite has at most one "deopt", at most one "funclet", at
3344   // most one "gc-transition", at most one "cfguardtarget", at most one
3345   // "preallocated" operand bundle, and at most one "ptrauth" operand bundle.
3346   bool FoundDeoptBundle = false, FoundFuncletBundle = false,
3347        FoundGCTransitionBundle = false, FoundCFGuardTargetBundle = false,
3348        FoundPreallocatedBundle = false, FoundGCLiveBundle = false,
3349        FoundPtrauthBundle = false,
3350        FoundAttachedCallBundle = false;
3351   for (unsigned i = 0, e = Call.getNumOperandBundles(); i < e; ++i) {
3352     OperandBundleUse BU = Call.getOperandBundleAt(i);
3353     uint32_t Tag = BU.getTagID();
3354     if (Tag == LLVMContext::OB_deopt) {
3355       Check(!FoundDeoptBundle, "Multiple deopt operand bundles", Call);
3356       FoundDeoptBundle = true;
3357     } else if (Tag == LLVMContext::OB_gc_transition) {
3358       Check(!FoundGCTransitionBundle, "Multiple gc-transition operand bundles",
3359             Call);
3360       FoundGCTransitionBundle = true;
3361     } else if (Tag == LLVMContext::OB_funclet) {
3362       Check(!FoundFuncletBundle, "Multiple funclet operand bundles", Call);
3363       FoundFuncletBundle = true;
3364       Check(BU.Inputs.size() == 1,
3365             "Expected exactly one funclet bundle operand", Call);
3366       Check(isa<FuncletPadInst>(BU.Inputs.front()),
3367             "Funclet bundle operands should correspond to a FuncletPadInst",
3368             Call);
3369     } else if (Tag == LLVMContext::OB_cfguardtarget) {
3370       Check(!FoundCFGuardTargetBundle, "Multiple CFGuardTarget operand bundles",
3371             Call);
3372       FoundCFGuardTargetBundle = true;
3373       Check(BU.Inputs.size() == 1,
3374             "Expected exactly one cfguardtarget bundle operand", Call);
3375     } else if (Tag == LLVMContext::OB_ptrauth) {
3376       Check(!FoundPtrauthBundle, "Multiple ptrauth operand bundles", Call);
3377       FoundPtrauthBundle = true;
3378       Check(BU.Inputs.size() == 2,
3379             "Expected exactly two ptrauth bundle operands", Call);
3380       Check(isa<ConstantInt>(BU.Inputs[0]) &&
3381                 BU.Inputs[0]->getType()->isIntegerTy(32),
3382             "Ptrauth bundle key operand must be an i32 constant", Call);
3383       Check(BU.Inputs[1]->getType()->isIntegerTy(64),
3384             "Ptrauth bundle discriminator operand must be an i64", Call);
3385     } else if (Tag == LLVMContext::OB_preallocated) {
3386       Check(!FoundPreallocatedBundle, "Multiple preallocated operand bundles",
3387             Call);
3388       FoundPreallocatedBundle = true;
3389       Check(BU.Inputs.size() == 1,
3390             "Expected exactly one preallocated bundle operand", Call);
3391       auto Input = dyn_cast<IntrinsicInst>(BU.Inputs.front());
3392       Check(Input &&
3393                 Input->getIntrinsicID() == Intrinsic::call_preallocated_setup,
3394             "\"preallocated\" argument must be a token from "
3395             "llvm.call.preallocated.setup",
3396             Call);
3397     } else if (Tag == LLVMContext::OB_gc_live) {
3398       Check(!FoundGCLiveBundle, "Multiple gc-live operand bundles", Call);
3399       FoundGCLiveBundle = true;
3400     } else if (Tag == LLVMContext::OB_clang_arc_attachedcall) {
3401       Check(!FoundAttachedCallBundle,
3402             "Multiple \"clang.arc.attachedcall\" operand bundles", Call);
3403       FoundAttachedCallBundle = true;
3404       verifyAttachedCallBundle(Call, BU);
3405     }
3406   }
3407 
3408   // Verify that callee and callsite agree on whether to use pointer auth.
3409   Check(!(Call.getCalledFunction() && FoundPtrauthBundle),
3410         "Direct call cannot have a ptrauth bundle", Call);
3411 
3412   // Verify that each inlinable callsite of a debug-info-bearing function in a
3413   // debug-info-bearing function has a debug location attached to it. Failure to
3414   // do so causes assertion failures when the inliner sets up inline scope info.
3415   if (Call.getFunction()->getSubprogram() && Call.getCalledFunction() &&
3416       Call.getCalledFunction()->getSubprogram())
3417     CheckDI(Call.getDebugLoc(),
3418             "inlinable function call in a function with "
3419             "debug info must have a !dbg location",
3420             Call);
3421 
3422   if (Call.isInlineAsm())
3423     verifyInlineAsmCall(Call);
3424 
3425   visitInstruction(Call);
3426 }
3427 
3428 void Verifier::verifyTailCCMustTailAttrs(const AttrBuilder &Attrs,
3429                                          StringRef Context) {
3430   Check(!Attrs.contains(Attribute::InAlloca),
3431         Twine("inalloca attribute not allowed in ") + Context);
3432   Check(!Attrs.contains(Attribute::InReg),
3433         Twine("inreg attribute not allowed in ") + Context);
3434   Check(!Attrs.contains(Attribute::SwiftError),
3435         Twine("swifterror attribute not allowed in ") + Context);
3436   Check(!Attrs.contains(Attribute::Preallocated),
3437         Twine("preallocated attribute not allowed in ") + Context);
3438   Check(!Attrs.contains(Attribute::ByRef),
3439         Twine("byref attribute not allowed in ") + Context);
3440 }
3441 
3442 /// Two types are "congruent" if they are identical, or if they are both pointer
3443 /// types with different pointee types and the same address space.
3444 static bool isTypeCongruent(Type *L, Type *R) {
3445   if (L == R)
3446     return true;
3447   PointerType *PL = dyn_cast<PointerType>(L);
3448   PointerType *PR = dyn_cast<PointerType>(R);
3449   if (!PL || !PR)
3450     return false;
3451   return PL->getAddressSpace() == PR->getAddressSpace();
3452 }
3453 
3454 static AttrBuilder getParameterABIAttributes(LLVMContext& C, unsigned I, AttributeList Attrs) {
3455   static const Attribute::AttrKind ABIAttrs[] = {
3456       Attribute::StructRet,  Attribute::ByVal,          Attribute::InAlloca,
3457       Attribute::InReg,      Attribute::StackAlignment, Attribute::SwiftSelf,
3458       Attribute::SwiftAsync, Attribute::SwiftError,     Attribute::Preallocated,
3459       Attribute::ByRef};
3460   AttrBuilder Copy(C);
3461   for (auto AK : ABIAttrs) {
3462     Attribute Attr = Attrs.getParamAttrs(I).getAttribute(AK);
3463     if (Attr.isValid())
3464       Copy.addAttribute(Attr);
3465   }
3466 
3467   // `align` is ABI-affecting only in combination with `byval` or `byref`.
3468   if (Attrs.hasParamAttr(I, Attribute::Alignment) &&
3469       (Attrs.hasParamAttr(I, Attribute::ByVal) ||
3470        Attrs.hasParamAttr(I, Attribute::ByRef)))
3471     Copy.addAlignmentAttr(Attrs.getParamAlignment(I));
3472   return Copy;
3473 }
3474 
3475 void Verifier::verifyMustTailCall(CallInst &CI) {
3476   Check(!CI.isInlineAsm(), "cannot use musttail call with inline asm", &CI);
3477 
3478   Function *F = CI.getParent()->getParent();
3479   FunctionType *CallerTy = F->getFunctionType();
3480   FunctionType *CalleeTy = CI.getFunctionType();
3481   Check(CallerTy->isVarArg() == CalleeTy->isVarArg(),
3482         "cannot guarantee tail call due to mismatched varargs", &CI);
3483   Check(isTypeCongruent(CallerTy->getReturnType(), CalleeTy->getReturnType()),
3484         "cannot guarantee tail call due to mismatched return types", &CI);
3485 
3486   // - The calling conventions of the caller and callee must match.
3487   Check(F->getCallingConv() == CI.getCallingConv(),
3488         "cannot guarantee tail call due to mismatched calling conv", &CI);
3489 
3490   // - The call must immediately precede a :ref:`ret <i_ret>` instruction,
3491   //   or a pointer bitcast followed by a ret instruction.
3492   // - The ret instruction must return the (possibly bitcasted) value
3493   //   produced by the call or void.
3494   Value *RetVal = &CI;
3495   Instruction *Next = CI.getNextNode();
3496 
3497   // Handle the optional bitcast.
3498   if (BitCastInst *BI = dyn_cast_or_null<BitCastInst>(Next)) {
3499     Check(BI->getOperand(0) == RetVal,
3500           "bitcast following musttail call must use the call", BI);
3501     RetVal = BI;
3502     Next = BI->getNextNode();
3503   }
3504 
3505   // Check the return.
3506   ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next);
3507   Check(Ret, "musttail call must precede a ret with an optional bitcast", &CI);
3508   Check(!Ret->getReturnValue() || Ret->getReturnValue() == RetVal ||
3509             isa<UndefValue>(Ret->getReturnValue()),
3510         "musttail call result must be returned", Ret);
3511 
3512   AttributeList CallerAttrs = F->getAttributes();
3513   AttributeList CalleeAttrs = CI.getAttributes();
3514   if (CI.getCallingConv() == CallingConv::SwiftTail ||
3515       CI.getCallingConv() == CallingConv::Tail) {
3516     StringRef CCName =
3517         CI.getCallingConv() == CallingConv::Tail ? "tailcc" : "swifttailcc";
3518 
3519     // - Only sret, byval, swiftself, and swiftasync ABI-impacting attributes
3520     //   are allowed in swifttailcc call
3521     for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3522       AttrBuilder ABIAttrs = getParameterABIAttributes(F->getContext(), I, CallerAttrs);
3523       SmallString<32> Context{CCName, StringRef(" musttail caller")};
3524       verifyTailCCMustTailAttrs(ABIAttrs, Context);
3525     }
3526     for (unsigned I = 0, E = CalleeTy->getNumParams(); I != E; ++I) {
3527       AttrBuilder ABIAttrs = getParameterABIAttributes(F->getContext(), I, CalleeAttrs);
3528       SmallString<32> Context{CCName, StringRef(" musttail callee")};
3529       verifyTailCCMustTailAttrs(ABIAttrs, Context);
3530     }
3531     // - Varargs functions are not allowed
3532     Check(!CallerTy->isVarArg(), Twine("cannot guarantee ") + CCName +
3533                                      " tail call for varargs function");
3534     return;
3535   }
3536 
3537   // - The caller and callee prototypes must match.  Pointer types of
3538   //   parameters or return types may differ in pointee type, but not
3539   //   address space.
3540   if (!CI.getCalledFunction() || !CI.getCalledFunction()->isIntrinsic()) {
3541     Check(CallerTy->getNumParams() == CalleeTy->getNumParams(),
3542           "cannot guarantee tail call due to mismatched parameter counts", &CI);
3543     for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3544       Check(
3545           isTypeCongruent(CallerTy->getParamType(I), CalleeTy->getParamType(I)),
3546           "cannot guarantee tail call due to mismatched parameter types", &CI);
3547     }
3548   }
3549 
3550   // - All ABI-impacting function attributes, such as sret, byval, inreg,
3551   //   returned, preallocated, and inalloca, must match.
3552   for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3553     AttrBuilder CallerABIAttrs = getParameterABIAttributes(F->getContext(), I, CallerAttrs);
3554     AttrBuilder CalleeABIAttrs = getParameterABIAttributes(F->getContext(), I, CalleeAttrs);
3555     Check(CallerABIAttrs == CalleeABIAttrs,
3556           "cannot guarantee tail call due to mismatched ABI impacting "
3557           "function attributes",
3558           &CI, CI.getOperand(I));
3559   }
3560 }
3561 
3562 void Verifier::visitCallInst(CallInst &CI) {
3563   visitCallBase(CI);
3564 
3565   if (CI.isMustTailCall())
3566     verifyMustTailCall(CI);
3567 }
3568 
3569 void Verifier::visitInvokeInst(InvokeInst &II) {
3570   visitCallBase(II);
3571 
3572   // Verify that the first non-PHI instruction of the unwind destination is an
3573   // exception handling instruction.
3574   Check(
3575       II.getUnwindDest()->isEHPad(),
3576       "The unwind destination does not have an exception handling instruction!",
3577       &II);
3578 
3579   visitTerminator(II);
3580 }
3581 
3582 /// visitUnaryOperator - Check the argument to the unary operator.
3583 ///
3584 void Verifier::visitUnaryOperator(UnaryOperator &U) {
3585   Check(U.getType() == U.getOperand(0)->getType(),
3586         "Unary operators must have same type for"
3587         "operands and result!",
3588         &U);
3589 
3590   switch (U.getOpcode()) {
3591   // Check that floating-point arithmetic operators are only used with
3592   // floating-point operands.
3593   case Instruction::FNeg:
3594     Check(U.getType()->isFPOrFPVectorTy(),
3595           "FNeg operator only works with float types!", &U);
3596     break;
3597   default:
3598     llvm_unreachable("Unknown UnaryOperator opcode!");
3599   }
3600 
3601   visitInstruction(U);
3602 }
3603 
3604 /// visitBinaryOperator - Check that both arguments to the binary operator are
3605 /// of the same type!
3606 ///
3607 void Verifier::visitBinaryOperator(BinaryOperator &B) {
3608   Check(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
3609         "Both operands to a binary operator are not of the same type!", &B);
3610 
3611   switch (B.getOpcode()) {
3612   // Check that integer arithmetic operators are only used with
3613   // integral operands.
3614   case Instruction::Add:
3615   case Instruction::Sub:
3616   case Instruction::Mul:
3617   case Instruction::SDiv:
3618   case Instruction::UDiv:
3619   case Instruction::SRem:
3620   case Instruction::URem:
3621     Check(B.getType()->isIntOrIntVectorTy(),
3622           "Integer arithmetic operators only work with integral types!", &B);
3623     Check(B.getType() == B.getOperand(0)->getType(),
3624           "Integer arithmetic operators must have same type "
3625           "for operands and result!",
3626           &B);
3627     break;
3628   // Check that floating-point arithmetic operators are only used with
3629   // floating-point operands.
3630   case Instruction::FAdd:
3631   case Instruction::FSub:
3632   case Instruction::FMul:
3633   case Instruction::FDiv:
3634   case Instruction::FRem:
3635     Check(B.getType()->isFPOrFPVectorTy(),
3636           "Floating-point arithmetic operators only work with "
3637           "floating-point types!",
3638           &B);
3639     Check(B.getType() == B.getOperand(0)->getType(),
3640           "Floating-point arithmetic operators must have same type "
3641           "for operands and result!",
3642           &B);
3643     break;
3644   // Check that logical operators are only used with integral operands.
3645   case Instruction::And:
3646   case Instruction::Or:
3647   case Instruction::Xor:
3648     Check(B.getType()->isIntOrIntVectorTy(),
3649           "Logical operators only work with integral types!", &B);
3650     Check(B.getType() == B.getOperand(0)->getType(),
3651           "Logical operators must have same type for operands and result!", &B);
3652     break;
3653   case Instruction::Shl:
3654   case Instruction::LShr:
3655   case Instruction::AShr:
3656     Check(B.getType()->isIntOrIntVectorTy(),
3657           "Shifts only work with integral types!", &B);
3658     Check(B.getType() == B.getOperand(0)->getType(),
3659           "Shift return type must be same as operands!", &B);
3660     break;
3661   default:
3662     llvm_unreachable("Unknown BinaryOperator opcode!");
3663   }
3664 
3665   visitInstruction(B);
3666 }
3667 
3668 void Verifier::visitICmpInst(ICmpInst &IC) {
3669   // Check that the operands are the same type
3670   Type *Op0Ty = IC.getOperand(0)->getType();
3671   Type *Op1Ty = IC.getOperand(1)->getType();
3672   Check(Op0Ty == Op1Ty,
3673         "Both operands to ICmp instruction are not of the same type!", &IC);
3674   // Check that the operands are the right type
3675   Check(Op0Ty->isIntOrIntVectorTy() || Op0Ty->isPtrOrPtrVectorTy(),
3676         "Invalid operand types for ICmp instruction", &IC);
3677   // Check that the predicate is valid.
3678   Check(IC.isIntPredicate(), "Invalid predicate in ICmp instruction!", &IC);
3679 
3680   visitInstruction(IC);
3681 }
3682 
3683 void Verifier::visitFCmpInst(FCmpInst &FC) {
3684   // Check that the operands are the same type
3685   Type *Op0Ty = FC.getOperand(0)->getType();
3686   Type *Op1Ty = FC.getOperand(1)->getType();
3687   Check(Op0Ty == Op1Ty,
3688         "Both operands to FCmp instruction are not of the same type!", &FC);
3689   // Check that the operands are the right type
3690   Check(Op0Ty->isFPOrFPVectorTy(), "Invalid operand types for FCmp instruction",
3691         &FC);
3692   // Check that the predicate is valid.
3693   Check(FC.isFPPredicate(), "Invalid predicate in FCmp instruction!", &FC);
3694 
3695   visitInstruction(FC);
3696 }
3697 
3698 void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
3699   Check(ExtractElementInst::isValidOperands(EI.getOperand(0), EI.getOperand(1)),
3700         "Invalid extractelement operands!", &EI);
3701   visitInstruction(EI);
3702 }
3703 
3704 void Verifier::visitInsertElementInst(InsertElementInst &IE) {
3705   Check(InsertElementInst::isValidOperands(IE.getOperand(0), IE.getOperand(1),
3706                                            IE.getOperand(2)),
3707         "Invalid insertelement operands!", &IE);
3708   visitInstruction(IE);
3709 }
3710 
3711 void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
3712   Check(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1),
3713                                            SV.getShuffleMask()),
3714         "Invalid shufflevector operands!", &SV);
3715   visitInstruction(SV);
3716 }
3717 
3718 void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
3719   Type *TargetTy = GEP.getPointerOperandType()->getScalarType();
3720 
3721   Check(isa<PointerType>(TargetTy),
3722         "GEP base pointer is not a vector or a vector of pointers", &GEP);
3723   Check(GEP.getSourceElementType()->isSized(), "GEP into unsized type!", &GEP);
3724 
3725   SmallVector<Value *, 16> Idxs(GEP.indices());
3726   Check(
3727       all_of(Idxs, [](Value *V) { return V->getType()->isIntOrIntVectorTy(); }),
3728       "GEP indexes must be integers", &GEP);
3729   Type *ElTy =
3730       GetElementPtrInst::getIndexedType(GEP.getSourceElementType(), Idxs);
3731   Check(ElTy, "Invalid indices for GEP pointer type!", &GEP);
3732 
3733   Check(GEP.getType()->isPtrOrPtrVectorTy() &&
3734             GEP.getResultElementType() == ElTy,
3735         "GEP is not of right type for indices!", &GEP, ElTy);
3736 
3737   if (auto *GEPVTy = dyn_cast<VectorType>(GEP.getType())) {
3738     // Additional checks for vector GEPs.
3739     ElementCount GEPWidth = GEPVTy->getElementCount();
3740     if (GEP.getPointerOperandType()->isVectorTy())
3741       Check(
3742           GEPWidth ==
3743               cast<VectorType>(GEP.getPointerOperandType())->getElementCount(),
3744           "Vector GEP result width doesn't match operand's", &GEP);
3745     for (Value *Idx : Idxs) {
3746       Type *IndexTy = Idx->getType();
3747       if (auto *IndexVTy = dyn_cast<VectorType>(IndexTy)) {
3748         ElementCount IndexWidth = IndexVTy->getElementCount();
3749         Check(IndexWidth == GEPWidth, "Invalid GEP index vector width", &GEP);
3750       }
3751       Check(IndexTy->isIntOrIntVectorTy(),
3752             "All GEP indices should be of integer type");
3753     }
3754   }
3755 
3756   if (auto *PTy = dyn_cast<PointerType>(GEP.getType())) {
3757     Check(GEP.getAddressSpace() == PTy->getAddressSpace(),
3758           "GEP address space doesn't match type", &GEP);
3759   }
3760 
3761   visitInstruction(GEP);
3762 }
3763 
3764 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
3765   return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
3766 }
3767 
3768 void Verifier::visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty) {
3769   assert(Range && Range == I.getMetadata(LLVMContext::MD_range) &&
3770          "precondition violation");
3771 
3772   unsigned NumOperands = Range->getNumOperands();
3773   Check(NumOperands % 2 == 0, "Unfinished range!", Range);
3774   unsigned NumRanges = NumOperands / 2;
3775   Check(NumRanges >= 1, "It should have at least one range!", Range);
3776 
3777   ConstantRange LastRange(1, true); // Dummy initial value
3778   for (unsigned i = 0; i < NumRanges; ++i) {
3779     ConstantInt *Low =
3780         mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i));
3781     Check(Low, "The lower limit must be an integer!", Low);
3782     ConstantInt *High =
3783         mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i + 1));
3784     Check(High, "The upper limit must be an integer!", High);
3785     Check(High->getType() == Low->getType() && High->getType() == Ty,
3786           "Range types must match instruction type!", &I);
3787 
3788     APInt HighV = High->getValue();
3789     APInt LowV = Low->getValue();
3790     ConstantRange CurRange(LowV, HighV);
3791     Check(!CurRange.isEmptySet() && !CurRange.isFullSet(),
3792           "Range must not be empty!", Range);
3793     if (i != 0) {
3794       Check(CurRange.intersectWith(LastRange).isEmptySet(),
3795             "Intervals are overlapping", Range);
3796       Check(LowV.sgt(LastRange.getLower()), "Intervals are not in order",
3797             Range);
3798       Check(!isContiguous(CurRange, LastRange), "Intervals are contiguous",
3799             Range);
3800     }
3801     LastRange = ConstantRange(LowV, HighV);
3802   }
3803   if (NumRanges > 2) {
3804     APInt FirstLow =
3805         mdconst::dyn_extract<ConstantInt>(Range->getOperand(0))->getValue();
3806     APInt FirstHigh =
3807         mdconst::dyn_extract<ConstantInt>(Range->getOperand(1))->getValue();
3808     ConstantRange FirstRange(FirstLow, FirstHigh);
3809     Check(FirstRange.intersectWith(LastRange).isEmptySet(),
3810           "Intervals are overlapping", Range);
3811     Check(!isContiguous(FirstRange, LastRange), "Intervals are contiguous",
3812           Range);
3813   }
3814 }
3815 
3816 void Verifier::checkAtomicMemAccessSize(Type *Ty, const Instruction *I) {
3817   unsigned Size = DL.getTypeSizeInBits(Ty);
3818   Check(Size >= 8, "atomic memory access' size must be byte-sized", Ty, I);
3819   Check(!(Size & (Size - 1)),
3820         "atomic memory access' operand must have a power-of-two size", Ty, I);
3821 }
3822 
3823 void Verifier::visitLoadInst(LoadInst &LI) {
3824   PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType());
3825   Check(PTy, "Load operand must be a pointer.", &LI);
3826   Type *ElTy = LI.getType();
3827   if (MaybeAlign A = LI.getAlign()) {
3828     Check(A->value() <= Value::MaximumAlignment,
3829           "huge alignment values are unsupported", &LI);
3830   }
3831   Check(ElTy->isSized(), "loading unsized types is not allowed", &LI);
3832   if (LI.isAtomic()) {
3833     Check(LI.getOrdering() != AtomicOrdering::Release &&
3834               LI.getOrdering() != AtomicOrdering::AcquireRelease,
3835           "Load cannot have Release ordering", &LI);
3836     Check(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
3837           "atomic load operand must have integer, pointer, or floating point "
3838           "type!",
3839           ElTy, &LI);
3840     checkAtomicMemAccessSize(ElTy, &LI);
3841   } else {
3842     Check(LI.getSyncScopeID() == SyncScope::System,
3843           "Non-atomic load cannot have SynchronizationScope specified", &LI);
3844   }
3845 
3846   visitInstruction(LI);
3847 }
3848 
3849 void Verifier::visitStoreInst(StoreInst &SI) {
3850   PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType());
3851   Check(PTy, "Store operand must be a pointer.", &SI);
3852   Type *ElTy = SI.getOperand(0)->getType();
3853   Check(PTy->isOpaqueOrPointeeTypeMatches(ElTy),
3854         "Stored value type does not match pointer operand type!", &SI, ElTy);
3855   if (MaybeAlign A = SI.getAlign()) {
3856     Check(A->value() <= Value::MaximumAlignment,
3857           "huge alignment values are unsupported", &SI);
3858   }
3859   Check(ElTy->isSized(), "storing unsized types is not allowed", &SI);
3860   if (SI.isAtomic()) {
3861     Check(SI.getOrdering() != AtomicOrdering::Acquire &&
3862               SI.getOrdering() != AtomicOrdering::AcquireRelease,
3863           "Store cannot have Acquire ordering", &SI);
3864     Check(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
3865           "atomic store operand must have integer, pointer, or floating point "
3866           "type!",
3867           ElTy, &SI);
3868     checkAtomicMemAccessSize(ElTy, &SI);
3869   } else {
3870     Check(SI.getSyncScopeID() == SyncScope::System,
3871           "Non-atomic store cannot have SynchronizationScope specified", &SI);
3872   }
3873   visitInstruction(SI);
3874 }
3875 
3876 /// Check that SwiftErrorVal is used as a swifterror argument in CS.
3877 void Verifier::verifySwiftErrorCall(CallBase &Call,
3878                                     const Value *SwiftErrorVal) {
3879   for (const auto &I : llvm::enumerate(Call.args())) {
3880     if (I.value() == SwiftErrorVal) {
3881       Check(Call.paramHasAttr(I.index(), Attribute::SwiftError),
3882             "swifterror value when used in a callsite should be marked "
3883             "with swifterror attribute",
3884             SwiftErrorVal, Call);
3885     }
3886   }
3887 }
3888 
3889 void Verifier::verifySwiftErrorValue(const Value *SwiftErrorVal) {
3890   // Check that swifterror value is only used by loads, stores, or as
3891   // a swifterror argument.
3892   for (const User *U : SwiftErrorVal->users()) {
3893     Check(isa<LoadInst>(U) || isa<StoreInst>(U) || isa<CallInst>(U) ||
3894               isa<InvokeInst>(U),
3895           "swifterror value can only be loaded and stored from, or "
3896           "as a swifterror argument!",
3897           SwiftErrorVal, U);
3898     // If it is used by a store, check it is the second operand.
3899     if (auto StoreI = dyn_cast<StoreInst>(U))
3900       Check(StoreI->getOperand(1) == SwiftErrorVal,
3901             "swifterror value should be the second operand when used "
3902             "by stores",
3903             SwiftErrorVal, U);
3904     if (auto *Call = dyn_cast<CallBase>(U))
3905       verifySwiftErrorCall(*const_cast<CallBase *>(Call), SwiftErrorVal);
3906   }
3907 }
3908 
3909 void Verifier::visitAllocaInst(AllocaInst &AI) {
3910   SmallPtrSet<Type*, 4> Visited;
3911   Check(AI.getAllocatedType()->isSized(&Visited),
3912         "Cannot allocate unsized type", &AI);
3913   Check(AI.getArraySize()->getType()->isIntegerTy(),
3914         "Alloca array size must have integer type", &AI);
3915   if (MaybeAlign A = AI.getAlign()) {
3916     Check(A->value() <= Value::MaximumAlignment,
3917           "huge alignment values are unsupported", &AI);
3918   }
3919 
3920   if (AI.isSwiftError()) {
3921     Check(AI.getAllocatedType()->isPointerTy(),
3922           "swifterror alloca must have pointer type", &AI);
3923     Check(!AI.isArrayAllocation(),
3924           "swifterror alloca must not be array allocation", &AI);
3925     verifySwiftErrorValue(&AI);
3926   }
3927 
3928   visitInstruction(AI);
3929 }
3930 
3931 void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI) {
3932   Type *ElTy = CXI.getOperand(1)->getType();
3933   Check(ElTy->isIntOrPtrTy(),
3934         "cmpxchg operand must have integer or pointer type", ElTy, &CXI);
3935   checkAtomicMemAccessSize(ElTy, &CXI);
3936   visitInstruction(CXI);
3937 }
3938 
3939 void Verifier::visitAtomicRMWInst(AtomicRMWInst &RMWI) {
3940   Check(RMWI.getOrdering() != AtomicOrdering::Unordered,
3941         "atomicrmw instructions cannot be unordered.", &RMWI);
3942   auto Op = RMWI.getOperation();
3943   Type *ElTy = RMWI.getOperand(1)->getType();
3944   if (Op == AtomicRMWInst::Xchg) {
3945     Check(ElTy->isIntegerTy() || ElTy->isFloatingPointTy() ||
3946               ElTy->isPointerTy(),
3947           "atomicrmw " + AtomicRMWInst::getOperationName(Op) +
3948               " operand must have integer or floating point type!",
3949           &RMWI, ElTy);
3950   } else if (AtomicRMWInst::isFPOperation(Op)) {
3951     Check(ElTy->isFloatingPointTy(),
3952           "atomicrmw " + AtomicRMWInst::getOperationName(Op) +
3953               " operand must have floating point type!",
3954           &RMWI, ElTy);
3955   } else {
3956     Check(ElTy->isIntegerTy(),
3957           "atomicrmw " + AtomicRMWInst::getOperationName(Op) +
3958               " operand must have integer type!",
3959           &RMWI, ElTy);
3960   }
3961   checkAtomicMemAccessSize(ElTy, &RMWI);
3962   Check(AtomicRMWInst::FIRST_BINOP <= Op && Op <= AtomicRMWInst::LAST_BINOP,
3963         "Invalid binary operation!", &RMWI);
3964   visitInstruction(RMWI);
3965 }
3966 
3967 void Verifier::visitFenceInst(FenceInst &FI) {
3968   const AtomicOrdering Ordering = FI.getOrdering();
3969   Check(Ordering == AtomicOrdering::Acquire ||
3970             Ordering == AtomicOrdering::Release ||
3971             Ordering == AtomicOrdering::AcquireRelease ||
3972             Ordering == AtomicOrdering::SequentiallyConsistent,
3973         "fence instructions may only have acquire, release, acq_rel, or "
3974         "seq_cst ordering.",
3975         &FI);
3976   visitInstruction(FI);
3977 }
3978 
3979 void Verifier::visitExtractValueInst(ExtractValueInst &EVI) {
3980   Check(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(),
3981                                          EVI.getIndices()) == EVI.getType(),
3982         "Invalid ExtractValueInst operands!", &EVI);
3983 
3984   visitInstruction(EVI);
3985 }
3986 
3987 void Verifier::visitInsertValueInst(InsertValueInst &IVI) {
3988   Check(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(),
3989                                          IVI.getIndices()) ==
3990             IVI.getOperand(1)->getType(),
3991         "Invalid InsertValueInst operands!", &IVI);
3992 
3993   visitInstruction(IVI);
3994 }
3995 
3996 static Value *getParentPad(Value *EHPad) {
3997   if (auto *FPI = dyn_cast<FuncletPadInst>(EHPad))
3998     return FPI->getParentPad();
3999 
4000   return cast<CatchSwitchInst>(EHPad)->getParentPad();
4001 }
4002 
4003 void Verifier::visitEHPadPredecessors(Instruction &I) {
4004   assert(I.isEHPad());
4005 
4006   BasicBlock *BB = I.getParent();
4007   Function *F = BB->getParent();
4008 
4009   Check(BB != &F->getEntryBlock(), "EH pad cannot be in entry block.", &I);
4010 
4011   if (auto *LPI = dyn_cast<LandingPadInst>(&I)) {
4012     // The landingpad instruction defines its parent as a landing pad block. The
4013     // landing pad block may be branched to only by the unwind edge of an
4014     // invoke.
4015     for (BasicBlock *PredBB : predecessors(BB)) {
4016       const auto *II = dyn_cast<InvokeInst>(PredBB->getTerminator());
4017       Check(II && II->getUnwindDest() == BB && II->getNormalDest() != BB,
4018             "Block containing LandingPadInst must be jumped to "
4019             "only by the unwind edge of an invoke.",
4020             LPI);
4021     }
4022     return;
4023   }
4024   if (auto *CPI = dyn_cast<CatchPadInst>(&I)) {
4025     if (!pred_empty(BB))
4026       Check(BB->getUniquePredecessor() == CPI->getCatchSwitch()->getParent(),
4027             "Block containg CatchPadInst must be jumped to "
4028             "only by its catchswitch.",
4029             CPI);
4030     Check(BB != CPI->getCatchSwitch()->getUnwindDest(),
4031           "Catchswitch cannot unwind to one of its catchpads",
4032           CPI->getCatchSwitch(), CPI);
4033     return;
4034   }
4035 
4036   // Verify that each pred has a legal terminator with a legal to/from EH
4037   // pad relationship.
4038   Instruction *ToPad = &I;
4039   Value *ToPadParent = getParentPad(ToPad);
4040   for (BasicBlock *PredBB : predecessors(BB)) {
4041     Instruction *TI = PredBB->getTerminator();
4042     Value *FromPad;
4043     if (auto *II = dyn_cast<InvokeInst>(TI)) {
4044       Check(II->getUnwindDest() == BB && II->getNormalDest() != BB,
4045             "EH pad must be jumped to via an unwind edge", ToPad, II);
4046       if (auto Bundle = II->getOperandBundle(LLVMContext::OB_funclet))
4047         FromPad = Bundle->Inputs[0];
4048       else
4049         FromPad = ConstantTokenNone::get(II->getContext());
4050     } else if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) {
4051       FromPad = CRI->getOperand(0);
4052       Check(FromPad != ToPadParent, "A cleanupret must exit its cleanup", CRI);
4053     } else if (auto *CSI = dyn_cast<CatchSwitchInst>(TI)) {
4054       FromPad = CSI;
4055     } else {
4056       Check(false, "EH pad must be jumped to via an unwind edge", ToPad, TI);
4057     }
4058 
4059     // The edge may exit from zero or more nested pads.
4060     SmallSet<Value *, 8> Seen;
4061     for (;; FromPad = getParentPad(FromPad)) {
4062       Check(FromPad != ToPad,
4063             "EH pad cannot handle exceptions raised within it", FromPad, TI);
4064       if (FromPad == ToPadParent) {
4065         // This is a legal unwind edge.
4066         break;
4067       }
4068       Check(!isa<ConstantTokenNone>(FromPad),
4069             "A single unwind edge may only enter one EH pad", TI);
4070       Check(Seen.insert(FromPad).second, "EH pad jumps through a cycle of pads",
4071             FromPad);
4072 
4073       // This will be diagnosed on the corresponding instruction already. We
4074       // need the extra check here to make sure getParentPad() works.
4075       Check(isa<FuncletPadInst>(FromPad) || isa<CatchSwitchInst>(FromPad),
4076             "Parent pad must be catchpad/cleanuppad/catchswitch", TI);
4077     }
4078   }
4079 }
4080 
4081 void Verifier::visitLandingPadInst(LandingPadInst &LPI) {
4082   // The landingpad instruction is ill-formed if it doesn't have any clauses and
4083   // isn't a cleanup.
4084   Check(LPI.getNumClauses() > 0 || LPI.isCleanup(),
4085         "LandingPadInst needs at least one clause or to be a cleanup.", &LPI);
4086 
4087   visitEHPadPredecessors(LPI);
4088 
4089   if (!LandingPadResultTy)
4090     LandingPadResultTy = LPI.getType();
4091   else
4092     Check(LandingPadResultTy == LPI.getType(),
4093           "The landingpad instruction should have a consistent result type "
4094           "inside a function.",
4095           &LPI);
4096 
4097   Function *F = LPI.getParent()->getParent();
4098   Check(F->hasPersonalityFn(),
4099         "LandingPadInst needs to be in a function with a personality.", &LPI);
4100 
4101   // The landingpad instruction must be the first non-PHI instruction in the
4102   // block.
4103   Check(LPI.getParent()->getLandingPadInst() == &LPI,
4104         "LandingPadInst not the first non-PHI instruction in the block.", &LPI);
4105 
4106   for (unsigned i = 0, e = LPI.getNumClauses(); i < e; ++i) {
4107     Constant *Clause = LPI.getClause(i);
4108     if (LPI.isCatch(i)) {
4109       Check(isa<PointerType>(Clause->getType()),
4110             "Catch operand does not have pointer type!", &LPI);
4111     } else {
4112       Check(LPI.isFilter(i), "Clause is neither catch nor filter!", &LPI);
4113       Check(isa<ConstantArray>(Clause) || isa<ConstantAggregateZero>(Clause),
4114             "Filter operand is not an array of constants!", &LPI);
4115     }
4116   }
4117 
4118   visitInstruction(LPI);
4119 }
4120 
4121 void Verifier::visitResumeInst(ResumeInst &RI) {
4122   Check(RI.getFunction()->hasPersonalityFn(),
4123         "ResumeInst needs to be in a function with a personality.", &RI);
4124 
4125   if (!LandingPadResultTy)
4126     LandingPadResultTy = RI.getValue()->getType();
4127   else
4128     Check(LandingPadResultTy == RI.getValue()->getType(),
4129           "The resume instruction should have a consistent result type "
4130           "inside a function.",
4131           &RI);
4132 
4133   visitTerminator(RI);
4134 }
4135 
4136 void Verifier::visitCatchPadInst(CatchPadInst &CPI) {
4137   BasicBlock *BB = CPI.getParent();
4138 
4139   Function *F = BB->getParent();
4140   Check(F->hasPersonalityFn(),
4141         "CatchPadInst needs to be in a function with a personality.", &CPI);
4142 
4143   Check(isa<CatchSwitchInst>(CPI.getParentPad()),
4144         "CatchPadInst needs to be directly nested in a CatchSwitchInst.",
4145         CPI.getParentPad());
4146 
4147   // The catchpad instruction must be the first non-PHI instruction in the
4148   // block.
4149   Check(BB->getFirstNonPHI() == &CPI,
4150         "CatchPadInst not the first non-PHI instruction in the block.", &CPI);
4151 
4152   visitEHPadPredecessors(CPI);
4153   visitFuncletPadInst(CPI);
4154 }
4155 
4156 void Verifier::visitCatchReturnInst(CatchReturnInst &CatchReturn) {
4157   Check(isa<CatchPadInst>(CatchReturn.getOperand(0)),
4158         "CatchReturnInst needs to be provided a CatchPad", &CatchReturn,
4159         CatchReturn.getOperand(0));
4160 
4161   visitTerminator(CatchReturn);
4162 }
4163 
4164 void Verifier::visitCleanupPadInst(CleanupPadInst &CPI) {
4165   BasicBlock *BB = CPI.getParent();
4166 
4167   Function *F = BB->getParent();
4168   Check(F->hasPersonalityFn(),
4169         "CleanupPadInst needs to be in a function with a personality.", &CPI);
4170 
4171   // The cleanuppad instruction must be the first non-PHI instruction in the
4172   // block.
4173   Check(BB->getFirstNonPHI() == &CPI,
4174         "CleanupPadInst not the first non-PHI instruction in the block.", &CPI);
4175 
4176   auto *ParentPad = CPI.getParentPad();
4177   Check(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
4178         "CleanupPadInst has an invalid parent.", &CPI);
4179 
4180   visitEHPadPredecessors(CPI);
4181   visitFuncletPadInst(CPI);
4182 }
4183 
4184 void Verifier::visitFuncletPadInst(FuncletPadInst &FPI) {
4185   User *FirstUser = nullptr;
4186   Value *FirstUnwindPad = nullptr;
4187   SmallVector<FuncletPadInst *, 8> Worklist({&FPI});
4188   SmallSet<FuncletPadInst *, 8> Seen;
4189 
4190   while (!Worklist.empty()) {
4191     FuncletPadInst *CurrentPad = Worklist.pop_back_val();
4192     Check(Seen.insert(CurrentPad).second,
4193           "FuncletPadInst must not be nested within itself", CurrentPad);
4194     Value *UnresolvedAncestorPad = nullptr;
4195     for (User *U : CurrentPad->users()) {
4196       BasicBlock *UnwindDest;
4197       if (auto *CRI = dyn_cast<CleanupReturnInst>(U)) {
4198         UnwindDest = CRI->getUnwindDest();
4199       } else if (auto *CSI = dyn_cast<CatchSwitchInst>(U)) {
4200         // We allow catchswitch unwind to caller to nest
4201         // within an outer pad that unwinds somewhere else,
4202         // because catchswitch doesn't have a nounwind variant.
4203         // See e.g. SimplifyCFGOpt::SimplifyUnreachable.
4204         if (CSI->unwindsToCaller())
4205           continue;
4206         UnwindDest = CSI->getUnwindDest();
4207       } else if (auto *II = dyn_cast<InvokeInst>(U)) {
4208         UnwindDest = II->getUnwindDest();
4209       } else if (isa<CallInst>(U)) {
4210         // Calls which don't unwind may be found inside funclet
4211         // pads that unwind somewhere else.  We don't *require*
4212         // such calls to be annotated nounwind.
4213         continue;
4214       } else if (auto *CPI = dyn_cast<CleanupPadInst>(U)) {
4215         // The unwind dest for a cleanup can only be found by
4216         // recursive search.  Add it to the worklist, and we'll
4217         // search for its first use that determines where it unwinds.
4218         Worklist.push_back(CPI);
4219         continue;
4220       } else {
4221         Check(isa<CatchReturnInst>(U), "Bogus funclet pad use", U);
4222         continue;
4223       }
4224 
4225       Value *UnwindPad;
4226       bool ExitsFPI;
4227       if (UnwindDest) {
4228         UnwindPad = UnwindDest->getFirstNonPHI();
4229         if (!cast<Instruction>(UnwindPad)->isEHPad())
4230           continue;
4231         Value *UnwindParent = getParentPad(UnwindPad);
4232         // Ignore unwind edges that don't exit CurrentPad.
4233         if (UnwindParent == CurrentPad)
4234           continue;
4235         // Determine whether the original funclet pad is exited,
4236         // and if we are scanning nested pads determine how many
4237         // of them are exited so we can stop searching their
4238         // children.
4239         Value *ExitedPad = CurrentPad;
4240         ExitsFPI = false;
4241         do {
4242           if (ExitedPad == &FPI) {
4243             ExitsFPI = true;
4244             // Now we can resolve any ancestors of CurrentPad up to
4245             // FPI, but not including FPI since we need to make sure
4246             // to check all direct users of FPI for consistency.
4247             UnresolvedAncestorPad = &FPI;
4248             break;
4249           }
4250           Value *ExitedParent = getParentPad(ExitedPad);
4251           if (ExitedParent == UnwindParent) {
4252             // ExitedPad is the ancestor-most pad which this unwind
4253             // edge exits, so we can resolve up to it, meaning that
4254             // ExitedParent is the first ancestor still unresolved.
4255             UnresolvedAncestorPad = ExitedParent;
4256             break;
4257           }
4258           ExitedPad = ExitedParent;
4259         } while (!isa<ConstantTokenNone>(ExitedPad));
4260       } else {
4261         // Unwinding to caller exits all pads.
4262         UnwindPad = ConstantTokenNone::get(FPI.getContext());
4263         ExitsFPI = true;
4264         UnresolvedAncestorPad = &FPI;
4265       }
4266 
4267       if (ExitsFPI) {
4268         // This unwind edge exits FPI.  Make sure it agrees with other
4269         // such edges.
4270         if (FirstUser) {
4271           Check(UnwindPad == FirstUnwindPad,
4272                 "Unwind edges out of a funclet "
4273                 "pad must have the same unwind "
4274                 "dest",
4275                 &FPI, U, FirstUser);
4276         } else {
4277           FirstUser = U;
4278           FirstUnwindPad = UnwindPad;
4279           // Record cleanup sibling unwinds for verifySiblingFuncletUnwinds
4280           if (isa<CleanupPadInst>(&FPI) && !isa<ConstantTokenNone>(UnwindPad) &&
4281               getParentPad(UnwindPad) == getParentPad(&FPI))
4282             SiblingFuncletInfo[&FPI] = cast<Instruction>(U);
4283         }
4284       }
4285       // Make sure we visit all uses of FPI, but for nested pads stop as
4286       // soon as we know where they unwind to.
4287       if (CurrentPad != &FPI)
4288         break;
4289     }
4290     if (UnresolvedAncestorPad) {
4291       if (CurrentPad == UnresolvedAncestorPad) {
4292         // When CurrentPad is FPI itself, we don't mark it as resolved even if
4293         // we've found an unwind edge that exits it, because we need to verify
4294         // all direct uses of FPI.
4295         assert(CurrentPad == &FPI);
4296         continue;
4297       }
4298       // Pop off the worklist any nested pads that we've found an unwind
4299       // destination for.  The pads on the worklist are the uncles,
4300       // great-uncles, etc. of CurrentPad.  We've found an unwind destination
4301       // for all ancestors of CurrentPad up to but not including
4302       // UnresolvedAncestorPad.
4303       Value *ResolvedPad = CurrentPad;
4304       while (!Worklist.empty()) {
4305         Value *UnclePad = Worklist.back();
4306         Value *AncestorPad = getParentPad(UnclePad);
4307         // Walk ResolvedPad up the ancestor list until we either find the
4308         // uncle's parent or the last resolved ancestor.
4309         while (ResolvedPad != AncestorPad) {
4310           Value *ResolvedParent = getParentPad(ResolvedPad);
4311           if (ResolvedParent == UnresolvedAncestorPad) {
4312             break;
4313           }
4314           ResolvedPad = ResolvedParent;
4315         }
4316         // If the resolved ancestor search didn't find the uncle's parent,
4317         // then the uncle is not yet resolved.
4318         if (ResolvedPad != AncestorPad)
4319           break;
4320         // This uncle is resolved, so pop it from the worklist.
4321         Worklist.pop_back();
4322       }
4323     }
4324   }
4325 
4326   if (FirstUnwindPad) {
4327     if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FPI.getParentPad())) {
4328       BasicBlock *SwitchUnwindDest = CatchSwitch->getUnwindDest();
4329       Value *SwitchUnwindPad;
4330       if (SwitchUnwindDest)
4331         SwitchUnwindPad = SwitchUnwindDest->getFirstNonPHI();
4332       else
4333         SwitchUnwindPad = ConstantTokenNone::get(FPI.getContext());
4334       Check(SwitchUnwindPad == FirstUnwindPad,
4335             "Unwind edges out of a catch must have the same unwind dest as "
4336             "the parent catchswitch",
4337             &FPI, FirstUser, CatchSwitch);
4338     }
4339   }
4340 
4341   visitInstruction(FPI);
4342 }
4343 
4344 void Verifier::visitCatchSwitchInst(CatchSwitchInst &CatchSwitch) {
4345   BasicBlock *BB = CatchSwitch.getParent();
4346 
4347   Function *F = BB->getParent();
4348   Check(F->hasPersonalityFn(),
4349         "CatchSwitchInst needs to be in a function with a personality.",
4350         &CatchSwitch);
4351 
4352   // The catchswitch instruction must be the first non-PHI instruction in the
4353   // block.
4354   Check(BB->getFirstNonPHI() == &CatchSwitch,
4355         "CatchSwitchInst not the first non-PHI instruction in the block.",
4356         &CatchSwitch);
4357 
4358   auto *ParentPad = CatchSwitch.getParentPad();
4359   Check(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
4360         "CatchSwitchInst has an invalid parent.", ParentPad);
4361 
4362   if (BasicBlock *UnwindDest = CatchSwitch.getUnwindDest()) {
4363     Instruction *I = UnwindDest->getFirstNonPHI();
4364     Check(I->isEHPad() && !isa<LandingPadInst>(I),
4365           "CatchSwitchInst must unwind to an EH block which is not a "
4366           "landingpad.",
4367           &CatchSwitch);
4368 
4369     // Record catchswitch sibling unwinds for verifySiblingFuncletUnwinds
4370     if (getParentPad(I) == ParentPad)
4371       SiblingFuncletInfo[&CatchSwitch] = &CatchSwitch;
4372   }
4373 
4374   Check(CatchSwitch.getNumHandlers() != 0,
4375         "CatchSwitchInst cannot have empty handler list", &CatchSwitch);
4376 
4377   for (BasicBlock *Handler : CatchSwitch.handlers()) {
4378     Check(isa<CatchPadInst>(Handler->getFirstNonPHI()),
4379           "CatchSwitchInst handlers must be catchpads", &CatchSwitch, Handler);
4380   }
4381 
4382   visitEHPadPredecessors(CatchSwitch);
4383   visitTerminator(CatchSwitch);
4384 }
4385 
4386 void Verifier::visitCleanupReturnInst(CleanupReturnInst &CRI) {
4387   Check(isa<CleanupPadInst>(CRI.getOperand(0)),
4388         "CleanupReturnInst needs to be provided a CleanupPad", &CRI,
4389         CRI.getOperand(0));
4390 
4391   if (BasicBlock *UnwindDest = CRI.getUnwindDest()) {
4392     Instruction *I = UnwindDest->getFirstNonPHI();
4393     Check(I->isEHPad() && !isa<LandingPadInst>(I),
4394           "CleanupReturnInst must unwind to an EH block which is not a "
4395           "landingpad.",
4396           &CRI);
4397   }
4398 
4399   visitTerminator(CRI);
4400 }
4401 
4402 void Verifier::verifyDominatesUse(Instruction &I, unsigned i) {
4403   Instruction *Op = cast<Instruction>(I.getOperand(i));
4404   // If the we have an invalid invoke, don't try to compute the dominance.
4405   // We already reject it in the invoke specific checks and the dominance
4406   // computation doesn't handle multiple edges.
4407   if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
4408     if (II->getNormalDest() == II->getUnwindDest())
4409       return;
4410   }
4411 
4412   // Quick check whether the def has already been encountered in the same block.
4413   // PHI nodes are not checked to prevent accepting preceding PHIs, because PHI
4414   // uses are defined to happen on the incoming edge, not at the instruction.
4415   //
4416   // FIXME: If this operand is a MetadataAsValue (wrapping a LocalAsMetadata)
4417   // wrapping an SSA value, assert that we've already encountered it.  See
4418   // related FIXME in Mapper::mapLocalAsMetadata in ValueMapper.cpp.
4419   if (!isa<PHINode>(I) && InstsInThisBlock.count(Op))
4420     return;
4421 
4422   const Use &U = I.getOperandUse(i);
4423   Check(DT.dominates(Op, U), "Instruction does not dominate all uses!", Op, &I);
4424 }
4425 
4426 void Verifier::visitDereferenceableMetadata(Instruction& I, MDNode* MD) {
4427   Check(I.getType()->isPointerTy(),
4428         "dereferenceable, dereferenceable_or_null "
4429         "apply only to pointer types",
4430         &I);
4431   Check((isa<LoadInst>(I) || isa<IntToPtrInst>(I)),
4432         "dereferenceable, dereferenceable_or_null apply only to load"
4433         " and inttoptr instructions, use attributes for calls or invokes",
4434         &I);
4435   Check(MD->getNumOperands() == 1,
4436         "dereferenceable, dereferenceable_or_null "
4437         "take one operand!",
4438         &I);
4439   ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(MD->getOperand(0));
4440   Check(CI && CI->getType()->isIntegerTy(64),
4441         "dereferenceable, "
4442         "dereferenceable_or_null metadata value must be an i64!",
4443         &I);
4444 }
4445 
4446 void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
4447   Check(MD->getNumOperands() >= 2,
4448         "!prof annotations should have no less than 2 operands", MD);
4449 
4450   // Check first operand.
4451   Check(MD->getOperand(0) != nullptr, "first operand should not be null", MD);
4452   Check(isa<MDString>(MD->getOperand(0)),
4453         "expected string with name of the !prof annotation", MD);
4454   MDString *MDS = cast<MDString>(MD->getOperand(0));
4455   StringRef ProfName = MDS->getString();
4456 
4457   // Check consistency of !prof branch_weights metadata.
4458   if (ProfName.equals("branch_weights")) {
4459     if (isa<InvokeInst>(&I)) {
4460       Check(MD->getNumOperands() == 2 || MD->getNumOperands() == 3,
4461             "Wrong number of InvokeInst branch_weights operands", MD);
4462     } else {
4463       unsigned ExpectedNumOperands = 0;
4464       if (BranchInst *BI = dyn_cast<BranchInst>(&I))
4465         ExpectedNumOperands = BI->getNumSuccessors();
4466       else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I))
4467         ExpectedNumOperands = SI->getNumSuccessors();
4468       else if (isa<CallInst>(&I))
4469         ExpectedNumOperands = 1;
4470       else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I))
4471         ExpectedNumOperands = IBI->getNumDestinations();
4472       else if (isa<SelectInst>(&I))
4473         ExpectedNumOperands = 2;
4474       else
4475         CheckFailed("!prof branch_weights are not allowed for this instruction",
4476                     MD);
4477 
4478       Check(MD->getNumOperands() == 1 + ExpectedNumOperands,
4479             "Wrong number of operands", MD);
4480     }
4481     for (unsigned i = 1; i < MD->getNumOperands(); ++i) {
4482       auto &MDO = MD->getOperand(i);
4483       Check(MDO, "second operand should not be null", MD);
4484       Check(mdconst::dyn_extract<ConstantInt>(MDO),
4485             "!prof brunch_weights operand is not a const int");
4486     }
4487   }
4488 }
4489 
4490 void Verifier::visitAnnotationMetadata(MDNode *Annotation) {
4491   Check(isa<MDTuple>(Annotation), "annotation must be a tuple");
4492   Check(Annotation->getNumOperands() >= 1,
4493         "annotation must have at least one operand");
4494   for (const MDOperand &Op : Annotation->operands())
4495     Check(isa<MDString>(Op.get()), "operands must be strings");
4496 }
4497 
4498 void Verifier::visitAliasScopeMetadata(const MDNode *MD) {
4499   unsigned NumOps = MD->getNumOperands();
4500   Check(NumOps >= 2 && NumOps <= 3, "scope must have two or three operands",
4501         MD);
4502   Check(MD->getOperand(0).get() == MD || isa<MDString>(MD->getOperand(0)),
4503         "first scope operand must be self-referential or string", MD);
4504   if (NumOps == 3)
4505     Check(isa<MDString>(MD->getOperand(2)),
4506           "third scope operand must be string (if used)", MD);
4507 
4508   MDNode *Domain = dyn_cast<MDNode>(MD->getOperand(1));
4509   Check(Domain != nullptr, "second scope operand must be MDNode", MD);
4510 
4511   unsigned NumDomainOps = Domain->getNumOperands();
4512   Check(NumDomainOps >= 1 && NumDomainOps <= 2,
4513         "domain must have one or two operands", Domain);
4514   Check(Domain->getOperand(0).get() == Domain ||
4515             isa<MDString>(Domain->getOperand(0)),
4516         "first domain operand must be self-referential or string", Domain);
4517   if (NumDomainOps == 2)
4518     Check(isa<MDString>(Domain->getOperand(1)),
4519           "second domain operand must be string (if used)", Domain);
4520 }
4521 
4522 void Verifier::visitAliasScopeListMetadata(const MDNode *MD) {
4523   for (const MDOperand &Op : MD->operands()) {
4524     const MDNode *OpMD = dyn_cast<MDNode>(Op);
4525     Check(OpMD != nullptr, "scope list must consist of MDNodes", MD);
4526     visitAliasScopeMetadata(OpMD);
4527   }
4528 }
4529 
4530 void Verifier::visitAccessGroupMetadata(const MDNode *MD) {
4531   auto IsValidAccessScope = [](const MDNode *MD) {
4532     return MD->getNumOperands() == 0 && MD->isDistinct();
4533   };
4534 
4535   // It must be either an access scope itself...
4536   if (IsValidAccessScope(MD))
4537     return;
4538 
4539   // ...or a list of access scopes.
4540   for (const MDOperand &Op : MD->operands()) {
4541     const MDNode *OpMD = dyn_cast<MDNode>(Op);
4542     Check(OpMD != nullptr, "Access scope list must consist of MDNodes", MD);
4543     Check(IsValidAccessScope(OpMD),
4544           "Access scope list contains invalid access scope", MD);
4545   }
4546 }
4547 
4548 /// verifyInstruction - Verify that an instruction is well formed.
4549 ///
4550 void Verifier::visitInstruction(Instruction &I) {
4551   BasicBlock *BB = I.getParent();
4552   Check(BB, "Instruction not embedded in basic block!", &I);
4553 
4554   if (!isa<PHINode>(I)) {   // Check that non-phi nodes are not self referential
4555     for (User *U : I.users()) {
4556       Check(U != (User *)&I || !DT.isReachableFromEntry(BB),
4557             "Only PHI nodes may reference their own value!", &I);
4558     }
4559   }
4560 
4561   // Check that void typed values don't have names
4562   Check(!I.getType()->isVoidTy() || !I.hasName(),
4563         "Instruction has a name, but provides a void value!", &I);
4564 
4565   // Check that the return value of the instruction is either void or a legal
4566   // value type.
4567   Check(I.getType()->isVoidTy() || I.getType()->isFirstClassType(),
4568         "Instruction returns a non-scalar type!", &I);
4569 
4570   // Check that the instruction doesn't produce metadata. Calls are already
4571   // checked against the callee type.
4572   Check(!I.getType()->isMetadataTy() || isa<CallInst>(I) || isa<InvokeInst>(I),
4573         "Invalid use of metadata!", &I);
4574 
4575   // Check that all uses of the instruction, if they are instructions
4576   // themselves, actually have parent basic blocks.  If the use is not an
4577   // instruction, it is an error!
4578   for (Use &U : I.uses()) {
4579     if (Instruction *Used = dyn_cast<Instruction>(U.getUser()))
4580       Check(Used->getParent() != nullptr,
4581             "Instruction referencing"
4582             " instruction not embedded in a basic block!",
4583             &I, Used);
4584     else {
4585       CheckFailed("Use of instruction is not an instruction!", U);
4586       return;
4587     }
4588   }
4589 
4590   // Get a pointer to the call base of the instruction if it is some form of
4591   // call.
4592   const CallBase *CBI = dyn_cast<CallBase>(&I);
4593 
4594   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
4595     Check(I.getOperand(i) != nullptr, "Instruction has null operand!", &I);
4596 
4597     // Check to make sure that only first-class-values are operands to
4598     // instructions.
4599     if (!I.getOperand(i)->getType()->isFirstClassType()) {
4600       Check(false, "Instruction operands must be first-class values!", &I);
4601     }
4602 
4603     if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
4604       // This code checks whether the function is used as the operand of a
4605       // clang_arc_attachedcall operand bundle.
4606       auto IsAttachedCallOperand = [](Function *F, const CallBase *CBI,
4607                                       int Idx) {
4608         return CBI && CBI->isOperandBundleOfType(
4609                           LLVMContext::OB_clang_arc_attachedcall, Idx);
4610       };
4611 
4612       // Check to make sure that the "address of" an intrinsic function is never
4613       // taken. Ignore cases where the address of the intrinsic function is used
4614       // as the argument of operand bundle "clang.arc.attachedcall" as those
4615       // cases are handled in verifyAttachedCallBundle.
4616       Check((!F->isIntrinsic() ||
4617              (CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i)) ||
4618              IsAttachedCallOperand(F, CBI, i)),
4619             "Cannot take the address of an intrinsic!", &I);
4620       Check(!F->isIntrinsic() || isa<CallInst>(I) ||
4621                 F->getIntrinsicID() == Intrinsic::donothing ||
4622                 F->getIntrinsicID() == Intrinsic::seh_try_begin ||
4623                 F->getIntrinsicID() == Intrinsic::seh_try_end ||
4624                 F->getIntrinsicID() == Intrinsic::seh_scope_begin ||
4625                 F->getIntrinsicID() == Intrinsic::seh_scope_end ||
4626                 F->getIntrinsicID() == Intrinsic::coro_resume ||
4627                 F->getIntrinsicID() == Intrinsic::coro_destroy ||
4628                 F->getIntrinsicID() ==
4629                     Intrinsic::experimental_patchpoint_void ||
4630                 F->getIntrinsicID() == Intrinsic::experimental_patchpoint_i64 ||
4631                 F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint ||
4632                 F->getIntrinsicID() == Intrinsic::wasm_rethrow ||
4633                 IsAttachedCallOperand(F, CBI, i),
4634             "Cannot invoke an intrinsic other than donothing, patchpoint, "
4635             "statepoint, coro_resume, coro_destroy or clang.arc.attachedcall",
4636             &I);
4637       Check(F->getParent() == &M, "Referencing function in another module!", &I,
4638             &M, F, F->getParent());
4639     } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
4640       Check(OpBB->getParent() == BB->getParent(),
4641             "Referring to a basic block in another function!", &I);
4642     } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
4643       Check(OpArg->getParent() == BB->getParent(),
4644             "Referring to an argument in another function!", &I);
4645     } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) {
4646       Check(GV->getParent() == &M, "Referencing global in another module!", &I,
4647             &M, GV, GV->getParent());
4648     } else if (isa<Instruction>(I.getOperand(i))) {
4649       verifyDominatesUse(I, i);
4650     } else if (isa<InlineAsm>(I.getOperand(i))) {
4651       Check(CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i),
4652             "Cannot take the address of an inline asm!", &I);
4653     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(i))) {
4654       if (CE->getType()->isPtrOrPtrVectorTy()) {
4655         // If we have a ConstantExpr pointer, we need to see if it came from an
4656         // illegal bitcast.
4657         visitConstantExprsRecursively(CE);
4658       }
4659     }
4660   }
4661 
4662   if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpmath)) {
4663     Check(I.getType()->isFPOrFPVectorTy(),
4664           "fpmath requires a floating point result!", &I);
4665     Check(MD->getNumOperands() == 1, "fpmath takes one operand!", &I);
4666     if (ConstantFP *CFP0 =
4667             mdconst::dyn_extract_or_null<ConstantFP>(MD->getOperand(0))) {
4668       const APFloat &Accuracy = CFP0->getValueAPF();
4669       Check(&Accuracy.getSemantics() == &APFloat::IEEEsingle(),
4670             "fpmath accuracy must have float type", &I);
4671       Check(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(),
4672             "fpmath accuracy not a positive number!", &I);
4673     } else {
4674       Check(false, "invalid fpmath accuracy!", &I);
4675     }
4676   }
4677 
4678   if (MDNode *Range = I.getMetadata(LLVMContext::MD_range)) {
4679     Check(isa<LoadInst>(I) || isa<CallInst>(I) || isa<InvokeInst>(I),
4680           "Ranges are only for loads, calls and invokes!", &I);
4681     visitRangeMetadata(I, Range, I.getType());
4682   }
4683 
4684   if (I.hasMetadata(LLVMContext::MD_invariant_group)) {
4685     Check(isa<LoadInst>(I) || isa<StoreInst>(I),
4686           "invariant.group metadata is only for loads and stores", &I);
4687   }
4688 
4689   if (I.getMetadata(LLVMContext::MD_nonnull)) {
4690     Check(I.getType()->isPointerTy(), "nonnull applies only to pointer types",
4691           &I);
4692     Check(isa<LoadInst>(I),
4693           "nonnull applies only to load instructions, use attributes"
4694           " for calls or invokes",
4695           &I);
4696   }
4697 
4698   if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable))
4699     visitDereferenceableMetadata(I, MD);
4700 
4701   if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable_or_null))
4702     visitDereferenceableMetadata(I, MD);
4703 
4704   if (MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa))
4705     TBAAVerifyHelper.visitTBAAMetadata(I, TBAA);
4706 
4707   if (MDNode *MD = I.getMetadata(LLVMContext::MD_noalias))
4708     visitAliasScopeListMetadata(MD);
4709   if (MDNode *MD = I.getMetadata(LLVMContext::MD_alias_scope))
4710     visitAliasScopeListMetadata(MD);
4711 
4712   if (MDNode *MD = I.getMetadata(LLVMContext::MD_access_group))
4713     visitAccessGroupMetadata(MD);
4714 
4715   if (MDNode *AlignMD = I.getMetadata(LLVMContext::MD_align)) {
4716     Check(I.getType()->isPointerTy(), "align applies only to pointer types",
4717           &I);
4718     Check(isa<LoadInst>(I),
4719           "align applies only to load instructions, "
4720           "use attributes for calls or invokes",
4721           &I);
4722     Check(AlignMD->getNumOperands() == 1, "align takes one operand!", &I);
4723     ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(AlignMD->getOperand(0));
4724     Check(CI && CI->getType()->isIntegerTy(64),
4725           "align metadata value must be an i64!", &I);
4726     uint64_t Align = CI->getZExtValue();
4727     Check(isPowerOf2_64(Align), "align metadata value must be a power of 2!",
4728           &I);
4729     Check(Align <= Value::MaximumAlignment,
4730           "alignment is larger that implementation defined limit", &I);
4731   }
4732 
4733   if (MDNode *MD = I.getMetadata(LLVMContext::MD_prof))
4734     visitProfMetadata(I, MD);
4735 
4736   if (MDNode *Annotation = I.getMetadata(LLVMContext::MD_annotation))
4737     visitAnnotationMetadata(Annotation);
4738 
4739   if (MDNode *N = I.getDebugLoc().getAsMDNode()) {
4740     CheckDI(isa<DILocation>(N), "invalid !dbg metadata attachment", &I, N);
4741     visitMDNode(*N, AreDebugLocsAllowed::Yes);
4742   }
4743 
4744   if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) {
4745     verifyFragmentExpression(*DII);
4746     verifyNotEntryValue(*DII);
4747   }
4748 
4749   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
4750   I.getAllMetadata(MDs);
4751   for (auto Attachment : MDs) {
4752     unsigned Kind = Attachment.first;
4753     auto AllowLocs =
4754         (Kind == LLVMContext::MD_dbg || Kind == LLVMContext::MD_loop)
4755             ? AreDebugLocsAllowed::Yes
4756             : AreDebugLocsAllowed::No;
4757     visitMDNode(*Attachment.second, AllowLocs);
4758   }
4759 
4760   InstsInThisBlock.insert(&I);
4761 }
4762 
4763 /// Allow intrinsics to be verified in different ways.
4764 void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
4765   Function *IF = Call.getCalledFunction();
4766   Check(IF->isDeclaration(), "Intrinsic functions should never be defined!",
4767         IF);
4768 
4769   // Verify that the intrinsic prototype lines up with what the .td files
4770   // describe.
4771   FunctionType *IFTy = IF->getFunctionType();
4772   bool IsVarArg = IFTy->isVarArg();
4773 
4774   SmallVector<Intrinsic::IITDescriptor, 8> Table;
4775   getIntrinsicInfoTableEntries(ID, Table);
4776   ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
4777 
4778   // Walk the descriptors to extract overloaded types.
4779   SmallVector<Type *, 4> ArgTys;
4780   Intrinsic::MatchIntrinsicTypesResult Res =
4781       Intrinsic::matchIntrinsicSignature(IFTy, TableRef, ArgTys);
4782   Check(Res != Intrinsic::MatchIntrinsicTypes_NoMatchRet,
4783         "Intrinsic has incorrect return type!", IF);
4784   Check(Res != Intrinsic::MatchIntrinsicTypes_NoMatchArg,
4785         "Intrinsic has incorrect argument type!", IF);
4786 
4787   // Verify if the intrinsic call matches the vararg property.
4788   if (IsVarArg)
4789     Check(!Intrinsic::matchIntrinsicVarArg(IsVarArg, TableRef),
4790           "Intrinsic was not defined with variable arguments!", IF);
4791   else
4792     Check(!Intrinsic::matchIntrinsicVarArg(IsVarArg, TableRef),
4793           "Callsite was not defined with variable arguments!", IF);
4794 
4795   // All descriptors should be absorbed by now.
4796   Check(TableRef.empty(), "Intrinsic has too few arguments!", IF);
4797 
4798   // Now that we have the intrinsic ID and the actual argument types (and we
4799   // know they are legal for the intrinsic!) get the intrinsic name through the
4800   // usual means.  This allows us to verify the mangling of argument types into
4801   // the name.
4802   const std::string ExpectedName =
4803       Intrinsic::getName(ID, ArgTys, IF->getParent(), IFTy);
4804   Check(ExpectedName == IF->getName(),
4805         "Intrinsic name not mangled correctly for type arguments! "
4806         "Should be: " +
4807             ExpectedName,
4808         IF);
4809 
4810   // If the intrinsic takes MDNode arguments, verify that they are either global
4811   // or are local to *this* function.
4812   for (Value *V : Call.args()) {
4813     if (auto *MD = dyn_cast<MetadataAsValue>(V))
4814       visitMetadataAsValue(*MD, Call.getCaller());
4815     if (auto *Const = dyn_cast<Constant>(V))
4816       Check(!Const->getType()->isX86_AMXTy(),
4817             "const x86_amx is not allowed in argument!");
4818   }
4819 
4820   switch (ID) {
4821   default:
4822     break;
4823   case Intrinsic::assume: {
4824     for (auto &Elem : Call.bundle_op_infos()) {
4825       Check(Elem.Tag->getKey() == "ignore" ||
4826                 Attribute::isExistingAttribute(Elem.Tag->getKey()),
4827             "tags must be valid attribute names", Call);
4828       Attribute::AttrKind Kind =
4829           Attribute::getAttrKindFromName(Elem.Tag->getKey());
4830       unsigned ArgCount = Elem.End - Elem.Begin;
4831       if (Kind == Attribute::Alignment) {
4832         Check(ArgCount <= 3 && ArgCount >= 2,
4833               "alignment assumptions should have 2 or 3 arguments", Call);
4834         Check(Call.getOperand(Elem.Begin)->getType()->isPointerTy(),
4835               "first argument should be a pointer", Call);
4836         Check(Call.getOperand(Elem.Begin + 1)->getType()->isIntegerTy(),
4837               "second argument should be an integer", Call);
4838         if (ArgCount == 3)
4839           Check(Call.getOperand(Elem.Begin + 2)->getType()->isIntegerTy(),
4840                 "third argument should be an integer if present", Call);
4841         return;
4842       }
4843       Check(ArgCount <= 2, "too many arguments", Call);
4844       if (Kind == Attribute::None)
4845         break;
4846       if (Attribute::isIntAttrKind(Kind)) {
4847         Check(ArgCount == 2, "this attribute should have 2 arguments", Call);
4848         Check(isa<ConstantInt>(Call.getOperand(Elem.Begin + 1)),
4849               "the second argument should be a constant integral value", Call);
4850       } else if (Attribute::canUseAsParamAttr(Kind)) {
4851         Check((ArgCount) == 1, "this attribute should have one argument", Call);
4852       } else if (Attribute::canUseAsFnAttr(Kind)) {
4853         Check((ArgCount) == 0, "this attribute has no argument", Call);
4854       }
4855     }
4856     break;
4857   }
4858   case Intrinsic::coro_id: {
4859     auto *InfoArg = Call.getArgOperand(3)->stripPointerCasts();
4860     if (isa<ConstantPointerNull>(InfoArg))
4861       break;
4862     auto *GV = dyn_cast<GlobalVariable>(InfoArg);
4863     Check(GV && GV->isConstant() && GV->hasDefinitiveInitializer(),
4864           "info argument of llvm.coro.id must refer to an initialized "
4865           "constant");
4866     Constant *Init = GV->getInitializer();
4867     Check(isa<ConstantStruct>(Init) || isa<ConstantArray>(Init),
4868           "info argument of llvm.coro.id must refer to either a struct or "
4869           "an array");
4870     break;
4871   }
4872   case Intrinsic::fptrunc_round: {
4873     // Check the rounding mode
4874     Metadata *MD = nullptr;
4875     auto *MAV = dyn_cast<MetadataAsValue>(Call.getOperand(1));
4876     if (MAV)
4877       MD = MAV->getMetadata();
4878 
4879     Check(MD != nullptr, "missing rounding mode argument", Call);
4880 
4881     Check(isa<MDString>(MD),
4882           ("invalid value for llvm.fptrunc.round metadata operand"
4883            " (the operand should be a string)"),
4884           MD);
4885 
4886     Optional<RoundingMode> RoundMode =
4887         convertStrToRoundingMode(cast<MDString>(MD)->getString());
4888     Check(RoundMode && *RoundMode != RoundingMode::Dynamic,
4889           "unsupported rounding mode argument", Call);
4890     break;
4891   }
4892 #define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
4893 #include "llvm/IR/VPIntrinsics.def"
4894     visitVPIntrinsic(cast<VPIntrinsic>(Call));
4895     break;
4896 #define INSTRUCTION(NAME, NARGS, ROUND_MODE, INTRINSIC)                        \
4897   case Intrinsic::INTRINSIC:
4898 #include "llvm/IR/ConstrainedOps.def"
4899     visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(Call));
4900     break;
4901   case Intrinsic::dbg_declare: // llvm.dbg.declare
4902     Check(isa<MetadataAsValue>(Call.getArgOperand(0)),
4903           "invalid llvm.dbg.declare intrinsic call 1", Call);
4904     visitDbgIntrinsic("declare", cast<DbgVariableIntrinsic>(Call));
4905     break;
4906   case Intrinsic::dbg_addr: // llvm.dbg.addr
4907     visitDbgIntrinsic("addr", cast<DbgVariableIntrinsic>(Call));
4908     break;
4909   case Intrinsic::dbg_value: // llvm.dbg.value
4910     visitDbgIntrinsic("value", cast<DbgVariableIntrinsic>(Call));
4911     break;
4912   case Intrinsic::dbg_label: // llvm.dbg.label
4913     visitDbgLabelIntrinsic("label", cast<DbgLabelInst>(Call));
4914     break;
4915   case Intrinsic::memcpy:
4916   case Intrinsic::memcpy_inline:
4917   case Intrinsic::memmove:
4918   case Intrinsic::memset:
4919   case Intrinsic::memset_inline: {
4920     const auto *MI = cast<MemIntrinsic>(&Call);
4921     auto IsValidAlignment = [&](unsigned Alignment) -> bool {
4922       return Alignment == 0 || isPowerOf2_32(Alignment);
4923     };
4924     Check(IsValidAlignment(MI->getDestAlignment()),
4925           "alignment of arg 0 of memory intrinsic must be 0 or a power of 2",
4926           Call);
4927     if (const auto *MTI = dyn_cast<MemTransferInst>(MI)) {
4928       Check(IsValidAlignment(MTI->getSourceAlignment()),
4929             "alignment of arg 1 of memory intrinsic must be 0 or a power of 2",
4930             Call);
4931     }
4932 
4933     break;
4934   }
4935   case Intrinsic::memcpy_element_unordered_atomic:
4936   case Intrinsic::memmove_element_unordered_atomic:
4937   case Intrinsic::memset_element_unordered_atomic: {
4938     const auto *AMI = cast<AtomicMemIntrinsic>(&Call);
4939 
4940     ConstantInt *ElementSizeCI =
4941         cast<ConstantInt>(AMI->getRawElementSizeInBytes());
4942     const APInt &ElementSizeVal = ElementSizeCI->getValue();
4943     Check(ElementSizeVal.isPowerOf2(),
4944           "element size of the element-wise atomic memory intrinsic "
4945           "must be a power of 2",
4946           Call);
4947 
4948     auto IsValidAlignment = [&](uint64_t Alignment) {
4949       return isPowerOf2_64(Alignment) && ElementSizeVal.ule(Alignment);
4950     };
4951     uint64_t DstAlignment = AMI->getDestAlignment();
4952     Check(IsValidAlignment(DstAlignment),
4953           "incorrect alignment of the destination argument", Call);
4954     if (const auto *AMT = dyn_cast<AtomicMemTransferInst>(AMI)) {
4955       uint64_t SrcAlignment = AMT->getSourceAlignment();
4956       Check(IsValidAlignment(SrcAlignment),
4957             "incorrect alignment of the source argument", Call);
4958     }
4959     break;
4960   }
4961   case Intrinsic::call_preallocated_setup: {
4962     auto *NumArgs = dyn_cast<ConstantInt>(Call.getArgOperand(0));
4963     Check(NumArgs != nullptr,
4964           "llvm.call.preallocated.setup argument must be a constant");
4965     bool FoundCall = false;
4966     for (User *U : Call.users()) {
4967       auto *UseCall = dyn_cast<CallBase>(U);
4968       Check(UseCall != nullptr,
4969             "Uses of llvm.call.preallocated.setup must be calls");
4970       const Function *Fn = UseCall->getCalledFunction();
4971       if (Fn && Fn->getIntrinsicID() == Intrinsic::call_preallocated_arg) {
4972         auto *AllocArgIndex = dyn_cast<ConstantInt>(UseCall->getArgOperand(1));
4973         Check(AllocArgIndex != nullptr,
4974               "llvm.call.preallocated.alloc arg index must be a constant");
4975         auto AllocArgIndexInt = AllocArgIndex->getValue();
4976         Check(AllocArgIndexInt.sge(0) &&
4977                   AllocArgIndexInt.slt(NumArgs->getValue()),
4978               "llvm.call.preallocated.alloc arg index must be between 0 and "
4979               "corresponding "
4980               "llvm.call.preallocated.setup's argument count");
4981       } else if (Fn && Fn->getIntrinsicID() ==
4982                            Intrinsic::call_preallocated_teardown) {
4983         // nothing to do
4984       } else {
4985         Check(!FoundCall, "Can have at most one call corresponding to a "
4986                           "llvm.call.preallocated.setup");
4987         FoundCall = true;
4988         size_t NumPreallocatedArgs = 0;
4989         for (unsigned i = 0; i < UseCall->arg_size(); i++) {
4990           if (UseCall->paramHasAttr(i, Attribute::Preallocated)) {
4991             ++NumPreallocatedArgs;
4992           }
4993         }
4994         Check(NumPreallocatedArgs != 0,
4995               "cannot use preallocated intrinsics on a call without "
4996               "preallocated arguments");
4997         Check(NumArgs->equalsInt(NumPreallocatedArgs),
4998               "llvm.call.preallocated.setup arg size must be equal to number "
4999               "of preallocated arguments "
5000               "at call site",
5001               Call, *UseCall);
5002         // getOperandBundle() cannot be called if more than one of the operand
5003         // bundle exists. There is already a check elsewhere for this, so skip
5004         // here if we see more than one.
5005         if (UseCall->countOperandBundlesOfType(LLVMContext::OB_preallocated) >
5006             1) {
5007           return;
5008         }
5009         auto PreallocatedBundle =
5010             UseCall->getOperandBundle(LLVMContext::OB_preallocated);
5011         Check(PreallocatedBundle,
5012               "Use of llvm.call.preallocated.setup outside intrinsics "
5013               "must be in \"preallocated\" operand bundle");
5014         Check(PreallocatedBundle->Inputs.front().get() == &Call,
5015               "preallocated bundle must have token from corresponding "
5016               "llvm.call.preallocated.setup");
5017       }
5018     }
5019     break;
5020   }
5021   case Intrinsic::call_preallocated_arg: {
5022     auto *Token = dyn_cast<CallBase>(Call.getArgOperand(0));
5023     Check(Token && Token->getCalledFunction()->getIntrinsicID() ==
5024                        Intrinsic::call_preallocated_setup,
5025           "llvm.call.preallocated.arg token argument must be a "
5026           "llvm.call.preallocated.setup");
5027     Check(Call.hasFnAttr(Attribute::Preallocated),
5028           "llvm.call.preallocated.arg must be called with a \"preallocated\" "
5029           "call site attribute");
5030     break;
5031   }
5032   case Intrinsic::call_preallocated_teardown: {
5033     auto *Token = dyn_cast<CallBase>(Call.getArgOperand(0));
5034     Check(Token && Token->getCalledFunction()->getIntrinsicID() ==
5035                        Intrinsic::call_preallocated_setup,
5036           "llvm.call.preallocated.teardown token argument must be a "
5037           "llvm.call.preallocated.setup");
5038     break;
5039   }
5040   case Intrinsic::gcroot:
5041   case Intrinsic::gcwrite:
5042   case Intrinsic::gcread:
5043     if (ID == Intrinsic::gcroot) {
5044       AllocaInst *AI =
5045           dyn_cast<AllocaInst>(Call.getArgOperand(0)->stripPointerCasts());
5046       Check(AI, "llvm.gcroot parameter #1 must be an alloca.", Call);
5047       Check(isa<Constant>(Call.getArgOperand(1)),
5048             "llvm.gcroot parameter #2 must be a constant.", Call);
5049       if (!AI->getAllocatedType()->isPointerTy()) {
5050         Check(!isa<ConstantPointerNull>(Call.getArgOperand(1)),
5051               "llvm.gcroot parameter #1 must either be a pointer alloca, "
5052               "or argument #2 must be a non-null constant.",
5053               Call);
5054       }
5055     }
5056 
5057     Check(Call.getParent()->getParent()->hasGC(),
5058           "Enclosing function does not use GC.", Call);
5059     break;
5060   case Intrinsic::init_trampoline:
5061     Check(isa<Function>(Call.getArgOperand(1)->stripPointerCasts()),
5062           "llvm.init_trampoline parameter #2 must resolve to a function.",
5063           Call);
5064     break;
5065   case Intrinsic::prefetch:
5066     Check(cast<ConstantInt>(Call.getArgOperand(1))->getZExtValue() < 2 &&
5067               cast<ConstantInt>(Call.getArgOperand(2))->getZExtValue() < 4,
5068           "invalid arguments to llvm.prefetch", Call);
5069     break;
5070   case Intrinsic::stackprotector:
5071     Check(isa<AllocaInst>(Call.getArgOperand(1)->stripPointerCasts()),
5072           "llvm.stackprotector parameter #2 must resolve to an alloca.", Call);
5073     break;
5074   case Intrinsic::localescape: {
5075     BasicBlock *BB = Call.getParent();
5076     Check(BB == &BB->getParent()->front(),
5077           "llvm.localescape used outside of entry block", Call);
5078     Check(!SawFrameEscape, "multiple calls to llvm.localescape in one function",
5079           Call);
5080     for (Value *Arg : Call.args()) {
5081       if (isa<ConstantPointerNull>(Arg))
5082         continue; // Null values are allowed as placeholders.
5083       auto *AI = dyn_cast<AllocaInst>(Arg->stripPointerCasts());
5084       Check(AI && AI->isStaticAlloca(),
5085             "llvm.localescape only accepts static allocas", Call);
5086     }
5087     FrameEscapeInfo[BB->getParent()].first = Call.arg_size();
5088     SawFrameEscape = true;
5089     break;
5090   }
5091   case Intrinsic::localrecover: {
5092     Value *FnArg = Call.getArgOperand(0)->stripPointerCasts();
5093     Function *Fn = dyn_cast<Function>(FnArg);
5094     Check(Fn && !Fn->isDeclaration(),
5095           "llvm.localrecover first "
5096           "argument must be function defined in this module",
5097           Call);
5098     auto *IdxArg = cast<ConstantInt>(Call.getArgOperand(2));
5099     auto &Entry = FrameEscapeInfo[Fn];
5100     Entry.second = unsigned(
5101         std::max(uint64_t(Entry.second), IdxArg->getLimitedValue(~0U) + 1));
5102     break;
5103   }
5104 
5105   case Intrinsic::experimental_gc_statepoint:
5106     if (auto *CI = dyn_cast<CallInst>(&Call))
5107       Check(!CI->isInlineAsm(),
5108             "gc.statepoint support for inline assembly unimplemented", CI);
5109     Check(Call.getParent()->getParent()->hasGC(),
5110           "Enclosing function does not use GC.", Call);
5111 
5112     verifyStatepoint(Call);
5113     break;
5114   case Intrinsic::experimental_gc_result: {
5115     Check(Call.getParent()->getParent()->hasGC(),
5116           "Enclosing function does not use GC.", Call);
5117     // Are we tied to a statepoint properly?
5118     const auto *StatepointCall = dyn_cast<CallBase>(Call.getArgOperand(0));
5119     const Function *StatepointFn =
5120         StatepointCall ? StatepointCall->getCalledFunction() : nullptr;
5121     Check(StatepointFn && StatepointFn->isDeclaration() &&
5122               StatepointFn->getIntrinsicID() ==
5123                   Intrinsic::experimental_gc_statepoint,
5124           "gc.result operand #1 must be from a statepoint", Call,
5125           Call.getArgOperand(0));
5126 
5127     // Check that result type matches wrapped callee.
5128     auto *TargetFuncType =
5129         cast<FunctionType>(StatepointCall->getParamElementType(2));
5130     Check(Call.getType() == TargetFuncType->getReturnType(),
5131           "gc.result result type does not match wrapped callee", Call);
5132     break;
5133   }
5134   case Intrinsic::experimental_gc_relocate: {
5135     Check(Call.arg_size() == 3, "wrong number of arguments", Call);
5136 
5137     Check(isa<PointerType>(Call.getType()->getScalarType()),
5138           "gc.relocate must return a pointer or a vector of pointers", Call);
5139 
5140     // Check that this relocate is correctly tied to the statepoint
5141 
5142     // This is case for relocate on the unwinding path of an invoke statepoint
5143     if (LandingPadInst *LandingPad =
5144             dyn_cast<LandingPadInst>(Call.getArgOperand(0))) {
5145 
5146       const BasicBlock *InvokeBB =
5147           LandingPad->getParent()->getUniquePredecessor();
5148 
5149       // Landingpad relocates should have only one predecessor with invoke
5150       // statepoint terminator
5151       Check(InvokeBB, "safepoints should have unique landingpads",
5152             LandingPad->getParent());
5153       Check(InvokeBB->getTerminator(), "safepoint block should be well formed",
5154             InvokeBB);
5155       Check(isa<GCStatepointInst>(InvokeBB->getTerminator()),
5156             "gc relocate should be linked to a statepoint", InvokeBB);
5157     } else {
5158       // In all other cases relocate should be tied to the statepoint directly.
5159       // This covers relocates on a normal return path of invoke statepoint and
5160       // relocates of a call statepoint.
5161       auto *Token = Call.getArgOperand(0);
5162       Check(isa<GCStatepointInst>(Token) || isa<UndefValue>(Token),
5163             "gc relocate is incorrectly tied to the statepoint", Call, Token);
5164     }
5165 
5166     // Verify rest of the relocate arguments.
5167     const Value &StatepointCall = *cast<GCRelocateInst>(Call).getStatepoint();
5168 
5169     // Both the base and derived must be piped through the safepoint.
5170     Value *Base = Call.getArgOperand(1);
5171     Check(isa<ConstantInt>(Base),
5172           "gc.relocate operand #2 must be integer offset", Call);
5173 
5174     Value *Derived = Call.getArgOperand(2);
5175     Check(isa<ConstantInt>(Derived),
5176           "gc.relocate operand #3 must be integer offset", Call);
5177 
5178     const uint64_t BaseIndex = cast<ConstantInt>(Base)->getZExtValue();
5179     const uint64_t DerivedIndex = cast<ConstantInt>(Derived)->getZExtValue();
5180 
5181     // Check the bounds
5182     if (isa<UndefValue>(StatepointCall))
5183       break;
5184     if (auto Opt = cast<GCStatepointInst>(StatepointCall)
5185                        .getOperandBundle(LLVMContext::OB_gc_live)) {
5186       Check(BaseIndex < Opt->Inputs.size(),
5187             "gc.relocate: statepoint base index out of bounds", Call);
5188       Check(DerivedIndex < Opt->Inputs.size(),
5189             "gc.relocate: statepoint derived index out of bounds", Call);
5190     }
5191 
5192     // Relocated value must be either a pointer type or vector-of-pointer type,
5193     // but gc_relocate does not need to return the same pointer type as the
5194     // relocated pointer. It can be casted to the correct type later if it's
5195     // desired. However, they must have the same address space and 'vectorness'
5196     GCRelocateInst &Relocate = cast<GCRelocateInst>(Call);
5197     Check(Relocate.getDerivedPtr()->getType()->isPtrOrPtrVectorTy(),
5198           "gc.relocate: relocated value must be a gc pointer", Call);
5199 
5200     auto ResultType = Call.getType();
5201     auto DerivedType = Relocate.getDerivedPtr()->getType();
5202     Check(ResultType->isVectorTy() == DerivedType->isVectorTy(),
5203           "gc.relocate: vector relocates to vector and pointer to pointer",
5204           Call);
5205     Check(
5206         ResultType->getPointerAddressSpace() ==
5207             DerivedType->getPointerAddressSpace(),
5208         "gc.relocate: relocating a pointer shouldn't change its address space",
5209         Call);
5210     break;
5211   }
5212   case Intrinsic::eh_exceptioncode:
5213   case Intrinsic::eh_exceptionpointer: {
5214     Check(isa<CatchPadInst>(Call.getArgOperand(0)),
5215           "eh.exceptionpointer argument must be a catchpad", Call);
5216     break;
5217   }
5218   case Intrinsic::get_active_lane_mask: {
5219     Check(Call.getType()->isVectorTy(),
5220           "get_active_lane_mask: must return a "
5221           "vector",
5222           Call);
5223     auto *ElemTy = Call.getType()->getScalarType();
5224     Check(ElemTy->isIntegerTy(1),
5225           "get_active_lane_mask: element type is not "
5226           "i1",
5227           Call);
5228     break;
5229   }
5230   case Intrinsic::masked_load: {
5231     Check(Call.getType()->isVectorTy(), "masked_load: must return a vector",
5232           Call);
5233 
5234     Value *Ptr = Call.getArgOperand(0);
5235     ConstantInt *Alignment = cast<ConstantInt>(Call.getArgOperand(1));
5236     Value *Mask = Call.getArgOperand(2);
5237     Value *PassThru = Call.getArgOperand(3);
5238     Check(Mask->getType()->isVectorTy(), "masked_load: mask must be vector",
5239           Call);
5240     Check(Alignment->getValue().isPowerOf2(),
5241           "masked_load: alignment must be a power of 2", Call);
5242 
5243     PointerType *PtrTy = cast<PointerType>(Ptr->getType());
5244     Check(PtrTy->isOpaqueOrPointeeTypeMatches(Call.getType()),
5245           "masked_load: return must match pointer type", Call);
5246     Check(PassThru->getType() == Call.getType(),
5247           "masked_load: pass through and return type must match", Call);
5248     Check(cast<VectorType>(Mask->getType())->getElementCount() ==
5249               cast<VectorType>(Call.getType())->getElementCount(),
5250           "masked_load: vector mask must be same length as return", Call);
5251     break;
5252   }
5253   case Intrinsic::masked_store: {
5254     Value *Val = Call.getArgOperand(0);
5255     Value *Ptr = Call.getArgOperand(1);
5256     ConstantInt *Alignment = cast<ConstantInt>(Call.getArgOperand(2));
5257     Value *Mask = Call.getArgOperand(3);
5258     Check(Mask->getType()->isVectorTy(), "masked_store: mask must be vector",
5259           Call);
5260     Check(Alignment->getValue().isPowerOf2(),
5261           "masked_store: alignment must be a power of 2", Call);
5262 
5263     PointerType *PtrTy = cast<PointerType>(Ptr->getType());
5264     Check(PtrTy->isOpaqueOrPointeeTypeMatches(Val->getType()),
5265           "masked_store: storee must match pointer type", Call);
5266     Check(cast<VectorType>(Mask->getType())->getElementCount() ==
5267               cast<VectorType>(Val->getType())->getElementCount(),
5268           "masked_store: vector mask must be same length as value", Call);
5269     break;
5270   }
5271 
5272   case Intrinsic::masked_gather: {
5273     const APInt &Alignment =
5274         cast<ConstantInt>(Call.getArgOperand(1))->getValue();
5275     Check(Alignment.isZero() || Alignment.isPowerOf2(),
5276           "masked_gather: alignment must be 0 or a power of 2", Call);
5277     break;
5278   }
5279   case Intrinsic::masked_scatter: {
5280     const APInt &Alignment =
5281         cast<ConstantInt>(Call.getArgOperand(2))->getValue();
5282     Check(Alignment.isZero() || Alignment.isPowerOf2(),
5283           "masked_scatter: alignment must be 0 or a power of 2", Call);
5284     break;
5285   }
5286 
5287   case Intrinsic::experimental_guard: {
5288     Check(isa<CallInst>(Call), "experimental_guard cannot be invoked", Call);
5289     Check(Call.countOperandBundlesOfType(LLVMContext::OB_deopt) == 1,
5290           "experimental_guard must have exactly one "
5291           "\"deopt\" operand bundle");
5292     break;
5293   }
5294 
5295   case Intrinsic::experimental_deoptimize: {
5296     Check(isa<CallInst>(Call), "experimental_deoptimize cannot be invoked",
5297           Call);
5298     Check(Call.countOperandBundlesOfType(LLVMContext::OB_deopt) == 1,
5299           "experimental_deoptimize must have exactly one "
5300           "\"deopt\" operand bundle");
5301     Check(Call.getType() == Call.getFunction()->getReturnType(),
5302           "experimental_deoptimize return type must match caller return type");
5303 
5304     if (isa<CallInst>(Call)) {
5305       auto *RI = dyn_cast<ReturnInst>(Call.getNextNode());
5306       Check(RI,
5307             "calls to experimental_deoptimize must be followed by a return");
5308 
5309       if (!Call.getType()->isVoidTy() && RI)
5310         Check(RI->getReturnValue() == &Call,
5311               "calls to experimental_deoptimize must be followed by a return "
5312               "of the value computed by experimental_deoptimize");
5313     }
5314 
5315     break;
5316   }
5317   case Intrinsic::vector_reduce_and:
5318   case Intrinsic::vector_reduce_or:
5319   case Intrinsic::vector_reduce_xor:
5320   case Intrinsic::vector_reduce_add:
5321   case Intrinsic::vector_reduce_mul:
5322   case Intrinsic::vector_reduce_smax:
5323   case Intrinsic::vector_reduce_smin:
5324   case Intrinsic::vector_reduce_umax:
5325   case Intrinsic::vector_reduce_umin: {
5326     Type *ArgTy = Call.getArgOperand(0)->getType();
5327     Check(ArgTy->isIntOrIntVectorTy() && ArgTy->isVectorTy(),
5328           "Intrinsic has incorrect argument type!");
5329     break;
5330   }
5331   case Intrinsic::vector_reduce_fmax:
5332   case Intrinsic::vector_reduce_fmin: {
5333     Type *ArgTy = Call.getArgOperand(0)->getType();
5334     Check(ArgTy->isFPOrFPVectorTy() && ArgTy->isVectorTy(),
5335           "Intrinsic has incorrect argument type!");
5336     break;
5337   }
5338   case Intrinsic::vector_reduce_fadd:
5339   case Intrinsic::vector_reduce_fmul: {
5340     // Unlike the other reductions, the first argument is a start value. The
5341     // second argument is the vector to be reduced.
5342     Type *ArgTy = Call.getArgOperand(1)->getType();
5343     Check(ArgTy->isFPOrFPVectorTy() && ArgTy->isVectorTy(),
5344           "Intrinsic has incorrect argument type!");
5345     break;
5346   }
5347   case Intrinsic::smul_fix:
5348   case Intrinsic::smul_fix_sat:
5349   case Intrinsic::umul_fix:
5350   case Intrinsic::umul_fix_sat:
5351   case Intrinsic::sdiv_fix:
5352   case Intrinsic::sdiv_fix_sat:
5353   case Intrinsic::udiv_fix:
5354   case Intrinsic::udiv_fix_sat: {
5355     Value *Op1 = Call.getArgOperand(0);
5356     Value *Op2 = Call.getArgOperand(1);
5357     Check(Op1->getType()->isIntOrIntVectorTy(),
5358           "first operand of [us][mul|div]_fix[_sat] must be an int type or "
5359           "vector of ints");
5360     Check(Op2->getType()->isIntOrIntVectorTy(),
5361           "second operand of [us][mul|div]_fix[_sat] must be an int type or "
5362           "vector of ints");
5363 
5364     auto *Op3 = cast<ConstantInt>(Call.getArgOperand(2));
5365     Check(Op3->getType()->getBitWidth() <= 32,
5366           "third argument of [us][mul|div]_fix[_sat] must fit within 32 bits");
5367 
5368     if (ID == Intrinsic::smul_fix || ID == Intrinsic::smul_fix_sat ||
5369         ID == Intrinsic::sdiv_fix || ID == Intrinsic::sdiv_fix_sat) {
5370       Check(Op3->getZExtValue() < Op1->getType()->getScalarSizeInBits(),
5371             "the scale of s[mul|div]_fix[_sat] must be less than the width of "
5372             "the operands");
5373     } else {
5374       Check(Op3->getZExtValue() <= Op1->getType()->getScalarSizeInBits(),
5375             "the scale of u[mul|div]_fix[_sat] must be less than or equal "
5376             "to the width of the operands");
5377     }
5378     break;
5379   }
5380   case Intrinsic::lround:
5381   case Intrinsic::llround:
5382   case Intrinsic::lrint:
5383   case Intrinsic::llrint: {
5384     Type *ValTy = Call.getArgOperand(0)->getType();
5385     Type *ResultTy = Call.getType();
5386     Check(!ValTy->isVectorTy() && !ResultTy->isVectorTy(),
5387           "Intrinsic does not support vectors", &Call);
5388     break;
5389   }
5390   case Intrinsic::bswap: {
5391     Type *Ty = Call.getType();
5392     unsigned Size = Ty->getScalarSizeInBits();
5393     Check(Size % 16 == 0, "bswap must be an even number of bytes", &Call);
5394     break;
5395   }
5396   case Intrinsic::invariant_start: {
5397     ConstantInt *InvariantSize = dyn_cast<ConstantInt>(Call.getArgOperand(0));
5398     Check(InvariantSize &&
5399               (!InvariantSize->isNegative() || InvariantSize->isMinusOne()),
5400           "invariant_start parameter must be -1, 0 or a positive number",
5401           &Call);
5402     break;
5403   }
5404   case Intrinsic::matrix_multiply:
5405   case Intrinsic::matrix_transpose:
5406   case Intrinsic::matrix_column_major_load:
5407   case Intrinsic::matrix_column_major_store: {
5408     Function *IF = Call.getCalledFunction();
5409     ConstantInt *Stride = nullptr;
5410     ConstantInt *NumRows;
5411     ConstantInt *NumColumns;
5412     VectorType *ResultTy;
5413     Type *Op0ElemTy = nullptr;
5414     Type *Op1ElemTy = nullptr;
5415     switch (ID) {
5416     case Intrinsic::matrix_multiply:
5417       NumRows = cast<ConstantInt>(Call.getArgOperand(2));
5418       NumColumns = cast<ConstantInt>(Call.getArgOperand(4));
5419       ResultTy = cast<VectorType>(Call.getType());
5420       Op0ElemTy =
5421           cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType();
5422       Op1ElemTy =
5423           cast<VectorType>(Call.getArgOperand(1)->getType())->getElementType();
5424       break;
5425     case Intrinsic::matrix_transpose:
5426       NumRows = cast<ConstantInt>(Call.getArgOperand(1));
5427       NumColumns = cast<ConstantInt>(Call.getArgOperand(2));
5428       ResultTy = cast<VectorType>(Call.getType());
5429       Op0ElemTy =
5430           cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType();
5431       break;
5432     case Intrinsic::matrix_column_major_load: {
5433       Stride = dyn_cast<ConstantInt>(Call.getArgOperand(1));
5434       NumRows = cast<ConstantInt>(Call.getArgOperand(3));
5435       NumColumns = cast<ConstantInt>(Call.getArgOperand(4));
5436       ResultTy = cast<VectorType>(Call.getType());
5437 
5438       PointerType *Op0PtrTy =
5439           cast<PointerType>(Call.getArgOperand(0)->getType());
5440       if (!Op0PtrTy->isOpaque())
5441         Op0ElemTy = Op0PtrTy->getNonOpaquePointerElementType();
5442       break;
5443     }
5444     case Intrinsic::matrix_column_major_store: {
5445       Stride = dyn_cast<ConstantInt>(Call.getArgOperand(2));
5446       NumRows = cast<ConstantInt>(Call.getArgOperand(4));
5447       NumColumns = cast<ConstantInt>(Call.getArgOperand(5));
5448       ResultTy = cast<VectorType>(Call.getArgOperand(0)->getType());
5449       Op0ElemTy =
5450           cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType();
5451 
5452       PointerType *Op1PtrTy =
5453           cast<PointerType>(Call.getArgOperand(1)->getType());
5454       if (!Op1PtrTy->isOpaque())
5455         Op1ElemTy = Op1PtrTy->getNonOpaquePointerElementType();
5456       break;
5457     }
5458     default:
5459       llvm_unreachable("unexpected intrinsic");
5460     }
5461 
5462     Check(ResultTy->getElementType()->isIntegerTy() ||
5463               ResultTy->getElementType()->isFloatingPointTy(),
5464           "Result type must be an integer or floating-point type!", IF);
5465 
5466     if (Op0ElemTy)
5467       Check(ResultTy->getElementType() == Op0ElemTy,
5468             "Vector element type mismatch of the result and first operand "
5469             "vector!",
5470             IF);
5471 
5472     if (Op1ElemTy)
5473       Check(ResultTy->getElementType() == Op1ElemTy,
5474             "Vector element type mismatch of the result and second operand "
5475             "vector!",
5476             IF);
5477 
5478     Check(cast<FixedVectorType>(ResultTy)->getNumElements() ==
5479               NumRows->getZExtValue() * NumColumns->getZExtValue(),
5480           "Result of a matrix operation does not fit in the returned vector!");
5481 
5482     if (Stride)
5483       Check(Stride->getZExtValue() >= NumRows->getZExtValue(),
5484             "Stride must be greater or equal than the number of rows!", IF);
5485 
5486     break;
5487   }
5488   case Intrinsic::experimental_vector_splice: {
5489     VectorType *VecTy = cast<VectorType>(Call.getType());
5490     int64_t Idx = cast<ConstantInt>(Call.getArgOperand(2))->getSExtValue();
5491     int64_t KnownMinNumElements = VecTy->getElementCount().getKnownMinValue();
5492     if (Call.getParent() && Call.getParent()->getParent()) {
5493       AttributeList Attrs = Call.getParent()->getParent()->getAttributes();
5494       if (Attrs.hasFnAttr(Attribute::VScaleRange))
5495         KnownMinNumElements *= Attrs.getFnAttrs().getVScaleRangeMin();
5496     }
5497     Check((Idx < 0 && std::abs(Idx) <= KnownMinNumElements) ||
5498               (Idx >= 0 && Idx < KnownMinNumElements),
5499           "The splice index exceeds the range [-VL, VL-1] where VL is the "
5500           "known minimum number of elements in the vector. For scalable "
5501           "vectors the minimum number of elements is determined from "
5502           "vscale_range.",
5503           &Call);
5504     break;
5505   }
5506   case Intrinsic::experimental_stepvector: {
5507     VectorType *VecTy = dyn_cast<VectorType>(Call.getType());
5508     Check(VecTy && VecTy->getScalarType()->isIntegerTy() &&
5509               VecTy->getScalarSizeInBits() >= 8,
5510           "experimental_stepvector only supported for vectors of integers "
5511           "with a bitwidth of at least 8.",
5512           &Call);
5513     break;
5514   }
5515   case Intrinsic::vector_insert: {
5516     Value *Vec = Call.getArgOperand(0);
5517     Value *SubVec = Call.getArgOperand(1);
5518     Value *Idx = Call.getArgOperand(2);
5519     unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
5520 
5521     VectorType *VecTy = cast<VectorType>(Vec->getType());
5522     VectorType *SubVecTy = cast<VectorType>(SubVec->getType());
5523 
5524     ElementCount VecEC = VecTy->getElementCount();
5525     ElementCount SubVecEC = SubVecTy->getElementCount();
5526     Check(VecTy->getElementType() == SubVecTy->getElementType(),
5527           "vector_insert parameters must have the same element "
5528           "type.",
5529           &Call);
5530     Check(IdxN % SubVecEC.getKnownMinValue() == 0,
5531           "vector_insert index must be a constant multiple of "
5532           "the subvector's known minimum vector length.");
5533 
5534     // If this insertion is not the 'mixed' case where a fixed vector is
5535     // inserted into a scalable vector, ensure that the insertion of the
5536     // subvector does not overrun the parent vector.
5537     if (VecEC.isScalable() == SubVecEC.isScalable()) {
5538       Check(IdxN < VecEC.getKnownMinValue() &&
5539                 IdxN + SubVecEC.getKnownMinValue() <= VecEC.getKnownMinValue(),
5540             "subvector operand of vector_insert would overrun the "
5541             "vector being inserted into.");
5542     }
5543     break;
5544   }
5545   case Intrinsic::vector_extract: {
5546     Value *Vec = Call.getArgOperand(0);
5547     Value *Idx = Call.getArgOperand(1);
5548     unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
5549 
5550     VectorType *ResultTy = cast<VectorType>(Call.getType());
5551     VectorType *VecTy = cast<VectorType>(Vec->getType());
5552 
5553     ElementCount VecEC = VecTy->getElementCount();
5554     ElementCount ResultEC = ResultTy->getElementCount();
5555 
5556     Check(ResultTy->getElementType() == VecTy->getElementType(),
5557           "vector_extract result must have the same element "
5558           "type as the input vector.",
5559           &Call);
5560     Check(IdxN % ResultEC.getKnownMinValue() == 0,
5561           "vector_extract index must be a constant multiple of "
5562           "the result type's known minimum vector length.");
5563 
5564     // If this extraction is not the 'mixed' case where a fixed vector is is
5565     // extracted from a scalable vector, ensure that the extraction does not
5566     // overrun the parent vector.
5567     if (VecEC.isScalable() == ResultEC.isScalable()) {
5568       Check(IdxN < VecEC.getKnownMinValue() &&
5569                 IdxN + ResultEC.getKnownMinValue() <= VecEC.getKnownMinValue(),
5570             "vector_extract would overrun.");
5571     }
5572     break;
5573   }
5574   case Intrinsic::experimental_noalias_scope_decl: {
5575     NoAliasScopeDecls.push_back(cast<IntrinsicInst>(&Call));
5576     break;
5577   }
5578   case Intrinsic::preserve_array_access_index:
5579   case Intrinsic::preserve_struct_access_index:
5580   case Intrinsic::aarch64_ldaxr:
5581   case Intrinsic::aarch64_ldxr:
5582   case Intrinsic::arm_ldaex:
5583   case Intrinsic::arm_ldrex: {
5584     Type *ElemTy = Call.getParamElementType(0);
5585     Check(ElemTy, "Intrinsic requires elementtype attribute on first argument.",
5586           &Call);
5587     break;
5588   }
5589   case Intrinsic::aarch64_stlxr:
5590   case Intrinsic::aarch64_stxr:
5591   case Intrinsic::arm_stlex:
5592   case Intrinsic::arm_strex: {
5593     Type *ElemTy = Call.getAttributes().getParamElementType(1);
5594     Check(ElemTy,
5595           "Intrinsic requires elementtype attribute on second argument.",
5596           &Call);
5597     break;
5598   }
5599   };
5600 }
5601 
5602 /// Carefully grab the subprogram from a local scope.
5603 ///
5604 /// This carefully grabs the subprogram from a local scope, avoiding the
5605 /// built-in assertions that would typically fire.
5606 static DISubprogram *getSubprogram(Metadata *LocalScope) {
5607   if (!LocalScope)
5608     return nullptr;
5609 
5610   if (auto *SP = dyn_cast<DISubprogram>(LocalScope))
5611     return SP;
5612 
5613   if (auto *LB = dyn_cast<DILexicalBlockBase>(LocalScope))
5614     return getSubprogram(LB->getRawScope());
5615 
5616   // Just return null; broken scope chains are checked elsewhere.
5617   assert(!isa<DILocalScope>(LocalScope) && "Unknown type of local scope");
5618   return nullptr;
5619 }
5620 
5621 void Verifier::visitVPIntrinsic(VPIntrinsic &VPI) {
5622   if (auto *VPCast = dyn_cast<VPCastIntrinsic>(&VPI)) {
5623     auto *RetTy = cast<VectorType>(VPCast->getType());
5624     auto *ValTy = cast<VectorType>(VPCast->getOperand(0)->getType());
5625     Check(RetTy->getElementCount() == ValTy->getElementCount(),
5626           "VP cast intrinsic first argument and result vector lengths must be "
5627           "equal",
5628           *VPCast);
5629 
5630     switch (VPCast->getIntrinsicID()) {
5631     default:
5632       llvm_unreachable("Unknown VP cast intrinsic");
5633     case Intrinsic::vp_trunc:
5634       Check(RetTy->isIntOrIntVectorTy() && ValTy->isIntOrIntVectorTy(),
5635             "llvm.vp.trunc intrinsic first argument and result element type "
5636             "must be integer",
5637             *VPCast);
5638       Check(RetTy->getScalarSizeInBits() < ValTy->getScalarSizeInBits(),
5639             "llvm.vp.trunc intrinsic the bit size of first argument must be "
5640             "larger than the bit size of the return type",
5641             *VPCast);
5642       break;
5643     case Intrinsic::vp_zext:
5644     case Intrinsic::vp_sext:
5645       Check(RetTy->isIntOrIntVectorTy() && ValTy->isIntOrIntVectorTy(),
5646             "llvm.vp.zext or llvm.vp.sext intrinsic first argument and result "
5647             "element type must be integer",
5648             *VPCast);
5649       Check(RetTy->getScalarSizeInBits() > ValTy->getScalarSizeInBits(),
5650             "llvm.vp.zext or llvm.vp.sext intrinsic the bit size of first "
5651             "argument must be smaller than the bit size of the return type",
5652             *VPCast);
5653       break;
5654     case Intrinsic::vp_fptoui:
5655     case Intrinsic::vp_fptosi:
5656       Check(
5657           RetTy->isIntOrIntVectorTy() && ValTy->isFPOrFPVectorTy(),
5658           "llvm.vp.fptoui or llvm.vp.fptosi intrinsic first argument element "
5659           "type must be floating-point and result element type must be integer",
5660           *VPCast);
5661       break;
5662     case Intrinsic::vp_uitofp:
5663     case Intrinsic::vp_sitofp:
5664       Check(
5665           RetTy->isFPOrFPVectorTy() && ValTy->isIntOrIntVectorTy(),
5666           "llvm.vp.uitofp or llvm.vp.sitofp intrinsic first argument element "
5667           "type must be integer and result element type must be floating-point",
5668           *VPCast);
5669       break;
5670     case Intrinsic::vp_fptrunc:
5671       Check(RetTy->isFPOrFPVectorTy() && ValTy->isFPOrFPVectorTy(),
5672             "llvm.vp.fptrunc intrinsic first argument and result element type "
5673             "must be floating-point",
5674             *VPCast);
5675       Check(RetTy->getScalarSizeInBits() < ValTy->getScalarSizeInBits(),
5676             "llvm.vp.fptrunc intrinsic the bit size of first argument must be "
5677             "larger than the bit size of the return type",
5678             *VPCast);
5679       break;
5680     case Intrinsic::vp_fpext:
5681       Check(RetTy->isFPOrFPVectorTy() && ValTy->isFPOrFPVectorTy(),
5682             "llvm.vp.fpext intrinsic first argument and result element type "
5683             "must be floating-point",
5684             *VPCast);
5685       Check(RetTy->getScalarSizeInBits() > ValTy->getScalarSizeInBits(),
5686             "llvm.vp.fpext intrinsic the bit size of first argument must be "
5687             "smaller than the bit size of the return type",
5688             *VPCast);
5689       break;
5690     case Intrinsic::vp_ptrtoint:
5691       Check(RetTy->isIntOrIntVectorTy() && ValTy->isPtrOrPtrVectorTy(),
5692             "llvm.vp.ptrtoint intrinsic first argument element type must be "
5693             "pointer and result element type must be integer",
5694             *VPCast);
5695       break;
5696     case Intrinsic::vp_inttoptr:
5697       Check(RetTy->isPtrOrPtrVectorTy() && ValTy->isIntOrIntVectorTy(),
5698             "llvm.vp.inttoptr intrinsic first argument element type must be "
5699             "integer and result element type must be pointer",
5700             *VPCast);
5701       break;
5702     }
5703   }
5704   if (VPI.getIntrinsicID() == Intrinsic::vp_fcmp) {
5705     auto Pred = cast<VPCmpIntrinsic>(&VPI)->getPredicate();
5706     Check(CmpInst::isFPPredicate(Pred),
5707           "invalid predicate for VP FP comparison intrinsic", &VPI);
5708   }
5709   if (VPI.getIntrinsicID() == Intrinsic::vp_icmp) {
5710     auto Pred = cast<VPCmpIntrinsic>(&VPI)->getPredicate();
5711     Check(CmpInst::isIntPredicate(Pred),
5712           "invalid predicate for VP integer comparison intrinsic", &VPI);
5713   }
5714 }
5715 
5716 void Verifier::visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI) {
5717   unsigned NumOperands;
5718   bool HasRoundingMD;
5719   switch (FPI.getIntrinsicID()) {
5720 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC)                         \
5721   case Intrinsic::INTRINSIC:                                                   \
5722     NumOperands = NARG;                                                        \
5723     HasRoundingMD = ROUND_MODE;                                                \
5724     break;
5725 #include "llvm/IR/ConstrainedOps.def"
5726   default:
5727     llvm_unreachable("Invalid constrained FP intrinsic!");
5728   }
5729   NumOperands += (1 + HasRoundingMD);
5730   // Compare intrinsics carry an extra predicate metadata operand.
5731   if (isa<ConstrainedFPCmpIntrinsic>(FPI))
5732     NumOperands += 1;
5733   Check((FPI.arg_size() == NumOperands),
5734         "invalid arguments for constrained FP intrinsic", &FPI);
5735 
5736   switch (FPI.getIntrinsicID()) {
5737   case Intrinsic::experimental_constrained_lrint:
5738   case Intrinsic::experimental_constrained_llrint: {
5739     Type *ValTy = FPI.getArgOperand(0)->getType();
5740     Type *ResultTy = FPI.getType();
5741     Check(!ValTy->isVectorTy() && !ResultTy->isVectorTy(),
5742           "Intrinsic does not support vectors", &FPI);
5743   }
5744     break;
5745 
5746   case Intrinsic::experimental_constrained_lround:
5747   case Intrinsic::experimental_constrained_llround: {
5748     Type *ValTy = FPI.getArgOperand(0)->getType();
5749     Type *ResultTy = FPI.getType();
5750     Check(!ValTy->isVectorTy() && !ResultTy->isVectorTy(),
5751           "Intrinsic does not support vectors", &FPI);
5752     break;
5753   }
5754 
5755   case Intrinsic::experimental_constrained_fcmp:
5756   case Intrinsic::experimental_constrained_fcmps: {
5757     auto Pred = cast<ConstrainedFPCmpIntrinsic>(&FPI)->getPredicate();
5758     Check(CmpInst::isFPPredicate(Pred),
5759           "invalid predicate for constrained FP comparison intrinsic", &FPI);
5760     break;
5761   }
5762 
5763   case Intrinsic::experimental_constrained_fptosi:
5764   case Intrinsic::experimental_constrained_fptoui: {
5765     Value *Operand = FPI.getArgOperand(0);
5766     uint64_t NumSrcElem = 0;
5767     Check(Operand->getType()->isFPOrFPVectorTy(),
5768           "Intrinsic first argument must be floating point", &FPI);
5769     if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
5770       NumSrcElem = cast<FixedVectorType>(OperandT)->getNumElements();
5771     }
5772 
5773     Operand = &FPI;
5774     Check((NumSrcElem > 0) == Operand->getType()->isVectorTy(),
5775           "Intrinsic first argument and result disagree on vector use", &FPI);
5776     Check(Operand->getType()->isIntOrIntVectorTy(),
5777           "Intrinsic result must be an integer", &FPI);
5778     if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
5779       Check(NumSrcElem == cast<FixedVectorType>(OperandT)->getNumElements(),
5780             "Intrinsic first argument and result vector lengths must be equal",
5781             &FPI);
5782     }
5783   }
5784     break;
5785 
5786   case Intrinsic::experimental_constrained_sitofp:
5787   case Intrinsic::experimental_constrained_uitofp: {
5788     Value *Operand = FPI.getArgOperand(0);
5789     uint64_t NumSrcElem = 0;
5790     Check(Operand->getType()->isIntOrIntVectorTy(),
5791           "Intrinsic first argument must be integer", &FPI);
5792     if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
5793       NumSrcElem = cast<FixedVectorType>(OperandT)->getNumElements();
5794     }
5795 
5796     Operand = &FPI;
5797     Check((NumSrcElem > 0) == Operand->getType()->isVectorTy(),
5798           "Intrinsic first argument and result disagree on vector use", &FPI);
5799     Check(Operand->getType()->isFPOrFPVectorTy(),
5800           "Intrinsic result must be a floating point", &FPI);
5801     if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
5802       Check(NumSrcElem == cast<FixedVectorType>(OperandT)->getNumElements(),
5803             "Intrinsic first argument and result vector lengths must be equal",
5804             &FPI);
5805     }
5806   } break;
5807 
5808   case Intrinsic::experimental_constrained_fptrunc:
5809   case Intrinsic::experimental_constrained_fpext: {
5810     Value *Operand = FPI.getArgOperand(0);
5811     Type *OperandTy = Operand->getType();
5812     Value *Result = &FPI;
5813     Type *ResultTy = Result->getType();
5814     Check(OperandTy->isFPOrFPVectorTy(),
5815           "Intrinsic first argument must be FP or FP vector", &FPI);
5816     Check(ResultTy->isFPOrFPVectorTy(),
5817           "Intrinsic result must be FP or FP vector", &FPI);
5818     Check(OperandTy->isVectorTy() == ResultTy->isVectorTy(),
5819           "Intrinsic first argument and result disagree on vector use", &FPI);
5820     if (OperandTy->isVectorTy()) {
5821       Check(cast<FixedVectorType>(OperandTy)->getNumElements() ==
5822                 cast<FixedVectorType>(ResultTy)->getNumElements(),
5823             "Intrinsic first argument and result vector lengths must be equal",
5824             &FPI);
5825     }
5826     if (FPI.getIntrinsicID() == Intrinsic::experimental_constrained_fptrunc) {
5827       Check(OperandTy->getScalarSizeInBits() > ResultTy->getScalarSizeInBits(),
5828             "Intrinsic first argument's type must be larger than result type",
5829             &FPI);
5830     } else {
5831       Check(OperandTy->getScalarSizeInBits() < ResultTy->getScalarSizeInBits(),
5832             "Intrinsic first argument's type must be smaller than result type",
5833             &FPI);
5834     }
5835   }
5836     break;
5837 
5838   default:
5839     break;
5840   }
5841 
5842   // If a non-metadata argument is passed in a metadata slot then the
5843   // error will be caught earlier when the incorrect argument doesn't
5844   // match the specification in the intrinsic call table. Thus, no
5845   // argument type check is needed here.
5846 
5847   Check(FPI.getExceptionBehavior().has_value(),
5848         "invalid exception behavior argument", &FPI);
5849   if (HasRoundingMD) {
5850     Check(FPI.getRoundingMode().has_value(), "invalid rounding mode argument",
5851           &FPI);
5852   }
5853 }
5854 
5855 void Verifier::visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII) {
5856   auto *MD = DII.getRawLocation();
5857   CheckDI(isa<ValueAsMetadata>(MD) || isa<DIArgList>(MD) ||
5858               (isa<MDNode>(MD) && !cast<MDNode>(MD)->getNumOperands()),
5859           "invalid llvm.dbg." + Kind + " intrinsic address/value", &DII, MD);
5860   CheckDI(isa<DILocalVariable>(DII.getRawVariable()),
5861           "invalid llvm.dbg." + Kind + " intrinsic variable", &DII,
5862           DII.getRawVariable());
5863   CheckDI(isa<DIExpression>(DII.getRawExpression()),
5864           "invalid llvm.dbg." + Kind + " intrinsic expression", &DII,
5865           DII.getRawExpression());
5866 
5867   // Ignore broken !dbg attachments; they're checked elsewhere.
5868   if (MDNode *N = DII.getDebugLoc().getAsMDNode())
5869     if (!isa<DILocation>(N))
5870       return;
5871 
5872   BasicBlock *BB = DII.getParent();
5873   Function *F = BB ? BB->getParent() : nullptr;
5874 
5875   // The scopes for variables and !dbg attachments must agree.
5876   DILocalVariable *Var = DII.getVariable();
5877   DILocation *Loc = DII.getDebugLoc();
5878   CheckDI(Loc, "llvm.dbg." + Kind + " intrinsic requires a !dbg attachment",
5879           &DII, BB, F);
5880 
5881   DISubprogram *VarSP = getSubprogram(Var->getRawScope());
5882   DISubprogram *LocSP = getSubprogram(Loc->getRawScope());
5883   if (!VarSP || !LocSP)
5884     return; // Broken scope chains are checked elsewhere.
5885 
5886   CheckDI(VarSP == LocSP,
5887           "mismatched subprogram between llvm.dbg." + Kind +
5888               " variable and !dbg attachment",
5889           &DII, BB, F, Var, Var->getScope()->getSubprogram(), Loc,
5890           Loc->getScope()->getSubprogram());
5891 
5892   // This check is redundant with one in visitLocalVariable().
5893   CheckDI(isType(Var->getRawType()), "invalid type ref", Var,
5894           Var->getRawType());
5895   verifyFnArgs(DII);
5896 }
5897 
5898 void Verifier::visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI) {
5899   CheckDI(isa<DILabel>(DLI.getRawLabel()),
5900           "invalid llvm.dbg." + Kind + " intrinsic variable", &DLI,
5901           DLI.getRawLabel());
5902 
5903   // Ignore broken !dbg attachments; they're checked elsewhere.
5904   if (MDNode *N = DLI.getDebugLoc().getAsMDNode())
5905     if (!isa<DILocation>(N))
5906       return;
5907 
5908   BasicBlock *BB = DLI.getParent();
5909   Function *F = BB ? BB->getParent() : nullptr;
5910 
5911   // The scopes for variables and !dbg attachments must agree.
5912   DILabel *Label = DLI.getLabel();
5913   DILocation *Loc = DLI.getDebugLoc();
5914   Check(Loc, "llvm.dbg." + Kind + " intrinsic requires a !dbg attachment", &DLI,
5915         BB, F);
5916 
5917   DISubprogram *LabelSP = getSubprogram(Label->getRawScope());
5918   DISubprogram *LocSP = getSubprogram(Loc->getRawScope());
5919   if (!LabelSP || !LocSP)
5920     return;
5921 
5922   CheckDI(LabelSP == LocSP,
5923           "mismatched subprogram between llvm.dbg." + Kind +
5924               " label and !dbg attachment",
5925           &DLI, BB, F, Label, Label->getScope()->getSubprogram(), Loc,
5926           Loc->getScope()->getSubprogram());
5927 }
5928 
5929 void Verifier::verifyFragmentExpression(const DbgVariableIntrinsic &I) {
5930   DILocalVariable *V = dyn_cast_or_null<DILocalVariable>(I.getRawVariable());
5931   DIExpression *E = dyn_cast_or_null<DIExpression>(I.getRawExpression());
5932 
5933   // We don't know whether this intrinsic verified correctly.
5934   if (!V || !E || !E->isValid())
5935     return;
5936 
5937   // Nothing to do if this isn't a DW_OP_LLVM_fragment expression.
5938   auto Fragment = E->getFragmentInfo();
5939   if (!Fragment)
5940     return;
5941 
5942   // The frontend helps out GDB by emitting the members of local anonymous
5943   // unions as artificial local variables with shared storage. When SROA splits
5944   // the storage for artificial local variables that are smaller than the entire
5945   // union, the overhang piece will be outside of the allotted space for the
5946   // variable and this check fails.
5947   // FIXME: Remove this check as soon as clang stops doing this; it hides bugs.
5948   if (V->isArtificial())
5949     return;
5950 
5951   verifyFragmentExpression(*V, *Fragment, &I);
5952 }
5953 
5954 template <typename ValueOrMetadata>
5955 void Verifier::verifyFragmentExpression(const DIVariable &V,
5956                                         DIExpression::FragmentInfo Fragment,
5957                                         ValueOrMetadata *Desc) {
5958   // If there's no size, the type is broken, but that should be checked
5959   // elsewhere.
5960   auto VarSize = V.getSizeInBits();
5961   if (!VarSize)
5962     return;
5963 
5964   unsigned FragSize = Fragment.SizeInBits;
5965   unsigned FragOffset = Fragment.OffsetInBits;
5966   CheckDI(FragSize + FragOffset <= *VarSize,
5967           "fragment is larger than or outside of variable", Desc, &V);
5968   CheckDI(FragSize != *VarSize, "fragment covers entire variable", Desc, &V);
5969 }
5970 
5971 void Verifier::verifyFnArgs(const DbgVariableIntrinsic &I) {
5972   // This function does not take the scope of noninlined function arguments into
5973   // account. Don't run it if current function is nodebug, because it may
5974   // contain inlined debug intrinsics.
5975   if (!HasDebugInfo)
5976     return;
5977 
5978   // For performance reasons only check non-inlined ones.
5979   if (I.getDebugLoc()->getInlinedAt())
5980     return;
5981 
5982   DILocalVariable *Var = I.getVariable();
5983   CheckDI(Var, "dbg intrinsic without variable");
5984 
5985   unsigned ArgNo = Var->getArg();
5986   if (!ArgNo)
5987     return;
5988 
5989   // Verify there are no duplicate function argument debug info entries.
5990   // These will cause hard-to-debug assertions in the DWARF backend.
5991   if (DebugFnArgs.size() < ArgNo)
5992     DebugFnArgs.resize(ArgNo, nullptr);
5993 
5994   auto *Prev = DebugFnArgs[ArgNo - 1];
5995   DebugFnArgs[ArgNo - 1] = Var;
5996   CheckDI(!Prev || (Prev == Var), "conflicting debug info for argument", &I,
5997           Prev, Var);
5998 }
5999 
6000 void Verifier::verifyNotEntryValue(const DbgVariableIntrinsic &I) {
6001   DIExpression *E = dyn_cast_or_null<DIExpression>(I.getRawExpression());
6002 
6003   // We don't know whether this intrinsic verified correctly.
6004   if (!E || !E->isValid())
6005     return;
6006 
6007   CheckDI(!E->isEntryValue(), "Entry values are only allowed in MIR", &I);
6008 }
6009 
6010 void Verifier::verifyCompileUnits() {
6011   // When more than one Module is imported into the same context, such as during
6012   // an LTO build before linking the modules, ODR type uniquing may cause types
6013   // to point to a different CU. This check does not make sense in this case.
6014   if (M.getContext().isODRUniquingDebugTypes())
6015     return;
6016   auto *CUs = M.getNamedMetadata("llvm.dbg.cu");
6017   SmallPtrSet<const Metadata *, 2> Listed;
6018   if (CUs)
6019     Listed.insert(CUs->op_begin(), CUs->op_end());
6020   for (auto *CU : CUVisited)
6021     CheckDI(Listed.count(CU), "DICompileUnit not listed in llvm.dbg.cu", CU);
6022   CUVisited.clear();
6023 }
6024 
6025 void Verifier::verifyDeoptimizeCallingConvs() {
6026   if (DeoptimizeDeclarations.empty())
6027     return;
6028 
6029   const Function *First = DeoptimizeDeclarations[0];
6030   for (auto *F : makeArrayRef(DeoptimizeDeclarations).slice(1)) {
6031     Check(First->getCallingConv() == F->getCallingConv(),
6032           "All llvm.experimental.deoptimize declarations must have the same "
6033           "calling convention",
6034           First, F);
6035   }
6036 }
6037 
6038 void Verifier::verifyAttachedCallBundle(const CallBase &Call,
6039                                         const OperandBundleUse &BU) {
6040   FunctionType *FTy = Call.getFunctionType();
6041 
6042   Check((FTy->getReturnType()->isPointerTy() ||
6043          (Call.doesNotReturn() && FTy->getReturnType()->isVoidTy())),
6044         "a call with operand bundle \"clang.arc.attachedcall\" must call a "
6045         "function returning a pointer or a non-returning function that has a "
6046         "void return type",
6047         Call);
6048 
6049   Check(BU.Inputs.size() == 1 && isa<Function>(BU.Inputs.front()),
6050         "operand bundle \"clang.arc.attachedcall\" requires one function as "
6051         "an argument",
6052         Call);
6053 
6054   auto *Fn = cast<Function>(BU.Inputs.front());
6055   Intrinsic::ID IID = Fn->getIntrinsicID();
6056 
6057   if (IID) {
6058     Check((IID == Intrinsic::objc_retainAutoreleasedReturnValue ||
6059            IID == Intrinsic::objc_unsafeClaimAutoreleasedReturnValue),
6060           "invalid function argument", Call);
6061   } else {
6062     StringRef FnName = Fn->getName();
6063     Check((FnName == "objc_retainAutoreleasedReturnValue" ||
6064            FnName == "objc_unsafeClaimAutoreleasedReturnValue"),
6065           "invalid function argument", Call);
6066   }
6067 }
6068 
6069 void Verifier::verifySourceDebugInfo(const DICompileUnit &U, const DIFile &F) {
6070   bool HasSource = F.getSource().has_value();
6071   if (!HasSourceDebugInfo.count(&U))
6072     HasSourceDebugInfo[&U] = HasSource;
6073   CheckDI(HasSource == HasSourceDebugInfo[&U],
6074           "inconsistent use of embedded source");
6075 }
6076 
6077 void Verifier::verifyNoAliasScopeDecl() {
6078   if (NoAliasScopeDecls.empty())
6079     return;
6080 
6081   // only a single scope must be declared at a time.
6082   for (auto *II : NoAliasScopeDecls) {
6083     assert(II->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl &&
6084            "Not a llvm.experimental.noalias.scope.decl ?");
6085     const auto *ScopeListMV = dyn_cast<MetadataAsValue>(
6086         II->getOperand(Intrinsic::NoAliasScopeDeclScopeArg));
6087     Check(ScopeListMV != nullptr,
6088           "llvm.experimental.noalias.scope.decl must have a MetadataAsValue "
6089           "argument",
6090           II);
6091 
6092     const auto *ScopeListMD = dyn_cast<MDNode>(ScopeListMV->getMetadata());
6093     Check(ScopeListMD != nullptr, "!id.scope.list must point to an MDNode", II);
6094     Check(ScopeListMD->getNumOperands() == 1,
6095           "!id.scope.list must point to a list with a single scope", II);
6096     visitAliasScopeListMetadata(ScopeListMD);
6097   }
6098 
6099   // Only check the domination rule when requested. Once all passes have been
6100   // adapted this option can go away.
6101   if (!VerifyNoAliasScopeDomination)
6102     return;
6103 
6104   // Now sort the intrinsics based on the scope MDNode so that declarations of
6105   // the same scopes are next to each other.
6106   auto GetScope = [](IntrinsicInst *II) {
6107     const auto *ScopeListMV = cast<MetadataAsValue>(
6108         II->getOperand(Intrinsic::NoAliasScopeDeclScopeArg));
6109     return &cast<MDNode>(ScopeListMV->getMetadata())->getOperand(0);
6110   };
6111 
6112   // We are sorting on MDNode pointers here. For valid input IR this is ok.
6113   // TODO: Sort on Metadata ID to avoid non-deterministic error messages.
6114   auto Compare = [GetScope](IntrinsicInst *Lhs, IntrinsicInst *Rhs) {
6115     return GetScope(Lhs) < GetScope(Rhs);
6116   };
6117 
6118   llvm::sort(NoAliasScopeDecls, Compare);
6119 
6120   // Go over the intrinsics and check that for the same scope, they are not
6121   // dominating each other.
6122   auto ItCurrent = NoAliasScopeDecls.begin();
6123   while (ItCurrent != NoAliasScopeDecls.end()) {
6124     auto CurScope = GetScope(*ItCurrent);
6125     auto ItNext = ItCurrent;
6126     do {
6127       ++ItNext;
6128     } while (ItNext != NoAliasScopeDecls.end() &&
6129              GetScope(*ItNext) == CurScope);
6130 
6131     // [ItCurrent, ItNext) represents the declarations for the same scope.
6132     // Ensure they are not dominating each other.. but only if it is not too
6133     // expensive.
6134     if (ItNext - ItCurrent < 32)
6135       for (auto *I : llvm::make_range(ItCurrent, ItNext))
6136         for (auto *J : llvm::make_range(ItCurrent, ItNext))
6137           if (I != J)
6138             Check(!DT.dominates(I, J),
6139                   "llvm.experimental.noalias.scope.decl dominates another one "
6140                   "with the same scope",
6141                   I);
6142     ItCurrent = ItNext;
6143   }
6144 }
6145 
6146 //===----------------------------------------------------------------------===//
6147 //  Implement the public interfaces to this file...
6148 //===----------------------------------------------------------------------===//
6149 
6150 bool llvm::verifyFunction(const Function &f, raw_ostream *OS) {
6151   Function &F = const_cast<Function &>(f);
6152 
6153   // Don't use a raw_null_ostream.  Printing IR is expensive.
6154   Verifier V(OS, /*ShouldTreatBrokenDebugInfoAsError=*/true, *f.getParent());
6155 
6156   // Note that this function's return value is inverted from what you would
6157   // expect of a function called "verify".
6158   return !V.verify(F);
6159 }
6160 
6161 bool llvm::verifyModule(const Module &M, raw_ostream *OS,
6162                         bool *BrokenDebugInfo) {
6163   // Don't use a raw_null_ostream.  Printing IR is expensive.
6164   Verifier V(OS, /*ShouldTreatBrokenDebugInfoAsError=*/!BrokenDebugInfo, M);
6165 
6166   bool Broken = false;
6167   for (const Function &F : M)
6168     Broken |= !V.verify(F);
6169 
6170   Broken |= !V.verify();
6171   if (BrokenDebugInfo)
6172     *BrokenDebugInfo = V.hasBrokenDebugInfo();
6173   // Note that this function's return value is inverted from what you would
6174   // expect of a function called "verify".
6175   return Broken;
6176 }
6177 
6178 namespace {
6179 
6180 struct VerifierLegacyPass : public FunctionPass {
6181   static char ID;
6182 
6183   std::unique_ptr<Verifier> V;
6184   bool FatalErrors = true;
6185 
6186   VerifierLegacyPass() : FunctionPass(ID) {
6187     initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
6188   }
6189   explicit VerifierLegacyPass(bool FatalErrors)
6190       : FunctionPass(ID),
6191         FatalErrors(FatalErrors) {
6192     initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
6193   }
6194 
6195   bool doInitialization(Module &M) override {
6196     V = std::make_unique<Verifier>(
6197         &dbgs(), /*ShouldTreatBrokenDebugInfoAsError=*/false, M);
6198     return false;
6199   }
6200 
6201   bool runOnFunction(Function &F) override {
6202     if (!V->verify(F) && FatalErrors) {
6203       errs() << "in function " << F.getName() << '\n';
6204       report_fatal_error("Broken function found, compilation aborted!");
6205     }
6206     return false;
6207   }
6208 
6209   bool doFinalization(Module &M) override {
6210     bool HasErrors = false;
6211     for (Function &F : M)
6212       if (F.isDeclaration())
6213         HasErrors |= !V->verify(F);
6214 
6215     HasErrors |= !V->verify();
6216     if (FatalErrors && (HasErrors || V->hasBrokenDebugInfo()))
6217       report_fatal_error("Broken module found, compilation aborted!");
6218     return false;
6219   }
6220 
6221   void getAnalysisUsage(AnalysisUsage &AU) const override {
6222     AU.setPreservesAll();
6223   }
6224 };
6225 
6226 } // end anonymous namespace
6227 
6228 /// Helper to issue failure from the TBAA verification
6229 template <typename... Tys> void TBAAVerifier::CheckFailed(Tys &&... Args) {
6230   if (Diagnostic)
6231     return Diagnostic->CheckFailed(Args...);
6232 }
6233 
6234 #define CheckTBAA(C, ...)                                                      \
6235   do {                                                                         \
6236     if (!(C)) {                                                                \
6237       CheckFailed(__VA_ARGS__);                                                \
6238       return false;                                                            \
6239     }                                                                          \
6240   } while (false)
6241 
6242 /// Verify that \p BaseNode can be used as the "base type" in the struct-path
6243 /// TBAA scheme.  This means \p BaseNode is either a scalar node, or a
6244 /// struct-type node describing an aggregate data structure (like a struct).
6245 TBAAVerifier::TBAABaseNodeSummary
6246 TBAAVerifier::verifyTBAABaseNode(Instruction &I, const MDNode *BaseNode,
6247                                  bool IsNewFormat) {
6248   if (BaseNode->getNumOperands() < 2) {
6249     CheckFailed("Base nodes must have at least two operands", &I, BaseNode);
6250     return {true, ~0u};
6251   }
6252 
6253   auto Itr = TBAABaseNodes.find(BaseNode);
6254   if (Itr != TBAABaseNodes.end())
6255     return Itr->second;
6256 
6257   auto Result = verifyTBAABaseNodeImpl(I, BaseNode, IsNewFormat);
6258   auto InsertResult = TBAABaseNodes.insert({BaseNode, Result});
6259   (void)InsertResult;
6260   assert(InsertResult.second && "We just checked!");
6261   return Result;
6262 }
6263 
6264 TBAAVerifier::TBAABaseNodeSummary
6265 TBAAVerifier::verifyTBAABaseNodeImpl(Instruction &I, const MDNode *BaseNode,
6266                                      bool IsNewFormat) {
6267   const TBAAVerifier::TBAABaseNodeSummary InvalidNode = {true, ~0u};
6268 
6269   if (BaseNode->getNumOperands() == 2) {
6270     // Scalar nodes can only be accessed at offset 0.
6271     return isValidScalarTBAANode(BaseNode)
6272                ? TBAAVerifier::TBAABaseNodeSummary({false, 0})
6273                : InvalidNode;
6274   }
6275 
6276   if (IsNewFormat) {
6277     if (BaseNode->getNumOperands() % 3 != 0) {
6278       CheckFailed("Access tag nodes must have the number of operands that is a "
6279                   "multiple of 3!", BaseNode);
6280       return InvalidNode;
6281     }
6282   } else {
6283     if (BaseNode->getNumOperands() % 2 != 1) {
6284       CheckFailed("Struct tag nodes must have an odd number of operands!",
6285                   BaseNode);
6286       return InvalidNode;
6287     }
6288   }
6289 
6290   // Check the type size field.
6291   if (IsNewFormat) {
6292     auto *TypeSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
6293         BaseNode->getOperand(1));
6294     if (!TypeSizeNode) {
6295       CheckFailed("Type size nodes must be constants!", &I, BaseNode);
6296       return InvalidNode;
6297     }
6298   }
6299 
6300   // Check the type name field. In the new format it can be anything.
6301   if (!IsNewFormat && !isa<MDString>(BaseNode->getOperand(0))) {
6302     CheckFailed("Struct tag nodes have a string as their first operand",
6303                 BaseNode);
6304     return InvalidNode;
6305   }
6306 
6307   bool Failed = false;
6308 
6309   Optional<APInt> PrevOffset;
6310   unsigned BitWidth = ~0u;
6311 
6312   // We've already checked that BaseNode is not a degenerate root node with one
6313   // operand in \c verifyTBAABaseNode, so this loop should run at least once.
6314   unsigned FirstFieldOpNo = IsNewFormat ? 3 : 1;
6315   unsigned NumOpsPerField = IsNewFormat ? 3 : 2;
6316   for (unsigned Idx = FirstFieldOpNo; Idx < BaseNode->getNumOperands();
6317            Idx += NumOpsPerField) {
6318     const MDOperand &FieldTy = BaseNode->getOperand(Idx);
6319     const MDOperand &FieldOffset = BaseNode->getOperand(Idx + 1);
6320     if (!isa<MDNode>(FieldTy)) {
6321       CheckFailed("Incorrect field entry in struct type node!", &I, BaseNode);
6322       Failed = true;
6323       continue;
6324     }
6325 
6326     auto *OffsetEntryCI =
6327         mdconst::dyn_extract_or_null<ConstantInt>(FieldOffset);
6328     if (!OffsetEntryCI) {
6329       CheckFailed("Offset entries must be constants!", &I, BaseNode);
6330       Failed = true;
6331       continue;
6332     }
6333 
6334     if (BitWidth == ~0u)
6335       BitWidth = OffsetEntryCI->getBitWidth();
6336 
6337     if (OffsetEntryCI->getBitWidth() != BitWidth) {
6338       CheckFailed(
6339           "Bitwidth between the offsets and struct type entries must match", &I,
6340           BaseNode);
6341       Failed = true;
6342       continue;
6343     }
6344 
6345     // NB! As far as I can tell, we generate a non-strictly increasing offset
6346     // sequence only from structs that have zero size bit fields.  When
6347     // recursing into a contained struct in \c getFieldNodeFromTBAABaseNode we
6348     // pick the field lexically the latest in struct type metadata node.  This
6349     // mirrors the actual behavior of the alias analysis implementation.
6350     bool IsAscending =
6351         !PrevOffset || PrevOffset->ule(OffsetEntryCI->getValue());
6352 
6353     if (!IsAscending) {
6354       CheckFailed("Offsets must be increasing!", &I, BaseNode);
6355       Failed = true;
6356     }
6357 
6358     PrevOffset = OffsetEntryCI->getValue();
6359 
6360     if (IsNewFormat) {
6361       auto *MemberSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
6362           BaseNode->getOperand(Idx + 2));
6363       if (!MemberSizeNode) {
6364         CheckFailed("Member size entries must be constants!", &I, BaseNode);
6365         Failed = true;
6366         continue;
6367       }
6368     }
6369   }
6370 
6371   return Failed ? InvalidNode
6372                 : TBAAVerifier::TBAABaseNodeSummary(false, BitWidth);
6373 }
6374 
6375 static bool IsRootTBAANode(const MDNode *MD) {
6376   return MD->getNumOperands() < 2;
6377 }
6378 
6379 static bool IsScalarTBAANodeImpl(const MDNode *MD,
6380                                  SmallPtrSetImpl<const MDNode *> &Visited) {
6381   if (MD->getNumOperands() != 2 && MD->getNumOperands() != 3)
6382     return false;
6383 
6384   if (!isa<MDString>(MD->getOperand(0)))
6385     return false;
6386 
6387   if (MD->getNumOperands() == 3) {
6388     auto *Offset = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
6389     if (!(Offset && Offset->isZero() && isa<MDString>(MD->getOperand(0))))
6390       return false;
6391   }
6392 
6393   auto *Parent = dyn_cast_or_null<MDNode>(MD->getOperand(1));
6394   return Parent && Visited.insert(Parent).second &&
6395          (IsRootTBAANode(Parent) || IsScalarTBAANodeImpl(Parent, Visited));
6396 }
6397 
6398 bool TBAAVerifier::isValidScalarTBAANode(const MDNode *MD) {
6399   auto ResultIt = TBAAScalarNodes.find(MD);
6400   if (ResultIt != TBAAScalarNodes.end())
6401     return ResultIt->second;
6402 
6403   SmallPtrSet<const MDNode *, 4> Visited;
6404   bool Result = IsScalarTBAANodeImpl(MD, Visited);
6405   auto InsertResult = TBAAScalarNodes.insert({MD, Result});
6406   (void)InsertResult;
6407   assert(InsertResult.second && "Just checked!");
6408 
6409   return Result;
6410 }
6411 
6412 /// Returns the field node at the offset \p Offset in \p BaseNode.  Update \p
6413 /// Offset in place to be the offset within the field node returned.
6414 ///
6415 /// We assume we've okayed \p BaseNode via \c verifyTBAABaseNode.
6416 MDNode *TBAAVerifier::getFieldNodeFromTBAABaseNode(Instruction &I,
6417                                                    const MDNode *BaseNode,
6418                                                    APInt &Offset,
6419                                                    bool IsNewFormat) {
6420   assert(BaseNode->getNumOperands() >= 2 && "Invalid base node!");
6421 
6422   // Scalar nodes have only one possible "field" -- their parent in the access
6423   // hierarchy.  Offset must be zero at this point, but our caller is supposed
6424   // to check that.
6425   if (BaseNode->getNumOperands() == 2)
6426     return cast<MDNode>(BaseNode->getOperand(1));
6427 
6428   unsigned FirstFieldOpNo = IsNewFormat ? 3 : 1;
6429   unsigned NumOpsPerField = IsNewFormat ? 3 : 2;
6430   for (unsigned Idx = FirstFieldOpNo; Idx < BaseNode->getNumOperands();
6431            Idx += NumOpsPerField) {
6432     auto *OffsetEntryCI =
6433         mdconst::extract<ConstantInt>(BaseNode->getOperand(Idx + 1));
6434     if (OffsetEntryCI->getValue().ugt(Offset)) {
6435       if (Idx == FirstFieldOpNo) {
6436         CheckFailed("Could not find TBAA parent in struct type node", &I,
6437                     BaseNode, &Offset);
6438         return nullptr;
6439       }
6440 
6441       unsigned PrevIdx = Idx - NumOpsPerField;
6442       auto *PrevOffsetEntryCI =
6443           mdconst::extract<ConstantInt>(BaseNode->getOperand(PrevIdx + 1));
6444       Offset -= PrevOffsetEntryCI->getValue();
6445       return cast<MDNode>(BaseNode->getOperand(PrevIdx));
6446     }
6447   }
6448 
6449   unsigned LastIdx = BaseNode->getNumOperands() - NumOpsPerField;
6450   auto *LastOffsetEntryCI = mdconst::extract<ConstantInt>(
6451       BaseNode->getOperand(LastIdx + 1));
6452   Offset -= LastOffsetEntryCI->getValue();
6453   return cast<MDNode>(BaseNode->getOperand(LastIdx));
6454 }
6455 
6456 static bool isNewFormatTBAATypeNode(llvm::MDNode *Type) {
6457   if (!Type || Type->getNumOperands() < 3)
6458     return false;
6459 
6460   // In the new format type nodes shall have a reference to the parent type as
6461   // its first operand.
6462   return isa_and_nonnull<MDNode>(Type->getOperand(0));
6463 }
6464 
6465 bool TBAAVerifier::visitTBAAMetadata(Instruction &I, const MDNode *MD) {
6466   CheckTBAA(isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallInst>(I) ||
6467                 isa<VAArgInst>(I) || isa<AtomicRMWInst>(I) ||
6468                 isa<AtomicCmpXchgInst>(I),
6469             "This instruction shall not have a TBAA access tag!", &I);
6470 
6471   bool IsStructPathTBAA =
6472       isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3;
6473 
6474   CheckTBAA(IsStructPathTBAA,
6475             "Old-style TBAA is no longer allowed, use struct-path TBAA instead",
6476             &I);
6477 
6478   MDNode *BaseNode = dyn_cast_or_null<MDNode>(MD->getOperand(0));
6479   MDNode *AccessType = dyn_cast_or_null<MDNode>(MD->getOperand(1));
6480 
6481   bool IsNewFormat = isNewFormatTBAATypeNode(AccessType);
6482 
6483   if (IsNewFormat) {
6484     CheckTBAA(MD->getNumOperands() == 4 || MD->getNumOperands() == 5,
6485               "Access tag metadata must have either 4 or 5 operands", &I, MD);
6486   } else {
6487     CheckTBAA(MD->getNumOperands() < 5,
6488               "Struct tag metadata must have either 3 or 4 operands", &I, MD);
6489   }
6490 
6491   // Check the access size field.
6492   if (IsNewFormat) {
6493     auto *AccessSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
6494         MD->getOperand(3));
6495     CheckTBAA(AccessSizeNode, "Access size field must be a constant", &I, MD);
6496   }
6497 
6498   // Check the immutability flag.
6499   unsigned ImmutabilityFlagOpNo = IsNewFormat ? 4 : 3;
6500   if (MD->getNumOperands() == ImmutabilityFlagOpNo + 1) {
6501     auto *IsImmutableCI = mdconst::dyn_extract_or_null<ConstantInt>(
6502         MD->getOperand(ImmutabilityFlagOpNo));
6503     CheckTBAA(IsImmutableCI,
6504               "Immutability tag on struct tag metadata must be a constant", &I,
6505               MD);
6506     CheckTBAA(
6507         IsImmutableCI->isZero() || IsImmutableCI->isOne(),
6508         "Immutability part of the struct tag metadata must be either 0 or 1",
6509         &I, MD);
6510   }
6511 
6512   CheckTBAA(BaseNode && AccessType,
6513             "Malformed struct tag metadata: base and access-type "
6514             "should be non-null and point to Metadata nodes",
6515             &I, MD, BaseNode, AccessType);
6516 
6517   if (!IsNewFormat) {
6518     CheckTBAA(isValidScalarTBAANode(AccessType),
6519               "Access type node must be a valid scalar type", &I, MD,
6520               AccessType);
6521   }
6522 
6523   auto *OffsetCI = mdconst::dyn_extract_or_null<ConstantInt>(MD->getOperand(2));
6524   CheckTBAA(OffsetCI, "Offset must be constant integer", &I, MD);
6525 
6526   APInt Offset = OffsetCI->getValue();
6527   bool SeenAccessTypeInPath = false;
6528 
6529   SmallPtrSet<MDNode *, 4> StructPath;
6530 
6531   for (/* empty */; BaseNode && !IsRootTBAANode(BaseNode);
6532        BaseNode = getFieldNodeFromTBAABaseNode(I, BaseNode, Offset,
6533                                                IsNewFormat)) {
6534     if (!StructPath.insert(BaseNode).second) {
6535       CheckFailed("Cycle detected in struct path", &I, MD);
6536       return false;
6537     }
6538 
6539     bool Invalid;
6540     unsigned BaseNodeBitWidth;
6541     std::tie(Invalid, BaseNodeBitWidth) = verifyTBAABaseNode(I, BaseNode,
6542                                                              IsNewFormat);
6543 
6544     // If the base node is invalid in itself, then we've already printed all the
6545     // errors we wanted to print.
6546     if (Invalid)
6547       return false;
6548 
6549     SeenAccessTypeInPath |= BaseNode == AccessType;
6550 
6551     if (isValidScalarTBAANode(BaseNode) || BaseNode == AccessType)
6552       CheckTBAA(Offset == 0, "Offset not zero at the point of scalar access",
6553                 &I, MD, &Offset);
6554 
6555     CheckTBAA(BaseNodeBitWidth == Offset.getBitWidth() ||
6556                   (BaseNodeBitWidth == 0 && Offset == 0) ||
6557                   (IsNewFormat && BaseNodeBitWidth == ~0u),
6558               "Access bit-width not the same as description bit-width", &I, MD,
6559               BaseNodeBitWidth, Offset.getBitWidth());
6560 
6561     if (IsNewFormat && SeenAccessTypeInPath)
6562       break;
6563   }
6564 
6565   CheckTBAA(SeenAccessTypeInPath, "Did not see access type in access path!", &I,
6566             MD);
6567   return true;
6568 }
6569 
6570 char VerifierLegacyPass::ID = 0;
6571 INITIALIZE_PASS(VerifierLegacyPass, "verify", "Module Verifier", false, false)
6572 
6573 FunctionPass *llvm::createVerifierPass(bool FatalErrors) {
6574   return new VerifierLegacyPass(FatalErrors);
6575 }
6576 
6577 AnalysisKey VerifierAnalysis::Key;
6578 VerifierAnalysis::Result VerifierAnalysis::run(Module &M,
6579                                                ModuleAnalysisManager &) {
6580   Result Res;
6581   Res.IRBroken = llvm::verifyModule(M, &dbgs(), &Res.DebugInfoBroken);
6582   return Res;
6583 }
6584 
6585 VerifierAnalysis::Result VerifierAnalysis::run(Function &F,
6586                                                FunctionAnalysisManager &) {
6587   return { llvm::verifyFunction(F, &dbgs()), false };
6588 }
6589 
6590 PreservedAnalyses VerifierPass::run(Module &M, ModuleAnalysisManager &AM) {
6591   auto Res = AM.getResult<VerifierAnalysis>(M);
6592   if (FatalErrors && (Res.IRBroken || Res.DebugInfoBroken))
6593     report_fatal_error("Broken module found, compilation aborted!");
6594 
6595   return PreservedAnalyses::all();
6596 }
6597 
6598 PreservedAnalyses VerifierPass::run(Function &F, FunctionAnalysisManager &AM) {
6599   auto res = AM.getResult<VerifierAnalysis>(F);
6600   if (res.IRBroken && FatalErrors)
6601     report_fatal_error("Broken function found, compilation aborted!");
6602 
6603   return PreservedAnalyses::all();
6604 }
6605