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