106f32e7eSjoerg //===-- Verifier.cpp - Implement the Module Verifier -----------------------==//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This file defines the function verifier interface, that can be used for some
1006f32e7eSjoerg // sanity checking of input to the system.
1106f32e7eSjoerg //
1206f32e7eSjoerg // Note that this does not provide full `Java style' security and verifications,
1306f32e7eSjoerg // instead it just tries to ensure that code is well-formed.
1406f32e7eSjoerg //
1506f32e7eSjoerg //  * Both of a binary operator's parameters are of the same type
1606f32e7eSjoerg //  * Verify that the indices of mem access instructions match other operands
1706f32e7eSjoerg //  * Verify that arithmetic and other things are only performed on first-class
1806f32e7eSjoerg //    types.  Verify that shifts & logicals only happen on integrals f.e.
1906f32e7eSjoerg //  * All of the constants in a switch statement are of the correct type
2006f32e7eSjoerg //  * The code is in valid SSA form
2106f32e7eSjoerg //  * It should be illegal to put a label into any other type (like a structure)
2206f32e7eSjoerg //    or to return one. [except constant arrays!]
2306f32e7eSjoerg //  * Only phi nodes can be self referential: 'add i32 %0, %0 ; <int>:0' is bad
2406f32e7eSjoerg //  * PHI nodes must have an entry for each predecessor, with no extras.
2506f32e7eSjoerg //  * PHI nodes must be the first thing in a basic block, all grouped together
2606f32e7eSjoerg //  * PHI nodes must have at least one entry
2706f32e7eSjoerg //  * All basic blocks should only end with terminator insts, not contain them
2806f32e7eSjoerg //  * The entry node to a function must not have predecessors
2906f32e7eSjoerg //  * All Instructions must be embedded into a basic block
3006f32e7eSjoerg //  * Functions cannot take a void-typed parameter
3106f32e7eSjoerg //  * Verify that a function's argument list agrees with it's declared type.
3206f32e7eSjoerg //  * It is illegal to specify a name for a void value.
3306f32e7eSjoerg //  * It is illegal to have a internal global value with no initializer
3406f32e7eSjoerg //  * It is illegal to have a ret instruction that returns a value that does not
3506f32e7eSjoerg //    agree with the function return value type.
3606f32e7eSjoerg //  * Function call argument types match the function prototype
3706f32e7eSjoerg //  * A landing pad is defined by a landingpad instruction, and can be jumped to
3806f32e7eSjoerg //    only by the unwind edge of an invoke instruction.
3906f32e7eSjoerg //  * A landingpad instruction must be the first non-PHI instruction in the
4006f32e7eSjoerg //    block.
4106f32e7eSjoerg //  * Landingpad instructions must be in a function with a personality function.
4206f32e7eSjoerg //  * All other things that are tested by asserts spread about the code...
4306f32e7eSjoerg //
4406f32e7eSjoerg //===----------------------------------------------------------------------===//
4506f32e7eSjoerg 
4606f32e7eSjoerg #include "llvm/IR/Verifier.h"
4706f32e7eSjoerg #include "llvm/ADT/APFloat.h"
4806f32e7eSjoerg #include "llvm/ADT/APInt.h"
4906f32e7eSjoerg #include "llvm/ADT/ArrayRef.h"
5006f32e7eSjoerg #include "llvm/ADT/DenseMap.h"
5106f32e7eSjoerg #include "llvm/ADT/MapVector.h"
5206f32e7eSjoerg #include "llvm/ADT/Optional.h"
5306f32e7eSjoerg #include "llvm/ADT/STLExtras.h"
5406f32e7eSjoerg #include "llvm/ADT/SmallPtrSet.h"
5506f32e7eSjoerg #include "llvm/ADT/SmallSet.h"
5606f32e7eSjoerg #include "llvm/ADT/SmallVector.h"
5706f32e7eSjoerg #include "llvm/ADT/StringExtras.h"
5806f32e7eSjoerg #include "llvm/ADT/StringMap.h"
5906f32e7eSjoerg #include "llvm/ADT/StringRef.h"
6006f32e7eSjoerg #include "llvm/ADT/Twine.h"
6106f32e7eSjoerg #include "llvm/ADT/ilist.h"
6206f32e7eSjoerg #include "llvm/BinaryFormat/Dwarf.h"
6306f32e7eSjoerg #include "llvm/IR/Argument.h"
6406f32e7eSjoerg #include "llvm/IR/Attributes.h"
6506f32e7eSjoerg #include "llvm/IR/BasicBlock.h"
6606f32e7eSjoerg #include "llvm/IR/CFG.h"
6706f32e7eSjoerg #include "llvm/IR/CallingConv.h"
6806f32e7eSjoerg #include "llvm/IR/Comdat.h"
6906f32e7eSjoerg #include "llvm/IR/Constant.h"
7006f32e7eSjoerg #include "llvm/IR/ConstantRange.h"
7106f32e7eSjoerg #include "llvm/IR/Constants.h"
7206f32e7eSjoerg #include "llvm/IR/DataLayout.h"
7306f32e7eSjoerg #include "llvm/IR/DebugInfo.h"
7406f32e7eSjoerg #include "llvm/IR/DebugInfoMetadata.h"
7506f32e7eSjoerg #include "llvm/IR/DebugLoc.h"
7606f32e7eSjoerg #include "llvm/IR/DerivedTypes.h"
7706f32e7eSjoerg #include "llvm/IR/Dominators.h"
7806f32e7eSjoerg #include "llvm/IR/Function.h"
7906f32e7eSjoerg #include "llvm/IR/GlobalAlias.h"
8006f32e7eSjoerg #include "llvm/IR/GlobalValue.h"
8106f32e7eSjoerg #include "llvm/IR/GlobalVariable.h"
8206f32e7eSjoerg #include "llvm/IR/InlineAsm.h"
8306f32e7eSjoerg #include "llvm/IR/InstVisitor.h"
8406f32e7eSjoerg #include "llvm/IR/InstrTypes.h"
8506f32e7eSjoerg #include "llvm/IR/Instruction.h"
8606f32e7eSjoerg #include "llvm/IR/Instructions.h"
8706f32e7eSjoerg #include "llvm/IR/IntrinsicInst.h"
8806f32e7eSjoerg #include "llvm/IR/Intrinsics.h"
89*da58b97aSjoerg #include "llvm/IR/IntrinsicsWebAssembly.h"
9006f32e7eSjoerg #include "llvm/IR/LLVMContext.h"
9106f32e7eSjoerg #include "llvm/IR/Metadata.h"
9206f32e7eSjoerg #include "llvm/IR/Module.h"
9306f32e7eSjoerg #include "llvm/IR/ModuleSlotTracker.h"
9406f32e7eSjoerg #include "llvm/IR/PassManager.h"
9506f32e7eSjoerg #include "llvm/IR/Statepoint.h"
9606f32e7eSjoerg #include "llvm/IR/Type.h"
9706f32e7eSjoerg #include "llvm/IR/Use.h"
9806f32e7eSjoerg #include "llvm/IR/User.h"
9906f32e7eSjoerg #include "llvm/IR/Value.h"
100*da58b97aSjoerg #include "llvm/InitializePasses.h"
10106f32e7eSjoerg #include "llvm/Pass.h"
10206f32e7eSjoerg #include "llvm/Support/AtomicOrdering.h"
10306f32e7eSjoerg #include "llvm/Support/Casting.h"
10406f32e7eSjoerg #include "llvm/Support/CommandLine.h"
10506f32e7eSjoerg #include "llvm/Support/Debug.h"
10606f32e7eSjoerg #include "llvm/Support/ErrorHandling.h"
10706f32e7eSjoerg #include "llvm/Support/MathExtras.h"
10806f32e7eSjoerg #include "llvm/Support/raw_ostream.h"
10906f32e7eSjoerg #include <algorithm>
11006f32e7eSjoerg #include <cassert>
11106f32e7eSjoerg #include <cstdint>
11206f32e7eSjoerg #include <memory>
11306f32e7eSjoerg #include <string>
11406f32e7eSjoerg #include <utility>
11506f32e7eSjoerg 
11606f32e7eSjoerg using namespace llvm;
11706f32e7eSjoerg 
118*da58b97aSjoerg static cl::opt<bool> VerifyNoAliasScopeDomination(
119*da58b97aSjoerg     "verify-noalias-scope-decl-dom", cl::Hidden, cl::init(false),
120*da58b97aSjoerg     cl::desc("Ensure that llvm.experimental.noalias.scope.decl for identical "
121*da58b97aSjoerg              "scopes are not dominating"));
122*da58b97aSjoerg 
12306f32e7eSjoerg namespace llvm {
12406f32e7eSjoerg 
12506f32e7eSjoerg struct VerifierSupport {
12606f32e7eSjoerg   raw_ostream *OS;
12706f32e7eSjoerg   const Module &M;
12806f32e7eSjoerg   ModuleSlotTracker MST;
12906f32e7eSjoerg   Triple TT;
13006f32e7eSjoerg   const DataLayout &DL;
13106f32e7eSjoerg   LLVMContext &Context;
13206f32e7eSjoerg 
13306f32e7eSjoerg   /// Track the brokenness of the module while recursively visiting.
13406f32e7eSjoerg   bool Broken = false;
13506f32e7eSjoerg   /// Broken debug info can be "recovered" from by stripping the debug info.
13606f32e7eSjoerg   bool BrokenDebugInfo = false;
13706f32e7eSjoerg   /// Whether to treat broken debug info as an error.
13806f32e7eSjoerg   bool TreatBrokenDebugInfoAsError = true;
13906f32e7eSjoerg 
VerifierSupportllvm::VerifierSupport14006f32e7eSjoerg   explicit VerifierSupport(raw_ostream *OS, const Module &M)
14106f32e7eSjoerg       : OS(OS), M(M), MST(&M), TT(M.getTargetTriple()), DL(M.getDataLayout()),
14206f32e7eSjoerg         Context(M.getContext()) {}
14306f32e7eSjoerg 
14406f32e7eSjoerg private:
Writellvm::VerifierSupport14506f32e7eSjoerg   void Write(const Module *M) {
14606f32e7eSjoerg     *OS << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
14706f32e7eSjoerg   }
14806f32e7eSjoerg 
Writellvm::VerifierSupport14906f32e7eSjoerg   void Write(const Value *V) {
15006f32e7eSjoerg     if (V)
15106f32e7eSjoerg       Write(*V);
15206f32e7eSjoerg   }
15306f32e7eSjoerg 
Writellvm::VerifierSupport15406f32e7eSjoerg   void Write(const Value &V) {
15506f32e7eSjoerg     if (isa<Instruction>(V)) {
15606f32e7eSjoerg       V.print(*OS, MST);
15706f32e7eSjoerg       *OS << '\n';
15806f32e7eSjoerg     } else {
15906f32e7eSjoerg       V.printAsOperand(*OS, true, MST);
16006f32e7eSjoerg       *OS << '\n';
16106f32e7eSjoerg     }
16206f32e7eSjoerg   }
16306f32e7eSjoerg 
Writellvm::VerifierSupport16406f32e7eSjoerg   void Write(const Metadata *MD) {
16506f32e7eSjoerg     if (!MD)
16606f32e7eSjoerg       return;
16706f32e7eSjoerg     MD->print(*OS, MST, &M);
16806f32e7eSjoerg     *OS << '\n';
16906f32e7eSjoerg   }
17006f32e7eSjoerg 
Writellvm::VerifierSupport17106f32e7eSjoerg   template <class T> void Write(const MDTupleTypedArrayWrapper<T> &MD) {
17206f32e7eSjoerg     Write(MD.get());
17306f32e7eSjoerg   }
17406f32e7eSjoerg 
Writellvm::VerifierSupport17506f32e7eSjoerg   void Write(const NamedMDNode *NMD) {
17606f32e7eSjoerg     if (!NMD)
17706f32e7eSjoerg       return;
17806f32e7eSjoerg     NMD->print(*OS, MST);
17906f32e7eSjoerg     *OS << '\n';
18006f32e7eSjoerg   }
18106f32e7eSjoerg 
Writellvm::VerifierSupport18206f32e7eSjoerg   void Write(Type *T) {
18306f32e7eSjoerg     if (!T)
18406f32e7eSjoerg       return;
18506f32e7eSjoerg     *OS << ' ' << *T;
18606f32e7eSjoerg   }
18706f32e7eSjoerg 
Writellvm::VerifierSupport18806f32e7eSjoerg   void Write(const Comdat *C) {
18906f32e7eSjoerg     if (!C)
19006f32e7eSjoerg       return;
19106f32e7eSjoerg     *OS << *C;
19206f32e7eSjoerg   }
19306f32e7eSjoerg 
Writellvm::VerifierSupport19406f32e7eSjoerg   void Write(const APInt *AI) {
19506f32e7eSjoerg     if (!AI)
19606f32e7eSjoerg       return;
19706f32e7eSjoerg     *OS << *AI << '\n';
19806f32e7eSjoerg   }
19906f32e7eSjoerg 
Writellvm::VerifierSupport20006f32e7eSjoerg   void Write(const unsigned i) { *OS << i << '\n'; }
20106f32e7eSjoerg 
202*da58b97aSjoerg   // NOLINTNEXTLINE(readability-identifier-naming)
Writellvm::VerifierSupport203*da58b97aSjoerg   void Write(const Attribute *A) {
204*da58b97aSjoerg     if (!A)
205*da58b97aSjoerg       return;
206*da58b97aSjoerg     *OS << A->getAsString() << '\n';
207*da58b97aSjoerg   }
208*da58b97aSjoerg 
209*da58b97aSjoerg   // NOLINTNEXTLINE(readability-identifier-naming)
Writellvm::VerifierSupport210*da58b97aSjoerg   void Write(const AttributeSet *AS) {
211*da58b97aSjoerg     if (!AS)
212*da58b97aSjoerg       return;
213*da58b97aSjoerg     *OS << AS->getAsString() << '\n';
214*da58b97aSjoerg   }
215*da58b97aSjoerg 
216*da58b97aSjoerg   // NOLINTNEXTLINE(readability-identifier-naming)
Writellvm::VerifierSupport217*da58b97aSjoerg   void Write(const AttributeList *AL) {
218*da58b97aSjoerg     if (!AL)
219*da58b97aSjoerg       return;
220*da58b97aSjoerg     AL->print(*OS);
221*da58b97aSjoerg   }
222*da58b97aSjoerg 
Writellvm::VerifierSupport22306f32e7eSjoerg   template <typename T> void Write(ArrayRef<T> Vs) {
22406f32e7eSjoerg     for (const T &V : Vs)
22506f32e7eSjoerg       Write(V);
22606f32e7eSjoerg   }
22706f32e7eSjoerg 
22806f32e7eSjoerg   template <typename T1, typename... Ts>
WriteTsllvm::VerifierSupport22906f32e7eSjoerg   void WriteTs(const T1 &V1, const Ts &... Vs) {
23006f32e7eSjoerg     Write(V1);
23106f32e7eSjoerg     WriteTs(Vs...);
23206f32e7eSjoerg   }
23306f32e7eSjoerg 
WriteTsllvm::VerifierSupport23406f32e7eSjoerg   template <typename... Ts> void WriteTs() {}
23506f32e7eSjoerg 
23606f32e7eSjoerg public:
23706f32e7eSjoerg   /// A check failed, so printout out the condition and the message.
23806f32e7eSjoerg   ///
23906f32e7eSjoerg   /// This provides a nice place to put a breakpoint if you want to see why
24006f32e7eSjoerg   /// something is not correct.
CheckFailedllvm::VerifierSupport24106f32e7eSjoerg   void CheckFailed(const Twine &Message) {
24206f32e7eSjoerg     if (OS)
24306f32e7eSjoerg       *OS << Message << '\n';
24406f32e7eSjoerg     Broken = true;
24506f32e7eSjoerg   }
24606f32e7eSjoerg 
24706f32e7eSjoerg   /// A check failed (with values to print).
24806f32e7eSjoerg   ///
24906f32e7eSjoerg   /// This calls the Message-only version so that the above is easier to set a
25006f32e7eSjoerg   /// breakpoint on.
25106f32e7eSjoerg   template <typename T1, typename... Ts>
CheckFailedllvm::VerifierSupport25206f32e7eSjoerg   void CheckFailed(const Twine &Message, const T1 &V1, const Ts &... Vs) {
25306f32e7eSjoerg     CheckFailed(Message);
25406f32e7eSjoerg     if (OS)
25506f32e7eSjoerg       WriteTs(V1, Vs...);
25606f32e7eSjoerg   }
25706f32e7eSjoerg 
25806f32e7eSjoerg   /// A debug info check failed.
DebugInfoCheckFailedllvm::VerifierSupport25906f32e7eSjoerg   void DebugInfoCheckFailed(const Twine &Message) {
26006f32e7eSjoerg     if (OS)
26106f32e7eSjoerg       *OS << Message << '\n';
26206f32e7eSjoerg     Broken |= TreatBrokenDebugInfoAsError;
26306f32e7eSjoerg     BrokenDebugInfo = true;
26406f32e7eSjoerg   }
26506f32e7eSjoerg 
26606f32e7eSjoerg   /// A debug info check failed (with values to print).
26706f32e7eSjoerg   template <typename T1, typename... Ts>
DebugInfoCheckFailedllvm::VerifierSupport26806f32e7eSjoerg   void DebugInfoCheckFailed(const Twine &Message, const T1 &V1,
26906f32e7eSjoerg                             const Ts &... Vs) {
27006f32e7eSjoerg     DebugInfoCheckFailed(Message);
27106f32e7eSjoerg     if (OS)
27206f32e7eSjoerg       WriteTs(V1, Vs...);
27306f32e7eSjoerg   }
27406f32e7eSjoerg };
27506f32e7eSjoerg 
27606f32e7eSjoerg } // namespace llvm
27706f32e7eSjoerg 
27806f32e7eSjoerg namespace {
27906f32e7eSjoerg 
28006f32e7eSjoerg class Verifier : public InstVisitor<Verifier>, VerifierSupport {
28106f32e7eSjoerg   friend class InstVisitor<Verifier>;
28206f32e7eSjoerg 
28306f32e7eSjoerg   DominatorTree DT;
28406f32e7eSjoerg 
28506f32e7eSjoerg   /// When verifying a basic block, keep track of all of the
28606f32e7eSjoerg   /// instructions we have seen so far.
28706f32e7eSjoerg   ///
28806f32e7eSjoerg   /// This allows us to do efficient dominance checks for the case when an
28906f32e7eSjoerg   /// instruction has an operand that is an instruction in the same block.
29006f32e7eSjoerg   SmallPtrSet<Instruction *, 16> InstsInThisBlock;
29106f32e7eSjoerg 
29206f32e7eSjoerg   /// Keep track of the metadata nodes that have been checked already.
29306f32e7eSjoerg   SmallPtrSet<const Metadata *, 32> MDNodes;
29406f32e7eSjoerg 
29506f32e7eSjoerg   /// Keep track which DISubprogram is attached to which function.
29606f32e7eSjoerg   DenseMap<const DISubprogram *, const Function *> DISubprogramAttachments;
29706f32e7eSjoerg 
29806f32e7eSjoerg   /// Track all DICompileUnits visited.
29906f32e7eSjoerg   SmallPtrSet<const Metadata *, 2> CUVisited;
30006f32e7eSjoerg 
30106f32e7eSjoerg   /// The result type for a landingpad.
30206f32e7eSjoerg   Type *LandingPadResultTy;
30306f32e7eSjoerg 
30406f32e7eSjoerg   /// Whether we've seen a call to @llvm.localescape in this function
30506f32e7eSjoerg   /// already.
30606f32e7eSjoerg   bool SawFrameEscape;
30706f32e7eSjoerg 
30806f32e7eSjoerg   /// Whether the current function has a DISubprogram attached to it.
30906f32e7eSjoerg   bool HasDebugInfo = false;
31006f32e7eSjoerg 
311*da58b97aSjoerg   /// The current source language.
312*da58b97aSjoerg   dwarf::SourceLanguage CurrentSourceLang = dwarf::DW_LANG_lo_user;
313*da58b97aSjoerg 
31406f32e7eSjoerg   /// Whether source was present on the first DIFile encountered in each CU.
31506f32e7eSjoerg   DenseMap<const DICompileUnit *, bool> HasSourceDebugInfo;
31606f32e7eSjoerg 
31706f32e7eSjoerg   /// Stores the count of how many objects were passed to llvm.localescape for a
31806f32e7eSjoerg   /// given function and the largest index passed to llvm.localrecover.
31906f32e7eSjoerg   DenseMap<Function *, std::pair<unsigned, unsigned>> FrameEscapeInfo;
32006f32e7eSjoerg 
32106f32e7eSjoerg   // Maps catchswitches and cleanuppads that unwind to siblings to the
32206f32e7eSjoerg   // terminators that indicate the unwind, used to detect cycles therein.
32306f32e7eSjoerg   MapVector<Instruction *, Instruction *> SiblingFuncletInfo;
32406f32e7eSjoerg 
32506f32e7eSjoerg   /// Cache of constants visited in search of ConstantExprs.
32606f32e7eSjoerg   SmallPtrSet<const Constant *, 32> ConstantExprVisited;
32706f32e7eSjoerg 
32806f32e7eSjoerg   /// Cache of declarations of the llvm.experimental.deoptimize.<ty> intrinsic.
32906f32e7eSjoerg   SmallVector<const Function *, 4> DeoptimizeDeclarations;
33006f32e7eSjoerg 
331*da58b97aSjoerg   /// Cache of attribute lists verified.
332*da58b97aSjoerg   SmallPtrSet<const void *, 32> AttributeListsVisited;
333*da58b97aSjoerg 
33406f32e7eSjoerg   // Verify that this GlobalValue is only used in this module.
33506f32e7eSjoerg   // This map is used to avoid visiting uses twice. We can arrive at a user
33606f32e7eSjoerg   // twice, if they have multiple operands. In particular for very large
33706f32e7eSjoerg   // constant expressions, we can arrive at a particular user many times.
33806f32e7eSjoerg   SmallPtrSet<const Value *, 32> GlobalValueVisited;
33906f32e7eSjoerg 
34006f32e7eSjoerg   // Keeps track of duplicate function argument debug info.
34106f32e7eSjoerg   SmallVector<const DILocalVariable *, 16> DebugFnArgs;
34206f32e7eSjoerg 
34306f32e7eSjoerg   TBAAVerifier TBAAVerifyHelper;
34406f32e7eSjoerg 
345*da58b97aSjoerg   SmallVector<IntrinsicInst *, 4> NoAliasScopeDecls;
346*da58b97aSjoerg 
34706f32e7eSjoerg   void checkAtomicMemAccessSize(Type *Ty, const Instruction *I);
34806f32e7eSjoerg 
34906f32e7eSjoerg public:
Verifier(raw_ostream * OS,bool ShouldTreatBrokenDebugInfoAsError,const Module & M)35006f32e7eSjoerg   explicit Verifier(raw_ostream *OS, bool ShouldTreatBrokenDebugInfoAsError,
35106f32e7eSjoerg                     const Module &M)
35206f32e7eSjoerg       : VerifierSupport(OS, M), LandingPadResultTy(nullptr),
35306f32e7eSjoerg         SawFrameEscape(false), TBAAVerifyHelper(this) {
35406f32e7eSjoerg     TreatBrokenDebugInfoAsError = ShouldTreatBrokenDebugInfoAsError;
35506f32e7eSjoerg   }
35606f32e7eSjoerg 
hasBrokenDebugInfo() const35706f32e7eSjoerg   bool hasBrokenDebugInfo() const { return BrokenDebugInfo; }
35806f32e7eSjoerg 
verify(const Function & F)35906f32e7eSjoerg   bool verify(const Function &F) {
36006f32e7eSjoerg     assert(F.getParent() == &M &&
36106f32e7eSjoerg            "An instance of this class only works with a specific module!");
36206f32e7eSjoerg 
36306f32e7eSjoerg     // First ensure the function is well-enough formed to compute dominance
36406f32e7eSjoerg     // information, and directly compute a dominance tree. We don't rely on the
36506f32e7eSjoerg     // pass manager to provide this as it isolates us from a potentially
36606f32e7eSjoerg     // out-of-date dominator tree and makes it significantly more complex to run
36706f32e7eSjoerg     // this code outside of a pass manager.
36806f32e7eSjoerg     // FIXME: It's really gross that we have to cast away constness here.
36906f32e7eSjoerg     if (!F.empty())
37006f32e7eSjoerg       DT.recalculate(const_cast<Function &>(F));
37106f32e7eSjoerg 
37206f32e7eSjoerg     for (const BasicBlock &BB : F) {
37306f32e7eSjoerg       if (!BB.empty() && BB.back().isTerminator())
37406f32e7eSjoerg         continue;
37506f32e7eSjoerg 
37606f32e7eSjoerg       if (OS) {
37706f32e7eSjoerg         *OS << "Basic Block in function '" << F.getName()
37806f32e7eSjoerg             << "' does not have terminator!\n";
37906f32e7eSjoerg         BB.printAsOperand(*OS, true, MST);
38006f32e7eSjoerg         *OS << "\n";
38106f32e7eSjoerg       }
38206f32e7eSjoerg       return false;
38306f32e7eSjoerg     }
38406f32e7eSjoerg 
38506f32e7eSjoerg     Broken = false;
38606f32e7eSjoerg     // FIXME: We strip const here because the inst visitor strips const.
38706f32e7eSjoerg     visit(const_cast<Function &>(F));
38806f32e7eSjoerg     verifySiblingFuncletUnwinds();
38906f32e7eSjoerg     InstsInThisBlock.clear();
39006f32e7eSjoerg     DebugFnArgs.clear();
39106f32e7eSjoerg     LandingPadResultTy = nullptr;
39206f32e7eSjoerg     SawFrameEscape = false;
39306f32e7eSjoerg     SiblingFuncletInfo.clear();
394*da58b97aSjoerg     verifyNoAliasScopeDecl();
395*da58b97aSjoerg     NoAliasScopeDecls.clear();
39606f32e7eSjoerg 
39706f32e7eSjoerg     return !Broken;
39806f32e7eSjoerg   }
39906f32e7eSjoerg 
40006f32e7eSjoerg   /// Verify the module that this instance of \c Verifier was initialized with.
verify()40106f32e7eSjoerg   bool verify() {
40206f32e7eSjoerg     Broken = false;
40306f32e7eSjoerg 
40406f32e7eSjoerg     // Collect all declarations of the llvm.experimental.deoptimize intrinsic.
40506f32e7eSjoerg     for (const Function &F : M)
40606f32e7eSjoerg       if (F.getIntrinsicID() == Intrinsic::experimental_deoptimize)
40706f32e7eSjoerg         DeoptimizeDeclarations.push_back(&F);
40806f32e7eSjoerg 
40906f32e7eSjoerg     // Now that we've visited every function, verify that we never asked to
41006f32e7eSjoerg     // recover a frame index that wasn't escaped.
41106f32e7eSjoerg     verifyFrameRecoverIndices();
41206f32e7eSjoerg     for (const GlobalVariable &GV : M.globals())
41306f32e7eSjoerg       visitGlobalVariable(GV);
41406f32e7eSjoerg 
41506f32e7eSjoerg     for (const GlobalAlias &GA : M.aliases())
41606f32e7eSjoerg       visitGlobalAlias(GA);
41706f32e7eSjoerg 
41806f32e7eSjoerg     for (const NamedMDNode &NMD : M.named_metadata())
41906f32e7eSjoerg       visitNamedMDNode(NMD);
42006f32e7eSjoerg 
42106f32e7eSjoerg     for (const StringMapEntry<Comdat> &SMEC : M.getComdatSymbolTable())
42206f32e7eSjoerg       visitComdat(SMEC.getValue());
42306f32e7eSjoerg 
42406f32e7eSjoerg     visitModuleFlags(M);
42506f32e7eSjoerg     visitModuleIdents(M);
42606f32e7eSjoerg     visitModuleCommandLines(M);
42706f32e7eSjoerg 
42806f32e7eSjoerg     verifyCompileUnits();
42906f32e7eSjoerg 
43006f32e7eSjoerg     verifyDeoptimizeCallingConvs();
43106f32e7eSjoerg     DISubprogramAttachments.clear();
43206f32e7eSjoerg     return !Broken;
43306f32e7eSjoerg   }
43406f32e7eSjoerg 
43506f32e7eSjoerg private:
436*da58b97aSjoerg   /// Whether a metadata node is allowed to be, or contain, a DILocation.
437*da58b97aSjoerg   enum class AreDebugLocsAllowed { No, Yes };
438*da58b97aSjoerg 
43906f32e7eSjoerg   // Verification methods...
44006f32e7eSjoerg   void visitGlobalValue(const GlobalValue &GV);
44106f32e7eSjoerg   void visitGlobalVariable(const GlobalVariable &GV);
44206f32e7eSjoerg   void visitGlobalAlias(const GlobalAlias &GA);
44306f32e7eSjoerg   void visitAliaseeSubExpr(const GlobalAlias &A, const Constant &C);
44406f32e7eSjoerg   void visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias *> &Visited,
44506f32e7eSjoerg                            const GlobalAlias &A, const Constant &C);
44606f32e7eSjoerg   void visitNamedMDNode(const NamedMDNode &NMD);
447*da58b97aSjoerg   void visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs);
44806f32e7eSjoerg   void visitMetadataAsValue(const MetadataAsValue &MD, Function *F);
44906f32e7eSjoerg   void visitValueAsMetadata(const ValueAsMetadata &MD, Function *F);
45006f32e7eSjoerg   void visitComdat(const Comdat &C);
45106f32e7eSjoerg   void visitModuleIdents(const Module &M);
45206f32e7eSjoerg   void visitModuleCommandLines(const Module &M);
45306f32e7eSjoerg   void visitModuleFlags(const Module &M);
45406f32e7eSjoerg   void visitModuleFlag(const MDNode *Op,
45506f32e7eSjoerg                        DenseMap<const MDString *, const MDNode *> &SeenIDs,
45606f32e7eSjoerg                        SmallVectorImpl<const MDNode *> &Requirements);
45706f32e7eSjoerg   void visitModuleFlagCGProfileEntry(const MDOperand &MDO);
45806f32e7eSjoerg   void visitFunction(const Function &F);
45906f32e7eSjoerg   void visitBasicBlock(BasicBlock &BB);
46006f32e7eSjoerg   void visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty);
46106f32e7eSjoerg   void visitDereferenceableMetadata(Instruction &I, MDNode *MD);
46206f32e7eSjoerg   void visitProfMetadata(Instruction &I, MDNode *MD);
463*da58b97aSjoerg   void visitAnnotationMetadata(MDNode *Annotation);
46406f32e7eSjoerg 
46506f32e7eSjoerg   template <class Ty> bool isValidMetadataArray(const MDTuple &N);
46606f32e7eSjoerg #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N);
46706f32e7eSjoerg #include "llvm/IR/Metadata.def"
46806f32e7eSjoerg   void visitDIScope(const DIScope &N);
46906f32e7eSjoerg   void visitDIVariable(const DIVariable &N);
47006f32e7eSjoerg   void visitDILexicalBlockBase(const DILexicalBlockBase &N);
47106f32e7eSjoerg   void visitDITemplateParameter(const DITemplateParameter &N);
47206f32e7eSjoerg 
47306f32e7eSjoerg   void visitTemplateParams(const MDNode &N, const Metadata &RawParams);
47406f32e7eSjoerg 
47506f32e7eSjoerg   // InstVisitor overrides...
47606f32e7eSjoerg   using InstVisitor<Verifier>::visit;
47706f32e7eSjoerg   void visit(Instruction &I);
47806f32e7eSjoerg 
47906f32e7eSjoerg   void visitTruncInst(TruncInst &I);
48006f32e7eSjoerg   void visitZExtInst(ZExtInst &I);
48106f32e7eSjoerg   void visitSExtInst(SExtInst &I);
48206f32e7eSjoerg   void visitFPTruncInst(FPTruncInst &I);
48306f32e7eSjoerg   void visitFPExtInst(FPExtInst &I);
48406f32e7eSjoerg   void visitFPToUIInst(FPToUIInst &I);
48506f32e7eSjoerg   void visitFPToSIInst(FPToSIInst &I);
48606f32e7eSjoerg   void visitUIToFPInst(UIToFPInst &I);
48706f32e7eSjoerg   void visitSIToFPInst(SIToFPInst &I);
48806f32e7eSjoerg   void visitIntToPtrInst(IntToPtrInst &I);
48906f32e7eSjoerg   void visitPtrToIntInst(PtrToIntInst &I);
49006f32e7eSjoerg   void visitBitCastInst(BitCastInst &I);
49106f32e7eSjoerg   void visitAddrSpaceCastInst(AddrSpaceCastInst &I);
49206f32e7eSjoerg   void visitPHINode(PHINode &PN);
49306f32e7eSjoerg   void visitCallBase(CallBase &Call);
49406f32e7eSjoerg   void visitUnaryOperator(UnaryOperator &U);
49506f32e7eSjoerg   void visitBinaryOperator(BinaryOperator &B);
49606f32e7eSjoerg   void visitICmpInst(ICmpInst &IC);
49706f32e7eSjoerg   void visitFCmpInst(FCmpInst &FC);
49806f32e7eSjoerg   void visitExtractElementInst(ExtractElementInst &EI);
49906f32e7eSjoerg   void visitInsertElementInst(InsertElementInst &EI);
50006f32e7eSjoerg   void visitShuffleVectorInst(ShuffleVectorInst &EI);
visitVAArgInst(VAArgInst & VAA)50106f32e7eSjoerg   void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
50206f32e7eSjoerg   void visitCallInst(CallInst &CI);
50306f32e7eSjoerg   void visitInvokeInst(InvokeInst &II);
50406f32e7eSjoerg   void visitGetElementPtrInst(GetElementPtrInst &GEP);
50506f32e7eSjoerg   void visitLoadInst(LoadInst &LI);
50606f32e7eSjoerg   void visitStoreInst(StoreInst &SI);
50706f32e7eSjoerg   void verifyDominatesUse(Instruction &I, unsigned i);
50806f32e7eSjoerg   void visitInstruction(Instruction &I);
50906f32e7eSjoerg   void visitTerminator(Instruction &I);
51006f32e7eSjoerg   void visitBranchInst(BranchInst &BI);
51106f32e7eSjoerg   void visitReturnInst(ReturnInst &RI);
51206f32e7eSjoerg   void visitSwitchInst(SwitchInst &SI);
51306f32e7eSjoerg   void visitIndirectBrInst(IndirectBrInst &BI);
51406f32e7eSjoerg   void visitCallBrInst(CallBrInst &CBI);
51506f32e7eSjoerg   void visitSelectInst(SelectInst &SI);
51606f32e7eSjoerg   void visitUserOp1(Instruction &I);
visitUserOp2(Instruction & I)51706f32e7eSjoerg   void visitUserOp2(Instruction &I) { visitUserOp1(I); }
51806f32e7eSjoerg   void visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call);
51906f32e7eSjoerg   void visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI);
52006f32e7eSjoerg   void visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII);
52106f32e7eSjoerg   void visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI);
52206f32e7eSjoerg   void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI);
52306f32e7eSjoerg   void visitAtomicRMWInst(AtomicRMWInst &RMWI);
52406f32e7eSjoerg   void visitFenceInst(FenceInst &FI);
52506f32e7eSjoerg   void visitAllocaInst(AllocaInst &AI);
52606f32e7eSjoerg   void visitExtractValueInst(ExtractValueInst &EVI);
52706f32e7eSjoerg   void visitInsertValueInst(InsertValueInst &IVI);
52806f32e7eSjoerg   void visitEHPadPredecessors(Instruction &I);
52906f32e7eSjoerg   void visitLandingPadInst(LandingPadInst &LPI);
53006f32e7eSjoerg   void visitResumeInst(ResumeInst &RI);
53106f32e7eSjoerg   void visitCatchPadInst(CatchPadInst &CPI);
53206f32e7eSjoerg   void visitCatchReturnInst(CatchReturnInst &CatchReturn);
53306f32e7eSjoerg   void visitCleanupPadInst(CleanupPadInst &CPI);
53406f32e7eSjoerg   void visitFuncletPadInst(FuncletPadInst &FPI);
53506f32e7eSjoerg   void visitCatchSwitchInst(CatchSwitchInst &CatchSwitch);
53606f32e7eSjoerg   void visitCleanupReturnInst(CleanupReturnInst &CRI);
53706f32e7eSjoerg 
53806f32e7eSjoerg   void verifySwiftErrorCall(CallBase &Call, const Value *SwiftErrorVal);
53906f32e7eSjoerg   void verifySwiftErrorValue(const Value *SwiftErrorVal);
54006f32e7eSjoerg   void verifyMustTailCall(CallInst &CI);
54106f32e7eSjoerg   bool verifyAttributeCount(AttributeList Attrs, unsigned Params);
54206f32e7eSjoerg   void verifyAttributeTypes(AttributeSet Attrs, bool IsFunction,
54306f32e7eSjoerg                             const Value *V);
54406f32e7eSjoerg   void verifyParameterAttrs(AttributeSet Attrs, Type *Ty, const Value *V);
54506f32e7eSjoerg   void verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
54606f32e7eSjoerg                            const Value *V, bool IsIntrinsic);
54706f32e7eSjoerg   void verifyFunctionMetadata(ArrayRef<std::pair<unsigned, MDNode *>> MDs);
54806f32e7eSjoerg 
54906f32e7eSjoerg   void visitConstantExprsRecursively(const Constant *EntryC);
55006f32e7eSjoerg   void visitConstantExpr(const ConstantExpr *CE);
55106f32e7eSjoerg   void verifyStatepoint(const CallBase &Call);
55206f32e7eSjoerg   void verifyFrameRecoverIndices();
55306f32e7eSjoerg   void verifySiblingFuncletUnwinds();
55406f32e7eSjoerg 
55506f32e7eSjoerg   void verifyFragmentExpression(const DbgVariableIntrinsic &I);
55606f32e7eSjoerg   template <typename ValueOrMetadata>
55706f32e7eSjoerg   void verifyFragmentExpression(const DIVariable &V,
55806f32e7eSjoerg                                 DIExpression::FragmentInfo Fragment,
55906f32e7eSjoerg                                 ValueOrMetadata *Desc);
56006f32e7eSjoerg   void verifyFnArgs(const DbgVariableIntrinsic &I);
56106f32e7eSjoerg   void verifyNotEntryValue(const DbgVariableIntrinsic &I);
56206f32e7eSjoerg 
56306f32e7eSjoerg   /// Module-level debug info verification...
56406f32e7eSjoerg   void verifyCompileUnits();
56506f32e7eSjoerg 
56606f32e7eSjoerg   /// Module-level verification that all @llvm.experimental.deoptimize
56706f32e7eSjoerg   /// declarations share the same calling convention.
56806f32e7eSjoerg   void verifyDeoptimizeCallingConvs();
56906f32e7eSjoerg 
57006f32e7eSjoerg   /// Verify all-or-nothing property of DIFile source attribute within a CU.
57106f32e7eSjoerg   void verifySourceDebugInfo(const DICompileUnit &U, const DIFile &F);
572*da58b97aSjoerg 
573*da58b97aSjoerg   /// Verify the llvm.experimental.noalias.scope.decl declarations
574*da58b97aSjoerg   void verifyNoAliasScopeDecl();
57506f32e7eSjoerg };
57606f32e7eSjoerg 
57706f32e7eSjoerg } // end anonymous namespace
57806f32e7eSjoerg 
57906f32e7eSjoerg /// We know that cond should be true, if not print an error message.
58006f32e7eSjoerg #define Assert(C, ...) \
58106f32e7eSjoerg   do { if (!(C)) { CheckFailed(__VA_ARGS__); return; } } while (false)
58206f32e7eSjoerg 
58306f32e7eSjoerg /// We know that a debug info condition should be true, if not print
58406f32e7eSjoerg /// an error message.
58506f32e7eSjoerg #define AssertDI(C, ...) \
58606f32e7eSjoerg   do { if (!(C)) { DebugInfoCheckFailed(__VA_ARGS__); return; } } while (false)
58706f32e7eSjoerg 
visit(Instruction & I)58806f32e7eSjoerg void Verifier::visit(Instruction &I) {
58906f32e7eSjoerg   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
59006f32e7eSjoerg     Assert(I.getOperand(i) != nullptr, "Operand is null", &I);
59106f32e7eSjoerg   InstVisitor<Verifier>::visit(I);
59206f32e7eSjoerg }
59306f32e7eSjoerg 
59406f32e7eSjoerg // Helper to recursively iterate over indirect users. By
59506f32e7eSjoerg // returning false, the callback can ask to stop recursing
59606f32e7eSjoerg // further.
forEachUser(const Value * User,SmallPtrSet<const Value *,32> & Visited,llvm::function_ref<bool (const Value *)> Callback)59706f32e7eSjoerg static void forEachUser(const Value *User,
59806f32e7eSjoerg                         SmallPtrSet<const Value *, 32> &Visited,
59906f32e7eSjoerg                         llvm::function_ref<bool(const Value *)> Callback) {
60006f32e7eSjoerg   if (!Visited.insert(User).second)
60106f32e7eSjoerg     return;
60206f32e7eSjoerg   for (const Value *TheNextUser : User->materialized_users())
60306f32e7eSjoerg     if (Callback(TheNextUser))
60406f32e7eSjoerg       forEachUser(TheNextUser, Visited, Callback);
60506f32e7eSjoerg }
60606f32e7eSjoerg 
visitGlobalValue(const GlobalValue & GV)60706f32e7eSjoerg void Verifier::visitGlobalValue(const GlobalValue &GV) {
60806f32e7eSjoerg   Assert(!GV.isDeclaration() || GV.hasValidDeclarationLinkage(),
60906f32e7eSjoerg          "Global is external, but doesn't have external or weak linkage!", &GV);
61006f32e7eSjoerg 
611*da58b97aSjoerg   if (const GlobalObject *GO = dyn_cast<GlobalObject>(&GV))
612*da58b97aSjoerg     Assert(GO->getAlignment() <= Value::MaximumAlignment,
613*da58b97aSjoerg            "huge alignment values are unsupported", GO);
61406f32e7eSjoerg   Assert(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
61506f32e7eSjoerg          "Only global variables can have appending linkage!", &GV);
61606f32e7eSjoerg 
61706f32e7eSjoerg   if (GV.hasAppendingLinkage()) {
61806f32e7eSjoerg     const GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV);
61906f32e7eSjoerg     Assert(GVar && GVar->getValueType()->isArrayTy(),
62006f32e7eSjoerg            "Only global arrays can have appending linkage!", GVar);
62106f32e7eSjoerg   }
62206f32e7eSjoerg 
62306f32e7eSjoerg   if (GV.isDeclarationForLinker())
62406f32e7eSjoerg     Assert(!GV.hasComdat(), "Declaration may not be in a Comdat!", &GV);
62506f32e7eSjoerg 
62606f32e7eSjoerg   if (GV.hasDLLImportStorageClass()) {
62706f32e7eSjoerg     Assert(!GV.isDSOLocal(),
62806f32e7eSjoerg            "GlobalValue with DLLImport Storage is dso_local!", &GV);
62906f32e7eSjoerg 
630*da58b97aSjoerg     Assert((GV.isDeclaration() &&
631*da58b97aSjoerg             (GV.hasExternalLinkage() || GV.hasExternalWeakLinkage())) ||
63206f32e7eSjoerg                GV.hasAvailableExternallyLinkage(),
63306f32e7eSjoerg            "Global is marked as dllimport, but not external", &GV);
63406f32e7eSjoerg   }
63506f32e7eSjoerg 
636*da58b97aSjoerg   if (GV.isImplicitDSOLocal())
63706f32e7eSjoerg     Assert(GV.isDSOLocal(),
638*da58b97aSjoerg            "GlobalValue with local linkage or non-default "
639*da58b97aSjoerg            "visibility must be dso_local!",
64006f32e7eSjoerg            &GV);
64106f32e7eSjoerg 
64206f32e7eSjoerg   forEachUser(&GV, GlobalValueVisited, [&](const Value *V) -> bool {
64306f32e7eSjoerg     if (const Instruction *I = dyn_cast<Instruction>(V)) {
64406f32e7eSjoerg       if (!I->getParent() || !I->getParent()->getParent())
64506f32e7eSjoerg         CheckFailed("Global is referenced by parentless instruction!", &GV, &M,
64606f32e7eSjoerg                     I);
64706f32e7eSjoerg       else if (I->getParent()->getParent()->getParent() != &M)
64806f32e7eSjoerg         CheckFailed("Global is referenced in a different module!", &GV, &M, I,
64906f32e7eSjoerg                     I->getParent()->getParent(),
65006f32e7eSjoerg                     I->getParent()->getParent()->getParent());
65106f32e7eSjoerg       return false;
65206f32e7eSjoerg     } else if (const Function *F = dyn_cast<Function>(V)) {
65306f32e7eSjoerg       if (F->getParent() != &M)
65406f32e7eSjoerg         CheckFailed("Global is used by function in a different module", &GV, &M,
65506f32e7eSjoerg                     F, F->getParent());
65606f32e7eSjoerg       return false;
65706f32e7eSjoerg     }
65806f32e7eSjoerg     return true;
65906f32e7eSjoerg   });
66006f32e7eSjoerg }
66106f32e7eSjoerg 
visitGlobalVariable(const GlobalVariable & GV)66206f32e7eSjoerg void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
66306f32e7eSjoerg   if (GV.hasInitializer()) {
66406f32e7eSjoerg     Assert(GV.getInitializer()->getType() == GV.getValueType(),
66506f32e7eSjoerg            "Global variable initializer type does not match global "
66606f32e7eSjoerg            "variable type!",
66706f32e7eSjoerg            &GV);
66806f32e7eSjoerg     // If the global has common linkage, it must have a zero initializer and
66906f32e7eSjoerg     // cannot be constant.
67006f32e7eSjoerg     if (GV.hasCommonLinkage()) {
67106f32e7eSjoerg       Assert(GV.getInitializer()->isNullValue(),
67206f32e7eSjoerg              "'common' global must have a zero initializer!", &GV);
67306f32e7eSjoerg       Assert(!GV.isConstant(), "'common' global may not be marked constant!",
67406f32e7eSjoerg              &GV);
67506f32e7eSjoerg       Assert(!GV.hasComdat(), "'common' global may not be in a Comdat!", &GV);
67606f32e7eSjoerg     }
67706f32e7eSjoerg   }
67806f32e7eSjoerg 
67906f32e7eSjoerg   if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
68006f32e7eSjoerg                        GV.getName() == "llvm.global_dtors")) {
68106f32e7eSjoerg     Assert(!GV.hasInitializer() || GV.hasAppendingLinkage(),
68206f32e7eSjoerg            "invalid linkage for intrinsic global variable", &GV);
68306f32e7eSjoerg     // Don't worry about emitting an error for it not being an array,
68406f32e7eSjoerg     // visitGlobalValue will complain on appending non-array.
68506f32e7eSjoerg     if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getValueType())) {
68606f32e7eSjoerg       StructType *STy = dyn_cast<StructType>(ATy->getElementType());
68706f32e7eSjoerg       PointerType *FuncPtrTy =
68806f32e7eSjoerg           FunctionType::get(Type::getVoidTy(Context), false)->
68906f32e7eSjoerg           getPointerTo(DL.getProgramAddressSpace());
69006f32e7eSjoerg       Assert(STy &&
69106f32e7eSjoerg                  (STy->getNumElements() == 2 || STy->getNumElements() == 3) &&
69206f32e7eSjoerg                  STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
69306f32e7eSjoerg                  STy->getTypeAtIndex(1) == FuncPtrTy,
69406f32e7eSjoerg              "wrong type for intrinsic global variable", &GV);
69506f32e7eSjoerg       Assert(STy->getNumElements() == 3,
69606f32e7eSjoerg              "the third field of the element type is mandatory, "
69706f32e7eSjoerg              "specify i8* null to migrate from the obsoleted 2-field form");
69806f32e7eSjoerg       Type *ETy = STy->getTypeAtIndex(2);
69906f32e7eSjoerg       Assert(ETy->isPointerTy() &&
70006f32e7eSjoerg                  cast<PointerType>(ETy)->getElementType()->isIntegerTy(8),
70106f32e7eSjoerg              "wrong type for intrinsic global variable", &GV);
70206f32e7eSjoerg     }
70306f32e7eSjoerg   }
70406f32e7eSjoerg 
70506f32e7eSjoerg   if (GV.hasName() && (GV.getName() == "llvm.used" ||
70606f32e7eSjoerg                        GV.getName() == "llvm.compiler.used")) {
70706f32e7eSjoerg     Assert(!GV.hasInitializer() || GV.hasAppendingLinkage(),
70806f32e7eSjoerg            "invalid linkage for intrinsic global variable", &GV);
70906f32e7eSjoerg     Type *GVType = GV.getValueType();
71006f32e7eSjoerg     if (ArrayType *ATy = dyn_cast<ArrayType>(GVType)) {
71106f32e7eSjoerg       PointerType *PTy = dyn_cast<PointerType>(ATy->getElementType());
71206f32e7eSjoerg       Assert(PTy, "wrong type for intrinsic global variable", &GV);
71306f32e7eSjoerg       if (GV.hasInitializer()) {
71406f32e7eSjoerg         const Constant *Init = GV.getInitializer();
71506f32e7eSjoerg         const ConstantArray *InitArray = dyn_cast<ConstantArray>(Init);
71606f32e7eSjoerg         Assert(InitArray, "wrong initalizer for intrinsic global variable",
71706f32e7eSjoerg                Init);
71806f32e7eSjoerg         for (Value *Op : InitArray->operands()) {
71906f32e7eSjoerg           Value *V = Op->stripPointerCasts();
72006f32e7eSjoerg           Assert(isa<GlobalVariable>(V) || isa<Function>(V) ||
72106f32e7eSjoerg                      isa<GlobalAlias>(V),
72206f32e7eSjoerg                  "invalid llvm.used member", V);
72306f32e7eSjoerg           Assert(V->hasName(), "members of llvm.used must be named", V);
72406f32e7eSjoerg         }
72506f32e7eSjoerg       }
72606f32e7eSjoerg     }
72706f32e7eSjoerg   }
72806f32e7eSjoerg 
72906f32e7eSjoerg   // Visit any debug info attachments.
73006f32e7eSjoerg   SmallVector<MDNode *, 1> MDs;
73106f32e7eSjoerg   GV.getMetadata(LLVMContext::MD_dbg, MDs);
73206f32e7eSjoerg   for (auto *MD : MDs) {
73306f32e7eSjoerg     if (auto *GVE = dyn_cast<DIGlobalVariableExpression>(MD))
73406f32e7eSjoerg       visitDIGlobalVariableExpression(*GVE);
73506f32e7eSjoerg     else
73606f32e7eSjoerg       AssertDI(false, "!dbg attachment of global variable must be a "
73706f32e7eSjoerg                       "DIGlobalVariableExpression");
73806f32e7eSjoerg   }
73906f32e7eSjoerg 
74006f32e7eSjoerg   // Scalable vectors cannot be global variables, since we don't know
741*da58b97aSjoerg   // the runtime size. If the global is an array containing scalable vectors,
742*da58b97aSjoerg   // that will be caught by the isValidElementType methods in StructType or
743*da58b97aSjoerg   // ArrayType instead.
744*da58b97aSjoerg   Assert(!isa<ScalableVectorType>(GV.getValueType()),
745*da58b97aSjoerg          "Globals cannot contain scalable vectors", &GV);
746*da58b97aSjoerg 
747*da58b97aSjoerg   if (auto *STy = dyn_cast<StructType>(GV.getValueType()))
748*da58b97aSjoerg     Assert(!STy->containsScalableVectorType(),
749*da58b97aSjoerg            "Globals cannot contain scalable vectors", &GV);
75006f32e7eSjoerg 
75106f32e7eSjoerg   if (!GV.hasInitializer()) {
75206f32e7eSjoerg     visitGlobalValue(GV);
75306f32e7eSjoerg     return;
75406f32e7eSjoerg   }
75506f32e7eSjoerg 
75606f32e7eSjoerg   // Walk any aggregate initializers looking for bitcasts between address spaces
75706f32e7eSjoerg   visitConstantExprsRecursively(GV.getInitializer());
75806f32e7eSjoerg 
75906f32e7eSjoerg   visitGlobalValue(GV);
76006f32e7eSjoerg }
76106f32e7eSjoerg 
visitAliaseeSubExpr(const GlobalAlias & GA,const Constant & C)76206f32e7eSjoerg void Verifier::visitAliaseeSubExpr(const GlobalAlias &GA, const Constant &C) {
76306f32e7eSjoerg   SmallPtrSet<const GlobalAlias*, 4> Visited;
76406f32e7eSjoerg   Visited.insert(&GA);
76506f32e7eSjoerg   visitAliaseeSubExpr(Visited, GA, C);
76606f32e7eSjoerg }
76706f32e7eSjoerg 
visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias * > & Visited,const GlobalAlias & GA,const Constant & C)76806f32e7eSjoerg void Verifier::visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias*> &Visited,
76906f32e7eSjoerg                                    const GlobalAlias &GA, const Constant &C) {
77006f32e7eSjoerg   if (const auto *GV = dyn_cast<GlobalValue>(&C)) {
77106f32e7eSjoerg     Assert(!GV->isDeclarationForLinker(), "Alias must point to a definition",
77206f32e7eSjoerg            &GA);
77306f32e7eSjoerg 
77406f32e7eSjoerg     if (const auto *GA2 = dyn_cast<GlobalAlias>(GV)) {
77506f32e7eSjoerg       Assert(Visited.insert(GA2).second, "Aliases cannot form a cycle", &GA);
77606f32e7eSjoerg 
77706f32e7eSjoerg       Assert(!GA2->isInterposable(), "Alias cannot point to an interposable alias",
77806f32e7eSjoerg              &GA);
77906f32e7eSjoerg     } else {
78006f32e7eSjoerg       // Only continue verifying subexpressions of GlobalAliases.
78106f32e7eSjoerg       // Do not recurse into global initializers.
78206f32e7eSjoerg       return;
78306f32e7eSjoerg     }
78406f32e7eSjoerg   }
78506f32e7eSjoerg 
78606f32e7eSjoerg   if (const auto *CE = dyn_cast<ConstantExpr>(&C))
78706f32e7eSjoerg     visitConstantExprsRecursively(CE);
78806f32e7eSjoerg 
78906f32e7eSjoerg   for (const Use &U : C.operands()) {
79006f32e7eSjoerg     Value *V = &*U;
79106f32e7eSjoerg     if (const auto *GA2 = dyn_cast<GlobalAlias>(V))
79206f32e7eSjoerg       visitAliaseeSubExpr(Visited, GA, *GA2->getAliasee());
79306f32e7eSjoerg     else if (const auto *C2 = dyn_cast<Constant>(V))
79406f32e7eSjoerg       visitAliaseeSubExpr(Visited, GA, *C2);
79506f32e7eSjoerg   }
79606f32e7eSjoerg }
79706f32e7eSjoerg 
visitGlobalAlias(const GlobalAlias & GA)79806f32e7eSjoerg void Verifier::visitGlobalAlias(const GlobalAlias &GA) {
79906f32e7eSjoerg   Assert(GlobalAlias::isValidLinkage(GA.getLinkage()),
80006f32e7eSjoerg          "Alias should have private, internal, linkonce, weak, linkonce_odr, "
80106f32e7eSjoerg          "weak_odr, or external linkage!",
80206f32e7eSjoerg          &GA);
80306f32e7eSjoerg   const Constant *Aliasee = GA.getAliasee();
80406f32e7eSjoerg   Assert(Aliasee, "Aliasee cannot be NULL!", &GA);
80506f32e7eSjoerg   Assert(GA.getType() == Aliasee->getType(),
80606f32e7eSjoerg          "Alias and aliasee types should match!", &GA);
80706f32e7eSjoerg 
80806f32e7eSjoerg   Assert(isa<GlobalValue>(Aliasee) || isa<ConstantExpr>(Aliasee),
80906f32e7eSjoerg          "Aliasee should be either GlobalValue or ConstantExpr", &GA);
81006f32e7eSjoerg 
81106f32e7eSjoerg   visitAliaseeSubExpr(GA, *Aliasee);
81206f32e7eSjoerg 
81306f32e7eSjoerg   visitGlobalValue(GA);
81406f32e7eSjoerg }
81506f32e7eSjoerg 
visitNamedMDNode(const NamedMDNode & NMD)81606f32e7eSjoerg void Verifier::visitNamedMDNode(const NamedMDNode &NMD) {
81706f32e7eSjoerg   // There used to be various other llvm.dbg.* nodes, but we don't support
81806f32e7eSjoerg   // upgrading them and we want to reserve the namespace for future uses.
81906f32e7eSjoerg   if (NMD.getName().startswith("llvm.dbg."))
82006f32e7eSjoerg     AssertDI(NMD.getName() == "llvm.dbg.cu",
82106f32e7eSjoerg              "unrecognized named metadata node in the llvm.dbg namespace",
82206f32e7eSjoerg              &NMD);
82306f32e7eSjoerg   for (const MDNode *MD : NMD.operands()) {
82406f32e7eSjoerg     if (NMD.getName() == "llvm.dbg.cu")
82506f32e7eSjoerg       AssertDI(MD && isa<DICompileUnit>(MD), "invalid compile unit", &NMD, MD);
82606f32e7eSjoerg 
82706f32e7eSjoerg     if (!MD)
82806f32e7eSjoerg       continue;
82906f32e7eSjoerg 
830*da58b97aSjoerg     visitMDNode(*MD, AreDebugLocsAllowed::Yes);
83106f32e7eSjoerg   }
83206f32e7eSjoerg }
83306f32e7eSjoerg 
visitMDNode(const MDNode & MD,AreDebugLocsAllowed AllowLocs)834*da58b97aSjoerg void Verifier::visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs) {
83506f32e7eSjoerg   // Only visit each node once.  Metadata can be mutually recursive, so this
83606f32e7eSjoerg   // avoids infinite recursion here, as well as being an optimization.
83706f32e7eSjoerg   if (!MDNodes.insert(&MD).second)
83806f32e7eSjoerg     return;
83906f32e7eSjoerg 
840*da58b97aSjoerg   Assert(&MD.getContext() == &Context,
841*da58b97aSjoerg          "MDNode context does not match Module context!", &MD);
842*da58b97aSjoerg 
84306f32e7eSjoerg   switch (MD.getMetadataID()) {
84406f32e7eSjoerg   default:
84506f32e7eSjoerg     llvm_unreachable("Invalid MDNode subclass");
84606f32e7eSjoerg   case Metadata::MDTupleKind:
84706f32e7eSjoerg     break;
84806f32e7eSjoerg #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS)                                  \
84906f32e7eSjoerg   case Metadata::CLASS##Kind:                                                  \
85006f32e7eSjoerg     visit##CLASS(cast<CLASS>(MD));                                             \
85106f32e7eSjoerg     break;
85206f32e7eSjoerg #include "llvm/IR/Metadata.def"
85306f32e7eSjoerg   }
85406f32e7eSjoerg 
85506f32e7eSjoerg   for (const Metadata *Op : MD.operands()) {
85606f32e7eSjoerg     if (!Op)
85706f32e7eSjoerg       continue;
85806f32e7eSjoerg     Assert(!isa<LocalAsMetadata>(Op), "Invalid operand for global metadata!",
85906f32e7eSjoerg            &MD, Op);
860*da58b97aSjoerg     AssertDI(!isa<DILocation>(Op) || AllowLocs == AreDebugLocsAllowed::Yes,
861*da58b97aSjoerg              "DILocation not allowed within this metadata node", &MD, Op);
86206f32e7eSjoerg     if (auto *N = dyn_cast<MDNode>(Op)) {
863*da58b97aSjoerg       visitMDNode(*N, AllowLocs);
86406f32e7eSjoerg       continue;
86506f32e7eSjoerg     }
86606f32e7eSjoerg     if (auto *V = dyn_cast<ValueAsMetadata>(Op)) {
86706f32e7eSjoerg       visitValueAsMetadata(*V, nullptr);
86806f32e7eSjoerg       continue;
86906f32e7eSjoerg     }
87006f32e7eSjoerg   }
87106f32e7eSjoerg 
87206f32e7eSjoerg   // Check these last, so we diagnose problems in operands first.
87306f32e7eSjoerg   Assert(!MD.isTemporary(), "Expected no forward declarations!", &MD);
87406f32e7eSjoerg   Assert(MD.isResolved(), "All nodes should be resolved!", &MD);
87506f32e7eSjoerg }
87606f32e7eSjoerg 
visitValueAsMetadata(const ValueAsMetadata & MD,Function * F)87706f32e7eSjoerg void Verifier::visitValueAsMetadata(const ValueAsMetadata &MD, Function *F) {
87806f32e7eSjoerg   Assert(MD.getValue(), "Expected valid value", &MD);
87906f32e7eSjoerg   Assert(!MD.getValue()->getType()->isMetadataTy(),
88006f32e7eSjoerg          "Unexpected metadata round-trip through values", &MD, MD.getValue());
88106f32e7eSjoerg 
88206f32e7eSjoerg   auto *L = dyn_cast<LocalAsMetadata>(&MD);
88306f32e7eSjoerg   if (!L)
88406f32e7eSjoerg     return;
88506f32e7eSjoerg 
88606f32e7eSjoerg   Assert(F, "function-local metadata used outside a function", L);
88706f32e7eSjoerg 
88806f32e7eSjoerg   // If this was an instruction, bb, or argument, verify that it is in the
88906f32e7eSjoerg   // function that we expect.
89006f32e7eSjoerg   Function *ActualF = nullptr;
89106f32e7eSjoerg   if (Instruction *I = dyn_cast<Instruction>(L->getValue())) {
89206f32e7eSjoerg     Assert(I->getParent(), "function-local metadata not in basic block", L, I);
89306f32e7eSjoerg     ActualF = I->getParent()->getParent();
89406f32e7eSjoerg   } else if (BasicBlock *BB = dyn_cast<BasicBlock>(L->getValue()))
89506f32e7eSjoerg     ActualF = BB->getParent();
89606f32e7eSjoerg   else if (Argument *A = dyn_cast<Argument>(L->getValue()))
89706f32e7eSjoerg     ActualF = A->getParent();
89806f32e7eSjoerg   assert(ActualF && "Unimplemented function local metadata case!");
89906f32e7eSjoerg 
90006f32e7eSjoerg   Assert(ActualF == F, "function-local metadata used in wrong function", L);
90106f32e7eSjoerg }
90206f32e7eSjoerg 
visitMetadataAsValue(const MetadataAsValue & MDV,Function * F)90306f32e7eSjoerg void Verifier::visitMetadataAsValue(const MetadataAsValue &MDV, Function *F) {
90406f32e7eSjoerg   Metadata *MD = MDV.getMetadata();
90506f32e7eSjoerg   if (auto *N = dyn_cast<MDNode>(MD)) {
906*da58b97aSjoerg     visitMDNode(*N, AreDebugLocsAllowed::No);
90706f32e7eSjoerg     return;
90806f32e7eSjoerg   }
90906f32e7eSjoerg 
91006f32e7eSjoerg   // Only visit each node once.  Metadata can be mutually recursive, so this
91106f32e7eSjoerg   // avoids infinite recursion here, as well as being an optimization.
91206f32e7eSjoerg   if (!MDNodes.insert(MD).second)
91306f32e7eSjoerg     return;
91406f32e7eSjoerg 
91506f32e7eSjoerg   if (auto *V = dyn_cast<ValueAsMetadata>(MD))
91606f32e7eSjoerg     visitValueAsMetadata(*V, F);
91706f32e7eSjoerg }
91806f32e7eSjoerg 
isType(const Metadata * MD)91906f32e7eSjoerg static bool isType(const Metadata *MD) { return !MD || isa<DIType>(MD); }
isScope(const Metadata * MD)92006f32e7eSjoerg static bool isScope(const Metadata *MD) { return !MD || isa<DIScope>(MD); }
isDINode(const Metadata * MD)92106f32e7eSjoerg static bool isDINode(const Metadata *MD) { return !MD || isa<DINode>(MD); }
92206f32e7eSjoerg 
visitDILocation(const DILocation & N)92306f32e7eSjoerg void Verifier::visitDILocation(const DILocation &N) {
92406f32e7eSjoerg   AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
92506f32e7eSjoerg            "location requires a valid scope", &N, N.getRawScope());
92606f32e7eSjoerg   if (auto *IA = N.getRawInlinedAt())
92706f32e7eSjoerg     AssertDI(isa<DILocation>(IA), "inlined-at should be a location", &N, IA);
92806f32e7eSjoerg   if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
92906f32e7eSjoerg     AssertDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
93006f32e7eSjoerg }
93106f32e7eSjoerg 
visitGenericDINode(const GenericDINode & N)93206f32e7eSjoerg void Verifier::visitGenericDINode(const GenericDINode &N) {
93306f32e7eSjoerg   AssertDI(N.getTag(), "invalid tag", &N);
93406f32e7eSjoerg }
93506f32e7eSjoerg 
visitDIScope(const DIScope & N)93606f32e7eSjoerg void Verifier::visitDIScope(const DIScope &N) {
93706f32e7eSjoerg   if (auto *F = N.getRawFile())
93806f32e7eSjoerg     AssertDI(isa<DIFile>(F), "invalid file", &N, F);
93906f32e7eSjoerg }
94006f32e7eSjoerg 
visitDISubrange(const DISubrange & N)94106f32e7eSjoerg void Verifier::visitDISubrange(const DISubrange &N) {
94206f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_subrange_type, "invalid tag", &N);
943*da58b97aSjoerg   bool HasAssumedSizedArraySupport = dwarf::isFortran(CurrentSourceLang);
944*da58b97aSjoerg   AssertDI(HasAssumedSizedArraySupport || N.getRawCountNode() ||
945*da58b97aSjoerg                N.getRawUpperBound(),
946*da58b97aSjoerg            "Subrange must contain count or upperBound", &N);
947*da58b97aSjoerg   AssertDI(!N.getRawCountNode() || !N.getRawUpperBound(),
948*da58b97aSjoerg            "Subrange can have any one of count or upperBound", &N);
949*da58b97aSjoerg   auto *CBound = N.getRawCountNode();
950*da58b97aSjoerg   AssertDI(!CBound || isa<ConstantAsMetadata>(CBound) ||
951*da58b97aSjoerg                isa<DIVariable>(CBound) || isa<DIExpression>(CBound),
952*da58b97aSjoerg            "Count must be signed constant or DIVariable or DIExpression", &N);
95306f32e7eSjoerg   auto Count = N.getCount();
954*da58b97aSjoerg   AssertDI(!Count || !Count.is<ConstantInt *>() ||
95506f32e7eSjoerg                Count.get<ConstantInt *>()->getSExtValue() >= -1,
95606f32e7eSjoerg            "invalid subrange count", &N);
957*da58b97aSjoerg   auto *LBound = N.getRawLowerBound();
958*da58b97aSjoerg   AssertDI(!LBound || isa<ConstantAsMetadata>(LBound) ||
959*da58b97aSjoerg                isa<DIVariable>(LBound) || isa<DIExpression>(LBound),
960*da58b97aSjoerg            "LowerBound must be signed constant or DIVariable or DIExpression",
961*da58b97aSjoerg            &N);
962*da58b97aSjoerg   auto *UBound = N.getRawUpperBound();
963*da58b97aSjoerg   AssertDI(!UBound || isa<ConstantAsMetadata>(UBound) ||
964*da58b97aSjoerg                isa<DIVariable>(UBound) || isa<DIExpression>(UBound),
965*da58b97aSjoerg            "UpperBound must be signed constant or DIVariable or DIExpression",
966*da58b97aSjoerg            &N);
967*da58b97aSjoerg   auto *Stride = N.getRawStride();
968*da58b97aSjoerg   AssertDI(!Stride || isa<ConstantAsMetadata>(Stride) ||
969*da58b97aSjoerg                isa<DIVariable>(Stride) || isa<DIExpression>(Stride),
970*da58b97aSjoerg            "Stride must be signed constant or DIVariable or DIExpression", &N);
971*da58b97aSjoerg }
972*da58b97aSjoerg 
visitDIGenericSubrange(const DIGenericSubrange & N)973*da58b97aSjoerg void Verifier::visitDIGenericSubrange(const DIGenericSubrange &N) {
974*da58b97aSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_generic_subrange, "invalid tag", &N);
975*da58b97aSjoerg   AssertDI(N.getRawCountNode() || N.getRawUpperBound(),
976*da58b97aSjoerg            "GenericSubrange must contain count or upperBound", &N);
977*da58b97aSjoerg   AssertDI(!N.getRawCountNode() || !N.getRawUpperBound(),
978*da58b97aSjoerg            "GenericSubrange can have any one of count or upperBound", &N);
979*da58b97aSjoerg   auto *CBound = N.getRawCountNode();
980*da58b97aSjoerg   AssertDI(!CBound || isa<DIVariable>(CBound) || isa<DIExpression>(CBound),
981*da58b97aSjoerg            "Count must be signed constant or DIVariable or DIExpression", &N);
982*da58b97aSjoerg   auto *LBound = N.getRawLowerBound();
983*da58b97aSjoerg   AssertDI(LBound, "GenericSubrange must contain lowerBound", &N);
984*da58b97aSjoerg   AssertDI(isa<DIVariable>(LBound) || isa<DIExpression>(LBound),
985*da58b97aSjoerg            "LowerBound must be signed constant or DIVariable or DIExpression",
986*da58b97aSjoerg            &N);
987*da58b97aSjoerg   auto *UBound = N.getRawUpperBound();
988*da58b97aSjoerg   AssertDI(!UBound || isa<DIVariable>(UBound) || isa<DIExpression>(UBound),
989*da58b97aSjoerg            "UpperBound must be signed constant or DIVariable or DIExpression",
990*da58b97aSjoerg            &N);
991*da58b97aSjoerg   auto *Stride = N.getRawStride();
992*da58b97aSjoerg   AssertDI(Stride, "GenericSubrange must contain stride", &N);
993*da58b97aSjoerg   AssertDI(isa<DIVariable>(Stride) || isa<DIExpression>(Stride),
994*da58b97aSjoerg            "Stride must be signed constant or DIVariable or DIExpression", &N);
99506f32e7eSjoerg }
99606f32e7eSjoerg 
visitDIEnumerator(const DIEnumerator & N)99706f32e7eSjoerg void Verifier::visitDIEnumerator(const DIEnumerator &N) {
99806f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_enumerator, "invalid tag", &N);
99906f32e7eSjoerg }
100006f32e7eSjoerg 
visitDIBasicType(const DIBasicType & N)100106f32e7eSjoerg void Verifier::visitDIBasicType(const DIBasicType &N) {
100206f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_base_type ||
1003*da58b97aSjoerg                N.getTag() == dwarf::DW_TAG_unspecified_type ||
1004*da58b97aSjoerg                N.getTag() == dwarf::DW_TAG_string_type,
100506f32e7eSjoerg            "invalid tag", &N);
1006*da58b97aSjoerg }
1007*da58b97aSjoerg 
visitDIStringType(const DIStringType & N)1008*da58b97aSjoerg void Verifier::visitDIStringType(const DIStringType &N) {
1009*da58b97aSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_string_type, "invalid tag", &N);
101006f32e7eSjoerg   AssertDI(!(N.isBigEndian() && N.isLittleEndian()) ,
101106f32e7eSjoerg             "has conflicting flags", &N);
101206f32e7eSjoerg }
101306f32e7eSjoerg 
visitDIDerivedType(const DIDerivedType & N)101406f32e7eSjoerg void Verifier::visitDIDerivedType(const DIDerivedType &N) {
101506f32e7eSjoerg   // Common scope checks.
101606f32e7eSjoerg   visitDIScope(N);
101706f32e7eSjoerg 
101806f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_typedef ||
101906f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_pointer_type ||
102006f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_ptr_to_member_type ||
102106f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_reference_type ||
102206f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_rvalue_reference_type ||
102306f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_const_type ||
102406f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_volatile_type ||
102506f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_restrict_type ||
102606f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_atomic_type ||
102706f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_member ||
102806f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_inheritance ||
1029*da58b97aSjoerg                N.getTag() == dwarf::DW_TAG_friend ||
1030*da58b97aSjoerg                N.getTag() == dwarf::DW_TAG_set_type,
103106f32e7eSjoerg            "invalid tag", &N);
103206f32e7eSjoerg   if (N.getTag() == dwarf::DW_TAG_ptr_to_member_type) {
103306f32e7eSjoerg     AssertDI(isType(N.getRawExtraData()), "invalid pointer to member type", &N,
103406f32e7eSjoerg              N.getRawExtraData());
103506f32e7eSjoerg   }
103606f32e7eSjoerg 
1037*da58b97aSjoerg   if (N.getTag() == dwarf::DW_TAG_set_type) {
1038*da58b97aSjoerg     if (auto *T = N.getRawBaseType()) {
1039*da58b97aSjoerg       auto *Enum = dyn_cast_or_null<DICompositeType>(T);
1040*da58b97aSjoerg       auto *Basic = dyn_cast_or_null<DIBasicType>(T);
1041*da58b97aSjoerg       AssertDI(
1042*da58b97aSjoerg           (Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type) ||
1043*da58b97aSjoerg               (Basic && (Basic->getEncoding() == dwarf::DW_ATE_unsigned ||
1044*da58b97aSjoerg                          Basic->getEncoding() == dwarf::DW_ATE_signed ||
1045*da58b97aSjoerg                          Basic->getEncoding() == dwarf::DW_ATE_unsigned_char ||
1046*da58b97aSjoerg                          Basic->getEncoding() == dwarf::DW_ATE_signed_char ||
1047*da58b97aSjoerg                          Basic->getEncoding() == dwarf::DW_ATE_boolean)),
1048*da58b97aSjoerg           "invalid set base type", &N, T);
1049*da58b97aSjoerg     }
1050*da58b97aSjoerg   }
1051*da58b97aSjoerg 
105206f32e7eSjoerg   AssertDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
105306f32e7eSjoerg   AssertDI(isType(N.getRawBaseType()), "invalid base type", &N,
105406f32e7eSjoerg            N.getRawBaseType());
105506f32e7eSjoerg 
105606f32e7eSjoerg   if (N.getDWARFAddressSpace()) {
105706f32e7eSjoerg     AssertDI(N.getTag() == dwarf::DW_TAG_pointer_type ||
105806f32e7eSjoerg                  N.getTag() == dwarf::DW_TAG_reference_type ||
105906f32e7eSjoerg                  N.getTag() == dwarf::DW_TAG_rvalue_reference_type,
106006f32e7eSjoerg              "DWARF address space only applies to pointer or reference types",
106106f32e7eSjoerg              &N);
106206f32e7eSjoerg   }
106306f32e7eSjoerg }
106406f32e7eSjoerg 
106506f32e7eSjoerg /// Detect mutually exclusive flags.
hasConflictingReferenceFlags(unsigned Flags)106606f32e7eSjoerg static bool hasConflictingReferenceFlags(unsigned Flags) {
106706f32e7eSjoerg   return ((Flags & DINode::FlagLValueReference) &&
106806f32e7eSjoerg           (Flags & DINode::FlagRValueReference)) ||
106906f32e7eSjoerg          ((Flags & DINode::FlagTypePassByValue) &&
107006f32e7eSjoerg           (Flags & DINode::FlagTypePassByReference));
107106f32e7eSjoerg }
107206f32e7eSjoerg 
visitTemplateParams(const MDNode & N,const Metadata & RawParams)107306f32e7eSjoerg void Verifier::visitTemplateParams(const MDNode &N, const Metadata &RawParams) {
107406f32e7eSjoerg   auto *Params = dyn_cast<MDTuple>(&RawParams);
107506f32e7eSjoerg   AssertDI(Params, "invalid template params", &N, &RawParams);
107606f32e7eSjoerg   for (Metadata *Op : Params->operands()) {
107706f32e7eSjoerg     AssertDI(Op && isa<DITemplateParameter>(Op), "invalid template parameter",
107806f32e7eSjoerg              &N, Params, Op);
107906f32e7eSjoerg   }
108006f32e7eSjoerg }
108106f32e7eSjoerg 
visitDICompositeType(const DICompositeType & N)108206f32e7eSjoerg void Verifier::visitDICompositeType(const DICompositeType &N) {
108306f32e7eSjoerg   // Common scope checks.
108406f32e7eSjoerg   visitDIScope(N);
108506f32e7eSjoerg 
108606f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_array_type ||
108706f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_structure_type ||
108806f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_union_type ||
108906f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_enumeration_type ||
109006f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_class_type ||
109106f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_variant_part,
109206f32e7eSjoerg            "invalid tag", &N);
109306f32e7eSjoerg 
109406f32e7eSjoerg   AssertDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
109506f32e7eSjoerg   AssertDI(isType(N.getRawBaseType()), "invalid base type", &N,
109606f32e7eSjoerg            N.getRawBaseType());
109706f32e7eSjoerg 
109806f32e7eSjoerg   AssertDI(!N.getRawElements() || isa<MDTuple>(N.getRawElements()),
109906f32e7eSjoerg            "invalid composite elements", &N, N.getRawElements());
110006f32e7eSjoerg   AssertDI(isType(N.getRawVTableHolder()), "invalid vtable holder", &N,
110106f32e7eSjoerg            N.getRawVTableHolder());
110206f32e7eSjoerg   AssertDI(!hasConflictingReferenceFlags(N.getFlags()),
110306f32e7eSjoerg            "invalid reference flags", &N);
110406f32e7eSjoerg   unsigned DIBlockByRefStruct = 1 << 4;
110506f32e7eSjoerg   AssertDI((N.getFlags() & DIBlockByRefStruct) == 0,
110606f32e7eSjoerg            "DIBlockByRefStruct on DICompositeType is no longer supported", &N);
110706f32e7eSjoerg 
110806f32e7eSjoerg   if (N.isVector()) {
110906f32e7eSjoerg     const DINodeArray Elements = N.getElements();
111006f32e7eSjoerg     AssertDI(Elements.size() == 1 &&
111106f32e7eSjoerg              Elements[0]->getTag() == dwarf::DW_TAG_subrange_type,
111206f32e7eSjoerg              "invalid vector, expected one element of type subrange", &N);
111306f32e7eSjoerg   }
111406f32e7eSjoerg 
111506f32e7eSjoerg   if (auto *Params = N.getRawTemplateParams())
111606f32e7eSjoerg     visitTemplateParams(N, *Params);
111706f32e7eSjoerg 
111806f32e7eSjoerg   if (auto *D = N.getRawDiscriminator()) {
111906f32e7eSjoerg     AssertDI(isa<DIDerivedType>(D) && N.getTag() == dwarf::DW_TAG_variant_part,
112006f32e7eSjoerg              "discriminator can only appear on variant part");
112106f32e7eSjoerg   }
1122*da58b97aSjoerg 
1123*da58b97aSjoerg   if (N.getRawDataLocation()) {
1124*da58b97aSjoerg     AssertDI(N.getTag() == dwarf::DW_TAG_array_type,
1125*da58b97aSjoerg              "dataLocation can only appear in array type");
1126*da58b97aSjoerg   }
1127*da58b97aSjoerg 
1128*da58b97aSjoerg   if (N.getRawAssociated()) {
1129*da58b97aSjoerg     AssertDI(N.getTag() == dwarf::DW_TAG_array_type,
1130*da58b97aSjoerg              "associated can only appear in array type");
1131*da58b97aSjoerg   }
1132*da58b97aSjoerg 
1133*da58b97aSjoerg   if (N.getRawAllocated()) {
1134*da58b97aSjoerg     AssertDI(N.getTag() == dwarf::DW_TAG_array_type,
1135*da58b97aSjoerg              "allocated can only appear in array type");
1136*da58b97aSjoerg   }
1137*da58b97aSjoerg 
1138*da58b97aSjoerg   if (N.getRawRank()) {
1139*da58b97aSjoerg     AssertDI(N.getTag() == dwarf::DW_TAG_array_type,
1140*da58b97aSjoerg              "rank can only appear in array type");
1141*da58b97aSjoerg   }
114206f32e7eSjoerg }
114306f32e7eSjoerg 
visitDISubroutineType(const DISubroutineType & N)114406f32e7eSjoerg void Verifier::visitDISubroutineType(const DISubroutineType &N) {
114506f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_subroutine_type, "invalid tag", &N);
114606f32e7eSjoerg   if (auto *Types = N.getRawTypeArray()) {
114706f32e7eSjoerg     AssertDI(isa<MDTuple>(Types), "invalid composite elements", &N, Types);
114806f32e7eSjoerg     for (Metadata *Ty : N.getTypeArray()->operands()) {
114906f32e7eSjoerg       AssertDI(isType(Ty), "invalid subroutine type ref", &N, Types, Ty);
115006f32e7eSjoerg     }
115106f32e7eSjoerg   }
115206f32e7eSjoerg   AssertDI(!hasConflictingReferenceFlags(N.getFlags()),
115306f32e7eSjoerg            "invalid reference flags", &N);
115406f32e7eSjoerg }
115506f32e7eSjoerg 
visitDIFile(const DIFile & N)115606f32e7eSjoerg void Verifier::visitDIFile(const DIFile &N) {
115706f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_file_type, "invalid tag", &N);
115806f32e7eSjoerg   Optional<DIFile::ChecksumInfo<StringRef>> Checksum = N.getChecksum();
115906f32e7eSjoerg   if (Checksum) {
116006f32e7eSjoerg     AssertDI(Checksum->Kind <= DIFile::ChecksumKind::CSK_Last,
116106f32e7eSjoerg              "invalid checksum kind", &N);
116206f32e7eSjoerg     size_t Size;
116306f32e7eSjoerg     switch (Checksum->Kind) {
116406f32e7eSjoerg     case DIFile::CSK_MD5:
116506f32e7eSjoerg       Size = 32;
116606f32e7eSjoerg       break;
116706f32e7eSjoerg     case DIFile::CSK_SHA1:
116806f32e7eSjoerg       Size = 40;
116906f32e7eSjoerg       break;
1170*da58b97aSjoerg     case DIFile::CSK_SHA256:
1171*da58b97aSjoerg       Size = 64;
1172*da58b97aSjoerg       break;
117306f32e7eSjoerg     }
117406f32e7eSjoerg     AssertDI(Checksum->Value.size() == Size, "invalid checksum length", &N);
117506f32e7eSjoerg     AssertDI(Checksum->Value.find_if_not(llvm::isHexDigit) == StringRef::npos,
117606f32e7eSjoerg              "invalid checksum", &N);
117706f32e7eSjoerg   }
117806f32e7eSjoerg }
117906f32e7eSjoerg 
visitDICompileUnit(const DICompileUnit & N)118006f32e7eSjoerg void Verifier::visitDICompileUnit(const DICompileUnit &N) {
118106f32e7eSjoerg   AssertDI(N.isDistinct(), "compile units must be distinct", &N);
118206f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_compile_unit, "invalid tag", &N);
118306f32e7eSjoerg 
118406f32e7eSjoerg   // Don't bother verifying the compilation directory or producer string
118506f32e7eSjoerg   // as those could be empty.
118606f32e7eSjoerg   AssertDI(N.getRawFile() && isa<DIFile>(N.getRawFile()), "invalid file", &N,
118706f32e7eSjoerg            N.getRawFile());
118806f32e7eSjoerg   AssertDI(!N.getFile()->getFilename().empty(), "invalid filename", &N,
118906f32e7eSjoerg            N.getFile());
119006f32e7eSjoerg 
1191*da58b97aSjoerg   CurrentSourceLang = (dwarf::SourceLanguage)N.getSourceLanguage();
1192*da58b97aSjoerg 
119306f32e7eSjoerg   verifySourceDebugInfo(N, *N.getFile());
119406f32e7eSjoerg 
119506f32e7eSjoerg   AssertDI((N.getEmissionKind() <= DICompileUnit::LastEmissionKind),
119606f32e7eSjoerg            "invalid emission kind", &N);
119706f32e7eSjoerg 
119806f32e7eSjoerg   if (auto *Array = N.getRawEnumTypes()) {
119906f32e7eSjoerg     AssertDI(isa<MDTuple>(Array), "invalid enum list", &N, Array);
120006f32e7eSjoerg     for (Metadata *Op : N.getEnumTypes()->operands()) {
120106f32e7eSjoerg       auto *Enum = dyn_cast_or_null<DICompositeType>(Op);
120206f32e7eSjoerg       AssertDI(Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type,
120306f32e7eSjoerg                "invalid enum type", &N, N.getEnumTypes(), Op);
120406f32e7eSjoerg     }
120506f32e7eSjoerg   }
120606f32e7eSjoerg   if (auto *Array = N.getRawRetainedTypes()) {
120706f32e7eSjoerg     AssertDI(isa<MDTuple>(Array), "invalid retained type list", &N, Array);
120806f32e7eSjoerg     for (Metadata *Op : N.getRetainedTypes()->operands()) {
120906f32e7eSjoerg       AssertDI(Op && (isa<DIType>(Op) ||
121006f32e7eSjoerg                       (isa<DISubprogram>(Op) &&
121106f32e7eSjoerg                        !cast<DISubprogram>(Op)->isDefinition())),
121206f32e7eSjoerg                "invalid retained type", &N, Op);
121306f32e7eSjoerg     }
121406f32e7eSjoerg   }
121506f32e7eSjoerg   if (auto *Array = N.getRawGlobalVariables()) {
121606f32e7eSjoerg     AssertDI(isa<MDTuple>(Array), "invalid global variable list", &N, Array);
121706f32e7eSjoerg     for (Metadata *Op : N.getGlobalVariables()->operands()) {
121806f32e7eSjoerg       AssertDI(Op && (isa<DIGlobalVariableExpression>(Op)),
121906f32e7eSjoerg                "invalid global variable ref", &N, Op);
122006f32e7eSjoerg     }
122106f32e7eSjoerg   }
122206f32e7eSjoerg   if (auto *Array = N.getRawImportedEntities()) {
122306f32e7eSjoerg     AssertDI(isa<MDTuple>(Array), "invalid imported entity list", &N, Array);
122406f32e7eSjoerg     for (Metadata *Op : N.getImportedEntities()->operands()) {
122506f32e7eSjoerg       AssertDI(Op && isa<DIImportedEntity>(Op), "invalid imported entity ref",
122606f32e7eSjoerg                &N, Op);
122706f32e7eSjoerg     }
122806f32e7eSjoerg   }
122906f32e7eSjoerg   if (auto *Array = N.getRawMacros()) {
123006f32e7eSjoerg     AssertDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
123106f32e7eSjoerg     for (Metadata *Op : N.getMacros()->operands()) {
123206f32e7eSjoerg       AssertDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
123306f32e7eSjoerg     }
123406f32e7eSjoerg   }
123506f32e7eSjoerg   CUVisited.insert(&N);
123606f32e7eSjoerg }
123706f32e7eSjoerg 
visitDISubprogram(const DISubprogram & N)123806f32e7eSjoerg void Verifier::visitDISubprogram(const DISubprogram &N) {
123906f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_subprogram, "invalid tag", &N);
124006f32e7eSjoerg   AssertDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
124106f32e7eSjoerg   if (auto *F = N.getRawFile())
124206f32e7eSjoerg     AssertDI(isa<DIFile>(F), "invalid file", &N, F);
124306f32e7eSjoerg   else
124406f32e7eSjoerg     AssertDI(N.getLine() == 0, "line specified with no file", &N, N.getLine());
124506f32e7eSjoerg   if (auto *T = N.getRawType())
124606f32e7eSjoerg     AssertDI(isa<DISubroutineType>(T), "invalid subroutine type", &N, T);
124706f32e7eSjoerg   AssertDI(isType(N.getRawContainingType()), "invalid containing type", &N,
124806f32e7eSjoerg            N.getRawContainingType());
124906f32e7eSjoerg   if (auto *Params = N.getRawTemplateParams())
125006f32e7eSjoerg     visitTemplateParams(N, *Params);
125106f32e7eSjoerg   if (auto *S = N.getRawDeclaration())
125206f32e7eSjoerg     AssertDI(isa<DISubprogram>(S) && !cast<DISubprogram>(S)->isDefinition(),
125306f32e7eSjoerg              "invalid subprogram declaration", &N, S);
125406f32e7eSjoerg   if (auto *RawNode = N.getRawRetainedNodes()) {
125506f32e7eSjoerg     auto *Node = dyn_cast<MDTuple>(RawNode);
125606f32e7eSjoerg     AssertDI(Node, "invalid retained nodes list", &N, RawNode);
125706f32e7eSjoerg     for (Metadata *Op : Node->operands()) {
125806f32e7eSjoerg       AssertDI(Op && (isa<DILocalVariable>(Op) || isa<DILabel>(Op)),
125906f32e7eSjoerg                "invalid retained nodes, expected DILocalVariable or DILabel",
126006f32e7eSjoerg                &N, Node, Op);
126106f32e7eSjoerg     }
126206f32e7eSjoerg   }
126306f32e7eSjoerg   AssertDI(!hasConflictingReferenceFlags(N.getFlags()),
126406f32e7eSjoerg            "invalid reference flags", &N);
126506f32e7eSjoerg 
126606f32e7eSjoerg   auto *Unit = N.getRawUnit();
126706f32e7eSjoerg   if (N.isDefinition()) {
126806f32e7eSjoerg     // Subprogram definitions (not part of the type hierarchy).
126906f32e7eSjoerg     AssertDI(N.isDistinct(), "subprogram definitions must be distinct", &N);
127006f32e7eSjoerg     AssertDI(Unit, "subprogram definitions must have a compile unit", &N);
127106f32e7eSjoerg     AssertDI(isa<DICompileUnit>(Unit), "invalid unit type", &N, Unit);
127206f32e7eSjoerg     if (N.getFile())
127306f32e7eSjoerg       verifySourceDebugInfo(*N.getUnit(), *N.getFile());
127406f32e7eSjoerg   } else {
127506f32e7eSjoerg     // Subprogram declarations (part of the type hierarchy).
127606f32e7eSjoerg     AssertDI(!Unit, "subprogram declarations must not have a compile unit", &N);
127706f32e7eSjoerg   }
127806f32e7eSjoerg 
127906f32e7eSjoerg   if (auto *RawThrownTypes = N.getRawThrownTypes()) {
128006f32e7eSjoerg     auto *ThrownTypes = dyn_cast<MDTuple>(RawThrownTypes);
128106f32e7eSjoerg     AssertDI(ThrownTypes, "invalid thrown types list", &N, RawThrownTypes);
128206f32e7eSjoerg     for (Metadata *Op : ThrownTypes->operands())
128306f32e7eSjoerg       AssertDI(Op && isa<DIType>(Op), "invalid thrown type", &N, ThrownTypes,
128406f32e7eSjoerg                Op);
128506f32e7eSjoerg   }
128606f32e7eSjoerg 
128706f32e7eSjoerg   if (N.areAllCallsDescribed())
128806f32e7eSjoerg     AssertDI(N.isDefinition(),
128906f32e7eSjoerg              "DIFlagAllCallsDescribed must be attached to a definition");
129006f32e7eSjoerg }
129106f32e7eSjoerg 
visitDILexicalBlockBase(const DILexicalBlockBase & N)129206f32e7eSjoerg void Verifier::visitDILexicalBlockBase(const DILexicalBlockBase &N) {
129306f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_lexical_block, "invalid tag", &N);
129406f32e7eSjoerg   AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
129506f32e7eSjoerg            "invalid local scope", &N, N.getRawScope());
129606f32e7eSjoerg   if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
129706f32e7eSjoerg     AssertDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
129806f32e7eSjoerg }
129906f32e7eSjoerg 
visitDILexicalBlock(const DILexicalBlock & N)130006f32e7eSjoerg void Verifier::visitDILexicalBlock(const DILexicalBlock &N) {
130106f32e7eSjoerg   visitDILexicalBlockBase(N);
130206f32e7eSjoerg 
130306f32e7eSjoerg   AssertDI(N.getLine() || !N.getColumn(),
130406f32e7eSjoerg            "cannot have column info without line info", &N);
130506f32e7eSjoerg }
130606f32e7eSjoerg 
visitDILexicalBlockFile(const DILexicalBlockFile & N)130706f32e7eSjoerg void Verifier::visitDILexicalBlockFile(const DILexicalBlockFile &N) {
130806f32e7eSjoerg   visitDILexicalBlockBase(N);
130906f32e7eSjoerg }
131006f32e7eSjoerg 
visitDICommonBlock(const DICommonBlock & N)131106f32e7eSjoerg void Verifier::visitDICommonBlock(const DICommonBlock &N) {
131206f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_common_block, "invalid tag", &N);
131306f32e7eSjoerg   if (auto *S = N.getRawScope())
131406f32e7eSjoerg     AssertDI(isa<DIScope>(S), "invalid scope ref", &N, S);
131506f32e7eSjoerg   if (auto *S = N.getRawDecl())
131606f32e7eSjoerg     AssertDI(isa<DIGlobalVariable>(S), "invalid declaration", &N, S);
131706f32e7eSjoerg }
131806f32e7eSjoerg 
visitDINamespace(const DINamespace & N)131906f32e7eSjoerg void Verifier::visitDINamespace(const DINamespace &N) {
132006f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_namespace, "invalid tag", &N);
132106f32e7eSjoerg   if (auto *S = N.getRawScope())
132206f32e7eSjoerg     AssertDI(isa<DIScope>(S), "invalid scope ref", &N, S);
132306f32e7eSjoerg }
132406f32e7eSjoerg 
visitDIMacro(const DIMacro & N)132506f32e7eSjoerg void Verifier::visitDIMacro(const DIMacro &N) {
132606f32e7eSjoerg   AssertDI(N.getMacinfoType() == dwarf::DW_MACINFO_define ||
132706f32e7eSjoerg                N.getMacinfoType() == dwarf::DW_MACINFO_undef,
132806f32e7eSjoerg            "invalid macinfo type", &N);
132906f32e7eSjoerg   AssertDI(!N.getName().empty(), "anonymous macro", &N);
133006f32e7eSjoerg   if (!N.getValue().empty()) {
133106f32e7eSjoerg     assert(N.getValue().data()[0] != ' ' && "Macro value has a space prefix");
133206f32e7eSjoerg   }
133306f32e7eSjoerg }
133406f32e7eSjoerg 
visitDIMacroFile(const DIMacroFile & N)133506f32e7eSjoerg void Verifier::visitDIMacroFile(const DIMacroFile &N) {
133606f32e7eSjoerg   AssertDI(N.getMacinfoType() == dwarf::DW_MACINFO_start_file,
133706f32e7eSjoerg            "invalid macinfo type", &N);
133806f32e7eSjoerg   if (auto *F = N.getRawFile())
133906f32e7eSjoerg     AssertDI(isa<DIFile>(F), "invalid file", &N, F);
134006f32e7eSjoerg 
134106f32e7eSjoerg   if (auto *Array = N.getRawElements()) {
134206f32e7eSjoerg     AssertDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
134306f32e7eSjoerg     for (Metadata *Op : N.getElements()->operands()) {
134406f32e7eSjoerg       AssertDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
134506f32e7eSjoerg     }
134606f32e7eSjoerg   }
134706f32e7eSjoerg }
134806f32e7eSjoerg 
visitDIArgList(const DIArgList & N)1349*da58b97aSjoerg void Verifier::visitDIArgList(const DIArgList &N) {
1350*da58b97aSjoerg   AssertDI(!N.getNumOperands(),
1351*da58b97aSjoerg            "DIArgList should have no operands other than a list of "
1352*da58b97aSjoerg            "ValueAsMetadata",
1353*da58b97aSjoerg            &N);
1354*da58b97aSjoerg }
1355*da58b97aSjoerg 
visitDIModule(const DIModule & N)135606f32e7eSjoerg void Verifier::visitDIModule(const DIModule &N) {
135706f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_module, "invalid tag", &N);
135806f32e7eSjoerg   AssertDI(!N.getName().empty(), "anonymous module", &N);
135906f32e7eSjoerg }
136006f32e7eSjoerg 
visitDITemplateParameter(const DITemplateParameter & N)136106f32e7eSjoerg void Verifier::visitDITemplateParameter(const DITemplateParameter &N) {
136206f32e7eSjoerg   AssertDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
136306f32e7eSjoerg }
136406f32e7eSjoerg 
visitDITemplateTypeParameter(const DITemplateTypeParameter & N)136506f32e7eSjoerg void Verifier::visitDITemplateTypeParameter(const DITemplateTypeParameter &N) {
136606f32e7eSjoerg   visitDITemplateParameter(N);
136706f32e7eSjoerg 
136806f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_template_type_parameter, "invalid tag",
136906f32e7eSjoerg            &N);
137006f32e7eSjoerg }
137106f32e7eSjoerg 
visitDITemplateValueParameter(const DITemplateValueParameter & N)137206f32e7eSjoerg void Verifier::visitDITemplateValueParameter(
137306f32e7eSjoerg     const DITemplateValueParameter &N) {
137406f32e7eSjoerg   visitDITemplateParameter(N);
137506f32e7eSjoerg 
137606f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_template_value_parameter ||
137706f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_GNU_template_template_param ||
137806f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack,
137906f32e7eSjoerg            "invalid tag", &N);
138006f32e7eSjoerg }
138106f32e7eSjoerg 
visitDIVariable(const DIVariable & N)138206f32e7eSjoerg void Verifier::visitDIVariable(const DIVariable &N) {
138306f32e7eSjoerg   if (auto *S = N.getRawScope())
138406f32e7eSjoerg     AssertDI(isa<DIScope>(S), "invalid scope", &N, S);
138506f32e7eSjoerg   if (auto *F = N.getRawFile())
138606f32e7eSjoerg     AssertDI(isa<DIFile>(F), "invalid file", &N, F);
138706f32e7eSjoerg }
138806f32e7eSjoerg 
visitDIGlobalVariable(const DIGlobalVariable & N)138906f32e7eSjoerg void Verifier::visitDIGlobalVariable(const DIGlobalVariable &N) {
139006f32e7eSjoerg   // Checks common to all variables.
139106f32e7eSjoerg   visitDIVariable(N);
139206f32e7eSjoerg 
139306f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
139406f32e7eSjoerg   AssertDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1395*da58b97aSjoerg   // Assert only if the global variable is not an extern
1396*da58b97aSjoerg   if (N.isDefinition())
139706f32e7eSjoerg     AssertDI(N.getType(), "missing global variable type", &N);
139806f32e7eSjoerg   if (auto *Member = N.getRawStaticDataMemberDeclaration()) {
139906f32e7eSjoerg     AssertDI(isa<DIDerivedType>(Member),
140006f32e7eSjoerg              "invalid static data member declaration", &N, Member);
140106f32e7eSjoerg   }
140206f32e7eSjoerg }
140306f32e7eSjoerg 
visitDILocalVariable(const DILocalVariable & N)140406f32e7eSjoerg void Verifier::visitDILocalVariable(const DILocalVariable &N) {
140506f32e7eSjoerg   // Checks common to all variables.
140606f32e7eSjoerg   visitDIVariable(N);
140706f32e7eSjoerg 
140806f32e7eSjoerg   AssertDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
140906f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
141006f32e7eSjoerg   AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
141106f32e7eSjoerg            "local variable requires a valid scope", &N, N.getRawScope());
141206f32e7eSjoerg   if (auto Ty = N.getType())
141306f32e7eSjoerg     AssertDI(!isa<DISubroutineType>(Ty), "invalid type", &N, N.getType());
141406f32e7eSjoerg }
141506f32e7eSjoerg 
visitDILabel(const DILabel & N)141606f32e7eSjoerg void Verifier::visitDILabel(const DILabel &N) {
141706f32e7eSjoerg   if (auto *S = N.getRawScope())
141806f32e7eSjoerg     AssertDI(isa<DIScope>(S), "invalid scope", &N, S);
141906f32e7eSjoerg   if (auto *F = N.getRawFile())
142006f32e7eSjoerg     AssertDI(isa<DIFile>(F), "invalid file", &N, F);
142106f32e7eSjoerg 
142206f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_label, "invalid tag", &N);
142306f32e7eSjoerg   AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
142406f32e7eSjoerg            "label requires a valid scope", &N, N.getRawScope());
142506f32e7eSjoerg }
142606f32e7eSjoerg 
visitDIExpression(const DIExpression & N)142706f32e7eSjoerg void Verifier::visitDIExpression(const DIExpression &N) {
142806f32e7eSjoerg   AssertDI(N.isValid(), "invalid expression", &N);
142906f32e7eSjoerg }
143006f32e7eSjoerg 
visitDIGlobalVariableExpression(const DIGlobalVariableExpression & GVE)143106f32e7eSjoerg void Verifier::visitDIGlobalVariableExpression(
143206f32e7eSjoerg     const DIGlobalVariableExpression &GVE) {
143306f32e7eSjoerg   AssertDI(GVE.getVariable(), "missing variable");
143406f32e7eSjoerg   if (auto *Var = GVE.getVariable())
143506f32e7eSjoerg     visitDIGlobalVariable(*Var);
143606f32e7eSjoerg   if (auto *Expr = GVE.getExpression()) {
143706f32e7eSjoerg     visitDIExpression(*Expr);
143806f32e7eSjoerg     if (auto Fragment = Expr->getFragmentInfo())
143906f32e7eSjoerg       verifyFragmentExpression(*GVE.getVariable(), *Fragment, &GVE);
144006f32e7eSjoerg   }
144106f32e7eSjoerg }
144206f32e7eSjoerg 
visitDIObjCProperty(const DIObjCProperty & N)144306f32e7eSjoerg void Verifier::visitDIObjCProperty(const DIObjCProperty &N) {
144406f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_APPLE_property, "invalid tag", &N);
144506f32e7eSjoerg   if (auto *T = N.getRawType())
144606f32e7eSjoerg     AssertDI(isType(T), "invalid type ref", &N, T);
144706f32e7eSjoerg   if (auto *F = N.getRawFile())
144806f32e7eSjoerg     AssertDI(isa<DIFile>(F), "invalid file", &N, F);
144906f32e7eSjoerg }
145006f32e7eSjoerg 
visitDIImportedEntity(const DIImportedEntity & N)145106f32e7eSjoerg void Verifier::visitDIImportedEntity(const DIImportedEntity &N) {
145206f32e7eSjoerg   AssertDI(N.getTag() == dwarf::DW_TAG_imported_module ||
145306f32e7eSjoerg                N.getTag() == dwarf::DW_TAG_imported_declaration,
145406f32e7eSjoerg            "invalid tag", &N);
145506f32e7eSjoerg   if (auto *S = N.getRawScope())
145606f32e7eSjoerg     AssertDI(isa<DIScope>(S), "invalid scope for imported entity", &N, S);
145706f32e7eSjoerg   AssertDI(isDINode(N.getRawEntity()), "invalid imported entity", &N,
145806f32e7eSjoerg            N.getRawEntity());
145906f32e7eSjoerg }
146006f32e7eSjoerg 
visitComdat(const Comdat & C)146106f32e7eSjoerg void Verifier::visitComdat(const Comdat &C) {
146206f32e7eSjoerg   // In COFF the Module is invalid if the GlobalValue has private linkage.
146306f32e7eSjoerg   // Entities with private linkage don't have entries in the symbol table.
146406f32e7eSjoerg   if (TT.isOSBinFormatCOFF())
146506f32e7eSjoerg     if (const GlobalValue *GV = M.getNamedValue(C.getName()))
146606f32e7eSjoerg       Assert(!GV->hasPrivateLinkage(),
146706f32e7eSjoerg              "comdat global value has private linkage", GV);
146806f32e7eSjoerg }
146906f32e7eSjoerg 
visitModuleIdents(const Module & M)147006f32e7eSjoerg void Verifier::visitModuleIdents(const Module &M) {
147106f32e7eSjoerg   const NamedMDNode *Idents = M.getNamedMetadata("llvm.ident");
147206f32e7eSjoerg   if (!Idents)
147306f32e7eSjoerg     return;
147406f32e7eSjoerg 
147506f32e7eSjoerg   // llvm.ident takes a list of metadata entry. Each entry has only one string.
147606f32e7eSjoerg   // Scan each llvm.ident entry and make sure that this requirement is met.
147706f32e7eSjoerg   for (const MDNode *N : Idents->operands()) {
147806f32e7eSjoerg     Assert(N->getNumOperands() == 1,
147906f32e7eSjoerg            "incorrect number of operands in llvm.ident metadata", N);
148006f32e7eSjoerg     Assert(dyn_cast_or_null<MDString>(N->getOperand(0)),
148106f32e7eSjoerg            ("invalid value for llvm.ident metadata entry operand"
148206f32e7eSjoerg             "(the operand should be a string)"),
148306f32e7eSjoerg            N->getOperand(0));
148406f32e7eSjoerg   }
148506f32e7eSjoerg }
148606f32e7eSjoerg 
visitModuleCommandLines(const Module & M)148706f32e7eSjoerg void Verifier::visitModuleCommandLines(const Module &M) {
148806f32e7eSjoerg   const NamedMDNode *CommandLines = M.getNamedMetadata("llvm.commandline");
148906f32e7eSjoerg   if (!CommandLines)
149006f32e7eSjoerg     return;
149106f32e7eSjoerg 
149206f32e7eSjoerg   // llvm.commandline takes a list of metadata entry. Each entry has only one
149306f32e7eSjoerg   // string. Scan each llvm.commandline entry and make sure that this
149406f32e7eSjoerg   // requirement is met.
149506f32e7eSjoerg   for (const MDNode *N : CommandLines->operands()) {
149606f32e7eSjoerg     Assert(N->getNumOperands() == 1,
149706f32e7eSjoerg            "incorrect number of operands in llvm.commandline metadata", N);
149806f32e7eSjoerg     Assert(dyn_cast_or_null<MDString>(N->getOperand(0)),
149906f32e7eSjoerg            ("invalid value for llvm.commandline metadata entry operand"
150006f32e7eSjoerg             "(the operand should be a string)"),
150106f32e7eSjoerg            N->getOperand(0));
150206f32e7eSjoerg   }
150306f32e7eSjoerg }
150406f32e7eSjoerg 
visitModuleFlags(const Module & M)150506f32e7eSjoerg void Verifier::visitModuleFlags(const Module &M) {
150606f32e7eSjoerg   const NamedMDNode *Flags = M.getModuleFlagsMetadata();
150706f32e7eSjoerg   if (!Flags) return;
150806f32e7eSjoerg 
150906f32e7eSjoerg   // Scan each flag, and track the flags and requirements.
151006f32e7eSjoerg   DenseMap<const MDString*, const MDNode*> SeenIDs;
151106f32e7eSjoerg   SmallVector<const MDNode*, 16> Requirements;
151206f32e7eSjoerg   for (const MDNode *MDN : Flags->operands())
151306f32e7eSjoerg     visitModuleFlag(MDN, SeenIDs, Requirements);
151406f32e7eSjoerg 
151506f32e7eSjoerg   // Validate that the requirements in the module are valid.
151606f32e7eSjoerg   for (const MDNode *Requirement : Requirements) {
151706f32e7eSjoerg     const MDString *Flag = cast<MDString>(Requirement->getOperand(0));
151806f32e7eSjoerg     const Metadata *ReqValue = Requirement->getOperand(1);
151906f32e7eSjoerg 
152006f32e7eSjoerg     const MDNode *Op = SeenIDs.lookup(Flag);
152106f32e7eSjoerg     if (!Op) {
152206f32e7eSjoerg       CheckFailed("invalid requirement on flag, flag is not present in module",
152306f32e7eSjoerg                   Flag);
152406f32e7eSjoerg       continue;
152506f32e7eSjoerg     }
152606f32e7eSjoerg 
152706f32e7eSjoerg     if (Op->getOperand(2) != ReqValue) {
152806f32e7eSjoerg       CheckFailed(("invalid requirement on flag, "
152906f32e7eSjoerg                    "flag does not have the required value"),
153006f32e7eSjoerg                   Flag);
153106f32e7eSjoerg       continue;
153206f32e7eSjoerg     }
153306f32e7eSjoerg   }
153406f32e7eSjoerg }
153506f32e7eSjoerg 
153606f32e7eSjoerg void
visitModuleFlag(const MDNode * Op,DenseMap<const MDString *,const MDNode * > & SeenIDs,SmallVectorImpl<const MDNode * > & Requirements)153706f32e7eSjoerg Verifier::visitModuleFlag(const MDNode *Op,
153806f32e7eSjoerg                           DenseMap<const MDString *, const MDNode *> &SeenIDs,
153906f32e7eSjoerg                           SmallVectorImpl<const MDNode *> &Requirements) {
154006f32e7eSjoerg   // Each module flag should have three arguments, the merge behavior (a
154106f32e7eSjoerg   // constant int), the flag ID (an MDString), and the value.
154206f32e7eSjoerg   Assert(Op->getNumOperands() == 3,
154306f32e7eSjoerg          "incorrect number of operands in module flag", Op);
154406f32e7eSjoerg   Module::ModFlagBehavior MFB;
154506f32e7eSjoerg   if (!Module::isValidModFlagBehavior(Op->getOperand(0), MFB)) {
154606f32e7eSjoerg     Assert(
154706f32e7eSjoerg         mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0)),
154806f32e7eSjoerg         "invalid behavior operand in module flag (expected constant integer)",
154906f32e7eSjoerg         Op->getOperand(0));
155006f32e7eSjoerg     Assert(false,
155106f32e7eSjoerg            "invalid behavior operand in module flag (unexpected constant)",
155206f32e7eSjoerg            Op->getOperand(0));
155306f32e7eSjoerg   }
155406f32e7eSjoerg   MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
155506f32e7eSjoerg   Assert(ID, "invalid ID operand in module flag (expected metadata string)",
155606f32e7eSjoerg          Op->getOperand(1));
155706f32e7eSjoerg 
155806f32e7eSjoerg   // Sanity check the values for behaviors with additional requirements.
155906f32e7eSjoerg   switch (MFB) {
156006f32e7eSjoerg   case Module::Error:
156106f32e7eSjoerg   case Module::Warning:
156206f32e7eSjoerg   case Module::Override:
156306f32e7eSjoerg     // These behavior types accept any value.
156406f32e7eSjoerg     break;
156506f32e7eSjoerg 
156606f32e7eSjoerg   case Module::Max: {
156706f32e7eSjoerg     Assert(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2)),
156806f32e7eSjoerg            "invalid value for 'max' module flag (expected constant integer)",
156906f32e7eSjoerg            Op->getOperand(2));
157006f32e7eSjoerg     break;
157106f32e7eSjoerg   }
157206f32e7eSjoerg 
157306f32e7eSjoerg   case Module::Require: {
157406f32e7eSjoerg     // The value should itself be an MDNode with two operands, a flag ID (an
157506f32e7eSjoerg     // MDString), and a value.
157606f32e7eSjoerg     MDNode *Value = dyn_cast<MDNode>(Op->getOperand(2));
157706f32e7eSjoerg     Assert(Value && Value->getNumOperands() == 2,
157806f32e7eSjoerg            "invalid value for 'require' module flag (expected metadata pair)",
157906f32e7eSjoerg            Op->getOperand(2));
158006f32e7eSjoerg     Assert(isa<MDString>(Value->getOperand(0)),
158106f32e7eSjoerg            ("invalid value for 'require' module flag "
158206f32e7eSjoerg             "(first value operand should be a string)"),
158306f32e7eSjoerg            Value->getOperand(0));
158406f32e7eSjoerg 
158506f32e7eSjoerg     // Append it to the list of requirements, to check once all module flags are
158606f32e7eSjoerg     // scanned.
158706f32e7eSjoerg     Requirements.push_back(Value);
158806f32e7eSjoerg     break;
158906f32e7eSjoerg   }
159006f32e7eSjoerg 
159106f32e7eSjoerg   case Module::Append:
159206f32e7eSjoerg   case Module::AppendUnique: {
159306f32e7eSjoerg     // These behavior types require the operand be an MDNode.
159406f32e7eSjoerg     Assert(isa<MDNode>(Op->getOperand(2)),
159506f32e7eSjoerg            "invalid value for 'append'-type module flag "
159606f32e7eSjoerg            "(expected a metadata node)",
159706f32e7eSjoerg            Op->getOperand(2));
159806f32e7eSjoerg     break;
159906f32e7eSjoerg   }
160006f32e7eSjoerg   }
160106f32e7eSjoerg 
160206f32e7eSjoerg   // Unless this is a "requires" flag, check the ID is unique.
160306f32e7eSjoerg   if (MFB != Module::Require) {
160406f32e7eSjoerg     bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
160506f32e7eSjoerg     Assert(Inserted,
160606f32e7eSjoerg            "module flag identifiers must be unique (or of 'require' type)", ID);
160706f32e7eSjoerg   }
160806f32e7eSjoerg 
160906f32e7eSjoerg   if (ID->getString() == "wchar_size") {
161006f32e7eSjoerg     ConstantInt *Value
161106f32e7eSjoerg       = mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
161206f32e7eSjoerg     Assert(Value, "wchar_size metadata requires constant integer argument");
161306f32e7eSjoerg   }
161406f32e7eSjoerg 
161506f32e7eSjoerg   if (ID->getString() == "Linker Options") {
161606f32e7eSjoerg     // If the llvm.linker.options named metadata exists, we assume that the
161706f32e7eSjoerg     // bitcode reader has upgraded the module flag. Otherwise the flag might
161806f32e7eSjoerg     // have been created by a client directly.
161906f32e7eSjoerg     Assert(M.getNamedMetadata("llvm.linker.options"),
162006f32e7eSjoerg            "'Linker Options' named metadata no longer supported");
162106f32e7eSjoerg   }
162206f32e7eSjoerg 
1623*da58b97aSjoerg   if (ID->getString() == "SemanticInterposition") {
1624*da58b97aSjoerg     ConstantInt *Value =
1625*da58b97aSjoerg         mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1626*da58b97aSjoerg     Assert(Value,
1627*da58b97aSjoerg            "SemanticInterposition metadata requires constant integer argument");
1628*da58b97aSjoerg   }
1629*da58b97aSjoerg 
163006f32e7eSjoerg   if (ID->getString() == "CG Profile") {
163106f32e7eSjoerg     for (const MDOperand &MDO : cast<MDNode>(Op->getOperand(2))->operands())
163206f32e7eSjoerg       visitModuleFlagCGProfileEntry(MDO);
163306f32e7eSjoerg   }
163406f32e7eSjoerg }
163506f32e7eSjoerg 
visitModuleFlagCGProfileEntry(const MDOperand & MDO)163606f32e7eSjoerg void Verifier::visitModuleFlagCGProfileEntry(const MDOperand &MDO) {
163706f32e7eSjoerg   auto CheckFunction = [&](const MDOperand &FuncMDO) {
163806f32e7eSjoerg     if (!FuncMDO)
163906f32e7eSjoerg       return;
164006f32e7eSjoerg     auto F = dyn_cast<ValueAsMetadata>(FuncMDO);
1641*da58b97aSjoerg     Assert(F && isa<Function>(F->getValue()->stripPointerCasts()),
1642*da58b97aSjoerg            "expected a Function or null", FuncMDO);
164306f32e7eSjoerg   };
164406f32e7eSjoerg   auto Node = dyn_cast_or_null<MDNode>(MDO);
164506f32e7eSjoerg   Assert(Node && Node->getNumOperands() == 3, "expected a MDNode triple", MDO);
164606f32e7eSjoerg   CheckFunction(Node->getOperand(0));
164706f32e7eSjoerg   CheckFunction(Node->getOperand(1));
164806f32e7eSjoerg   auto Count = dyn_cast_or_null<ConstantAsMetadata>(Node->getOperand(2));
164906f32e7eSjoerg   Assert(Count && Count->getType()->isIntegerTy(),
165006f32e7eSjoerg          "expected an integer constant", Node->getOperand(2));
165106f32e7eSjoerg }
165206f32e7eSjoerg 
165306f32e7eSjoerg /// Return true if this attribute kind only applies to functions.
isFuncOnlyAttr(Attribute::AttrKind Kind)165406f32e7eSjoerg static bool isFuncOnlyAttr(Attribute::AttrKind Kind) {
165506f32e7eSjoerg   switch (Kind) {
1656*da58b97aSjoerg   case Attribute::NoMerge:
165706f32e7eSjoerg   case Attribute::NoReturn:
165806f32e7eSjoerg   case Attribute::NoSync:
165906f32e7eSjoerg   case Attribute::WillReturn:
1660*da58b97aSjoerg   case Attribute::NoCallback:
166106f32e7eSjoerg   case Attribute::NoCfCheck:
166206f32e7eSjoerg   case Attribute::NoUnwind:
166306f32e7eSjoerg   case Attribute::NoInline:
166406f32e7eSjoerg   case Attribute::AlwaysInline:
166506f32e7eSjoerg   case Attribute::OptimizeForSize:
166606f32e7eSjoerg   case Attribute::StackProtect:
166706f32e7eSjoerg   case Attribute::StackProtectReq:
166806f32e7eSjoerg   case Attribute::StackProtectStrong:
166906f32e7eSjoerg   case Attribute::SafeStack:
167006f32e7eSjoerg   case Attribute::ShadowCallStack:
167106f32e7eSjoerg   case Attribute::NoRedZone:
167206f32e7eSjoerg   case Attribute::NoImplicitFloat:
167306f32e7eSjoerg   case Attribute::Naked:
167406f32e7eSjoerg   case Attribute::InlineHint:
167506f32e7eSjoerg   case Attribute::UWTable:
1676*da58b97aSjoerg   case Attribute::VScaleRange:
167706f32e7eSjoerg   case Attribute::NonLazyBind:
167806f32e7eSjoerg   case Attribute::ReturnsTwice:
167906f32e7eSjoerg   case Attribute::SanitizeAddress:
168006f32e7eSjoerg   case Attribute::SanitizeHWAddress:
168106f32e7eSjoerg   case Attribute::SanitizeMemTag:
168206f32e7eSjoerg   case Attribute::SanitizeThread:
168306f32e7eSjoerg   case Attribute::SanitizeMemory:
168406f32e7eSjoerg   case Attribute::MinSize:
168506f32e7eSjoerg   case Attribute::NoDuplicate:
168606f32e7eSjoerg   case Attribute::Builtin:
168706f32e7eSjoerg   case Attribute::NoBuiltin:
168806f32e7eSjoerg   case Attribute::Cold:
1689*da58b97aSjoerg   case Attribute::Hot:
169006f32e7eSjoerg   case Attribute::OptForFuzzing:
169106f32e7eSjoerg   case Attribute::OptimizeNone:
169206f32e7eSjoerg   case Attribute::JumpTable:
169306f32e7eSjoerg   case Attribute::Convergent:
169406f32e7eSjoerg   case Attribute::ArgMemOnly:
169506f32e7eSjoerg   case Attribute::NoRecurse:
169606f32e7eSjoerg   case Attribute::InaccessibleMemOnly:
169706f32e7eSjoerg   case Attribute::InaccessibleMemOrArgMemOnly:
169806f32e7eSjoerg   case Attribute::AllocSize:
169906f32e7eSjoerg   case Attribute::SpeculativeLoadHardening:
170006f32e7eSjoerg   case Attribute::Speculatable:
170106f32e7eSjoerg   case Attribute::StrictFP:
1702*da58b97aSjoerg   case Attribute::NullPointerIsValid:
1703*da58b97aSjoerg   case Attribute::MustProgress:
1704*da58b97aSjoerg   case Attribute::NoProfile:
170506f32e7eSjoerg     return true;
170606f32e7eSjoerg   default:
170706f32e7eSjoerg     break;
170806f32e7eSjoerg   }
170906f32e7eSjoerg   return false;
171006f32e7eSjoerg }
171106f32e7eSjoerg 
171206f32e7eSjoerg /// Return true if this is a function attribute that can also appear on
171306f32e7eSjoerg /// arguments.
isFuncOrArgAttr(Attribute::AttrKind Kind)171406f32e7eSjoerg static bool isFuncOrArgAttr(Attribute::AttrKind Kind) {
171506f32e7eSjoerg   return Kind == Attribute::ReadOnly || Kind == Attribute::WriteOnly ||
1716*da58b97aSjoerg          Kind == Attribute::ReadNone || Kind == Attribute::NoFree ||
1717*da58b97aSjoerg          Kind == Attribute::Preallocated || Kind == Attribute::StackAlignment;
171806f32e7eSjoerg }
171906f32e7eSjoerg 
verifyAttributeTypes(AttributeSet Attrs,bool IsFunction,const Value * V)172006f32e7eSjoerg void Verifier::verifyAttributeTypes(AttributeSet Attrs, bool IsFunction,
172106f32e7eSjoerg                                     const Value *V) {
172206f32e7eSjoerg   for (Attribute A : Attrs) {
1723*da58b97aSjoerg 
1724*da58b97aSjoerg     if (A.isStringAttribute()) {
1725*da58b97aSjoerg #define GET_ATTR_NAMES
1726*da58b97aSjoerg #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME)
1727*da58b97aSjoerg #define ATTRIBUTE_STRBOOL(ENUM_NAME, DISPLAY_NAME)                             \
1728*da58b97aSjoerg   if (A.getKindAsString() == #DISPLAY_NAME) {                                  \
1729*da58b97aSjoerg     auto V = A.getValueAsString();                                             \
1730*da58b97aSjoerg     if (!(V.empty() || V == "true" || V == "false"))                           \
1731*da58b97aSjoerg       CheckFailed("invalid value for '" #DISPLAY_NAME "' attribute: " + V +    \
1732*da58b97aSjoerg                   "");                                                         \
1733*da58b97aSjoerg   }
1734*da58b97aSjoerg 
1735*da58b97aSjoerg #include "llvm/IR/Attributes.inc"
173606f32e7eSjoerg       continue;
1737*da58b97aSjoerg     }
1738*da58b97aSjoerg 
1739*da58b97aSjoerg     if (A.isIntAttribute() !=
1740*da58b97aSjoerg         Attribute::doesAttrKindHaveArgument(A.getKindAsEnum())) {
1741*da58b97aSjoerg       CheckFailed("Attribute '" + A.getAsString() + "' should have an Argument",
1742*da58b97aSjoerg                   V);
1743*da58b97aSjoerg       return;
1744*da58b97aSjoerg     }
174506f32e7eSjoerg 
174606f32e7eSjoerg     if (isFuncOnlyAttr(A.getKindAsEnum())) {
174706f32e7eSjoerg       if (!IsFunction) {
174806f32e7eSjoerg         CheckFailed("Attribute '" + A.getAsString() +
174906f32e7eSjoerg                         "' only applies to functions!",
175006f32e7eSjoerg                     V);
175106f32e7eSjoerg         return;
175206f32e7eSjoerg       }
175306f32e7eSjoerg     } else if (IsFunction && !isFuncOrArgAttr(A.getKindAsEnum())) {
175406f32e7eSjoerg       CheckFailed("Attribute '" + A.getAsString() +
175506f32e7eSjoerg                       "' does not apply to functions!",
175606f32e7eSjoerg                   V);
175706f32e7eSjoerg       return;
175806f32e7eSjoerg     }
175906f32e7eSjoerg   }
176006f32e7eSjoerg }
176106f32e7eSjoerg 
176206f32e7eSjoerg // VerifyParameterAttrs - Check the given attributes for an argument or return
176306f32e7eSjoerg // value of the specified type.  The value V is printed in error messages.
verifyParameterAttrs(AttributeSet Attrs,Type * Ty,const Value * V)176406f32e7eSjoerg void Verifier::verifyParameterAttrs(AttributeSet Attrs, Type *Ty,
176506f32e7eSjoerg                                     const Value *V) {
176606f32e7eSjoerg   if (!Attrs.hasAttributes())
176706f32e7eSjoerg     return;
176806f32e7eSjoerg 
176906f32e7eSjoerg   verifyAttributeTypes(Attrs, /*IsFunction=*/false, V);
177006f32e7eSjoerg 
177106f32e7eSjoerg   if (Attrs.hasAttribute(Attribute::ImmArg)) {
177206f32e7eSjoerg     Assert(Attrs.getNumAttributes() == 1,
177306f32e7eSjoerg            "Attribute 'immarg' is incompatible with other attributes", V);
177406f32e7eSjoerg   }
177506f32e7eSjoerg 
177606f32e7eSjoerg   // Check for mutually incompatible attributes.  Only inreg is compatible with
177706f32e7eSjoerg   // sret.
177806f32e7eSjoerg   unsigned AttrCount = 0;
177906f32e7eSjoerg   AttrCount += Attrs.hasAttribute(Attribute::ByVal);
178006f32e7eSjoerg   AttrCount += Attrs.hasAttribute(Attribute::InAlloca);
1781*da58b97aSjoerg   AttrCount += Attrs.hasAttribute(Attribute::Preallocated);
178206f32e7eSjoerg   AttrCount += Attrs.hasAttribute(Attribute::StructRet) ||
178306f32e7eSjoerg                Attrs.hasAttribute(Attribute::InReg);
178406f32e7eSjoerg   AttrCount += Attrs.hasAttribute(Attribute::Nest);
1785*da58b97aSjoerg   AttrCount += Attrs.hasAttribute(Attribute::ByRef);
1786*da58b97aSjoerg   Assert(AttrCount <= 1,
1787*da58b97aSjoerg          "Attributes 'byval', 'inalloca', 'preallocated', 'inreg', 'nest', "
1788*da58b97aSjoerg          "'byref', and 'sret' are incompatible!",
178906f32e7eSjoerg          V);
179006f32e7eSjoerg 
179106f32e7eSjoerg   Assert(!(Attrs.hasAttribute(Attribute::InAlloca) &&
179206f32e7eSjoerg            Attrs.hasAttribute(Attribute::ReadOnly)),
179306f32e7eSjoerg          "Attributes "
179406f32e7eSjoerg          "'inalloca and readonly' are incompatible!",
179506f32e7eSjoerg          V);
179606f32e7eSjoerg 
179706f32e7eSjoerg   Assert(!(Attrs.hasAttribute(Attribute::StructRet) &&
179806f32e7eSjoerg            Attrs.hasAttribute(Attribute::Returned)),
179906f32e7eSjoerg          "Attributes "
180006f32e7eSjoerg          "'sret and returned' are incompatible!",
180106f32e7eSjoerg          V);
180206f32e7eSjoerg 
180306f32e7eSjoerg   Assert(!(Attrs.hasAttribute(Attribute::ZExt) &&
180406f32e7eSjoerg            Attrs.hasAttribute(Attribute::SExt)),
180506f32e7eSjoerg          "Attributes "
180606f32e7eSjoerg          "'zeroext and signext' are incompatible!",
180706f32e7eSjoerg          V);
180806f32e7eSjoerg 
180906f32e7eSjoerg   Assert(!(Attrs.hasAttribute(Attribute::ReadNone) &&
181006f32e7eSjoerg            Attrs.hasAttribute(Attribute::ReadOnly)),
181106f32e7eSjoerg          "Attributes "
181206f32e7eSjoerg          "'readnone and readonly' are incompatible!",
181306f32e7eSjoerg          V);
181406f32e7eSjoerg 
181506f32e7eSjoerg   Assert(!(Attrs.hasAttribute(Attribute::ReadNone) &&
181606f32e7eSjoerg            Attrs.hasAttribute(Attribute::WriteOnly)),
181706f32e7eSjoerg          "Attributes "
181806f32e7eSjoerg          "'readnone and writeonly' are incompatible!",
181906f32e7eSjoerg          V);
182006f32e7eSjoerg 
182106f32e7eSjoerg   Assert(!(Attrs.hasAttribute(Attribute::ReadOnly) &&
182206f32e7eSjoerg            Attrs.hasAttribute(Attribute::WriteOnly)),
182306f32e7eSjoerg          "Attributes "
182406f32e7eSjoerg          "'readonly and writeonly' are incompatible!",
182506f32e7eSjoerg          V);
182606f32e7eSjoerg 
182706f32e7eSjoerg   Assert(!(Attrs.hasAttribute(Attribute::NoInline) &&
182806f32e7eSjoerg            Attrs.hasAttribute(Attribute::AlwaysInline)),
182906f32e7eSjoerg          "Attributes "
183006f32e7eSjoerg          "'noinline and alwaysinline' are incompatible!",
183106f32e7eSjoerg          V);
183206f32e7eSjoerg 
183306f32e7eSjoerg   AttrBuilder IncompatibleAttrs = AttributeFuncs::typeIncompatible(Ty);
183406f32e7eSjoerg   Assert(!AttrBuilder(Attrs).overlaps(IncompatibleAttrs),
183506f32e7eSjoerg          "Wrong types for attribute: " +
183606f32e7eSjoerg              AttributeSet::get(Context, IncompatibleAttrs).getAsString(),
183706f32e7eSjoerg          V);
183806f32e7eSjoerg 
183906f32e7eSjoerg   if (PointerType *PTy = dyn_cast<PointerType>(Ty)) {
184006f32e7eSjoerg     SmallPtrSet<Type*, 4> Visited;
184106f32e7eSjoerg     if (!PTy->getElementType()->isSized(&Visited)) {
184206f32e7eSjoerg       Assert(!Attrs.hasAttribute(Attribute::ByVal) &&
1843*da58b97aSjoerg              !Attrs.hasAttribute(Attribute::ByRef) &&
1844*da58b97aSjoerg              !Attrs.hasAttribute(Attribute::InAlloca) &&
1845*da58b97aSjoerg              !Attrs.hasAttribute(Attribute::Preallocated),
1846*da58b97aSjoerg              "Attributes 'byval', 'byref', 'inalloca', and 'preallocated' do not "
1847*da58b97aSjoerg              "support unsized types!",
184806f32e7eSjoerg              V);
184906f32e7eSjoerg     }
185006f32e7eSjoerg     if (!isa<PointerType>(PTy->getElementType()))
185106f32e7eSjoerg       Assert(!Attrs.hasAttribute(Attribute::SwiftError),
185206f32e7eSjoerg              "Attribute 'swifterror' only applies to parameters "
185306f32e7eSjoerg              "with pointer to pointer type!",
185406f32e7eSjoerg              V);
1855*da58b97aSjoerg 
1856*da58b97aSjoerg     if (Attrs.hasAttribute(Attribute::ByRef)) {
1857*da58b97aSjoerg       Assert(Attrs.getByRefType() == PTy->getElementType(),
1858*da58b97aSjoerg              "Attribute 'byref' type does not match parameter!", V);
1859*da58b97aSjoerg     }
1860*da58b97aSjoerg 
1861*da58b97aSjoerg     if (Attrs.hasAttribute(Attribute::ByVal) && Attrs.getByValType()) {
1862*da58b97aSjoerg       Assert(Attrs.getByValType() == PTy->getElementType(),
1863*da58b97aSjoerg              "Attribute 'byval' type does not match parameter!", V);
1864*da58b97aSjoerg     }
1865*da58b97aSjoerg 
1866*da58b97aSjoerg     if (Attrs.hasAttribute(Attribute::Preallocated)) {
1867*da58b97aSjoerg       Assert(Attrs.getPreallocatedType() == PTy->getElementType(),
1868*da58b97aSjoerg              "Attribute 'preallocated' type does not match parameter!", V);
1869*da58b97aSjoerg     }
1870*da58b97aSjoerg 
1871*da58b97aSjoerg     if (Attrs.hasAttribute(Attribute::InAlloca)) {
1872*da58b97aSjoerg       Assert(Attrs.getInAllocaType() == PTy->getElementType(),
1873*da58b97aSjoerg              "Attribute 'inalloca' type does not match parameter!", V);
1874*da58b97aSjoerg     }
187506f32e7eSjoerg   } else {
187606f32e7eSjoerg     Assert(!Attrs.hasAttribute(Attribute::ByVal),
187706f32e7eSjoerg            "Attribute 'byval' only applies to parameters with pointer type!",
187806f32e7eSjoerg            V);
1879*da58b97aSjoerg     Assert(!Attrs.hasAttribute(Attribute::ByRef),
1880*da58b97aSjoerg            "Attribute 'byref' only applies to parameters with pointer type!",
1881*da58b97aSjoerg            V);
188206f32e7eSjoerg     Assert(!Attrs.hasAttribute(Attribute::SwiftError),
188306f32e7eSjoerg            "Attribute 'swifterror' only applies to parameters "
188406f32e7eSjoerg            "with pointer type!",
188506f32e7eSjoerg            V);
188606f32e7eSjoerg   }
188706f32e7eSjoerg }
188806f32e7eSjoerg 
188906f32e7eSjoerg // Check parameter attributes against a function type.
189006f32e7eSjoerg // The value V is printed in error messages.
verifyFunctionAttrs(FunctionType * FT,AttributeList Attrs,const Value * V,bool IsIntrinsic)189106f32e7eSjoerg void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
189206f32e7eSjoerg                                    const Value *V, bool IsIntrinsic) {
189306f32e7eSjoerg   if (Attrs.isEmpty())
189406f32e7eSjoerg     return;
189506f32e7eSjoerg 
1896*da58b97aSjoerg   if (AttributeListsVisited.insert(Attrs.getRawPointer()).second) {
1897*da58b97aSjoerg     Assert(Attrs.hasParentContext(Context),
1898*da58b97aSjoerg            "Attribute list does not match Module context!", &Attrs, V);
1899*da58b97aSjoerg     for (const auto &AttrSet : Attrs) {
1900*da58b97aSjoerg       Assert(!AttrSet.hasAttributes() || AttrSet.hasParentContext(Context),
1901*da58b97aSjoerg              "Attribute set does not match Module context!", &AttrSet, V);
1902*da58b97aSjoerg       for (const auto &A : AttrSet) {
1903*da58b97aSjoerg         Assert(A.hasParentContext(Context),
1904*da58b97aSjoerg                "Attribute does not match Module context!", &A, V);
1905*da58b97aSjoerg       }
1906*da58b97aSjoerg     }
1907*da58b97aSjoerg   }
1908*da58b97aSjoerg 
190906f32e7eSjoerg   bool SawNest = false;
191006f32e7eSjoerg   bool SawReturned = false;
191106f32e7eSjoerg   bool SawSRet = false;
191206f32e7eSjoerg   bool SawSwiftSelf = false;
1913*da58b97aSjoerg   bool SawSwiftAsync = false;
191406f32e7eSjoerg   bool SawSwiftError = false;
191506f32e7eSjoerg 
191606f32e7eSjoerg   // Verify return value attributes.
191706f32e7eSjoerg   AttributeSet RetAttrs = Attrs.getRetAttributes();
191806f32e7eSjoerg   Assert((!RetAttrs.hasAttribute(Attribute::ByVal) &&
191906f32e7eSjoerg           !RetAttrs.hasAttribute(Attribute::Nest) &&
192006f32e7eSjoerg           !RetAttrs.hasAttribute(Attribute::StructRet) &&
192106f32e7eSjoerg           !RetAttrs.hasAttribute(Attribute::NoCapture) &&
1922*da58b97aSjoerg           !RetAttrs.hasAttribute(Attribute::NoFree) &&
192306f32e7eSjoerg           !RetAttrs.hasAttribute(Attribute::Returned) &&
192406f32e7eSjoerg           !RetAttrs.hasAttribute(Attribute::InAlloca) &&
1925*da58b97aSjoerg           !RetAttrs.hasAttribute(Attribute::Preallocated) &&
1926*da58b97aSjoerg           !RetAttrs.hasAttribute(Attribute::ByRef) &&
192706f32e7eSjoerg           !RetAttrs.hasAttribute(Attribute::SwiftSelf) &&
1928*da58b97aSjoerg           !RetAttrs.hasAttribute(Attribute::SwiftAsync) &&
192906f32e7eSjoerg           !RetAttrs.hasAttribute(Attribute::SwiftError)),
1930*da58b97aSjoerg          "Attributes 'byval', 'inalloca', 'preallocated', 'byref', "
1931*da58b97aSjoerg          "'nest', 'sret', 'nocapture', 'nofree', "
1932*da58b97aSjoerg          "'returned', 'swiftself', 'swiftasync', and 'swifterror'"
1933*da58b97aSjoerg          " do not apply to return values!",
193406f32e7eSjoerg          V);
193506f32e7eSjoerg   Assert((!RetAttrs.hasAttribute(Attribute::ReadOnly) &&
193606f32e7eSjoerg           !RetAttrs.hasAttribute(Attribute::WriteOnly) &&
193706f32e7eSjoerg           !RetAttrs.hasAttribute(Attribute::ReadNone)),
193806f32e7eSjoerg          "Attribute '" + RetAttrs.getAsString() +
193906f32e7eSjoerg              "' does not apply to function returns",
194006f32e7eSjoerg          V);
194106f32e7eSjoerg   verifyParameterAttrs(RetAttrs, FT->getReturnType(), V);
194206f32e7eSjoerg 
194306f32e7eSjoerg   // Verify parameter attributes.
194406f32e7eSjoerg   for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
194506f32e7eSjoerg     Type *Ty = FT->getParamType(i);
194606f32e7eSjoerg     AttributeSet ArgAttrs = Attrs.getParamAttributes(i);
194706f32e7eSjoerg 
194806f32e7eSjoerg     if (!IsIntrinsic) {
194906f32e7eSjoerg       Assert(!ArgAttrs.hasAttribute(Attribute::ImmArg),
195006f32e7eSjoerg              "immarg attribute only applies to intrinsics",V);
195106f32e7eSjoerg     }
195206f32e7eSjoerg 
195306f32e7eSjoerg     verifyParameterAttrs(ArgAttrs, Ty, V);
195406f32e7eSjoerg 
195506f32e7eSjoerg     if (ArgAttrs.hasAttribute(Attribute::Nest)) {
195606f32e7eSjoerg       Assert(!SawNest, "More than one parameter has attribute nest!", V);
195706f32e7eSjoerg       SawNest = true;
195806f32e7eSjoerg     }
195906f32e7eSjoerg 
196006f32e7eSjoerg     if (ArgAttrs.hasAttribute(Attribute::Returned)) {
196106f32e7eSjoerg       Assert(!SawReturned, "More than one parameter has attribute returned!",
196206f32e7eSjoerg              V);
196306f32e7eSjoerg       Assert(Ty->canLosslesslyBitCastTo(FT->getReturnType()),
196406f32e7eSjoerg              "Incompatible argument and return types for 'returned' attribute",
196506f32e7eSjoerg              V);
196606f32e7eSjoerg       SawReturned = true;
196706f32e7eSjoerg     }
196806f32e7eSjoerg 
196906f32e7eSjoerg     if (ArgAttrs.hasAttribute(Attribute::StructRet)) {
197006f32e7eSjoerg       Assert(!SawSRet, "Cannot have multiple 'sret' parameters!", V);
197106f32e7eSjoerg       Assert(i == 0 || i == 1,
197206f32e7eSjoerg              "Attribute 'sret' is not on first or second parameter!", V);
197306f32e7eSjoerg       SawSRet = true;
197406f32e7eSjoerg     }
197506f32e7eSjoerg 
197606f32e7eSjoerg     if (ArgAttrs.hasAttribute(Attribute::SwiftSelf)) {
197706f32e7eSjoerg       Assert(!SawSwiftSelf, "Cannot have multiple 'swiftself' parameters!", V);
197806f32e7eSjoerg       SawSwiftSelf = true;
197906f32e7eSjoerg     }
198006f32e7eSjoerg 
1981*da58b97aSjoerg     if (ArgAttrs.hasAttribute(Attribute::SwiftAsync)) {
1982*da58b97aSjoerg       Assert(!SawSwiftAsync, "Cannot have multiple 'swiftasync' parameters!", V);
1983*da58b97aSjoerg       SawSwiftAsync = true;
1984*da58b97aSjoerg     }
1985*da58b97aSjoerg 
198606f32e7eSjoerg     if (ArgAttrs.hasAttribute(Attribute::SwiftError)) {
198706f32e7eSjoerg       Assert(!SawSwiftError, "Cannot have multiple 'swifterror' parameters!",
198806f32e7eSjoerg              V);
198906f32e7eSjoerg       SawSwiftError = true;
199006f32e7eSjoerg     }
199106f32e7eSjoerg 
199206f32e7eSjoerg     if (ArgAttrs.hasAttribute(Attribute::InAlloca)) {
199306f32e7eSjoerg       Assert(i == FT->getNumParams() - 1,
199406f32e7eSjoerg              "inalloca isn't on the last parameter!", V);
199506f32e7eSjoerg     }
199606f32e7eSjoerg   }
199706f32e7eSjoerg 
199806f32e7eSjoerg   if (!Attrs.hasAttributes(AttributeList::FunctionIndex))
199906f32e7eSjoerg     return;
200006f32e7eSjoerg 
200106f32e7eSjoerg   verifyAttributeTypes(Attrs.getFnAttributes(), /*IsFunction=*/true, V);
200206f32e7eSjoerg 
200306f32e7eSjoerg   Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
200406f32e7eSjoerg            Attrs.hasFnAttribute(Attribute::ReadOnly)),
200506f32e7eSjoerg          "Attributes 'readnone and readonly' are incompatible!", V);
200606f32e7eSjoerg 
200706f32e7eSjoerg   Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
200806f32e7eSjoerg            Attrs.hasFnAttribute(Attribute::WriteOnly)),
200906f32e7eSjoerg          "Attributes 'readnone and writeonly' are incompatible!", V);
201006f32e7eSjoerg 
201106f32e7eSjoerg   Assert(!(Attrs.hasFnAttribute(Attribute::ReadOnly) &&
201206f32e7eSjoerg            Attrs.hasFnAttribute(Attribute::WriteOnly)),
201306f32e7eSjoerg          "Attributes 'readonly and writeonly' are incompatible!", V);
201406f32e7eSjoerg 
201506f32e7eSjoerg   Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
201606f32e7eSjoerg            Attrs.hasFnAttribute(Attribute::InaccessibleMemOrArgMemOnly)),
201706f32e7eSjoerg          "Attributes 'readnone and inaccessiblemem_or_argmemonly' are "
201806f32e7eSjoerg          "incompatible!",
201906f32e7eSjoerg          V);
202006f32e7eSjoerg 
202106f32e7eSjoerg   Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
202206f32e7eSjoerg            Attrs.hasFnAttribute(Attribute::InaccessibleMemOnly)),
202306f32e7eSjoerg          "Attributes 'readnone and inaccessiblememonly' are incompatible!", V);
202406f32e7eSjoerg 
202506f32e7eSjoerg   Assert(!(Attrs.hasFnAttribute(Attribute::NoInline) &&
202606f32e7eSjoerg            Attrs.hasFnAttribute(Attribute::AlwaysInline)),
202706f32e7eSjoerg          "Attributes 'noinline and alwaysinline' are incompatible!", V);
202806f32e7eSjoerg 
202906f32e7eSjoerg   if (Attrs.hasFnAttribute(Attribute::OptimizeNone)) {
203006f32e7eSjoerg     Assert(Attrs.hasFnAttribute(Attribute::NoInline),
203106f32e7eSjoerg            "Attribute 'optnone' requires 'noinline'!", V);
203206f32e7eSjoerg 
203306f32e7eSjoerg     Assert(!Attrs.hasFnAttribute(Attribute::OptimizeForSize),
203406f32e7eSjoerg            "Attributes 'optsize and optnone' are incompatible!", V);
203506f32e7eSjoerg 
203606f32e7eSjoerg     Assert(!Attrs.hasFnAttribute(Attribute::MinSize),
203706f32e7eSjoerg            "Attributes 'minsize and optnone' are incompatible!", V);
203806f32e7eSjoerg   }
203906f32e7eSjoerg 
204006f32e7eSjoerg   if (Attrs.hasFnAttribute(Attribute::JumpTable)) {
204106f32e7eSjoerg     const GlobalValue *GV = cast<GlobalValue>(V);
204206f32e7eSjoerg     Assert(GV->hasGlobalUnnamedAddr(),
204306f32e7eSjoerg            "Attribute 'jumptable' requires 'unnamed_addr'", V);
204406f32e7eSjoerg   }
204506f32e7eSjoerg 
204606f32e7eSjoerg   if (Attrs.hasFnAttribute(Attribute::AllocSize)) {
204706f32e7eSjoerg     std::pair<unsigned, Optional<unsigned>> Args =
204806f32e7eSjoerg         Attrs.getAllocSizeArgs(AttributeList::FunctionIndex);
204906f32e7eSjoerg 
205006f32e7eSjoerg     auto CheckParam = [&](StringRef Name, unsigned ParamNo) {
205106f32e7eSjoerg       if (ParamNo >= FT->getNumParams()) {
205206f32e7eSjoerg         CheckFailed("'allocsize' " + Name + " argument is out of bounds", V);
205306f32e7eSjoerg         return false;
205406f32e7eSjoerg       }
205506f32e7eSjoerg 
205606f32e7eSjoerg       if (!FT->getParamType(ParamNo)->isIntegerTy()) {
205706f32e7eSjoerg         CheckFailed("'allocsize' " + Name +
205806f32e7eSjoerg                         " argument must refer to an integer parameter",
205906f32e7eSjoerg                     V);
206006f32e7eSjoerg         return false;
206106f32e7eSjoerg       }
206206f32e7eSjoerg 
206306f32e7eSjoerg       return true;
206406f32e7eSjoerg     };
206506f32e7eSjoerg 
206606f32e7eSjoerg     if (!CheckParam("element size", Args.first))
206706f32e7eSjoerg       return;
206806f32e7eSjoerg 
206906f32e7eSjoerg     if (Args.second && !CheckParam("number of elements", *Args.second))
207006f32e7eSjoerg       return;
207106f32e7eSjoerg   }
2072*da58b97aSjoerg 
2073*da58b97aSjoerg   if (Attrs.hasFnAttribute(Attribute::VScaleRange)) {
2074*da58b97aSjoerg     std::pair<unsigned, unsigned> Args =
2075*da58b97aSjoerg         Attrs.getVScaleRangeArgs(AttributeList::FunctionIndex);
2076*da58b97aSjoerg 
2077*da58b97aSjoerg     if (Args.first > Args.second && Args.second != 0)
2078*da58b97aSjoerg       CheckFailed("'vscale_range' minimum cannot be greater than maximum", V);
2079*da58b97aSjoerg   }
2080*da58b97aSjoerg 
2081*da58b97aSjoerg   if (Attrs.hasFnAttribute("frame-pointer")) {
2082*da58b97aSjoerg     StringRef FP = Attrs.getAttribute(AttributeList::FunctionIndex,
2083*da58b97aSjoerg                                       "frame-pointer").getValueAsString();
2084*da58b97aSjoerg     if (FP != "all" && FP != "non-leaf" && FP != "none")
2085*da58b97aSjoerg       CheckFailed("invalid value for 'frame-pointer' attribute: " + FP, V);
2086*da58b97aSjoerg   }
2087*da58b97aSjoerg 
2088*da58b97aSjoerg   if (Attrs.hasFnAttribute("patchable-function-prefix")) {
2089*da58b97aSjoerg     StringRef S = Attrs
2090*da58b97aSjoerg                       .getAttribute(AttributeList::FunctionIndex,
2091*da58b97aSjoerg                                     "patchable-function-prefix")
2092*da58b97aSjoerg                       .getValueAsString();
2093*da58b97aSjoerg     unsigned N;
2094*da58b97aSjoerg     if (S.getAsInteger(10, N))
2095*da58b97aSjoerg       CheckFailed(
2096*da58b97aSjoerg           "\"patchable-function-prefix\" takes an unsigned integer: " + S, V);
2097*da58b97aSjoerg   }
2098*da58b97aSjoerg   if (Attrs.hasFnAttribute("patchable-function-entry")) {
2099*da58b97aSjoerg     StringRef S = Attrs
2100*da58b97aSjoerg                       .getAttribute(AttributeList::FunctionIndex,
2101*da58b97aSjoerg                                     "patchable-function-entry")
2102*da58b97aSjoerg                       .getValueAsString();
2103*da58b97aSjoerg     unsigned N;
2104*da58b97aSjoerg     if (S.getAsInteger(10, N))
2105*da58b97aSjoerg       CheckFailed(
2106*da58b97aSjoerg           "\"patchable-function-entry\" takes an unsigned integer: " + S, V);
2107*da58b97aSjoerg   }
210806f32e7eSjoerg }
210906f32e7eSjoerg 
verifyFunctionMetadata(ArrayRef<std::pair<unsigned,MDNode * >> MDs)211006f32e7eSjoerg void Verifier::verifyFunctionMetadata(
211106f32e7eSjoerg     ArrayRef<std::pair<unsigned, MDNode *>> MDs) {
211206f32e7eSjoerg   for (const auto &Pair : MDs) {
211306f32e7eSjoerg     if (Pair.first == LLVMContext::MD_prof) {
211406f32e7eSjoerg       MDNode *MD = Pair.second;
211506f32e7eSjoerg       Assert(MD->getNumOperands() >= 2,
211606f32e7eSjoerg              "!prof annotations should have no less than 2 operands", MD);
211706f32e7eSjoerg 
211806f32e7eSjoerg       // Check first operand.
211906f32e7eSjoerg       Assert(MD->getOperand(0) != nullptr, "first operand should not be null",
212006f32e7eSjoerg              MD);
212106f32e7eSjoerg       Assert(isa<MDString>(MD->getOperand(0)),
212206f32e7eSjoerg              "expected string with name of the !prof annotation", MD);
212306f32e7eSjoerg       MDString *MDS = cast<MDString>(MD->getOperand(0));
212406f32e7eSjoerg       StringRef ProfName = MDS->getString();
212506f32e7eSjoerg       Assert(ProfName.equals("function_entry_count") ||
212606f32e7eSjoerg                  ProfName.equals("synthetic_function_entry_count"),
212706f32e7eSjoerg              "first operand should be 'function_entry_count'"
212806f32e7eSjoerg              " or 'synthetic_function_entry_count'",
212906f32e7eSjoerg              MD);
213006f32e7eSjoerg 
213106f32e7eSjoerg       // Check second operand.
213206f32e7eSjoerg       Assert(MD->getOperand(1) != nullptr, "second operand should not be null",
213306f32e7eSjoerg              MD);
213406f32e7eSjoerg       Assert(isa<ConstantAsMetadata>(MD->getOperand(1)),
213506f32e7eSjoerg              "expected integer argument to function_entry_count", MD);
213606f32e7eSjoerg     }
213706f32e7eSjoerg   }
213806f32e7eSjoerg }
213906f32e7eSjoerg 
visitConstantExprsRecursively(const Constant * EntryC)214006f32e7eSjoerg void Verifier::visitConstantExprsRecursively(const Constant *EntryC) {
214106f32e7eSjoerg   if (!ConstantExprVisited.insert(EntryC).second)
214206f32e7eSjoerg     return;
214306f32e7eSjoerg 
214406f32e7eSjoerg   SmallVector<const Constant *, 16> Stack;
214506f32e7eSjoerg   Stack.push_back(EntryC);
214606f32e7eSjoerg 
214706f32e7eSjoerg   while (!Stack.empty()) {
214806f32e7eSjoerg     const Constant *C = Stack.pop_back_val();
214906f32e7eSjoerg 
215006f32e7eSjoerg     // Check this constant expression.
215106f32e7eSjoerg     if (const auto *CE = dyn_cast<ConstantExpr>(C))
215206f32e7eSjoerg       visitConstantExpr(CE);
215306f32e7eSjoerg 
215406f32e7eSjoerg     if (const auto *GV = dyn_cast<GlobalValue>(C)) {
215506f32e7eSjoerg       // Global Values get visited separately, but we do need to make sure
215606f32e7eSjoerg       // that the global value is in the correct module
215706f32e7eSjoerg       Assert(GV->getParent() == &M, "Referencing global in another module!",
215806f32e7eSjoerg              EntryC, &M, GV, GV->getParent());
215906f32e7eSjoerg       continue;
216006f32e7eSjoerg     }
216106f32e7eSjoerg 
216206f32e7eSjoerg     // Visit all sub-expressions.
216306f32e7eSjoerg     for (const Use &U : C->operands()) {
216406f32e7eSjoerg       const auto *OpC = dyn_cast<Constant>(U);
216506f32e7eSjoerg       if (!OpC)
216606f32e7eSjoerg         continue;
216706f32e7eSjoerg       if (!ConstantExprVisited.insert(OpC).second)
216806f32e7eSjoerg         continue;
216906f32e7eSjoerg       Stack.push_back(OpC);
217006f32e7eSjoerg     }
217106f32e7eSjoerg   }
217206f32e7eSjoerg }
217306f32e7eSjoerg 
visitConstantExpr(const ConstantExpr * CE)217406f32e7eSjoerg void Verifier::visitConstantExpr(const ConstantExpr *CE) {
217506f32e7eSjoerg   if (CE->getOpcode() == Instruction::BitCast)
217606f32e7eSjoerg     Assert(CastInst::castIsValid(Instruction::BitCast, CE->getOperand(0),
217706f32e7eSjoerg                                  CE->getType()),
217806f32e7eSjoerg            "Invalid bitcast", CE);
217906f32e7eSjoerg 
218006f32e7eSjoerg   if (CE->getOpcode() == Instruction::IntToPtr ||
218106f32e7eSjoerg       CE->getOpcode() == Instruction::PtrToInt) {
218206f32e7eSjoerg     auto *PtrTy = CE->getOpcode() == Instruction::IntToPtr
218306f32e7eSjoerg                       ? CE->getType()
218406f32e7eSjoerg                       : CE->getOperand(0)->getType();
218506f32e7eSjoerg     StringRef Msg = CE->getOpcode() == Instruction::IntToPtr
218606f32e7eSjoerg                         ? "inttoptr not supported for non-integral pointers"
218706f32e7eSjoerg                         : "ptrtoint not supported for non-integral pointers";
218806f32e7eSjoerg     Assert(
218906f32e7eSjoerg         !DL.isNonIntegralPointerType(cast<PointerType>(PtrTy->getScalarType())),
219006f32e7eSjoerg         Msg);
219106f32e7eSjoerg   }
219206f32e7eSjoerg }
219306f32e7eSjoerg 
verifyAttributeCount(AttributeList Attrs,unsigned Params)219406f32e7eSjoerg bool Verifier::verifyAttributeCount(AttributeList Attrs, unsigned Params) {
219506f32e7eSjoerg   // There shouldn't be more attribute sets than there are parameters plus the
219606f32e7eSjoerg   // function and return value.
219706f32e7eSjoerg   return Attrs.getNumAttrSets() <= Params + 2;
219806f32e7eSjoerg }
219906f32e7eSjoerg 
220006f32e7eSjoerg /// Verify that statepoint intrinsic is well formed.
verifyStatepoint(const CallBase & Call)220106f32e7eSjoerg void Verifier::verifyStatepoint(const CallBase &Call) {
220206f32e7eSjoerg   assert(Call.getCalledFunction() &&
220306f32e7eSjoerg          Call.getCalledFunction()->getIntrinsicID() ==
220406f32e7eSjoerg              Intrinsic::experimental_gc_statepoint);
220506f32e7eSjoerg 
220606f32e7eSjoerg   Assert(!Call.doesNotAccessMemory() && !Call.onlyReadsMemory() &&
220706f32e7eSjoerg              !Call.onlyAccessesArgMemory(),
220806f32e7eSjoerg          "gc.statepoint must read and write all memory to preserve "
220906f32e7eSjoerg          "reordering restrictions required by safepoint semantics",
221006f32e7eSjoerg          Call);
221106f32e7eSjoerg 
221206f32e7eSjoerg   const int64_t NumPatchBytes =
221306f32e7eSjoerg       cast<ConstantInt>(Call.getArgOperand(1))->getSExtValue();
221406f32e7eSjoerg   assert(isInt<32>(NumPatchBytes) && "NumPatchBytesV is an i32!");
221506f32e7eSjoerg   Assert(NumPatchBytes >= 0,
221606f32e7eSjoerg          "gc.statepoint number of patchable bytes must be "
221706f32e7eSjoerg          "positive",
221806f32e7eSjoerg          Call);
221906f32e7eSjoerg 
222006f32e7eSjoerg   const Value *Target = Call.getArgOperand(2);
222106f32e7eSjoerg   auto *PT = dyn_cast<PointerType>(Target->getType());
222206f32e7eSjoerg   Assert(PT && PT->getElementType()->isFunctionTy(),
222306f32e7eSjoerg          "gc.statepoint callee must be of function pointer type", Call, Target);
222406f32e7eSjoerg   FunctionType *TargetFuncType = cast<FunctionType>(PT->getElementType());
222506f32e7eSjoerg 
222606f32e7eSjoerg   const int NumCallArgs = cast<ConstantInt>(Call.getArgOperand(3))->getZExtValue();
222706f32e7eSjoerg   Assert(NumCallArgs >= 0,
222806f32e7eSjoerg          "gc.statepoint number of arguments to underlying call "
222906f32e7eSjoerg          "must be positive",
223006f32e7eSjoerg          Call);
223106f32e7eSjoerg   const int NumParams = (int)TargetFuncType->getNumParams();
223206f32e7eSjoerg   if (TargetFuncType->isVarArg()) {
223306f32e7eSjoerg     Assert(NumCallArgs >= NumParams,
223406f32e7eSjoerg            "gc.statepoint mismatch in number of vararg call args", Call);
223506f32e7eSjoerg 
223606f32e7eSjoerg     // TODO: Remove this limitation
223706f32e7eSjoerg     Assert(TargetFuncType->getReturnType()->isVoidTy(),
223806f32e7eSjoerg            "gc.statepoint doesn't support wrapping non-void "
223906f32e7eSjoerg            "vararg functions yet",
224006f32e7eSjoerg            Call);
224106f32e7eSjoerg   } else
224206f32e7eSjoerg     Assert(NumCallArgs == NumParams,
224306f32e7eSjoerg            "gc.statepoint mismatch in number of call args", Call);
224406f32e7eSjoerg 
224506f32e7eSjoerg   const uint64_t Flags
224606f32e7eSjoerg     = cast<ConstantInt>(Call.getArgOperand(4))->getZExtValue();
224706f32e7eSjoerg   Assert((Flags & ~(uint64_t)StatepointFlags::MaskAll) == 0,
224806f32e7eSjoerg          "unknown flag used in gc.statepoint flags argument", Call);
224906f32e7eSjoerg 
225006f32e7eSjoerg   // Verify that the types of the call parameter arguments match
225106f32e7eSjoerg   // the type of the wrapped callee.
225206f32e7eSjoerg   AttributeList Attrs = Call.getAttributes();
225306f32e7eSjoerg   for (int i = 0; i < NumParams; i++) {
225406f32e7eSjoerg     Type *ParamType = TargetFuncType->getParamType(i);
225506f32e7eSjoerg     Type *ArgType = Call.getArgOperand(5 + i)->getType();
225606f32e7eSjoerg     Assert(ArgType == ParamType,
225706f32e7eSjoerg            "gc.statepoint call argument does not match wrapped "
225806f32e7eSjoerg            "function type",
225906f32e7eSjoerg            Call);
226006f32e7eSjoerg 
226106f32e7eSjoerg     if (TargetFuncType->isVarArg()) {
226206f32e7eSjoerg       AttributeSet ArgAttrs = Attrs.getParamAttributes(5 + i);
226306f32e7eSjoerg       Assert(!ArgAttrs.hasAttribute(Attribute::StructRet),
226406f32e7eSjoerg              "Attribute 'sret' cannot be used for vararg call arguments!",
226506f32e7eSjoerg              Call);
226606f32e7eSjoerg     }
226706f32e7eSjoerg   }
226806f32e7eSjoerg 
226906f32e7eSjoerg   const int EndCallArgsInx = 4 + NumCallArgs;
227006f32e7eSjoerg 
227106f32e7eSjoerg   const Value *NumTransitionArgsV = Call.getArgOperand(EndCallArgsInx + 1);
227206f32e7eSjoerg   Assert(isa<ConstantInt>(NumTransitionArgsV),
227306f32e7eSjoerg          "gc.statepoint number of transition arguments "
227406f32e7eSjoerg          "must be constant integer",
227506f32e7eSjoerg          Call);
227606f32e7eSjoerg   const int NumTransitionArgs =
227706f32e7eSjoerg       cast<ConstantInt>(NumTransitionArgsV)->getZExtValue();
2278*da58b97aSjoerg   Assert(NumTransitionArgs == 0,
2279*da58b97aSjoerg          "gc.statepoint w/inline transition bundle is deprecated", Call);
228006f32e7eSjoerg   const int EndTransitionArgsInx = EndCallArgsInx + 1 + NumTransitionArgs;
228106f32e7eSjoerg 
228206f32e7eSjoerg   const Value *NumDeoptArgsV = Call.getArgOperand(EndTransitionArgsInx + 1);
228306f32e7eSjoerg   Assert(isa<ConstantInt>(NumDeoptArgsV),
228406f32e7eSjoerg          "gc.statepoint number of deoptimization arguments "
228506f32e7eSjoerg          "must be constant integer",
228606f32e7eSjoerg          Call);
228706f32e7eSjoerg   const int NumDeoptArgs = cast<ConstantInt>(NumDeoptArgsV)->getZExtValue();
2288*da58b97aSjoerg   Assert(NumDeoptArgs == 0,
2289*da58b97aSjoerg          "gc.statepoint w/inline deopt operands is deprecated", Call);
229006f32e7eSjoerg 
2291*da58b97aSjoerg   const int ExpectedNumArgs = 7 + NumCallArgs;
2292*da58b97aSjoerg   Assert(ExpectedNumArgs == (int)Call.arg_size(),
2293*da58b97aSjoerg          "gc.statepoint too many arguments", Call);
229406f32e7eSjoerg 
229506f32e7eSjoerg   // Check that the only uses of this gc.statepoint are gc.result or
229606f32e7eSjoerg   // gc.relocate calls which are tied to this statepoint and thus part
229706f32e7eSjoerg   // of the same statepoint sequence
229806f32e7eSjoerg   for (const User *U : Call.users()) {
229906f32e7eSjoerg     const CallInst *UserCall = dyn_cast<const CallInst>(U);
230006f32e7eSjoerg     Assert(UserCall, "illegal use of statepoint token", Call, U);
230106f32e7eSjoerg     if (!UserCall)
230206f32e7eSjoerg       continue;
230306f32e7eSjoerg     Assert(isa<GCRelocateInst>(UserCall) || isa<GCResultInst>(UserCall),
230406f32e7eSjoerg            "gc.result or gc.relocate are the only value uses "
230506f32e7eSjoerg            "of a gc.statepoint",
230606f32e7eSjoerg            Call, U);
230706f32e7eSjoerg     if (isa<GCResultInst>(UserCall)) {
230806f32e7eSjoerg       Assert(UserCall->getArgOperand(0) == &Call,
230906f32e7eSjoerg              "gc.result connected to wrong gc.statepoint", Call, UserCall);
231006f32e7eSjoerg     } else if (isa<GCRelocateInst>(Call)) {
231106f32e7eSjoerg       Assert(UserCall->getArgOperand(0) == &Call,
231206f32e7eSjoerg              "gc.relocate connected to wrong gc.statepoint", Call, UserCall);
231306f32e7eSjoerg     }
231406f32e7eSjoerg   }
231506f32e7eSjoerg 
231606f32e7eSjoerg   // Note: It is legal for a single derived pointer to be listed multiple
231706f32e7eSjoerg   // times.  It's non-optimal, but it is legal.  It can also happen after
231806f32e7eSjoerg   // insertion if we strip a bitcast away.
231906f32e7eSjoerg   // Note: It is really tempting to check that each base is relocated and
232006f32e7eSjoerg   // that a derived pointer is never reused as a base pointer.  This turns
232106f32e7eSjoerg   // out to be problematic since optimizations run after safepoint insertion
232206f32e7eSjoerg   // can recognize equality properties that the insertion logic doesn't know
232306f32e7eSjoerg   // about.  See example statepoint.ll in the verifier subdirectory
232406f32e7eSjoerg }
232506f32e7eSjoerg 
verifyFrameRecoverIndices()232606f32e7eSjoerg void Verifier::verifyFrameRecoverIndices() {
232706f32e7eSjoerg   for (auto &Counts : FrameEscapeInfo) {
232806f32e7eSjoerg     Function *F = Counts.first;
232906f32e7eSjoerg     unsigned EscapedObjectCount = Counts.second.first;
233006f32e7eSjoerg     unsigned MaxRecoveredIndex = Counts.second.second;
233106f32e7eSjoerg     Assert(MaxRecoveredIndex <= EscapedObjectCount,
233206f32e7eSjoerg            "all indices passed to llvm.localrecover must be less than the "
233306f32e7eSjoerg            "number of arguments passed to llvm.localescape in the parent "
233406f32e7eSjoerg            "function",
233506f32e7eSjoerg            F);
233606f32e7eSjoerg   }
233706f32e7eSjoerg }
233806f32e7eSjoerg 
getSuccPad(Instruction * Terminator)233906f32e7eSjoerg static Instruction *getSuccPad(Instruction *Terminator) {
234006f32e7eSjoerg   BasicBlock *UnwindDest;
234106f32e7eSjoerg   if (auto *II = dyn_cast<InvokeInst>(Terminator))
234206f32e7eSjoerg     UnwindDest = II->getUnwindDest();
234306f32e7eSjoerg   else if (auto *CSI = dyn_cast<CatchSwitchInst>(Terminator))
234406f32e7eSjoerg     UnwindDest = CSI->getUnwindDest();
234506f32e7eSjoerg   else
234606f32e7eSjoerg     UnwindDest = cast<CleanupReturnInst>(Terminator)->getUnwindDest();
234706f32e7eSjoerg   return UnwindDest->getFirstNonPHI();
234806f32e7eSjoerg }
234906f32e7eSjoerg 
verifySiblingFuncletUnwinds()235006f32e7eSjoerg void Verifier::verifySiblingFuncletUnwinds() {
235106f32e7eSjoerg   SmallPtrSet<Instruction *, 8> Visited;
235206f32e7eSjoerg   SmallPtrSet<Instruction *, 8> Active;
235306f32e7eSjoerg   for (const auto &Pair : SiblingFuncletInfo) {
235406f32e7eSjoerg     Instruction *PredPad = Pair.first;
235506f32e7eSjoerg     if (Visited.count(PredPad))
235606f32e7eSjoerg       continue;
235706f32e7eSjoerg     Active.insert(PredPad);
235806f32e7eSjoerg     Instruction *Terminator = Pair.second;
235906f32e7eSjoerg     do {
236006f32e7eSjoerg       Instruction *SuccPad = getSuccPad(Terminator);
236106f32e7eSjoerg       if (Active.count(SuccPad)) {
236206f32e7eSjoerg         // Found a cycle; report error
236306f32e7eSjoerg         Instruction *CyclePad = SuccPad;
236406f32e7eSjoerg         SmallVector<Instruction *, 8> CycleNodes;
236506f32e7eSjoerg         do {
236606f32e7eSjoerg           CycleNodes.push_back(CyclePad);
236706f32e7eSjoerg           Instruction *CycleTerminator = SiblingFuncletInfo[CyclePad];
236806f32e7eSjoerg           if (CycleTerminator != CyclePad)
236906f32e7eSjoerg             CycleNodes.push_back(CycleTerminator);
237006f32e7eSjoerg           CyclePad = getSuccPad(CycleTerminator);
237106f32e7eSjoerg         } while (CyclePad != SuccPad);
237206f32e7eSjoerg         Assert(false, "EH pads can't handle each other's exceptions",
237306f32e7eSjoerg                ArrayRef<Instruction *>(CycleNodes));
237406f32e7eSjoerg       }
237506f32e7eSjoerg       // Don't re-walk a node we've already checked
237606f32e7eSjoerg       if (!Visited.insert(SuccPad).second)
237706f32e7eSjoerg         break;
237806f32e7eSjoerg       // Walk to this successor if it has a map entry.
237906f32e7eSjoerg       PredPad = SuccPad;
238006f32e7eSjoerg       auto TermI = SiblingFuncletInfo.find(PredPad);
238106f32e7eSjoerg       if (TermI == SiblingFuncletInfo.end())
238206f32e7eSjoerg         break;
238306f32e7eSjoerg       Terminator = TermI->second;
238406f32e7eSjoerg       Active.insert(PredPad);
238506f32e7eSjoerg     } while (true);
238606f32e7eSjoerg     // Each node only has one successor, so we've walked all the active
238706f32e7eSjoerg     // nodes' successors.
238806f32e7eSjoerg     Active.clear();
238906f32e7eSjoerg   }
239006f32e7eSjoerg }
239106f32e7eSjoerg 
239206f32e7eSjoerg // visitFunction - Verify that a function is ok.
239306f32e7eSjoerg //
visitFunction(const Function & F)239406f32e7eSjoerg void Verifier::visitFunction(const Function &F) {
239506f32e7eSjoerg   visitGlobalValue(F);
239606f32e7eSjoerg 
239706f32e7eSjoerg   // Check function arguments.
239806f32e7eSjoerg   FunctionType *FT = F.getFunctionType();
239906f32e7eSjoerg   unsigned NumArgs = F.arg_size();
240006f32e7eSjoerg 
240106f32e7eSjoerg   Assert(&Context == &F.getContext(),
240206f32e7eSjoerg          "Function context does not match Module context!", &F);
240306f32e7eSjoerg 
240406f32e7eSjoerg   Assert(!F.hasCommonLinkage(), "Functions may not have common linkage", &F);
240506f32e7eSjoerg   Assert(FT->getNumParams() == NumArgs,
240606f32e7eSjoerg          "# formal arguments must match # of arguments for function type!", &F,
240706f32e7eSjoerg          FT);
240806f32e7eSjoerg   Assert(F.getReturnType()->isFirstClassType() ||
240906f32e7eSjoerg              F.getReturnType()->isVoidTy() || F.getReturnType()->isStructTy(),
241006f32e7eSjoerg          "Functions cannot return aggregate values!", &F);
241106f32e7eSjoerg 
241206f32e7eSjoerg   Assert(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
241306f32e7eSjoerg          "Invalid struct return type!", &F);
241406f32e7eSjoerg 
241506f32e7eSjoerg   AttributeList Attrs = F.getAttributes();
241606f32e7eSjoerg 
241706f32e7eSjoerg   Assert(verifyAttributeCount(Attrs, FT->getNumParams()),
241806f32e7eSjoerg          "Attribute after last parameter!", &F);
241906f32e7eSjoerg 
242006f32e7eSjoerg   bool isLLVMdotName = F.getName().size() >= 5 &&
242106f32e7eSjoerg                        F.getName().substr(0, 5) == "llvm.";
242206f32e7eSjoerg 
242306f32e7eSjoerg   // Check function attributes.
242406f32e7eSjoerg   verifyFunctionAttrs(FT, Attrs, &F, isLLVMdotName);
242506f32e7eSjoerg 
242606f32e7eSjoerg   // On function declarations/definitions, we do not support the builtin
242706f32e7eSjoerg   // attribute. We do not check this in VerifyFunctionAttrs since that is
242806f32e7eSjoerg   // checking for Attributes that can/can not ever be on functions.
242906f32e7eSjoerg   Assert(!Attrs.hasFnAttribute(Attribute::Builtin),
243006f32e7eSjoerg          "Attribute 'builtin' can only be applied to a callsite.", &F);
243106f32e7eSjoerg 
243206f32e7eSjoerg   // Check that this function meets the restrictions on this calling convention.
243306f32e7eSjoerg   // Sometimes varargs is used for perfectly forwarding thunks, so some of these
243406f32e7eSjoerg   // restrictions can be lifted.
243506f32e7eSjoerg   switch (F.getCallingConv()) {
243606f32e7eSjoerg   default:
243706f32e7eSjoerg   case CallingConv::C:
243806f32e7eSjoerg     break;
2439*da58b97aSjoerg   case CallingConv::X86_INTR: {
2440*da58b97aSjoerg     Assert(F.arg_empty() || Attrs.hasParamAttribute(0, Attribute::ByVal),
2441*da58b97aSjoerg            "Calling convention parameter requires byval", &F);
2442*da58b97aSjoerg     break;
2443*da58b97aSjoerg   }
244406f32e7eSjoerg   case CallingConv::AMDGPU_KERNEL:
244506f32e7eSjoerg   case CallingConv::SPIR_KERNEL:
244606f32e7eSjoerg     Assert(F.getReturnType()->isVoidTy(),
244706f32e7eSjoerg            "Calling convention requires void return type", &F);
244806f32e7eSjoerg     LLVM_FALLTHROUGH;
244906f32e7eSjoerg   case CallingConv::AMDGPU_VS:
245006f32e7eSjoerg   case CallingConv::AMDGPU_HS:
245106f32e7eSjoerg   case CallingConv::AMDGPU_GS:
245206f32e7eSjoerg   case CallingConv::AMDGPU_PS:
245306f32e7eSjoerg   case CallingConv::AMDGPU_CS:
245406f32e7eSjoerg     Assert(!F.hasStructRetAttr(),
245506f32e7eSjoerg            "Calling convention does not allow sret", &F);
2456*da58b97aSjoerg     if (F.getCallingConv() != CallingConv::SPIR_KERNEL) {
2457*da58b97aSjoerg       const unsigned StackAS = DL.getAllocaAddrSpace();
2458*da58b97aSjoerg       unsigned i = 0;
2459*da58b97aSjoerg       for (const Argument &Arg : F.args()) {
2460*da58b97aSjoerg         Assert(!Attrs.hasParamAttribute(i, Attribute::ByVal),
2461*da58b97aSjoerg                "Calling convention disallows byval", &F);
2462*da58b97aSjoerg         Assert(!Attrs.hasParamAttribute(i, Attribute::Preallocated),
2463*da58b97aSjoerg                "Calling convention disallows preallocated", &F);
2464*da58b97aSjoerg         Assert(!Attrs.hasParamAttribute(i, Attribute::InAlloca),
2465*da58b97aSjoerg                "Calling convention disallows inalloca", &F);
2466*da58b97aSjoerg 
2467*da58b97aSjoerg         if (Attrs.hasParamAttribute(i, Attribute::ByRef)) {
2468*da58b97aSjoerg           // FIXME: Should also disallow LDS and GDS, but we don't have the enum
2469*da58b97aSjoerg           // value here.
2470*da58b97aSjoerg           Assert(Arg.getType()->getPointerAddressSpace() != StackAS,
2471*da58b97aSjoerg                  "Calling convention disallows stack byref", &F);
2472*da58b97aSjoerg         }
2473*da58b97aSjoerg 
2474*da58b97aSjoerg         ++i;
2475*da58b97aSjoerg       }
2476*da58b97aSjoerg     }
2477*da58b97aSjoerg 
247806f32e7eSjoerg     LLVM_FALLTHROUGH;
247906f32e7eSjoerg   case CallingConv::Fast:
248006f32e7eSjoerg   case CallingConv::Cold:
248106f32e7eSjoerg   case CallingConv::Intel_OCL_BI:
248206f32e7eSjoerg   case CallingConv::PTX_Kernel:
248306f32e7eSjoerg   case CallingConv::PTX_Device:
248406f32e7eSjoerg     Assert(!F.isVarArg(), "Calling convention does not support varargs or "
248506f32e7eSjoerg                           "perfect forwarding!",
248606f32e7eSjoerg            &F);
248706f32e7eSjoerg     break;
248806f32e7eSjoerg   }
248906f32e7eSjoerg 
249006f32e7eSjoerg   // Check that the argument values match the function type for this function...
249106f32e7eSjoerg   unsigned i = 0;
249206f32e7eSjoerg   for (const Argument &Arg : F.args()) {
249306f32e7eSjoerg     Assert(Arg.getType() == FT->getParamType(i),
249406f32e7eSjoerg            "Argument value does not match function argument type!", &Arg,
249506f32e7eSjoerg            FT->getParamType(i));
249606f32e7eSjoerg     Assert(Arg.getType()->isFirstClassType(),
249706f32e7eSjoerg            "Function arguments must have first-class types!", &Arg);
249806f32e7eSjoerg     if (!isLLVMdotName) {
249906f32e7eSjoerg       Assert(!Arg.getType()->isMetadataTy(),
250006f32e7eSjoerg              "Function takes metadata but isn't an intrinsic", &Arg, &F);
250106f32e7eSjoerg       Assert(!Arg.getType()->isTokenTy(),
250206f32e7eSjoerg              "Function takes token but isn't an intrinsic", &Arg, &F);
2503*da58b97aSjoerg       Assert(!Arg.getType()->isX86_AMXTy(),
2504*da58b97aSjoerg              "Function takes x86_amx but isn't an intrinsic", &Arg, &F);
250506f32e7eSjoerg     }
250606f32e7eSjoerg 
250706f32e7eSjoerg     // Check that swifterror argument is only used by loads and stores.
250806f32e7eSjoerg     if (Attrs.hasParamAttribute(i, Attribute::SwiftError)) {
250906f32e7eSjoerg       verifySwiftErrorValue(&Arg);
251006f32e7eSjoerg     }
251106f32e7eSjoerg     ++i;
251206f32e7eSjoerg   }
251306f32e7eSjoerg 
2514*da58b97aSjoerg   if (!isLLVMdotName) {
251506f32e7eSjoerg     Assert(!F.getReturnType()->isTokenTy(),
2516*da58b97aSjoerg            "Function returns a token but isn't an intrinsic", &F);
2517*da58b97aSjoerg     Assert(!F.getReturnType()->isX86_AMXTy(),
2518*da58b97aSjoerg            "Function returns a x86_amx but isn't an intrinsic", &F);
2519*da58b97aSjoerg   }
252006f32e7eSjoerg 
252106f32e7eSjoerg   // Get the function metadata attachments.
252206f32e7eSjoerg   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
252306f32e7eSjoerg   F.getAllMetadata(MDs);
252406f32e7eSjoerg   assert(F.hasMetadata() != MDs.empty() && "Bit out-of-sync");
252506f32e7eSjoerg   verifyFunctionMetadata(MDs);
252606f32e7eSjoerg 
252706f32e7eSjoerg   // Check validity of the personality function
252806f32e7eSjoerg   if (F.hasPersonalityFn()) {
252906f32e7eSjoerg     auto *Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
253006f32e7eSjoerg     if (Per)
253106f32e7eSjoerg       Assert(Per->getParent() == F.getParent(),
253206f32e7eSjoerg              "Referencing personality function in another module!",
253306f32e7eSjoerg              &F, F.getParent(), Per, Per->getParent());
253406f32e7eSjoerg   }
253506f32e7eSjoerg 
253606f32e7eSjoerg   if (F.isMaterializable()) {
253706f32e7eSjoerg     // Function has a body somewhere we can't see.
253806f32e7eSjoerg     Assert(MDs.empty(), "unmaterialized function cannot have metadata", &F,
253906f32e7eSjoerg            MDs.empty() ? nullptr : MDs.front().second);
254006f32e7eSjoerg   } else if (F.isDeclaration()) {
254106f32e7eSjoerg     for (const auto &I : MDs) {
254206f32e7eSjoerg       // This is used for call site debug information.
254306f32e7eSjoerg       AssertDI(I.first != LLVMContext::MD_dbg ||
254406f32e7eSjoerg                    !cast<DISubprogram>(I.second)->isDistinct(),
254506f32e7eSjoerg                "function declaration may only have a unique !dbg attachment",
254606f32e7eSjoerg                &F);
254706f32e7eSjoerg       Assert(I.first != LLVMContext::MD_prof,
254806f32e7eSjoerg              "function declaration may not have a !prof attachment", &F);
254906f32e7eSjoerg 
255006f32e7eSjoerg       // Verify the metadata itself.
2551*da58b97aSjoerg       visitMDNode(*I.second, AreDebugLocsAllowed::Yes);
255206f32e7eSjoerg     }
255306f32e7eSjoerg     Assert(!F.hasPersonalityFn(),
255406f32e7eSjoerg            "Function declaration shouldn't have a personality routine", &F);
255506f32e7eSjoerg   } else {
255606f32e7eSjoerg     // Verify that this function (which has a body) is not named "llvm.*".  It
255706f32e7eSjoerg     // is not legal to define intrinsics.
255806f32e7eSjoerg     Assert(!isLLVMdotName, "llvm intrinsics cannot be defined!", &F);
255906f32e7eSjoerg 
256006f32e7eSjoerg     // Check the entry node
256106f32e7eSjoerg     const BasicBlock *Entry = &F.getEntryBlock();
256206f32e7eSjoerg     Assert(pred_empty(Entry),
256306f32e7eSjoerg            "Entry block to function must not have predecessors!", Entry);
256406f32e7eSjoerg 
256506f32e7eSjoerg     // The address of the entry block cannot be taken, unless it is dead.
256606f32e7eSjoerg     if (Entry->hasAddressTaken()) {
256706f32e7eSjoerg       Assert(!BlockAddress::lookup(Entry)->isConstantUsed(),
256806f32e7eSjoerg              "blockaddress may not be used with the entry block!", Entry);
256906f32e7eSjoerg     }
257006f32e7eSjoerg 
257106f32e7eSjoerg     unsigned NumDebugAttachments = 0, NumProfAttachments = 0;
257206f32e7eSjoerg     // Visit metadata attachments.
257306f32e7eSjoerg     for (const auto &I : MDs) {
257406f32e7eSjoerg       // Verify that the attachment is legal.
2575*da58b97aSjoerg       auto AllowLocs = AreDebugLocsAllowed::No;
257606f32e7eSjoerg       switch (I.first) {
257706f32e7eSjoerg       default:
257806f32e7eSjoerg         break;
257906f32e7eSjoerg       case LLVMContext::MD_dbg: {
258006f32e7eSjoerg         ++NumDebugAttachments;
258106f32e7eSjoerg         AssertDI(NumDebugAttachments == 1,
258206f32e7eSjoerg                  "function must have a single !dbg attachment", &F, I.second);
258306f32e7eSjoerg         AssertDI(isa<DISubprogram>(I.second),
258406f32e7eSjoerg                  "function !dbg attachment must be a subprogram", &F, I.second);
2585*da58b97aSjoerg         AssertDI(cast<DISubprogram>(I.second)->isDistinct(),
2586*da58b97aSjoerg                  "function definition may only have a distinct !dbg attachment",
2587*da58b97aSjoerg                  &F);
2588*da58b97aSjoerg 
258906f32e7eSjoerg         auto *SP = cast<DISubprogram>(I.second);
259006f32e7eSjoerg         const Function *&AttachedTo = DISubprogramAttachments[SP];
259106f32e7eSjoerg         AssertDI(!AttachedTo || AttachedTo == &F,
259206f32e7eSjoerg                  "DISubprogram attached to more than one function", SP, &F);
259306f32e7eSjoerg         AttachedTo = &F;
2594*da58b97aSjoerg         AllowLocs = AreDebugLocsAllowed::Yes;
259506f32e7eSjoerg         break;
259606f32e7eSjoerg       }
259706f32e7eSjoerg       case LLVMContext::MD_prof:
259806f32e7eSjoerg         ++NumProfAttachments;
259906f32e7eSjoerg         Assert(NumProfAttachments == 1,
260006f32e7eSjoerg                "function must have a single !prof attachment", &F, I.second);
260106f32e7eSjoerg         break;
260206f32e7eSjoerg       }
260306f32e7eSjoerg 
260406f32e7eSjoerg       // Verify the metadata itself.
2605*da58b97aSjoerg       visitMDNode(*I.second, AllowLocs);
260606f32e7eSjoerg     }
260706f32e7eSjoerg   }
260806f32e7eSjoerg 
260906f32e7eSjoerg   // If this function is actually an intrinsic, verify that it is only used in
261006f32e7eSjoerg   // direct call/invokes, never having its "address taken".
261106f32e7eSjoerg   // Only do this if the module is materialized, otherwise we don't have all the
261206f32e7eSjoerg   // uses.
261306f32e7eSjoerg   if (F.getIntrinsicID() && F.getParent()->isMaterialized()) {
261406f32e7eSjoerg     const User *U;
261506f32e7eSjoerg     if (F.hasAddressTaken(&U))
261606f32e7eSjoerg       Assert(false, "Invalid user of intrinsic instruction!", U);
261706f32e7eSjoerg   }
261806f32e7eSjoerg 
261906f32e7eSjoerg   auto *N = F.getSubprogram();
262006f32e7eSjoerg   HasDebugInfo = (N != nullptr);
262106f32e7eSjoerg   if (!HasDebugInfo)
262206f32e7eSjoerg     return;
262306f32e7eSjoerg 
2624*da58b97aSjoerg   // Check that all !dbg attachments lead to back to N.
262506f32e7eSjoerg   //
262606f32e7eSjoerg   // FIXME: Check this incrementally while visiting !dbg attachments.
262706f32e7eSjoerg   // FIXME: Only check when N is the canonical subprogram for F.
262806f32e7eSjoerg   SmallPtrSet<const MDNode *, 32> Seen;
262906f32e7eSjoerg   auto VisitDebugLoc = [&](const Instruction &I, const MDNode *Node) {
263006f32e7eSjoerg     // Be careful about using DILocation here since we might be dealing with
263106f32e7eSjoerg     // broken code (this is the Verifier after all).
263206f32e7eSjoerg     const DILocation *DL = dyn_cast_or_null<DILocation>(Node);
263306f32e7eSjoerg     if (!DL)
263406f32e7eSjoerg       return;
263506f32e7eSjoerg     if (!Seen.insert(DL).second)
263606f32e7eSjoerg       return;
263706f32e7eSjoerg 
263806f32e7eSjoerg     Metadata *Parent = DL->getRawScope();
263906f32e7eSjoerg     AssertDI(Parent && isa<DILocalScope>(Parent),
264006f32e7eSjoerg              "DILocation's scope must be a DILocalScope", N, &F, &I, DL,
264106f32e7eSjoerg              Parent);
2642*da58b97aSjoerg 
264306f32e7eSjoerg     DILocalScope *Scope = DL->getInlinedAtScope();
2644*da58b97aSjoerg     Assert(Scope, "Failed to find DILocalScope", DL);
2645*da58b97aSjoerg 
2646*da58b97aSjoerg     if (!Seen.insert(Scope).second)
264706f32e7eSjoerg       return;
264806f32e7eSjoerg 
2649*da58b97aSjoerg     DISubprogram *SP = Scope->getSubprogram();
265006f32e7eSjoerg 
265106f32e7eSjoerg     // Scope and SP could be the same MDNode and we don't want to skip
265206f32e7eSjoerg     // validation in that case
265306f32e7eSjoerg     if (SP && ((Scope != SP) && !Seen.insert(SP).second))
265406f32e7eSjoerg       return;
265506f32e7eSjoerg 
265606f32e7eSjoerg     AssertDI(SP->describes(&F),
265706f32e7eSjoerg              "!dbg attachment points at wrong subprogram for function", N, &F,
265806f32e7eSjoerg              &I, DL, Scope, SP);
265906f32e7eSjoerg   };
266006f32e7eSjoerg   for (auto &BB : F)
266106f32e7eSjoerg     for (auto &I : BB) {
266206f32e7eSjoerg       VisitDebugLoc(I, I.getDebugLoc().getAsMDNode());
266306f32e7eSjoerg       // The llvm.loop annotations also contain two DILocations.
266406f32e7eSjoerg       if (auto MD = I.getMetadata(LLVMContext::MD_loop))
266506f32e7eSjoerg         for (unsigned i = 1; i < MD->getNumOperands(); ++i)
266606f32e7eSjoerg           VisitDebugLoc(I, dyn_cast_or_null<MDNode>(MD->getOperand(i)));
266706f32e7eSjoerg       if (BrokenDebugInfo)
266806f32e7eSjoerg         return;
266906f32e7eSjoerg     }
267006f32e7eSjoerg }
267106f32e7eSjoerg 
267206f32e7eSjoerg // verifyBasicBlock - Verify that a basic block is well formed...
267306f32e7eSjoerg //
visitBasicBlock(BasicBlock & BB)267406f32e7eSjoerg void Verifier::visitBasicBlock(BasicBlock &BB) {
267506f32e7eSjoerg   InstsInThisBlock.clear();
267606f32e7eSjoerg 
267706f32e7eSjoerg   // Ensure that basic blocks have terminators!
267806f32e7eSjoerg   Assert(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
267906f32e7eSjoerg 
268006f32e7eSjoerg   // Check constraints that this basic block imposes on all of the PHI nodes in
268106f32e7eSjoerg   // it.
268206f32e7eSjoerg   if (isa<PHINode>(BB.front())) {
2683*da58b97aSjoerg     SmallVector<BasicBlock *, 8> Preds(predecessors(&BB));
268406f32e7eSjoerg     SmallVector<std::pair<BasicBlock*, Value*>, 8> Values;
268506f32e7eSjoerg     llvm::sort(Preds);
268606f32e7eSjoerg     for (const PHINode &PN : BB.phis()) {
268706f32e7eSjoerg       Assert(PN.getNumIncomingValues() == Preds.size(),
268806f32e7eSjoerg              "PHINode should have one entry for each predecessor of its "
268906f32e7eSjoerg              "parent basic block!",
269006f32e7eSjoerg              &PN);
269106f32e7eSjoerg 
269206f32e7eSjoerg       // Get and sort all incoming values in the PHI node...
269306f32e7eSjoerg       Values.clear();
269406f32e7eSjoerg       Values.reserve(PN.getNumIncomingValues());
269506f32e7eSjoerg       for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
269606f32e7eSjoerg         Values.push_back(
269706f32e7eSjoerg             std::make_pair(PN.getIncomingBlock(i), PN.getIncomingValue(i)));
269806f32e7eSjoerg       llvm::sort(Values);
269906f32e7eSjoerg 
270006f32e7eSjoerg       for (unsigned i = 0, e = Values.size(); i != e; ++i) {
270106f32e7eSjoerg         // Check to make sure that if there is more than one entry for a
270206f32e7eSjoerg         // particular basic block in this PHI node, that the incoming values are
270306f32e7eSjoerg         // all identical.
270406f32e7eSjoerg         //
270506f32e7eSjoerg         Assert(i == 0 || Values[i].first != Values[i - 1].first ||
270606f32e7eSjoerg                    Values[i].second == Values[i - 1].second,
270706f32e7eSjoerg                "PHI node has multiple entries for the same basic block with "
270806f32e7eSjoerg                "different incoming values!",
270906f32e7eSjoerg                &PN, Values[i].first, Values[i].second, Values[i - 1].second);
271006f32e7eSjoerg 
271106f32e7eSjoerg         // Check to make sure that the predecessors and PHI node entries are
271206f32e7eSjoerg         // matched up.
271306f32e7eSjoerg         Assert(Values[i].first == Preds[i],
271406f32e7eSjoerg                "PHI node entries do not match predecessors!", &PN,
271506f32e7eSjoerg                Values[i].first, Preds[i]);
271606f32e7eSjoerg       }
271706f32e7eSjoerg     }
271806f32e7eSjoerg   }
271906f32e7eSjoerg 
272006f32e7eSjoerg   // Check that all instructions have their parent pointers set up correctly.
272106f32e7eSjoerg   for (auto &I : BB)
272206f32e7eSjoerg   {
272306f32e7eSjoerg     Assert(I.getParent() == &BB, "Instruction has bogus parent pointer!");
272406f32e7eSjoerg   }
272506f32e7eSjoerg }
272606f32e7eSjoerg 
visitTerminator(Instruction & I)272706f32e7eSjoerg void Verifier::visitTerminator(Instruction &I) {
272806f32e7eSjoerg   // Ensure that terminators only exist at the end of the basic block.
272906f32e7eSjoerg   Assert(&I == I.getParent()->getTerminator(),
273006f32e7eSjoerg          "Terminator found in the middle of a basic block!", I.getParent());
273106f32e7eSjoerg   visitInstruction(I);
273206f32e7eSjoerg }
273306f32e7eSjoerg 
visitBranchInst(BranchInst & BI)273406f32e7eSjoerg void Verifier::visitBranchInst(BranchInst &BI) {
273506f32e7eSjoerg   if (BI.isConditional()) {
273606f32e7eSjoerg     Assert(BI.getCondition()->getType()->isIntegerTy(1),
273706f32e7eSjoerg            "Branch condition is not 'i1' type!", &BI, BI.getCondition());
273806f32e7eSjoerg   }
273906f32e7eSjoerg   visitTerminator(BI);
274006f32e7eSjoerg }
274106f32e7eSjoerg 
visitReturnInst(ReturnInst & RI)274206f32e7eSjoerg void Verifier::visitReturnInst(ReturnInst &RI) {
274306f32e7eSjoerg   Function *F = RI.getParent()->getParent();
274406f32e7eSjoerg   unsigned N = RI.getNumOperands();
274506f32e7eSjoerg   if (F->getReturnType()->isVoidTy())
274606f32e7eSjoerg     Assert(N == 0,
274706f32e7eSjoerg            "Found return instr that returns non-void in Function of void "
274806f32e7eSjoerg            "return type!",
274906f32e7eSjoerg            &RI, F->getReturnType());
275006f32e7eSjoerg   else
275106f32e7eSjoerg     Assert(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(),
275206f32e7eSjoerg            "Function return type does not match operand "
275306f32e7eSjoerg            "type of return inst!",
275406f32e7eSjoerg            &RI, F->getReturnType());
275506f32e7eSjoerg 
275606f32e7eSjoerg   // Check to make sure that the return value has necessary properties for
275706f32e7eSjoerg   // terminators...
275806f32e7eSjoerg   visitTerminator(RI);
275906f32e7eSjoerg }
276006f32e7eSjoerg 
visitSwitchInst(SwitchInst & SI)276106f32e7eSjoerg void Verifier::visitSwitchInst(SwitchInst &SI) {
276206f32e7eSjoerg   // Check to make sure that all of the constants in the switch instruction
276306f32e7eSjoerg   // have the same type as the switched-on value.
276406f32e7eSjoerg   Type *SwitchTy = SI.getCondition()->getType();
276506f32e7eSjoerg   SmallPtrSet<ConstantInt*, 32> Constants;
276606f32e7eSjoerg   for (auto &Case : SI.cases()) {
276706f32e7eSjoerg     Assert(Case.getCaseValue()->getType() == SwitchTy,
276806f32e7eSjoerg            "Switch constants must all be same type as switch value!", &SI);
276906f32e7eSjoerg     Assert(Constants.insert(Case.getCaseValue()).second,
277006f32e7eSjoerg            "Duplicate integer as switch case", &SI, Case.getCaseValue());
277106f32e7eSjoerg   }
277206f32e7eSjoerg 
277306f32e7eSjoerg   visitTerminator(SI);
277406f32e7eSjoerg }
277506f32e7eSjoerg 
visitIndirectBrInst(IndirectBrInst & BI)277606f32e7eSjoerg void Verifier::visitIndirectBrInst(IndirectBrInst &BI) {
277706f32e7eSjoerg   Assert(BI.getAddress()->getType()->isPointerTy(),
277806f32e7eSjoerg          "Indirectbr operand must have pointer type!", &BI);
277906f32e7eSjoerg   for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i)
278006f32e7eSjoerg     Assert(BI.getDestination(i)->getType()->isLabelTy(),
278106f32e7eSjoerg            "Indirectbr destinations must all have pointer type!", &BI);
278206f32e7eSjoerg 
278306f32e7eSjoerg   visitTerminator(BI);
278406f32e7eSjoerg }
278506f32e7eSjoerg 
visitCallBrInst(CallBrInst & CBI)278606f32e7eSjoerg void Verifier::visitCallBrInst(CallBrInst &CBI) {
278706f32e7eSjoerg   Assert(CBI.isInlineAsm(), "Callbr is currently only used for asm-goto!",
278806f32e7eSjoerg          &CBI);
2789*da58b97aSjoerg   const InlineAsm *IA = cast<InlineAsm>(CBI.getCalledOperand());
2790*da58b97aSjoerg   Assert(!IA->canThrow(), "Unwinding from Callbr is not allowed");
279106f32e7eSjoerg   for (unsigned i = 0, e = CBI.getNumSuccessors(); i != e; ++i)
279206f32e7eSjoerg     Assert(CBI.getSuccessor(i)->getType()->isLabelTy(),
279306f32e7eSjoerg            "Callbr successors must all have pointer type!", &CBI);
279406f32e7eSjoerg   for (unsigned i = 0, e = CBI.getNumOperands(); i != e; ++i) {
279506f32e7eSjoerg     Assert(i >= CBI.getNumArgOperands() || !isa<BasicBlock>(CBI.getOperand(i)),
279606f32e7eSjoerg            "Using an unescaped label as a callbr argument!", &CBI);
279706f32e7eSjoerg     if (isa<BasicBlock>(CBI.getOperand(i)))
279806f32e7eSjoerg       for (unsigned j = i + 1; j != e; ++j)
279906f32e7eSjoerg         Assert(CBI.getOperand(i) != CBI.getOperand(j),
280006f32e7eSjoerg                "Duplicate callbr destination!", &CBI);
280106f32e7eSjoerg   }
280206f32e7eSjoerg   {
280306f32e7eSjoerg     SmallPtrSet<BasicBlock *, 4> ArgBBs;
280406f32e7eSjoerg     for (Value *V : CBI.args())
280506f32e7eSjoerg       if (auto *BA = dyn_cast<BlockAddress>(V))
280606f32e7eSjoerg         ArgBBs.insert(BA->getBasicBlock());
280706f32e7eSjoerg     for (BasicBlock *BB : CBI.getIndirectDests())
2808*da58b97aSjoerg       Assert(ArgBBs.count(BB), "Indirect label missing from arglist.", &CBI);
280906f32e7eSjoerg   }
281006f32e7eSjoerg 
281106f32e7eSjoerg   visitTerminator(CBI);
281206f32e7eSjoerg }
281306f32e7eSjoerg 
visitSelectInst(SelectInst & SI)281406f32e7eSjoerg void Verifier::visitSelectInst(SelectInst &SI) {
281506f32e7eSjoerg   Assert(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1),
281606f32e7eSjoerg                                          SI.getOperand(2)),
281706f32e7eSjoerg          "Invalid operands for select instruction!", &SI);
281806f32e7eSjoerg 
281906f32e7eSjoerg   Assert(SI.getTrueValue()->getType() == SI.getType(),
282006f32e7eSjoerg          "Select values must have same type as select instruction!", &SI);
282106f32e7eSjoerg   visitInstruction(SI);
282206f32e7eSjoerg }
282306f32e7eSjoerg 
282406f32e7eSjoerg /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
282506f32e7eSjoerg /// a pass, if any exist, it's an error.
282606f32e7eSjoerg ///
visitUserOp1(Instruction & I)282706f32e7eSjoerg void Verifier::visitUserOp1(Instruction &I) {
282806f32e7eSjoerg   Assert(false, "User-defined operators should not live outside of a pass!", &I);
282906f32e7eSjoerg }
283006f32e7eSjoerg 
visitTruncInst(TruncInst & I)283106f32e7eSjoerg void Verifier::visitTruncInst(TruncInst &I) {
283206f32e7eSjoerg   // Get the source and destination types
283306f32e7eSjoerg   Type *SrcTy = I.getOperand(0)->getType();
283406f32e7eSjoerg   Type *DestTy = I.getType();
283506f32e7eSjoerg 
283606f32e7eSjoerg   // Get the size of the types in bits, we'll need this later
283706f32e7eSjoerg   unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
283806f32e7eSjoerg   unsigned DestBitSize = DestTy->getScalarSizeInBits();
283906f32e7eSjoerg 
284006f32e7eSjoerg   Assert(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I);
284106f32e7eSjoerg   Assert(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I);
284206f32e7eSjoerg   Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
284306f32e7eSjoerg          "trunc source and destination must both be a vector or neither", &I);
284406f32e7eSjoerg   Assert(SrcBitSize > DestBitSize, "DestTy too big for Trunc", &I);
284506f32e7eSjoerg 
284606f32e7eSjoerg   visitInstruction(I);
284706f32e7eSjoerg }
284806f32e7eSjoerg 
visitZExtInst(ZExtInst & I)284906f32e7eSjoerg void Verifier::visitZExtInst(ZExtInst &I) {
285006f32e7eSjoerg   // Get the source and destination types
285106f32e7eSjoerg   Type *SrcTy = I.getOperand(0)->getType();
285206f32e7eSjoerg   Type *DestTy = I.getType();
285306f32e7eSjoerg 
285406f32e7eSjoerg   // Get the size of the types in bits, we'll need this later
285506f32e7eSjoerg   Assert(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I);
285606f32e7eSjoerg   Assert(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I);
285706f32e7eSjoerg   Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
285806f32e7eSjoerg          "zext source and destination must both be a vector or neither", &I);
285906f32e7eSjoerg   unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
286006f32e7eSjoerg   unsigned DestBitSize = DestTy->getScalarSizeInBits();
286106f32e7eSjoerg 
286206f32e7eSjoerg   Assert(SrcBitSize < DestBitSize, "Type too small for ZExt", &I);
286306f32e7eSjoerg 
286406f32e7eSjoerg   visitInstruction(I);
286506f32e7eSjoerg }
286606f32e7eSjoerg 
visitSExtInst(SExtInst & I)286706f32e7eSjoerg void Verifier::visitSExtInst(SExtInst &I) {
286806f32e7eSjoerg   // Get the source and destination types
286906f32e7eSjoerg   Type *SrcTy = I.getOperand(0)->getType();
287006f32e7eSjoerg   Type *DestTy = I.getType();
287106f32e7eSjoerg 
287206f32e7eSjoerg   // Get the size of the types in bits, we'll need this later
287306f32e7eSjoerg   unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
287406f32e7eSjoerg   unsigned DestBitSize = DestTy->getScalarSizeInBits();
287506f32e7eSjoerg 
287606f32e7eSjoerg   Assert(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I);
287706f32e7eSjoerg   Assert(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I);
287806f32e7eSjoerg   Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
287906f32e7eSjoerg          "sext source and destination must both be a vector or neither", &I);
288006f32e7eSjoerg   Assert(SrcBitSize < DestBitSize, "Type too small for SExt", &I);
288106f32e7eSjoerg 
288206f32e7eSjoerg   visitInstruction(I);
288306f32e7eSjoerg }
288406f32e7eSjoerg 
visitFPTruncInst(FPTruncInst & I)288506f32e7eSjoerg void Verifier::visitFPTruncInst(FPTruncInst &I) {
288606f32e7eSjoerg   // Get the source and destination types
288706f32e7eSjoerg   Type *SrcTy = I.getOperand(0)->getType();
288806f32e7eSjoerg   Type *DestTy = I.getType();
288906f32e7eSjoerg   // Get the size of the types in bits, we'll need this later
289006f32e7eSjoerg   unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
289106f32e7eSjoerg   unsigned DestBitSize = DestTy->getScalarSizeInBits();
289206f32e7eSjoerg 
289306f32e7eSjoerg   Assert(SrcTy->isFPOrFPVectorTy(), "FPTrunc only operates on FP", &I);
289406f32e7eSjoerg   Assert(DestTy->isFPOrFPVectorTy(), "FPTrunc only produces an FP", &I);
289506f32e7eSjoerg   Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
289606f32e7eSjoerg          "fptrunc source and destination must both be a vector or neither", &I);
289706f32e7eSjoerg   Assert(SrcBitSize > DestBitSize, "DestTy too big for FPTrunc", &I);
289806f32e7eSjoerg 
289906f32e7eSjoerg   visitInstruction(I);
290006f32e7eSjoerg }
290106f32e7eSjoerg 
visitFPExtInst(FPExtInst & I)290206f32e7eSjoerg void Verifier::visitFPExtInst(FPExtInst &I) {
290306f32e7eSjoerg   // Get the source and destination types
290406f32e7eSjoerg   Type *SrcTy = I.getOperand(0)->getType();
290506f32e7eSjoerg   Type *DestTy = I.getType();
290606f32e7eSjoerg 
290706f32e7eSjoerg   // Get the size of the types in bits, we'll need this later
290806f32e7eSjoerg   unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
290906f32e7eSjoerg   unsigned DestBitSize = DestTy->getScalarSizeInBits();
291006f32e7eSjoerg 
291106f32e7eSjoerg   Assert(SrcTy->isFPOrFPVectorTy(), "FPExt only operates on FP", &I);
291206f32e7eSjoerg   Assert(DestTy->isFPOrFPVectorTy(), "FPExt only produces an FP", &I);
291306f32e7eSjoerg   Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
291406f32e7eSjoerg          "fpext source and destination must both be a vector or neither", &I);
291506f32e7eSjoerg   Assert(SrcBitSize < DestBitSize, "DestTy too small for FPExt", &I);
291606f32e7eSjoerg 
291706f32e7eSjoerg   visitInstruction(I);
291806f32e7eSjoerg }
291906f32e7eSjoerg 
visitUIToFPInst(UIToFPInst & I)292006f32e7eSjoerg void Verifier::visitUIToFPInst(UIToFPInst &I) {
292106f32e7eSjoerg   // Get the source and destination types
292206f32e7eSjoerg   Type *SrcTy = I.getOperand(0)->getType();
292306f32e7eSjoerg   Type *DestTy = I.getType();
292406f32e7eSjoerg 
292506f32e7eSjoerg   bool SrcVec = SrcTy->isVectorTy();
292606f32e7eSjoerg   bool DstVec = DestTy->isVectorTy();
292706f32e7eSjoerg 
292806f32e7eSjoerg   Assert(SrcVec == DstVec,
292906f32e7eSjoerg          "UIToFP source and dest must both be vector or scalar", &I);
293006f32e7eSjoerg   Assert(SrcTy->isIntOrIntVectorTy(),
293106f32e7eSjoerg          "UIToFP source must be integer or integer vector", &I);
293206f32e7eSjoerg   Assert(DestTy->isFPOrFPVectorTy(), "UIToFP result must be FP or FP vector",
293306f32e7eSjoerg          &I);
293406f32e7eSjoerg 
293506f32e7eSjoerg   if (SrcVec && DstVec)
2936*da58b97aSjoerg     Assert(cast<VectorType>(SrcTy)->getElementCount() ==
2937*da58b97aSjoerg                cast<VectorType>(DestTy)->getElementCount(),
293806f32e7eSjoerg            "UIToFP source and dest vector length mismatch", &I);
293906f32e7eSjoerg 
294006f32e7eSjoerg   visitInstruction(I);
294106f32e7eSjoerg }
294206f32e7eSjoerg 
visitSIToFPInst(SIToFPInst & I)294306f32e7eSjoerg void Verifier::visitSIToFPInst(SIToFPInst &I) {
294406f32e7eSjoerg   // Get the source and destination types
294506f32e7eSjoerg   Type *SrcTy = I.getOperand(0)->getType();
294606f32e7eSjoerg   Type *DestTy = I.getType();
294706f32e7eSjoerg 
294806f32e7eSjoerg   bool SrcVec = SrcTy->isVectorTy();
294906f32e7eSjoerg   bool DstVec = DestTy->isVectorTy();
295006f32e7eSjoerg 
295106f32e7eSjoerg   Assert(SrcVec == DstVec,
295206f32e7eSjoerg          "SIToFP source and dest must both be vector or scalar", &I);
295306f32e7eSjoerg   Assert(SrcTy->isIntOrIntVectorTy(),
295406f32e7eSjoerg          "SIToFP source must be integer or integer vector", &I);
295506f32e7eSjoerg   Assert(DestTy->isFPOrFPVectorTy(), "SIToFP result must be FP or FP vector",
295606f32e7eSjoerg          &I);
295706f32e7eSjoerg 
295806f32e7eSjoerg   if (SrcVec && DstVec)
2959*da58b97aSjoerg     Assert(cast<VectorType>(SrcTy)->getElementCount() ==
2960*da58b97aSjoerg                cast<VectorType>(DestTy)->getElementCount(),
296106f32e7eSjoerg            "SIToFP source and dest vector length mismatch", &I);
296206f32e7eSjoerg 
296306f32e7eSjoerg   visitInstruction(I);
296406f32e7eSjoerg }
296506f32e7eSjoerg 
visitFPToUIInst(FPToUIInst & I)296606f32e7eSjoerg void Verifier::visitFPToUIInst(FPToUIInst &I) {
296706f32e7eSjoerg   // Get the source and destination types
296806f32e7eSjoerg   Type *SrcTy = I.getOperand(0)->getType();
296906f32e7eSjoerg   Type *DestTy = I.getType();
297006f32e7eSjoerg 
297106f32e7eSjoerg   bool SrcVec = SrcTy->isVectorTy();
297206f32e7eSjoerg   bool DstVec = DestTy->isVectorTy();
297306f32e7eSjoerg 
297406f32e7eSjoerg   Assert(SrcVec == DstVec,
297506f32e7eSjoerg          "FPToUI source and dest must both be vector or scalar", &I);
297606f32e7eSjoerg   Assert(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector",
297706f32e7eSjoerg          &I);
297806f32e7eSjoerg   Assert(DestTy->isIntOrIntVectorTy(),
297906f32e7eSjoerg          "FPToUI result must be integer or integer vector", &I);
298006f32e7eSjoerg 
298106f32e7eSjoerg   if (SrcVec && DstVec)
2982*da58b97aSjoerg     Assert(cast<VectorType>(SrcTy)->getElementCount() ==
2983*da58b97aSjoerg                cast<VectorType>(DestTy)->getElementCount(),
298406f32e7eSjoerg            "FPToUI source and dest vector length mismatch", &I);
298506f32e7eSjoerg 
298606f32e7eSjoerg   visitInstruction(I);
298706f32e7eSjoerg }
298806f32e7eSjoerg 
visitFPToSIInst(FPToSIInst & I)298906f32e7eSjoerg void Verifier::visitFPToSIInst(FPToSIInst &I) {
299006f32e7eSjoerg   // Get the source and destination types
299106f32e7eSjoerg   Type *SrcTy = I.getOperand(0)->getType();
299206f32e7eSjoerg   Type *DestTy = I.getType();
299306f32e7eSjoerg 
299406f32e7eSjoerg   bool SrcVec = SrcTy->isVectorTy();
299506f32e7eSjoerg   bool DstVec = DestTy->isVectorTy();
299606f32e7eSjoerg 
299706f32e7eSjoerg   Assert(SrcVec == DstVec,
299806f32e7eSjoerg          "FPToSI source and dest must both be vector or scalar", &I);
299906f32e7eSjoerg   Assert(SrcTy->isFPOrFPVectorTy(), "FPToSI source must be FP or FP vector",
300006f32e7eSjoerg          &I);
300106f32e7eSjoerg   Assert(DestTy->isIntOrIntVectorTy(),
300206f32e7eSjoerg          "FPToSI result must be integer or integer vector", &I);
300306f32e7eSjoerg 
300406f32e7eSjoerg   if (SrcVec && DstVec)
3005*da58b97aSjoerg     Assert(cast<VectorType>(SrcTy)->getElementCount() ==
3006*da58b97aSjoerg                cast<VectorType>(DestTy)->getElementCount(),
300706f32e7eSjoerg            "FPToSI source and dest vector length mismatch", &I);
300806f32e7eSjoerg 
300906f32e7eSjoerg   visitInstruction(I);
301006f32e7eSjoerg }
301106f32e7eSjoerg 
visitPtrToIntInst(PtrToIntInst & I)301206f32e7eSjoerg void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
301306f32e7eSjoerg   // Get the source and destination types
301406f32e7eSjoerg   Type *SrcTy = I.getOperand(0)->getType();
301506f32e7eSjoerg   Type *DestTy = I.getType();
301606f32e7eSjoerg 
301706f32e7eSjoerg   Assert(SrcTy->isPtrOrPtrVectorTy(), "PtrToInt source must be pointer", &I);
301806f32e7eSjoerg 
301906f32e7eSjoerg   if (auto *PTy = dyn_cast<PointerType>(SrcTy->getScalarType()))
302006f32e7eSjoerg     Assert(!DL.isNonIntegralPointerType(PTy),
302106f32e7eSjoerg            "ptrtoint not supported for non-integral pointers");
302206f32e7eSjoerg 
302306f32e7eSjoerg   Assert(DestTy->isIntOrIntVectorTy(), "PtrToInt result must be integral", &I);
302406f32e7eSjoerg   Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(), "PtrToInt type mismatch",
302506f32e7eSjoerg          &I);
302606f32e7eSjoerg 
302706f32e7eSjoerg   if (SrcTy->isVectorTy()) {
3028*da58b97aSjoerg     auto *VSrc = cast<VectorType>(SrcTy);
3029*da58b97aSjoerg     auto *VDest = cast<VectorType>(DestTy);
3030*da58b97aSjoerg     Assert(VSrc->getElementCount() == VDest->getElementCount(),
303106f32e7eSjoerg            "PtrToInt Vector width mismatch", &I);
303206f32e7eSjoerg   }
303306f32e7eSjoerg 
303406f32e7eSjoerg   visitInstruction(I);
303506f32e7eSjoerg }
303606f32e7eSjoerg 
visitIntToPtrInst(IntToPtrInst & I)303706f32e7eSjoerg void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
303806f32e7eSjoerg   // Get the source and destination types
303906f32e7eSjoerg   Type *SrcTy = I.getOperand(0)->getType();
304006f32e7eSjoerg   Type *DestTy = I.getType();
304106f32e7eSjoerg 
304206f32e7eSjoerg   Assert(SrcTy->isIntOrIntVectorTy(),
304306f32e7eSjoerg          "IntToPtr source must be an integral", &I);
304406f32e7eSjoerg   Assert(DestTy->isPtrOrPtrVectorTy(), "IntToPtr result must be a pointer", &I);
304506f32e7eSjoerg 
304606f32e7eSjoerg   if (auto *PTy = dyn_cast<PointerType>(DestTy->getScalarType()))
304706f32e7eSjoerg     Assert(!DL.isNonIntegralPointerType(PTy),
304806f32e7eSjoerg            "inttoptr not supported for non-integral pointers");
304906f32e7eSjoerg 
305006f32e7eSjoerg   Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(), "IntToPtr type mismatch",
305106f32e7eSjoerg          &I);
305206f32e7eSjoerg   if (SrcTy->isVectorTy()) {
3053*da58b97aSjoerg     auto *VSrc = cast<VectorType>(SrcTy);
3054*da58b97aSjoerg     auto *VDest = cast<VectorType>(DestTy);
3055*da58b97aSjoerg     Assert(VSrc->getElementCount() == VDest->getElementCount(),
305606f32e7eSjoerg            "IntToPtr Vector width mismatch", &I);
305706f32e7eSjoerg   }
305806f32e7eSjoerg   visitInstruction(I);
305906f32e7eSjoerg }
306006f32e7eSjoerg 
visitBitCastInst(BitCastInst & I)306106f32e7eSjoerg void Verifier::visitBitCastInst(BitCastInst &I) {
306206f32e7eSjoerg   Assert(
306306f32e7eSjoerg       CastInst::castIsValid(Instruction::BitCast, I.getOperand(0), I.getType()),
306406f32e7eSjoerg       "Invalid bitcast", &I);
306506f32e7eSjoerg   visitInstruction(I);
306606f32e7eSjoerg }
306706f32e7eSjoerg 
visitAddrSpaceCastInst(AddrSpaceCastInst & I)306806f32e7eSjoerg void Verifier::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
306906f32e7eSjoerg   Type *SrcTy = I.getOperand(0)->getType();
307006f32e7eSjoerg   Type *DestTy = I.getType();
307106f32e7eSjoerg 
307206f32e7eSjoerg   Assert(SrcTy->isPtrOrPtrVectorTy(), "AddrSpaceCast source must be a pointer",
307306f32e7eSjoerg          &I);
307406f32e7eSjoerg   Assert(DestTy->isPtrOrPtrVectorTy(), "AddrSpaceCast result must be a pointer",
307506f32e7eSjoerg          &I);
307606f32e7eSjoerg   Assert(SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace(),
307706f32e7eSjoerg          "AddrSpaceCast must be between different address spaces", &I);
3078*da58b97aSjoerg   if (auto *SrcVTy = dyn_cast<VectorType>(SrcTy))
3079*da58b97aSjoerg     Assert(SrcVTy->getElementCount() ==
3080*da58b97aSjoerg                cast<VectorType>(DestTy)->getElementCount(),
308106f32e7eSjoerg            "AddrSpaceCast vector pointer number of elements mismatch", &I);
308206f32e7eSjoerg   visitInstruction(I);
308306f32e7eSjoerg }
308406f32e7eSjoerg 
308506f32e7eSjoerg /// visitPHINode - Ensure that a PHI node is well formed.
308606f32e7eSjoerg ///
visitPHINode(PHINode & PN)308706f32e7eSjoerg void Verifier::visitPHINode(PHINode &PN) {
308806f32e7eSjoerg   // Ensure that the PHI nodes are all grouped together at the top of the block.
308906f32e7eSjoerg   // This can be tested by checking whether the instruction before this is
309006f32e7eSjoerg   // either nonexistent (because this is begin()) or is a PHI node.  If not,
309106f32e7eSjoerg   // then there is some other instruction before a PHI.
309206f32e7eSjoerg   Assert(&PN == &PN.getParent()->front() ||
309306f32e7eSjoerg              isa<PHINode>(--BasicBlock::iterator(&PN)),
309406f32e7eSjoerg          "PHI nodes not grouped at top of basic block!", &PN, PN.getParent());
309506f32e7eSjoerg 
309606f32e7eSjoerg   // Check that a PHI doesn't yield a Token.
309706f32e7eSjoerg   Assert(!PN.getType()->isTokenTy(), "PHI nodes cannot have token type!");
309806f32e7eSjoerg 
309906f32e7eSjoerg   // Check that all of the values of the PHI node have the same type as the
310006f32e7eSjoerg   // result, and that the incoming blocks are really basic blocks.
310106f32e7eSjoerg   for (Value *IncValue : PN.incoming_values()) {
310206f32e7eSjoerg     Assert(PN.getType() == IncValue->getType(),
310306f32e7eSjoerg            "PHI node operands are not the same type as the result!", &PN);
310406f32e7eSjoerg   }
310506f32e7eSjoerg 
310606f32e7eSjoerg   // All other PHI node constraints are checked in the visitBasicBlock method.
310706f32e7eSjoerg 
310806f32e7eSjoerg   visitInstruction(PN);
310906f32e7eSjoerg }
311006f32e7eSjoerg 
visitCallBase(CallBase & Call)311106f32e7eSjoerg void Verifier::visitCallBase(CallBase &Call) {
3112*da58b97aSjoerg   Assert(Call.getCalledOperand()->getType()->isPointerTy(),
311306f32e7eSjoerg          "Called function must be a pointer!", Call);
3114*da58b97aSjoerg   PointerType *FPTy = cast<PointerType>(Call.getCalledOperand()->getType());
311506f32e7eSjoerg 
311606f32e7eSjoerg   Assert(FPTy->getElementType()->isFunctionTy(),
311706f32e7eSjoerg          "Called function is not pointer to function type!", Call);
311806f32e7eSjoerg 
311906f32e7eSjoerg   Assert(FPTy->getElementType() == Call.getFunctionType(),
312006f32e7eSjoerg          "Called function is not the same type as the call!", Call);
312106f32e7eSjoerg 
312206f32e7eSjoerg   FunctionType *FTy = Call.getFunctionType();
312306f32e7eSjoerg 
312406f32e7eSjoerg   // Verify that the correct number of arguments are being passed
312506f32e7eSjoerg   if (FTy->isVarArg())
312606f32e7eSjoerg     Assert(Call.arg_size() >= FTy->getNumParams(),
312706f32e7eSjoerg            "Called function requires more parameters than were provided!",
312806f32e7eSjoerg            Call);
312906f32e7eSjoerg   else
313006f32e7eSjoerg     Assert(Call.arg_size() == FTy->getNumParams(),
313106f32e7eSjoerg            "Incorrect number of arguments passed to called function!", Call);
313206f32e7eSjoerg 
313306f32e7eSjoerg   // Verify that all arguments to the call match the function type.
313406f32e7eSjoerg   for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
313506f32e7eSjoerg     Assert(Call.getArgOperand(i)->getType() == FTy->getParamType(i),
313606f32e7eSjoerg            "Call parameter type does not match function signature!",
313706f32e7eSjoerg            Call.getArgOperand(i), FTy->getParamType(i), Call);
313806f32e7eSjoerg 
313906f32e7eSjoerg   AttributeList Attrs = Call.getAttributes();
314006f32e7eSjoerg 
314106f32e7eSjoerg   Assert(verifyAttributeCount(Attrs, Call.arg_size()),
314206f32e7eSjoerg          "Attribute after last parameter!", Call);
314306f32e7eSjoerg 
314406f32e7eSjoerg   bool IsIntrinsic = Call.getCalledFunction() &&
314506f32e7eSjoerg                      Call.getCalledFunction()->getName().startswith("llvm.");
314606f32e7eSjoerg 
3147*da58b97aSjoerg   Function *Callee =
3148*da58b97aSjoerg       dyn_cast<Function>(Call.getCalledOperand()->stripPointerCasts());
314906f32e7eSjoerg 
3150*da58b97aSjoerg   if (Attrs.hasFnAttribute(Attribute::Speculatable)) {
315106f32e7eSjoerg     // Don't allow speculatable on call sites, unless the underlying function
315206f32e7eSjoerg     // declaration is also speculatable.
315306f32e7eSjoerg     Assert(Callee && Callee->isSpeculatable(),
315406f32e7eSjoerg            "speculatable attribute may not apply to call sites", Call);
315506f32e7eSjoerg   }
315606f32e7eSjoerg 
3157*da58b97aSjoerg   if (Attrs.hasFnAttribute(Attribute::Preallocated)) {
3158*da58b97aSjoerg     Assert(Call.getCalledFunction()->getIntrinsicID() ==
3159*da58b97aSjoerg                Intrinsic::call_preallocated_arg,
3160*da58b97aSjoerg            "preallocated as a call site attribute can only be on "
3161*da58b97aSjoerg            "llvm.call.preallocated.arg");
3162*da58b97aSjoerg   }
3163*da58b97aSjoerg 
316406f32e7eSjoerg   // Verify call attributes.
316506f32e7eSjoerg   verifyFunctionAttrs(FTy, Attrs, &Call, IsIntrinsic);
316606f32e7eSjoerg 
316706f32e7eSjoerg   // Conservatively check the inalloca argument.
316806f32e7eSjoerg   // We have a bug if we can find that there is an underlying alloca without
316906f32e7eSjoerg   // inalloca.
317006f32e7eSjoerg   if (Call.hasInAllocaArgument()) {
317106f32e7eSjoerg     Value *InAllocaArg = Call.getArgOperand(FTy->getNumParams() - 1);
317206f32e7eSjoerg     if (auto AI = dyn_cast<AllocaInst>(InAllocaArg->stripInBoundsOffsets()))
317306f32e7eSjoerg       Assert(AI->isUsedWithInAlloca(),
317406f32e7eSjoerg              "inalloca argument for call has mismatched alloca", AI, Call);
317506f32e7eSjoerg   }
317606f32e7eSjoerg 
317706f32e7eSjoerg   // For each argument of the callsite, if it has the swifterror argument,
317806f32e7eSjoerg   // make sure the underlying alloca/parameter it comes from has a swifterror as
317906f32e7eSjoerg   // well.
318006f32e7eSjoerg   for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
318106f32e7eSjoerg     if (Call.paramHasAttr(i, Attribute::SwiftError)) {
318206f32e7eSjoerg       Value *SwiftErrorArg = Call.getArgOperand(i);
318306f32e7eSjoerg       if (auto AI = dyn_cast<AllocaInst>(SwiftErrorArg->stripInBoundsOffsets())) {
318406f32e7eSjoerg         Assert(AI->isSwiftError(),
318506f32e7eSjoerg                "swifterror argument for call has mismatched alloca", AI, Call);
318606f32e7eSjoerg         continue;
318706f32e7eSjoerg       }
318806f32e7eSjoerg       auto ArgI = dyn_cast<Argument>(SwiftErrorArg);
318906f32e7eSjoerg       Assert(ArgI,
319006f32e7eSjoerg              "swifterror argument should come from an alloca or parameter",
319106f32e7eSjoerg              SwiftErrorArg, Call);
319206f32e7eSjoerg       Assert(ArgI->hasSwiftErrorAttr(),
319306f32e7eSjoerg              "swifterror argument for call has mismatched parameter", ArgI,
319406f32e7eSjoerg              Call);
319506f32e7eSjoerg     }
319606f32e7eSjoerg 
319706f32e7eSjoerg     if (Attrs.hasParamAttribute(i, Attribute::ImmArg)) {
319806f32e7eSjoerg       // Don't allow immarg on call sites, unless the underlying declaration
319906f32e7eSjoerg       // also has the matching immarg.
320006f32e7eSjoerg       Assert(Callee && Callee->hasParamAttribute(i, Attribute::ImmArg),
320106f32e7eSjoerg              "immarg may not apply only to call sites",
320206f32e7eSjoerg              Call.getArgOperand(i), Call);
320306f32e7eSjoerg     }
320406f32e7eSjoerg 
320506f32e7eSjoerg     if (Call.paramHasAttr(i, Attribute::ImmArg)) {
320606f32e7eSjoerg       Value *ArgVal = Call.getArgOperand(i);
320706f32e7eSjoerg       Assert(isa<ConstantInt>(ArgVal) || isa<ConstantFP>(ArgVal),
320806f32e7eSjoerg              "immarg operand has non-immediate parameter", ArgVal, Call);
320906f32e7eSjoerg     }
3210*da58b97aSjoerg 
3211*da58b97aSjoerg     if (Call.paramHasAttr(i, Attribute::Preallocated)) {
3212*da58b97aSjoerg       Value *ArgVal = Call.getArgOperand(i);
3213*da58b97aSjoerg       bool hasOB =
3214*da58b97aSjoerg           Call.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0;
3215*da58b97aSjoerg       bool isMustTail = Call.isMustTailCall();
3216*da58b97aSjoerg       Assert(hasOB != isMustTail,
3217*da58b97aSjoerg              "preallocated operand either requires a preallocated bundle or "
3218*da58b97aSjoerg              "the call to be musttail (but not both)",
3219*da58b97aSjoerg              ArgVal, Call);
3220*da58b97aSjoerg     }
322106f32e7eSjoerg   }
322206f32e7eSjoerg 
322306f32e7eSjoerg   if (FTy->isVarArg()) {
322406f32e7eSjoerg     // FIXME? is 'nest' even legal here?
322506f32e7eSjoerg     bool SawNest = false;
322606f32e7eSjoerg     bool SawReturned = false;
322706f32e7eSjoerg 
322806f32e7eSjoerg     for (unsigned Idx = 0; Idx < FTy->getNumParams(); ++Idx) {
322906f32e7eSjoerg       if (Attrs.hasParamAttribute(Idx, Attribute::Nest))
323006f32e7eSjoerg         SawNest = true;
323106f32e7eSjoerg       if (Attrs.hasParamAttribute(Idx, Attribute::Returned))
323206f32e7eSjoerg         SawReturned = true;
323306f32e7eSjoerg     }
323406f32e7eSjoerg 
323506f32e7eSjoerg     // Check attributes on the varargs part.
323606f32e7eSjoerg     for (unsigned Idx = FTy->getNumParams(); Idx < Call.arg_size(); ++Idx) {
323706f32e7eSjoerg       Type *Ty = Call.getArgOperand(Idx)->getType();
323806f32e7eSjoerg       AttributeSet ArgAttrs = Attrs.getParamAttributes(Idx);
323906f32e7eSjoerg       verifyParameterAttrs(ArgAttrs, Ty, &Call);
324006f32e7eSjoerg 
324106f32e7eSjoerg       if (ArgAttrs.hasAttribute(Attribute::Nest)) {
324206f32e7eSjoerg         Assert(!SawNest, "More than one parameter has attribute nest!", Call);
324306f32e7eSjoerg         SawNest = true;
324406f32e7eSjoerg       }
324506f32e7eSjoerg 
324606f32e7eSjoerg       if (ArgAttrs.hasAttribute(Attribute::Returned)) {
324706f32e7eSjoerg         Assert(!SawReturned, "More than one parameter has attribute returned!",
324806f32e7eSjoerg                Call);
324906f32e7eSjoerg         Assert(Ty->canLosslesslyBitCastTo(FTy->getReturnType()),
325006f32e7eSjoerg                "Incompatible argument and return types for 'returned' "
325106f32e7eSjoerg                "attribute",
325206f32e7eSjoerg                Call);
325306f32e7eSjoerg         SawReturned = true;
325406f32e7eSjoerg       }
325506f32e7eSjoerg 
325606f32e7eSjoerg       // Statepoint intrinsic is vararg but the wrapped function may be not.
325706f32e7eSjoerg       // Allow sret here and check the wrapped function in verifyStatepoint.
325806f32e7eSjoerg       if (!Call.getCalledFunction() ||
325906f32e7eSjoerg           Call.getCalledFunction()->getIntrinsicID() !=
326006f32e7eSjoerg               Intrinsic::experimental_gc_statepoint)
326106f32e7eSjoerg         Assert(!ArgAttrs.hasAttribute(Attribute::StructRet),
326206f32e7eSjoerg                "Attribute 'sret' cannot be used for vararg call arguments!",
326306f32e7eSjoerg                Call);
326406f32e7eSjoerg 
326506f32e7eSjoerg       if (ArgAttrs.hasAttribute(Attribute::InAlloca))
326606f32e7eSjoerg         Assert(Idx == Call.arg_size() - 1,
326706f32e7eSjoerg                "inalloca isn't on the last argument!", Call);
326806f32e7eSjoerg     }
326906f32e7eSjoerg   }
327006f32e7eSjoerg 
327106f32e7eSjoerg   // Verify that there's no metadata unless it's a direct call to an intrinsic.
327206f32e7eSjoerg   if (!IsIntrinsic) {
327306f32e7eSjoerg     for (Type *ParamTy : FTy->params()) {
327406f32e7eSjoerg       Assert(!ParamTy->isMetadataTy(),
327506f32e7eSjoerg              "Function has metadata parameter but isn't an intrinsic", Call);
327606f32e7eSjoerg       Assert(!ParamTy->isTokenTy(),
327706f32e7eSjoerg              "Function has token parameter but isn't an intrinsic", Call);
327806f32e7eSjoerg     }
327906f32e7eSjoerg   }
328006f32e7eSjoerg 
328106f32e7eSjoerg   // Verify that indirect calls don't return tokens.
3282*da58b97aSjoerg   if (!Call.getCalledFunction()) {
328306f32e7eSjoerg     Assert(!FTy->getReturnType()->isTokenTy(),
328406f32e7eSjoerg            "Return type cannot be token for indirect call!");
3285*da58b97aSjoerg     Assert(!FTy->getReturnType()->isX86_AMXTy(),
3286*da58b97aSjoerg            "Return type cannot be x86_amx for indirect call!");
3287*da58b97aSjoerg   }
328806f32e7eSjoerg 
328906f32e7eSjoerg   if (Function *F = Call.getCalledFunction())
329006f32e7eSjoerg     if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
329106f32e7eSjoerg       visitIntrinsicCall(ID, Call);
329206f32e7eSjoerg 
329306f32e7eSjoerg   // Verify that a callsite has at most one "deopt", at most one "funclet", at
3294*da58b97aSjoerg   // most one "gc-transition", at most one "cfguardtarget",
3295*da58b97aSjoerg   // and at most one "preallocated" operand bundle.
329606f32e7eSjoerg   bool FoundDeoptBundle = false, FoundFuncletBundle = false,
3297*da58b97aSjoerg        FoundGCTransitionBundle = false, FoundCFGuardTargetBundle = false,
3298*da58b97aSjoerg        FoundPreallocatedBundle = false, FoundGCLiveBundle = false,
3299*da58b97aSjoerg        FoundAttachedCallBundle = false;
330006f32e7eSjoerg   for (unsigned i = 0, e = Call.getNumOperandBundles(); i < e; ++i) {
330106f32e7eSjoerg     OperandBundleUse BU = Call.getOperandBundleAt(i);
330206f32e7eSjoerg     uint32_t Tag = BU.getTagID();
330306f32e7eSjoerg     if (Tag == LLVMContext::OB_deopt) {
330406f32e7eSjoerg       Assert(!FoundDeoptBundle, "Multiple deopt operand bundles", Call);
330506f32e7eSjoerg       FoundDeoptBundle = true;
330606f32e7eSjoerg     } else if (Tag == LLVMContext::OB_gc_transition) {
330706f32e7eSjoerg       Assert(!FoundGCTransitionBundle, "Multiple gc-transition operand bundles",
330806f32e7eSjoerg              Call);
330906f32e7eSjoerg       FoundGCTransitionBundle = true;
331006f32e7eSjoerg     } else if (Tag == LLVMContext::OB_funclet) {
331106f32e7eSjoerg       Assert(!FoundFuncletBundle, "Multiple funclet operand bundles", Call);
331206f32e7eSjoerg       FoundFuncletBundle = true;
331306f32e7eSjoerg       Assert(BU.Inputs.size() == 1,
331406f32e7eSjoerg              "Expected exactly one funclet bundle operand", Call);
331506f32e7eSjoerg       Assert(isa<FuncletPadInst>(BU.Inputs.front()),
331606f32e7eSjoerg              "Funclet bundle operands should correspond to a FuncletPadInst",
331706f32e7eSjoerg              Call);
331806f32e7eSjoerg     } else if (Tag == LLVMContext::OB_cfguardtarget) {
331906f32e7eSjoerg       Assert(!FoundCFGuardTargetBundle,
332006f32e7eSjoerg              "Multiple CFGuardTarget operand bundles", Call);
332106f32e7eSjoerg       FoundCFGuardTargetBundle = true;
332206f32e7eSjoerg       Assert(BU.Inputs.size() == 1,
332306f32e7eSjoerg              "Expected exactly one cfguardtarget bundle operand", Call);
3324*da58b97aSjoerg     } else if (Tag == LLVMContext::OB_preallocated) {
3325*da58b97aSjoerg       Assert(!FoundPreallocatedBundle, "Multiple preallocated operand bundles",
3326*da58b97aSjoerg              Call);
3327*da58b97aSjoerg       FoundPreallocatedBundle = true;
3328*da58b97aSjoerg       Assert(BU.Inputs.size() == 1,
3329*da58b97aSjoerg              "Expected exactly one preallocated bundle operand", Call);
3330*da58b97aSjoerg       auto Input = dyn_cast<IntrinsicInst>(BU.Inputs.front());
3331*da58b97aSjoerg       Assert(Input &&
3332*da58b97aSjoerg                  Input->getIntrinsicID() == Intrinsic::call_preallocated_setup,
3333*da58b97aSjoerg              "\"preallocated\" argument must be a token from "
3334*da58b97aSjoerg              "llvm.call.preallocated.setup",
3335*da58b97aSjoerg              Call);
3336*da58b97aSjoerg     } else if (Tag == LLVMContext::OB_gc_live) {
3337*da58b97aSjoerg       Assert(!FoundGCLiveBundle, "Multiple gc-live operand bundles",
3338*da58b97aSjoerg              Call);
3339*da58b97aSjoerg       FoundGCLiveBundle = true;
3340*da58b97aSjoerg     } else if (Tag == LLVMContext::OB_clang_arc_attachedcall) {
3341*da58b97aSjoerg       Assert(!FoundAttachedCallBundle,
3342*da58b97aSjoerg              "Multiple \"clang.arc.attachedcall\" operand bundles", Call);
3343*da58b97aSjoerg       FoundAttachedCallBundle = true;
334406f32e7eSjoerg     }
334506f32e7eSjoerg   }
334606f32e7eSjoerg 
3347*da58b97aSjoerg   if (FoundAttachedCallBundle)
3348*da58b97aSjoerg     Assert(FTy->getReturnType()->isPointerTy(),
3349*da58b97aSjoerg            "a call with operand bundle \"clang.arc.attachedcall\" must call a "
3350*da58b97aSjoerg            "function returning a pointer",
3351*da58b97aSjoerg            Call);
3352*da58b97aSjoerg 
335306f32e7eSjoerg   // Verify that each inlinable callsite of a debug-info-bearing function in a
335406f32e7eSjoerg   // debug-info-bearing function has a debug location attached to it. Failure to
335506f32e7eSjoerg   // do so causes assertion failures when the inliner sets up inline scope info.
335606f32e7eSjoerg   if (Call.getFunction()->getSubprogram() && Call.getCalledFunction() &&
335706f32e7eSjoerg       Call.getCalledFunction()->getSubprogram())
335806f32e7eSjoerg     AssertDI(Call.getDebugLoc(),
335906f32e7eSjoerg              "inlinable function call in a function with "
336006f32e7eSjoerg              "debug info must have a !dbg location",
336106f32e7eSjoerg              Call);
336206f32e7eSjoerg 
336306f32e7eSjoerg   visitInstruction(Call);
336406f32e7eSjoerg }
336506f32e7eSjoerg 
336606f32e7eSjoerg /// Two types are "congruent" if they are identical, or if they are both pointer
336706f32e7eSjoerg /// types with different pointee types and the same address space.
isTypeCongruent(Type * L,Type * R)336806f32e7eSjoerg static bool isTypeCongruent(Type *L, Type *R) {
336906f32e7eSjoerg   if (L == R)
337006f32e7eSjoerg     return true;
337106f32e7eSjoerg   PointerType *PL = dyn_cast<PointerType>(L);
337206f32e7eSjoerg   PointerType *PR = dyn_cast<PointerType>(R);
337306f32e7eSjoerg   if (!PL || !PR)
337406f32e7eSjoerg     return false;
337506f32e7eSjoerg   return PL->getAddressSpace() == PR->getAddressSpace();
337606f32e7eSjoerg }
337706f32e7eSjoerg 
getParameterABIAttributes(int I,AttributeList Attrs)337806f32e7eSjoerg static AttrBuilder getParameterABIAttributes(int I, AttributeList Attrs) {
337906f32e7eSjoerg   static const Attribute::AttrKind ABIAttrs[] = {
338006f32e7eSjoerg       Attribute::StructRet,  Attribute::ByVal,          Attribute::InAlloca,
3381*da58b97aSjoerg       Attribute::InReg,      Attribute::StackAlignment, Attribute::SwiftSelf,
3382*da58b97aSjoerg       Attribute::SwiftAsync, Attribute::SwiftError,     Attribute::Preallocated,
3383*da58b97aSjoerg       Attribute::ByRef};
338406f32e7eSjoerg   AttrBuilder Copy;
338506f32e7eSjoerg   for (auto AK : ABIAttrs) {
338606f32e7eSjoerg     if (Attrs.hasParamAttribute(I, AK))
338706f32e7eSjoerg       Copy.addAttribute(AK);
338806f32e7eSjoerg   }
3389*da58b97aSjoerg 
3390*da58b97aSjoerg   // `align` is ABI-affecting only in combination with `byval` or `byref`.
3391*da58b97aSjoerg   if (Attrs.hasParamAttribute(I, Attribute::Alignment) &&
3392*da58b97aSjoerg       (Attrs.hasParamAttribute(I, Attribute::ByVal) ||
3393*da58b97aSjoerg        Attrs.hasParamAttribute(I, Attribute::ByRef)))
339406f32e7eSjoerg     Copy.addAlignmentAttr(Attrs.getParamAlignment(I));
339506f32e7eSjoerg   return Copy;
339606f32e7eSjoerg }
339706f32e7eSjoerg 
verifyMustTailCall(CallInst & CI)339806f32e7eSjoerg void Verifier::verifyMustTailCall(CallInst &CI) {
339906f32e7eSjoerg   Assert(!CI.isInlineAsm(), "cannot use musttail call with inline asm", &CI);
340006f32e7eSjoerg 
340106f32e7eSjoerg   // - The caller and callee prototypes must match.  Pointer types of
340206f32e7eSjoerg   //   parameters or return types may differ in pointee type, but not
340306f32e7eSjoerg   //   address space.
340406f32e7eSjoerg   Function *F = CI.getParent()->getParent();
340506f32e7eSjoerg   FunctionType *CallerTy = F->getFunctionType();
340606f32e7eSjoerg   FunctionType *CalleeTy = CI.getFunctionType();
340706f32e7eSjoerg   if (!CI.getCalledFunction() || !CI.getCalledFunction()->isIntrinsic()) {
340806f32e7eSjoerg     Assert(CallerTy->getNumParams() == CalleeTy->getNumParams(),
340906f32e7eSjoerg            "cannot guarantee tail call due to mismatched parameter counts",
341006f32e7eSjoerg            &CI);
341106f32e7eSjoerg     for (int I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
341206f32e7eSjoerg       Assert(
341306f32e7eSjoerg           isTypeCongruent(CallerTy->getParamType(I), CalleeTy->getParamType(I)),
341406f32e7eSjoerg           "cannot guarantee tail call due to mismatched parameter types", &CI);
341506f32e7eSjoerg     }
341606f32e7eSjoerg   }
341706f32e7eSjoerg   Assert(CallerTy->isVarArg() == CalleeTy->isVarArg(),
341806f32e7eSjoerg          "cannot guarantee tail call due to mismatched varargs", &CI);
341906f32e7eSjoerg   Assert(isTypeCongruent(CallerTy->getReturnType(), CalleeTy->getReturnType()),
342006f32e7eSjoerg          "cannot guarantee tail call due to mismatched return types", &CI);
342106f32e7eSjoerg 
342206f32e7eSjoerg   // - The calling conventions of the caller and callee must match.
342306f32e7eSjoerg   Assert(F->getCallingConv() == CI.getCallingConv(),
342406f32e7eSjoerg          "cannot guarantee tail call due to mismatched calling conv", &CI);
342506f32e7eSjoerg 
342606f32e7eSjoerg   // - All ABI-impacting function attributes, such as sret, byval, inreg,
3427*da58b97aSjoerg   //   returned, preallocated, and inalloca, must match.
342806f32e7eSjoerg   AttributeList CallerAttrs = F->getAttributes();
342906f32e7eSjoerg   AttributeList CalleeAttrs = CI.getAttributes();
343006f32e7eSjoerg   for (int I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
343106f32e7eSjoerg     AttrBuilder CallerABIAttrs = getParameterABIAttributes(I, CallerAttrs);
343206f32e7eSjoerg     AttrBuilder CalleeABIAttrs = getParameterABIAttributes(I, CalleeAttrs);
343306f32e7eSjoerg     Assert(CallerABIAttrs == CalleeABIAttrs,
343406f32e7eSjoerg            "cannot guarantee tail call due to mismatched ABI impacting "
343506f32e7eSjoerg            "function attributes",
343606f32e7eSjoerg            &CI, CI.getOperand(I));
343706f32e7eSjoerg   }
343806f32e7eSjoerg 
343906f32e7eSjoerg   // - The call must immediately precede a :ref:`ret <i_ret>` instruction,
344006f32e7eSjoerg   //   or a pointer bitcast followed by a ret instruction.
344106f32e7eSjoerg   // - The ret instruction must return the (possibly bitcasted) value
344206f32e7eSjoerg   //   produced by the call or void.
344306f32e7eSjoerg   Value *RetVal = &CI;
344406f32e7eSjoerg   Instruction *Next = CI.getNextNode();
344506f32e7eSjoerg 
344606f32e7eSjoerg   // Handle the optional bitcast.
344706f32e7eSjoerg   if (BitCastInst *BI = dyn_cast_or_null<BitCastInst>(Next)) {
344806f32e7eSjoerg     Assert(BI->getOperand(0) == RetVal,
344906f32e7eSjoerg            "bitcast following musttail call must use the call", BI);
345006f32e7eSjoerg     RetVal = BI;
345106f32e7eSjoerg     Next = BI->getNextNode();
345206f32e7eSjoerg   }
345306f32e7eSjoerg 
345406f32e7eSjoerg   // Check the return.
345506f32e7eSjoerg   ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next);
345606f32e7eSjoerg   Assert(Ret, "musttail call must precede a ret with an optional bitcast",
345706f32e7eSjoerg          &CI);
345806f32e7eSjoerg   Assert(!Ret->getReturnValue() || Ret->getReturnValue() == RetVal,
345906f32e7eSjoerg          "musttail call result must be returned", Ret);
346006f32e7eSjoerg }
346106f32e7eSjoerg 
visitCallInst(CallInst & CI)346206f32e7eSjoerg void Verifier::visitCallInst(CallInst &CI) {
346306f32e7eSjoerg   visitCallBase(CI);
346406f32e7eSjoerg 
346506f32e7eSjoerg   if (CI.isMustTailCall())
346606f32e7eSjoerg     verifyMustTailCall(CI);
346706f32e7eSjoerg }
346806f32e7eSjoerg 
visitInvokeInst(InvokeInst & II)346906f32e7eSjoerg void Verifier::visitInvokeInst(InvokeInst &II) {
347006f32e7eSjoerg   visitCallBase(II);
347106f32e7eSjoerg 
347206f32e7eSjoerg   // Verify that the first non-PHI instruction of the unwind destination is an
347306f32e7eSjoerg   // exception handling instruction.
347406f32e7eSjoerg   Assert(
347506f32e7eSjoerg       II.getUnwindDest()->isEHPad(),
347606f32e7eSjoerg       "The unwind destination does not have an exception handling instruction!",
347706f32e7eSjoerg       &II);
347806f32e7eSjoerg 
347906f32e7eSjoerg   visitTerminator(II);
348006f32e7eSjoerg }
348106f32e7eSjoerg 
348206f32e7eSjoerg /// visitUnaryOperator - Check the argument to the unary operator.
348306f32e7eSjoerg ///
visitUnaryOperator(UnaryOperator & U)348406f32e7eSjoerg void Verifier::visitUnaryOperator(UnaryOperator &U) {
348506f32e7eSjoerg   Assert(U.getType() == U.getOperand(0)->getType(),
348606f32e7eSjoerg          "Unary operators must have same type for"
348706f32e7eSjoerg          "operands and result!",
348806f32e7eSjoerg          &U);
348906f32e7eSjoerg 
349006f32e7eSjoerg   switch (U.getOpcode()) {
349106f32e7eSjoerg   // Check that floating-point arithmetic operators are only used with
349206f32e7eSjoerg   // floating-point operands.
349306f32e7eSjoerg   case Instruction::FNeg:
349406f32e7eSjoerg     Assert(U.getType()->isFPOrFPVectorTy(),
349506f32e7eSjoerg            "FNeg operator only works with float types!", &U);
349606f32e7eSjoerg     break;
349706f32e7eSjoerg   default:
349806f32e7eSjoerg     llvm_unreachable("Unknown UnaryOperator opcode!");
349906f32e7eSjoerg   }
350006f32e7eSjoerg 
350106f32e7eSjoerg   visitInstruction(U);
350206f32e7eSjoerg }
350306f32e7eSjoerg 
350406f32e7eSjoerg /// visitBinaryOperator - Check that both arguments to the binary operator are
350506f32e7eSjoerg /// of the same type!
350606f32e7eSjoerg ///
visitBinaryOperator(BinaryOperator & B)350706f32e7eSjoerg void Verifier::visitBinaryOperator(BinaryOperator &B) {
350806f32e7eSjoerg   Assert(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
350906f32e7eSjoerg          "Both operands to a binary operator are not of the same type!", &B);
351006f32e7eSjoerg 
351106f32e7eSjoerg   switch (B.getOpcode()) {
351206f32e7eSjoerg   // Check that integer arithmetic operators are only used with
351306f32e7eSjoerg   // integral operands.
351406f32e7eSjoerg   case Instruction::Add:
351506f32e7eSjoerg   case Instruction::Sub:
351606f32e7eSjoerg   case Instruction::Mul:
351706f32e7eSjoerg   case Instruction::SDiv:
351806f32e7eSjoerg   case Instruction::UDiv:
351906f32e7eSjoerg   case Instruction::SRem:
352006f32e7eSjoerg   case Instruction::URem:
352106f32e7eSjoerg     Assert(B.getType()->isIntOrIntVectorTy(),
352206f32e7eSjoerg            "Integer arithmetic operators only work with integral types!", &B);
352306f32e7eSjoerg     Assert(B.getType() == B.getOperand(0)->getType(),
352406f32e7eSjoerg            "Integer arithmetic operators must have same type "
352506f32e7eSjoerg            "for operands and result!",
352606f32e7eSjoerg            &B);
352706f32e7eSjoerg     break;
352806f32e7eSjoerg   // Check that floating-point arithmetic operators are only used with
352906f32e7eSjoerg   // floating-point operands.
353006f32e7eSjoerg   case Instruction::FAdd:
353106f32e7eSjoerg   case Instruction::FSub:
353206f32e7eSjoerg   case Instruction::FMul:
353306f32e7eSjoerg   case Instruction::FDiv:
353406f32e7eSjoerg   case Instruction::FRem:
353506f32e7eSjoerg     Assert(B.getType()->isFPOrFPVectorTy(),
353606f32e7eSjoerg            "Floating-point arithmetic operators only work with "
353706f32e7eSjoerg            "floating-point types!",
353806f32e7eSjoerg            &B);
353906f32e7eSjoerg     Assert(B.getType() == B.getOperand(0)->getType(),
354006f32e7eSjoerg            "Floating-point arithmetic operators must have same type "
354106f32e7eSjoerg            "for operands and result!",
354206f32e7eSjoerg            &B);
354306f32e7eSjoerg     break;
354406f32e7eSjoerg   // Check that logical operators are only used with integral operands.
354506f32e7eSjoerg   case Instruction::And:
354606f32e7eSjoerg   case Instruction::Or:
354706f32e7eSjoerg   case Instruction::Xor:
354806f32e7eSjoerg     Assert(B.getType()->isIntOrIntVectorTy(),
354906f32e7eSjoerg            "Logical operators only work with integral types!", &B);
355006f32e7eSjoerg     Assert(B.getType() == B.getOperand(0)->getType(),
355106f32e7eSjoerg            "Logical operators must have same type for operands and result!",
355206f32e7eSjoerg            &B);
355306f32e7eSjoerg     break;
355406f32e7eSjoerg   case Instruction::Shl:
355506f32e7eSjoerg   case Instruction::LShr:
355606f32e7eSjoerg   case Instruction::AShr:
355706f32e7eSjoerg     Assert(B.getType()->isIntOrIntVectorTy(),
355806f32e7eSjoerg            "Shifts only work with integral types!", &B);
355906f32e7eSjoerg     Assert(B.getType() == B.getOperand(0)->getType(),
356006f32e7eSjoerg            "Shift return type must be same as operands!", &B);
356106f32e7eSjoerg     break;
356206f32e7eSjoerg   default:
356306f32e7eSjoerg     llvm_unreachable("Unknown BinaryOperator opcode!");
356406f32e7eSjoerg   }
356506f32e7eSjoerg 
356606f32e7eSjoerg   visitInstruction(B);
356706f32e7eSjoerg }
356806f32e7eSjoerg 
visitICmpInst(ICmpInst & IC)356906f32e7eSjoerg void Verifier::visitICmpInst(ICmpInst &IC) {
357006f32e7eSjoerg   // Check that the operands are the same type
357106f32e7eSjoerg   Type *Op0Ty = IC.getOperand(0)->getType();
357206f32e7eSjoerg   Type *Op1Ty = IC.getOperand(1)->getType();
357306f32e7eSjoerg   Assert(Op0Ty == Op1Ty,
357406f32e7eSjoerg          "Both operands to ICmp instruction are not of the same type!", &IC);
357506f32e7eSjoerg   // Check that the operands are the right type
357606f32e7eSjoerg   Assert(Op0Ty->isIntOrIntVectorTy() || Op0Ty->isPtrOrPtrVectorTy(),
357706f32e7eSjoerg          "Invalid operand types for ICmp instruction", &IC);
357806f32e7eSjoerg   // Check that the predicate is valid.
357906f32e7eSjoerg   Assert(IC.isIntPredicate(),
358006f32e7eSjoerg          "Invalid predicate in ICmp instruction!", &IC);
358106f32e7eSjoerg 
358206f32e7eSjoerg   visitInstruction(IC);
358306f32e7eSjoerg }
358406f32e7eSjoerg 
visitFCmpInst(FCmpInst & FC)358506f32e7eSjoerg void Verifier::visitFCmpInst(FCmpInst &FC) {
358606f32e7eSjoerg   // Check that the operands are the same type
358706f32e7eSjoerg   Type *Op0Ty = FC.getOperand(0)->getType();
358806f32e7eSjoerg   Type *Op1Ty = FC.getOperand(1)->getType();
358906f32e7eSjoerg   Assert(Op0Ty == Op1Ty,
359006f32e7eSjoerg          "Both operands to FCmp instruction are not of the same type!", &FC);
359106f32e7eSjoerg   // Check that the operands are the right type
359206f32e7eSjoerg   Assert(Op0Ty->isFPOrFPVectorTy(),
359306f32e7eSjoerg          "Invalid operand types for FCmp instruction", &FC);
359406f32e7eSjoerg   // Check that the predicate is valid.
359506f32e7eSjoerg   Assert(FC.isFPPredicate(),
359606f32e7eSjoerg          "Invalid predicate in FCmp instruction!", &FC);
359706f32e7eSjoerg 
359806f32e7eSjoerg   visitInstruction(FC);
359906f32e7eSjoerg }
360006f32e7eSjoerg 
visitExtractElementInst(ExtractElementInst & EI)360106f32e7eSjoerg void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
360206f32e7eSjoerg   Assert(
360306f32e7eSjoerg       ExtractElementInst::isValidOperands(EI.getOperand(0), EI.getOperand(1)),
360406f32e7eSjoerg       "Invalid extractelement operands!", &EI);
360506f32e7eSjoerg   visitInstruction(EI);
360606f32e7eSjoerg }
360706f32e7eSjoerg 
visitInsertElementInst(InsertElementInst & IE)360806f32e7eSjoerg void Verifier::visitInsertElementInst(InsertElementInst &IE) {
360906f32e7eSjoerg   Assert(InsertElementInst::isValidOperands(IE.getOperand(0), IE.getOperand(1),
361006f32e7eSjoerg                                             IE.getOperand(2)),
361106f32e7eSjoerg          "Invalid insertelement operands!", &IE);
361206f32e7eSjoerg   visitInstruction(IE);
361306f32e7eSjoerg }
361406f32e7eSjoerg 
visitShuffleVectorInst(ShuffleVectorInst & SV)361506f32e7eSjoerg void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
361606f32e7eSjoerg   Assert(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1),
3617*da58b97aSjoerg                                             SV.getShuffleMask()),
361806f32e7eSjoerg          "Invalid shufflevector operands!", &SV);
361906f32e7eSjoerg   visitInstruction(SV);
362006f32e7eSjoerg }
362106f32e7eSjoerg 
visitGetElementPtrInst(GetElementPtrInst & GEP)362206f32e7eSjoerg void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
362306f32e7eSjoerg   Type *TargetTy = GEP.getPointerOperandType()->getScalarType();
362406f32e7eSjoerg 
362506f32e7eSjoerg   Assert(isa<PointerType>(TargetTy),
362606f32e7eSjoerg          "GEP base pointer is not a vector or a vector of pointers", &GEP);
362706f32e7eSjoerg   Assert(GEP.getSourceElementType()->isSized(), "GEP into unsized type!", &GEP);
362806f32e7eSjoerg 
3629*da58b97aSjoerg   SmallVector<Value *, 16> Idxs(GEP.indices());
363006f32e7eSjoerg   Assert(all_of(
363106f32e7eSjoerg       Idxs, [](Value* V) { return V->getType()->isIntOrIntVectorTy(); }),
363206f32e7eSjoerg       "GEP indexes must be integers", &GEP);
363306f32e7eSjoerg   Type *ElTy =
363406f32e7eSjoerg       GetElementPtrInst::getIndexedType(GEP.getSourceElementType(), Idxs);
363506f32e7eSjoerg   Assert(ElTy, "Invalid indices for GEP pointer type!", &GEP);
363606f32e7eSjoerg 
363706f32e7eSjoerg   Assert(GEP.getType()->isPtrOrPtrVectorTy() &&
363806f32e7eSjoerg              GEP.getResultElementType() == ElTy,
363906f32e7eSjoerg          "GEP is not of right type for indices!", &GEP, ElTy);
364006f32e7eSjoerg 
3641*da58b97aSjoerg   if (auto *GEPVTy = dyn_cast<VectorType>(GEP.getType())) {
364206f32e7eSjoerg     // Additional checks for vector GEPs.
3643*da58b97aSjoerg     ElementCount GEPWidth = GEPVTy->getElementCount();
364406f32e7eSjoerg     if (GEP.getPointerOperandType()->isVectorTy())
3645*da58b97aSjoerg       Assert(
3646*da58b97aSjoerg           GEPWidth ==
3647*da58b97aSjoerg               cast<VectorType>(GEP.getPointerOperandType())->getElementCount(),
364806f32e7eSjoerg           "Vector GEP result width doesn't match operand's", &GEP);
364906f32e7eSjoerg     for (Value *Idx : Idxs) {
365006f32e7eSjoerg       Type *IndexTy = Idx->getType();
3651*da58b97aSjoerg       if (auto *IndexVTy = dyn_cast<VectorType>(IndexTy)) {
3652*da58b97aSjoerg         ElementCount IndexWidth = IndexVTy->getElementCount();
365306f32e7eSjoerg         Assert(IndexWidth == GEPWidth, "Invalid GEP index vector width", &GEP);
365406f32e7eSjoerg       }
365506f32e7eSjoerg       Assert(IndexTy->isIntOrIntVectorTy(),
365606f32e7eSjoerg              "All GEP indices should be of integer type");
365706f32e7eSjoerg     }
365806f32e7eSjoerg   }
365906f32e7eSjoerg 
366006f32e7eSjoerg   if (auto *PTy = dyn_cast<PointerType>(GEP.getType())) {
366106f32e7eSjoerg     Assert(GEP.getAddressSpace() == PTy->getAddressSpace(),
366206f32e7eSjoerg            "GEP address space doesn't match type", &GEP);
366306f32e7eSjoerg   }
366406f32e7eSjoerg 
366506f32e7eSjoerg   visitInstruction(GEP);
366606f32e7eSjoerg }
366706f32e7eSjoerg 
isContiguous(const ConstantRange & A,const ConstantRange & B)366806f32e7eSjoerg static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
366906f32e7eSjoerg   return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
367006f32e7eSjoerg }
367106f32e7eSjoerg 
visitRangeMetadata(Instruction & I,MDNode * Range,Type * Ty)367206f32e7eSjoerg void Verifier::visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty) {
367306f32e7eSjoerg   assert(Range && Range == I.getMetadata(LLVMContext::MD_range) &&
367406f32e7eSjoerg          "precondition violation");
367506f32e7eSjoerg 
367606f32e7eSjoerg   unsigned NumOperands = Range->getNumOperands();
367706f32e7eSjoerg   Assert(NumOperands % 2 == 0, "Unfinished range!", Range);
367806f32e7eSjoerg   unsigned NumRanges = NumOperands / 2;
367906f32e7eSjoerg   Assert(NumRanges >= 1, "It should have at least one range!", Range);
368006f32e7eSjoerg 
368106f32e7eSjoerg   ConstantRange LastRange(1, true); // Dummy initial value
368206f32e7eSjoerg   for (unsigned i = 0; i < NumRanges; ++i) {
368306f32e7eSjoerg     ConstantInt *Low =
368406f32e7eSjoerg         mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i));
368506f32e7eSjoerg     Assert(Low, "The lower limit must be an integer!", Low);
368606f32e7eSjoerg     ConstantInt *High =
368706f32e7eSjoerg         mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i + 1));
368806f32e7eSjoerg     Assert(High, "The upper limit must be an integer!", High);
368906f32e7eSjoerg     Assert(High->getType() == Low->getType() && High->getType() == Ty,
369006f32e7eSjoerg            "Range types must match instruction type!", &I);
369106f32e7eSjoerg 
369206f32e7eSjoerg     APInt HighV = High->getValue();
369306f32e7eSjoerg     APInt LowV = Low->getValue();
369406f32e7eSjoerg     ConstantRange CurRange(LowV, HighV);
369506f32e7eSjoerg     Assert(!CurRange.isEmptySet() && !CurRange.isFullSet(),
369606f32e7eSjoerg            "Range must not be empty!", Range);
369706f32e7eSjoerg     if (i != 0) {
369806f32e7eSjoerg       Assert(CurRange.intersectWith(LastRange).isEmptySet(),
369906f32e7eSjoerg              "Intervals are overlapping", Range);
370006f32e7eSjoerg       Assert(LowV.sgt(LastRange.getLower()), "Intervals are not in order",
370106f32e7eSjoerg              Range);
370206f32e7eSjoerg       Assert(!isContiguous(CurRange, LastRange), "Intervals are contiguous",
370306f32e7eSjoerg              Range);
370406f32e7eSjoerg     }
370506f32e7eSjoerg     LastRange = ConstantRange(LowV, HighV);
370606f32e7eSjoerg   }
370706f32e7eSjoerg   if (NumRanges > 2) {
370806f32e7eSjoerg     APInt FirstLow =
370906f32e7eSjoerg         mdconst::dyn_extract<ConstantInt>(Range->getOperand(0))->getValue();
371006f32e7eSjoerg     APInt FirstHigh =
371106f32e7eSjoerg         mdconst::dyn_extract<ConstantInt>(Range->getOperand(1))->getValue();
371206f32e7eSjoerg     ConstantRange FirstRange(FirstLow, FirstHigh);
371306f32e7eSjoerg     Assert(FirstRange.intersectWith(LastRange).isEmptySet(),
371406f32e7eSjoerg            "Intervals are overlapping", Range);
371506f32e7eSjoerg     Assert(!isContiguous(FirstRange, LastRange), "Intervals are contiguous",
371606f32e7eSjoerg            Range);
371706f32e7eSjoerg   }
371806f32e7eSjoerg }
371906f32e7eSjoerg 
checkAtomicMemAccessSize(Type * Ty,const Instruction * I)372006f32e7eSjoerg void Verifier::checkAtomicMemAccessSize(Type *Ty, const Instruction *I) {
372106f32e7eSjoerg   unsigned Size = DL.getTypeSizeInBits(Ty);
372206f32e7eSjoerg   Assert(Size >= 8, "atomic memory access' size must be byte-sized", Ty, I);
372306f32e7eSjoerg   Assert(!(Size & (Size - 1)),
372406f32e7eSjoerg          "atomic memory access' operand must have a power-of-two size", Ty, I);
372506f32e7eSjoerg }
372606f32e7eSjoerg 
visitLoadInst(LoadInst & LI)372706f32e7eSjoerg void Verifier::visitLoadInst(LoadInst &LI) {
372806f32e7eSjoerg   PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType());
372906f32e7eSjoerg   Assert(PTy, "Load operand must be a pointer.", &LI);
373006f32e7eSjoerg   Type *ElTy = LI.getType();
373106f32e7eSjoerg   Assert(LI.getAlignment() <= Value::MaximumAlignment,
373206f32e7eSjoerg          "huge alignment values are unsupported", &LI);
373306f32e7eSjoerg   Assert(ElTy->isSized(), "loading unsized types is not allowed", &LI);
373406f32e7eSjoerg   if (LI.isAtomic()) {
373506f32e7eSjoerg     Assert(LI.getOrdering() != AtomicOrdering::Release &&
373606f32e7eSjoerg                LI.getOrdering() != AtomicOrdering::AcquireRelease,
373706f32e7eSjoerg            "Load cannot have Release ordering", &LI);
373806f32e7eSjoerg     Assert(LI.getAlignment() != 0,
373906f32e7eSjoerg            "Atomic load must specify explicit alignment", &LI);
374006f32e7eSjoerg     Assert(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
374106f32e7eSjoerg            "atomic load operand must have integer, pointer, or floating point "
374206f32e7eSjoerg            "type!",
374306f32e7eSjoerg            ElTy, &LI);
374406f32e7eSjoerg     checkAtomicMemAccessSize(ElTy, &LI);
374506f32e7eSjoerg   } else {
374606f32e7eSjoerg     Assert(LI.getSyncScopeID() == SyncScope::System,
374706f32e7eSjoerg            "Non-atomic load cannot have SynchronizationScope specified", &LI);
374806f32e7eSjoerg   }
374906f32e7eSjoerg 
375006f32e7eSjoerg   visitInstruction(LI);
375106f32e7eSjoerg }
375206f32e7eSjoerg 
visitStoreInst(StoreInst & SI)375306f32e7eSjoerg void Verifier::visitStoreInst(StoreInst &SI) {
375406f32e7eSjoerg   PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType());
375506f32e7eSjoerg   Assert(PTy, "Store operand must be a pointer.", &SI);
3756*da58b97aSjoerg   Type *ElTy = SI.getOperand(0)->getType();
3757*da58b97aSjoerg   Assert(PTy->isOpaqueOrPointeeTypeMatches(ElTy),
375806f32e7eSjoerg          "Stored value type does not match pointer operand type!", &SI, ElTy);
375906f32e7eSjoerg   Assert(SI.getAlignment() <= Value::MaximumAlignment,
376006f32e7eSjoerg          "huge alignment values are unsupported", &SI);
376106f32e7eSjoerg   Assert(ElTy->isSized(), "storing unsized types is not allowed", &SI);
376206f32e7eSjoerg   if (SI.isAtomic()) {
376306f32e7eSjoerg     Assert(SI.getOrdering() != AtomicOrdering::Acquire &&
376406f32e7eSjoerg                SI.getOrdering() != AtomicOrdering::AcquireRelease,
376506f32e7eSjoerg            "Store cannot have Acquire ordering", &SI);
376606f32e7eSjoerg     Assert(SI.getAlignment() != 0,
376706f32e7eSjoerg            "Atomic store must specify explicit alignment", &SI);
376806f32e7eSjoerg     Assert(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
376906f32e7eSjoerg            "atomic store operand must have integer, pointer, or floating point "
377006f32e7eSjoerg            "type!",
377106f32e7eSjoerg            ElTy, &SI);
377206f32e7eSjoerg     checkAtomicMemAccessSize(ElTy, &SI);
377306f32e7eSjoerg   } else {
377406f32e7eSjoerg     Assert(SI.getSyncScopeID() == SyncScope::System,
377506f32e7eSjoerg            "Non-atomic store cannot have SynchronizationScope specified", &SI);
377606f32e7eSjoerg   }
377706f32e7eSjoerg   visitInstruction(SI);
377806f32e7eSjoerg }
377906f32e7eSjoerg 
378006f32e7eSjoerg /// Check that SwiftErrorVal is used as a swifterror argument in CS.
verifySwiftErrorCall(CallBase & Call,const Value * SwiftErrorVal)378106f32e7eSjoerg void Verifier::verifySwiftErrorCall(CallBase &Call,
378206f32e7eSjoerg                                     const Value *SwiftErrorVal) {
3783*da58b97aSjoerg   for (const auto &I : llvm::enumerate(Call.args())) {
3784*da58b97aSjoerg     if (I.value() == SwiftErrorVal) {
3785*da58b97aSjoerg       Assert(Call.paramHasAttr(I.index(), Attribute::SwiftError),
378606f32e7eSjoerg              "swifterror value when used in a callsite should be marked "
378706f32e7eSjoerg              "with swifterror attribute",
378806f32e7eSjoerg              SwiftErrorVal, Call);
378906f32e7eSjoerg     }
379006f32e7eSjoerg   }
379106f32e7eSjoerg }
379206f32e7eSjoerg 
verifySwiftErrorValue(const Value * SwiftErrorVal)379306f32e7eSjoerg void Verifier::verifySwiftErrorValue(const Value *SwiftErrorVal) {
379406f32e7eSjoerg   // Check that swifterror value is only used by loads, stores, or as
379506f32e7eSjoerg   // a swifterror argument.
379606f32e7eSjoerg   for (const User *U : SwiftErrorVal->users()) {
379706f32e7eSjoerg     Assert(isa<LoadInst>(U) || isa<StoreInst>(U) || isa<CallInst>(U) ||
379806f32e7eSjoerg            isa<InvokeInst>(U),
379906f32e7eSjoerg            "swifterror value can only be loaded and stored from, or "
380006f32e7eSjoerg            "as a swifterror argument!",
380106f32e7eSjoerg            SwiftErrorVal, U);
380206f32e7eSjoerg     // If it is used by a store, check it is the second operand.
380306f32e7eSjoerg     if (auto StoreI = dyn_cast<StoreInst>(U))
380406f32e7eSjoerg       Assert(StoreI->getOperand(1) == SwiftErrorVal,
380506f32e7eSjoerg              "swifterror value should be the second operand when used "
380606f32e7eSjoerg              "by stores", SwiftErrorVal, U);
380706f32e7eSjoerg     if (auto *Call = dyn_cast<CallBase>(U))
380806f32e7eSjoerg       verifySwiftErrorCall(*const_cast<CallBase *>(Call), SwiftErrorVal);
380906f32e7eSjoerg   }
381006f32e7eSjoerg }
381106f32e7eSjoerg 
visitAllocaInst(AllocaInst & AI)381206f32e7eSjoerg void Verifier::visitAllocaInst(AllocaInst &AI) {
381306f32e7eSjoerg   SmallPtrSet<Type*, 4> Visited;
381406f32e7eSjoerg   Assert(AI.getAllocatedType()->isSized(&Visited),
381506f32e7eSjoerg          "Cannot allocate unsized type", &AI);
381606f32e7eSjoerg   Assert(AI.getArraySize()->getType()->isIntegerTy(),
381706f32e7eSjoerg          "Alloca array size must have integer type", &AI);
381806f32e7eSjoerg   Assert(AI.getAlignment() <= Value::MaximumAlignment,
381906f32e7eSjoerg          "huge alignment values are unsupported", &AI);
382006f32e7eSjoerg 
382106f32e7eSjoerg   if (AI.isSwiftError()) {
382206f32e7eSjoerg     verifySwiftErrorValue(&AI);
382306f32e7eSjoerg   }
382406f32e7eSjoerg 
382506f32e7eSjoerg   visitInstruction(AI);
382606f32e7eSjoerg }
382706f32e7eSjoerg 
visitAtomicCmpXchgInst(AtomicCmpXchgInst & CXI)382806f32e7eSjoerg void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI) {
3829*da58b97aSjoerg   Type *ElTy = CXI.getOperand(1)->getType();
383006f32e7eSjoerg   Assert(ElTy->isIntOrPtrTy(),
383106f32e7eSjoerg          "cmpxchg operand must have integer or pointer type", ElTy, &CXI);
383206f32e7eSjoerg   checkAtomicMemAccessSize(ElTy, &CXI);
383306f32e7eSjoerg   visitInstruction(CXI);
383406f32e7eSjoerg }
383506f32e7eSjoerg 
visitAtomicRMWInst(AtomicRMWInst & RMWI)383606f32e7eSjoerg void Verifier::visitAtomicRMWInst(AtomicRMWInst &RMWI) {
383706f32e7eSjoerg   Assert(RMWI.getOrdering() != AtomicOrdering::Unordered,
383806f32e7eSjoerg          "atomicrmw instructions cannot be unordered.", &RMWI);
383906f32e7eSjoerg   auto Op = RMWI.getOperation();
3840*da58b97aSjoerg   Type *ElTy = RMWI.getOperand(1)->getType();
384106f32e7eSjoerg   if (Op == AtomicRMWInst::Xchg) {
384206f32e7eSjoerg     Assert(ElTy->isIntegerTy() || ElTy->isFloatingPointTy(), "atomicrmw " +
384306f32e7eSjoerg            AtomicRMWInst::getOperationName(Op) +
384406f32e7eSjoerg            " operand must have integer or floating point type!",
384506f32e7eSjoerg            &RMWI, ElTy);
384606f32e7eSjoerg   } else if (AtomicRMWInst::isFPOperation(Op)) {
384706f32e7eSjoerg     Assert(ElTy->isFloatingPointTy(), "atomicrmw " +
384806f32e7eSjoerg            AtomicRMWInst::getOperationName(Op) +
384906f32e7eSjoerg            " operand must have floating point type!",
385006f32e7eSjoerg            &RMWI, ElTy);
385106f32e7eSjoerg   } else {
385206f32e7eSjoerg     Assert(ElTy->isIntegerTy(), "atomicrmw " +
385306f32e7eSjoerg            AtomicRMWInst::getOperationName(Op) +
385406f32e7eSjoerg            " operand must have integer type!",
385506f32e7eSjoerg            &RMWI, ElTy);
385606f32e7eSjoerg   }
385706f32e7eSjoerg   checkAtomicMemAccessSize(ElTy, &RMWI);
385806f32e7eSjoerg   Assert(AtomicRMWInst::FIRST_BINOP <= Op && Op <= AtomicRMWInst::LAST_BINOP,
385906f32e7eSjoerg          "Invalid binary operation!", &RMWI);
386006f32e7eSjoerg   visitInstruction(RMWI);
386106f32e7eSjoerg }
386206f32e7eSjoerg 
visitFenceInst(FenceInst & FI)386306f32e7eSjoerg void Verifier::visitFenceInst(FenceInst &FI) {
386406f32e7eSjoerg   const AtomicOrdering Ordering = FI.getOrdering();
386506f32e7eSjoerg   Assert(Ordering == AtomicOrdering::Acquire ||
386606f32e7eSjoerg              Ordering == AtomicOrdering::Release ||
386706f32e7eSjoerg              Ordering == AtomicOrdering::AcquireRelease ||
386806f32e7eSjoerg              Ordering == AtomicOrdering::SequentiallyConsistent,
386906f32e7eSjoerg          "fence instructions may only have acquire, release, acq_rel, or "
387006f32e7eSjoerg          "seq_cst ordering.",
387106f32e7eSjoerg          &FI);
387206f32e7eSjoerg   visitInstruction(FI);
387306f32e7eSjoerg }
387406f32e7eSjoerg 
visitExtractValueInst(ExtractValueInst & EVI)387506f32e7eSjoerg void Verifier::visitExtractValueInst(ExtractValueInst &EVI) {
387606f32e7eSjoerg   Assert(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(),
387706f32e7eSjoerg                                           EVI.getIndices()) == EVI.getType(),
387806f32e7eSjoerg          "Invalid ExtractValueInst operands!", &EVI);
387906f32e7eSjoerg 
388006f32e7eSjoerg   visitInstruction(EVI);
388106f32e7eSjoerg }
388206f32e7eSjoerg 
visitInsertValueInst(InsertValueInst & IVI)388306f32e7eSjoerg void Verifier::visitInsertValueInst(InsertValueInst &IVI) {
388406f32e7eSjoerg   Assert(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(),
388506f32e7eSjoerg                                           IVI.getIndices()) ==
388606f32e7eSjoerg              IVI.getOperand(1)->getType(),
388706f32e7eSjoerg          "Invalid InsertValueInst operands!", &IVI);
388806f32e7eSjoerg 
388906f32e7eSjoerg   visitInstruction(IVI);
389006f32e7eSjoerg }
389106f32e7eSjoerg 
getParentPad(Value * EHPad)389206f32e7eSjoerg static Value *getParentPad(Value *EHPad) {
389306f32e7eSjoerg   if (auto *FPI = dyn_cast<FuncletPadInst>(EHPad))
389406f32e7eSjoerg     return FPI->getParentPad();
389506f32e7eSjoerg 
389606f32e7eSjoerg   return cast<CatchSwitchInst>(EHPad)->getParentPad();
389706f32e7eSjoerg }
389806f32e7eSjoerg 
visitEHPadPredecessors(Instruction & I)389906f32e7eSjoerg void Verifier::visitEHPadPredecessors(Instruction &I) {
390006f32e7eSjoerg   assert(I.isEHPad());
390106f32e7eSjoerg 
390206f32e7eSjoerg   BasicBlock *BB = I.getParent();
390306f32e7eSjoerg   Function *F = BB->getParent();
390406f32e7eSjoerg 
390506f32e7eSjoerg   Assert(BB != &F->getEntryBlock(), "EH pad cannot be in entry block.", &I);
390606f32e7eSjoerg 
390706f32e7eSjoerg   if (auto *LPI = dyn_cast<LandingPadInst>(&I)) {
390806f32e7eSjoerg     // The landingpad instruction defines its parent as a landing pad block. The
390906f32e7eSjoerg     // landing pad block may be branched to only by the unwind edge of an
391006f32e7eSjoerg     // invoke.
391106f32e7eSjoerg     for (BasicBlock *PredBB : predecessors(BB)) {
391206f32e7eSjoerg       const auto *II = dyn_cast<InvokeInst>(PredBB->getTerminator());
391306f32e7eSjoerg       Assert(II && II->getUnwindDest() == BB && II->getNormalDest() != BB,
391406f32e7eSjoerg              "Block containing LandingPadInst must be jumped to "
391506f32e7eSjoerg              "only by the unwind edge of an invoke.",
391606f32e7eSjoerg              LPI);
391706f32e7eSjoerg     }
391806f32e7eSjoerg     return;
391906f32e7eSjoerg   }
392006f32e7eSjoerg   if (auto *CPI = dyn_cast<CatchPadInst>(&I)) {
392106f32e7eSjoerg     if (!pred_empty(BB))
392206f32e7eSjoerg       Assert(BB->getUniquePredecessor() == CPI->getCatchSwitch()->getParent(),
392306f32e7eSjoerg              "Block containg CatchPadInst must be jumped to "
392406f32e7eSjoerg              "only by its catchswitch.",
392506f32e7eSjoerg              CPI);
392606f32e7eSjoerg     Assert(BB != CPI->getCatchSwitch()->getUnwindDest(),
392706f32e7eSjoerg            "Catchswitch cannot unwind to one of its catchpads",
392806f32e7eSjoerg            CPI->getCatchSwitch(), CPI);
392906f32e7eSjoerg     return;
393006f32e7eSjoerg   }
393106f32e7eSjoerg 
393206f32e7eSjoerg   // Verify that each pred has a legal terminator with a legal to/from EH
393306f32e7eSjoerg   // pad relationship.
393406f32e7eSjoerg   Instruction *ToPad = &I;
393506f32e7eSjoerg   Value *ToPadParent = getParentPad(ToPad);
393606f32e7eSjoerg   for (BasicBlock *PredBB : predecessors(BB)) {
393706f32e7eSjoerg     Instruction *TI = PredBB->getTerminator();
393806f32e7eSjoerg     Value *FromPad;
393906f32e7eSjoerg     if (auto *II = dyn_cast<InvokeInst>(TI)) {
394006f32e7eSjoerg       Assert(II->getUnwindDest() == BB && II->getNormalDest() != BB,
394106f32e7eSjoerg              "EH pad must be jumped to via an unwind edge", ToPad, II);
394206f32e7eSjoerg       if (auto Bundle = II->getOperandBundle(LLVMContext::OB_funclet))
394306f32e7eSjoerg         FromPad = Bundle->Inputs[0];
394406f32e7eSjoerg       else
394506f32e7eSjoerg         FromPad = ConstantTokenNone::get(II->getContext());
394606f32e7eSjoerg     } else if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) {
394706f32e7eSjoerg       FromPad = CRI->getOperand(0);
394806f32e7eSjoerg       Assert(FromPad != ToPadParent, "A cleanupret must exit its cleanup", CRI);
394906f32e7eSjoerg     } else if (auto *CSI = dyn_cast<CatchSwitchInst>(TI)) {
395006f32e7eSjoerg       FromPad = CSI;
395106f32e7eSjoerg     } else {
395206f32e7eSjoerg       Assert(false, "EH pad must be jumped to via an unwind edge", ToPad, TI);
395306f32e7eSjoerg     }
395406f32e7eSjoerg 
395506f32e7eSjoerg     // The edge may exit from zero or more nested pads.
395606f32e7eSjoerg     SmallSet<Value *, 8> Seen;
395706f32e7eSjoerg     for (;; FromPad = getParentPad(FromPad)) {
395806f32e7eSjoerg       Assert(FromPad != ToPad,
395906f32e7eSjoerg              "EH pad cannot handle exceptions raised within it", FromPad, TI);
396006f32e7eSjoerg       if (FromPad == ToPadParent) {
396106f32e7eSjoerg         // This is a legal unwind edge.
396206f32e7eSjoerg         break;
396306f32e7eSjoerg       }
396406f32e7eSjoerg       Assert(!isa<ConstantTokenNone>(FromPad),
396506f32e7eSjoerg              "A single unwind edge may only enter one EH pad", TI);
396606f32e7eSjoerg       Assert(Seen.insert(FromPad).second,
396706f32e7eSjoerg              "EH pad jumps through a cycle of pads", FromPad);
396806f32e7eSjoerg     }
396906f32e7eSjoerg   }
397006f32e7eSjoerg }
397106f32e7eSjoerg 
visitLandingPadInst(LandingPadInst & LPI)397206f32e7eSjoerg void Verifier::visitLandingPadInst(LandingPadInst &LPI) {
397306f32e7eSjoerg   // The landingpad instruction is ill-formed if it doesn't have any clauses and
397406f32e7eSjoerg   // isn't a cleanup.
397506f32e7eSjoerg   Assert(LPI.getNumClauses() > 0 || LPI.isCleanup(),
397606f32e7eSjoerg          "LandingPadInst needs at least one clause or to be a cleanup.", &LPI);
397706f32e7eSjoerg 
397806f32e7eSjoerg   visitEHPadPredecessors(LPI);
397906f32e7eSjoerg 
398006f32e7eSjoerg   if (!LandingPadResultTy)
398106f32e7eSjoerg     LandingPadResultTy = LPI.getType();
398206f32e7eSjoerg   else
398306f32e7eSjoerg     Assert(LandingPadResultTy == LPI.getType(),
398406f32e7eSjoerg            "The landingpad instruction should have a consistent result type "
398506f32e7eSjoerg            "inside a function.",
398606f32e7eSjoerg            &LPI);
398706f32e7eSjoerg 
398806f32e7eSjoerg   Function *F = LPI.getParent()->getParent();
398906f32e7eSjoerg   Assert(F->hasPersonalityFn(),
399006f32e7eSjoerg          "LandingPadInst needs to be in a function with a personality.", &LPI);
399106f32e7eSjoerg 
399206f32e7eSjoerg   // The landingpad instruction must be the first non-PHI instruction in the
399306f32e7eSjoerg   // block.
399406f32e7eSjoerg   Assert(LPI.getParent()->getLandingPadInst() == &LPI,
399506f32e7eSjoerg          "LandingPadInst not the first non-PHI instruction in the block.",
399606f32e7eSjoerg          &LPI);
399706f32e7eSjoerg 
399806f32e7eSjoerg   for (unsigned i = 0, e = LPI.getNumClauses(); i < e; ++i) {
399906f32e7eSjoerg     Constant *Clause = LPI.getClause(i);
400006f32e7eSjoerg     if (LPI.isCatch(i)) {
400106f32e7eSjoerg       Assert(isa<PointerType>(Clause->getType()),
400206f32e7eSjoerg              "Catch operand does not have pointer type!", &LPI);
400306f32e7eSjoerg     } else {
400406f32e7eSjoerg       Assert(LPI.isFilter(i), "Clause is neither catch nor filter!", &LPI);
400506f32e7eSjoerg       Assert(isa<ConstantArray>(Clause) || isa<ConstantAggregateZero>(Clause),
400606f32e7eSjoerg              "Filter operand is not an array of constants!", &LPI);
400706f32e7eSjoerg     }
400806f32e7eSjoerg   }
400906f32e7eSjoerg 
401006f32e7eSjoerg   visitInstruction(LPI);
401106f32e7eSjoerg }
401206f32e7eSjoerg 
visitResumeInst(ResumeInst & RI)401306f32e7eSjoerg void Verifier::visitResumeInst(ResumeInst &RI) {
401406f32e7eSjoerg   Assert(RI.getFunction()->hasPersonalityFn(),
401506f32e7eSjoerg          "ResumeInst needs to be in a function with a personality.", &RI);
401606f32e7eSjoerg 
401706f32e7eSjoerg   if (!LandingPadResultTy)
401806f32e7eSjoerg     LandingPadResultTy = RI.getValue()->getType();
401906f32e7eSjoerg   else
402006f32e7eSjoerg     Assert(LandingPadResultTy == RI.getValue()->getType(),
402106f32e7eSjoerg            "The resume instruction should have a consistent result type "
402206f32e7eSjoerg            "inside a function.",
402306f32e7eSjoerg            &RI);
402406f32e7eSjoerg 
402506f32e7eSjoerg   visitTerminator(RI);
402606f32e7eSjoerg }
402706f32e7eSjoerg 
visitCatchPadInst(CatchPadInst & CPI)402806f32e7eSjoerg void Verifier::visitCatchPadInst(CatchPadInst &CPI) {
402906f32e7eSjoerg   BasicBlock *BB = CPI.getParent();
403006f32e7eSjoerg 
403106f32e7eSjoerg   Function *F = BB->getParent();
403206f32e7eSjoerg   Assert(F->hasPersonalityFn(),
403306f32e7eSjoerg          "CatchPadInst needs to be in a function with a personality.", &CPI);
403406f32e7eSjoerg 
403506f32e7eSjoerg   Assert(isa<CatchSwitchInst>(CPI.getParentPad()),
403606f32e7eSjoerg          "CatchPadInst needs to be directly nested in a CatchSwitchInst.",
403706f32e7eSjoerg          CPI.getParentPad());
403806f32e7eSjoerg 
403906f32e7eSjoerg   // The catchpad instruction must be the first non-PHI instruction in the
404006f32e7eSjoerg   // block.
404106f32e7eSjoerg   Assert(BB->getFirstNonPHI() == &CPI,
404206f32e7eSjoerg          "CatchPadInst not the first non-PHI instruction in the block.", &CPI);
404306f32e7eSjoerg 
404406f32e7eSjoerg   visitEHPadPredecessors(CPI);
404506f32e7eSjoerg   visitFuncletPadInst(CPI);
404606f32e7eSjoerg }
404706f32e7eSjoerg 
visitCatchReturnInst(CatchReturnInst & CatchReturn)404806f32e7eSjoerg void Verifier::visitCatchReturnInst(CatchReturnInst &CatchReturn) {
404906f32e7eSjoerg   Assert(isa<CatchPadInst>(CatchReturn.getOperand(0)),
405006f32e7eSjoerg          "CatchReturnInst needs to be provided a CatchPad", &CatchReturn,
405106f32e7eSjoerg          CatchReturn.getOperand(0));
405206f32e7eSjoerg 
405306f32e7eSjoerg   visitTerminator(CatchReturn);
405406f32e7eSjoerg }
405506f32e7eSjoerg 
visitCleanupPadInst(CleanupPadInst & CPI)405606f32e7eSjoerg void Verifier::visitCleanupPadInst(CleanupPadInst &CPI) {
405706f32e7eSjoerg   BasicBlock *BB = CPI.getParent();
405806f32e7eSjoerg 
405906f32e7eSjoerg   Function *F = BB->getParent();
406006f32e7eSjoerg   Assert(F->hasPersonalityFn(),
406106f32e7eSjoerg          "CleanupPadInst needs to be in a function with a personality.", &CPI);
406206f32e7eSjoerg 
406306f32e7eSjoerg   // The cleanuppad instruction must be the first non-PHI instruction in the
406406f32e7eSjoerg   // block.
406506f32e7eSjoerg   Assert(BB->getFirstNonPHI() == &CPI,
406606f32e7eSjoerg          "CleanupPadInst not the first non-PHI instruction in the block.",
406706f32e7eSjoerg          &CPI);
406806f32e7eSjoerg 
406906f32e7eSjoerg   auto *ParentPad = CPI.getParentPad();
407006f32e7eSjoerg   Assert(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
407106f32e7eSjoerg          "CleanupPadInst has an invalid parent.", &CPI);
407206f32e7eSjoerg 
407306f32e7eSjoerg   visitEHPadPredecessors(CPI);
407406f32e7eSjoerg   visitFuncletPadInst(CPI);
407506f32e7eSjoerg }
407606f32e7eSjoerg 
visitFuncletPadInst(FuncletPadInst & FPI)407706f32e7eSjoerg void Verifier::visitFuncletPadInst(FuncletPadInst &FPI) {
407806f32e7eSjoerg   User *FirstUser = nullptr;
407906f32e7eSjoerg   Value *FirstUnwindPad = nullptr;
408006f32e7eSjoerg   SmallVector<FuncletPadInst *, 8> Worklist({&FPI});
408106f32e7eSjoerg   SmallSet<FuncletPadInst *, 8> Seen;
408206f32e7eSjoerg 
408306f32e7eSjoerg   while (!Worklist.empty()) {
408406f32e7eSjoerg     FuncletPadInst *CurrentPad = Worklist.pop_back_val();
408506f32e7eSjoerg     Assert(Seen.insert(CurrentPad).second,
408606f32e7eSjoerg            "FuncletPadInst must not be nested within itself", CurrentPad);
408706f32e7eSjoerg     Value *UnresolvedAncestorPad = nullptr;
408806f32e7eSjoerg     for (User *U : CurrentPad->users()) {
408906f32e7eSjoerg       BasicBlock *UnwindDest;
409006f32e7eSjoerg       if (auto *CRI = dyn_cast<CleanupReturnInst>(U)) {
409106f32e7eSjoerg         UnwindDest = CRI->getUnwindDest();
409206f32e7eSjoerg       } else if (auto *CSI = dyn_cast<CatchSwitchInst>(U)) {
409306f32e7eSjoerg         // We allow catchswitch unwind to caller to nest
409406f32e7eSjoerg         // within an outer pad that unwinds somewhere else,
409506f32e7eSjoerg         // because catchswitch doesn't have a nounwind variant.
409606f32e7eSjoerg         // See e.g. SimplifyCFGOpt::SimplifyUnreachable.
409706f32e7eSjoerg         if (CSI->unwindsToCaller())
409806f32e7eSjoerg           continue;
409906f32e7eSjoerg         UnwindDest = CSI->getUnwindDest();
410006f32e7eSjoerg       } else if (auto *II = dyn_cast<InvokeInst>(U)) {
410106f32e7eSjoerg         UnwindDest = II->getUnwindDest();
410206f32e7eSjoerg       } else if (isa<CallInst>(U)) {
410306f32e7eSjoerg         // Calls which don't unwind may be found inside funclet
410406f32e7eSjoerg         // pads that unwind somewhere else.  We don't *require*
410506f32e7eSjoerg         // such calls to be annotated nounwind.
410606f32e7eSjoerg         continue;
410706f32e7eSjoerg       } else if (auto *CPI = dyn_cast<CleanupPadInst>(U)) {
410806f32e7eSjoerg         // The unwind dest for a cleanup can only be found by
410906f32e7eSjoerg         // recursive search.  Add it to the worklist, and we'll
411006f32e7eSjoerg         // search for its first use that determines where it unwinds.
411106f32e7eSjoerg         Worklist.push_back(CPI);
411206f32e7eSjoerg         continue;
411306f32e7eSjoerg       } else {
411406f32e7eSjoerg         Assert(isa<CatchReturnInst>(U), "Bogus funclet pad use", U);
411506f32e7eSjoerg         continue;
411606f32e7eSjoerg       }
411706f32e7eSjoerg 
411806f32e7eSjoerg       Value *UnwindPad;
411906f32e7eSjoerg       bool ExitsFPI;
412006f32e7eSjoerg       if (UnwindDest) {
412106f32e7eSjoerg         UnwindPad = UnwindDest->getFirstNonPHI();
412206f32e7eSjoerg         if (!cast<Instruction>(UnwindPad)->isEHPad())
412306f32e7eSjoerg           continue;
412406f32e7eSjoerg         Value *UnwindParent = getParentPad(UnwindPad);
412506f32e7eSjoerg         // Ignore unwind edges that don't exit CurrentPad.
412606f32e7eSjoerg         if (UnwindParent == CurrentPad)
412706f32e7eSjoerg           continue;
412806f32e7eSjoerg         // Determine whether the original funclet pad is exited,
412906f32e7eSjoerg         // and if we are scanning nested pads determine how many
413006f32e7eSjoerg         // of them are exited so we can stop searching their
413106f32e7eSjoerg         // children.
413206f32e7eSjoerg         Value *ExitedPad = CurrentPad;
413306f32e7eSjoerg         ExitsFPI = false;
413406f32e7eSjoerg         do {
413506f32e7eSjoerg           if (ExitedPad == &FPI) {
413606f32e7eSjoerg             ExitsFPI = true;
413706f32e7eSjoerg             // Now we can resolve any ancestors of CurrentPad up to
413806f32e7eSjoerg             // FPI, but not including FPI since we need to make sure
413906f32e7eSjoerg             // to check all direct users of FPI for consistency.
414006f32e7eSjoerg             UnresolvedAncestorPad = &FPI;
414106f32e7eSjoerg             break;
414206f32e7eSjoerg           }
414306f32e7eSjoerg           Value *ExitedParent = getParentPad(ExitedPad);
414406f32e7eSjoerg           if (ExitedParent == UnwindParent) {
414506f32e7eSjoerg             // ExitedPad is the ancestor-most pad which this unwind
414606f32e7eSjoerg             // edge exits, so we can resolve up to it, meaning that
414706f32e7eSjoerg             // ExitedParent is the first ancestor still unresolved.
414806f32e7eSjoerg             UnresolvedAncestorPad = ExitedParent;
414906f32e7eSjoerg             break;
415006f32e7eSjoerg           }
415106f32e7eSjoerg           ExitedPad = ExitedParent;
415206f32e7eSjoerg         } while (!isa<ConstantTokenNone>(ExitedPad));
415306f32e7eSjoerg       } else {
415406f32e7eSjoerg         // Unwinding to caller exits all pads.
415506f32e7eSjoerg         UnwindPad = ConstantTokenNone::get(FPI.getContext());
415606f32e7eSjoerg         ExitsFPI = true;
415706f32e7eSjoerg         UnresolvedAncestorPad = &FPI;
415806f32e7eSjoerg       }
415906f32e7eSjoerg 
416006f32e7eSjoerg       if (ExitsFPI) {
416106f32e7eSjoerg         // This unwind edge exits FPI.  Make sure it agrees with other
416206f32e7eSjoerg         // such edges.
416306f32e7eSjoerg         if (FirstUser) {
416406f32e7eSjoerg           Assert(UnwindPad == FirstUnwindPad, "Unwind edges out of a funclet "
416506f32e7eSjoerg                                               "pad must have the same unwind "
416606f32e7eSjoerg                                               "dest",
416706f32e7eSjoerg                  &FPI, U, FirstUser);
416806f32e7eSjoerg         } else {
416906f32e7eSjoerg           FirstUser = U;
417006f32e7eSjoerg           FirstUnwindPad = UnwindPad;
417106f32e7eSjoerg           // Record cleanup sibling unwinds for verifySiblingFuncletUnwinds
417206f32e7eSjoerg           if (isa<CleanupPadInst>(&FPI) && !isa<ConstantTokenNone>(UnwindPad) &&
417306f32e7eSjoerg               getParentPad(UnwindPad) == getParentPad(&FPI))
417406f32e7eSjoerg             SiblingFuncletInfo[&FPI] = cast<Instruction>(U);
417506f32e7eSjoerg         }
417606f32e7eSjoerg       }
417706f32e7eSjoerg       // Make sure we visit all uses of FPI, but for nested pads stop as
417806f32e7eSjoerg       // soon as we know where they unwind to.
417906f32e7eSjoerg       if (CurrentPad != &FPI)
418006f32e7eSjoerg         break;
418106f32e7eSjoerg     }
418206f32e7eSjoerg     if (UnresolvedAncestorPad) {
418306f32e7eSjoerg       if (CurrentPad == UnresolvedAncestorPad) {
418406f32e7eSjoerg         // When CurrentPad is FPI itself, we don't mark it as resolved even if
418506f32e7eSjoerg         // we've found an unwind edge that exits it, because we need to verify
418606f32e7eSjoerg         // all direct uses of FPI.
418706f32e7eSjoerg         assert(CurrentPad == &FPI);
418806f32e7eSjoerg         continue;
418906f32e7eSjoerg       }
419006f32e7eSjoerg       // Pop off the worklist any nested pads that we've found an unwind
419106f32e7eSjoerg       // destination for.  The pads on the worklist are the uncles,
419206f32e7eSjoerg       // great-uncles, etc. of CurrentPad.  We've found an unwind destination
419306f32e7eSjoerg       // for all ancestors of CurrentPad up to but not including
419406f32e7eSjoerg       // UnresolvedAncestorPad.
419506f32e7eSjoerg       Value *ResolvedPad = CurrentPad;
419606f32e7eSjoerg       while (!Worklist.empty()) {
419706f32e7eSjoerg         Value *UnclePad = Worklist.back();
419806f32e7eSjoerg         Value *AncestorPad = getParentPad(UnclePad);
419906f32e7eSjoerg         // Walk ResolvedPad up the ancestor list until we either find the
420006f32e7eSjoerg         // uncle's parent or the last resolved ancestor.
420106f32e7eSjoerg         while (ResolvedPad != AncestorPad) {
420206f32e7eSjoerg           Value *ResolvedParent = getParentPad(ResolvedPad);
420306f32e7eSjoerg           if (ResolvedParent == UnresolvedAncestorPad) {
420406f32e7eSjoerg             break;
420506f32e7eSjoerg           }
420606f32e7eSjoerg           ResolvedPad = ResolvedParent;
420706f32e7eSjoerg         }
420806f32e7eSjoerg         // If the resolved ancestor search didn't find the uncle's parent,
420906f32e7eSjoerg         // then the uncle is not yet resolved.
421006f32e7eSjoerg         if (ResolvedPad != AncestorPad)
421106f32e7eSjoerg           break;
421206f32e7eSjoerg         // This uncle is resolved, so pop it from the worklist.
421306f32e7eSjoerg         Worklist.pop_back();
421406f32e7eSjoerg       }
421506f32e7eSjoerg     }
421606f32e7eSjoerg   }
421706f32e7eSjoerg 
421806f32e7eSjoerg   if (FirstUnwindPad) {
421906f32e7eSjoerg     if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FPI.getParentPad())) {
422006f32e7eSjoerg       BasicBlock *SwitchUnwindDest = CatchSwitch->getUnwindDest();
422106f32e7eSjoerg       Value *SwitchUnwindPad;
422206f32e7eSjoerg       if (SwitchUnwindDest)
422306f32e7eSjoerg         SwitchUnwindPad = SwitchUnwindDest->getFirstNonPHI();
422406f32e7eSjoerg       else
422506f32e7eSjoerg         SwitchUnwindPad = ConstantTokenNone::get(FPI.getContext());
422606f32e7eSjoerg       Assert(SwitchUnwindPad == FirstUnwindPad,
422706f32e7eSjoerg              "Unwind edges out of a catch must have the same unwind dest as "
422806f32e7eSjoerg              "the parent catchswitch",
422906f32e7eSjoerg              &FPI, FirstUser, CatchSwitch);
423006f32e7eSjoerg     }
423106f32e7eSjoerg   }
423206f32e7eSjoerg 
423306f32e7eSjoerg   visitInstruction(FPI);
423406f32e7eSjoerg }
423506f32e7eSjoerg 
visitCatchSwitchInst(CatchSwitchInst & CatchSwitch)423606f32e7eSjoerg void Verifier::visitCatchSwitchInst(CatchSwitchInst &CatchSwitch) {
423706f32e7eSjoerg   BasicBlock *BB = CatchSwitch.getParent();
423806f32e7eSjoerg 
423906f32e7eSjoerg   Function *F = BB->getParent();
424006f32e7eSjoerg   Assert(F->hasPersonalityFn(),
424106f32e7eSjoerg          "CatchSwitchInst needs to be in a function with a personality.",
424206f32e7eSjoerg          &CatchSwitch);
424306f32e7eSjoerg 
424406f32e7eSjoerg   // The catchswitch instruction must be the first non-PHI instruction in the
424506f32e7eSjoerg   // block.
424606f32e7eSjoerg   Assert(BB->getFirstNonPHI() == &CatchSwitch,
424706f32e7eSjoerg          "CatchSwitchInst not the first non-PHI instruction in the block.",
424806f32e7eSjoerg          &CatchSwitch);
424906f32e7eSjoerg 
425006f32e7eSjoerg   auto *ParentPad = CatchSwitch.getParentPad();
425106f32e7eSjoerg   Assert(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
425206f32e7eSjoerg          "CatchSwitchInst has an invalid parent.", ParentPad);
425306f32e7eSjoerg 
425406f32e7eSjoerg   if (BasicBlock *UnwindDest = CatchSwitch.getUnwindDest()) {
425506f32e7eSjoerg     Instruction *I = UnwindDest->getFirstNonPHI();
425606f32e7eSjoerg     Assert(I->isEHPad() && !isa<LandingPadInst>(I),
425706f32e7eSjoerg            "CatchSwitchInst must unwind to an EH block which is not a "
425806f32e7eSjoerg            "landingpad.",
425906f32e7eSjoerg            &CatchSwitch);
426006f32e7eSjoerg 
426106f32e7eSjoerg     // Record catchswitch sibling unwinds for verifySiblingFuncletUnwinds
426206f32e7eSjoerg     if (getParentPad(I) == ParentPad)
426306f32e7eSjoerg       SiblingFuncletInfo[&CatchSwitch] = &CatchSwitch;
426406f32e7eSjoerg   }
426506f32e7eSjoerg 
426606f32e7eSjoerg   Assert(CatchSwitch.getNumHandlers() != 0,
426706f32e7eSjoerg          "CatchSwitchInst cannot have empty handler list", &CatchSwitch);
426806f32e7eSjoerg 
426906f32e7eSjoerg   for (BasicBlock *Handler : CatchSwitch.handlers()) {
427006f32e7eSjoerg     Assert(isa<CatchPadInst>(Handler->getFirstNonPHI()),
427106f32e7eSjoerg            "CatchSwitchInst handlers must be catchpads", &CatchSwitch, Handler);
427206f32e7eSjoerg   }
427306f32e7eSjoerg 
427406f32e7eSjoerg   visitEHPadPredecessors(CatchSwitch);
427506f32e7eSjoerg   visitTerminator(CatchSwitch);
427606f32e7eSjoerg }
427706f32e7eSjoerg 
visitCleanupReturnInst(CleanupReturnInst & CRI)427806f32e7eSjoerg void Verifier::visitCleanupReturnInst(CleanupReturnInst &CRI) {
427906f32e7eSjoerg   Assert(isa<CleanupPadInst>(CRI.getOperand(0)),
428006f32e7eSjoerg          "CleanupReturnInst needs to be provided a CleanupPad", &CRI,
428106f32e7eSjoerg          CRI.getOperand(0));
428206f32e7eSjoerg 
428306f32e7eSjoerg   if (BasicBlock *UnwindDest = CRI.getUnwindDest()) {
428406f32e7eSjoerg     Instruction *I = UnwindDest->getFirstNonPHI();
428506f32e7eSjoerg     Assert(I->isEHPad() && !isa<LandingPadInst>(I),
428606f32e7eSjoerg            "CleanupReturnInst must unwind to an EH block which is not a "
428706f32e7eSjoerg            "landingpad.",
428806f32e7eSjoerg            &CRI);
428906f32e7eSjoerg   }
429006f32e7eSjoerg 
429106f32e7eSjoerg   visitTerminator(CRI);
429206f32e7eSjoerg }
429306f32e7eSjoerg 
verifyDominatesUse(Instruction & I,unsigned i)429406f32e7eSjoerg void Verifier::verifyDominatesUse(Instruction &I, unsigned i) {
429506f32e7eSjoerg   Instruction *Op = cast<Instruction>(I.getOperand(i));
429606f32e7eSjoerg   // If the we have an invalid invoke, don't try to compute the dominance.
429706f32e7eSjoerg   // We already reject it in the invoke specific checks and the dominance
429806f32e7eSjoerg   // computation doesn't handle multiple edges.
429906f32e7eSjoerg   if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
430006f32e7eSjoerg     if (II->getNormalDest() == II->getUnwindDest())
430106f32e7eSjoerg       return;
430206f32e7eSjoerg   }
430306f32e7eSjoerg 
430406f32e7eSjoerg   // Quick check whether the def has already been encountered in the same block.
430506f32e7eSjoerg   // PHI nodes are not checked to prevent accepting preceding PHIs, because PHI
430606f32e7eSjoerg   // uses are defined to happen on the incoming edge, not at the instruction.
430706f32e7eSjoerg   //
430806f32e7eSjoerg   // FIXME: If this operand is a MetadataAsValue (wrapping a LocalAsMetadata)
430906f32e7eSjoerg   // wrapping an SSA value, assert that we've already encountered it.  See
431006f32e7eSjoerg   // related FIXME in Mapper::mapLocalAsMetadata in ValueMapper.cpp.
431106f32e7eSjoerg   if (!isa<PHINode>(I) && InstsInThisBlock.count(Op))
431206f32e7eSjoerg     return;
431306f32e7eSjoerg 
431406f32e7eSjoerg   const Use &U = I.getOperandUse(i);
431506f32e7eSjoerg   Assert(DT.dominates(Op, U),
431606f32e7eSjoerg          "Instruction does not dominate all uses!", Op, &I);
431706f32e7eSjoerg }
431806f32e7eSjoerg 
visitDereferenceableMetadata(Instruction & I,MDNode * MD)431906f32e7eSjoerg void Verifier::visitDereferenceableMetadata(Instruction& I, MDNode* MD) {
432006f32e7eSjoerg   Assert(I.getType()->isPointerTy(), "dereferenceable, dereferenceable_or_null "
432106f32e7eSjoerg          "apply only to pointer types", &I);
432206f32e7eSjoerg   Assert((isa<LoadInst>(I) || isa<IntToPtrInst>(I)),
432306f32e7eSjoerg          "dereferenceable, dereferenceable_or_null apply only to load"
432406f32e7eSjoerg          " and inttoptr instructions, use attributes for calls or invokes", &I);
432506f32e7eSjoerg   Assert(MD->getNumOperands() == 1, "dereferenceable, dereferenceable_or_null "
432606f32e7eSjoerg          "take one operand!", &I);
432706f32e7eSjoerg   ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(MD->getOperand(0));
432806f32e7eSjoerg   Assert(CI && CI->getType()->isIntegerTy(64), "dereferenceable, "
432906f32e7eSjoerg          "dereferenceable_or_null metadata value must be an i64!", &I);
433006f32e7eSjoerg }
433106f32e7eSjoerg 
visitProfMetadata(Instruction & I,MDNode * MD)433206f32e7eSjoerg void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
433306f32e7eSjoerg   Assert(MD->getNumOperands() >= 2,
433406f32e7eSjoerg          "!prof annotations should have no less than 2 operands", MD);
433506f32e7eSjoerg 
433606f32e7eSjoerg   // Check first operand.
433706f32e7eSjoerg   Assert(MD->getOperand(0) != nullptr, "first operand should not be null", MD);
433806f32e7eSjoerg   Assert(isa<MDString>(MD->getOperand(0)),
433906f32e7eSjoerg          "expected string with name of the !prof annotation", MD);
434006f32e7eSjoerg   MDString *MDS = cast<MDString>(MD->getOperand(0));
434106f32e7eSjoerg   StringRef ProfName = MDS->getString();
434206f32e7eSjoerg 
434306f32e7eSjoerg   // Check consistency of !prof branch_weights metadata.
434406f32e7eSjoerg   if (ProfName.equals("branch_weights")) {
4345*da58b97aSjoerg     if (isa<InvokeInst>(&I)) {
4346*da58b97aSjoerg       Assert(MD->getNumOperands() == 2 || MD->getNumOperands() == 3,
4347*da58b97aSjoerg              "Wrong number of InvokeInst branch_weights operands", MD);
4348*da58b97aSjoerg     } else {
434906f32e7eSjoerg       unsigned ExpectedNumOperands = 0;
435006f32e7eSjoerg       if (BranchInst *BI = dyn_cast<BranchInst>(&I))
435106f32e7eSjoerg         ExpectedNumOperands = BI->getNumSuccessors();
435206f32e7eSjoerg       else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I))
435306f32e7eSjoerg         ExpectedNumOperands = SI->getNumSuccessors();
4354*da58b97aSjoerg       else if (isa<CallInst>(&I))
435506f32e7eSjoerg         ExpectedNumOperands = 1;
435606f32e7eSjoerg       else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I))
435706f32e7eSjoerg         ExpectedNumOperands = IBI->getNumDestinations();
435806f32e7eSjoerg       else if (isa<SelectInst>(&I))
435906f32e7eSjoerg         ExpectedNumOperands = 2;
436006f32e7eSjoerg       else
436106f32e7eSjoerg         CheckFailed("!prof branch_weights are not allowed for this instruction",
436206f32e7eSjoerg                     MD);
436306f32e7eSjoerg 
436406f32e7eSjoerg       Assert(MD->getNumOperands() == 1 + ExpectedNumOperands,
436506f32e7eSjoerg              "Wrong number of operands", MD);
4366*da58b97aSjoerg     }
436706f32e7eSjoerg     for (unsigned i = 1; i < MD->getNumOperands(); ++i) {
436806f32e7eSjoerg       auto &MDO = MD->getOperand(i);
436906f32e7eSjoerg       Assert(MDO, "second operand should not be null", MD);
437006f32e7eSjoerg       Assert(mdconst::dyn_extract<ConstantInt>(MDO),
437106f32e7eSjoerg              "!prof brunch_weights operand is not a const int");
437206f32e7eSjoerg     }
437306f32e7eSjoerg   }
437406f32e7eSjoerg }
437506f32e7eSjoerg 
visitAnnotationMetadata(MDNode * Annotation)4376*da58b97aSjoerg void Verifier::visitAnnotationMetadata(MDNode *Annotation) {
4377*da58b97aSjoerg   Assert(isa<MDTuple>(Annotation), "annotation must be a tuple");
4378*da58b97aSjoerg   Assert(Annotation->getNumOperands() >= 1,
4379*da58b97aSjoerg          "annotation must have at least one operand");
4380*da58b97aSjoerg   for (const MDOperand &Op : Annotation->operands())
4381*da58b97aSjoerg     Assert(isa<MDString>(Op.get()), "operands must be strings");
4382*da58b97aSjoerg }
4383*da58b97aSjoerg 
438406f32e7eSjoerg /// verifyInstruction - Verify that an instruction is well formed.
438506f32e7eSjoerg ///
visitInstruction(Instruction & I)438606f32e7eSjoerg void Verifier::visitInstruction(Instruction &I) {
438706f32e7eSjoerg   BasicBlock *BB = I.getParent();
438806f32e7eSjoerg   Assert(BB, "Instruction not embedded in basic block!", &I);
438906f32e7eSjoerg 
439006f32e7eSjoerg   if (!isa<PHINode>(I)) {   // Check that non-phi nodes are not self referential
439106f32e7eSjoerg     for (User *U : I.users()) {
439206f32e7eSjoerg       Assert(U != (User *)&I || !DT.isReachableFromEntry(BB),
439306f32e7eSjoerg              "Only PHI nodes may reference their own value!", &I);
439406f32e7eSjoerg     }
439506f32e7eSjoerg   }
439606f32e7eSjoerg 
439706f32e7eSjoerg   // Check that void typed values don't have names
439806f32e7eSjoerg   Assert(!I.getType()->isVoidTy() || !I.hasName(),
439906f32e7eSjoerg          "Instruction has a name, but provides a void value!", &I);
440006f32e7eSjoerg 
440106f32e7eSjoerg   // Check that the return value of the instruction is either void or a legal
440206f32e7eSjoerg   // value type.
440306f32e7eSjoerg   Assert(I.getType()->isVoidTy() || I.getType()->isFirstClassType(),
440406f32e7eSjoerg          "Instruction returns a non-scalar type!", &I);
440506f32e7eSjoerg 
440606f32e7eSjoerg   // Check that the instruction doesn't produce metadata. Calls are already
440706f32e7eSjoerg   // checked against the callee type.
440806f32e7eSjoerg   Assert(!I.getType()->isMetadataTy() || isa<CallInst>(I) || isa<InvokeInst>(I),
440906f32e7eSjoerg          "Invalid use of metadata!", &I);
441006f32e7eSjoerg 
441106f32e7eSjoerg   // Check that all uses of the instruction, if they are instructions
441206f32e7eSjoerg   // themselves, actually have parent basic blocks.  If the use is not an
441306f32e7eSjoerg   // instruction, it is an error!
441406f32e7eSjoerg   for (Use &U : I.uses()) {
441506f32e7eSjoerg     if (Instruction *Used = dyn_cast<Instruction>(U.getUser()))
441606f32e7eSjoerg       Assert(Used->getParent() != nullptr,
441706f32e7eSjoerg              "Instruction referencing"
441806f32e7eSjoerg              " instruction not embedded in a basic block!",
441906f32e7eSjoerg              &I, Used);
442006f32e7eSjoerg     else {
442106f32e7eSjoerg       CheckFailed("Use of instruction is not an instruction!", U);
442206f32e7eSjoerg       return;
442306f32e7eSjoerg     }
442406f32e7eSjoerg   }
442506f32e7eSjoerg 
442606f32e7eSjoerg   // Get a pointer to the call base of the instruction if it is some form of
442706f32e7eSjoerg   // call.
442806f32e7eSjoerg   const CallBase *CBI = dyn_cast<CallBase>(&I);
442906f32e7eSjoerg 
443006f32e7eSjoerg   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
443106f32e7eSjoerg     Assert(I.getOperand(i) != nullptr, "Instruction has null operand!", &I);
443206f32e7eSjoerg 
443306f32e7eSjoerg     // Check to make sure that only first-class-values are operands to
443406f32e7eSjoerg     // instructions.
443506f32e7eSjoerg     if (!I.getOperand(i)->getType()->isFirstClassType()) {
443606f32e7eSjoerg       Assert(false, "Instruction operands must be first-class values!", &I);
443706f32e7eSjoerg     }
443806f32e7eSjoerg 
443906f32e7eSjoerg     if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
444006f32e7eSjoerg       // Check to make sure that the "address of" an intrinsic function is never
444106f32e7eSjoerg       // taken.
444206f32e7eSjoerg       Assert(!F->isIntrinsic() ||
444306f32e7eSjoerg                  (CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i)),
444406f32e7eSjoerg              "Cannot take the address of an intrinsic!", &I);
444506f32e7eSjoerg       Assert(
444606f32e7eSjoerg           !F->isIntrinsic() || isa<CallInst>(I) ||
444706f32e7eSjoerg               F->getIntrinsicID() == Intrinsic::donothing ||
4448*da58b97aSjoerg               F->getIntrinsicID() == Intrinsic::seh_try_begin ||
4449*da58b97aSjoerg               F->getIntrinsicID() == Intrinsic::seh_try_end ||
4450*da58b97aSjoerg               F->getIntrinsicID() == Intrinsic::seh_scope_begin ||
4451*da58b97aSjoerg               F->getIntrinsicID() == Intrinsic::seh_scope_end ||
445206f32e7eSjoerg               F->getIntrinsicID() == Intrinsic::coro_resume ||
445306f32e7eSjoerg               F->getIntrinsicID() == Intrinsic::coro_destroy ||
445406f32e7eSjoerg               F->getIntrinsicID() == Intrinsic::experimental_patchpoint_void ||
445506f32e7eSjoerg               F->getIntrinsicID() == Intrinsic::experimental_patchpoint_i64 ||
445606f32e7eSjoerg               F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint ||
4457*da58b97aSjoerg               F->getIntrinsicID() == Intrinsic::wasm_rethrow,
445806f32e7eSjoerg           "Cannot invoke an intrinsic other than donothing, patchpoint, "
445906f32e7eSjoerg           "statepoint, coro_resume or coro_destroy",
446006f32e7eSjoerg           &I);
446106f32e7eSjoerg       Assert(F->getParent() == &M, "Referencing function in another module!",
446206f32e7eSjoerg              &I, &M, F, F->getParent());
446306f32e7eSjoerg     } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
446406f32e7eSjoerg       Assert(OpBB->getParent() == BB->getParent(),
446506f32e7eSjoerg              "Referring to a basic block in another function!", &I);
446606f32e7eSjoerg     } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
446706f32e7eSjoerg       Assert(OpArg->getParent() == BB->getParent(),
446806f32e7eSjoerg              "Referring to an argument in another function!", &I);
446906f32e7eSjoerg     } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) {
447006f32e7eSjoerg       Assert(GV->getParent() == &M, "Referencing global in another module!", &I,
447106f32e7eSjoerg              &M, GV, GV->getParent());
447206f32e7eSjoerg     } else if (isa<Instruction>(I.getOperand(i))) {
447306f32e7eSjoerg       verifyDominatesUse(I, i);
447406f32e7eSjoerg     } else if (isa<InlineAsm>(I.getOperand(i))) {
447506f32e7eSjoerg       Assert(CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i),
447606f32e7eSjoerg              "Cannot take the address of an inline asm!", &I);
447706f32e7eSjoerg     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(i))) {
447806f32e7eSjoerg       if (CE->getType()->isPtrOrPtrVectorTy() ||
447906f32e7eSjoerg           !DL.getNonIntegralAddressSpaces().empty()) {
448006f32e7eSjoerg         // If we have a ConstantExpr pointer, we need to see if it came from an
448106f32e7eSjoerg         // illegal bitcast.  If the datalayout string specifies non-integral
448206f32e7eSjoerg         // address spaces then we also need to check for illegal ptrtoint and
448306f32e7eSjoerg         // inttoptr expressions.
448406f32e7eSjoerg         visitConstantExprsRecursively(CE);
448506f32e7eSjoerg       }
448606f32e7eSjoerg     }
448706f32e7eSjoerg   }
448806f32e7eSjoerg 
448906f32e7eSjoerg   if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpmath)) {
449006f32e7eSjoerg     Assert(I.getType()->isFPOrFPVectorTy(),
449106f32e7eSjoerg            "fpmath requires a floating point result!", &I);
449206f32e7eSjoerg     Assert(MD->getNumOperands() == 1, "fpmath takes one operand!", &I);
449306f32e7eSjoerg     if (ConstantFP *CFP0 =
449406f32e7eSjoerg             mdconst::dyn_extract_or_null<ConstantFP>(MD->getOperand(0))) {
449506f32e7eSjoerg       const APFloat &Accuracy = CFP0->getValueAPF();
449606f32e7eSjoerg       Assert(&Accuracy.getSemantics() == &APFloat::IEEEsingle(),
449706f32e7eSjoerg              "fpmath accuracy must have float type", &I);
449806f32e7eSjoerg       Assert(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(),
449906f32e7eSjoerg              "fpmath accuracy not a positive number!", &I);
450006f32e7eSjoerg     } else {
450106f32e7eSjoerg       Assert(false, "invalid fpmath accuracy!", &I);
450206f32e7eSjoerg     }
450306f32e7eSjoerg   }
450406f32e7eSjoerg 
450506f32e7eSjoerg   if (MDNode *Range = I.getMetadata(LLVMContext::MD_range)) {
450606f32e7eSjoerg     Assert(isa<LoadInst>(I) || isa<CallInst>(I) || isa<InvokeInst>(I),
450706f32e7eSjoerg            "Ranges are only for loads, calls and invokes!", &I);
450806f32e7eSjoerg     visitRangeMetadata(I, Range, I.getType());
450906f32e7eSjoerg   }
451006f32e7eSjoerg 
451106f32e7eSjoerg   if (I.getMetadata(LLVMContext::MD_nonnull)) {
451206f32e7eSjoerg     Assert(I.getType()->isPointerTy(), "nonnull applies only to pointer types",
451306f32e7eSjoerg            &I);
451406f32e7eSjoerg     Assert(isa<LoadInst>(I),
451506f32e7eSjoerg            "nonnull applies only to load instructions, use attributes"
451606f32e7eSjoerg            " for calls or invokes",
451706f32e7eSjoerg            &I);
451806f32e7eSjoerg   }
451906f32e7eSjoerg 
452006f32e7eSjoerg   if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable))
452106f32e7eSjoerg     visitDereferenceableMetadata(I, MD);
452206f32e7eSjoerg 
452306f32e7eSjoerg   if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable_or_null))
452406f32e7eSjoerg     visitDereferenceableMetadata(I, MD);
452506f32e7eSjoerg 
452606f32e7eSjoerg   if (MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa))
452706f32e7eSjoerg     TBAAVerifyHelper.visitTBAAMetadata(I, TBAA);
452806f32e7eSjoerg 
452906f32e7eSjoerg   if (MDNode *AlignMD = I.getMetadata(LLVMContext::MD_align)) {
453006f32e7eSjoerg     Assert(I.getType()->isPointerTy(), "align applies only to pointer types",
453106f32e7eSjoerg            &I);
453206f32e7eSjoerg     Assert(isa<LoadInst>(I), "align applies only to load instructions, "
453306f32e7eSjoerg            "use attributes for calls or invokes", &I);
453406f32e7eSjoerg     Assert(AlignMD->getNumOperands() == 1, "align takes one operand!", &I);
453506f32e7eSjoerg     ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(AlignMD->getOperand(0));
453606f32e7eSjoerg     Assert(CI && CI->getType()->isIntegerTy(64),
453706f32e7eSjoerg            "align metadata value must be an i64!", &I);
453806f32e7eSjoerg     uint64_t Align = CI->getZExtValue();
453906f32e7eSjoerg     Assert(isPowerOf2_64(Align),
454006f32e7eSjoerg            "align metadata value must be a power of 2!", &I);
454106f32e7eSjoerg     Assert(Align <= Value::MaximumAlignment,
454206f32e7eSjoerg            "alignment is larger that implementation defined limit", &I);
454306f32e7eSjoerg   }
454406f32e7eSjoerg 
454506f32e7eSjoerg   if (MDNode *MD = I.getMetadata(LLVMContext::MD_prof))
454606f32e7eSjoerg     visitProfMetadata(I, MD);
454706f32e7eSjoerg 
4548*da58b97aSjoerg   if (MDNode *Annotation = I.getMetadata(LLVMContext::MD_annotation))
4549*da58b97aSjoerg     visitAnnotationMetadata(Annotation);
4550*da58b97aSjoerg 
455106f32e7eSjoerg   if (MDNode *N = I.getDebugLoc().getAsMDNode()) {
455206f32e7eSjoerg     AssertDI(isa<DILocation>(N), "invalid !dbg metadata attachment", &I, N);
4553*da58b97aSjoerg     visitMDNode(*N, AreDebugLocsAllowed::Yes);
455406f32e7eSjoerg   }
455506f32e7eSjoerg 
455606f32e7eSjoerg   if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) {
455706f32e7eSjoerg     verifyFragmentExpression(*DII);
455806f32e7eSjoerg     verifyNotEntryValue(*DII);
455906f32e7eSjoerg   }
456006f32e7eSjoerg 
4561*da58b97aSjoerg   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
4562*da58b97aSjoerg   I.getAllMetadata(MDs);
4563*da58b97aSjoerg   for (auto Attachment : MDs) {
4564*da58b97aSjoerg     unsigned Kind = Attachment.first;
4565*da58b97aSjoerg     auto AllowLocs =
4566*da58b97aSjoerg         (Kind == LLVMContext::MD_dbg || Kind == LLVMContext::MD_loop)
4567*da58b97aSjoerg             ? AreDebugLocsAllowed::Yes
4568*da58b97aSjoerg             : AreDebugLocsAllowed::No;
4569*da58b97aSjoerg     visitMDNode(*Attachment.second, AllowLocs);
4570*da58b97aSjoerg   }
4571*da58b97aSjoerg 
457206f32e7eSjoerg   InstsInThisBlock.insert(&I);
457306f32e7eSjoerg }
457406f32e7eSjoerg 
457506f32e7eSjoerg /// Allow intrinsics to be verified in different ways.
visitIntrinsicCall(Intrinsic::ID ID,CallBase & Call)457606f32e7eSjoerg void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
457706f32e7eSjoerg   Function *IF = Call.getCalledFunction();
457806f32e7eSjoerg   Assert(IF->isDeclaration(), "Intrinsic functions should never be defined!",
457906f32e7eSjoerg          IF);
458006f32e7eSjoerg 
458106f32e7eSjoerg   // Verify that the intrinsic prototype lines up with what the .td files
458206f32e7eSjoerg   // describe.
458306f32e7eSjoerg   FunctionType *IFTy = IF->getFunctionType();
458406f32e7eSjoerg   bool IsVarArg = IFTy->isVarArg();
458506f32e7eSjoerg 
458606f32e7eSjoerg   SmallVector<Intrinsic::IITDescriptor, 8> Table;
458706f32e7eSjoerg   getIntrinsicInfoTableEntries(ID, Table);
458806f32e7eSjoerg   ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
458906f32e7eSjoerg 
459006f32e7eSjoerg   // Walk the descriptors to extract overloaded types.
459106f32e7eSjoerg   SmallVector<Type *, 4> ArgTys;
459206f32e7eSjoerg   Intrinsic::MatchIntrinsicTypesResult Res =
459306f32e7eSjoerg       Intrinsic::matchIntrinsicSignature(IFTy, TableRef, ArgTys);
459406f32e7eSjoerg   Assert(Res != Intrinsic::MatchIntrinsicTypes_NoMatchRet,
459506f32e7eSjoerg          "Intrinsic has incorrect return type!", IF);
459606f32e7eSjoerg   Assert(Res != Intrinsic::MatchIntrinsicTypes_NoMatchArg,
459706f32e7eSjoerg          "Intrinsic has incorrect argument type!", IF);
459806f32e7eSjoerg 
459906f32e7eSjoerg   // Verify if the intrinsic call matches the vararg property.
460006f32e7eSjoerg   if (IsVarArg)
460106f32e7eSjoerg     Assert(!Intrinsic::matchIntrinsicVarArg(IsVarArg, TableRef),
460206f32e7eSjoerg            "Intrinsic was not defined with variable arguments!", IF);
460306f32e7eSjoerg   else
460406f32e7eSjoerg     Assert(!Intrinsic::matchIntrinsicVarArg(IsVarArg, TableRef),
460506f32e7eSjoerg            "Callsite was not defined with variable arguments!", IF);
460606f32e7eSjoerg 
460706f32e7eSjoerg   // All descriptors should be absorbed by now.
460806f32e7eSjoerg   Assert(TableRef.empty(), "Intrinsic has too few arguments!", IF);
460906f32e7eSjoerg 
461006f32e7eSjoerg   // Now that we have the intrinsic ID and the actual argument types (and we
461106f32e7eSjoerg   // know they are legal for the intrinsic!) get the intrinsic name through the
461206f32e7eSjoerg   // usual means.  This allows us to verify the mangling of argument types into
461306f32e7eSjoerg   // the name.
4614*da58b97aSjoerg   const std::string ExpectedName =
4615*da58b97aSjoerg       Intrinsic::getName(ID, ArgTys, IF->getParent(), IFTy);
461606f32e7eSjoerg   Assert(ExpectedName == IF->getName(),
461706f32e7eSjoerg          "Intrinsic name not mangled correctly for type arguments! "
461806f32e7eSjoerg          "Should be: " +
461906f32e7eSjoerg              ExpectedName,
462006f32e7eSjoerg          IF);
462106f32e7eSjoerg 
462206f32e7eSjoerg   // If the intrinsic takes MDNode arguments, verify that they are either global
462306f32e7eSjoerg   // or are local to *this* function.
4624*da58b97aSjoerg   for (Value *V : Call.args()) {
462506f32e7eSjoerg     if (auto *MD = dyn_cast<MetadataAsValue>(V))
462606f32e7eSjoerg       visitMetadataAsValue(*MD, Call.getCaller());
4627*da58b97aSjoerg     if (auto *Const = dyn_cast<Constant>(V))
4628*da58b97aSjoerg       Assert(!Const->getType()->isX86_AMXTy(),
4629*da58b97aSjoerg              "const x86_amx is not allowed in argument!");
4630*da58b97aSjoerg   }
463106f32e7eSjoerg 
463206f32e7eSjoerg   switch (ID) {
463306f32e7eSjoerg   default:
463406f32e7eSjoerg     break;
4635*da58b97aSjoerg   case Intrinsic::assume: {
4636*da58b97aSjoerg     for (auto &Elem : Call.bundle_op_infos()) {
4637*da58b97aSjoerg       Assert(Elem.Tag->getKey() == "ignore" ||
4638*da58b97aSjoerg                  Attribute::isExistingAttribute(Elem.Tag->getKey()),
4639*da58b97aSjoerg              "tags must be valid attribute names");
4640*da58b97aSjoerg       Attribute::AttrKind Kind =
4641*da58b97aSjoerg           Attribute::getAttrKindFromName(Elem.Tag->getKey());
4642*da58b97aSjoerg       unsigned ArgCount = Elem.End - Elem.Begin;
4643*da58b97aSjoerg       if (Kind == Attribute::Alignment) {
4644*da58b97aSjoerg         Assert(ArgCount <= 3 && ArgCount >= 2,
4645*da58b97aSjoerg                "alignment assumptions should have 2 or 3 arguments");
4646*da58b97aSjoerg         Assert(Call.getOperand(Elem.Begin)->getType()->isPointerTy(),
4647*da58b97aSjoerg                "first argument should be a pointer");
4648*da58b97aSjoerg         Assert(Call.getOperand(Elem.Begin + 1)->getType()->isIntegerTy(),
4649*da58b97aSjoerg                "second argument should be an integer");
4650*da58b97aSjoerg         if (ArgCount == 3)
4651*da58b97aSjoerg           Assert(Call.getOperand(Elem.Begin + 2)->getType()->isIntegerTy(),
4652*da58b97aSjoerg                  "third argument should be an integer if present");
4653*da58b97aSjoerg         return;
4654*da58b97aSjoerg       }
4655*da58b97aSjoerg       Assert(ArgCount <= 2, "to many arguments");
4656*da58b97aSjoerg       if (Kind == Attribute::None)
4657*da58b97aSjoerg         break;
4658*da58b97aSjoerg       if (Attribute::doesAttrKindHaveArgument(Kind)) {
4659*da58b97aSjoerg         Assert(ArgCount == 2, "this attribute should have 2 arguments");
4660*da58b97aSjoerg         Assert(isa<ConstantInt>(Call.getOperand(Elem.Begin + 1)),
4661*da58b97aSjoerg                "the second argument should be a constant integral value");
4662*da58b97aSjoerg       } else if (isFuncOnlyAttr(Kind)) {
4663*da58b97aSjoerg         Assert((ArgCount) == 0, "this attribute has no argument");
4664*da58b97aSjoerg       } else if (!isFuncOrArgAttr(Kind)) {
4665*da58b97aSjoerg         Assert((ArgCount) == 1, "this attribute should have one argument");
4666*da58b97aSjoerg       }
4667*da58b97aSjoerg     }
4668*da58b97aSjoerg     break;
4669*da58b97aSjoerg   }
467006f32e7eSjoerg   case Intrinsic::coro_id: {
467106f32e7eSjoerg     auto *InfoArg = Call.getArgOperand(3)->stripPointerCasts();
467206f32e7eSjoerg     if (isa<ConstantPointerNull>(InfoArg))
467306f32e7eSjoerg       break;
467406f32e7eSjoerg     auto *GV = dyn_cast<GlobalVariable>(InfoArg);
467506f32e7eSjoerg     Assert(GV && GV->isConstant() && GV->hasDefinitiveInitializer(),
4676*da58b97aSjoerg            "info argument of llvm.coro.id must refer to an initialized "
467706f32e7eSjoerg            "constant");
467806f32e7eSjoerg     Constant *Init = GV->getInitializer();
467906f32e7eSjoerg     Assert(isa<ConstantStruct>(Init) || isa<ConstantArray>(Init),
4680*da58b97aSjoerg            "info argument of llvm.coro.id must refer to either a struct or "
468106f32e7eSjoerg            "an array");
468206f32e7eSjoerg     break;
468306f32e7eSjoerg   }
4684*da58b97aSjoerg #define INSTRUCTION(NAME, NARGS, ROUND_MODE, INTRINSIC)                        \
4685*da58b97aSjoerg   case Intrinsic::INTRINSIC:
4686*da58b97aSjoerg #include "llvm/IR/ConstrainedOps.def"
468706f32e7eSjoerg     visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(Call));
468806f32e7eSjoerg     break;
468906f32e7eSjoerg   case Intrinsic::dbg_declare: // llvm.dbg.declare
469006f32e7eSjoerg     Assert(isa<MetadataAsValue>(Call.getArgOperand(0)),
469106f32e7eSjoerg            "invalid llvm.dbg.declare intrinsic call 1", Call);
469206f32e7eSjoerg     visitDbgIntrinsic("declare", cast<DbgVariableIntrinsic>(Call));
469306f32e7eSjoerg     break;
469406f32e7eSjoerg   case Intrinsic::dbg_addr: // llvm.dbg.addr
469506f32e7eSjoerg     visitDbgIntrinsic("addr", cast<DbgVariableIntrinsic>(Call));
469606f32e7eSjoerg     break;
469706f32e7eSjoerg   case Intrinsic::dbg_value: // llvm.dbg.value
469806f32e7eSjoerg     visitDbgIntrinsic("value", cast<DbgVariableIntrinsic>(Call));
469906f32e7eSjoerg     break;
470006f32e7eSjoerg   case Intrinsic::dbg_label: // llvm.dbg.label
470106f32e7eSjoerg     visitDbgLabelIntrinsic("label", cast<DbgLabelInst>(Call));
470206f32e7eSjoerg     break;
470306f32e7eSjoerg   case Intrinsic::memcpy:
4704*da58b97aSjoerg   case Intrinsic::memcpy_inline:
470506f32e7eSjoerg   case Intrinsic::memmove:
470606f32e7eSjoerg   case Intrinsic::memset: {
470706f32e7eSjoerg     const auto *MI = cast<MemIntrinsic>(&Call);
470806f32e7eSjoerg     auto IsValidAlignment = [&](unsigned Alignment) -> bool {
470906f32e7eSjoerg       return Alignment == 0 || isPowerOf2_32(Alignment);
471006f32e7eSjoerg     };
471106f32e7eSjoerg     Assert(IsValidAlignment(MI->getDestAlignment()),
471206f32e7eSjoerg            "alignment of arg 0 of memory intrinsic must be 0 or a power of 2",
471306f32e7eSjoerg            Call);
471406f32e7eSjoerg     if (const auto *MTI = dyn_cast<MemTransferInst>(MI)) {
471506f32e7eSjoerg       Assert(IsValidAlignment(MTI->getSourceAlignment()),
471606f32e7eSjoerg              "alignment of arg 1 of memory intrinsic must be 0 or a power of 2",
471706f32e7eSjoerg              Call);
471806f32e7eSjoerg     }
471906f32e7eSjoerg 
472006f32e7eSjoerg     break;
472106f32e7eSjoerg   }
472206f32e7eSjoerg   case Intrinsic::memcpy_element_unordered_atomic:
472306f32e7eSjoerg   case Intrinsic::memmove_element_unordered_atomic:
472406f32e7eSjoerg   case Intrinsic::memset_element_unordered_atomic: {
472506f32e7eSjoerg     const auto *AMI = cast<AtomicMemIntrinsic>(&Call);
472606f32e7eSjoerg 
472706f32e7eSjoerg     ConstantInt *ElementSizeCI =
472806f32e7eSjoerg         cast<ConstantInt>(AMI->getRawElementSizeInBytes());
472906f32e7eSjoerg     const APInt &ElementSizeVal = ElementSizeCI->getValue();
473006f32e7eSjoerg     Assert(ElementSizeVal.isPowerOf2(),
473106f32e7eSjoerg            "element size of the element-wise atomic memory intrinsic "
473206f32e7eSjoerg            "must be a power of 2",
473306f32e7eSjoerg            Call);
473406f32e7eSjoerg 
473506f32e7eSjoerg     auto IsValidAlignment = [&](uint64_t Alignment) {
473606f32e7eSjoerg       return isPowerOf2_64(Alignment) && ElementSizeVal.ule(Alignment);
473706f32e7eSjoerg     };
473806f32e7eSjoerg     uint64_t DstAlignment = AMI->getDestAlignment();
473906f32e7eSjoerg     Assert(IsValidAlignment(DstAlignment),
474006f32e7eSjoerg            "incorrect alignment of the destination argument", Call);
474106f32e7eSjoerg     if (const auto *AMT = dyn_cast<AtomicMemTransferInst>(AMI)) {
474206f32e7eSjoerg       uint64_t SrcAlignment = AMT->getSourceAlignment();
474306f32e7eSjoerg       Assert(IsValidAlignment(SrcAlignment),
474406f32e7eSjoerg              "incorrect alignment of the source argument", Call);
474506f32e7eSjoerg     }
474606f32e7eSjoerg     break;
474706f32e7eSjoerg   }
4748*da58b97aSjoerg   case Intrinsic::call_preallocated_setup: {
4749*da58b97aSjoerg     auto *NumArgs = dyn_cast<ConstantInt>(Call.getArgOperand(0));
4750*da58b97aSjoerg     Assert(NumArgs != nullptr,
4751*da58b97aSjoerg            "llvm.call.preallocated.setup argument must be a constant");
4752*da58b97aSjoerg     bool FoundCall = false;
4753*da58b97aSjoerg     for (User *U : Call.users()) {
4754*da58b97aSjoerg       auto *UseCall = dyn_cast<CallBase>(U);
4755*da58b97aSjoerg       Assert(UseCall != nullptr,
4756*da58b97aSjoerg              "Uses of llvm.call.preallocated.setup must be calls");
4757*da58b97aSjoerg       const Function *Fn = UseCall->getCalledFunction();
4758*da58b97aSjoerg       if (Fn && Fn->getIntrinsicID() == Intrinsic::call_preallocated_arg) {
4759*da58b97aSjoerg         auto *AllocArgIndex = dyn_cast<ConstantInt>(UseCall->getArgOperand(1));
4760*da58b97aSjoerg         Assert(AllocArgIndex != nullptr,
4761*da58b97aSjoerg                "llvm.call.preallocated.alloc arg index must be a constant");
4762*da58b97aSjoerg         auto AllocArgIndexInt = AllocArgIndex->getValue();
4763*da58b97aSjoerg         Assert(AllocArgIndexInt.sge(0) &&
4764*da58b97aSjoerg                    AllocArgIndexInt.slt(NumArgs->getValue()),
4765*da58b97aSjoerg                "llvm.call.preallocated.alloc arg index must be between 0 and "
4766*da58b97aSjoerg                "corresponding "
4767*da58b97aSjoerg                "llvm.call.preallocated.setup's argument count");
4768*da58b97aSjoerg       } else if (Fn && Fn->getIntrinsicID() ==
4769*da58b97aSjoerg                            Intrinsic::call_preallocated_teardown) {
4770*da58b97aSjoerg         // nothing to do
4771*da58b97aSjoerg       } else {
4772*da58b97aSjoerg         Assert(!FoundCall, "Can have at most one call corresponding to a "
4773*da58b97aSjoerg                            "llvm.call.preallocated.setup");
4774*da58b97aSjoerg         FoundCall = true;
4775*da58b97aSjoerg         size_t NumPreallocatedArgs = 0;
4776*da58b97aSjoerg         for (unsigned i = 0; i < UseCall->getNumArgOperands(); i++) {
4777*da58b97aSjoerg           if (UseCall->paramHasAttr(i, Attribute::Preallocated)) {
4778*da58b97aSjoerg             ++NumPreallocatedArgs;
4779*da58b97aSjoerg           }
4780*da58b97aSjoerg         }
4781*da58b97aSjoerg         Assert(NumPreallocatedArgs != 0,
4782*da58b97aSjoerg                "cannot use preallocated intrinsics on a call without "
4783*da58b97aSjoerg                "preallocated arguments");
4784*da58b97aSjoerg         Assert(NumArgs->equalsInt(NumPreallocatedArgs),
4785*da58b97aSjoerg                "llvm.call.preallocated.setup arg size must be equal to number "
4786*da58b97aSjoerg                "of preallocated arguments "
4787*da58b97aSjoerg                "at call site",
4788*da58b97aSjoerg                Call, *UseCall);
4789*da58b97aSjoerg         // getOperandBundle() cannot be called if more than one of the operand
4790*da58b97aSjoerg         // bundle exists. There is already a check elsewhere for this, so skip
4791*da58b97aSjoerg         // here if we see more than one.
4792*da58b97aSjoerg         if (UseCall->countOperandBundlesOfType(LLVMContext::OB_preallocated) >
4793*da58b97aSjoerg             1) {
4794*da58b97aSjoerg           return;
4795*da58b97aSjoerg         }
4796*da58b97aSjoerg         auto PreallocatedBundle =
4797*da58b97aSjoerg             UseCall->getOperandBundle(LLVMContext::OB_preallocated);
4798*da58b97aSjoerg         Assert(PreallocatedBundle,
4799*da58b97aSjoerg                "Use of llvm.call.preallocated.setup outside intrinsics "
4800*da58b97aSjoerg                "must be in \"preallocated\" operand bundle");
4801*da58b97aSjoerg         Assert(PreallocatedBundle->Inputs.front().get() == &Call,
4802*da58b97aSjoerg                "preallocated bundle must have token from corresponding "
4803*da58b97aSjoerg                "llvm.call.preallocated.setup");
4804*da58b97aSjoerg       }
4805*da58b97aSjoerg     }
4806*da58b97aSjoerg     break;
4807*da58b97aSjoerg   }
4808*da58b97aSjoerg   case Intrinsic::call_preallocated_arg: {
4809*da58b97aSjoerg     auto *Token = dyn_cast<CallBase>(Call.getArgOperand(0));
4810*da58b97aSjoerg     Assert(Token && Token->getCalledFunction()->getIntrinsicID() ==
4811*da58b97aSjoerg                         Intrinsic::call_preallocated_setup,
4812*da58b97aSjoerg            "llvm.call.preallocated.arg token argument must be a "
4813*da58b97aSjoerg            "llvm.call.preallocated.setup");
4814*da58b97aSjoerg     Assert(Call.hasFnAttr(Attribute::Preallocated),
4815*da58b97aSjoerg            "llvm.call.preallocated.arg must be called with a \"preallocated\" "
4816*da58b97aSjoerg            "call site attribute");
4817*da58b97aSjoerg     break;
4818*da58b97aSjoerg   }
4819*da58b97aSjoerg   case Intrinsic::call_preallocated_teardown: {
4820*da58b97aSjoerg     auto *Token = dyn_cast<CallBase>(Call.getArgOperand(0));
4821*da58b97aSjoerg     Assert(Token && Token->getCalledFunction()->getIntrinsicID() ==
4822*da58b97aSjoerg                         Intrinsic::call_preallocated_setup,
4823*da58b97aSjoerg            "llvm.call.preallocated.teardown token argument must be a "
4824*da58b97aSjoerg            "llvm.call.preallocated.setup");
4825*da58b97aSjoerg     break;
4826*da58b97aSjoerg   }
482706f32e7eSjoerg   case Intrinsic::gcroot:
482806f32e7eSjoerg   case Intrinsic::gcwrite:
482906f32e7eSjoerg   case Intrinsic::gcread:
483006f32e7eSjoerg     if (ID == Intrinsic::gcroot) {
483106f32e7eSjoerg       AllocaInst *AI =
483206f32e7eSjoerg           dyn_cast<AllocaInst>(Call.getArgOperand(0)->stripPointerCasts());
483306f32e7eSjoerg       Assert(AI, "llvm.gcroot parameter #1 must be an alloca.", Call);
483406f32e7eSjoerg       Assert(isa<Constant>(Call.getArgOperand(1)),
483506f32e7eSjoerg              "llvm.gcroot parameter #2 must be a constant.", Call);
483606f32e7eSjoerg       if (!AI->getAllocatedType()->isPointerTy()) {
483706f32e7eSjoerg         Assert(!isa<ConstantPointerNull>(Call.getArgOperand(1)),
483806f32e7eSjoerg                "llvm.gcroot parameter #1 must either be a pointer alloca, "
483906f32e7eSjoerg                "or argument #2 must be a non-null constant.",
484006f32e7eSjoerg                Call);
484106f32e7eSjoerg       }
484206f32e7eSjoerg     }
484306f32e7eSjoerg 
484406f32e7eSjoerg     Assert(Call.getParent()->getParent()->hasGC(),
484506f32e7eSjoerg            "Enclosing function does not use GC.", Call);
484606f32e7eSjoerg     break;
484706f32e7eSjoerg   case Intrinsic::init_trampoline:
484806f32e7eSjoerg     Assert(isa<Function>(Call.getArgOperand(1)->stripPointerCasts()),
484906f32e7eSjoerg            "llvm.init_trampoline parameter #2 must resolve to a function.",
485006f32e7eSjoerg            Call);
485106f32e7eSjoerg     break;
485206f32e7eSjoerg   case Intrinsic::prefetch:
485306f32e7eSjoerg     Assert(cast<ConstantInt>(Call.getArgOperand(1))->getZExtValue() < 2 &&
485406f32e7eSjoerg            cast<ConstantInt>(Call.getArgOperand(2))->getZExtValue() < 4,
485506f32e7eSjoerg            "invalid arguments to llvm.prefetch", Call);
485606f32e7eSjoerg     break;
485706f32e7eSjoerg   case Intrinsic::stackprotector:
485806f32e7eSjoerg     Assert(isa<AllocaInst>(Call.getArgOperand(1)->stripPointerCasts()),
485906f32e7eSjoerg            "llvm.stackprotector parameter #2 must resolve to an alloca.", Call);
486006f32e7eSjoerg     break;
486106f32e7eSjoerg   case Intrinsic::localescape: {
486206f32e7eSjoerg     BasicBlock *BB = Call.getParent();
486306f32e7eSjoerg     Assert(BB == &BB->getParent()->front(),
486406f32e7eSjoerg            "llvm.localescape used outside of entry block", Call);
486506f32e7eSjoerg     Assert(!SawFrameEscape,
486606f32e7eSjoerg            "multiple calls to llvm.localescape in one function", Call);
486706f32e7eSjoerg     for (Value *Arg : Call.args()) {
486806f32e7eSjoerg       if (isa<ConstantPointerNull>(Arg))
486906f32e7eSjoerg         continue; // Null values are allowed as placeholders.
487006f32e7eSjoerg       auto *AI = dyn_cast<AllocaInst>(Arg->stripPointerCasts());
487106f32e7eSjoerg       Assert(AI && AI->isStaticAlloca(),
487206f32e7eSjoerg              "llvm.localescape only accepts static allocas", Call);
487306f32e7eSjoerg     }
487406f32e7eSjoerg     FrameEscapeInfo[BB->getParent()].first = Call.getNumArgOperands();
487506f32e7eSjoerg     SawFrameEscape = true;
487606f32e7eSjoerg     break;
487706f32e7eSjoerg   }
487806f32e7eSjoerg   case Intrinsic::localrecover: {
487906f32e7eSjoerg     Value *FnArg = Call.getArgOperand(0)->stripPointerCasts();
488006f32e7eSjoerg     Function *Fn = dyn_cast<Function>(FnArg);
488106f32e7eSjoerg     Assert(Fn && !Fn->isDeclaration(),
488206f32e7eSjoerg            "llvm.localrecover first "
488306f32e7eSjoerg            "argument must be function defined in this module",
488406f32e7eSjoerg            Call);
488506f32e7eSjoerg     auto *IdxArg = cast<ConstantInt>(Call.getArgOperand(2));
488606f32e7eSjoerg     auto &Entry = FrameEscapeInfo[Fn];
488706f32e7eSjoerg     Entry.second = unsigned(
488806f32e7eSjoerg         std::max(uint64_t(Entry.second), IdxArg->getLimitedValue(~0U) + 1));
488906f32e7eSjoerg     break;
489006f32e7eSjoerg   }
489106f32e7eSjoerg 
489206f32e7eSjoerg   case Intrinsic::experimental_gc_statepoint:
489306f32e7eSjoerg     if (auto *CI = dyn_cast<CallInst>(&Call))
489406f32e7eSjoerg       Assert(!CI->isInlineAsm(),
489506f32e7eSjoerg              "gc.statepoint support for inline assembly unimplemented", CI);
489606f32e7eSjoerg     Assert(Call.getParent()->getParent()->hasGC(),
489706f32e7eSjoerg            "Enclosing function does not use GC.", Call);
489806f32e7eSjoerg 
489906f32e7eSjoerg     verifyStatepoint(Call);
490006f32e7eSjoerg     break;
490106f32e7eSjoerg   case Intrinsic::experimental_gc_result: {
490206f32e7eSjoerg     Assert(Call.getParent()->getParent()->hasGC(),
490306f32e7eSjoerg            "Enclosing function does not use GC.", Call);
490406f32e7eSjoerg     // Are we tied to a statepoint properly?
490506f32e7eSjoerg     const auto *StatepointCall = dyn_cast<CallBase>(Call.getArgOperand(0));
490606f32e7eSjoerg     const Function *StatepointFn =
490706f32e7eSjoerg         StatepointCall ? StatepointCall->getCalledFunction() : nullptr;
490806f32e7eSjoerg     Assert(StatepointFn && StatepointFn->isDeclaration() &&
490906f32e7eSjoerg                StatepointFn->getIntrinsicID() ==
491006f32e7eSjoerg                    Intrinsic::experimental_gc_statepoint,
491106f32e7eSjoerg            "gc.result operand #1 must be from a statepoint", Call,
491206f32e7eSjoerg            Call.getArgOperand(0));
491306f32e7eSjoerg 
491406f32e7eSjoerg     // Assert that result type matches wrapped callee.
491506f32e7eSjoerg     const Value *Target = StatepointCall->getArgOperand(2);
491606f32e7eSjoerg     auto *PT = cast<PointerType>(Target->getType());
491706f32e7eSjoerg     auto *TargetFuncType = cast<FunctionType>(PT->getElementType());
491806f32e7eSjoerg     Assert(Call.getType() == TargetFuncType->getReturnType(),
491906f32e7eSjoerg            "gc.result result type does not match wrapped callee", Call);
492006f32e7eSjoerg     break;
492106f32e7eSjoerg   }
492206f32e7eSjoerg   case Intrinsic::experimental_gc_relocate: {
492306f32e7eSjoerg     Assert(Call.getNumArgOperands() == 3, "wrong number of arguments", Call);
492406f32e7eSjoerg 
492506f32e7eSjoerg     Assert(isa<PointerType>(Call.getType()->getScalarType()),
492606f32e7eSjoerg            "gc.relocate must return a pointer or a vector of pointers", Call);
492706f32e7eSjoerg 
492806f32e7eSjoerg     // Check that this relocate is correctly tied to the statepoint
492906f32e7eSjoerg 
493006f32e7eSjoerg     // This is case for relocate on the unwinding path of an invoke statepoint
493106f32e7eSjoerg     if (LandingPadInst *LandingPad =
493206f32e7eSjoerg             dyn_cast<LandingPadInst>(Call.getArgOperand(0))) {
493306f32e7eSjoerg 
493406f32e7eSjoerg       const BasicBlock *InvokeBB =
493506f32e7eSjoerg           LandingPad->getParent()->getUniquePredecessor();
493606f32e7eSjoerg 
493706f32e7eSjoerg       // Landingpad relocates should have only one predecessor with invoke
493806f32e7eSjoerg       // statepoint terminator
493906f32e7eSjoerg       Assert(InvokeBB, "safepoints should have unique landingpads",
494006f32e7eSjoerg              LandingPad->getParent());
494106f32e7eSjoerg       Assert(InvokeBB->getTerminator(), "safepoint block should be well formed",
494206f32e7eSjoerg              InvokeBB);
4943*da58b97aSjoerg       Assert(isa<GCStatepointInst>(InvokeBB->getTerminator()),
494406f32e7eSjoerg              "gc relocate should be linked to a statepoint", InvokeBB);
494506f32e7eSjoerg     } else {
494606f32e7eSjoerg       // In all other cases relocate should be tied to the statepoint directly.
494706f32e7eSjoerg       // This covers relocates on a normal return path of invoke statepoint and
494806f32e7eSjoerg       // relocates of a call statepoint.
494906f32e7eSjoerg       auto Token = Call.getArgOperand(0);
4950*da58b97aSjoerg       Assert(isa<GCStatepointInst>(Token),
495106f32e7eSjoerg              "gc relocate is incorrectly tied to the statepoint", Call, Token);
495206f32e7eSjoerg     }
495306f32e7eSjoerg 
495406f32e7eSjoerg     // Verify rest of the relocate arguments.
495506f32e7eSjoerg     const CallBase &StatepointCall =
4956*da58b97aSjoerg       *cast<GCRelocateInst>(Call).getStatepoint();
495706f32e7eSjoerg 
495806f32e7eSjoerg     // Both the base and derived must be piped through the safepoint.
495906f32e7eSjoerg     Value *Base = Call.getArgOperand(1);
496006f32e7eSjoerg     Assert(isa<ConstantInt>(Base),
496106f32e7eSjoerg            "gc.relocate operand #2 must be integer offset", Call);
496206f32e7eSjoerg 
496306f32e7eSjoerg     Value *Derived = Call.getArgOperand(2);
496406f32e7eSjoerg     Assert(isa<ConstantInt>(Derived),
496506f32e7eSjoerg            "gc.relocate operand #3 must be integer offset", Call);
496606f32e7eSjoerg 
4967*da58b97aSjoerg     const uint64_t BaseIndex = cast<ConstantInt>(Base)->getZExtValue();
4968*da58b97aSjoerg     const uint64_t DerivedIndex = cast<ConstantInt>(Derived)->getZExtValue();
496906f32e7eSjoerg 
4970*da58b97aSjoerg     // Check the bounds
4971*da58b97aSjoerg     if (auto Opt = StatepointCall.getOperandBundle(LLVMContext::OB_gc_live)) {
4972*da58b97aSjoerg       Assert(BaseIndex < Opt->Inputs.size(),
4973*da58b97aSjoerg              "gc.relocate: statepoint base index out of bounds", Call);
4974*da58b97aSjoerg       Assert(DerivedIndex < Opt->Inputs.size(),
4975*da58b97aSjoerg              "gc.relocate: statepoint derived index out of bounds", Call);
4976*da58b97aSjoerg     }
497706f32e7eSjoerg 
497806f32e7eSjoerg     // Relocated value must be either a pointer type or vector-of-pointer type,
497906f32e7eSjoerg     // but gc_relocate does not need to return the same pointer type as the
498006f32e7eSjoerg     // relocated pointer. It can be casted to the correct type later if it's
498106f32e7eSjoerg     // desired. However, they must have the same address space and 'vectorness'
498206f32e7eSjoerg     GCRelocateInst &Relocate = cast<GCRelocateInst>(Call);
498306f32e7eSjoerg     Assert(Relocate.getDerivedPtr()->getType()->isPtrOrPtrVectorTy(),
498406f32e7eSjoerg            "gc.relocate: relocated value must be a gc pointer", Call);
498506f32e7eSjoerg 
498606f32e7eSjoerg     auto ResultType = Call.getType();
498706f32e7eSjoerg     auto DerivedType = Relocate.getDerivedPtr()->getType();
498806f32e7eSjoerg     Assert(ResultType->isVectorTy() == DerivedType->isVectorTy(),
498906f32e7eSjoerg            "gc.relocate: vector relocates to vector and pointer to pointer",
499006f32e7eSjoerg            Call);
499106f32e7eSjoerg     Assert(
499206f32e7eSjoerg         ResultType->getPointerAddressSpace() ==
499306f32e7eSjoerg             DerivedType->getPointerAddressSpace(),
499406f32e7eSjoerg         "gc.relocate: relocating a pointer shouldn't change its address space",
499506f32e7eSjoerg         Call);
499606f32e7eSjoerg     break;
499706f32e7eSjoerg   }
499806f32e7eSjoerg   case Intrinsic::eh_exceptioncode:
499906f32e7eSjoerg   case Intrinsic::eh_exceptionpointer: {
500006f32e7eSjoerg     Assert(isa<CatchPadInst>(Call.getArgOperand(0)),
500106f32e7eSjoerg            "eh.exceptionpointer argument must be a catchpad", Call);
500206f32e7eSjoerg     break;
500306f32e7eSjoerg   }
5004*da58b97aSjoerg   case Intrinsic::get_active_lane_mask: {
5005*da58b97aSjoerg     Assert(Call.getType()->isVectorTy(), "get_active_lane_mask: must return a "
5006*da58b97aSjoerg            "vector", Call);
5007*da58b97aSjoerg     auto *ElemTy = Call.getType()->getScalarType();
5008*da58b97aSjoerg     Assert(ElemTy->isIntegerTy(1), "get_active_lane_mask: element type is not "
5009*da58b97aSjoerg            "i1", Call);
5010*da58b97aSjoerg     break;
5011*da58b97aSjoerg   }
501206f32e7eSjoerg   case Intrinsic::masked_load: {
501306f32e7eSjoerg     Assert(Call.getType()->isVectorTy(), "masked_load: must return a vector",
501406f32e7eSjoerg            Call);
501506f32e7eSjoerg 
501606f32e7eSjoerg     Value *Ptr = Call.getArgOperand(0);
501706f32e7eSjoerg     ConstantInt *Alignment = cast<ConstantInt>(Call.getArgOperand(1));
501806f32e7eSjoerg     Value *Mask = Call.getArgOperand(2);
501906f32e7eSjoerg     Value *PassThru = Call.getArgOperand(3);
502006f32e7eSjoerg     Assert(Mask->getType()->isVectorTy(), "masked_load: mask must be vector",
502106f32e7eSjoerg            Call);
502206f32e7eSjoerg     Assert(Alignment->getValue().isPowerOf2(),
502306f32e7eSjoerg            "masked_load: alignment must be a power of 2", Call);
502406f32e7eSjoerg 
502506f32e7eSjoerg     // DataTy is the overloaded type
502606f32e7eSjoerg     Type *DataTy = cast<PointerType>(Ptr->getType())->getElementType();
502706f32e7eSjoerg     Assert(DataTy == Call.getType(),
502806f32e7eSjoerg            "masked_load: return must match pointer type", Call);
502906f32e7eSjoerg     Assert(PassThru->getType() == DataTy,
503006f32e7eSjoerg            "masked_load: pass through and data type must match", Call);
5031*da58b97aSjoerg     Assert(cast<VectorType>(Mask->getType())->getElementCount() ==
5032*da58b97aSjoerg                cast<VectorType>(DataTy)->getElementCount(),
503306f32e7eSjoerg            "masked_load: vector mask must be same length as data", Call);
503406f32e7eSjoerg     break;
503506f32e7eSjoerg   }
503606f32e7eSjoerg   case Intrinsic::masked_store: {
503706f32e7eSjoerg     Value *Val = Call.getArgOperand(0);
503806f32e7eSjoerg     Value *Ptr = Call.getArgOperand(1);
503906f32e7eSjoerg     ConstantInt *Alignment = cast<ConstantInt>(Call.getArgOperand(2));
504006f32e7eSjoerg     Value *Mask = Call.getArgOperand(3);
504106f32e7eSjoerg     Assert(Mask->getType()->isVectorTy(), "masked_store: mask must be vector",
504206f32e7eSjoerg            Call);
504306f32e7eSjoerg     Assert(Alignment->getValue().isPowerOf2(),
504406f32e7eSjoerg            "masked_store: alignment must be a power of 2", Call);
504506f32e7eSjoerg 
504606f32e7eSjoerg     // DataTy is the overloaded type
504706f32e7eSjoerg     Type *DataTy = cast<PointerType>(Ptr->getType())->getElementType();
504806f32e7eSjoerg     Assert(DataTy == Val->getType(),
504906f32e7eSjoerg            "masked_store: storee must match pointer type", Call);
5050*da58b97aSjoerg     Assert(cast<VectorType>(Mask->getType())->getElementCount() ==
5051*da58b97aSjoerg                cast<VectorType>(DataTy)->getElementCount(),
505206f32e7eSjoerg            "masked_store: vector mask must be same length as data", Call);
505306f32e7eSjoerg     break;
505406f32e7eSjoerg   }
505506f32e7eSjoerg 
5056*da58b97aSjoerg   case Intrinsic::masked_gather: {
5057*da58b97aSjoerg     const APInt &Alignment =
5058*da58b97aSjoerg         cast<ConstantInt>(Call.getArgOperand(1))->getValue();
5059*da58b97aSjoerg     Assert(Alignment.isNullValue() || Alignment.isPowerOf2(),
5060*da58b97aSjoerg            "masked_gather: alignment must be 0 or a power of 2", Call);
5061*da58b97aSjoerg     break;
5062*da58b97aSjoerg   }
5063*da58b97aSjoerg   case Intrinsic::masked_scatter: {
5064*da58b97aSjoerg     const APInt &Alignment =
5065*da58b97aSjoerg         cast<ConstantInt>(Call.getArgOperand(2))->getValue();
5066*da58b97aSjoerg     Assert(Alignment.isNullValue() || Alignment.isPowerOf2(),
5067*da58b97aSjoerg            "masked_scatter: alignment must be 0 or a power of 2", Call);
5068*da58b97aSjoerg     break;
5069*da58b97aSjoerg   }
5070*da58b97aSjoerg 
507106f32e7eSjoerg   case Intrinsic::experimental_guard: {
507206f32e7eSjoerg     Assert(isa<CallInst>(Call), "experimental_guard cannot be invoked", Call);
507306f32e7eSjoerg     Assert(Call.countOperandBundlesOfType(LLVMContext::OB_deopt) == 1,
507406f32e7eSjoerg            "experimental_guard must have exactly one "
507506f32e7eSjoerg            "\"deopt\" operand bundle");
507606f32e7eSjoerg     break;
507706f32e7eSjoerg   }
507806f32e7eSjoerg 
507906f32e7eSjoerg   case Intrinsic::experimental_deoptimize: {
508006f32e7eSjoerg     Assert(isa<CallInst>(Call), "experimental_deoptimize cannot be invoked",
508106f32e7eSjoerg            Call);
508206f32e7eSjoerg     Assert(Call.countOperandBundlesOfType(LLVMContext::OB_deopt) == 1,
508306f32e7eSjoerg            "experimental_deoptimize must have exactly one "
508406f32e7eSjoerg            "\"deopt\" operand bundle");
508506f32e7eSjoerg     Assert(Call.getType() == Call.getFunction()->getReturnType(),
508606f32e7eSjoerg            "experimental_deoptimize return type must match caller return type");
508706f32e7eSjoerg 
508806f32e7eSjoerg     if (isa<CallInst>(Call)) {
508906f32e7eSjoerg       auto *RI = dyn_cast<ReturnInst>(Call.getNextNode());
509006f32e7eSjoerg       Assert(RI,
509106f32e7eSjoerg              "calls to experimental_deoptimize must be followed by a return");
509206f32e7eSjoerg 
509306f32e7eSjoerg       if (!Call.getType()->isVoidTy() && RI)
509406f32e7eSjoerg         Assert(RI->getReturnValue() == &Call,
509506f32e7eSjoerg                "calls to experimental_deoptimize must be followed by a return "
509606f32e7eSjoerg                "of the value computed by experimental_deoptimize");
509706f32e7eSjoerg     }
509806f32e7eSjoerg 
509906f32e7eSjoerg     break;
510006f32e7eSjoerg   }
5101*da58b97aSjoerg   case Intrinsic::vector_reduce_and:
5102*da58b97aSjoerg   case Intrinsic::vector_reduce_or:
5103*da58b97aSjoerg   case Intrinsic::vector_reduce_xor:
5104*da58b97aSjoerg   case Intrinsic::vector_reduce_add:
5105*da58b97aSjoerg   case Intrinsic::vector_reduce_mul:
5106*da58b97aSjoerg   case Intrinsic::vector_reduce_smax:
5107*da58b97aSjoerg   case Intrinsic::vector_reduce_smin:
5108*da58b97aSjoerg   case Intrinsic::vector_reduce_umax:
5109*da58b97aSjoerg   case Intrinsic::vector_reduce_umin: {
5110*da58b97aSjoerg     Type *ArgTy = Call.getArgOperand(0)->getType();
5111*da58b97aSjoerg     Assert(ArgTy->isIntOrIntVectorTy() && ArgTy->isVectorTy(),
5112*da58b97aSjoerg            "Intrinsic has incorrect argument type!");
5113*da58b97aSjoerg     break;
5114*da58b97aSjoerg   }
5115*da58b97aSjoerg   case Intrinsic::vector_reduce_fmax:
5116*da58b97aSjoerg   case Intrinsic::vector_reduce_fmin: {
5117*da58b97aSjoerg     Type *ArgTy = Call.getArgOperand(0)->getType();
5118*da58b97aSjoerg     Assert(ArgTy->isFPOrFPVectorTy() && ArgTy->isVectorTy(),
5119*da58b97aSjoerg            "Intrinsic has incorrect argument type!");
5120*da58b97aSjoerg     break;
5121*da58b97aSjoerg   }
5122*da58b97aSjoerg   case Intrinsic::vector_reduce_fadd:
5123*da58b97aSjoerg   case Intrinsic::vector_reduce_fmul: {
5124*da58b97aSjoerg     // Unlike the other reductions, the first argument is a start value. The
5125*da58b97aSjoerg     // second argument is the vector to be reduced.
5126*da58b97aSjoerg     Type *ArgTy = Call.getArgOperand(1)->getType();
5127*da58b97aSjoerg     Assert(ArgTy->isFPOrFPVectorTy() && ArgTy->isVectorTy(),
5128*da58b97aSjoerg            "Intrinsic has incorrect argument type!");
512906f32e7eSjoerg     break;
513006f32e7eSjoerg   }
513106f32e7eSjoerg   case Intrinsic::smul_fix:
513206f32e7eSjoerg   case Intrinsic::smul_fix_sat:
513306f32e7eSjoerg   case Intrinsic::umul_fix:
5134*da58b97aSjoerg   case Intrinsic::umul_fix_sat:
5135*da58b97aSjoerg   case Intrinsic::sdiv_fix:
5136*da58b97aSjoerg   case Intrinsic::sdiv_fix_sat:
5137*da58b97aSjoerg   case Intrinsic::udiv_fix:
5138*da58b97aSjoerg   case Intrinsic::udiv_fix_sat: {
513906f32e7eSjoerg     Value *Op1 = Call.getArgOperand(0);
514006f32e7eSjoerg     Value *Op2 = Call.getArgOperand(1);
514106f32e7eSjoerg     Assert(Op1->getType()->isIntOrIntVectorTy(),
5142*da58b97aSjoerg            "first operand of [us][mul|div]_fix[_sat] must be an int type or "
5143*da58b97aSjoerg            "vector of ints");
514406f32e7eSjoerg     Assert(Op2->getType()->isIntOrIntVectorTy(),
5145*da58b97aSjoerg            "second operand of [us][mul|div]_fix[_sat] must be an int type or "
5146*da58b97aSjoerg            "vector of ints");
514706f32e7eSjoerg 
514806f32e7eSjoerg     auto *Op3 = cast<ConstantInt>(Call.getArgOperand(2));
514906f32e7eSjoerg     Assert(Op3->getType()->getBitWidth() <= 32,
5150*da58b97aSjoerg            "third argument of [us][mul|div]_fix[_sat] must fit within 32 bits");
515106f32e7eSjoerg 
5152*da58b97aSjoerg     if (ID == Intrinsic::smul_fix || ID == Intrinsic::smul_fix_sat ||
5153*da58b97aSjoerg         ID == Intrinsic::sdiv_fix || ID == Intrinsic::sdiv_fix_sat) {
515406f32e7eSjoerg       Assert(
515506f32e7eSjoerg           Op3->getZExtValue() < Op1->getType()->getScalarSizeInBits(),
5156*da58b97aSjoerg           "the scale of s[mul|div]_fix[_sat] must be less than the width of "
5157*da58b97aSjoerg           "the operands");
515806f32e7eSjoerg     } else {
515906f32e7eSjoerg       Assert(Op3->getZExtValue() <= Op1->getType()->getScalarSizeInBits(),
5160*da58b97aSjoerg              "the scale of u[mul|div]_fix[_sat] must be less than or equal "
5161*da58b97aSjoerg              "to the width of the operands");
516206f32e7eSjoerg     }
516306f32e7eSjoerg     break;
516406f32e7eSjoerg   }
516506f32e7eSjoerg   case Intrinsic::lround:
516606f32e7eSjoerg   case Intrinsic::llround:
516706f32e7eSjoerg   case Intrinsic::lrint:
516806f32e7eSjoerg   case Intrinsic::llrint: {
516906f32e7eSjoerg     Type *ValTy = Call.getArgOperand(0)->getType();
517006f32e7eSjoerg     Type *ResultTy = Call.getType();
517106f32e7eSjoerg     Assert(!ValTy->isVectorTy() && !ResultTy->isVectorTy(),
517206f32e7eSjoerg            "Intrinsic does not support vectors", &Call);
517306f32e7eSjoerg     break;
517406f32e7eSjoerg   }
5175*da58b97aSjoerg   case Intrinsic::bswap: {
5176*da58b97aSjoerg     Type *Ty = Call.getType();
5177*da58b97aSjoerg     unsigned Size = Ty->getScalarSizeInBits();
5178*da58b97aSjoerg     Assert(Size % 16 == 0, "bswap must be an even number of bytes", &Call);
5179*da58b97aSjoerg     break;
5180*da58b97aSjoerg   }
5181*da58b97aSjoerg   case Intrinsic::invariant_start: {
5182*da58b97aSjoerg     ConstantInt *InvariantSize = dyn_cast<ConstantInt>(Call.getArgOperand(0));
5183*da58b97aSjoerg     Assert(InvariantSize &&
5184*da58b97aSjoerg                (!InvariantSize->isNegative() || InvariantSize->isMinusOne()),
5185*da58b97aSjoerg            "invariant_start parameter must be -1, 0 or a positive number",
5186*da58b97aSjoerg            &Call);
5187*da58b97aSjoerg     break;
5188*da58b97aSjoerg   }
5189*da58b97aSjoerg   case Intrinsic::matrix_multiply:
5190*da58b97aSjoerg   case Intrinsic::matrix_transpose:
5191*da58b97aSjoerg   case Intrinsic::matrix_column_major_load:
5192*da58b97aSjoerg   case Intrinsic::matrix_column_major_store: {
5193*da58b97aSjoerg     Function *IF = Call.getCalledFunction();
5194*da58b97aSjoerg     ConstantInt *Stride = nullptr;
5195*da58b97aSjoerg     ConstantInt *NumRows;
5196*da58b97aSjoerg     ConstantInt *NumColumns;
5197*da58b97aSjoerg     VectorType *ResultTy;
5198*da58b97aSjoerg     Type *Op0ElemTy = nullptr;
5199*da58b97aSjoerg     Type *Op1ElemTy = nullptr;
5200*da58b97aSjoerg     switch (ID) {
5201*da58b97aSjoerg     case Intrinsic::matrix_multiply:
5202*da58b97aSjoerg       NumRows = cast<ConstantInt>(Call.getArgOperand(2));
5203*da58b97aSjoerg       NumColumns = cast<ConstantInt>(Call.getArgOperand(4));
5204*da58b97aSjoerg       ResultTy = cast<VectorType>(Call.getType());
5205*da58b97aSjoerg       Op0ElemTy =
5206*da58b97aSjoerg           cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType();
5207*da58b97aSjoerg       Op1ElemTy =
5208*da58b97aSjoerg           cast<VectorType>(Call.getArgOperand(1)->getType())->getElementType();
5209*da58b97aSjoerg       break;
5210*da58b97aSjoerg     case Intrinsic::matrix_transpose:
5211*da58b97aSjoerg       NumRows = cast<ConstantInt>(Call.getArgOperand(1));
5212*da58b97aSjoerg       NumColumns = cast<ConstantInt>(Call.getArgOperand(2));
5213*da58b97aSjoerg       ResultTy = cast<VectorType>(Call.getType());
5214*da58b97aSjoerg       Op0ElemTy =
5215*da58b97aSjoerg           cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType();
5216*da58b97aSjoerg       break;
5217*da58b97aSjoerg     case Intrinsic::matrix_column_major_load:
5218*da58b97aSjoerg       Stride = dyn_cast<ConstantInt>(Call.getArgOperand(1));
5219*da58b97aSjoerg       NumRows = cast<ConstantInt>(Call.getArgOperand(3));
5220*da58b97aSjoerg       NumColumns = cast<ConstantInt>(Call.getArgOperand(4));
5221*da58b97aSjoerg       ResultTy = cast<VectorType>(Call.getType());
5222*da58b97aSjoerg       Op0ElemTy =
5223*da58b97aSjoerg           cast<PointerType>(Call.getArgOperand(0)->getType())->getElementType();
5224*da58b97aSjoerg       break;
5225*da58b97aSjoerg     case Intrinsic::matrix_column_major_store:
5226*da58b97aSjoerg       Stride = dyn_cast<ConstantInt>(Call.getArgOperand(2));
5227*da58b97aSjoerg       NumRows = cast<ConstantInt>(Call.getArgOperand(4));
5228*da58b97aSjoerg       NumColumns = cast<ConstantInt>(Call.getArgOperand(5));
5229*da58b97aSjoerg       ResultTy = cast<VectorType>(Call.getArgOperand(0)->getType());
5230*da58b97aSjoerg       Op0ElemTy =
5231*da58b97aSjoerg           cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType();
5232*da58b97aSjoerg       Op1ElemTy =
5233*da58b97aSjoerg           cast<PointerType>(Call.getArgOperand(1)->getType())->getElementType();
5234*da58b97aSjoerg       break;
5235*da58b97aSjoerg     default:
5236*da58b97aSjoerg       llvm_unreachable("unexpected intrinsic");
5237*da58b97aSjoerg     }
5238*da58b97aSjoerg 
5239*da58b97aSjoerg     Assert(ResultTy->getElementType()->isIntegerTy() ||
5240*da58b97aSjoerg            ResultTy->getElementType()->isFloatingPointTy(),
5241*da58b97aSjoerg            "Result type must be an integer or floating-point type!", IF);
5242*da58b97aSjoerg 
5243*da58b97aSjoerg     Assert(ResultTy->getElementType() == Op0ElemTy,
5244*da58b97aSjoerg            "Vector element type mismatch of the result and first operand "
5245*da58b97aSjoerg            "vector!", IF);
5246*da58b97aSjoerg 
5247*da58b97aSjoerg     if (Op1ElemTy)
5248*da58b97aSjoerg       Assert(ResultTy->getElementType() == Op1ElemTy,
5249*da58b97aSjoerg              "Vector element type mismatch of the result and second operand "
5250*da58b97aSjoerg              "vector!", IF);
5251*da58b97aSjoerg 
5252*da58b97aSjoerg     Assert(cast<FixedVectorType>(ResultTy)->getNumElements() ==
5253*da58b97aSjoerg                NumRows->getZExtValue() * NumColumns->getZExtValue(),
5254*da58b97aSjoerg            "Result of a matrix operation does not fit in the returned vector!");
5255*da58b97aSjoerg 
5256*da58b97aSjoerg     if (Stride)
5257*da58b97aSjoerg       Assert(Stride->getZExtValue() >= NumRows->getZExtValue(),
5258*da58b97aSjoerg              "Stride must be greater or equal than the number of rows!", IF);
5259*da58b97aSjoerg 
5260*da58b97aSjoerg     break;
5261*da58b97aSjoerg   }
5262*da58b97aSjoerg   case Intrinsic::experimental_stepvector: {
5263*da58b97aSjoerg     VectorType *VecTy = dyn_cast<VectorType>(Call.getType());
5264*da58b97aSjoerg     Assert(VecTy && VecTy->getScalarType()->isIntegerTy() &&
5265*da58b97aSjoerg                VecTy->getScalarSizeInBits() >= 8,
5266*da58b97aSjoerg            "experimental_stepvector only supported for vectors of integers "
5267*da58b97aSjoerg            "with a bitwidth of at least 8.",
5268*da58b97aSjoerg            &Call);
5269*da58b97aSjoerg     break;
5270*da58b97aSjoerg   }
5271*da58b97aSjoerg   case Intrinsic::experimental_vector_insert: {
5272*da58b97aSjoerg     VectorType *VecTy = cast<VectorType>(Call.getArgOperand(0)->getType());
5273*da58b97aSjoerg     VectorType *SubVecTy = cast<VectorType>(Call.getArgOperand(1)->getType());
5274*da58b97aSjoerg 
5275*da58b97aSjoerg     Assert(VecTy->getElementType() == SubVecTy->getElementType(),
5276*da58b97aSjoerg            "experimental_vector_insert parameters must have the same element "
5277*da58b97aSjoerg            "type.",
5278*da58b97aSjoerg            &Call);
5279*da58b97aSjoerg     break;
5280*da58b97aSjoerg   }
5281*da58b97aSjoerg   case Intrinsic::experimental_vector_extract: {
5282*da58b97aSjoerg     VectorType *ResultTy = cast<VectorType>(Call.getType());
5283*da58b97aSjoerg     VectorType *VecTy = cast<VectorType>(Call.getArgOperand(0)->getType());
5284*da58b97aSjoerg 
5285*da58b97aSjoerg     Assert(ResultTy->getElementType() == VecTy->getElementType(),
5286*da58b97aSjoerg            "experimental_vector_extract result must have the same element "
5287*da58b97aSjoerg            "type as the input vector.",
5288*da58b97aSjoerg            &Call);
5289*da58b97aSjoerg     break;
5290*da58b97aSjoerg   }
5291*da58b97aSjoerg   case Intrinsic::experimental_noalias_scope_decl: {
5292*da58b97aSjoerg     NoAliasScopeDecls.push_back(cast<IntrinsicInst>(&Call));
5293*da58b97aSjoerg     break;
5294*da58b97aSjoerg   }
529506f32e7eSjoerg   };
529606f32e7eSjoerg }
529706f32e7eSjoerg 
529806f32e7eSjoerg /// Carefully grab the subprogram from a local scope.
529906f32e7eSjoerg ///
530006f32e7eSjoerg /// This carefully grabs the subprogram from a local scope, avoiding the
530106f32e7eSjoerg /// built-in assertions that would typically fire.
getSubprogram(Metadata * LocalScope)530206f32e7eSjoerg static DISubprogram *getSubprogram(Metadata *LocalScope) {
530306f32e7eSjoerg   if (!LocalScope)
530406f32e7eSjoerg     return nullptr;
530506f32e7eSjoerg 
530606f32e7eSjoerg   if (auto *SP = dyn_cast<DISubprogram>(LocalScope))
530706f32e7eSjoerg     return SP;
530806f32e7eSjoerg 
530906f32e7eSjoerg   if (auto *LB = dyn_cast<DILexicalBlockBase>(LocalScope))
531006f32e7eSjoerg     return getSubprogram(LB->getRawScope());
531106f32e7eSjoerg 
531206f32e7eSjoerg   // Just return null; broken scope chains are checked elsewhere.
531306f32e7eSjoerg   assert(!isa<DILocalScope>(LocalScope) && "Unknown type of local scope");
531406f32e7eSjoerg   return nullptr;
531506f32e7eSjoerg }
531606f32e7eSjoerg 
visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic & FPI)531706f32e7eSjoerg void Verifier::visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI) {
5318*da58b97aSjoerg   unsigned NumOperands;
5319*da58b97aSjoerg   bool HasRoundingMD;
532006f32e7eSjoerg   switch (FPI.getIntrinsicID()) {
5321*da58b97aSjoerg #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC)                         \
5322*da58b97aSjoerg   case Intrinsic::INTRINSIC:                                                   \
5323*da58b97aSjoerg     NumOperands = NARG;                                                        \
5324*da58b97aSjoerg     HasRoundingMD = ROUND_MODE;                                                \
532506f32e7eSjoerg     break;
5326*da58b97aSjoerg #include "llvm/IR/ConstrainedOps.def"
5327*da58b97aSjoerg   default:
5328*da58b97aSjoerg     llvm_unreachable("Invalid constrained FP intrinsic!");
5329*da58b97aSjoerg   }
5330*da58b97aSjoerg   NumOperands += (1 + HasRoundingMD);
5331*da58b97aSjoerg   // Compare intrinsics carry an extra predicate metadata operand.
5332*da58b97aSjoerg   if (isa<ConstrainedFPCmpIntrinsic>(FPI))
5333*da58b97aSjoerg     NumOperands += 1;
5334*da58b97aSjoerg   Assert((FPI.getNumArgOperands() == NumOperands),
5335*da58b97aSjoerg          "invalid arguments for constrained FP intrinsic", &FPI);
533606f32e7eSjoerg 
5337*da58b97aSjoerg   switch (FPI.getIntrinsicID()) {
533806f32e7eSjoerg   case Intrinsic::experimental_constrained_lrint:
533906f32e7eSjoerg   case Intrinsic::experimental_constrained_llrint: {
534006f32e7eSjoerg     Type *ValTy = FPI.getArgOperand(0)->getType();
534106f32e7eSjoerg     Type *ResultTy = FPI.getType();
534206f32e7eSjoerg     Assert(!ValTy->isVectorTy() && !ResultTy->isVectorTy(),
534306f32e7eSjoerg            "Intrinsic does not support vectors", &FPI);
534406f32e7eSjoerg   }
534506f32e7eSjoerg     break;
534606f32e7eSjoerg 
534706f32e7eSjoerg   case Intrinsic::experimental_constrained_lround:
534806f32e7eSjoerg   case Intrinsic::experimental_constrained_llround: {
534906f32e7eSjoerg     Type *ValTy = FPI.getArgOperand(0)->getType();
535006f32e7eSjoerg     Type *ResultTy = FPI.getType();
535106f32e7eSjoerg     Assert(!ValTy->isVectorTy() && !ResultTy->isVectorTy(),
535206f32e7eSjoerg            "Intrinsic does not support vectors", &FPI);
535306f32e7eSjoerg     break;
535406f32e7eSjoerg   }
535506f32e7eSjoerg 
5356*da58b97aSjoerg   case Intrinsic::experimental_constrained_fcmp:
5357*da58b97aSjoerg   case Intrinsic::experimental_constrained_fcmps: {
5358*da58b97aSjoerg     auto Pred = cast<ConstrainedFPCmpIntrinsic>(&FPI)->getPredicate();
5359*da58b97aSjoerg     Assert(CmpInst::isFPPredicate(Pred),
5360*da58b97aSjoerg            "invalid predicate for constrained FP comparison intrinsic", &FPI);
536106f32e7eSjoerg     break;
5362*da58b97aSjoerg   }
536306f32e7eSjoerg 
536406f32e7eSjoerg   case Intrinsic::experimental_constrained_fptosi:
536506f32e7eSjoerg   case Intrinsic::experimental_constrained_fptoui: {
536606f32e7eSjoerg     Value *Operand = FPI.getArgOperand(0);
536706f32e7eSjoerg     uint64_t NumSrcElem = 0;
536806f32e7eSjoerg     Assert(Operand->getType()->isFPOrFPVectorTy(),
536906f32e7eSjoerg            "Intrinsic first argument must be floating point", &FPI);
537006f32e7eSjoerg     if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
5371*da58b97aSjoerg       NumSrcElem = cast<FixedVectorType>(OperandT)->getNumElements();
537206f32e7eSjoerg     }
537306f32e7eSjoerg 
537406f32e7eSjoerg     Operand = &FPI;
537506f32e7eSjoerg     Assert((NumSrcElem > 0) == Operand->getType()->isVectorTy(),
537606f32e7eSjoerg            "Intrinsic first argument and result disagree on vector use", &FPI);
537706f32e7eSjoerg     Assert(Operand->getType()->isIntOrIntVectorTy(),
537806f32e7eSjoerg            "Intrinsic result must be an integer", &FPI);
537906f32e7eSjoerg     if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
5380*da58b97aSjoerg       Assert(NumSrcElem == cast<FixedVectorType>(OperandT)->getNumElements(),
538106f32e7eSjoerg              "Intrinsic first argument and result vector lengths must be equal",
538206f32e7eSjoerg              &FPI);
538306f32e7eSjoerg     }
538406f32e7eSjoerg   }
538506f32e7eSjoerg     break;
538606f32e7eSjoerg 
5387*da58b97aSjoerg   case Intrinsic::experimental_constrained_sitofp:
5388*da58b97aSjoerg   case Intrinsic::experimental_constrained_uitofp: {
5389*da58b97aSjoerg     Value *Operand = FPI.getArgOperand(0);
5390*da58b97aSjoerg     uint64_t NumSrcElem = 0;
5391*da58b97aSjoerg     Assert(Operand->getType()->isIntOrIntVectorTy(),
5392*da58b97aSjoerg            "Intrinsic first argument must be integer", &FPI);
5393*da58b97aSjoerg     if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
5394*da58b97aSjoerg       NumSrcElem = cast<FixedVectorType>(OperandT)->getNumElements();
5395*da58b97aSjoerg     }
5396*da58b97aSjoerg 
5397*da58b97aSjoerg     Operand = &FPI;
5398*da58b97aSjoerg     Assert((NumSrcElem > 0) == Operand->getType()->isVectorTy(),
5399*da58b97aSjoerg            "Intrinsic first argument and result disagree on vector use", &FPI);
5400*da58b97aSjoerg     Assert(Operand->getType()->isFPOrFPVectorTy(),
5401*da58b97aSjoerg            "Intrinsic result must be a floating point", &FPI);
5402*da58b97aSjoerg     if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
5403*da58b97aSjoerg       Assert(NumSrcElem == cast<FixedVectorType>(OperandT)->getNumElements(),
5404*da58b97aSjoerg              "Intrinsic first argument and result vector lengths must be equal",
5405*da58b97aSjoerg              &FPI);
5406*da58b97aSjoerg     }
5407*da58b97aSjoerg   } break;
5408*da58b97aSjoerg 
540906f32e7eSjoerg   case Intrinsic::experimental_constrained_fptrunc:
541006f32e7eSjoerg   case Intrinsic::experimental_constrained_fpext: {
541106f32e7eSjoerg     Value *Operand = FPI.getArgOperand(0);
541206f32e7eSjoerg     Type *OperandTy = Operand->getType();
541306f32e7eSjoerg     Value *Result = &FPI;
541406f32e7eSjoerg     Type *ResultTy = Result->getType();
541506f32e7eSjoerg     Assert(OperandTy->isFPOrFPVectorTy(),
541606f32e7eSjoerg            "Intrinsic first argument must be FP or FP vector", &FPI);
541706f32e7eSjoerg     Assert(ResultTy->isFPOrFPVectorTy(),
541806f32e7eSjoerg            "Intrinsic result must be FP or FP vector", &FPI);
541906f32e7eSjoerg     Assert(OperandTy->isVectorTy() == ResultTy->isVectorTy(),
542006f32e7eSjoerg            "Intrinsic first argument and result disagree on vector use", &FPI);
542106f32e7eSjoerg     if (OperandTy->isVectorTy()) {
5422*da58b97aSjoerg       Assert(cast<FixedVectorType>(OperandTy)->getNumElements() ==
5423*da58b97aSjoerg                  cast<FixedVectorType>(ResultTy)->getNumElements(),
542406f32e7eSjoerg              "Intrinsic first argument and result vector lengths must be equal",
542506f32e7eSjoerg              &FPI);
542606f32e7eSjoerg     }
542706f32e7eSjoerg     if (FPI.getIntrinsicID() == Intrinsic::experimental_constrained_fptrunc) {
542806f32e7eSjoerg       Assert(OperandTy->getScalarSizeInBits() > ResultTy->getScalarSizeInBits(),
542906f32e7eSjoerg              "Intrinsic first argument's type must be larger than result type",
543006f32e7eSjoerg              &FPI);
543106f32e7eSjoerg     } else {
543206f32e7eSjoerg       Assert(OperandTy->getScalarSizeInBits() < ResultTy->getScalarSizeInBits(),
543306f32e7eSjoerg              "Intrinsic first argument's type must be smaller than result type",
543406f32e7eSjoerg              &FPI);
543506f32e7eSjoerg     }
543606f32e7eSjoerg   }
543706f32e7eSjoerg     break;
543806f32e7eSjoerg 
543906f32e7eSjoerg   default:
5440*da58b97aSjoerg     break;
544106f32e7eSjoerg   }
544206f32e7eSjoerg 
544306f32e7eSjoerg   // If a non-metadata argument is passed in a metadata slot then the
544406f32e7eSjoerg   // error will be caught earlier when the incorrect argument doesn't
544506f32e7eSjoerg   // match the specification in the intrinsic call table. Thus, no
544606f32e7eSjoerg   // argument type check is needed here.
544706f32e7eSjoerg 
544806f32e7eSjoerg   Assert(FPI.getExceptionBehavior().hasValue(),
544906f32e7eSjoerg          "invalid exception behavior argument", &FPI);
545006f32e7eSjoerg   if (HasRoundingMD) {
545106f32e7eSjoerg     Assert(FPI.getRoundingMode().hasValue(),
545206f32e7eSjoerg            "invalid rounding mode argument", &FPI);
545306f32e7eSjoerg   }
545406f32e7eSjoerg }
545506f32e7eSjoerg 
visitDbgIntrinsic(StringRef Kind,DbgVariableIntrinsic & DII)545606f32e7eSjoerg void Verifier::visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII) {
5457*da58b97aSjoerg   auto *MD = DII.getRawLocation();
5458*da58b97aSjoerg   AssertDI(isa<ValueAsMetadata>(MD) || isa<DIArgList>(MD) ||
545906f32e7eSjoerg                (isa<MDNode>(MD) && !cast<MDNode>(MD)->getNumOperands()),
546006f32e7eSjoerg            "invalid llvm.dbg." + Kind + " intrinsic address/value", &DII, MD);
546106f32e7eSjoerg   AssertDI(isa<DILocalVariable>(DII.getRawVariable()),
546206f32e7eSjoerg          "invalid llvm.dbg." + Kind + " intrinsic variable", &DII,
546306f32e7eSjoerg          DII.getRawVariable());
546406f32e7eSjoerg   AssertDI(isa<DIExpression>(DII.getRawExpression()),
546506f32e7eSjoerg          "invalid llvm.dbg." + Kind + " intrinsic expression", &DII,
546606f32e7eSjoerg          DII.getRawExpression());
546706f32e7eSjoerg 
546806f32e7eSjoerg   // Ignore broken !dbg attachments; they're checked elsewhere.
546906f32e7eSjoerg   if (MDNode *N = DII.getDebugLoc().getAsMDNode())
547006f32e7eSjoerg     if (!isa<DILocation>(N))
547106f32e7eSjoerg       return;
547206f32e7eSjoerg 
547306f32e7eSjoerg   BasicBlock *BB = DII.getParent();
547406f32e7eSjoerg   Function *F = BB ? BB->getParent() : nullptr;
547506f32e7eSjoerg 
547606f32e7eSjoerg   // The scopes for variables and !dbg attachments must agree.
547706f32e7eSjoerg   DILocalVariable *Var = DII.getVariable();
547806f32e7eSjoerg   DILocation *Loc = DII.getDebugLoc();
547906f32e7eSjoerg   AssertDI(Loc, "llvm.dbg." + Kind + " intrinsic requires a !dbg attachment",
548006f32e7eSjoerg            &DII, BB, F);
548106f32e7eSjoerg 
548206f32e7eSjoerg   DISubprogram *VarSP = getSubprogram(Var->getRawScope());
548306f32e7eSjoerg   DISubprogram *LocSP = getSubprogram(Loc->getRawScope());
548406f32e7eSjoerg   if (!VarSP || !LocSP)
548506f32e7eSjoerg     return; // Broken scope chains are checked elsewhere.
548606f32e7eSjoerg 
548706f32e7eSjoerg   AssertDI(VarSP == LocSP, "mismatched subprogram between llvm.dbg." + Kind +
548806f32e7eSjoerg                                " variable and !dbg attachment",
548906f32e7eSjoerg            &DII, BB, F, Var, Var->getScope()->getSubprogram(), Loc,
549006f32e7eSjoerg            Loc->getScope()->getSubprogram());
549106f32e7eSjoerg 
549206f32e7eSjoerg   // This check is redundant with one in visitLocalVariable().
549306f32e7eSjoerg   AssertDI(isType(Var->getRawType()), "invalid type ref", Var,
549406f32e7eSjoerg            Var->getRawType());
549506f32e7eSjoerg   verifyFnArgs(DII);
549606f32e7eSjoerg }
549706f32e7eSjoerg 
visitDbgLabelIntrinsic(StringRef Kind,DbgLabelInst & DLI)549806f32e7eSjoerg void Verifier::visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI) {
549906f32e7eSjoerg   AssertDI(isa<DILabel>(DLI.getRawLabel()),
550006f32e7eSjoerg          "invalid llvm.dbg." + Kind + " intrinsic variable", &DLI,
550106f32e7eSjoerg          DLI.getRawLabel());
550206f32e7eSjoerg 
550306f32e7eSjoerg   // Ignore broken !dbg attachments; they're checked elsewhere.
550406f32e7eSjoerg   if (MDNode *N = DLI.getDebugLoc().getAsMDNode())
550506f32e7eSjoerg     if (!isa<DILocation>(N))
550606f32e7eSjoerg       return;
550706f32e7eSjoerg 
550806f32e7eSjoerg   BasicBlock *BB = DLI.getParent();
550906f32e7eSjoerg   Function *F = BB ? BB->getParent() : nullptr;
551006f32e7eSjoerg 
551106f32e7eSjoerg   // The scopes for variables and !dbg attachments must agree.
551206f32e7eSjoerg   DILabel *Label = DLI.getLabel();
551306f32e7eSjoerg   DILocation *Loc = DLI.getDebugLoc();
551406f32e7eSjoerg   Assert(Loc, "llvm.dbg." + Kind + " intrinsic requires a !dbg attachment",
551506f32e7eSjoerg          &DLI, BB, F);
551606f32e7eSjoerg 
551706f32e7eSjoerg   DISubprogram *LabelSP = getSubprogram(Label->getRawScope());
551806f32e7eSjoerg   DISubprogram *LocSP = getSubprogram(Loc->getRawScope());
551906f32e7eSjoerg   if (!LabelSP || !LocSP)
552006f32e7eSjoerg     return;
552106f32e7eSjoerg 
552206f32e7eSjoerg   AssertDI(LabelSP == LocSP, "mismatched subprogram between llvm.dbg." + Kind +
552306f32e7eSjoerg                              " label and !dbg attachment",
552406f32e7eSjoerg            &DLI, BB, F, Label, Label->getScope()->getSubprogram(), Loc,
552506f32e7eSjoerg            Loc->getScope()->getSubprogram());
552606f32e7eSjoerg }
552706f32e7eSjoerg 
verifyFragmentExpression(const DbgVariableIntrinsic & I)552806f32e7eSjoerg void Verifier::verifyFragmentExpression(const DbgVariableIntrinsic &I) {
552906f32e7eSjoerg   DILocalVariable *V = dyn_cast_or_null<DILocalVariable>(I.getRawVariable());
553006f32e7eSjoerg   DIExpression *E = dyn_cast_or_null<DIExpression>(I.getRawExpression());
553106f32e7eSjoerg 
553206f32e7eSjoerg   // We don't know whether this intrinsic verified correctly.
553306f32e7eSjoerg   if (!V || !E || !E->isValid())
553406f32e7eSjoerg     return;
553506f32e7eSjoerg 
553606f32e7eSjoerg   // Nothing to do if this isn't a DW_OP_LLVM_fragment expression.
553706f32e7eSjoerg   auto Fragment = E->getFragmentInfo();
553806f32e7eSjoerg   if (!Fragment)
553906f32e7eSjoerg     return;
554006f32e7eSjoerg 
554106f32e7eSjoerg   // The frontend helps out GDB by emitting the members of local anonymous
554206f32e7eSjoerg   // unions as artificial local variables with shared storage. When SROA splits
554306f32e7eSjoerg   // the storage for artificial local variables that are smaller than the entire
554406f32e7eSjoerg   // union, the overhang piece will be outside of the allotted space for the
554506f32e7eSjoerg   // variable and this check fails.
554606f32e7eSjoerg   // FIXME: Remove this check as soon as clang stops doing this; it hides bugs.
554706f32e7eSjoerg   if (V->isArtificial())
554806f32e7eSjoerg     return;
554906f32e7eSjoerg 
555006f32e7eSjoerg   verifyFragmentExpression(*V, *Fragment, &I);
555106f32e7eSjoerg }
555206f32e7eSjoerg 
555306f32e7eSjoerg template <typename ValueOrMetadata>
verifyFragmentExpression(const DIVariable & V,DIExpression::FragmentInfo Fragment,ValueOrMetadata * Desc)555406f32e7eSjoerg void Verifier::verifyFragmentExpression(const DIVariable &V,
555506f32e7eSjoerg                                         DIExpression::FragmentInfo Fragment,
555606f32e7eSjoerg                                         ValueOrMetadata *Desc) {
555706f32e7eSjoerg   // If there's no size, the type is broken, but that should be checked
555806f32e7eSjoerg   // elsewhere.
555906f32e7eSjoerg   auto VarSize = V.getSizeInBits();
556006f32e7eSjoerg   if (!VarSize)
556106f32e7eSjoerg     return;
556206f32e7eSjoerg 
556306f32e7eSjoerg   unsigned FragSize = Fragment.SizeInBits;
556406f32e7eSjoerg   unsigned FragOffset = Fragment.OffsetInBits;
556506f32e7eSjoerg   AssertDI(FragSize + FragOffset <= *VarSize,
556606f32e7eSjoerg          "fragment is larger than or outside of variable", Desc, &V);
556706f32e7eSjoerg   AssertDI(FragSize != *VarSize, "fragment covers entire variable", Desc, &V);
556806f32e7eSjoerg }
556906f32e7eSjoerg 
verifyFnArgs(const DbgVariableIntrinsic & I)557006f32e7eSjoerg void Verifier::verifyFnArgs(const DbgVariableIntrinsic &I) {
557106f32e7eSjoerg   // This function does not take the scope of noninlined function arguments into
557206f32e7eSjoerg   // account. Don't run it if current function is nodebug, because it may
557306f32e7eSjoerg   // contain inlined debug intrinsics.
557406f32e7eSjoerg   if (!HasDebugInfo)
557506f32e7eSjoerg     return;
557606f32e7eSjoerg 
557706f32e7eSjoerg   // For performance reasons only check non-inlined ones.
557806f32e7eSjoerg   if (I.getDebugLoc()->getInlinedAt())
557906f32e7eSjoerg     return;
558006f32e7eSjoerg 
558106f32e7eSjoerg   DILocalVariable *Var = I.getVariable();
558206f32e7eSjoerg   AssertDI(Var, "dbg intrinsic without variable");
558306f32e7eSjoerg 
558406f32e7eSjoerg   unsigned ArgNo = Var->getArg();
558506f32e7eSjoerg   if (!ArgNo)
558606f32e7eSjoerg     return;
558706f32e7eSjoerg 
558806f32e7eSjoerg   // Verify there are no duplicate function argument debug info entries.
558906f32e7eSjoerg   // These will cause hard-to-debug assertions in the DWARF backend.
559006f32e7eSjoerg   if (DebugFnArgs.size() < ArgNo)
559106f32e7eSjoerg     DebugFnArgs.resize(ArgNo, nullptr);
559206f32e7eSjoerg 
559306f32e7eSjoerg   auto *Prev = DebugFnArgs[ArgNo - 1];
559406f32e7eSjoerg   DebugFnArgs[ArgNo - 1] = Var;
559506f32e7eSjoerg   AssertDI(!Prev || (Prev == Var), "conflicting debug info for argument", &I,
559606f32e7eSjoerg            Prev, Var);
559706f32e7eSjoerg }
559806f32e7eSjoerg 
verifyNotEntryValue(const DbgVariableIntrinsic & I)559906f32e7eSjoerg void Verifier::verifyNotEntryValue(const DbgVariableIntrinsic &I) {
560006f32e7eSjoerg   DIExpression *E = dyn_cast_or_null<DIExpression>(I.getRawExpression());
560106f32e7eSjoerg 
560206f32e7eSjoerg   // We don't know whether this intrinsic verified correctly.
560306f32e7eSjoerg   if (!E || !E->isValid())
560406f32e7eSjoerg     return;
560506f32e7eSjoerg 
560606f32e7eSjoerg   AssertDI(!E->isEntryValue(), "Entry values are only allowed in MIR", &I);
560706f32e7eSjoerg }
560806f32e7eSjoerg 
verifyCompileUnits()560906f32e7eSjoerg void Verifier::verifyCompileUnits() {
561006f32e7eSjoerg   // When more than one Module is imported into the same context, such as during
561106f32e7eSjoerg   // an LTO build before linking the modules, ODR type uniquing may cause types
561206f32e7eSjoerg   // to point to a different CU. This check does not make sense in this case.
561306f32e7eSjoerg   if (M.getContext().isODRUniquingDebugTypes())
561406f32e7eSjoerg     return;
561506f32e7eSjoerg   auto *CUs = M.getNamedMetadata("llvm.dbg.cu");
561606f32e7eSjoerg   SmallPtrSet<const Metadata *, 2> Listed;
561706f32e7eSjoerg   if (CUs)
561806f32e7eSjoerg     Listed.insert(CUs->op_begin(), CUs->op_end());
561906f32e7eSjoerg   for (auto *CU : CUVisited)
562006f32e7eSjoerg     AssertDI(Listed.count(CU), "DICompileUnit not listed in llvm.dbg.cu", CU);
562106f32e7eSjoerg   CUVisited.clear();
562206f32e7eSjoerg }
562306f32e7eSjoerg 
verifyDeoptimizeCallingConvs()562406f32e7eSjoerg void Verifier::verifyDeoptimizeCallingConvs() {
562506f32e7eSjoerg   if (DeoptimizeDeclarations.empty())
562606f32e7eSjoerg     return;
562706f32e7eSjoerg 
562806f32e7eSjoerg   const Function *First = DeoptimizeDeclarations[0];
562906f32e7eSjoerg   for (auto *F : makeArrayRef(DeoptimizeDeclarations).slice(1)) {
563006f32e7eSjoerg     Assert(First->getCallingConv() == F->getCallingConv(),
563106f32e7eSjoerg            "All llvm.experimental.deoptimize declarations must have the same "
563206f32e7eSjoerg            "calling convention",
563306f32e7eSjoerg            First, F);
563406f32e7eSjoerg   }
563506f32e7eSjoerg }
563606f32e7eSjoerg 
verifySourceDebugInfo(const DICompileUnit & U,const DIFile & F)563706f32e7eSjoerg void Verifier::verifySourceDebugInfo(const DICompileUnit &U, const DIFile &F) {
563806f32e7eSjoerg   bool HasSource = F.getSource().hasValue();
563906f32e7eSjoerg   if (!HasSourceDebugInfo.count(&U))
564006f32e7eSjoerg     HasSourceDebugInfo[&U] = HasSource;
564106f32e7eSjoerg   AssertDI(HasSource == HasSourceDebugInfo[&U],
564206f32e7eSjoerg            "inconsistent use of embedded source");
564306f32e7eSjoerg }
564406f32e7eSjoerg 
verifyNoAliasScopeDecl()5645*da58b97aSjoerg void Verifier::verifyNoAliasScopeDecl() {
5646*da58b97aSjoerg   if (NoAliasScopeDecls.empty())
5647*da58b97aSjoerg     return;
5648*da58b97aSjoerg 
5649*da58b97aSjoerg   // only a single scope must be declared at a time.
5650*da58b97aSjoerg   for (auto *II : NoAliasScopeDecls) {
5651*da58b97aSjoerg     assert(II->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl &&
5652*da58b97aSjoerg            "Not a llvm.experimental.noalias.scope.decl ?");
5653*da58b97aSjoerg     const auto *ScopeListMV = dyn_cast<MetadataAsValue>(
5654*da58b97aSjoerg         II->getOperand(Intrinsic::NoAliasScopeDeclScopeArg));
5655*da58b97aSjoerg     Assert(ScopeListMV != nullptr,
5656*da58b97aSjoerg            "llvm.experimental.noalias.scope.decl must have a MetadataAsValue "
5657*da58b97aSjoerg            "argument",
5658*da58b97aSjoerg            II);
5659*da58b97aSjoerg 
5660*da58b97aSjoerg     const auto *ScopeListMD = dyn_cast<MDNode>(ScopeListMV->getMetadata());
5661*da58b97aSjoerg     Assert(ScopeListMD != nullptr, "!id.scope.list must point to an MDNode",
5662*da58b97aSjoerg            II);
5663*da58b97aSjoerg     Assert(ScopeListMD->getNumOperands() == 1,
5664*da58b97aSjoerg            "!id.scope.list must point to a list with a single scope", II);
5665*da58b97aSjoerg   }
5666*da58b97aSjoerg 
5667*da58b97aSjoerg   // Only check the domination rule when requested. Once all passes have been
5668*da58b97aSjoerg   // adapted this option can go away.
5669*da58b97aSjoerg   if (!VerifyNoAliasScopeDomination)
5670*da58b97aSjoerg     return;
5671*da58b97aSjoerg 
5672*da58b97aSjoerg   // Now sort the intrinsics based on the scope MDNode so that declarations of
5673*da58b97aSjoerg   // the same scopes are next to each other.
5674*da58b97aSjoerg   auto GetScope = [](IntrinsicInst *II) {
5675*da58b97aSjoerg     const auto *ScopeListMV = cast<MetadataAsValue>(
5676*da58b97aSjoerg         II->getOperand(Intrinsic::NoAliasScopeDeclScopeArg));
5677*da58b97aSjoerg     return &cast<MDNode>(ScopeListMV->getMetadata())->getOperand(0);
5678*da58b97aSjoerg   };
5679*da58b97aSjoerg 
5680*da58b97aSjoerg   // We are sorting on MDNode pointers here. For valid input IR this is ok.
5681*da58b97aSjoerg   // TODO: Sort on Metadata ID to avoid non-deterministic error messages.
5682*da58b97aSjoerg   auto Compare = [GetScope](IntrinsicInst *Lhs, IntrinsicInst *Rhs) {
5683*da58b97aSjoerg     return GetScope(Lhs) < GetScope(Rhs);
5684*da58b97aSjoerg   };
5685*da58b97aSjoerg 
5686*da58b97aSjoerg   llvm::sort(NoAliasScopeDecls, Compare);
5687*da58b97aSjoerg 
5688*da58b97aSjoerg   // Go over the intrinsics and check that for the same scope, they are not
5689*da58b97aSjoerg   // dominating each other.
5690*da58b97aSjoerg   auto ItCurrent = NoAliasScopeDecls.begin();
5691*da58b97aSjoerg   while (ItCurrent != NoAliasScopeDecls.end()) {
5692*da58b97aSjoerg     auto CurScope = GetScope(*ItCurrent);
5693*da58b97aSjoerg     auto ItNext = ItCurrent;
5694*da58b97aSjoerg     do {
5695*da58b97aSjoerg       ++ItNext;
5696*da58b97aSjoerg     } while (ItNext != NoAliasScopeDecls.end() &&
5697*da58b97aSjoerg              GetScope(*ItNext) == CurScope);
5698*da58b97aSjoerg 
5699*da58b97aSjoerg     // [ItCurrent, ItNext) represents the declarations for the same scope.
5700*da58b97aSjoerg     // Ensure they are not dominating each other.. but only if it is not too
5701*da58b97aSjoerg     // expensive.
5702*da58b97aSjoerg     if (ItNext - ItCurrent < 32)
5703*da58b97aSjoerg       for (auto *I : llvm::make_range(ItCurrent, ItNext))
5704*da58b97aSjoerg         for (auto *J : llvm::make_range(ItCurrent, ItNext))
5705*da58b97aSjoerg           if (I != J)
5706*da58b97aSjoerg             Assert(!DT.dominates(I, J),
5707*da58b97aSjoerg                    "llvm.experimental.noalias.scope.decl dominates another one "
5708*da58b97aSjoerg                    "with the same scope",
5709*da58b97aSjoerg                    I);
5710*da58b97aSjoerg     ItCurrent = ItNext;
5711*da58b97aSjoerg   }
5712*da58b97aSjoerg }
5713*da58b97aSjoerg 
571406f32e7eSjoerg //===----------------------------------------------------------------------===//
571506f32e7eSjoerg //  Implement the public interfaces to this file...
571606f32e7eSjoerg //===----------------------------------------------------------------------===//
571706f32e7eSjoerg 
verifyFunction(const Function & f,raw_ostream * OS)571806f32e7eSjoerg bool llvm::verifyFunction(const Function &f, raw_ostream *OS) {
571906f32e7eSjoerg   Function &F = const_cast<Function &>(f);
572006f32e7eSjoerg 
572106f32e7eSjoerg   // Don't use a raw_null_ostream.  Printing IR is expensive.
572206f32e7eSjoerg   Verifier V(OS, /*ShouldTreatBrokenDebugInfoAsError=*/true, *f.getParent());
572306f32e7eSjoerg 
572406f32e7eSjoerg   // Note that this function's return value is inverted from what you would
572506f32e7eSjoerg   // expect of a function called "verify".
572606f32e7eSjoerg   return !V.verify(F);
572706f32e7eSjoerg }
572806f32e7eSjoerg 
verifyModule(const Module & M,raw_ostream * OS,bool * BrokenDebugInfo)572906f32e7eSjoerg bool llvm::verifyModule(const Module &M, raw_ostream *OS,
573006f32e7eSjoerg                         bool *BrokenDebugInfo) {
573106f32e7eSjoerg   // Don't use a raw_null_ostream.  Printing IR is expensive.
573206f32e7eSjoerg   Verifier V(OS, /*ShouldTreatBrokenDebugInfoAsError=*/!BrokenDebugInfo, M);
573306f32e7eSjoerg 
573406f32e7eSjoerg   bool Broken = false;
573506f32e7eSjoerg   for (const Function &F : M)
573606f32e7eSjoerg     Broken |= !V.verify(F);
573706f32e7eSjoerg 
573806f32e7eSjoerg   Broken |= !V.verify();
573906f32e7eSjoerg   if (BrokenDebugInfo)
574006f32e7eSjoerg     *BrokenDebugInfo = V.hasBrokenDebugInfo();
574106f32e7eSjoerg   // Note that this function's return value is inverted from what you would
574206f32e7eSjoerg   // expect of a function called "verify".
574306f32e7eSjoerg   return Broken;
574406f32e7eSjoerg }
574506f32e7eSjoerg 
574606f32e7eSjoerg namespace {
574706f32e7eSjoerg 
574806f32e7eSjoerg struct VerifierLegacyPass : public FunctionPass {
574906f32e7eSjoerg   static char ID;
575006f32e7eSjoerg 
575106f32e7eSjoerg   std::unique_ptr<Verifier> V;
575206f32e7eSjoerg   bool FatalErrors = true;
575306f32e7eSjoerg 
VerifierLegacyPass__anona49007370b11::VerifierLegacyPass575406f32e7eSjoerg   VerifierLegacyPass() : FunctionPass(ID) {
575506f32e7eSjoerg     initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
575606f32e7eSjoerg   }
VerifierLegacyPass__anona49007370b11::VerifierLegacyPass575706f32e7eSjoerg   explicit VerifierLegacyPass(bool FatalErrors)
575806f32e7eSjoerg       : FunctionPass(ID),
575906f32e7eSjoerg         FatalErrors(FatalErrors) {
576006f32e7eSjoerg     initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
576106f32e7eSjoerg   }
576206f32e7eSjoerg 
doInitialization__anona49007370b11::VerifierLegacyPass576306f32e7eSjoerg   bool doInitialization(Module &M) override {
576406f32e7eSjoerg     V = std::make_unique<Verifier>(
576506f32e7eSjoerg         &dbgs(), /*ShouldTreatBrokenDebugInfoAsError=*/false, M);
576606f32e7eSjoerg     return false;
576706f32e7eSjoerg   }
576806f32e7eSjoerg 
runOnFunction__anona49007370b11::VerifierLegacyPass576906f32e7eSjoerg   bool runOnFunction(Function &F) override {
577006f32e7eSjoerg     if (!V->verify(F) && FatalErrors) {
577106f32e7eSjoerg       errs() << "in function " << F.getName() << '\n';
577206f32e7eSjoerg       report_fatal_error("Broken function found, compilation aborted!");
577306f32e7eSjoerg     }
577406f32e7eSjoerg     return false;
577506f32e7eSjoerg   }
577606f32e7eSjoerg 
doFinalization__anona49007370b11::VerifierLegacyPass577706f32e7eSjoerg   bool doFinalization(Module &M) override {
577806f32e7eSjoerg     bool HasErrors = false;
577906f32e7eSjoerg     for (Function &F : M)
578006f32e7eSjoerg       if (F.isDeclaration())
578106f32e7eSjoerg         HasErrors |= !V->verify(F);
578206f32e7eSjoerg 
578306f32e7eSjoerg     HasErrors |= !V->verify();
578406f32e7eSjoerg     if (FatalErrors && (HasErrors || V->hasBrokenDebugInfo()))
578506f32e7eSjoerg       report_fatal_error("Broken module found, compilation aborted!");
578606f32e7eSjoerg     return false;
578706f32e7eSjoerg   }
578806f32e7eSjoerg 
getAnalysisUsage__anona49007370b11::VerifierLegacyPass578906f32e7eSjoerg   void getAnalysisUsage(AnalysisUsage &AU) const override {
579006f32e7eSjoerg     AU.setPreservesAll();
579106f32e7eSjoerg   }
579206f32e7eSjoerg };
579306f32e7eSjoerg 
579406f32e7eSjoerg } // end anonymous namespace
579506f32e7eSjoerg 
579606f32e7eSjoerg /// Helper to issue failure from the TBAA verification
CheckFailed(Tys &&...Args)579706f32e7eSjoerg template <typename... Tys> void TBAAVerifier::CheckFailed(Tys &&... Args) {
579806f32e7eSjoerg   if (Diagnostic)
579906f32e7eSjoerg     return Diagnostic->CheckFailed(Args...);
580006f32e7eSjoerg }
580106f32e7eSjoerg 
580206f32e7eSjoerg #define AssertTBAA(C, ...)                                                     \
580306f32e7eSjoerg   do {                                                                         \
580406f32e7eSjoerg     if (!(C)) {                                                                \
580506f32e7eSjoerg       CheckFailed(__VA_ARGS__);                                                \
580606f32e7eSjoerg       return false;                                                            \
580706f32e7eSjoerg     }                                                                          \
580806f32e7eSjoerg   } while (false)
580906f32e7eSjoerg 
581006f32e7eSjoerg /// Verify that \p BaseNode can be used as the "base type" in the struct-path
581106f32e7eSjoerg /// TBAA scheme.  This means \p BaseNode is either a scalar node, or a
581206f32e7eSjoerg /// struct-type node describing an aggregate data structure (like a struct).
581306f32e7eSjoerg TBAAVerifier::TBAABaseNodeSummary
verifyTBAABaseNode(Instruction & I,const MDNode * BaseNode,bool IsNewFormat)581406f32e7eSjoerg TBAAVerifier::verifyTBAABaseNode(Instruction &I, const MDNode *BaseNode,
581506f32e7eSjoerg                                  bool IsNewFormat) {
581606f32e7eSjoerg   if (BaseNode->getNumOperands() < 2) {
581706f32e7eSjoerg     CheckFailed("Base nodes must have at least two operands", &I, BaseNode);
581806f32e7eSjoerg     return {true, ~0u};
581906f32e7eSjoerg   }
582006f32e7eSjoerg 
582106f32e7eSjoerg   auto Itr = TBAABaseNodes.find(BaseNode);
582206f32e7eSjoerg   if (Itr != TBAABaseNodes.end())
582306f32e7eSjoerg     return Itr->second;
582406f32e7eSjoerg 
582506f32e7eSjoerg   auto Result = verifyTBAABaseNodeImpl(I, BaseNode, IsNewFormat);
582606f32e7eSjoerg   auto InsertResult = TBAABaseNodes.insert({BaseNode, Result});
582706f32e7eSjoerg   (void)InsertResult;
582806f32e7eSjoerg   assert(InsertResult.second && "We just checked!");
582906f32e7eSjoerg   return Result;
583006f32e7eSjoerg }
583106f32e7eSjoerg 
583206f32e7eSjoerg TBAAVerifier::TBAABaseNodeSummary
verifyTBAABaseNodeImpl(Instruction & I,const MDNode * BaseNode,bool IsNewFormat)583306f32e7eSjoerg TBAAVerifier::verifyTBAABaseNodeImpl(Instruction &I, const MDNode *BaseNode,
583406f32e7eSjoerg                                      bool IsNewFormat) {
583506f32e7eSjoerg   const TBAAVerifier::TBAABaseNodeSummary InvalidNode = {true, ~0u};
583606f32e7eSjoerg 
583706f32e7eSjoerg   if (BaseNode->getNumOperands() == 2) {
583806f32e7eSjoerg     // Scalar nodes can only be accessed at offset 0.
583906f32e7eSjoerg     return isValidScalarTBAANode(BaseNode)
584006f32e7eSjoerg                ? TBAAVerifier::TBAABaseNodeSummary({false, 0})
584106f32e7eSjoerg                : InvalidNode;
584206f32e7eSjoerg   }
584306f32e7eSjoerg 
584406f32e7eSjoerg   if (IsNewFormat) {
584506f32e7eSjoerg     if (BaseNode->getNumOperands() % 3 != 0) {
584606f32e7eSjoerg       CheckFailed("Access tag nodes must have the number of operands that is a "
584706f32e7eSjoerg                   "multiple of 3!", BaseNode);
584806f32e7eSjoerg       return InvalidNode;
584906f32e7eSjoerg     }
585006f32e7eSjoerg   } else {
585106f32e7eSjoerg     if (BaseNode->getNumOperands() % 2 != 1) {
585206f32e7eSjoerg       CheckFailed("Struct tag nodes must have an odd number of operands!",
585306f32e7eSjoerg                   BaseNode);
585406f32e7eSjoerg       return InvalidNode;
585506f32e7eSjoerg     }
585606f32e7eSjoerg   }
585706f32e7eSjoerg 
585806f32e7eSjoerg   // Check the type size field.
585906f32e7eSjoerg   if (IsNewFormat) {
586006f32e7eSjoerg     auto *TypeSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
586106f32e7eSjoerg         BaseNode->getOperand(1));
586206f32e7eSjoerg     if (!TypeSizeNode) {
586306f32e7eSjoerg       CheckFailed("Type size nodes must be constants!", &I, BaseNode);
586406f32e7eSjoerg       return InvalidNode;
586506f32e7eSjoerg     }
586606f32e7eSjoerg   }
586706f32e7eSjoerg 
586806f32e7eSjoerg   // Check the type name field. In the new format it can be anything.
586906f32e7eSjoerg   if (!IsNewFormat && !isa<MDString>(BaseNode->getOperand(0))) {
587006f32e7eSjoerg     CheckFailed("Struct tag nodes have a string as their first operand",
587106f32e7eSjoerg                 BaseNode);
587206f32e7eSjoerg     return InvalidNode;
587306f32e7eSjoerg   }
587406f32e7eSjoerg 
587506f32e7eSjoerg   bool Failed = false;
587606f32e7eSjoerg 
587706f32e7eSjoerg   Optional<APInt> PrevOffset;
587806f32e7eSjoerg   unsigned BitWidth = ~0u;
587906f32e7eSjoerg 
588006f32e7eSjoerg   // We've already checked that BaseNode is not a degenerate root node with one
588106f32e7eSjoerg   // operand in \c verifyTBAABaseNode, so this loop should run at least once.
588206f32e7eSjoerg   unsigned FirstFieldOpNo = IsNewFormat ? 3 : 1;
588306f32e7eSjoerg   unsigned NumOpsPerField = IsNewFormat ? 3 : 2;
588406f32e7eSjoerg   for (unsigned Idx = FirstFieldOpNo; Idx < BaseNode->getNumOperands();
588506f32e7eSjoerg            Idx += NumOpsPerField) {
588606f32e7eSjoerg     const MDOperand &FieldTy = BaseNode->getOperand(Idx);
588706f32e7eSjoerg     const MDOperand &FieldOffset = BaseNode->getOperand(Idx + 1);
588806f32e7eSjoerg     if (!isa<MDNode>(FieldTy)) {
588906f32e7eSjoerg       CheckFailed("Incorrect field entry in struct type node!", &I, BaseNode);
589006f32e7eSjoerg       Failed = true;
589106f32e7eSjoerg       continue;
589206f32e7eSjoerg     }
589306f32e7eSjoerg 
589406f32e7eSjoerg     auto *OffsetEntryCI =
589506f32e7eSjoerg         mdconst::dyn_extract_or_null<ConstantInt>(FieldOffset);
589606f32e7eSjoerg     if (!OffsetEntryCI) {
589706f32e7eSjoerg       CheckFailed("Offset entries must be constants!", &I, BaseNode);
589806f32e7eSjoerg       Failed = true;
589906f32e7eSjoerg       continue;
590006f32e7eSjoerg     }
590106f32e7eSjoerg 
590206f32e7eSjoerg     if (BitWidth == ~0u)
590306f32e7eSjoerg       BitWidth = OffsetEntryCI->getBitWidth();
590406f32e7eSjoerg 
590506f32e7eSjoerg     if (OffsetEntryCI->getBitWidth() != BitWidth) {
590606f32e7eSjoerg       CheckFailed(
590706f32e7eSjoerg           "Bitwidth between the offsets and struct type entries must match", &I,
590806f32e7eSjoerg           BaseNode);
590906f32e7eSjoerg       Failed = true;
591006f32e7eSjoerg       continue;
591106f32e7eSjoerg     }
591206f32e7eSjoerg 
591306f32e7eSjoerg     // NB! As far as I can tell, we generate a non-strictly increasing offset
591406f32e7eSjoerg     // sequence only from structs that have zero size bit fields.  When
591506f32e7eSjoerg     // recursing into a contained struct in \c getFieldNodeFromTBAABaseNode we
591606f32e7eSjoerg     // pick the field lexically the latest in struct type metadata node.  This
591706f32e7eSjoerg     // mirrors the actual behavior of the alias analysis implementation.
591806f32e7eSjoerg     bool IsAscending =
591906f32e7eSjoerg         !PrevOffset || PrevOffset->ule(OffsetEntryCI->getValue());
592006f32e7eSjoerg 
592106f32e7eSjoerg     if (!IsAscending) {
592206f32e7eSjoerg       CheckFailed("Offsets must be increasing!", &I, BaseNode);
592306f32e7eSjoerg       Failed = true;
592406f32e7eSjoerg     }
592506f32e7eSjoerg 
592606f32e7eSjoerg     PrevOffset = OffsetEntryCI->getValue();
592706f32e7eSjoerg 
592806f32e7eSjoerg     if (IsNewFormat) {
592906f32e7eSjoerg       auto *MemberSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
593006f32e7eSjoerg           BaseNode->getOperand(Idx + 2));
593106f32e7eSjoerg       if (!MemberSizeNode) {
593206f32e7eSjoerg         CheckFailed("Member size entries must be constants!", &I, BaseNode);
593306f32e7eSjoerg         Failed = true;
593406f32e7eSjoerg         continue;
593506f32e7eSjoerg       }
593606f32e7eSjoerg     }
593706f32e7eSjoerg   }
593806f32e7eSjoerg 
593906f32e7eSjoerg   return Failed ? InvalidNode
594006f32e7eSjoerg                 : TBAAVerifier::TBAABaseNodeSummary(false, BitWidth);
594106f32e7eSjoerg }
594206f32e7eSjoerg 
IsRootTBAANode(const MDNode * MD)594306f32e7eSjoerg static bool IsRootTBAANode(const MDNode *MD) {
594406f32e7eSjoerg   return MD->getNumOperands() < 2;
594506f32e7eSjoerg }
594606f32e7eSjoerg 
IsScalarTBAANodeImpl(const MDNode * MD,SmallPtrSetImpl<const MDNode * > & Visited)594706f32e7eSjoerg static bool IsScalarTBAANodeImpl(const MDNode *MD,
594806f32e7eSjoerg                                  SmallPtrSetImpl<const MDNode *> &Visited) {
594906f32e7eSjoerg   if (MD->getNumOperands() != 2 && MD->getNumOperands() != 3)
595006f32e7eSjoerg     return false;
595106f32e7eSjoerg 
595206f32e7eSjoerg   if (!isa<MDString>(MD->getOperand(0)))
595306f32e7eSjoerg     return false;
595406f32e7eSjoerg 
595506f32e7eSjoerg   if (MD->getNumOperands() == 3) {
595606f32e7eSjoerg     auto *Offset = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
595706f32e7eSjoerg     if (!(Offset && Offset->isZero() && isa<MDString>(MD->getOperand(0))))
595806f32e7eSjoerg       return false;
595906f32e7eSjoerg   }
596006f32e7eSjoerg 
596106f32e7eSjoerg   auto *Parent = dyn_cast_or_null<MDNode>(MD->getOperand(1));
596206f32e7eSjoerg   return Parent && Visited.insert(Parent).second &&
596306f32e7eSjoerg          (IsRootTBAANode(Parent) || IsScalarTBAANodeImpl(Parent, Visited));
596406f32e7eSjoerg }
596506f32e7eSjoerg 
isValidScalarTBAANode(const MDNode * MD)596606f32e7eSjoerg bool TBAAVerifier::isValidScalarTBAANode(const MDNode *MD) {
596706f32e7eSjoerg   auto ResultIt = TBAAScalarNodes.find(MD);
596806f32e7eSjoerg   if (ResultIt != TBAAScalarNodes.end())
596906f32e7eSjoerg     return ResultIt->second;
597006f32e7eSjoerg 
597106f32e7eSjoerg   SmallPtrSet<const MDNode *, 4> Visited;
597206f32e7eSjoerg   bool Result = IsScalarTBAANodeImpl(MD, Visited);
597306f32e7eSjoerg   auto InsertResult = TBAAScalarNodes.insert({MD, Result});
597406f32e7eSjoerg   (void)InsertResult;
597506f32e7eSjoerg   assert(InsertResult.second && "Just checked!");
597606f32e7eSjoerg 
597706f32e7eSjoerg   return Result;
597806f32e7eSjoerg }
597906f32e7eSjoerg 
598006f32e7eSjoerg /// Returns the field node at the offset \p Offset in \p BaseNode.  Update \p
598106f32e7eSjoerg /// Offset in place to be the offset within the field node returned.
598206f32e7eSjoerg ///
598306f32e7eSjoerg /// We assume we've okayed \p BaseNode via \c verifyTBAABaseNode.
getFieldNodeFromTBAABaseNode(Instruction & I,const MDNode * BaseNode,APInt & Offset,bool IsNewFormat)598406f32e7eSjoerg MDNode *TBAAVerifier::getFieldNodeFromTBAABaseNode(Instruction &I,
598506f32e7eSjoerg                                                    const MDNode *BaseNode,
598606f32e7eSjoerg                                                    APInt &Offset,
598706f32e7eSjoerg                                                    bool IsNewFormat) {
598806f32e7eSjoerg   assert(BaseNode->getNumOperands() >= 2 && "Invalid base node!");
598906f32e7eSjoerg 
599006f32e7eSjoerg   // Scalar nodes have only one possible "field" -- their parent in the access
599106f32e7eSjoerg   // hierarchy.  Offset must be zero at this point, but our caller is supposed
599206f32e7eSjoerg   // to Assert that.
599306f32e7eSjoerg   if (BaseNode->getNumOperands() == 2)
599406f32e7eSjoerg     return cast<MDNode>(BaseNode->getOperand(1));
599506f32e7eSjoerg 
599606f32e7eSjoerg   unsigned FirstFieldOpNo = IsNewFormat ? 3 : 1;
599706f32e7eSjoerg   unsigned NumOpsPerField = IsNewFormat ? 3 : 2;
599806f32e7eSjoerg   for (unsigned Idx = FirstFieldOpNo; Idx < BaseNode->getNumOperands();
599906f32e7eSjoerg            Idx += NumOpsPerField) {
600006f32e7eSjoerg     auto *OffsetEntryCI =
600106f32e7eSjoerg         mdconst::extract<ConstantInt>(BaseNode->getOperand(Idx + 1));
600206f32e7eSjoerg     if (OffsetEntryCI->getValue().ugt(Offset)) {
600306f32e7eSjoerg       if (Idx == FirstFieldOpNo) {
600406f32e7eSjoerg         CheckFailed("Could not find TBAA parent in struct type node", &I,
600506f32e7eSjoerg                     BaseNode, &Offset);
600606f32e7eSjoerg         return nullptr;
600706f32e7eSjoerg       }
600806f32e7eSjoerg 
600906f32e7eSjoerg       unsigned PrevIdx = Idx - NumOpsPerField;
601006f32e7eSjoerg       auto *PrevOffsetEntryCI =
601106f32e7eSjoerg           mdconst::extract<ConstantInt>(BaseNode->getOperand(PrevIdx + 1));
601206f32e7eSjoerg       Offset -= PrevOffsetEntryCI->getValue();
601306f32e7eSjoerg       return cast<MDNode>(BaseNode->getOperand(PrevIdx));
601406f32e7eSjoerg     }
601506f32e7eSjoerg   }
601606f32e7eSjoerg 
601706f32e7eSjoerg   unsigned LastIdx = BaseNode->getNumOperands() - NumOpsPerField;
601806f32e7eSjoerg   auto *LastOffsetEntryCI = mdconst::extract<ConstantInt>(
601906f32e7eSjoerg       BaseNode->getOperand(LastIdx + 1));
602006f32e7eSjoerg   Offset -= LastOffsetEntryCI->getValue();
602106f32e7eSjoerg   return cast<MDNode>(BaseNode->getOperand(LastIdx));
602206f32e7eSjoerg }
602306f32e7eSjoerg 
isNewFormatTBAATypeNode(llvm::MDNode * Type)602406f32e7eSjoerg static bool isNewFormatTBAATypeNode(llvm::MDNode *Type) {
602506f32e7eSjoerg   if (!Type || Type->getNumOperands() < 3)
602606f32e7eSjoerg     return false;
602706f32e7eSjoerg 
602806f32e7eSjoerg   // In the new format type nodes shall have a reference to the parent type as
602906f32e7eSjoerg   // its first operand.
603006f32e7eSjoerg   MDNode *Parent = dyn_cast_or_null<MDNode>(Type->getOperand(0));
603106f32e7eSjoerg   if (!Parent)
603206f32e7eSjoerg     return false;
603306f32e7eSjoerg 
603406f32e7eSjoerg   return true;
603506f32e7eSjoerg }
603606f32e7eSjoerg 
visitTBAAMetadata(Instruction & I,const MDNode * MD)603706f32e7eSjoerg bool TBAAVerifier::visitTBAAMetadata(Instruction &I, const MDNode *MD) {
603806f32e7eSjoerg   AssertTBAA(isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallInst>(I) ||
603906f32e7eSjoerg                  isa<VAArgInst>(I) || isa<AtomicRMWInst>(I) ||
604006f32e7eSjoerg                  isa<AtomicCmpXchgInst>(I),
604106f32e7eSjoerg              "This instruction shall not have a TBAA access tag!", &I);
604206f32e7eSjoerg 
604306f32e7eSjoerg   bool IsStructPathTBAA =
604406f32e7eSjoerg       isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3;
604506f32e7eSjoerg 
604606f32e7eSjoerg   AssertTBAA(
604706f32e7eSjoerg       IsStructPathTBAA,
604806f32e7eSjoerg       "Old-style TBAA is no longer allowed, use struct-path TBAA instead", &I);
604906f32e7eSjoerg 
605006f32e7eSjoerg   MDNode *BaseNode = dyn_cast_or_null<MDNode>(MD->getOperand(0));
605106f32e7eSjoerg   MDNode *AccessType = dyn_cast_or_null<MDNode>(MD->getOperand(1));
605206f32e7eSjoerg 
605306f32e7eSjoerg   bool IsNewFormat = isNewFormatTBAATypeNode(AccessType);
605406f32e7eSjoerg 
605506f32e7eSjoerg   if (IsNewFormat) {
605606f32e7eSjoerg     AssertTBAA(MD->getNumOperands() == 4 || MD->getNumOperands() == 5,
605706f32e7eSjoerg                "Access tag metadata must have either 4 or 5 operands", &I, MD);
605806f32e7eSjoerg   } else {
605906f32e7eSjoerg     AssertTBAA(MD->getNumOperands() < 5,
606006f32e7eSjoerg                "Struct tag metadata must have either 3 or 4 operands", &I, MD);
606106f32e7eSjoerg   }
606206f32e7eSjoerg 
606306f32e7eSjoerg   // Check the access size field.
606406f32e7eSjoerg   if (IsNewFormat) {
606506f32e7eSjoerg     auto *AccessSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
606606f32e7eSjoerg         MD->getOperand(3));
606706f32e7eSjoerg     AssertTBAA(AccessSizeNode, "Access size field must be a constant", &I, MD);
606806f32e7eSjoerg   }
606906f32e7eSjoerg 
607006f32e7eSjoerg   // Check the immutability flag.
607106f32e7eSjoerg   unsigned ImmutabilityFlagOpNo = IsNewFormat ? 4 : 3;
607206f32e7eSjoerg   if (MD->getNumOperands() == ImmutabilityFlagOpNo + 1) {
607306f32e7eSjoerg     auto *IsImmutableCI = mdconst::dyn_extract_or_null<ConstantInt>(
607406f32e7eSjoerg         MD->getOperand(ImmutabilityFlagOpNo));
607506f32e7eSjoerg     AssertTBAA(IsImmutableCI,
607606f32e7eSjoerg                "Immutability tag on struct tag metadata must be a constant",
607706f32e7eSjoerg                &I, MD);
607806f32e7eSjoerg     AssertTBAA(
607906f32e7eSjoerg         IsImmutableCI->isZero() || IsImmutableCI->isOne(),
608006f32e7eSjoerg         "Immutability part of the struct tag metadata must be either 0 or 1",
608106f32e7eSjoerg         &I, MD);
608206f32e7eSjoerg   }
608306f32e7eSjoerg 
608406f32e7eSjoerg   AssertTBAA(BaseNode && AccessType,
608506f32e7eSjoerg              "Malformed struct tag metadata: base and access-type "
608606f32e7eSjoerg              "should be non-null and point to Metadata nodes",
608706f32e7eSjoerg              &I, MD, BaseNode, AccessType);
608806f32e7eSjoerg 
608906f32e7eSjoerg   if (!IsNewFormat) {
609006f32e7eSjoerg     AssertTBAA(isValidScalarTBAANode(AccessType),
609106f32e7eSjoerg                "Access type node must be a valid scalar type", &I, MD,
609206f32e7eSjoerg                AccessType);
609306f32e7eSjoerg   }
609406f32e7eSjoerg 
609506f32e7eSjoerg   auto *OffsetCI = mdconst::dyn_extract_or_null<ConstantInt>(MD->getOperand(2));
609606f32e7eSjoerg   AssertTBAA(OffsetCI, "Offset must be constant integer", &I, MD);
609706f32e7eSjoerg 
609806f32e7eSjoerg   APInt Offset = OffsetCI->getValue();
609906f32e7eSjoerg   bool SeenAccessTypeInPath = false;
610006f32e7eSjoerg 
610106f32e7eSjoerg   SmallPtrSet<MDNode *, 4> StructPath;
610206f32e7eSjoerg 
610306f32e7eSjoerg   for (/* empty */; BaseNode && !IsRootTBAANode(BaseNode);
610406f32e7eSjoerg        BaseNode = getFieldNodeFromTBAABaseNode(I, BaseNode, Offset,
610506f32e7eSjoerg                                                IsNewFormat)) {
610606f32e7eSjoerg     if (!StructPath.insert(BaseNode).second) {
610706f32e7eSjoerg       CheckFailed("Cycle detected in struct path", &I, MD);
610806f32e7eSjoerg       return false;
610906f32e7eSjoerg     }
611006f32e7eSjoerg 
611106f32e7eSjoerg     bool Invalid;
611206f32e7eSjoerg     unsigned BaseNodeBitWidth;
611306f32e7eSjoerg     std::tie(Invalid, BaseNodeBitWidth) = verifyTBAABaseNode(I, BaseNode,
611406f32e7eSjoerg                                                              IsNewFormat);
611506f32e7eSjoerg 
611606f32e7eSjoerg     // If the base node is invalid in itself, then we've already printed all the
611706f32e7eSjoerg     // errors we wanted to print.
611806f32e7eSjoerg     if (Invalid)
611906f32e7eSjoerg       return false;
612006f32e7eSjoerg 
612106f32e7eSjoerg     SeenAccessTypeInPath |= BaseNode == AccessType;
612206f32e7eSjoerg 
612306f32e7eSjoerg     if (isValidScalarTBAANode(BaseNode) || BaseNode == AccessType)
612406f32e7eSjoerg       AssertTBAA(Offset == 0, "Offset not zero at the point of scalar access",
612506f32e7eSjoerg                  &I, MD, &Offset);
612606f32e7eSjoerg 
612706f32e7eSjoerg     AssertTBAA(BaseNodeBitWidth == Offset.getBitWidth() ||
612806f32e7eSjoerg                    (BaseNodeBitWidth == 0 && Offset == 0) ||
612906f32e7eSjoerg                    (IsNewFormat && BaseNodeBitWidth == ~0u),
613006f32e7eSjoerg                "Access bit-width not the same as description bit-width", &I, MD,
613106f32e7eSjoerg                BaseNodeBitWidth, Offset.getBitWidth());
613206f32e7eSjoerg 
613306f32e7eSjoerg     if (IsNewFormat && SeenAccessTypeInPath)
613406f32e7eSjoerg       break;
613506f32e7eSjoerg   }
613606f32e7eSjoerg 
613706f32e7eSjoerg   AssertTBAA(SeenAccessTypeInPath, "Did not see access type in access path!",
613806f32e7eSjoerg              &I, MD);
613906f32e7eSjoerg   return true;
614006f32e7eSjoerg }
614106f32e7eSjoerg 
614206f32e7eSjoerg char VerifierLegacyPass::ID = 0;
614306f32e7eSjoerg INITIALIZE_PASS(VerifierLegacyPass, "verify", "Module Verifier", false, false)
614406f32e7eSjoerg 
createVerifierPass(bool FatalErrors)614506f32e7eSjoerg FunctionPass *llvm::createVerifierPass(bool FatalErrors) {
614606f32e7eSjoerg   return new VerifierLegacyPass(FatalErrors);
614706f32e7eSjoerg }
614806f32e7eSjoerg 
614906f32e7eSjoerg AnalysisKey VerifierAnalysis::Key;
run(Module & M,ModuleAnalysisManager &)615006f32e7eSjoerg VerifierAnalysis::Result VerifierAnalysis::run(Module &M,
615106f32e7eSjoerg                                                ModuleAnalysisManager &) {
615206f32e7eSjoerg   Result Res;
615306f32e7eSjoerg   Res.IRBroken = llvm::verifyModule(M, &dbgs(), &Res.DebugInfoBroken);
615406f32e7eSjoerg   return Res;
615506f32e7eSjoerg }
615606f32e7eSjoerg 
run(Function & F,FunctionAnalysisManager &)615706f32e7eSjoerg VerifierAnalysis::Result VerifierAnalysis::run(Function &F,
615806f32e7eSjoerg                                                FunctionAnalysisManager &) {
615906f32e7eSjoerg   return { llvm::verifyFunction(F, &dbgs()), false };
616006f32e7eSjoerg }
616106f32e7eSjoerg 
run(Module & M,ModuleAnalysisManager & AM)616206f32e7eSjoerg PreservedAnalyses VerifierPass::run(Module &M, ModuleAnalysisManager &AM) {
616306f32e7eSjoerg   auto Res = AM.getResult<VerifierAnalysis>(M);
616406f32e7eSjoerg   if (FatalErrors && (Res.IRBroken || Res.DebugInfoBroken))
616506f32e7eSjoerg     report_fatal_error("Broken module found, compilation aborted!");
616606f32e7eSjoerg 
616706f32e7eSjoerg   return PreservedAnalyses::all();
616806f32e7eSjoerg }
616906f32e7eSjoerg 
run(Function & F,FunctionAnalysisManager & AM)617006f32e7eSjoerg PreservedAnalyses VerifierPass::run(Function &F, FunctionAnalysisManager &AM) {
617106f32e7eSjoerg   auto res = AM.getResult<VerifierAnalysis>(F);
617206f32e7eSjoerg   if (res.IRBroken && FatalErrors)
617306f32e7eSjoerg     report_fatal_error("Broken function found, compilation aborted!");
617406f32e7eSjoerg 
617506f32e7eSjoerg   return PreservedAnalyses::all();
617606f32e7eSjoerg }
6177