10b57cec5SDimitry Andric //===-- Verifier.cpp - Implement the Module Verifier -----------------------==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines the function verifier interface, that can be used for some
104824e7fdSDimitry Andric // basic correctness checking of input to the system.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric // Note that this does not provide full `Java style' security and verifications,
130b57cec5SDimitry Andric // instead it just tries to ensure that code is well-formed.
140b57cec5SDimitry Andric //
150b57cec5SDimitry Andric // * Both of a binary operator's parameters are of the same type
160b57cec5SDimitry Andric // * Verify that the indices of mem access instructions match other operands
170b57cec5SDimitry Andric // * Verify that arithmetic and other things are only performed on first-class
180b57cec5SDimitry Andric // types. Verify that shifts & logicals only happen on integrals f.e.
190b57cec5SDimitry Andric // * All of the constants in a switch statement are of the correct type
200b57cec5SDimitry Andric // * The code is in valid SSA form
210b57cec5SDimitry Andric // * It should be illegal to put a label into any other type (like a structure)
220b57cec5SDimitry Andric // or to return one. [except constant arrays!]
230b57cec5SDimitry Andric // * Only phi nodes can be self referential: 'add i32 %0, %0 ; <int>:0' is bad
240b57cec5SDimitry Andric // * PHI nodes must have an entry for each predecessor, with no extras.
250b57cec5SDimitry Andric // * PHI nodes must be the first thing in a basic block, all grouped together
260b57cec5SDimitry Andric // * All basic blocks should only end with terminator insts, not contain them
270b57cec5SDimitry Andric // * The entry node to a function must not have predecessors
280b57cec5SDimitry Andric // * All Instructions must be embedded into a basic block
290b57cec5SDimitry Andric // * Functions cannot take a void-typed parameter
300b57cec5SDimitry Andric // * Verify that a function's argument list agrees with it's declared type.
310b57cec5SDimitry Andric // * It is illegal to specify a name for a void value.
320b57cec5SDimitry Andric // * It is illegal to have a internal global value with no initializer
330b57cec5SDimitry Andric // * It is illegal to have a ret instruction that returns a value that does not
340b57cec5SDimitry Andric // agree with the function return value type.
350b57cec5SDimitry Andric // * Function call argument types match the function prototype
360b57cec5SDimitry Andric // * A landing pad is defined by a landingpad instruction, and can be jumped to
370b57cec5SDimitry Andric // only by the unwind edge of an invoke instruction.
380b57cec5SDimitry Andric // * A landingpad instruction must be the first non-PHI instruction in the
390b57cec5SDimitry Andric // block.
400b57cec5SDimitry Andric // * Landingpad instructions must be in a function with a personality function.
4106c3fb27SDimitry Andric // * Convergence control intrinsics are introduced in ConvergentOperations.rst.
4206c3fb27SDimitry Andric // The applied restrictions are too numerous to list here.
4306c3fb27SDimitry Andric // * The convergence entry intrinsic and the loop heart must be the first
4406c3fb27SDimitry Andric // non-PHI instruction in their respective block. This does not conflict with
4506c3fb27SDimitry Andric // the landing pads, since these two kinds cannot occur in the same block.
460b57cec5SDimitry Andric // * All other things that are tested by asserts spread about the code...
470b57cec5SDimitry Andric //
480b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
490b57cec5SDimitry Andric
500b57cec5SDimitry Andric #include "llvm/IR/Verifier.h"
510b57cec5SDimitry Andric #include "llvm/ADT/APFloat.h"
520b57cec5SDimitry Andric #include "llvm/ADT/APInt.h"
530b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
540b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
550b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h"
5606c3fb27SDimitry Andric #include "llvm/ADT/PostOrderIterator.h"
570b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
580b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
590b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h"
600b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
610b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
620b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h"
630b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
640b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
650b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
660b57cec5SDimitry Andric #include "llvm/IR/Argument.h"
6706c3fb27SDimitry Andric #include "llvm/IR/AttributeMask.h"
680b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
690b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
700b57cec5SDimitry Andric #include "llvm/IR/CFG.h"
710b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h"
720b57cec5SDimitry Andric #include "llvm/IR/Comdat.h"
730b57cec5SDimitry Andric #include "llvm/IR/Constant.h"
740b57cec5SDimitry Andric #include "llvm/IR/ConstantRange.h"
750b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
765f757f3fSDimitry Andric #include "llvm/IR/ConvergenceVerifier.h"
770b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
78bdd1243dSDimitry Andric #include "llvm/IR/DebugInfo.h"
790b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
800b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
810b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
820b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
8306c3fb27SDimitry Andric #include "llvm/IR/EHPersonalities.h"
840b57cec5SDimitry Andric #include "llvm/IR/Function.h"
85bdd1243dSDimitry Andric #include "llvm/IR/GCStrategy.h"
860b57cec5SDimitry Andric #include "llvm/IR/GlobalAlias.h"
870b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
880b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h"
890b57cec5SDimitry Andric #include "llvm/IR/InlineAsm.h"
900b57cec5SDimitry Andric #include "llvm/IR/InstVisitor.h"
910b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h"
920b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
930b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
940b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
950b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h"
9681ad6265SDimitry Andric #include "llvm/IR/IntrinsicsAArch64.h"
9706c3fb27SDimitry Andric #include "llvm/IR/IntrinsicsAMDGPU.h"
9881ad6265SDimitry Andric #include "llvm/IR/IntrinsicsARM.h"
99297eecfbSDimitry Andric #include "llvm/IR/IntrinsicsNVPTX.h"
100480093f4SDimitry Andric #include "llvm/IR/IntrinsicsWebAssembly.h"
1010b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
1020b57cec5SDimitry Andric #include "llvm/IR/Metadata.h"
1030b57cec5SDimitry Andric #include "llvm/IR/Module.h"
1040b57cec5SDimitry Andric #include "llvm/IR/ModuleSlotTracker.h"
1050b57cec5SDimitry Andric #include "llvm/IR/PassManager.h"
1060b57cec5SDimitry Andric #include "llvm/IR/Statepoint.h"
1070b57cec5SDimitry Andric #include "llvm/IR/Type.h"
1080b57cec5SDimitry Andric #include "llvm/IR/Use.h"
1090b57cec5SDimitry Andric #include "llvm/IR/User.h"
1107a6dacacSDimitry Andric #include "llvm/IR/VFABIDemangler.h"
1110b57cec5SDimitry Andric #include "llvm/IR/Value.h"
112480093f4SDimitry Andric #include "llvm/InitializePasses.h"
1130b57cec5SDimitry Andric #include "llvm/Pass.h"
1140b57cec5SDimitry Andric #include "llvm/Support/AtomicOrdering.h"
1150b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
1160b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
1170b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
1180b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
1190b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
1200b57cec5SDimitry Andric #include <algorithm>
1210b57cec5SDimitry Andric #include <cassert>
1220b57cec5SDimitry Andric #include <cstdint>
1230b57cec5SDimitry Andric #include <memory>
124bdd1243dSDimitry Andric #include <optional>
1250b57cec5SDimitry Andric #include <string>
1260b57cec5SDimitry Andric #include <utility>
1270b57cec5SDimitry Andric
1280b57cec5SDimitry Andric using namespace llvm;
1290b57cec5SDimitry Andric
130e8d8bef9SDimitry Andric static cl::opt<bool> VerifyNoAliasScopeDomination(
131e8d8bef9SDimitry Andric "verify-noalias-scope-decl-dom", cl::Hidden, cl::init(false),
132e8d8bef9SDimitry Andric cl::desc("Ensure that llvm.experimental.noalias.scope.decl for identical "
133e8d8bef9SDimitry Andric "scopes are not dominating"));
134e8d8bef9SDimitry Andric
1350b57cec5SDimitry Andric namespace llvm {
1360b57cec5SDimitry Andric
1370b57cec5SDimitry Andric struct VerifierSupport {
1380b57cec5SDimitry Andric raw_ostream *OS;
1390b57cec5SDimitry Andric const Module &M;
1400b57cec5SDimitry Andric ModuleSlotTracker MST;
1418bcb0991SDimitry Andric Triple TT;
1420b57cec5SDimitry Andric const DataLayout &DL;
1430b57cec5SDimitry Andric LLVMContext &Context;
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric /// Track the brokenness of the module while recursively visiting.
1460b57cec5SDimitry Andric bool Broken = false;
1470b57cec5SDimitry Andric /// Broken debug info can be "recovered" from by stripping the debug info.
1480b57cec5SDimitry Andric bool BrokenDebugInfo = false;
1490b57cec5SDimitry Andric /// Whether to treat broken debug info as an error.
1500b57cec5SDimitry Andric bool TreatBrokenDebugInfoAsError = true;
1510b57cec5SDimitry Andric
VerifierSupportllvm::VerifierSupport1520b57cec5SDimitry Andric explicit VerifierSupport(raw_ostream *OS, const Module &M)
1538bcb0991SDimitry Andric : OS(OS), M(M), MST(&M), TT(M.getTargetTriple()), DL(M.getDataLayout()),
1548bcb0991SDimitry Andric Context(M.getContext()) {}
1550b57cec5SDimitry Andric
1560b57cec5SDimitry Andric private:
Writellvm::VerifierSupport1570b57cec5SDimitry Andric void Write(const Module *M) {
1580b57cec5SDimitry Andric *OS << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1590b57cec5SDimitry Andric }
1600b57cec5SDimitry Andric
Writellvm::VerifierSupport1610b57cec5SDimitry Andric void Write(const Value *V) {
1620b57cec5SDimitry Andric if (V)
1630b57cec5SDimitry Andric Write(*V);
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric
Writellvm::VerifierSupport1660b57cec5SDimitry Andric void Write(const Value &V) {
1670b57cec5SDimitry Andric if (isa<Instruction>(V)) {
1680b57cec5SDimitry Andric V.print(*OS, MST);
1690b57cec5SDimitry Andric *OS << '\n';
1700b57cec5SDimitry Andric } else {
1710b57cec5SDimitry Andric V.printAsOperand(*OS, true, MST);
1720b57cec5SDimitry Andric *OS << '\n';
1730b57cec5SDimitry Andric }
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric
Writellvm::VerifierSupport1767a6dacacSDimitry Andric void Write(const DPValue *V) {
1777a6dacacSDimitry Andric if (V)
1787a6dacacSDimitry Andric V->print(*OS, MST, false);
1797a6dacacSDimitry Andric }
1807a6dacacSDimitry Andric
Writellvm::VerifierSupport1810b57cec5SDimitry Andric void Write(const Metadata *MD) {
1820b57cec5SDimitry Andric if (!MD)
1830b57cec5SDimitry Andric return;
1840b57cec5SDimitry Andric MD->print(*OS, MST, &M);
1850b57cec5SDimitry Andric *OS << '\n';
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric
Writellvm::VerifierSupport1880b57cec5SDimitry Andric template <class T> void Write(const MDTupleTypedArrayWrapper<T> &MD) {
1890b57cec5SDimitry Andric Write(MD.get());
1900b57cec5SDimitry Andric }
1910b57cec5SDimitry Andric
Writellvm::VerifierSupport1920b57cec5SDimitry Andric void Write(const NamedMDNode *NMD) {
1930b57cec5SDimitry Andric if (!NMD)
1940b57cec5SDimitry Andric return;
1950b57cec5SDimitry Andric NMD->print(*OS, MST);
1960b57cec5SDimitry Andric *OS << '\n';
1970b57cec5SDimitry Andric }
1980b57cec5SDimitry Andric
Writellvm::VerifierSupport1990b57cec5SDimitry Andric void Write(Type *T) {
2000b57cec5SDimitry Andric if (!T)
2010b57cec5SDimitry Andric return;
2020b57cec5SDimitry Andric *OS << ' ' << *T;
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric
Writellvm::VerifierSupport2050b57cec5SDimitry Andric void Write(const Comdat *C) {
2060b57cec5SDimitry Andric if (!C)
2070b57cec5SDimitry Andric return;
2080b57cec5SDimitry Andric *OS << *C;
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric
Writellvm::VerifierSupport2110b57cec5SDimitry Andric void Write(const APInt *AI) {
2120b57cec5SDimitry Andric if (!AI)
2130b57cec5SDimitry Andric return;
2140b57cec5SDimitry Andric *OS << *AI << '\n';
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric
Writellvm::VerifierSupport2170b57cec5SDimitry Andric void Write(const unsigned i) { *OS << i << '\n'; }
2180b57cec5SDimitry Andric
219fe6060f1SDimitry Andric // NOLINTNEXTLINE(readability-identifier-naming)
Writellvm::VerifierSupport220fe6060f1SDimitry Andric void Write(const Attribute *A) {
221fe6060f1SDimitry Andric if (!A)
222fe6060f1SDimitry Andric return;
223fe6060f1SDimitry Andric *OS << A->getAsString() << '\n';
224fe6060f1SDimitry Andric }
225fe6060f1SDimitry Andric
226fe6060f1SDimitry Andric // NOLINTNEXTLINE(readability-identifier-naming)
Writellvm::VerifierSupport227fe6060f1SDimitry Andric void Write(const AttributeSet *AS) {
228fe6060f1SDimitry Andric if (!AS)
229fe6060f1SDimitry Andric return;
230fe6060f1SDimitry Andric *OS << AS->getAsString() << '\n';
231fe6060f1SDimitry Andric }
232fe6060f1SDimitry Andric
233fe6060f1SDimitry Andric // NOLINTNEXTLINE(readability-identifier-naming)
Writellvm::VerifierSupport234fe6060f1SDimitry Andric void Write(const AttributeList *AL) {
235fe6060f1SDimitry Andric if (!AL)
236fe6060f1SDimitry Andric return;
237fe6060f1SDimitry Andric AL->print(*OS);
238fe6060f1SDimitry Andric }
239fe6060f1SDimitry Andric
Writellvm::VerifierSupport24006c3fb27SDimitry Andric void Write(Printable P) { *OS << P << '\n'; }
24106c3fb27SDimitry Andric
Writellvm::VerifierSupport2420b57cec5SDimitry Andric template <typename T> void Write(ArrayRef<T> Vs) {
2430b57cec5SDimitry Andric for (const T &V : Vs)
2440b57cec5SDimitry Andric Write(V);
2450b57cec5SDimitry Andric }
2460b57cec5SDimitry Andric
2470b57cec5SDimitry Andric template <typename T1, typename... Ts>
WriteTsllvm::VerifierSupport2480b57cec5SDimitry Andric void WriteTs(const T1 &V1, const Ts &... Vs) {
2490b57cec5SDimitry Andric Write(V1);
2500b57cec5SDimitry Andric WriteTs(Vs...);
2510b57cec5SDimitry Andric }
2520b57cec5SDimitry Andric
WriteTsllvm::VerifierSupport2530b57cec5SDimitry Andric template <typename... Ts> void WriteTs() {}
2540b57cec5SDimitry Andric
2550b57cec5SDimitry Andric public:
2560b57cec5SDimitry Andric /// A check failed, so printout out the condition and the message.
2570b57cec5SDimitry Andric ///
2580b57cec5SDimitry Andric /// This provides a nice place to put a breakpoint if you want to see why
2590b57cec5SDimitry Andric /// something is not correct.
CheckFailedllvm::VerifierSupport2600b57cec5SDimitry Andric void CheckFailed(const Twine &Message) {
2610b57cec5SDimitry Andric if (OS)
2620b57cec5SDimitry Andric *OS << Message << '\n';
2630b57cec5SDimitry Andric Broken = true;
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric
2660b57cec5SDimitry Andric /// A check failed (with values to print).
2670b57cec5SDimitry Andric ///
2680b57cec5SDimitry Andric /// This calls the Message-only version so that the above is easier to set a
2690b57cec5SDimitry Andric /// breakpoint on.
2700b57cec5SDimitry Andric template <typename T1, typename... Ts>
CheckFailedllvm::VerifierSupport2710b57cec5SDimitry Andric void CheckFailed(const Twine &Message, const T1 &V1, const Ts &... Vs) {
2720b57cec5SDimitry Andric CheckFailed(Message);
2730b57cec5SDimitry Andric if (OS)
2740b57cec5SDimitry Andric WriteTs(V1, Vs...);
2750b57cec5SDimitry Andric }
2760b57cec5SDimitry Andric
2770b57cec5SDimitry Andric /// A debug info check failed.
DebugInfoCheckFailedllvm::VerifierSupport2780b57cec5SDimitry Andric void DebugInfoCheckFailed(const Twine &Message) {
2790b57cec5SDimitry Andric if (OS)
2800b57cec5SDimitry Andric *OS << Message << '\n';
2810b57cec5SDimitry Andric Broken |= TreatBrokenDebugInfoAsError;
2820b57cec5SDimitry Andric BrokenDebugInfo = true;
2830b57cec5SDimitry Andric }
2840b57cec5SDimitry Andric
2850b57cec5SDimitry Andric /// A debug info check failed (with values to print).
2860b57cec5SDimitry Andric template <typename T1, typename... Ts>
DebugInfoCheckFailedllvm::VerifierSupport2870b57cec5SDimitry Andric void DebugInfoCheckFailed(const Twine &Message, const T1 &V1,
2880b57cec5SDimitry Andric const Ts &... Vs) {
2890b57cec5SDimitry Andric DebugInfoCheckFailed(Message);
2900b57cec5SDimitry Andric if (OS)
2910b57cec5SDimitry Andric WriteTs(V1, Vs...);
2920b57cec5SDimitry Andric }
2930b57cec5SDimitry Andric };
2940b57cec5SDimitry Andric
2950b57cec5SDimitry Andric } // namespace llvm
2960b57cec5SDimitry Andric
2970b57cec5SDimitry Andric namespace {
2980b57cec5SDimitry Andric
2990b57cec5SDimitry Andric class Verifier : public InstVisitor<Verifier>, VerifierSupport {
3000b57cec5SDimitry Andric friend class InstVisitor<Verifier>;
3010b57cec5SDimitry Andric
30281ad6265SDimitry Andric // ISD::ArgFlagsTy::MemAlign only have 4 bits for alignment, so
30381ad6265SDimitry Andric // the alignment size should not exceed 2^15. Since encode(Align)
30481ad6265SDimitry Andric // would plus the shift value by 1, the alignment size should
30581ad6265SDimitry Andric // not exceed 2^14, otherwise it can NOT be properly lowered
30681ad6265SDimitry Andric // in backend.
30781ad6265SDimitry Andric static constexpr unsigned ParamMaxAlignment = 1 << 14;
3080b57cec5SDimitry Andric DominatorTree DT;
3090b57cec5SDimitry Andric
3100b57cec5SDimitry Andric /// When verifying a basic block, keep track of all of the
3110b57cec5SDimitry Andric /// instructions we have seen so far.
3120b57cec5SDimitry Andric ///
3130b57cec5SDimitry Andric /// This allows us to do efficient dominance checks for the case when an
3140b57cec5SDimitry Andric /// instruction has an operand that is an instruction in the same block.
3150b57cec5SDimitry Andric SmallPtrSet<Instruction *, 16> InstsInThisBlock;
3160b57cec5SDimitry Andric
3170b57cec5SDimitry Andric /// Keep track of the metadata nodes that have been checked already.
3180b57cec5SDimitry Andric SmallPtrSet<const Metadata *, 32> MDNodes;
3190b57cec5SDimitry Andric
3200b57cec5SDimitry Andric /// Keep track which DISubprogram is attached to which function.
3210b57cec5SDimitry Andric DenseMap<const DISubprogram *, const Function *> DISubprogramAttachments;
3220b57cec5SDimitry Andric
3230b57cec5SDimitry Andric /// Track all DICompileUnits visited.
3240b57cec5SDimitry Andric SmallPtrSet<const Metadata *, 2> CUVisited;
3250b57cec5SDimitry Andric
3260b57cec5SDimitry Andric /// The result type for a landingpad.
3270b57cec5SDimitry Andric Type *LandingPadResultTy;
3280b57cec5SDimitry Andric
3290b57cec5SDimitry Andric /// Whether we've seen a call to @llvm.localescape in this function
3300b57cec5SDimitry Andric /// already.
3310b57cec5SDimitry Andric bool SawFrameEscape;
3320b57cec5SDimitry Andric
3330b57cec5SDimitry Andric /// Whether the current function has a DISubprogram attached to it.
3340b57cec5SDimitry Andric bool HasDebugInfo = false;
3350b57cec5SDimitry Andric
336e8d8bef9SDimitry Andric /// The current source language.
337e8d8bef9SDimitry Andric dwarf::SourceLanguage CurrentSourceLang = dwarf::DW_LANG_lo_user;
338e8d8bef9SDimitry Andric
3390b57cec5SDimitry Andric /// Stores the count of how many objects were passed to llvm.localescape for a
3400b57cec5SDimitry Andric /// given function and the largest index passed to llvm.localrecover.
3410b57cec5SDimitry Andric DenseMap<Function *, std::pair<unsigned, unsigned>> FrameEscapeInfo;
3420b57cec5SDimitry Andric
3430b57cec5SDimitry Andric // Maps catchswitches and cleanuppads that unwind to siblings to the
3440b57cec5SDimitry Andric // terminators that indicate the unwind, used to detect cycles therein.
3450b57cec5SDimitry Andric MapVector<Instruction *, Instruction *> SiblingFuncletInfo;
3460b57cec5SDimitry Andric
34706c3fb27SDimitry Andric /// Cache which blocks are in which funclet, if an EH funclet personality is
34806c3fb27SDimitry Andric /// in use. Otherwise empty.
34906c3fb27SDimitry Andric DenseMap<BasicBlock *, ColorVector> BlockEHFuncletColors;
35006c3fb27SDimitry Andric
3510b57cec5SDimitry Andric /// Cache of constants visited in search of ConstantExprs.
3520b57cec5SDimitry Andric SmallPtrSet<const Constant *, 32> ConstantExprVisited;
3530b57cec5SDimitry Andric
3540b57cec5SDimitry Andric /// Cache of declarations of the llvm.experimental.deoptimize.<ty> intrinsic.
3550b57cec5SDimitry Andric SmallVector<const Function *, 4> DeoptimizeDeclarations;
3560b57cec5SDimitry Andric
357fe6060f1SDimitry Andric /// Cache of attribute lists verified.
358fe6060f1SDimitry Andric SmallPtrSet<const void *, 32> AttributeListsVisited;
359fe6060f1SDimitry Andric
3600b57cec5SDimitry Andric // Verify that this GlobalValue is only used in this module.
3610b57cec5SDimitry Andric // This map is used to avoid visiting uses twice. We can arrive at a user
3620b57cec5SDimitry Andric // twice, if they have multiple operands. In particular for very large
3630b57cec5SDimitry Andric // constant expressions, we can arrive at a particular user many times.
3640b57cec5SDimitry Andric SmallPtrSet<const Value *, 32> GlobalValueVisited;
3650b57cec5SDimitry Andric
3660b57cec5SDimitry Andric // Keeps track of duplicate function argument debug info.
3670b57cec5SDimitry Andric SmallVector<const DILocalVariable *, 16> DebugFnArgs;
3680b57cec5SDimitry Andric
3690b57cec5SDimitry Andric TBAAVerifier TBAAVerifyHelper;
3705f757f3fSDimitry Andric ConvergenceVerifier ConvergenceVerifyHelper;
3710b57cec5SDimitry Andric
372e8d8bef9SDimitry Andric SmallVector<IntrinsicInst *, 4> NoAliasScopeDecls;
373e8d8bef9SDimitry Andric
3740b57cec5SDimitry Andric void checkAtomicMemAccessSize(Type *Ty, const Instruction *I);
3750b57cec5SDimitry Andric
3760b57cec5SDimitry Andric public:
Verifier(raw_ostream * OS,bool ShouldTreatBrokenDebugInfoAsError,const Module & M)3770b57cec5SDimitry Andric explicit Verifier(raw_ostream *OS, bool ShouldTreatBrokenDebugInfoAsError,
3780b57cec5SDimitry Andric const Module &M)
3790b57cec5SDimitry Andric : VerifierSupport(OS, M), LandingPadResultTy(nullptr),
3800b57cec5SDimitry Andric SawFrameEscape(false), TBAAVerifyHelper(this) {
3810b57cec5SDimitry Andric TreatBrokenDebugInfoAsError = ShouldTreatBrokenDebugInfoAsError;
3820b57cec5SDimitry Andric }
3830b57cec5SDimitry Andric
hasBrokenDebugInfo() const3840b57cec5SDimitry Andric bool hasBrokenDebugInfo() const { return BrokenDebugInfo; }
3850b57cec5SDimitry Andric
verify(const Function & F)3860b57cec5SDimitry Andric bool verify(const Function &F) {
3870b57cec5SDimitry Andric assert(F.getParent() == &M &&
3880b57cec5SDimitry Andric "An instance of this class only works with a specific module!");
3890b57cec5SDimitry Andric
3900b57cec5SDimitry Andric // First ensure the function is well-enough formed to compute dominance
3910b57cec5SDimitry Andric // information, and directly compute a dominance tree. We don't rely on the
3920b57cec5SDimitry Andric // pass manager to provide this as it isolates us from a potentially
3930b57cec5SDimitry Andric // out-of-date dominator tree and makes it significantly more complex to run
3940b57cec5SDimitry Andric // this code outside of a pass manager.
3950b57cec5SDimitry Andric // FIXME: It's really gross that we have to cast away constness here.
3960b57cec5SDimitry Andric if (!F.empty())
3970b57cec5SDimitry Andric DT.recalculate(const_cast<Function &>(F));
3980b57cec5SDimitry Andric
3990b57cec5SDimitry Andric for (const BasicBlock &BB : F) {
4000b57cec5SDimitry Andric if (!BB.empty() && BB.back().isTerminator())
4010b57cec5SDimitry Andric continue;
4020b57cec5SDimitry Andric
4030b57cec5SDimitry Andric if (OS) {
4040b57cec5SDimitry Andric *OS << "Basic Block in function '" << F.getName()
4050b57cec5SDimitry Andric << "' does not have terminator!\n";
4060b57cec5SDimitry Andric BB.printAsOperand(*OS, true, MST);
4070b57cec5SDimitry Andric *OS << "\n";
4080b57cec5SDimitry Andric }
4090b57cec5SDimitry Andric return false;
4100b57cec5SDimitry Andric }
4110b57cec5SDimitry Andric
4125f757f3fSDimitry Andric auto FailureCB = [this](const Twine &Message) {
4135f757f3fSDimitry Andric this->CheckFailed(Message);
4145f757f3fSDimitry Andric };
4155f757f3fSDimitry Andric ConvergenceVerifyHelper.initialize(OS, FailureCB, F);
4165f757f3fSDimitry Andric
4170b57cec5SDimitry Andric Broken = false;
4180b57cec5SDimitry Andric // FIXME: We strip const here because the inst visitor strips const.
4190b57cec5SDimitry Andric visit(const_cast<Function &>(F));
4200b57cec5SDimitry Andric verifySiblingFuncletUnwinds();
4215f757f3fSDimitry Andric
4225f757f3fSDimitry Andric if (ConvergenceVerifyHelper.sawTokens())
4235f757f3fSDimitry Andric ConvergenceVerifyHelper.verify(DT);
4245f757f3fSDimitry Andric
4250b57cec5SDimitry Andric InstsInThisBlock.clear();
4260b57cec5SDimitry Andric DebugFnArgs.clear();
4270b57cec5SDimitry Andric LandingPadResultTy = nullptr;
4280b57cec5SDimitry Andric SawFrameEscape = false;
4290b57cec5SDimitry Andric SiblingFuncletInfo.clear();
430e8d8bef9SDimitry Andric verifyNoAliasScopeDecl();
431e8d8bef9SDimitry Andric NoAliasScopeDecls.clear();
4320b57cec5SDimitry Andric
4330b57cec5SDimitry Andric return !Broken;
4340b57cec5SDimitry Andric }
4350b57cec5SDimitry Andric
4360b57cec5SDimitry Andric /// Verify the module that this instance of \c Verifier was initialized with.
verify()4370b57cec5SDimitry Andric bool verify() {
4380b57cec5SDimitry Andric Broken = false;
4390b57cec5SDimitry Andric
4400b57cec5SDimitry Andric // Collect all declarations of the llvm.experimental.deoptimize intrinsic.
4410b57cec5SDimitry Andric for (const Function &F : M)
4420b57cec5SDimitry Andric if (F.getIntrinsicID() == Intrinsic::experimental_deoptimize)
4430b57cec5SDimitry Andric DeoptimizeDeclarations.push_back(&F);
4440b57cec5SDimitry Andric
4450b57cec5SDimitry Andric // Now that we've visited every function, verify that we never asked to
4460b57cec5SDimitry Andric // recover a frame index that wasn't escaped.
4470b57cec5SDimitry Andric verifyFrameRecoverIndices();
4480b57cec5SDimitry Andric for (const GlobalVariable &GV : M.globals())
4490b57cec5SDimitry Andric visitGlobalVariable(GV);
4500b57cec5SDimitry Andric
4510b57cec5SDimitry Andric for (const GlobalAlias &GA : M.aliases())
4520b57cec5SDimitry Andric visitGlobalAlias(GA);
4530b57cec5SDimitry Andric
454349cc55cSDimitry Andric for (const GlobalIFunc &GI : M.ifuncs())
455349cc55cSDimitry Andric visitGlobalIFunc(GI);
456349cc55cSDimitry Andric
4570b57cec5SDimitry Andric for (const NamedMDNode &NMD : M.named_metadata())
4580b57cec5SDimitry Andric visitNamedMDNode(NMD);
4590b57cec5SDimitry Andric
4600b57cec5SDimitry Andric for (const StringMapEntry<Comdat> &SMEC : M.getComdatSymbolTable())
4610b57cec5SDimitry Andric visitComdat(SMEC.getValue());
4620b57cec5SDimitry Andric
463349cc55cSDimitry Andric visitModuleFlags();
464349cc55cSDimitry Andric visitModuleIdents();
465349cc55cSDimitry Andric visitModuleCommandLines();
4660b57cec5SDimitry Andric
4670b57cec5SDimitry Andric verifyCompileUnits();
4680b57cec5SDimitry Andric
4690b57cec5SDimitry Andric verifyDeoptimizeCallingConvs();
4700b57cec5SDimitry Andric DISubprogramAttachments.clear();
4710b57cec5SDimitry Andric return !Broken;
4720b57cec5SDimitry Andric }
4730b57cec5SDimitry Andric
4740b57cec5SDimitry Andric private:
4755ffd83dbSDimitry Andric /// Whether a metadata node is allowed to be, or contain, a DILocation.
4765ffd83dbSDimitry Andric enum class AreDebugLocsAllowed { No, Yes };
4775ffd83dbSDimitry Andric
4780b57cec5SDimitry Andric // Verification methods...
4790b57cec5SDimitry Andric void visitGlobalValue(const GlobalValue &GV);
4800b57cec5SDimitry Andric void visitGlobalVariable(const GlobalVariable &GV);
4810b57cec5SDimitry Andric void visitGlobalAlias(const GlobalAlias &GA);
482349cc55cSDimitry Andric void visitGlobalIFunc(const GlobalIFunc &GI);
4830b57cec5SDimitry Andric void visitAliaseeSubExpr(const GlobalAlias &A, const Constant &C);
4840b57cec5SDimitry Andric void visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias *> &Visited,
4850b57cec5SDimitry Andric const GlobalAlias &A, const Constant &C);
4860b57cec5SDimitry Andric void visitNamedMDNode(const NamedMDNode &NMD);
4875ffd83dbSDimitry Andric void visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs);
4880b57cec5SDimitry Andric void visitMetadataAsValue(const MetadataAsValue &MD, Function *F);
4890b57cec5SDimitry Andric void visitValueAsMetadata(const ValueAsMetadata &MD, Function *F);
4905f757f3fSDimitry Andric void visitDIArgList(const DIArgList &AL, Function *F);
4910b57cec5SDimitry Andric void visitComdat(const Comdat &C);
492349cc55cSDimitry Andric void visitModuleIdents();
493349cc55cSDimitry Andric void visitModuleCommandLines();
494349cc55cSDimitry Andric void visitModuleFlags();
4950b57cec5SDimitry Andric void visitModuleFlag(const MDNode *Op,
4960b57cec5SDimitry Andric DenseMap<const MDString *, const MDNode *> &SeenIDs,
4970b57cec5SDimitry Andric SmallVectorImpl<const MDNode *> &Requirements);
4980b57cec5SDimitry Andric void visitModuleFlagCGProfileEntry(const MDOperand &MDO);
4990b57cec5SDimitry Andric void visitFunction(const Function &F);
5000b57cec5SDimitry Andric void visitBasicBlock(BasicBlock &BB);
50106c3fb27SDimitry Andric void verifyRangeMetadata(const Value &V, const MDNode *Range, Type *Ty,
50206c3fb27SDimitry Andric bool IsAbsoluteSymbol);
5030b57cec5SDimitry Andric void visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty);
5040b57cec5SDimitry Andric void visitDereferenceableMetadata(Instruction &I, MDNode *MD);
5058bcb0991SDimitry Andric void visitProfMetadata(Instruction &I, MDNode *MD);
506fcaf7f86SDimitry Andric void visitCallStackMetadata(MDNode *MD);
507fcaf7f86SDimitry Andric void visitMemProfMetadata(Instruction &I, MDNode *MD);
508fcaf7f86SDimitry Andric void visitCallsiteMetadata(Instruction &I, MDNode *MD);
509bdd1243dSDimitry Andric void visitDIAssignIDMetadata(Instruction &I, MDNode *MD);
510e8d8bef9SDimitry Andric void visitAnnotationMetadata(MDNode *Annotation);
511349cc55cSDimitry Andric void visitAliasScopeMetadata(const MDNode *MD);
512349cc55cSDimitry Andric void visitAliasScopeListMetadata(const MDNode *MD);
51381ad6265SDimitry Andric void visitAccessGroupMetadata(const MDNode *MD);
5140b57cec5SDimitry Andric
5150b57cec5SDimitry Andric template <class Ty> bool isValidMetadataArray(const MDTuple &N);
5160b57cec5SDimitry Andric #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N);
5170b57cec5SDimitry Andric #include "llvm/IR/Metadata.def"
5180b57cec5SDimitry Andric void visitDIScope(const DIScope &N);
5190b57cec5SDimitry Andric void visitDIVariable(const DIVariable &N);
5200b57cec5SDimitry Andric void visitDILexicalBlockBase(const DILexicalBlockBase &N);
5210b57cec5SDimitry Andric void visitDITemplateParameter(const DITemplateParameter &N);
5220b57cec5SDimitry Andric
5230b57cec5SDimitry Andric void visitTemplateParams(const MDNode &N, const Metadata &RawParams);
5240b57cec5SDimitry Andric
5250b57cec5SDimitry Andric // InstVisitor overrides...
5260b57cec5SDimitry Andric using InstVisitor<Verifier>::visit;
5270b57cec5SDimitry Andric void visit(Instruction &I);
5280b57cec5SDimitry Andric
5290b57cec5SDimitry Andric void visitTruncInst(TruncInst &I);
5300b57cec5SDimitry Andric void visitZExtInst(ZExtInst &I);
5310b57cec5SDimitry Andric void visitSExtInst(SExtInst &I);
5320b57cec5SDimitry Andric void visitFPTruncInst(FPTruncInst &I);
5330b57cec5SDimitry Andric void visitFPExtInst(FPExtInst &I);
5340b57cec5SDimitry Andric void visitFPToUIInst(FPToUIInst &I);
5350b57cec5SDimitry Andric void visitFPToSIInst(FPToSIInst &I);
5360b57cec5SDimitry Andric void visitUIToFPInst(UIToFPInst &I);
5370b57cec5SDimitry Andric void visitSIToFPInst(SIToFPInst &I);
5380b57cec5SDimitry Andric void visitIntToPtrInst(IntToPtrInst &I);
5390b57cec5SDimitry Andric void visitPtrToIntInst(PtrToIntInst &I);
5400b57cec5SDimitry Andric void visitBitCastInst(BitCastInst &I);
5410b57cec5SDimitry Andric void visitAddrSpaceCastInst(AddrSpaceCastInst &I);
5420b57cec5SDimitry Andric void visitPHINode(PHINode &PN);
5430b57cec5SDimitry Andric void visitCallBase(CallBase &Call);
5440b57cec5SDimitry Andric void visitUnaryOperator(UnaryOperator &U);
5450b57cec5SDimitry Andric void visitBinaryOperator(BinaryOperator &B);
5460b57cec5SDimitry Andric void visitICmpInst(ICmpInst &IC);
5470b57cec5SDimitry Andric void visitFCmpInst(FCmpInst &FC);
5480b57cec5SDimitry Andric void visitExtractElementInst(ExtractElementInst &EI);
5490b57cec5SDimitry Andric void visitInsertElementInst(InsertElementInst &EI);
5500b57cec5SDimitry Andric void visitShuffleVectorInst(ShuffleVectorInst &EI);
visitVAArgInst(VAArgInst & VAA)5510b57cec5SDimitry Andric void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
5520b57cec5SDimitry Andric void visitCallInst(CallInst &CI);
5530b57cec5SDimitry Andric void visitInvokeInst(InvokeInst &II);
5540b57cec5SDimitry Andric void visitGetElementPtrInst(GetElementPtrInst &GEP);
5550b57cec5SDimitry Andric void visitLoadInst(LoadInst &LI);
5560b57cec5SDimitry Andric void visitStoreInst(StoreInst &SI);
5570b57cec5SDimitry Andric void verifyDominatesUse(Instruction &I, unsigned i);
5580b57cec5SDimitry Andric void visitInstruction(Instruction &I);
5590b57cec5SDimitry Andric void visitTerminator(Instruction &I);
5600b57cec5SDimitry Andric void visitBranchInst(BranchInst &BI);
5610b57cec5SDimitry Andric void visitReturnInst(ReturnInst &RI);
5620b57cec5SDimitry Andric void visitSwitchInst(SwitchInst &SI);
5630b57cec5SDimitry Andric void visitIndirectBrInst(IndirectBrInst &BI);
5640b57cec5SDimitry Andric void visitCallBrInst(CallBrInst &CBI);
5650b57cec5SDimitry Andric void visitSelectInst(SelectInst &SI);
5660b57cec5SDimitry Andric void visitUserOp1(Instruction &I);
visitUserOp2(Instruction & I)5670b57cec5SDimitry Andric void visitUserOp2(Instruction &I) { visitUserOp1(I); }
5680b57cec5SDimitry Andric void visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call);
5690b57cec5SDimitry Andric void visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI);
57081ad6265SDimitry Andric void visitVPIntrinsic(VPIntrinsic &VPI);
5710b57cec5SDimitry Andric void visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII);
5720b57cec5SDimitry Andric void visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI);
5730b57cec5SDimitry Andric void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI);
5740b57cec5SDimitry Andric void visitAtomicRMWInst(AtomicRMWInst &RMWI);
5750b57cec5SDimitry Andric void visitFenceInst(FenceInst &FI);
5760b57cec5SDimitry Andric void visitAllocaInst(AllocaInst &AI);
5770b57cec5SDimitry Andric void visitExtractValueInst(ExtractValueInst &EVI);
5780b57cec5SDimitry Andric void visitInsertValueInst(InsertValueInst &IVI);
5790b57cec5SDimitry Andric void visitEHPadPredecessors(Instruction &I);
5800b57cec5SDimitry Andric void visitLandingPadInst(LandingPadInst &LPI);
5810b57cec5SDimitry Andric void visitResumeInst(ResumeInst &RI);
5820b57cec5SDimitry Andric void visitCatchPadInst(CatchPadInst &CPI);
5830b57cec5SDimitry Andric void visitCatchReturnInst(CatchReturnInst &CatchReturn);
5840b57cec5SDimitry Andric void visitCleanupPadInst(CleanupPadInst &CPI);
5850b57cec5SDimitry Andric void visitFuncletPadInst(FuncletPadInst &FPI);
5860b57cec5SDimitry Andric void visitCatchSwitchInst(CatchSwitchInst &CatchSwitch);
5870b57cec5SDimitry Andric void visitCleanupReturnInst(CleanupReturnInst &CRI);
5880b57cec5SDimitry Andric
5890b57cec5SDimitry Andric void verifySwiftErrorCall(CallBase &Call, const Value *SwiftErrorVal);
5900b57cec5SDimitry Andric void verifySwiftErrorValue(const Value *SwiftErrorVal);
5910eae32dcSDimitry Andric void verifyTailCCMustTailAttrs(const AttrBuilder &Attrs, StringRef Context);
5920b57cec5SDimitry Andric void verifyMustTailCall(CallInst &CI);
5930b57cec5SDimitry Andric bool verifyAttributeCount(AttributeList Attrs, unsigned Params);
594fe6060f1SDimitry Andric void verifyAttributeTypes(AttributeSet Attrs, const Value *V);
5950b57cec5SDimitry Andric void verifyParameterAttrs(AttributeSet Attrs, Type *Ty, const Value *V);
596fe6060f1SDimitry Andric void checkUnsignedBaseTenFuncAttr(AttributeList Attrs, StringRef Attr,
597fe6060f1SDimitry Andric const Value *V);
5980b57cec5SDimitry Andric void verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
59904eeddc0SDimitry Andric const Value *V, bool IsIntrinsic, bool IsInlineAsm);
6000b57cec5SDimitry Andric void verifyFunctionMetadata(ArrayRef<std::pair<unsigned, MDNode *>> MDs);
6010b57cec5SDimitry Andric
6020b57cec5SDimitry Andric void visitConstantExprsRecursively(const Constant *EntryC);
6030b57cec5SDimitry Andric void visitConstantExpr(const ConstantExpr *CE);
60404eeddc0SDimitry Andric void verifyInlineAsmCall(const CallBase &Call);
6050b57cec5SDimitry Andric void verifyStatepoint(const CallBase &Call);
6060b57cec5SDimitry Andric void verifyFrameRecoverIndices();
6070b57cec5SDimitry Andric void verifySiblingFuncletUnwinds();
6080b57cec5SDimitry Andric
6090b57cec5SDimitry Andric void verifyFragmentExpression(const DbgVariableIntrinsic &I);
6100b57cec5SDimitry Andric template <typename ValueOrMetadata>
6110b57cec5SDimitry Andric void verifyFragmentExpression(const DIVariable &V,
6120b57cec5SDimitry Andric DIExpression::FragmentInfo Fragment,
6130b57cec5SDimitry Andric ValueOrMetadata *Desc);
6140b57cec5SDimitry Andric void verifyFnArgs(const DbgVariableIntrinsic &I);
6158bcb0991SDimitry Andric void verifyNotEntryValue(const DbgVariableIntrinsic &I);
6160b57cec5SDimitry Andric
6170b57cec5SDimitry Andric /// Module-level debug info verification...
6180b57cec5SDimitry Andric void verifyCompileUnits();
6190b57cec5SDimitry Andric
6200b57cec5SDimitry Andric /// Module-level verification that all @llvm.experimental.deoptimize
6210b57cec5SDimitry Andric /// declarations share the same calling convention.
6220b57cec5SDimitry Andric void verifyDeoptimizeCallingConvs();
6230b57cec5SDimitry Andric
624349cc55cSDimitry Andric void verifyAttachedCallBundle(const CallBase &Call,
625349cc55cSDimitry Andric const OperandBundleUse &BU);
626349cc55cSDimitry Andric
627e8d8bef9SDimitry Andric /// Verify the llvm.experimental.noalias.scope.decl declarations
628e8d8bef9SDimitry Andric void verifyNoAliasScopeDecl();
6290b57cec5SDimitry Andric };
6300b57cec5SDimitry Andric
6310b57cec5SDimitry Andric } // end anonymous namespace
6320b57cec5SDimitry Andric
6330b57cec5SDimitry Andric /// We know that cond should be true, if not print an error message.
63481ad6265SDimitry Andric #define Check(C, ...) \
63581ad6265SDimitry Andric do { \
63681ad6265SDimitry Andric if (!(C)) { \
63781ad6265SDimitry Andric CheckFailed(__VA_ARGS__); \
63881ad6265SDimitry Andric return; \
63981ad6265SDimitry Andric } \
64081ad6265SDimitry Andric } while (false)
6410b57cec5SDimitry Andric
6420b57cec5SDimitry Andric /// We know that a debug info condition should be true, if not print
6430b57cec5SDimitry Andric /// an error message.
64481ad6265SDimitry Andric #define CheckDI(C, ...) \
64581ad6265SDimitry Andric do { \
64681ad6265SDimitry Andric if (!(C)) { \
64781ad6265SDimitry Andric DebugInfoCheckFailed(__VA_ARGS__); \
64881ad6265SDimitry Andric return; \
64981ad6265SDimitry Andric } \
65081ad6265SDimitry Andric } while (false)
6510b57cec5SDimitry Andric
visit(Instruction & I)6520b57cec5SDimitry Andric void Verifier::visit(Instruction &I) {
6530b57cec5SDimitry Andric for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
65481ad6265SDimitry Andric Check(I.getOperand(i) != nullptr, "Operand is null", &I);
6550b57cec5SDimitry Andric InstVisitor<Verifier>::visit(I);
6560b57cec5SDimitry Andric }
6570b57cec5SDimitry Andric
6580eae32dcSDimitry Andric // Helper to iterate over indirect users. By returning false, the callback can ask to stop traversing further.
forEachUser(const Value * User,SmallPtrSet<const Value *,32> & Visited,llvm::function_ref<bool (const Value *)> Callback)6590b57cec5SDimitry Andric static void forEachUser(const Value *User,
6600b57cec5SDimitry Andric SmallPtrSet<const Value *, 32> &Visited,
6610b57cec5SDimitry Andric llvm::function_ref<bool(const Value *)> Callback) {
6620b57cec5SDimitry Andric if (!Visited.insert(User).second)
6630b57cec5SDimitry Andric return;
6640eae32dcSDimitry Andric
6650eae32dcSDimitry Andric SmallVector<const Value *> WorkList;
6660eae32dcSDimitry Andric append_range(WorkList, User->materialized_users());
6670eae32dcSDimitry Andric while (!WorkList.empty()) {
6680eae32dcSDimitry Andric const Value *Cur = WorkList.pop_back_val();
6690eae32dcSDimitry Andric if (!Visited.insert(Cur).second)
6700eae32dcSDimitry Andric continue;
6710eae32dcSDimitry Andric if (Callback(Cur))
6720eae32dcSDimitry Andric append_range(WorkList, Cur->materialized_users());
6730eae32dcSDimitry Andric }
6740b57cec5SDimitry Andric }
6750b57cec5SDimitry Andric
visitGlobalValue(const GlobalValue & GV)6760b57cec5SDimitry Andric void Verifier::visitGlobalValue(const GlobalValue &GV) {
67781ad6265SDimitry Andric Check(!GV.isDeclaration() || GV.hasValidDeclarationLinkage(),
6780b57cec5SDimitry Andric "Global is external, but doesn't have external or weak linkage!", &GV);
6790b57cec5SDimitry Andric
6800eae32dcSDimitry Andric if (const GlobalObject *GO = dyn_cast<GlobalObject>(&GV)) {
6810eae32dcSDimitry Andric
6820eae32dcSDimitry Andric if (MaybeAlign A = GO->getAlign()) {
68381ad6265SDimitry Andric Check(A->value() <= Value::MaximumAlignment,
6845ffd83dbSDimitry Andric "huge alignment values are unsupported", GO);
6850eae32dcSDimitry Andric }
68606c3fb27SDimitry Andric
68706c3fb27SDimitry Andric if (const MDNode *Associated =
68806c3fb27SDimitry Andric GO->getMetadata(LLVMContext::MD_associated)) {
68906c3fb27SDimitry Andric Check(Associated->getNumOperands() == 1,
69006c3fb27SDimitry Andric "associated metadata must have one operand", &GV, Associated);
69106c3fb27SDimitry Andric const Metadata *Op = Associated->getOperand(0).get();
69206c3fb27SDimitry Andric Check(Op, "associated metadata must have a global value", GO, Associated);
69306c3fb27SDimitry Andric
69406c3fb27SDimitry Andric const auto *VM = dyn_cast_or_null<ValueAsMetadata>(Op);
69506c3fb27SDimitry Andric Check(VM, "associated metadata must be ValueAsMetadata", GO, Associated);
69606c3fb27SDimitry Andric if (VM) {
69706c3fb27SDimitry Andric Check(isa<PointerType>(VM->getValue()->getType()),
69806c3fb27SDimitry Andric "associated value must be pointer typed", GV, Associated);
69906c3fb27SDimitry Andric
70006c3fb27SDimitry Andric const Value *Stripped = VM->getValue()->stripPointerCastsAndAliases();
70106c3fb27SDimitry Andric Check(isa<GlobalObject>(Stripped) || isa<Constant>(Stripped),
70206c3fb27SDimitry Andric "associated metadata must point to a GlobalObject", GO, Stripped);
70306c3fb27SDimitry Andric Check(Stripped != GO,
70406c3fb27SDimitry Andric "global values should not associate to themselves", GO,
70506c3fb27SDimitry Andric Associated);
7060eae32dcSDimitry Andric }
70706c3fb27SDimitry Andric }
70806c3fb27SDimitry Andric
70906c3fb27SDimitry Andric // FIXME: Why is getMetadata on GlobalValue protected?
71006c3fb27SDimitry Andric if (const MDNode *AbsoluteSymbol =
71106c3fb27SDimitry Andric GO->getMetadata(LLVMContext::MD_absolute_symbol)) {
71206c3fb27SDimitry Andric verifyRangeMetadata(*GO, AbsoluteSymbol, DL.getIntPtrType(GO->getType()),
71306c3fb27SDimitry Andric true);
71406c3fb27SDimitry Andric }
71506c3fb27SDimitry Andric }
71606c3fb27SDimitry Andric
71781ad6265SDimitry Andric Check(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
7180b57cec5SDimitry Andric "Only global variables can have appending linkage!", &GV);
7190b57cec5SDimitry Andric
7200b57cec5SDimitry Andric if (GV.hasAppendingLinkage()) {
7210b57cec5SDimitry Andric const GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV);
72281ad6265SDimitry Andric Check(GVar && GVar->getValueType()->isArrayTy(),
7230b57cec5SDimitry Andric "Only global arrays can have appending linkage!", GVar);
7240b57cec5SDimitry Andric }
7250b57cec5SDimitry Andric
7260b57cec5SDimitry Andric if (GV.isDeclarationForLinker())
72781ad6265SDimitry Andric Check(!GV.hasComdat(), "Declaration may not be in a Comdat!", &GV);
7280b57cec5SDimitry Andric
729bdd1243dSDimitry Andric if (GV.hasDLLExportStorageClass()) {
730bdd1243dSDimitry Andric Check(!GV.hasHiddenVisibility(),
731bdd1243dSDimitry Andric "dllexport GlobalValue must have default or protected visibility",
732bdd1243dSDimitry Andric &GV);
733bdd1243dSDimitry Andric }
7340b57cec5SDimitry Andric if (GV.hasDLLImportStorageClass()) {
735bdd1243dSDimitry Andric Check(GV.hasDefaultVisibility(),
736bdd1243dSDimitry Andric "dllimport GlobalValue must have default visibility", &GV);
73781ad6265SDimitry Andric Check(!GV.isDSOLocal(), "GlobalValue with DLLImport Storage is dso_local!",
73881ad6265SDimitry Andric &GV);
7390b57cec5SDimitry Andric
74081ad6265SDimitry Andric Check((GV.isDeclaration() &&
741e8d8bef9SDimitry Andric (GV.hasExternalLinkage() || GV.hasExternalWeakLinkage())) ||
7420b57cec5SDimitry Andric GV.hasAvailableExternallyLinkage(),
7430b57cec5SDimitry Andric "Global is marked as dllimport, but not external", &GV);
7440b57cec5SDimitry Andric }
7450b57cec5SDimitry Andric
7465ffd83dbSDimitry Andric if (GV.isImplicitDSOLocal())
74781ad6265SDimitry Andric Check(GV.isDSOLocal(),
7485ffd83dbSDimitry Andric "GlobalValue with local linkage or non-default "
7495ffd83dbSDimitry Andric "visibility must be dso_local!",
7500b57cec5SDimitry Andric &GV);
7510b57cec5SDimitry Andric
7520b57cec5SDimitry Andric forEachUser(&GV, GlobalValueVisited, [&](const Value *V) -> bool {
7530b57cec5SDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(V)) {
7540b57cec5SDimitry Andric if (!I->getParent() || !I->getParent()->getParent())
7550b57cec5SDimitry Andric CheckFailed("Global is referenced by parentless instruction!", &GV, &M,
7560b57cec5SDimitry Andric I);
7570b57cec5SDimitry Andric else if (I->getParent()->getParent()->getParent() != &M)
7580b57cec5SDimitry Andric CheckFailed("Global is referenced in a different module!", &GV, &M, I,
7590b57cec5SDimitry Andric I->getParent()->getParent(),
7600b57cec5SDimitry Andric I->getParent()->getParent()->getParent());
7610b57cec5SDimitry Andric return false;
7620b57cec5SDimitry Andric } else if (const Function *F = dyn_cast<Function>(V)) {
7630b57cec5SDimitry Andric if (F->getParent() != &M)
7640b57cec5SDimitry Andric CheckFailed("Global is used by function in a different module", &GV, &M,
7650b57cec5SDimitry Andric F, F->getParent());
7660b57cec5SDimitry Andric return false;
7670b57cec5SDimitry Andric }
7680b57cec5SDimitry Andric return true;
7690b57cec5SDimitry Andric });
7700b57cec5SDimitry Andric }
7710b57cec5SDimitry Andric
visitGlobalVariable(const GlobalVariable & GV)7720b57cec5SDimitry Andric void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
7730b57cec5SDimitry Andric if (GV.hasInitializer()) {
77481ad6265SDimitry Andric Check(GV.getInitializer()->getType() == GV.getValueType(),
7750b57cec5SDimitry Andric "Global variable initializer type does not match global "
7760b57cec5SDimitry Andric "variable type!",
7770b57cec5SDimitry Andric &GV);
7780b57cec5SDimitry Andric // If the global has common linkage, it must have a zero initializer and
7790b57cec5SDimitry Andric // cannot be constant.
7800b57cec5SDimitry Andric if (GV.hasCommonLinkage()) {
78181ad6265SDimitry Andric Check(GV.getInitializer()->isNullValue(),
7820b57cec5SDimitry Andric "'common' global must have a zero initializer!", &GV);
78381ad6265SDimitry Andric Check(!GV.isConstant(), "'common' global may not be marked constant!",
7840b57cec5SDimitry Andric &GV);
78581ad6265SDimitry Andric Check(!GV.hasComdat(), "'common' global may not be in a Comdat!", &GV);
7860b57cec5SDimitry Andric }
7870b57cec5SDimitry Andric }
7880b57cec5SDimitry Andric
7890b57cec5SDimitry Andric if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
7900b57cec5SDimitry Andric GV.getName() == "llvm.global_dtors")) {
79181ad6265SDimitry Andric Check(!GV.hasInitializer() || GV.hasAppendingLinkage(),
7920b57cec5SDimitry Andric "invalid linkage for intrinsic global variable", &GV);
79306c3fb27SDimitry Andric Check(GV.materialized_use_empty(),
79406c3fb27SDimitry Andric "invalid uses of intrinsic global variable", &GV);
79506c3fb27SDimitry Andric
7960b57cec5SDimitry Andric // Don't worry about emitting an error for it not being an array,
7970b57cec5SDimitry Andric // visitGlobalValue will complain on appending non-array.
7980b57cec5SDimitry Andric if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getValueType())) {
7990b57cec5SDimitry Andric StructType *STy = dyn_cast<StructType>(ATy->getElementType());
8000b57cec5SDimitry Andric PointerType *FuncPtrTy =
8015f757f3fSDimitry Andric PointerType::get(Context, DL.getProgramAddressSpace());
80281ad6265SDimitry Andric Check(STy && (STy->getNumElements() == 2 || STy->getNumElements() == 3) &&
8030b57cec5SDimitry Andric STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
8040b57cec5SDimitry Andric STy->getTypeAtIndex(1) == FuncPtrTy,
8050b57cec5SDimitry Andric "wrong type for intrinsic global variable", &GV);
80681ad6265SDimitry Andric Check(STy->getNumElements() == 3,
8070b57cec5SDimitry Andric "the third field of the element type is mandatory, "
808bdd1243dSDimitry Andric "specify ptr null to migrate from the obsoleted 2-field form");
8090b57cec5SDimitry Andric Type *ETy = STy->getTypeAtIndex(2);
81006c3fb27SDimitry Andric Check(ETy->isPointerTy(), "wrong type for intrinsic global variable",
81106c3fb27SDimitry Andric &GV);
8120b57cec5SDimitry Andric }
8130b57cec5SDimitry Andric }
8140b57cec5SDimitry Andric
8150b57cec5SDimitry Andric if (GV.hasName() && (GV.getName() == "llvm.used" ||
8160b57cec5SDimitry Andric GV.getName() == "llvm.compiler.used")) {
81781ad6265SDimitry Andric Check(!GV.hasInitializer() || GV.hasAppendingLinkage(),
8180b57cec5SDimitry Andric "invalid linkage for intrinsic global variable", &GV);
81906c3fb27SDimitry Andric Check(GV.materialized_use_empty(),
82006c3fb27SDimitry Andric "invalid uses of intrinsic global variable", &GV);
82106c3fb27SDimitry Andric
8220b57cec5SDimitry Andric Type *GVType = GV.getValueType();
8230b57cec5SDimitry Andric if (ArrayType *ATy = dyn_cast<ArrayType>(GVType)) {
8240b57cec5SDimitry Andric PointerType *PTy = dyn_cast<PointerType>(ATy->getElementType());
82581ad6265SDimitry Andric Check(PTy, "wrong type for intrinsic global variable", &GV);
8260b57cec5SDimitry Andric if (GV.hasInitializer()) {
8270b57cec5SDimitry Andric const Constant *Init = GV.getInitializer();
8280b57cec5SDimitry Andric const ConstantArray *InitArray = dyn_cast<ConstantArray>(Init);
82981ad6265SDimitry Andric Check(InitArray, "wrong initalizer for intrinsic global variable",
8300b57cec5SDimitry Andric Init);
8310b57cec5SDimitry Andric for (Value *Op : InitArray->operands()) {
8328bcb0991SDimitry Andric Value *V = Op->stripPointerCasts();
83381ad6265SDimitry Andric Check(isa<GlobalVariable>(V) || isa<Function>(V) ||
8340b57cec5SDimitry Andric isa<GlobalAlias>(V),
8350eae32dcSDimitry Andric Twine("invalid ") + GV.getName() + " member", V);
83681ad6265SDimitry Andric Check(V->hasName(),
8370eae32dcSDimitry Andric Twine("members of ") + GV.getName() + " must be named", V);
8380b57cec5SDimitry Andric }
8390b57cec5SDimitry Andric }
8400b57cec5SDimitry Andric }
8410b57cec5SDimitry Andric }
8420b57cec5SDimitry Andric
8430b57cec5SDimitry Andric // Visit any debug info attachments.
8440b57cec5SDimitry Andric SmallVector<MDNode *, 1> MDs;
8450b57cec5SDimitry Andric GV.getMetadata(LLVMContext::MD_dbg, MDs);
8460b57cec5SDimitry Andric for (auto *MD : MDs) {
8470b57cec5SDimitry Andric if (auto *GVE = dyn_cast<DIGlobalVariableExpression>(MD))
8480b57cec5SDimitry Andric visitDIGlobalVariableExpression(*GVE);
8490b57cec5SDimitry Andric else
85081ad6265SDimitry Andric CheckDI(false, "!dbg attachment of global variable must be a "
8510b57cec5SDimitry Andric "DIGlobalVariableExpression");
8520b57cec5SDimitry Andric }
8530b57cec5SDimitry Andric
8540b57cec5SDimitry Andric // Scalable vectors cannot be global variables, since we don't know
8555f757f3fSDimitry Andric // the runtime size.
8565f757f3fSDimitry Andric Check(!GV.getValueType()->isScalableTy(),
8575f757f3fSDimitry Andric "Globals cannot contain scalable types", &GV);
858e8d8bef9SDimitry Andric
859bdd1243dSDimitry Andric // Check if it's a target extension type that disallows being used as a
860bdd1243dSDimitry Andric // global.
861bdd1243dSDimitry Andric if (auto *TTy = dyn_cast<TargetExtType>(GV.getValueType()))
862bdd1243dSDimitry Andric Check(TTy->hasProperty(TargetExtType::CanBeGlobal),
863bdd1243dSDimitry Andric "Global @" + GV.getName() + " has illegal target extension type",
864bdd1243dSDimitry Andric TTy);
865bdd1243dSDimitry Andric
8660b57cec5SDimitry Andric if (!GV.hasInitializer()) {
8670b57cec5SDimitry Andric visitGlobalValue(GV);
8680b57cec5SDimitry Andric return;
8690b57cec5SDimitry Andric }
8700b57cec5SDimitry Andric
8710b57cec5SDimitry Andric // Walk any aggregate initializers looking for bitcasts between address spaces
8720b57cec5SDimitry Andric visitConstantExprsRecursively(GV.getInitializer());
8730b57cec5SDimitry Andric
8740b57cec5SDimitry Andric visitGlobalValue(GV);
8750b57cec5SDimitry Andric }
8760b57cec5SDimitry Andric
visitAliaseeSubExpr(const GlobalAlias & GA,const Constant & C)8770b57cec5SDimitry Andric void Verifier::visitAliaseeSubExpr(const GlobalAlias &GA, const Constant &C) {
8780b57cec5SDimitry Andric SmallPtrSet<const GlobalAlias*, 4> Visited;
8790b57cec5SDimitry Andric Visited.insert(&GA);
8800b57cec5SDimitry Andric visitAliaseeSubExpr(Visited, GA, C);
8810b57cec5SDimitry Andric }
8820b57cec5SDimitry Andric
visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias * > & Visited,const GlobalAlias & GA,const Constant & C)8830b57cec5SDimitry Andric void Verifier::visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias*> &Visited,
8840b57cec5SDimitry Andric const GlobalAlias &GA, const Constant &C) {
885bdd1243dSDimitry Andric if (GA.hasAvailableExternallyLinkage()) {
886bdd1243dSDimitry Andric Check(isa<GlobalValue>(C) &&
887bdd1243dSDimitry Andric cast<GlobalValue>(C).hasAvailableExternallyLinkage(),
888bdd1243dSDimitry Andric "available_externally alias must point to available_externally "
889bdd1243dSDimitry Andric "global value",
890bdd1243dSDimitry Andric &GA);
891bdd1243dSDimitry Andric }
8920b57cec5SDimitry Andric if (const auto *GV = dyn_cast<GlobalValue>(&C)) {
893bdd1243dSDimitry Andric if (!GA.hasAvailableExternallyLinkage()) {
89481ad6265SDimitry Andric Check(!GV->isDeclarationForLinker(), "Alias must point to a definition",
8950b57cec5SDimitry Andric &GA);
896bdd1243dSDimitry Andric }
8970b57cec5SDimitry Andric
8980b57cec5SDimitry Andric if (const auto *GA2 = dyn_cast<GlobalAlias>(GV)) {
89981ad6265SDimitry Andric Check(Visited.insert(GA2).second, "Aliases cannot form a cycle", &GA);
9000b57cec5SDimitry Andric
90181ad6265SDimitry Andric Check(!GA2->isInterposable(),
90281ad6265SDimitry Andric "Alias cannot point to an interposable alias", &GA);
9030b57cec5SDimitry Andric } else {
9040b57cec5SDimitry Andric // Only continue verifying subexpressions of GlobalAliases.
9050b57cec5SDimitry Andric // Do not recurse into global initializers.
9060b57cec5SDimitry Andric return;
9070b57cec5SDimitry Andric }
9080b57cec5SDimitry Andric }
9090b57cec5SDimitry Andric
9100b57cec5SDimitry Andric if (const auto *CE = dyn_cast<ConstantExpr>(&C))
9110b57cec5SDimitry Andric visitConstantExprsRecursively(CE);
9120b57cec5SDimitry Andric
9130b57cec5SDimitry Andric for (const Use &U : C.operands()) {
9140b57cec5SDimitry Andric Value *V = &*U;
9150b57cec5SDimitry Andric if (const auto *GA2 = dyn_cast<GlobalAlias>(V))
9160b57cec5SDimitry Andric visitAliaseeSubExpr(Visited, GA, *GA2->getAliasee());
9170b57cec5SDimitry Andric else if (const auto *C2 = dyn_cast<Constant>(V))
9180b57cec5SDimitry Andric visitAliaseeSubExpr(Visited, GA, *C2);
9190b57cec5SDimitry Andric }
9200b57cec5SDimitry Andric }
9210b57cec5SDimitry Andric
visitGlobalAlias(const GlobalAlias & GA)9220b57cec5SDimitry Andric void Verifier::visitGlobalAlias(const GlobalAlias &GA) {
92381ad6265SDimitry Andric Check(GlobalAlias::isValidLinkage(GA.getLinkage()),
9240b57cec5SDimitry Andric "Alias should have private, internal, linkonce, weak, linkonce_odr, "
925bdd1243dSDimitry Andric "weak_odr, external, or available_externally linkage!",
9260b57cec5SDimitry Andric &GA);
9270b57cec5SDimitry Andric const Constant *Aliasee = GA.getAliasee();
92881ad6265SDimitry Andric Check(Aliasee, "Aliasee cannot be NULL!", &GA);
92981ad6265SDimitry Andric Check(GA.getType() == Aliasee->getType(),
9300b57cec5SDimitry Andric "Alias and aliasee types should match!", &GA);
9310b57cec5SDimitry Andric
93281ad6265SDimitry Andric Check(isa<GlobalValue>(Aliasee) || isa<ConstantExpr>(Aliasee),
9330b57cec5SDimitry Andric "Aliasee should be either GlobalValue or ConstantExpr", &GA);
9340b57cec5SDimitry Andric
9350b57cec5SDimitry Andric visitAliaseeSubExpr(GA, *Aliasee);
9360b57cec5SDimitry Andric
9370b57cec5SDimitry Andric visitGlobalValue(GA);
9380b57cec5SDimitry Andric }
9390b57cec5SDimitry Andric
visitGlobalIFunc(const GlobalIFunc & GI)940349cc55cSDimitry Andric void Verifier::visitGlobalIFunc(const GlobalIFunc &GI) {
94181ad6265SDimitry Andric Check(GlobalIFunc::isValidLinkage(GI.getLinkage()),
94281ad6265SDimitry Andric "IFunc should have private, internal, linkonce, weak, linkonce_odr, "
94381ad6265SDimitry Andric "weak_odr, or external linkage!",
94481ad6265SDimitry Andric &GI);
945349cc55cSDimitry Andric // Pierce through ConstantExprs and GlobalAliases and check that the resolver
94681ad6265SDimitry Andric // is a Function definition.
947349cc55cSDimitry Andric const Function *Resolver = GI.getResolverFunction();
94881ad6265SDimitry Andric Check(Resolver, "IFunc must have a Function resolver", &GI);
94981ad6265SDimitry Andric Check(!Resolver->isDeclarationForLinker(),
95081ad6265SDimitry Andric "IFunc resolver must be a definition", &GI);
951349cc55cSDimitry Andric
952349cc55cSDimitry Andric // Check that the immediate resolver operand (prior to any bitcasts) has the
95381ad6265SDimitry Andric // correct type.
954349cc55cSDimitry Andric const Type *ResolverTy = GI.getResolver()->getType();
955bdd1243dSDimitry Andric
956bdd1243dSDimitry Andric Check(isa<PointerType>(Resolver->getFunctionType()->getReturnType()),
957bdd1243dSDimitry Andric "IFunc resolver must return a pointer", &GI);
958bdd1243dSDimitry Andric
959349cc55cSDimitry Andric const Type *ResolverFuncTy =
960349cc55cSDimitry Andric GlobalIFunc::getResolverFunctionType(GI.getValueType());
961bdd1243dSDimitry Andric Check(ResolverTy == ResolverFuncTy->getPointerTo(GI.getAddressSpace()),
962349cc55cSDimitry Andric "IFunc resolver has incorrect type", &GI);
963349cc55cSDimitry Andric }
964349cc55cSDimitry Andric
visitNamedMDNode(const NamedMDNode & NMD)9650b57cec5SDimitry Andric void Verifier::visitNamedMDNode(const NamedMDNode &NMD) {
9660b57cec5SDimitry Andric // There used to be various other llvm.dbg.* nodes, but we don't support
9670b57cec5SDimitry Andric // upgrading them and we want to reserve the namespace for future uses.
9685f757f3fSDimitry Andric if (NMD.getName().starts_with("llvm.dbg."))
96981ad6265SDimitry Andric CheckDI(NMD.getName() == "llvm.dbg.cu",
97081ad6265SDimitry Andric "unrecognized named metadata node in the llvm.dbg namespace", &NMD);
9710b57cec5SDimitry Andric for (const MDNode *MD : NMD.operands()) {
9720b57cec5SDimitry Andric if (NMD.getName() == "llvm.dbg.cu")
97381ad6265SDimitry Andric CheckDI(MD && isa<DICompileUnit>(MD), "invalid compile unit", &NMD, MD);
9740b57cec5SDimitry Andric
9750b57cec5SDimitry Andric if (!MD)
9760b57cec5SDimitry Andric continue;
9770b57cec5SDimitry Andric
9785ffd83dbSDimitry Andric visitMDNode(*MD, AreDebugLocsAllowed::Yes);
9790b57cec5SDimitry Andric }
9800b57cec5SDimitry Andric }
9810b57cec5SDimitry Andric
visitMDNode(const MDNode & MD,AreDebugLocsAllowed AllowLocs)9825ffd83dbSDimitry Andric void Verifier::visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs) {
9830b57cec5SDimitry Andric // Only visit each node once. Metadata can be mutually recursive, so this
9840b57cec5SDimitry Andric // avoids infinite recursion here, as well as being an optimization.
9850b57cec5SDimitry Andric if (!MDNodes.insert(&MD).second)
9860b57cec5SDimitry Andric return;
9870b57cec5SDimitry Andric
98881ad6265SDimitry Andric Check(&MD.getContext() == &Context,
989fe6060f1SDimitry Andric "MDNode context does not match Module context!", &MD);
990fe6060f1SDimitry Andric
9910b57cec5SDimitry Andric switch (MD.getMetadataID()) {
9920b57cec5SDimitry Andric default:
9930b57cec5SDimitry Andric llvm_unreachable("Invalid MDNode subclass");
9940b57cec5SDimitry Andric case Metadata::MDTupleKind:
9950b57cec5SDimitry Andric break;
9960b57cec5SDimitry Andric #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
9970b57cec5SDimitry Andric case Metadata::CLASS##Kind: \
9980b57cec5SDimitry Andric visit##CLASS(cast<CLASS>(MD)); \
9990b57cec5SDimitry Andric break;
10000b57cec5SDimitry Andric #include "llvm/IR/Metadata.def"
10010b57cec5SDimitry Andric }
10020b57cec5SDimitry Andric
10030b57cec5SDimitry Andric for (const Metadata *Op : MD.operands()) {
10040b57cec5SDimitry Andric if (!Op)
10050b57cec5SDimitry Andric continue;
100681ad6265SDimitry Andric Check(!isa<LocalAsMetadata>(Op), "Invalid operand for global metadata!",
10070b57cec5SDimitry Andric &MD, Op);
100881ad6265SDimitry Andric CheckDI(!isa<DILocation>(Op) || AllowLocs == AreDebugLocsAllowed::Yes,
10095ffd83dbSDimitry Andric "DILocation not allowed within this metadata node", &MD, Op);
10100b57cec5SDimitry Andric if (auto *N = dyn_cast<MDNode>(Op)) {
10115ffd83dbSDimitry Andric visitMDNode(*N, AllowLocs);
10120b57cec5SDimitry Andric continue;
10130b57cec5SDimitry Andric }
10140b57cec5SDimitry Andric if (auto *V = dyn_cast<ValueAsMetadata>(Op)) {
10150b57cec5SDimitry Andric visitValueAsMetadata(*V, nullptr);
10160b57cec5SDimitry Andric continue;
10170b57cec5SDimitry Andric }
10180b57cec5SDimitry Andric }
10190b57cec5SDimitry Andric
10200b57cec5SDimitry Andric // Check these last, so we diagnose problems in operands first.
102181ad6265SDimitry Andric Check(!MD.isTemporary(), "Expected no forward declarations!", &MD);
102281ad6265SDimitry Andric Check(MD.isResolved(), "All nodes should be resolved!", &MD);
10230b57cec5SDimitry Andric }
10240b57cec5SDimitry Andric
visitValueAsMetadata(const ValueAsMetadata & MD,Function * F)10250b57cec5SDimitry Andric void Verifier::visitValueAsMetadata(const ValueAsMetadata &MD, Function *F) {
102681ad6265SDimitry Andric Check(MD.getValue(), "Expected valid value", &MD);
102781ad6265SDimitry Andric Check(!MD.getValue()->getType()->isMetadataTy(),
10280b57cec5SDimitry Andric "Unexpected metadata round-trip through values", &MD, MD.getValue());
10290b57cec5SDimitry Andric
10300b57cec5SDimitry Andric auto *L = dyn_cast<LocalAsMetadata>(&MD);
10310b57cec5SDimitry Andric if (!L)
10320b57cec5SDimitry Andric return;
10330b57cec5SDimitry Andric
103481ad6265SDimitry Andric Check(F, "function-local metadata used outside a function", L);
10350b57cec5SDimitry Andric
10360b57cec5SDimitry Andric // If this was an instruction, bb, or argument, verify that it is in the
10370b57cec5SDimitry Andric // function that we expect.
10380b57cec5SDimitry Andric Function *ActualF = nullptr;
10390b57cec5SDimitry Andric if (Instruction *I = dyn_cast<Instruction>(L->getValue())) {
104081ad6265SDimitry Andric Check(I->getParent(), "function-local metadata not in basic block", L, I);
10410b57cec5SDimitry Andric ActualF = I->getParent()->getParent();
10420b57cec5SDimitry Andric } else if (BasicBlock *BB = dyn_cast<BasicBlock>(L->getValue()))
10430b57cec5SDimitry Andric ActualF = BB->getParent();
10440b57cec5SDimitry Andric else if (Argument *A = dyn_cast<Argument>(L->getValue()))
10450b57cec5SDimitry Andric ActualF = A->getParent();
10460b57cec5SDimitry Andric assert(ActualF && "Unimplemented function local metadata case!");
10470b57cec5SDimitry Andric
104881ad6265SDimitry Andric Check(ActualF == F, "function-local metadata used in wrong function", L);
10490b57cec5SDimitry Andric }
10500b57cec5SDimitry Andric
visitDIArgList(const DIArgList & AL,Function * F)10515f757f3fSDimitry Andric void Verifier::visitDIArgList(const DIArgList &AL, Function *F) {
10525f757f3fSDimitry Andric for (const ValueAsMetadata *VAM : AL.getArgs())
10535f757f3fSDimitry Andric visitValueAsMetadata(*VAM, F);
10545f757f3fSDimitry Andric }
10555f757f3fSDimitry Andric
visitMetadataAsValue(const MetadataAsValue & MDV,Function * F)10560b57cec5SDimitry Andric void Verifier::visitMetadataAsValue(const MetadataAsValue &MDV, Function *F) {
10570b57cec5SDimitry Andric Metadata *MD = MDV.getMetadata();
10580b57cec5SDimitry Andric if (auto *N = dyn_cast<MDNode>(MD)) {
10595ffd83dbSDimitry Andric visitMDNode(*N, AreDebugLocsAllowed::No);
10600b57cec5SDimitry Andric return;
10610b57cec5SDimitry Andric }
10620b57cec5SDimitry Andric
10630b57cec5SDimitry Andric // Only visit each node once. Metadata can be mutually recursive, so this
10640b57cec5SDimitry Andric // avoids infinite recursion here, as well as being an optimization.
10650b57cec5SDimitry Andric if (!MDNodes.insert(MD).second)
10660b57cec5SDimitry Andric return;
10670b57cec5SDimitry Andric
10680b57cec5SDimitry Andric if (auto *V = dyn_cast<ValueAsMetadata>(MD))
10690b57cec5SDimitry Andric visitValueAsMetadata(*V, F);
10705f757f3fSDimitry Andric
10715f757f3fSDimitry Andric if (auto *AL = dyn_cast<DIArgList>(MD))
10725f757f3fSDimitry Andric visitDIArgList(*AL, F);
10730b57cec5SDimitry Andric }
10740b57cec5SDimitry Andric
isType(const Metadata * MD)10750b57cec5SDimitry Andric static bool isType(const Metadata *MD) { return !MD || isa<DIType>(MD); }
isScope(const Metadata * MD)10760b57cec5SDimitry Andric static bool isScope(const Metadata *MD) { return !MD || isa<DIScope>(MD); }
isDINode(const Metadata * MD)10770b57cec5SDimitry Andric static bool isDINode(const Metadata *MD) { return !MD || isa<DINode>(MD); }
10780b57cec5SDimitry Andric
visitDILocation(const DILocation & N)10790b57cec5SDimitry Andric void Verifier::visitDILocation(const DILocation &N) {
108081ad6265SDimitry Andric CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
10810b57cec5SDimitry Andric "location requires a valid scope", &N, N.getRawScope());
10820b57cec5SDimitry Andric if (auto *IA = N.getRawInlinedAt())
108381ad6265SDimitry Andric CheckDI(isa<DILocation>(IA), "inlined-at should be a location", &N, IA);
10840b57cec5SDimitry Andric if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
108581ad6265SDimitry Andric CheckDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
10860b57cec5SDimitry Andric }
10870b57cec5SDimitry Andric
visitGenericDINode(const GenericDINode & N)10880b57cec5SDimitry Andric void Verifier::visitGenericDINode(const GenericDINode &N) {
108981ad6265SDimitry Andric CheckDI(N.getTag(), "invalid tag", &N);
10900b57cec5SDimitry Andric }
10910b57cec5SDimitry Andric
visitDIScope(const DIScope & N)10920b57cec5SDimitry Andric void Verifier::visitDIScope(const DIScope &N) {
10930b57cec5SDimitry Andric if (auto *F = N.getRawFile())
109481ad6265SDimitry Andric CheckDI(isa<DIFile>(F), "invalid file", &N, F);
10950b57cec5SDimitry Andric }
10960b57cec5SDimitry Andric
visitDISubrange(const DISubrange & N)10970b57cec5SDimitry Andric void Verifier::visitDISubrange(const DISubrange &N) {
109881ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_subrange_type, "invalid tag", &N);
1099e8d8bef9SDimitry Andric bool HasAssumedSizedArraySupport = dwarf::isFortran(CurrentSourceLang);
110081ad6265SDimitry Andric CheckDI(HasAssumedSizedArraySupport || N.getRawCountNode() ||
1101e8d8bef9SDimitry Andric N.getRawUpperBound(),
11025ffd83dbSDimitry Andric "Subrange must contain count or upperBound", &N);
110381ad6265SDimitry Andric CheckDI(!N.getRawCountNode() || !N.getRawUpperBound(),
11045ffd83dbSDimitry Andric "Subrange can have any one of count or upperBound", &N);
1105fe6060f1SDimitry Andric auto *CBound = N.getRawCountNode();
110681ad6265SDimitry Andric CheckDI(!CBound || isa<ConstantAsMetadata>(CBound) ||
1107fe6060f1SDimitry Andric isa<DIVariable>(CBound) || isa<DIExpression>(CBound),
1108fe6060f1SDimitry Andric "Count must be signed constant or DIVariable or DIExpression", &N);
11090b57cec5SDimitry Andric auto Count = N.getCount();
111006c3fb27SDimitry Andric CheckDI(!Count || !isa<ConstantInt *>(Count) ||
111106c3fb27SDimitry Andric cast<ConstantInt *>(Count)->getSExtValue() >= -1,
11120b57cec5SDimitry Andric "invalid subrange count", &N);
11135ffd83dbSDimitry Andric auto *LBound = N.getRawLowerBound();
111481ad6265SDimitry Andric CheckDI(!LBound || isa<ConstantAsMetadata>(LBound) ||
11155ffd83dbSDimitry Andric isa<DIVariable>(LBound) || isa<DIExpression>(LBound),
11165ffd83dbSDimitry Andric "LowerBound must be signed constant or DIVariable or DIExpression",
11175ffd83dbSDimitry Andric &N);
11185ffd83dbSDimitry Andric auto *UBound = N.getRawUpperBound();
111981ad6265SDimitry Andric CheckDI(!UBound || isa<ConstantAsMetadata>(UBound) ||
11205ffd83dbSDimitry Andric isa<DIVariable>(UBound) || isa<DIExpression>(UBound),
11215ffd83dbSDimitry Andric "UpperBound must be signed constant or DIVariable or DIExpression",
11225ffd83dbSDimitry Andric &N);
11235ffd83dbSDimitry Andric auto *Stride = N.getRawStride();
112481ad6265SDimitry Andric CheckDI(!Stride || isa<ConstantAsMetadata>(Stride) ||
11255ffd83dbSDimitry Andric isa<DIVariable>(Stride) || isa<DIExpression>(Stride),
11265ffd83dbSDimitry Andric "Stride must be signed constant or DIVariable or DIExpression", &N);
11270b57cec5SDimitry Andric }
11280b57cec5SDimitry Andric
visitDIGenericSubrange(const DIGenericSubrange & N)1129e8d8bef9SDimitry Andric void Verifier::visitDIGenericSubrange(const DIGenericSubrange &N) {
113081ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_generic_subrange, "invalid tag", &N);
113181ad6265SDimitry Andric CheckDI(N.getRawCountNode() || N.getRawUpperBound(),
1132e8d8bef9SDimitry Andric "GenericSubrange must contain count or upperBound", &N);
113381ad6265SDimitry Andric CheckDI(!N.getRawCountNode() || !N.getRawUpperBound(),
1134e8d8bef9SDimitry Andric "GenericSubrange can have any one of count or upperBound", &N);
1135e8d8bef9SDimitry Andric auto *CBound = N.getRawCountNode();
113681ad6265SDimitry Andric CheckDI(!CBound || isa<DIVariable>(CBound) || isa<DIExpression>(CBound),
1137e8d8bef9SDimitry Andric "Count must be signed constant or DIVariable or DIExpression", &N);
1138e8d8bef9SDimitry Andric auto *LBound = N.getRawLowerBound();
113981ad6265SDimitry Andric CheckDI(LBound, "GenericSubrange must contain lowerBound", &N);
114081ad6265SDimitry Andric CheckDI(isa<DIVariable>(LBound) || isa<DIExpression>(LBound),
1141e8d8bef9SDimitry Andric "LowerBound must be signed constant or DIVariable or DIExpression",
1142e8d8bef9SDimitry Andric &N);
1143e8d8bef9SDimitry Andric auto *UBound = N.getRawUpperBound();
114481ad6265SDimitry Andric CheckDI(!UBound || isa<DIVariable>(UBound) || isa<DIExpression>(UBound),
1145e8d8bef9SDimitry Andric "UpperBound must be signed constant or DIVariable or DIExpression",
1146e8d8bef9SDimitry Andric &N);
1147e8d8bef9SDimitry Andric auto *Stride = N.getRawStride();
114881ad6265SDimitry Andric CheckDI(Stride, "GenericSubrange must contain stride", &N);
114981ad6265SDimitry Andric CheckDI(isa<DIVariable>(Stride) || isa<DIExpression>(Stride),
1150e8d8bef9SDimitry Andric "Stride must be signed constant or DIVariable or DIExpression", &N);
1151e8d8bef9SDimitry Andric }
1152e8d8bef9SDimitry Andric
visitDIEnumerator(const DIEnumerator & N)11530b57cec5SDimitry Andric void Verifier::visitDIEnumerator(const DIEnumerator &N) {
115481ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_enumerator, "invalid tag", &N);
11550b57cec5SDimitry Andric }
11560b57cec5SDimitry Andric
visitDIBasicType(const DIBasicType & N)11570b57cec5SDimitry Andric void Verifier::visitDIBasicType(const DIBasicType &N) {
115881ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_base_type ||
1159e8d8bef9SDimitry Andric N.getTag() == dwarf::DW_TAG_unspecified_type ||
1160e8d8bef9SDimitry Andric N.getTag() == dwarf::DW_TAG_string_type,
11610b57cec5SDimitry Andric "invalid tag", &N);
1162e8d8bef9SDimitry Andric }
1163e8d8bef9SDimitry Andric
visitDIStringType(const DIStringType & N)1164e8d8bef9SDimitry Andric void Verifier::visitDIStringType(const DIStringType &N) {
116581ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_string_type, "invalid tag", &N);
116681ad6265SDimitry Andric CheckDI(!(N.isBigEndian() && N.isLittleEndian()), "has conflicting flags",
116781ad6265SDimitry Andric &N);
11680b57cec5SDimitry Andric }
11690b57cec5SDimitry Andric
visitDIDerivedType(const DIDerivedType & N)11700b57cec5SDimitry Andric void Verifier::visitDIDerivedType(const DIDerivedType &N) {
11710b57cec5SDimitry Andric // Common scope checks.
11720b57cec5SDimitry Andric visitDIScope(N);
11730b57cec5SDimitry Andric
117481ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_typedef ||
11750b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_pointer_type ||
11760b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_ptr_to_member_type ||
11770b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_reference_type ||
11780b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_rvalue_reference_type ||
11790b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_const_type ||
118004eeddc0SDimitry Andric N.getTag() == dwarf::DW_TAG_immutable_type ||
11810b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_volatile_type ||
11820b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_restrict_type ||
11830b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_atomic_type ||
11840b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_member ||
11855f757f3fSDimitry Andric (N.getTag() == dwarf::DW_TAG_variable && N.isStaticMember()) ||
11860b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_inheritance ||
1187fe6060f1SDimitry Andric N.getTag() == dwarf::DW_TAG_friend ||
1188fe6060f1SDimitry Andric N.getTag() == dwarf::DW_TAG_set_type,
11890b57cec5SDimitry Andric "invalid tag", &N);
11900b57cec5SDimitry Andric if (N.getTag() == dwarf::DW_TAG_ptr_to_member_type) {
119181ad6265SDimitry Andric CheckDI(isType(N.getRawExtraData()), "invalid pointer to member type", &N,
11920b57cec5SDimitry Andric N.getRawExtraData());
11930b57cec5SDimitry Andric }
11940b57cec5SDimitry Andric
1195fe6060f1SDimitry Andric if (N.getTag() == dwarf::DW_TAG_set_type) {
1196fe6060f1SDimitry Andric if (auto *T = N.getRawBaseType()) {
1197fe6060f1SDimitry Andric auto *Enum = dyn_cast_or_null<DICompositeType>(T);
1198fe6060f1SDimitry Andric auto *Basic = dyn_cast_or_null<DIBasicType>(T);
119981ad6265SDimitry Andric CheckDI(
1200fe6060f1SDimitry Andric (Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type) ||
1201fe6060f1SDimitry Andric (Basic && (Basic->getEncoding() == dwarf::DW_ATE_unsigned ||
1202fe6060f1SDimitry Andric Basic->getEncoding() == dwarf::DW_ATE_signed ||
1203fe6060f1SDimitry Andric Basic->getEncoding() == dwarf::DW_ATE_unsigned_char ||
1204fe6060f1SDimitry Andric Basic->getEncoding() == dwarf::DW_ATE_signed_char ||
1205fe6060f1SDimitry Andric Basic->getEncoding() == dwarf::DW_ATE_boolean)),
1206fe6060f1SDimitry Andric "invalid set base type", &N, T);
1207fe6060f1SDimitry Andric }
1208fe6060f1SDimitry Andric }
1209fe6060f1SDimitry Andric
121081ad6265SDimitry Andric CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
121181ad6265SDimitry Andric CheckDI(isType(N.getRawBaseType()), "invalid base type", &N,
12120b57cec5SDimitry Andric N.getRawBaseType());
12130b57cec5SDimitry Andric
12140b57cec5SDimitry Andric if (N.getDWARFAddressSpace()) {
121581ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_pointer_type ||
12160b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_reference_type ||
12170b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_rvalue_reference_type,
12180b57cec5SDimitry Andric "DWARF address space only applies to pointer or reference types",
12190b57cec5SDimitry Andric &N);
12200b57cec5SDimitry Andric }
12210b57cec5SDimitry Andric }
12220b57cec5SDimitry Andric
12230b57cec5SDimitry Andric /// Detect mutually exclusive flags.
hasConflictingReferenceFlags(unsigned Flags)12240b57cec5SDimitry Andric static bool hasConflictingReferenceFlags(unsigned Flags) {
12250b57cec5SDimitry Andric return ((Flags & DINode::FlagLValueReference) &&
12260b57cec5SDimitry Andric (Flags & DINode::FlagRValueReference)) ||
12270b57cec5SDimitry Andric ((Flags & DINode::FlagTypePassByValue) &&
12280b57cec5SDimitry Andric (Flags & DINode::FlagTypePassByReference));
12290b57cec5SDimitry Andric }
12300b57cec5SDimitry Andric
visitTemplateParams(const MDNode & N,const Metadata & RawParams)12310b57cec5SDimitry Andric void Verifier::visitTemplateParams(const MDNode &N, const Metadata &RawParams) {
12320b57cec5SDimitry Andric auto *Params = dyn_cast<MDTuple>(&RawParams);
123381ad6265SDimitry Andric CheckDI(Params, "invalid template params", &N, &RawParams);
12340b57cec5SDimitry Andric for (Metadata *Op : Params->operands()) {
123581ad6265SDimitry Andric CheckDI(Op && isa<DITemplateParameter>(Op), "invalid template parameter",
12360b57cec5SDimitry Andric &N, Params, Op);
12370b57cec5SDimitry Andric }
12380b57cec5SDimitry Andric }
12390b57cec5SDimitry Andric
visitDICompositeType(const DICompositeType & N)12400b57cec5SDimitry Andric void Verifier::visitDICompositeType(const DICompositeType &N) {
12410b57cec5SDimitry Andric // Common scope checks.
12420b57cec5SDimitry Andric visitDIScope(N);
12430b57cec5SDimitry Andric
124481ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_array_type ||
12450b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_structure_type ||
12460b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_union_type ||
12470b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_enumeration_type ||
12480b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_class_type ||
1249349cc55cSDimitry Andric N.getTag() == dwarf::DW_TAG_variant_part ||
1250349cc55cSDimitry Andric N.getTag() == dwarf::DW_TAG_namelist,
12510b57cec5SDimitry Andric "invalid tag", &N);
12520b57cec5SDimitry Andric
125381ad6265SDimitry Andric CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
125481ad6265SDimitry Andric CheckDI(isType(N.getRawBaseType()), "invalid base type", &N,
12550b57cec5SDimitry Andric N.getRawBaseType());
12560b57cec5SDimitry Andric
125781ad6265SDimitry Andric CheckDI(!N.getRawElements() || isa<MDTuple>(N.getRawElements()),
12580b57cec5SDimitry Andric "invalid composite elements", &N, N.getRawElements());
125981ad6265SDimitry Andric CheckDI(isType(N.getRawVTableHolder()), "invalid vtable holder", &N,
12600b57cec5SDimitry Andric N.getRawVTableHolder());
126181ad6265SDimitry Andric CheckDI(!hasConflictingReferenceFlags(N.getFlags()),
12620b57cec5SDimitry Andric "invalid reference flags", &N);
12638bcb0991SDimitry Andric unsigned DIBlockByRefStruct = 1 << 4;
126481ad6265SDimitry Andric CheckDI((N.getFlags() & DIBlockByRefStruct) == 0,
12658bcb0991SDimitry Andric "DIBlockByRefStruct on DICompositeType is no longer supported", &N);
12660b57cec5SDimitry Andric
12670b57cec5SDimitry Andric if (N.isVector()) {
12680b57cec5SDimitry Andric const DINodeArray Elements = N.getElements();
126981ad6265SDimitry Andric CheckDI(Elements.size() == 1 &&
12700b57cec5SDimitry Andric Elements[0]->getTag() == dwarf::DW_TAG_subrange_type,
12710b57cec5SDimitry Andric "invalid vector, expected one element of type subrange", &N);
12720b57cec5SDimitry Andric }
12730b57cec5SDimitry Andric
12740b57cec5SDimitry Andric if (auto *Params = N.getRawTemplateParams())
12750b57cec5SDimitry Andric visitTemplateParams(N, *Params);
12760b57cec5SDimitry Andric
12770b57cec5SDimitry Andric if (auto *D = N.getRawDiscriminator()) {
127881ad6265SDimitry Andric CheckDI(isa<DIDerivedType>(D) && N.getTag() == dwarf::DW_TAG_variant_part,
12790b57cec5SDimitry Andric "discriminator can only appear on variant part");
12800b57cec5SDimitry Andric }
12815ffd83dbSDimitry Andric
12825ffd83dbSDimitry Andric if (N.getRawDataLocation()) {
128381ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
12845ffd83dbSDimitry Andric "dataLocation can only appear in array type");
12855ffd83dbSDimitry Andric }
1286e8d8bef9SDimitry Andric
1287e8d8bef9SDimitry Andric if (N.getRawAssociated()) {
128881ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1289e8d8bef9SDimitry Andric "associated can only appear in array type");
1290e8d8bef9SDimitry Andric }
1291e8d8bef9SDimitry Andric
1292e8d8bef9SDimitry Andric if (N.getRawAllocated()) {
129381ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1294e8d8bef9SDimitry Andric "allocated can only appear in array type");
1295e8d8bef9SDimitry Andric }
1296e8d8bef9SDimitry Andric
1297e8d8bef9SDimitry Andric if (N.getRawRank()) {
129881ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1299e8d8bef9SDimitry Andric "rank can only appear in array type");
1300e8d8bef9SDimitry Andric }
13015f757f3fSDimitry Andric
13025f757f3fSDimitry Andric if (N.getTag() == dwarf::DW_TAG_array_type) {
13035f757f3fSDimitry Andric CheckDI(N.getRawBaseType(), "array types must have a base type", &N);
13045f757f3fSDimitry Andric }
13050b57cec5SDimitry Andric }
13060b57cec5SDimitry Andric
visitDISubroutineType(const DISubroutineType & N)13070b57cec5SDimitry Andric void Verifier::visitDISubroutineType(const DISubroutineType &N) {
130881ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_subroutine_type, "invalid tag", &N);
13090b57cec5SDimitry Andric if (auto *Types = N.getRawTypeArray()) {
131081ad6265SDimitry Andric CheckDI(isa<MDTuple>(Types), "invalid composite elements", &N, Types);
13110b57cec5SDimitry Andric for (Metadata *Ty : N.getTypeArray()->operands()) {
131281ad6265SDimitry Andric CheckDI(isType(Ty), "invalid subroutine type ref", &N, Types, Ty);
13130b57cec5SDimitry Andric }
13140b57cec5SDimitry Andric }
131581ad6265SDimitry Andric CheckDI(!hasConflictingReferenceFlags(N.getFlags()),
13160b57cec5SDimitry Andric "invalid reference flags", &N);
13170b57cec5SDimitry Andric }
13180b57cec5SDimitry Andric
visitDIFile(const DIFile & N)13190b57cec5SDimitry Andric void Verifier::visitDIFile(const DIFile &N) {
132081ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_file_type, "invalid tag", &N);
1321bdd1243dSDimitry Andric std::optional<DIFile::ChecksumInfo<StringRef>> Checksum = N.getChecksum();
13220b57cec5SDimitry Andric if (Checksum) {
132381ad6265SDimitry Andric CheckDI(Checksum->Kind <= DIFile::ChecksumKind::CSK_Last,
13240b57cec5SDimitry Andric "invalid checksum kind", &N);
13250b57cec5SDimitry Andric size_t Size;
13260b57cec5SDimitry Andric switch (Checksum->Kind) {
13270b57cec5SDimitry Andric case DIFile::CSK_MD5:
13280b57cec5SDimitry Andric Size = 32;
13290b57cec5SDimitry Andric break;
13300b57cec5SDimitry Andric case DIFile::CSK_SHA1:
13310b57cec5SDimitry Andric Size = 40;
13320b57cec5SDimitry Andric break;
13335ffd83dbSDimitry Andric case DIFile::CSK_SHA256:
13345ffd83dbSDimitry Andric Size = 64;
13355ffd83dbSDimitry Andric break;
13360b57cec5SDimitry Andric }
133781ad6265SDimitry Andric CheckDI(Checksum->Value.size() == Size, "invalid checksum length", &N);
133881ad6265SDimitry Andric CheckDI(Checksum->Value.find_if_not(llvm::isHexDigit) == StringRef::npos,
13390b57cec5SDimitry Andric "invalid checksum", &N);
13400b57cec5SDimitry Andric }
13410b57cec5SDimitry Andric }
13420b57cec5SDimitry Andric
visitDICompileUnit(const DICompileUnit & N)13430b57cec5SDimitry Andric void Verifier::visitDICompileUnit(const DICompileUnit &N) {
134481ad6265SDimitry Andric CheckDI(N.isDistinct(), "compile units must be distinct", &N);
134581ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_compile_unit, "invalid tag", &N);
13460b57cec5SDimitry Andric
13470b57cec5SDimitry Andric // Don't bother verifying the compilation directory or producer string
13480b57cec5SDimitry Andric // as those could be empty.
134981ad6265SDimitry Andric CheckDI(N.getRawFile() && isa<DIFile>(N.getRawFile()), "invalid file", &N,
13500b57cec5SDimitry Andric N.getRawFile());
135181ad6265SDimitry Andric CheckDI(!N.getFile()->getFilename().empty(), "invalid filename", &N,
13520b57cec5SDimitry Andric N.getFile());
13530b57cec5SDimitry Andric
1354e8d8bef9SDimitry Andric CurrentSourceLang = (dwarf::SourceLanguage)N.getSourceLanguage();
1355e8d8bef9SDimitry Andric
135681ad6265SDimitry Andric CheckDI((N.getEmissionKind() <= DICompileUnit::LastEmissionKind),
13570b57cec5SDimitry Andric "invalid emission kind", &N);
13580b57cec5SDimitry Andric
13590b57cec5SDimitry Andric if (auto *Array = N.getRawEnumTypes()) {
136081ad6265SDimitry Andric CheckDI(isa<MDTuple>(Array), "invalid enum list", &N, Array);
13610b57cec5SDimitry Andric for (Metadata *Op : N.getEnumTypes()->operands()) {
13620b57cec5SDimitry Andric auto *Enum = dyn_cast_or_null<DICompositeType>(Op);
136381ad6265SDimitry Andric CheckDI(Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type,
13640b57cec5SDimitry Andric "invalid enum type", &N, N.getEnumTypes(), Op);
13650b57cec5SDimitry Andric }
13660b57cec5SDimitry Andric }
13670b57cec5SDimitry Andric if (auto *Array = N.getRawRetainedTypes()) {
136881ad6265SDimitry Andric CheckDI(isa<MDTuple>(Array), "invalid retained type list", &N, Array);
13690b57cec5SDimitry Andric for (Metadata *Op : N.getRetainedTypes()->operands()) {
137081ad6265SDimitry Andric CheckDI(
137181ad6265SDimitry Andric Op && (isa<DIType>(Op) || (isa<DISubprogram>(Op) &&
13720b57cec5SDimitry Andric !cast<DISubprogram>(Op)->isDefinition())),
13730b57cec5SDimitry Andric "invalid retained type", &N, Op);
13740b57cec5SDimitry Andric }
13750b57cec5SDimitry Andric }
13760b57cec5SDimitry Andric if (auto *Array = N.getRawGlobalVariables()) {
137781ad6265SDimitry Andric CheckDI(isa<MDTuple>(Array), "invalid global variable list", &N, Array);
13780b57cec5SDimitry Andric for (Metadata *Op : N.getGlobalVariables()->operands()) {
137981ad6265SDimitry Andric CheckDI(Op && (isa<DIGlobalVariableExpression>(Op)),
13800b57cec5SDimitry Andric "invalid global variable ref", &N, Op);
13810b57cec5SDimitry Andric }
13820b57cec5SDimitry Andric }
13830b57cec5SDimitry Andric if (auto *Array = N.getRawImportedEntities()) {
138481ad6265SDimitry Andric CheckDI(isa<MDTuple>(Array), "invalid imported entity list", &N, Array);
13850b57cec5SDimitry Andric for (Metadata *Op : N.getImportedEntities()->operands()) {
138681ad6265SDimitry Andric CheckDI(Op && isa<DIImportedEntity>(Op), "invalid imported entity ref",
13870b57cec5SDimitry Andric &N, Op);
13880b57cec5SDimitry Andric }
13890b57cec5SDimitry Andric }
13900b57cec5SDimitry Andric if (auto *Array = N.getRawMacros()) {
139181ad6265SDimitry Andric CheckDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
13920b57cec5SDimitry Andric for (Metadata *Op : N.getMacros()->operands()) {
139381ad6265SDimitry Andric CheckDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
13940b57cec5SDimitry Andric }
13950b57cec5SDimitry Andric }
13960b57cec5SDimitry Andric CUVisited.insert(&N);
13970b57cec5SDimitry Andric }
13980b57cec5SDimitry Andric
visitDISubprogram(const DISubprogram & N)13990b57cec5SDimitry Andric void Verifier::visitDISubprogram(const DISubprogram &N) {
140081ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_subprogram, "invalid tag", &N);
140181ad6265SDimitry Andric CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
14020b57cec5SDimitry Andric if (auto *F = N.getRawFile())
140381ad6265SDimitry Andric CheckDI(isa<DIFile>(F), "invalid file", &N, F);
14040b57cec5SDimitry Andric else
140581ad6265SDimitry Andric CheckDI(N.getLine() == 0, "line specified with no file", &N, N.getLine());
14060b57cec5SDimitry Andric if (auto *T = N.getRawType())
140781ad6265SDimitry Andric CheckDI(isa<DISubroutineType>(T), "invalid subroutine type", &N, T);
140881ad6265SDimitry Andric CheckDI(isType(N.getRawContainingType()), "invalid containing type", &N,
14090b57cec5SDimitry Andric N.getRawContainingType());
14100b57cec5SDimitry Andric if (auto *Params = N.getRawTemplateParams())
14110b57cec5SDimitry Andric visitTemplateParams(N, *Params);
14120b57cec5SDimitry Andric if (auto *S = N.getRawDeclaration())
141381ad6265SDimitry Andric CheckDI(isa<DISubprogram>(S) && !cast<DISubprogram>(S)->isDefinition(),
14140b57cec5SDimitry Andric "invalid subprogram declaration", &N, S);
14150b57cec5SDimitry Andric if (auto *RawNode = N.getRawRetainedNodes()) {
14160b57cec5SDimitry Andric auto *Node = dyn_cast<MDTuple>(RawNode);
141781ad6265SDimitry Andric CheckDI(Node, "invalid retained nodes list", &N, RawNode);
14180b57cec5SDimitry Andric for (Metadata *Op : Node->operands()) {
141906c3fb27SDimitry Andric CheckDI(Op && (isa<DILocalVariable>(Op) || isa<DILabel>(Op) ||
142006c3fb27SDimitry Andric isa<DIImportedEntity>(Op)),
142106c3fb27SDimitry Andric "invalid retained nodes, expected DILocalVariable, DILabel or "
142206c3fb27SDimitry Andric "DIImportedEntity",
142306c3fb27SDimitry Andric &N, Node, Op);
14240b57cec5SDimitry Andric }
14250b57cec5SDimitry Andric }
142681ad6265SDimitry Andric CheckDI(!hasConflictingReferenceFlags(N.getFlags()),
14270b57cec5SDimitry Andric "invalid reference flags", &N);
14280b57cec5SDimitry Andric
14290b57cec5SDimitry Andric auto *Unit = N.getRawUnit();
14300b57cec5SDimitry Andric if (N.isDefinition()) {
14310b57cec5SDimitry Andric // Subprogram definitions (not part of the type hierarchy).
143281ad6265SDimitry Andric CheckDI(N.isDistinct(), "subprogram definitions must be distinct", &N);
143381ad6265SDimitry Andric CheckDI(Unit, "subprogram definitions must have a compile unit", &N);
143481ad6265SDimitry Andric CheckDI(isa<DICompileUnit>(Unit), "invalid unit type", &N, Unit);
14355f757f3fSDimitry Andric // There's no good way to cross the CU boundary to insert a nested
14365f757f3fSDimitry Andric // DISubprogram definition in one CU into a type defined in another CU.
14375f757f3fSDimitry Andric auto *CT = dyn_cast_or_null<DICompositeType>(N.getRawScope());
14385f757f3fSDimitry Andric if (CT && CT->getRawIdentifier() &&
14395f757f3fSDimitry Andric M.getContext().isODRUniquingDebugTypes())
14405f757f3fSDimitry Andric CheckDI(N.getDeclaration(),
14415f757f3fSDimitry Andric "definition subprograms cannot be nested within DICompositeType "
14425f757f3fSDimitry Andric "when enabling ODR",
14435f757f3fSDimitry Andric &N);
14440b57cec5SDimitry Andric } else {
14450b57cec5SDimitry Andric // Subprogram declarations (part of the type hierarchy).
144681ad6265SDimitry Andric CheckDI(!Unit, "subprogram declarations must not have a compile unit", &N);
144706c3fb27SDimitry Andric CheckDI(!N.getRawDeclaration(),
144806c3fb27SDimitry Andric "subprogram declaration must not have a declaration field");
14490b57cec5SDimitry Andric }
14500b57cec5SDimitry Andric
14510b57cec5SDimitry Andric if (auto *RawThrownTypes = N.getRawThrownTypes()) {
14520b57cec5SDimitry Andric auto *ThrownTypes = dyn_cast<MDTuple>(RawThrownTypes);
145381ad6265SDimitry Andric CheckDI(ThrownTypes, "invalid thrown types list", &N, RawThrownTypes);
14540b57cec5SDimitry Andric for (Metadata *Op : ThrownTypes->operands())
145581ad6265SDimitry Andric CheckDI(Op && isa<DIType>(Op), "invalid thrown type", &N, ThrownTypes,
14560b57cec5SDimitry Andric Op);
14570b57cec5SDimitry Andric }
14580b57cec5SDimitry Andric
14590b57cec5SDimitry Andric if (N.areAllCallsDescribed())
146081ad6265SDimitry Andric CheckDI(N.isDefinition(),
14610b57cec5SDimitry Andric "DIFlagAllCallsDescribed must be attached to a definition");
14620b57cec5SDimitry Andric }
14630b57cec5SDimitry Andric
visitDILexicalBlockBase(const DILexicalBlockBase & N)14640b57cec5SDimitry Andric void Verifier::visitDILexicalBlockBase(const DILexicalBlockBase &N) {
146581ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_lexical_block, "invalid tag", &N);
146681ad6265SDimitry Andric CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
14670b57cec5SDimitry Andric "invalid local scope", &N, N.getRawScope());
14680b57cec5SDimitry Andric if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
146981ad6265SDimitry Andric CheckDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
14700b57cec5SDimitry Andric }
14710b57cec5SDimitry Andric
visitDILexicalBlock(const DILexicalBlock & N)14720b57cec5SDimitry Andric void Verifier::visitDILexicalBlock(const DILexicalBlock &N) {
14730b57cec5SDimitry Andric visitDILexicalBlockBase(N);
14740b57cec5SDimitry Andric
147581ad6265SDimitry Andric CheckDI(N.getLine() || !N.getColumn(),
14760b57cec5SDimitry Andric "cannot have column info without line info", &N);
14770b57cec5SDimitry Andric }
14780b57cec5SDimitry Andric
visitDILexicalBlockFile(const DILexicalBlockFile & N)14790b57cec5SDimitry Andric void Verifier::visitDILexicalBlockFile(const DILexicalBlockFile &N) {
14800b57cec5SDimitry Andric visitDILexicalBlockBase(N);
14810b57cec5SDimitry Andric }
14820b57cec5SDimitry Andric
visitDICommonBlock(const DICommonBlock & N)14830b57cec5SDimitry Andric void Verifier::visitDICommonBlock(const DICommonBlock &N) {
148481ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_common_block, "invalid tag", &N);
14850b57cec5SDimitry Andric if (auto *S = N.getRawScope())
148681ad6265SDimitry Andric CheckDI(isa<DIScope>(S), "invalid scope ref", &N, S);
14870b57cec5SDimitry Andric if (auto *S = N.getRawDecl())
148881ad6265SDimitry Andric CheckDI(isa<DIGlobalVariable>(S), "invalid declaration", &N, S);
14890b57cec5SDimitry Andric }
14900b57cec5SDimitry Andric
visitDINamespace(const DINamespace & N)14910b57cec5SDimitry Andric void Verifier::visitDINamespace(const DINamespace &N) {
149281ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_namespace, "invalid tag", &N);
14930b57cec5SDimitry Andric if (auto *S = N.getRawScope())
149481ad6265SDimitry Andric CheckDI(isa<DIScope>(S), "invalid scope ref", &N, S);
14950b57cec5SDimitry Andric }
14960b57cec5SDimitry Andric
visitDIMacro(const DIMacro & N)14970b57cec5SDimitry Andric void Verifier::visitDIMacro(const DIMacro &N) {
149881ad6265SDimitry Andric CheckDI(N.getMacinfoType() == dwarf::DW_MACINFO_define ||
14990b57cec5SDimitry Andric N.getMacinfoType() == dwarf::DW_MACINFO_undef,
15000b57cec5SDimitry Andric "invalid macinfo type", &N);
150181ad6265SDimitry Andric CheckDI(!N.getName().empty(), "anonymous macro", &N);
15020b57cec5SDimitry Andric if (!N.getValue().empty()) {
15030b57cec5SDimitry Andric assert(N.getValue().data()[0] != ' ' && "Macro value has a space prefix");
15040b57cec5SDimitry Andric }
15050b57cec5SDimitry Andric }
15060b57cec5SDimitry Andric
visitDIMacroFile(const DIMacroFile & N)15070b57cec5SDimitry Andric void Verifier::visitDIMacroFile(const DIMacroFile &N) {
150881ad6265SDimitry Andric CheckDI(N.getMacinfoType() == dwarf::DW_MACINFO_start_file,
15090b57cec5SDimitry Andric "invalid macinfo type", &N);
15100b57cec5SDimitry Andric if (auto *F = N.getRawFile())
151181ad6265SDimitry Andric CheckDI(isa<DIFile>(F), "invalid file", &N, F);
15120b57cec5SDimitry Andric
15130b57cec5SDimitry Andric if (auto *Array = N.getRawElements()) {
151481ad6265SDimitry Andric CheckDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
15150b57cec5SDimitry Andric for (Metadata *Op : N.getElements()->operands()) {
151681ad6265SDimitry Andric CheckDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
15170b57cec5SDimitry Andric }
15180b57cec5SDimitry Andric }
15190b57cec5SDimitry Andric }
15200b57cec5SDimitry Andric
visitDIModule(const DIModule & N)15210b57cec5SDimitry Andric void Verifier::visitDIModule(const DIModule &N) {
152281ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_module, "invalid tag", &N);
152381ad6265SDimitry Andric CheckDI(!N.getName().empty(), "anonymous module", &N);
15240b57cec5SDimitry Andric }
15250b57cec5SDimitry Andric
visitDITemplateParameter(const DITemplateParameter & N)15260b57cec5SDimitry Andric void Verifier::visitDITemplateParameter(const DITemplateParameter &N) {
152781ad6265SDimitry Andric CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
15280b57cec5SDimitry Andric }
15290b57cec5SDimitry Andric
visitDITemplateTypeParameter(const DITemplateTypeParameter & N)15300b57cec5SDimitry Andric void Verifier::visitDITemplateTypeParameter(const DITemplateTypeParameter &N) {
15310b57cec5SDimitry Andric visitDITemplateParameter(N);
15320b57cec5SDimitry Andric
153381ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_template_type_parameter, "invalid tag",
15340b57cec5SDimitry Andric &N);
15350b57cec5SDimitry Andric }
15360b57cec5SDimitry Andric
visitDITemplateValueParameter(const DITemplateValueParameter & N)15370b57cec5SDimitry Andric void Verifier::visitDITemplateValueParameter(
15380b57cec5SDimitry Andric const DITemplateValueParameter &N) {
15390b57cec5SDimitry Andric visitDITemplateParameter(N);
15400b57cec5SDimitry Andric
154181ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_template_value_parameter ||
15420b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_GNU_template_template_param ||
15430b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack,
15440b57cec5SDimitry Andric "invalid tag", &N);
15450b57cec5SDimitry Andric }
15460b57cec5SDimitry Andric
visitDIVariable(const DIVariable & N)15470b57cec5SDimitry Andric void Verifier::visitDIVariable(const DIVariable &N) {
15480b57cec5SDimitry Andric if (auto *S = N.getRawScope())
154981ad6265SDimitry Andric CheckDI(isa<DIScope>(S), "invalid scope", &N, S);
15500b57cec5SDimitry Andric if (auto *F = N.getRawFile())
155181ad6265SDimitry Andric CheckDI(isa<DIFile>(F), "invalid file", &N, F);
15520b57cec5SDimitry Andric }
15530b57cec5SDimitry Andric
visitDIGlobalVariable(const DIGlobalVariable & N)15540b57cec5SDimitry Andric void Verifier::visitDIGlobalVariable(const DIGlobalVariable &N) {
15550b57cec5SDimitry Andric // Checks common to all variables.
15560b57cec5SDimitry Andric visitDIVariable(N);
15570b57cec5SDimitry Andric
155881ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
155981ad6265SDimitry Andric CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
156081ad6265SDimitry Andric // Check only if the global variable is not an extern
15615ffd83dbSDimitry Andric if (N.isDefinition())
156281ad6265SDimitry Andric CheckDI(N.getType(), "missing global variable type", &N);
15630b57cec5SDimitry Andric if (auto *Member = N.getRawStaticDataMemberDeclaration()) {
156481ad6265SDimitry Andric CheckDI(isa<DIDerivedType>(Member),
15650b57cec5SDimitry Andric "invalid static data member declaration", &N, Member);
15660b57cec5SDimitry Andric }
15670b57cec5SDimitry Andric }
15680b57cec5SDimitry Andric
visitDILocalVariable(const DILocalVariable & N)15690b57cec5SDimitry Andric void Verifier::visitDILocalVariable(const DILocalVariable &N) {
15700b57cec5SDimitry Andric // Checks common to all variables.
15710b57cec5SDimitry Andric visitDIVariable(N);
15720b57cec5SDimitry Andric
157381ad6265SDimitry Andric CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
157481ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
157581ad6265SDimitry Andric CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
15760b57cec5SDimitry Andric "local variable requires a valid scope", &N, N.getRawScope());
15770b57cec5SDimitry Andric if (auto Ty = N.getType())
157881ad6265SDimitry Andric CheckDI(!isa<DISubroutineType>(Ty), "invalid type", &N, N.getType());
15790b57cec5SDimitry Andric }
15800b57cec5SDimitry Andric
visitDIAssignID(const DIAssignID & N)1581bdd1243dSDimitry Andric void Verifier::visitDIAssignID(const DIAssignID &N) {
1582bdd1243dSDimitry Andric CheckDI(!N.getNumOperands(), "DIAssignID has no arguments", &N);
1583bdd1243dSDimitry Andric CheckDI(N.isDistinct(), "DIAssignID must be distinct", &N);
1584bdd1243dSDimitry Andric }
1585bdd1243dSDimitry Andric
visitDILabel(const DILabel & N)15860b57cec5SDimitry Andric void Verifier::visitDILabel(const DILabel &N) {
15870b57cec5SDimitry Andric if (auto *S = N.getRawScope())
158881ad6265SDimitry Andric CheckDI(isa<DIScope>(S), "invalid scope", &N, S);
15890b57cec5SDimitry Andric if (auto *F = N.getRawFile())
159081ad6265SDimitry Andric CheckDI(isa<DIFile>(F), "invalid file", &N, F);
15910b57cec5SDimitry Andric
159281ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_label, "invalid tag", &N);
159381ad6265SDimitry Andric CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
15940b57cec5SDimitry Andric "label requires a valid scope", &N, N.getRawScope());
15950b57cec5SDimitry Andric }
15960b57cec5SDimitry Andric
visitDIExpression(const DIExpression & N)15970b57cec5SDimitry Andric void Verifier::visitDIExpression(const DIExpression &N) {
159881ad6265SDimitry Andric CheckDI(N.isValid(), "invalid expression", &N);
15990b57cec5SDimitry Andric }
16000b57cec5SDimitry Andric
visitDIGlobalVariableExpression(const DIGlobalVariableExpression & GVE)16010b57cec5SDimitry Andric void Verifier::visitDIGlobalVariableExpression(
16020b57cec5SDimitry Andric const DIGlobalVariableExpression &GVE) {
160381ad6265SDimitry Andric CheckDI(GVE.getVariable(), "missing variable");
16040b57cec5SDimitry Andric if (auto *Var = GVE.getVariable())
16050b57cec5SDimitry Andric visitDIGlobalVariable(*Var);
16060b57cec5SDimitry Andric if (auto *Expr = GVE.getExpression()) {
16070b57cec5SDimitry Andric visitDIExpression(*Expr);
16080b57cec5SDimitry Andric if (auto Fragment = Expr->getFragmentInfo())
16090b57cec5SDimitry Andric verifyFragmentExpression(*GVE.getVariable(), *Fragment, &GVE);
16100b57cec5SDimitry Andric }
16110b57cec5SDimitry Andric }
16120b57cec5SDimitry Andric
visitDIObjCProperty(const DIObjCProperty & N)16130b57cec5SDimitry Andric void Verifier::visitDIObjCProperty(const DIObjCProperty &N) {
161481ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_APPLE_property, "invalid tag", &N);
16150b57cec5SDimitry Andric if (auto *T = N.getRawType())
161681ad6265SDimitry Andric CheckDI(isType(T), "invalid type ref", &N, T);
16170b57cec5SDimitry Andric if (auto *F = N.getRawFile())
161881ad6265SDimitry Andric CheckDI(isa<DIFile>(F), "invalid file", &N, F);
16190b57cec5SDimitry Andric }
16200b57cec5SDimitry Andric
visitDIImportedEntity(const DIImportedEntity & N)16210b57cec5SDimitry Andric void Verifier::visitDIImportedEntity(const DIImportedEntity &N) {
162281ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_imported_module ||
16230b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_imported_declaration,
16240b57cec5SDimitry Andric "invalid tag", &N);
16250b57cec5SDimitry Andric if (auto *S = N.getRawScope())
162681ad6265SDimitry Andric CheckDI(isa<DIScope>(S), "invalid scope for imported entity", &N, S);
162781ad6265SDimitry Andric CheckDI(isDINode(N.getRawEntity()), "invalid imported entity", &N,
16280b57cec5SDimitry Andric N.getRawEntity());
16290b57cec5SDimitry Andric }
16300b57cec5SDimitry Andric
visitComdat(const Comdat & C)16310b57cec5SDimitry Andric void Verifier::visitComdat(const Comdat &C) {
16328bcb0991SDimitry Andric // In COFF the Module is invalid if the GlobalValue has private linkage.
16338bcb0991SDimitry Andric // Entities with private linkage don't have entries in the symbol table.
16348bcb0991SDimitry Andric if (TT.isOSBinFormatCOFF())
16350b57cec5SDimitry Andric if (const GlobalValue *GV = M.getNamedValue(C.getName()))
163681ad6265SDimitry Andric Check(!GV->hasPrivateLinkage(), "comdat global value has private linkage",
163781ad6265SDimitry Andric GV);
16380b57cec5SDimitry Andric }
16390b57cec5SDimitry Andric
visitModuleIdents()1640349cc55cSDimitry Andric void Verifier::visitModuleIdents() {
16410b57cec5SDimitry Andric const NamedMDNode *Idents = M.getNamedMetadata("llvm.ident");
16420b57cec5SDimitry Andric if (!Idents)
16430b57cec5SDimitry Andric return;
16440b57cec5SDimitry Andric
16450b57cec5SDimitry Andric // llvm.ident takes a list of metadata entry. Each entry has only one string.
16460b57cec5SDimitry Andric // Scan each llvm.ident entry and make sure that this requirement is met.
16470b57cec5SDimitry Andric for (const MDNode *N : Idents->operands()) {
164881ad6265SDimitry Andric Check(N->getNumOperands() == 1,
16490b57cec5SDimitry Andric "incorrect number of operands in llvm.ident metadata", N);
165081ad6265SDimitry Andric Check(dyn_cast_or_null<MDString>(N->getOperand(0)),
16510b57cec5SDimitry Andric ("invalid value for llvm.ident metadata entry operand"
16520b57cec5SDimitry Andric "(the operand should be a string)"),
16530b57cec5SDimitry Andric N->getOperand(0));
16540b57cec5SDimitry Andric }
16550b57cec5SDimitry Andric }
16560b57cec5SDimitry Andric
visitModuleCommandLines()1657349cc55cSDimitry Andric void Verifier::visitModuleCommandLines() {
16580b57cec5SDimitry Andric const NamedMDNode *CommandLines = M.getNamedMetadata("llvm.commandline");
16590b57cec5SDimitry Andric if (!CommandLines)
16600b57cec5SDimitry Andric return;
16610b57cec5SDimitry Andric
16620b57cec5SDimitry Andric // llvm.commandline takes a list of metadata entry. Each entry has only one
16630b57cec5SDimitry Andric // string. Scan each llvm.commandline entry and make sure that this
16640b57cec5SDimitry Andric // requirement is met.
16650b57cec5SDimitry Andric for (const MDNode *N : CommandLines->operands()) {
166681ad6265SDimitry Andric Check(N->getNumOperands() == 1,
16670b57cec5SDimitry Andric "incorrect number of operands in llvm.commandline metadata", N);
166881ad6265SDimitry Andric Check(dyn_cast_or_null<MDString>(N->getOperand(0)),
16690b57cec5SDimitry Andric ("invalid value for llvm.commandline metadata entry operand"
16700b57cec5SDimitry Andric "(the operand should be a string)"),
16710b57cec5SDimitry Andric N->getOperand(0));
16720b57cec5SDimitry Andric }
16730b57cec5SDimitry Andric }
16740b57cec5SDimitry Andric
visitModuleFlags()1675349cc55cSDimitry Andric void Verifier::visitModuleFlags() {
16760b57cec5SDimitry Andric const NamedMDNode *Flags = M.getModuleFlagsMetadata();
16770b57cec5SDimitry Andric if (!Flags) return;
16780b57cec5SDimitry Andric
16790b57cec5SDimitry Andric // Scan each flag, and track the flags and requirements.
16800b57cec5SDimitry Andric DenseMap<const MDString*, const MDNode*> SeenIDs;
16810b57cec5SDimitry Andric SmallVector<const MDNode*, 16> Requirements;
16820b57cec5SDimitry Andric for (const MDNode *MDN : Flags->operands())
16830b57cec5SDimitry Andric visitModuleFlag(MDN, SeenIDs, Requirements);
16840b57cec5SDimitry Andric
16850b57cec5SDimitry Andric // Validate that the requirements in the module are valid.
16860b57cec5SDimitry Andric for (const MDNode *Requirement : Requirements) {
16870b57cec5SDimitry Andric const MDString *Flag = cast<MDString>(Requirement->getOperand(0));
16880b57cec5SDimitry Andric const Metadata *ReqValue = Requirement->getOperand(1);
16890b57cec5SDimitry Andric
16900b57cec5SDimitry Andric const MDNode *Op = SeenIDs.lookup(Flag);
16910b57cec5SDimitry Andric if (!Op) {
16920b57cec5SDimitry Andric CheckFailed("invalid requirement on flag, flag is not present in module",
16930b57cec5SDimitry Andric Flag);
16940b57cec5SDimitry Andric continue;
16950b57cec5SDimitry Andric }
16960b57cec5SDimitry Andric
16970b57cec5SDimitry Andric if (Op->getOperand(2) != ReqValue) {
16980b57cec5SDimitry Andric CheckFailed(("invalid requirement on flag, "
16990b57cec5SDimitry Andric "flag does not have the required value"),
17000b57cec5SDimitry Andric Flag);
17010b57cec5SDimitry Andric continue;
17020b57cec5SDimitry Andric }
17030b57cec5SDimitry Andric }
17040b57cec5SDimitry Andric }
17050b57cec5SDimitry Andric
17060b57cec5SDimitry Andric void
visitModuleFlag(const MDNode * Op,DenseMap<const MDString *,const MDNode * > & SeenIDs,SmallVectorImpl<const MDNode * > & Requirements)17070b57cec5SDimitry Andric Verifier::visitModuleFlag(const MDNode *Op,
17080b57cec5SDimitry Andric DenseMap<const MDString *, const MDNode *> &SeenIDs,
17090b57cec5SDimitry Andric SmallVectorImpl<const MDNode *> &Requirements) {
17100b57cec5SDimitry Andric // Each module flag should have three arguments, the merge behavior (a
17110b57cec5SDimitry Andric // constant int), the flag ID (an MDString), and the value.
171281ad6265SDimitry Andric Check(Op->getNumOperands() == 3,
17130b57cec5SDimitry Andric "incorrect number of operands in module flag", Op);
17140b57cec5SDimitry Andric Module::ModFlagBehavior MFB;
17150b57cec5SDimitry Andric if (!Module::isValidModFlagBehavior(Op->getOperand(0), MFB)) {
171681ad6265SDimitry Andric Check(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0)),
17170b57cec5SDimitry Andric "invalid behavior operand in module flag (expected constant integer)",
17180b57cec5SDimitry Andric Op->getOperand(0));
171981ad6265SDimitry Andric Check(false,
17200b57cec5SDimitry Andric "invalid behavior operand in module flag (unexpected constant)",
17210b57cec5SDimitry Andric Op->getOperand(0));
17220b57cec5SDimitry Andric }
17230b57cec5SDimitry Andric MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
172481ad6265SDimitry Andric Check(ID, "invalid ID operand in module flag (expected metadata string)",
17250b57cec5SDimitry Andric Op->getOperand(1));
17260b57cec5SDimitry Andric
17274824e7fdSDimitry Andric // Check the values for behaviors with additional requirements.
17280b57cec5SDimitry Andric switch (MFB) {
17290b57cec5SDimitry Andric case Module::Error:
17300b57cec5SDimitry Andric case Module::Warning:
17310b57cec5SDimitry Andric case Module::Override:
17320b57cec5SDimitry Andric // These behavior types accept any value.
17330b57cec5SDimitry Andric break;
17340b57cec5SDimitry Andric
173581ad6265SDimitry Andric case Module::Min: {
1736fcaf7f86SDimitry Andric auto *V = mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1737fcaf7f86SDimitry Andric Check(V && V->getValue().isNonNegative(),
1738fcaf7f86SDimitry Andric "invalid value for 'min' module flag (expected constant non-negative "
1739fcaf7f86SDimitry Andric "integer)",
174081ad6265SDimitry Andric Op->getOperand(2));
174181ad6265SDimitry Andric break;
174281ad6265SDimitry Andric }
174381ad6265SDimitry Andric
17440b57cec5SDimitry Andric case Module::Max: {
174581ad6265SDimitry Andric Check(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2)),
17460b57cec5SDimitry Andric "invalid value for 'max' module flag (expected constant integer)",
17470b57cec5SDimitry Andric Op->getOperand(2));
17480b57cec5SDimitry Andric break;
17490b57cec5SDimitry Andric }
17500b57cec5SDimitry Andric
17510b57cec5SDimitry Andric case Module::Require: {
17520b57cec5SDimitry Andric // The value should itself be an MDNode with two operands, a flag ID (an
17530b57cec5SDimitry Andric // MDString), and a value.
17540b57cec5SDimitry Andric MDNode *Value = dyn_cast<MDNode>(Op->getOperand(2));
175581ad6265SDimitry Andric Check(Value && Value->getNumOperands() == 2,
17560b57cec5SDimitry Andric "invalid value for 'require' module flag (expected metadata pair)",
17570b57cec5SDimitry Andric Op->getOperand(2));
175881ad6265SDimitry Andric Check(isa<MDString>(Value->getOperand(0)),
17590b57cec5SDimitry Andric ("invalid value for 'require' module flag "
17600b57cec5SDimitry Andric "(first value operand should be a string)"),
17610b57cec5SDimitry Andric Value->getOperand(0));
17620b57cec5SDimitry Andric
17630b57cec5SDimitry Andric // Append it to the list of requirements, to check once all module flags are
17640b57cec5SDimitry Andric // scanned.
17650b57cec5SDimitry Andric Requirements.push_back(Value);
17660b57cec5SDimitry Andric break;
17670b57cec5SDimitry Andric }
17680b57cec5SDimitry Andric
17690b57cec5SDimitry Andric case Module::Append:
17700b57cec5SDimitry Andric case Module::AppendUnique: {
17710b57cec5SDimitry Andric // These behavior types require the operand be an MDNode.
177281ad6265SDimitry Andric Check(isa<MDNode>(Op->getOperand(2)),
17730b57cec5SDimitry Andric "invalid value for 'append'-type module flag "
17740b57cec5SDimitry Andric "(expected a metadata node)",
17750b57cec5SDimitry Andric Op->getOperand(2));
17760b57cec5SDimitry Andric break;
17770b57cec5SDimitry Andric }
17780b57cec5SDimitry Andric }
17790b57cec5SDimitry Andric
17800b57cec5SDimitry Andric // Unless this is a "requires" flag, check the ID is unique.
17810b57cec5SDimitry Andric if (MFB != Module::Require) {
17820b57cec5SDimitry Andric bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
178381ad6265SDimitry Andric Check(Inserted,
17840b57cec5SDimitry Andric "module flag identifiers must be unique (or of 'require' type)", ID);
17850b57cec5SDimitry Andric }
17860b57cec5SDimitry Andric
17870b57cec5SDimitry Andric if (ID->getString() == "wchar_size") {
17880b57cec5SDimitry Andric ConstantInt *Value
17890b57cec5SDimitry Andric = mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
179081ad6265SDimitry Andric Check(Value, "wchar_size metadata requires constant integer argument");
17910b57cec5SDimitry Andric }
17920b57cec5SDimitry Andric
17930b57cec5SDimitry Andric if (ID->getString() == "Linker Options") {
17940b57cec5SDimitry Andric // If the llvm.linker.options named metadata exists, we assume that the
17950b57cec5SDimitry Andric // bitcode reader has upgraded the module flag. Otherwise the flag might
17960b57cec5SDimitry Andric // have been created by a client directly.
179781ad6265SDimitry Andric Check(M.getNamedMetadata("llvm.linker.options"),
17980b57cec5SDimitry Andric "'Linker Options' named metadata no longer supported");
17990b57cec5SDimitry Andric }
18000b57cec5SDimitry Andric
18015ffd83dbSDimitry Andric if (ID->getString() == "SemanticInterposition") {
18025ffd83dbSDimitry Andric ConstantInt *Value =
18035ffd83dbSDimitry Andric mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
180481ad6265SDimitry Andric Check(Value,
18055ffd83dbSDimitry Andric "SemanticInterposition metadata requires constant integer argument");
18065ffd83dbSDimitry Andric }
18075ffd83dbSDimitry Andric
18080b57cec5SDimitry Andric if (ID->getString() == "CG Profile") {
18090b57cec5SDimitry Andric for (const MDOperand &MDO : cast<MDNode>(Op->getOperand(2))->operands())
18100b57cec5SDimitry Andric visitModuleFlagCGProfileEntry(MDO);
18110b57cec5SDimitry Andric }
18120b57cec5SDimitry Andric }
18130b57cec5SDimitry Andric
visitModuleFlagCGProfileEntry(const MDOperand & MDO)18140b57cec5SDimitry Andric void Verifier::visitModuleFlagCGProfileEntry(const MDOperand &MDO) {
18150b57cec5SDimitry Andric auto CheckFunction = [&](const MDOperand &FuncMDO) {
18160b57cec5SDimitry Andric if (!FuncMDO)
18170b57cec5SDimitry Andric return;
18180b57cec5SDimitry Andric auto F = dyn_cast<ValueAsMetadata>(FuncMDO);
181981ad6265SDimitry Andric Check(F && isa<Function>(F->getValue()->stripPointerCasts()),
1820e8d8bef9SDimitry Andric "expected a Function or null", FuncMDO);
18210b57cec5SDimitry Andric };
18220b57cec5SDimitry Andric auto Node = dyn_cast_or_null<MDNode>(MDO);
182381ad6265SDimitry Andric Check(Node && Node->getNumOperands() == 3, "expected a MDNode triple", MDO);
18240b57cec5SDimitry Andric CheckFunction(Node->getOperand(0));
18250b57cec5SDimitry Andric CheckFunction(Node->getOperand(1));
18260b57cec5SDimitry Andric auto Count = dyn_cast_or_null<ConstantAsMetadata>(Node->getOperand(2));
182781ad6265SDimitry Andric Check(Count && Count->getType()->isIntegerTy(),
18280b57cec5SDimitry Andric "expected an integer constant", Node->getOperand(2));
18290b57cec5SDimitry Andric }
18300b57cec5SDimitry Andric
verifyAttributeTypes(AttributeSet Attrs,const Value * V)1831fe6060f1SDimitry Andric void Verifier::verifyAttributeTypes(AttributeSet Attrs, const Value *V) {
18320b57cec5SDimitry Andric for (Attribute A : Attrs) {
1833fe6060f1SDimitry Andric
1834fe6060f1SDimitry Andric if (A.isStringAttribute()) {
1835fe6060f1SDimitry Andric #define GET_ATTR_NAMES
1836fe6060f1SDimitry Andric #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME)
1837fe6060f1SDimitry Andric #define ATTRIBUTE_STRBOOL(ENUM_NAME, DISPLAY_NAME) \
1838fe6060f1SDimitry Andric if (A.getKindAsString() == #DISPLAY_NAME) { \
1839fe6060f1SDimitry Andric auto V = A.getValueAsString(); \
1840fe6060f1SDimitry Andric if (!(V.empty() || V == "true" || V == "false")) \
1841fe6060f1SDimitry Andric CheckFailed("invalid value for '" #DISPLAY_NAME "' attribute: " + V + \
1842fe6060f1SDimitry Andric ""); \
1843fe6060f1SDimitry Andric }
1844fe6060f1SDimitry Andric
1845fe6060f1SDimitry Andric #include "llvm/IR/Attributes.inc"
18460b57cec5SDimitry Andric continue;
1847fe6060f1SDimitry Andric }
18480b57cec5SDimitry Andric
1849fe6060f1SDimitry Andric if (A.isIntAttribute() != Attribute::isIntAttrKind(A.getKindAsEnum())) {
18505ffd83dbSDimitry Andric CheckFailed("Attribute '" + A.getAsString() + "' should have an Argument",
18515ffd83dbSDimitry Andric V);
18525ffd83dbSDimitry Andric return;
18535ffd83dbSDimitry Andric }
18540b57cec5SDimitry Andric }
18550b57cec5SDimitry Andric }
18560b57cec5SDimitry Andric
18570b57cec5SDimitry Andric // VerifyParameterAttrs - Check the given attributes for an argument or return
18580b57cec5SDimitry Andric // value of the specified type. The value V is printed in error messages.
verifyParameterAttrs(AttributeSet Attrs,Type * Ty,const Value * V)18590b57cec5SDimitry Andric void Verifier::verifyParameterAttrs(AttributeSet Attrs, Type *Ty,
18600b57cec5SDimitry Andric const Value *V) {
18610b57cec5SDimitry Andric if (!Attrs.hasAttributes())
18620b57cec5SDimitry Andric return;
18630b57cec5SDimitry Andric
1864fe6060f1SDimitry Andric verifyAttributeTypes(Attrs, V);
1865fe6060f1SDimitry Andric
1866fe6060f1SDimitry Andric for (Attribute Attr : Attrs)
186781ad6265SDimitry Andric Check(Attr.isStringAttribute() ||
1868fe6060f1SDimitry Andric Attribute::canUseAsParamAttr(Attr.getKindAsEnum()),
186981ad6265SDimitry Andric "Attribute '" + Attr.getAsString() + "' does not apply to parameters",
1870fe6060f1SDimitry Andric V);
18710b57cec5SDimitry Andric
18720b57cec5SDimitry Andric if (Attrs.hasAttribute(Attribute::ImmArg)) {
187381ad6265SDimitry Andric Check(Attrs.getNumAttributes() == 1,
18740b57cec5SDimitry Andric "Attribute 'immarg' is incompatible with other attributes", V);
18750b57cec5SDimitry Andric }
18760b57cec5SDimitry Andric
18770b57cec5SDimitry Andric // Check for mutually incompatible attributes. Only inreg is compatible with
18780b57cec5SDimitry Andric // sret.
18790b57cec5SDimitry Andric unsigned AttrCount = 0;
18800b57cec5SDimitry Andric AttrCount += Attrs.hasAttribute(Attribute::ByVal);
18810b57cec5SDimitry Andric AttrCount += Attrs.hasAttribute(Attribute::InAlloca);
18825ffd83dbSDimitry Andric AttrCount += Attrs.hasAttribute(Attribute::Preallocated);
18830b57cec5SDimitry Andric AttrCount += Attrs.hasAttribute(Attribute::StructRet) ||
18840b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::InReg);
18850b57cec5SDimitry Andric AttrCount += Attrs.hasAttribute(Attribute::Nest);
1886e8d8bef9SDimitry Andric AttrCount += Attrs.hasAttribute(Attribute::ByRef);
188781ad6265SDimitry Andric Check(AttrCount <= 1,
18885ffd83dbSDimitry Andric "Attributes 'byval', 'inalloca', 'preallocated', 'inreg', 'nest', "
1889e8d8bef9SDimitry Andric "'byref', and 'sret' are incompatible!",
18900b57cec5SDimitry Andric V);
18910b57cec5SDimitry Andric
189281ad6265SDimitry Andric Check(!(Attrs.hasAttribute(Attribute::InAlloca) &&
18930b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::ReadOnly)),
18940b57cec5SDimitry Andric "Attributes "
18950b57cec5SDimitry Andric "'inalloca and readonly' are incompatible!",
18960b57cec5SDimitry Andric V);
18970b57cec5SDimitry Andric
189881ad6265SDimitry Andric Check(!(Attrs.hasAttribute(Attribute::StructRet) &&
18990b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::Returned)),
19000b57cec5SDimitry Andric "Attributes "
19010b57cec5SDimitry Andric "'sret and returned' are incompatible!",
19020b57cec5SDimitry Andric V);
19030b57cec5SDimitry Andric
190481ad6265SDimitry Andric Check(!(Attrs.hasAttribute(Attribute::ZExt) &&
19050b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::SExt)),
19060b57cec5SDimitry Andric "Attributes "
19070b57cec5SDimitry Andric "'zeroext and signext' are incompatible!",
19080b57cec5SDimitry Andric V);
19090b57cec5SDimitry Andric
191081ad6265SDimitry Andric Check(!(Attrs.hasAttribute(Attribute::ReadNone) &&
19110b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::ReadOnly)),
19120b57cec5SDimitry Andric "Attributes "
19130b57cec5SDimitry Andric "'readnone and readonly' are incompatible!",
19140b57cec5SDimitry Andric V);
19150b57cec5SDimitry Andric
191681ad6265SDimitry Andric Check(!(Attrs.hasAttribute(Attribute::ReadNone) &&
19170b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::WriteOnly)),
19180b57cec5SDimitry Andric "Attributes "
19190b57cec5SDimitry Andric "'readnone and writeonly' are incompatible!",
19200b57cec5SDimitry Andric V);
19210b57cec5SDimitry Andric
192281ad6265SDimitry Andric Check(!(Attrs.hasAttribute(Attribute::ReadOnly) &&
19230b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::WriteOnly)),
19240b57cec5SDimitry Andric "Attributes "
19250b57cec5SDimitry Andric "'readonly and writeonly' are incompatible!",
19260b57cec5SDimitry Andric V);
19270b57cec5SDimitry Andric
192881ad6265SDimitry Andric Check(!(Attrs.hasAttribute(Attribute::NoInline) &&
19290b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::AlwaysInline)),
19300b57cec5SDimitry Andric "Attributes "
19310b57cec5SDimitry Andric "'noinline and alwaysinline' are incompatible!",
19320b57cec5SDimitry Andric V);
19330b57cec5SDimitry Andric
19345f757f3fSDimitry Andric Check(!(Attrs.hasAttribute(Attribute::Writable) &&
19355f757f3fSDimitry Andric Attrs.hasAttribute(Attribute::ReadNone)),
19365f757f3fSDimitry Andric "Attributes writable and readnone are incompatible!", V);
19375f757f3fSDimitry Andric
19385f757f3fSDimitry Andric Check(!(Attrs.hasAttribute(Attribute::Writable) &&
19395f757f3fSDimitry Andric Attrs.hasAttribute(Attribute::ReadOnly)),
19405f757f3fSDimitry Andric "Attributes writable and readonly are incompatible!", V);
19415f757f3fSDimitry Andric
194204eeddc0SDimitry Andric AttributeMask IncompatibleAttrs = AttributeFuncs::typeIncompatible(Ty);
1943fe6060f1SDimitry Andric for (Attribute Attr : Attrs) {
1944fe6060f1SDimitry Andric if (!Attr.isStringAttribute() &&
1945fe6060f1SDimitry Andric IncompatibleAttrs.contains(Attr.getKindAsEnum())) {
1946fe6060f1SDimitry Andric CheckFailed("Attribute '" + Attr.getAsString() +
1947fe6060f1SDimitry Andric "' applied to incompatible type!", V);
1948fe6060f1SDimitry Andric return;
1949fe6060f1SDimitry Andric }
1950fe6060f1SDimitry Andric }
19510b57cec5SDimitry Andric
195206c3fb27SDimitry Andric if (isa<PointerType>(Ty)) {
1953fe6060f1SDimitry Andric if (Attrs.hasAttribute(Attribute::ByVal)) {
195481ad6265SDimitry Andric if (Attrs.hasAttribute(Attribute::Alignment)) {
195581ad6265SDimitry Andric Align AttrAlign = Attrs.getAlignment().valueOrOne();
195681ad6265SDimitry Andric Align MaxAlign(ParamMaxAlignment);
195781ad6265SDimitry Andric Check(AttrAlign <= MaxAlign,
195881ad6265SDimitry Andric "Attribute 'align' exceed the max size 2^14", V);
195981ad6265SDimitry Andric }
19600b57cec5SDimitry Andric SmallPtrSet<Type *, 4> Visited;
196181ad6265SDimitry Andric Check(Attrs.getByValType()->isSized(&Visited),
1962fe6060f1SDimitry Andric "Attribute 'byval' does not support unsized types!", V);
19630b57cec5SDimitry Andric }
1964fe6060f1SDimitry Andric if (Attrs.hasAttribute(Attribute::ByRef)) {
1965fe6060f1SDimitry Andric SmallPtrSet<Type *, 4> Visited;
196681ad6265SDimitry Andric Check(Attrs.getByRefType()->isSized(&Visited),
1967fe6060f1SDimitry Andric "Attribute 'byref' does not support unsized types!", V);
1968fe6060f1SDimitry Andric }
1969fe6060f1SDimitry Andric if (Attrs.hasAttribute(Attribute::InAlloca)) {
1970fe6060f1SDimitry Andric SmallPtrSet<Type *, 4> Visited;
197181ad6265SDimitry Andric Check(Attrs.getInAllocaType()->isSized(&Visited),
1972fe6060f1SDimitry Andric "Attribute 'inalloca' does not support unsized types!", V);
1973fe6060f1SDimitry Andric }
1974fe6060f1SDimitry Andric if (Attrs.hasAttribute(Attribute::Preallocated)) {
1975fe6060f1SDimitry Andric SmallPtrSet<Type *, 4> Visited;
197681ad6265SDimitry Andric Check(Attrs.getPreallocatedType()->isSized(&Visited),
1977fe6060f1SDimitry Andric "Attribute 'preallocated' does not support unsized types!", V);
1978fe6060f1SDimitry Andric }
197906c3fb27SDimitry Andric }
198006c3fb27SDimitry Andric
198106c3fb27SDimitry Andric if (Attrs.hasAttribute(Attribute::NoFPClass)) {
198206c3fb27SDimitry Andric uint64_t Val = Attrs.getAttribute(Attribute::NoFPClass).getValueAsInt();
198306c3fb27SDimitry Andric Check(Val != 0, "Attribute 'nofpclass' must have at least one test bit set",
19840b57cec5SDimitry Andric V);
198506c3fb27SDimitry Andric Check((Val & ~static_cast<unsigned>(fcAllFlags)) == 0,
198606c3fb27SDimitry Andric "Invalid value for 'nofpclass' test mask", V);
1987fe6060f1SDimitry Andric }
1988fe6060f1SDimitry Andric }
1989fe6060f1SDimitry Andric
checkUnsignedBaseTenFuncAttr(AttributeList Attrs,StringRef Attr,const Value * V)1990fe6060f1SDimitry Andric void Verifier::checkUnsignedBaseTenFuncAttr(AttributeList Attrs, StringRef Attr,
1991fe6060f1SDimitry Andric const Value *V) {
1992349cc55cSDimitry Andric if (Attrs.hasFnAttr(Attr)) {
1993349cc55cSDimitry Andric StringRef S = Attrs.getFnAttr(Attr).getValueAsString();
1994fe6060f1SDimitry Andric unsigned N;
1995fe6060f1SDimitry Andric if (S.getAsInteger(10, N))
1996fe6060f1SDimitry Andric CheckFailed("\"" + Attr + "\" takes an unsigned integer: " + S, V);
19970b57cec5SDimitry Andric }
19980b57cec5SDimitry Andric }
19990b57cec5SDimitry Andric
20000b57cec5SDimitry Andric // Check parameter attributes against a function type.
20010b57cec5SDimitry Andric // The value V is printed in error messages.
verifyFunctionAttrs(FunctionType * FT,AttributeList Attrs,const Value * V,bool IsIntrinsic,bool IsInlineAsm)20020b57cec5SDimitry Andric void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
200304eeddc0SDimitry Andric const Value *V, bool IsIntrinsic,
200404eeddc0SDimitry Andric bool IsInlineAsm) {
20050b57cec5SDimitry Andric if (Attrs.isEmpty())
20060b57cec5SDimitry Andric return;
20070b57cec5SDimitry Andric
2008fe6060f1SDimitry Andric if (AttributeListsVisited.insert(Attrs.getRawPointer()).second) {
200981ad6265SDimitry Andric Check(Attrs.hasParentContext(Context),
2010fe6060f1SDimitry Andric "Attribute list does not match Module context!", &Attrs, V);
2011fe6060f1SDimitry Andric for (const auto &AttrSet : Attrs) {
201281ad6265SDimitry Andric Check(!AttrSet.hasAttributes() || AttrSet.hasParentContext(Context),
2013fe6060f1SDimitry Andric "Attribute set does not match Module context!", &AttrSet, V);
2014fe6060f1SDimitry Andric for (const auto &A : AttrSet) {
201581ad6265SDimitry Andric Check(A.hasParentContext(Context),
2016fe6060f1SDimitry Andric "Attribute does not match Module context!", &A, V);
2017fe6060f1SDimitry Andric }
2018fe6060f1SDimitry Andric }
2019fe6060f1SDimitry Andric }
2020fe6060f1SDimitry Andric
20210b57cec5SDimitry Andric bool SawNest = false;
20220b57cec5SDimitry Andric bool SawReturned = false;
20230b57cec5SDimitry Andric bool SawSRet = false;
20240b57cec5SDimitry Andric bool SawSwiftSelf = false;
2025fe6060f1SDimitry Andric bool SawSwiftAsync = false;
20260b57cec5SDimitry Andric bool SawSwiftError = false;
20270b57cec5SDimitry Andric
20280b57cec5SDimitry Andric // Verify return value attributes.
2029349cc55cSDimitry Andric AttributeSet RetAttrs = Attrs.getRetAttrs();
2030fe6060f1SDimitry Andric for (Attribute RetAttr : RetAttrs)
203181ad6265SDimitry Andric Check(RetAttr.isStringAttribute() ||
2032fe6060f1SDimitry Andric Attribute::canUseAsRetAttr(RetAttr.getKindAsEnum()),
2033fe6060f1SDimitry Andric "Attribute '" + RetAttr.getAsString() +
2034fe6060f1SDimitry Andric "' does not apply to function return values",
20350b57cec5SDimitry Andric V);
2036fe6060f1SDimitry Andric
20375f757f3fSDimitry Andric unsigned MaxParameterWidth = 0;
20385f757f3fSDimitry Andric auto GetMaxParameterWidth = [&MaxParameterWidth](Type *Ty) {
20395f757f3fSDimitry Andric if (Ty->isVectorTy()) {
20405f757f3fSDimitry Andric if (auto *VT = dyn_cast<FixedVectorType>(Ty)) {
20415f757f3fSDimitry Andric unsigned Size = VT->getPrimitiveSizeInBits().getFixedValue();
20425f757f3fSDimitry Andric if (Size > MaxParameterWidth)
20435f757f3fSDimitry Andric MaxParameterWidth = Size;
20445f757f3fSDimitry Andric }
20455f757f3fSDimitry Andric }
20465f757f3fSDimitry Andric };
20475f757f3fSDimitry Andric GetMaxParameterWidth(FT->getReturnType());
20480b57cec5SDimitry Andric verifyParameterAttrs(RetAttrs, FT->getReturnType(), V);
20490b57cec5SDimitry Andric
20500b57cec5SDimitry Andric // Verify parameter attributes.
20510b57cec5SDimitry Andric for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
20520b57cec5SDimitry Andric Type *Ty = FT->getParamType(i);
2053349cc55cSDimitry Andric AttributeSet ArgAttrs = Attrs.getParamAttrs(i);
20540b57cec5SDimitry Andric
20550b57cec5SDimitry Andric if (!IsIntrinsic) {
205681ad6265SDimitry Andric Check(!ArgAttrs.hasAttribute(Attribute::ImmArg),
20570b57cec5SDimitry Andric "immarg attribute only applies to intrinsics", V);
205804eeddc0SDimitry Andric if (!IsInlineAsm)
205981ad6265SDimitry Andric Check(!ArgAttrs.hasAttribute(Attribute::ElementType),
206004eeddc0SDimitry Andric "Attribute 'elementtype' can only be applied to intrinsics"
206181ad6265SDimitry Andric " and inline asm.",
206281ad6265SDimitry Andric V);
20630b57cec5SDimitry Andric }
20640b57cec5SDimitry Andric
20650b57cec5SDimitry Andric verifyParameterAttrs(ArgAttrs, Ty, V);
20665f757f3fSDimitry Andric GetMaxParameterWidth(Ty);
20670b57cec5SDimitry Andric
20680b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::Nest)) {
206981ad6265SDimitry Andric Check(!SawNest, "More than one parameter has attribute nest!", V);
20700b57cec5SDimitry Andric SawNest = true;
20710b57cec5SDimitry Andric }
20720b57cec5SDimitry Andric
20730b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::Returned)) {
207481ad6265SDimitry Andric Check(!SawReturned, "More than one parameter has attribute returned!", V);
207581ad6265SDimitry Andric Check(Ty->canLosslesslyBitCastTo(FT->getReturnType()),
20760b57cec5SDimitry Andric "Incompatible argument and return types for 'returned' attribute",
20770b57cec5SDimitry Andric V);
20780b57cec5SDimitry Andric SawReturned = true;
20790b57cec5SDimitry Andric }
20800b57cec5SDimitry Andric
20810b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::StructRet)) {
208281ad6265SDimitry Andric Check(!SawSRet, "Cannot have multiple 'sret' parameters!", V);
208381ad6265SDimitry Andric Check(i == 0 || i == 1,
20840b57cec5SDimitry Andric "Attribute 'sret' is not on first or second parameter!", V);
20850b57cec5SDimitry Andric SawSRet = true;
20860b57cec5SDimitry Andric }
20870b57cec5SDimitry Andric
20880b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::SwiftSelf)) {
208981ad6265SDimitry Andric Check(!SawSwiftSelf, "Cannot have multiple 'swiftself' parameters!", V);
20900b57cec5SDimitry Andric SawSwiftSelf = true;
20910b57cec5SDimitry Andric }
20920b57cec5SDimitry Andric
2093fe6060f1SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::SwiftAsync)) {
209481ad6265SDimitry Andric Check(!SawSwiftAsync, "Cannot have multiple 'swiftasync' parameters!", V);
2095fe6060f1SDimitry Andric SawSwiftAsync = true;
2096fe6060f1SDimitry Andric }
2097fe6060f1SDimitry Andric
20980b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::SwiftError)) {
209981ad6265SDimitry Andric Check(!SawSwiftError, "Cannot have multiple 'swifterror' parameters!", V);
21000b57cec5SDimitry Andric SawSwiftError = true;
21010b57cec5SDimitry Andric }
21020b57cec5SDimitry Andric
21030b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::InAlloca)) {
210481ad6265SDimitry Andric Check(i == FT->getNumParams() - 1,
21050b57cec5SDimitry Andric "inalloca isn't on the last parameter!", V);
21060b57cec5SDimitry Andric }
21070b57cec5SDimitry Andric }
21080b57cec5SDimitry Andric
2109349cc55cSDimitry Andric if (!Attrs.hasFnAttrs())
21100b57cec5SDimitry Andric return;
21110b57cec5SDimitry Andric
2112349cc55cSDimitry Andric verifyAttributeTypes(Attrs.getFnAttrs(), V);
2113349cc55cSDimitry Andric for (Attribute FnAttr : Attrs.getFnAttrs())
211481ad6265SDimitry Andric Check(FnAttr.isStringAttribute() ||
2115fe6060f1SDimitry Andric Attribute::canUseAsFnAttr(FnAttr.getKindAsEnum()),
2116fe6060f1SDimitry Andric "Attribute '" + FnAttr.getAsString() +
2117fe6060f1SDimitry Andric "' does not apply to functions!",
2118fe6060f1SDimitry Andric V);
21190b57cec5SDimitry Andric
212081ad6265SDimitry Andric Check(!(Attrs.hasFnAttr(Attribute::NoInline) &&
2121349cc55cSDimitry Andric Attrs.hasFnAttr(Attribute::AlwaysInline)),
21220b57cec5SDimitry Andric "Attributes 'noinline and alwaysinline' are incompatible!", V);
21230b57cec5SDimitry Andric
2124349cc55cSDimitry Andric if (Attrs.hasFnAttr(Attribute::OptimizeNone)) {
212581ad6265SDimitry Andric Check(Attrs.hasFnAttr(Attribute::NoInline),
21260b57cec5SDimitry Andric "Attribute 'optnone' requires 'noinline'!", V);
21270b57cec5SDimitry Andric
212881ad6265SDimitry Andric Check(!Attrs.hasFnAttr(Attribute::OptimizeForSize),
21290b57cec5SDimitry Andric "Attributes 'optsize and optnone' are incompatible!", V);
21300b57cec5SDimitry Andric
213181ad6265SDimitry Andric Check(!Attrs.hasFnAttr(Attribute::MinSize),
21320b57cec5SDimitry Andric "Attributes 'minsize and optnone' are incompatible!", V);
21335f757f3fSDimitry Andric
21345f757f3fSDimitry Andric Check(!Attrs.hasFnAttr(Attribute::OptimizeForDebugging),
21355f757f3fSDimitry Andric "Attributes 'optdebug and optnone' are incompatible!", V);
21360b57cec5SDimitry Andric }
21370b57cec5SDimitry Andric
21385f757f3fSDimitry Andric if (Attrs.hasFnAttr(Attribute::OptimizeForDebugging)) {
21395f757f3fSDimitry Andric Check(!Attrs.hasFnAttr(Attribute::OptimizeForSize),
21405f757f3fSDimitry Andric "Attributes 'optsize and optdebug' are incompatible!", V);
21415f757f3fSDimitry Andric
21425f757f3fSDimitry Andric Check(!Attrs.hasFnAttr(Attribute::MinSize),
21435f757f3fSDimitry Andric "Attributes 'minsize and optdebug' are incompatible!", V);
21445f757f3fSDimitry Andric }
21455f757f3fSDimitry Andric
21465f757f3fSDimitry Andric Check(!Attrs.hasAttrSomewhere(Attribute::Writable) ||
21475f757f3fSDimitry Andric isModSet(Attrs.getMemoryEffects().getModRef(IRMemLocation::ArgMem)),
21485f757f3fSDimitry Andric "Attribute writable and memory without argmem: write are incompatible!",
21495f757f3fSDimitry Andric V);
21505f757f3fSDimitry Andric
2151bdd1243dSDimitry Andric if (Attrs.hasFnAttr("aarch64_pstate_sm_enabled")) {
2152bdd1243dSDimitry Andric Check(!Attrs.hasFnAttr("aarch64_pstate_sm_compatible"),
2153bdd1243dSDimitry Andric "Attributes 'aarch64_pstate_sm_enabled and "
2154bdd1243dSDimitry Andric "aarch64_pstate_sm_compatible' are incompatible!",
2155bdd1243dSDimitry Andric V);
2156bdd1243dSDimitry Andric }
2157bdd1243dSDimitry Andric
2158bdd1243dSDimitry Andric if (Attrs.hasFnAttr("aarch64_pstate_za_new")) {
2159bdd1243dSDimitry Andric Check(!Attrs.hasFnAttr("aarch64_pstate_za_preserved"),
2160bdd1243dSDimitry Andric "Attributes 'aarch64_pstate_za_new and aarch64_pstate_za_preserved' "
2161bdd1243dSDimitry Andric "are incompatible!",
2162bdd1243dSDimitry Andric V);
2163bdd1243dSDimitry Andric
2164bdd1243dSDimitry Andric Check(!Attrs.hasFnAttr("aarch64_pstate_za_shared"),
2165bdd1243dSDimitry Andric "Attributes 'aarch64_pstate_za_new and aarch64_pstate_za_shared' "
2166bdd1243dSDimitry Andric "are incompatible!",
2167bdd1243dSDimitry Andric V);
2168bdd1243dSDimitry Andric }
2169bdd1243dSDimitry Andric
21707a6dacacSDimitry Andric Check(
21717a6dacacSDimitry Andric (Attrs.hasFnAttr("aarch64_new_zt0") + Attrs.hasFnAttr("aarch64_in_zt0") +
21727a6dacacSDimitry Andric Attrs.hasFnAttr("aarch64_inout_zt0") +
21737a6dacacSDimitry Andric Attrs.hasFnAttr("aarch64_out_zt0") +
21747a6dacacSDimitry Andric Attrs.hasFnAttr("aarch64_preserves_zt0")) <= 1,
21757a6dacacSDimitry Andric "Attributes 'aarch64_new_zt0', 'aarch64_in_zt0', 'aarch64_out_zt0', "
21767a6dacacSDimitry Andric "'aarch64_inout_zt0' and 'aarch64_preserves_zt0' are mutually exclusive",
21777a6dacacSDimitry Andric V);
21787a6dacacSDimitry Andric
2179349cc55cSDimitry Andric if (Attrs.hasFnAttr(Attribute::JumpTable)) {
21800b57cec5SDimitry Andric const GlobalValue *GV = cast<GlobalValue>(V);
218181ad6265SDimitry Andric Check(GV->hasGlobalUnnamedAddr(),
21820b57cec5SDimitry Andric "Attribute 'jumptable' requires 'unnamed_addr'", V);
21830b57cec5SDimitry Andric }
21840b57cec5SDimitry Andric
2185bdd1243dSDimitry Andric if (auto Args = Attrs.getFnAttrs().getAllocSizeArgs()) {
21860b57cec5SDimitry Andric auto CheckParam = [&](StringRef Name, unsigned ParamNo) {
21870b57cec5SDimitry Andric if (ParamNo >= FT->getNumParams()) {
21880b57cec5SDimitry Andric CheckFailed("'allocsize' " + Name + " argument is out of bounds", V);
21890b57cec5SDimitry Andric return false;
21900b57cec5SDimitry Andric }
21910b57cec5SDimitry Andric
21920b57cec5SDimitry Andric if (!FT->getParamType(ParamNo)->isIntegerTy()) {
21930b57cec5SDimitry Andric CheckFailed("'allocsize' " + Name +
21940b57cec5SDimitry Andric " argument must refer to an integer parameter",
21950b57cec5SDimitry Andric V);
21960b57cec5SDimitry Andric return false;
21970b57cec5SDimitry Andric }
21980b57cec5SDimitry Andric
21990b57cec5SDimitry Andric return true;
22000b57cec5SDimitry Andric };
22010b57cec5SDimitry Andric
2202bdd1243dSDimitry Andric if (!CheckParam("element size", Args->first))
22030b57cec5SDimitry Andric return;
22040b57cec5SDimitry Andric
2205bdd1243dSDimitry Andric if (Args->second && !CheckParam("number of elements", *Args->second))
22060b57cec5SDimitry Andric return;
22070b57cec5SDimitry Andric }
2208480093f4SDimitry Andric
220981ad6265SDimitry Andric if (Attrs.hasFnAttr(Attribute::AllocKind)) {
221081ad6265SDimitry Andric AllocFnKind K = Attrs.getAllocKind();
221181ad6265SDimitry Andric AllocFnKind Type =
221281ad6265SDimitry Andric K & (AllocFnKind::Alloc | AllocFnKind::Realloc | AllocFnKind::Free);
221381ad6265SDimitry Andric if (!is_contained(
221481ad6265SDimitry Andric {AllocFnKind::Alloc, AllocFnKind::Realloc, AllocFnKind::Free},
221581ad6265SDimitry Andric Type))
221681ad6265SDimitry Andric CheckFailed(
221781ad6265SDimitry Andric "'allockind()' requires exactly one of alloc, realloc, and free");
221881ad6265SDimitry Andric if ((Type == AllocFnKind::Free) &&
221981ad6265SDimitry Andric ((K & (AllocFnKind::Uninitialized | AllocFnKind::Zeroed |
222081ad6265SDimitry Andric AllocFnKind::Aligned)) != AllocFnKind::Unknown))
222181ad6265SDimitry Andric CheckFailed("'allockind(\"free\")' doesn't allow uninitialized, zeroed, "
222281ad6265SDimitry Andric "or aligned modifiers.");
222381ad6265SDimitry Andric AllocFnKind ZeroedUninit = AllocFnKind::Uninitialized | AllocFnKind::Zeroed;
222481ad6265SDimitry Andric if ((K & ZeroedUninit) == ZeroedUninit)
222581ad6265SDimitry Andric CheckFailed("'allockind()' can't be both zeroed and uninitialized");
222681ad6265SDimitry Andric }
222781ad6265SDimitry Andric
2228349cc55cSDimitry Andric if (Attrs.hasFnAttr(Attribute::VScaleRange)) {
22290eae32dcSDimitry Andric unsigned VScaleMin = Attrs.getFnAttrs().getVScaleRangeMin();
22300eae32dcSDimitry Andric if (VScaleMin == 0)
22310eae32dcSDimitry Andric CheckFailed("'vscale_range' minimum must be greater than 0", V);
223206c3fb27SDimitry Andric else if (!isPowerOf2_32(VScaleMin))
223306c3fb27SDimitry Andric CheckFailed("'vscale_range' minimum must be power-of-two value", V);
2234bdd1243dSDimitry Andric std::optional<unsigned> VScaleMax = Attrs.getFnAttrs().getVScaleRangeMax();
22350eae32dcSDimitry Andric if (VScaleMax && VScaleMin > VScaleMax)
2236fe6060f1SDimitry Andric CheckFailed("'vscale_range' minimum cannot be greater than maximum", V);
223706c3fb27SDimitry Andric else if (VScaleMax && !isPowerOf2_32(*VScaleMax))
223806c3fb27SDimitry Andric CheckFailed("'vscale_range' maximum must be power-of-two value", V);
2239fe6060f1SDimitry Andric }
2240fe6060f1SDimitry Andric
2241349cc55cSDimitry Andric if (Attrs.hasFnAttr("frame-pointer")) {
2242349cc55cSDimitry Andric StringRef FP = Attrs.getFnAttr("frame-pointer").getValueAsString();
2243480093f4SDimitry Andric if (FP != "all" && FP != "non-leaf" && FP != "none")
2244480093f4SDimitry Andric CheckFailed("invalid value for 'frame-pointer' attribute: " + FP, V);
2245480093f4SDimitry Andric }
2246480093f4SDimitry Andric
22475f757f3fSDimitry Andric // Check EVEX512 feature.
22485f757f3fSDimitry Andric if (MaxParameterWidth >= 512 && Attrs.hasFnAttr("target-features") &&
22495f757f3fSDimitry Andric TT.isX86()) {
22505f757f3fSDimitry Andric StringRef TF = Attrs.getFnAttr("target-features").getValueAsString();
22515f757f3fSDimitry Andric Check(!TF.contains("+avx512f") || !TF.contains("-evex512"),
22525f757f3fSDimitry Andric "512-bit vector arguments require 'evex512' for AVX512", V);
22535f757f3fSDimitry Andric }
22545f757f3fSDimitry Andric
2255fe6060f1SDimitry Andric checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-prefix", V);
2256fe6060f1SDimitry Andric checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-entry", V);
2257fe6060f1SDimitry Andric checkUnsignedBaseTenFuncAttr(Attrs, "warn-stack-size", V);
22585f757f3fSDimitry Andric
22595f757f3fSDimitry Andric if (auto A = Attrs.getFnAttr("sign-return-address"); A.isValid()) {
22605f757f3fSDimitry Andric StringRef S = A.getValueAsString();
22615f757f3fSDimitry Andric if (S != "none" && S != "all" && S != "non-leaf")
22625f757f3fSDimitry Andric CheckFailed("invalid value for 'sign-return-address' attribute: " + S, V);
22635f757f3fSDimitry Andric }
22645f757f3fSDimitry Andric
22655f757f3fSDimitry Andric if (auto A = Attrs.getFnAttr("sign-return-address-key"); A.isValid()) {
22665f757f3fSDimitry Andric StringRef S = A.getValueAsString();
22675f757f3fSDimitry Andric if (S != "a_key" && S != "b_key")
22685f757f3fSDimitry Andric CheckFailed("invalid value for 'sign-return-address-key' attribute: " + S,
22695f757f3fSDimitry Andric V);
22705f757f3fSDimitry Andric }
22715f757f3fSDimitry Andric
22725f757f3fSDimitry Andric if (auto A = Attrs.getFnAttr("branch-target-enforcement"); A.isValid()) {
22735f757f3fSDimitry Andric StringRef S = A.getValueAsString();
22745f757f3fSDimitry Andric if (S != "true" && S != "false")
22755f757f3fSDimitry Andric CheckFailed(
22765f757f3fSDimitry Andric "invalid value for 'branch-target-enforcement' attribute: " + S, V);
22775f757f3fSDimitry Andric }
22787a6dacacSDimitry Andric
22797a6dacacSDimitry Andric if (auto A = Attrs.getFnAttr("vector-function-abi-variant"); A.isValid()) {
22807a6dacacSDimitry Andric StringRef S = A.getValueAsString();
22817a6dacacSDimitry Andric const std::optional<VFInfo> Info = VFABI::tryDemangleForVFABI(S, FT);
22827a6dacacSDimitry Andric if (!Info)
22837a6dacacSDimitry Andric CheckFailed("invalid name for a VFABI variant: " + S, V);
22847a6dacacSDimitry Andric }
22850b57cec5SDimitry Andric }
22860b57cec5SDimitry Andric
verifyFunctionMetadata(ArrayRef<std::pair<unsigned,MDNode * >> MDs)22870b57cec5SDimitry Andric void Verifier::verifyFunctionMetadata(
22880b57cec5SDimitry Andric ArrayRef<std::pair<unsigned, MDNode *>> MDs) {
22890b57cec5SDimitry Andric for (const auto &Pair : MDs) {
22900b57cec5SDimitry Andric if (Pair.first == LLVMContext::MD_prof) {
22910b57cec5SDimitry Andric MDNode *MD = Pair.second;
229281ad6265SDimitry Andric Check(MD->getNumOperands() >= 2,
22930b57cec5SDimitry Andric "!prof annotations should have no less than 2 operands", MD);
22940b57cec5SDimitry Andric
22950b57cec5SDimitry Andric // Check first operand.
229681ad6265SDimitry Andric Check(MD->getOperand(0) != nullptr, "first operand should not be null",
22970b57cec5SDimitry Andric MD);
229881ad6265SDimitry Andric Check(isa<MDString>(MD->getOperand(0)),
22990b57cec5SDimitry Andric "expected string with name of the !prof annotation", MD);
23000b57cec5SDimitry Andric MDString *MDS = cast<MDString>(MD->getOperand(0));
23010b57cec5SDimitry Andric StringRef ProfName = MDS->getString();
230281ad6265SDimitry Andric Check(ProfName.equals("function_entry_count") ||
23030b57cec5SDimitry Andric ProfName.equals("synthetic_function_entry_count"),
23040b57cec5SDimitry Andric "first operand should be 'function_entry_count'"
23050b57cec5SDimitry Andric " or 'synthetic_function_entry_count'",
23060b57cec5SDimitry Andric MD);
23070b57cec5SDimitry Andric
23080b57cec5SDimitry Andric // Check second operand.
230981ad6265SDimitry Andric Check(MD->getOperand(1) != nullptr, "second operand should not be null",
23100b57cec5SDimitry Andric MD);
231181ad6265SDimitry Andric Check(isa<ConstantAsMetadata>(MD->getOperand(1)),
23120b57cec5SDimitry Andric "expected integer argument to function_entry_count", MD);
2313bdd1243dSDimitry Andric } else if (Pair.first == LLVMContext::MD_kcfi_type) {
2314bdd1243dSDimitry Andric MDNode *MD = Pair.second;
2315bdd1243dSDimitry Andric Check(MD->getNumOperands() == 1,
2316bdd1243dSDimitry Andric "!kcfi_type must have exactly one operand", MD);
2317bdd1243dSDimitry Andric Check(MD->getOperand(0) != nullptr, "!kcfi_type operand must not be null",
2318bdd1243dSDimitry Andric MD);
2319bdd1243dSDimitry Andric Check(isa<ConstantAsMetadata>(MD->getOperand(0)),
2320bdd1243dSDimitry Andric "expected a constant operand for !kcfi_type", MD);
2321bdd1243dSDimitry Andric Constant *C = cast<ConstantAsMetadata>(MD->getOperand(0))->getValue();
2322cb14a3feSDimitry Andric Check(isa<ConstantInt>(C) && isa<IntegerType>(C->getType()),
2323bdd1243dSDimitry Andric "expected a constant integer operand for !kcfi_type", MD);
2324cb14a3feSDimitry Andric Check(cast<ConstantInt>(C)->getBitWidth() == 32,
2325bdd1243dSDimitry Andric "expected a 32-bit integer constant operand for !kcfi_type", MD);
23260b57cec5SDimitry Andric }
23270b57cec5SDimitry Andric }
23280b57cec5SDimitry Andric }
23290b57cec5SDimitry Andric
visitConstantExprsRecursively(const Constant * EntryC)23300b57cec5SDimitry Andric void Verifier::visitConstantExprsRecursively(const Constant *EntryC) {
23310b57cec5SDimitry Andric if (!ConstantExprVisited.insert(EntryC).second)
23320b57cec5SDimitry Andric return;
23330b57cec5SDimitry Andric
23340b57cec5SDimitry Andric SmallVector<const Constant *, 16> Stack;
23350b57cec5SDimitry Andric Stack.push_back(EntryC);
23360b57cec5SDimitry Andric
23370b57cec5SDimitry Andric while (!Stack.empty()) {
23380b57cec5SDimitry Andric const Constant *C = Stack.pop_back_val();
23390b57cec5SDimitry Andric
23400b57cec5SDimitry Andric // Check this constant expression.
23410b57cec5SDimitry Andric if (const auto *CE = dyn_cast<ConstantExpr>(C))
23420b57cec5SDimitry Andric visitConstantExpr(CE);
23430b57cec5SDimitry Andric
23440b57cec5SDimitry Andric if (const auto *GV = dyn_cast<GlobalValue>(C)) {
23450b57cec5SDimitry Andric // Global Values get visited separately, but we do need to make sure
23460b57cec5SDimitry Andric // that the global value is in the correct module
234781ad6265SDimitry Andric Check(GV->getParent() == &M, "Referencing global in another module!",
23480b57cec5SDimitry Andric EntryC, &M, GV, GV->getParent());
23490b57cec5SDimitry Andric continue;
23500b57cec5SDimitry Andric }
23510b57cec5SDimitry Andric
23520b57cec5SDimitry Andric // Visit all sub-expressions.
23530b57cec5SDimitry Andric for (const Use &U : C->operands()) {
23540b57cec5SDimitry Andric const auto *OpC = dyn_cast<Constant>(U);
23550b57cec5SDimitry Andric if (!OpC)
23560b57cec5SDimitry Andric continue;
23570b57cec5SDimitry Andric if (!ConstantExprVisited.insert(OpC).second)
23580b57cec5SDimitry Andric continue;
23590b57cec5SDimitry Andric Stack.push_back(OpC);
23600b57cec5SDimitry Andric }
23610b57cec5SDimitry Andric }
23620b57cec5SDimitry Andric }
23630b57cec5SDimitry Andric
visitConstantExpr(const ConstantExpr * CE)23640b57cec5SDimitry Andric void Verifier::visitConstantExpr(const ConstantExpr *CE) {
23650b57cec5SDimitry Andric if (CE->getOpcode() == Instruction::BitCast)
236681ad6265SDimitry Andric Check(CastInst::castIsValid(Instruction::BitCast, CE->getOperand(0),
23670b57cec5SDimitry Andric CE->getType()),
23680b57cec5SDimitry Andric "Invalid bitcast", CE);
23690b57cec5SDimitry Andric }
23700b57cec5SDimitry Andric
verifyAttributeCount(AttributeList Attrs,unsigned Params)23710b57cec5SDimitry Andric bool Verifier::verifyAttributeCount(AttributeList Attrs, unsigned Params) {
23720b57cec5SDimitry Andric // There shouldn't be more attribute sets than there are parameters plus the
23730b57cec5SDimitry Andric // function and return value.
23740b57cec5SDimitry Andric return Attrs.getNumAttrSets() <= Params + 2;
23750b57cec5SDimitry Andric }
23760b57cec5SDimitry Andric
verifyInlineAsmCall(const CallBase & Call)237704eeddc0SDimitry Andric void Verifier::verifyInlineAsmCall(const CallBase &Call) {
237804eeddc0SDimitry Andric const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
237904eeddc0SDimitry Andric unsigned ArgNo = 0;
2380fcaf7f86SDimitry Andric unsigned LabelNo = 0;
238104eeddc0SDimitry Andric for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) {
2382fcaf7f86SDimitry Andric if (CI.Type == InlineAsm::isLabel) {
2383fcaf7f86SDimitry Andric ++LabelNo;
2384fcaf7f86SDimitry Andric continue;
2385fcaf7f86SDimitry Andric }
2386fcaf7f86SDimitry Andric
238704eeddc0SDimitry Andric // Only deal with constraints that correspond to call arguments.
238804eeddc0SDimitry Andric if (!CI.hasArg())
238904eeddc0SDimitry Andric continue;
239004eeddc0SDimitry Andric
239104eeddc0SDimitry Andric if (CI.isIndirect) {
239204eeddc0SDimitry Andric const Value *Arg = Call.getArgOperand(ArgNo);
239381ad6265SDimitry Andric Check(Arg->getType()->isPointerTy(),
239481ad6265SDimitry Andric "Operand for indirect constraint must have pointer type", &Call);
239504eeddc0SDimitry Andric
239681ad6265SDimitry Andric Check(Call.getParamElementType(ArgNo),
239704eeddc0SDimitry Andric "Operand for indirect constraint must have elementtype attribute",
239804eeddc0SDimitry Andric &Call);
239904eeddc0SDimitry Andric } else {
240081ad6265SDimitry Andric Check(!Call.paramHasAttr(ArgNo, Attribute::ElementType),
240104eeddc0SDimitry Andric "Elementtype attribute can only be applied for indirect "
240281ad6265SDimitry Andric "constraints",
240381ad6265SDimitry Andric &Call);
240404eeddc0SDimitry Andric }
240504eeddc0SDimitry Andric
240604eeddc0SDimitry Andric ArgNo++;
240704eeddc0SDimitry Andric }
2408fcaf7f86SDimitry Andric
2409fcaf7f86SDimitry Andric if (auto *CallBr = dyn_cast<CallBrInst>(&Call)) {
2410fcaf7f86SDimitry Andric Check(LabelNo == CallBr->getNumIndirectDests(),
2411fcaf7f86SDimitry Andric "Number of label constraints does not match number of callbr dests",
2412fcaf7f86SDimitry Andric &Call);
2413fcaf7f86SDimitry Andric } else {
2414fcaf7f86SDimitry Andric Check(LabelNo == 0, "Label constraints can only be used with callbr",
2415fcaf7f86SDimitry Andric &Call);
2416fcaf7f86SDimitry Andric }
241704eeddc0SDimitry Andric }
241804eeddc0SDimitry Andric
24190b57cec5SDimitry Andric /// Verify that statepoint intrinsic is well formed.
verifyStatepoint(const CallBase & Call)24200b57cec5SDimitry Andric void Verifier::verifyStatepoint(const CallBase &Call) {
24210b57cec5SDimitry Andric assert(Call.getCalledFunction() &&
24220b57cec5SDimitry Andric Call.getCalledFunction()->getIntrinsicID() ==
24230b57cec5SDimitry Andric Intrinsic::experimental_gc_statepoint);
24240b57cec5SDimitry Andric
242581ad6265SDimitry Andric Check(!Call.doesNotAccessMemory() && !Call.onlyReadsMemory() &&
24260b57cec5SDimitry Andric !Call.onlyAccessesArgMemory(),
24270b57cec5SDimitry Andric "gc.statepoint must read and write all memory to preserve "
24280b57cec5SDimitry Andric "reordering restrictions required by safepoint semantics",
24290b57cec5SDimitry Andric Call);
24300b57cec5SDimitry Andric
24310b57cec5SDimitry Andric const int64_t NumPatchBytes =
24320b57cec5SDimitry Andric cast<ConstantInt>(Call.getArgOperand(1))->getSExtValue();
24330b57cec5SDimitry Andric assert(isInt<32>(NumPatchBytes) && "NumPatchBytesV is an i32!");
243481ad6265SDimitry Andric Check(NumPatchBytes >= 0,
24350b57cec5SDimitry Andric "gc.statepoint number of patchable bytes must be "
24360b57cec5SDimitry Andric "positive",
24370b57cec5SDimitry Andric Call);
24380b57cec5SDimitry Andric
243981ad6265SDimitry Andric Type *TargetElemType = Call.getParamElementType(2);
244081ad6265SDimitry Andric Check(TargetElemType,
244181ad6265SDimitry Andric "gc.statepoint callee argument must have elementtype attribute", Call);
244281ad6265SDimitry Andric FunctionType *TargetFuncType = dyn_cast<FunctionType>(TargetElemType);
244381ad6265SDimitry Andric Check(TargetFuncType,
244481ad6265SDimitry Andric "gc.statepoint callee elementtype must be function type", Call);
24450b57cec5SDimitry Andric
24460b57cec5SDimitry Andric const int NumCallArgs = cast<ConstantInt>(Call.getArgOperand(3))->getZExtValue();
244781ad6265SDimitry Andric Check(NumCallArgs >= 0,
24480b57cec5SDimitry Andric "gc.statepoint number of arguments to underlying call "
24490b57cec5SDimitry Andric "must be positive",
24500b57cec5SDimitry Andric Call);
24510b57cec5SDimitry Andric const int NumParams = (int)TargetFuncType->getNumParams();
24520b57cec5SDimitry Andric if (TargetFuncType->isVarArg()) {
245381ad6265SDimitry Andric Check(NumCallArgs >= NumParams,
24540b57cec5SDimitry Andric "gc.statepoint mismatch in number of vararg call args", Call);
24550b57cec5SDimitry Andric
24560b57cec5SDimitry Andric // TODO: Remove this limitation
245781ad6265SDimitry Andric Check(TargetFuncType->getReturnType()->isVoidTy(),
24580b57cec5SDimitry Andric "gc.statepoint doesn't support wrapping non-void "
24590b57cec5SDimitry Andric "vararg functions yet",
24600b57cec5SDimitry Andric Call);
24610b57cec5SDimitry Andric } else
246281ad6265SDimitry Andric Check(NumCallArgs == NumParams,
24630b57cec5SDimitry Andric "gc.statepoint mismatch in number of call args", Call);
24640b57cec5SDimitry Andric
24650b57cec5SDimitry Andric const uint64_t Flags
24660b57cec5SDimitry Andric = cast<ConstantInt>(Call.getArgOperand(4))->getZExtValue();
246781ad6265SDimitry Andric Check((Flags & ~(uint64_t)StatepointFlags::MaskAll) == 0,
24680b57cec5SDimitry Andric "unknown flag used in gc.statepoint flags argument", Call);
24690b57cec5SDimitry Andric
24700b57cec5SDimitry Andric // Verify that the types of the call parameter arguments match
24710b57cec5SDimitry Andric // the type of the wrapped callee.
24720b57cec5SDimitry Andric AttributeList Attrs = Call.getAttributes();
24730b57cec5SDimitry Andric for (int i = 0; i < NumParams; i++) {
24740b57cec5SDimitry Andric Type *ParamType = TargetFuncType->getParamType(i);
24750b57cec5SDimitry Andric Type *ArgType = Call.getArgOperand(5 + i)->getType();
247681ad6265SDimitry Andric Check(ArgType == ParamType,
24770b57cec5SDimitry Andric "gc.statepoint call argument does not match wrapped "
24780b57cec5SDimitry Andric "function type",
24790b57cec5SDimitry Andric Call);
24800b57cec5SDimitry Andric
24810b57cec5SDimitry Andric if (TargetFuncType->isVarArg()) {
2482349cc55cSDimitry Andric AttributeSet ArgAttrs = Attrs.getParamAttrs(5 + i);
248381ad6265SDimitry Andric Check(!ArgAttrs.hasAttribute(Attribute::StructRet),
248481ad6265SDimitry Andric "Attribute 'sret' cannot be used for vararg call arguments!", Call);
24850b57cec5SDimitry Andric }
24860b57cec5SDimitry Andric }
24870b57cec5SDimitry Andric
24880b57cec5SDimitry Andric const int EndCallArgsInx = 4 + NumCallArgs;
24890b57cec5SDimitry Andric
24900b57cec5SDimitry Andric const Value *NumTransitionArgsV = Call.getArgOperand(EndCallArgsInx + 1);
249181ad6265SDimitry Andric Check(isa<ConstantInt>(NumTransitionArgsV),
24920b57cec5SDimitry Andric "gc.statepoint number of transition arguments "
24930b57cec5SDimitry Andric "must be constant integer",
24940b57cec5SDimitry Andric Call);
24950b57cec5SDimitry Andric const int NumTransitionArgs =
24960b57cec5SDimitry Andric cast<ConstantInt>(NumTransitionArgsV)->getZExtValue();
249781ad6265SDimitry Andric Check(NumTransitionArgs == 0,
2498e8d8bef9SDimitry Andric "gc.statepoint w/inline transition bundle is deprecated", Call);
2499e8d8bef9SDimitry Andric const int EndTransitionArgsInx = EndCallArgsInx + 1 + NumTransitionArgs;
25005ffd83dbSDimitry Andric
25010b57cec5SDimitry Andric const Value *NumDeoptArgsV = Call.getArgOperand(EndTransitionArgsInx + 1);
250281ad6265SDimitry Andric Check(isa<ConstantInt>(NumDeoptArgsV),
25030b57cec5SDimitry Andric "gc.statepoint number of deoptimization arguments "
25040b57cec5SDimitry Andric "must be constant integer",
25050b57cec5SDimitry Andric Call);
25060b57cec5SDimitry Andric const int NumDeoptArgs = cast<ConstantInt>(NumDeoptArgsV)->getZExtValue();
250781ad6265SDimitry Andric Check(NumDeoptArgs == 0,
2508e8d8bef9SDimitry Andric "gc.statepoint w/inline deopt operands is deprecated", Call);
25095ffd83dbSDimitry Andric
2510e8d8bef9SDimitry Andric const int ExpectedNumArgs = 7 + NumCallArgs;
251181ad6265SDimitry Andric Check(ExpectedNumArgs == (int)Call.arg_size(),
2512e8d8bef9SDimitry Andric "gc.statepoint too many arguments", Call);
25130b57cec5SDimitry Andric
25140b57cec5SDimitry Andric // Check that the only uses of this gc.statepoint are gc.result or
25150b57cec5SDimitry Andric // gc.relocate calls which are tied to this statepoint and thus part
25160b57cec5SDimitry Andric // of the same statepoint sequence
25170b57cec5SDimitry Andric for (const User *U : Call.users()) {
25180b57cec5SDimitry Andric const CallInst *UserCall = dyn_cast<const CallInst>(U);
251981ad6265SDimitry Andric Check(UserCall, "illegal use of statepoint token", Call, U);
25200b57cec5SDimitry Andric if (!UserCall)
25210b57cec5SDimitry Andric continue;
252281ad6265SDimitry Andric Check(isa<GCRelocateInst>(UserCall) || isa<GCResultInst>(UserCall),
25230b57cec5SDimitry Andric "gc.result or gc.relocate are the only value uses "
25240b57cec5SDimitry Andric "of a gc.statepoint",
25250b57cec5SDimitry Andric Call, U);
25260b57cec5SDimitry Andric if (isa<GCResultInst>(UserCall)) {
252781ad6265SDimitry Andric Check(UserCall->getArgOperand(0) == &Call,
25280b57cec5SDimitry Andric "gc.result connected to wrong gc.statepoint", Call, UserCall);
25290b57cec5SDimitry Andric } else if (isa<GCRelocateInst>(Call)) {
253081ad6265SDimitry Andric Check(UserCall->getArgOperand(0) == &Call,
25310b57cec5SDimitry Andric "gc.relocate connected to wrong gc.statepoint", Call, UserCall);
25320b57cec5SDimitry Andric }
25330b57cec5SDimitry Andric }
25340b57cec5SDimitry Andric
25350b57cec5SDimitry Andric // Note: It is legal for a single derived pointer to be listed multiple
25360b57cec5SDimitry Andric // times. It's non-optimal, but it is legal. It can also happen after
25370b57cec5SDimitry Andric // insertion if we strip a bitcast away.
25380b57cec5SDimitry Andric // Note: It is really tempting to check that each base is relocated and
25390b57cec5SDimitry Andric // that a derived pointer is never reused as a base pointer. This turns
25400b57cec5SDimitry Andric // out to be problematic since optimizations run after safepoint insertion
25410b57cec5SDimitry Andric // can recognize equality properties that the insertion logic doesn't know
25420b57cec5SDimitry Andric // about. See example statepoint.ll in the verifier subdirectory
25430b57cec5SDimitry Andric }
25440b57cec5SDimitry Andric
verifyFrameRecoverIndices()25450b57cec5SDimitry Andric void Verifier::verifyFrameRecoverIndices() {
25460b57cec5SDimitry Andric for (auto &Counts : FrameEscapeInfo) {
25470b57cec5SDimitry Andric Function *F = Counts.first;
25480b57cec5SDimitry Andric unsigned EscapedObjectCount = Counts.second.first;
25490b57cec5SDimitry Andric unsigned MaxRecoveredIndex = Counts.second.second;
255081ad6265SDimitry Andric Check(MaxRecoveredIndex <= EscapedObjectCount,
25510b57cec5SDimitry Andric "all indices passed to llvm.localrecover must be less than the "
25520b57cec5SDimitry Andric "number of arguments passed to llvm.localescape in the parent "
25530b57cec5SDimitry Andric "function",
25540b57cec5SDimitry Andric F);
25550b57cec5SDimitry Andric }
25560b57cec5SDimitry Andric }
25570b57cec5SDimitry Andric
getSuccPad(Instruction * Terminator)25580b57cec5SDimitry Andric static Instruction *getSuccPad(Instruction *Terminator) {
25590b57cec5SDimitry Andric BasicBlock *UnwindDest;
25600b57cec5SDimitry Andric if (auto *II = dyn_cast<InvokeInst>(Terminator))
25610b57cec5SDimitry Andric UnwindDest = II->getUnwindDest();
25620b57cec5SDimitry Andric else if (auto *CSI = dyn_cast<CatchSwitchInst>(Terminator))
25630b57cec5SDimitry Andric UnwindDest = CSI->getUnwindDest();
25640b57cec5SDimitry Andric else
25650b57cec5SDimitry Andric UnwindDest = cast<CleanupReturnInst>(Terminator)->getUnwindDest();
25660b57cec5SDimitry Andric return UnwindDest->getFirstNonPHI();
25670b57cec5SDimitry Andric }
25680b57cec5SDimitry Andric
verifySiblingFuncletUnwinds()25690b57cec5SDimitry Andric void Verifier::verifySiblingFuncletUnwinds() {
25700b57cec5SDimitry Andric SmallPtrSet<Instruction *, 8> Visited;
25710b57cec5SDimitry Andric SmallPtrSet<Instruction *, 8> Active;
25720b57cec5SDimitry Andric for (const auto &Pair : SiblingFuncletInfo) {
25730b57cec5SDimitry Andric Instruction *PredPad = Pair.first;
25740b57cec5SDimitry Andric if (Visited.count(PredPad))
25750b57cec5SDimitry Andric continue;
25760b57cec5SDimitry Andric Active.insert(PredPad);
25770b57cec5SDimitry Andric Instruction *Terminator = Pair.second;
25780b57cec5SDimitry Andric do {
25790b57cec5SDimitry Andric Instruction *SuccPad = getSuccPad(Terminator);
25800b57cec5SDimitry Andric if (Active.count(SuccPad)) {
25810b57cec5SDimitry Andric // Found a cycle; report error
25820b57cec5SDimitry Andric Instruction *CyclePad = SuccPad;
25830b57cec5SDimitry Andric SmallVector<Instruction *, 8> CycleNodes;
25840b57cec5SDimitry Andric do {
25850b57cec5SDimitry Andric CycleNodes.push_back(CyclePad);
25860b57cec5SDimitry Andric Instruction *CycleTerminator = SiblingFuncletInfo[CyclePad];
25870b57cec5SDimitry Andric if (CycleTerminator != CyclePad)
25880b57cec5SDimitry Andric CycleNodes.push_back(CycleTerminator);
25890b57cec5SDimitry Andric CyclePad = getSuccPad(CycleTerminator);
25900b57cec5SDimitry Andric } while (CyclePad != SuccPad);
259181ad6265SDimitry Andric Check(false, "EH pads can't handle each other's exceptions",
25920b57cec5SDimitry Andric ArrayRef<Instruction *>(CycleNodes));
25930b57cec5SDimitry Andric }
25940b57cec5SDimitry Andric // Don't re-walk a node we've already checked
25950b57cec5SDimitry Andric if (!Visited.insert(SuccPad).second)
25960b57cec5SDimitry Andric break;
25970b57cec5SDimitry Andric // Walk to this successor if it has a map entry.
25980b57cec5SDimitry Andric PredPad = SuccPad;
25990b57cec5SDimitry Andric auto TermI = SiblingFuncletInfo.find(PredPad);
26000b57cec5SDimitry Andric if (TermI == SiblingFuncletInfo.end())
26010b57cec5SDimitry Andric break;
26020b57cec5SDimitry Andric Terminator = TermI->second;
26030b57cec5SDimitry Andric Active.insert(PredPad);
26040b57cec5SDimitry Andric } while (true);
26050b57cec5SDimitry Andric // Each node only has one successor, so we've walked all the active
26060b57cec5SDimitry Andric // nodes' successors.
26070b57cec5SDimitry Andric Active.clear();
26080b57cec5SDimitry Andric }
26090b57cec5SDimitry Andric }
26100b57cec5SDimitry Andric
26110b57cec5SDimitry Andric // visitFunction - Verify that a function is ok.
26120b57cec5SDimitry Andric //
visitFunction(const Function & F)26130b57cec5SDimitry Andric void Verifier::visitFunction(const Function &F) {
26140b57cec5SDimitry Andric visitGlobalValue(F);
26150b57cec5SDimitry Andric
26160b57cec5SDimitry Andric // Check function arguments.
26170b57cec5SDimitry Andric FunctionType *FT = F.getFunctionType();
26180b57cec5SDimitry Andric unsigned NumArgs = F.arg_size();
26190b57cec5SDimitry Andric
262081ad6265SDimitry Andric Check(&Context == &F.getContext(),
26210b57cec5SDimitry Andric "Function context does not match Module context!", &F);
26220b57cec5SDimitry Andric
262381ad6265SDimitry Andric Check(!F.hasCommonLinkage(), "Functions may not have common linkage", &F);
262481ad6265SDimitry Andric Check(FT->getNumParams() == NumArgs,
26250b57cec5SDimitry Andric "# formal arguments must match # of arguments for function type!", &F,
26260b57cec5SDimitry Andric FT);
262781ad6265SDimitry Andric Check(F.getReturnType()->isFirstClassType() ||
26280b57cec5SDimitry Andric F.getReturnType()->isVoidTy() || F.getReturnType()->isStructTy(),
26290b57cec5SDimitry Andric "Functions cannot return aggregate values!", &F);
26300b57cec5SDimitry Andric
263181ad6265SDimitry Andric Check(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
26320b57cec5SDimitry Andric "Invalid struct return type!", &F);
26330b57cec5SDimitry Andric
26340b57cec5SDimitry Andric AttributeList Attrs = F.getAttributes();
26350b57cec5SDimitry Andric
263681ad6265SDimitry Andric Check(verifyAttributeCount(Attrs, FT->getNumParams()),
26370b57cec5SDimitry Andric "Attribute after last parameter!", &F);
26380b57cec5SDimitry Andric
2639fe6060f1SDimitry Andric bool IsIntrinsic = F.isIntrinsic();
26400b57cec5SDimitry Andric
26410b57cec5SDimitry Andric // Check function attributes.
264204eeddc0SDimitry Andric verifyFunctionAttrs(FT, Attrs, &F, IsIntrinsic, /* IsInlineAsm */ false);
26430b57cec5SDimitry Andric
26440b57cec5SDimitry Andric // On function declarations/definitions, we do not support the builtin
26450b57cec5SDimitry Andric // attribute. We do not check this in VerifyFunctionAttrs since that is
26460b57cec5SDimitry Andric // checking for Attributes that can/can not ever be on functions.
264781ad6265SDimitry Andric Check(!Attrs.hasFnAttr(Attribute::Builtin),
26480b57cec5SDimitry Andric "Attribute 'builtin' can only be applied to a callsite.", &F);
26490b57cec5SDimitry Andric
265081ad6265SDimitry Andric Check(!Attrs.hasAttrSomewhere(Attribute::ElementType),
2651fe6060f1SDimitry Andric "Attribute 'elementtype' can only be applied to a callsite.", &F);
2652fe6060f1SDimitry Andric
26530b57cec5SDimitry Andric // Check that this function meets the restrictions on this calling convention.
26540b57cec5SDimitry Andric // Sometimes varargs is used for perfectly forwarding thunks, so some of these
26550b57cec5SDimitry Andric // restrictions can be lifted.
26560b57cec5SDimitry Andric switch (F.getCallingConv()) {
26570b57cec5SDimitry Andric default:
26580b57cec5SDimitry Andric case CallingConv::C:
26590b57cec5SDimitry Andric break;
2660e8d8bef9SDimitry Andric case CallingConv::X86_INTR: {
266181ad6265SDimitry Andric Check(F.arg_empty() || Attrs.hasParamAttr(0, Attribute::ByVal),
2662e8d8bef9SDimitry Andric "Calling convention parameter requires byval", &F);
2663e8d8bef9SDimitry Andric break;
2664e8d8bef9SDimitry Andric }
26650b57cec5SDimitry Andric case CallingConv::AMDGPU_KERNEL:
26660b57cec5SDimitry Andric case CallingConv::SPIR_KERNEL:
266706c3fb27SDimitry Andric case CallingConv::AMDGPU_CS_Chain:
266806c3fb27SDimitry Andric case CallingConv::AMDGPU_CS_ChainPreserve:
266981ad6265SDimitry Andric Check(F.getReturnType()->isVoidTy(),
26700b57cec5SDimitry Andric "Calling convention requires void return type", &F);
2671bdd1243dSDimitry Andric [[fallthrough]];
26720b57cec5SDimitry Andric case CallingConv::AMDGPU_VS:
26730b57cec5SDimitry Andric case CallingConv::AMDGPU_HS:
26740b57cec5SDimitry Andric case CallingConv::AMDGPU_GS:
26750b57cec5SDimitry Andric case CallingConv::AMDGPU_PS:
26760b57cec5SDimitry Andric case CallingConv::AMDGPU_CS:
267781ad6265SDimitry Andric Check(!F.hasStructRetAttr(), "Calling convention does not allow sret", &F);
2678e8d8bef9SDimitry Andric if (F.getCallingConv() != CallingConv::SPIR_KERNEL) {
2679e8d8bef9SDimitry Andric const unsigned StackAS = DL.getAllocaAddrSpace();
2680e8d8bef9SDimitry Andric unsigned i = 0;
2681e8d8bef9SDimitry Andric for (const Argument &Arg : F.args()) {
268281ad6265SDimitry Andric Check(!Attrs.hasParamAttr(i, Attribute::ByVal),
2683e8d8bef9SDimitry Andric "Calling convention disallows byval", &F);
268481ad6265SDimitry Andric Check(!Attrs.hasParamAttr(i, Attribute::Preallocated),
2685e8d8bef9SDimitry Andric "Calling convention disallows preallocated", &F);
268681ad6265SDimitry Andric Check(!Attrs.hasParamAttr(i, Attribute::InAlloca),
2687e8d8bef9SDimitry Andric "Calling convention disallows inalloca", &F);
2688e8d8bef9SDimitry Andric
2689349cc55cSDimitry Andric if (Attrs.hasParamAttr(i, Attribute::ByRef)) {
2690e8d8bef9SDimitry Andric // FIXME: Should also disallow LDS and GDS, but we don't have the enum
2691e8d8bef9SDimitry Andric // value here.
269281ad6265SDimitry Andric Check(Arg.getType()->getPointerAddressSpace() != StackAS,
2693e8d8bef9SDimitry Andric "Calling convention disallows stack byref", &F);
2694e8d8bef9SDimitry Andric }
2695e8d8bef9SDimitry Andric
2696e8d8bef9SDimitry Andric ++i;
2697e8d8bef9SDimitry Andric }
2698e8d8bef9SDimitry Andric }
2699e8d8bef9SDimitry Andric
2700bdd1243dSDimitry Andric [[fallthrough]];
27010b57cec5SDimitry Andric case CallingConv::Fast:
27020b57cec5SDimitry Andric case CallingConv::Cold:
27030b57cec5SDimitry Andric case CallingConv::Intel_OCL_BI:
27040b57cec5SDimitry Andric case CallingConv::PTX_Kernel:
27050b57cec5SDimitry Andric case CallingConv::PTX_Device:
270681ad6265SDimitry Andric Check(!F.isVarArg(),
270781ad6265SDimitry Andric "Calling convention does not support varargs or "
27080b57cec5SDimitry Andric "perfect forwarding!",
27090b57cec5SDimitry Andric &F);
27100b57cec5SDimitry Andric break;
27110b57cec5SDimitry Andric }
27120b57cec5SDimitry Andric
27130b57cec5SDimitry Andric // Check that the argument values match the function type for this function...
27140b57cec5SDimitry Andric unsigned i = 0;
27150b57cec5SDimitry Andric for (const Argument &Arg : F.args()) {
271681ad6265SDimitry Andric Check(Arg.getType() == FT->getParamType(i),
27170b57cec5SDimitry Andric "Argument value does not match function argument type!", &Arg,
27180b57cec5SDimitry Andric FT->getParamType(i));
271981ad6265SDimitry Andric Check(Arg.getType()->isFirstClassType(),
27200b57cec5SDimitry Andric "Function arguments must have first-class types!", &Arg);
2721fe6060f1SDimitry Andric if (!IsIntrinsic) {
272281ad6265SDimitry Andric Check(!Arg.getType()->isMetadataTy(),
27230b57cec5SDimitry Andric "Function takes metadata but isn't an intrinsic", &Arg, &F);
272481ad6265SDimitry Andric Check(!Arg.getType()->isTokenTy(),
27250b57cec5SDimitry Andric "Function takes token but isn't an intrinsic", &Arg, &F);
272681ad6265SDimitry Andric Check(!Arg.getType()->isX86_AMXTy(),
2727fe6060f1SDimitry Andric "Function takes x86_amx but isn't an intrinsic", &Arg, &F);
27280b57cec5SDimitry Andric }
27290b57cec5SDimitry Andric
27300b57cec5SDimitry Andric // Check that swifterror argument is only used by loads and stores.
2731349cc55cSDimitry Andric if (Attrs.hasParamAttr(i, Attribute::SwiftError)) {
27320b57cec5SDimitry Andric verifySwiftErrorValue(&Arg);
27330b57cec5SDimitry Andric }
27340b57cec5SDimitry Andric ++i;
27350b57cec5SDimitry Andric }
27360b57cec5SDimitry Andric
2737fe6060f1SDimitry Andric if (!IsIntrinsic) {
273881ad6265SDimitry Andric Check(!F.getReturnType()->isTokenTy(),
2739fe6060f1SDimitry Andric "Function returns a token but isn't an intrinsic", &F);
274081ad6265SDimitry Andric Check(!F.getReturnType()->isX86_AMXTy(),
2741fe6060f1SDimitry Andric "Function returns a x86_amx but isn't an intrinsic", &F);
2742fe6060f1SDimitry Andric }
27430b57cec5SDimitry Andric
27440b57cec5SDimitry Andric // Get the function metadata attachments.
27450b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
27460b57cec5SDimitry Andric F.getAllMetadata(MDs);
27470b57cec5SDimitry Andric assert(F.hasMetadata() != MDs.empty() && "Bit out-of-sync");
27480b57cec5SDimitry Andric verifyFunctionMetadata(MDs);
27490b57cec5SDimitry Andric
27500b57cec5SDimitry Andric // Check validity of the personality function
27510b57cec5SDimitry Andric if (F.hasPersonalityFn()) {
27520b57cec5SDimitry Andric auto *Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
27530b57cec5SDimitry Andric if (Per)
275481ad6265SDimitry Andric Check(Per->getParent() == F.getParent(),
275581ad6265SDimitry Andric "Referencing personality function in another module!", &F,
275681ad6265SDimitry Andric F.getParent(), Per, Per->getParent());
27570b57cec5SDimitry Andric }
27580b57cec5SDimitry Andric
275906c3fb27SDimitry Andric // EH funclet coloring can be expensive, recompute on-demand
276006c3fb27SDimitry Andric BlockEHFuncletColors.clear();
276106c3fb27SDimitry Andric
27620b57cec5SDimitry Andric if (F.isMaterializable()) {
27630b57cec5SDimitry Andric // Function has a body somewhere we can't see.
276481ad6265SDimitry Andric Check(MDs.empty(), "unmaterialized function cannot have metadata", &F,
27650b57cec5SDimitry Andric MDs.empty() ? nullptr : MDs.front().second);
27660b57cec5SDimitry Andric } else if (F.isDeclaration()) {
27670b57cec5SDimitry Andric for (const auto &I : MDs) {
27680b57cec5SDimitry Andric // This is used for call site debug information.
276981ad6265SDimitry Andric CheckDI(I.first != LLVMContext::MD_dbg ||
27700b57cec5SDimitry Andric !cast<DISubprogram>(I.second)->isDistinct(),
27710b57cec5SDimitry Andric "function declaration may only have a unique !dbg attachment",
27720b57cec5SDimitry Andric &F);
277381ad6265SDimitry Andric Check(I.first != LLVMContext::MD_prof,
27740b57cec5SDimitry Andric "function declaration may not have a !prof attachment", &F);
27750b57cec5SDimitry Andric
27760b57cec5SDimitry Andric // Verify the metadata itself.
27775ffd83dbSDimitry Andric visitMDNode(*I.second, AreDebugLocsAllowed::Yes);
27780b57cec5SDimitry Andric }
277981ad6265SDimitry Andric Check(!F.hasPersonalityFn(),
27800b57cec5SDimitry Andric "Function declaration shouldn't have a personality routine", &F);
27810b57cec5SDimitry Andric } else {
27820b57cec5SDimitry Andric // Verify that this function (which has a body) is not named "llvm.*". It
27830b57cec5SDimitry Andric // is not legal to define intrinsics.
278481ad6265SDimitry Andric Check(!IsIntrinsic, "llvm intrinsics cannot be defined!", &F);
27850b57cec5SDimitry Andric
27860b57cec5SDimitry Andric // Check the entry node
27870b57cec5SDimitry Andric const BasicBlock *Entry = &F.getEntryBlock();
278881ad6265SDimitry Andric Check(pred_empty(Entry),
27890b57cec5SDimitry Andric "Entry block to function must not have predecessors!", Entry);
27900b57cec5SDimitry Andric
27910b57cec5SDimitry Andric // The address of the entry block cannot be taken, unless it is dead.
27920b57cec5SDimitry Andric if (Entry->hasAddressTaken()) {
279381ad6265SDimitry Andric Check(!BlockAddress::lookup(Entry)->isConstantUsed(),
27940b57cec5SDimitry Andric "blockaddress may not be used with the entry block!", Entry);
27950b57cec5SDimitry Andric }
27960b57cec5SDimitry Andric
2797bdd1243dSDimitry Andric unsigned NumDebugAttachments = 0, NumProfAttachments = 0,
2798bdd1243dSDimitry Andric NumKCFIAttachments = 0;
27990b57cec5SDimitry Andric // Visit metadata attachments.
28000b57cec5SDimitry Andric for (const auto &I : MDs) {
28010b57cec5SDimitry Andric // Verify that the attachment is legal.
28025ffd83dbSDimitry Andric auto AllowLocs = AreDebugLocsAllowed::No;
28030b57cec5SDimitry Andric switch (I.first) {
28040b57cec5SDimitry Andric default:
28050b57cec5SDimitry Andric break;
28060b57cec5SDimitry Andric case LLVMContext::MD_dbg: {
28070b57cec5SDimitry Andric ++NumDebugAttachments;
280881ad6265SDimitry Andric CheckDI(NumDebugAttachments == 1,
28090b57cec5SDimitry Andric "function must have a single !dbg attachment", &F, I.second);
281081ad6265SDimitry Andric CheckDI(isa<DISubprogram>(I.second),
28110b57cec5SDimitry Andric "function !dbg attachment must be a subprogram", &F, I.second);
281281ad6265SDimitry Andric CheckDI(cast<DISubprogram>(I.second)->isDistinct(),
2813e8d8bef9SDimitry Andric "function definition may only have a distinct !dbg attachment",
2814e8d8bef9SDimitry Andric &F);
2815e8d8bef9SDimitry Andric
28160b57cec5SDimitry Andric auto *SP = cast<DISubprogram>(I.second);
28170b57cec5SDimitry Andric const Function *&AttachedTo = DISubprogramAttachments[SP];
281881ad6265SDimitry Andric CheckDI(!AttachedTo || AttachedTo == &F,
28190b57cec5SDimitry Andric "DISubprogram attached to more than one function", SP, &F);
28200b57cec5SDimitry Andric AttachedTo = &F;
28215ffd83dbSDimitry Andric AllowLocs = AreDebugLocsAllowed::Yes;
28220b57cec5SDimitry Andric break;
28230b57cec5SDimitry Andric }
28240b57cec5SDimitry Andric case LLVMContext::MD_prof:
28250b57cec5SDimitry Andric ++NumProfAttachments;
282681ad6265SDimitry Andric Check(NumProfAttachments == 1,
28270b57cec5SDimitry Andric "function must have a single !prof attachment", &F, I.second);
28280b57cec5SDimitry Andric break;
2829bdd1243dSDimitry Andric case LLVMContext::MD_kcfi_type:
2830bdd1243dSDimitry Andric ++NumKCFIAttachments;
2831bdd1243dSDimitry Andric Check(NumKCFIAttachments == 1,
2832bdd1243dSDimitry Andric "function must have a single !kcfi_type attachment", &F,
2833bdd1243dSDimitry Andric I.second);
2834bdd1243dSDimitry Andric break;
28350b57cec5SDimitry Andric }
28360b57cec5SDimitry Andric
28370b57cec5SDimitry Andric // Verify the metadata itself.
28385ffd83dbSDimitry Andric visitMDNode(*I.second, AllowLocs);
28390b57cec5SDimitry Andric }
28400b57cec5SDimitry Andric }
28410b57cec5SDimitry Andric
28420b57cec5SDimitry Andric // If this function is actually an intrinsic, verify that it is only used in
28430b57cec5SDimitry Andric // direct call/invokes, never having its "address taken".
28440b57cec5SDimitry Andric // Only do this if the module is materialized, otherwise we don't have all the
28450b57cec5SDimitry Andric // uses.
2846fe6060f1SDimitry Andric if (F.isIntrinsic() && F.getParent()->isMaterialized()) {
28470b57cec5SDimitry Andric const User *U;
2848349cc55cSDimitry Andric if (F.hasAddressTaken(&U, false, true, false,
2849349cc55cSDimitry Andric /*IgnoreARCAttachedCall=*/true))
285081ad6265SDimitry Andric Check(false, "Invalid user of intrinsic instruction!", U);
28510b57cec5SDimitry Andric }
28520b57cec5SDimitry Andric
2853fe6060f1SDimitry Andric // Check intrinsics' signatures.
2854fe6060f1SDimitry Andric switch (F.getIntrinsicID()) {
2855fe6060f1SDimitry Andric case Intrinsic::experimental_gc_get_pointer_base: {
2856fe6060f1SDimitry Andric FunctionType *FT = F.getFunctionType();
285781ad6265SDimitry Andric Check(FT->getNumParams() == 1, "wrong number of parameters", F);
285881ad6265SDimitry Andric Check(isa<PointerType>(F.getReturnType()),
2859fe6060f1SDimitry Andric "gc.get.pointer.base must return a pointer", F);
286081ad6265SDimitry Andric Check(FT->getParamType(0) == F.getReturnType(),
286181ad6265SDimitry Andric "gc.get.pointer.base operand and result must be of the same type", F);
2862fe6060f1SDimitry Andric break;
2863fe6060f1SDimitry Andric }
2864fe6060f1SDimitry Andric case Intrinsic::experimental_gc_get_pointer_offset: {
2865fe6060f1SDimitry Andric FunctionType *FT = F.getFunctionType();
286681ad6265SDimitry Andric Check(FT->getNumParams() == 1, "wrong number of parameters", F);
286781ad6265SDimitry Andric Check(isa<PointerType>(FT->getParamType(0)),
2868fe6060f1SDimitry Andric "gc.get.pointer.offset operand must be a pointer", F);
286981ad6265SDimitry Andric Check(F.getReturnType()->isIntegerTy(),
2870fe6060f1SDimitry Andric "gc.get.pointer.offset must return integer", F);
2871fe6060f1SDimitry Andric break;
2872fe6060f1SDimitry Andric }
2873fe6060f1SDimitry Andric }
2874fe6060f1SDimitry Andric
28750b57cec5SDimitry Andric auto *N = F.getSubprogram();
28760b57cec5SDimitry Andric HasDebugInfo = (N != nullptr);
28770b57cec5SDimitry Andric if (!HasDebugInfo)
28780b57cec5SDimitry Andric return;
28790b57cec5SDimitry Andric
28805ffd83dbSDimitry Andric // Check that all !dbg attachments lead to back to N.
28810b57cec5SDimitry Andric //
28820b57cec5SDimitry Andric // FIXME: Check this incrementally while visiting !dbg attachments.
28830b57cec5SDimitry Andric // FIXME: Only check when N is the canonical subprogram for F.
28840b57cec5SDimitry Andric SmallPtrSet<const MDNode *, 32> Seen;
28850b57cec5SDimitry Andric auto VisitDebugLoc = [&](const Instruction &I, const MDNode *Node) {
28860b57cec5SDimitry Andric // Be careful about using DILocation here since we might be dealing with
28870b57cec5SDimitry Andric // broken code (this is the Verifier after all).
28880b57cec5SDimitry Andric const DILocation *DL = dyn_cast_or_null<DILocation>(Node);
28890b57cec5SDimitry Andric if (!DL)
28900b57cec5SDimitry Andric return;
28910b57cec5SDimitry Andric if (!Seen.insert(DL).second)
28920b57cec5SDimitry Andric return;
28930b57cec5SDimitry Andric
28940b57cec5SDimitry Andric Metadata *Parent = DL->getRawScope();
289581ad6265SDimitry Andric CheckDI(Parent && isa<DILocalScope>(Parent),
289681ad6265SDimitry Andric "DILocation's scope must be a DILocalScope", N, &F, &I, DL, Parent);
28975ffd83dbSDimitry Andric
28980b57cec5SDimitry Andric DILocalScope *Scope = DL->getInlinedAtScope();
289981ad6265SDimitry Andric Check(Scope, "Failed to find DILocalScope", DL);
29005ffd83dbSDimitry Andric
29015ffd83dbSDimitry Andric if (!Seen.insert(Scope).second)
29020b57cec5SDimitry Andric return;
29030b57cec5SDimitry Andric
29045ffd83dbSDimitry Andric DISubprogram *SP = Scope->getSubprogram();
29050b57cec5SDimitry Andric
29060b57cec5SDimitry Andric // Scope and SP could be the same MDNode and we don't want to skip
29070b57cec5SDimitry Andric // validation in that case
29080b57cec5SDimitry Andric if (SP && ((Scope != SP) && !Seen.insert(SP).second))
29090b57cec5SDimitry Andric return;
29100b57cec5SDimitry Andric
291181ad6265SDimitry Andric CheckDI(SP->describes(&F),
29120b57cec5SDimitry Andric "!dbg attachment points at wrong subprogram for function", N, &F,
29130b57cec5SDimitry Andric &I, DL, Scope, SP);
29140b57cec5SDimitry Andric };
29150b57cec5SDimitry Andric for (auto &BB : F)
29160b57cec5SDimitry Andric for (auto &I : BB) {
29170b57cec5SDimitry Andric VisitDebugLoc(I, I.getDebugLoc().getAsMDNode());
29180b57cec5SDimitry Andric // The llvm.loop annotations also contain two DILocations.
29190b57cec5SDimitry Andric if (auto MD = I.getMetadata(LLVMContext::MD_loop))
29200b57cec5SDimitry Andric for (unsigned i = 1; i < MD->getNumOperands(); ++i)
29210b57cec5SDimitry Andric VisitDebugLoc(I, dyn_cast_or_null<MDNode>(MD->getOperand(i)));
29220b57cec5SDimitry Andric if (BrokenDebugInfo)
29230b57cec5SDimitry Andric return;
29240b57cec5SDimitry Andric }
29250b57cec5SDimitry Andric }
29260b57cec5SDimitry Andric
29270b57cec5SDimitry Andric // verifyBasicBlock - Verify that a basic block is well formed...
29280b57cec5SDimitry Andric //
visitBasicBlock(BasicBlock & BB)29290b57cec5SDimitry Andric void Verifier::visitBasicBlock(BasicBlock &BB) {
29300b57cec5SDimitry Andric InstsInThisBlock.clear();
29315f757f3fSDimitry Andric ConvergenceVerifyHelper.visit(BB);
29320b57cec5SDimitry Andric
29330b57cec5SDimitry Andric // Ensure that basic blocks have terminators!
293481ad6265SDimitry Andric Check(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
29350b57cec5SDimitry Andric
29360b57cec5SDimitry Andric // Check constraints that this basic block imposes on all of the PHI nodes in
29370b57cec5SDimitry Andric // it.
29380b57cec5SDimitry Andric if (isa<PHINode>(BB.front())) {
2939e8d8bef9SDimitry Andric SmallVector<BasicBlock *, 8> Preds(predecessors(&BB));
29400b57cec5SDimitry Andric SmallVector<std::pair<BasicBlock*, Value*>, 8> Values;
29410b57cec5SDimitry Andric llvm::sort(Preds);
29420b57cec5SDimitry Andric for (const PHINode &PN : BB.phis()) {
294381ad6265SDimitry Andric Check(PN.getNumIncomingValues() == Preds.size(),
29440b57cec5SDimitry Andric "PHINode should have one entry for each predecessor of its "
29450b57cec5SDimitry Andric "parent basic block!",
29460b57cec5SDimitry Andric &PN);
29470b57cec5SDimitry Andric
29480b57cec5SDimitry Andric // Get and sort all incoming values in the PHI node...
29490b57cec5SDimitry Andric Values.clear();
29500b57cec5SDimitry Andric Values.reserve(PN.getNumIncomingValues());
29510b57cec5SDimitry Andric for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
29520b57cec5SDimitry Andric Values.push_back(
29530b57cec5SDimitry Andric std::make_pair(PN.getIncomingBlock(i), PN.getIncomingValue(i)));
29540b57cec5SDimitry Andric llvm::sort(Values);
29550b57cec5SDimitry Andric
29560b57cec5SDimitry Andric for (unsigned i = 0, e = Values.size(); i != e; ++i) {
29570b57cec5SDimitry Andric // Check to make sure that if there is more than one entry for a
29580b57cec5SDimitry Andric // particular basic block in this PHI node, that the incoming values are
29590b57cec5SDimitry Andric // all identical.
29600b57cec5SDimitry Andric //
296181ad6265SDimitry Andric Check(i == 0 || Values[i].first != Values[i - 1].first ||
29620b57cec5SDimitry Andric Values[i].second == Values[i - 1].second,
29630b57cec5SDimitry Andric "PHI node has multiple entries for the same basic block with "
29640b57cec5SDimitry Andric "different incoming values!",
29650b57cec5SDimitry Andric &PN, Values[i].first, Values[i].second, Values[i - 1].second);
29660b57cec5SDimitry Andric
29670b57cec5SDimitry Andric // Check to make sure that the predecessors and PHI node entries are
29680b57cec5SDimitry Andric // matched up.
296981ad6265SDimitry Andric Check(Values[i].first == Preds[i],
29700b57cec5SDimitry Andric "PHI node entries do not match predecessors!", &PN,
29710b57cec5SDimitry Andric Values[i].first, Preds[i]);
29720b57cec5SDimitry Andric }
29730b57cec5SDimitry Andric }
29740b57cec5SDimitry Andric }
29750b57cec5SDimitry Andric
29760b57cec5SDimitry Andric // Check that all instructions have their parent pointers set up correctly.
29770b57cec5SDimitry Andric for (auto &I : BB)
29780b57cec5SDimitry Andric {
297981ad6265SDimitry Andric Check(I.getParent() == &BB, "Instruction has bogus parent pointer!");
29800b57cec5SDimitry Andric }
29815f757f3fSDimitry Andric
29825f757f3fSDimitry Andric // Confirm that no issues arise from the debug program.
29835f757f3fSDimitry Andric if (BB.IsNewDbgInfoFormat) {
29845f757f3fSDimitry Andric // Configure the validate function to not fire assertions, instead print
29855f757f3fSDimitry Andric // errors and return true if there's a problem.
29865f757f3fSDimitry Andric bool RetVal = BB.validateDbgValues(false, true, OS);
29875f757f3fSDimitry Andric Check(!RetVal, "Invalid configuration of new-debug-info data found");
29885f757f3fSDimitry Andric }
29890b57cec5SDimitry Andric }
29900b57cec5SDimitry Andric
visitTerminator(Instruction & I)29910b57cec5SDimitry Andric void Verifier::visitTerminator(Instruction &I) {
29920b57cec5SDimitry Andric // Ensure that terminators only exist at the end of the basic block.
299381ad6265SDimitry Andric Check(&I == I.getParent()->getTerminator(),
29940b57cec5SDimitry Andric "Terminator found in the middle of a basic block!", I.getParent());
29950b57cec5SDimitry Andric visitInstruction(I);
29960b57cec5SDimitry Andric }
29970b57cec5SDimitry Andric
visitBranchInst(BranchInst & BI)29980b57cec5SDimitry Andric void Verifier::visitBranchInst(BranchInst &BI) {
29990b57cec5SDimitry Andric if (BI.isConditional()) {
300081ad6265SDimitry Andric Check(BI.getCondition()->getType()->isIntegerTy(1),
30010b57cec5SDimitry Andric "Branch condition is not 'i1' type!", &BI, BI.getCondition());
30020b57cec5SDimitry Andric }
30030b57cec5SDimitry Andric visitTerminator(BI);
30040b57cec5SDimitry Andric }
30050b57cec5SDimitry Andric
visitReturnInst(ReturnInst & RI)30060b57cec5SDimitry Andric void Verifier::visitReturnInst(ReturnInst &RI) {
30070b57cec5SDimitry Andric Function *F = RI.getParent()->getParent();
30080b57cec5SDimitry Andric unsigned N = RI.getNumOperands();
30090b57cec5SDimitry Andric if (F->getReturnType()->isVoidTy())
301081ad6265SDimitry Andric Check(N == 0,
30110b57cec5SDimitry Andric "Found return instr that returns non-void in Function of void "
30120b57cec5SDimitry Andric "return type!",
30130b57cec5SDimitry Andric &RI, F->getReturnType());
30140b57cec5SDimitry Andric else
301581ad6265SDimitry Andric Check(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(),
30160b57cec5SDimitry Andric "Function return type does not match operand "
30170b57cec5SDimitry Andric "type of return inst!",
30180b57cec5SDimitry Andric &RI, F->getReturnType());
30190b57cec5SDimitry Andric
30200b57cec5SDimitry Andric // Check to make sure that the return value has necessary properties for
30210b57cec5SDimitry Andric // terminators...
30220b57cec5SDimitry Andric visitTerminator(RI);
30230b57cec5SDimitry Andric }
30240b57cec5SDimitry Andric
visitSwitchInst(SwitchInst & SI)30250b57cec5SDimitry Andric void Verifier::visitSwitchInst(SwitchInst &SI) {
302681ad6265SDimitry Andric Check(SI.getType()->isVoidTy(), "Switch must have void result type!", &SI);
30270b57cec5SDimitry Andric // Check to make sure that all of the constants in the switch instruction
30280b57cec5SDimitry Andric // have the same type as the switched-on value.
30290b57cec5SDimitry Andric Type *SwitchTy = SI.getCondition()->getType();
30300b57cec5SDimitry Andric SmallPtrSet<ConstantInt*, 32> Constants;
30310b57cec5SDimitry Andric for (auto &Case : SI.cases()) {
3032bdd1243dSDimitry Andric Check(isa<ConstantInt>(SI.getOperand(Case.getCaseIndex() * 2 + 2)),
3033bdd1243dSDimitry Andric "Case value is not a constant integer.", &SI);
303481ad6265SDimitry Andric Check(Case.getCaseValue()->getType() == SwitchTy,
30350b57cec5SDimitry Andric "Switch constants must all be same type as switch value!", &SI);
303681ad6265SDimitry Andric Check(Constants.insert(Case.getCaseValue()).second,
30370b57cec5SDimitry Andric "Duplicate integer as switch case", &SI, Case.getCaseValue());
30380b57cec5SDimitry Andric }
30390b57cec5SDimitry Andric
30400b57cec5SDimitry Andric visitTerminator(SI);
30410b57cec5SDimitry Andric }
30420b57cec5SDimitry Andric
visitIndirectBrInst(IndirectBrInst & BI)30430b57cec5SDimitry Andric void Verifier::visitIndirectBrInst(IndirectBrInst &BI) {
304481ad6265SDimitry Andric Check(BI.getAddress()->getType()->isPointerTy(),
30450b57cec5SDimitry Andric "Indirectbr operand must have pointer type!", &BI);
30460b57cec5SDimitry Andric for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i)
304781ad6265SDimitry Andric Check(BI.getDestination(i)->getType()->isLabelTy(),
30480b57cec5SDimitry Andric "Indirectbr destinations must all have pointer type!", &BI);
30490b57cec5SDimitry Andric
30500b57cec5SDimitry Andric visitTerminator(BI);
30510b57cec5SDimitry Andric }
30520b57cec5SDimitry Andric
visitCallBrInst(CallBrInst & CBI)30530b57cec5SDimitry Andric void Verifier::visitCallBrInst(CallBrInst &CBI) {
305481ad6265SDimitry Andric Check(CBI.isInlineAsm(), "Callbr is currently only used for asm-goto!", &CBI);
3055fe6060f1SDimitry Andric const InlineAsm *IA = cast<InlineAsm>(CBI.getCalledOperand());
305681ad6265SDimitry Andric Check(!IA->canThrow(), "Unwinding from Callbr is not allowed");
30570b57cec5SDimitry Andric
305804eeddc0SDimitry Andric verifyInlineAsmCall(CBI);
30590b57cec5SDimitry Andric visitTerminator(CBI);
30600b57cec5SDimitry Andric }
30610b57cec5SDimitry Andric
visitSelectInst(SelectInst & SI)30620b57cec5SDimitry Andric void Verifier::visitSelectInst(SelectInst &SI) {
306381ad6265SDimitry Andric Check(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1),
30640b57cec5SDimitry Andric SI.getOperand(2)),
30650b57cec5SDimitry Andric "Invalid operands for select instruction!", &SI);
30660b57cec5SDimitry Andric
306781ad6265SDimitry Andric Check(SI.getTrueValue()->getType() == SI.getType(),
30680b57cec5SDimitry Andric "Select values must have same type as select instruction!", &SI);
30690b57cec5SDimitry Andric visitInstruction(SI);
30700b57cec5SDimitry Andric }
30710b57cec5SDimitry Andric
30720b57cec5SDimitry Andric /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
30730b57cec5SDimitry Andric /// a pass, if any exist, it's an error.
30740b57cec5SDimitry Andric ///
visitUserOp1(Instruction & I)30750b57cec5SDimitry Andric void Verifier::visitUserOp1(Instruction &I) {
307681ad6265SDimitry Andric Check(false, "User-defined operators should not live outside of a pass!", &I);
30770b57cec5SDimitry Andric }
30780b57cec5SDimitry Andric
visitTruncInst(TruncInst & I)30790b57cec5SDimitry Andric void Verifier::visitTruncInst(TruncInst &I) {
30800b57cec5SDimitry Andric // Get the source and destination types
30810b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType();
30820b57cec5SDimitry Andric Type *DestTy = I.getType();
30830b57cec5SDimitry Andric
30840b57cec5SDimitry Andric // Get the size of the types in bits, we'll need this later
30850b57cec5SDimitry Andric unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
30860b57cec5SDimitry Andric unsigned DestBitSize = DestTy->getScalarSizeInBits();
30870b57cec5SDimitry Andric
308881ad6265SDimitry Andric Check(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I);
308981ad6265SDimitry Andric Check(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I);
309081ad6265SDimitry Andric Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
30910b57cec5SDimitry Andric "trunc source and destination must both be a vector or neither", &I);
309281ad6265SDimitry Andric Check(SrcBitSize > DestBitSize, "DestTy too big for Trunc", &I);
30930b57cec5SDimitry Andric
30940b57cec5SDimitry Andric visitInstruction(I);
30950b57cec5SDimitry Andric }
30960b57cec5SDimitry Andric
visitZExtInst(ZExtInst & I)30970b57cec5SDimitry Andric void Verifier::visitZExtInst(ZExtInst &I) {
30980b57cec5SDimitry Andric // Get the source and destination types
30990b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType();
31000b57cec5SDimitry Andric Type *DestTy = I.getType();
31010b57cec5SDimitry Andric
31020b57cec5SDimitry Andric // Get the size of the types in bits, we'll need this later
310381ad6265SDimitry Andric Check(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I);
310481ad6265SDimitry Andric Check(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I);
310581ad6265SDimitry Andric Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
31060b57cec5SDimitry Andric "zext source and destination must both be a vector or neither", &I);
31070b57cec5SDimitry Andric unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
31080b57cec5SDimitry Andric unsigned DestBitSize = DestTy->getScalarSizeInBits();
31090b57cec5SDimitry Andric
311081ad6265SDimitry Andric Check(SrcBitSize < DestBitSize, "Type too small for ZExt", &I);
31110b57cec5SDimitry Andric
31120b57cec5SDimitry Andric visitInstruction(I);
31130b57cec5SDimitry Andric }
31140b57cec5SDimitry Andric
visitSExtInst(SExtInst & I)31150b57cec5SDimitry Andric void Verifier::visitSExtInst(SExtInst &I) {
31160b57cec5SDimitry Andric // Get the source and destination types
31170b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType();
31180b57cec5SDimitry Andric Type *DestTy = I.getType();
31190b57cec5SDimitry Andric
31200b57cec5SDimitry Andric // Get the size of the types in bits, we'll need this later
31210b57cec5SDimitry Andric unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
31220b57cec5SDimitry Andric unsigned DestBitSize = DestTy->getScalarSizeInBits();
31230b57cec5SDimitry Andric
312481ad6265SDimitry Andric Check(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I);
312581ad6265SDimitry Andric Check(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I);
312681ad6265SDimitry Andric Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
31270b57cec5SDimitry Andric "sext source and destination must both be a vector or neither", &I);
312881ad6265SDimitry Andric Check(SrcBitSize < DestBitSize, "Type too small for SExt", &I);
31290b57cec5SDimitry Andric
31300b57cec5SDimitry Andric visitInstruction(I);
31310b57cec5SDimitry Andric }
31320b57cec5SDimitry Andric
visitFPTruncInst(FPTruncInst & I)31330b57cec5SDimitry Andric void Verifier::visitFPTruncInst(FPTruncInst &I) {
31340b57cec5SDimitry Andric // Get the source and destination types
31350b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType();
31360b57cec5SDimitry Andric Type *DestTy = I.getType();
31370b57cec5SDimitry Andric // Get the size of the types in bits, we'll need this later
31380b57cec5SDimitry Andric unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
31390b57cec5SDimitry Andric unsigned DestBitSize = DestTy->getScalarSizeInBits();
31400b57cec5SDimitry Andric
314181ad6265SDimitry Andric Check(SrcTy->isFPOrFPVectorTy(), "FPTrunc only operates on FP", &I);
314281ad6265SDimitry Andric Check(DestTy->isFPOrFPVectorTy(), "FPTrunc only produces an FP", &I);
314381ad6265SDimitry Andric Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
31440b57cec5SDimitry Andric "fptrunc source and destination must both be a vector or neither", &I);
314581ad6265SDimitry Andric Check(SrcBitSize > DestBitSize, "DestTy too big for FPTrunc", &I);
31460b57cec5SDimitry Andric
31470b57cec5SDimitry Andric visitInstruction(I);
31480b57cec5SDimitry Andric }
31490b57cec5SDimitry Andric
visitFPExtInst(FPExtInst & I)31500b57cec5SDimitry Andric void Verifier::visitFPExtInst(FPExtInst &I) {
31510b57cec5SDimitry Andric // Get the source and destination types
31520b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType();
31530b57cec5SDimitry Andric Type *DestTy = I.getType();
31540b57cec5SDimitry Andric
31550b57cec5SDimitry Andric // Get the size of the types in bits, we'll need this later
31560b57cec5SDimitry Andric unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
31570b57cec5SDimitry Andric unsigned DestBitSize = DestTy->getScalarSizeInBits();
31580b57cec5SDimitry Andric
315981ad6265SDimitry Andric Check(SrcTy->isFPOrFPVectorTy(), "FPExt only operates on FP", &I);
316081ad6265SDimitry Andric Check(DestTy->isFPOrFPVectorTy(), "FPExt only produces an FP", &I);
316181ad6265SDimitry Andric Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
31620b57cec5SDimitry Andric "fpext source and destination must both be a vector or neither", &I);
316381ad6265SDimitry Andric Check(SrcBitSize < DestBitSize, "DestTy too small for FPExt", &I);
31640b57cec5SDimitry Andric
31650b57cec5SDimitry Andric visitInstruction(I);
31660b57cec5SDimitry Andric }
31670b57cec5SDimitry Andric
visitUIToFPInst(UIToFPInst & I)31680b57cec5SDimitry Andric void Verifier::visitUIToFPInst(UIToFPInst &I) {
31690b57cec5SDimitry Andric // Get the source and destination types
31700b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType();
31710b57cec5SDimitry Andric Type *DestTy = I.getType();
31720b57cec5SDimitry Andric
31730b57cec5SDimitry Andric bool SrcVec = SrcTy->isVectorTy();
31740b57cec5SDimitry Andric bool DstVec = DestTy->isVectorTy();
31750b57cec5SDimitry Andric
317681ad6265SDimitry Andric Check(SrcVec == DstVec,
31770b57cec5SDimitry Andric "UIToFP source and dest must both be vector or scalar", &I);
317881ad6265SDimitry Andric Check(SrcTy->isIntOrIntVectorTy(),
31790b57cec5SDimitry Andric "UIToFP source must be integer or integer vector", &I);
318081ad6265SDimitry Andric Check(DestTy->isFPOrFPVectorTy(), "UIToFP result must be FP or FP vector",
31810b57cec5SDimitry Andric &I);
31820b57cec5SDimitry Andric
31830b57cec5SDimitry Andric if (SrcVec && DstVec)
318481ad6265SDimitry Andric Check(cast<VectorType>(SrcTy)->getElementCount() ==
31855ffd83dbSDimitry Andric cast<VectorType>(DestTy)->getElementCount(),
31860b57cec5SDimitry Andric "UIToFP source and dest vector length mismatch", &I);
31870b57cec5SDimitry Andric
31880b57cec5SDimitry Andric visitInstruction(I);
31890b57cec5SDimitry Andric }
31900b57cec5SDimitry Andric
visitSIToFPInst(SIToFPInst & I)31910b57cec5SDimitry Andric void Verifier::visitSIToFPInst(SIToFPInst &I) {
31920b57cec5SDimitry Andric // Get the source and destination types
31930b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType();
31940b57cec5SDimitry Andric Type *DestTy = I.getType();
31950b57cec5SDimitry Andric
31960b57cec5SDimitry Andric bool SrcVec = SrcTy->isVectorTy();
31970b57cec5SDimitry Andric bool DstVec = DestTy->isVectorTy();
31980b57cec5SDimitry Andric
319981ad6265SDimitry Andric Check(SrcVec == DstVec,
32000b57cec5SDimitry Andric "SIToFP source and dest must both be vector or scalar", &I);
320181ad6265SDimitry Andric Check(SrcTy->isIntOrIntVectorTy(),
32020b57cec5SDimitry Andric "SIToFP source must be integer or integer vector", &I);
320381ad6265SDimitry Andric Check(DestTy->isFPOrFPVectorTy(), "SIToFP result must be FP or FP vector",
32040b57cec5SDimitry Andric &I);
32050b57cec5SDimitry Andric
32060b57cec5SDimitry Andric if (SrcVec && DstVec)
320781ad6265SDimitry Andric Check(cast<VectorType>(SrcTy)->getElementCount() ==
32085ffd83dbSDimitry Andric cast<VectorType>(DestTy)->getElementCount(),
32090b57cec5SDimitry Andric "SIToFP source and dest vector length mismatch", &I);
32100b57cec5SDimitry Andric
32110b57cec5SDimitry Andric visitInstruction(I);
32120b57cec5SDimitry Andric }
32130b57cec5SDimitry Andric
visitFPToUIInst(FPToUIInst & I)32140b57cec5SDimitry Andric void Verifier::visitFPToUIInst(FPToUIInst &I) {
32150b57cec5SDimitry Andric // Get the source and destination types
32160b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType();
32170b57cec5SDimitry Andric Type *DestTy = I.getType();
32180b57cec5SDimitry Andric
32190b57cec5SDimitry Andric bool SrcVec = SrcTy->isVectorTy();
32200b57cec5SDimitry Andric bool DstVec = DestTy->isVectorTy();
32210b57cec5SDimitry Andric
322281ad6265SDimitry Andric Check(SrcVec == DstVec,
32230b57cec5SDimitry Andric "FPToUI source and dest must both be vector or scalar", &I);
322481ad6265SDimitry Andric Check(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector", &I);
322581ad6265SDimitry Andric Check(DestTy->isIntOrIntVectorTy(),
32260b57cec5SDimitry Andric "FPToUI result must be integer or integer vector", &I);
32270b57cec5SDimitry Andric
32280b57cec5SDimitry Andric if (SrcVec && DstVec)
322981ad6265SDimitry Andric Check(cast<VectorType>(SrcTy)->getElementCount() ==
32305ffd83dbSDimitry Andric cast<VectorType>(DestTy)->getElementCount(),
32310b57cec5SDimitry Andric "FPToUI source and dest vector length mismatch", &I);
32320b57cec5SDimitry Andric
32330b57cec5SDimitry Andric visitInstruction(I);
32340b57cec5SDimitry Andric }
32350b57cec5SDimitry Andric
visitFPToSIInst(FPToSIInst & I)32360b57cec5SDimitry Andric void Verifier::visitFPToSIInst(FPToSIInst &I) {
32370b57cec5SDimitry Andric // Get the source and destination types
32380b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType();
32390b57cec5SDimitry Andric Type *DestTy = I.getType();
32400b57cec5SDimitry Andric
32410b57cec5SDimitry Andric bool SrcVec = SrcTy->isVectorTy();
32420b57cec5SDimitry Andric bool DstVec = DestTy->isVectorTy();
32430b57cec5SDimitry Andric
324481ad6265SDimitry Andric Check(SrcVec == DstVec,
32450b57cec5SDimitry Andric "FPToSI source and dest must both be vector or scalar", &I);
324681ad6265SDimitry Andric Check(SrcTy->isFPOrFPVectorTy(), "FPToSI source must be FP or FP vector", &I);
324781ad6265SDimitry Andric Check(DestTy->isIntOrIntVectorTy(),
32480b57cec5SDimitry Andric "FPToSI result must be integer or integer vector", &I);
32490b57cec5SDimitry Andric
32500b57cec5SDimitry Andric if (SrcVec && DstVec)
325181ad6265SDimitry Andric Check(cast<VectorType>(SrcTy)->getElementCount() ==
32525ffd83dbSDimitry Andric cast<VectorType>(DestTy)->getElementCount(),
32530b57cec5SDimitry Andric "FPToSI source and dest vector length mismatch", &I);
32540b57cec5SDimitry Andric
32550b57cec5SDimitry Andric visitInstruction(I);
32560b57cec5SDimitry Andric }
32570b57cec5SDimitry Andric
visitPtrToIntInst(PtrToIntInst & I)32580b57cec5SDimitry Andric void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
32590b57cec5SDimitry Andric // Get the source and destination types
32600b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType();
32610b57cec5SDimitry Andric Type *DestTy = I.getType();
32620b57cec5SDimitry Andric
326381ad6265SDimitry Andric Check(SrcTy->isPtrOrPtrVectorTy(), "PtrToInt source must be pointer", &I);
32640b57cec5SDimitry Andric
326581ad6265SDimitry Andric Check(DestTy->isIntOrIntVectorTy(), "PtrToInt result must be integral", &I);
326681ad6265SDimitry Andric Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), "PtrToInt type mismatch",
32670b57cec5SDimitry Andric &I);
32680b57cec5SDimitry Andric
32690b57cec5SDimitry Andric if (SrcTy->isVectorTy()) {
32705ffd83dbSDimitry Andric auto *VSrc = cast<VectorType>(SrcTy);
32715ffd83dbSDimitry Andric auto *VDest = cast<VectorType>(DestTy);
327281ad6265SDimitry Andric Check(VSrc->getElementCount() == VDest->getElementCount(),
32730b57cec5SDimitry Andric "PtrToInt Vector width mismatch", &I);
32740b57cec5SDimitry Andric }
32750b57cec5SDimitry Andric
32760b57cec5SDimitry Andric visitInstruction(I);
32770b57cec5SDimitry Andric }
32780b57cec5SDimitry Andric
visitIntToPtrInst(IntToPtrInst & I)32790b57cec5SDimitry Andric void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
32800b57cec5SDimitry Andric // Get the source and destination types
32810b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType();
32820b57cec5SDimitry Andric Type *DestTy = I.getType();
32830b57cec5SDimitry Andric
328481ad6265SDimitry Andric Check(SrcTy->isIntOrIntVectorTy(), "IntToPtr source must be an integral", &I);
328581ad6265SDimitry Andric Check(DestTy->isPtrOrPtrVectorTy(), "IntToPtr result must be a pointer", &I);
32860b57cec5SDimitry Andric
328781ad6265SDimitry Andric Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), "IntToPtr type mismatch",
32880b57cec5SDimitry Andric &I);
32890b57cec5SDimitry Andric if (SrcTy->isVectorTy()) {
32905ffd83dbSDimitry Andric auto *VSrc = cast<VectorType>(SrcTy);
32915ffd83dbSDimitry Andric auto *VDest = cast<VectorType>(DestTy);
329281ad6265SDimitry Andric Check(VSrc->getElementCount() == VDest->getElementCount(),
32930b57cec5SDimitry Andric "IntToPtr Vector width mismatch", &I);
32940b57cec5SDimitry Andric }
32950b57cec5SDimitry Andric visitInstruction(I);
32960b57cec5SDimitry Andric }
32970b57cec5SDimitry Andric
visitBitCastInst(BitCastInst & I)32980b57cec5SDimitry Andric void Verifier::visitBitCastInst(BitCastInst &I) {
329981ad6265SDimitry Andric Check(
33000b57cec5SDimitry Andric CastInst::castIsValid(Instruction::BitCast, I.getOperand(0), I.getType()),
33010b57cec5SDimitry Andric "Invalid bitcast", &I);
33020b57cec5SDimitry Andric visitInstruction(I);
33030b57cec5SDimitry Andric }
33040b57cec5SDimitry Andric
visitAddrSpaceCastInst(AddrSpaceCastInst & I)33050b57cec5SDimitry Andric void Verifier::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
33060b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType();
33070b57cec5SDimitry Andric Type *DestTy = I.getType();
33080b57cec5SDimitry Andric
330981ad6265SDimitry Andric Check(SrcTy->isPtrOrPtrVectorTy(), "AddrSpaceCast source must be a pointer",
33100b57cec5SDimitry Andric &I);
331181ad6265SDimitry Andric Check(DestTy->isPtrOrPtrVectorTy(), "AddrSpaceCast result must be a pointer",
33120b57cec5SDimitry Andric &I);
331381ad6265SDimitry Andric Check(SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace(),
33140b57cec5SDimitry Andric "AddrSpaceCast must be between different address spaces", &I);
33155ffd83dbSDimitry Andric if (auto *SrcVTy = dyn_cast<VectorType>(SrcTy))
331681ad6265SDimitry Andric Check(SrcVTy->getElementCount() ==
3317e8d8bef9SDimitry Andric cast<VectorType>(DestTy)->getElementCount(),
33180b57cec5SDimitry Andric "AddrSpaceCast vector pointer number of elements mismatch", &I);
33190b57cec5SDimitry Andric visitInstruction(I);
33200b57cec5SDimitry Andric }
33210b57cec5SDimitry Andric
33220b57cec5SDimitry Andric /// visitPHINode - Ensure that a PHI node is well formed.
33230b57cec5SDimitry Andric ///
visitPHINode(PHINode & PN)33240b57cec5SDimitry Andric void Verifier::visitPHINode(PHINode &PN) {
33250b57cec5SDimitry Andric // Ensure that the PHI nodes are all grouped together at the top of the block.
33260b57cec5SDimitry Andric // This can be tested by checking whether the instruction before this is
33270b57cec5SDimitry Andric // either nonexistent (because this is begin()) or is a PHI node. If not,
33280b57cec5SDimitry Andric // then there is some other instruction before a PHI.
332981ad6265SDimitry Andric Check(&PN == &PN.getParent()->front() ||
33300b57cec5SDimitry Andric isa<PHINode>(--BasicBlock::iterator(&PN)),
33310b57cec5SDimitry Andric "PHI nodes not grouped at top of basic block!", &PN, PN.getParent());
33320b57cec5SDimitry Andric
33330b57cec5SDimitry Andric // Check that a PHI doesn't yield a Token.
333481ad6265SDimitry Andric Check(!PN.getType()->isTokenTy(), "PHI nodes cannot have token type!");
33350b57cec5SDimitry Andric
33360b57cec5SDimitry Andric // Check that all of the values of the PHI node have the same type as the
33370b57cec5SDimitry Andric // result, and that the incoming blocks are really basic blocks.
33380b57cec5SDimitry Andric for (Value *IncValue : PN.incoming_values()) {
333981ad6265SDimitry Andric Check(PN.getType() == IncValue->getType(),
33400b57cec5SDimitry Andric "PHI node operands are not the same type as the result!", &PN);
33410b57cec5SDimitry Andric }
33420b57cec5SDimitry Andric
33430b57cec5SDimitry Andric // All other PHI node constraints are checked in the visitBasicBlock method.
33440b57cec5SDimitry Andric
33450b57cec5SDimitry Andric visitInstruction(PN);
33460b57cec5SDimitry Andric }
33470b57cec5SDimitry Andric
visitCallBase(CallBase & Call)33480b57cec5SDimitry Andric void Verifier::visitCallBase(CallBase &Call) {
334981ad6265SDimitry Andric Check(Call.getCalledOperand()->getType()->isPointerTy(),
33500b57cec5SDimitry Andric "Called function must be a pointer!", Call);
33510b57cec5SDimitry Andric FunctionType *FTy = Call.getFunctionType();
33520b57cec5SDimitry Andric
33530b57cec5SDimitry Andric // Verify that the correct number of arguments are being passed
33540b57cec5SDimitry Andric if (FTy->isVarArg())
335581ad6265SDimitry Andric Check(Call.arg_size() >= FTy->getNumParams(),
335681ad6265SDimitry Andric "Called function requires more parameters than were provided!", Call);
33570b57cec5SDimitry Andric else
335881ad6265SDimitry Andric Check(Call.arg_size() == FTy->getNumParams(),
33590b57cec5SDimitry Andric "Incorrect number of arguments passed to called function!", Call);
33600b57cec5SDimitry Andric
33610b57cec5SDimitry Andric // Verify that all arguments to the call match the function type.
33620b57cec5SDimitry Andric for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
336381ad6265SDimitry Andric Check(Call.getArgOperand(i)->getType() == FTy->getParamType(i),
33640b57cec5SDimitry Andric "Call parameter type does not match function signature!",
33650b57cec5SDimitry Andric Call.getArgOperand(i), FTy->getParamType(i), Call);
33660b57cec5SDimitry Andric
33670b57cec5SDimitry Andric AttributeList Attrs = Call.getAttributes();
33680b57cec5SDimitry Andric
336981ad6265SDimitry Andric Check(verifyAttributeCount(Attrs, Call.arg_size()),
33700b57cec5SDimitry Andric "Attribute after last parameter!", Call);
33710b57cec5SDimitry Andric
3372bdd1243dSDimitry Andric Function *Callee =
3373bdd1243dSDimitry Andric dyn_cast<Function>(Call.getCalledOperand()->stripPointerCasts());
3374bdd1243dSDimitry Andric bool IsIntrinsic = Callee && Callee->isIntrinsic();
3375bdd1243dSDimitry Andric if (IsIntrinsic)
3376bdd1243dSDimitry Andric Check(Callee->getValueType() == FTy,
3377bdd1243dSDimitry Andric "Intrinsic called with incompatible signature", Call);
3378bdd1243dSDimitry Andric
337906c3fb27SDimitry Andric // Disallow calls to functions with the amdgpu_cs_chain[_preserve] calling
338006c3fb27SDimitry Andric // convention.
338106c3fb27SDimitry Andric auto CC = Call.getCallingConv();
338206c3fb27SDimitry Andric Check(CC != CallingConv::AMDGPU_CS_Chain &&
338306c3fb27SDimitry Andric CC != CallingConv::AMDGPU_CS_ChainPreserve,
338406c3fb27SDimitry Andric "Direct calls to amdgpu_cs_chain/amdgpu_cs_chain_preserve functions "
338506c3fb27SDimitry Andric "not allowed. Please use the @llvm.amdgpu.cs.chain intrinsic instead.",
338606c3fb27SDimitry Andric Call);
338706c3fb27SDimitry Andric
338881ad6265SDimitry Andric auto VerifyTypeAlign = [&](Type *Ty, const Twine &Message) {
338981ad6265SDimitry Andric if (!Ty->isSized())
339081ad6265SDimitry Andric return;
339181ad6265SDimitry Andric Align ABIAlign = DL.getABITypeAlign(Ty);
339281ad6265SDimitry Andric Align MaxAlign(ParamMaxAlignment);
339381ad6265SDimitry Andric Check(ABIAlign <= MaxAlign,
339481ad6265SDimitry Andric "Incorrect alignment of " + Message + " to called function!", Call);
339581ad6265SDimitry Andric };
339681ad6265SDimitry Andric
3397bdd1243dSDimitry Andric if (!IsIntrinsic) {
339881ad6265SDimitry Andric VerifyTypeAlign(FTy->getReturnType(), "return type");
339981ad6265SDimitry Andric for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
340081ad6265SDimitry Andric Type *Ty = FTy->getParamType(i);
340181ad6265SDimitry Andric VerifyTypeAlign(Ty, "argument passed");
340281ad6265SDimitry Andric }
3403bdd1243dSDimitry Andric }
34040b57cec5SDimitry Andric
3405349cc55cSDimitry Andric if (Attrs.hasFnAttr(Attribute::Speculatable)) {
34060b57cec5SDimitry Andric // Don't allow speculatable on call sites, unless the underlying function
34070b57cec5SDimitry Andric // declaration is also speculatable.
340881ad6265SDimitry Andric Check(Callee && Callee->isSpeculatable(),
34090b57cec5SDimitry Andric "speculatable attribute may not apply to call sites", Call);
34100b57cec5SDimitry Andric }
34110b57cec5SDimitry Andric
3412349cc55cSDimitry Andric if (Attrs.hasFnAttr(Attribute::Preallocated)) {
341381ad6265SDimitry Andric Check(Call.getCalledFunction()->getIntrinsicID() ==
34145ffd83dbSDimitry Andric Intrinsic::call_preallocated_arg,
34155ffd83dbSDimitry Andric "preallocated as a call site attribute can only be on "
34165ffd83dbSDimitry Andric "llvm.call.preallocated.arg");
34175ffd83dbSDimitry Andric }
34185ffd83dbSDimitry Andric
34190b57cec5SDimitry Andric // Verify call attributes.
342004eeddc0SDimitry Andric verifyFunctionAttrs(FTy, Attrs, &Call, IsIntrinsic, Call.isInlineAsm());
34210b57cec5SDimitry Andric
34220b57cec5SDimitry Andric // Conservatively check the inalloca argument.
34230b57cec5SDimitry Andric // We have a bug if we can find that there is an underlying alloca without
34240b57cec5SDimitry Andric // inalloca.
34250b57cec5SDimitry Andric if (Call.hasInAllocaArgument()) {
34260b57cec5SDimitry Andric Value *InAllocaArg = Call.getArgOperand(FTy->getNumParams() - 1);
34270b57cec5SDimitry Andric if (auto AI = dyn_cast<AllocaInst>(InAllocaArg->stripInBoundsOffsets()))
342881ad6265SDimitry Andric Check(AI->isUsedWithInAlloca(),
34290b57cec5SDimitry Andric "inalloca argument for call has mismatched alloca", AI, Call);
34300b57cec5SDimitry Andric }
34310b57cec5SDimitry Andric
34320b57cec5SDimitry Andric // For each argument of the callsite, if it has the swifterror argument,
34330b57cec5SDimitry Andric // make sure the underlying alloca/parameter it comes from has a swifterror as
34340b57cec5SDimitry Andric // well.
34350b57cec5SDimitry Andric for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
34360b57cec5SDimitry Andric if (Call.paramHasAttr(i, Attribute::SwiftError)) {
34370b57cec5SDimitry Andric Value *SwiftErrorArg = Call.getArgOperand(i);
34380b57cec5SDimitry Andric if (auto AI = dyn_cast<AllocaInst>(SwiftErrorArg->stripInBoundsOffsets())) {
343981ad6265SDimitry Andric Check(AI->isSwiftError(),
34400b57cec5SDimitry Andric "swifterror argument for call has mismatched alloca", AI, Call);
34410b57cec5SDimitry Andric continue;
34420b57cec5SDimitry Andric }
34430b57cec5SDimitry Andric auto ArgI = dyn_cast<Argument>(SwiftErrorArg);
344481ad6265SDimitry Andric Check(ArgI, "swifterror argument should come from an alloca or parameter",
34450b57cec5SDimitry Andric SwiftErrorArg, Call);
344681ad6265SDimitry Andric Check(ArgI->hasSwiftErrorAttr(),
34470b57cec5SDimitry Andric "swifterror argument for call has mismatched parameter", ArgI,
34480b57cec5SDimitry Andric Call);
34490b57cec5SDimitry Andric }
34500b57cec5SDimitry Andric
3451349cc55cSDimitry Andric if (Attrs.hasParamAttr(i, Attribute::ImmArg)) {
34520b57cec5SDimitry Andric // Don't allow immarg on call sites, unless the underlying declaration
34530b57cec5SDimitry Andric // also has the matching immarg.
345481ad6265SDimitry Andric Check(Callee && Callee->hasParamAttribute(i, Attribute::ImmArg),
345581ad6265SDimitry Andric "immarg may not apply only to call sites", Call.getArgOperand(i),
345681ad6265SDimitry Andric Call);
34570b57cec5SDimitry Andric }
34580b57cec5SDimitry Andric
34590b57cec5SDimitry Andric if (Call.paramHasAttr(i, Attribute::ImmArg)) {
34600b57cec5SDimitry Andric Value *ArgVal = Call.getArgOperand(i);
346181ad6265SDimitry Andric Check(isa<ConstantInt>(ArgVal) || isa<ConstantFP>(ArgVal),
34620b57cec5SDimitry Andric "immarg operand has non-immediate parameter", ArgVal, Call);
34630b57cec5SDimitry Andric }
34645ffd83dbSDimitry Andric
34655ffd83dbSDimitry Andric if (Call.paramHasAttr(i, Attribute::Preallocated)) {
34665ffd83dbSDimitry Andric Value *ArgVal = Call.getArgOperand(i);
34675ffd83dbSDimitry Andric bool hasOB =
34685ffd83dbSDimitry Andric Call.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0;
34695ffd83dbSDimitry Andric bool isMustTail = Call.isMustTailCall();
347081ad6265SDimitry Andric Check(hasOB != isMustTail,
34715ffd83dbSDimitry Andric "preallocated operand either requires a preallocated bundle or "
34725ffd83dbSDimitry Andric "the call to be musttail (but not both)",
34735ffd83dbSDimitry Andric ArgVal, Call);
34745ffd83dbSDimitry Andric }
34750b57cec5SDimitry Andric }
34760b57cec5SDimitry Andric
34770b57cec5SDimitry Andric if (FTy->isVarArg()) {
34780b57cec5SDimitry Andric // FIXME? is 'nest' even legal here?
34790b57cec5SDimitry Andric bool SawNest = false;
34800b57cec5SDimitry Andric bool SawReturned = false;
34810b57cec5SDimitry Andric
34820b57cec5SDimitry Andric for (unsigned Idx = 0; Idx < FTy->getNumParams(); ++Idx) {
3483349cc55cSDimitry Andric if (Attrs.hasParamAttr(Idx, Attribute::Nest))
34840b57cec5SDimitry Andric SawNest = true;
3485349cc55cSDimitry Andric if (Attrs.hasParamAttr(Idx, Attribute::Returned))
34860b57cec5SDimitry Andric SawReturned = true;
34870b57cec5SDimitry Andric }
34880b57cec5SDimitry Andric
34890b57cec5SDimitry Andric // Check attributes on the varargs part.
34900b57cec5SDimitry Andric for (unsigned Idx = FTy->getNumParams(); Idx < Call.arg_size(); ++Idx) {
34910b57cec5SDimitry Andric Type *Ty = Call.getArgOperand(Idx)->getType();
3492349cc55cSDimitry Andric AttributeSet ArgAttrs = Attrs.getParamAttrs(Idx);
34930b57cec5SDimitry Andric verifyParameterAttrs(ArgAttrs, Ty, &Call);
34940b57cec5SDimitry Andric
34950b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::Nest)) {
349681ad6265SDimitry Andric Check(!SawNest, "More than one parameter has attribute nest!", Call);
34970b57cec5SDimitry Andric SawNest = true;
34980b57cec5SDimitry Andric }
34990b57cec5SDimitry Andric
35000b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::Returned)) {
350181ad6265SDimitry Andric Check(!SawReturned, "More than one parameter has attribute returned!",
35020b57cec5SDimitry Andric Call);
350381ad6265SDimitry Andric Check(Ty->canLosslesslyBitCastTo(FTy->getReturnType()),
35040b57cec5SDimitry Andric "Incompatible argument and return types for 'returned' "
35050b57cec5SDimitry Andric "attribute",
35060b57cec5SDimitry Andric Call);
35070b57cec5SDimitry Andric SawReturned = true;
35080b57cec5SDimitry Andric }
35090b57cec5SDimitry Andric
35100b57cec5SDimitry Andric // Statepoint intrinsic is vararg but the wrapped function may be not.
35110b57cec5SDimitry Andric // Allow sret here and check the wrapped function in verifyStatepoint.
35120b57cec5SDimitry Andric if (!Call.getCalledFunction() ||
35130b57cec5SDimitry Andric Call.getCalledFunction()->getIntrinsicID() !=
35140b57cec5SDimitry Andric Intrinsic::experimental_gc_statepoint)
351581ad6265SDimitry Andric Check(!ArgAttrs.hasAttribute(Attribute::StructRet),
35160b57cec5SDimitry Andric "Attribute 'sret' cannot be used for vararg call arguments!",
35170b57cec5SDimitry Andric Call);
35180b57cec5SDimitry Andric
35190b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::InAlloca))
352081ad6265SDimitry Andric Check(Idx == Call.arg_size() - 1,
35210b57cec5SDimitry Andric "inalloca isn't on the last argument!", Call);
35220b57cec5SDimitry Andric }
35230b57cec5SDimitry Andric }
35240b57cec5SDimitry Andric
35250b57cec5SDimitry Andric // Verify that there's no metadata unless it's a direct call to an intrinsic.
35260b57cec5SDimitry Andric if (!IsIntrinsic) {
35270b57cec5SDimitry Andric for (Type *ParamTy : FTy->params()) {
352881ad6265SDimitry Andric Check(!ParamTy->isMetadataTy(),
35290b57cec5SDimitry Andric "Function has metadata parameter but isn't an intrinsic", Call);
353081ad6265SDimitry Andric Check(!ParamTy->isTokenTy(),
35310b57cec5SDimitry Andric "Function has token parameter but isn't an intrinsic", Call);
35320b57cec5SDimitry Andric }
35330b57cec5SDimitry Andric }
35340b57cec5SDimitry Andric
35350b57cec5SDimitry Andric // Verify that indirect calls don't return tokens.
3536fe6060f1SDimitry Andric if (!Call.getCalledFunction()) {
353781ad6265SDimitry Andric Check(!FTy->getReturnType()->isTokenTy(),
35380b57cec5SDimitry Andric "Return type cannot be token for indirect call!");
353981ad6265SDimitry Andric Check(!FTy->getReturnType()->isX86_AMXTy(),
3540fe6060f1SDimitry Andric "Return type cannot be x86_amx for indirect call!");
3541fe6060f1SDimitry Andric }
35420b57cec5SDimitry Andric
35430b57cec5SDimitry Andric if (Function *F = Call.getCalledFunction())
35440b57cec5SDimitry Andric if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
35450b57cec5SDimitry Andric visitIntrinsicCall(ID, Call);
35460b57cec5SDimitry Andric
3547480093f4SDimitry Andric // Verify that a callsite has at most one "deopt", at most one "funclet", at
354881ad6265SDimitry Andric // most one "gc-transition", at most one "cfguardtarget", at most one
354981ad6265SDimitry Andric // "preallocated" operand bundle, and at most one "ptrauth" operand bundle.
35500b57cec5SDimitry Andric bool FoundDeoptBundle = false, FoundFuncletBundle = false,
35515ffd83dbSDimitry Andric FoundGCTransitionBundle = false, FoundCFGuardTargetBundle = false,
3552fe6060f1SDimitry Andric FoundPreallocatedBundle = false, FoundGCLiveBundle = false,
3553bdd1243dSDimitry Andric FoundPtrauthBundle = false, FoundKCFIBundle = false,
3554fe6060f1SDimitry Andric FoundAttachedCallBundle = false;
35550b57cec5SDimitry Andric for (unsigned i = 0, e = Call.getNumOperandBundles(); i < e; ++i) {
35560b57cec5SDimitry Andric OperandBundleUse BU = Call.getOperandBundleAt(i);
35570b57cec5SDimitry Andric uint32_t Tag = BU.getTagID();
35580b57cec5SDimitry Andric if (Tag == LLVMContext::OB_deopt) {
355981ad6265SDimitry Andric Check(!FoundDeoptBundle, "Multiple deopt operand bundles", Call);
35600b57cec5SDimitry Andric FoundDeoptBundle = true;
35610b57cec5SDimitry Andric } else if (Tag == LLVMContext::OB_gc_transition) {
356281ad6265SDimitry Andric Check(!FoundGCTransitionBundle, "Multiple gc-transition operand bundles",
35630b57cec5SDimitry Andric Call);
35640b57cec5SDimitry Andric FoundGCTransitionBundle = true;
35650b57cec5SDimitry Andric } else if (Tag == LLVMContext::OB_funclet) {
356681ad6265SDimitry Andric Check(!FoundFuncletBundle, "Multiple funclet operand bundles", Call);
35670b57cec5SDimitry Andric FoundFuncletBundle = true;
356881ad6265SDimitry Andric Check(BU.Inputs.size() == 1,
35690b57cec5SDimitry Andric "Expected exactly one funclet bundle operand", Call);
357081ad6265SDimitry Andric Check(isa<FuncletPadInst>(BU.Inputs.front()),
35710b57cec5SDimitry Andric "Funclet bundle operands should correspond to a FuncletPadInst",
35720b57cec5SDimitry Andric Call);
3573480093f4SDimitry Andric } else if (Tag == LLVMContext::OB_cfguardtarget) {
357481ad6265SDimitry Andric Check(!FoundCFGuardTargetBundle, "Multiple CFGuardTarget operand bundles",
357581ad6265SDimitry Andric Call);
3576480093f4SDimitry Andric FoundCFGuardTargetBundle = true;
357781ad6265SDimitry Andric Check(BU.Inputs.size() == 1,
3578480093f4SDimitry Andric "Expected exactly one cfguardtarget bundle operand", Call);
357981ad6265SDimitry Andric } else if (Tag == LLVMContext::OB_ptrauth) {
358081ad6265SDimitry Andric Check(!FoundPtrauthBundle, "Multiple ptrauth operand bundles", Call);
358181ad6265SDimitry Andric FoundPtrauthBundle = true;
358281ad6265SDimitry Andric Check(BU.Inputs.size() == 2,
358381ad6265SDimitry Andric "Expected exactly two ptrauth bundle operands", Call);
358481ad6265SDimitry Andric Check(isa<ConstantInt>(BU.Inputs[0]) &&
358581ad6265SDimitry Andric BU.Inputs[0]->getType()->isIntegerTy(32),
358681ad6265SDimitry Andric "Ptrauth bundle key operand must be an i32 constant", Call);
358781ad6265SDimitry Andric Check(BU.Inputs[1]->getType()->isIntegerTy(64),
358881ad6265SDimitry Andric "Ptrauth bundle discriminator operand must be an i64", Call);
3589bdd1243dSDimitry Andric } else if (Tag == LLVMContext::OB_kcfi) {
3590bdd1243dSDimitry Andric Check(!FoundKCFIBundle, "Multiple kcfi operand bundles", Call);
3591bdd1243dSDimitry Andric FoundKCFIBundle = true;
3592bdd1243dSDimitry Andric Check(BU.Inputs.size() == 1, "Expected exactly one kcfi bundle operand",
3593bdd1243dSDimitry Andric Call);
3594bdd1243dSDimitry Andric Check(isa<ConstantInt>(BU.Inputs[0]) &&
3595bdd1243dSDimitry Andric BU.Inputs[0]->getType()->isIntegerTy(32),
3596bdd1243dSDimitry Andric "Kcfi bundle operand must be an i32 constant", Call);
35975ffd83dbSDimitry Andric } else if (Tag == LLVMContext::OB_preallocated) {
359881ad6265SDimitry Andric Check(!FoundPreallocatedBundle, "Multiple preallocated operand bundles",
35995ffd83dbSDimitry Andric Call);
36005ffd83dbSDimitry Andric FoundPreallocatedBundle = true;
360181ad6265SDimitry Andric Check(BU.Inputs.size() == 1,
36025ffd83dbSDimitry Andric "Expected exactly one preallocated bundle operand", Call);
36035ffd83dbSDimitry Andric auto Input = dyn_cast<IntrinsicInst>(BU.Inputs.front());
360481ad6265SDimitry Andric Check(Input &&
36055ffd83dbSDimitry Andric Input->getIntrinsicID() == Intrinsic::call_preallocated_setup,
36065ffd83dbSDimitry Andric "\"preallocated\" argument must be a token from "
36075ffd83dbSDimitry Andric "llvm.call.preallocated.setup",
36085ffd83dbSDimitry Andric Call);
36095ffd83dbSDimitry Andric } else if (Tag == LLVMContext::OB_gc_live) {
361081ad6265SDimitry Andric Check(!FoundGCLiveBundle, "Multiple gc-live operand bundles", Call);
36115ffd83dbSDimitry Andric FoundGCLiveBundle = true;
3612fe6060f1SDimitry Andric } else if (Tag == LLVMContext::OB_clang_arc_attachedcall) {
361381ad6265SDimitry Andric Check(!FoundAttachedCallBundle,
3614fe6060f1SDimitry Andric "Multiple \"clang.arc.attachedcall\" operand bundles", Call);
3615fe6060f1SDimitry Andric FoundAttachedCallBundle = true;
3616349cc55cSDimitry Andric verifyAttachedCallBundle(Call, BU);
36170b57cec5SDimitry Andric }
36180b57cec5SDimitry Andric }
36190b57cec5SDimitry Andric
362081ad6265SDimitry Andric // Verify that callee and callsite agree on whether to use pointer auth.
362181ad6265SDimitry Andric Check(!(Call.getCalledFunction() && FoundPtrauthBundle),
362281ad6265SDimitry Andric "Direct call cannot have a ptrauth bundle", Call);
362381ad6265SDimitry Andric
36240b57cec5SDimitry Andric // Verify that each inlinable callsite of a debug-info-bearing function in a
36250b57cec5SDimitry Andric // debug-info-bearing function has a debug location attached to it. Failure to
3626bdd1243dSDimitry Andric // do so causes assertion failures when the inliner sets up inline scope info
3627bdd1243dSDimitry Andric // (Interposable functions are not inlinable, neither are functions without
3628bdd1243dSDimitry Andric // definitions.)
36290b57cec5SDimitry Andric if (Call.getFunction()->getSubprogram() && Call.getCalledFunction() &&
3630bdd1243dSDimitry Andric !Call.getCalledFunction()->isInterposable() &&
3631bdd1243dSDimitry Andric !Call.getCalledFunction()->isDeclaration() &&
36320b57cec5SDimitry Andric Call.getCalledFunction()->getSubprogram())
363381ad6265SDimitry Andric CheckDI(Call.getDebugLoc(),
36340b57cec5SDimitry Andric "inlinable function call in a function with "
36350b57cec5SDimitry Andric "debug info must have a !dbg location",
36360b57cec5SDimitry Andric Call);
36370b57cec5SDimitry Andric
363804eeddc0SDimitry Andric if (Call.isInlineAsm())
363904eeddc0SDimitry Andric verifyInlineAsmCall(Call);
364004eeddc0SDimitry Andric
36415f757f3fSDimitry Andric ConvergenceVerifyHelper.visit(Call);
364206c3fb27SDimitry Andric
36430b57cec5SDimitry Andric visitInstruction(Call);
36440b57cec5SDimitry Andric }
36450b57cec5SDimitry Andric
verifyTailCCMustTailAttrs(const AttrBuilder & Attrs,StringRef Context)36460eae32dcSDimitry Andric void Verifier::verifyTailCCMustTailAttrs(const AttrBuilder &Attrs,
3647fe6060f1SDimitry Andric StringRef Context) {
364881ad6265SDimitry Andric Check(!Attrs.contains(Attribute::InAlloca),
3649fe6060f1SDimitry Andric Twine("inalloca attribute not allowed in ") + Context);
365081ad6265SDimitry Andric Check(!Attrs.contains(Attribute::InReg),
3651fe6060f1SDimitry Andric Twine("inreg attribute not allowed in ") + Context);
365281ad6265SDimitry Andric Check(!Attrs.contains(Attribute::SwiftError),
3653fe6060f1SDimitry Andric Twine("swifterror attribute not allowed in ") + Context);
365481ad6265SDimitry Andric Check(!Attrs.contains(Attribute::Preallocated),
3655fe6060f1SDimitry Andric Twine("preallocated attribute not allowed in ") + Context);
365681ad6265SDimitry Andric Check(!Attrs.contains(Attribute::ByRef),
3657fe6060f1SDimitry Andric Twine("byref attribute not allowed in ") + Context);
3658fe6060f1SDimitry Andric }
3659fe6060f1SDimitry Andric
36600b57cec5SDimitry Andric /// Two types are "congruent" if they are identical, or if they are both pointer
36610b57cec5SDimitry Andric /// types with different pointee types and the same address space.
isTypeCongruent(Type * L,Type * R)36620b57cec5SDimitry Andric static bool isTypeCongruent(Type *L, Type *R) {
36630b57cec5SDimitry Andric if (L == R)
36640b57cec5SDimitry Andric return true;
36650b57cec5SDimitry Andric PointerType *PL = dyn_cast<PointerType>(L);
36660b57cec5SDimitry Andric PointerType *PR = dyn_cast<PointerType>(R);
36670b57cec5SDimitry Andric if (!PL || !PR)
36680b57cec5SDimitry Andric return false;
36690b57cec5SDimitry Andric return PL->getAddressSpace() == PR->getAddressSpace();
36700b57cec5SDimitry Andric }
36710b57cec5SDimitry Andric
getParameterABIAttributes(LLVMContext & C,unsigned I,AttributeList Attrs)367204eeddc0SDimitry Andric static AttrBuilder getParameterABIAttributes(LLVMContext& C, unsigned I, AttributeList Attrs) {
36730b57cec5SDimitry Andric static const Attribute::AttrKind ABIAttrs[] = {
36740b57cec5SDimitry Andric Attribute::StructRet, Attribute::ByVal, Attribute::InAlloca,
3675fe6060f1SDimitry Andric Attribute::InReg, Attribute::StackAlignment, Attribute::SwiftSelf,
3676fe6060f1SDimitry Andric Attribute::SwiftAsync, Attribute::SwiftError, Attribute::Preallocated,
3677fe6060f1SDimitry Andric Attribute::ByRef};
367804eeddc0SDimitry Andric AttrBuilder Copy(C);
36790b57cec5SDimitry Andric for (auto AK : ABIAttrs) {
3680349cc55cSDimitry Andric Attribute Attr = Attrs.getParamAttrs(I).getAttribute(AK);
3681fe6060f1SDimitry Andric if (Attr.isValid())
3682fe6060f1SDimitry Andric Copy.addAttribute(Attr);
36830b57cec5SDimitry Andric }
3684e8d8bef9SDimitry Andric
3685e8d8bef9SDimitry Andric // `align` is ABI-affecting only in combination with `byval` or `byref`.
3686349cc55cSDimitry Andric if (Attrs.hasParamAttr(I, Attribute::Alignment) &&
3687349cc55cSDimitry Andric (Attrs.hasParamAttr(I, Attribute::ByVal) ||
3688349cc55cSDimitry Andric Attrs.hasParamAttr(I, Attribute::ByRef)))
36890b57cec5SDimitry Andric Copy.addAlignmentAttr(Attrs.getParamAlignment(I));
36900b57cec5SDimitry Andric return Copy;
36910b57cec5SDimitry Andric }
36920b57cec5SDimitry Andric
verifyMustTailCall(CallInst & CI)36930b57cec5SDimitry Andric void Verifier::verifyMustTailCall(CallInst &CI) {
369481ad6265SDimitry Andric Check(!CI.isInlineAsm(), "cannot use musttail call with inline asm", &CI);
36950b57cec5SDimitry Andric
36960b57cec5SDimitry Andric Function *F = CI.getParent()->getParent();
36970b57cec5SDimitry Andric FunctionType *CallerTy = F->getFunctionType();
36980b57cec5SDimitry Andric FunctionType *CalleeTy = CI.getFunctionType();
369981ad6265SDimitry Andric Check(CallerTy->isVarArg() == CalleeTy->isVarArg(),
37000b57cec5SDimitry Andric "cannot guarantee tail call due to mismatched varargs", &CI);
370181ad6265SDimitry Andric Check(isTypeCongruent(CallerTy->getReturnType(), CalleeTy->getReturnType()),
37020b57cec5SDimitry Andric "cannot guarantee tail call due to mismatched return types", &CI);
37030b57cec5SDimitry Andric
37040b57cec5SDimitry Andric // - The calling conventions of the caller and callee must match.
370581ad6265SDimitry Andric Check(F->getCallingConv() == CI.getCallingConv(),
37060b57cec5SDimitry Andric "cannot guarantee tail call due to mismatched calling conv", &CI);
37070b57cec5SDimitry Andric
37080b57cec5SDimitry Andric // - The call must immediately precede a :ref:`ret <i_ret>` instruction,
37090b57cec5SDimitry Andric // or a pointer bitcast followed by a ret instruction.
37100b57cec5SDimitry Andric // - The ret instruction must return the (possibly bitcasted) value
37110b57cec5SDimitry Andric // produced by the call or void.
37120b57cec5SDimitry Andric Value *RetVal = &CI;
37130b57cec5SDimitry Andric Instruction *Next = CI.getNextNode();
37140b57cec5SDimitry Andric
37150b57cec5SDimitry Andric // Handle the optional bitcast.
37160b57cec5SDimitry Andric if (BitCastInst *BI = dyn_cast_or_null<BitCastInst>(Next)) {
371781ad6265SDimitry Andric Check(BI->getOperand(0) == RetVal,
37180b57cec5SDimitry Andric "bitcast following musttail call must use the call", BI);
37190b57cec5SDimitry Andric RetVal = BI;
37200b57cec5SDimitry Andric Next = BI->getNextNode();
37210b57cec5SDimitry Andric }
37220b57cec5SDimitry Andric
37230b57cec5SDimitry Andric // Check the return.
37240b57cec5SDimitry Andric ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next);
372581ad6265SDimitry Andric Check(Ret, "musttail call must precede a ret with an optional bitcast", &CI);
372681ad6265SDimitry Andric Check(!Ret->getReturnValue() || Ret->getReturnValue() == RetVal ||
3727fe6060f1SDimitry Andric isa<UndefValue>(Ret->getReturnValue()),
37280b57cec5SDimitry Andric "musttail call result must be returned", Ret);
3729fe6060f1SDimitry Andric
3730fe6060f1SDimitry Andric AttributeList CallerAttrs = F->getAttributes();
3731fe6060f1SDimitry Andric AttributeList CalleeAttrs = CI.getAttributes();
3732fe6060f1SDimitry Andric if (CI.getCallingConv() == CallingConv::SwiftTail ||
3733fe6060f1SDimitry Andric CI.getCallingConv() == CallingConv::Tail) {
3734fe6060f1SDimitry Andric StringRef CCName =
3735fe6060f1SDimitry Andric CI.getCallingConv() == CallingConv::Tail ? "tailcc" : "swifttailcc";
3736fe6060f1SDimitry Andric
3737fe6060f1SDimitry Andric // - Only sret, byval, swiftself, and swiftasync ABI-impacting attributes
3738fe6060f1SDimitry Andric // are allowed in swifttailcc call
3739349cc55cSDimitry Andric for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
374004eeddc0SDimitry Andric AttrBuilder ABIAttrs = getParameterABIAttributes(F->getContext(), I, CallerAttrs);
3741fe6060f1SDimitry Andric SmallString<32> Context{CCName, StringRef(" musttail caller")};
3742fe6060f1SDimitry Andric verifyTailCCMustTailAttrs(ABIAttrs, Context);
3743fe6060f1SDimitry Andric }
3744349cc55cSDimitry Andric for (unsigned I = 0, E = CalleeTy->getNumParams(); I != E; ++I) {
374504eeddc0SDimitry Andric AttrBuilder ABIAttrs = getParameterABIAttributes(F->getContext(), I, CalleeAttrs);
3746fe6060f1SDimitry Andric SmallString<32> Context{CCName, StringRef(" musttail callee")};
3747fe6060f1SDimitry Andric verifyTailCCMustTailAttrs(ABIAttrs, Context);
3748fe6060f1SDimitry Andric }
3749fe6060f1SDimitry Andric // - Varargs functions are not allowed
375081ad6265SDimitry Andric Check(!CallerTy->isVarArg(), Twine("cannot guarantee ") + CCName +
3751fe6060f1SDimitry Andric " tail call for varargs function");
3752fe6060f1SDimitry Andric return;
3753fe6060f1SDimitry Andric }
3754fe6060f1SDimitry Andric
3755fe6060f1SDimitry Andric // - The caller and callee prototypes must match. Pointer types of
3756fe6060f1SDimitry Andric // parameters or return types may differ in pointee type, but not
3757fe6060f1SDimitry Andric // address space.
3758fe6060f1SDimitry Andric if (!CI.getCalledFunction() || !CI.getCalledFunction()->isIntrinsic()) {
375981ad6265SDimitry Andric Check(CallerTy->getNumParams() == CalleeTy->getNumParams(),
376081ad6265SDimitry Andric "cannot guarantee tail call due to mismatched parameter counts", &CI);
3761349cc55cSDimitry Andric for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
376281ad6265SDimitry Andric Check(
3763fe6060f1SDimitry Andric isTypeCongruent(CallerTy->getParamType(I), CalleeTy->getParamType(I)),
3764fe6060f1SDimitry Andric "cannot guarantee tail call due to mismatched parameter types", &CI);
3765fe6060f1SDimitry Andric }
3766fe6060f1SDimitry Andric }
3767fe6060f1SDimitry Andric
3768fe6060f1SDimitry Andric // - All ABI-impacting function attributes, such as sret, byval, inreg,
3769fe6060f1SDimitry Andric // returned, preallocated, and inalloca, must match.
3770349cc55cSDimitry Andric for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
377104eeddc0SDimitry Andric AttrBuilder CallerABIAttrs = getParameterABIAttributes(F->getContext(), I, CallerAttrs);
377204eeddc0SDimitry Andric AttrBuilder CalleeABIAttrs = getParameterABIAttributes(F->getContext(), I, CalleeAttrs);
377381ad6265SDimitry Andric Check(CallerABIAttrs == CalleeABIAttrs,
3774fe6060f1SDimitry Andric "cannot guarantee tail call due to mismatched ABI impacting "
3775fe6060f1SDimitry Andric "function attributes",
3776fe6060f1SDimitry Andric &CI, CI.getOperand(I));
3777fe6060f1SDimitry Andric }
37780b57cec5SDimitry Andric }
37790b57cec5SDimitry Andric
visitCallInst(CallInst & CI)37800b57cec5SDimitry Andric void Verifier::visitCallInst(CallInst &CI) {
37810b57cec5SDimitry Andric visitCallBase(CI);
37820b57cec5SDimitry Andric
37830b57cec5SDimitry Andric if (CI.isMustTailCall())
37840b57cec5SDimitry Andric verifyMustTailCall(CI);
37850b57cec5SDimitry Andric }
37860b57cec5SDimitry Andric
visitInvokeInst(InvokeInst & II)37870b57cec5SDimitry Andric void Verifier::visitInvokeInst(InvokeInst &II) {
37880b57cec5SDimitry Andric visitCallBase(II);
37890b57cec5SDimitry Andric
37900b57cec5SDimitry Andric // Verify that the first non-PHI instruction of the unwind destination is an
37910b57cec5SDimitry Andric // exception handling instruction.
379281ad6265SDimitry Andric Check(
37930b57cec5SDimitry Andric II.getUnwindDest()->isEHPad(),
37940b57cec5SDimitry Andric "The unwind destination does not have an exception handling instruction!",
37950b57cec5SDimitry Andric &II);
37960b57cec5SDimitry Andric
37970b57cec5SDimitry Andric visitTerminator(II);
37980b57cec5SDimitry Andric }
37990b57cec5SDimitry Andric
38000b57cec5SDimitry Andric /// visitUnaryOperator - Check the argument to the unary operator.
38010b57cec5SDimitry Andric ///
visitUnaryOperator(UnaryOperator & U)38020b57cec5SDimitry Andric void Verifier::visitUnaryOperator(UnaryOperator &U) {
380381ad6265SDimitry Andric Check(U.getType() == U.getOperand(0)->getType(),
38040b57cec5SDimitry Andric "Unary operators must have same type for"
38050b57cec5SDimitry Andric "operands and result!",
38060b57cec5SDimitry Andric &U);
38070b57cec5SDimitry Andric
38080b57cec5SDimitry Andric switch (U.getOpcode()) {
38090b57cec5SDimitry Andric // Check that floating-point arithmetic operators are only used with
38100b57cec5SDimitry Andric // floating-point operands.
38110b57cec5SDimitry Andric case Instruction::FNeg:
381281ad6265SDimitry Andric Check(U.getType()->isFPOrFPVectorTy(),
38130b57cec5SDimitry Andric "FNeg operator only works with float types!", &U);
38140b57cec5SDimitry Andric break;
38150b57cec5SDimitry Andric default:
38160b57cec5SDimitry Andric llvm_unreachable("Unknown UnaryOperator opcode!");
38170b57cec5SDimitry Andric }
38180b57cec5SDimitry Andric
38190b57cec5SDimitry Andric visitInstruction(U);
38200b57cec5SDimitry Andric }
38210b57cec5SDimitry Andric
38220b57cec5SDimitry Andric /// visitBinaryOperator - Check that both arguments to the binary operator are
38230b57cec5SDimitry Andric /// of the same type!
38240b57cec5SDimitry Andric ///
visitBinaryOperator(BinaryOperator & B)38250b57cec5SDimitry Andric void Verifier::visitBinaryOperator(BinaryOperator &B) {
382681ad6265SDimitry Andric Check(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
38270b57cec5SDimitry Andric "Both operands to a binary operator are not of the same type!", &B);
38280b57cec5SDimitry Andric
38290b57cec5SDimitry Andric switch (B.getOpcode()) {
38300b57cec5SDimitry Andric // Check that integer arithmetic operators are only used with
38310b57cec5SDimitry Andric // integral operands.
38320b57cec5SDimitry Andric case Instruction::Add:
38330b57cec5SDimitry Andric case Instruction::Sub:
38340b57cec5SDimitry Andric case Instruction::Mul:
38350b57cec5SDimitry Andric case Instruction::SDiv:
38360b57cec5SDimitry Andric case Instruction::UDiv:
38370b57cec5SDimitry Andric case Instruction::SRem:
38380b57cec5SDimitry Andric case Instruction::URem:
383981ad6265SDimitry Andric Check(B.getType()->isIntOrIntVectorTy(),
38400b57cec5SDimitry Andric "Integer arithmetic operators only work with integral types!", &B);
384181ad6265SDimitry Andric Check(B.getType() == B.getOperand(0)->getType(),
38420b57cec5SDimitry Andric "Integer arithmetic operators must have same type "
38430b57cec5SDimitry Andric "for operands and result!",
38440b57cec5SDimitry Andric &B);
38450b57cec5SDimitry Andric break;
38460b57cec5SDimitry Andric // Check that floating-point arithmetic operators are only used with
38470b57cec5SDimitry Andric // floating-point operands.
38480b57cec5SDimitry Andric case Instruction::FAdd:
38490b57cec5SDimitry Andric case Instruction::FSub:
38500b57cec5SDimitry Andric case Instruction::FMul:
38510b57cec5SDimitry Andric case Instruction::FDiv:
38520b57cec5SDimitry Andric case Instruction::FRem:
385381ad6265SDimitry Andric Check(B.getType()->isFPOrFPVectorTy(),
38540b57cec5SDimitry Andric "Floating-point arithmetic operators only work with "
38550b57cec5SDimitry Andric "floating-point types!",
38560b57cec5SDimitry Andric &B);
385781ad6265SDimitry Andric Check(B.getType() == B.getOperand(0)->getType(),
38580b57cec5SDimitry Andric "Floating-point arithmetic operators must have same type "
38590b57cec5SDimitry Andric "for operands and result!",
38600b57cec5SDimitry Andric &B);
38610b57cec5SDimitry Andric break;
38620b57cec5SDimitry Andric // Check that logical operators are only used with integral operands.
38630b57cec5SDimitry Andric case Instruction::And:
38640b57cec5SDimitry Andric case Instruction::Or:
38650b57cec5SDimitry Andric case Instruction::Xor:
386681ad6265SDimitry Andric Check(B.getType()->isIntOrIntVectorTy(),
38670b57cec5SDimitry Andric "Logical operators only work with integral types!", &B);
386881ad6265SDimitry Andric Check(B.getType() == B.getOperand(0)->getType(),
386981ad6265SDimitry Andric "Logical operators must have same type for operands and result!", &B);
38700b57cec5SDimitry Andric break;
38710b57cec5SDimitry Andric case Instruction::Shl:
38720b57cec5SDimitry Andric case Instruction::LShr:
38730b57cec5SDimitry Andric case Instruction::AShr:
387481ad6265SDimitry Andric Check(B.getType()->isIntOrIntVectorTy(),
38750b57cec5SDimitry Andric "Shifts only work with integral types!", &B);
387681ad6265SDimitry Andric Check(B.getType() == B.getOperand(0)->getType(),
38770b57cec5SDimitry Andric "Shift return type must be same as operands!", &B);
38780b57cec5SDimitry Andric break;
38790b57cec5SDimitry Andric default:
38800b57cec5SDimitry Andric llvm_unreachable("Unknown BinaryOperator opcode!");
38810b57cec5SDimitry Andric }
38820b57cec5SDimitry Andric
38830b57cec5SDimitry Andric visitInstruction(B);
38840b57cec5SDimitry Andric }
38850b57cec5SDimitry Andric
visitICmpInst(ICmpInst & IC)38860b57cec5SDimitry Andric void Verifier::visitICmpInst(ICmpInst &IC) {
38870b57cec5SDimitry Andric // Check that the operands are the same type
38880b57cec5SDimitry Andric Type *Op0Ty = IC.getOperand(0)->getType();
38890b57cec5SDimitry Andric Type *Op1Ty = IC.getOperand(1)->getType();
389081ad6265SDimitry Andric Check(Op0Ty == Op1Ty,
38910b57cec5SDimitry Andric "Both operands to ICmp instruction are not of the same type!", &IC);
38920b57cec5SDimitry Andric // Check that the operands are the right type
389381ad6265SDimitry Andric Check(Op0Ty->isIntOrIntVectorTy() || Op0Ty->isPtrOrPtrVectorTy(),
38940b57cec5SDimitry Andric "Invalid operand types for ICmp instruction", &IC);
38950b57cec5SDimitry Andric // Check that the predicate is valid.
389681ad6265SDimitry Andric Check(IC.isIntPredicate(), "Invalid predicate in ICmp instruction!", &IC);
38970b57cec5SDimitry Andric
38980b57cec5SDimitry Andric visitInstruction(IC);
38990b57cec5SDimitry Andric }
39000b57cec5SDimitry Andric
visitFCmpInst(FCmpInst & FC)39010b57cec5SDimitry Andric void Verifier::visitFCmpInst(FCmpInst &FC) {
39020b57cec5SDimitry Andric // Check that the operands are the same type
39030b57cec5SDimitry Andric Type *Op0Ty = FC.getOperand(0)->getType();
39040b57cec5SDimitry Andric Type *Op1Ty = FC.getOperand(1)->getType();
390581ad6265SDimitry Andric Check(Op0Ty == Op1Ty,
39060b57cec5SDimitry Andric "Both operands to FCmp instruction are not of the same type!", &FC);
39070b57cec5SDimitry Andric // Check that the operands are the right type
390881ad6265SDimitry Andric Check(Op0Ty->isFPOrFPVectorTy(), "Invalid operand types for FCmp instruction",
390981ad6265SDimitry Andric &FC);
39100b57cec5SDimitry Andric // Check that the predicate is valid.
391181ad6265SDimitry Andric Check(FC.isFPPredicate(), "Invalid predicate in FCmp instruction!", &FC);
39120b57cec5SDimitry Andric
39130b57cec5SDimitry Andric visitInstruction(FC);
39140b57cec5SDimitry Andric }
39150b57cec5SDimitry Andric
visitExtractElementInst(ExtractElementInst & EI)39160b57cec5SDimitry Andric void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
391781ad6265SDimitry Andric Check(ExtractElementInst::isValidOperands(EI.getOperand(0), EI.getOperand(1)),
39180b57cec5SDimitry Andric "Invalid extractelement operands!", &EI);
39190b57cec5SDimitry Andric visitInstruction(EI);
39200b57cec5SDimitry Andric }
39210b57cec5SDimitry Andric
visitInsertElementInst(InsertElementInst & IE)39220b57cec5SDimitry Andric void Verifier::visitInsertElementInst(InsertElementInst &IE) {
392381ad6265SDimitry Andric Check(InsertElementInst::isValidOperands(IE.getOperand(0), IE.getOperand(1),
39240b57cec5SDimitry Andric IE.getOperand(2)),
39250b57cec5SDimitry Andric "Invalid insertelement operands!", &IE);
39260b57cec5SDimitry Andric visitInstruction(IE);
39270b57cec5SDimitry Andric }
39280b57cec5SDimitry Andric
visitShuffleVectorInst(ShuffleVectorInst & SV)39290b57cec5SDimitry Andric void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
393081ad6265SDimitry Andric Check(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1),
39315ffd83dbSDimitry Andric SV.getShuffleMask()),
39320b57cec5SDimitry Andric "Invalid shufflevector operands!", &SV);
39330b57cec5SDimitry Andric visitInstruction(SV);
39340b57cec5SDimitry Andric }
39350b57cec5SDimitry Andric
visitGetElementPtrInst(GetElementPtrInst & GEP)39360b57cec5SDimitry Andric void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
39370b57cec5SDimitry Andric Type *TargetTy = GEP.getPointerOperandType()->getScalarType();
39380b57cec5SDimitry Andric
393981ad6265SDimitry Andric Check(isa<PointerType>(TargetTy),
39400b57cec5SDimitry Andric "GEP base pointer is not a vector or a vector of pointers", &GEP);
394181ad6265SDimitry Andric Check(GEP.getSourceElementType()->isSized(), "GEP into unsized type!", &GEP);
39420b57cec5SDimitry Andric
394306c3fb27SDimitry Andric if (auto *STy = dyn_cast<StructType>(GEP.getSourceElementType())) {
394406c3fb27SDimitry Andric SmallPtrSet<Type *, 4> Visited;
394506c3fb27SDimitry Andric Check(!STy->containsScalableVectorType(&Visited),
394606c3fb27SDimitry Andric "getelementptr cannot target structure that contains scalable vector"
394706c3fb27SDimitry Andric "type",
394806c3fb27SDimitry Andric &GEP);
394906c3fb27SDimitry Andric }
395006c3fb27SDimitry Andric
3951e8d8bef9SDimitry Andric SmallVector<Value *, 16> Idxs(GEP.indices());
395281ad6265SDimitry Andric Check(
395381ad6265SDimitry Andric all_of(Idxs, [](Value *V) { return V->getType()->isIntOrIntVectorTy(); }),
39540b57cec5SDimitry Andric "GEP indexes must be integers", &GEP);
39550b57cec5SDimitry Andric Type *ElTy =
39560b57cec5SDimitry Andric GetElementPtrInst::getIndexedType(GEP.getSourceElementType(), Idxs);
395781ad6265SDimitry Andric Check(ElTy, "Invalid indices for GEP pointer type!", &GEP);
39580b57cec5SDimitry Andric
395981ad6265SDimitry Andric Check(GEP.getType()->isPtrOrPtrVectorTy() &&
39600b57cec5SDimitry Andric GEP.getResultElementType() == ElTy,
39610b57cec5SDimitry Andric "GEP is not of right type for indices!", &GEP, ElTy);
39620b57cec5SDimitry Andric
39635ffd83dbSDimitry Andric if (auto *GEPVTy = dyn_cast<VectorType>(GEP.getType())) {
39640b57cec5SDimitry Andric // Additional checks for vector GEPs.
39655ffd83dbSDimitry Andric ElementCount GEPWidth = GEPVTy->getElementCount();
39660b57cec5SDimitry Andric if (GEP.getPointerOperandType()->isVectorTy())
396781ad6265SDimitry Andric Check(
39685ffd83dbSDimitry Andric GEPWidth ==
39695ffd83dbSDimitry Andric cast<VectorType>(GEP.getPointerOperandType())->getElementCount(),
39700b57cec5SDimitry Andric "Vector GEP result width doesn't match operand's", &GEP);
39710b57cec5SDimitry Andric for (Value *Idx : Idxs) {
39720b57cec5SDimitry Andric Type *IndexTy = Idx->getType();
39735ffd83dbSDimitry Andric if (auto *IndexVTy = dyn_cast<VectorType>(IndexTy)) {
39745ffd83dbSDimitry Andric ElementCount IndexWidth = IndexVTy->getElementCount();
397581ad6265SDimitry Andric Check(IndexWidth == GEPWidth, "Invalid GEP index vector width", &GEP);
39760b57cec5SDimitry Andric }
397781ad6265SDimitry Andric Check(IndexTy->isIntOrIntVectorTy(),
39780b57cec5SDimitry Andric "All GEP indices should be of integer type");
39790b57cec5SDimitry Andric }
39800b57cec5SDimitry Andric }
39810b57cec5SDimitry Andric
39820b57cec5SDimitry Andric if (auto *PTy = dyn_cast<PointerType>(GEP.getType())) {
398381ad6265SDimitry Andric Check(GEP.getAddressSpace() == PTy->getAddressSpace(),
39840b57cec5SDimitry Andric "GEP address space doesn't match type", &GEP);
39850b57cec5SDimitry Andric }
39860b57cec5SDimitry Andric
39870b57cec5SDimitry Andric visitInstruction(GEP);
39880b57cec5SDimitry Andric }
39890b57cec5SDimitry Andric
isContiguous(const ConstantRange & A,const ConstantRange & B)39900b57cec5SDimitry Andric static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
39910b57cec5SDimitry Andric return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
39920b57cec5SDimitry Andric }
39930b57cec5SDimitry Andric
399406c3fb27SDimitry Andric /// Verify !range and !absolute_symbol metadata. These have the same
399506c3fb27SDimitry Andric /// restrictions, except !absolute_symbol allows the full set.
verifyRangeMetadata(const Value & I,const MDNode * Range,Type * Ty,bool IsAbsoluteSymbol)399606c3fb27SDimitry Andric void Verifier::verifyRangeMetadata(const Value &I, const MDNode *Range,
399706c3fb27SDimitry Andric Type *Ty, bool IsAbsoluteSymbol) {
39980b57cec5SDimitry Andric unsigned NumOperands = Range->getNumOperands();
399981ad6265SDimitry Andric Check(NumOperands % 2 == 0, "Unfinished range!", Range);
40000b57cec5SDimitry Andric unsigned NumRanges = NumOperands / 2;
400181ad6265SDimitry Andric Check(NumRanges >= 1, "It should have at least one range!", Range);
40020b57cec5SDimitry Andric
40030b57cec5SDimitry Andric ConstantRange LastRange(1, true); // Dummy initial value
40040b57cec5SDimitry Andric for (unsigned i = 0; i < NumRanges; ++i) {
40050b57cec5SDimitry Andric ConstantInt *Low =
40060b57cec5SDimitry Andric mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i));
400781ad6265SDimitry Andric Check(Low, "The lower limit must be an integer!", Low);
40080b57cec5SDimitry Andric ConstantInt *High =
40090b57cec5SDimitry Andric mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i + 1));
401081ad6265SDimitry Andric Check(High, "The upper limit must be an integer!", High);
401106c3fb27SDimitry Andric Check(High->getType() == Low->getType() &&
401206c3fb27SDimitry Andric High->getType() == Ty->getScalarType(),
40130b57cec5SDimitry Andric "Range types must match instruction type!", &I);
40140b57cec5SDimitry Andric
40150b57cec5SDimitry Andric APInt HighV = High->getValue();
40160b57cec5SDimitry Andric APInt LowV = Low->getValue();
401706c3fb27SDimitry Andric
401806c3fb27SDimitry Andric // ConstantRange asserts if the ranges are the same except for the min/max
401906c3fb27SDimitry Andric // value. Leave the cases it tolerates for the empty range error below.
402006c3fb27SDimitry Andric Check(LowV != HighV || LowV.isMaxValue() || LowV.isMinValue(),
402106c3fb27SDimitry Andric "The upper and lower limits cannot be the same value", &I);
402206c3fb27SDimitry Andric
40230b57cec5SDimitry Andric ConstantRange CurRange(LowV, HighV);
402406c3fb27SDimitry Andric Check(!CurRange.isEmptySet() && (IsAbsoluteSymbol || !CurRange.isFullSet()),
40250b57cec5SDimitry Andric "Range must not be empty!", Range);
40260b57cec5SDimitry Andric if (i != 0) {
402781ad6265SDimitry Andric Check(CurRange.intersectWith(LastRange).isEmptySet(),
40280b57cec5SDimitry Andric "Intervals are overlapping", Range);
402981ad6265SDimitry Andric Check(LowV.sgt(LastRange.getLower()), "Intervals are not in order",
40300b57cec5SDimitry Andric Range);
403181ad6265SDimitry Andric Check(!isContiguous(CurRange, LastRange), "Intervals are contiguous",
40320b57cec5SDimitry Andric Range);
40330b57cec5SDimitry Andric }
40340b57cec5SDimitry Andric LastRange = ConstantRange(LowV, HighV);
40350b57cec5SDimitry Andric }
40360b57cec5SDimitry Andric if (NumRanges > 2) {
40370b57cec5SDimitry Andric APInt FirstLow =
40380b57cec5SDimitry Andric mdconst::dyn_extract<ConstantInt>(Range->getOperand(0))->getValue();
40390b57cec5SDimitry Andric APInt FirstHigh =
40400b57cec5SDimitry Andric mdconst::dyn_extract<ConstantInt>(Range->getOperand(1))->getValue();
40410b57cec5SDimitry Andric ConstantRange FirstRange(FirstLow, FirstHigh);
404281ad6265SDimitry Andric Check(FirstRange.intersectWith(LastRange).isEmptySet(),
40430b57cec5SDimitry Andric "Intervals are overlapping", Range);
404481ad6265SDimitry Andric Check(!isContiguous(FirstRange, LastRange), "Intervals are contiguous",
40450b57cec5SDimitry Andric Range);
40460b57cec5SDimitry Andric }
40470b57cec5SDimitry Andric }
40480b57cec5SDimitry Andric
visitRangeMetadata(Instruction & I,MDNode * Range,Type * Ty)404906c3fb27SDimitry Andric void Verifier::visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty) {
405006c3fb27SDimitry Andric assert(Range && Range == I.getMetadata(LLVMContext::MD_range) &&
405106c3fb27SDimitry Andric "precondition violation");
405206c3fb27SDimitry Andric verifyRangeMetadata(I, Range, Ty, false);
405306c3fb27SDimitry Andric }
405406c3fb27SDimitry Andric
checkAtomicMemAccessSize(Type * Ty,const Instruction * I)40550b57cec5SDimitry Andric void Verifier::checkAtomicMemAccessSize(Type *Ty, const Instruction *I) {
40560b57cec5SDimitry Andric unsigned Size = DL.getTypeSizeInBits(Ty);
405781ad6265SDimitry Andric Check(Size >= 8, "atomic memory access' size must be byte-sized", Ty, I);
405881ad6265SDimitry Andric Check(!(Size & (Size - 1)),
40590b57cec5SDimitry Andric "atomic memory access' operand must have a power-of-two size", Ty, I);
40600b57cec5SDimitry Andric }
40610b57cec5SDimitry Andric
visitLoadInst(LoadInst & LI)40620b57cec5SDimitry Andric void Verifier::visitLoadInst(LoadInst &LI) {
40630b57cec5SDimitry Andric PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType());
406481ad6265SDimitry Andric Check(PTy, "Load operand must be a pointer.", &LI);
40650b57cec5SDimitry Andric Type *ElTy = LI.getType();
40660eae32dcSDimitry Andric if (MaybeAlign A = LI.getAlign()) {
406781ad6265SDimitry Andric Check(A->value() <= Value::MaximumAlignment,
40680b57cec5SDimitry Andric "huge alignment values are unsupported", &LI);
40690eae32dcSDimitry Andric }
407081ad6265SDimitry Andric Check(ElTy->isSized(), "loading unsized types is not allowed", &LI);
40710b57cec5SDimitry Andric if (LI.isAtomic()) {
407281ad6265SDimitry Andric Check(LI.getOrdering() != AtomicOrdering::Release &&
40730b57cec5SDimitry Andric LI.getOrdering() != AtomicOrdering::AcquireRelease,
40740b57cec5SDimitry Andric "Load cannot have Release ordering", &LI);
407581ad6265SDimitry Andric Check(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
40760b57cec5SDimitry Andric "atomic load operand must have integer, pointer, or floating point "
40770b57cec5SDimitry Andric "type!",
40780b57cec5SDimitry Andric ElTy, &LI);
40790b57cec5SDimitry Andric checkAtomicMemAccessSize(ElTy, &LI);
40800b57cec5SDimitry Andric } else {
408181ad6265SDimitry Andric Check(LI.getSyncScopeID() == SyncScope::System,
40820b57cec5SDimitry Andric "Non-atomic load cannot have SynchronizationScope specified", &LI);
40830b57cec5SDimitry Andric }
40840b57cec5SDimitry Andric
40850b57cec5SDimitry Andric visitInstruction(LI);
40860b57cec5SDimitry Andric }
40870b57cec5SDimitry Andric
visitStoreInst(StoreInst & SI)40880b57cec5SDimitry Andric void Verifier::visitStoreInst(StoreInst &SI) {
40890b57cec5SDimitry Andric PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType());
409081ad6265SDimitry Andric Check(PTy, "Store operand must be a pointer.", &SI);
4091fe6060f1SDimitry Andric Type *ElTy = SI.getOperand(0)->getType();
40920eae32dcSDimitry Andric if (MaybeAlign A = SI.getAlign()) {
409381ad6265SDimitry Andric Check(A->value() <= Value::MaximumAlignment,
40940b57cec5SDimitry Andric "huge alignment values are unsupported", &SI);
40950eae32dcSDimitry Andric }
409681ad6265SDimitry Andric Check(ElTy->isSized(), "storing unsized types is not allowed", &SI);
40970b57cec5SDimitry Andric if (SI.isAtomic()) {
409881ad6265SDimitry Andric Check(SI.getOrdering() != AtomicOrdering::Acquire &&
40990b57cec5SDimitry Andric SI.getOrdering() != AtomicOrdering::AcquireRelease,
41000b57cec5SDimitry Andric "Store cannot have Acquire ordering", &SI);
410181ad6265SDimitry Andric Check(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
41020b57cec5SDimitry Andric "atomic store operand must have integer, pointer, or floating point "
41030b57cec5SDimitry Andric "type!",
41040b57cec5SDimitry Andric ElTy, &SI);
41050b57cec5SDimitry Andric checkAtomicMemAccessSize(ElTy, &SI);
41060b57cec5SDimitry Andric } else {
410781ad6265SDimitry Andric Check(SI.getSyncScopeID() == SyncScope::System,
41080b57cec5SDimitry Andric "Non-atomic store cannot have SynchronizationScope specified", &SI);
41090b57cec5SDimitry Andric }
41100b57cec5SDimitry Andric visitInstruction(SI);
41110b57cec5SDimitry Andric }
41120b57cec5SDimitry Andric
41130b57cec5SDimitry Andric /// Check that SwiftErrorVal is used as a swifterror argument in CS.
verifySwiftErrorCall(CallBase & Call,const Value * SwiftErrorVal)41140b57cec5SDimitry Andric void Verifier::verifySwiftErrorCall(CallBase &Call,
41150b57cec5SDimitry Andric const Value *SwiftErrorVal) {
4116fe6060f1SDimitry Andric for (const auto &I : llvm::enumerate(Call.args())) {
4117fe6060f1SDimitry Andric if (I.value() == SwiftErrorVal) {
411881ad6265SDimitry Andric Check(Call.paramHasAttr(I.index(), Attribute::SwiftError),
41190b57cec5SDimitry Andric "swifterror value when used in a callsite should be marked "
41200b57cec5SDimitry Andric "with swifterror attribute",
41210b57cec5SDimitry Andric SwiftErrorVal, Call);
41220b57cec5SDimitry Andric }
41230b57cec5SDimitry Andric }
41240b57cec5SDimitry Andric }
41250b57cec5SDimitry Andric
verifySwiftErrorValue(const Value * SwiftErrorVal)41260b57cec5SDimitry Andric void Verifier::verifySwiftErrorValue(const Value *SwiftErrorVal) {
41270b57cec5SDimitry Andric // Check that swifterror value is only used by loads, stores, or as
41280b57cec5SDimitry Andric // a swifterror argument.
41290b57cec5SDimitry Andric for (const User *U : SwiftErrorVal->users()) {
413081ad6265SDimitry Andric Check(isa<LoadInst>(U) || isa<StoreInst>(U) || isa<CallInst>(U) ||
41310b57cec5SDimitry Andric isa<InvokeInst>(U),
41320b57cec5SDimitry Andric "swifterror value can only be loaded and stored from, or "
41330b57cec5SDimitry Andric "as a swifterror argument!",
41340b57cec5SDimitry Andric SwiftErrorVal, U);
41350b57cec5SDimitry Andric // If it is used by a store, check it is the second operand.
41360b57cec5SDimitry Andric if (auto StoreI = dyn_cast<StoreInst>(U))
413781ad6265SDimitry Andric Check(StoreI->getOperand(1) == SwiftErrorVal,
41380b57cec5SDimitry Andric "swifterror value should be the second operand when used "
413981ad6265SDimitry Andric "by stores",
414081ad6265SDimitry Andric SwiftErrorVal, U);
41410b57cec5SDimitry Andric if (auto *Call = dyn_cast<CallBase>(U))
41420b57cec5SDimitry Andric verifySwiftErrorCall(*const_cast<CallBase *>(Call), SwiftErrorVal);
41430b57cec5SDimitry Andric }
41440b57cec5SDimitry Andric }
41450b57cec5SDimitry Andric
visitAllocaInst(AllocaInst & AI)41460b57cec5SDimitry Andric void Verifier::visitAllocaInst(AllocaInst &AI) {
41470b57cec5SDimitry Andric SmallPtrSet<Type*, 4> Visited;
414881ad6265SDimitry Andric Check(AI.getAllocatedType()->isSized(&Visited),
41490b57cec5SDimitry Andric "Cannot allocate unsized type", &AI);
415081ad6265SDimitry Andric Check(AI.getArraySize()->getType()->isIntegerTy(),
41510b57cec5SDimitry Andric "Alloca array size must have integer type", &AI);
41520eae32dcSDimitry Andric if (MaybeAlign A = AI.getAlign()) {
415381ad6265SDimitry Andric Check(A->value() <= Value::MaximumAlignment,
41540b57cec5SDimitry Andric "huge alignment values are unsupported", &AI);
41550eae32dcSDimitry Andric }
41560b57cec5SDimitry Andric
41570b57cec5SDimitry Andric if (AI.isSwiftError()) {
415881ad6265SDimitry Andric Check(AI.getAllocatedType()->isPointerTy(),
415981ad6265SDimitry Andric "swifterror alloca must have pointer type", &AI);
416081ad6265SDimitry Andric Check(!AI.isArrayAllocation(),
416181ad6265SDimitry Andric "swifterror alloca must not be array allocation", &AI);
41620b57cec5SDimitry Andric verifySwiftErrorValue(&AI);
41630b57cec5SDimitry Andric }
41640b57cec5SDimitry Andric
41650b57cec5SDimitry Andric visitInstruction(AI);
41660b57cec5SDimitry Andric }
41670b57cec5SDimitry Andric
visitAtomicCmpXchgInst(AtomicCmpXchgInst & CXI)41680b57cec5SDimitry Andric void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI) {
4169fe6060f1SDimitry Andric Type *ElTy = CXI.getOperand(1)->getType();
417081ad6265SDimitry Andric Check(ElTy->isIntOrPtrTy(),
41710b57cec5SDimitry Andric "cmpxchg operand must have integer or pointer type", ElTy, &CXI);
41720b57cec5SDimitry Andric checkAtomicMemAccessSize(ElTy, &CXI);
41730b57cec5SDimitry Andric visitInstruction(CXI);
41740b57cec5SDimitry Andric }
41750b57cec5SDimitry Andric
visitAtomicRMWInst(AtomicRMWInst & RMWI)41760b57cec5SDimitry Andric void Verifier::visitAtomicRMWInst(AtomicRMWInst &RMWI) {
417781ad6265SDimitry Andric Check(RMWI.getOrdering() != AtomicOrdering::Unordered,
41780b57cec5SDimitry Andric "atomicrmw instructions cannot be unordered.", &RMWI);
41790b57cec5SDimitry Andric auto Op = RMWI.getOperation();
4180fe6060f1SDimitry Andric Type *ElTy = RMWI.getOperand(1)->getType();
41810b57cec5SDimitry Andric if (Op == AtomicRMWInst::Xchg) {
418281ad6265SDimitry Andric Check(ElTy->isIntegerTy() || ElTy->isFloatingPointTy() ||
418381ad6265SDimitry Andric ElTy->isPointerTy(),
418481ad6265SDimitry Andric "atomicrmw " + AtomicRMWInst::getOperationName(Op) +
41850b57cec5SDimitry Andric " operand must have integer or floating point type!",
41860b57cec5SDimitry Andric &RMWI, ElTy);
41870b57cec5SDimitry Andric } else if (AtomicRMWInst::isFPOperation(Op)) {
418881ad6265SDimitry Andric Check(ElTy->isFloatingPointTy(),
418981ad6265SDimitry Andric "atomicrmw " + AtomicRMWInst::getOperationName(Op) +
41900b57cec5SDimitry Andric " operand must have floating point type!",
41910b57cec5SDimitry Andric &RMWI, ElTy);
41920b57cec5SDimitry Andric } else {
419381ad6265SDimitry Andric Check(ElTy->isIntegerTy(),
419481ad6265SDimitry Andric "atomicrmw " + AtomicRMWInst::getOperationName(Op) +
41950b57cec5SDimitry Andric " operand must have integer type!",
41960b57cec5SDimitry Andric &RMWI, ElTy);
41970b57cec5SDimitry Andric }
41980b57cec5SDimitry Andric checkAtomicMemAccessSize(ElTy, &RMWI);
419981ad6265SDimitry Andric Check(AtomicRMWInst::FIRST_BINOP <= Op && Op <= AtomicRMWInst::LAST_BINOP,
42000b57cec5SDimitry Andric "Invalid binary operation!", &RMWI);
42010b57cec5SDimitry Andric visitInstruction(RMWI);
42020b57cec5SDimitry Andric }
42030b57cec5SDimitry Andric
visitFenceInst(FenceInst & FI)42040b57cec5SDimitry Andric void Verifier::visitFenceInst(FenceInst &FI) {
42050b57cec5SDimitry Andric const AtomicOrdering Ordering = FI.getOrdering();
420681ad6265SDimitry Andric Check(Ordering == AtomicOrdering::Acquire ||
42070b57cec5SDimitry Andric Ordering == AtomicOrdering::Release ||
42080b57cec5SDimitry Andric Ordering == AtomicOrdering::AcquireRelease ||
42090b57cec5SDimitry Andric Ordering == AtomicOrdering::SequentiallyConsistent,
42100b57cec5SDimitry Andric "fence instructions may only have acquire, release, acq_rel, or "
42110b57cec5SDimitry Andric "seq_cst ordering.",
42120b57cec5SDimitry Andric &FI);
42130b57cec5SDimitry Andric visitInstruction(FI);
42140b57cec5SDimitry Andric }
42150b57cec5SDimitry Andric
visitExtractValueInst(ExtractValueInst & EVI)42160b57cec5SDimitry Andric void Verifier::visitExtractValueInst(ExtractValueInst &EVI) {
421781ad6265SDimitry Andric Check(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(),
42180b57cec5SDimitry Andric EVI.getIndices()) == EVI.getType(),
42190b57cec5SDimitry Andric "Invalid ExtractValueInst operands!", &EVI);
42200b57cec5SDimitry Andric
42210b57cec5SDimitry Andric visitInstruction(EVI);
42220b57cec5SDimitry Andric }
42230b57cec5SDimitry Andric
visitInsertValueInst(InsertValueInst & IVI)42240b57cec5SDimitry Andric void Verifier::visitInsertValueInst(InsertValueInst &IVI) {
422581ad6265SDimitry Andric Check(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(),
42260b57cec5SDimitry Andric IVI.getIndices()) ==
42270b57cec5SDimitry Andric IVI.getOperand(1)->getType(),
42280b57cec5SDimitry Andric "Invalid InsertValueInst operands!", &IVI);
42290b57cec5SDimitry Andric
42300b57cec5SDimitry Andric visitInstruction(IVI);
42310b57cec5SDimitry Andric }
42320b57cec5SDimitry Andric
getParentPad(Value * EHPad)42330b57cec5SDimitry Andric static Value *getParentPad(Value *EHPad) {
42340b57cec5SDimitry Andric if (auto *FPI = dyn_cast<FuncletPadInst>(EHPad))
42350b57cec5SDimitry Andric return FPI->getParentPad();
42360b57cec5SDimitry Andric
42370b57cec5SDimitry Andric return cast<CatchSwitchInst>(EHPad)->getParentPad();
42380b57cec5SDimitry Andric }
42390b57cec5SDimitry Andric
visitEHPadPredecessors(Instruction & I)42400b57cec5SDimitry Andric void Verifier::visitEHPadPredecessors(Instruction &I) {
42410b57cec5SDimitry Andric assert(I.isEHPad());
42420b57cec5SDimitry Andric
42430b57cec5SDimitry Andric BasicBlock *BB = I.getParent();
42440b57cec5SDimitry Andric Function *F = BB->getParent();
42450b57cec5SDimitry Andric
424681ad6265SDimitry Andric Check(BB != &F->getEntryBlock(), "EH pad cannot be in entry block.", &I);
42470b57cec5SDimitry Andric
42480b57cec5SDimitry Andric if (auto *LPI = dyn_cast<LandingPadInst>(&I)) {
42490b57cec5SDimitry Andric // The landingpad instruction defines its parent as a landing pad block. The
42500b57cec5SDimitry Andric // landing pad block may be branched to only by the unwind edge of an
42510b57cec5SDimitry Andric // invoke.
42520b57cec5SDimitry Andric for (BasicBlock *PredBB : predecessors(BB)) {
42530b57cec5SDimitry Andric const auto *II = dyn_cast<InvokeInst>(PredBB->getTerminator());
425481ad6265SDimitry Andric Check(II && II->getUnwindDest() == BB && II->getNormalDest() != BB,
42550b57cec5SDimitry Andric "Block containing LandingPadInst must be jumped to "
42560b57cec5SDimitry Andric "only by the unwind edge of an invoke.",
42570b57cec5SDimitry Andric LPI);
42580b57cec5SDimitry Andric }
42590b57cec5SDimitry Andric return;
42600b57cec5SDimitry Andric }
42610b57cec5SDimitry Andric if (auto *CPI = dyn_cast<CatchPadInst>(&I)) {
42620b57cec5SDimitry Andric if (!pred_empty(BB))
426381ad6265SDimitry Andric Check(BB->getUniquePredecessor() == CPI->getCatchSwitch()->getParent(),
42640b57cec5SDimitry Andric "Block containg CatchPadInst must be jumped to "
42650b57cec5SDimitry Andric "only by its catchswitch.",
42660b57cec5SDimitry Andric CPI);
426781ad6265SDimitry Andric Check(BB != CPI->getCatchSwitch()->getUnwindDest(),
42680b57cec5SDimitry Andric "Catchswitch cannot unwind to one of its catchpads",
42690b57cec5SDimitry Andric CPI->getCatchSwitch(), CPI);
42700b57cec5SDimitry Andric return;
42710b57cec5SDimitry Andric }
42720b57cec5SDimitry Andric
42730b57cec5SDimitry Andric // Verify that each pred has a legal terminator with a legal to/from EH
42740b57cec5SDimitry Andric // pad relationship.
42750b57cec5SDimitry Andric Instruction *ToPad = &I;
42760b57cec5SDimitry Andric Value *ToPadParent = getParentPad(ToPad);
42770b57cec5SDimitry Andric for (BasicBlock *PredBB : predecessors(BB)) {
42780b57cec5SDimitry Andric Instruction *TI = PredBB->getTerminator();
42790b57cec5SDimitry Andric Value *FromPad;
42800b57cec5SDimitry Andric if (auto *II = dyn_cast<InvokeInst>(TI)) {
428181ad6265SDimitry Andric Check(II->getUnwindDest() == BB && II->getNormalDest() != BB,
42820b57cec5SDimitry Andric "EH pad must be jumped to via an unwind edge", ToPad, II);
42830b57cec5SDimitry Andric if (auto Bundle = II->getOperandBundle(LLVMContext::OB_funclet))
42840b57cec5SDimitry Andric FromPad = Bundle->Inputs[0];
42850b57cec5SDimitry Andric else
42860b57cec5SDimitry Andric FromPad = ConstantTokenNone::get(II->getContext());
42870b57cec5SDimitry Andric } else if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) {
42880b57cec5SDimitry Andric FromPad = CRI->getOperand(0);
428981ad6265SDimitry Andric Check(FromPad != ToPadParent, "A cleanupret must exit its cleanup", CRI);
42900b57cec5SDimitry Andric } else if (auto *CSI = dyn_cast<CatchSwitchInst>(TI)) {
42910b57cec5SDimitry Andric FromPad = CSI;
42920b57cec5SDimitry Andric } else {
429381ad6265SDimitry Andric Check(false, "EH pad must be jumped to via an unwind edge", ToPad, TI);
42940b57cec5SDimitry Andric }
42950b57cec5SDimitry Andric
42960b57cec5SDimitry Andric // The edge may exit from zero or more nested pads.
42970b57cec5SDimitry Andric SmallSet<Value *, 8> Seen;
42980b57cec5SDimitry Andric for (;; FromPad = getParentPad(FromPad)) {
429981ad6265SDimitry Andric Check(FromPad != ToPad,
43000b57cec5SDimitry Andric "EH pad cannot handle exceptions raised within it", FromPad, TI);
43010b57cec5SDimitry Andric if (FromPad == ToPadParent) {
43020b57cec5SDimitry Andric // This is a legal unwind edge.
43030b57cec5SDimitry Andric break;
43040b57cec5SDimitry Andric }
430581ad6265SDimitry Andric Check(!isa<ConstantTokenNone>(FromPad),
43060b57cec5SDimitry Andric "A single unwind edge may only enter one EH pad", TI);
430781ad6265SDimitry Andric Check(Seen.insert(FromPad).second, "EH pad jumps through a cycle of pads",
430881ad6265SDimitry Andric FromPad);
430904eeddc0SDimitry Andric
431004eeddc0SDimitry Andric // This will be diagnosed on the corresponding instruction already. We
431104eeddc0SDimitry Andric // need the extra check here to make sure getParentPad() works.
431281ad6265SDimitry Andric Check(isa<FuncletPadInst>(FromPad) || isa<CatchSwitchInst>(FromPad),
431304eeddc0SDimitry Andric "Parent pad must be catchpad/cleanuppad/catchswitch", TI);
43140b57cec5SDimitry Andric }
43150b57cec5SDimitry Andric }
43160b57cec5SDimitry Andric }
43170b57cec5SDimitry Andric
visitLandingPadInst(LandingPadInst & LPI)43180b57cec5SDimitry Andric void Verifier::visitLandingPadInst(LandingPadInst &LPI) {
43190b57cec5SDimitry Andric // The landingpad instruction is ill-formed if it doesn't have any clauses and
43200b57cec5SDimitry Andric // isn't a cleanup.
432181ad6265SDimitry Andric Check(LPI.getNumClauses() > 0 || LPI.isCleanup(),
43220b57cec5SDimitry Andric "LandingPadInst needs at least one clause or to be a cleanup.", &LPI);
43230b57cec5SDimitry Andric
43240b57cec5SDimitry Andric visitEHPadPredecessors(LPI);
43250b57cec5SDimitry Andric
43260b57cec5SDimitry Andric if (!LandingPadResultTy)
43270b57cec5SDimitry Andric LandingPadResultTy = LPI.getType();
43280b57cec5SDimitry Andric else
432981ad6265SDimitry Andric Check(LandingPadResultTy == LPI.getType(),
43300b57cec5SDimitry Andric "The landingpad instruction should have a consistent result type "
43310b57cec5SDimitry Andric "inside a function.",
43320b57cec5SDimitry Andric &LPI);
43330b57cec5SDimitry Andric
43340b57cec5SDimitry Andric Function *F = LPI.getParent()->getParent();
433581ad6265SDimitry Andric Check(F->hasPersonalityFn(),
43360b57cec5SDimitry Andric "LandingPadInst needs to be in a function with a personality.", &LPI);
43370b57cec5SDimitry Andric
43380b57cec5SDimitry Andric // The landingpad instruction must be the first non-PHI instruction in the
43390b57cec5SDimitry Andric // block.
434081ad6265SDimitry Andric Check(LPI.getParent()->getLandingPadInst() == &LPI,
434181ad6265SDimitry Andric "LandingPadInst not the first non-PHI instruction in the block.", &LPI);
43420b57cec5SDimitry Andric
43430b57cec5SDimitry Andric for (unsigned i = 0, e = LPI.getNumClauses(); i < e; ++i) {
43440b57cec5SDimitry Andric Constant *Clause = LPI.getClause(i);
43450b57cec5SDimitry Andric if (LPI.isCatch(i)) {
434681ad6265SDimitry Andric Check(isa<PointerType>(Clause->getType()),
43470b57cec5SDimitry Andric "Catch operand does not have pointer type!", &LPI);
43480b57cec5SDimitry Andric } else {
434981ad6265SDimitry Andric Check(LPI.isFilter(i), "Clause is neither catch nor filter!", &LPI);
435081ad6265SDimitry Andric Check(isa<ConstantArray>(Clause) || isa<ConstantAggregateZero>(Clause),
43510b57cec5SDimitry Andric "Filter operand is not an array of constants!", &LPI);
43520b57cec5SDimitry Andric }
43530b57cec5SDimitry Andric }
43540b57cec5SDimitry Andric
43550b57cec5SDimitry Andric visitInstruction(LPI);
43560b57cec5SDimitry Andric }
43570b57cec5SDimitry Andric
visitResumeInst(ResumeInst & RI)43580b57cec5SDimitry Andric void Verifier::visitResumeInst(ResumeInst &RI) {
435981ad6265SDimitry Andric Check(RI.getFunction()->hasPersonalityFn(),
43600b57cec5SDimitry Andric "ResumeInst needs to be in a function with a personality.", &RI);
43610b57cec5SDimitry Andric
43620b57cec5SDimitry Andric if (!LandingPadResultTy)
43630b57cec5SDimitry Andric LandingPadResultTy = RI.getValue()->getType();
43640b57cec5SDimitry Andric else
436581ad6265SDimitry Andric Check(LandingPadResultTy == RI.getValue()->getType(),
43660b57cec5SDimitry Andric "The resume instruction should have a consistent result type "
43670b57cec5SDimitry Andric "inside a function.",
43680b57cec5SDimitry Andric &RI);
43690b57cec5SDimitry Andric
43700b57cec5SDimitry Andric visitTerminator(RI);
43710b57cec5SDimitry Andric }
43720b57cec5SDimitry Andric
visitCatchPadInst(CatchPadInst & CPI)43730b57cec5SDimitry Andric void Verifier::visitCatchPadInst(CatchPadInst &CPI) {
43740b57cec5SDimitry Andric BasicBlock *BB = CPI.getParent();
43750b57cec5SDimitry Andric
43760b57cec5SDimitry Andric Function *F = BB->getParent();
437781ad6265SDimitry Andric Check(F->hasPersonalityFn(),
43780b57cec5SDimitry Andric "CatchPadInst needs to be in a function with a personality.", &CPI);
43790b57cec5SDimitry Andric
438081ad6265SDimitry Andric Check(isa<CatchSwitchInst>(CPI.getParentPad()),
43810b57cec5SDimitry Andric "CatchPadInst needs to be directly nested in a CatchSwitchInst.",
43820b57cec5SDimitry Andric CPI.getParentPad());
43830b57cec5SDimitry Andric
43840b57cec5SDimitry Andric // The catchpad instruction must be the first non-PHI instruction in the
43850b57cec5SDimitry Andric // block.
438681ad6265SDimitry Andric Check(BB->getFirstNonPHI() == &CPI,
43870b57cec5SDimitry Andric "CatchPadInst not the first non-PHI instruction in the block.", &CPI);
43880b57cec5SDimitry Andric
43890b57cec5SDimitry Andric visitEHPadPredecessors(CPI);
43900b57cec5SDimitry Andric visitFuncletPadInst(CPI);
43910b57cec5SDimitry Andric }
43920b57cec5SDimitry Andric
visitCatchReturnInst(CatchReturnInst & CatchReturn)43930b57cec5SDimitry Andric void Verifier::visitCatchReturnInst(CatchReturnInst &CatchReturn) {
439481ad6265SDimitry Andric Check(isa<CatchPadInst>(CatchReturn.getOperand(0)),
43950b57cec5SDimitry Andric "CatchReturnInst needs to be provided a CatchPad", &CatchReturn,
43960b57cec5SDimitry Andric CatchReturn.getOperand(0));
43970b57cec5SDimitry Andric
43980b57cec5SDimitry Andric visitTerminator(CatchReturn);
43990b57cec5SDimitry Andric }
44000b57cec5SDimitry Andric
visitCleanupPadInst(CleanupPadInst & CPI)44010b57cec5SDimitry Andric void Verifier::visitCleanupPadInst(CleanupPadInst &CPI) {
44020b57cec5SDimitry Andric BasicBlock *BB = CPI.getParent();
44030b57cec5SDimitry Andric
44040b57cec5SDimitry Andric Function *F = BB->getParent();
440581ad6265SDimitry Andric Check(F->hasPersonalityFn(),
44060b57cec5SDimitry Andric "CleanupPadInst needs to be in a function with a personality.", &CPI);
44070b57cec5SDimitry Andric
44080b57cec5SDimitry Andric // The cleanuppad instruction must be the first non-PHI instruction in the
44090b57cec5SDimitry Andric // block.
441081ad6265SDimitry Andric Check(BB->getFirstNonPHI() == &CPI,
441181ad6265SDimitry Andric "CleanupPadInst not the first non-PHI instruction in the block.", &CPI);
44120b57cec5SDimitry Andric
44130b57cec5SDimitry Andric auto *ParentPad = CPI.getParentPad();
441481ad6265SDimitry Andric Check(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
44150b57cec5SDimitry Andric "CleanupPadInst has an invalid parent.", &CPI);
44160b57cec5SDimitry Andric
44170b57cec5SDimitry Andric visitEHPadPredecessors(CPI);
44180b57cec5SDimitry Andric visitFuncletPadInst(CPI);
44190b57cec5SDimitry Andric }
44200b57cec5SDimitry Andric
visitFuncletPadInst(FuncletPadInst & FPI)44210b57cec5SDimitry Andric void Verifier::visitFuncletPadInst(FuncletPadInst &FPI) {
44220b57cec5SDimitry Andric User *FirstUser = nullptr;
44230b57cec5SDimitry Andric Value *FirstUnwindPad = nullptr;
44240b57cec5SDimitry Andric SmallVector<FuncletPadInst *, 8> Worklist({&FPI});
44250b57cec5SDimitry Andric SmallSet<FuncletPadInst *, 8> Seen;
44260b57cec5SDimitry Andric
44270b57cec5SDimitry Andric while (!Worklist.empty()) {
44280b57cec5SDimitry Andric FuncletPadInst *CurrentPad = Worklist.pop_back_val();
442981ad6265SDimitry Andric Check(Seen.insert(CurrentPad).second,
44300b57cec5SDimitry Andric "FuncletPadInst must not be nested within itself", CurrentPad);
44310b57cec5SDimitry Andric Value *UnresolvedAncestorPad = nullptr;
44320b57cec5SDimitry Andric for (User *U : CurrentPad->users()) {
44330b57cec5SDimitry Andric BasicBlock *UnwindDest;
44340b57cec5SDimitry Andric if (auto *CRI = dyn_cast<CleanupReturnInst>(U)) {
44350b57cec5SDimitry Andric UnwindDest = CRI->getUnwindDest();
44360b57cec5SDimitry Andric } else if (auto *CSI = dyn_cast<CatchSwitchInst>(U)) {
44370b57cec5SDimitry Andric // We allow catchswitch unwind to caller to nest
44380b57cec5SDimitry Andric // within an outer pad that unwinds somewhere else,
44390b57cec5SDimitry Andric // because catchswitch doesn't have a nounwind variant.
44400b57cec5SDimitry Andric // See e.g. SimplifyCFGOpt::SimplifyUnreachable.
44410b57cec5SDimitry Andric if (CSI->unwindsToCaller())
44420b57cec5SDimitry Andric continue;
44430b57cec5SDimitry Andric UnwindDest = CSI->getUnwindDest();
44440b57cec5SDimitry Andric } else if (auto *II = dyn_cast<InvokeInst>(U)) {
44450b57cec5SDimitry Andric UnwindDest = II->getUnwindDest();
44460b57cec5SDimitry Andric } else if (isa<CallInst>(U)) {
44470b57cec5SDimitry Andric // Calls which don't unwind may be found inside funclet
44480b57cec5SDimitry Andric // pads that unwind somewhere else. We don't *require*
44490b57cec5SDimitry Andric // such calls to be annotated nounwind.
44500b57cec5SDimitry Andric continue;
44510b57cec5SDimitry Andric } else if (auto *CPI = dyn_cast<CleanupPadInst>(U)) {
44520b57cec5SDimitry Andric // The unwind dest for a cleanup can only be found by
44530b57cec5SDimitry Andric // recursive search. Add it to the worklist, and we'll
44540b57cec5SDimitry Andric // search for its first use that determines where it unwinds.
44550b57cec5SDimitry Andric Worklist.push_back(CPI);
44560b57cec5SDimitry Andric continue;
44570b57cec5SDimitry Andric } else {
445881ad6265SDimitry Andric Check(isa<CatchReturnInst>(U), "Bogus funclet pad use", U);
44590b57cec5SDimitry Andric continue;
44600b57cec5SDimitry Andric }
44610b57cec5SDimitry Andric
44620b57cec5SDimitry Andric Value *UnwindPad;
44630b57cec5SDimitry Andric bool ExitsFPI;
44640b57cec5SDimitry Andric if (UnwindDest) {
44650b57cec5SDimitry Andric UnwindPad = UnwindDest->getFirstNonPHI();
44660b57cec5SDimitry Andric if (!cast<Instruction>(UnwindPad)->isEHPad())
44670b57cec5SDimitry Andric continue;
44680b57cec5SDimitry Andric Value *UnwindParent = getParentPad(UnwindPad);
44690b57cec5SDimitry Andric // Ignore unwind edges that don't exit CurrentPad.
44700b57cec5SDimitry Andric if (UnwindParent == CurrentPad)
44710b57cec5SDimitry Andric continue;
44720b57cec5SDimitry Andric // Determine whether the original funclet pad is exited,
44730b57cec5SDimitry Andric // and if we are scanning nested pads determine how many
44740b57cec5SDimitry Andric // of them are exited so we can stop searching their
44750b57cec5SDimitry Andric // children.
44760b57cec5SDimitry Andric Value *ExitedPad = CurrentPad;
44770b57cec5SDimitry Andric ExitsFPI = false;
44780b57cec5SDimitry Andric do {
44790b57cec5SDimitry Andric if (ExitedPad == &FPI) {
44800b57cec5SDimitry Andric ExitsFPI = true;
44810b57cec5SDimitry Andric // Now we can resolve any ancestors of CurrentPad up to
44820b57cec5SDimitry Andric // FPI, but not including FPI since we need to make sure
44830b57cec5SDimitry Andric // to check all direct users of FPI for consistency.
44840b57cec5SDimitry Andric UnresolvedAncestorPad = &FPI;
44850b57cec5SDimitry Andric break;
44860b57cec5SDimitry Andric }
44870b57cec5SDimitry Andric Value *ExitedParent = getParentPad(ExitedPad);
44880b57cec5SDimitry Andric if (ExitedParent == UnwindParent) {
44890b57cec5SDimitry Andric // ExitedPad is the ancestor-most pad which this unwind
44900b57cec5SDimitry Andric // edge exits, so we can resolve up to it, meaning that
44910b57cec5SDimitry Andric // ExitedParent is the first ancestor still unresolved.
44920b57cec5SDimitry Andric UnresolvedAncestorPad = ExitedParent;
44930b57cec5SDimitry Andric break;
44940b57cec5SDimitry Andric }
44950b57cec5SDimitry Andric ExitedPad = ExitedParent;
44960b57cec5SDimitry Andric } while (!isa<ConstantTokenNone>(ExitedPad));
44970b57cec5SDimitry Andric } else {
44980b57cec5SDimitry Andric // Unwinding to caller exits all pads.
44990b57cec5SDimitry Andric UnwindPad = ConstantTokenNone::get(FPI.getContext());
45000b57cec5SDimitry Andric ExitsFPI = true;
45010b57cec5SDimitry Andric UnresolvedAncestorPad = &FPI;
45020b57cec5SDimitry Andric }
45030b57cec5SDimitry Andric
45040b57cec5SDimitry Andric if (ExitsFPI) {
45050b57cec5SDimitry Andric // This unwind edge exits FPI. Make sure it agrees with other
45060b57cec5SDimitry Andric // such edges.
45070b57cec5SDimitry Andric if (FirstUser) {
450881ad6265SDimitry Andric Check(UnwindPad == FirstUnwindPad,
450981ad6265SDimitry Andric "Unwind edges out of a funclet "
45100b57cec5SDimitry Andric "pad must have the same unwind "
45110b57cec5SDimitry Andric "dest",
45120b57cec5SDimitry Andric &FPI, U, FirstUser);
45130b57cec5SDimitry Andric } else {
45140b57cec5SDimitry Andric FirstUser = U;
45150b57cec5SDimitry Andric FirstUnwindPad = UnwindPad;
45160b57cec5SDimitry Andric // Record cleanup sibling unwinds for verifySiblingFuncletUnwinds
45170b57cec5SDimitry Andric if (isa<CleanupPadInst>(&FPI) && !isa<ConstantTokenNone>(UnwindPad) &&
45180b57cec5SDimitry Andric getParentPad(UnwindPad) == getParentPad(&FPI))
45190b57cec5SDimitry Andric SiblingFuncletInfo[&FPI] = cast<Instruction>(U);
45200b57cec5SDimitry Andric }
45210b57cec5SDimitry Andric }
45220b57cec5SDimitry Andric // Make sure we visit all uses of FPI, but for nested pads stop as
45230b57cec5SDimitry Andric // soon as we know where they unwind to.
45240b57cec5SDimitry Andric if (CurrentPad != &FPI)
45250b57cec5SDimitry Andric break;
45260b57cec5SDimitry Andric }
45270b57cec5SDimitry Andric if (UnresolvedAncestorPad) {
45280b57cec5SDimitry Andric if (CurrentPad == UnresolvedAncestorPad) {
45290b57cec5SDimitry Andric // When CurrentPad is FPI itself, we don't mark it as resolved even if
45300b57cec5SDimitry Andric // we've found an unwind edge that exits it, because we need to verify
45310b57cec5SDimitry Andric // all direct uses of FPI.
45320b57cec5SDimitry Andric assert(CurrentPad == &FPI);
45330b57cec5SDimitry Andric continue;
45340b57cec5SDimitry Andric }
45350b57cec5SDimitry Andric // Pop off the worklist any nested pads that we've found an unwind
45360b57cec5SDimitry Andric // destination for. The pads on the worklist are the uncles,
45370b57cec5SDimitry Andric // great-uncles, etc. of CurrentPad. We've found an unwind destination
45380b57cec5SDimitry Andric // for all ancestors of CurrentPad up to but not including
45390b57cec5SDimitry Andric // UnresolvedAncestorPad.
45400b57cec5SDimitry Andric Value *ResolvedPad = CurrentPad;
45410b57cec5SDimitry Andric while (!Worklist.empty()) {
45420b57cec5SDimitry Andric Value *UnclePad = Worklist.back();
45430b57cec5SDimitry Andric Value *AncestorPad = getParentPad(UnclePad);
45440b57cec5SDimitry Andric // Walk ResolvedPad up the ancestor list until we either find the
45450b57cec5SDimitry Andric // uncle's parent or the last resolved ancestor.
45460b57cec5SDimitry Andric while (ResolvedPad != AncestorPad) {
45470b57cec5SDimitry Andric Value *ResolvedParent = getParentPad(ResolvedPad);
45480b57cec5SDimitry Andric if (ResolvedParent == UnresolvedAncestorPad) {
45490b57cec5SDimitry Andric break;
45500b57cec5SDimitry Andric }
45510b57cec5SDimitry Andric ResolvedPad = ResolvedParent;
45520b57cec5SDimitry Andric }
45530b57cec5SDimitry Andric // If the resolved ancestor search didn't find the uncle's parent,
45540b57cec5SDimitry Andric // then the uncle is not yet resolved.
45550b57cec5SDimitry Andric if (ResolvedPad != AncestorPad)
45560b57cec5SDimitry Andric break;
45570b57cec5SDimitry Andric // This uncle is resolved, so pop it from the worklist.
45580b57cec5SDimitry Andric Worklist.pop_back();
45590b57cec5SDimitry Andric }
45600b57cec5SDimitry Andric }
45610b57cec5SDimitry Andric }
45620b57cec5SDimitry Andric
45630b57cec5SDimitry Andric if (FirstUnwindPad) {
45640b57cec5SDimitry Andric if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FPI.getParentPad())) {
45650b57cec5SDimitry Andric BasicBlock *SwitchUnwindDest = CatchSwitch->getUnwindDest();
45660b57cec5SDimitry Andric Value *SwitchUnwindPad;
45670b57cec5SDimitry Andric if (SwitchUnwindDest)
45680b57cec5SDimitry Andric SwitchUnwindPad = SwitchUnwindDest->getFirstNonPHI();
45690b57cec5SDimitry Andric else
45700b57cec5SDimitry Andric SwitchUnwindPad = ConstantTokenNone::get(FPI.getContext());
457181ad6265SDimitry Andric Check(SwitchUnwindPad == FirstUnwindPad,
45720b57cec5SDimitry Andric "Unwind edges out of a catch must have the same unwind dest as "
45730b57cec5SDimitry Andric "the parent catchswitch",
45740b57cec5SDimitry Andric &FPI, FirstUser, CatchSwitch);
45750b57cec5SDimitry Andric }
45760b57cec5SDimitry Andric }
45770b57cec5SDimitry Andric
45780b57cec5SDimitry Andric visitInstruction(FPI);
45790b57cec5SDimitry Andric }
45800b57cec5SDimitry Andric
visitCatchSwitchInst(CatchSwitchInst & CatchSwitch)45810b57cec5SDimitry Andric void Verifier::visitCatchSwitchInst(CatchSwitchInst &CatchSwitch) {
45820b57cec5SDimitry Andric BasicBlock *BB = CatchSwitch.getParent();
45830b57cec5SDimitry Andric
45840b57cec5SDimitry Andric Function *F = BB->getParent();
458581ad6265SDimitry Andric Check(F->hasPersonalityFn(),
45860b57cec5SDimitry Andric "CatchSwitchInst needs to be in a function with a personality.",
45870b57cec5SDimitry Andric &CatchSwitch);
45880b57cec5SDimitry Andric
45890b57cec5SDimitry Andric // The catchswitch instruction must be the first non-PHI instruction in the
45900b57cec5SDimitry Andric // block.
459181ad6265SDimitry Andric Check(BB->getFirstNonPHI() == &CatchSwitch,
45920b57cec5SDimitry Andric "CatchSwitchInst not the first non-PHI instruction in the block.",
45930b57cec5SDimitry Andric &CatchSwitch);
45940b57cec5SDimitry Andric
45950b57cec5SDimitry Andric auto *ParentPad = CatchSwitch.getParentPad();
459681ad6265SDimitry Andric Check(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
45970b57cec5SDimitry Andric "CatchSwitchInst has an invalid parent.", ParentPad);
45980b57cec5SDimitry Andric
45990b57cec5SDimitry Andric if (BasicBlock *UnwindDest = CatchSwitch.getUnwindDest()) {
46000b57cec5SDimitry Andric Instruction *I = UnwindDest->getFirstNonPHI();
460181ad6265SDimitry Andric Check(I->isEHPad() && !isa<LandingPadInst>(I),
46020b57cec5SDimitry Andric "CatchSwitchInst must unwind to an EH block which is not a "
46030b57cec5SDimitry Andric "landingpad.",
46040b57cec5SDimitry Andric &CatchSwitch);
46050b57cec5SDimitry Andric
46060b57cec5SDimitry Andric // Record catchswitch sibling unwinds for verifySiblingFuncletUnwinds
46070b57cec5SDimitry Andric if (getParentPad(I) == ParentPad)
46080b57cec5SDimitry Andric SiblingFuncletInfo[&CatchSwitch] = &CatchSwitch;
46090b57cec5SDimitry Andric }
46100b57cec5SDimitry Andric
461181ad6265SDimitry Andric Check(CatchSwitch.getNumHandlers() != 0,
46120b57cec5SDimitry Andric "CatchSwitchInst cannot have empty handler list", &CatchSwitch);
46130b57cec5SDimitry Andric
46140b57cec5SDimitry Andric for (BasicBlock *Handler : CatchSwitch.handlers()) {
461581ad6265SDimitry Andric Check(isa<CatchPadInst>(Handler->getFirstNonPHI()),
46160b57cec5SDimitry Andric "CatchSwitchInst handlers must be catchpads", &CatchSwitch, Handler);
46170b57cec5SDimitry Andric }
46180b57cec5SDimitry Andric
46190b57cec5SDimitry Andric visitEHPadPredecessors(CatchSwitch);
46200b57cec5SDimitry Andric visitTerminator(CatchSwitch);
46210b57cec5SDimitry Andric }
46220b57cec5SDimitry Andric
visitCleanupReturnInst(CleanupReturnInst & CRI)46230b57cec5SDimitry Andric void Verifier::visitCleanupReturnInst(CleanupReturnInst &CRI) {
462481ad6265SDimitry Andric Check(isa<CleanupPadInst>(CRI.getOperand(0)),
46250b57cec5SDimitry Andric "CleanupReturnInst needs to be provided a CleanupPad", &CRI,
46260b57cec5SDimitry Andric CRI.getOperand(0));
46270b57cec5SDimitry Andric
46280b57cec5SDimitry Andric if (BasicBlock *UnwindDest = CRI.getUnwindDest()) {
46290b57cec5SDimitry Andric Instruction *I = UnwindDest->getFirstNonPHI();
463081ad6265SDimitry Andric Check(I->isEHPad() && !isa<LandingPadInst>(I),
46310b57cec5SDimitry Andric "CleanupReturnInst must unwind to an EH block which is not a "
46320b57cec5SDimitry Andric "landingpad.",
46330b57cec5SDimitry Andric &CRI);
46340b57cec5SDimitry Andric }
46350b57cec5SDimitry Andric
46360b57cec5SDimitry Andric visitTerminator(CRI);
46370b57cec5SDimitry Andric }
46380b57cec5SDimitry Andric
verifyDominatesUse(Instruction & I,unsigned i)46390b57cec5SDimitry Andric void Verifier::verifyDominatesUse(Instruction &I, unsigned i) {
46400b57cec5SDimitry Andric Instruction *Op = cast<Instruction>(I.getOperand(i));
46410b57cec5SDimitry Andric // If the we have an invalid invoke, don't try to compute the dominance.
46420b57cec5SDimitry Andric // We already reject it in the invoke specific checks and the dominance
46430b57cec5SDimitry Andric // computation doesn't handle multiple edges.
46440b57cec5SDimitry Andric if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
46450b57cec5SDimitry Andric if (II->getNormalDest() == II->getUnwindDest())
46460b57cec5SDimitry Andric return;
46470b57cec5SDimitry Andric }
46480b57cec5SDimitry Andric
46490b57cec5SDimitry Andric // Quick check whether the def has already been encountered in the same block.
46500b57cec5SDimitry Andric // PHI nodes are not checked to prevent accepting preceding PHIs, because PHI
46510b57cec5SDimitry Andric // uses are defined to happen on the incoming edge, not at the instruction.
46520b57cec5SDimitry Andric //
46530b57cec5SDimitry Andric // FIXME: If this operand is a MetadataAsValue (wrapping a LocalAsMetadata)
46540b57cec5SDimitry Andric // wrapping an SSA value, assert that we've already encountered it. See
46550b57cec5SDimitry Andric // related FIXME in Mapper::mapLocalAsMetadata in ValueMapper.cpp.
46560b57cec5SDimitry Andric if (!isa<PHINode>(I) && InstsInThisBlock.count(Op))
46570b57cec5SDimitry Andric return;
46580b57cec5SDimitry Andric
46590b57cec5SDimitry Andric const Use &U = I.getOperandUse(i);
466081ad6265SDimitry Andric Check(DT.dominates(Op, U), "Instruction does not dominate all uses!", Op, &I);
46610b57cec5SDimitry Andric }
46620b57cec5SDimitry Andric
visitDereferenceableMetadata(Instruction & I,MDNode * MD)46630b57cec5SDimitry Andric void Verifier::visitDereferenceableMetadata(Instruction& I, MDNode* MD) {
466481ad6265SDimitry Andric Check(I.getType()->isPointerTy(),
466581ad6265SDimitry Andric "dereferenceable, dereferenceable_or_null "
466681ad6265SDimitry Andric "apply only to pointer types",
466781ad6265SDimitry Andric &I);
466881ad6265SDimitry Andric Check((isa<LoadInst>(I) || isa<IntToPtrInst>(I)),
46690b57cec5SDimitry Andric "dereferenceable, dereferenceable_or_null apply only to load"
467081ad6265SDimitry Andric " and inttoptr instructions, use attributes for calls or invokes",
467181ad6265SDimitry Andric &I);
467281ad6265SDimitry Andric Check(MD->getNumOperands() == 1,
467381ad6265SDimitry Andric "dereferenceable, dereferenceable_or_null "
467481ad6265SDimitry Andric "take one operand!",
467581ad6265SDimitry Andric &I);
46760b57cec5SDimitry Andric ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(MD->getOperand(0));
467781ad6265SDimitry Andric Check(CI && CI->getType()->isIntegerTy(64),
467881ad6265SDimitry Andric "dereferenceable, "
467981ad6265SDimitry Andric "dereferenceable_or_null metadata value must be an i64!",
468081ad6265SDimitry Andric &I);
46810b57cec5SDimitry Andric }
46820b57cec5SDimitry Andric
visitProfMetadata(Instruction & I,MDNode * MD)46838bcb0991SDimitry Andric void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
468481ad6265SDimitry Andric Check(MD->getNumOperands() >= 2,
46858bcb0991SDimitry Andric "!prof annotations should have no less than 2 operands", MD);
46868bcb0991SDimitry Andric
46878bcb0991SDimitry Andric // Check first operand.
468881ad6265SDimitry Andric Check(MD->getOperand(0) != nullptr, "first operand should not be null", MD);
468981ad6265SDimitry Andric Check(isa<MDString>(MD->getOperand(0)),
46908bcb0991SDimitry Andric "expected string with name of the !prof annotation", MD);
46918bcb0991SDimitry Andric MDString *MDS = cast<MDString>(MD->getOperand(0));
46928bcb0991SDimitry Andric StringRef ProfName = MDS->getString();
46938bcb0991SDimitry Andric
46948bcb0991SDimitry Andric // Check consistency of !prof branch_weights metadata.
46958bcb0991SDimitry Andric if (ProfName.equals("branch_weights")) {
46965ffd83dbSDimitry Andric if (isa<InvokeInst>(&I)) {
469781ad6265SDimitry Andric Check(MD->getNumOperands() == 2 || MD->getNumOperands() == 3,
46985ffd83dbSDimitry Andric "Wrong number of InvokeInst branch_weights operands", MD);
46995ffd83dbSDimitry Andric } else {
47008bcb0991SDimitry Andric unsigned ExpectedNumOperands = 0;
47018bcb0991SDimitry Andric if (BranchInst *BI = dyn_cast<BranchInst>(&I))
47028bcb0991SDimitry Andric ExpectedNumOperands = BI->getNumSuccessors();
47038bcb0991SDimitry Andric else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I))
47048bcb0991SDimitry Andric ExpectedNumOperands = SI->getNumSuccessors();
47055ffd83dbSDimitry Andric else if (isa<CallInst>(&I))
47068bcb0991SDimitry Andric ExpectedNumOperands = 1;
47078bcb0991SDimitry Andric else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I))
47088bcb0991SDimitry Andric ExpectedNumOperands = IBI->getNumDestinations();
47098bcb0991SDimitry Andric else if (isa<SelectInst>(&I))
47108bcb0991SDimitry Andric ExpectedNumOperands = 2;
4711bdd1243dSDimitry Andric else if (CallBrInst *CI = dyn_cast<CallBrInst>(&I))
4712bdd1243dSDimitry Andric ExpectedNumOperands = CI->getNumSuccessors();
47138bcb0991SDimitry Andric else
47148bcb0991SDimitry Andric CheckFailed("!prof branch_weights are not allowed for this instruction",
47158bcb0991SDimitry Andric MD);
47168bcb0991SDimitry Andric
471781ad6265SDimitry Andric Check(MD->getNumOperands() == 1 + ExpectedNumOperands,
47188bcb0991SDimitry Andric "Wrong number of operands", MD);
47195ffd83dbSDimitry Andric }
47208bcb0991SDimitry Andric for (unsigned i = 1; i < MD->getNumOperands(); ++i) {
47218bcb0991SDimitry Andric auto &MDO = MD->getOperand(i);
472281ad6265SDimitry Andric Check(MDO, "second operand should not be null", MD);
472381ad6265SDimitry Andric Check(mdconst::dyn_extract<ConstantInt>(MDO),
47248bcb0991SDimitry Andric "!prof brunch_weights operand is not a const int");
47258bcb0991SDimitry Andric }
47268bcb0991SDimitry Andric }
47278bcb0991SDimitry Andric }
47288bcb0991SDimitry Andric
visitDIAssignIDMetadata(Instruction & I,MDNode * MD)4729bdd1243dSDimitry Andric void Verifier::visitDIAssignIDMetadata(Instruction &I, MDNode *MD) {
4730bdd1243dSDimitry Andric assert(I.hasMetadata(LLVMContext::MD_DIAssignID));
4731bdd1243dSDimitry Andric bool ExpectedInstTy =
4732bdd1243dSDimitry Andric isa<AllocaInst>(I) || isa<StoreInst>(I) || isa<MemIntrinsic>(I);
4733bdd1243dSDimitry Andric CheckDI(ExpectedInstTy, "!DIAssignID attached to unexpected instruction kind",
4734bdd1243dSDimitry Andric I, MD);
4735bdd1243dSDimitry Andric // Iterate over the MetadataAsValue uses of the DIAssignID - these should
4736bdd1243dSDimitry Andric // only be found as DbgAssignIntrinsic operands.
4737bdd1243dSDimitry Andric if (auto *AsValue = MetadataAsValue::getIfExists(Context, MD)) {
4738bdd1243dSDimitry Andric for (auto *User : AsValue->users()) {
4739bdd1243dSDimitry Andric CheckDI(isa<DbgAssignIntrinsic>(User),
4740bdd1243dSDimitry Andric "!DIAssignID should only be used by llvm.dbg.assign intrinsics",
4741bdd1243dSDimitry Andric MD, User);
4742bdd1243dSDimitry Andric // All of the dbg.assign intrinsics should be in the same function as I.
4743bdd1243dSDimitry Andric if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(User))
4744bdd1243dSDimitry Andric CheckDI(DAI->getFunction() == I.getFunction(),
4745bdd1243dSDimitry Andric "dbg.assign not in same function as inst", DAI, &I);
4746bdd1243dSDimitry Andric }
4747bdd1243dSDimitry Andric }
47487a6dacacSDimitry Andric for (DPValue *DPV : cast<DIAssignID>(MD)->getAllDPValueUsers()) {
47497a6dacacSDimitry Andric CheckDI(DPV->isDbgAssign(),
47507a6dacacSDimitry Andric "!DIAssignID should only be used by Assign DPVs.", MD, DPV);
47517a6dacacSDimitry Andric CheckDI(DPV->getFunction() == I.getFunction(),
47527a6dacacSDimitry Andric "DPVAssign not in same function as inst", DPV, &I);
47537a6dacacSDimitry Andric }
4754bdd1243dSDimitry Andric }
4755bdd1243dSDimitry Andric
visitCallStackMetadata(MDNode * MD)4756fcaf7f86SDimitry Andric void Verifier::visitCallStackMetadata(MDNode *MD) {
4757fcaf7f86SDimitry Andric // Call stack metadata should consist of a list of at least 1 constant int
4758fcaf7f86SDimitry Andric // (representing a hash of the location).
4759fcaf7f86SDimitry Andric Check(MD->getNumOperands() >= 1,
4760fcaf7f86SDimitry Andric "call stack metadata should have at least 1 operand", MD);
4761fcaf7f86SDimitry Andric
4762fcaf7f86SDimitry Andric for (const auto &Op : MD->operands())
4763fcaf7f86SDimitry Andric Check(mdconst::dyn_extract_or_null<ConstantInt>(Op),
4764fcaf7f86SDimitry Andric "call stack metadata operand should be constant integer", Op);
4765fcaf7f86SDimitry Andric }
4766fcaf7f86SDimitry Andric
visitMemProfMetadata(Instruction & I,MDNode * MD)4767fcaf7f86SDimitry Andric void Verifier::visitMemProfMetadata(Instruction &I, MDNode *MD) {
4768fcaf7f86SDimitry Andric Check(isa<CallBase>(I), "!memprof metadata should only exist on calls", &I);
4769fcaf7f86SDimitry Andric Check(MD->getNumOperands() >= 1,
4770fcaf7f86SDimitry Andric "!memprof annotations should have at least 1 metadata operand "
4771fcaf7f86SDimitry Andric "(MemInfoBlock)",
4772fcaf7f86SDimitry Andric MD);
4773fcaf7f86SDimitry Andric
4774fcaf7f86SDimitry Andric // Check each MIB
4775fcaf7f86SDimitry Andric for (auto &MIBOp : MD->operands()) {
4776fcaf7f86SDimitry Andric MDNode *MIB = dyn_cast<MDNode>(MIBOp);
4777fcaf7f86SDimitry Andric // The first operand of an MIB should be the call stack metadata.
4778fcaf7f86SDimitry Andric // There rest of the operands should be MDString tags, and there should be
4779fcaf7f86SDimitry Andric // at least one.
4780fcaf7f86SDimitry Andric Check(MIB->getNumOperands() >= 2,
4781fcaf7f86SDimitry Andric "Each !memprof MemInfoBlock should have at least 2 operands", MIB);
4782fcaf7f86SDimitry Andric
4783fcaf7f86SDimitry Andric // Check call stack metadata (first operand).
4784fcaf7f86SDimitry Andric Check(MIB->getOperand(0) != nullptr,
4785fcaf7f86SDimitry Andric "!memprof MemInfoBlock first operand should not be null", MIB);
4786fcaf7f86SDimitry Andric Check(isa<MDNode>(MIB->getOperand(0)),
4787fcaf7f86SDimitry Andric "!memprof MemInfoBlock first operand should be an MDNode", MIB);
4788fcaf7f86SDimitry Andric MDNode *StackMD = dyn_cast<MDNode>(MIB->getOperand(0));
4789fcaf7f86SDimitry Andric visitCallStackMetadata(StackMD);
4790fcaf7f86SDimitry Andric
4791fcaf7f86SDimitry Andric // Check that remaining operands are MDString.
4792bdd1243dSDimitry Andric Check(llvm::all_of(llvm::drop_begin(MIB->operands()),
4793fcaf7f86SDimitry Andric [](const MDOperand &Op) { return isa<MDString>(Op); }),
4794fcaf7f86SDimitry Andric "Not all !memprof MemInfoBlock operands 1 to N are MDString", MIB);
4795fcaf7f86SDimitry Andric }
4796fcaf7f86SDimitry Andric }
4797fcaf7f86SDimitry Andric
visitCallsiteMetadata(Instruction & I,MDNode * MD)4798fcaf7f86SDimitry Andric void Verifier::visitCallsiteMetadata(Instruction &I, MDNode *MD) {
4799fcaf7f86SDimitry Andric Check(isa<CallBase>(I), "!callsite metadata should only exist on calls", &I);
4800fcaf7f86SDimitry Andric // Verify the partial callstack annotated from memprof profiles. This callsite
4801fcaf7f86SDimitry Andric // is a part of a profiled allocation callstack.
4802fcaf7f86SDimitry Andric visitCallStackMetadata(MD);
4803fcaf7f86SDimitry Andric }
4804fcaf7f86SDimitry Andric
visitAnnotationMetadata(MDNode * Annotation)4805e8d8bef9SDimitry Andric void Verifier::visitAnnotationMetadata(MDNode *Annotation) {
480681ad6265SDimitry Andric Check(isa<MDTuple>(Annotation), "annotation must be a tuple");
480781ad6265SDimitry Andric Check(Annotation->getNumOperands() >= 1,
4808e8d8bef9SDimitry Andric "annotation must have at least one operand");
480906c3fb27SDimitry Andric for (const MDOperand &Op : Annotation->operands()) {
481006c3fb27SDimitry Andric bool TupleOfStrings =
481106c3fb27SDimitry Andric isa<MDTuple>(Op.get()) &&
481206c3fb27SDimitry Andric all_of(cast<MDTuple>(Op)->operands(), [](auto &Annotation) {
481306c3fb27SDimitry Andric return isa<MDString>(Annotation.get());
481406c3fb27SDimitry Andric });
481506c3fb27SDimitry Andric Check(isa<MDString>(Op.get()) || TupleOfStrings,
481606c3fb27SDimitry Andric "operands must be a string or a tuple of strings");
481706c3fb27SDimitry Andric }
4818e8d8bef9SDimitry Andric }
4819e8d8bef9SDimitry Andric
visitAliasScopeMetadata(const MDNode * MD)4820349cc55cSDimitry Andric void Verifier::visitAliasScopeMetadata(const MDNode *MD) {
4821349cc55cSDimitry Andric unsigned NumOps = MD->getNumOperands();
482281ad6265SDimitry Andric Check(NumOps >= 2 && NumOps <= 3, "scope must have two or three operands",
4823349cc55cSDimitry Andric MD);
482481ad6265SDimitry Andric Check(MD->getOperand(0).get() == MD || isa<MDString>(MD->getOperand(0)),
4825349cc55cSDimitry Andric "first scope operand must be self-referential or string", MD);
4826349cc55cSDimitry Andric if (NumOps == 3)
482781ad6265SDimitry Andric Check(isa<MDString>(MD->getOperand(2)),
4828349cc55cSDimitry Andric "third scope operand must be string (if used)", MD);
4829349cc55cSDimitry Andric
4830349cc55cSDimitry Andric MDNode *Domain = dyn_cast<MDNode>(MD->getOperand(1));
483181ad6265SDimitry Andric Check(Domain != nullptr, "second scope operand must be MDNode", MD);
4832349cc55cSDimitry Andric
4833349cc55cSDimitry Andric unsigned NumDomainOps = Domain->getNumOperands();
483481ad6265SDimitry Andric Check(NumDomainOps >= 1 && NumDomainOps <= 2,
4835349cc55cSDimitry Andric "domain must have one or two operands", Domain);
483681ad6265SDimitry Andric Check(Domain->getOperand(0).get() == Domain ||
4837349cc55cSDimitry Andric isa<MDString>(Domain->getOperand(0)),
4838349cc55cSDimitry Andric "first domain operand must be self-referential or string", Domain);
4839349cc55cSDimitry Andric if (NumDomainOps == 2)
484081ad6265SDimitry Andric Check(isa<MDString>(Domain->getOperand(1)),
4841349cc55cSDimitry Andric "second domain operand must be string (if used)", Domain);
4842349cc55cSDimitry Andric }
4843349cc55cSDimitry Andric
visitAliasScopeListMetadata(const MDNode * MD)4844349cc55cSDimitry Andric void Verifier::visitAliasScopeListMetadata(const MDNode *MD) {
4845349cc55cSDimitry Andric for (const MDOperand &Op : MD->operands()) {
4846349cc55cSDimitry Andric const MDNode *OpMD = dyn_cast<MDNode>(Op);
484781ad6265SDimitry Andric Check(OpMD != nullptr, "scope list must consist of MDNodes", MD);
4848349cc55cSDimitry Andric visitAliasScopeMetadata(OpMD);
4849349cc55cSDimitry Andric }
4850349cc55cSDimitry Andric }
4851349cc55cSDimitry Andric
visitAccessGroupMetadata(const MDNode * MD)485281ad6265SDimitry Andric void Verifier::visitAccessGroupMetadata(const MDNode *MD) {
485381ad6265SDimitry Andric auto IsValidAccessScope = [](const MDNode *MD) {
485481ad6265SDimitry Andric return MD->getNumOperands() == 0 && MD->isDistinct();
485581ad6265SDimitry Andric };
485681ad6265SDimitry Andric
485781ad6265SDimitry Andric // It must be either an access scope itself...
485881ad6265SDimitry Andric if (IsValidAccessScope(MD))
485981ad6265SDimitry Andric return;
486081ad6265SDimitry Andric
486181ad6265SDimitry Andric // ...or a list of access scopes.
486281ad6265SDimitry Andric for (const MDOperand &Op : MD->operands()) {
486381ad6265SDimitry Andric const MDNode *OpMD = dyn_cast<MDNode>(Op);
486481ad6265SDimitry Andric Check(OpMD != nullptr, "Access scope list must consist of MDNodes", MD);
486581ad6265SDimitry Andric Check(IsValidAccessScope(OpMD),
486681ad6265SDimitry Andric "Access scope list contains invalid access scope", MD);
486781ad6265SDimitry Andric }
486881ad6265SDimitry Andric }
486981ad6265SDimitry Andric
48700b57cec5SDimitry Andric /// verifyInstruction - Verify that an instruction is well formed.
48710b57cec5SDimitry Andric ///
visitInstruction(Instruction & I)48720b57cec5SDimitry Andric void Verifier::visitInstruction(Instruction &I) {
48730b57cec5SDimitry Andric BasicBlock *BB = I.getParent();
487481ad6265SDimitry Andric Check(BB, "Instruction not embedded in basic block!", &I);
48750b57cec5SDimitry Andric
48760b57cec5SDimitry Andric if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
48770b57cec5SDimitry Andric for (User *U : I.users()) {
487881ad6265SDimitry Andric Check(U != (User *)&I || !DT.isReachableFromEntry(BB),
48790b57cec5SDimitry Andric "Only PHI nodes may reference their own value!", &I);
48800b57cec5SDimitry Andric }
48810b57cec5SDimitry Andric }
48820b57cec5SDimitry Andric
48830b57cec5SDimitry Andric // Check that void typed values don't have names
488481ad6265SDimitry Andric Check(!I.getType()->isVoidTy() || !I.hasName(),
48850b57cec5SDimitry Andric "Instruction has a name, but provides a void value!", &I);
48860b57cec5SDimitry Andric
48870b57cec5SDimitry Andric // Check that the return value of the instruction is either void or a legal
48880b57cec5SDimitry Andric // value type.
488981ad6265SDimitry Andric Check(I.getType()->isVoidTy() || I.getType()->isFirstClassType(),
48900b57cec5SDimitry Andric "Instruction returns a non-scalar type!", &I);
48910b57cec5SDimitry Andric
48920b57cec5SDimitry Andric // Check that the instruction doesn't produce metadata. Calls are already
48930b57cec5SDimitry Andric // checked against the callee type.
489481ad6265SDimitry Andric Check(!I.getType()->isMetadataTy() || isa<CallInst>(I) || isa<InvokeInst>(I),
48950b57cec5SDimitry Andric "Invalid use of metadata!", &I);
48960b57cec5SDimitry Andric
48970b57cec5SDimitry Andric // Check that all uses of the instruction, if they are instructions
48980b57cec5SDimitry Andric // themselves, actually have parent basic blocks. If the use is not an
48990b57cec5SDimitry Andric // instruction, it is an error!
49000b57cec5SDimitry Andric for (Use &U : I.uses()) {
49010b57cec5SDimitry Andric if (Instruction *Used = dyn_cast<Instruction>(U.getUser()))
490281ad6265SDimitry Andric Check(Used->getParent() != nullptr,
49030b57cec5SDimitry Andric "Instruction referencing"
49040b57cec5SDimitry Andric " instruction not embedded in a basic block!",
49050b57cec5SDimitry Andric &I, Used);
49060b57cec5SDimitry Andric else {
49070b57cec5SDimitry Andric CheckFailed("Use of instruction is not an instruction!", U);
49080b57cec5SDimitry Andric return;
49090b57cec5SDimitry Andric }
49100b57cec5SDimitry Andric }
49110b57cec5SDimitry Andric
49120b57cec5SDimitry Andric // Get a pointer to the call base of the instruction if it is some form of
49130b57cec5SDimitry Andric // call.
49140b57cec5SDimitry Andric const CallBase *CBI = dyn_cast<CallBase>(&I);
49150b57cec5SDimitry Andric
49160b57cec5SDimitry Andric for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
491781ad6265SDimitry Andric Check(I.getOperand(i) != nullptr, "Instruction has null operand!", &I);
49180b57cec5SDimitry Andric
49190b57cec5SDimitry Andric // Check to make sure that only first-class-values are operands to
49200b57cec5SDimitry Andric // instructions.
49210b57cec5SDimitry Andric if (!I.getOperand(i)->getType()->isFirstClassType()) {
492281ad6265SDimitry Andric Check(false, "Instruction operands must be first-class values!", &I);
49230b57cec5SDimitry Andric }
49240b57cec5SDimitry Andric
49250b57cec5SDimitry Andric if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
4926349cc55cSDimitry Andric // This code checks whether the function is used as the operand of a
4927349cc55cSDimitry Andric // clang_arc_attachedcall operand bundle.
4928349cc55cSDimitry Andric auto IsAttachedCallOperand = [](Function *F, const CallBase *CBI,
4929349cc55cSDimitry Andric int Idx) {
4930349cc55cSDimitry Andric return CBI && CBI->isOperandBundleOfType(
4931349cc55cSDimitry Andric LLVMContext::OB_clang_arc_attachedcall, Idx);
4932349cc55cSDimitry Andric };
4933349cc55cSDimitry Andric
49340b57cec5SDimitry Andric // Check to make sure that the "address of" an intrinsic function is never
4935349cc55cSDimitry Andric // taken. Ignore cases where the address of the intrinsic function is used
4936349cc55cSDimitry Andric // as the argument of operand bundle "clang.arc.attachedcall" as those
4937349cc55cSDimitry Andric // cases are handled in verifyAttachedCallBundle.
493881ad6265SDimitry Andric Check((!F->isIntrinsic() ||
4939349cc55cSDimitry Andric (CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i)) ||
4940349cc55cSDimitry Andric IsAttachedCallOperand(F, CBI, i)),
49410b57cec5SDimitry Andric "Cannot take the address of an intrinsic!", &I);
494281ad6265SDimitry Andric Check(!F->isIntrinsic() || isa<CallInst>(I) ||
49430b57cec5SDimitry Andric F->getIntrinsicID() == Intrinsic::donothing ||
4944fe6060f1SDimitry Andric F->getIntrinsicID() == Intrinsic::seh_try_begin ||
4945fe6060f1SDimitry Andric F->getIntrinsicID() == Intrinsic::seh_try_end ||
4946fe6060f1SDimitry Andric F->getIntrinsicID() == Intrinsic::seh_scope_begin ||
4947fe6060f1SDimitry Andric F->getIntrinsicID() == Intrinsic::seh_scope_end ||
49480b57cec5SDimitry Andric F->getIntrinsicID() == Intrinsic::coro_resume ||
49490b57cec5SDimitry Andric F->getIntrinsicID() == Intrinsic::coro_destroy ||
495081ad6265SDimitry Andric F->getIntrinsicID() ==
495181ad6265SDimitry Andric Intrinsic::experimental_patchpoint_void ||
49520b57cec5SDimitry Andric F->getIntrinsicID() == Intrinsic::experimental_patchpoint_i64 ||
49530b57cec5SDimitry Andric F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint ||
4954349cc55cSDimitry Andric F->getIntrinsicID() == Intrinsic::wasm_rethrow ||
4955349cc55cSDimitry Andric IsAttachedCallOperand(F, CBI, i),
49560b57cec5SDimitry Andric "Cannot invoke an intrinsic other than donothing, patchpoint, "
4957349cc55cSDimitry Andric "statepoint, coro_resume, coro_destroy or clang.arc.attachedcall",
49580b57cec5SDimitry Andric &I);
495981ad6265SDimitry Andric Check(F->getParent() == &M, "Referencing function in another module!", &I,
496081ad6265SDimitry Andric &M, F, F->getParent());
49610b57cec5SDimitry Andric } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
496281ad6265SDimitry Andric Check(OpBB->getParent() == BB->getParent(),
49630b57cec5SDimitry Andric "Referring to a basic block in another function!", &I);
49640b57cec5SDimitry Andric } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
496581ad6265SDimitry Andric Check(OpArg->getParent() == BB->getParent(),
49660b57cec5SDimitry Andric "Referring to an argument in another function!", &I);
49670b57cec5SDimitry Andric } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) {
496881ad6265SDimitry Andric Check(GV->getParent() == &M, "Referencing global in another module!", &I,
49690b57cec5SDimitry Andric &M, GV, GV->getParent());
49700b57cec5SDimitry Andric } else if (isa<Instruction>(I.getOperand(i))) {
49710b57cec5SDimitry Andric verifyDominatesUse(I, i);
49720b57cec5SDimitry Andric } else if (isa<InlineAsm>(I.getOperand(i))) {
497381ad6265SDimitry Andric Check(CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i),
49740b57cec5SDimitry Andric "Cannot take the address of an inline asm!", &I);
49750b57cec5SDimitry Andric } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(i))) {
4976fe6060f1SDimitry Andric if (CE->getType()->isPtrOrPtrVectorTy()) {
49770b57cec5SDimitry Andric // If we have a ConstantExpr pointer, we need to see if it came from an
4978fe6060f1SDimitry Andric // illegal bitcast.
49790b57cec5SDimitry Andric visitConstantExprsRecursively(CE);
49800b57cec5SDimitry Andric }
49810b57cec5SDimitry Andric }
49820b57cec5SDimitry Andric }
49830b57cec5SDimitry Andric
49840b57cec5SDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpmath)) {
498581ad6265SDimitry Andric Check(I.getType()->isFPOrFPVectorTy(),
49860b57cec5SDimitry Andric "fpmath requires a floating point result!", &I);
498781ad6265SDimitry Andric Check(MD->getNumOperands() == 1, "fpmath takes one operand!", &I);
49880b57cec5SDimitry Andric if (ConstantFP *CFP0 =
49890b57cec5SDimitry Andric mdconst::dyn_extract_or_null<ConstantFP>(MD->getOperand(0))) {
49900b57cec5SDimitry Andric const APFloat &Accuracy = CFP0->getValueAPF();
499181ad6265SDimitry Andric Check(&Accuracy.getSemantics() == &APFloat::IEEEsingle(),
49920b57cec5SDimitry Andric "fpmath accuracy must have float type", &I);
499381ad6265SDimitry Andric Check(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(),
49940b57cec5SDimitry Andric "fpmath accuracy not a positive number!", &I);
49950b57cec5SDimitry Andric } else {
499681ad6265SDimitry Andric Check(false, "invalid fpmath accuracy!", &I);
49970b57cec5SDimitry Andric }
49980b57cec5SDimitry Andric }
49990b57cec5SDimitry Andric
50000b57cec5SDimitry Andric if (MDNode *Range = I.getMetadata(LLVMContext::MD_range)) {
500181ad6265SDimitry Andric Check(isa<LoadInst>(I) || isa<CallInst>(I) || isa<InvokeInst>(I),
50020b57cec5SDimitry Andric "Ranges are only for loads, calls and invokes!", &I);
50030b57cec5SDimitry Andric visitRangeMetadata(I, Range, I.getType());
50040b57cec5SDimitry Andric }
50050b57cec5SDimitry Andric
5006349cc55cSDimitry Andric if (I.hasMetadata(LLVMContext::MD_invariant_group)) {
500781ad6265SDimitry Andric Check(isa<LoadInst>(I) || isa<StoreInst>(I),
5008349cc55cSDimitry Andric "invariant.group metadata is only for loads and stores", &I);
5009349cc55cSDimitry Andric }
5010349cc55cSDimitry Andric
5011bdd1243dSDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_nonnull)) {
501281ad6265SDimitry Andric Check(I.getType()->isPointerTy(), "nonnull applies only to pointer types",
50130b57cec5SDimitry Andric &I);
501481ad6265SDimitry Andric Check(isa<LoadInst>(I),
50150b57cec5SDimitry Andric "nonnull applies only to load instructions, use attributes"
50160b57cec5SDimitry Andric " for calls or invokes",
50170b57cec5SDimitry Andric &I);
5018bdd1243dSDimitry Andric Check(MD->getNumOperands() == 0, "nonnull metadata must be empty", &I);
50190b57cec5SDimitry Andric }
50200b57cec5SDimitry Andric
50210b57cec5SDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable))
50220b57cec5SDimitry Andric visitDereferenceableMetadata(I, MD);
50230b57cec5SDimitry Andric
50240b57cec5SDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable_or_null))
50250b57cec5SDimitry Andric visitDereferenceableMetadata(I, MD);
50260b57cec5SDimitry Andric
50270b57cec5SDimitry Andric if (MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa))
50280b57cec5SDimitry Andric TBAAVerifyHelper.visitTBAAMetadata(I, TBAA);
50290b57cec5SDimitry Andric
5030349cc55cSDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_noalias))
5031349cc55cSDimitry Andric visitAliasScopeListMetadata(MD);
5032349cc55cSDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_alias_scope))
5033349cc55cSDimitry Andric visitAliasScopeListMetadata(MD);
5034349cc55cSDimitry Andric
503581ad6265SDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_access_group))
503681ad6265SDimitry Andric visitAccessGroupMetadata(MD);
503781ad6265SDimitry Andric
50380b57cec5SDimitry Andric if (MDNode *AlignMD = I.getMetadata(LLVMContext::MD_align)) {
503981ad6265SDimitry Andric Check(I.getType()->isPointerTy(), "align applies only to pointer types",
50400b57cec5SDimitry Andric &I);
504181ad6265SDimitry Andric Check(isa<LoadInst>(I),
504281ad6265SDimitry Andric "align applies only to load instructions, "
504381ad6265SDimitry Andric "use attributes for calls or invokes",
504481ad6265SDimitry Andric &I);
504581ad6265SDimitry Andric Check(AlignMD->getNumOperands() == 1, "align takes one operand!", &I);
50460b57cec5SDimitry Andric ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(AlignMD->getOperand(0));
504781ad6265SDimitry Andric Check(CI && CI->getType()->isIntegerTy(64),
50480b57cec5SDimitry Andric "align metadata value must be an i64!", &I);
50490b57cec5SDimitry Andric uint64_t Align = CI->getZExtValue();
505081ad6265SDimitry Andric Check(isPowerOf2_64(Align), "align metadata value must be a power of 2!",
505181ad6265SDimitry Andric &I);
505281ad6265SDimitry Andric Check(Align <= Value::MaximumAlignment,
50530b57cec5SDimitry Andric "alignment is larger that implementation defined limit", &I);
50540b57cec5SDimitry Andric }
50550b57cec5SDimitry Andric
50568bcb0991SDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_prof))
50578bcb0991SDimitry Andric visitProfMetadata(I, MD);
50588bcb0991SDimitry Andric
5059fcaf7f86SDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_memprof))
5060fcaf7f86SDimitry Andric visitMemProfMetadata(I, MD);
5061fcaf7f86SDimitry Andric
5062fcaf7f86SDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_callsite))
5063fcaf7f86SDimitry Andric visitCallsiteMetadata(I, MD);
5064fcaf7f86SDimitry Andric
5065bdd1243dSDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_DIAssignID))
5066bdd1243dSDimitry Andric visitDIAssignIDMetadata(I, MD);
5067bdd1243dSDimitry Andric
5068e8d8bef9SDimitry Andric if (MDNode *Annotation = I.getMetadata(LLVMContext::MD_annotation))
5069e8d8bef9SDimitry Andric visitAnnotationMetadata(Annotation);
5070e8d8bef9SDimitry Andric
50710b57cec5SDimitry Andric if (MDNode *N = I.getDebugLoc().getAsMDNode()) {
507281ad6265SDimitry Andric CheckDI(isa<DILocation>(N), "invalid !dbg metadata attachment", &I, N);
50735ffd83dbSDimitry Andric visitMDNode(*N, AreDebugLocsAllowed::Yes);
50740b57cec5SDimitry Andric }
50750b57cec5SDimitry Andric
50768bcb0991SDimitry Andric if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) {
50770b57cec5SDimitry Andric verifyFragmentExpression(*DII);
50788bcb0991SDimitry Andric verifyNotEntryValue(*DII);
50798bcb0991SDimitry Andric }
50800b57cec5SDimitry Andric
50815ffd83dbSDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
50825ffd83dbSDimitry Andric I.getAllMetadata(MDs);
50835ffd83dbSDimitry Andric for (auto Attachment : MDs) {
50845ffd83dbSDimitry Andric unsigned Kind = Attachment.first;
50855ffd83dbSDimitry Andric auto AllowLocs =
50865ffd83dbSDimitry Andric (Kind == LLVMContext::MD_dbg || Kind == LLVMContext::MD_loop)
50875ffd83dbSDimitry Andric ? AreDebugLocsAllowed::Yes
50885ffd83dbSDimitry Andric : AreDebugLocsAllowed::No;
50895ffd83dbSDimitry Andric visitMDNode(*Attachment.second, AllowLocs);
50905ffd83dbSDimitry Andric }
50915ffd83dbSDimitry Andric
50920b57cec5SDimitry Andric InstsInThisBlock.insert(&I);
50930b57cec5SDimitry Andric }
50940b57cec5SDimitry Andric
50950b57cec5SDimitry Andric /// Allow intrinsics to be verified in different ways.
visitIntrinsicCall(Intrinsic::ID ID,CallBase & Call)50960b57cec5SDimitry Andric void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
50970b57cec5SDimitry Andric Function *IF = Call.getCalledFunction();
509881ad6265SDimitry Andric Check(IF->isDeclaration(), "Intrinsic functions should never be defined!",
50990b57cec5SDimitry Andric IF);
51000b57cec5SDimitry Andric
51010b57cec5SDimitry Andric // Verify that the intrinsic prototype lines up with what the .td files
51020b57cec5SDimitry Andric // describe.
51030b57cec5SDimitry Andric FunctionType *IFTy = IF->getFunctionType();
51040b57cec5SDimitry Andric bool IsVarArg = IFTy->isVarArg();
51050b57cec5SDimitry Andric
51060b57cec5SDimitry Andric SmallVector<Intrinsic::IITDescriptor, 8> Table;
51070b57cec5SDimitry Andric getIntrinsicInfoTableEntries(ID, Table);
51080b57cec5SDimitry Andric ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
51090b57cec5SDimitry Andric
51100b57cec5SDimitry Andric // Walk the descriptors to extract overloaded types.
51110b57cec5SDimitry Andric SmallVector<Type *, 4> ArgTys;
51120b57cec5SDimitry Andric Intrinsic::MatchIntrinsicTypesResult Res =
51130b57cec5SDimitry Andric Intrinsic::matchIntrinsicSignature(IFTy, TableRef, ArgTys);
511481ad6265SDimitry Andric Check(Res != Intrinsic::MatchIntrinsicTypes_NoMatchRet,
51150b57cec5SDimitry Andric "Intrinsic has incorrect return type!", IF);
511681ad6265SDimitry Andric Check(Res != Intrinsic::MatchIntrinsicTypes_NoMatchArg,
51170b57cec5SDimitry Andric "Intrinsic has incorrect argument type!", IF);
51180b57cec5SDimitry Andric
51190b57cec5SDimitry Andric // Verify if the intrinsic call matches the vararg property.
51200b57cec5SDimitry Andric if (IsVarArg)
512181ad6265SDimitry Andric Check(!Intrinsic::matchIntrinsicVarArg(IsVarArg, TableRef),
51220b57cec5SDimitry Andric "Intrinsic was not defined with variable arguments!", IF);
51230b57cec5SDimitry Andric else
512481ad6265SDimitry Andric Check(!Intrinsic::matchIntrinsicVarArg(IsVarArg, TableRef),
51250b57cec5SDimitry Andric "Callsite was not defined with variable arguments!", IF);
51260b57cec5SDimitry Andric
51270b57cec5SDimitry Andric // All descriptors should be absorbed by now.
512881ad6265SDimitry Andric Check(TableRef.empty(), "Intrinsic has too few arguments!", IF);
51290b57cec5SDimitry Andric
51300b57cec5SDimitry Andric // Now that we have the intrinsic ID and the actual argument types (and we
51310b57cec5SDimitry Andric // know they are legal for the intrinsic!) get the intrinsic name through the
51320b57cec5SDimitry Andric // usual means. This allows us to verify the mangling of argument types into
51330b57cec5SDimitry Andric // the name.
5134fe6060f1SDimitry Andric const std::string ExpectedName =
5135fe6060f1SDimitry Andric Intrinsic::getName(ID, ArgTys, IF->getParent(), IFTy);
513681ad6265SDimitry Andric Check(ExpectedName == IF->getName(),
51370b57cec5SDimitry Andric "Intrinsic name not mangled correctly for type arguments! "
51380b57cec5SDimitry Andric "Should be: " +
51390b57cec5SDimitry Andric ExpectedName,
51400b57cec5SDimitry Andric IF);
51410b57cec5SDimitry Andric
51420b57cec5SDimitry Andric // If the intrinsic takes MDNode arguments, verify that they are either global
51430b57cec5SDimitry Andric // or are local to *this* function.
5144fe6060f1SDimitry Andric for (Value *V : Call.args()) {
51450b57cec5SDimitry Andric if (auto *MD = dyn_cast<MetadataAsValue>(V))
51460b57cec5SDimitry Andric visitMetadataAsValue(*MD, Call.getCaller());
5147fe6060f1SDimitry Andric if (auto *Const = dyn_cast<Constant>(V))
514881ad6265SDimitry Andric Check(!Const->getType()->isX86_AMXTy(),
5149fe6060f1SDimitry Andric "const x86_amx is not allowed in argument!");
5150fe6060f1SDimitry Andric }
51510b57cec5SDimitry Andric
51520b57cec5SDimitry Andric switch (ID) {
51530b57cec5SDimitry Andric default:
51540b57cec5SDimitry Andric break;
51555ffd83dbSDimitry Andric case Intrinsic::assume: {
51565ffd83dbSDimitry Andric for (auto &Elem : Call.bundle_op_infos()) {
5157bdd1243dSDimitry Andric unsigned ArgCount = Elem.End - Elem.Begin;
5158bdd1243dSDimitry Andric // Separate storage assumptions are special insofar as they're the only
5159bdd1243dSDimitry Andric // operand bundles allowed on assumes that aren't parameter attributes.
5160bdd1243dSDimitry Andric if (Elem.Tag->getKey() == "separate_storage") {
5161bdd1243dSDimitry Andric Check(ArgCount == 2,
5162bdd1243dSDimitry Andric "separate_storage assumptions should have 2 arguments", Call);
5163bdd1243dSDimitry Andric Check(Call.getOperand(Elem.Begin)->getType()->isPointerTy() &&
5164bdd1243dSDimitry Andric Call.getOperand(Elem.Begin + 1)->getType()->isPointerTy(),
5165bdd1243dSDimitry Andric "arguments to separate_storage assumptions should be pointers",
5166bdd1243dSDimitry Andric Call);
5167bdd1243dSDimitry Andric return;
5168bdd1243dSDimitry Andric }
516981ad6265SDimitry Andric Check(Elem.Tag->getKey() == "ignore" ||
51705ffd83dbSDimitry Andric Attribute::isExistingAttribute(Elem.Tag->getKey()),
5171349cc55cSDimitry Andric "tags must be valid attribute names", Call);
51725ffd83dbSDimitry Andric Attribute::AttrKind Kind =
51735ffd83dbSDimitry Andric Attribute::getAttrKindFromName(Elem.Tag->getKey());
5174e8d8bef9SDimitry Andric if (Kind == Attribute::Alignment) {
517581ad6265SDimitry Andric Check(ArgCount <= 3 && ArgCount >= 2,
5176349cc55cSDimitry Andric "alignment assumptions should have 2 or 3 arguments", Call);
517781ad6265SDimitry Andric Check(Call.getOperand(Elem.Begin)->getType()->isPointerTy(),
5178349cc55cSDimitry Andric "first argument should be a pointer", Call);
517981ad6265SDimitry Andric Check(Call.getOperand(Elem.Begin + 1)->getType()->isIntegerTy(),
5180349cc55cSDimitry Andric "second argument should be an integer", Call);
5181e8d8bef9SDimitry Andric if (ArgCount == 3)
518281ad6265SDimitry Andric Check(Call.getOperand(Elem.Begin + 2)->getType()->isIntegerTy(),
5183349cc55cSDimitry Andric "third argument should be an integer if present", Call);
5184e8d8bef9SDimitry Andric return;
5185e8d8bef9SDimitry Andric }
518681ad6265SDimitry Andric Check(ArgCount <= 2, "too many arguments", Call);
51875ffd83dbSDimitry Andric if (Kind == Attribute::None)
51885ffd83dbSDimitry Andric break;
5189fe6060f1SDimitry Andric if (Attribute::isIntAttrKind(Kind)) {
519081ad6265SDimitry Andric Check(ArgCount == 2, "this attribute should have 2 arguments", Call);
519181ad6265SDimitry Andric Check(isa<ConstantInt>(Call.getOperand(Elem.Begin + 1)),
5192349cc55cSDimitry Andric "the second argument should be a constant integral value", Call);
5193fe6060f1SDimitry Andric } else if (Attribute::canUseAsParamAttr(Kind)) {
519481ad6265SDimitry Andric Check((ArgCount) == 1, "this attribute should have one argument", Call);
5195fe6060f1SDimitry Andric } else if (Attribute::canUseAsFnAttr(Kind)) {
519681ad6265SDimitry Andric Check((ArgCount) == 0, "this attribute has no argument", Call);
51975ffd83dbSDimitry Andric }
51985ffd83dbSDimitry Andric }
51995ffd83dbSDimitry Andric break;
52005ffd83dbSDimitry Andric }
52010b57cec5SDimitry Andric case Intrinsic::coro_id: {
52020b57cec5SDimitry Andric auto *InfoArg = Call.getArgOperand(3)->stripPointerCasts();
52030b57cec5SDimitry Andric if (isa<ConstantPointerNull>(InfoArg))
52040b57cec5SDimitry Andric break;
52050b57cec5SDimitry Andric auto *GV = dyn_cast<GlobalVariable>(InfoArg);
520681ad6265SDimitry Andric Check(GV && GV->isConstant() && GV->hasDefinitiveInitializer(),
5207fe6060f1SDimitry Andric "info argument of llvm.coro.id must refer to an initialized "
52080b57cec5SDimitry Andric "constant");
52090b57cec5SDimitry Andric Constant *Init = GV->getInitializer();
521081ad6265SDimitry Andric Check(isa<ConstantStruct>(Init) || isa<ConstantArray>(Init),
5211fe6060f1SDimitry Andric "info argument of llvm.coro.id must refer to either a struct or "
52120b57cec5SDimitry Andric "an array");
52130b57cec5SDimitry Andric break;
52140b57cec5SDimitry Andric }
5215bdd1243dSDimitry Andric case Intrinsic::is_fpclass: {
5216bdd1243dSDimitry Andric const ConstantInt *TestMask = cast<ConstantInt>(Call.getOperand(1));
521706c3fb27SDimitry Andric Check((TestMask->getZExtValue() & ~static_cast<unsigned>(fcAllFlags)) == 0,
5218bdd1243dSDimitry Andric "unsupported bits for llvm.is.fpclass test mask");
5219bdd1243dSDimitry Andric break;
5220bdd1243dSDimitry Andric }
522181ad6265SDimitry Andric case Intrinsic::fptrunc_round: {
522281ad6265SDimitry Andric // Check the rounding mode
522381ad6265SDimitry Andric Metadata *MD = nullptr;
522481ad6265SDimitry Andric auto *MAV = dyn_cast<MetadataAsValue>(Call.getOperand(1));
522581ad6265SDimitry Andric if (MAV)
522681ad6265SDimitry Andric MD = MAV->getMetadata();
522781ad6265SDimitry Andric
522881ad6265SDimitry Andric Check(MD != nullptr, "missing rounding mode argument", Call);
522981ad6265SDimitry Andric
523081ad6265SDimitry Andric Check(isa<MDString>(MD),
523181ad6265SDimitry Andric ("invalid value for llvm.fptrunc.round metadata operand"
523281ad6265SDimitry Andric " (the operand should be a string)"),
523381ad6265SDimitry Andric MD);
523481ad6265SDimitry Andric
5235bdd1243dSDimitry Andric std::optional<RoundingMode> RoundMode =
523681ad6265SDimitry Andric convertStrToRoundingMode(cast<MDString>(MD)->getString());
523781ad6265SDimitry Andric Check(RoundMode && *RoundMode != RoundingMode::Dynamic,
523881ad6265SDimitry Andric "unsupported rounding mode argument", Call);
523981ad6265SDimitry Andric break;
524081ad6265SDimitry Andric }
524181ad6265SDimitry Andric #define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
524281ad6265SDimitry Andric #include "llvm/IR/VPIntrinsics.def"
524381ad6265SDimitry Andric visitVPIntrinsic(cast<VPIntrinsic>(Call));
524481ad6265SDimitry Andric break;
52455ffd83dbSDimitry Andric #define INSTRUCTION(NAME, NARGS, ROUND_MODE, INTRINSIC) \
5246480093f4SDimitry Andric case Intrinsic::INTRINSIC:
5247480093f4SDimitry Andric #include "llvm/IR/ConstrainedOps.def"
52480b57cec5SDimitry Andric visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(Call));
52490b57cec5SDimitry Andric break;
52500b57cec5SDimitry Andric case Intrinsic::dbg_declare: // llvm.dbg.declare
525181ad6265SDimitry Andric Check(isa<MetadataAsValue>(Call.getArgOperand(0)),
52520b57cec5SDimitry Andric "invalid llvm.dbg.declare intrinsic call 1", Call);
52530b57cec5SDimitry Andric visitDbgIntrinsic("declare", cast<DbgVariableIntrinsic>(Call));
52540b57cec5SDimitry Andric break;
52550b57cec5SDimitry Andric case Intrinsic::dbg_value: // llvm.dbg.value
52560b57cec5SDimitry Andric visitDbgIntrinsic("value", cast<DbgVariableIntrinsic>(Call));
52570b57cec5SDimitry Andric break;
5258bdd1243dSDimitry Andric case Intrinsic::dbg_assign: // llvm.dbg.assign
5259bdd1243dSDimitry Andric visitDbgIntrinsic("assign", cast<DbgVariableIntrinsic>(Call));
5260bdd1243dSDimitry Andric break;
52610b57cec5SDimitry Andric case Intrinsic::dbg_label: // llvm.dbg.label
52620b57cec5SDimitry Andric visitDbgLabelIntrinsic("label", cast<DbgLabelInst>(Call));
52630b57cec5SDimitry Andric break;
52640b57cec5SDimitry Andric case Intrinsic::memcpy:
52655ffd83dbSDimitry Andric case Intrinsic::memcpy_inline:
52660b57cec5SDimitry Andric case Intrinsic::memmove:
526781ad6265SDimitry Andric case Intrinsic::memset:
526881ad6265SDimitry Andric case Intrinsic::memset_inline: {
52690b57cec5SDimitry Andric break;
52700b57cec5SDimitry Andric }
52710b57cec5SDimitry Andric case Intrinsic::memcpy_element_unordered_atomic:
52720b57cec5SDimitry Andric case Intrinsic::memmove_element_unordered_atomic:
52730b57cec5SDimitry Andric case Intrinsic::memset_element_unordered_atomic: {
52740b57cec5SDimitry Andric const auto *AMI = cast<AtomicMemIntrinsic>(&Call);
52750b57cec5SDimitry Andric
52760b57cec5SDimitry Andric ConstantInt *ElementSizeCI =
52770b57cec5SDimitry Andric cast<ConstantInt>(AMI->getRawElementSizeInBytes());
52780b57cec5SDimitry Andric const APInt &ElementSizeVal = ElementSizeCI->getValue();
527981ad6265SDimitry Andric Check(ElementSizeVal.isPowerOf2(),
52800b57cec5SDimitry Andric "element size of the element-wise atomic memory intrinsic "
52810b57cec5SDimitry Andric "must be a power of 2",
52820b57cec5SDimitry Andric Call);
52830b57cec5SDimitry Andric
5284bdd1243dSDimitry Andric auto IsValidAlignment = [&](MaybeAlign Alignment) {
5285bdd1243dSDimitry Andric return Alignment && ElementSizeVal.ule(Alignment->value());
52860b57cec5SDimitry Andric };
5287bdd1243dSDimitry Andric Check(IsValidAlignment(AMI->getDestAlign()),
52880b57cec5SDimitry Andric "incorrect alignment of the destination argument", Call);
52890b57cec5SDimitry Andric if (const auto *AMT = dyn_cast<AtomicMemTransferInst>(AMI)) {
5290bdd1243dSDimitry Andric Check(IsValidAlignment(AMT->getSourceAlign()),
52910b57cec5SDimitry Andric "incorrect alignment of the source argument", Call);
52920b57cec5SDimitry Andric }
52930b57cec5SDimitry Andric break;
52940b57cec5SDimitry Andric }
52955ffd83dbSDimitry Andric case Intrinsic::call_preallocated_setup: {
52965ffd83dbSDimitry Andric auto *NumArgs = dyn_cast<ConstantInt>(Call.getArgOperand(0));
529781ad6265SDimitry Andric Check(NumArgs != nullptr,
52985ffd83dbSDimitry Andric "llvm.call.preallocated.setup argument must be a constant");
52995ffd83dbSDimitry Andric bool FoundCall = false;
53005ffd83dbSDimitry Andric for (User *U : Call.users()) {
53015ffd83dbSDimitry Andric auto *UseCall = dyn_cast<CallBase>(U);
530281ad6265SDimitry Andric Check(UseCall != nullptr,
53035ffd83dbSDimitry Andric "Uses of llvm.call.preallocated.setup must be calls");
53045ffd83dbSDimitry Andric const Function *Fn = UseCall->getCalledFunction();
53055ffd83dbSDimitry Andric if (Fn && Fn->getIntrinsicID() == Intrinsic::call_preallocated_arg) {
53065ffd83dbSDimitry Andric auto *AllocArgIndex = dyn_cast<ConstantInt>(UseCall->getArgOperand(1));
530781ad6265SDimitry Andric Check(AllocArgIndex != nullptr,
53085ffd83dbSDimitry Andric "llvm.call.preallocated.alloc arg index must be a constant");
53095ffd83dbSDimitry Andric auto AllocArgIndexInt = AllocArgIndex->getValue();
531081ad6265SDimitry Andric Check(AllocArgIndexInt.sge(0) &&
53115ffd83dbSDimitry Andric AllocArgIndexInt.slt(NumArgs->getValue()),
53125ffd83dbSDimitry Andric "llvm.call.preallocated.alloc arg index must be between 0 and "
53135ffd83dbSDimitry Andric "corresponding "
53145ffd83dbSDimitry Andric "llvm.call.preallocated.setup's argument count");
53155ffd83dbSDimitry Andric } else if (Fn && Fn->getIntrinsicID() ==
53165ffd83dbSDimitry Andric Intrinsic::call_preallocated_teardown) {
53175ffd83dbSDimitry Andric // nothing to do
53185ffd83dbSDimitry Andric } else {
531981ad6265SDimitry Andric Check(!FoundCall, "Can have at most one call corresponding to a "
53205ffd83dbSDimitry Andric "llvm.call.preallocated.setup");
53215ffd83dbSDimitry Andric FoundCall = true;
53225ffd83dbSDimitry Andric size_t NumPreallocatedArgs = 0;
5323349cc55cSDimitry Andric for (unsigned i = 0; i < UseCall->arg_size(); i++) {
53245ffd83dbSDimitry Andric if (UseCall->paramHasAttr(i, Attribute::Preallocated)) {
53255ffd83dbSDimitry Andric ++NumPreallocatedArgs;
53265ffd83dbSDimitry Andric }
53275ffd83dbSDimitry Andric }
532881ad6265SDimitry Andric Check(NumPreallocatedArgs != 0,
53295ffd83dbSDimitry Andric "cannot use preallocated intrinsics on a call without "
53305ffd83dbSDimitry Andric "preallocated arguments");
533181ad6265SDimitry Andric Check(NumArgs->equalsInt(NumPreallocatedArgs),
53325ffd83dbSDimitry Andric "llvm.call.preallocated.setup arg size must be equal to number "
53335ffd83dbSDimitry Andric "of preallocated arguments "
53345ffd83dbSDimitry Andric "at call site",
53355ffd83dbSDimitry Andric Call, *UseCall);
53365ffd83dbSDimitry Andric // getOperandBundle() cannot be called if more than one of the operand
53375ffd83dbSDimitry Andric // bundle exists. There is already a check elsewhere for this, so skip
53385ffd83dbSDimitry Andric // here if we see more than one.
53395ffd83dbSDimitry Andric if (UseCall->countOperandBundlesOfType(LLVMContext::OB_preallocated) >
53405ffd83dbSDimitry Andric 1) {
53415ffd83dbSDimitry Andric return;
53425ffd83dbSDimitry Andric }
53435ffd83dbSDimitry Andric auto PreallocatedBundle =
53445ffd83dbSDimitry Andric UseCall->getOperandBundle(LLVMContext::OB_preallocated);
534581ad6265SDimitry Andric Check(PreallocatedBundle,
53465ffd83dbSDimitry Andric "Use of llvm.call.preallocated.setup outside intrinsics "
53475ffd83dbSDimitry Andric "must be in \"preallocated\" operand bundle");
534881ad6265SDimitry Andric Check(PreallocatedBundle->Inputs.front().get() == &Call,
53495ffd83dbSDimitry Andric "preallocated bundle must have token from corresponding "
53505ffd83dbSDimitry Andric "llvm.call.preallocated.setup");
53515ffd83dbSDimitry Andric }
53525ffd83dbSDimitry Andric }
53535ffd83dbSDimitry Andric break;
53545ffd83dbSDimitry Andric }
53555ffd83dbSDimitry Andric case Intrinsic::call_preallocated_arg: {
53565ffd83dbSDimitry Andric auto *Token = dyn_cast<CallBase>(Call.getArgOperand(0));
535781ad6265SDimitry Andric Check(Token && Token->getCalledFunction()->getIntrinsicID() ==
53585ffd83dbSDimitry Andric Intrinsic::call_preallocated_setup,
53595ffd83dbSDimitry Andric "llvm.call.preallocated.arg token argument must be a "
53605ffd83dbSDimitry Andric "llvm.call.preallocated.setup");
536181ad6265SDimitry Andric Check(Call.hasFnAttr(Attribute::Preallocated),
53625ffd83dbSDimitry Andric "llvm.call.preallocated.arg must be called with a \"preallocated\" "
53635ffd83dbSDimitry Andric "call site attribute");
53645ffd83dbSDimitry Andric break;
53655ffd83dbSDimitry Andric }
53665ffd83dbSDimitry Andric case Intrinsic::call_preallocated_teardown: {
53675ffd83dbSDimitry Andric auto *Token = dyn_cast<CallBase>(Call.getArgOperand(0));
536881ad6265SDimitry Andric Check(Token && Token->getCalledFunction()->getIntrinsicID() ==
53695ffd83dbSDimitry Andric Intrinsic::call_preallocated_setup,
53705ffd83dbSDimitry Andric "llvm.call.preallocated.teardown token argument must be a "
53715ffd83dbSDimitry Andric "llvm.call.preallocated.setup");
53725ffd83dbSDimitry Andric break;
53735ffd83dbSDimitry Andric }
53740b57cec5SDimitry Andric case Intrinsic::gcroot:
53750b57cec5SDimitry Andric case Intrinsic::gcwrite:
53760b57cec5SDimitry Andric case Intrinsic::gcread:
53770b57cec5SDimitry Andric if (ID == Intrinsic::gcroot) {
53780b57cec5SDimitry Andric AllocaInst *AI =
53790b57cec5SDimitry Andric dyn_cast<AllocaInst>(Call.getArgOperand(0)->stripPointerCasts());
538081ad6265SDimitry Andric Check(AI, "llvm.gcroot parameter #1 must be an alloca.", Call);
538181ad6265SDimitry Andric Check(isa<Constant>(Call.getArgOperand(1)),
53820b57cec5SDimitry Andric "llvm.gcroot parameter #2 must be a constant.", Call);
53830b57cec5SDimitry Andric if (!AI->getAllocatedType()->isPointerTy()) {
538481ad6265SDimitry Andric Check(!isa<ConstantPointerNull>(Call.getArgOperand(1)),
53850b57cec5SDimitry Andric "llvm.gcroot parameter #1 must either be a pointer alloca, "
53860b57cec5SDimitry Andric "or argument #2 must be a non-null constant.",
53870b57cec5SDimitry Andric Call);
53880b57cec5SDimitry Andric }
53890b57cec5SDimitry Andric }
53900b57cec5SDimitry Andric
539181ad6265SDimitry Andric Check(Call.getParent()->getParent()->hasGC(),
53920b57cec5SDimitry Andric "Enclosing function does not use GC.", Call);
53930b57cec5SDimitry Andric break;
53940b57cec5SDimitry Andric case Intrinsic::init_trampoline:
539581ad6265SDimitry Andric Check(isa<Function>(Call.getArgOperand(1)->stripPointerCasts()),
53960b57cec5SDimitry Andric "llvm.init_trampoline parameter #2 must resolve to a function.",
53970b57cec5SDimitry Andric Call);
53980b57cec5SDimitry Andric break;
53990b57cec5SDimitry Andric case Intrinsic::prefetch:
5400bdd1243dSDimitry Andric Check(cast<ConstantInt>(Call.getArgOperand(1))->getZExtValue() < 2,
5401bdd1243dSDimitry Andric "rw argument to llvm.prefetch must be 0-1", Call);
5402bdd1243dSDimitry Andric Check(cast<ConstantInt>(Call.getArgOperand(2))->getZExtValue() < 4,
54035f757f3fSDimitry Andric "locality argument to llvm.prefetch must be 0-3", Call);
5404bdd1243dSDimitry Andric Check(cast<ConstantInt>(Call.getArgOperand(3))->getZExtValue() < 2,
5405bdd1243dSDimitry Andric "cache type argument to llvm.prefetch must be 0-1", Call);
54060b57cec5SDimitry Andric break;
54070b57cec5SDimitry Andric case Intrinsic::stackprotector:
540881ad6265SDimitry Andric Check(isa<AllocaInst>(Call.getArgOperand(1)->stripPointerCasts()),
54090b57cec5SDimitry Andric "llvm.stackprotector parameter #2 must resolve to an alloca.", Call);
54100b57cec5SDimitry Andric break;
54110b57cec5SDimitry Andric case Intrinsic::localescape: {
54120b57cec5SDimitry Andric BasicBlock *BB = Call.getParent();
5413bdd1243dSDimitry Andric Check(BB->isEntryBlock(), "llvm.localescape used outside of entry block",
5414bdd1243dSDimitry Andric Call);
541581ad6265SDimitry Andric Check(!SawFrameEscape, "multiple calls to llvm.localescape in one function",
541681ad6265SDimitry Andric Call);
54170b57cec5SDimitry Andric for (Value *Arg : Call.args()) {
54180b57cec5SDimitry Andric if (isa<ConstantPointerNull>(Arg))
54190b57cec5SDimitry Andric continue; // Null values are allowed as placeholders.
54200b57cec5SDimitry Andric auto *AI = dyn_cast<AllocaInst>(Arg->stripPointerCasts());
542181ad6265SDimitry Andric Check(AI && AI->isStaticAlloca(),
54220b57cec5SDimitry Andric "llvm.localescape only accepts static allocas", Call);
54230b57cec5SDimitry Andric }
5424349cc55cSDimitry Andric FrameEscapeInfo[BB->getParent()].first = Call.arg_size();
54250b57cec5SDimitry Andric SawFrameEscape = true;
54260b57cec5SDimitry Andric break;
54270b57cec5SDimitry Andric }
54280b57cec5SDimitry Andric case Intrinsic::localrecover: {
54290b57cec5SDimitry Andric Value *FnArg = Call.getArgOperand(0)->stripPointerCasts();
54300b57cec5SDimitry Andric Function *Fn = dyn_cast<Function>(FnArg);
543181ad6265SDimitry Andric Check(Fn && !Fn->isDeclaration(),
54320b57cec5SDimitry Andric "llvm.localrecover first "
54330b57cec5SDimitry Andric "argument must be function defined in this module",
54340b57cec5SDimitry Andric Call);
54350b57cec5SDimitry Andric auto *IdxArg = cast<ConstantInt>(Call.getArgOperand(2));
54360b57cec5SDimitry Andric auto &Entry = FrameEscapeInfo[Fn];
54370b57cec5SDimitry Andric Entry.second = unsigned(
54380b57cec5SDimitry Andric std::max(uint64_t(Entry.second), IdxArg->getLimitedValue(~0U) + 1));
54390b57cec5SDimitry Andric break;
54400b57cec5SDimitry Andric }
54410b57cec5SDimitry Andric
54420b57cec5SDimitry Andric case Intrinsic::experimental_gc_statepoint:
54430b57cec5SDimitry Andric if (auto *CI = dyn_cast<CallInst>(&Call))
544481ad6265SDimitry Andric Check(!CI->isInlineAsm(),
54450b57cec5SDimitry Andric "gc.statepoint support for inline assembly unimplemented", CI);
544681ad6265SDimitry Andric Check(Call.getParent()->getParent()->hasGC(),
54470b57cec5SDimitry Andric "Enclosing function does not use GC.", Call);
54480b57cec5SDimitry Andric
54490b57cec5SDimitry Andric verifyStatepoint(Call);
54500b57cec5SDimitry Andric break;
54510b57cec5SDimitry Andric case Intrinsic::experimental_gc_result: {
545281ad6265SDimitry Andric Check(Call.getParent()->getParent()->hasGC(),
54530b57cec5SDimitry Andric "Enclosing function does not use GC.", Call);
5454bdd1243dSDimitry Andric
5455bdd1243dSDimitry Andric auto *Statepoint = Call.getArgOperand(0);
5456bdd1243dSDimitry Andric if (isa<UndefValue>(Statepoint))
5457bdd1243dSDimitry Andric break;
5458bdd1243dSDimitry Andric
54590b57cec5SDimitry Andric // Are we tied to a statepoint properly?
5460bdd1243dSDimitry Andric const auto *StatepointCall = dyn_cast<CallBase>(Statepoint);
54610b57cec5SDimitry Andric const Function *StatepointFn =
54620b57cec5SDimitry Andric StatepointCall ? StatepointCall->getCalledFunction() : nullptr;
546381ad6265SDimitry Andric Check(StatepointFn && StatepointFn->isDeclaration() &&
54640b57cec5SDimitry Andric StatepointFn->getIntrinsicID() ==
54650b57cec5SDimitry Andric Intrinsic::experimental_gc_statepoint,
54660b57cec5SDimitry Andric "gc.result operand #1 must be from a statepoint", Call,
54670b57cec5SDimitry Andric Call.getArgOperand(0));
54680b57cec5SDimitry Andric
546981ad6265SDimitry Andric // Check that result type matches wrapped callee.
547081ad6265SDimitry Andric auto *TargetFuncType =
547181ad6265SDimitry Andric cast<FunctionType>(StatepointCall->getParamElementType(2));
547281ad6265SDimitry Andric Check(Call.getType() == TargetFuncType->getReturnType(),
54730b57cec5SDimitry Andric "gc.result result type does not match wrapped callee", Call);
54740b57cec5SDimitry Andric break;
54750b57cec5SDimitry Andric }
54760b57cec5SDimitry Andric case Intrinsic::experimental_gc_relocate: {
547781ad6265SDimitry Andric Check(Call.arg_size() == 3, "wrong number of arguments", Call);
54780b57cec5SDimitry Andric
547981ad6265SDimitry Andric Check(isa<PointerType>(Call.getType()->getScalarType()),
54800b57cec5SDimitry Andric "gc.relocate must return a pointer or a vector of pointers", Call);
54810b57cec5SDimitry Andric
54820b57cec5SDimitry Andric // Check that this relocate is correctly tied to the statepoint
54830b57cec5SDimitry Andric
54840b57cec5SDimitry Andric // This is case for relocate on the unwinding path of an invoke statepoint
54850b57cec5SDimitry Andric if (LandingPadInst *LandingPad =
54860b57cec5SDimitry Andric dyn_cast<LandingPadInst>(Call.getArgOperand(0))) {
54870b57cec5SDimitry Andric
54880b57cec5SDimitry Andric const BasicBlock *InvokeBB =
54890b57cec5SDimitry Andric LandingPad->getParent()->getUniquePredecessor();
54900b57cec5SDimitry Andric
54910b57cec5SDimitry Andric // Landingpad relocates should have only one predecessor with invoke
54920b57cec5SDimitry Andric // statepoint terminator
549381ad6265SDimitry Andric Check(InvokeBB, "safepoints should have unique landingpads",
54940b57cec5SDimitry Andric LandingPad->getParent());
549581ad6265SDimitry Andric Check(InvokeBB->getTerminator(), "safepoint block should be well formed",
54960b57cec5SDimitry Andric InvokeBB);
549781ad6265SDimitry Andric Check(isa<GCStatepointInst>(InvokeBB->getTerminator()),
54980b57cec5SDimitry Andric "gc relocate should be linked to a statepoint", InvokeBB);
54990b57cec5SDimitry Andric } else {
55000b57cec5SDimitry Andric // In all other cases relocate should be tied to the statepoint directly.
55010b57cec5SDimitry Andric // This covers relocates on a normal return path of invoke statepoint and
55020b57cec5SDimitry Andric // relocates of a call statepoint.
5503fcaf7f86SDimitry Andric auto *Token = Call.getArgOperand(0);
5504fcaf7f86SDimitry Andric Check(isa<GCStatepointInst>(Token) || isa<UndefValue>(Token),
55050b57cec5SDimitry Andric "gc relocate is incorrectly tied to the statepoint", Call, Token);
55060b57cec5SDimitry Andric }
55070b57cec5SDimitry Andric
55080b57cec5SDimitry Andric // Verify rest of the relocate arguments.
5509fcaf7f86SDimitry Andric const Value &StatepointCall = *cast<GCRelocateInst>(Call).getStatepoint();
55100b57cec5SDimitry Andric
55110b57cec5SDimitry Andric // Both the base and derived must be piped through the safepoint.
55120b57cec5SDimitry Andric Value *Base = Call.getArgOperand(1);
551381ad6265SDimitry Andric Check(isa<ConstantInt>(Base),
55140b57cec5SDimitry Andric "gc.relocate operand #2 must be integer offset", Call);
55150b57cec5SDimitry Andric
55160b57cec5SDimitry Andric Value *Derived = Call.getArgOperand(2);
551781ad6265SDimitry Andric Check(isa<ConstantInt>(Derived),
55180b57cec5SDimitry Andric "gc.relocate operand #3 must be integer offset", Call);
55190b57cec5SDimitry Andric
55205ffd83dbSDimitry Andric const uint64_t BaseIndex = cast<ConstantInt>(Base)->getZExtValue();
55215ffd83dbSDimitry Andric const uint64_t DerivedIndex = cast<ConstantInt>(Derived)->getZExtValue();
55225ffd83dbSDimitry Andric
55230b57cec5SDimitry Andric // Check the bounds
5524fcaf7f86SDimitry Andric if (isa<UndefValue>(StatepointCall))
5525fcaf7f86SDimitry Andric break;
5526fcaf7f86SDimitry Andric if (auto Opt = cast<GCStatepointInst>(StatepointCall)
5527fcaf7f86SDimitry Andric .getOperandBundle(LLVMContext::OB_gc_live)) {
552881ad6265SDimitry Andric Check(BaseIndex < Opt->Inputs.size(),
55290b57cec5SDimitry Andric "gc.relocate: statepoint base index out of bounds", Call);
553081ad6265SDimitry Andric Check(DerivedIndex < Opt->Inputs.size(),
55315ffd83dbSDimitry Andric "gc.relocate: statepoint derived index out of bounds", Call);
55325ffd83dbSDimitry Andric }
55330b57cec5SDimitry Andric
55340b57cec5SDimitry Andric // Relocated value must be either a pointer type or vector-of-pointer type,
55350b57cec5SDimitry Andric // but gc_relocate does not need to return the same pointer type as the
55360b57cec5SDimitry Andric // relocated pointer. It can be casted to the correct type later if it's
55370b57cec5SDimitry Andric // desired. However, they must have the same address space and 'vectorness'
55380b57cec5SDimitry Andric GCRelocateInst &Relocate = cast<GCRelocateInst>(Call);
5539bdd1243dSDimitry Andric auto *ResultType = Call.getType();
5540bdd1243dSDimitry Andric auto *DerivedType = Relocate.getDerivedPtr()->getType();
5541bdd1243dSDimitry Andric auto *BaseType = Relocate.getBasePtr()->getType();
55420b57cec5SDimitry Andric
5543bdd1243dSDimitry Andric Check(BaseType->isPtrOrPtrVectorTy(),
5544bdd1243dSDimitry Andric "gc.relocate: relocated value must be a pointer", Call);
5545bdd1243dSDimitry Andric Check(DerivedType->isPtrOrPtrVectorTy(),
5546bdd1243dSDimitry Andric "gc.relocate: relocated value must be a pointer", Call);
5547bdd1243dSDimitry Andric
554881ad6265SDimitry Andric Check(ResultType->isVectorTy() == DerivedType->isVectorTy(),
55490b57cec5SDimitry Andric "gc.relocate: vector relocates to vector and pointer to pointer",
55500b57cec5SDimitry Andric Call);
555181ad6265SDimitry Andric Check(
55520b57cec5SDimitry Andric ResultType->getPointerAddressSpace() ==
55530b57cec5SDimitry Andric DerivedType->getPointerAddressSpace(),
55540b57cec5SDimitry Andric "gc.relocate: relocating a pointer shouldn't change its address space",
55550b57cec5SDimitry Andric Call);
5556bdd1243dSDimitry Andric
5557bdd1243dSDimitry Andric auto GC = llvm::getGCStrategy(Relocate.getFunction()->getGC());
5558bdd1243dSDimitry Andric Check(GC, "gc.relocate: calling function must have GCStrategy",
5559bdd1243dSDimitry Andric Call.getFunction());
5560bdd1243dSDimitry Andric if (GC) {
5561bdd1243dSDimitry Andric auto isGCPtr = [&GC](Type *PTy) {
5562bdd1243dSDimitry Andric return GC->isGCManagedPointer(PTy->getScalarType()).value_or(true);
5563bdd1243dSDimitry Andric };
5564bdd1243dSDimitry Andric Check(isGCPtr(ResultType), "gc.relocate: must return gc pointer", Call);
5565bdd1243dSDimitry Andric Check(isGCPtr(BaseType),
5566bdd1243dSDimitry Andric "gc.relocate: relocated value must be a gc pointer", Call);
5567bdd1243dSDimitry Andric Check(isGCPtr(DerivedType),
5568bdd1243dSDimitry Andric "gc.relocate: relocated value must be a gc pointer", Call);
5569bdd1243dSDimitry Andric }
55700b57cec5SDimitry Andric break;
55710b57cec5SDimitry Andric }
55720b57cec5SDimitry Andric case Intrinsic::eh_exceptioncode:
55730b57cec5SDimitry Andric case Intrinsic::eh_exceptionpointer: {
557481ad6265SDimitry Andric Check(isa<CatchPadInst>(Call.getArgOperand(0)),
55750b57cec5SDimitry Andric "eh.exceptionpointer argument must be a catchpad", Call);
55760b57cec5SDimitry Andric break;
55770b57cec5SDimitry Andric }
55785ffd83dbSDimitry Andric case Intrinsic::get_active_lane_mask: {
557981ad6265SDimitry Andric Check(Call.getType()->isVectorTy(),
558081ad6265SDimitry Andric "get_active_lane_mask: must return a "
558181ad6265SDimitry Andric "vector",
558281ad6265SDimitry Andric Call);
55835ffd83dbSDimitry Andric auto *ElemTy = Call.getType()->getScalarType();
558481ad6265SDimitry Andric Check(ElemTy->isIntegerTy(1),
558581ad6265SDimitry Andric "get_active_lane_mask: element type is not "
558681ad6265SDimitry Andric "i1",
558781ad6265SDimitry Andric Call);
55885ffd83dbSDimitry Andric break;
55895ffd83dbSDimitry Andric }
559006c3fb27SDimitry Andric case Intrinsic::experimental_get_vector_length: {
559106c3fb27SDimitry Andric ConstantInt *VF = cast<ConstantInt>(Call.getArgOperand(1));
559206c3fb27SDimitry Andric Check(!VF->isNegative() && !VF->isZero(),
559306c3fb27SDimitry Andric "get_vector_length: VF must be positive", Call);
559406c3fb27SDimitry Andric break;
559506c3fb27SDimitry Andric }
55960b57cec5SDimitry Andric case Intrinsic::masked_load: {
559781ad6265SDimitry Andric Check(Call.getType()->isVectorTy(), "masked_load: must return a vector",
55980b57cec5SDimitry Andric Call);
55990b57cec5SDimitry Andric
56000b57cec5SDimitry Andric ConstantInt *Alignment = cast<ConstantInt>(Call.getArgOperand(1));
56010b57cec5SDimitry Andric Value *Mask = Call.getArgOperand(2);
56020b57cec5SDimitry Andric Value *PassThru = Call.getArgOperand(3);
560381ad6265SDimitry Andric Check(Mask->getType()->isVectorTy(), "masked_load: mask must be vector",
56040b57cec5SDimitry Andric Call);
560581ad6265SDimitry Andric Check(Alignment->getValue().isPowerOf2(),
56060b57cec5SDimitry Andric "masked_load: alignment must be a power of 2", Call);
560781ad6265SDimitry Andric Check(PassThru->getType() == Call.getType(),
5608fe6060f1SDimitry Andric "masked_load: pass through and return type must match", Call);
560981ad6265SDimitry Andric Check(cast<VectorType>(Mask->getType())->getElementCount() ==
5610fe6060f1SDimitry Andric cast<VectorType>(Call.getType())->getElementCount(),
5611fe6060f1SDimitry Andric "masked_load: vector mask must be same length as return", Call);
56120b57cec5SDimitry Andric break;
56130b57cec5SDimitry Andric }
56140b57cec5SDimitry Andric case Intrinsic::masked_store: {
56150b57cec5SDimitry Andric Value *Val = Call.getArgOperand(0);
56160b57cec5SDimitry Andric ConstantInt *Alignment = cast<ConstantInt>(Call.getArgOperand(2));
56170b57cec5SDimitry Andric Value *Mask = Call.getArgOperand(3);
561881ad6265SDimitry Andric Check(Mask->getType()->isVectorTy(), "masked_store: mask must be vector",
56190b57cec5SDimitry Andric Call);
562081ad6265SDimitry Andric Check(Alignment->getValue().isPowerOf2(),
56210b57cec5SDimitry Andric "masked_store: alignment must be a power of 2", Call);
562281ad6265SDimitry Andric Check(cast<VectorType>(Mask->getType())->getElementCount() ==
5623fe6060f1SDimitry Andric cast<VectorType>(Val->getType())->getElementCount(),
5624fe6060f1SDimitry Andric "masked_store: vector mask must be same length as value", Call);
56250b57cec5SDimitry Andric break;
56260b57cec5SDimitry Andric }
56270b57cec5SDimitry Andric
56285ffd83dbSDimitry Andric case Intrinsic::masked_gather: {
56295ffd83dbSDimitry Andric const APInt &Alignment =
56305ffd83dbSDimitry Andric cast<ConstantInt>(Call.getArgOperand(1))->getValue();
563181ad6265SDimitry Andric Check(Alignment.isZero() || Alignment.isPowerOf2(),
56325ffd83dbSDimitry Andric "masked_gather: alignment must be 0 or a power of 2", Call);
56335ffd83dbSDimitry Andric break;
56345ffd83dbSDimitry Andric }
56355ffd83dbSDimitry Andric case Intrinsic::masked_scatter: {
56365ffd83dbSDimitry Andric const APInt &Alignment =
56375ffd83dbSDimitry Andric cast<ConstantInt>(Call.getArgOperand(2))->getValue();
563881ad6265SDimitry Andric Check(Alignment.isZero() || Alignment.isPowerOf2(),
56395ffd83dbSDimitry Andric "masked_scatter: alignment must be 0 or a power of 2", Call);
56405ffd83dbSDimitry Andric break;
56415ffd83dbSDimitry Andric }
56425ffd83dbSDimitry Andric
56430b57cec5SDimitry Andric case Intrinsic::experimental_guard: {
564481ad6265SDimitry Andric Check(isa<CallInst>(Call), "experimental_guard cannot be invoked", Call);
564581ad6265SDimitry Andric Check(Call.countOperandBundlesOfType(LLVMContext::OB_deopt) == 1,
56460b57cec5SDimitry Andric "experimental_guard must have exactly one "
56470b57cec5SDimitry Andric "\"deopt\" operand bundle");
56480b57cec5SDimitry Andric break;
56490b57cec5SDimitry Andric }
56500b57cec5SDimitry Andric
56510b57cec5SDimitry Andric case Intrinsic::experimental_deoptimize: {
565281ad6265SDimitry Andric Check(isa<CallInst>(Call), "experimental_deoptimize cannot be invoked",
56530b57cec5SDimitry Andric Call);
565481ad6265SDimitry Andric Check(Call.countOperandBundlesOfType(LLVMContext::OB_deopt) == 1,
56550b57cec5SDimitry Andric "experimental_deoptimize must have exactly one "
56560b57cec5SDimitry Andric "\"deopt\" operand bundle");
565781ad6265SDimitry Andric Check(Call.getType() == Call.getFunction()->getReturnType(),
56580b57cec5SDimitry Andric "experimental_deoptimize return type must match caller return type");
56590b57cec5SDimitry Andric
56600b57cec5SDimitry Andric if (isa<CallInst>(Call)) {
56610b57cec5SDimitry Andric auto *RI = dyn_cast<ReturnInst>(Call.getNextNode());
566281ad6265SDimitry Andric Check(RI,
56630b57cec5SDimitry Andric "calls to experimental_deoptimize must be followed by a return");
56640b57cec5SDimitry Andric
56650b57cec5SDimitry Andric if (!Call.getType()->isVoidTy() && RI)
566681ad6265SDimitry Andric Check(RI->getReturnValue() == &Call,
56670b57cec5SDimitry Andric "calls to experimental_deoptimize must be followed by a return "
56680b57cec5SDimitry Andric "of the value computed by experimental_deoptimize");
56690b57cec5SDimitry Andric }
56700b57cec5SDimitry Andric
56710b57cec5SDimitry Andric break;
56720b57cec5SDimitry Andric }
5673fe6060f1SDimitry Andric case Intrinsic::vector_reduce_and:
5674fe6060f1SDimitry Andric case Intrinsic::vector_reduce_or:
5675fe6060f1SDimitry Andric case Intrinsic::vector_reduce_xor:
5676fe6060f1SDimitry Andric case Intrinsic::vector_reduce_add:
5677fe6060f1SDimitry Andric case Intrinsic::vector_reduce_mul:
5678fe6060f1SDimitry Andric case Intrinsic::vector_reduce_smax:
5679fe6060f1SDimitry Andric case Intrinsic::vector_reduce_smin:
5680fe6060f1SDimitry Andric case Intrinsic::vector_reduce_umax:
5681fe6060f1SDimitry Andric case Intrinsic::vector_reduce_umin: {
5682fe6060f1SDimitry Andric Type *ArgTy = Call.getArgOperand(0)->getType();
568381ad6265SDimitry Andric Check(ArgTy->isIntOrIntVectorTy() && ArgTy->isVectorTy(),
5684fe6060f1SDimitry Andric "Intrinsic has incorrect argument type!");
5685fe6060f1SDimitry Andric break;
5686fe6060f1SDimitry Andric }
5687fe6060f1SDimitry Andric case Intrinsic::vector_reduce_fmax:
5688fe6060f1SDimitry Andric case Intrinsic::vector_reduce_fmin: {
5689fe6060f1SDimitry Andric Type *ArgTy = Call.getArgOperand(0)->getType();
569081ad6265SDimitry Andric Check(ArgTy->isFPOrFPVectorTy() && ArgTy->isVectorTy(),
5691fe6060f1SDimitry Andric "Intrinsic has incorrect argument type!");
5692fe6060f1SDimitry Andric break;
5693fe6060f1SDimitry Andric }
5694fe6060f1SDimitry Andric case Intrinsic::vector_reduce_fadd:
5695fe6060f1SDimitry Andric case Intrinsic::vector_reduce_fmul: {
5696fe6060f1SDimitry Andric // Unlike the other reductions, the first argument is a start value. The
5697fe6060f1SDimitry Andric // second argument is the vector to be reduced.
5698fe6060f1SDimitry Andric Type *ArgTy = Call.getArgOperand(1)->getType();
569981ad6265SDimitry Andric Check(ArgTy->isFPOrFPVectorTy() && ArgTy->isVectorTy(),
5700fe6060f1SDimitry Andric "Intrinsic has incorrect argument type!");
57010b57cec5SDimitry Andric break;
57020b57cec5SDimitry Andric }
57030b57cec5SDimitry Andric case Intrinsic::smul_fix:
57040b57cec5SDimitry Andric case Intrinsic::smul_fix_sat:
57058bcb0991SDimitry Andric case Intrinsic::umul_fix:
5706480093f4SDimitry Andric case Intrinsic::umul_fix_sat:
5707480093f4SDimitry Andric case Intrinsic::sdiv_fix:
57085ffd83dbSDimitry Andric case Intrinsic::sdiv_fix_sat:
57095ffd83dbSDimitry Andric case Intrinsic::udiv_fix:
57105ffd83dbSDimitry Andric case Intrinsic::udiv_fix_sat: {
57110b57cec5SDimitry Andric Value *Op1 = Call.getArgOperand(0);
57120b57cec5SDimitry Andric Value *Op2 = Call.getArgOperand(1);
571381ad6265SDimitry Andric Check(Op1->getType()->isIntOrIntVectorTy(),
5714480093f4SDimitry Andric "first operand of [us][mul|div]_fix[_sat] must be an int type or "
5715480093f4SDimitry Andric "vector of ints");
571681ad6265SDimitry Andric Check(Op2->getType()->isIntOrIntVectorTy(),
5717480093f4SDimitry Andric "second operand of [us][mul|div]_fix[_sat] must be an int type or "
5718480093f4SDimitry Andric "vector of ints");
57190b57cec5SDimitry Andric
57200b57cec5SDimitry Andric auto *Op3 = cast<ConstantInt>(Call.getArgOperand(2));
5721cb14a3feSDimitry Andric Check(Op3->getType()->isIntegerTy(),
5722cb14a3feSDimitry Andric "third operand of [us][mul|div]_fix[_sat] must be an int type");
5723cb14a3feSDimitry Andric Check(Op3->getBitWidth() <= 32,
5724cb14a3feSDimitry Andric "third operand of [us][mul|div]_fix[_sat] must fit within 32 bits");
57250b57cec5SDimitry Andric
5726480093f4SDimitry Andric if (ID == Intrinsic::smul_fix || ID == Intrinsic::smul_fix_sat ||
57275ffd83dbSDimitry Andric ID == Intrinsic::sdiv_fix || ID == Intrinsic::sdiv_fix_sat) {
572881ad6265SDimitry Andric Check(Op3->getZExtValue() < Op1->getType()->getScalarSizeInBits(),
5729480093f4SDimitry Andric "the scale of s[mul|div]_fix[_sat] must be less than the width of "
5730480093f4SDimitry Andric "the operands");
57310b57cec5SDimitry Andric } else {
573281ad6265SDimitry Andric Check(Op3->getZExtValue() <= Op1->getType()->getScalarSizeInBits(),
5733480093f4SDimitry Andric "the scale of u[mul|div]_fix[_sat] must be less than or equal "
5734480093f4SDimitry Andric "to the width of the operands");
57350b57cec5SDimitry Andric }
57360b57cec5SDimitry Andric break;
57370b57cec5SDimitry Andric }
57380b57cec5SDimitry Andric case Intrinsic::lrint:
57390b57cec5SDimitry Andric case Intrinsic::llrint: {
57400b57cec5SDimitry Andric Type *ValTy = Call.getArgOperand(0)->getType();
57410b57cec5SDimitry Andric Type *ResultTy = Call.getType();
57425f757f3fSDimitry Andric Check(
57435f757f3fSDimitry Andric ValTy->isFPOrFPVectorTy() && ResultTy->isIntOrIntVectorTy(),
57445f757f3fSDimitry Andric "llvm.lrint, llvm.llrint: argument must be floating-point or vector "
57455f757f3fSDimitry Andric "of floating-points, and result must be integer or vector of integers",
57465f757f3fSDimitry Andric &Call);
57475f757f3fSDimitry Andric Check(ValTy->isVectorTy() == ResultTy->isVectorTy(),
57485f757f3fSDimitry Andric "llvm.lrint, llvm.llrint: argument and result disagree on vector use",
57495f757f3fSDimitry Andric &Call);
57505f757f3fSDimitry Andric if (ValTy->isVectorTy()) {
57515f757f3fSDimitry Andric Check(cast<VectorType>(ValTy)->getElementCount() ==
57525f757f3fSDimitry Andric cast<VectorType>(ResultTy)->getElementCount(),
57535f757f3fSDimitry Andric "llvm.lrint, llvm.llrint: argument must be same length as result",
57545f757f3fSDimitry Andric &Call);
57555f757f3fSDimitry Andric }
57565f757f3fSDimitry Andric break;
57575f757f3fSDimitry Andric }
57585f757f3fSDimitry Andric case Intrinsic::lround:
57595f757f3fSDimitry Andric case Intrinsic::llround: {
57605f757f3fSDimitry Andric Type *ValTy = Call.getArgOperand(0)->getType();
57615f757f3fSDimitry Andric Type *ResultTy = Call.getType();
576281ad6265SDimitry Andric Check(!ValTy->isVectorTy() && !ResultTy->isVectorTy(),
57630b57cec5SDimitry Andric "Intrinsic does not support vectors", &Call);
57640b57cec5SDimitry Andric break;
57650b57cec5SDimitry Andric }
57665ffd83dbSDimitry Andric case Intrinsic::bswap: {
57675ffd83dbSDimitry Andric Type *Ty = Call.getType();
57685ffd83dbSDimitry Andric unsigned Size = Ty->getScalarSizeInBits();
576981ad6265SDimitry Andric Check(Size % 16 == 0, "bswap must be an even number of bytes", &Call);
57705ffd83dbSDimitry Andric break;
57715ffd83dbSDimitry Andric }
5772e8d8bef9SDimitry Andric case Intrinsic::invariant_start: {
5773e8d8bef9SDimitry Andric ConstantInt *InvariantSize = dyn_cast<ConstantInt>(Call.getArgOperand(0));
577481ad6265SDimitry Andric Check(InvariantSize &&
5775e8d8bef9SDimitry Andric (!InvariantSize->isNegative() || InvariantSize->isMinusOne()),
5776e8d8bef9SDimitry Andric "invariant_start parameter must be -1, 0 or a positive number",
5777e8d8bef9SDimitry Andric &Call);
5778e8d8bef9SDimitry Andric break;
5779e8d8bef9SDimitry Andric }
57805ffd83dbSDimitry Andric case Intrinsic::matrix_multiply:
57815ffd83dbSDimitry Andric case Intrinsic::matrix_transpose:
57825ffd83dbSDimitry Andric case Intrinsic::matrix_column_major_load:
57835ffd83dbSDimitry Andric case Intrinsic::matrix_column_major_store: {
57845ffd83dbSDimitry Andric Function *IF = Call.getCalledFunction();
57855ffd83dbSDimitry Andric ConstantInt *Stride = nullptr;
57865ffd83dbSDimitry Andric ConstantInt *NumRows;
57875ffd83dbSDimitry Andric ConstantInt *NumColumns;
57885ffd83dbSDimitry Andric VectorType *ResultTy;
57895ffd83dbSDimitry Andric Type *Op0ElemTy = nullptr;
57905ffd83dbSDimitry Andric Type *Op1ElemTy = nullptr;
57915ffd83dbSDimitry Andric switch (ID) {
579206c3fb27SDimitry Andric case Intrinsic::matrix_multiply: {
57935ffd83dbSDimitry Andric NumRows = cast<ConstantInt>(Call.getArgOperand(2));
579406c3fb27SDimitry Andric ConstantInt *N = cast<ConstantInt>(Call.getArgOperand(3));
57955ffd83dbSDimitry Andric NumColumns = cast<ConstantInt>(Call.getArgOperand(4));
579606c3fb27SDimitry Andric Check(cast<FixedVectorType>(Call.getArgOperand(0)->getType())
579706c3fb27SDimitry Andric ->getNumElements() ==
579806c3fb27SDimitry Andric NumRows->getZExtValue() * N->getZExtValue(),
579906c3fb27SDimitry Andric "First argument of a matrix operation does not match specified "
580006c3fb27SDimitry Andric "shape!");
580106c3fb27SDimitry Andric Check(cast<FixedVectorType>(Call.getArgOperand(1)->getType())
580206c3fb27SDimitry Andric ->getNumElements() ==
580306c3fb27SDimitry Andric N->getZExtValue() * NumColumns->getZExtValue(),
580406c3fb27SDimitry Andric "Second argument of a matrix operation does not match specified "
580506c3fb27SDimitry Andric "shape!");
580606c3fb27SDimitry Andric
58075ffd83dbSDimitry Andric ResultTy = cast<VectorType>(Call.getType());
58085ffd83dbSDimitry Andric Op0ElemTy =
58095ffd83dbSDimitry Andric cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType();
58105ffd83dbSDimitry Andric Op1ElemTy =
58115ffd83dbSDimitry Andric cast<VectorType>(Call.getArgOperand(1)->getType())->getElementType();
58125ffd83dbSDimitry Andric break;
581306c3fb27SDimitry Andric }
58145ffd83dbSDimitry Andric case Intrinsic::matrix_transpose:
58155ffd83dbSDimitry Andric NumRows = cast<ConstantInt>(Call.getArgOperand(1));
58165ffd83dbSDimitry Andric NumColumns = cast<ConstantInt>(Call.getArgOperand(2));
58175ffd83dbSDimitry Andric ResultTy = cast<VectorType>(Call.getType());
58185ffd83dbSDimitry Andric Op0ElemTy =
58195ffd83dbSDimitry Andric cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType();
58205ffd83dbSDimitry Andric break;
58214824e7fdSDimitry Andric case Intrinsic::matrix_column_major_load: {
58225ffd83dbSDimitry Andric Stride = dyn_cast<ConstantInt>(Call.getArgOperand(1));
58235ffd83dbSDimitry Andric NumRows = cast<ConstantInt>(Call.getArgOperand(3));
58245ffd83dbSDimitry Andric NumColumns = cast<ConstantInt>(Call.getArgOperand(4));
58255ffd83dbSDimitry Andric ResultTy = cast<VectorType>(Call.getType());
58265ffd83dbSDimitry Andric break;
58274824e7fdSDimitry Andric }
58284824e7fdSDimitry Andric case Intrinsic::matrix_column_major_store: {
58295ffd83dbSDimitry Andric Stride = dyn_cast<ConstantInt>(Call.getArgOperand(2));
58305ffd83dbSDimitry Andric NumRows = cast<ConstantInt>(Call.getArgOperand(4));
58315ffd83dbSDimitry Andric NumColumns = cast<ConstantInt>(Call.getArgOperand(5));
58325ffd83dbSDimitry Andric ResultTy = cast<VectorType>(Call.getArgOperand(0)->getType());
58335ffd83dbSDimitry Andric Op0ElemTy =
58345ffd83dbSDimitry Andric cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType();
58355ffd83dbSDimitry Andric break;
58364824e7fdSDimitry Andric }
58375ffd83dbSDimitry Andric default:
58385ffd83dbSDimitry Andric llvm_unreachable("unexpected intrinsic");
58395ffd83dbSDimitry Andric }
58405ffd83dbSDimitry Andric
584181ad6265SDimitry Andric Check(ResultTy->getElementType()->isIntegerTy() ||
58425ffd83dbSDimitry Andric ResultTy->getElementType()->isFloatingPointTy(),
58435ffd83dbSDimitry Andric "Result type must be an integer or floating-point type!", IF);
58445ffd83dbSDimitry Andric
58454824e7fdSDimitry Andric if (Op0ElemTy)
584681ad6265SDimitry Andric Check(ResultTy->getElementType() == Op0ElemTy,
58475ffd83dbSDimitry Andric "Vector element type mismatch of the result and first operand "
584881ad6265SDimitry Andric "vector!",
584981ad6265SDimitry Andric IF);
58505ffd83dbSDimitry Andric
58515ffd83dbSDimitry Andric if (Op1ElemTy)
585281ad6265SDimitry Andric Check(ResultTy->getElementType() == Op1ElemTy,
58535ffd83dbSDimitry Andric "Vector element type mismatch of the result and second operand "
585481ad6265SDimitry Andric "vector!",
585581ad6265SDimitry Andric IF);
58565ffd83dbSDimitry Andric
585781ad6265SDimitry Andric Check(cast<FixedVectorType>(ResultTy)->getNumElements() ==
58585ffd83dbSDimitry Andric NumRows->getZExtValue() * NumColumns->getZExtValue(),
58595ffd83dbSDimitry Andric "Result of a matrix operation does not fit in the returned vector!");
58605ffd83dbSDimitry Andric
58615ffd83dbSDimitry Andric if (Stride)
586281ad6265SDimitry Andric Check(Stride->getZExtValue() >= NumRows->getZExtValue(),
58635ffd83dbSDimitry Andric "Stride must be greater or equal than the number of rows!", IF);
58645ffd83dbSDimitry Andric
58655ffd83dbSDimitry Andric break;
58665ffd83dbSDimitry Andric }
586704eeddc0SDimitry Andric case Intrinsic::experimental_vector_splice: {
586804eeddc0SDimitry Andric VectorType *VecTy = cast<VectorType>(Call.getType());
586904eeddc0SDimitry Andric int64_t Idx = cast<ConstantInt>(Call.getArgOperand(2))->getSExtValue();
587004eeddc0SDimitry Andric int64_t KnownMinNumElements = VecTy->getElementCount().getKnownMinValue();
587104eeddc0SDimitry Andric if (Call.getParent() && Call.getParent()->getParent()) {
587204eeddc0SDimitry Andric AttributeList Attrs = Call.getParent()->getParent()->getAttributes();
587304eeddc0SDimitry Andric if (Attrs.hasFnAttr(Attribute::VScaleRange))
587404eeddc0SDimitry Andric KnownMinNumElements *= Attrs.getFnAttrs().getVScaleRangeMin();
587504eeddc0SDimitry Andric }
587681ad6265SDimitry Andric Check((Idx < 0 && std::abs(Idx) <= KnownMinNumElements) ||
587704eeddc0SDimitry Andric (Idx >= 0 && Idx < KnownMinNumElements),
587804eeddc0SDimitry Andric "The splice index exceeds the range [-VL, VL-1] where VL is the "
587904eeddc0SDimitry Andric "known minimum number of elements in the vector. For scalable "
588004eeddc0SDimitry Andric "vectors the minimum number of elements is determined from "
588104eeddc0SDimitry Andric "vscale_range.",
588204eeddc0SDimitry Andric &Call);
588304eeddc0SDimitry Andric break;
588404eeddc0SDimitry Andric }
5885fe6060f1SDimitry Andric case Intrinsic::experimental_stepvector: {
5886fe6060f1SDimitry Andric VectorType *VecTy = dyn_cast<VectorType>(Call.getType());
588781ad6265SDimitry Andric Check(VecTy && VecTy->getScalarType()->isIntegerTy() &&
5888fe6060f1SDimitry Andric VecTy->getScalarSizeInBits() >= 8,
5889fe6060f1SDimitry Andric "experimental_stepvector only supported for vectors of integers "
5890fe6060f1SDimitry Andric "with a bitwidth of at least 8.",
5891fe6060f1SDimitry Andric &Call);
5892fe6060f1SDimitry Andric break;
5893fe6060f1SDimitry Andric }
589481ad6265SDimitry Andric case Intrinsic::vector_insert: {
5895fe6060f1SDimitry Andric Value *Vec = Call.getArgOperand(0);
5896fe6060f1SDimitry Andric Value *SubVec = Call.getArgOperand(1);
5897fe6060f1SDimitry Andric Value *Idx = Call.getArgOperand(2);
5898fe6060f1SDimitry Andric unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
5899e8d8bef9SDimitry Andric
5900fe6060f1SDimitry Andric VectorType *VecTy = cast<VectorType>(Vec->getType());
5901fe6060f1SDimitry Andric VectorType *SubVecTy = cast<VectorType>(SubVec->getType());
5902fe6060f1SDimitry Andric
5903fe6060f1SDimitry Andric ElementCount VecEC = VecTy->getElementCount();
5904fe6060f1SDimitry Andric ElementCount SubVecEC = SubVecTy->getElementCount();
590581ad6265SDimitry Andric Check(VecTy->getElementType() == SubVecTy->getElementType(),
590681ad6265SDimitry Andric "vector_insert parameters must have the same element "
5907e8d8bef9SDimitry Andric "type.",
5908e8d8bef9SDimitry Andric &Call);
590981ad6265SDimitry Andric Check(IdxN % SubVecEC.getKnownMinValue() == 0,
591081ad6265SDimitry Andric "vector_insert index must be a constant multiple of "
5911fe6060f1SDimitry Andric "the subvector's known minimum vector length.");
5912fe6060f1SDimitry Andric
5913fe6060f1SDimitry Andric // If this insertion is not the 'mixed' case where a fixed vector is
5914fe6060f1SDimitry Andric // inserted into a scalable vector, ensure that the insertion of the
5915fe6060f1SDimitry Andric // subvector does not overrun the parent vector.
5916fe6060f1SDimitry Andric if (VecEC.isScalable() == SubVecEC.isScalable()) {
591781ad6265SDimitry Andric Check(IdxN < VecEC.getKnownMinValue() &&
5918fe6060f1SDimitry Andric IdxN + SubVecEC.getKnownMinValue() <= VecEC.getKnownMinValue(),
591981ad6265SDimitry Andric "subvector operand of vector_insert would overrun the "
5920fe6060f1SDimitry Andric "vector being inserted into.");
5921fe6060f1SDimitry Andric }
5922e8d8bef9SDimitry Andric break;
5923e8d8bef9SDimitry Andric }
592481ad6265SDimitry Andric case Intrinsic::vector_extract: {
5925fe6060f1SDimitry Andric Value *Vec = Call.getArgOperand(0);
5926fe6060f1SDimitry Andric Value *Idx = Call.getArgOperand(1);
5927fe6060f1SDimitry Andric unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
5928fe6060f1SDimitry Andric
5929e8d8bef9SDimitry Andric VectorType *ResultTy = cast<VectorType>(Call.getType());
5930fe6060f1SDimitry Andric VectorType *VecTy = cast<VectorType>(Vec->getType());
5931fe6060f1SDimitry Andric
5932fe6060f1SDimitry Andric ElementCount VecEC = VecTy->getElementCount();
5933fe6060f1SDimitry Andric ElementCount ResultEC = ResultTy->getElementCount();
5934e8d8bef9SDimitry Andric
593581ad6265SDimitry Andric Check(ResultTy->getElementType() == VecTy->getElementType(),
593681ad6265SDimitry Andric "vector_extract result must have the same element "
5937e8d8bef9SDimitry Andric "type as the input vector.",
5938e8d8bef9SDimitry Andric &Call);
593981ad6265SDimitry Andric Check(IdxN % ResultEC.getKnownMinValue() == 0,
594081ad6265SDimitry Andric "vector_extract index must be a constant multiple of "
5941fe6060f1SDimitry Andric "the result type's known minimum vector length.");
5942fe6060f1SDimitry Andric
59435f757f3fSDimitry Andric // If this extraction is not the 'mixed' case where a fixed vector is
5944fe6060f1SDimitry Andric // extracted from a scalable vector, ensure that the extraction does not
5945fe6060f1SDimitry Andric // overrun the parent vector.
5946fe6060f1SDimitry Andric if (VecEC.isScalable() == ResultEC.isScalable()) {
594781ad6265SDimitry Andric Check(IdxN < VecEC.getKnownMinValue() &&
5948fe6060f1SDimitry Andric IdxN + ResultEC.getKnownMinValue() <= VecEC.getKnownMinValue(),
594981ad6265SDimitry Andric "vector_extract would overrun.");
5950fe6060f1SDimitry Andric }
5951e8d8bef9SDimitry Andric break;
5952e8d8bef9SDimitry Andric }
5953e8d8bef9SDimitry Andric case Intrinsic::experimental_noalias_scope_decl: {
5954e8d8bef9SDimitry Andric NoAliasScopeDecls.push_back(cast<IntrinsicInst>(&Call));
5955e8d8bef9SDimitry Andric break;
5956e8d8bef9SDimitry Andric }
5957fe6060f1SDimitry Andric case Intrinsic::preserve_array_access_index:
595881ad6265SDimitry Andric case Intrinsic::preserve_struct_access_index:
595981ad6265SDimitry Andric case Intrinsic::aarch64_ldaxr:
596081ad6265SDimitry Andric case Intrinsic::aarch64_ldxr:
596181ad6265SDimitry Andric case Intrinsic::arm_ldaex:
596281ad6265SDimitry Andric case Intrinsic::arm_ldrex: {
596381ad6265SDimitry Andric Type *ElemTy = Call.getParamElementType(0);
596481ad6265SDimitry Andric Check(ElemTy, "Intrinsic requires elementtype attribute on first argument.",
596581ad6265SDimitry Andric &Call);
596681ad6265SDimitry Andric break;
596781ad6265SDimitry Andric }
596881ad6265SDimitry Andric case Intrinsic::aarch64_stlxr:
596981ad6265SDimitry Andric case Intrinsic::aarch64_stxr:
597081ad6265SDimitry Andric case Intrinsic::arm_stlex:
597181ad6265SDimitry Andric case Intrinsic::arm_strex: {
597281ad6265SDimitry Andric Type *ElemTy = Call.getAttributes().getParamElementType(1);
597381ad6265SDimitry Andric Check(ElemTy,
597481ad6265SDimitry Andric "Intrinsic requires elementtype attribute on second argument.",
5975fe6060f1SDimitry Andric &Call);
5976fe6060f1SDimitry Andric break;
5977fe6060f1SDimitry Andric }
5978bdd1243dSDimitry Andric case Intrinsic::aarch64_prefetch: {
5979bdd1243dSDimitry Andric Check(cast<ConstantInt>(Call.getArgOperand(1))->getZExtValue() < 2,
5980bdd1243dSDimitry Andric "write argument to llvm.aarch64.prefetch must be 0 or 1", Call);
5981bdd1243dSDimitry Andric Check(cast<ConstantInt>(Call.getArgOperand(2))->getZExtValue() < 4,
5982bdd1243dSDimitry Andric "target argument to llvm.aarch64.prefetch must be 0-3", Call);
5983bdd1243dSDimitry Andric Check(cast<ConstantInt>(Call.getArgOperand(3))->getZExtValue() < 2,
5984bdd1243dSDimitry Andric "stream argument to llvm.aarch64.prefetch must be 0 or 1", Call);
5985bdd1243dSDimitry Andric Check(cast<ConstantInt>(Call.getArgOperand(4))->getZExtValue() < 2,
5986bdd1243dSDimitry Andric "isdata argument to llvm.aarch64.prefetch must be 0 or 1", Call);
5987bdd1243dSDimitry Andric break;
5988bdd1243dSDimitry Andric }
598906c3fb27SDimitry Andric case Intrinsic::callbr_landingpad: {
599006c3fb27SDimitry Andric const auto *CBR = dyn_cast<CallBrInst>(Call.getOperand(0));
599106c3fb27SDimitry Andric Check(CBR, "intrinstic requires callbr operand", &Call);
599206c3fb27SDimitry Andric if (!CBR)
599306c3fb27SDimitry Andric break;
599406c3fb27SDimitry Andric
599506c3fb27SDimitry Andric const BasicBlock *LandingPadBB = Call.getParent();
599606c3fb27SDimitry Andric const BasicBlock *PredBB = LandingPadBB->getUniquePredecessor();
599706c3fb27SDimitry Andric if (!PredBB) {
599806c3fb27SDimitry Andric CheckFailed("Intrinsic in block must have 1 unique predecessor", &Call);
599906c3fb27SDimitry Andric break;
600006c3fb27SDimitry Andric }
600106c3fb27SDimitry Andric if (!isa<CallBrInst>(PredBB->getTerminator())) {
600206c3fb27SDimitry Andric CheckFailed("Intrinsic must have corresponding callbr in predecessor",
600306c3fb27SDimitry Andric &Call);
600406c3fb27SDimitry Andric break;
600506c3fb27SDimitry Andric }
600606c3fb27SDimitry Andric Check(llvm::any_of(CBR->getIndirectDests(),
600706c3fb27SDimitry Andric [LandingPadBB](const BasicBlock *IndDest) {
600806c3fb27SDimitry Andric return IndDest == LandingPadBB;
600906c3fb27SDimitry Andric }),
601006c3fb27SDimitry Andric "Intrinsic's corresponding callbr must have intrinsic's parent basic "
601106c3fb27SDimitry Andric "block in indirect destination list",
601206c3fb27SDimitry Andric &Call);
601306c3fb27SDimitry Andric const Instruction &First = *LandingPadBB->begin();
601406c3fb27SDimitry Andric Check(&First == &Call, "No other instructions may proceed intrinsic",
601506c3fb27SDimitry Andric &Call);
601606c3fb27SDimitry Andric break;
601706c3fb27SDimitry Andric }
601806c3fb27SDimitry Andric case Intrinsic::amdgcn_cs_chain: {
601906c3fb27SDimitry Andric auto CallerCC = Call.getCaller()->getCallingConv();
602006c3fb27SDimitry Andric switch (CallerCC) {
602106c3fb27SDimitry Andric case CallingConv::AMDGPU_CS:
602206c3fb27SDimitry Andric case CallingConv::AMDGPU_CS_Chain:
602306c3fb27SDimitry Andric case CallingConv::AMDGPU_CS_ChainPreserve:
602406c3fb27SDimitry Andric break;
602506c3fb27SDimitry Andric default:
602606c3fb27SDimitry Andric CheckFailed("Intrinsic can only be used from functions with the "
602706c3fb27SDimitry Andric "amdgpu_cs, amdgpu_cs_chain or amdgpu_cs_chain_preserve "
602806c3fb27SDimitry Andric "calling conventions",
602906c3fb27SDimitry Andric &Call);
603006c3fb27SDimitry Andric break;
603106c3fb27SDimitry Andric }
60325f757f3fSDimitry Andric
60335f757f3fSDimitry Andric Check(Call.paramHasAttr(2, Attribute::InReg),
60345f757f3fSDimitry Andric "SGPR arguments must have the `inreg` attribute", &Call);
60355f757f3fSDimitry Andric Check(!Call.paramHasAttr(3, Attribute::InReg),
60365f757f3fSDimitry Andric "VGPR arguments must not have the `inreg` attribute", &Call);
60375f757f3fSDimitry Andric break;
60385f757f3fSDimitry Andric }
60395f757f3fSDimitry Andric case Intrinsic::amdgcn_set_inactive_chain_arg: {
60405f757f3fSDimitry Andric auto CallerCC = Call.getCaller()->getCallingConv();
60415f757f3fSDimitry Andric switch (CallerCC) {
60425f757f3fSDimitry Andric case CallingConv::AMDGPU_CS_Chain:
60435f757f3fSDimitry Andric case CallingConv::AMDGPU_CS_ChainPreserve:
60445f757f3fSDimitry Andric break;
60455f757f3fSDimitry Andric default:
60465f757f3fSDimitry Andric CheckFailed("Intrinsic can only be used from functions with the "
60475f757f3fSDimitry Andric "amdgpu_cs_chain or amdgpu_cs_chain_preserve "
60485f757f3fSDimitry Andric "calling conventions",
60495f757f3fSDimitry Andric &Call);
60505f757f3fSDimitry Andric break;
60515f757f3fSDimitry Andric }
60525f757f3fSDimitry Andric
60535f757f3fSDimitry Andric unsigned InactiveIdx = 1;
60545f757f3fSDimitry Andric Check(!Call.paramHasAttr(InactiveIdx, Attribute::InReg),
60555f757f3fSDimitry Andric "Value for inactive lanes must not have the `inreg` attribute",
60565f757f3fSDimitry Andric &Call);
60575f757f3fSDimitry Andric Check(isa<Argument>(Call.getArgOperand(InactiveIdx)),
60585f757f3fSDimitry Andric "Value for inactive lanes must be a function argument", &Call);
60595f757f3fSDimitry Andric Check(!cast<Argument>(Call.getArgOperand(InactiveIdx))->hasInRegAttr(),
60605f757f3fSDimitry Andric "Value for inactive lanes must be a VGPR function argument", &Call);
606106c3fb27SDimitry Andric break;
606206c3fb27SDimitry Andric }
6063297eecfbSDimitry Andric case Intrinsic::nvvm_setmaxnreg_inc_sync_aligned_u32:
6064297eecfbSDimitry Andric case Intrinsic::nvvm_setmaxnreg_dec_sync_aligned_u32: {
6065297eecfbSDimitry Andric Value *V = Call.getArgOperand(0);
6066297eecfbSDimitry Andric unsigned RegCount = cast<ConstantInt>(V)->getZExtValue();
6067297eecfbSDimitry Andric Check(RegCount % 8 == 0,
6068297eecfbSDimitry Andric "reg_count argument to nvvm.setmaxnreg must be in multiples of 8");
6069297eecfbSDimitry Andric Check((RegCount >= 24 && RegCount <= 256),
6070297eecfbSDimitry Andric "reg_count argument to nvvm.setmaxnreg must be within [24, 256]");
6071297eecfbSDimitry Andric break;
6072297eecfbSDimitry Andric }
607306c3fb27SDimitry Andric case Intrinsic::experimental_convergence_entry:
607406c3fb27SDimitry Andric LLVM_FALLTHROUGH;
607506c3fb27SDimitry Andric case Intrinsic::experimental_convergence_anchor:
607606c3fb27SDimitry Andric break;
607706c3fb27SDimitry Andric case Intrinsic::experimental_convergence_loop:
607806c3fb27SDimitry Andric break;
60795f757f3fSDimitry Andric case Intrinsic::ptrmask: {
60805f757f3fSDimitry Andric Type *Ty0 = Call.getArgOperand(0)->getType();
60815f757f3fSDimitry Andric Type *Ty1 = Call.getArgOperand(1)->getType();
60825f757f3fSDimitry Andric Check(Ty0->isPtrOrPtrVectorTy(),
60835f757f3fSDimitry Andric "llvm.ptrmask intrinsic first argument must be pointer or vector "
60845f757f3fSDimitry Andric "of pointers",
60855f757f3fSDimitry Andric &Call);
60865f757f3fSDimitry Andric Check(
60875f757f3fSDimitry Andric Ty0->isVectorTy() == Ty1->isVectorTy(),
60885f757f3fSDimitry Andric "llvm.ptrmask intrinsic arguments must be both scalars or both vectors",
60895f757f3fSDimitry Andric &Call);
60905f757f3fSDimitry Andric if (Ty0->isVectorTy())
60915f757f3fSDimitry Andric Check(cast<VectorType>(Ty0)->getElementCount() ==
60925f757f3fSDimitry Andric cast<VectorType>(Ty1)->getElementCount(),
60935f757f3fSDimitry Andric "llvm.ptrmask intrinsic arguments must have the same number of "
60945f757f3fSDimitry Andric "elements",
60955f757f3fSDimitry Andric &Call);
60965f757f3fSDimitry Andric Check(DL.getIndexTypeSizeInBits(Ty0) == Ty1->getScalarSizeInBits(),
60975f757f3fSDimitry Andric "llvm.ptrmask intrinsic second argument bitwidth must match "
60985f757f3fSDimitry Andric "pointer index type size of first argument",
60995f757f3fSDimitry Andric &Call);
61005f757f3fSDimitry Andric break;
61015f757f3fSDimitry Andric }
61020b57cec5SDimitry Andric };
610306c3fb27SDimitry Andric
610406c3fb27SDimitry Andric // Verify that there aren't any unmediated control transfers between funclets.
610506c3fb27SDimitry Andric if (IntrinsicInst::mayLowerToFunctionCall(ID)) {
610606c3fb27SDimitry Andric Function *F = Call.getParent()->getParent();
610706c3fb27SDimitry Andric if (F->hasPersonalityFn() &&
610806c3fb27SDimitry Andric isScopedEHPersonality(classifyEHPersonality(F->getPersonalityFn()))) {
610906c3fb27SDimitry Andric // Run EH funclet coloring on-demand and cache results for other intrinsic
611006c3fb27SDimitry Andric // calls in this function
611106c3fb27SDimitry Andric if (BlockEHFuncletColors.empty())
611206c3fb27SDimitry Andric BlockEHFuncletColors = colorEHFunclets(*F);
611306c3fb27SDimitry Andric
611406c3fb27SDimitry Andric // Check for catch-/cleanup-pad in first funclet block
611506c3fb27SDimitry Andric bool InEHFunclet = false;
611606c3fb27SDimitry Andric BasicBlock *CallBB = Call.getParent();
611706c3fb27SDimitry Andric const ColorVector &CV = BlockEHFuncletColors.find(CallBB)->second;
611806c3fb27SDimitry Andric assert(CV.size() > 0 && "Uncolored block");
611906c3fb27SDimitry Andric for (BasicBlock *ColorFirstBB : CV)
612006c3fb27SDimitry Andric if (dyn_cast_or_null<FuncletPadInst>(ColorFirstBB->getFirstNonPHI()))
612106c3fb27SDimitry Andric InEHFunclet = true;
612206c3fb27SDimitry Andric
612306c3fb27SDimitry Andric // Check for funclet operand bundle
612406c3fb27SDimitry Andric bool HasToken = false;
612506c3fb27SDimitry Andric for (unsigned I = 0, E = Call.getNumOperandBundles(); I != E; ++I)
612606c3fb27SDimitry Andric if (Call.getOperandBundleAt(I).getTagID() == LLVMContext::OB_funclet)
612706c3fb27SDimitry Andric HasToken = true;
612806c3fb27SDimitry Andric
612906c3fb27SDimitry Andric // This would cause silent code truncation in WinEHPrepare
613006c3fb27SDimitry Andric if (InEHFunclet)
613106c3fb27SDimitry Andric Check(HasToken, "Missing funclet token on intrinsic call", &Call);
613206c3fb27SDimitry Andric }
613306c3fb27SDimitry Andric }
61340b57cec5SDimitry Andric }
61350b57cec5SDimitry Andric
61360b57cec5SDimitry Andric /// Carefully grab the subprogram from a local scope.
61370b57cec5SDimitry Andric ///
61380b57cec5SDimitry Andric /// This carefully grabs the subprogram from a local scope, avoiding the
61390b57cec5SDimitry Andric /// built-in assertions that would typically fire.
getSubprogram(Metadata * LocalScope)61400b57cec5SDimitry Andric static DISubprogram *getSubprogram(Metadata *LocalScope) {
61410b57cec5SDimitry Andric if (!LocalScope)
61420b57cec5SDimitry Andric return nullptr;
61430b57cec5SDimitry Andric
61440b57cec5SDimitry Andric if (auto *SP = dyn_cast<DISubprogram>(LocalScope))
61450b57cec5SDimitry Andric return SP;
61460b57cec5SDimitry Andric
61470b57cec5SDimitry Andric if (auto *LB = dyn_cast<DILexicalBlockBase>(LocalScope))
61480b57cec5SDimitry Andric return getSubprogram(LB->getRawScope());
61490b57cec5SDimitry Andric
61500b57cec5SDimitry Andric // Just return null; broken scope chains are checked elsewhere.
61510b57cec5SDimitry Andric assert(!isa<DILocalScope>(LocalScope) && "Unknown type of local scope");
61520b57cec5SDimitry Andric return nullptr;
61530b57cec5SDimitry Andric }
61540b57cec5SDimitry Andric
visitVPIntrinsic(VPIntrinsic & VPI)615581ad6265SDimitry Andric void Verifier::visitVPIntrinsic(VPIntrinsic &VPI) {
615681ad6265SDimitry Andric if (auto *VPCast = dyn_cast<VPCastIntrinsic>(&VPI)) {
615781ad6265SDimitry Andric auto *RetTy = cast<VectorType>(VPCast->getType());
615881ad6265SDimitry Andric auto *ValTy = cast<VectorType>(VPCast->getOperand(0)->getType());
615981ad6265SDimitry Andric Check(RetTy->getElementCount() == ValTy->getElementCount(),
616081ad6265SDimitry Andric "VP cast intrinsic first argument and result vector lengths must be "
616181ad6265SDimitry Andric "equal",
616281ad6265SDimitry Andric *VPCast);
616381ad6265SDimitry Andric
616481ad6265SDimitry Andric switch (VPCast->getIntrinsicID()) {
616581ad6265SDimitry Andric default:
616681ad6265SDimitry Andric llvm_unreachable("Unknown VP cast intrinsic");
616781ad6265SDimitry Andric case Intrinsic::vp_trunc:
616881ad6265SDimitry Andric Check(RetTy->isIntOrIntVectorTy() && ValTy->isIntOrIntVectorTy(),
616981ad6265SDimitry Andric "llvm.vp.trunc intrinsic first argument and result element type "
617081ad6265SDimitry Andric "must be integer",
617181ad6265SDimitry Andric *VPCast);
617281ad6265SDimitry Andric Check(RetTy->getScalarSizeInBits() < ValTy->getScalarSizeInBits(),
617381ad6265SDimitry Andric "llvm.vp.trunc intrinsic the bit size of first argument must be "
617481ad6265SDimitry Andric "larger than the bit size of the return type",
617581ad6265SDimitry Andric *VPCast);
617681ad6265SDimitry Andric break;
617781ad6265SDimitry Andric case Intrinsic::vp_zext:
617881ad6265SDimitry Andric case Intrinsic::vp_sext:
617981ad6265SDimitry Andric Check(RetTy->isIntOrIntVectorTy() && ValTy->isIntOrIntVectorTy(),
618081ad6265SDimitry Andric "llvm.vp.zext or llvm.vp.sext intrinsic first argument and result "
618181ad6265SDimitry Andric "element type must be integer",
618281ad6265SDimitry Andric *VPCast);
618381ad6265SDimitry Andric Check(RetTy->getScalarSizeInBits() > ValTy->getScalarSizeInBits(),
618481ad6265SDimitry Andric "llvm.vp.zext or llvm.vp.sext intrinsic the bit size of first "
618581ad6265SDimitry Andric "argument must be smaller than the bit size of the return type",
618681ad6265SDimitry Andric *VPCast);
618781ad6265SDimitry Andric break;
618881ad6265SDimitry Andric case Intrinsic::vp_fptoui:
618981ad6265SDimitry Andric case Intrinsic::vp_fptosi:
619081ad6265SDimitry Andric Check(
619181ad6265SDimitry Andric RetTy->isIntOrIntVectorTy() && ValTy->isFPOrFPVectorTy(),
619281ad6265SDimitry Andric "llvm.vp.fptoui or llvm.vp.fptosi intrinsic first argument element "
619381ad6265SDimitry Andric "type must be floating-point and result element type must be integer",
619481ad6265SDimitry Andric *VPCast);
619581ad6265SDimitry Andric break;
619681ad6265SDimitry Andric case Intrinsic::vp_uitofp:
619781ad6265SDimitry Andric case Intrinsic::vp_sitofp:
619881ad6265SDimitry Andric Check(
619981ad6265SDimitry Andric RetTy->isFPOrFPVectorTy() && ValTy->isIntOrIntVectorTy(),
620081ad6265SDimitry Andric "llvm.vp.uitofp or llvm.vp.sitofp intrinsic first argument element "
620181ad6265SDimitry Andric "type must be integer and result element type must be floating-point",
620281ad6265SDimitry Andric *VPCast);
620381ad6265SDimitry Andric break;
620481ad6265SDimitry Andric case Intrinsic::vp_fptrunc:
620581ad6265SDimitry Andric Check(RetTy->isFPOrFPVectorTy() && ValTy->isFPOrFPVectorTy(),
620681ad6265SDimitry Andric "llvm.vp.fptrunc intrinsic first argument and result element type "
620781ad6265SDimitry Andric "must be floating-point",
620881ad6265SDimitry Andric *VPCast);
620981ad6265SDimitry Andric Check(RetTy->getScalarSizeInBits() < ValTy->getScalarSizeInBits(),
621081ad6265SDimitry Andric "llvm.vp.fptrunc intrinsic the bit size of first argument must be "
621181ad6265SDimitry Andric "larger than the bit size of the return type",
621281ad6265SDimitry Andric *VPCast);
621381ad6265SDimitry Andric break;
621481ad6265SDimitry Andric case Intrinsic::vp_fpext:
621581ad6265SDimitry Andric Check(RetTy->isFPOrFPVectorTy() && ValTy->isFPOrFPVectorTy(),
621681ad6265SDimitry Andric "llvm.vp.fpext intrinsic first argument and result element type "
621781ad6265SDimitry Andric "must be floating-point",
621881ad6265SDimitry Andric *VPCast);
621981ad6265SDimitry Andric Check(RetTy->getScalarSizeInBits() > ValTy->getScalarSizeInBits(),
622081ad6265SDimitry Andric "llvm.vp.fpext intrinsic the bit size of first argument must be "
622181ad6265SDimitry Andric "smaller than the bit size of the return type",
622281ad6265SDimitry Andric *VPCast);
622381ad6265SDimitry Andric break;
622481ad6265SDimitry Andric case Intrinsic::vp_ptrtoint:
622581ad6265SDimitry Andric Check(RetTy->isIntOrIntVectorTy() && ValTy->isPtrOrPtrVectorTy(),
622681ad6265SDimitry Andric "llvm.vp.ptrtoint intrinsic first argument element type must be "
622781ad6265SDimitry Andric "pointer and result element type must be integer",
622881ad6265SDimitry Andric *VPCast);
622981ad6265SDimitry Andric break;
623081ad6265SDimitry Andric case Intrinsic::vp_inttoptr:
623181ad6265SDimitry Andric Check(RetTy->isPtrOrPtrVectorTy() && ValTy->isIntOrIntVectorTy(),
623281ad6265SDimitry Andric "llvm.vp.inttoptr intrinsic first argument element type must be "
623381ad6265SDimitry Andric "integer and result element type must be pointer",
623481ad6265SDimitry Andric *VPCast);
623581ad6265SDimitry Andric break;
623681ad6265SDimitry Andric }
623781ad6265SDimitry Andric }
623881ad6265SDimitry Andric if (VPI.getIntrinsicID() == Intrinsic::vp_fcmp) {
623981ad6265SDimitry Andric auto Pred = cast<VPCmpIntrinsic>(&VPI)->getPredicate();
624081ad6265SDimitry Andric Check(CmpInst::isFPPredicate(Pred),
624181ad6265SDimitry Andric "invalid predicate for VP FP comparison intrinsic", &VPI);
624281ad6265SDimitry Andric }
624381ad6265SDimitry Andric if (VPI.getIntrinsicID() == Intrinsic::vp_icmp) {
624481ad6265SDimitry Andric auto Pred = cast<VPCmpIntrinsic>(&VPI)->getPredicate();
624581ad6265SDimitry Andric Check(CmpInst::isIntPredicate(Pred),
624681ad6265SDimitry Andric "invalid predicate for VP integer comparison intrinsic", &VPI);
624781ad6265SDimitry Andric }
62485f757f3fSDimitry Andric if (VPI.getIntrinsicID() == Intrinsic::vp_is_fpclass) {
62495f757f3fSDimitry Andric auto TestMask = cast<ConstantInt>(VPI.getOperand(1));
62505f757f3fSDimitry Andric Check((TestMask->getZExtValue() & ~static_cast<unsigned>(fcAllFlags)) == 0,
62515f757f3fSDimitry Andric "unsupported bits for llvm.vp.is.fpclass test mask");
62525f757f3fSDimitry Andric }
625381ad6265SDimitry Andric }
625481ad6265SDimitry Andric
visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic & FPI)62550b57cec5SDimitry Andric void Verifier::visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI) {
6256480093f4SDimitry Andric unsigned NumOperands;
6257480093f4SDimitry Andric bool HasRoundingMD;
62580b57cec5SDimitry Andric switch (FPI.getIntrinsicID()) {
62595ffd83dbSDimitry Andric #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
6260480093f4SDimitry Andric case Intrinsic::INTRINSIC: \
6261480093f4SDimitry Andric NumOperands = NARG; \
6262480093f4SDimitry Andric HasRoundingMD = ROUND_MODE; \
62630b57cec5SDimitry Andric break;
6264480093f4SDimitry Andric #include "llvm/IR/ConstrainedOps.def"
6265480093f4SDimitry Andric default:
6266480093f4SDimitry Andric llvm_unreachable("Invalid constrained FP intrinsic!");
6267480093f4SDimitry Andric }
6268480093f4SDimitry Andric NumOperands += (1 + HasRoundingMD);
6269480093f4SDimitry Andric // Compare intrinsics carry an extra predicate metadata operand.
6270480093f4SDimitry Andric if (isa<ConstrainedFPCmpIntrinsic>(FPI))
6271480093f4SDimitry Andric NumOperands += 1;
627281ad6265SDimitry Andric Check((FPI.arg_size() == NumOperands),
6273480093f4SDimitry Andric "invalid arguments for constrained FP intrinsic", &FPI);
62740b57cec5SDimitry Andric
6275480093f4SDimitry Andric switch (FPI.getIntrinsicID()) {
62768bcb0991SDimitry Andric case Intrinsic::experimental_constrained_lrint:
62778bcb0991SDimitry Andric case Intrinsic::experimental_constrained_llrint: {
62788bcb0991SDimitry Andric Type *ValTy = FPI.getArgOperand(0)->getType();
62798bcb0991SDimitry Andric Type *ResultTy = FPI.getType();
628081ad6265SDimitry Andric Check(!ValTy->isVectorTy() && !ResultTy->isVectorTy(),
62818bcb0991SDimitry Andric "Intrinsic does not support vectors", &FPI);
62828bcb0991SDimitry Andric }
62838bcb0991SDimitry Andric break;
62848bcb0991SDimitry Andric
62858bcb0991SDimitry Andric case Intrinsic::experimental_constrained_lround:
62868bcb0991SDimitry Andric case Intrinsic::experimental_constrained_llround: {
62878bcb0991SDimitry Andric Type *ValTy = FPI.getArgOperand(0)->getType();
62888bcb0991SDimitry Andric Type *ResultTy = FPI.getType();
628981ad6265SDimitry Andric Check(!ValTy->isVectorTy() && !ResultTy->isVectorTy(),
62908bcb0991SDimitry Andric "Intrinsic does not support vectors", &FPI);
62918bcb0991SDimitry Andric break;
62928bcb0991SDimitry Andric }
62938bcb0991SDimitry Andric
6294480093f4SDimitry Andric case Intrinsic::experimental_constrained_fcmp:
6295480093f4SDimitry Andric case Intrinsic::experimental_constrained_fcmps: {
6296480093f4SDimitry Andric auto Pred = cast<ConstrainedFPCmpIntrinsic>(&FPI)->getPredicate();
629781ad6265SDimitry Andric Check(CmpInst::isFPPredicate(Pred),
6298480093f4SDimitry Andric "invalid predicate for constrained FP comparison intrinsic", &FPI);
62990b57cec5SDimitry Andric break;
6300480093f4SDimitry Andric }
63010b57cec5SDimitry Andric
63028bcb0991SDimitry Andric case Intrinsic::experimental_constrained_fptosi:
63038bcb0991SDimitry Andric case Intrinsic::experimental_constrained_fptoui: {
63048bcb0991SDimitry Andric Value *Operand = FPI.getArgOperand(0);
630506c3fb27SDimitry Andric ElementCount SrcEC;
630681ad6265SDimitry Andric Check(Operand->getType()->isFPOrFPVectorTy(),
63078bcb0991SDimitry Andric "Intrinsic first argument must be floating point", &FPI);
63088bcb0991SDimitry Andric if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
630906c3fb27SDimitry Andric SrcEC = cast<VectorType>(OperandT)->getElementCount();
63108bcb0991SDimitry Andric }
63118bcb0991SDimitry Andric
63128bcb0991SDimitry Andric Operand = &FPI;
631306c3fb27SDimitry Andric Check(SrcEC.isNonZero() == Operand->getType()->isVectorTy(),
63148bcb0991SDimitry Andric "Intrinsic first argument and result disagree on vector use", &FPI);
631581ad6265SDimitry Andric Check(Operand->getType()->isIntOrIntVectorTy(),
63168bcb0991SDimitry Andric "Intrinsic result must be an integer", &FPI);
63178bcb0991SDimitry Andric if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
631806c3fb27SDimitry Andric Check(SrcEC == cast<VectorType>(OperandT)->getElementCount(),
63198bcb0991SDimitry Andric "Intrinsic first argument and result vector lengths must be equal",
63208bcb0991SDimitry Andric &FPI);
63218bcb0991SDimitry Andric }
63228bcb0991SDimitry Andric }
63238bcb0991SDimitry Andric break;
63248bcb0991SDimitry Andric
6325480093f4SDimitry Andric case Intrinsic::experimental_constrained_sitofp:
6326480093f4SDimitry Andric case Intrinsic::experimental_constrained_uitofp: {
6327480093f4SDimitry Andric Value *Operand = FPI.getArgOperand(0);
632806c3fb27SDimitry Andric ElementCount SrcEC;
632981ad6265SDimitry Andric Check(Operand->getType()->isIntOrIntVectorTy(),
6330480093f4SDimitry Andric "Intrinsic first argument must be integer", &FPI);
6331480093f4SDimitry Andric if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
633206c3fb27SDimitry Andric SrcEC = cast<VectorType>(OperandT)->getElementCount();
6333480093f4SDimitry Andric }
6334480093f4SDimitry Andric
6335480093f4SDimitry Andric Operand = &FPI;
633606c3fb27SDimitry Andric Check(SrcEC.isNonZero() == Operand->getType()->isVectorTy(),
6337480093f4SDimitry Andric "Intrinsic first argument and result disagree on vector use", &FPI);
633881ad6265SDimitry Andric Check(Operand->getType()->isFPOrFPVectorTy(),
6339480093f4SDimitry Andric "Intrinsic result must be a floating point", &FPI);
6340480093f4SDimitry Andric if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
634106c3fb27SDimitry Andric Check(SrcEC == cast<VectorType>(OperandT)->getElementCount(),
6342480093f4SDimitry Andric "Intrinsic first argument and result vector lengths must be equal",
6343480093f4SDimitry Andric &FPI);
6344480093f4SDimitry Andric }
6345480093f4SDimitry Andric } break;
6346480093f4SDimitry Andric
63470b57cec5SDimitry Andric case Intrinsic::experimental_constrained_fptrunc:
63480b57cec5SDimitry Andric case Intrinsic::experimental_constrained_fpext: {
63490b57cec5SDimitry Andric Value *Operand = FPI.getArgOperand(0);
63500b57cec5SDimitry Andric Type *OperandTy = Operand->getType();
63510b57cec5SDimitry Andric Value *Result = &FPI;
63520b57cec5SDimitry Andric Type *ResultTy = Result->getType();
635381ad6265SDimitry Andric Check(OperandTy->isFPOrFPVectorTy(),
63540b57cec5SDimitry Andric "Intrinsic first argument must be FP or FP vector", &FPI);
635581ad6265SDimitry Andric Check(ResultTy->isFPOrFPVectorTy(),
63560b57cec5SDimitry Andric "Intrinsic result must be FP or FP vector", &FPI);
635781ad6265SDimitry Andric Check(OperandTy->isVectorTy() == ResultTy->isVectorTy(),
63580b57cec5SDimitry Andric "Intrinsic first argument and result disagree on vector use", &FPI);
63590b57cec5SDimitry Andric if (OperandTy->isVectorTy()) {
636006c3fb27SDimitry Andric Check(cast<VectorType>(OperandTy)->getElementCount() ==
636106c3fb27SDimitry Andric cast<VectorType>(ResultTy)->getElementCount(),
63620b57cec5SDimitry Andric "Intrinsic first argument and result vector lengths must be equal",
63630b57cec5SDimitry Andric &FPI);
63640b57cec5SDimitry Andric }
63650b57cec5SDimitry Andric if (FPI.getIntrinsicID() == Intrinsic::experimental_constrained_fptrunc) {
636681ad6265SDimitry Andric Check(OperandTy->getScalarSizeInBits() > ResultTy->getScalarSizeInBits(),
63670b57cec5SDimitry Andric "Intrinsic first argument's type must be larger than result type",
63680b57cec5SDimitry Andric &FPI);
63690b57cec5SDimitry Andric } else {
637081ad6265SDimitry Andric Check(OperandTy->getScalarSizeInBits() < ResultTy->getScalarSizeInBits(),
63710b57cec5SDimitry Andric "Intrinsic first argument's type must be smaller than result type",
63720b57cec5SDimitry Andric &FPI);
63730b57cec5SDimitry Andric }
63740b57cec5SDimitry Andric }
63750b57cec5SDimitry Andric break;
63760b57cec5SDimitry Andric
63770b57cec5SDimitry Andric default:
6378480093f4SDimitry Andric break;
63790b57cec5SDimitry Andric }
63800b57cec5SDimitry Andric
63810b57cec5SDimitry Andric // If a non-metadata argument is passed in a metadata slot then the
63820b57cec5SDimitry Andric // error will be caught earlier when the incorrect argument doesn't
63830b57cec5SDimitry Andric // match the specification in the intrinsic call table. Thus, no
63840b57cec5SDimitry Andric // argument type check is needed here.
63850b57cec5SDimitry Andric
638681ad6265SDimitry Andric Check(FPI.getExceptionBehavior().has_value(),
63870b57cec5SDimitry Andric "invalid exception behavior argument", &FPI);
63880b57cec5SDimitry Andric if (HasRoundingMD) {
638981ad6265SDimitry Andric Check(FPI.getRoundingMode().has_value(), "invalid rounding mode argument",
639081ad6265SDimitry Andric &FPI);
63910b57cec5SDimitry Andric }
63920b57cec5SDimitry Andric }
63930b57cec5SDimitry Andric
visitDbgIntrinsic(StringRef Kind,DbgVariableIntrinsic & DII)63940b57cec5SDimitry Andric void Verifier::visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII) {
6395fe6060f1SDimitry Andric auto *MD = DII.getRawLocation();
639681ad6265SDimitry Andric CheckDI(isa<ValueAsMetadata>(MD) || isa<DIArgList>(MD) ||
63970b57cec5SDimitry Andric (isa<MDNode>(MD) && !cast<MDNode>(MD)->getNumOperands()),
63980b57cec5SDimitry Andric "invalid llvm.dbg." + Kind + " intrinsic address/value", &DII, MD);
639981ad6265SDimitry Andric CheckDI(isa<DILocalVariable>(DII.getRawVariable()),
64000b57cec5SDimitry Andric "invalid llvm.dbg." + Kind + " intrinsic variable", &DII,
64010b57cec5SDimitry Andric DII.getRawVariable());
640281ad6265SDimitry Andric CheckDI(isa<DIExpression>(DII.getRawExpression()),
64030b57cec5SDimitry Andric "invalid llvm.dbg." + Kind + " intrinsic expression", &DII,
64040b57cec5SDimitry Andric DII.getRawExpression());
64050b57cec5SDimitry Andric
6406bdd1243dSDimitry Andric if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(&DII)) {
6407bdd1243dSDimitry Andric CheckDI(isa<DIAssignID>(DAI->getRawAssignID()),
6408bdd1243dSDimitry Andric "invalid llvm.dbg.assign intrinsic DIAssignID", &DII,
6409bdd1243dSDimitry Andric DAI->getRawAssignID());
6410bdd1243dSDimitry Andric const auto *RawAddr = DAI->getRawAddress();
6411bdd1243dSDimitry Andric CheckDI(
6412bdd1243dSDimitry Andric isa<ValueAsMetadata>(RawAddr) ||
6413bdd1243dSDimitry Andric (isa<MDNode>(RawAddr) && !cast<MDNode>(RawAddr)->getNumOperands()),
6414bdd1243dSDimitry Andric "invalid llvm.dbg.assign intrinsic address", &DII,
6415bdd1243dSDimitry Andric DAI->getRawAddress());
6416bdd1243dSDimitry Andric CheckDI(isa<DIExpression>(DAI->getRawAddressExpression()),
6417bdd1243dSDimitry Andric "invalid llvm.dbg.assign intrinsic address expression", &DII,
6418bdd1243dSDimitry Andric DAI->getRawAddressExpression());
6419bdd1243dSDimitry Andric // All of the linked instructions should be in the same function as DII.
6420bdd1243dSDimitry Andric for (Instruction *I : at::getAssignmentInsts(DAI))
6421bdd1243dSDimitry Andric CheckDI(DAI->getFunction() == I->getFunction(),
6422bdd1243dSDimitry Andric "inst not in same function as dbg.assign", I, DAI);
6423bdd1243dSDimitry Andric }
6424bdd1243dSDimitry Andric
64250b57cec5SDimitry Andric // Ignore broken !dbg attachments; they're checked elsewhere.
64260b57cec5SDimitry Andric if (MDNode *N = DII.getDebugLoc().getAsMDNode())
64270b57cec5SDimitry Andric if (!isa<DILocation>(N))
64280b57cec5SDimitry Andric return;
64290b57cec5SDimitry Andric
64300b57cec5SDimitry Andric BasicBlock *BB = DII.getParent();
64310b57cec5SDimitry Andric Function *F = BB ? BB->getParent() : nullptr;
64320b57cec5SDimitry Andric
64330b57cec5SDimitry Andric // The scopes for variables and !dbg attachments must agree.
64340b57cec5SDimitry Andric DILocalVariable *Var = DII.getVariable();
64350b57cec5SDimitry Andric DILocation *Loc = DII.getDebugLoc();
643681ad6265SDimitry Andric CheckDI(Loc, "llvm.dbg." + Kind + " intrinsic requires a !dbg attachment",
64370b57cec5SDimitry Andric &DII, BB, F);
64380b57cec5SDimitry Andric
64390b57cec5SDimitry Andric DISubprogram *VarSP = getSubprogram(Var->getRawScope());
64400b57cec5SDimitry Andric DISubprogram *LocSP = getSubprogram(Loc->getRawScope());
64410b57cec5SDimitry Andric if (!VarSP || !LocSP)
64420b57cec5SDimitry Andric return; // Broken scope chains are checked elsewhere.
64430b57cec5SDimitry Andric
644481ad6265SDimitry Andric CheckDI(VarSP == LocSP,
644581ad6265SDimitry Andric "mismatched subprogram between llvm.dbg." + Kind +
64460b57cec5SDimitry Andric " variable and !dbg attachment",
64470b57cec5SDimitry Andric &DII, BB, F, Var, Var->getScope()->getSubprogram(), Loc,
64480b57cec5SDimitry Andric Loc->getScope()->getSubprogram());
64490b57cec5SDimitry Andric
64500b57cec5SDimitry Andric // This check is redundant with one in visitLocalVariable().
645181ad6265SDimitry Andric CheckDI(isType(Var->getRawType()), "invalid type ref", Var,
64520b57cec5SDimitry Andric Var->getRawType());
64530b57cec5SDimitry Andric verifyFnArgs(DII);
64540b57cec5SDimitry Andric }
64550b57cec5SDimitry Andric
visitDbgLabelIntrinsic(StringRef Kind,DbgLabelInst & DLI)64560b57cec5SDimitry Andric void Verifier::visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI) {
645781ad6265SDimitry Andric CheckDI(isa<DILabel>(DLI.getRawLabel()),
64580b57cec5SDimitry Andric "invalid llvm.dbg." + Kind + " intrinsic variable", &DLI,
64590b57cec5SDimitry Andric DLI.getRawLabel());
64600b57cec5SDimitry Andric
64610b57cec5SDimitry Andric // Ignore broken !dbg attachments; they're checked elsewhere.
64620b57cec5SDimitry Andric if (MDNode *N = DLI.getDebugLoc().getAsMDNode())
64630b57cec5SDimitry Andric if (!isa<DILocation>(N))
64640b57cec5SDimitry Andric return;
64650b57cec5SDimitry Andric
64660b57cec5SDimitry Andric BasicBlock *BB = DLI.getParent();
64670b57cec5SDimitry Andric Function *F = BB ? BB->getParent() : nullptr;
64680b57cec5SDimitry Andric
64690b57cec5SDimitry Andric // The scopes for variables and !dbg attachments must agree.
64700b57cec5SDimitry Andric DILabel *Label = DLI.getLabel();
64710b57cec5SDimitry Andric DILocation *Loc = DLI.getDebugLoc();
647281ad6265SDimitry Andric Check(Loc, "llvm.dbg." + Kind + " intrinsic requires a !dbg attachment", &DLI,
647381ad6265SDimitry Andric BB, F);
64740b57cec5SDimitry Andric
64750b57cec5SDimitry Andric DISubprogram *LabelSP = getSubprogram(Label->getRawScope());
64760b57cec5SDimitry Andric DISubprogram *LocSP = getSubprogram(Loc->getRawScope());
64770b57cec5SDimitry Andric if (!LabelSP || !LocSP)
64780b57cec5SDimitry Andric return;
64790b57cec5SDimitry Andric
648081ad6265SDimitry Andric CheckDI(LabelSP == LocSP,
648181ad6265SDimitry Andric "mismatched subprogram between llvm.dbg." + Kind +
64820b57cec5SDimitry Andric " label and !dbg attachment",
64830b57cec5SDimitry Andric &DLI, BB, F, Label, Label->getScope()->getSubprogram(), Loc,
64840b57cec5SDimitry Andric Loc->getScope()->getSubprogram());
64850b57cec5SDimitry Andric }
64860b57cec5SDimitry Andric
verifyFragmentExpression(const DbgVariableIntrinsic & I)64870b57cec5SDimitry Andric void Verifier::verifyFragmentExpression(const DbgVariableIntrinsic &I) {
64880b57cec5SDimitry Andric DILocalVariable *V = dyn_cast_or_null<DILocalVariable>(I.getRawVariable());
64890b57cec5SDimitry Andric DIExpression *E = dyn_cast_or_null<DIExpression>(I.getRawExpression());
64900b57cec5SDimitry Andric
64910b57cec5SDimitry Andric // We don't know whether this intrinsic verified correctly.
64920b57cec5SDimitry Andric if (!V || !E || !E->isValid())
64930b57cec5SDimitry Andric return;
64940b57cec5SDimitry Andric
64950b57cec5SDimitry Andric // Nothing to do if this isn't a DW_OP_LLVM_fragment expression.
64960b57cec5SDimitry Andric auto Fragment = E->getFragmentInfo();
64970b57cec5SDimitry Andric if (!Fragment)
64980b57cec5SDimitry Andric return;
64990b57cec5SDimitry Andric
65000b57cec5SDimitry Andric // The frontend helps out GDB by emitting the members of local anonymous
65010b57cec5SDimitry Andric // unions as artificial local variables with shared storage. When SROA splits
65020b57cec5SDimitry Andric // the storage for artificial local variables that are smaller than the entire
65030b57cec5SDimitry Andric // union, the overhang piece will be outside of the allotted space for the
65040b57cec5SDimitry Andric // variable and this check fails.
65050b57cec5SDimitry Andric // FIXME: Remove this check as soon as clang stops doing this; it hides bugs.
65060b57cec5SDimitry Andric if (V->isArtificial())
65070b57cec5SDimitry Andric return;
65080b57cec5SDimitry Andric
65090b57cec5SDimitry Andric verifyFragmentExpression(*V, *Fragment, &I);
65100b57cec5SDimitry Andric }
65110b57cec5SDimitry Andric
65120b57cec5SDimitry Andric template <typename ValueOrMetadata>
verifyFragmentExpression(const DIVariable & V,DIExpression::FragmentInfo Fragment,ValueOrMetadata * Desc)65130b57cec5SDimitry Andric void Verifier::verifyFragmentExpression(const DIVariable &V,
65140b57cec5SDimitry Andric DIExpression::FragmentInfo Fragment,
65150b57cec5SDimitry Andric ValueOrMetadata *Desc) {
65160b57cec5SDimitry Andric // If there's no size, the type is broken, but that should be checked
65170b57cec5SDimitry Andric // elsewhere.
65180b57cec5SDimitry Andric auto VarSize = V.getSizeInBits();
65190b57cec5SDimitry Andric if (!VarSize)
65200b57cec5SDimitry Andric return;
65210b57cec5SDimitry Andric
65220b57cec5SDimitry Andric unsigned FragSize = Fragment.SizeInBits;
65230b57cec5SDimitry Andric unsigned FragOffset = Fragment.OffsetInBits;
652481ad6265SDimitry Andric CheckDI(FragSize + FragOffset <= *VarSize,
65250b57cec5SDimitry Andric "fragment is larger than or outside of variable", Desc, &V);
652681ad6265SDimitry Andric CheckDI(FragSize != *VarSize, "fragment covers entire variable", Desc, &V);
65270b57cec5SDimitry Andric }
65280b57cec5SDimitry Andric
verifyFnArgs(const DbgVariableIntrinsic & I)65290b57cec5SDimitry Andric void Verifier::verifyFnArgs(const DbgVariableIntrinsic &I) {
65300b57cec5SDimitry Andric // This function does not take the scope of noninlined function arguments into
65310b57cec5SDimitry Andric // account. Don't run it if current function is nodebug, because it may
65320b57cec5SDimitry Andric // contain inlined debug intrinsics.
65330b57cec5SDimitry Andric if (!HasDebugInfo)
65340b57cec5SDimitry Andric return;
65350b57cec5SDimitry Andric
65360b57cec5SDimitry Andric // For performance reasons only check non-inlined ones.
65370b57cec5SDimitry Andric if (I.getDebugLoc()->getInlinedAt())
65380b57cec5SDimitry Andric return;
65390b57cec5SDimitry Andric
65400b57cec5SDimitry Andric DILocalVariable *Var = I.getVariable();
654181ad6265SDimitry Andric CheckDI(Var, "dbg intrinsic without variable");
65420b57cec5SDimitry Andric
65430b57cec5SDimitry Andric unsigned ArgNo = Var->getArg();
65440b57cec5SDimitry Andric if (!ArgNo)
65450b57cec5SDimitry Andric return;
65460b57cec5SDimitry Andric
65470b57cec5SDimitry Andric // Verify there are no duplicate function argument debug info entries.
65480b57cec5SDimitry Andric // These will cause hard-to-debug assertions in the DWARF backend.
65490b57cec5SDimitry Andric if (DebugFnArgs.size() < ArgNo)
65500b57cec5SDimitry Andric DebugFnArgs.resize(ArgNo, nullptr);
65510b57cec5SDimitry Andric
65520b57cec5SDimitry Andric auto *Prev = DebugFnArgs[ArgNo - 1];
65530b57cec5SDimitry Andric DebugFnArgs[ArgNo - 1] = Var;
655481ad6265SDimitry Andric CheckDI(!Prev || (Prev == Var), "conflicting debug info for argument", &I,
65550b57cec5SDimitry Andric Prev, Var);
65560b57cec5SDimitry Andric }
65570b57cec5SDimitry Andric
verifyNotEntryValue(const DbgVariableIntrinsic & I)65588bcb0991SDimitry Andric void Verifier::verifyNotEntryValue(const DbgVariableIntrinsic &I) {
65598bcb0991SDimitry Andric DIExpression *E = dyn_cast_or_null<DIExpression>(I.getRawExpression());
65608bcb0991SDimitry Andric
65618bcb0991SDimitry Andric // We don't know whether this intrinsic verified correctly.
65628bcb0991SDimitry Andric if (!E || !E->isValid())
65638bcb0991SDimitry Andric return;
65648bcb0991SDimitry Andric
65655f757f3fSDimitry Andric if (isa<ValueAsMetadata>(I.getRawLocation())) {
65665f757f3fSDimitry Andric Value *VarValue = I.getVariableLocationOp(0);
65675f757f3fSDimitry Andric if (isa<UndefValue>(VarValue) || isa<PoisonValue>(VarValue))
65685f757f3fSDimitry Andric return;
656906c3fb27SDimitry Andric // We allow EntryValues for swift async arguments, as they have an
657006c3fb27SDimitry Andric // ABI-guarantee to be turned into a specific register.
65715f757f3fSDimitry Andric if (auto *ArgLoc = dyn_cast_or_null<Argument>(VarValue);
657206c3fb27SDimitry Andric ArgLoc && ArgLoc->hasAttribute(Attribute::SwiftAsync))
657306c3fb27SDimitry Andric return;
65745f757f3fSDimitry Andric }
657506c3fb27SDimitry Andric
657606c3fb27SDimitry Andric CheckDI(!E->isEntryValue(),
657706c3fb27SDimitry Andric "Entry values are only allowed in MIR unless they target a "
657806c3fb27SDimitry Andric "swiftasync Argument",
657906c3fb27SDimitry Andric &I);
65808bcb0991SDimitry Andric }
65818bcb0991SDimitry Andric
verifyCompileUnits()65820b57cec5SDimitry Andric void Verifier::verifyCompileUnits() {
65830b57cec5SDimitry Andric // When more than one Module is imported into the same context, such as during
65840b57cec5SDimitry Andric // an LTO build before linking the modules, ODR type uniquing may cause types
65850b57cec5SDimitry Andric // to point to a different CU. This check does not make sense in this case.
65860b57cec5SDimitry Andric if (M.getContext().isODRUniquingDebugTypes())
65870b57cec5SDimitry Andric return;
65880b57cec5SDimitry Andric auto *CUs = M.getNamedMetadata("llvm.dbg.cu");
65890b57cec5SDimitry Andric SmallPtrSet<const Metadata *, 2> Listed;
65900b57cec5SDimitry Andric if (CUs)
65910b57cec5SDimitry Andric Listed.insert(CUs->op_begin(), CUs->op_end());
6592bdd1243dSDimitry Andric for (const auto *CU : CUVisited)
659381ad6265SDimitry Andric CheckDI(Listed.count(CU), "DICompileUnit not listed in llvm.dbg.cu", CU);
65940b57cec5SDimitry Andric CUVisited.clear();
65950b57cec5SDimitry Andric }
65960b57cec5SDimitry Andric
verifyDeoptimizeCallingConvs()65970b57cec5SDimitry Andric void Verifier::verifyDeoptimizeCallingConvs() {
65980b57cec5SDimitry Andric if (DeoptimizeDeclarations.empty())
65990b57cec5SDimitry Andric return;
66000b57cec5SDimitry Andric
66010b57cec5SDimitry Andric const Function *First = DeoptimizeDeclarations[0];
6602bdd1243dSDimitry Andric for (const auto *F : ArrayRef(DeoptimizeDeclarations).slice(1)) {
660381ad6265SDimitry Andric Check(First->getCallingConv() == F->getCallingConv(),
66040b57cec5SDimitry Andric "All llvm.experimental.deoptimize declarations must have the same "
66050b57cec5SDimitry Andric "calling convention",
66060b57cec5SDimitry Andric First, F);
66070b57cec5SDimitry Andric }
66080b57cec5SDimitry Andric }
66090b57cec5SDimitry Andric
verifyAttachedCallBundle(const CallBase & Call,const OperandBundleUse & BU)6610349cc55cSDimitry Andric void Verifier::verifyAttachedCallBundle(const CallBase &Call,
6611349cc55cSDimitry Andric const OperandBundleUse &BU) {
6612349cc55cSDimitry Andric FunctionType *FTy = Call.getFunctionType();
6613349cc55cSDimitry Andric
661481ad6265SDimitry Andric Check((FTy->getReturnType()->isPointerTy() ||
6615349cc55cSDimitry Andric (Call.doesNotReturn() && FTy->getReturnType()->isVoidTy())),
6616349cc55cSDimitry Andric "a call with operand bundle \"clang.arc.attachedcall\" must call a "
6617349cc55cSDimitry Andric "function returning a pointer or a non-returning function that has a "
6618349cc55cSDimitry Andric "void return type",
6619349cc55cSDimitry Andric Call);
6620349cc55cSDimitry Andric
662181ad6265SDimitry Andric Check(BU.Inputs.size() == 1 && isa<Function>(BU.Inputs.front()),
66221fd87a68SDimitry Andric "operand bundle \"clang.arc.attachedcall\" requires one function as "
66231fd87a68SDimitry Andric "an argument",
6624349cc55cSDimitry Andric Call);
6625349cc55cSDimitry Andric
6626349cc55cSDimitry Andric auto *Fn = cast<Function>(BU.Inputs.front());
6627349cc55cSDimitry Andric Intrinsic::ID IID = Fn->getIntrinsicID();
6628349cc55cSDimitry Andric
6629349cc55cSDimitry Andric if (IID) {
663081ad6265SDimitry Andric Check((IID == Intrinsic::objc_retainAutoreleasedReturnValue ||
6631349cc55cSDimitry Andric IID == Intrinsic::objc_unsafeClaimAutoreleasedReturnValue),
6632349cc55cSDimitry Andric "invalid function argument", Call);
6633349cc55cSDimitry Andric } else {
6634349cc55cSDimitry Andric StringRef FnName = Fn->getName();
663581ad6265SDimitry Andric Check((FnName == "objc_retainAutoreleasedReturnValue" ||
6636349cc55cSDimitry Andric FnName == "objc_unsafeClaimAutoreleasedReturnValue"),
6637349cc55cSDimitry Andric "invalid function argument", Call);
6638349cc55cSDimitry Andric }
6639349cc55cSDimitry Andric }
6640349cc55cSDimitry Andric
verifyNoAliasScopeDecl()6641e8d8bef9SDimitry Andric void Verifier::verifyNoAliasScopeDecl() {
6642e8d8bef9SDimitry Andric if (NoAliasScopeDecls.empty())
6643e8d8bef9SDimitry Andric return;
6644e8d8bef9SDimitry Andric
6645e8d8bef9SDimitry Andric // only a single scope must be declared at a time.
6646e8d8bef9SDimitry Andric for (auto *II : NoAliasScopeDecls) {
6647e8d8bef9SDimitry Andric assert(II->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl &&
6648e8d8bef9SDimitry Andric "Not a llvm.experimental.noalias.scope.decl ?");
6649e8d8bef9SDimitry Andric const auto *ScopeListMV = dyn_cast<MetadataAsValue>(
6650e8d8bef9SDimitry Andric II->getOperand(Intrinsic::NoAliasScopeDeclScopeArg));
665181ad6265SDimitry Andric Check(ScopeListMV != nullptr,
6652e8d8bef9SDimitry Andric "llvm.experimental.noalias.scope.decl must have a MetadataAsValue "
6653e8d8bef9SDimitry Andric "argument",
6654e8d8bef9SDimitry Andric II);
6655e8d8bef9SDimitry Andric
6656e8d8bef9SDimitry Andric const auto *ScopeListMD = dyn_cast<MDNode>(ScopeListMV->getMetadata());
665781ad6265SDimitry Andric Check(ScopeListMD != nullptr, "!id.scope.list must point to an MDNode", II);
665881ad6265SDimitry Andric Check(ScopeListMD->getNumOperands() == 1,
6659e8d8bef9SDimitry Andric "!id.scope.list must point to a list with a single scope", II);
6660349cc55cSDimitry Andric visitAliasScopeListMetadata(ScopeListMD);
6661e8d8bef9SDimitry Andric }
6662e8d8bef9SDimitry Andric
6663e8d8bef9SDimitry Andric // Only check the domination rule when requested. Once all passes have been
6664e8d8bef9SDimitry Andric // adapted this option can go away.
6665e8d8bef9SDimitry Andric if (!VerifyNoAliasScopeDomination)
6666e8d8bef9SDimitry Andric return;
6667e8d8bef9SDimitry Andric
6668e8d8bef9SDimitry Andric // Now sort the intrinsics based on the scope MDNode so that declarations of
6669e8d8bef9SDimitry Andric // the same scopes are next to each other.
6670e8d8bef9SDimitry Andric auto GetScope = [](IntrinsicInst *II) {
6671e8d8bef9SDimitry Andric const auto *ScopeListMV = cast<MetadataAsValue>(
6672e8d8bef9SDimitry Andric II->getOperand(Intrinsic::NoAliasScopeDeclScopeArg));
6673e8d8bef9SDimitry Andric return &cast<MDNode>(ScopeListMV->getMetadata())->getOperand(0);
6674e8d8bef9SDimitry Andric };
6675e8d8bef9SDimitry Andric
6676e8d8bef9SDimitry Andric // We are sorting on MDNode pointers here. For valid input IR this is ok.
6677e8d8bef9SDimitry Andric // TODO: Sort on Metadata ID to avoid non-deterministic error messages.
6678e8d8bef9SDimitry Andric auto Compare = [GetScope](IntrinsicInst *Lhs, IntrinsicInst *Rhs) {
6679e8d8bef9SDimitry Andric return GetScope(Lhs) < GetScope(Rhs);
6680e8d8bef9SDimitry Andric };
6681e8d8bef9SDimitry Andric
6682e8d8bef9SDimitry Andric llvm::sort(NoAliasScopeDecls, Compare);
6683e8d8bef9SDimitry Andric
6684e8d8bef9SDimitry Andric // Go over the intrinsics and check that for the same scope, they are not
6685e8d8bef9SDimitry Andric // dominating each other.
6686e8d8bef9SDimitry Andric auto ItCurrent = NoAliasScopeDecls.begin();
6687e8d8bef9SDimitry Andric while (ItCurrent != NoAliasScopeDecls.end()) {
6688e8d8bef9SDimitry Andric auto CurScope = GetScope(*ItCurrent);
6689e8d8bef9SDimitry Andric auto ItNext = ItCurrent;
6690e8d8bef9SDimitry Andric do {
6691e8d8bef9SDimitry Andric ++ItNext;
6692e8d8bef9SDimitry Andric } while (ItNext != NoAliasScopeDecls.end() &&
6693e8d8bef9SDimitry Andric GetScope(*ItNext) == CurScope);
6694e8d8bef9SDimitry Andric
6695e8d8bef9SDimitry Andric // [ItCurrent, ItNext) represents the declarations for the same scope.
6696e8d8bef9SDimitry Andric // Ensure they are not dominating each other.. but only if it is not too
6697e8d8bef9SDimitry Andric // expensive.
6698e8d8bef9SDimitry Andric if (ItNext - ItCurrent < 32)
6699e8d8bef9SDimitry Andric for (auto *I : llvm::make_range(ItCurrent, ItNext))
6700e8d8bef9SDimitry Andric for (auto *J : llvm::make_range(ItCurrent, ItNext))
6701e8d8bef9SDimitry Andric if (I != J)
670281ad6265SDimitry Andric Check(!DT.dominates(I, J),
6703e8d8bef9SDimitry Andric "llvm.experimental.noalias.scope.decl dominates another one "
6704e8d8bef9SDimitry Andric "with the same scope",
6705e8d8bef9SDimitry Andric I);
6706e8d8bef9SDimitry Andric ItCurrent = ItNext;
6707e8d8bef9SDimitry Andric }
6708e8d8bef9SDimitry Andric }
6709e8d8bef9SDimitry Andric
67100b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
67110b57cec5SDimitry Andric // Implement the public interfaces to this file...
67120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
67130b57cec5SDimitry Andric
verifyFunction(const Function & f,raw_ostream * OS)67140b57cec5SDimitry Andric bool llvm::verifyFunction(const Function &f, raw_ostream *OS) {
67150b57cec5SDimitry Andric Function &F = const_cast<Function &>(f);
67160b57cec5SDimitry Andric
67170b57cec5SDimitry Andric // Don't use a raw_null_ostream. Printing IR is expensive.
67180b57cec5SDimitry Andric Verifier V(OS, /*ShouldTreatBrokenDebugInfoAsError=*/true, *f.getParent());
67190b57cec5SDimitry Andric
67200b57cec5SDimitry Andric // Note that this function's return value is inverted from what you would
67210b57cec5SDimitry Andric // expect of a function called "verify".
67220b57cec5SDimitry Andric return !V.verify(F);
67230b57cec5SDimitry Andric }
67240b57cec5SDimitry Andric
verifyModule(const Module & M,raw_ostream * OS,bool * BrokenDebugInfo)67250b57cec5SDimitry Andric bool llvm::verifyModule(const Module &M, raw_ostream *OS,
67260b57cec5SDimitry Andric bool *BrokenDebugInfo) {
67270b57cec5SDimitry Andric // Don't use a raw_null_ostream. Printing IR is expensive.
67280b57cec5SDimitry Andric Verifier V(OS, /*ShouldTreatBrokenDebugInfoAsError=*/!BrokenDebugInfo, M);
67290b57cec5SDimitry Andric
67300b57cec5SDimitry Andric bool Broken = false;
67310b57cec5SDimitry Andric for (const Function &F : M)
67320b57cec5SDimitry Andric Broken |= !V.verify(F);
67330b57cec5SDimitry Andric
67340b57cec5SDimitry Andric Broken |= !V.verify();
67350b57cec5SDimitry Andric if (BrokenDebugInfo)
67360b57cec5SDimitry Andric *BrokenDebugInfo = V.hasBrokenDebugInfo();
67370b57cec5SDimitry Andric // Note that this function's return value is inverted from what you would
67380b57cec5SDimitry Andric // expect of a function called "verify".
67390b57cec5SDimitry Andric return Broken;
67400b57cec5SDimitry Andric }
67410b57cec5SDimitry Andric
67420b57cec5SDimitry Andric namespace {
67430b57cec5SDimitry Andric
67440b57cec5SDimitry Andric struct VerifierLegacyPass : public FunctionPass {
67450b57cec5SDimitry Andric static char ID;
67460b57cec5SDimitry Andric
67470b57cec5SDimitry Andric std::unique_ptr<Verifier> V;
67480b57cec5SDimitry Andric bool FatalErrors = true;
67490b57cec5SDimitry Andric
VerifierLegacyPass__anoneaa63fbe1311::VerifierLegacyPass67500b57cec5SDimitry Andric VerifierLegacyPass() : FunctionPass(ID) {
67510b57cec5SDimitry Andric initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
67520b57cec5SDimitry Andric }
VerifierLegacyPass__anoneaa63fbe1311::VerifierLegacyPass67530b57cec5SDimitry Andric explicit VerifierLegacyPass(bool FatalErrors)
67540b57cec5SDimitry Andric : FunctionPass(ID),
67550b57cec5SDimitry Andric FatalErrors(FatalErrors) {
67560b57cec5SDimitry Andric initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
67570b57cec5SDimitry Andric }
67580b57cec5SDimitry Andric
doInitialization__anoneaa63fbe1311::VerifierLegacyPass67590b57cec5SDimitry Andric bool doInitialization(Module &M) override {
67608bcb0991SDimitry Andric V = std::make_unique<Verifier>(
67610b57cec5SDimitry Andric &dbgs(), /*ShouldTreatBrokenDebugInfoAsError=*/false, M);
67620b57cec5SDimitry Andric return false;
67630b57cec5SDimitry Andric }
67640b57cec5SDimitry Andric
runOnFunction__anoneaa63fbe1311::VerifierLegacyPass67650b57cec5SDimitry Andric bool runOnFunction(Function &F) override {
67660b57cec5SDimitry Andric if (!V->verify(F) && FatalErrors) {
67670b57cec5SDimitry Andric errs() << "in function " << F.getName() << '\n';
67680b57cec5SDimitry Andric report_fatal_error("Broken function found, compilation aborted!");
67690b57cec5SDimitry Andric }
67700b57cec5SDimitry Andric return false;
67710b57cec5SDimitry Andric }
67720b57cec5SDimitry Andric
doFinalization__anoneaa63fbe1311::VerifierLegacyPass67730b57cec5SDimitry Andric bool doFinalization(Module &M) override {
67740b57cec5SDimitry Andric bool HasErrors = false;
67750b57cec5SDimitry Andric for (Function &F : M)
67760b57cec5SDimitry Andric if (F.isDeclaration())
67770b57cec5SDimitry Andric HasErrors |= !V->verify(F);
67780b57cec5SDimitry Andric
67790b57cec5SDimitry Andric HasErrors |= !V->verify();
67800b57cec5SDimitry Andric if (FatalErrors && (HasErrors || V->hasBrokenDebugInfo()))
67810b57cec5SDimitry Andric report_fatal_error("Broken module found, compilation aborted!");
67820b57cec5SDimitry Andric return false;
67830b57cec5SDimitry Andric }
67840b57cec5SDimitry Andric
getAnalysisUsage__anoneaa63fbe1311::VerifierLegacyPass67850b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
67860b57cec5SDimitry Andric AU.setPreservesAll();
67870b57cec5SDimitry Andric }
67880b57cec5SDimitry Andric };
67890b57cec5SDimitry Andric
67900b57cec5SDimitry Andric } // end anonymous namespace
67910b57cec5SDimitry Andric
67920b57cec5SDimitry Andric /// Helper to issue failure from the TBAA verification
CheckFailed(Tys &&...Args)67930b57cec5SDimitry Andric template <typename... Tys> void TBAAVerifier::CheckFailed(Tys &&... Args) {
67940b57cec5SDimitry Andric if (Diagnostic)
67950b57cec5SDimitry Andric return Diagnostic->CheckFailed(Args...);
67960b57cec5SDimitry Andric }
67970b57cec5SDimitry Andric
679881ad6265SDimitry Andric #define CheckTBAA(C, ...) \
67990b57cec5SDimitry Andric do { \
68000b57cec5SDimitry Andric if (!(C)) { \
68010b57cec5SDimitry Andric CheckFailed(__VA_ARGS__); \
68020b57cec5SDimitry Andric return false; \
68030b57cec5SDimitry Andric } \
68040b57cec5SDimitry Andric } while (false)
68050b57cec5SDimitry Andric
68060b57cec5SDimitry Andric /// Verify that \p BaseNode can be used as the "base type" in the struct-path
68070b57cec5SDimitry Andric /// TBAA scheme. This means \p BaseNode is either a scalar node, or a
68080b57cec5SDimitry Andric /// struct-type node describing an aggregate data structure (like a struct).
68090b57cec5SDimitry Andric TBAAVerifier::TBAABaseNodeSummary
verifyTBAABaseNode(Instruction & I,const MDNode * BaseNode,bool IsNewFormat)68100b57cec5SDimitry Andric TBAAVerifier::verifyTBAABaseNode(Instruction &I, const MDNode *BaseNode,
68110b57cec5SDimitry Andric bool IsNewFormat) {
68120b57cec5SDimitry Andric if (BaseNode->getNumOperands() < 2) {
68130b57cec5SDimitry Andric CheckFailed("Base nodes must have at least two operands", &I, BaseNode);
68140b57cec5SDimitry Andric return {true, ~0u};
68150b57cec5SDimitry Andric }
68160b57cec5SDimitry Andric
68170b57cec5SDimitry Andric auto Itr = TBAABaseNodes.find(BaseNode);
68180b57cec5SDimitry Andric if (Itr != TBAABaseNodes.end())
68190b57cec5SDimitry Andric return Itr->second;
68200b57cec5SDimitry Andric
68210b57cec5SDimitry Andric auto Result = verifyTBAABaseNodeImpl(I, BaseNode, IsNewFormat);
68220b57cec5SDimitry Andric auto InsertResult = TBAABaseNodes.insert({BaseNode, Result});
68230b57cec5SDimitry Andric (void)InsertResult;
68240b57cec5SDimitry Andric assert(InsertResult.second && "We just checked!");
68250b57cec5SDimitry Andric return Result;
68260b57cec5SDimitry Andric }
68270b57cec5SDimitry Andric
68280b57cec5SDimitry Andric TBAAVerifier::TBAABaseNodeSummary
verifyTBAABaseNodeImpl(Instruction & I,const MDNode * BaseNode,bool IsNewFormat)68290b57cec5SDimitry Andric TBAAVerifier::verifyTBAABaseNodeImpl(Instruction &I, const MDNode *BaseNode,
68300b57cec5SDimitry Andric bool IsNewFormat) {
68310b57cec5SDimitry Andric const TBAAVerifier::TBAABaseNodeSummary InvalidNode = {true, ~0u};
68320b57cec5SDimitry Andric
68330b57cec5SDimitry Andric if (BaseNode->getNumOperands() == 2) {
68340b57cec5SDimitry Andric // Scalar nodes can only be accessed at offset 0.
68350b57cec5SDimitry Andric return isValidScalarTBAANode(BaseNode)
68360b57cec5SDimitry Andric ? TBAAVerifier::TBAABaseNodeSummary({false, 0})
68370b57cec5SDimitry Andric : InvalidNode;
68380b57cec5SDimitry Andric }
68390b57cec5SDimitry Andric
68400b57cec5SDimitry Andric if (IsNewFormat) {
68410b57cec5SDimitry Andric if (BaseNode->getNumOperands() % 3 != 0) {
68420b57cec5SDimitry Andric CheckFailed("Access tag nodes must have the number of operands that is a "
68430b57cec5SDimitry Andric "multiple of 3!", BaseNode);
68440b57cec5SDimitry Andric return InvalidNode;
68450b57cec5SDimitry Andric }
68460b57cec5SDimitry Andric } else {
68470b57cec5SDimitry Andric if (BaseNode->getNumOperands() % 2 != 1) {
68480b57cec5SDimitry Andric CheckFailed("Struct tag nodes must have an odd number of operands!",
68490b57cec5SDimitry Andric BaseNode);
68500b57cec5SDimitry Andric return InvalidNode;
68510b57cec5SDimitry Andric }
68520b57cec5SDimitry Andric }
68530b57cec5SDimitry Andric
68540b57cec5SDimitry Andric // Check the type size field.
68550b57cec5SDimitry Andric if (IsNewFormat) {
68560b57cec5SDimitry Andric auto *TypeSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
68570b57cec5SDimitry Andric BaseNode->getOperand(1));
68580b57cec5SDimitry Andric if (!TypeSizeNode) {
68590b57cec5SDimitry Andric CheckFailed("Type size nodes must be constants!", &I, BaseNode);
68600b57cec5SDimitry Andric return InvalidNode;
68610b57cec5SDimitry Andric }
68620b57cec5SDimitry Andric }
68630b57cec5SDimitry Andric
68640b57cec5SDimitry Andric // Check the type name field. In the new format it can be anything.
68650b57cec5SDimitry Andric if (!IsNewFormat && !isa<MDString>(BaseNode->getOperand(0))) {
68660b57cec5SDimitry Andric CheckFailed("Struct tag nodes have a string as their first operand",
68670b57cec5SDimitry Andric BaseNode);
68680b57cec5SDimitry Andric return InvalidNode;
68690b57cec5SDimitry Andric }
68700b57cec5SDimitry Andric
68710b57cec5SDimitry Andric bool Failed = false;
68720b57cec5SDimitry Andric
6873bdd1243dSDimitry Andric std::optional<APInt> PrevOffset;
68740b57cec5SDimitry Andric unsigned BitWidth = ~0u;
68750b57cec5SDimitry Andric
68760b57cec5SDimitry Andric // We've already checked that BaseNode is not a degenerate root node with one
68770b57cec5SDimitry Andric // operand in \c verifyTBAABaseNode, so this loop should run at least once.
68780b57cec5SDimitry Andric unsigned FirstFieldOpNo = IsNewFormat ? 3 : 1;
68790b57cec5SDimitry Andric unsigned NumOpsPerField = IsNewFormat ? 3 : 2;
68800b57cec5SDimitry Andric for (unsigned Idx = FirstFieldOpNo; Idx < BaseNode->getNumOperands();
68810b57cec5SDimitry Andric Idx += NumOpsPerField) {
68820b57cec5SDimitry Andric const MDOperand &FieldTy = BaseNode->getOperand(Idx);
68830b57cec5SDimitry Andric const MDOperand &FieldOffset = BaseNode->getOperand(Idx + 1);
68840b57cec5SDimitry Andric if (!isa<MDNode>(FieldTy)) {
68850b57cec5SDimitry Andric CheckFailed("Incorrect field entry in struct type node!", &I, BaseNode);
68860b57cec5SDimitry Andric Failed = true;
68870b57cec5SDimitry Andric continue;
68880b57cec5SDimitry Andric }
68890b57cec5SDimitry Andric
68900b57cec5SDimitry Andric auto *OffsetEntryCI =
68910b57cec5SDimitry Andric mdconst::dyn_extract_or_null<ConstantInt>(FieldOffset);
68920b57cec5SDimitry Andric if (!OffsetEntryCI) {
68930b57cec5SDimitry Andric CheckFailed("Offset entries must be constants!", &I, BaseNode);
68940b57cec5SDimitry Andric Failed = true;
68950b57cec5SDimitry Andric continue;
68960b57cec5SDimitry Andric }
68970b57cec5SDimitry Andric
68980b57cec5SDimitry Andric if (BitWidth == ~0u)
68990b57cec5SDimitry Andric BitWidth = OffsetEntryCI->getBitWidth();
69000b57cec5SDimitry Andric
69010b57cec5SDimitry Andric if (OffsetEntryCI->getBitWidth() != BitWidth) {
69020b57cec5SDimitry Andric CheckFailed(
69030b57cec5SDimitry Andric "Bitwidth between the offsets and struct type entries must match", &I,
69040b57cec5SDimitry Andric BaseNode);
69050b57cec5SDimitry Andric Failed = true;
69060b57cec5SDimitry Andric continue;
69070b57cec5SDimitry Andric }
69080b57cec5SDimitry Andric
69090b57cec5SDimitry Andric // NB! As far as I can tell, we generate a non-strictly increasing offset
69100b57cec5SDimitry Andric // sequence only from structs that have zero size bit fields. When
69110b57cec5SDimitry Andric // recursing into a contained struct in \c getFieldNodeFromTBAABaseNode we
69120b57cec5SDimitry Andric // pick the field lexically the latest in struct type metadata node. This
69130b57cec5SDimitry Andric // mirrors the actual behavior of the alias analysis implementation.
69140b57cec5SDimitry Andric bool IsAscending =
69150b57cec5SDimitry Andric !PrevOffset || PrevOffset->ule(OffsetEntryCI->getValue());
69160b57cec5SDimitry Andric
69170b57cec5SDimitry Andric if (!IsAscending) {
69180b57cec5SDimitry Andric CheckFailed("Offsets must be increasing!", &I, BaseNode);
69190b57cec5SDimitry Andric Failed = true;
69200b57cec5SDimitry Andric }
69210b57cec5SDimitry Andric
69220b57cec5SDimitry Andric PrevOffset = OffsetEntryCI->getValue();
69230b57cec5SDimitry Andric
69240b57cec5SDimitry Andric if (IsNewFormat) {
69250b57cec5SDimitry Andric auto *MemberSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
69260b57cec5SDimitry Andric BaseNode->getOperand(Idx + 2));
69270b57cec5SDimitry Andric if (!MemberSizeNode) {
69280b57cec5SDimitry Andric CheckFailed("Member size entries must be constants!", &I, BaseNode);
69290b57cec5SDimitry Andric Failed = true;
69300b57cec5SDimitry Andric continue;
69310b57cec5SDimitry Andric }
69320b57cec5SDimitry Andric }
69330b57cec5SDimitry Andric }
69340b57cec5SDimitry Andric
69350b57cec5SDimitry Andric return Failed ? InvalidNode
69360b57cec5SDimitry Andric : TBAAVerifier::TBAABaseNodeSummary(false, BitWidth);
69370b57cec5SDimitry Andric }
69380b57cec5SDimitry Andric
IsRootTBAANode(const MDNode * MD)69390b57cec5SDimitry Andric static bool IsRootTBAANode(const MDNode *MD) {
69400b57cec5SDimitry Andric return MD->getNumOperands() < 2;
69410b57cec5SDimitry Andric }
69420b57cec5SDimitry Andric
IsScalarTBAANodeImpl(const MDNode * MD,SmallPtrSetImpl<const MDNode * > & Visited)69430b57cec5SDimitry Andric static bool IsScalarTBAANodeImpl(const MDNode *MD,
69440b57cec5SDimitry Andric SmallPtrSetImpl<const MDNode *> &Visited) {
69450b57cec5SDimitry Andric if (MD->getNumOperands() != 2 && MD->getNumOperands() != 3)
69460b57cec5SDimitry Andric return false;
69470b57cec5SDimitry Andric
69480b57cec5SDimitry Andric if (!isa<MDString>(MD->getOperand(0)))
69490b57cec5SDimitry Andric return false;
69500b57cec5SDimitry Andric
69510b57cec5SDimitry Andric if (MD->getNumOperands() == 3) {
69520b57cec5SDimitry Andric auto *Offset = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
69530b57cec5SDimitry Andric if (!(Offset && Offset->isZero() && isa<MDString>(MD->getOperand(0))))
69540b57cec5SDimitry Andric return false;
69550b57cec5SDimitry Andric }
69560b57cec5SDimitry Andric
69570b57cec5SDimitry Andric auto *Parent = dyn_cast_or_null<MDNode>(MD->getOperand(1));
69580b57cec5SDimitry Andric return Parent && Visited.insert(Parent).second &&
69590b57cec5SDimitry Andric (IsRootTBAANode(Parent) || IsScalarTBAANodeImpl(Parent, Visited));
69600b57cec5SDimitry Andric }
69610b57cec5SDimitry Andric
isValidScalarTBAANode(const MDNode * MD)69620b57cec5SDimitry Andric bool TBAAVerifier::isValidScalarTBAANode(const MDNode *MD) {
69630b57cec5SDimitry Andric auto ResultIt = TBAAScalarNodes.find(MD);
69640b57cec5SDimitry Andric if (ResultIt != TBAAScalarNodes.end())
69650b57cec5SDimitry Andric return ResultIt->second;
69660b57cec5SDimitry Andric
69670b57cec5SDimitry Andric SmallPtrSet<const MDNode *, 4> Visited;
69680b57cec5SDimitry Andric bool Result = IsScalarTBAANodeImpl(MD, Visited);
69690b57cec5SDimitry Andric auto InsertResult = TBAAScalarNodes.insert({MD, Result});
69700b57cec5SDimitry Andric (void)InsertResult;
69710b57cec5SDimitry Andric assert(InsertResult.second && "Just checked!");
69720b57cec5SDimitry Andric
69730b57cec5SDimitry Andric return Result;
69740b57cec5SDimitry Andric }
69750b57cec5SDimitry Andric
69760b57cec5SDimitry Andric /// Returns the field node at the offset \p Offset in \p BaseNode. Update \p
69770b57cec5SDimitry Andric /// Offset in place to be the offset within the field node returned.
69780b57cec5SDimitry Andric ///
69790b57cec5SDimitry Andric /// We assume we've okayed \p BaseNode via \c verifyTBAABaseNode.
getFieldNodeFromTBAABaseNode(Instruction & I,const MDNode * BaseNode,APInt & Offset,bool IsNewFormat)69800b57cec5SDimitry Andric MDNode *TBAAVerifier::getFieldNodeFromTBAABaseNode(Instruction &I,
69810b57cec5SDimitry Andric const MDNode *BaseNode,
69820b57cec5SDimitry Andric APInt &Offset,
69830b57cec5SDimitry Andric bool IsNewFormat) {
69840b57cec5SDimitry Andric assert(BaseNode->getNumOperands() >= 2 && "Invalid base node!");
69850b57cec5SDimitry Andric
69860b57cec5SDimitry Andric // Scalar nodes have only one possible "field" -- their parent in the access
69870b57cec5SDimitry Andric // hierarchy. Offset must be zero at this point, but our caller is supposed
698881ad6265SDimitry Andric // to check that.
69890b57cec5SDimitry Andric if (BaseNode->getNumOperands() == 2)
69900b57cec5SDimitry Andric return cast<MDNode>(BaseNode->getOperand(1));
69910b57cec5SDimitry Andric
69920b57cec5SDimitry Andric unsigned FirstFieldOpNo = IsNewFormat ? 3 : 1;
69930b57cec5SDimitry Andric unsigned NumOpsPerField = IsNewFormat ? 3 : 2;
69940b57cec5SDimitry Andric for (unsigned Idx = FirstFieldOpNo; Idx < BaseNode->getNumOperands();
69950b57cec5SDimitry Andric Idx += NumOpsPerField) {
69960b57cec5SDimitry Andric auto *OffsetEntryCI =
69970b57cec5SDimitry Andric mdconst::extract<ConstantInt>(BaseNode->getOperand(Idx + 1));
69980b57cec5SDimitry Andric if (OffsetEntryCI->getValue().ugt(Offset)) {
69990b57cec5SDimitry Andric if (Idx == FirstFieldOpNo) {
70000b57cec5SDimitry Andric CheckFailed("Could not find TBAA parent in struct type node", &I,
70010b57cec5SDimitry Andric BaseNode, &Offset);
70020b57cec5SDimitry Andric return nullptr;
70030b57cec5SDimitry Andric }
70040b57cec5SDimitry Andric
70050b57cec5SDimitry Andric unsigned PrevIdx = Idx - NumOpsPerField;
70060b57cec5SDimitry Andric auto *PrevOffsetEntryCI =
70070b57cec5SDimitry Andric mdconst::extract<ConstantInt>(BaseNode->getOperand(PrevIdx + 1));
70080b57cec5SDimitry Andric Offset -= PrevOffsetEntryCI->getValue();
70090b57cec5SDimitry Andric return cast<MDNode>(BaseNode->getOperand(PrevIdx));
70100b57cec5SDimitry Andric }
70110b57cec5SDimitry Andric }
70120b57cec5SDimitry Andric
70130b57cec5SDimitry Andric unsigned LastIdx = BaseNode->getNumOperands() - NumOpsPerField;
70140b57cec5SDimitry Andric auto *LastOffsetEntryCI = mdconst::extract<ConstantInt>(
70150b57cec5SDimitry Andric BaseNode->getOperand(LastIdx + 1));
70160b57cec5SDimitry Andric Offset -= LastOffsetEntryCI->getValue();
70170b57cec5SDimitry Andric return cast<MDNode>(BaseNode->getOperand(LastIdx));
70180b57cec5SDimitry Andric }
70190b57cec5SDimitry Andric
isNewFormatTBAATypeNode(llvm::MDNode * Type)70200b57cec5SDimitry Andric static bool isNewFormatTBAATypeNode(llvm::MDNode *Type) {
70210b57cec5SDimitry Andric if (!Type || Type->getNumOperands() < 3)
70220b57cec5SDimitry Andric return false;
70230b57cec5SDimitry Andric
70240b57cec5SDimitry Andric // In the new format type nodes shall have a reference to the parent type as
70250b57cec5SDimitry Andric // its first operand.
7026349cc55cSDimitry Andric return isa_and_nonnull<MDNode>(Type->getOperand(0));
70270b57cec5SDimitry Andric }
70280b57cec5SDimitry Andric
visitTBAAMetadata(Instruction & I,const MDNode * MD)70290b57cec5SDimitry Andric bool TBAAVerifier::visitTBAAMetadata(Instruction &I, const MDNode *MD) {
703006c3fb27SDimitry Andric CheckTBAA(MD->getNumOperands() > 0, "TBAA metadata cannot have 0 operands",
703106c3fb27SDimitry Andric &I, MD);
703206c3fb27SDimitry Andric
703381ad6265SDimitry Andric CheckTBAA(isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallInst>(I) ||
70340b57cec5SDimitry Andric isa<VAArgInst>(I) || isa<AtomicRMWInst>(I) ||
70350b57cec5SDimitry Andric isa<AtomicCmpXchgInst>(I),
70360b57cec5SDimitry Andric "This instruction shall not have a TBAA access tag!", &I);
70370b57cec5SDimitry Andric
70380b57cec5SDimitry Andric bool IsStructPathTBAA =
70390b57cec5SDimitry Andric isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3;
70400b57cec5SDimitry Andric
704181ad6265SDimitry Andric CheckTBAA(IsStructPathTBAA,
704281ad6265SDimitry Andric "Old-style TBAA is no longer allowed, use struct-path TBAA instead",
704381ad6265SDimitry Andric &I);
70440b57cec5SDimitry Andric
70450b57cec5SDimitry Andric MDNode *BaseNode = dyn_cast_or_null<MDNode>(MD->getOperand(0));
70460b57cec5SDimitry Andric MDNode *AccessType = dyn_cast_or_null<MDNode>(MD->getOperand(1));
70470b57cec5SDimitry Andric
70480b57cec5SDimitry Andric bool IsNewFormat = isNewFormatTBAATypeNode(AccessType);
70490b57cec5SDimitry Andric
70500b57cec5SDimitry Andric if (IsNewFormat) {
705181ad6265SDimitry Andric CheckTBAA(MD->getNumOperands() == 4 || MD->getNumOperands() == 5,
70520b57cec5SDimitry Andric "Access tag metadata must have either 4 or 5 operands", &I, MD);
70530b57cec5SDimitry Andric } else {
705481ad6265SDimitry Andric CheckTBAA(MD->getNumOperands() < 5,
70550b57cec5SDimitry Andric "Struct tag metadata must have either 3 or 4 operands", &I, MD);
70560b57cec5SDimitry Andric }
70570b57cec5SDimitry Andric
70580b57cec5SDimitry Andric // Check the access size field.
70590b57cec5SDimitry Andric if (IsNewFormat) {
70600b57cec5SDimitry Andric auto *AccessSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
70610b57cec5SDimitry Andric MD->getOperand(3));
706281ad6265SDimitry Andric CheckTBAA(AccessSizeNode, "Access size field must be a constant", &I, MD);
70630b57cec5SDimitry Andric }
70640b57cec5SDimitry Andric
70650b57cec5SDimitry Andric // Check the immutability flag.
70660b57cec5SDimitry Andric unsigned ImmutabilityFlagOpNo = IsNewFormat ? 4 : 3;
70670b57cec5SDimitry Andric if (MD->getNumOperands() == ImmutabilityFlagOpNo + 1) {
70680b57cec5SDimitry Andric auto *IsImmutableCI = mdconst::dyn_extract_or_null<ConstantInt>(
70690b57cec5SDimitry Andric MD->getOperand(ImmutabilityFlagOpNo));
707081ad6265SDimitry Andric CheckTBAA(IsImmutableCI,
707181ad6265SDimitry Andric "Immutability tag on struct tag metadata must be a constant", &I,
707281ad6265SDimitry Andric MD);
707381ad6265SDimitry Andric CheckTBAA(
70740b57cec5SDimitry Andric IsImmutableCI->isZero() || IsImmutableCI->isOne(),
70750b57cec5SDimitry Andric "Immutability part of the struct tag metadata must be either 0 or 1",
70760b57cec5SDimitry Andric &I, MD);
70770b57cec5SDimitry Andric }
70780b57cec5SDimitry Andric
707981ad6265SDimitry Andric CheckTBAA(BaseNode && AccessType,
70800b57cec5SDimitry Andric "Malformed struct tag metadata: base and access-type "
70810b57cec5SDimitry Andric "should be non-null and point to Metadata nodes",
70820b57cec5SDimitry Andric &I, MD, BaseNode, AccessType);
70830b57cec5SDimitry Andric
70840b57cec5SDimitry Andric if (!IsNewFormat) {
708581ad6265SDimitry Andric CheckTBAA(isValidScalarTBAANode(AccessType),
70860b57cec5SDimitry Andric "Access type node must be a valid scalar type", &I, MD,
70870b57cec5SDimitry Andric AccessType);
70880b57cec5SDimitry Andric }
70890b57cec5SDimitry Andric
70900b57cec5SDimitry Andric auto *OffsetCI = mdconst::dyn_extract_or_null<ConstantInt>(MD->getOperand(2));
709181ad6265SDimitry Andric CheckTBAA(OffsetCI, "Offset must be constant integer", &I, MD);
70920b57cec5SDimitry Andric
70930b57cec5SDimitry Andric APInt Offset = OffsetCI->getValue();
70940b57cec5SDimitry Andric bool SeenAccessTypeInPath = false;
70950b57cec5SDimitry Andric
70960b57cec5SDimitry Andric SmallPtrSet<MDNode *, 4> StructPath;
70970b57cec5SDimitry Andric
70980b57cec5SDimitry Andric for (/* empty */; BaseNode && !IsRootTBAANode(BaseNode);
70990b57cec5SDimitry Andric BaseNode = getFieldNodeFromTBAABaseNode(I, BaseNode, Offset,
71000b57cec5SDimitry Andric IsNewFormat)) {
71010b57cec5SDimitry Andric if (!StructPath.insert(BaseNode).second) {
71020b57cec5SDimitry Andric CheckFailed("Cycle detected in struct path", &I, MD);
71030b57cec5SDimitry Andric return false;
71040b57cec5SDimitry Andric }
71050b57cec5SDimitry Andric
71060b57cec5SDimitry Andric bool Invalid;
71070b57cec5SDimitry Andric unsigned BaseNodeBitWidth;
71080b57cec5SDimitry Andric std::tie(Invalid, BaseNodeBitWidth) = verifyTBAABaseNode(I, BaseNode,
71090b57cec5SDimitry Andric IsNewFormat);
71100b57cec5SDimitry Andric
71110b57cec5SDimitry Andric // If the base node is invalid in itself, then we've already printed all the
71120b57cec5SDimitry Andric // errors we wanted to print.
71130b57cec5SDimitry Andric if (Invalid)
71140b57cec5SDimitry Andric return false;
71150b57cec5SDimitry Andric
71160b57cec5SDimitry Andric SeenAccessTypeInPath |= BaseNode == AccessType;
71170b57cec5SDimitry Andric
71180b57cec5SDimitry Andric if (isValidScalarTBAANode(BaseNode) || BaseNode == AccessType)
711981ad6265SDimitry Andric CheckTBAA(Offset == 0, "Offset not zero at the point of scalar access",
71200b57cec5SDimitry Andric &I, MD, &Offset);
71210b57cec5SDimitry Andric
712281ad6265SDimitry Andric CheckTBAA(BaseNodeBitWidth == Offset.getBitWidth() ||
71230b57cec5SDimitry Andric (BaseNodeBitWidth == 0 && Offset == 0) ||
71240b57cec5SDimitry Andric (IsNewFormat && BaseNodeBitWidth == ~0u),
71250b57cec5SDimitry Andric "Access bit-width not the same as description bit-width", &I, MD,
71260b57cec5SDimitry Andric BaseNodeBitWidth, Offset.getBitWidth());
71270b57cec5SDimitry Andric
71280b57cec5SDimitry Andric if (IsNewFormat && SeenAccessTypeInPath)
71290b57cec5SDimitry Andric break;
71300b57cec5SDimitry Andric }
71310b57cec5SDimitry Andric
713281ad6265SDimitry Andric CheckTBAA(SeenAccessTypeInPath, "Did not see access type in access path!", &I,
713381ad6265SDimitry Andric MD);
71340b57cec5SDimitry Andric return true;
71350b57cec5SDimitry Andric }
71360b57cec5SDimitry Andric
71370b57cec5SDimitry Andric char VerifierLegacyPass::ID = 0;
71380b57cec5SDimitry Andric INITIALIZE_PASS(VerifierLegacyPass, "verify", "Module Verifier", false, false)
71390b57cec5SDimitry Andric
createVerifierPass(bool FatalErrors)71400b57cec5SDimitry Andric FunctionPass *llvm::createVerifierPass(bool FatalErrors) {
71410b57cec5SDimitry Andric return new VerifierLegacyPass(FatalErrors);
71420b57cec5SDimitry Andric }
71430b57cec5SDimitry Andric
71440b57cec5SDimitry Andric AnalysisKey VerifierAnalysis::Key;
run(Module & M,ModuleAnalysisManager &)71450b57cec5SDimitry Andric VerifierAnalysis::Result VerifierAnalysis::run(Module &M,
71460b57cec5SDimitry Andric ModuleAnalysisManager &) {
71470b57cec5SDimitry Andric Result Res;
71480b57cec5SDimitry Andric Res.IRBroken = llvm::verifyModule(M, &dbgs(), &Res.DebugInfoBroken);
71490b57cec5SDimitry Andric return Res;
71500b57cec5SDimitry Andric }
71510b57cec5SDimitry Andric
run(Function & F,FunctionAnalysisManager &)71520b57cec5SDimitry Andric VerifierAnalysis::Result VerifierAnalysis::run(Function &F,
71530b57cec5SDimitry Andric FunctionAnalysisManager &) {
71540b57cec5SDimitry Andric return { llvm::verifyFunction(F, &dbgs()), false };
71550b57cec5SDimitry Andric }
71560b57cec5SDimitry Andric
run(Module & M,ModuleAnalysisManager & AM)71570b57cec5SDimitry Andric PreservedAnalyses VerifierPass::run(Module &M, ModuleAnalysisManager &AM) {
71580b57cec5SDimitry Andric auto Res = AM.getResult<VerifierAnalysis>(M);
71590b57cec5SDimitry Andric if (FatalErrors && (Res.IRBroken || Res.DebugInfoBroken))
71600b57cec5SDimitry Andric report_fatal_error("Broken module found, compilation aborted!");
71610b57cec5SDimitry Andric
71620b57cec5SDimitry Andric return PreservedAnalyses::all();
71630b57cec5SDimitry Andric }
71640b57cec5SDimitry Andric
run(Function & F,FunctionAnalysisManager & AM)71650b57cec5SDimitry Andric PreservedAnalyses VerifierPass::run(Function &F, FunctionAnalysisManager &AM) {
71660b57cec5SDimitry Andric auto res = AM.getResult<VerifierAnalysis>(F);
71670b57cec5SDimitry Andric if (res.IRBroken && FatalErrors)
71680b57cec5SDimitry Andric report_fatal_error("Broken function found, compilation aborted!");
71690b57cec5SDimitry Andric
71700b57cec5SDimitry Andric return PreservedAnalyses::all();
71710b57cec5SDimitry Andric }
7172