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 // * PHI nodes must have at least one entry 270b57cec5SDimitry Andric // * All basic blocks should only end with terminator insts, not contain them 280b57cec5SDimitry Andric // * The entry node to a function must not have predecessors 290b57cec5SDimitry Andric // * All Instructions must be embedded into a basic block 300b57cec5SDimitry Andric // * Functions cannot take a void-typed parameter 310b57cec5SDimitry Andric // * Verify that a function's argument list agrees with it's declared type. 320b57cec5SDimitry Andric // * It is illegal to specify a name for a void value. 330b57cec5SDimitry Andric // * It is illegal to have a internal global value with no initializer 340b57cec5SDimitry Andric // * It is illegal to have a ret instruction that returns a value that does not 350b57cec5SDimitry Andric // agree with the function return value type. 360b57cec5SDimitry Andric // * Function call argument types match the function prototype 370b57cec5SDimitry Andric // * A landing pad is defined by a landingpad instruction, and can be jumped to 380b57cec5SDimitry Andric // only by the unwind edge of an invoke instruction. 390b57cec5SDimitry Andric // * A landingpad instruction must be the first non-PHI instruction in the 400b57cec5SDimitry Andric // block. 410b57cec5SDimitry Andric // * Landingpad instructions must be in a function with a personality function. 420b57cec5SDimitry Andric // * All other things that are tested by asserts spread about the code... 430b57cec5SDimitry Andric // 440b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric #include "llvm/IR/Verifier.h" 470b57cec5SDimitry Andric #include "llvm/ADT/APFloat.h" 480b57cec5SDimitry Andric #include "llvm/ADT/APInt.h" 490b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 500b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 510b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h" 520b57cec5SDimitry Andric #include "llvm/ADT/Optional.h" 530b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 540b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 550b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h" 560b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 570b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 580b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h" 590b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 600b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 610b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 620b57cec5SDimitry Andric #include "llvm/IR/Argument.h" 630b57cec5SDimitry Andric #include "llvm/IR/Attributes.h" 640b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 650b57cec5SDimitry Andric #include "llvm/IR/CFG.h" 660b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h" 670b57cec5SDimitry Andric #include "llvm/IR/Comdat.h" 680b57cec5SDimitry Andric #include "llvm/IR/Constant.h" 690b57cec5SDimitry Andric #include "llvm/IR/ConstantRange.h" 700b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 710b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 720b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 730b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h" 740b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 750b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 760b57cec5SDimitry Andric #include "llvm/IR/Function.h" 770b57cec5SDimitry Andric #include "llvm/IR/GlobalAlias.h" 780b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 790b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 800b57cec5SDimitry Andric #include "llvm/IR/InlineAsm.h" 810b57cec5SDimitry Andric #include "llvm/IR/InstVisitor.h" 820b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h" 830b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 840b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 850b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 860b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h" 87*81ad6265SDimitry Andric #include "llvm/IR/IntrinsicsAArch64.h" 88*81ad6265SDimitry Andric #include "llvm/IR/IntrinsicsARM.h" 89480093f4SDimitry Andric #include "llvm/IR/IntrinsicsWebAssembly.h" 900b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 910b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 920b57cec5SDimitry Andric #include "llvm/IR/Module.h" 930b57cec5SDimitry Andric #include "llvm/IR/ModuleSlotTracker.h" 940b57cec5SDimitry Andric #include "llvm/IR/PassManager.h" 950b57cec5SDimitry Andric #include "llvm/IR/Statepoint.h" 960b57cec5SDimitry Andric #include "llvm/IR/Type.h" 970b57cec5SDimitry Andric #include "llvm/IR/Use.h" 980b57cec5SDimitry Andric #include "llvm/IR/User.h" 990b57cec5SDimitry Andric #include "llvm/IR/Value.h" 100480093f4SDimitry Andric #include "llvm/InitializePasses.h" 1010b57cec5SDimitry Andric #include "llvm/Pass.h" 1020b57cec5SDimitry Andric #include "llvm/Support/AtomicOrdering.h" 1030b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 1040b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 1050b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 1060b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 1070b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 1080b57cec5SDimitry Andric #include <algorithm> 1090b57cec5SDimitry Andric #include <cassert> 1100b57cec5SDimitry Andric #include <cstdint> 1110b57cec5SDimitry Andric #include <memory> 1120b57cec5SDimitry Andric #include <string> 1130b57cec5SDimitry Andric #include <utility> 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric using namespace llvm; 1160b57cec5SDimitry Andric 117e8d8bef9SDimitry Andric static cl::opt<bool> VerifyNoAliasScopeDomination( 118e8d8bef9SDimitry Andric "verify-noalias-scope-decl-dom", cl::Hidden, cl::init(false), 119e8d8bef9SDimitry Andric cl::desc("Ensure that llvm.experimental.noalias.scope.decl for identical " 120e8d8bef9SDimitry Andric "scopes are not dominating")); 121e8d8bef9SDimitry Andric 1220b57cec5SDimitry Andric namespace llvm { 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric struct VerifierSupport { 1250b57cec5SDimitry Andric raw_ostream *OS; 1260b57cec5SDimitry Andric const Module &M; 1270b57cec5SDimitry Andric ModuleSlotTracker MST; 1288bcb0991SDimitry Andric Triple TT; 1290b57cec5SDimitry Andric const DataLayout &DL; 1300b57cec5SDimitry Andric LLVMContext &Context; 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric /// Track the brokenness of the module while recursively visiting. 1330b57cec5SDimitry Andric bool Broken = false; 1340b57cec5SDimitry Andric /// Broken debug info can be "recovered" from by stripping the debug info. 1350b57cec5SDimitry Andric bool BrokenDebugInfo = false; 1360b57cec5SDimitry Andric /// Whether to treat broken debug info as an error. 1370b57cec5SDimitry Andric bool TreatBrokenDebugInfoAsError = true; 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric explicit VerifierSupport(raw_ostream *OS, const Module &M) 1408bcb0991SDimitry Andric : OS(OS), M(M), MST(&M), TT(M.getTargetTriple()), DL(M.getDataLayout()), 1418bcb0991SDimitry Andric Context(M.getContext()) {} 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric private: 1440b57cec5SDimitry Andric void Write(const Module *M) { 1450b57cec5SDimitry Andric *OS << "; ModuleID = '" << M->getModuleIdentifier() << "'\n"; 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric void Write(const Value *V) { 1490b57cec5SDimitry Andric if (V) 1500b57cec5SDimitry Andric Write(*V); 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric void Write(const Value &V) { 1540b57cec5SDimitry Andric if (isa<Instruction>(V)) { 1550b57cec5SDimitry Andric V.print(*OS, MST); 1560b57cec5SDimitry Andric *OS << '\n'; 1570b57cec5SDimitry Andric } else { 1580b57cec5SDimitry Andric V.printAsOperand(*OS, true, MST); 1590b57cec5SDimitry Andric *OS << '\n'; 1600b57cec5SDimitry Andric } 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric void Write(const Metadata *MD) { 1640b57cec5SDimitry Andric if (!MD) 1650b57cec5SDimitry Andric return; 1660b57cec5SDimitry Andric MD->print(*OS, MST, &M); 1670b57cec5SDimitry Andric *OS << '\n'; 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric template <class T> void Write(const MDTupleTypedArrayWrapper<T> &MD) { 1710b57cec5SDimitry Andric Write(MD.get()); 1720b57cec5SDimitry Andric } 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric void Write(const NamedMDNode *NMD) { 1750b57cec5SDimitry Andric if (!NMD) 1760b57cec5SDimitry Andric return; 1770b57cec5SDimitry Andric NMD->print(*OS, MST); 1780b57cec5SDimitry Andric *OS << '\n'; 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric void Write(Type *T) { 1820b57cec5SDimitry Andric if (!T) 1830b57cec5SDimitry Andric return; 1840b57cec5SDimitry Andric *OS << ' ' << *T; 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric void Write(const Comdat *C) { 1880b57cec5SDimitry Andric if (!C) 1890b57cec5SDimitry Andric return; 1900b57cec5SDimitry Andric *OS << *C; 1910b57cec5SDimitry Andric } 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric void Write(const APInt *AI) { 1940b57cec5SDimitry Andric if (!AI) 1950b57cec5SDimitry Andric return; 1960b57cec5SDimitry Andric *OS << *AI << '\n'; 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric void Write(const unsigned i) { *OS << i << '\n'; } 2000b57cec5SDimitry Andric 201fe6060f1SDimitry Andric // NOLINTNEXTLINE(readability-identifier-naming) 202fe6060f1SDimitry Andric void Write(const Attribute *A) { 203fe6060f1SDimitry Andric if (!A) 204fe6060f1SDimitry Andric return; 205fe6060f1SDimitry Andric *OS << A->getAsString() << '\n'; 206fe6060f1SDimitry Andric } 207fe6060f1SDimitry Andric 208fe6060f1SDimitry Andric // NOLINTNEXTLINE(readability-identifier-naming) 209fe6060f1SDimitry Andric void Write(const AttributeSet *AS) { 210fe6060f1SDimitry Andric if (!AS) 211fe6060f1SDimitry Andric return; 212fe6060f1SDimitry Andric *OS << AS->getAsString() << '\n'; 213fe6060f1SDimitry Andric } 214fe6060f1SDimitry Andric 215fe6060f1SDimitry Andric // NOLINTNEXTLINE(readability-identifier-naming) 216fe6060f1SDimitry Andric void Write(const AttributeList *AL) { 217fe6060f1SDimitry Andric if (!AL) 218fe6060f1SDimitry Andric return; 219fe6060f1SDimitry Andric AL->print(*OS); 220fe6060f1SDimitry Andric } 221fe6060f1SDimitry Andric 2220b57cec5SDimitry Andric template <typename T> void Write(ArrayRef<T> Vs) { 2230b57cec5SDimitry Andric for (const T &V : Vs) 2240b57cec5SDimitry Andric Write(V); 2250b57cec5SDimitry Andric } 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric template <typename T1, typename... Ts> 2280b57cec5SDimitry Andric void WriteTs(const T1 &V1, const Ts &... Vs) { 2290b57cec5SDimitry Andric Write(V1); 2300b57cec5SDimitry Andric WriteTs(Vs...); 2310b57cec5SDimitry Andric } 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric template <typename... Ts> void WriteTs() {} 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric public: 2360b57cec5SDimitry Andric /// A check failed, so printout out the condition and the message. 2370b57cec5SDimitry Andric /// 2380b57cec5SDimitry Andric /// This provides a nice place to put a breakpoint if you want to see why 2390b57cec5SDimitry Andric /// something is not correct. 2400b57cec5SDimitry Andric void CheckFailed(const Twine &Message) { 2410b57cec5SDimitry Andric if (OS) 2420b57cec5SDimitry Andric *OS << Message << '\n'; 2430b57cec5SDimitry Andric Broken = true; 2440b57cec5SDimitry Andric } 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric /// A check failed (with values to print). 2470b57cec5SDimitry Andric /// 2480b57cec5SDimitry Andric /// This calls the Message-only version so that the above is easier to set a 2490b57cec5SDimitry Andric /// breakpoint on. 2500b57cec5SDimitry Andric template <typename T1, typename... Ts> 2510b57cec5SDimitry Andric void CheckFailed(const Twine &Message, const T1 &V1, const Ts &... Vs) { 2520b57cec5SDimitry Andric CheckFailed(Message); 2530b57cec5SDimitry Andric if (OS) 2540b57cec5SDimitry Andric WriteTs(V1, Vs...); 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric /// A debug info check failed. 2580b57cec5SDimitry Andric void DebugInfoCheckFailed(const Twine &Message) { 2590b57cec5SDimitry Andric if (OS) 2600b57cec5SDimitry Andric *OS << Message << '\n'; 2610b57cec5SDimitry Andric Broken |= TreatBrokenDebugInfoAsError; 2620b57cec5SDimitry Andric BrokenDebugInfo = true; 2630b57cec5SDimitry Andric } 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric /// A debug info check failed (with values to print). 2660b57cec5SDimitry Andric template <typename T1, typename... Ts> 2670b57cec5SDimitry Andric void DebugInfoCheckFailed(const Twine &Message, const T1 &V1, 2680b57cec5SDimitry Andric const Ts &... Vs) { 2690b57cec5SDimitry Andric DebugInfoCheckFailed(Message); 2700b57cec5SDimitry Andric if (OS) 2710b57cec5SDimitry Andric WriteTs(V1, Vs...); 2720b57cec5SDimitry Andric } 2730b57cec5SDimitry Andric }; 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric } // namespace llvm 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric namespace { 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric class Verifier : public InstVisitor<Verifier>, VerifierSupport { 2800b57cec5SDimitry Andric friend class InstVisitor<Verifier>; 2810b57cec5SDimitry Andric 282*81ad6265SDimitry Andric // ISD::ArgFlagsTy::MemAlign only have 4 bits for alignment, so 283*81ad6265SDimitry Andric // the alignment size should not exceed 2^15. Since encode(Align) 284*81ad6265SDimitry Andric // would plus the shift value by 1, the alignment size should 285*81ad6265SDimitry Andric // not exceed 2^14, otherwise it can NOT be properly lowered 286*81ad6265SDimitry Andric // in backend. 287*81ad6265SDimitry Andric static constexpr unsigned ParamMaxAlignment = 1 << 14; 2880b57cec5SDimitry Andric DominatorTree DT; 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric /// When verifying a basic block, keep track of all of the 2910b57cec5SDimitry Andric /// instructions we have seen so far. 2920b57cec5SDimitry Andric /// 2930b57cec5SDimitry Andric /// This allows us to do efficient dominance checks for the case when an 2940b57cec5SDimitry Andric /// instruction has an operand that is an instruction in the same block. 2950b57cec5SDimitry Andric SmallPtrSet<Instruction *, 16> InstsInThisBlock; 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andric /// Keep track of the metadata nodes that have been checked already. 2980b57cec5SDimitry Andric SmallPtrSet<const Metadata *, 32> MDNodes; 2990b57cec5SDimitry Andric 3000b57cec5SDimitry Andric /// Keep track which DISubprogram is attached to which function. 3010b57cec5SDimitry Andric DenseMap<const DISubprogram *, const Function *> DISubprogramAttachments; 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric /// Track all DICompileUnits visited. 3040b57cec5SDimitry Andric SmallPtrSet<const Metadata *, 2> CUVisited; 3050b57cec5SDimitry Andric 3060b57cec5SDimitry Andric /// The result type for a landingpad. 3070b57cec5SDimitry Andric Type *LandingPadResultTy; 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric /// Whether we've seen a call to @llvm.localescape in this function 3100b57cec5SDimitry Andric /// already. 3110b57cec5SDimitry Andric bool SawFrameEscape; 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric /// Whether the current function has a DISubprogram attached to it. 3140b57cec5SDimitry Andric bool HasDebugInfo = false; 3150b57cec5SDimitry Andric 316e8d8bef9SDimitry Andric /// The current source language. 317e8d8bef9SDimitry Andric dwarf::SourceLanguage CurrentSourceLang = dwarf::DW_LANG_lo_user; 318e8d8bef9SDimitry Andric 3190b57cec5SDimitry Andric /// Whether source was present on the first DIFile encountered in each CU. 3200b57cec5SDimitry Andric DenseMap<const DICompileUnit *, bool> HasSourceDebugInfo; 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric /// Stores the count of how many objects were passed to llvm.localescape for a 3230b57cec5SDimitry Andric /// given function and the largest index passed to llvm.localrecover. 3240b57cec5SDimitry Andric DenseMap<Function *, std::pair<unsigned, unsigned>> FrameEscapeInfo; 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric // Maps catchswitches and cleanuppads that unwind to siblings to the 3270b57cec5SDimitry Andric // terminators that indicate the unwind, used to detect cycles therein. 3280b57cec5SDimitry Andric MapVector<Instruction *, Instruction *> SiblingFuncletInfo; 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric /// Cache of constants visited in search of ConstantExprs. 3310b57cec5SDimitry Andric SmallPtrSet<const Constant *, 32> ConstantExprVisited; 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric /// Cache of declarations of the llvm.experimental.deoptimize.<ty> intrinsic. 3340b57cec5SDimitry Andric SmallVector<const Function *, 4> DeoptimizeDeclarations; 3350b57cec5SDimitry Andric 336fe6060f1SDimitry Andric /// Cache of attribute lists verified. 337fe6060f1SDimitry Andric SmallPtrSet<const void *, 32> AttributeListsVisited; 338fe6060f1SDimitry Andric 3390b57cec5SDimitry Andric // Verify that this GlobalValue is only used in this module. 3400b57cec5SDimitry Andric // This map is used to avoid visiting uses twice. We can arrive at a user 3410b57cec5SDimitry Andric // twice, if they have multiple operands. In particular for very large 3420b57cec5SDimitry Andric // constant expressions, we can arrive at a particular user many times. 3430b57cec5SDimitry Andric SmallPtrSet<const Value *, 32> GlobalValueVisited; 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric // Keeps track of duplicate function argument debug info. 3460b57cec5SDimitry Andric SmallVector<const DILocalVariable *, 16> DebugFnArgs; 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric TBAAVerifier TBAAVerifyHelper; 3490b57cec5SDimitry Andric 350e8d8bef9SDimitry Andric SmallVector<IntrinsicInst *, 4> NoAliasScopeDecls; 351e8d8bef9SDimitry Andric 3520b57cec5SDimitry Andric void checkAtomicMemAccessSize(Type *Ty, const Instruction *I); 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric public: 3550b57cec5SDimitry Andric explicit Verifier(raw_ostream *OS, bool ShouldTreatBrokenDebugInfoAsError, 3560b57cec5SDimitry Andric const Module &M) 3570b57cec5SDimitry Andric : VerifierSupport(OS, M), LandingPadResultTy(nullptr), 3580b57cec5SDimitry Andric SawFrameEscape(false), TBAAVerifyHelper(this) { 3590b57cec5SDimitry Andric TreatBrokenDebugInfoAsError = ShouldTreatBrokenDebugInfoAsError; 3600b57cec5SDimitry Andric } 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric bool hasBrokenDebugInfo() const { return BrokenDebugInfo; } 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andric bool verify(const Function &F) { 3650b57cec5SDimitry Andric assert(F.getParent() == &M && 3660b57cec5SDimitry Andric "An instance of this class only works with a specific module!"); 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric // First ensure the function is well-enough formed to compute dominance 3690b57cec5SDimitry Andric // information, and directly compute a dominance tree. We don't rely on the 3700b57cec5SDimitry Andric // pass manager to provide this as it isolates us from a potentially 3710b57cec5SDimitry Andric // out-of-date dominator tree and makes it significantly more complex to run 3720b57cec5SDimitry Andric // this code outside of a pass manager. 3730b57cec5SDimitry Andric // FIXME: It's really gross that we have to cast away constness here. 3740b57cec5SDimitry Andric if (!F.empty()) 3750b57cec5SDimitry Andric DT.recalculate(const_cast<Function &>(F)); 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric for (const BasicBlock &BB : F) { 3780b57cec5SDimitry Andric if (!BB.empty() && BB.back().isTerminator()) 3790b57cec5SDimitry Andric continue; 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric if (OS) { 3820b57cec5SDimitry Andric *OS << "Basic Block in function '" << F.getName() 3830b57cec5SDimitry Andric << "' does not have terminator!\n"; 3840b57cec5SDimitry Andric BB.printAsOperand(*OS, true, MST); 3850b57cec5SDimitry Andric *OS << "\n"; 3860b57cec5SDimitry Andric } 3870b57cec5SDimitry Andric return false; 3880b57cec5SDimitry Andric } 3890b57cec5SDimitry Andric 3900b57cec5SDimitry Andric Broken = false; 3910b57cec5SDimitry Andric // FIXME: We strip const here because the inst visitor strips const. 3920b57cec5SDimitry Andric visit(const_cast<Function &>(F)); 3930b57cec5SDimitry Andric verifySiblingFuncletUnwinds(); 3940b57cec5SDimitry Andric InstsInThisBlock.clear(); 3950b57cec5SDimitry Andric DebugFnArgs.clear(); 3960b57cec5SDimitry Andric LandingPadResultTy = nullptr; 3970b57cec5SDimitry Andric SawFrameEscape = false; 3980b57cec5SDimitry Andric SiblingFuncletInfo.clear(); 399e8d8bef9SDimitry Andric verifyNoAliasScopeDecl(); 400e8d8bef9SDimitry Andric NoAliasScopeDecls.clear(); 4010b57cec5SDimitry Andric 4020b57cec5SDimitry Andric return !Broken; 4030b57cec5SDimitry Andric } 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric /// Verify the module that this instance of \c Verifier was initialized with. 4060b57cec5SDimitry Andric bool verify() { 4070b57cec5SDimitry Andric Broken = false; 4080b57cec5SDimitry Andric 4090b57cec5SDimitry Andric // Collect all declarations of the llvm.experimental.deoptimize intrinsic. 4100b57cec5SDimitry Andric for (const Function &F : M) 4110b57cec5SDimitry Andric if (F.getIntrinsicID() == Intrinsic::experimental_deoptimize) 4120b57cec5SDimitry Andric DeoptimizeDeclarations.push_back(&F); 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric // Now that we've visited every function, verify that we never asked to 4150b57cec5SDimitry Andric // recover a frame index that wasn't escaped. 4160b57cec5SDimitry Andric verifyFrameRecoverIndices(); 4170b57cec5SDimitry Andric for (const GlobalVariable &GV : M.globals()) 4180b57cec5SDimitry Andric visitGlobalVariable(GV); 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric for (const GlobalAlias &GA : M.aliases()) 4210b57cec5SDimitry Andric visitGlobalAlias(GA); 4220b57cec5SDimitry Andric 423349cc55cSDimitry Andric for (const GlobalIFunc &GI : M.ifuncs()) 424349cc55cSDimitry Andric visitGlobalIFunc(GI); 425349cc55cSDimitry Andric 4260b57cec5SDimitry Andric for (const NamedMDNode &NMD : M.named_metadata()) 4270b57cec5SDimitry Andric visitNamedMDNode(NMD); 4280b57cec5SDimitry Andric 4290b57cec5SDimitry Andric for (const StringMapEntry<Comdat> &SMEC : M.getComdatSymbolTable()) 4300b57cec5SDimitry Andric visitComdat(SMEC.getValue()); 4310b57cec5SDimitry Andric 432349cc55cSDimitry Andric visitModuleFlags(); 433349cc55cSDimitry Andric visitModuleIdents(); 434349cc55cSDimitry Andric visitModuleCommandLines(); 4350b57cec5SDimitry Andric 4360b57cec5SDimitry Andric verifyCompileUnits(); 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric verifyDeoptimizeCallingConvs(); 4390b57cec5SDimitry Andric DISubprogramAttachments.clear(); 4400b57cec5SDimitry Andric return !Broken; 4410b57cec5SDimitry Andric } 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric private: 4445ffd83dbSDimitry Andric /// Whether a metadata node is allowed to be, or contain, a DILocation. 4455ffd83dbSDimitry Andric enum class AreDebugLocsAllowed { No, Yes }; 4465ffd83dbSDimitry Andric 4470b57cec5SDimitry Andric // Verification methods... 4480b57cec5SDimitry Andric void visitGlobalValue(const GlobalValue &GV); 4490b57cec5SDimitry Andric void visitGlobalVariable(const GlobalVariable &GV); 4500b57cec5SDimitry Andric void visitGlobalAlias(const GlobalAlias &GA); 451349cc55cSDimitry Andric void visitGlobalIFunc(const GlobalIFunc &GI); 4520b57cec5SDimitry Andric void visitAliaseeSubExpr(const GlobalAlias &A, const Constant &C); 4530b57cec5SDimitry Andric void visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias *> &Visited, 4540b57cec5SDimitry Andric const GlobalAlias &A, const Constant &C); 4550b57cec5SDimitry Andric void visitNamedMDNode(const NamedMDNode &NMD); 4565ffd83dbSDimitry Andric void visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs); 4570b57cec5SDimitry Andric void visitMetadataAsValue(const MetadataAsValue &MD, Function *F); 4580b57cec5SDimitry Andric void visitValueAsMetadata(const ValueAsMetadata &MD, Function *F); 4590b57cec5SDimitry Andric void visitComdat(const Comdat &C); 460349cc55cSDimitry Andric void visitModuleIdents(); 461349cc55cSDimitry Andric void visitModuleCommandLines(); 462349cc55cSDimitry Andric void visitModuleFlags(); 4630b57cec5SDimitry Andric void visitModuleFlag(const MDNode *Op, 4640b57cec5SDimitry Andric DenseMap<const MDString *, const MDNode *> &SeenIDs, 4650b57cec5SDimitry Andric SmallVectorImpl<const MDNode *> &Requirements); 4660b57cec5SDimitry Andric void visitModuleFlagCGProfileEntry(const MDOperand &MDO); 4670b57cec5SDimitry Andric void visitFunction(const Function &F); 4680b57cec5SDimitry Andric void visitBasicBlock(BasicBlock &BB); 4690b57cec5SDimitry Andric void visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty); 4700b57cec5SDimitry Andric void visitDereferenceableMetadata(Instruction &I, MDNode *MD); 4718bcb0991SDimitry Andric void visitProfMetadata(Instruction &I, MDNode *MD); 472e8d8bef9SDimitry Andric void visitAnnotationMetadata(MDNode *Annotation); 473349cc55cSDimitry Andric void visitAliasScopeMetadata(const MDNode *MD); 474349cc55cSDimitry Andric void visitAliasScopeListMetadata(const MDNode *MD); 475*81ad6265SDimitry Andric void visitAccessGroupMetadata(const MDNode *MD); 4760b57cec5SDimitry Andric 4770b57cec5SDimitry Andric template <class Ty> bool isValidMetadataArray(const MDTuple &N); 4780b57cec5SDimitry Andric #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N); 4790b57cec5SDimitry Andric #include "llvm/IR/Metadata.def" 4800b57cec5SDimitry Andric void visitDIScope(const DIScope &N); 4810b57cec5SDimitry Andric void visitDIVariable(const DIVariable &N); 4820b57cec5SDimitry Andric void visitDILexicalBlockBase(const DILexicalBlockBase &N); 4830b57cec5SDimitry Andric void visitDITemplateParameter(const DITemplateParameter &N); 4840b57cec5SDimitry Andric 4850b57cec5SDimitry Andric void visitTemplateParams(const MDNode &N, const Metadata &RawParams); 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric // InstVisitor overrides... 4880b57cec5SDimitry Andric using InstVisitor<Verifier>::visit; 4890b57cec5SDimitry Andric void visit(Instruction &I); 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric void visitTruncInst(TruncInst &I); 4920b57cec5SDimitry Andric void visitZExtInst(ZExtInst &I); 4930b57cec5SDimitry Andric void visitSExtInst(SExtInst &I); 4940b57cec5SDimitry Andric void visitFPTruncInst(FPTruncInst &I); 4950b57cec5SDimitry Andric void visitFPExtInst(FPExtInst &I); 4960b57cec5SDimitry Andric void visitFPToUIInst(FPToUIInst &I); 4970b57cec5SDimitry Andric void visitFPToSIInst(FPToSIInst &I); 4980b57cec5SDimitry Andric void visitUIToFPInst(UIToFPInst &I); 4990b57cec5SDimitry Andric void visitSIToFPInst(SIToFPInst &I); 5000b57cec5SDimitry Andric void visitIntToPtrInst(IntToPtrInst &I); 5010b57cec5SDimitry Andric void visitPtrToIntInst(PtrToIntInst &I); 5020b57cec5SDimitry Andric void visitBitCastInst(BitCastInst &I); 5030b57cec5SDimitry Andric void visitAddrSpaceCastInst(AddrSpaceCastInst &I); 5040b57cec5SDimitry Andric void visitPHINode(PHINode &PN); 5050b57cec5SDimitry Andric void visitCallBase(CallBase &Call); 5060b57cec5SDimitry Andric void visitUnaryOperator(UnaryOperator &U); 5070b57cec5SDimitry Andric void visitBinaryOperator(BinaryOperator &B); 5080b57cec5SDimitry Andric void visitICmpInst(ICmpInst &IC); 5090b57cec5SDimitry Andric void visitFCmpInst(FCmpInst &FC); 5100b57cec5SDimitry Andric void visitExtractElementInst(ExtractElementInst &EI); 5110b57cec5SDimitry Andric void visitInsertElementInst(InsertElementInst &EI); 5120b57cec5SDimitry Andric void visitShuffleVectorInst(ShuffleVectorInst &EI); 5130b57cec5SDimitry Andric void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); } 5140b57cec5SDimitry Andric void visitCallInst(CallInst &CI); 5150b57cec5SDimitry Andric void visitInvokeInst(InvokeInst &II); 5160b57cec5SDimitry Andric void visitGetElementPtrInst(GetElementPtrInst &GEP); 5170b57cec5SDimitry Andric void visitLoadInst(LoadInst &LI); 5180b57cec5SDimitry Andric void visitStoreInst(StoreInst &SI); 5190b57cec5SDimitry Andric void verifyDominatesUse(Instruction &I, unsigned i); 5200b57cec5SDimitry Andric void visitInstruction(Instruction &I); 5210b57cec5SDimitry Andric void visitTerminator(Instruction &I); 5220b57cec5SDimitry Andric void visitBranchInst(BranchInst &BI); 5230b57cec5SDimitry Andric void visitReturnInst(ReturnInst &RI); 5240b57cec5SDimitry Andric void visitSwitchInst(SwitchInst &SI); 5250b57cec5SDimitry Andric void visitIndirectBrInst(IndirectBrInst &BI); 5260b57cec5SDimitry Andric void visitCallBrInst(CallBrInst &CBI); 5270b57cec5SDimitry Andric void visitSelectInst(SelectInst &SI); 5280b57cec5SDimitry Andric void visitUserOp1(Instruction &I); 5290b57cec5SDimitry Andric void visitUserOp2(Instruction &I) { visitUserOp1(I); } 5300b57cec5SDimitry Andric void visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call); 5310b57cec5SDimitry Andric void visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI); 532*81ad6265SDimitry Andric void visitVPIntrinsic(VPIntrinsic &VPI); 5330b57cec5SDimitry Andric void visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII); 5340b57cec5SDimitry Andric void visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI); 5350b57cec5SDimitry Andric void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI); 5360b57cec5SDimitry Andric void visitAtomicRMWInst(AtomicRMWInst &RMWI); 5370b57cec5SDimitry Andric void visitFenceInst(FenceInst &FI); 5380b57cec5SDimitry Andric void visitAllocaInst(AllocaInst &AI); 5390b57cec5SDimitry Andric void visitExtractValueInst(ExtractValueInst &EVI); 5400b57cec5SDimitry Andric void visitInsertValueInst(InsertValueInst &IVI); 5410b57cec5SDimitry Andric void visitEHPadPredecessors(Instruction &I); 5420b57cec5SDimitry Andric void visitLandingPadInst(LandingPadInst &LPI); 5430b57cec5SDimitry Andric void visitResumeInst(ResumeInst &RI); 5440b57cec5SDimitry Andric void visitCatchPadInst(CatchPadInst &CPI); 5450b57cec5SDimitry Andric void visitCatchReturnInst(CatchReturnInst &CatchReturn); 5460b57cec5SDimitry Andric void visitCleanupPadInst(CleanupPadInst &CPI); 5470b57cec5SDimitry Andric void visitFuncletPadInst(FuncletPadInst &FPI); 5480b57cec5SDimitry Andric void visitCatchSwitchInst(CatchSwitchInst &CatchSwitch); 5490b57cec5SDimitry Andric void visitCleanupReturnInst(CleanupReturnInst &CRI); 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andric void verifySwiftErrorCall(CallBase &Call, const Value *SwiftErrorVal); 5520b57cec5SDimitry Andric void verifySwiftErrorValue(const Value *SwiftErrorVal); 5530eae32dcSDimitry Andric void verifyTailCCMustTailAttrs(const AttrBuilder &Attrs, StringRef Context); 5540b57cec5SDimitry Andric void verifyMustTailCall(CallInst &CI); 5550b57cec5SDimitry Andric bool verifyAttributeCount(AttributeList Attrs, unsigned Params); 556fe6060f1SDimitry Andric void verifyAttributeTypes(AttributeSet Attrs, const Value *V); 5570b57cec5SDimitry Andric void verifyParameterAttrs(AttributeSet Attrs, Type *Ty, const Value *V); 558fe6060f1SDimitry Andric void checkUnsignedBaseTenFuncAttr(AttributeList Attrs, StringRef Attr, 559fe6060f1SDimitry Andric const Value *V); 5600b57cec5SDimitry Andric void verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs, 56104eeddc0SDimitry Andric const Value *V, bool IsIntrinsic, bool IsInlineAsm); 5620b57cec5SDimitry Andric void verifyFunctionMetadata(ArrayRef<std::pair<unsigned, MDNode *>> MDs); 5630b57cec5SDimitry Andric 5640b57cec5SDimitry Andric void visitConstantExprsRecursively(const Constant *EntryC); 5650b57cec5SDimitry Andric void visitConstantExpr(const ConstantExpr *CE); 56604eeddc0SDimitry Andric void verifyInlineAsmCall(const CallBase &Call); 5670b57cec5SDimitry Andric void verifyStatepoint(const CallBase &Call); 5680b57cec5SDimitry Andric void verifyFrameRecoverIndices(); 5690b57cec5SDimitry Andric void verifySiblingFuncletUnwinds(); 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric void verifyFragmentExpression(const DbgVariableIntrinsic &I); 5720b57cec5SDimitry Andric template <typename ValueOrMetadata> 5730b57cec5SDimitry Andric void verifyFragmentExpression(const DIVariable &V, 5740b57cec5SDimitry Andric DIExpression::FragmentInfo Fragment, 5750b57cec5SDimitry Andric ValueOrMetadata *Desc); 5760b57cec5SDimitry Andric void verifyFnArgs(const DbgVariableIntrinsic &I); 5778bcb0991SDimitry Andric void verifyNotEntryValue(const DbgVariableIntrinsic &I); 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andric /// Module-level debug info verification... 5800b57cec5SDimitry Andric void verifyCompileUnits(); 5810b57cec5SDimitry Andric 5820b57cec5SDimitry Andric /// Module-level verification that all @llvm.experimental.deoptimize 5830b57cec5SDimitry Andric /// declarations share the same calling convention. 5840b57cec5SDimitry Andric void verifyDeoptimizeCallingConvs(); 5850b57cec5SDimitry Andric 586349cc55cSDimitry Andric void verifyAttachedCallBundle(const CallBase &Call, 587349cc55cSDimitry Andric const OperandBundleUse &BU); 588349cc55cSDimitry Andric 5890b57cec5SDimitry Andric /// Verify all-or-nothing property of DIFile source attribute within a CU. 5900b57cec5SDimitry Andric void verifySourceDebugInfo(const DICompileUnit &U, const DIFile &F); 591e8d8bef9SDimitry Andric 592e8d8bef9SDimitry Andric /// Verify the llvm.experimental.noalias.scope.decl declarations 593e8d8bef9SDimitry Andric void verifyNoAliasScopeDecl(); 5940b57cec5SDimitry Andric }; 5950b57cec5SDimitry Andric 5960b57cec5SDimitry Andric } // end anonymous namespace 5970b57cec5SDimitry Andric 5980b57cec5SDimitry Andric /// We know that cond should be true, if not print an error message. 599*81ad6265SDimitry Andric #define Check(C, ...) \ 600*81ad6265SDimitry Andric do { \ 601*81ad6265SDimitry Andric if (!(C)) { \ 602*81ad6265SDimitry Andric CheckFailed(__VA_ARGS__); \ 603*81ad6265SDimitry Andric return; \ 604*81ad6265SDimitry Andric } \ 605*81ad6265SDimitry Andric } while (false) 6060b57cec5SDimitry Andric 6070b57cec5SDimitry Andric /// We know that a debug info condition should be true, if not print 6080b57cec5SDimitry Andric /// an error message. 609*81ad6265SDimitry Andric #define CheckDI(C, ...) \ 610*81ad6265SDimitry Andric do { \ 611*81ad6265SDimitry Andric if (!(C)) { \ 612*81ad6265SDimitry Andric DebugInfoCheckFailed(__VA_ARGS__); \ 613*81ad6265SDimitry Andric return; \ 614*81ad6265SDimitry Andric } \ 615*81ad6265SDimitry Andric } while (false) 6160b57cec5SDimitry Andric 6170b57cec5SDimitry Andric void Verifier::visit(Instruction &I) { 6180b57cec5SDimitry Andric for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 619*81ad6265SDimitry Andric Check(I.getOperand(i) != nullptr, "Operand is null", &I); 6200b57cec5SDimitry Andric InstVisitor<Verifier>::visit(I); 6210b57cec5SDimitry Andric } 6220b57cec5SDimitry Andric 6230eae32dcSDimitry Andric // Helper to iterate over indirect users. By returning false, the callback can ask to stop traversing further. 6240b57cec5SDimitry Andric static void forEachUser(const Value *User, 6250b57cec5SDimitry Andric SmallPtrSet<const Value *, 32> &Visited, 6260b57cec5SDimitry Andric llvm::function_ref<bool(const Value *)> Callback) { 6270b57cec5SDimitry Andric if (!Visited.insert(User).second) 6280b57cec5SDimitry Andric return; 6290eae32dcSDimitry Andric 6300eae32dcSDimitry Andric SmallVector<const Value *> WorkList; 6310eae32dcSDimitry Andric append_range(WorkList, User->materialized_users()); 6320eae32dcSDimitry Andric while (!WorkList.empty()) { 6330eae32dcSDimitry Andric const Value *Cur = WorkList.pop_back_val(); 6340eae32dcSDimitry Andric if (!Visited.insert(Cur).second) 6350eae32dcSDimitry Andric continue; 6360eae32dcSDimitry Andric if (Callback(Cur)) 6370eae32dcSDimitry Andric append_range(WorkList, Cur->materialized_users()); 6380eae32dcSDimitry Andric } 6390b57cec5SDimitry Andric } 6400b57cec5SDimitry Andric 6410b57cec5SDimitry Andric void Verifier::visitGlobalValue(const GlobalValue &GV) { 642*81ad6265SDimitry Andric Check(!GV.isDeclaration() || GV.hasValidDeclarationLinkage(), 6430b57cec5SDimitry Andric "Global is external, but doesn't have external or weak linkage!", &GV); 6440b57cec5SDimitry Andric 6450eae32dcSDimitry Andric if (const GlobalObject *GO = dyn_cast<GlobalObject>(&GV)) { 6460eae32dcSDimitry Andric 6470eae32dcSDimitry Andric if (MaybeAlign A = GO->getAlign()) { 648*81ad6265SDimitry Andric Check(A->value() <= Value::MaximumAlignment, 6495ffd83dbSDimitry Andric "huge alignment values are unsupported", GO); 6500eae32dcSDimitry Andric } 6510eae32dcSDimitry Andric } 652*81ad6265SDimitry Andric Check(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV), 6530b57cec5SDimitry Andric "Only global variables can have appending linkage!", &GV); 6540b57cec5SDimitry Andric 6550b57cec5SDimitry Andric if (GV.hasAppendingLinkage()) { 6560b57cec5SDimitry Andric const GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV); 657*81ad6265SDimitry Andric Check(GVar && GVar->getValueType()->isArrayTy(), 6580b57cec5SDimitry Andric "Only global arrays can have appending linkage!", GVar); 6590b57cec5SDimitry Andric } 6600b57cec5SDimitry Andric 6610b57cec5SDimitry Andric if (GV.isDeclarationForLinker()) 662*81ad6265SDimitry Andric Check(!GV.hasComdat(), "Declaration may not be in a Comdat!", &GV); 6630b57cec5SDimitry Andric 6640b57cec5SDimitry Andric if (GV.hasDLLImportStorageClass()) { 665*81ad6265SDimitry Andric Check(!GV.isDSOLocal(), "GlobalValue with DLLImport Storage is dso_local!", 666*81ad6265SDimitry Andric &GV); 6670b57cec5SDimitry Andric 668*81ad6265SDimitry Andric Check((GV.isDeclaration() && 669e8d8bef9SDimitry Andric (GV.hasExternalLinkage() || GV.hasExternalWeakLinkage())) || 6700b57cec5SDimitry Andric GV.hasAvailableExternallyLinkage(), 6710b57cec5SDimitry Andric "Global is marked as dllimport, but not external", &GV); 6720b57cec5SDimitry Andric } 6730b57cec5SDimitry Andric 6745ffd83dbSDimitry Andric if (GV.isImplicitDSOLocal()) 675*81ad6265SDimitry Andric Check(GV.isDSOLocal(), 6765ffd83dbSDimitry Andric "GlobalValue with local linkage or non-default " 6775ffd83dbSDimitry Andric "visibility must be dso_local!", 6780b57cec5SDimitry Andric &GV); 6790b57cec5SDimitry Andric 6800b57cec5SDimitry Andric forEachUser(&GV, GlobalValueVisited, [&](const Value *V) -> bool { 6810b57cec5SDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(V)) { 6820b57cec5SDimitry Andric if (!I->getParent() || !I->getParent()->getParent()) 6830b57cec5SDimitry Andric CheckFailed("Global is referenced by parentless instruction!", &GV, &M, 6840b57cec5SDimitry Andric I); 6850b57cec5SDimitry Andric else if (I->getParent()->getParent()->getParent() != &M) 6860b57cec5SDimitry Andric CheckFailed("Global is referenced in a different module!", &GV, &M, I, 6870b57cec5SDimitry Andric I->getParent()->getParent(), 6880b57cec5SDimitry Andric I->getParent()->getParent()->getParent()); 6890b57cec5SDimitry Andric return false; 6900b57cec5SDimitry Andric } else if (const Function *F = dyn_cast<Function>(V)) { 6910b57cec5SDimitry Andric if (F->getParent() != &M) 6920b57cec5SDimitry Andric CheckFailed("Global is used by function in a different module", &GV, &M, 6930b57cec5SDimitry Andric F, F->getParent()); 6940b57cec5SDimitry Andric return false; 6950b57cec5SDimitry Andric } 6960b57cec5SDimitry Andric return true; 6970b57cec5SDimitry Andric }); 6980b57cec5SDimitry Andric } 6990b57cec5SDimitry Andric 7000b57cec5SDimitry Andric void Verifier::visitGlobalVariable(const GlobalVariable &GV) { 7010b57cec5SDimitry Andric if (GV.hasInitializer()) { 702*81ad6265SDimitry Andric Check(GV.getInitializer()->getType() == GV.getValueType(), 7030b57cec5SDimitry Andric "Global variable initializer type does not match global " 7040b57cec5SDimitry Andric "variable type!", 7050b57cec5SDimitry Andric &GV); 7060b57cec5SDimitry Andric // If the global has common linkage, it must have a zero initializer and 7070b57cec5SDimitry Andric // cannot be constant. 7080b57cec5SDimitry Andric if (GV.hasCommonLinkage()) { 709*81ad6265SDimitry Andric Check(GV.getInitializer()->isNullValue(), 7100b57cec5SDimitry Andric "'common' global must have a zero initializer!", &GV); 711*81ad6265SDimitry Andric Check(!GV.isConstant(), "'common' global may not be marked constant!", 7120b57cec5SDimitry Andric &GV); 713*81ad6265SDimitry Andric Check(!GV.hasComdat(), "'common' global may not be in a Comdat!", &GV); 7140b57cec5SDimitry Andric } 7150b57cec5SDimitry Andric } 7160b57cec5SDimitry Andric 7170b57cec5SDimitry Andric if (GV.hasName() && (GV.getName() == "llvm.global_ctors" || 7180b57cec5SDimitry Andric GV.getName() == "llvm.global_dtors")) { 719*81ad6265SDimitry Andric Check(!GV.hasInitializer() || GV.hasAppendingLinkage(), 7200b57cec5SDimitry Andric "invalid linkage for intrinsic global variable", &GV); 7210b57cec5SDimitry Andric // Don't worry about emitting an error for it not being an array, 7220b57cec5SDimitry Andric // visitGlobalValue will complain on appending non-array. 7230b57cec5SDimitry Andric if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getValueType())) { 7240b57cec5SDimitry Andric StructType *STy = dyn_cast<StructType>(ATy->getElementType()); 7250b57cec5SDimitry Andric PointerType *FuncPtrTy = 7260b57cec5SDimitry Andric FunctionType::get(Type::getVoidTy(Context), false)-> 7270b57cec5SDimitry Andric getPointerTo(DL.getProgramAddressSpace()); 728*81ad6265SDimitry Andric Check(STy && (STy->getNumElements() == 2 || STy->getNumElements() == 3) && 7290b57cec5SDimitry Andric STy->getTypeAtIndex(0u)->isIntegerTy(32) && 7300b57cec5SDimitry Andric STy->getTypeAtIndex(1) == FuncPtrTy, 7310b57cec5SDimitry Andric "wrong type for intrinsic global variable", &GV); 732*81ad6265SDimitry Andric Check(STy->getNumElements() == 3, 7330b57cec5SDimitry Andric "the third field of the element type is mandatory, " 7340b57cec5SDimitry Andric "specify i8* null to migrate from the obsoleted 2-field form"); 7350b57cec5SDimitry Andric Type *ETy = STy->getTypeAtIndex(2); 736fe6060f1SDimitry Andric Type *Int8Ty = Type::getInt8Ty(ETy->getContext()); 737*81ad6265SDimitry Andric Check(ETy->isPointerTy() && 738fe6060f1SDimitry Andric cast<PointerType>(ETy)->isOpaqueOrPointeeTypeMatches(Int8Ty), 7390b57cec5SDimitry Andric "wrong type for intrinsic global variable", &GV); 7400b57cec5SDimitry Andric } 7410b57cec5SDimitry Andric } 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric if (GV.hasName() && (GV.getName() == "llvm.used" || 7440b57cec5SDimitry Andric GV.getName() == "llvm.compiler.used")) { 745*81ad6265SDimitry Andric Check(!GV.hasInitializer() || GV.hasAppendingLinkage(), 7460b57cec5SDimitry Andric "invalid linkage for intrinsic global variable", &GV); 7470b57cec5SDimitry Andric Type *GVType = GV.getValueType(); 7480b57cec5SDimitry Andric if (ArrayType *ATy = dyn_cast<ArrayType>(GVType)) { 7490b57cec5SDimitry Andric PointerType *PTy = dyn_cast<PointerType>(ATy->getElementType()); 750*81ad6265SDimitry Andric Check(PTy, "wrong type for intrinsic global variable", &GV); 7510b57cec5SDimitry Andric if (GV.hasInitializer()) { 7520b57cec5SDimitry Andric const Constant *Init = GV.getInitializer(); 7530b57cec5SDimitry Andric const ConstantArray *InitArray = dyn_cast<ConstantArray>(Init); 754*81ad6265SDimitry Andric Check(InitArray, "wrong initalizer for intrinsic global variable", 7550b57cec5SDimitry Andric Init); 7560b57cec5SDimitry Andric for (Value *Op : InitArray->operands()) { 7578bcb0991SDimitry Andric Value *V = Op->stripPointerCasts(); 758*81ad6265SDimitry Andric Check(isa<GlobalVariable>(V) || isa<Function>(V) || 7590b57cec5SDimitry Andric isa<GlobalAlias>(V), 7600eae32dcSDimitry Andric Twine("invalid ") + GV.getName() + " member", V); 761*81ad6265SDimitry Andric Check(V->hasName(), 7620eae32dcSDimitry Andric Twine("members of ") + GV.getName() + " must be named", V); 7630b57cec5SDimitry Andric } 7640b57cec5SDimitry Andric } 7650b57cec5SDimitry Andric } 7660b57cec5SDimitry Andric } 7670b57cec5SDimitry Andric 7680b57cec5SDimitry Andric // Visit any debug info attachments. 7690b57cec5SDimitry Andric SmallVector<MDNode *, 1> MDs; 7700b57cec5SDimitry Andric GV.getMetadata(LLVMContext::MD_dbg, MDs); 7710b57cec5SDimitry Andric for (auto *MD : MDs) { 7720b57cec5SDimitry Andric if (auto *GVE = dyn_cast<DIGlobalVariableExpression>(MD)) 7730b57cec5SDimitry Andric visitDIGlobalVariableExpression(*GVE); 7740b57cec5SDimitry Andric else 775*81ad6265SDimitry Andric CheckDI(false, "!dbg attachment of global variable must be a " 7760b57cec5SDimitry Andric "DIGlobalVariableExpression"); 7770b57cec5SDimitry Andric } 7780b57cec5SDimitry Andric 7790b57cec5SDimitry Andric // Scalable vectors cannot be global variables, since we don't know 780e8d8bef9SDimitry Andric // the runtime size. If the global is an array containing scalable vectors, 781e8d8bef9SDimitry Andric // that will be caught by the isValidElementType methods in StructType or 782e8d8bef9SDimitry Andric // ArrayType instead. 783*81ad6265SDimitry Andric Check(!isa<ScalableVectorType>(GV.getValueType()), 7845ffd83dbSDimitry Andric "Globals cannot contain scalable vectors", &GV); 7850b57cec5SDimitry Andric 786e8d8bef9SDimitry Andric if (auto *STy = dyn_cast<StructType>(GV.getValueType())) 787*81ad6265SDimitry Andric Check(!STy->containsScalableVectorType(), 788e8d8bef9SDimitry Andric "Globals cannot contain scalable vectors", &GV); 789e8d8bef9SDimitry Andric 7900b57cec5SDimitry Andric if (!GV.hasInitializer()) { 7910b57cec5SDimitry Andric visitGlobalValue(GV); 7920b57cec5SDimitry Andric return; 7930b57cec5SDimitry Andric } 7940b57cec5SDimitry Andric 7950b57cec5SDimitry Andric // Walk any aggregate initializers looking for bitcasts between address spaces 7960b57cec5SDimitry Andric visitConstantExprsRecursively(GV.getInitializer()); 7970b57cec5SDimitry Andric 7980b57cec5SDimitry Andric visitGlobalValue(GV); 7990b57cec5SDimitry Andric } 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric void Verifier::visitAliaseeSubExpr(const GlobalAlias &GA, const Constant &C) { 8020b57cec5SDimitry Andric SmallPtrSet<const GlobalAlias*, 4> Visited; 8030b57cec5SDimitry Andric Visited.insert(&GA); 8040b57cec5SDimitry Andric visitAliaseeSubExpr(Visited, GA, C); 8050b57cec5SDimitry Andric } 8060b57cec5SDimitry Andric 8070b57cec5SDimitry Andric void Verifier::visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias*> &Visited, 8080b57cec5SDimitry Andric const GlobalAlias &GA, const Constant &C) { 8090b57cec5SDimitry Andric if (const auto *GV = dyn_cast<GlobalValue>(&C)) { 810*81ad6265SDimitry Andric Check(!GV->isDeclarationForLinker(), "Alias must point to a definition", 8110b57cec5SDimitry Andric &GA); 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric if (const auto *GA2 = dyn_cast<GlobalAlias>(GV)) { 814*81ad6265SDimitry Andric Check(Visited.insert(GA2).second, "Aliases cannot form a cycle", &GA); 8150b57cec5SDimitry Andric 816*81ad6265SDimitry Andric Check(!GA2->isInterposable(), 817*81ad6265SDimitry Andric "Alias cannot point to an interposable alias", &GA); 8180b57cec5SDimitry Andric } else { 8190b57cec5SDimitry Andric // Only continue verifying subexpressions of GlobalAliases. 8200b57cec5SDimitry Andric // Do not recurse into global initializers. 8210b57cec5SDimitry Andric return; 8220b57cec5SDimitry Andric } 8230b57cec5SDimitry Andric } 8240b57cec5SDimitry Andric 8250b57cec5SDimitry Andric if (const auto *CE = dyn_cast<ConstantExpr>(&C)) 8260b57cec5SDimitry Andric visitConstantExprsRecursively(CE); 8270b57cec5SDimitry Andric 8280b57cec5SDimitry Andric for (const Use &U : C.operands()) { 8290b57cec5SDimitry Andric Value *V = &*U; 8300b57cec5SDimitry Andric if (const auto *GA2 = dyn_cast<GlobalAlias>(V)) 8310b57cec5SDimitry Andric visitAliaseeSubExpr(Visited, GA, *GA2->getAliasee()); 8320b57cec5SDimitry Andric else if (const auto *C2 = dyn_cast<Constant>(V)) 8330b57cec5SDimitry Andric visitAliaseeSubExpr(Visited, GA, *C2); 8340b57cec5SDimitry Andric } 8350b57cec5SDimitry Andric } 8360b57cec5SDimitry Andric 8370b57cec5SDimitry Andric void Verifier::visitGlobalAlias(const GlobalAlias &GA) { 838*81ad6265SDimitry Andric Check(GlobalAlias::isValidLinkage(GA.getLinkage()), 8390b57cec5SDimitry Andric "Alias should have private, internal, linkonce, weak, linkonce_odr, " 8400b57cec5SDimitry Andric "weak_odr, or external linkage!", 8410b57cec5SDimitry Andric &GA); 8420b57cec5SDimitry Andric const Constant *Aliasee = GA.getAliasee(); 843*81ad6265SDimitry Andric Check(Aliasee, "Aliasee cannot be NULL!", &GA); 844*81ad6265SDimitry Andric Check(GA.getType() == Aliasee->getType(), 8450b57cec5SDimitry Andric "Alias and aliasee types should match!", &GA); 8460b57cec5SDimitry Andric 847*81ad6265SDimitry Andric Check(isa<GlobalValue>(Aliasee) || isa<ConstantExpr>(Aliasee), 8480b57cec5SDimitry Andric "Aliasee should be either GlobalValue or ConstantExpr", &GA); 8490b57cec5SDimitry Andric 8500b57cec5SDimitry Andric visitAliaseeSubExpr(GA, *Aliasee); 8510b57cec5SDimitry Andric 8520b57cec5SDimitry Andric visitGlobalValue(GA); 8530b57cec5SDimitry Andric } 8540b57cec5SDimitry Andric 855349cc55cSDimitry Andric void Verifier::visitGlobalIFunc(const GlobalIFunc &GI) { 856*81ad6265SDimitry Andric Check(GlobalIFunc::isValidLinkage(GI.getLinkage()), 857*81ad6265SDimitry Andric "IFunc should have private, internal, linkonce, weak, linkonce_odr, " 858*81ad6265SDimitry Andric "weak_odr, or external linkage!", 859*81ad6265SDimitry Andric &GI); 860349cc55cSDimitry Andric // Pierce through ConstantExprs and GlobalAliases and check that the resolver 861*81ad6265SDimitry Andric // is a Function definition. 862349cc55cSDimitry Andric const Function *Resolver = GI.getResolverFunction(); 863*81ad6265SDimitry Andric Check(Resolver, "IFunc must have a Function resolver", &GI); 864*81ad6265SDimitry Andric Check(!Resolver->isDeclarationForLinker(), 865*81ad6265SDimitry Andric "IFunc resolver must be a definition", &GI); 866349cc55cSDimitry Andric 867349cc55cSDimitry Andric // Check that the immediate resolver operand (prior to any bitcasts) has the 868*81ad6265SDimitry Andric // correct type. 869349cc55cSDimitry Andric const Type *ResolverTy = GI.getResolver()->getType(); 870349cc55cSDimitry Andric const Type *ResolverFuncTy = 871349cc55cSDimitry Andric GlobalIFunc::getResolverFunctionType(GI.getValueType()); 872*81ad6265SDimitry Andric Check(ResolverTy == ResolverFuncTy->getPointerTo(), 873349cc55cSDimitry Andric "IFunc resolver has incorrect type", &GI); 874349cc55cSDimitry Andric } 875349cc55cSDimitry Andric 8760b57cec5SDimitry Andric void Verifier::visitNamedMDNode(const NamedMDNode &NMD) { 8770b57cec5SDimitry Andric // There used to be various other llvm.dbg.* nodes, but we don't support 8780b57cec5SDimitry Andric // upgrading them and we want to reserve the namespace for future uses. 8790b57cec5SDimitry Andric if (NMD.getName().startswith("llvm.dbg.")) 880*81ad6265SDimitry Andric CheckDI(NMD.getName() == "llvm.dbg.cu", 881*81ad6265SDimitry Andric "unrecognized named metadata node in the llvm.dbg namespace", &NMD); 8820b57cec5SDimitry Andric for (const MDNode *MD : NMD.operands()) { 8830b57cec5SDimitry Andric if (NMD.getName() == "llvm.dbg.cu") 884*81ad6265SDimitry Andric CheckDI(MD && isa<DICompileUnit>(MD), "invalid compile unit", &NMD, MD); 8850b57cec5SDimitry Andric 8860b57cec5SDimitry Andric if (!MD) 8870b57cec5SDimitry Andric continue; 8880b57cec5SDimitry Andric 8895ffd83dbSDimitry Andric visitMDNode(*MD, AreDebugLocsAllowed::Yes); 8900b57cec5SDimitry Andric } 8910b57cec5SDimitry Andric } 8920b57cec5SDimitry Andric 8935ffd83dbSDimitry Andric void Verifier::visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs) { 8940b57cec5SDimitry Andric // Only visit each node once. Metadata can be mutually recursive, so this 8950b57cec5SDimitry Andric // avoids infinite recursion here, as well as being an optimization. 8960b57cec5SDimitry Andric if (!MDNodes.insert(&MD).second) 8970b57cec5SDimitry Andric return; 8980b57cec5SDimitry Andric 899*81ad6265SDimitry Andric Check(&MD.getContext() == &Context, 900fe6060f1SDimitry Andric "MDNode context does not match Module context!", &MD); 901fe6060f1SDimitry Andric 9020b57cec5SDimitry Andric switch (MD.getMetadataID()) { 9030b57cec5SDimitry Andric default: 9040b57cec5SDimitry Andric llvm_unreachable("Invalid MDNode subclass"); 9050b57cec5SDimitry Andric case Metadata::MDTupleKind: 9060b57cec5SDimitry Andric break; 9070b57cec5SDimitry Andric #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \ 9080b57cec5SDimitry Andric case Metadata::CLASS##Kind: \ 9090b57cec5SDimitry Andric visit##CLASS(cast<CLASS>(MD)); \ 9100b57cec5SDimitry Andric break; 9110b57cec5SDimitry Andric #include "llvm/IR/Metadata.def" 9120b57cec5SDimitry Andric } 9130b57cec5SDimitry Andric 9140b57cec5SDimitry Andric for (const Metadata *Op : MD.operands()) { 9150b57cec5SDimitry Andric if (!Op) 9160b57cec5SDimitry Andric continue; 917*81ad6265SDimitry Andric Check(!isa<LocalAsMetadata>(Op), "Invalid operand for global metadata!", 9180b57cec5SDimitry Andric &MD, Op); 919*81ad6265SDimitry Andric CheckDI(!isa<DILocation>(Op) || AllowLocs == AreDebugLocsAllowed::Yes, 9205ffd83dbSDimitry Andric "DILocation not allowed within this metadata node", &MD, Op); 9210b57cec5SDimitry Andric if (auto *N = dyn_cast<MDNode>(Op)) { 9225ffd83dbSDimitry Andric visitMDNode(*N, AllowLocs); 9230b57cec5SDimitry Andric continue; 9240b57cec5SDimitry Andric } 9250b57cec5SDimitry Andric if (auto *V = dyn_cast<ValueAsMetadata>(Op)) { 9260b57cec5SDimitry Andric visitValueAsMetadata(*V, nullptr); 9270b57cec5SDimitry Andric continue; 9280b57cec5SDimitry Andric } 9290b57cec5SDimitry Andric } 9300b57cec5SDimitry Andric 9310b57cec5SDimitry Andric // Check these last, so we diagnose problems in operands first. 932*81ad6265SDimitry Andric Check(!MD.isTemporary(), "Expected no forward declarations!", &MD); 933*81ad6265SDimitry Andric Check(MD.isResolved(), "All nodes should be resolved!", &MD); 9340b57cec5SDimitry Andric } 9350b57cec5SDimitry Andric 9360b57cec5SDimitry Andric void Verifier::visitValueAsMetadata(const ValueAsMetadata &MD, Function *F) { 937*81ad6265SDimitry Andric Check(MD.getValue(), "Expected valid value", &MD); 938*81ad6265SDimitry Andric Check(!MD.getValue()->getType()->isMetadataTy(), 9390b57cec5SDimitry Andric "Unexpected metadata round-trip through values", &MD, MD.getValue()); 9400b57cec5SDimitry Andric 9410b57cec5SDimitry Andric auto *L = dyn_cast<LocalAsMetadata>(&MD); 9420b57cec5SDimitry Andric if (!L) 9430b57cec5SDimitry Andric return; 9440b57cec5SDimitry Andric 945*81ad6265SDimitry Andric Check(F, "function-local metadata used outside a function", L); 9460b57cec5SDimitry Andric 9470b57cec5SDimitry Andric // If this was an instruction, bb, or argument, verify that it is in the 9480b57cec5SDimitry Andric // function that we expect. 9490b57cec5SDimitry Andric Function *ActualF = nullptr; 9500b57cec5SDimitry Andric if (Instruction *I = dyn_cast<Instruction>(L->getValue())) { 951*81ad6265SDimitry Andric Check(I->getParent(), "function-local metadata not in basic block", L, I); 9520b57cec5SDimitry Andric ActualF = I->getParent()->getParent(); 9530b57cec5SDimitry Andric } else if (BasicBlock *BB = dyn_cast<BasicBlock>(L->getValue())) 9540b57cec5SDimitry Andric ActualF = BB->getParent(); 9550b57cec5SDimitry Andric else if (Argument *A = dyn_cast<Argument>(L->getValue())) 9560b57cec5SDimitry Andric ActualF = A->getParent(); 9570b57cec5SDimitry Andric assert(ActualF && "Unimplemented function local metadata case!"); 9580b57cec5SDimitry Andric 959*81ad6265SDimitry Andric Check(ActualF == F, "function-local metadata used in wrong function", L); 9600b57cec5SDimitry Andric } 9610b57cec5SDimitry Andric 9620b57cec5SDimitry Andric void Verifier::visitMetadataAsValue(const MetadataAsValue &MDV, Function *F) { 9630b57cec5SDimitry Andric Metadata *MD = MDV.getMetadata(); 9640b57cec5SDimitry Andric if (auto *N = dyn_cast<MDNode>(MD)) { 9655ffd83dbSDimitry Andric visitMDNode(*N, AreDebugLocsAllowed::No); 9660b57cec5SDimitry Andric return; 9670b57cec5SDimitry Andric } 9680b57cec5SDimitry Andric 9690b57cec5SDimitry Andric // Only visit each node once. Metadata can be mutually recursive, so this 9700b57cec5SDimitry Andric // avoids infinite recursion here, as well as being an optimization. 9710b57cec5SDimitry Andric if (!MDNodes.insert(MD).second) 9720b57cec5SDimitry Andric return; 9730b57cec5SDimitry Andric 9740b57cec5SDimitry Andric if (auto *V = dyn_cast<ValueAsMetadata>(MD)) 9750b57cec5SDimitry Andric visitValueAsMetadata(*V, F); 9760b57cec5SDimitry Andric } 9770b57cec5SDimitry Andric 9780b57cec5SDimitry Andric static bool isType(const Metadata *MD) { return !MD || isa<DIType>(MD); } 9790b57cec5SDimitry Andric static bool isScope(const Metadata *MD) { return !MD || isa<DIScope>(MD); } 9800b57cec5SDimitry Andric static bool isDINode(const Metadata *MD) { return !MD || isa<DINode>(MD); } 9810b57cec5SDimitry Andric 9820b57cec5SDimitry Andric void Verifier::visitDILocation(const DILocation &N) { 983*81ad6265SDimitry Andric CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()), 9840b57cec5SDimitry Andric "location requires a valid scope", &N, N.getRawScope()); 9850b57cec5SDimitry Andric if (auto *IA = N.getRawInlinedAt()) 986*81ad6265SDimitry Andric CheckDI(isa<DILocation>(IA), "inlined-at should be a location", &N, IA); 9870b57cec5SDimitry Andric if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope())) 988*81ad6265SDimitry Andric CheckDI(SP->isDefinition(), "scope points into the type hierarchy", &N); 9890b57cec5SDimitry Andric } 9900b57cec5SDimitry Andric 9910b57cec5SDimitry Andric void Verifier::visitGenericDINode(const GenericDINode &N) { 992*81ad6265SDimitry Andric CheckDI(N.getTag(), "invalid tag", &N); 9930b57cec5SDimitry Andric } 9940b57cec5SDimitry Andric 9950b57cec5SDimitry Andric void Verifier::visitDIScope(const DIScope &N) { 9960b57cec5SDimitry Andric if (auto *F = N.getRawFile()) 997*81ad6265SDimitry Andric CheckDI(isa<DIFile>(F), "invalid file", &N, F); 9980b57cec5SDimitry Andric } 9990b57cec5SDimitry Andric 10000b57cec5SDimitry Andric void Verifier::visitDISubrange(const DISubrange &N) { 1001*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_subrange_type, "invalid tag", &N); 1002e8d8bef9SDimitry Andric bool HasAssumedSizedArraySupport = dwarf::isFortran(CurrentSourceLang); 1003*81ad6265SDimitry Andric CheckDI(HasAssumedSizedArraySupport || N.getRawCountNode() || 1004e8d8bef9SDimitry Andric N.getRawUpperBound(), 10055ffd83dbSDimitry Andric "Subrange must contain count or upperBound", &N); 1006*81ad6265SDimitry Andric CheckDI(!N.getRawCountNode() || !N.getRawUpperBound(), 10075ffd83dbSDimitry Andric "Subrange can have any one of count or upperBound", &N); 1008fe6060f1SDimitry Andric auto *CBound = N.getRawCountNode(); 1009*81ad6265SDimitry Andric CheckDI(!CBound || isa<ConstantAsMetadata>(CBound) || 1010fe6060f1SDimitry Andric isa<DIVariable>(CBound) || isa<DIExpression>(CBound), 1011fe6060f1SDimitry Andric "Count must be signed constant or DIVariable or DIExpression", &N); 10120b57cec5SDimitry Andric auto Count = N.getCount(); 1013*81ad6265SDimitry Andric CheckDI(!Count || !Count.is<ConstantInt *>() || 10140b57cec5SDimitry Andric Count.get<ConstantInt *>()->getSExtValue() >= -1, 10150b57cec5SDimitry Andric "invalid subrange count", &N); 10165ffd83dbSDimitry Andric auto *LBound = N.getRawLowerBound(); 1017*81ad6265SDimitry Andric CheckDI(!LBound || isa<ConstantAsMetadata>(LBound) || 10185ffd83dbSDimitry Andric isa<DIVariable>(LBound) || isa<DIExpression>(LBound), 10195ffd83dbSDimitry Andric "LowerBound must be signed constant or DIVariable or DIExpression", 10205ffd83dbSDimitry Andric &N); 10215ffd83dbSDimitry Andric auto *UBound = N.getRawUpperBound(); 1022*81ad6265SDimitry Andric CheckDI(!UBound || isa<ConstantAsMetadata>(UBound) || 10235ffd83dbSDimitry Andric isa<DIVariable>(UBound) || isa<DIExpression>(UBound), 10245ffd83dbSDimitry Andric "UpperBound must be signed constant or DIVariable or DIExpression", 10255ffd83dbSDimitry Andric &N); 10265ffd83dbSDimitry Andric auto *Stride = N.getRawStride(); 1027*81ad6265SDimitry Andric CheckDI(!Stride || isa<ConstantAsMetadata>(Stride) || 10285ffd83dbSDimitry Andric isa<DIVariable>(Stride) || isa<DIExpression>(Stride), 10295ffd83dbSDimitry Andric "Stride must be signed constant or DIVariable or DIExpression", &N); 10300b57cec5SDimitry Andric } 10310b57cec5SDimitry Andric 1032e8d8bef9SDimitry Andric void Verifier::visitDIGenericSubrange(const DIGenericSubrange &N) { 1033*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_generic_subrange, "invalid tag", &N); 1034*81ad6265SDimitry Andric CheckDI(N.getRawCountNode() || N.getRawUpperBound(), 1035e8d8bef9SDimitry Andric "GenericSubrange must contain count or upperBound", &N); 1036*81ad6265SDimitry Andric CheckDI(!N.getRawCountNode() || !N.getRawUpperBound(), 1037e8d8bef9SDimitry Andric "GenericSubrange can have any one of count or upperBound", &N); 1038e8d8bef9SDimitry Andric auto *CBound = N.getRawCountNode(); 1039*81ad6265SDimitry Andric CheckDI(!CBound || isa<DIVariable>(CBound) || isa<DIExpression>(CBound), 1040e8d8bef9SDimitry Andric "Count must be signed constant or DIVariable or DIExpression", &N); 1041e8d8bef9SDimitry Andric auto *LBound = N.getRawLowerBound(); 1042*81ad6265SDimitry Andric CheckDI(LBound, "GenericSubrange must contain lowerBound", &N); 1043*81ad6265SDimitry Andric CheckDI(isa<DIVariable>(LBound) || isa<DIExpression>(LBound), 1044e8d8bef9SDimitry Andric "LowerBound must be signed constant or DIVariable or DIExpression", 1045e8d8bef9SDimitry Andric &N); 1046e8d8bef9SDimitry Andric auto *UBound = N.getRawUpperBound(); 1047*81ad6265SDimitry Andric CheckDI(!UBound || isa<DIVariable>(UBound) || isa<DIExpression>(UBound), 1048e8d8bef9SDimitry Andric "UpperBound must be signed constant or DIVariable or DIExpression", 1049e8d8bef9SDimitry Andric &N); 1050e8d8bef9SDimitry Andric auto *Stride = N.getRawStride(); 1051*81ad6265SDimitry Andric CheckDI(Stride, "GenericSubrange must contain stride", &N); 1052*81ad6265SDimitry Andric CheckDI(isa<DIVariable>(Stride) || isa<DIExpression>(Stride), 1053e8d8bef9SDimitry Andric "Stride must be signed constant or DIVariable or DIExpression", &N); 1054e8d8bef9SDimitry Andric } 1055e8d8bef9SDimitry Andric 10560b57cec5SDimitry Andric void Verifier::visitDIEnumerator(const DIEnumerator &N) { 1057*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_enumerator, "invalid tag", &N); 10580b57cec5SDimitry Andric } 10590b57cec5SDimitry Andric 10600b57cec5SDimitry Andric void Verifier::visitDIBasicType(const DIBasicType &N) { 1061*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_base_type || 1062e8d8bef9SDimitry Andric N.getTag() == dwarf::DW_TAG_unspecified_type || 1063e8d8bef9SDimitry Andric N.getTag() == dwarf::DW_TAG_string_type, 10640b57cec5SDimitry Andric "invalid tag", &N); 1065e8d8bef9SDimitry Andric } 1066e8d8bef9SDimitry Andric 1067e8d8bef9SDimitry Andric void Verifier::visitDIStringType(const DIStringType &N) { 1068*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_string_type, "invalid tag", &N); 1069*81ad6265SDimitry Andric CheckDI(!(N.isBigEndian() && N.isLittleEndian()), "has conflicting flags", 1070*81ad6265SDimitry Andric &N); 10710b57cec5SDimitry Andric } 10720b57cec5SDimitry Andric 10730b57cec5SDimitry Andric void Verifier::visitDIDerivedType(const DIDerivedType &N) { 10740b57cec5SDimitry Andric // Common scope checks. 10750b57cec5SDimitry Andric visitDIScope(N); 10760b57cec5SDimitry Andric 1077*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_typedef || 10780b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_pointer_type || 10790b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_ptr_to_member_type || 10800b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_reference_type || 10810b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_rvalue_reference_type || 10820b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_const_type || 108304eeddc0SDimitry Andric N.getTag() == dwarf::DW_TAG_immutable_type || 10840b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_volatile_type || 10850b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_restrict_type || 10860b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_atomic_type || 10870b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_member || 10880b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_inheritance || 1089fe6060f1SDimitry Andric N.getTag() == dwarf::DW_TAG_friend || 1090fe6060f1SDimitry Andric N.getTag() == dwarf::DW_TAG_set_type, 10910b57cec5SDimitry Andric "invalid tag", &N); 10920b57cec5SDimitry Andric if (N.getTag() == dwarf::DW_TAG_ptr_to_member_type) { 1093*81ad6265SDimitry Andric CheckDI(isType(N.getRawExtraData()), "invalid pointer to member type", &N, 10940b57cec5SDimitry Andric N.getRawExtraData()); 10950b57cec5SDimitry Andric } 10960b57cec5SDimitry Andric 1097fe6060f1SDimitry Andric if (N.getTag() == dwarf::DW_TAG_set_type) { 1098fe6060f1SDimitry Andric if (auto *T = N.getRawBaseType()) { 1099fe6060f1SDimitry Andric auto *Enum = dyn_cast_or_null<DICompositeType>(T); 1100fe6060f1SDimitry Andric auto *Basic = dyn_cast_or_null<DIBasicType>(T); 1101*81ad6265SDimitry Andric CheckDI( 1102fe6060f1SDimitry Andric (Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type) || 1103fe6060f1SDimitry Andric (Basic && (Basic->getEncoding() == dwarf::DW_ATE_unsigned || 1104fe6060f1SDimitry Andric Basic->getEncoding() == dwarf::DW_ATE_signed || 1105fe6060f1SDimitry Andric Basic->getEncoding() == dwarf::DW_ATE_unsigned_char || 1106fe6060f1SDimitry Andric Basic->getEncoding() == dwarf::DW_ATE_signed_char || 1107fe6060f1SDimitry Andric Basic->getEncoding() == dwarf::DW_ATE_boolean)), 1108fe6060f1SDimitry Andric "invalid set base type", &N, T); 1109fe6060f1SDimitry Andric } 1110fe6060f1SDimitry Andric } 1111fe6060f1SDimitry Andric 1112*81ad6265SDimitry Andric CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope()); 1113*81ad6265SDimitry Andric CheckDI(isType(N.getRawBaseType()), "invalid base type", &N, 11140b57cec5SDimitry Andric N.getRawBaseType()); 11150b57cec5SDimitry Andric 11160b57cec5SDimitry Andric if (N.getDWARFAddressSpace()) { 1117*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_pointer_type || 11180b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_reference_type || 11190b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_rvalue_reference_type, 11200b57cec5SDimitry Andric "DWARF address space only applies to pointer or reference types", 11210b57cec5SDimitry Andric &N); 11220b57cec5SDimitry Andric } 11230b57cec5SDimitry Andric } 11240b57cec5SDimitry Andric 11250b57cec5SDimitry Andric /// Detect mutually exclusive flags. 11260b57cec5SDimitry Andric static bool hasConflictingReferenceFlags(unsigned Flags) { 11270b57cec5SDimitry Andric return ((Flags & DINode::FlagLValueReference) && 11280b57cec5SDimitry Andric (Flags & DINode::FlagRValueReference)) || 11290b57cec5SDimitry Andric ((Flags & DINode::FlagTypePassByValue) && 11300b57cec5SDimitry Andric (Flags & DINode::FlagTypePassByReference)); 11310b57cec5SDimitry Andric } 11320b57cec5SDimitry Andric 11330b57cec5SDimitry Andric void Verifier::visitTemplateParams(const MDNode &N, const Metadata &RawParams) { 11340b57cec5SDimitry Andric auto *Params = dyn_cast<MDTuple>(&RawParams); 1135*81ad6265SDimitry Andric CheckDI(Params, "invalid template params", &N, &RawParams); 11360b57cec5SDimitry Andric for (Metadata *Op : Params->operands()) { 1137*81ad6265SDimitry Andric CheckDI(Op && isa<DITemplateParameter>(Op), "invalid template parameter", 11380b57cec5SDimitry Andric &N, Params, Op); 11390b57cec5SDimitry Andric } 11400b57cec5SDimitry Andric } 11410b57cec5SDimitry Andric 11420b57cec5SDimitry Andric void Verifier::visitDICompositeType(const DICompositeType &N) { 11430b57cec5SDimitry Andric // Common scope checks. 11440b57cec5SDimitry Andric visitDIScope(N); 11450b57cec5SDimitry Andric 1146*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_array_type || 11470b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_structure_type || 11480b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_union_type || 11490b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_enumeration_type || 11500b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_class_type || 1151349cc55cSDimitry Andric N.getTag() == dwarf::DW_TAG_variant_part || 1152349cc55cSDimitry Andric N.getTag() == dwarf::DW_TAG_namelist, 11530b57cec5SDimitry Andric "invalid tag", &N); 11540b57cec5SDimitry Andric 1155*81ad6265SDimitry Andric CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope()); 1156*81ad6265SDimitry Andric CheckDI(isType(N.getRawBaseType()), "invalid base type", &N, 11570b57cec5SDimitry Andric N.getRawBaseType()); 11580b57cec5SDimitry Andric 1159*81ad6265SDimitry Andric CheckDI(!N.getRawElements() || isa<MDTuple>(N.getRawElements()), 11600b57cec5SDimitry Andric "invalid composite elements", &N, N.getRawElements()); 1161*81ad6265SDimitry Andric CheckDI(isType(N.getRawVTableHolder()), "invalid vtable holder", &N, 11620b57cec5SDimitry Andric N.getRawVTableHolder()); 1163*81ad6265SDimitry Andric CheckDI(!hasConflictingReferenceFlags(N.getFlags()), 11640b57cec5SDimitry Andric "invalid reference flags", &N); 11658bcb0991SDimitry Andric unsigned DIBlockByRefStruct = 1 << 4; 1166*81ad6265SDimitry Andric CheckDI((N.getFlags() & DIBlockByRefStruct) == 0, 11678bcb0991SDimitry Andric "DIBlockByRefStruct on DICompositeType is no longer supported", &N); 11680b57cec5SDimitry Andric 11690b57cec5SDimitry Andric if (N.isVector()) { 11700b57cec5SDimitry Andric const DINodeArray Elements = N.getElements(); 1171*81ad6265SDimitry Andric CheckDI(Elements.size() == 1 && 11720b57cec5SDimitry Andric Elements[0]->getTag() == dwarf::DW_TAG_subrange_type, 11730b57cec5SDimitry Andric "invalid vector, expected one element of type subrange", &N); 11740b57cec5SDimitry Andric } 11750b57cec5SDimitry Andric 11760b57cec5SDimitry Andric if (auto *Params = N.getRawTemplateParams()) 11770b57cec5SDimitry Andric visitTemplateParams(N, *Params); 11780b57cec5SDimitry Andric 11790b57cec5SDimitry Andric if (auto *D = N.getRawDiscriminator()) { 1180*81ad6265SDimitry Andric CheckDI(isa<DIDerivedType>(D) && N.getTag() == dwarf::DW_TAG_variant_part, 11810b57cec5SDimitry Andric "discriminator can only appear on variant part"); 11820b57cec5SDimitry Andric } 11835ffd83dbSDimitry Andric 11845ffd83dbSDimitry Andric if (N.getRawDataLocation()) { 1185*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_array_type, 11865ffd83dbSDimitry Andric "dataLocation can only appear in array type"); 11875ffd83dbSDimitry Andric } 1188e8d8bef9SDimitry Andric 1189e8d8bef9SDimitry Andric if (N.getRawAssociated()) { 1190*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_array_type, 1191e8d8bef9SDimitry Andric "associated can only appear in array type"); 1192e8d8bef9SDimitry Andric } 1193e8d8bef9SDimitry Andric 1194e8d8bef9SDimitry Andric if (N.getRawAllocated()) { 1195*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_array_type, 1196e8d8bef9SDimitry Andric "allocated can only appear in array type"); 1197e8d8bef9SDimitry Andric } 1198e8d8bef9SDimitry Andric 1199e8d8bef9SDimitry Andric if (N.getRawRank()) { 1200*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_array_type, 1201e8d8bef9SDimitry Andric "rank can only appear in array type"); 1202e8d8bef9SDimitry Andric } 12030b57cec5SDimitry Andric } 12040b57cec5SDimitry Andric 12050b57cec5SDimitry Andric void Verifier::visitDISubroutineType(const DISubroutineType &N) { 1206*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_subroutine_type, "invalid tag", &N); 12070b57cec5SDimitry Andric if (auto *Types = N.getRawTypeArray()) { 1208*81ad6265SDimitry Andric CheckDI(isa<MDTuple>(Types), "invalid composite elements", &N, Types); 12090b57cec5SDimitry Andric for (Metadata *Ty : N.getTypeArray()->operands()) { 1210*81ad6265SDimitry Andric CheckDI(isType(Ty), "invalid subroutine type ref", &N, Types, Ty); 12110b57cec5SDimitry Andric } 12120b57cec5SDimitry Andric } 1213*81ad6265SDimitry Andric CheckDI(!hasConflictingReferenceFlags(N.getFlags()), 12140b57cec5SDimitry Andric "invalid reference flags", &N); 12150b57cec5SDimitry Andric } 12160b57cec5SDimitry Andric 12170b57cec5SDimitry Andric void Verifier::visitDIFile(const DIFile &N) { 1218*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_file_type, "invalid tag", &N); 12190b57cec5SDimitry Andric Optional<DIFile::ChecksumInfo<StringRef>> Checksum = N.getChecksum(); 12200b57cec5SDimitry Andric if (Checksum) { 1221*81ad6265SDimitry Andric CheckDI(Checksum->Kind <= DIFile::ChecksumKind::CSK_Last, 12220b57cec5SDimitry Andric "invalid checksum kind", &N); 12230b57cec5SDimitry Andric size_t Size; 12240b57cec5SDimitry Andric switch (Checksum->Kind) { 12250b57cec5SDimitry Andric case DIFile::CSK_MD5: 12260b57cec5SDimitry Andric Size = 32; 12270b57cec5SDimitry Andric break; 12280b57cec5SDimitry Andric case DIFile::CSK_SHA1: 12290b57cec5SDimitry Andric Size = 40; 12300b57cec5SDimitry Andric break; 12315ffd83dbSDimitry Andric case DIFile::CSK_SHA256: 12325ffd83dbSDimitry Andric Size = 64; 12335ffd83dbSDimitry Andric break; 12340b57cec5SDimitry Andric } 1235*81ad6265SDimitry Andric CheckDI(Checksum->Value.size() == Size, "invalid checksum length", &N); 1236*81ad6265SDimitry Andric CheckDI(Checksum->Value.find_if_not(llvm::isHexDigit) == StringRef::npos, 12370b57cec5SDimitry Andric "invalid checksum", &N); 12380b57cec5SDimitry Andric } 12390b57cec5SDimitry Andric } 12400b57cec5SDimitry Andric 12410b57cec5SDimitry Andric void Verifier::visitDICompileUnit(const DICompileUnit &N) { 1242*81ad6265SDimitry Andric CheckDI(N.isDistinct(), "compile units must be distinct", &N); 1243*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_compile_unit, "invalid tag", &N); 12440b57cec5SDimitry Andric 12450b57cec5SDimitry Andric // Don't bother verifying the compilation directory or producer string 12460b57cec5SDimitry Andric // as those could be empty. 1247*81ad6265SDimitry Andric CheckDI(N.getRawFile() && isa<DIFile>(N.getRawFile()), "invalid file", &N, 12480b57cec5SDimitry Andric N.getRawFile()); 1249*81ad6265SDimitry Andric CheckDI(!N.getFile()->getFilename().empty(), "invalid filename", &N, 12500b57cec5SDimitry Andric N.getFile()); 12510b57cec5SDimitry Andric 1252e8d8bef9SDimitry Andric CurrentSourceLang = (dwarf::SourceLanguage)N.getSourceLanguage(); 1253e8d8bef9SDimitry Andric 12540b57cec5SDimitry Andric verifySourceDebugInfo(N, *N.getFile()); 12550b57cec5SDimitry Andric 1256*81ad6265SDimitry Andric CheckDI((N.getEmissionKind() <= DICompileUnit::LastEmissionKind), 12570b57cec5SDimitry Andric "invalid emission kind", &N); 12580b57cec5SDimitry Andric 12590b57cec5SDimitry Andric if (auto *Array = N.getRawEnumTypes()) { 1260*81ad6265SDimitry Andric CheckDI(isa<MDTuple>(Array), "invalid enum list", &N, Array); 12610b57cec5SDimitry Andric for (Metadata *Op : N.getEnumTypes()->operands()) { 12620b57cec5SDimitry Andric auto *Enum = dyn_cast_or_null<DICompositeType>(Op); 1263*81ad6265SDimitry Andric CheckDI(Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type, 12640b57cec5SDimitry Andric "invalid enum type", &N, N.getEnumTypes(), Op); 12650b57cec5SDimitry Andric } 12660b57cec5SDimitry Andric } 12670b57cec5SDimitry Andric if (auto *Array = N.getRawRetainedTypes()) { 1268*81ad6265SDimitry Andric CheckDI(isa<MDTuple>(Array), "invalid retained type list", &N, Array); 12690b57cec5SDimitry Andric for (Metadata *Op : N.getRetainedTypes()->operands()) { 1270*81ad6265SDimitry Andric CheckDI( 1271*81ad6265SDimitry Andric Op && (isa<DIType>(Op) || (isa<DISubprogram>(Op) && 12720b57cec5SDimitry Andric !cast<DISubprogram>(Op)->isDefinition())), 12730b57cec5SDimitry Andric "invalid retained type", &N, Op); 12740b57cec5SDimitry Andric } 12750b57cec5SDimitry Andric } 12760b57cec5SDimitry Andric if (auto *Array = N.getRawGlobalVariables()) { 1277*81ad6265SDimitry Andric CheckDI(isa<MDTuple>(Array), "invalid global variable list", &N, Array); 12780b57cec5SDimitry Andric for (Metadata *Op : N.getGlobalVariables()->operands()) { 1279*81ad6265SDimitry Andric CheckDI(Op && (isa<DIGlobalVariableExpression>(Op)), 12800b57cec5SDimitry Andric "invalid global variable ref", &N, Op); 12810b57cec5SDimitry Andric } 12820b57cec5SDimitry Andric } 12830b57cec5SDimitry Andric if (auto *Array = N.getRawImportedEntities()) { 1284*81ad6265SDimitry Andric CheckDI(isa<MDTuple>(Array), "invalid imported entity list", &N, Array); 12850b57cec5SDimitry Andric for (Metadata *Op : N.getImportedEntities()->operands()) { 1286*81ad6265SDimitry Andric CheckDI(Op && isa<DIImportedEntity>(Op), "invalid imported entity ref", 12870b57cec5SDimitry Andric &N, Op); 12880b57cec5SDimitry Andric } 12890b57cec5SDimitry Andric } 12900b57cec5SDimitry Andric if (auto *Array = N.getRawMacros()) { 1291*81ad6265SDimitry Andric CheckDI(isa<MDTuple>(Array), "invalid macro list", &N, Array); 12920b57cec5SDimitry Andric for (Metadata *Op : N.getMacros()->operands()) { 1293*81ad6265SDimitry Andric CheckDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op); 12940b57cec5SDimitry Andric } 12950b57cec5SDimitry Andric } 12960b57cec5SDimitry Andric CUVisited.insert(&N); 12970b57cec5SDimitry Andric } 12980b57cec5SDimitry Andric 12990b57cec5SDimitry Andric void Verifier::visitDISubprogram(const DISubprogram &N) { 1300*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_subprogram, "invalid tag", &N); 1301*81ad6265SDimitry Andric CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope()); 13020b57cec5SDimitry Andric if (auto *F = N.getRawFile()) 1303*81ad6265SDimitry Andric CheckDI(isa<DIFile>(F), "invalid file", &N, F); 13040b57cec5SDimitry Andric else 1305*81ad6265SDimitry Andric CheckDI(N.getLine() == 0, "line specified with no file", &N, N.getLine()); 13060b57cec5SDimitry Andric if (auto *T = N.getRawType()) 1307*81ad6265SDimitry Andric CheckDI(isa<DISubroutineType>(T), "invalid subroutine type", &N, T); 1308*81ad6265SDimitry Andric CheckDI(isType(N.getRawContainingType()), "invalid containing type", &N, 13090b57cec5SDimitry Andric N.getRawContainingType()); 13100b57cec5SDimitry Andric if (auto *Params = N.getRawTemplateParams()) 13110b57cec5SDimitry Andric visitTemplateParams(N, *Params); 13120b57cec5SDimitry Andric if (auto *S = N.getRawDeclaration()) 1313*81ad6265SDimitry Andric CheckDI(isa<DISubprogram>(S) && !cast<DISubprogram>(S)->isDefinition(), 13140b57cec5SDimitry Andric "invalid subprogram declaration", &N, S); 13150b57cec5SDimitry Andric if (auto *RawNode = N.getRawRetainedNodes()) { 13160b57cec5SDimitry Andric auto *Node = dyn_cast<MDTuple>(RawNode); 1317*81ad6265SDimitry Andric CheckDI(Node, "invalid retained nodes list", &N, RawNode); 13180b57cec5SDimitry Andric for (Metadata *Op : Node->operands()) { 1319*81ad6265SDimitry Andric CheckDI(Op && (isa<DILocalVariable>(Op) || isa<DILabel>(Op)), 1320*81ad6265SDimitry Andric "invalid retained nodes, expected DILocalVariable or DILabel", &N, 1321*81ad6265SDimitry Andric Node, Op); 13220b57cec5SDimitry Andric } 13230b57cec5SDimitry Andric } 1324*81ad6265SDimitry Andric CheckDI(!hasConflictingReferenceFlags(N.getFlags()), 13250b57cec5SDimitry Andric "invalid reference flags", &N); 13260b57cec5SDimitry Andric 13270b57cec5SDimitry Andric auto *Unit = N.getRawUnit(); 13280b57cec5SDimitry Andric if (N.isDefinition()) { 13290b57cec5SDimitry Andric // Subprogram definitions (not part of the type hierarchy). 1330*81ad6265SDimitry Andric CheckDI(N.isDistinct(), "subprogram definitions must be distinct", &N); 1331*81ad6265SDimitry Andric CheckDI(Unit, "subprogram definitions must have a compile unit", &N); 1332*81ad6265SDimitry Andric CheckDI(isa<DICompileUnit>(Unit), "invalid unit type", &N, Unit); 13330b57cec5SDimitry Andric if (N.getFile()) 13340b57cec5SDimitry Andric verifySourceDebugInfo(*N.getUnit(), *N.getFile()); 13350b57cec5SDimitry Andric } else { 13360b57cec5SDimitry Andric // Subprogram declarations (part of the type hierarchy). 1337*81ad6265SDimitry Andric CheckDI(!Unit, "subprogram declarations must not have a compile unit", &N); 13380b57cec5SDimitry Andric } 13390b57cec5SDimitry Andric 13400b57cec5SDimitry Andric if (auto *RawThrownTypes = N.getRawThrownTypes()) { 13410b57cec5SDimitry Andric auto *ThrownTypes = dyn_cast<MDTuple>(RawThrownTypes); 1342*81ad6265SDimitry Andric CheckDI(ThrownTypes, "invalid thrown types list", &N, RawThrownTypes); 13430b57cec5SDimitry Andric for (Metadata *Op : ThrownTypes->operands()) 1344*81ad6265SDimitry Andric CheckDI(Op && isa<DIType>(Op), "invalid thrown type", &N, ThrownTypes, 13450b57cec5SDimitry Andric Op); 13460b57cec5SDimitry Andric } 13470b57cec5SDimitry Andric 13480b57cec5SDimitry Andric if (N.areAllCallsDescribed()) 1349*81ad6265SDimitry Andric CheckDI(N.isDefinition(), 13500b57cec5SDimitry Andric "DIFlagAllCallsDescribed must be attached to a definition"); 13510b57cec5SDimitry Andric } 13520b57cec5SDimitry Andric 13530b57cec5SDimitry Andric void Verifier::visitDILexicalBlockBase(const DILexicalBlockBase &N) { 1354*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_lexical_block, "invalid tag", &N); 1355*81ad6265SDimitry Andric CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()), 13560b57cec5SDimitry Andric "invalid local scope", &N, N.getRawScope()); 13570b57cec5SDimitry Andric if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope())) 1358*81ad6265SDimitry Andric CheckDI(SP->isDefinition(), "scope points into the type hierarchy", &N); 13590b57cec5SDimitry Andric } 13600b57cec5SDimitry Andric 13610b57cec5SDimitry Andric void Verifier::visitDILexicalBlock(const DILexicalBlock &N) { 13620b57cec5SDimitry Andric visitDILexicalBlockBase(N); 13630b57cec5SDimitry Andric 1364*81ad6265SDimitry Andric CheckDI(N.getLine() || !N.getColumn(), 13650b57cec5SDimitry Andric "cannot have column info without line info", &N); 13660b57cec5SDimitry Andric } 13670b57cec5SDimitry Andric 13680b57cec5SDimitry Andric void Verifier::visitDILexicalBlockFile(const DILexicalBlockFile &N) { 13690b57cec5SDimitry Andric visitDILexicalBlockBase(N); 13700b57cec5SDimitry Andric } 13710b57cec5SDimitry Andric 13720b57cec5SDimitry Andric void Verifier::visitDICommonBlock(const DICommonBlock &N) { 1373*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_common_block, "invalid tag", &N); 13740b57cec5SDimitry Andric if (auto *S = N.getRawScope()) 1375*81ad6265SDimitry Andric CheckDI(isa<DIScope>(S), "invalid scope ref", &N, S); 13760b57cec5SDimitry Andric if (auto *S = N.getRawDecl()) 1377*81ad6265SDimitry Andric CheckDI(isa<DIGlobalVariable>(S), "invalid declaration", &N, S); 13780b57cec5SDimitry Andric } 13790b57cec5SDimitry Andric 13800b57cec5SDimitry Andric void Verifier::visitDINamespace(const DINamespace &N) { 1381*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_namespace, "invalid tag", &N); 13820b57cec5SDimitry Andric if (auto *S = N.getRawScope()) 1383*81ad6265SDimitry Andric CheckDI(isa<DIScope>(S), "invalid scope ref", &N, S); 13840b57cec5SDimitry Andric } 13850b57cec5SDimitry Andric 13860b57cec5SDimitry Andric void Verifier::visitDIMacro(const DIMacro &N) { 1387*81ad6265SDimitry Andric CheckDI(N.getMacinfoType() == dwarf::DW_MACINFO_define || 13880b57cec5SDimitry Andric N.getMacinfoType() == dwarf::DW_MACINFO_undef, 13890b57cec5SDimitry Andric "invalid macinfo type", &N); 1390*81ad6265SDimitry Andric CheckDI(!N.getName().empty(), "anonymous macro", &N); 13910b57cec5SDimitry Andric if (!N.getValue().empty()) { 13920b57cec5SDimitry Andric assert(N.getValue().data()[0] != ' ' && "Macro value has a space prefix"); 13930b57cec5SDimitry Andric } 13940b57cec5SDimitry Andric } 13950b57cec5SDimitry Andric 13960b57cec5SDimitry Andric void Verifier::visitDIMacroFile(const DIMacroFile &N) { 1397*81ad6265SDimitry Andric CheckDI(N.getMacinfoType() == dwarf::DW_MACINFO_start_file, 13980b57cec5SDimitry Andric "invalid macinfo type", &N); 13990b57cec5SDimitry Andric if (auto *F = N.getRawFile()) 1400*81ad6265SDimitry Andric CheckDI(isa<DIFile>(F), "invalid file", &N, F); 14010b57cec5SDimitry Andric 14020b57cec5SDimitry Andric if (auto *Array = N.getRawElements()) { 1403*81ad6265SDimitry Andric CheckDI(isa<MDTuple>(Array), "invalid macro list", &N, Array); 14040b57cec5SDimitry Andric for (Metadata *Op : N.getElements()->operands()) { 1405*81ad6265SDimitry Andric CheckDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op); 14060b57cec5SDimitry Andric } 14070b57cec5SDimitry Andric } 14080b57cec5SDimitry Andric } 14090b57cec5SDimitry Andric 1410fe6060f1SDimitry Andric void Verifier::visitDIArgList(const DIArgList &N) { 1411*81ad6265SDimitry Andric CheckDI(!N.getNumOperands(), 1412fe6060f1SDimitry Andric "DIArgList should have no operands other than a list of " 1413fe6060f1SDimitry Andric "ValueAsMetadata", 1414fe6060f1SDimitry Andric &N); 1415fe6060f1SDimitry Andric } 1416fe6060f1SDimitry Andric 14170b57cec5SDimitry Andric void Verifier::visitDIModule(const DIModule &N) { 1418*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_module, "invalid tag", &N); 1419*81ad6265SDimitry Andric CheckDI(!N.getName().empty(), "anonymous module", &N); 14200b57cec5SDimitry Andric } 14210b57cec5SDimitry Andric 14220b57cec5SDimitry Andric void Verifier::visitDITemplateParameter(const DITemplateParameter &N) { 1423*81ad6265SDimitry Andric CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType()); 14240b57cec5SDimitry Andric } 14250b57cec5SDimitry Andric 14260b57cec5SDimitry Andric void Verifier::visitDITemplateTypeParameter(const DITemplateTypeParameter &N) { 14270b57cec5SDimitry Andric visitDITemplateParameter(N); 14280b57cec5SDimitry Andric 1429*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_template_type_parameter, "invalid tag", 14300b57cec5SDimitry Andric &N); 14310b57cec5SDimitry Andric } 14320b57cec5SDimitry Andric 14330b57cec5SDimitry Andric void Verifier::visitDITemplateValueParameter( 14340b57cec5SDimitry Andric const DITemplateValueParameter &N) { 14350b57cec5SDimitry Andric visitDITemplateParameter(N); 14360b57cec5SDimitry Andric 1437*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_template_value_parameter || 14380b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_GNU_template_template_param || 14390b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack, 14400b57cec5SDimitry Andric "invalid tag", &N); 14410b57cec5SDimitry Andric } 14420b57cec5SDimitry Andric 14430b57cec5SDimitry Andric void Verifier::visitDIVariable(const DIVariable &N) { 14440b57cec5SDimitry Andric if (auto *S = N.getRawScope()) 1445*81ad6265SDimitry Andric CheckDI(isa<DIScope>(S), "invalid scope", &N, S); 14460b57cec5SDimitry Andric if (auto *F = N.getRawFile()) 1447*81ad6265SDimitry Andric CheckDI(isa<DIFile>(F), "invalid file", &N, F); 14480b57cec5SDimitry Andric } 14490b57cec5SDimitry Andric 14500b57cec5SDimitry Andric void Verifier::visitDIGlobalVariable(const DIGlobalVariable &N) { 14510b57cec5SDimitry Andric // Checks common to all variables. 14520b57cec5SDimitry Andric visitDIVariable(N); 14530b57cec5SDimitry Andric 1454*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N); 1455*81ad6265SDimitry Andric CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType()); 1456*81ad6265SDimitry Andric // Check only if the global variable is not an extern 14575ffd83dbSDimitry Andric if (N.isDefinition()) 1458*81ad6265SDimitry Andric CheckDI(N.getType(), "missing global variable type", &N); 14590b57cec5SDimitry Andric if (auto *Member = N.getRawStaticDataMemberDeclaration()) { 1460*81ad6265SDimitry Andric CheckDI(isa<DIDerivedType>(Member), 14610b57cec5SDimitry Andric "invalid static data member declaration", &N, Member); 14620b57cec5SDimitry Andric } 14630b57cec5SDimitry Andric } 14640b57cec5SDimitry Andric 14650b57cec5SDimitry Andric void Verifier::visitDILocalVariable(const DILocalVariable &N) { 14660b57cec5SDimitry Andric // Checks common to all variables. 14670b57cec5SDimitry Andric visitDIVariable(N); 14680b57cec5SDimitry Andric 1469*81ad6265SDimitry Andric CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType()); 1470*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N); 1471*81ad6265SDimitry Andric CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()), 14720b57cec5SDimitry Andric "local variable requires a valid scope", &N, N.getRawScope()); 14730b57cec5SDimitry Andric if (auto Ty = N.getType()) 1474*81ad6265SDimitry Andric CheckDI(!isa<DISubroutineType>(Ty), "invalid type", &N, N.getType()); 14750b57cec5SDimitry Andric } 14760b57cec5SDimitry Andric 14770b57cec5SDimitry Andric void Verifier::visitDILabel(const DILabel &N) { 14780b57cec5SDimitry Andric if (auto *S = N.getRawScope()) 1479*81ad6265SDimitry Andric CheckDI(isa<DIScope>(S), "invalid scope", &N, S); 14800b57cec5SDimitry Andric if (auto *F = N.getRawFile()) 1481*81ad6265SDimitry Andric CheckDI(isa<DIFile>(F), "invalid file", &N, F); 14820b57cec5SDimitry Andric 1483*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_label, "invalid tag", &N); 1484*81ad6265SDimitry Andric CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()), 14850b57cec5SDimitry Andric "label requires a valid scope", &N, N.getRawScope()); 14860b57cec5SDimitry Andric } 14870b57cec5SDimitry Andric 14880b57cec5SDimitry Andric void Verifier::visitDIExpression(const DIExpression &N) { 1489*81ad6265SDimitry Andric CheckDI(N.isValid(), "invalid expression", &N); 14900b57cec5SDimitry Andric } 14910b57cec5SDimitry Andric 14920b57cec5SDimitry Andric void Verifier::visitDIGlobalVariableExpression( 14930b57cec5SDimitry Andric const DIGlobalVariableExpression &GVE) { 1494*81ad6265SDimitry Andric CheckDI(GVE.getVariable(), "missing variable"); 14950b57cec5SDimitry Andric if (auto *Var = GVE.getVariable()) 14960b57cec5SDimitry Andric visitDIGlobalVariable(*Var); 14970b57cec5SDimitry Andric if (auto *Expr = GVE.getExpression()) { 14980b57cec5SDimitry Andric visitDIExpression(*Expr); 14990b57cec5SDimitry Andric if (auto Fragment = Expr->getFragmentInfo()) 15000b57cec5SDimitry Andric verifyFragmentExpression(*GVE.getVariable(), *Fragment, &GVE); 15010b57cec5SDimitry Andric } 15020b57cec5SDimitry Andric } 15030b57cec5SDimitry Andric 15040b57cec5SDimitry Andric void Verifier::visitDIObjCProperty(const DIObjCProperty &N) { 1505*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_APPLE_property, "invalid tag", &N); 15060b57cec5SDimitry Andric if (auto *T = N.getRawType()) 1507*81ad6265SDimitry Andric CheckDI(isType(T), "invalid type ref", &N, T); 15080b57cec5SDimitry Andric if (auto *F = N.getRawFile()) 1509*81ad6265SDimitry Andric CheckDI(isa<DIFile>(F), "invalid file", &N, F); 15100b57cec5SDimitry Andric } 15110b57cec5SDimitry Andric 15120b57cec5SDimitry Andric void Verifier::visitDIImportedEntity(const DIImportedEntity &N) { 1513*81ad6265SDimitry Andric CheckDI(N.getTag() == dwarf::DW_TAG_imported_module || 15140b57cec5SDimitry Andric N.getTag() == dwarf::DW_TAG_imported_declaration, 15150b57cec5SDimitry Andric "invalid tag", &N); 15160b57cec5SDimitry Andric if (auto *S = N.getRawScope()) 1517*81ad6265SDimitry Andric CheckDI(isa<DIScope>(S), "invalid scope for imported entity", &N, S); 1518*81ad6265SDimitry Andric CheckDI(isDINode(N.getRawEntity()), "invalid imported entity", &N, 15190b57cec5SDimitry Andric N.getRawEntity()); 15200b57cec5SDimitry Andric } 15210b57cec5SDimitry Andric 15220b57cec5SDimitry Andric void Verifier::visitComdat(const Comdat &C) { 15238bcb0991SDimitry Andric // In COFF the Module is invalid if the GlobalValue has private linkage. 15248bcb0991SDimitry Andric // Entities with private linkage don't have entries in the symbol table. 15258bcb0991SDimitry Andric if (TT.isOSBinFormatCOFF()) 15260b57cec5SDimitry Andric if (const GlobalValue *GV = M.getNamedValue(C.getName())) 1527*81ad6265SDimitry Andric Check(!GV->hasPrivateLinkage(), "comdat global value has private linkage", 1528*81ad6265SDimitry Andric GV); 15290b57cec5SDimitry Andric } 15300b57cec5SDimitry Andric 1531349cc55cSDimitry Andric void Verifier::visitModuleIdents() { 15320b57cec5SDimitry Andric const NamedMDNode *Idents = M.getNamedMetadata("llvm.ident"); 15330b57cec5SDimitry Andric if (!Idents) 15340b57cec5SDimitry Andric return; 15350b57cec5SDimitry Andric 15360b57cec5SDimitry Andric // llvm.ident takes a list of metadata entry. Each entry has only one string. 15370b57cec5SDimitry Andric // Scan each llvm.ident entry and make sure that this requirement is met. 15380b57cec5SDimitry Andric for (const MDNode *N : Idents->operands()) { 1539*81ad6265SDimitry Andric Check(N->getNumOperands() == 1, 15400b57cec5SDimitry Andric "incorrect number of operands in llvm.ident metadata", N); 1541*81ad6265SDimitry Andric Check(dyn_cast_or_null<MDString>(N->getOperand(0)), 15420b57cec5SDimitry Andric ("invalid value for llvm.ident metadata entry operand" 15430b57cec5SDimitry Andric "(the operand should be a string)"), 15440b57cec5SDimitry Andric N->getOperand(0)); 15450b57cec5SDimitry Andric } 15460b57cec5SDimitry Andric } 15470b57cec5SDimitry Andric 1548349cc55cSDimitry Andric void Verifier::visitModuleCommandLines() { 15490b57cec5SDimitry Andric const NamedMDNode *CommandLines = M.getNamedMetadata("llvm.commandline"); 15500b57cec5SDimitry Andric if (!CommandLines) 15510b57cec5SDimitry Andric return; 15520b57cec5SDimitry Andric 15530b57cec5SDimitry Andric // llvm.commandline takes a list of metadata entry. Each entry has only one 15540b57cec5SDimitry Andric // string. Scan each llvm.commandline entry and make sure that this 15550b57cec5SDimitry Andric // requirement is met. 15560b57cec5SDimitry Andric for (const MDNode *N : CommandLines->operands()) { 1557*81ad6265SDimitry Andric Check(N->getNumOperands() == 1, 15580b57cec5SDimitry Andric "incorrect number of operands in llvm.commandline metadata", N); 1559*81ad6265SDimitry Andric Check(dyn_cast_or_null<MDString>(N->getOperand(0)), 15600b57cec5SDimitry Andric ("invalid value for llvm.commandline metadata entry operand" 15610b57cec5SDimitry Andric "(the operand should be a string)"), 15620b57cec5SDimitry Andric N->getOperand(0)); 15630b57cec5SDimitry Andric } 15640b57cec5SDimitry Andric } 15650b57cec5SDimitry Andric 1566349cc55cSDimitry Andric void Verifier::visitModuleFlags() { 15670b57cec5SDimitry Andric const NamedMDNode *Flags = M.getModuleFlagsMetadata(); 15680b57cec5SDimitry Andric if (!Flags) return; 15690b57cec5SDimitry Andric 15700b57cec5SDimitry Andric // Scan each flag, and track the flags and requirements. 15710b57cec5SDimitry Andric DenseMap<const MDString*, const MDNode*> SeenIDs; 15720b57cec5SDimitry Andric SmallVector<const MDNode*, 16> Requirements; 15730b57cec5SDimitry Andric for (const MDNode *MDN : Flags->operands()) 15740b57cec5SDimitry Andric visitModuleFlag(MDN, SeenIDs, Requirements); 15750b57cec5SDimitry Andric 15760b57cec5SDimitry Andric // Validate that the requirements in the module are valid. 15770b57cec5SDimitry Andric for (const MDNode *Requirement : Requirements) { 15780b57cec5SDimitry Andric const MDString *Flag = cast<MDString>(Requirement->getOperand(0)); 15790b57cec5SDimitry Andric const Metadata *ReqValue = Requirement->getOperand(1); 15800b57cec5SDimitry Andric 15810b57cec5SDimitry Andric const MDNode *Op = SeenIDs.lookup(Flag); 15820b57cec5SDimitry Andric if (!Op) { 15830b57cec5SDimitry Andric CheckFailed("invalid requirement on flag, flag is not present in module", 15840b57cec5SDimitry Andric Flag); 15850b57cec5SDimitry Andric continue; 15860b57cec5SDimitry Andric } 15870b57cec5SDimitry Andric 15880b57cec5SDimitry Andric if (Op->getOperand(2) != ReqValue) { 15890b57cec5SDimitry Andric CheckFailed(("invalid requirement on flag, " 15900b57cec5SDimitry Andric "flag does not have the required value"), 15910b57cec5SDimitry Andric Flag); 15920b57cec5SDimitry Andric continue; 15930b57cec5SDimitry Andric } 15940b57cec5SDimitry Andric } 15950b57cec5SDimitry Andric } 15960b57cec5SDimitry Andric 15970b57cec5SDimitry Andric void 15980b57cec5SDimitry Andric Verifier::visitModuleFlag(const MDNode *Op, 15990b57cec5SDimitry Andric DenseMap<const MDString *, const MDNode *> &SeenIDs, 16000b57cec5SDimitry Andric SmallVectorImpl<const MDNode *> &Requirements) { 16010b57cec5SDimitry Andric // Each module flag should have three arguments, the merge behavior (a 16020b57cec5SDimitry Andric // constant int), the flag ID (an MDString), and the value. 1603*81ad6265SDimitry Andric Check(Op->getNumOperands() == 3, 16040b57cec5SDimitry Andric "incorrect number of operands in module flag", Op); 16050b57cec5SDimitry Andric Module::ModFlagBehavior MFB; 16060b57cec5SDimitry Andric if (!Module::isValidModFlagBehavior(Op->getOperand(0), MFB)) { 1607*81ad6265SDimitry Andric Check(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0)), 16080b57cec5SDimitry Andric "invalid behavior operand in module flag (expected constant integer)", 16090b57cec5SDimitry Andric Op->getOperand(0)); 1610*81ad6265SDimitry Andric Check(false, 16110b57cec5SDimitry Andric "invalid behavior operand in module flag (unexpected constant)", 16120b57cec5SDimitry Andric Op->getOperand(0)); 16130b57cec5SDimitry Andric } 16140b57cec5SDimitry Andric MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1)); 1615*81ad6265SDimitry Andric Check(ID, "invalid ID operand in module flag (expected metadata string)", 16160b57cec5SDimitry Andric Op->getOperand(1)); 16170b57cec5SDimitry Andric 16184824e7fdSDimitry Andric // Check the values for behaviors with additional requirements. 16190b57cec5SDimitry Andric switch (MFB) { 16200b57cec5SDimitry Andric case Module::Error: 16210b57cec5SDimitry Andric case Module::Warning: 16220b57cec5SDimitry Andric case Module::Override: 16230b57cec5SDimitry Andric // These behavior types accept any value. 16240b57cec5SDimitry Andric break; 16250b57cec5SDimitry Andric 1626*81ad6265SDimitry Andric case Module::Min: { 1627*81ad6265SDimitry Andric Check(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2)), 1628*81ad6265SDimitry Andric "invalid value for 'min' module flag (expected constant integer)", 1629*81ad6265SDimitry Andric Op->getOperand(2)); 1630*81ad6265SDimitry Andric break; 1631*81ad6265SDimitry Andric } 1632*81ad6265SDimitry Andric 16330b57cec5SDimitry Andric case Module::Max: { 1634*81ad6265SDimitry Andric Check(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2)), 16350b57cec5SDimitry Andric "invalid value for 'max' module flag (expected constant integer)", 16360b57cec5SDimitry Andric Op->getOperand(2)); 16370b57cec5SDimitry Andric break; 16380b57cec5SDimitry Andric } 16390b57cec5SDimitry Andric 16400b57cec5SDimitry Andric case Module::Require: { 16410b57cec5SDimitry Andric // The value should itself be an MDNode with two operands, a flag ID (an 16420b57cec5SDimitry Andric // MDString), and a value. 16430b57cec5SDimitry Andric MDNode *Value = dyn_cast<MDNode>(Op->getOperand(2)); 1644*81ad6265SDimitry Andric Check(Value && Value->getNumOperands() == 2, 16450b57cec5SDimitry Andric "invalid value for 'require' module flag (expected metadata pair)", 16460b57cec5SDimitry Andric Op->getOperand(2)); 1647*81ad6265SDimitry Andric Check(isa<MDString>(Value->getOperand(0)), 16480b57cec5SDimitry Andric ("invalid value for 'require' module flag " 16490b57cec5SDimitry Andric "(first value operand should be a string)"), 16500b57cec5SDimitry Andric Value->getOperand(0)); 16510b57cec5SDimitry Andric 16520b57cec5SDimitry Andric // Append it to the list of requirements, to check once all module flags are 16530b57cec5SDimitry Andric // scanned. 16540b57cec5SDimitry Andric Requirements.push_back(Value); 16550b57cec5SDimitry Andric break; 16560b57cec5SDimitry Andric } 16570b57cec5SDimitry Andric 16580b57cec5SDimitry Andric case Module::Append: 16590b57cec5SDimitry Andric case Module::AppendUnique: { 16600b57cec5SDimitry Andric // These behavior types require the operand be an MDNode. 1661*81ad6265SDimitry Andric Check(isa<MDNode>(Op->getOperand(2)), 16620b57cec5SDimitry Andric "invalid value for 'append'-type module flag " 16630b57cec5SDimitry Andric "(expected a metadata node)", 16640b57cec5SDimitry Andric Op->getOperand(2)); 16650b57cec5SDimitry Andric break; 16660b57cec5SDimitry Andric } 16670b57cec5SDimitry Andric } 16680b57cec5SDimitry Andric 16690b57cec5SDimitry Andric // Unless this is a "requires" flag, check the ID is unique. 16700b57cec5SDimitry Andric if (MFB != Module::Require) { 16710b57cec5SDimitry Andric bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second; 1672*81ad6265SDimitry Andric Check(Inserted, 16730b57cec5SDimitry Andric "module flag identifiers must be unique (or of 'require' type)", ID); 16740b57cec5SDimitry Andric } 16750b57cec5SDimitry Andric 16760b57cec5SDimitry Andric if (ID->getString() == "wchar_size") { 16770b57cec5SDimitry Andric ConstantInt *Value 16780b57cec5SDimitry Andric = mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2)); 1679*81ad6265SDimitry Andric Check(Value, "wchar_size metadata requires constant integer argument"); 16800b57cec5SDimitry Andric } 16810b57cec5SDimitry Andric 16820b57cec5SDimitry Andric if (ID->getString() == "Linker Options") { 16830b57cec5SDimitry Andric // If the llvm.linker.options named metadata exists, we assume that the 16840b57cec5SDimitry Andric // bitcode reader has upgraded the module flag. Otherwise the flag might 16850b57cec5SDimitry Andric // have been created by a client directly. 1686*81ad6265SDimitry Andric Check(M.getNamedMetadata("llvm.linker.options"), 16870b57cec5SDimitry Andric "'Linker Options' named metadata no longer supported"); 16880b57cec5SDimitry Andric } 16890b57cec5SDimitry Andric 16905ffd83dbSDimitry Andric if (ID->getString() == "SemanticInterposition") { 16915ffd83dbSDimitry Andric ConstantInt *Value = 16925ffd83dbSDimitry Andric mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2)); 1693*81ad6265SDimitry Andric Check(Value, 16945ffd83dbSDimitry Andric "SemanticInterposition metadata requires constant integer argument"); 16955ffd83dbSDimitry Andric } 16965ffd83dbSDimitry Andric 16970b57cec5SDimitry Andric if (ID->getString() == "CG Profile") { 16980b57cec5SDimitry Andric for (const MDOperand &MDO : cast<MDNode>(Op->getOperand(2))->operands()) 16990b57cec5SDimitry Andric visitModuleFlagCGProfileEntry(MDO); 17000b57cec5SDimitry Andric } 17010b57cec5SDimitry Andric } 17020b57cec5SDimitry Andric 17030b57cec5SDimitry Andric void Verifier::visitModuleFlagCGProfileEntry(const MDOperand &MDO) { 17040b57cec5SDimitry Andric auto CheckFunction = [&](const MDOperand &FuncMDO) { 17050b57cec5SDimitry Andric if (!FuncMDO) 17060b57cec5SDimitry Andric return; 17070b57cec5SDimitry Andric auto F = dyn_cast<ValueAsMetadata>(FuncMDO); 1708*81ad6265SDimitry Andric Check(F && isa<Function>(F->getValue()->stripPointerCasts()), 1709e8d8bef9SDimitry Andric "expected a Function or null", FuncMDO); 17100b57cec5SDimitry Andric }; 17110b57cec5SDimitry Andric auto Node = dyn_cast_or_null<MDNode>(MDO); 1712*81ad6265SDimitry Andric Check(Node && Node->getNumOperands() == 3, "expected a MDNode triple", MDO); 17130b57cec5SDimitry Andric CheckFunction(Node->getOperand(0)); 17140b57cec5SDimitry Andric CheckFunction(Node->getOperand(1)); 17150b57cec5SDimitry Andric auto Count = dyn_cast_or_null<ConstantAsMetadata>(Node->getOperand(2)); 1716*81ad6265SDimitry Andric Check(Count && Count->getType()->isIntegerTy(), 17170b57cec5SDimitry Andric "expected an integer constant", Node->getOperand(2)); 17180b57cec5SDimitry Andric } 17190b57cec5SDimitry Andric 1720fe6060f1SDimitry Andric void Verifier::verifyAttributeTypes(AttributeSet Attrs, const Value *V) { 17210b57cec5SDimitry Andric for (Attribute A : Attrs) { 1722fe6060f1SDimitry Andric 1723fe6060f1SDimitry Andric if (A.isStringAttribute()) { 1724fe6060f1SDimitry Andric #define GET_ATTR_NAMES 1725fe6060f1SDimitry Andric #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) 1726fe6060f1SDimitry Andric #define ATTRIBUTE_STRBOOL(ENUM_NAME, DISPLAY_NAME) \ 1727fe6060f1SDimitry Andric if (A.getKindAsString() == #DISPLAY_NAME) { \ 1728fe6060f1SDimitry Andric auto V = A.getValueAsString(); \ 1729fe6060f1SDimitry Andric if (!(V.empty() || V == "true" || V == "false")) \ 1730fe6060f1SDimitry Andric CheckFailed("invalid value for '" #DISPLAY_NAME "' attribute: " + V + \ 1731fe6060f1SDimitry Andric ""); \ 1732fe6060f1SDimitry Andric } 1733fe6060f1SDimitry Andric 1734fe6060f1SDimitry Andric #include "llvm/IR/Attributes.inc" 17350b57cec5SDimitry Andric continue; 1736fe6060f1SDimitry Andric } 17370b57cec5SDimitry Andric 1738fe6060f1SDimitry Andric if (A.isIntAttribute() != Attribute::isIntAttrKind(A.getKindAsEnum())) { 17395ffd83dbSDimitry Andric CheckFailed("Attribute '" + A.getAsString() + "' should have an Argument", 17405ffd83dbSDimitry Andric V); 17415ffd83dbSDimitry Andric return; 17425ffd83dbSDimitry Andric } 17430b57cec5SDimitry Andric } 17440b57cec5SDimitry Andric } 17450b57cec5SDimitry Andric 17460b57cec5SDimitry Andric // VerifyParameterAttrs - Check the given attributes for an argument or return 17470b57cec5SDimitry Andric // value of the specified type. The value V is printed in error messages. 17480b57cec5SDimitry Andric void Verifier::verifyParameterAttrs(AttributeSet Attrs, Type *Ty, 17490b57cec5SDimitry Andric const Value *V) { 17500b57cec5SDimitry Andric if (!Attrs.hasAttributes()) 17510b57cec5SDimitry Andric return; 17520b57cec5SDimitry Andric 1753fe6060f1SDimitry Andric verifyAttributeTypes(Attrs, V); 1754fe6060f1SDimitry Andric 1755fe6060f1SDimitry Andric for (Attribute Attr : Attrs) 1756*81ad6265SDimitry Andric Check(Attr.isStringAttribute() || 1757fe6060f1SDimitry Andric Attribute::canUseAsParamAttr(Attr.getKindAsEnum()), 1758*81ad6265SDimitry Andric "Attribute '" + Attr.getAsString() + "' does not apply to parameters", 1759fe6060f1SDimitry Andric V); 17600b57cec5SDimitry Andric 17610b57cec5SDimitry Andric if (Attrs.hasAttribute(Attribute::ImmArg)) { 1762*81ad6265SDimitry Andric Check(Attrs.getNumAttributes() == 1, 17630b57cec5SDimitry Andric "Attribute 'immarg' is incompatible with other attributes", V); 17640b57cec5SDimitry Andric } 17650b57cec5SDimitry Andric 17660b57cec5SDimitry Andric // Check for mutually incompatible attributes. Only inreg is compatible with 17670b57cec5SDimitry Andric // sret. 17680b57cec5SDimitry Andric unsigned AttrCount = 0; 17690b57cec5SDimitry Andric AttrCount += Attrs.hasAttribute(Attribute::ByVal); 17700b57cec5SDimitry Andric AttrCount += Attrs.hasAttribute(Attribute::InAlloca); 17715ffd83dbSDimitry Andric AttrCount += Attrs.hasAttribute(Attribute::Preallocated); 17720b57cec5SDimitry Andric AttrCount += Attrs.hasAttribute(Attribute::StructRet) || 17730b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::InReg); 17740b57cec5SDimitry Andric AttrCount += Attrs.hasAttribute(Attribute::Nest); 1775e8d8bef9SDimitry Andric AttrCount += Attrs.hasAttribute(Attribute::ByRef); 1776*81ad6265SDimitry Andric Check(AttrCount <= 1, 17775ffd83dbSDimitry Andric "Attributes 'byval', 'inalloca', 'preallocated', 'inreg', 'nest', " 1778e8d8bef9SDimitry Andric "'byref', and 'sret' are incompatible!", 17790b57cec5SDimitry Andric V); 17800b57cec5SDimitry Andric 1781*81ad6265SDimitry Andric Check(!(Attrs.hasAttribute(Attribute::InAlloca) && 17820b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::ReadOnly)), 17830b57cec5SDimitry Andric "Attributes " 17840b57cec5SDimitry Andric "'inalloca and readonly' are incompatible!", 17850b57cec5SDimitry Andric V); 17860b57cec5SDimitry Andric 1787*81ad6265SDimitry Andric Check(!(Attrs.hasAttribute(Attribute::StructRet) && 17880b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::Returned)), 17890b57cec5SDimitry Andric "Attributes " 17900b57cec5SDimitry Andric "'sret and returned' are incompatible!", 17910b57cec5SDimitry Andric V); 17920b57cec5SDimitry Andric 1793*81ad6265SDimitry Andric Check(!(Attrs.hasAttribute(Attribute::ZExt) && 17940b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::SExt)), 17950b57cec5SDimitry Andric "Attributes " 17960b57cec5SDimitry Andric "'zeroext and signext' are incompatible!", 17970b57cec5SDimitry Andric V); 17980b57cec5SDimitry Andric 1799*81ad6265SDimitry Andric Check(!(Attrs.hasAttribute(Attribute::ReadNone) && 18000b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::ReadOnly)), 18010b57cec5SDimitry Andric "Attributes " 18020b57cec5SDimitry Andric "'readnone and readonly' are incompatible!", 18030b57cec5SDimitry Andric V); 18040b57cec5SDimitry Andric 1805*81ad6265SDimitry Andric Check(!(Attrs.hasAttribute(Attribute::ReadNone) && 18060b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::WriteOnly)), 18070b57cec5SDimitry Andric "Attributes " 18080b57cec5SDimitry Andric "'readnone and writeonly' are incompatible!", 18090b57cec5SDimitry Andric V); 18100b57cec5SDimitry Andric 1811*81ad6265SDimitry Andric Check(!(Attrs.hasAttribute(Attribute::ReadOnly) && 18120b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::WriteOnly)), 18130b57cec5SDimitry Andric "Attributes " 18140b57cec5SDimitry Andric "'readonly and writeonly' are incompatible!", 18150b57cec5SDimitry Andric V); 18160b57cec5SDimitry Andric 1817*81ad6265SDimitry Andric Check(!(Attrs.hasAttribute(Attribute::NoInline) && 18180b57cec5SDimitry Andric Attrs.hasAttribute(Attribute::AlwaysInline)), 18190b57cec5SDimitry Andric "Attributes " 18200b57cec5SDimitry Andric "'noinline and alwaysinline' are incompatible!", 18210b57cec5SDimitry Andric V); 18220b57cec5SDimitry Andric 182304eeddc0SDimitry Andric AttributeMask IncompatibleAttrs = AttributeFuncs::typeIncompatible(Ty); 1824fe6060f1SDimitry Andric for (Attribute Attr : Attrs) { 1825fe6060f1SDimitry Andric if (!Attr.isStringAttribute() && 1826fe6060f1SDimitry Andric IncompatibleAttrs.contains(Attr.getKindAsEnum())) { 1827fe6060f1SDimitry Andric CheckFailed("Attribute '" + Attr.getAsString() + 1828fe6060f1SDimitry Andric "' applied to incompatible type!", V); 1829fe6060f1SDimitry Andric return; 1830fe6060f1SDimitry Andric } 1831fe6060f1SDimitry Andric } 18320b57cec5SDimitry Andric 18330b57cec5SDimitry Andric if (PointerType *PTy = dyn_cast<PointerType>(Ty)) { 1834fe6060f1SDimitry Andric if (Attrs.hasAttribute(Attribute::ByVal)) { 1835*81ad6265SDimitry Andric if (Attrs.hasAttribute(Attribute::Alignment)) { 1836*81ad6265SDimitry Andric Align AttrAlign = Attrs.getAlignment().valueOrOne(); 1837*81ad6265SDimitry Andric Align MaxAlign(ParamMaxAlignment); 1838*81ad6265SDimitry Andric Check(AttrAlign <= MaxAlign, 1839*81ad6265SDimitry Andric "Attribute 'align' exceed the max size 2^14", V); 1840*81ad6265SDimitry Andric } 18410b57cec5SDimitry Andric SmallPtrSet<Type *, 4> Visited; 1842*81ad6265SDimitry Andric Check(Attrs.getByValType()->isSized(&Visited), 1843fe6060f1SDimitry Andric "Attribute 'byval' does not support unsized types!", V); 18440b57cec5SDimitry Andric } 1845fe6060f1SDimitry Andric if (Attrs.hasAttribute(Attribute::ByRef)) { 1846fe6060f1SDimitry Andric SmallPtrSet<Type *, 4> Visited; 1847*81ad6265SDimitry Andric Check(Attrs.getByRefType()->isSized(&Visited), 1848fe6060f1SDimitry Andric "Attribute 'byref' does not support unsized types!", V); 1849fe6060f1SDimitry Andric } 1850fe6060f1SDimitry Andric if (Attrs.hasAttribute(Attribute::InAlloca)) { 1851fe6060f1SDimitry Andric SmallPtrSet<Type *, 4> Visited; 1852*81ad6265SDimitry Andric Check(Attrs.getInAllocaType()->isSized(&Visited), 1853fe6060f1SDimitry Andric "Attribute 'inalloca' does not support unsized types!", V); 1854fe6060f1SDimitry Andric } 1855fe6060f1SDimitry Andric if (Attrs.hasAttribute(Attribute::Preallocated)) { 1856fe6060f1SDimitry Andric SmallPtrSet<Type *, 4> Visited; 1857*81ad6265SDimitry Andric Check(Attrs.getPreallocatedType()->isSized(&Visited), 1858fe6060f1SDimitry Andric "Attribute 'preallocated' does not support unsized types!", V); 1859fe6060f1SDimitry Andric } 1860fe6060f1SDimitry Andric if (!PTy->isOpaque()) { 186104eeddc0SDimitry Andric if (!isa<PointerType>(PTy->getNonOpaquePointerElementType())) 1862*81ad6265SDimitry Andric Check(!Attrs.hasAttribute(Attribute::SwiftError), 18630b57cec5SDimitry Andric "Attribute 'swifterror' only applies to parameters " 18640b57cec5SDimitry Andric "with pointer to pointer type!", 18650b57cec5SDimitry Andric V); 1866e8d8bef9SDimitry Andric if (Attrs.hasAttribute(Attribute::ByRef)) { 1867*81ad6265SDimitry Andric Check(Attrs.getByRefType() == PTy->getNonOpaquePointerElementType(), 1868e8d8bef9SDimitry Andric "Attribute 'byref' type does not match parameter!", V); 1869e8d8bef9SDimitry Andric } 1870e8d8bef9SDimitry Andric 1871e8d8bef9SDimitry Andric if (Attrs.hasAttribute(Attribute::ByVal) && Attrs.getByValType()) { 1872*81ad6265SDimitry Andric Check(Attrs.getByValType() == PTy->getNonOpaquePointerElementType(), 1873e8d8bef9SDimitry Andric "Attribute 'byval' type does not match parameter!", V); 1874e8d8bef9SDimitry Andric } 1875e8d8bef9SDimitry Andric 1876e8d8bef9SDimitry Andric if (Attrs.hasAttribute(Attribute::Preallocated)) { 1877*81ad6265SDimitry Andric Check(Attrs.getPreallocatedType() == 187804eeddc0SDimitry Andric PTy->getNonOpaquePointerElementType(), 1879e8d8bef9SDimitry Andric "Attribute 'preallocated' type does not match parameter!", V); 1880e8d8bef9SDimitry Andric } 1881fe6060f1SDimitry Andric 1882fe6060f1SDimitry Andric if (Attrs.hasAttribute(Attribute::InAlloca)) { 1883*81ad6265SDimitry Andric Check(Attrs.getInAllocaType() == PTy->getNonOpaquePointerElementType(), 1884fe6060f1SDimitry Andric "Attribute 'inalloca' type does not match parameter!", V); 1885fe6060f1SDimitry Andric } 1886fe6060f1SDimitry Andric 1887fe6060f1SDimitry Andric if (Attrs.hasAttribute(Attribute::ElementType)) { 1888*81ad6265SDimitry Andric Check(Attrs.getElementType() == PTy->getNonOpaquePointerElementType(), 1889fe6060f1SDimitry Andric "Attribute 'elementtype' type does not match parameter!", V); 1890fe6060f1SDimitry Andric } 1891fe6060f1SDimitry Andric } 1892fe6060f1SDimitry Andric } 1893fe6060f1SDimitry Andric } 1894fe6060f1SDimitry Andric 1895fe6060f1SDimitry Andric void Verifier::checkUnsignedBaseTenFuncAttr(AttributeList Attrs, StringRef Attr, 1896fe6060f1SDimitry Andric const Value *V) { 1897349cc55cSDimitry Andric if (Attrs.hasFnAttr(Attr)) { 1898349cc55cSDimitry Andric StringRef S = Attrs.getFnAttr(Attr).getValueAsString(); 1899fe6060f1SDimitry Andric unsigned N; 1900fe6060f1SDimitry Andric if (S.getAsInteger(10, N)) 1901fe6060f1SDimitry Andric CheckFailed("\"" + Attr + "\" takes an unsigned integer: " + S, V); 19020b57cec5SDimitry Andric } 19030b57cec5SDimitry Andric } 19040b57cec5SDimitry Andric 19050b57cec5SDimitry Andric // Check parameter attributes against a function type. 19060b57cec5SDimitry Andric // The value V is printed in error messages. 19070b57cec5SDimitry Andric void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs, 190804eeddc0SDimitry Andric const Value *V, bool IsIntrinsic, 190904eeddc0SDimitry Andric bool IsInlineAsm) { 19100b57cec5SDimitry Andric if (Attrs.isEmpty()) 19110b57cec5SDimitry Andric return; 19120b57cec5SDimitry Andric 1913fe6060f1SDimitry Andric if (AttributeListsVisited.insert(Attrs.getRawPointer()).second) { 1914*81ad6265SDimitry Andric Check(Attrs.hasParentContext(Context), 1915fe6060f1SDimitry Andric "Attribute list does not match Module context!", &Attrs, V); 1916fe6060f1SDimitry Andric for (const auto &AttrSet : Attrs) { 1917*81ad6265SDimitry Andric Check(!AttrSet.hasAttributes() || AttrSet.hasParentContext(Context), 1918fe6060f1SDimitry Andric "Attribute set does not match Module context!", &AttrSet, V); 1919fe6060f1SDimitry Andric for (const auto &A : AttrSet) { 1920*81ad6265SDimitry Andric Check(A.hasParentContext(Context), 1921fe6060f1SDimitry Andric "Attribute does not match Module context!", &A, V); 1922fe6060f1SDimitry Andric } 1923fe6060f1SDimitry Andric } 1924fe6060f1SDimitry Andric } 1925fe6060f1SDimitry Andric 19260b57cec5SDimitry Andric bool SawNest = false; 19270b57cec5SDimitry Andric bool SawReturned = false; 19280b57cec5SDimitry Andric bool SawSRet = false; 19290b57cec5SDimitry Andric bool SawSwiftSelf = false; 1930fe6060f1SDimitry Andric bool SawSwiftAsync = false; 19310b57cec5SDimitry Andric bool SawSwiftError = false; 19320b57cec5SDimitry Andric 19330b57cec5SDimitry Andric // Verify return value attributes. 1934349cc55cSDimitry Andric AttributeSet RetAttrs = Attrs.getRetAttrs(); 1935fe6060f1SDimitry Andric for (Attribute RetAttr : RetAttrs) 1936*81ad6265SDimitry Andric Check(RetAttr.isStringAttribute() || 1937fe6060f1SDimitry Andric Attribute::canUseAsRetAttr(RetAttr.getKindAsEnum()), 1938fe6060f1SDimitry Andric "Attribute '" + RetAttr.getAsString() + 1939fe6060f1SDimitry Andric "' does not apply to function return values", 19400b57cec5SDimitry Andric V); 1941fe6060f1SDimitry Andric 19420b57cec5SDimitry Andric verifyParameterAttrs(RetAttrs, FT->getReturnType(), V); 19430b57cec5SDimitry Andric 19440b57cec5SDimitry Andric // Verify parameter attributes. 19450b57cec5SDimitry Andric for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { 19460b57cec5SDimitry Andric Type *Ty = FT->getParamType(i); 1947349cc55cSDimitry Andric AttributeSet ArgAttrs = Attrs.getParamAttrs(i); 19480b57cec5SDimitry Andric 19490b57cec5SDimitry Andric if (!IsIntrinsic) { 1950*81ad6265SDimitry Andric Check(!ArgAttrs.hasAttribute(Attribute::ImmArg), 19510b57cec5SDimitry Andric "immarg attribute only applies to intrinsics", V); 195204eeddc0SDimitry Andric if (!IsInlineAsm) 1953*81ad6265SDimitry Andric Check(!ArgAttrs.hasAttribute(Attribute::ElementType), 195404eeddc0SDimitry Andric "Attribute 'elementtype' can only be applied to intrinsics" 1955*81ad6265SDimitry Andric " and inline asm.", 1956*81ad6265SDimitry Andric V); 19570b57cec5SDimitry Andric } 19580b57cec5SDimitry Andric 19590b57cec5SDimitry Andric verifyParameterAttrs(ArgAttrs, Ty, V); 19600b57cec5SDimitry Andric 19610b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::Nest)) { 1962*81ad6265SDimitry Andric Check(!SawNest, "More than one parameter has attribute nest!", V); 19630b57cec5SDimitry Andric SawNest = true; 19640b57cec5SDimitry Andric } 19650b57cec5SDimitry Andric 19660b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::Returned)) { 1967*81ad6265SDimitry Andric Check(!SawReturned, "More than one parameter has attribute returned!", V); 1968*81ad6265SDimitry Andric Check(Ty->canLosslesslyBitCastTo(FT->getReturnType()), 19690b57cec5SDimitry Andric "Incompatible argument and return types for 'returned' attribute", 19700b57cec5SDimitry Andric V); 19710b57cec5SDimitry Andric SawReturned = true; 19720b57cec5SDimitry Andric } 19730b57cec5SDimitry Andric 19740b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::StructRet)) { 1975*81ad6265SDimitry Andric Check(!SawSRet, "Cannot have multiple 'sret' parameters!", V); 1976*81ad6265SDimitry Andric Check(i == 0 || i == 1, 19770b57cec5SDimitry Andric "Attribute 'sret' is not on first or second parameter!", V); 19780b57cec5SDimitry Andric SawSRet = true; 19790b57cec5SDimitry Andric } 19800b57cec5SDimitry Andric 19810b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::SwiftSelf)) { 1982*81ad6265SDimitry Andric Check(!SawSwiftSelf, "Cannot have multiple 'swiftself' parameters!", V); 19830b57cec5SDimitry Andric SawSwiftSelf = true; 19840b57cec5SDimitry Andric } 19850b57cec5SDimitry Andric 1986fe6060f1SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::SwiftAsync)) { 1987*81ad6265SDimitry Andric Check(!SawSwiftAsync, "Cannot have multiple 'swiftasync' parameters!", V); 1988fe6060f1SDimitry Andric SawSwiftAsync = true; 1989fe6060f1SDimitry Andric } 1990fe6060f1SDimitry Andric 19910b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::SwiftError)) { 1992*81ad6265SDimitry Andric Check(!SawSwiftError, "Cannot have multiple 'swifterror' parameters!", V); 19930b57cec5SDimitry Andric SawSwiftError = true; 19940b57cec5SDimitry Andric } 19950b57cec5SDimitry Andric 19960b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::InAlloca)) { 1997*81ad6265SDimitry Andric Check(i == FT->getNumParams() - 1, 19980b57cec5SDimitry Andric "inalloca isn't on the last parameter!", V); 19990b57cec5SDimitry Andric } 20000b57cec5SDimitry Andric } 20010b57cec5SDimitry Andric 2002349cc55cSDimitry Andric if (!Attrs.hasFnAttrs()) 20030b57cec5SDimitry Andric return; 20040b57cec5SDimitry Andric 2005349cc55cSDimitry Andric verifyAttributeTypes(Attrs.getFnAttrs(), V); 2006349cc55cSDimitry Andric for (Attribute FnAttr : Attrs.getFnAttrs()) 2007*81ad6265SDimitry Andric Check(FnAttr.isStringAttribute() || 2008fe6060f1SDimitry Andric Attribute::canUseAsFnAttr(FnAttr.getKindAsEnum()), 2009fe6060f1SDimitry Andric "Attribute '" + FnAttr.getAsString() + 2010fe6060f1SDimitry Andric "' does not apply to functions!", 2011fe6060f1SDimitry Andric V); 20120b57cec5SDimitry Andric 2013*81ad6265SDimitry Andric Check(!(Attrs.hasFnAttr(Attribute::ReadNone) && 2014349cc55cSDimitry Andric Attrs.hasFnAttr(Attribute::ReadOnly)), 20150b57cec5SDimitry Andric "Attributes 'readnone and readonly' are incompatible!", V); 20160b57cec5SDimitry Andric 2017*81ad6265SDimitry Andric Check(!(Attrs.hasFnAttr(Attribute::ReadNone) && 2018349cc55cSDimitry Andric Attrs.hasFnAttr(Attribute::WriteOnly)), 20190b57cec5SDimitry Andric "Attributes 'readnone and writeonly' are incompatible!", V); 20200b57cec5SDimitry Andric 2021*81ad6265SDimitry Andric Check(!(Attrs.hasFnAttr(Attribute::ReadOnly) && 2022349cc55cSDimitry Andric Attrs.hasFnAttr(Attribute::WriteOnly)), 20230b57cec5SDimitry Andric "Attributes 'readonly and writeonly' are incompatible!", V); 20240b57cec5SDimitry Andric 2025*81ad6265SDimitry Andric Check(!(Attrs.hasFnAttr(Attribute::ReadNone) && 2026349cc55cSDimitry Andric Attrs.hasFnAttr(Attribute::InaccessibleMemOrArgMemOnly)), 20270b57cec5SDimitry Andric "Attributes 'readnone and inaccessiblemem_or_argmemonly' are " 20280b57cec5SDimitry Andric "incompatible!", 20290b57cec5SDimitry Andric V); 20300b57cec5SDimitry Andric 2031*81ad6265SDimitry Andric Check(!(Attrs.hasFnAttr(Attribute::ReadNone) && 2032349cc55cSDimitry Andric Attrs.hasFnAttr(Attribute::InaccessibleMemOnly)), 20330b57cec5SDimitry Andric "Attributes 'readnone and inaccessiblememonly' are incompatible!", V); 20340b57cec5SDimitry Andric 2035*81ad6265SDimitry Andric Check(!(Attrs.hasFnAttr(Attribute::NoInline) && 2036349cc55cSDimitry Andric Attrs.hasFnAttr(Attribute::AlwaysInline)), 20370b57cec5SDimitry Andric "Attributes 'noinline and alwaysinline' are incompatible!", V); 20380b57cec5SDimitry Andric 2039349cc55cSDimitry Andric if (Attrs.hasFnAttr(Attribute::OptimizeNone)) { 2040*81ad6265SDimitry Andric Check(Attrs.hasFnAttr(Attribute::NoInline), 20410b57cec5SDimitry Andric "Attribute 'optnone' requires 'noinline'!", V); 20420b57cec5SDimitry Andric 2043*81ad6265SDimitry Andric Check(!Attrs.hasFnAttr(Attribute::OptimizeForSize), 20440b57cec5SDimitry Andric "Attributes 'optsize and optnone' are incompatible!", V); 20450b57cec5SDimitry Andric 2046*81ad6265SDimitry Andric Check(!Attrs.hasFnAttr(Attribute::MinSize), 20470b57cec5SDimitry Andric "Attributes 'minsize and optnone' are incompatible!", V); 20480b57cec5SDimitry Andric } 20490b57cec5SDimitry Andric 2050349cc55cSDimitry Andric if (Attrs.hasFnAttr(Attribute::JumpTable)) { 20510b57cec5SDimitry Andric const GlobalValue *GV = cast<GlobalValue>(V); 2052*81ad6265SDimitry Andric Check(GV->hasGlobalUnnamedAddr(), 20530b57cec5SDimitry Andric "Attribute 'jumptable' requires 'unnamed_addr'", V); 20540b57cec5SDimitry Andric } 20550b57cec5SDimitry Andric 2056349cc55cSDimitry Andric if (Attrs.hasFnAttr(Attribute::AllocSize)) { 20570b57cec5SDimitry Andric std::pair<unsigned, Optional<unsigned>> Args = 2058349cc55cSDimitry Andric Attrs.getFnAttrs().getAllocSizeArgs(); 20590b57cec5SDimitry Andric 20600b57cec5SDimitry Andric auto CheckParam = [&](StringRef Name, unsigned ParamNo) { 20610b57cec5SDimitry Andric if (ParamNo >= FT->getNumParams()) { 20620b57cec5SDimitry Andric CheckFailed("'allocsize' " + Name + " argument is out of bounds", V); 20630b57cec5SDimitry Andric return false; 20640b57cec5SDimitry Andric } 20650b57cec5SDimitry Andric 20660b57cec5SDimitry Andric if (!FT->getParamType(ParamNo)->isIntegerTy()) { 20670b57cec5SDimitry Andric CheckFailed("'allocsize' " + Name + 20680b57cec5SDimitry Andric " argument must refer to an integer parameter", 20690b57cec5SDimitry Andric V); 20700b57cec5SDimitry Andric return false; 20710b57cec5SDimitry Andric } 20720b57cec5SDimitry Andric 20730b57cec5SDimitry Andric return true; 20740b57cec5SDimitry Andric }; 20750b57cec5SDimitry Andric 20760b57cec5SDimitry Andric if (!CheckParam("element size", Args.first)) 20770b57cec5SDimitry Andric return; 20780b57cec5SDimitry Andric 20790b57cec5SDimitry Andric if (Args.second && !CheckParam("number of elements", *Args.second)) 20800b57cec5SDimitry Andric return; 20810b57cec5SDimitry Andric } 2082480093f4SDimitry Andric 2083*81ad6265SDimitry Andric if (Attrs.hasFnAttr(Attribute::AllocKind)) { 2084*81ad6265SDimitry Andric AllocFnKind K = Attrs.getAllocKind(); 2085*81ad6265SDimitry Andric AllocFnKind Type = 2086*81ad6265SDimitry Andric K & (AllocFnKind::Alloc | AllocFnKind::Realloc | AllocFnKind::Free); 2087*81ad6265SDimitry Andric if (!is_contained( 2088*81ad6265SDimitry Andric {AllocFnKind::Alloc, AllocFnKind::Realloc, AllocFnKind::Free}, 2089*81ad6265SDimitry Andric Type)) 2090*81ad6265SDimitry Andric CheckFailed( 2091*81ad6265SDimitry Andric "'allockind()' requires exactly one of alloc, realloc, and free"); 2092*81ad6265SDimitry Andric if ((Type == AllocFnKind::Free) && 2093*81ad6265SDimitry Andric ((K & (AllocFnKind::Uninitialized | AllocFnKind::Zeroed | 2094*81ad6265SDimitry Andric AllocFnKind::Aligned)) != AllocFnKind::Unknown)) 2095*81ad6265SDimitry Andric CheckFailed("'allockind(\"free\")' doesn't allow uninitialized, zeroed, " 2096*81ad6265SDimitry Andric "or aligned modifiers."); 2097*81ad6265SDimitry Andric AllocFnKind ZeroedUninit = AllocFnKind::Uninitialized | AllocFnKind::Zeroed; 2098*81ad6265SDimitry Andric if ((K & ZeroedUninit) == ZeroedUninit) 2099*81ad6265SDimitry Andric CheckFailed("'allockind()' can't be both zeroed and uninitialized"); 2100*81ad6265SDimitry Andric } 2101*81ad6265SDimitry Andric 2102349cc55cSDimitry Andric if (Attrs.hasFnAttr(Attribute::VScaleRange)) { 21030eae32dcSDimitry Andric unsigned VScaleMin = Attrs.getFnAttrs().getVScaleRangeMin(); 21040eae32dcSDimitry Andric if (VScaleMin == 0) 21050eae32dcSDimitry Andric CheckFailed("'vscale_range' minimum must be greater than 0", V); 2106fe6060f1SDimitry Andric 21070eae32dcSDimitry Andric Optional<unsigned> VScaleMax = Attrs.getFnAttrs().getVScaleRangeMax(); 21080eae32dcSDimitry Andric if (VScaleMax && VScaleMin > VScaleMax) 2109fe6060f1SDimitry Andric CheckFailed("'vscale_range' minimum cannot be greater than maximum", V); 2110fe6060f1SDimitry Andric } 2111fe6060f1SDimitry Andric 2112349cc55cSDimitry Andric if (Attrs.hasFnAttr("frame-pointer")) { 2113349cc55cSDimitry Andric StringRef FP = Attrs.getFnAttr("frame-pointer").getValueAsString(); 2114480093f4SDimitry Andric if (FP != "all" && FP != "non-leaf" && FP != "none") 2115480093f4SDimitry Andric CheckFailed("invalid value for 'frame-pointer' attribute: " + FP, V); 2116480093f4SDimitry Andric } 2117480093f4SDimitry Andric 2118fe6060f1SDimitry Andric checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-prefix", V); 2119fe6060f1SDimitry Andric checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-entry", V); 2120fe6060f1SDimitry Andric checkUnsignedBaseTenFuncAttr(Attrs, "warn-stack-size", V); 21210b57cec5SDimitry Andric } 21220b57cec5SDimitry Andric 21230b57cec5SDimitry Andric void Verifier::verifyFunctionMetadata( 21240b57cec5SDimitry Andric ArrayRef<std::pair<unsigned, MDNode *>> MDs) { 21250b57cec5SDimitry Andric for (const auto &Pair : MDs) { 21260b57cec5SDimitry Andric if (Pair.first == LLVMContext::MD_prof) { 21270b57cec5SDimitry Andric MDNode *MD = Pair.second; 2128*81ad6265SDimitry Andric Check(MD->getNumOperands() >= 2, 21290b57cec5SDimitry Andric "!prof annotations should have no less than 2 operands", MD); 21300b57cec5SDimitry Andric 21310b57cec5SDimitry Andric // Check first operand. 2132*81ad6265SDimitry Andric Check(MD->getOperand(0) != nullptr, "first operand should not be null", 21330b57cec5SDimitry Andric MD); 2134*81ad6265SDimitry Andric Check(isa<MDString>(MD->getOperand(0)), 21350b57cec5SDimitry Andric "expected string with name of the !prof annotation", MD); 21360b57cec5SDimitry Andric MDString *MDS = cast<MDString>(MD->getOperand(0)); 21370b57cec5SDimitry Andric StringRef ProfName = MDS->getString(); 2138*81ad6265SDimitry Andric Check(ProfName.equals("function_entry_count") || 21390b57cec5SDimitry Andric ProfName.equals("synthetic_function_entry_count"), 21400b57cec5SDimitry Andric "first operand should be 'function_entry_count'" 21410b57cec5SDimitry Andric " or 'synthetic_function_entry_count'", 21420b57cec5SDimitry Andric MD); 21430b57cec5SDimitry Andric 21440b57cec5SDimitry Andric // Check second operand. 2145*81ad6265SDimitry Andric Check(MD->getOperand(1) != nullptr, "second operand should not be null", 21460b57cec5SDimitry Andric MD); 2147*81ad6265SDimitry Andric Check(isa<ConstantAsMetadata>(MD->getOperand(1)), 21480b57cec5SDimitry Andric "expected integer argument to function_entry_count", MD); 21490b57cec5SDimitry Andric } 21500b57cec5SDimitry Andric } 21510b57cec5SDimitry Andric } 21520b57cec5SDimitry Andric 21530b57cec5SDimitry Andric void Verifier::visitConstantExprsRecursively(const Constant *EntryC) { 21540b57cec5SDimitry Andric if (!ConstantExprVisited.insert(EntryC).second) 21550b57cec5SDimitry Andric return; 21560b57cec5SDimitry Andric 21570b57cec5SDimitry Andric SmallVector<const Constant *, 16> Stack; 21580b57cec5SDimitry Andric Stack.push_back(EntryC); 21590b57cec5SDimitry Andric 21600b57cec5SDimitry Andric while (!Stack.empty()) { 21610b57cec5SDimitry Andric const Constant *C = Stack.pop_back_val(); 21620b57cec5SDimitry Andric 21630b57cec5SDimitry Andric // Check this constant expression. 21640b57cec5SDimitry Andric if (const auto *CE = dyn_cast<ConstantExpr>(C)) 21650b57cec5SDimitry Andric visitConstantExpr(CE); 21660b57cec5SDimitry Andric 21670b57cec5SDimitry Andric if (const auto *GV = dyn_cast<GlobalValue>(C)) { 21680b57cec5SDimitry Andric // Global Values get visited separately, but we do need to make sure 21690b57cec5SDimitry Andric // that the global value is in the correct module 2170*81ad6265SDimitry Andric Check(GV->getParent() == &M, "Referencing global in another module!", 21710b57cec5SDimitry Andric EntryC, &M, GV, GV->getParent()); 21720b57cec5SDimitry Andric continue; 21730b57cec5SDimitry Andric } 21740b57cec5SDimitry Andric 21750b57cec5SDimitry Andric // Visit all sub-expressions. 21760b57cec5SDimitry Andric for (const Use &U : C->operands()) { 21770b57cec5SDimitry Andric const auto *OpC = dyn_cast<Constant>(U); 21780b57cec5SDimitry Andric if (!OpC) 21790b57cec5SDimitry Andric continue; 21800b57cec5SDimitry Andric if (!ConstantExprVisited.insert(OpC).second) 21810b57cec5SDimitry Andric continue; 21820b57cec5SDimitry Andric Stack.push_back(OpC); 21830b57cec5SDimitry Andric } 21840b57cec5SDimitry Andric } 21850b57cec5SDimitry Andric } 21860b57cec5SDimitry Andric 21870b57cec5SDimitry Andric void Verifier::visitConstantExpr(const ConstantExpr *CE) { 21880b57cec5SDimitry Andric if (CE->getOpcode() == Instruction::BitCast) 2189*81ad6265SDimitry Andric Check(CastInst::castIsValid(Instruction::BitCast, CE->getOperand(0), 21900b57cec5SDimitry Andric CE->getType()), 21910b57cec5SDimitry Andric "Invalid bitcast", CE); 21920b57cec5SDimitry Andric } 21930b57cec5SDimitry Andric 21940b57cec5SDimitry Andric bool Verifier::verifyAttributeCount(AttributeList Attrs, unsigned Params) { 21950b57cec5SDimitry Andric // There shouldn't be more attribute sets than there are parameters plus the 21960b57cec5SDimitry Andric // function and return value. 21970b57cec5SDimitry Andric return Attrs.getNumAttrSets() <= Params + 2; 21980b57cec5SDimitry Andric } 21990b57cec5SDimitry Andric 220004eeddc0SDimitry Andric void Verifier::verifyInlineAsmCall(const CallBase &Call) { 220104eeddc0SDimitry Andric const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand()); 220204eeddc0SDimitry Andric unsigned ArgNo = 0; 220304eeddc0SDimitry Andric for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) { 220404eeddc0SDimitry Andric // Only deal with constraints that correspond to call arguments. 220504eeddc0SDimitry Andric if (!CI.hasArg()) 220604eeddc0SDimitry Andric continue; 220704eeddc0SDimitry Andric 220804eeddc0SDimitry Andric if (CI.isIndirect) { 220904eeddc0SDimitry Andric const Value *Arg = Call.getArgOperand(ArgNo); 2210*81ad6265SDimitry Andric Check(Arg->getType()->isPointerTy(), 2211*81ad6265SDimitry Andric "Operand for indirect constraint must have pointer type", &Call); 221204eeddc0SDimitry Andric 2213*81ad6265SDimitry Andric Check(Call.getParamElementType(ArgNo), 221404eeddc0SDimitry Andric "Operand for indirect constraint must have elementtype attribute", 221504eeddc0SDimitry Andric &Call); 221604eeddc0SDimitry Andric } else { 2217*81ad6265SDimitry Andric Check(!Call.paramHasAttr(ArgNo, Attribute::ElementType), 221804eeddc0SDimitry Andric "Elementtype attribute can only be applied for indirect " 2219*81ad6265SDimitry Andric "constraints", 2220*81ad6265SDimitry Andric &Call); 222104eeddc0SDimitry Andric } 222204eeddc0SDimitry Andric 222304eeddc0SDimitry Andric ArgNo++; 222404eeddc0SDimitry Andric } 222504eeddc0SDimitry Andric } 222604eeddc0SDimitry Andric 22270b57cec5SDimitry Andric /// Verify that statepoint intrinsic is well formed. 22280b57cec5SDimitry Andric void Verifier::verifyStatepoint(const CallBase &Call) { 22290b57cec5SDimitry Andric assert(Call.getCalledFunction() && 22300b57cec5SDimitry Andric Call.getCalledFunction()->getIntrinsicID() == 22310b57cec5SDimitry Andric Intrinsic::experimental_gc_statepoint); 22320b57cec5SDimitry Andric 2233*81ad6265SDimitry Andric Check(!Call.doesNotAccessMemory() && !Call.onlyReadsMemory() && 22340b57cec5SDimitry Andric !Call.onlyAccessesArgMemory(), 22350b57cec5SDimitry Andric "gc.statepoint must read and write all memory to preserve " 22360b57cec5SDimitry Andric "reordering restrictions required by safepoint semantics", 22370b57cec5SDimitry Andric Call); 22380b57cec5SDimitry Andric 22390b57cec5SDimitry Andric const int64_t NumPatchBytes = 22400b57cec5SDimitry Andric cast<ConstantInt>(Call.getArgOperand(1))->getSExtValue(); 22410b57cec5SDimitry Andric assert(isInt<32>(NumPatchBytes) && "NumPatchBytesV is an i32!"); 2242*81ad6265SDimitry Andric Check(NumPatchBytes >= 0, 22430b57cec5SDimitry Andric "gc.statepoint number of patchable bytes must be " 22440b57cec5SDimitry Andric "positive", 22450b57cec5SDimitry Andric Call); 22460b57cec5SDimitry Andric 2247*81ad6265SDimitry Andric Type *TargetElemType = Call.getParamElementType(2); 2248*81ad6265SDimitry Andric Check(TargetElemType, 2249*81ad6265SDimitry Andric "gc.statepoint callee argument must have elementtype attribute", Call); 2250*81ad6265SDimitry Andric FunctionType *TargetFuncType = dyn_cast<FunctionType>(TargetElemType); 2251*81ad6265SDimitry Andric Check(TargetFuncType, 2252*81ad6265SDimitry Andric "gc.statepoint callee elementtype must be function type", Call); 22530b57cec5SDimitry Andric 22540b57cec5SDimitry Andric const int NumCallArgs = cast<ConstantInt>(Call.getArgOperand(3))->getZExtValue(); 2255*81ad6265SDimitry Andric Check(NumCallArgs >= 0, 22560b57cec5SDimitry Andric "gc.statepoint number of arguments to underlying call " 22570b57cec5SDimitry Andric "must be positive", 22580b57cec5SDimitry Andric Call); 22590b57cec5SDimitry Andric const int NumParams = (int)TargetFuncType->getNumParams(); 22600b57cec5SDimitry Andric if (TargetFuncType->isVarArg()) { 2261*81ad6265SDimitry Andric Check(NumCallArgs >= NumParams, 22620b57cec5SDimitry Andric "gc.statepoint mismatch in number of vararg call args", Call); 22630b57cec5SDimitry Andric 22640b57cec5SDimitry Andric // TODO: Remove this limitation 2265*81ad6265SDimitry Andric Check(TargetFuncType->getReturnType()->isVoidTy(), 22660b57cec5SDimitry Andric "gc.statepoint doesn't support wrapping non-void " 22670b57cec5SDimitry Andric "vararg functions yet", 22680b57cec5SDimitry Andric Call); 22690b57cec5SDimitry Andric } else 2270*81ad6265SDimitry Andric Check(NumCallArgs == NumParams, 22710b57cec5SDimitry Andric "gc.statepoint mismatch in number of call args", Call); 22720b57cec5SDimitry Andric 22730b57cec5SDimitry Andric const uint64_t Flags 22740b57cec5SDimitry Andric = cast<ConstantInt>(Call.getArgOperand(4))->getZExtValue(); 2275*81ad6265SDimitry Andric Check((Flags & ~(uint64_t)StatepointFlags::MaskAll) == 0, 22760b57cec5SDimitry Andric "unknown flag used in gc.statepoint flags argument", Call); 22770b57cec5SDimitry Andric 22780b57cec5SDimitry Andric // Verify that the types of the call parameter arguments match 22790b57cec5SDimitry Andric // the type of the wrapped callee. 22800b57cec5SDimitry Andric AttributeList Attrs = Call.getAttributes(); 22810b57cec5SDimitry Andric for (int i = 0; i < NumParams; i++) { 22820b57cec5SDimitry Andric Type *ParamType = TargetFuncType->getParamType(i); 22830b57cec5SDimitry Andric Type *ArgType = Call.getArgOperand(5 + i)->getType(); 2284*81ad6265SDimitry Andric Check(ArgType == ParamType, 22850b57cec5SDimitry Andric "gc.statepoint call argument does not match wrapped " 22860b57cec5SDimitry Andric "function type", 22870b57cec5SDimitry Andric Call); 22880b57cec5SDimitry Andric 22890b57cec5SDimitry Andric if (TargetFuncType->isVarArg()) { 2290349cc55cSDimitry Andric AttributeSet ArgAttrs = Attrs.getParamAttrs(5 + i); 2291*81ad6265SDimitry Andric Check(!ArgAttrs.hasAttribute(Attribute::StructRet), 2292*81ad6265SDimitry Andric "Attribute 'sret' cannot be used for vararg call arguments!", Call); 22930b57cec5SDimitry Andric } 22940b57cec5SDimitry Andric } 22950b57cec5SDimitry Andric 22960b57cec5SDimitry Andric const int EndCallArgsInx = 4 + NumCallArgs; 22970b57cec5SDimitry Andric 22980b57cec5SDimitry Andric const Value *NumTransitionArgsV = Call.getArgOperand(EndCallArgsInx + 1); 2299*81ad6265SDimitry Andric Check(isa<ConstantInt>(NumTransitionArgsV), 23000b57cec5SDimitry Andric "gc.statepoint number of transition arguments " 23010b57cec5SDimitry Andric "must be constant integer", 23020b57cec5SDimitry Andric Call); 23030b57cec5SDimitry Andric const int NumTransitionArgs = 23040b57cec5SDimitry Andric cast<ConstantInt>(NumTransitionArgsV)->getZExtValue(); 2305*81ad6265SDimitry Andric Check(NumTransitionArgs == 0, 2306e8d8bef9SDimitry Andric "gc.statepoint w/inline transition bundle is deprecated", Call); 2307e8d8bef9SDimitry Andric const int EndTransitionArgsInx = EndCallArgsInx + 1 + NumTransitionArgs; 23085ffd83dbSDimitry Andric 23090b57cec5SDimitry Andric const Value *NumDeoptArgsV = Call.getArgOperand(EndTransitionArgsInx + 1); 2310*81ad6265SDimitry Andric Check(isa<ConstantInt>(NumDeoptArgsV), 23110b57cec5SDimitry Andric "gc.statepoint number of deoptimization arguments " 23120b57cec5SDimitry Andric "must be constant integer", 23130b57cec5SDimitry Andric Call); 23140b57cec5SDimitry Andric const int NumDeoptArgs = cast<ConstantInt>(NumDeoptArgsV)->getZExtValue(); 2315*81ad6265SDimitry Andric Check(NumDeoptArgs == 0, 2316e8d8bef9SDimitry Andric "gc.statepoint w/inline deopt operands is deprecated", Call); 23175ffd83dbSDimitry Andric 2318e8d8bef9SDimitry Andric const int ExpectedNumArgs = 7 + NumCallArgs; 2319*81ad6265SDimitry Andric Check(ExpectedNumArgs == (int)Call.arg_size(), 2320e8d8bef9SDimitry Andric "gc.statepoint too many arguments", Call); 23210b57cec5SDimitry Andric 23220b57cec5SDimitry Andric // Check that the only uses of this gc.statepoint are gc.result or 23230b57cec5SDimitry Andric // gc.relocate calls which are tied to this statepoint and thus part 23240b57cec5SDimitry Andric // of the same statepoint sequence 23250b57cec5SDimitry Andric for (const User *U : Call.users()) { 23260b57cec5SDimitry Andric const CallInst *UserCall = dyn_cast<const CallInst>(U); 2327*81ad6265SDimitry Andric Check(UserCall, "illegal use of statepoint token", Call, U); 23280b57cec5SDimitry Andric if (!UserCall) 23290b57cec5SDimitry Andric continue; 2330*81ad6265SDimitry Andric Check(isa<GCRelocateInst>(UserCall) || isa<GCResultInst>(UserCall), 23310b57cec5SDimitry Andric "gc.result or gc.relocate are the only value uses " 23320b57cec5SDimitry Andric "of a gc.statepoint", 23330b57cec5SDimitry Andric Call, U); 23340b57cec5SDimitry Andric if (isa<GCResultInst>(UserCall)) { 2335*81ad6265SDimitry Andric Check(UserCall->getArgOperand(0) == &Call, 23360b57cec5SDimitry Andric "gc.result connected to wrong gc.statepoint", Call, UserCall); 23370b57cec5SDimitry Andric } else if (isa<GCRelocateInst>(Call)) { 2338*81ad6265SDimitry Andric Check(UserCall->getArgOperand(0) == &Call, 23390b57cec5SDimitry Andric "gc.relocate connected to wrong gc.statepoint", Call, UserCall); 23400b57cec5SDimitry Andric } 23410b57cec5SDimitry Andric } 23420b57cec5SDimitry Andric 23430b57cec5SDimitry Andric // Note: It is legal for a single derived pointer to be listed multiple 23440b57cec5SDimitry Andric // times. It's non-optimal, but it is legal. It can also happen after 23450b57cec5SDimitry Andric // insertion if we strip a bitcast away. 23460b57cec5SDimitry Andric // Note: It is really tempting to check that each base is relocated and 23470b57cec5SDimitry Andric // that a derived pointer is never reused as a base pointer. This turns 23480b57cec5SDimitry Andric // out to be problematic since optimizations run after safepoint insertion 23490b57cec5SDimitry Andric // can recognize equality properties that the insertion logic doesn't know 23500b57cec5SDimitry Andric // about. See example statepoint.ll in the verifier subdirectory 23510b57cec5SDimitry Andric } 23520b57cec5SDimitry Andric 23530b57cec5SDimitry Andric void Verifier::verifyFrameRecoverIndices() { 23540b57cec5SDimitry Andric for (auto &Counts : FrameEscapeInfo) { 23550b57cec5SDimitry Andric Function *F = Counts.first; 23560b57cec5SDimitry Andric unsigned EscapedObjectCount = Counts.second.first; 23570b57cec5SDimitry Andric unsigned MaxRecoveredIndex = Counts.second.second; 2358*81ad6265SDimitry Andric Check(MaxRecoveredIndex <= EscapedObjectCount, 23590b57cec5SDimitry Andric "all indices passed to llvm.localrecover must be less than the " 23600b57cec5SDimitry Andric "number of arguments passed to llvm.localescape in the parent " 23610b57cec5SDimitry Andric "function", 23620b57cec5SDimitry Andric F); 23630b57cec5SDimitry Andric } 23640b57cec5SDimitry Andric } 23650b57cec5SDimitry Andric 23660b57cec5SDimitry Andric static Instruction *getSuccPad(Instruction *Terminator) { 23670b57cec5SDimitry Andric BasicBlock *UnwindDest; 23680b57cec5SDimitry Andric if (auto *II = dyn_cast<InvokeInst>(Terminator)) 23690b57cec5SDimitry Andric UnwindDest = II->getUnwindDest(); 23700b57cec5SDimitry Andric else if (auto *CSI = dyn_cast<CatchSwitchInst>(Terminator)) 23710b57cec5SDimitry Andric UnwindDest = CSI->getUnwindDest(); 23720b57cec5SDimitry Andric else 23730b57cec5SDimitry Andric UnwindDest = cast<CleanupReturnInst>(Terminator)->getUnwindDest(); 23740b57cec5SDimitry Andric return UnwindDest->getFirstNonPHI(); 23750b57cec5SDimitry Andric } 23760b57cec5SDimitry Andric 23770b57cec5SDimitry Andric void Verifier::verifySiblingFuncletUnwinds() { 23780b57cec5SDimitry Andric SmallPtrSet<Instruction *, 8> Visited; 23790b57cec5SDimitry Andric SmallPtrSet<Instruction *, 8> Active; 23800b57cec5SDimitry Andric for (const auto &Pair : SiblingFuncletInfo) { 23810b57cec5SDimitry Andric Instruction *PredPad = Pair.first; 23820b57cec5SDimitry Andric if (Visited.count(PredPad)) 23830b57cec5SDimitry Andric continue; 23840b57cec5SDimitry Andric Active.insert(PredPad); 23850b57cec5SDimitry Andric Instruction *Terminator = Pair.second; 23860b57cec5SDimitry Andric do { 23870b57cec5SDimitry Andric Instruction *SuccPad = getSuccPad(Terminator); 23880b57cec5SDimitry Andric if (Active.count(SuccPad)) { 23890b57cec5SDimitry Andric // Found a cycle; report error 23900b57cec5SDimitry Andric Instruction *CyclePad = SuccPad; 23910b57cec5SDimitry Andric SmallVector<Instruction *, 8> CycleNodes; 23920b57cec5SDimitry Andric do { 23930b57cec5SDimitry Andric CycleNodes.push_back(CyclePad); 23940b57cec5SDimitry Andric Instruction *CycleTerminator = SiblingFuncletInfo[CyclePad]; 23950b57cec5SDimitry Andric if (CycleTerminator != CyclePad) 23960b57cec5SDimitry Andric CycleNodes.push_back(CycleTerminator); 23970b57cec5SDimitry Andric CyclePad = getSuccPad(CycleTerminator); 23980b57cec5SDimitry Andric } while (CyclePad != SuccPad); 2399*81ad6265SDimitry Andric Check(false, "EH pads can't handle each other's exceptions", 24000b57cec5SDimitry Andric ArrayRef<Instruction *>(CycleNodes)); 24010b57cec5SDimitry Andric } 24020b57cec5SDimitry Andric // Don't re-walk a node we've already checked 24030b57cec5SDimitry Andric if (!Visited.insert(SuccPad).second) 24040b57cec5SDimitry Andric break; 24050b57cec5SDimitry Andric // Walk to this successor if it has a map entry. 24060b57cec5SDimitry Andric PredPad = SuccPad; 24070b57cec5SDimitry Andric auto TermI = SiblingFuncletInfo.find(PredPad); 24080b57cec5SDimitry Andric if (TermI == SiblingFuncletInfo.end()) 24090b57cec5SDimitry Andric break; 24100b57cec5SDimitry Andric Terminator = TermI->second; 24110b57cec5SDimitry Andric Active.insert(PredPad); 24120b57cec5SDimitry Andric } while (true); 24130b57cec5SDimitry Andric // Each node only has one successor, so we've walked all the active 24140b57cec5SDimitry Andric // nodes' successors. 24150b57cec5SDimitry Andric Active.clear(); 24160b57cec5SDimitry Andric } 24170b57cec5SDimitry Andric } 24180b57cec5SDimitry Andric 24190b57cec5SDimitry Andric // visitFunction - Verify that a function is ok. 24200b57cec5SDimitry Andric // 24210b57cec5SDimitry Andric void Verifier::visitFunction(const Function &F) { 24220b57cec5SDimitry Andric visitGlobalValue(F); 24230b57cec5SDimitry Andric 24240b57cec5SDimitry Andric // Check function arguments. 24250b57cec5SDimitry Andric FunctionType *FT = F.getFunctionType(); 24260b57cec5SDimitry Andric unsigned NumArgs = F.arg_size(); 24270b57cec5SDimitry Andric 2428*81ad6265SDimitry Andric Check(&Context == &F.getContext(), 24290b57cec5SDimitry Andric "Function context does not match Module context!", &F); 24300b57cec5SDimitry Andric 2431*81ad6265SDimitry Andric Check(!F.hasCommonLinkage(), "Functions may not have common linkage", &F); 2432*81ad6265SDimitry Andric Check(FT->getNumParams() == NumArgs, 24330b57cec5SDimitry Andric "# formal arguments must match # of arguments for function type!", &F, 24340b57cec5SDimitry Andric FT); 2435*81ad6265SDimitry Andric Check(F.getReturnType()->isFirstClassType() || 24360b57cec5SDimitry Andric F.getReturnType()->isVoidTy() || F.getReturnType()->isStructTy(), 24370b57cec5SDimitry Andric "Functions cannot return aggregate values!", &F); 24380b57cec5SDimitry Andric 2439*81ad6265SDimitry Andric Check(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(), 24400b57cec5SDimitry Andric "Invalid struct return type!", &F); 24410b57cec5SDimitry Andric 24420b57cec5SDimitry Andric AttributeList Attrs = F.getAttributes(); 24430b57cec5SDimitry Andric 2444*81ad6265SDimitry Andric Check(verifyAttributeCount(Attrs, FT->getNumParams()), 24450b57cec5SDimitry Andric "Attribute after last parameter!", &F); 24460b57cec5SDimitry Andric 2447fe6060f1SDimitry Andric bool IsIntrinsic = F.isIntrinsic(); 24480b57cec5SDimitry Andric 24490b57cec5SDimitry Andric // Check function attributes. 245004eeddc0SDimitry Andric verifyFunctionAttrs(FT, Attrs, &F, IsIntrinsic, /* IsInlineAsm */ false); 24510b57cec5SDimitry Andric 24520b57cec5SDimitry Andric // On function declarations/definitions, we do not support the builtin 24530b57cec5SDimitry Andric // attribute. We do not check this in VerifyFunctionAttrs since that is 24540b57cec5SDimitry Andric // checking for Attributes that can/can not ever be on functions. 2455*81ad6265SDimitry Andric Check(!Attrs.hasFnAttr(Attribute::Builtin), 24560b57cec5SDimitry Andric "Attribute 'builtin' can only be applied to a callsite.", &F); 24570b57cec5SDimitry Andric 2458*81ad6265SDimitry Andric Check(!Attrs.hasAttrSomewhere(Attribute::ElementType), 2459fe6060f1SDimitry Andric "Attribute 'elementtype' can only be applied to a callsite.", &F); 2460fe6060f1SDimitry Andric 24610b57cec5SDimitry Andric // Check that this function meets the restrictions on this calling convention. 24620b57cec5SDimitry Andric // Sometimes varargs is used for perfectly forwarding thunks, so some of these 24630b57cec5SDimitry Andric // restrictions can be lifted. 24640b57cec5SDimitry Andric switch (F.getCallingConv()) { 24650b57cec5SDimitry Andric default: 24660b57cec5SDimitry Andric case CallingConv::C: 24670b57cec5SDimitry Andric break; 2468e8d8bef9SDimitry Andric case CallingConv::X86_INTR: { 2469*81ad6265SDimitry Andric Check(F.arg_empty() || Attrs.hasParamAttr(0, Attribute::ByVal), 2470e8d8bef9SDimitry Andric "Calling convention parameter requires byval", &F); 2471e8d8bef9SDimitry Andric break; 2472e8d8bef9SDimitry Andric } 24730b57cec5SDimitry Andric case CallingConv::AMDGPU_KERNEL: 24740b57cec5SDimitry Andric case CallingConv::SPIR_KERNEL: 2475*81ad6265SDimitry Andric Check(F.getReturnType()->isVoidTy(), 24760b57cec5SDimitry Andric "Calling convention requires void return type", &F); 24770b57cec5SDimitry Andric LLVM_FALLTHROUGH; 24780b57cec5SDimitry Andric case CallingConv::AMDGPU_VS: 24790b57cec5SDimitry Andric case CallingConv::AMDGPU_HS: 24800b57cec5SDimitry Andric case CallingConv::AMDGPU_GS: 24810b57cec5SDimitry Andric case CallingConv::AMDGPU_PS: 24820b57cec5SDimitry Andric case CallingConv::AMDGPU_CS: 2483*81ad6265SDimitry Andric Check(!F.hasStructRetAttr(), "Calling convention does not allow sret", &F); 2484e8d8bef9SDimitry Andric if (F.getCallingConv() != CallingConv::SPIR_KERNEL) { 2485e8d8bef9SDimitry Andric const unsigned StackAS = DL.getAllocaAddrSpace(); 2486e8d8bef9SDimitry Andric unsigned i = 0; 2487e8d8bef9SDimitry Andric for (const Argument &Arg : F.args()) { 2488*81ad6265SDimitry Andric Check(!Attrs.hasParamAttr(i, Attribute::ByVal), 2489e8d8bef9SDimitry Andric "Calling convention disallows byval", &F); 2490*81ad6265SDimitry Andric Check(!Attrs.hasParamAttr(i, Attribute::Preallocated), 2491e8d8bef9SDimitry Andric "Calling convention disallows preallocated", &F); 2492*81ad6265SDimitry Andric Check(!Attrs.hasParamAttr(i, Attribute::InAlloca), 2493e8d8bef9SDimitry Andric "Calling convention disallows inalloca", &F); 2494e8d8bef9SDimitry Andric 2495349cc55cSDimitry Andric if (Attrs.hasParamAttr(i, Attribute::ByRef)) { 2496e8d8bef9SDimitry Andric // FIXME: Should also disallow LDS and GDS, but we don't have the enum 2497e8d8bef9SDimitry Andric // value here. 2498*81ad6265SDimitry Andric Check(Arg.getType()->getPointerAddressSpace() != StackAS, 2499e8d8bef9SDimitry Andric "Calling convention disallows stack byref", &F); 2500e8d8bef9SDimitry Andric } 2501e8d8bef9SDimitry Andric 2502e8d8bef9SDimitry Andric ++i; 2503e8d8bef9SDimitry Andric } 2504e8d8bef9SDimitry Andric } 2505e8d8bef9SDimitry Andric 25060b57cec5SDimitry Andric LLVM_FALLTHROUGH; 25070b57cec5SDimitry Andric case CallingConv::Fast: 25080b57cec5SDimitry Andric case CallingConv::Cold: 25090b57cec5SDimitry Andric case CallingConv::Intel_OCL_BI: 25100b57cec5SDimitry Andric case CallingConv::PTX_Kernel: 25110b57cec5SDimitry Andric case CallingConv::PTX_Device: 2512*81ad6265SDimitry Andric Check(!F.isVarArg(), 2513*81ad6265SDimitry Andric "Calling convention does not support varargs or " 25140b57cec5SDimitry Andric "perfect forwarding!", 25150b57cec5SDimitry Andric &F); 25160b57cec5SDimitry Andric break; 25170b57cec5SDimitry Andric } 25180b57cec5SDimitry Andric 25190b57cec5SDimitry Andric // Check that the argument values match the function type for this function... 25200b57cec5SDimitry Andric unsigned i = 0; 25210b57cec5SDimitry Andric for (const Argument &Arg : F.args()) { 2522*81ad6265SDimitry Andric Check(Arg.getType() == FT->getParamType(i), 25230b57cec5SDimitry Andric "Argument value does not match function argument type!", &Arg, 25240b57cec5SDimitry Andric FT->getParamType(i)); 2525*81ad6265SDimitry Andric Check(Arg.getType()->isFirstClassType(), 25260b57cec5SDimitry Andric "Function arguments must have first-class types!", &Arg); 2527fe6060f1SDimitry Andric if (!IsIntrinsic) { 2528*81ad6265SDimitry Andric Check(!Arg.getType()->isMetadataTy(), 25290b57cec5SDimitry Andric "Function takes metadata but isn't an intrinsic", &Arg, &F); 2530*81ad6265SDimitry Andric Check(!Arg.getType()->isTokenTy(), 25310b57cec5SDimitry Andric "Function takes token but isn't an intrinsic", &Arg, &F); 2532*81ad6265SDimitry Andric Check(!Arg.getType()->isX86_AMXTy(), 2533fe6060f1SDimitry Andric "Function takes x86_amx but isn't an intrinsic", &Arg, &F); 25340b57cec5SDimitry Andric } 25350b57cec5SDimitry Andric 25360b57cec5SDimitry Andric // Check that swifterror argument is only used by loads and stores. 2537349cc55cSDimitry Andric if (Attrs.hasParamAttr(i, Attribute::SwiftError)) { 25380b57cec5SDimitry Andric verifySwiftErrorValue(&Arg); 25390b57cec5SDimitry Andric } 25400b57cec5SDimitry Andric ++i; 25410b57cec5SDimitry Andric } 25420b57cec5SDimitry Andric 2543fe6060f1SDimitry Andric if (!IsIntrinsic) { 2544*81ad6265SDimitry Andric Check(!F.getReturnType()->isTokenTy(), 2545fe6060f1SDimitry Andric "Function returns a token but isn't an intrinsic", &F); 2546*81ad6265SDimitry Andric Check(!F.getReturnType()->isX86_AMXTy(), 2547fe6060f1SDimitry Andric "Function returns a x86_amx but isn't an intrinsic", &F); 2548fe6060f1SDimitry Andric } 25490b57cec5SDimitry Andric 25500b57cec5SDimitry Andric // Get the function metadata attachments. 25510b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 25520b57cec5SDimitry Andric F.getAllMetadata(MDs); 25530b57cec5SDimitry Andric assert(F.hasMetadata() != MDs.empty() && "Bit out-of-sync"); 25540b57cec5SDimitry Andric verifyFunctionMetadata(MDs); 25550b57cec5SDimitry Andric 25560b57cec5SDimitry Andric // Check validity of the personality function 25570b57cec5SDimitry Andric if (F.hasPersonalityFn()) { 25580b57cec5SDimitry Andric auto *Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts()); 25590b57cec5SDimitry Andric if (Per) 2560*81ad6265SDimitry Andric Check(Per->getParent() == F.getParent(), 2561*81ad6265SDimitry Andric "Referencing personality function in another module!", &F, 2562*81ad6265SDimitry Andric F.getParent(), Per, Per->getParent()); 25630b57cec5SDimitry Andric } 25640b57cec5SDimitry Andric 25650b57cec5SDimitry Andric if (F.isMaterializable()) { 25660b57cec5SDimitry Andric // Function has a body somewhere we can't see. 2567*81ad6265SDimitry Andric Check(MDs.empty(), "unmaterialized function cannot have metadata", &F, 25680b57cec5SDimitry Andric MDs.empty() ? nullptr : MDs.front().second); 25690b57cec5SDimitry Andric } else if (F.isDeclaration()) { 25700b57cec5SDimitry Andric for (const auto &I : MDs) { 25710b57cec5SDimitry Andric // This is used for call site debug information. 2572*81ad6265SDimitry Andric CheckDI(I.first != LLVMContext::MD_dbg || 25730b57cec5SDimitry Andric !cast<DISubprogram>(I.second)->isDistinct(), 25740b57cec5SDimitry Andric "function declaration may only have a unique !dbg attachment", 25750b57cec5SDimitry Andric &F); 2576*81ad6265SDimitry Andric Check(I.first != LLVMContext::MD_prof, 25770b57cec5SDimitry Andric "function declaration may not have a !prof attachment", &F); 25780b57cec5SDimitry Andric 25790b57cec5SDimitry Andric // Verify the metadata itself. 25805ffd83dbSDimitry Andric visitMDNode(*I.second, AreDebugLocsAllowed::Yes); 25810b57cec5SDimitry Andric } 2582*81ad6265SDimitry Andric Check(!F.hasPersonalityFn(), 25830b57cec5SDimitry Andric "Function declaration shouldn't have a personality routine", &F); 25840b57cec5SDimitry Andric } else { 25850b57cec5SDimitry Andric // Verify that this function (which has a body) is not named "llvm.*". It 25860b57cec5SDimitry Andric // is not legal to define intrinsics. 2587*81ad6265SDimitry Andric Check(!IsIntrinsic, "llvm intrinsics cannot be defined!", &F); 25880b57cec5SDimitry Andric 25890b57cec5SDimitry Andric // Check the entry node 25900b57cec5SDimitry Andric const BasicBlock *Entry = &F.getEntryBlock(); 2591*81ad6265SDimitry Andric Check(pred_empty(Entry), 25920b57cec5SDimitry Andric "Entry block to function must not have predecessors!", Entry); 25930b57cec5SDimitry Andric 25940b57cec5SDimitry Andric // The address of the entry block cannot be taken, unless it is dead. 25950b57cec5SDimitry Andric if (Entry->hasAddressTaken()) { 2596*81ad6265SDimitry Andric Check(!BlockAddress::lookup(Entry)->isConstantUsed(), 25970b57cec5SDimitry Andric "blockaddress may not be used with the entry block!", Entry); 25980b57cec5SDimitry Andric } 25990b57cec5SDimitry Andric 26000b57cec5SDimitry Andric unsigned NumDebugAttachments = 0, NumProfAttachments = 0; 26010b57cec5SDimitry Andric // Visit metadata attachments. 26020b57cec5SDimitry Andric for (const auto &I : MDs) { 26030b57cec5SDimitry Andric // Verify that the attachment is legal. 26045ffd83dbSDimitry Andric auto AllowLocs = AreDebugLocsAllowed::No; 26050b57cec5SDimitry Andric switch (I.first) { 26060b57cec5SDimitry Andric default: 26070b57cec5SDimitry Andric break; 26080b57cec5SDimitry Andric case LLVMContext::MD_dbg: { 26090b57cec5SDimitry Andric ++NumDebugAttachments; 2610*81ad6265SDimitry Andric CheckDI(NumDebugAttachments == 1, 26110b57cec5SDimitry Andric "function must have a single !dbg attachment", &F, I.second); 2612*81ad6265SDimitry Andric CheckDI(isa<DISubprogram>(I.second), 26130b57cec5SDimitry Andric "function !dbg attachment must be a subprogram", &F, I.second); 2614*81ad6265SDimitry Andric CheckDI(cast<DISubprogram>(I.second)->isDistinct(), 2615e8d8bef9SDimitry Andric "function definition may only have a distinct !dbg attachment", 2616e8d8bef9SDimitry Andric &F); 2617e8d8bef9SDimitry Andric 26180b57cec5SDimitry Andric auto *SP = cast<DISubprogram>(I.second); 26190b57cec5SDimitry Andric const Function *&AttachedTo = DISubprogramAttachments[SP]; 2620*81ad6265SDimitry Andric CheckDI(!AttachedTo || AttachedTo == &F, 26210b57cec5SDimitry Andric "DISubprogram attached to more than one function", SP, &F); 26220b57cec5SDimitry Andric AttachedTo = &F; 26235ffd83dbSDimitry Andric AllowLocs = AreDebugLocsAllowed::Yes; 26240b57cec5SDimitry Andric break; 26250b57cec5SDimitry Andric } 26260b57cec5SDimitry Andric case LLVMContext::MD_prof: 26270b57cec5SDimitry Andric ++NumProfAttachments; 2628*81ad6265SDimitry Andric Check(NumProfAttachments == 1, 26290b57cec5SDimitry Andric "function must have a single !prof attachment", &F, I.second); 26300b57cec5SDimitry Andric break; 26310b57cec5SDimitry Andric } 26320b57cec5SDimitry Andric 26330b57cec5SDimitry Andric // Verify the metadata itself. 26345ffd83dbSDimitry Andric visitMDNode(*I.second, AllowLocs); 26350b57cec5SDimitry Andric } 26360b57cec5SDimitry Andric } 26370b57cec5SDimitry Andric 26380b57cec5SDimitry Andric // If this function is actually an intrinsic, verify that it is only used in 26390b57cec5SDimitry Andric // direct call/invokes, never having its "address taken". 26400b57cec5SDimitry Andric // Only do this if the module is materialized, otherwise we don't have all the 26410b57cec5SDimitry Andric // uses. 2642fe6060f1SDimitry Andric if (F.isIntrinsic() && F.getParent()->isMaterialized()) { 26430b57cec5SDimitry Andric const User *U; 2644349cc55cSDimitry Andric if (F.hasAddressTaken(&U, false, true, false, 2645349cc55cSDimitry Andric /*IgnoreARCAttachedCall=*/true)) 2646*81ad6265SDimitry Andric Check(false, "Invalid user of intrinsic instruction!", U); 26470b57cec5SDimitry Andric } 26480b57cec5SDimitry Andric 2649fe6060f1SDimitry Andric // Check intrinsics' signatures. 2650fe6060f1SDimitry Andric switch (F.getIntrinsicID()) { 2651fe6060f1SDimitry Andric case Intrinsic::experimental_gc_get_pointer_base: { 2652fe6060f1SDimitry Andric FunctionType *FT = F.getFunctionType(); 2653*81ad6265SDimitry Andric Check(FT->getNumParams() == 1, "wrong number of parameters", F); 2654*81ad6265SDimitry Andric Check(isa<PointerType>(F.getReturnType()), 2655fe6060f1SDimitry Andric "gc.get.pointer.base must return a pointer", F); 2656*81ad6265SDimitry Andric Check(FT->getParamType(0) == F.getReturnType(), 2657*81ad6265SDimitry Andric "gc.get.pointer.base operand and result must be of the same type", F); 2658fe6060f1SDimitry Andric break; 2659fe6060f1SDimitry Andric } 2660fe6060f1SDimitry Andric case Intrinsic::experimental_gc_get_pointer_offset: { 2661fe6060f1SDimitry Andric FunctionType *FT = F.getFunctionType(); 2662*81ad6265SDimitry Andric Check(FT->getNumParams() == 1, "wrong number of parameters", F); 2663*81ad6265SDimitry Andric Check(isa<PointerType>(FT->getParamType(0)), 2664fe6060f1SDimitry Andric "gc.get.pointer.offset operand must be a pointer", F); 2665*81ad6265SDimitry Andric Check(F.getReturnType()->isIntegerTy(), 2666fe6060f1SDimitry Andric "gc.get.pointer.offset must return integer", F); 2667fe6060f1SDimitry Andric break; 2668fe6060f1SDimitry Andric } 2669fe6060f1SDimitry Andric } 2670fe6060f1SDimitry Andric 26710b57cec5SDimitry Andric auto *N = F.getSubprogram(); 26720b57cec5SDimitry Andric HasDebugInfo = (N != nullptr); 26730b57cec5SDimitry Andric if (!HasDebugInfo) 26740b57cec5SDimitry Andric return; 26750b57cec5SDimitry Andric 26765ffd83dbSDimitry Andric // Check that all !dbg attachments lead to back to N. 26770b57cec5SDimitry Andric // 26780b57cec5SDimitry Andric // FIXME: Check this incrementally while visiting !dbg attachments. 26790b57cec5SDimitry Andric // FIXME: Only check when N is the canonical subprogram for F. 26800b57cec5SDimitry Andric SmallPtrSet<const MDNode *, 32> Seen; 26810b57cec5SDimitry Andric auto VisitDebugLoc = [&](const Instruction &I, const MDNode *Node) { 26820b57cec5SDimitry Andric // Be careful about using DILocation here since we might be dealing with 26830b57cec5SDimitry Andric // broken code (this is the Verifier after all). 26840b57cec5SDimitry Andric const DILocation *DL = dyn_cast_or_null<DILocation>(Node); 26850b57cec5SDimitry Andric if (!DL) 26860b57cec5SDimitry Andric return; 26870b57cec5SDimitry Andric if (!Seen.insert(DL).second) 26880b57cec5SDimitry Andric return; 26890b57cec5SDimitry Andric 26900b57cec5SDimitry Andric Metadata *Parent = DL->getRawScope(); 2691*81ad6265SDimitry Andric CheckDI(Parent && isa<DILocalScope>(Parent), 2692*81ad6265SDimitry Andric "DILocation's scope must be a DILocalScope", N, &F, &I, DL, Parent); 26935ffd83dbSDimitry Andric 26940b57cec5SDimitry Andric DILocalScope *Scope = DL->getInlinedAtScope(); 2695*81ad6265SDimitry Andric Check(Scope, "Failed to find DILocalScope", DL); 26965ffd83dbSDimitry Andric 26975ffd83dbSDimitry Andric if (!Seen.insert(Scope).second) 26980b57cec5SDimitry Andric return; 26990b57cec5SDimitry Andric 27005ffd83dbSDimitry Andric DISubprogram *SP = Scope->getSubprogram(); 27010b57cec5SDimitry Andric 27020b57cec5SDimitry Andric // Scope and SP could be the same MDNode and we don't want to skip 27030b57cec5SDimitry Andric // validation in that case 27040b57cec5SDimitry Andric if (SP && ((Scope != SP) && !Seen.insert(SP).second)) 27050b57cec5SDimitry Andric return; 27060b57cec5SDimitry Andric 2707*81ad6265SDimitry Andric CheckDI(SP->describes(&F), 27080b57cec5SDimitry Andric "!dbg attachment points at wrong subprogram for function", N, &F, 27090b57cec5SDimitry Andric &I, DL, Scope, SP); 27100b57cec5SDimitry Andric }; 27110b57cec5SDimitry Andric for (auto &BB : F) 27120b57cec5SDimitry Andric for (auto &I : BB) { 27130b57cec5SDimitry Andric VisitDebugLoc(I, I.getDebugLoc().getAsMDNode()); 27140b57cec5SDimitry Andric // The llvm.loop annotations also contain two DILocations. 27150b57cec5SDimitry Andric if (auto MD = I.getMetadata(LLVMContext::MD_loop)) 27160b57cec5SDimitry Andric for (unsigned i = 1; i < MD->getNumOperands(); ++i) 27170b57cec5SDimitry Andric VisitDebugLoc(I, dyn_cast_or_null<MDNode>(MD->getOperand(i))); 27180b57cec5SDimitry Andric if (BrokenDebugInfo) 27190b57cec5SDimitry Andric return; 27200b57cec5SDimitry Andric } 27210b57cec5SDimitry Andric } 27220b57cec5SDimitry Andric 27230b57cec5SDimitry Andric // verifyBasicBlock - Verify that a basic block is well formed... 27240b57cec5SDimitry Andric // 27250b57cec5SDimitry Andric void Verifier::visitBasicBlock(BasicBlock &BB) { 27260b57cec5SDimitry Andric InstsInThisBlock.clear(); 27270b57cec5SDimitry Andric 27280b57cec5SDimitry Andric // Ensure that basic blocks have terminators! 2729*81ad6265SDimitry Andric Check(BB.getTerminator(), "Basic Block does not have terminator!", &BB); 27300b57cec5SDimitry Andric 27310b57cec5SDimitry Andric // Check constraints that this basic block imposes on all of the PHI nodes in 27320b57cec5SDimitry Andric // it. 27330b57cec5SDimitry Andric if (isa<PHINode>(BB.front())) { 2734e8d8bef9SDimitry Andric SmallVector<BasicBlock *, 8> Preds(predecessors(&BB)); 27350b57cec5SDimitry Andric SmallVector<std::pair<BasicBlock*, Value*>, 8> Values; 27360b57cec5SDimitry Andric llvm::sort(Preds); 27370b57cec5SDimitry Andric for (const PHINode &PN : BB.phis()) { 2738*81ad6265SDimitry Andric Check(PN.getNumIncomingValues() == Preds.size(), 27390b57cec5SDimitry Andric "PHINode should have one entry for each predecessor of its " 27400b57cec5SDimitry Andric "parent basic block!", 27410b57cec5SDimitry Andric &PN); 27420b57cec5SDimitry Andric 27430b57cec5SDimitry Andric // Get and sort all incoming values in the PHI node... 27440b57cec5SDimitry Andric Values.clear(); 27450b57cec5SDimitry Andric Values.reserve(PN.getNumIncomingValues()); 27460b57cec5SDimitry Andric for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) 27470b57cec5SDimitry Andric Values.push_back( 27480b57cec5SDimitry Andric std::make_pair(PN.getIncomingBlock(i), PN.getIncomingValue(i))); 27490b57cec5SDimitry Andric llvm::sort(Values); 27500b57cec5SDimitry Andric 27510b57cec5SDimitry Andric for (unsigned i = 0, e = Values.size(); i != e; ++i) { 27520b57cec5SDimitry Andric // Check to make sure that if there is more than one entry for a 27530b57cec5SDimitry Andric // particular basic block in this PHI node, that the incoming values are 27540b57cec5SDimitry Andric // all identical. 27550b57cec5SDimitry Andric // 2756*81ad6265SDimitry Andric Check(i == 0 || Values[i].first != Values[i - 1].first || 27570b57cec5SDimitry Andric Values[i].second == Values[i - 1].second, 27580b57cec5SDimitry Andric "PHI node has multiple entries for the same basic block with " 27590b57cec5SDimitry Andric "different incoming values!", 27600b57cec5SDimitry Andric &PN, Values[i].first, Values[i].second, Values[i - 1].second); 27610b57cec5SDimitry Andric 27620b57cec5SDimitry Andric // Check to make sure that the predecessors and PHI node entries are 27630b57cec5SDimitry Andric // matched up. 2764*81ad6265SDimitry Andric Check(Values[i].first == Preds[i], 27650b57cec5SDimitry Andric "PHI node entries do not match predecessors!", &PN, 27660b57cec5SDimitry Andric Values[i].first, Preds[i]); 27670b57cec5SDimitry Andric } 27680b57cec5SDimitry Andric } 27690b57cec5SDimitry Andric } 27700b57cec5SDimitry Andric 27710b57cec5SDimitry Andric // Check that all instructions have their parent pointers set up correctly. 27720b57cec5SDimitry Andric for (auto &I : BB) 27730b57cec5SDimitry Andric { 2774*81ad6265SDimitry Andric Check(I.getParent() == &BB, "Instruction has bogus parent pointer!"); 27750b57cec5SDimitry Andric } 27760b57cec5SDimitry Andric } 27770b57cec5SDimitry Andric 27780b57cec5SDimitry Andric void Verifier::visitTerminator(Instruction &I) { 27790b57cec5SDimitry Andric // Ensure that terminators only exist at the end of the basic block. 2780*81ad6265SDimitry Andric Check(&I == I.getParent()->getTerminator(), 27810b57cec5SDimitry Andric "Terminator found in the middle of a basic block!", I.getParent()); 27820b57cec5SDimitry Andric visitInstruction(I); 27830b57cec5SDimitry Andric } 27840b57cec5SDimitry Andric 27850b57cec5SDimitry Andric void Verifier::visitBranchInst(BranchInst &BI) { 27860b57cec5SDimitry Andric if (BI.isConditional()) { 2787*81ad6265SDimitry Andric Check(BI.getCondition()->getType()->isIntegerTy(1), 27880b57cec5SDimitry Andric "Branch condition is not 'i1' type!", &BI, BI.getCondition()); 27890b57cec5SDimitry Andric } 27900b57cec5SDimitry Andric visitTerminator(BI); 27910b57cec5SDimitry Andric } 27920b57cec5SDimitry Andric 27930b57cec5SDimitry Andric void Verifier::visitReturnInst(ReturnInst &RI) { 27940b57cec5SDimitry Andric Function *F = RI.getParent()->getParent(); 27950b57cec5SDimitry Andric unsigned N = RI.getNumOperands(); 27960b57cec5SDimitry Andric if (F->getReturnType()->isVoidTy()) 2797*81ad6265SDimitry Andric Check(N == 0, 27980b57cec5SDimitry Andric "Found return instr that returns non-void in Function of void " 27990b57cec5SDimitry Andric "return type!", 28000b57cec5SDimitry Andric &RI, F->getReturnType()); 28010b57cec5SDimitry Andric else 2802*81ad6265SDimitry Andric Check(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(), 28030b57cec5SDimitry Andric "Function return type does not match operand " 28040b57cec5SDimitry Andric "type of return inst!", 28050b57cec5SDimitry Andric &RI, F->getReturnType()); 28060b57cec5SDimitry Andric 28070b57cec5SDimitry Andric // Check to make sure that the return value has necessary properties for 28080b57cec5SDimitry Andric // terminators... 28090b57cec5SDimitry Andric visitTerminator(RI); 28100b57cec5SDimitry Andric } 28110b57cec5SDimitry Andric 28120b57cec5SDimitry Andric void Verifier::visitSwitchInst(SwitchInst &SI) { 2813*81ad6265SDimitry Andric Check(SI.getType()->isVoidTy(), "Switch must have void result type!", &SI); 28140b57cec5SDimitry Andric // Check to make sure that all of the constants in the switch instruction 28150b57cec5SDimitry Andric // have the same type as the switched-on value. 28160b57cec5SDimitry Andric Type *SwitchTy = SI.getCondition()->getType(); 28170b57cec5SDimitry Andric SmallPtrSet<ConstantInt*, 32> Constants; 28180b57cec5SDimitry Andric for (auto &Case : SI.cases()) { 2819*81ad6265SDimitry Andric Check(Case.getCaseValue()->getType() == SwitchTy, 28200b57cec5SDimitry Andric "Switch constants must all be same type as switch value!", &SI); 2821*81ad6265SDimitry Andric Check(Constants.insert(Case.getCaseValue()).second, 28220b57cec5SDimitry Andric "Duplicate integer as switch case", &SI, Case.getCaseValue()); 28230b57cec5SDimitry Andric } 28240b57cec5SDimitry Andric 28250b57cec5SDimitry Andric visitTerminator(SI); 28260b57cec5SDimitry Andric } 28270b57cec5SDimitry Andric 28280b57cec5SDimitry Andric void Verifier::visitIndirectBrInst(IndirectBrInst &BI) { 2829*81ad6265SDimitry Andric Check(BI.getAddress()->getType()->isPointerTy(), 28300b57cec5SDimitry Andric "Indirectbr operand must have pointer type!", &BI); 28310b57cec5SDimitry Andric for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i) 2832*81ad6265SDimitry Andric Check(BI.getDestination(i)->getType()->isLabelTy(), 28330b57cec5SDimitry Andric "Indirectbr destinations must all have pointer type!", &BI); 28340b57cec5SDimitry Andric 28350b57cec5SDimitry Andric visitTerminator(BI); 28360b57cec5SDimitry Andric } 28370b57cec5SDimitry Andric 28380b57cec5SDimitry Andric void Verifier::visitCallBrInst(CallBrInst &CBI) { 2839*81ad6265SDimitry Andric Check(CBI.isInlineAsm(), "Callbr is currently only used for asm-goto!", &CBI); 2840fe6060f1SDimitry Andric const InlineAsm *IA = cast<InlineAsm>(CBI.getCalledOperand()); 2841*81ad6265SDimitry Andric Check(!IA->canThrow(), "Unwinding from Callbr is not allowed"); 28420b57cec5SDimitry Andric for (unsigned i = 0, e = CBI.getNumSuccessors(); i != e; ++i) 2843*81ad6265SDimitry Andric Check(CBI.getSuccessor(i)->getType()->isLabelTy(), 28440b57cec5SDimitry Andric "Callbr successors must all have pointer type!", &CBI); 28450b57cec5SDimitry Andric for (unsigned i = 0, e = CBI.getNumOperands(); i != e; ++i) { 2846*81ad6265SDimitry Andric Check(i >= CBI.arg_size() || !isa<BasicBlock>(CBI.getOperand(i)), 28470b57cec5SDimitry Andric "Using an unescaped label as a callbr argument!", &CBI); 28480b57cec5SDimitry Andric if (isa<BasicBlock>(CBI.getOperand(i))) 28490b57cec5SDimitry Andric for (unsigned j = i + 1; j != e; ++j) 2850*81ad6265SDimitry Andric Check(CBI.getOperand(i) != CBI.getOperand(j), 28510b57cec5SDimitry Andric "Duplicate callbr destination!", &CBI); 28520b57cec5SDimitry Andric } 28538bcb0991SDimitry Andric { 28548bcb0991SDimitry Andric SmallPtrSet<BasicBlock *, 4> ArgBBs; 28558bcb0991SDimitry Andric for (Value *V : CBI.args()) 28568bcb0991SDimitry Andric if (auto *BA = dyn_cast<BlockAddress>(V)) 28578bcb0991SDimitry Andric ArgBBs.insert(BA->getBasicBlock()); 28588bcb0991SDimitry Andric for (BasicBlock *BB : CBI.getIndirectDests()) 2859*81ad6265SDimitry Andric Check(ArgBBs.count(BB), "Indirect label missing from arglist.", &CBI); 28608bcb0991SDimitry Andric } 28610b57cec5SDimitry Andric 286204eeddc0SDimitry Andric verifyInlineAsmCall(CBI); 28630b57cec5SDimitry Andric visitTerminator(CBI); 28640b57cec5SDimitry Andric } 28650b57cec5SDimitry Andric 28660b57cec5SDimitry Andric void Verifier::visitSelectInst(SelectInst &SI) { 2867*81ad6265SDimitry Andric Check(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1), 28680b57cec5SDimitry Andric SI.getOperand(2)), 28690b57cec5SDimitry Andric "Invalid operands for select instruction!", &SI); 28700b57cec5SDimitry Andric 2871*81ad6265SDimitry Andric Check(SI.getTrueValue()->getType() == SI.getType(), 28720b57cec5SDimitry Andric "Select values must have same type as select instruction!", &SI); 28730b57cec5SDimitry Andric visitInstruction(SI); 28740b57cec5SDimitry Andric } 28750b57cec5SDimitry Andric 28760b57cec5SDimitry Andric /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of 28770b57cec5SDimitry Andric /// a pass, if any exist, it's an error. 28780b57cec5SDimitry Andric /// 28790b57cec5SDimitry Andric void Verifier::visitUserOp1(Instruction &I) { 2880*81ad6265SDimitry Andric Check(false, "User-defined operators should not live outside of a pass!", &I); 28810b57cec5SDimitry Andric } 28820b57cec5SDimitry Andric 28830b57cec5SDimitry Andric void Verifier::visitTruncInst(TruncInst &I) { 28840b57cec5SDimitry Andric // Get the source and destination types 28850b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType(); 28860b57cec5SDimitry Andric Type *DestTy = I.getType(); 28870b57cec5SDimitry Andric 28880b57cec5SDimitry Andric // Get the size of the types in bits, we'll need this later 28890b57cec5SDimitry Andric unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); 28900b57cec5SDimitry Andric unsigned DestBitSize = DestTy->getScalarSizeInBits(); 28910b57cec5SDimitry Andric 2892*81ad6265SDimitry Andric Check(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I); 2893*81ad6265SDimitry Andric Check(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I); 2894*81ad6265SDimitry Andric Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), 28950b57cec5SDimitry Andric "trunc source and destination must both be a vector or neither", &I); 2896*81ad6265SDimitry Andric Check(SrcBitSize > DestBitSize, "DestTy too big for Trunc", &I); 28970b57cec5SDimitry Andric 28980b57cec5SDimitry Andric visitInstruction(I); 28990b57cec5SDimitry Andric } 29000b57cec5SDimitry Andric 29010b57cec5SDimitry Andric void Verifier::visitZExtInst(ZExtInst &I) { 29020b57cec5SDimitry Andric // Get the source and destination types 29030b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType(); 29040b57cec5SDimitry Andric Type *DestTy = I.getType(); 29050b57cec5SDimitry Andric 29060b57cec5SDimitry Andric // Get the size of the types in bits, we'll need this later 2907*81ad6265SDimitry Andric Check(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I); 2908*81ad6265SDimitry Andric Check(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I); 2909*81ad6265SDimitry Andric Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), 29100b57cec5SDimitry Andric "zext source and destination must both be a vector or neither", &I); 29110b57cec5SDimitry Andric unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); 29120b57cec5SDimitry Andric unsigned DestBitSize = DestTy->getScalarSizeInBits(); 29130b57cec5SDimitry Andric 2914*81ad6265SDimitry Andric Check(SrcBitSize < DestBitSize, "Type too small for ZExt", &I); 29150b57cec5SDimitry Andric 29160b57cec5SDimitry Andric visitInstruction(I); 29170b57cec5SDimitry Andric } 29180b57cec5SDimitry Andric 29190b57cec5SDimitry Andric void Verifier::visitSExtInst(SExtInst &I) { 29200b57cec5SDimitry Andric // Get the source and destination types 29210b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType(); 29220b57cec5SDimitry Andric Type *DestTy = I.getType(); 29230b57cec5SDimitry Andric 29240b57cec5SDimitry Andric // Get the size of the types in bits, we'll need this later 29250b57cec5SDimitry Andric unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); 29260b57cec5SDimitry Andric unsigned DestBitSize = DestTy->getScalarSizeInBits(); 29270b57cec5SDimitry Andric 2928*81ad6265SDimitry Andric Check(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I); 2929*81ad6265SDimitry Andric Check(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I); 2930*81ad6265SDimitry Andric Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), 29310b57cec5SDimitry Andric "sext source and destination must both be a vector or neither", &I); 2932*81ad6265SDimitry Andric Check(SrcBitSize < DestBitSize, "Type too small for SExt", &I); 29330b57cec5SDimitry Andric 29340b57cec5SDimitry Andric visitInstruction(I); 29350b57cec5SDimitry Andric } 29360b57cec5SDimitry Andric 29370b57cec5SDimitry Andric void Verifier::visitFPTruncInst(FPTruncInst &I) { 29380b57cec5SDimitry Andric // Get the source and destination types 29390b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType(); 29400b57cec5SDimitry Andric Type *DestTy = I.getType(); 29410b57cec5SDimitry Andric // Get the size of the types in bits, we'll need this later 29420b57cec5SDimitry Andric unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); 29430b57cec5SDimitry Andric unsigned DestBitSize = DestTy->getScalarSizeInBits(); 29440b57cec5SDimitry Andric 2945*81ad6265SDimitry Andric Check(SrcTy->isFPOrFPVectorTy(), "FPTrunc only operates on FP", &I); 2946*81ad6265SDimitry Andric Check(DestTy->isFPOrFPVectorTy(), "FPTrunc only produces an FP", &I); 2947*81ad6265SDimitry Andric Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), 29480b57cec5SDimitry Andric "fptrunc source and destination must both be a vector or neither", &I); 2949*81ad6265SDimitry Andric Check(SrcBitSize > DestBitSize, "DestTy too big for FPTrunc", &I); 29500b57cec5SDimitry Andric 29510b57cec5SDimitry Andric visitInstruction(I); 29520b57cec5SDimitry Andric } 29530b57cec5SDimitry Andric 29540b57cec5SDimitry Andric void Verifier::visitFPExtInst(FPExtInst &I) { 29550b57cec5SDimitry Andric // Get the source and destination types 29560b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType(); 29570b57cec5SDimitry Andric Type *DestTy = I.getType(); 29580b57cec5SDimitry Andric 29590b57cec5SDimitry Andric // Get the size of the types in bits, we'll need this later 29600b57cec5SDimitry Andric unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); 29610b57cec5SDimitry Andric unsigned DestBitSize = DestTy->getScalarSizeInBits(); 29620b57cec5SDimitry Andric 2963*81ad6265SDimitry Andric Check(SrcTy->isFPOrFPVectorTy(), "FPExt only operates on FP", &I); 2964*81ad6265SDimitry Andric Check(DestTy->isFPOrFPVectorTy(), "FPExt only produces an FP", &I); 2965*81ad6265SDimitry Andric Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), 29660b57cec5SDimitry Andric "fpext source and destination must both be a vector or neither", &I); 2967*81ad6265SDimitry Andric Check(SrcBitSize < DestBitSize, "DestTy too small for FPExt", &I); 29680b57cec5SDimitry Andric 29690b57cec5SDimitry Andric visitInstruction(I); 29700b57cec5SDimitry Andric } 29710b57cec5SDimitry Andric 29720b57cec5SDimitry Andric void Verifier::visitUIToFPInst(UIToFPInst &I) { 29730b57cec5SDimitry Andric // Get the source and destination types 29740b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType(); 29750b57cec5SDimitry Andric Type *DestTy = I.getType(); 29760b57cec5SDimitry Andric 29770b57cec5SDimitry Andric bool SrcVec = SrcTy->isVectorTy(); 29780b57cec5SDimitry Andric bool DstVec = DestTy->isVectorTy(); 29790b57cec5SDimitry Andric 2980*81ad6265SDimitry Andric Check(SrcVec == DstVec, 29810b57cec5SDimitry Andric "UIToFP source and dest must both be vector or scalar", &I); 2982*81ad6265SDimitry Andric Check(SrcTy->isIntOrIntVectorTy(), 29830b57cec5SDimitry Andric "UIToFP source must be integer or integer vector", &I); 2984*81ad6265SDimitry Andric Check(DestTy->isFPOrFPVectorTy(), "UIToFP result must be FP or FP vector", 29850b57cec5SDimitry Andric &I); 29860b57cec5SDimitry Andric 29870b57cec5SDimitry Andric if (SrcVec && DstVec) 2988*81ad6265SDimitry Andric Check(cast<VectorType>(SrcTy)->getElementCount() == 29895ffd83dbSDimitry Andric cast<VectorType>(DestTy)->getElementCount(), 29900b57cec5SDimitry Andric "UIToFP source and dest vector length mismatch", &I); 29910b57cec5SDimitry Andric 29920b57cec5SDimitry Andric visitInstruction(I); 29930b57cec5SDimitry Andric } 29940b57cec5SDimitry Andric 29950b57cec5SDimitry Andric void Verifier::visitSIToFPInst(SIToFPInst &I) { 29960b57cec5SDimitry Andric // Get the source and destination types 29970b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType(); 29980b57cec5SDimitry Andric Type *DestTy = I.getType(); 29990b57cec5SDimitry Andric 30000b57cec5SDimitry Andric bool SrcVec = SrcTy->isVectorTy(); 30010b57cec5SDimitry Andric bool DstVec = DestTy->isVectorTy(); 30020b57cec5SDimitry Andric 3003*81ad6265SDimitry Andric Check(SrcVec == DstVec, 30040b57cec5SDimitry Andric "SIToFP source and dest must both be vector or scalar", &I); 3005*81ad6265SDimitry Andric Check(SrcTy->isIntOrIntVectorTy(), 30060b57cec5SDimitry Andric "SIToFP source must be integer or integer vector", &I); 3007*81ad6265SDimitry Andric Check(DestTy->isFPOrFPVectorTy(), "SIToFP result must be FP or FP vector", 30080b57cec5SDimitry Andric &I); 30090b57cec5SDimitry Andric 30100b57cec5SDimitry Andric if (SrcVec && DstVec) 3011*81ad6265SDimitry Andric Check(cast<VectorType>(SrcTy)->getElementCount() == 30125ffd83dbSDimitry Andric cast<VectorType>(DestTy)->getElementCount(), 30130b57cec5SDimitry Andric "SIToFP source and dest vector length mismatch", &I); 30140b57cec5SDimitry Andric 30150b57cec5SDimitry Andric visitInstruction(I); 30160b57cec5SDimitry Andric } 30170b57cec5SDimitry Andric 30180b57cec5SDimitry Andric void Verifier::visitFPToUIInst(FPToUIInst &I) { 30190b57cec5SDimitry Andric // Get the source and destination types 30200b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType(); 30210b57cec5SDimitry Andric Type *DestTy = I.getType(); 30220b57cec5SDimitry Andric 30230b57cec5SDimitry Andric bool SrcVec = SrcTy->isVectorTy(); 30240b57cec5SDimitry Andric bool DstVec = DestTy->isVectorTy(); 30250b57cec5SDimitry Andric 3026*81ad6265SDimitry Andric Check(SrcVec == DstVec, 30270b57cec5SDimitry Andric "FPToUI source and dest must both be vector or scalar", &I); 3028*81ad6265SDimitry Andric Check(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector", &I); 3029*81ad6265SDimitry Andric Check(DestTy->isIntOrIntVectorTy(), 30300b57cec5SDimitry Andric "FPToUI result must be integer or integer vector", &I); 30310b57cec5SDimitry Andric 30320b57cec5SDimitry Andric if (SrcVec && DstVec) 3033*81ad6265SDimitry Andric Check(cast<VectorType>(SrcTy)->getElementCount() == 30345ffd83dbSDimitry Andric cast<VectorType>(DestTy)->getElementCount(), 30350b57cec5SDimitry Andric "FPToUI source and dest vector length mismatch", &I); 30360b57cec5SDimitry Andric 30370b57cec5SDimitry Andric visitInstruction(I); 30380b57cec5SDimitry Andric } 30390b57cec5SDimitry Andric 30400b57cec5SDimitry Andric void Verifier::visitFPToSIInst(FPToSIInst &I) { 30410b57cec5SDimitry Andric // Get the source and destination types 30420b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType(); 30430b57cec5SDimitry Andric Type *DestTy = I.getType(); 30440b57cec5SDimitry Andric 30450b57cec5SDimitry Andric bool SrcVec = SrcTy->isVectorTy(); 30460b57cec5SDimitry Andric bool DstVec = DestTy->isVectorTy(); 30470b57cec5SDimitry Andric 3048*81ad6265SDimitry Andric Check(SrcVec == DstVec, 30490b57cec5SDimitry Andric "FPToSI source and dest must both be vector or scalar", &I); 3050*81ad6265SDimitry Andric Check(SrcTy->isFPOrFPVectorTy(), "FPToSI source must be FP or FP vector", &I); 3051*81ad6265SDimitry Andric Check(DestTy->isIntOrIntVectorTy(), 30520b57cec5SDimitry Andric "FPToSI result must be integer or integer vector", &I); 30530b57cec5SDimitry Andric 30540b57cec5SDimitry Andric if (SrcVec && DstVec) 3055*81ad6265SDimitry Andric Check(cast<VectorType>(SrcTy)->getElementCount() == 30565ffd83dbSDimitry Andric cast<VectorType>(DestTy)->getElementCount(), 30570b57cec5SDimitry Andric "FPToSI source and dest vector length mismatch", &I); 30580b57cec5SDimitry Andric 30590b57cec5SDimitry Andric visitInstruction(I); 30600b57cec5SDimitry Andric } 30610b57cec5SDimitry Andric 30620b57cec5SDimitry Andric void Verifier::visitPtrToIntInst(PtrToIntInst &I) { 30630b57cec5SDimitry Andric // Get the source and destination types 30640b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType(); 30650b57cec5SDimitry Andric Type *DestTy = I.getType(); 30660b57cec5SDimitry Andric 3067*81ad6265SDimitry Andric Check(SrcTy->isPtrOrPtrVectorTy(), "PtrToInt source must be pointer", &I); 30680b57cec5SDimitry Andric 3069*81ad6265SDimitry Andric Check(DestTy->isIntOrIntVectorTy(), "PtrToInt result must be integral", &I); 3070*81ad6265SDimitry Andric Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), "PtrToInt type mismatch", 30710b57cec5SDimitry Andric &I); 30720b57cec5SDimitry Andric 30730b57cec5SDimitry Andric if (SrcTy->isVectorTy()) { 30745ffd83dbSDimitry Andric auto *VSrc = cast<VectorType>(SrcTy); 30755ffd83dbSDimitry Andric auto *VDest = cast<VectorType>(DestTy); 3076*81ad6265SDimitry Andric Check(VSrc->getElementCount() == VDest->getElementCount(), 30770b57cec5SDimitry Andric "PtrToInt Vector width mismatch", &I); 30780b57cec5SDimitry Andric } 30790b57cec5SDimitry Andric 30800b57cec5SDimitry Andric visitInstruction(I); 30810b57cec5SDimitry Andric } 30820b57cec5SDimitry Andric 30830b57cec5SDimitry Andric void Verifier::visitIntToPtrInst(IntToPtrInst &I) { 30840b57cec5SDimitry Andric // Get the source and destination types 30850b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType(); 30860b57cec5SDimitry Andric Type *DestTy = I.getType(); 30870b57cec5SDimitry Andric 3088*81ad6265SDimitry Andric Check(SrcTy->isIntOrIntVectorTy(), "IntToPtr source must be an integral", &I); 3089*81ad6265SDimitry Andric Check(DestTy->isPtrOrPtrVectorTy(), "IntToPtr result must be a pointer", &I); 30900b57cec5SDimitry Andric 3091*81ad6265SDimitry Andric Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), "IntToPtr type mismatch", 30920b57cec5SDimitry Andric &I); 30930b57cec5SDimitry Andric if (SrcTy->isVectorTy()) { 30945ffd83dbSDimitry Andric auto *VSrc = cast<VectorType>(SrcTy); 30955ffd83dbSDimitry Andric auto *VDest = cast<VectorType>(DestTy); 3096*81ad6265SDimitry Andric Check(VSrc->getElementCount() == VDest->getElementCount(), 30970b57cec5SDimitry Andric "IntToPtr Vector width mismatch", &I); 30980b57cec5SDimitry Andric } 30990b57cec5SDimitry Andric visitInstruction(I); 31000b57cec5SDimitry Andric } 31010b57cec5SDimitry Andric 31020b57cec5SDimitry Andric void Verifier::visitBitCastInst(BitCastInst &I) { 3103*81ad6265SDimitry Andric Check( 31040b57cec5SDimitry Andric CastInst::castIsValid(Instruction::BitCast, I.getOperand(0), I.getType()), 31050b57cec5SDimitry Andric "Invalid bitcast", &I); 31060b57cec5SDimitry Andric visitInstruction(I); 31070b57cec5SDimitry Andric } 31080b57cec5SDimitry Andric 31090b57cec5SDimitry Andric void Verifier::visitAddrSpaceCastInst(AddrSpaceCastInst &I) { 31100b57cec5SDimitry Andric Type *SrcTy = I.getOperand(0)->getType(); 31110b57cec5SDimitry Andric Type *DestTy = I.getType(); 31120b57cec5SDimitry Andric 3113*81ad6265SDimitry Andric Check(SrcTy->isPtrOrPtrVectorTy(), "AddrSpaceCast source must be a pointer", 31140b57cec5SDimitry Andric &I); 3115*81ad6265SDimitry Andric Check(DestTy->isPtrOrPtrVectorTy(), "AddrSpaceCast result must be a pointer", 31160b57cec5SDimitry Andric &I); 3117*81ad6265SDimitry Andric Check(SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace(), 31180b57cec5SDimitry Andric "AddrSpaceCast must be between different address spaces", &I); 31195ffd83dbSDimitry Andric if (auto *SrcVTy = dyn_cast<VectorType>(SrcTy)) 3120*81ad6265SDimitry Andric Check(SrcVTy->getElementCount() == 3121e8d8bef9SDimitry Andric cast<VectorType>(DestTy)->getElementCount(), 31220b57cec5SDimitry Andric "AddrSpaceCast vector pointer number of elements mismatch", &I); 31230b57cec5SDimitry Andric visitInstruction(I); 31240b57cec5SDimitry Andric } 31250b57cec5SDimitry Andric 31260b57cec5SDimitry Andric /// visitPHINode - Ensure that a PHI node is well formed. 31270b57cec5SDimitry Andric /// 31280b57cec5SDimitry Andric void Verifier::visitPHINode(PHINode &PN) { 31290b57cec5SDimitry Andric // Ensure that the PHI nodes are all grouped together at the top of the block. 31300b57cec5SDimitry Andric // This can be tested by checking whether the instruction before this is 31310b57cec5SDimitry Andric // either nonexistent (because this is begin()) or is a PHI node. If not, 31320b57cec5SDimitry Andric // then there is some other instruction before a PHI. 3133*81ad6265SDimitry Andric Check(&PN == &PN.getParent()->front() || 31340b57cec5SDimitry Andric isa<PHINode>(--BasicBlock::iterator(&PN)), 31350b57cec5SDimitry Andric "PHI nodes not grouped at top of basic block!", &PN, PN.getParent()); 31360b57cec5SDimitry Andric 31370b57cec5SDimitry Andric // Check that a PHI doesn't yield a Token. 3138*81ad6265SDimitry Andric Check(!PN.getType()->isTokenTy(), "PHI nodes cannot have token type!"); 31390b57cec5SDimitry Andric 31400b57cec5SDimitry Andric // Check that all of the values of the PHI node have the same type as the 31410b57cec5SDimitry Andric // result, and that the incoming blocks are really basic blocks. 31420b57cec5SDimitry Andric for (Value *IncValue : PN.incoming_values()) { 3143*81ad6265SDimitry Andric Check(PN.getType() == IncValue->getType(), 31440b57cec5SDimitry Andric "PHI node operands are not the same type as the result!", &PN); 31450b57cec5SDimitry Andric } 31460b57cec5SDimitry Andric 31470b57cec5SDimitry Andric // All other PHI node constraints are checked in the visitBasicBlock method. 31480b57cec5SDimitry Andric 31490b57cec5SDimitry Andric visitInstruction(PN); 31500b57cec5SDimitry Andric } 31510b57cec5SDimitry Andric 31520b57cec5SDimitry Andric void Verifier::visitCallBase(CallBase &Call) { 3153*81ad6265SDimitry Andric Check(Call.getCalledOperand()->getType()->isPointerTy(), 31540b57cec5SDimitry Andric "Called function must be a pointer!", Call); 31555ffd83dbSDimitry Andric PointerType *FPTy = cast<PointerType>(Call.getCalledOperand()->getType()); 31560b57cec5SDimitry Andric 3157*81ad6265SDimitry Andric Check(FPTy->isOpaqueOrPointeeTypeMatches(Call.getFunctionType()), 31580b57cec5SDimitry Andric "Called function is not the same type as the call!", Call); 31590b57cec5SDimitry Andric 31600b57cec5SDimitry Andric FunctionType *FTy = Call.getFunctionType(); 31610b57cec5SDimitry Andric 31620b57cec5SDimitry Andric // Verify that the correct number of arguments are being passed 31630b57cec5SDimitry Andric if (FTy->isVarArg()) 3164*81ad6265SDimitry Andric Check(Call.arg_size() >= FTy->getNumParams(), 3165*81ad6265SDimitry Andric "Called function requires more parameters than were provided!", Call); 31660b57cec5SDimitry Andric else 3167*81ad6265SDimitry Andric Check(Call.arg_size() == FTy->getNumParams(), 31680b57cec5SDimitry Andric "Incorrect number of arguments passed to called function!", Call); 31690b57cec5SDimitry Andric 31700b57cec5SDimitry Andric // Verify that all arguments to the call match the function type. 31710b57cec5SDimitry Andric for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 3172*81ad6265SDimitry Andric Check(Call.getArgOperand(i)->getType() == FTy->getParamType(i), 31730b57cec5SDimitry Andric "Call parameter type does not match function signature!", 31740b57cec5SDimitry Andric Call.getArgOperand(i), FTy->getParamType(i), Call); 31750b57cec5SDimitry Andric 31760b57cec5SDimitry Andric AttributeList Attrs = Call.getAttributes(); 31770b57cec5SDimitry Andric 3178*81ad6265SDimitry Andric Check(verifyAttributeCount(Attrs, Call.arg_size()), 31790b57cec5SDimitry Andric "Attribute after last parameter!", Call); 31800b57cec5SDimitry Andric 3181*81ad6265SDimitry Andric auto VerifyTypeAlign = [&](Type *Ty, const Twine &Message) { 3182*81ad6265SDimitry Andric if (!Ty->isSized()) 3183*81ad6265SDimitry Andric return; 3184*81ad6265SDimitry Andric Align ABIAlign = DL.getABITypeAlign(Ty); 3185*81ad6265SDimitry Andric Align MaxAlign(ParamMaxAlignment); 3186*81ad6265SDimitry Andric Check(ABIAlign <= MaxAlign, 3187*81ad6265SDimitry Andric "Incorrect alignment of " + Message + " to called function!", Call); 3188*81ad6265SDimitry Andric }; 3189*81ad6265SDimitry Andric 3190*81ad6265SDimitry Andric VerifyTypeAlign(FTy->getReturnType(), "return type"); 3191*81ad6265SDimitry Andric for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) { 3192*81ad6265SDimitry Andric Type *Ty = FTy->getParamType(i); 3193*81ad6265SDimitry Andric VerifyTypeAlign(Ty, "argument passed"); 3194*81ad6265SDimitry Andric } 3195*81ad6265SDimitry Andric 31965ffd83dbSDimitry Andric Function *Callee = 31975ffd83dbSDimitry Andric dyn_cast<Function>(Call.getCalledOperand()->stripPointerCasts()); 3198fe6060f1SDimitry Andric bool IsIntrinsic = Callee && Callee->isIntrinsic(); 3199fe6060f1SDimitry Andric if (IsIntrinsic) 3200*81ad6265SDimitry Andric Check(Callee->getValueType() == FTy, 3201fe6060f1SDimitry Andric "Intrinsic called with incompatible signature", Call); 32020b57cec5SDimitry Andric 3203349cc55cSDimitry Andric if (Attrs.hasFnAttr(Attribute::Speculatable)) { 32040b57cec5SDimitry Andric // Don't allow speculatable on call sites, unless the underlying function 32050b57cec5SDimitry Andric // declaration is also speculatable. 3206*81ad6265SDimitry Andric Check(Callee && Callee->isSpeculatable(), 32070b57cec5SDimitry Andric "speculatable attribute may not apply to call sites", Call); 32080b57cec5SDimitry Andric } 32090b57cec5SDimitry Andric 3210349cc55cSDimitry Andric if (Attrs.hasFnAttr(Attribute::Preallocated)) { 3211*81ad6265SDimitry Andric Check(Call.getCalledFunction()->getIntrinsicID() == 32125ffd83dbSDimitry Andric Intrinsic::call_preallocated_arg, 32135ffd83dbSDimitry Andric "preallocated as a call site attribute can only be on " 32145ffd83dbSDimitry Andric "llvm.call.preallocated.arg"); 32155ffd83dbSDimitry Andric } 32165ffd83dbSDimitry Andric 32170b57cec5SDimitry Andric // Verify call attributes. 321804eeddc0SDimitry Andric verifyFunctionAttrs(FTy, Attrs, &Call, IsIntrinsic, Call.isInlineAsm()); 32190b57cec5SDimitry Andric 32200b57cec5SDimitry Andric // Conservatively check the inalloca argument. 32210b57cec5SDimitry Andric // We have a bug if we can find that there is an underlying alloca without 32220b57cec5SDimitry Andric // inalloca. 32230b57cec5SDimitry Andric if (Call.hasInAllocaArgument()) { 32240b57cec5SDimitry Andric Value *InAllocaArg = Call.getArgOperand(FTy->getNumParams() - 1); 32250b57cec5SDimitry Andric if (auto AI = dyn_cast<AllocaInst>(InAllocaArg->stripInBoundsOffsets())) 3226*81ad6265SDimitry Andric Check(AI->isUsedWithInAlloca(), 32270b57cec5SDimitry Andric "inalloca argument for call has mismatched alloca", AI, Call); 32280b57cec5SDimitry Andric } 32290b57cec5SDimitry Andric 32300b57cec5SDimitry Andric // For each argument of the callsite, if it has the swifterror argument, 32310b57cec5SDimitry Andric // make sure the underlying alloca/parameter it comes from has a swifterror as 32320b57cec5SDimitry Andric // well. 32330b57cec5SDimitry Andric for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) { 32340b57cec5SDimitry Andric if (Call.paramHasAttr(i, Attribute::SwiftError)) { 32350b57cec5SDimitry Andric Value *SwiftErrorArg = Call.getArgOperand(i); 32360b57cec5SDimitry Andric if (auto AI = dyn_cast<AllocaInst>(SwiftErrorArg->stripInBoundsOffsets())) { 3237*81ad6265SDimitry Andric Check(AI->isSwiftError(), 32380b57cec5SDimitry Andric "swifterror argument for call has mismatched alloca", AI, Call); 32390b57cec5SDimitry Andric continue; 32400b57cec5SDimitry Andric } 32410b57cec5SDimitry Andric auto ArgI = dyn_cast<Argument>(SwiftErrorArg); 3242*81ad6265SDimitry Andric Check(ArgI, "swifterror argument should come from an alloca or parameter", 32430b57cec5SDimitry Andric SwiftErrorArg, Call); 3244*81ad6265SDimitry Andric Check(ArgI->hasSwiftErrorAttr(), 32450b57cec5SDimitry Andric "swifterror argument for call has mismatched parameter", ArgI, 32460b57cec5SDimitry Andric Call); 32470b57cec5SDimitry Andric } 32480b57cec5SDimitry Andric 3249349cc55cSDimitry Andric if (Attrs.hasParamAttr(i, Attribute::ImmArg)) { 32500b57cec5SDimitry Andric // Don't allow immarg on call sites, unless the underlying declaration 32510b57cec5SDimitry Andric // also has the matching immarg. 3252*81ad6265SDimitry Andric Check(Callee && Callee->hasParamAttribute(i, Attribute::ImmArg), 3253*81ad6265SDimitry Andric "immarg may not apply only to call sites", Call.getArgOperand(i), 3254*81ad6265SDimitry Andric Call); 32550b57cec5SDimitry Andric } 32560b57cec5SDimitry Andric 32570b57cec5SDimitry Andric if (Call.paramHasAttr(i, Attribute::ImmArg)) { 32580b57cec5SDimitry Andric Value *ArgVal = Call.getArgOperand(i); 3259*81ad6265SDimitry Andric Check(isa<ConstantInt>(ArgVal) || isa<ConstantFP>(ArgVal), 32600b57cec5SDimitry Andric "immarg operand has non-immediate parameter", ArgVal, Call); 32610b57cec5SDimitry Andric } 32625ffd83dbSDimitry Andric 32635ffd83dbSDimitry Andric if (Call.paramHasAttr(i, Attribute::Preallocated)) { 32645ffd83dbSDimitry Andric Value *ArgVal = Call.getArgOperand(i); 32655ffd83dbSDimitry Andric bool hasOB = 32665ffd83dbSDimitry Andric Call.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0; 32675ffd83dbSDimitry Andric bool isMustTail = Call.isMustTailCall(); 3268*81ad6265SDimitry Andric Check(hasOB != isMustTail, 32695ffd83dbSDimitry Andric "preallocated operand either requires a preallocated bundle or " 32705ffd83dbSDimitry Andric "the call to be musttail (but not both)", 32715ffd83dbSDimitry Andric ArgVal, Call); 32725ffd83dbSDimitry Andric } 32730b57cec5SDimitry Andric } 32740b57cec5SDimitry Andric 32750b57cec5SDimitry Andric if (FTy->isVarArg()) { 32760b57cec5SDimitry Andric // FIXME? is 'nest' even legal here? 32770b57cec5SDimitry Andric bool SawNest = false; 32780b57cec5SDimitry Andric bool SawReturned = false; 32790b57cec5SDimitry Andric 32800b57cec5SDimitry Andric for (unsigned Idx = 0; Idx < FTy->getNumParams(); ++Idx) { 3281349cc55cSDimitry Andric if (Attrs.hasParamAttr(Idx, Attribute::Nest)) 32820b57cec5SDimitry Andric SawNest = true; 3283349cc55cSDimitry Andric if (Attrs.hasParamAttr(Idx, Attribute::Returned)) 32840b57cec5SDimitry Andric SawReturned = true; 32850b57cec5SDimitry Andric } 32860b57cec5SDimitry Andric 32870b57cec5SDimitry Andric // Check attributes on the varargs part. 32880b57cec5SDimitry Andric for (unsigned Idx = FTy->getNumParams(); Idx < Call.arg_size(); ++Idx) { 32890b57cec5SDimitry Andric Type *Ty = Call.getArgOperand(Idx)->getType(); 3290349cc55cSDimitry Andric AttributeSet ArgAttrs = Attrs.getParamAttrs(Idx); 32910b57cec5SDimitry Andric verifyParameterAttrs(ArgAttrs, Ty, &Call); 32920b57cec5SDimitry Andric 32930b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::Nest)) { 3294*81ad6265SDimitry Andric Check(!SawNest, "More than one parameter has attribute nest!", Call); 32950b57cec5SDimitry Andric SawNest = true; 32960b57cec5SDimitry Andric } 32970b57cec5SDimitry Andric 32980b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::Returned)) { 3299*81ad6265SDimitry Andric Check(!SawReturned, "More than one parameter has attribute returned!", 33000b57cec5SDimitry Andric Call); 3301*81ad6265SDimitry Andric Check(Ty->canLosslesslyBitCastTo(FTy->getReturnType()), 33020b57cec5SDimitry Andric "Incompatible argument and return types for 'returned' " 33030b57cec5SDimitry Andric "attribute", 33040b57cec5SDimitry Andric Call); 33050b57cec5SDimitry Andric SawReturned = true; 33060b57cec5SDimitry Andric } 33070b57cec5SDimitry Andric 33080b57cec5SDimitry Andric // Statepoint intrinsic is vararg but the wrapped function may be not. 33090b57cec5SDimitry Andric // Allow sret here and check the wrapped function in verifyStatepoint. 33100b57cec5SDimitry Andric if (!Call.getCalledFunction() || 33110b57cec5SDimitry Andric Call.getCalledFunction()->getIntrinsicID() != 33120b57cec5SDimitry Andric Intrinsic::experimental_gc_statepoint) 3313*81ad6265SDimitry Andric Check(!ArgAttrs.hasAttribute(Attribute::StructRet), 33140b57cec5SDimitry Andric "Attribute 'sret' cannot be used for vararg call arguments!", 33150b57cec5SDimitry Andric Call); 33160b57cec5SDimitry Andric 33170b57cec5SDimitry Andric if (ArgAttrs.hasAttribute(Attribute::InAlloca)) 3318*81ad6265SDimitry Andric Check(Idx == Call.arg_size() - 1, 33190b57cec5SDimitry Andric "inalloca isn't on the last argument!", Call); 33200b57cec5SDimitry Andric } 33210b57cec5SDimitry Andric } 33220b57cec5SDimitry Andric 33230b57cec5SDimitry Andric // Verify that there's no metadata unless it's a direct call to an intrinsic. 33240b57cec5SDimitry Andric if (!IsIntrinsic) { 33250b57cec5SDimitry Andric for (Type *ParamTy : FTy->params()) { 3326*81ad6265SDimitry Andric Check(!ParamTy->isMetadataTy(), 33270b57cec5SDimitry Andric "Function has metadata parameter but isn't an intrinsic", Call); 3328*81ad6265SDimitry Andric Check(!ParamTy->isTokenTy(), 33290b57cec5SDimitry Andric "Function has token parameter but isn't an intrinsic", Call); 33300b57cec5SDimitry Andric } 33310b57cec5SDimitry Andric } 33320b57cec5SDimitry Andric 33330b57cec5SDimitry Andric // Verify that indirect calls don't return tokens. 3334fe6060f1SDimitry Andric if (!Call.getCalledFunction()) { 3335*81ad6265SDimitry Andric Check(!FTy->getReturnType()->isTokenTy(), 33360b57cec5SDimitry Andric "Return type cannot be token for indirect call!"); 3337*81ad6265SDimitry Andric Check(!FTy->getReturnType()->isX86_AMXTy(), 3338fe6060f1SDimitry Andric "Return type cannot be x86_amx for indirect call!"); 3339fe6060f1SDimitry Andric } 33400b57cec5SDimitry Andric 33410b57cec5SDimitry Andric if (Function *F = Call.getCalledFunction()) 33420b57cec5SDimitry Andric if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) 33430b57cec5SDimitry Andric visitIntrinsicCall(ID, Call); 33440b57cec5SDimitry Andric 3345480093f4SDimitry Andric // Verify that a callsite has at most one "deopt", at most one "funclet", at 3346*81ad6265SDimitry Andric // most one "gc-transition", at most one "cfguardtarget", at most one 3347*81ad6265SDimitry Andric // "preallocated" operand bundle, and at most one "ptrauth" operand bundle. 33480b57cec5SDimitry Andric bool FoundDeoptBundle = false, FoundFuncletBundle = false, 33495ffd83dbSDimitry Andric FoundGCTransitionBundle = false, FoundCFGuardTargetBundle = false, 3350fe6060f1SDimitry Andric FoundPreallocatedBundle = false, FoundGCLiveBundle = false, 3351*81ad6265SDimitry Andric FoundPtrauthBundle = false, 3352fe6060f1SDimitry Andric FoundAttachedCallBundle = false; 33530b57cec5SDimitry Andric for (unsigned i = 0, e = Call.getNumOperandBundles(); i < e; ++i) { 33540b57cec5SDimitry Andric OperandBundleUse BU = Call.getOperandBundleAt(i); 33550b57cec5SDimitry Andric uint32_t Tag = BU.getTagID(); 33560b57cec5SDimitry Andric if (Tag == LLVMContext::OB_deopt) { 3357*81ad6265SDimitry Andric Check(!FoundDeoptBundle, "Multiple deopt operand bundles", Call); 33580b57cec5SDimitry Andric FoundDeoptBundle = true; 33590b57cec5SDimitry Andric } else if (Tag == LLVMContext::OB_gc_transition) { 3360*81ad6265SDimitry Andric Check(!FoundGCTransitionBundle, "Multiple gc-transition operand bundles", 33610b57cec5SDimitry Andric Call); 33620b57cec5SDimitry Andric FoundGCTransitionBundle = true; 33630b57cec5SDimitry Andric } else if (Tag == LLVMContext::OB_funclet) { 3364*81ad6265SDimitry Andric Check(!FoundFuncletBundle, "Multiple funclet operand bundles", Call); 33650b57cec5SDimitry Andric FoundFuncletBundle = true; 3366*81ad6265SDimitry Andric Check(BU.Inputs.size() == 1, 33670b57cec5SDimitry Andric "Expected exactly one funclet bundle operand", Call); 3368*81ad6265SDimitry Andric Check(isa<FuncletPadInst>(BU.Inputs.front()), 33690b57cec5SDimitry Andric "Funclet bundle operands should correspond to a FuncletPadInst", 33700b57cec5SDimitry Andric Call); 3371480093f4SDimitry Andric } else if (Tag == LLVMContext::OB_cfguardtarget) { 3372*81ad6265SDimitry Andric Check(!FoundCFGuardTargetBundle, "Multiple CFGuardTarget operand bundles", 3373*81ad6265SDimitry Andric Call); 3374480093f4SDimitry Andric FoundCFGuardTargetBundle = true; 3375*81ad6265SDimitry Andric Check(BU.Inputs.size() == 1, 3376480093f4SDimitry Andric "Expected exactly one cfguardtarget bundle operand", Call); 3377*81ad6265SDimitry Andric } else if (Tag == LLVMContext::OB_ptrauth) { 3378*81ad6265SDimitry Andric Check(!FoundPtrauthBundle, "Multiple ptrauth operand bundles", Call); 3379*81ad6265SDimitry Andric FoundPtrauthBundle = true; 3380*81ad6265SDimitry Andric Check(BU.Inputs.size() == 2, 3381*81ad6265SDimitry Andric "Expected exactly two ptrauth bundle operands", Call); 3382*81ad6265SDimitry Andric Check(isa<ConstantInt>(BU.Inputs[0]) && 3383*81ad6265SDimitry Andric BU.Inputs[0]->getType()->isIntegerTy(32), 3384*81ad6265SDimitry Andric "Ptrauth bundle key operand must be an i32 constant", Call); 3385*81ad6265SDimitry Andric Check(BU.Inputs[1]->getType()->isIntegerTy(64), 3386*81ad6265SDimitry Andric "Ptrauth bundle discriminator operand must be an i64", Call); 33875ffd83dbSDimitry Andric } else if (Tag == LLVMContext::OB_preallocated) { 3388*81ad6265SDimitry Andric Check(!FoundPreallocatedBundle, "Multiple preallocated operand bundles", 33895ffd83dbSDimitry Andric Call); 33905ffd83dbSDimitry Andric FoundPreallocatedBundle = true; 3391*81ad6265SDimitry Andric Check(BU.Inputs.size() == 1, 33925ffd83dbSDimitry Andric "Expected exactly one preallocated bundle operand", Call); 33935ffd83dbSDimitry Andric auto Input = dyn_cast<IntrinsicInst>(BU.Inputs.front()); 3394*81ad6265SDimitry Andric Check(Input && 33955ffd83dbSDimitry Andric Input->getIntrinsicID() == Intrinsic::call_preallocated_setup, 33965ffd83dbSDimitry Andric "\"preallocated\" argument must be a token from " 33975ffd83dbSDimitry Andric "llvm.call.preallocated.setup", 33985ffd83dbSDimitry Andric Call); 33995ffd83dbSDimitry Andric } else if (Tag == LLVMContext::OB_gc_live) { 3400*81ad6265SDimitry Andric Check(!FoundGCLiveBundle, "Multiple gc-live operand bundles", Call); 34015ffd83dbSDimitry Andric FoundGCLiveBundle = true; 3402fe6060f1SDimitry Andric } else if (Tag == LLVMContext::OB_clang_arc_attachedcall) { 3403*81ad6265SDimitry Andric Check(!FoundAttachedCallBundle, 3404fe6060f1SDimitry Andric "Multiple \"clang.arc.attachedcall\" operand bundles", Call); 3405fe6060f1SDimitry Andric FoundAttachedCallBundle = true; 3406349cc55cSDimitry Andric verifyAttachedCallBundle(Call, BU); 34070b57cec5SDimitry Andric } 34080b57cec5SDimitry Andric } 34090b57cec5SDimitry Andric 3410*81ad6265SDimitry Andric // Verify that callee and callsite agree on whether to use pointer auth. 3411*81ad6265SDimitry Andric Check(!(Call.getCalledFunction() && FoundPtrauthBundle), 3412*81ad6265SDimitry Andric "Direct call cannot have a ptrauth bundle", Call); 3413*81ad6265SDimitry Andric 34140b57cec5SDimitry Andric // Verify that each inlinable callsite of a debug-info-bearing function in a 34150b57cec5SDimitry Andric // debug-info-bearing function has a debug location attached to it. Failure to 34160b57cec5SDimitry Andric // do so causes assertion failures when the inliner sets up inline scope info. 34170b57cec5SDimitry Andric if (Call.getFunction()->getSubprogram() && Call.getCalledFunction() && 34180b57cec5SDimitry Andric Call.getCalledFunction()->getSubprogram()) 3419*81ad6265SDimitry Andric CheckDI(Call.getDebugLoc(), 34200b57cec5SDimitry Andric "inlinable function call in a function with " 34210b57cec5SDimitry Andric "debug info must have a !dbg location", 34220b57cec5SDimitry Andric Call); 34230b57cec5SDimitry Andric 342404eeddc0SDimitry Andric if (Call.isInlineAsm()) 342504eeddc0SDimitry Andric verifyInlineAsmCall(Call); 342604eeddc0SDimitry Andric 34270b57cec5SDimitry Andric visitInstruction(Call); 34280b57cec5SDimitry Andric } 34290b57cec5SDimitry Andric 34300eae32dcSDimitry Andric void Verifier::verifyTailCCMustTailAttrs(const AttrBuilder &Attrs, 3431fe6060f1SDimitry Andric StringRef Context) { 3432*81ad6265SDimitry Andric Check(!Attrs.contains(Attribute::InAlloca), 3433fe6060f1SDimitry Andric Twine("inalloca attribute not allowed in ") + Context); 3434*81ad6265SDimitry Andric Check(!Attrs.contains(Attribute::InReg), 3435fe6060f1SDimitry Andric Twine("inreg attribute not allowed in ") + Context); 3436*81ad6265SDimitry Andric Check(!Attrs.contains(Attribute::SwiftError), 3437fe6060f1SDimitry Andric Twine("swifterror attribute not allowed in ") + Context); 3438*81ad6265SDimitry Andric Check(!Attrs.contains(Attribute::Preallocated), 3439fe6060f1SDimitry Andric Twine("preallocated attribute not allowed in ") + Context); 3440*81ad6265SDimitry Andric Check(!Attrs.contains(Attribute::ByRef), 3441fe6060f1SDimitry Andric Twine("byref attribute not allowed in ") + Context); 3442fe6060f1SDimitry Andric } 3443fe6060f1SDimitry Andric 34440b57cec5SDimitry Andric /// Two types are "congruent" if they are identical, or if they are both pointer 34450b57cec5SDimitry Andric /// types with different pointee types and the same address space. 34460b57cec5SDimitry Andric static bool isTypeCongruent(Type *L, Type *R) { 34470b57cec5SDimitry Andric if (L == R) 34480b57cec5SDimitry Andric return true; 34490b57cec5SDimitry Andric PointerType *PL = dyn_cast<PointerType>(L); 34500b57cec5SDimitry Andric PointerType *PR = dyn_cast<PointerType>(R); 34510b57cec5SDimitry Andric if (!PL || !PR) 34520b57cec5SDimitry Andric return false; 34530b57cec5SDimitry Andric return PL->getAddressSpace() == PR->getAddressSpace(); 34540b57cec5SDimitry Andric } 34550b57cec5SDimitry Andric 345604eeddc0SDimitry Andric static AttrBuilder getParameterABIAttributes(LLVMContext& C, unsigned I, AttributeList Attrs) { 34570b57cec5SDimitry Andric static const Attribute::AttrKind ABIAttrs[] = { 34580b57cec5SDimitry Andric Attribute::StructRet, Attribute::ByVal, Attribute::InAlloca, 3459fe6060f1SDimitry Andric Attribute::InReg, Attribute::StackAlignment, Attribute::SwiftSelf, 3460fe6060f1SDimitry Andric Attribute::SwiftAsync, Attribute::SwiftError, Attribute::Preallocated, 3461fe6060f1SDimitry Andric Attribute::ByRef}; 346204eeddc0SDimitry Andric AttrBuilder Copy(C); 34630b57cec5SDimitry Andric for (auto AK : ABIAttrs) { 3464349cc55cSDimitry Andric Attribute Attr = Attrs.getParamAttrs(I).getAttribute(AK); 3465fe6060f1SDimitry Andric if (Attr.isValid()) 3466fe6060f1SDimitry Andric Copy.addAttribute(Attr); 34670b57cec5SDimitry Andric } 3468e8d8bef9SDimitry Andric 3469e8d8bef9SDimitry Andric // `align` is ABI-affecting only in combination with `byval` or `byref`. 3470349cc55cSDimitry Andric if (Attrs.hasParamAttr(I, Attribute::Alignment) && 3471349cc55cSDimitry Andric (Attrs.hasParamAttr(I, Attribute::ByVal) || 3472349cc55cSDimitry Andric Attrs.hasParamAttr(I, Attribute::ByRef))) 34730b57cec5SDimitry Andric Copy.addAlignmentAttr(Attrs.getParamAlignment(I)); 34740b57cec5SDimitry Andric return Copy; 34750b57cec5SDimitry Andric } 34760b57cec5SDimitry Andric 34770b57cec5SDimitry Andric void Verifier::verifyMustTailCall(CallInst &CI) { 3478*81ad6265SDimitry Andric Check(!CI.isInlineAsm(), "cannot use musttail call with inline asm", &CI); 34790b57cec5SDimitry Andric 34800b57cec5SDimitry Andric Function *F = CI.getParent()->getParent(); 34810b57cec5SDimitry Andric FunctionType *CallerTy = F->getFunctionType(); 34820b57cec5SDimitry Andric FunctionType *CalleeTy = CI.getFunctionType(); 3483*81ad6265SDimitry Andric Check(CallerTy->isVarArg() == CalleeTy->isVarArg(), 34840b57cec5SDimitry Andric "cannot guarantee tail call due to mismatched varargs", &CI); 3485*81ad6265SDimitry Andric Check(isTypeCongruent(CallerTy->getReturnType(), CalleeTy->getReturnType()), 34860b57cec5SDimitry Andric "cannot guarantee tail call due to mismatched return types", &CI); 34870b57cec5SDimitry Andric 34880b57cec5SDimitry Andric // - The calling conventions of the caller and callee must match. 3489*81ad6265SDimitry Andric Check(F->getCallingConv() == CI.getCallingConv(), 34900b57cec5SDimitry Andric "cannot guarantee tail call due to mismatched calling conv", &CI); 34910b57cec5SDimitry Andric 34920b57cec5SDimitry Andric // - The call must immediately precede a :ref:`ret <i_ret>` instruction, 34930b57cec5SDimitry Andric // or a pointer bitcast followed by a ret instruction. 34940b57cec5SDimitry Andric // - The ret instruction must return the (possibly bitcasted) value 34950b57cec5SDimitry Andric // produced by the call or void. 34960b57cec5SDimitry Andric Value *RetVal = &CI; 34970b57cec5SDimitry Andric Instruction *Next = CI.getNextNode(); 34980b57cec5SDimitry Andric 34990b57cec5SDimitry Andric // Handle the optional bitcast. 35000b57cec5SDimitry Andric if (BitCastInst *BI = dyn_cast_or_null<BitCastInst>(Next)) { 3501*81ad6265SDimitry Andric Check(BI->getOperand(0) == RetVal, 35020b57cec5SDimitry Andric "bitcast following musttail call must use the call", BI); 35030b57cec5SDimitry Andric RetVal = BI; 35040b57cec5SDimitry Andric Next = BI->getNextNode(); 35050b57cec5SDimitry Andric } 35060b57cec5SDimitry Andric 35070b57cec5SDimitry Andric // Check the return. 35080b57cec5SDimitry Andric ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next); 3509*81ad6265SDimitry Andric Check(Ret, "musttail call must precede a ret with an optional bitcast", &CI); 3510*81ad6265SDimitry Andric Check(!Ret->getReturnValue() || Ret->getReturnValue() == RetVal || 3511fe6060f1SDimitry Andric isa<UndefValue>(Ret->getReturnValue()), 35120b57cec5SDimitry Andric "musttail call result must be returned", Ret); 3513fe6060f1SDimitry Andric 3514fe6060f1SDimitry Andric AttributeList CallerAttrs = F->getAttributes(); 3515fe6060f1SDimitry Andric AttributeList CalleeAttrs = CI.getAttributes(); 3516fe6060f1SDimitry Andric if (CI.getCallingConv() == CallingConv::SwiftTail || 3517fe6060f1SDimitry Andric CI.getCallingConv() == CallingConv::Tail) { 3518fe6060f1SDimitry Andric StringRef CCName = 3519fe6060f1SDimitry Andric CI.getCallingConv() == CallingConv::Tail ? "tailcc" : "swifttailcc"; 3520fe6060f1SDimitry Andric 3521fe6060f1SDimitry Andric // - Only sret, byval, swiftself, and swiftasync ABI-impacting attributes 3522fe6060f1SDimitry Andric // are allowed in swifttailcc call 3523349cc55cSDimitry Andric for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) { 352404eeddc0SDimitry Andric AttrBuilder ABIAttrs = getParameterABIAttributes(F->getContext(), I, CallerAttrs); 3525fe6060f1SDimitry Andric SmallString<32> Context{CCName, StringRef(" musttail caller")}; 3526fe6060f1SDimitry Andric verifyTailCCMustTailAttrs(ABIAttrs, Context); 3527fe6060f1SDimitry Andric } 3528349cc55cSDimitry Andric for (unsigned I = 0, E = CalleeTy->getNumParams(); I != E; ++I) { 352904eeddc0SDimitry Andric AttrBuilder ABIAttrs = getParameterABIAttributes(F->getContext(), I, CalleeAttrs); 3530fe6060f1SDimitry Andric SmallString<32> Context{CCName, StringRef(" musttail callee")}; 3531fe6060f1SDimitry Andric verifyTailCCMustTailAttrs(ABIAttrs, Context); 3532fe6060f1SDimitry Andric } 3533fe6060f1SDimitry Andric // - Varargs functions are not allowed 3534*81ad6265SDimitry Andric Check(!CallerTy->isVarArg(), Twine("cannot guarantee ") + CCName + 3535fe6060f1SDimitry Andric " tail call for varargs function"); 3536fe6060f1SDimitry Andric return; 3537fe6060f1SDimitry Andric } 3538fe6060f1SDimitry Andric 3539fe6060f1SDimitry Andric // - The caller and callee prototypes must match. Pointer types of 3540fe6060f1SDimitry Andric // parameters or return types may differ in pointee type, but not 3541fe6060f1SDimitry Andric // address space. 3542fe6060f1SDimitry Andric if (!CI.getCalledFunction() || !CI.getCalledFunction()->isIntrinsic()) { 3543*81ad6265SDimitry Andric Check(CallerTy->getNumParams() == CalleeTy->getNumParams(), 3544*81ad6265SDimitry Andric "cannot guarantee tail call due to mismatched parameter counts", &CI); 3545349cc55cSDimitry Andric for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) { 3546*81ad6265SDimitry Andric Check( 3547fe6060f1SDimitry Andric isTypeCongruent(CallerTy->getParamType(I), CalleeTy->getParamType(I)), 3548fe6060f1SDimitry Andric "cannot guarantee tail call due to mismatched parameter types", &CI); 3549fe6060f1SDimitry Andric } 3550fe6060f1SDimitry Andric } 3551fe6060f1SDimitry Andric 3552fe6060f1SDimitry Andric // - All ABI-impacting function attributes, such as sret, byval, inreg, 3553fe6060f1SDimitry Andric // returned, preallocated, and inalloca, must match. 3554349cc55cSDimitry Andric for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) { 355504eeddc0SDimitry Andric AttrBuilder CallerABIAttrs = getParameterABIAttributes(F->getContext(), I, CallerAttrs); 355604eeddc0SDimitry Andric AttrBuilder CalleeABIAttrs = getParameterABIAttributes(F->getContext(), I, CalleeAttrs); 3557*81ad6265SDimitry Andric Check(CallerABIAttrs == CalleeABIAttrs, 3558fe6060f1SDimitry Andric "cannot guarantee tail call due to mismatched ABI impacting " 3559fe6060f1SDimitry Andric "function attributes", 3560fe6060f1SDimitry Andric &CI, CI.getOperand(I)); 3561fe6060f1SDimitry Andric } 35620b57cec5SDimitry Andric } 35630b57cec5SDimitry Andric 35640b57cec5SDimitry Andric void Verifier::visitCallInst(CallInst &CI) { 35650b57cec5SDimitry Andric visitCallBase(CI); 35660b57cec5SDimitry Andric 35670b57cec5SDimitry Andric if (CI.isMustTailCall()) 35680b57cec5SDimitry Andric verifyMustTailCall(CI); 35690b57cec5SDimitry Andric } 35700b57cec5SDimitry Andric 35710b57cec5SDimitry Andric void Verifier::visitInvokeInst(InvokeInst &II) { 35720b57cec5SDimitry Andric visitCallBase(II); 35730b57cec5SDimitry Andric 35740b57cec5SDimitry Andric // Verify that the first non-PHI instruction of the unwind destination is an 35750b57cec5SDimitry Andric // exception handling instruction. 3576*81ad6265SDimitry Andric Check( 35770b57cec5SDimitry Andric II.getUnwindDest()->isEHPad(), 35780b57cec5SDimitry Andric "The unwind destination does not have an exception handling instruction!", 35790b57cec5SDimitry Andric &II); 35800b57cec5SDimitry Andric 35810b57cec5SDimitry Andric visitTerminator(II); 35820b57cec5SDimitry Andric } 35830b57cec5SDimitry Andric 35840b57cec5SDimitry Andric /// visitUnaryOperator - Check the argument to the unary operator. 35850b57cec5SDimitry Andric /// 35860b57cec5SDimitry Andric void Verifier::visitUnaryOperator(UnaryOperator &U) { 3587*81ad6265SDimitry Andric Check(U.getType() == U.getOperand(0)->getType(), 35880b57cec5SDimitry Andric "Unary operators must have same type for" 35890b57cec5SDimitry Andric "operands and result!", 35900b57cec5SDimitry Andric &U); 35910b57cec5SDimitry Andric 35920b57cec5SDimitry Andric switch (U.getOpcode()) { 35930b57cec5SDimitry Andric // Check that floating-point arithmetic operators are only used with 35940b57cec5SDimitry Andric // floating-point operands. 35950b57cec5SDimitry Andric case Instruction::FNeg: 3596*81ad6265SDimitry Andric Check(U.getType()->isFPOrFPVectorTy(), 35970b57cec5SDimitry Andric "FNeg operator only works with float types!", &U); 35980b57cec5SDimitry Andric break; 35990b57cec5SDimitry Andric default: 36000b57cec5SDimitry Andric llvm_unreachable("Unknown UnaryOperator opcode!"); 36010b57cec5SDimitry Andric } 36020b57cec5SDimitry Andric 36030b57cec5SDimitry Andric visitInstruction(U); 36040b57cec5SDimitry Andric } 36050b57cec5SDimitry Andric 36060b57cec5SDimitry Andric /// visitBinaryOperator - Check that both arguments to the binary operator are 36070b57cec5SDimitry Andric /// of the same type! 36080b57cec5SDimitry Andric /// 36090b57cec5SDimitry Andric void Verifier::visitBinaryOperator(BinaryOperator &B) { 3610*81ad6265SDimitry Andric Check(B.getOperand(0)->getType() == B.getOperand(1)->getType(), 36110b57cec5SDimitry Andric "Both operands to a binary operator are not of the same type!", &B); 36120b57cec5SDimitry Andric 36130b57cec5SDimitry Andric switch (B.getOpcode()) { 36140b57cec5SDimitry Andric // Check that integer arithmetic operators are only used with 36150b57cec5SDimitry Andric // integral operands. 36160b57cec5SDimitry Andric case Instruction::Add: 36170b57cec5SDimitry Andric case Instruction::Sub: 36180b57cec5SDimitry Andric case Instruction::Mul: 36190b57cec5SDimitry Andric case Instruction::SDiv: 36200b57cec5SDimitry Andric case Instruction::UDiv: 36210b57cec5SDimitry Andric case Instruction::SRem: 36220b57cec5SDimitry Andric case Instruction::URem: 3623*81ad6265SDimitry Andric Check(B.getType()->isIntOrIntVectorTy(), 36240b57cec5SDimitry Andric "Integer arithmetic operators only work with integral types!", &B); 3625*81ad6265SDimitry Andric Check(B.getType() == B.getOperand(0)->getType(), 36260b57cec5SDimitry Andric "Integer arithmetic operators must have same type " 36270b57cec5SDimitry Andric "for operands and result!", 36280b57cec5SDimitry Andric &B); 36290b57cec5SDimitry Andric break; 36300b57cec5SDimitry Andric // Check that floating-point arithmetic operators are only used with 36310b57cec5SDimitry Andric // floating-point operands. 36320b57cec5SDimitry Andric case Instruction::FAdd: 36330b57cec5SDimitry Andric case Instruction::FSub: 36340b57cec5SDimitry Andric case Instruction::FMul: 36350b57cec5SDimitry Andric case Instruction::FDiv: 36360b57cec5SDimitry Andric case Instruction::FRem: 3637*81ad6265SDimitry Andric Check(B.getType()->isFPOrFPVectorTy(), 36380b57cec5SDimitry Andric "Floating-point arithmetic operators only work with " 36390b57cec5SDimitry Andric "floating-point types!", 36400b57cec5SDimitry Andric &B); 3641*81ad6265SDimitry Andric Check(B.getType() == B.getOperand(0)->getType(), 36420b57cec5SDimitry Andric "Floating-point arithmetic operators must have same type " 36430b57cec5SDimitry Andric "for operands and result!", 36440b57cec5SDimitry Andric &B); 36450b57cec5SDimitry Andric break; 36460b57cec5SDimitry Andric // Check that logical operators are only used with integral operands. 36470b57cec5SDimitry Andric case Instruction::And: 36480b57cec5SDimitry Andric case Instruction::Or: 36490b57cec5SDimitry Andric case Instruction::Xor: 3650*81ad6265SDimitry Andric Check(B.getType()->isIntOrIntVectorTy(), 36510b57cec5SDimitry Andric "Logical operators only work with integral types!", &B); 3652*81ad6265SDimitry Andric Check(B.getType() == B.getOperand(0)->getType(), 3653*81ad6265SDimitry Andric "Logical operators must have same type for operands and result!", &B); 36540b57cec5SDimitry Andric break; 36550b57cec5SDimitry Andric case Instruction::Shl: 36560b57cec5SDimitry Andric case Instruction::LShr: 36570b57cec5SDimitry Andric case Instruction::AShr: 3658*81ad6265SDimitry Andric Check(B.getType()->isIntOrIntVectorTy(), 36590b57cec5SDimitry Andric "Shifts only work with integral types!", &B); 3660*81ad6265SDimitry Andric Check(B.getType() == B.getOperand(0)->getType(), 36610b57cec5SDimitry Andric "Shift return type must be same as operands!", &B); 36620b57cec5SDimitry Andric break; 36630b57cec5SDimitry Andric default: 36640b57cec5SDimitry Andric llvm_unreachable("Unknown BinaryOperator opcode!"); 36650b57cec5SDimitry Andric } 36660b57cec5SDimitry Andric 36670b57cec5SDimitry Andric visitInstruction(B); 36680b57cec5SDimitry Andric } 36690b57cec5SDimitry Andric 36700b57cec5SDimitry Andric void Verifier::visitICmpInst(ICmpInst &IC) { 36710b57cec5SDimitry Andric // Check that the operands are the same type 36720b57cec5SDimitry Andric Type *Op0Ty = IC.getOperand(0)->getType(); 36730b57cec5SDimitry Andric Type *Op1Ty = IC.getOperand(1)->getType(); 3674*81ad6265SDimitry Andric Check(Op0Ty == Op1Ty, 36750b57cec5SDimitry Andric "Both operands to ICmp instruction are not of the same type!", &IC); 36760b57cec5SDimitry Andric // Check that the operands are the right type 3677*81ad6265SDimitry Andric Check(Op0Ty->isIntOrIntVectorTy() || Op0Ty->isPtrOrPtrVectorTy(), 36780b57cec5SDimitry Andric "Invalid operand types for ICmp instruction", &IC); 36790b57cec5SDimitry Andric // Check that the predicate is valid. 3680*81ad6265SDimitry Andric Check(IC.isIntPredicate(), "Invalid predicate in ICmp instruction!", &IC); 36810b57cec5SDimitry Andric 36820b57cec5SDimitry Andric visitInstruction(IC); 36830b57cec5SDimitry Andric } 36840b57cec5SDimitry Andric 36850b57cec5SDimitry Andric void Verifier::visitFCmpInst(FCmpInst &FC) { 36860b57cec5SDimitry Andric // Check that the operands are the same type 36870b57cec5SDimitry Andric Type *Op0Ty = FC.getOperand(0)->getType(); 36880b57cec5SDimitry Andric Type *Op1Ty = FC.getOperand(1)->getType(); 3689*81ad6265SDimitry Andric Check(Op0Ty == Op1Ty, 36900b57cec5SDimitry Andric "Both operands to FCmp instruction are not of the same type!", &FC); 36910b57cec5SDimitry Andric // Check that the operands are the right type 3692*81ad6265SDimitry Andric Check(Op0Ty->isFPOrFPVectorTy(), "Invalid operand types for FCmp instruction", 3693*81ad6265SDimitry Andric &FC); 36940b57cec5SDimitry Andric // Check that the predicate is valid. 3695*81ad6265SDimitry Andric Check(FC.isFPPredicate(), "Invalid predicate in FCmp instruction!", &FC); 36960b57cec5SDimitry Andric 36970b57cec5SDimitry Andric visitInstruction(FC); 36980b57cec5SDimitry Andric } 36990b57cec5SDimitry Andric 37000b57cec5SDimitry Andric void Verifier::visitExtractElementInst(ExtractElementInst &EI) { 3701*81ad6265SDimitry Andric Check(ExtractElementInst::isValidOperands(EI.getOperand(0), EI.getOperand(1)), 37020b57cec5SDimitry Andric "Invalid extractelement operands!", &EI); 37030b57cec5SDimitry Andric visitInstruction(EI); 37040b57cec5SDimitry Andric } 37050b57cec5SDimitry Andric 37060b57cec5SDimitry Andric void Verifier::visitInsertElementInst(InsertElementInst &IE) { 3707*81ad6265SDimitry Andric Check(InsertElementInst::isValidOperands(IE.getOperand(0), IE.getOperand(1), 37080b57cec5SDimitry Andric IE.getOperand(2)), 37090b57cec5SDimitry Andric "Invalid insertelement operands!", &IE); 37100b57cec5SDimitry Andric visitInstruction(IE); 37110b57cec5SDimitry Andric } 37120b57cec5SDimitry Andric 37130b57cec5SDimitry Andric void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) { 3714*81ad6265SDimitry Andric Check(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1), 37155ffd83dbSDimitry Andric SV.getShuffleMask()), 37160b57cec5SDimitry Andric "Invalid shufflevector operands!", &SV); 37170b57cec5SDimitry Andric visitInstruction(SV); 37180b57cec5SDimitry Andric } 37190b57cec5SDimitry Andric 37200b57cec5SDimitry Andric void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) { 37210b57cec5SDimitry Andric Type *TargetTy = GEP.getPointerOperandType()->getScalarType(); 37220b57cec5SDimitry Andric 3723*81ad6265SDimitry Andric Check(isa<PointerType>(TargetTy), 37240b57cec5SDimitry Andric "GEP base pointer is not a vector or a vector of pointers", &GEP); 3725*81ad6265SDimitry Andric Check(GEP.getSourceElementType()->isSized(), "GEP into unsized type!", &GEP); 37260b57cec5SDimitry Andric 3727e8d8bef9SDimitry Andric SmallVector<Value *, 16> Idxs(GEP.indices()); 3728*81ad6265SDimitry Andric Check( 3729*81ad6265SDimitry Andric all_of(Idxs, [](Value *V) { return V->getType()->isIntOrIntVectorTy(); }), 37300b57cec5SDimitry Andric "GEP indexes must be integers", &GEP); 37310b57cec5SDimitry Andric Type *ElTy = 37320b57cec5SDimitry Andric GetElementPtrInst::getIndexedType(GEP.getSourceElementType(), Idxs); 3733*81ad6265SDimitry Andric Check(ElTy, "Invalid indices for GEP pointer type!", &GEP); 37340b57cec5SDimitry Andric 3735*81ad6265SDimitry Andric Check(GEP.getType()->isPtrOrPtrVectorTy() && 37360b57cec5SDimitry Andric GEP.getResultElementType() == ElTy, 37370b57cec5SDimitry Andric "GEP is not of right type for indices!", &GEP, ElTy); 37380b57cec5SDimitry Andric 37395ffd83dbSDimitry Andric if (auto *GEPVTy = dyn_cast<VectorType>(GEP.getType())) { 37400b57cec5SDimitry Andric // Additional checks for vector GEPs. 37415ffd83dbSDimitry Andric ElementCount GEPWidth = GEPVTy->getElementCount(); 37420b57cec5SDimitry Andric if (GEP.getPointerOperandType()->isVectorTy()) 3743*81ad6265SDimitry Andric Check( 37445ffd83dbSDimitry Andric GEPWidth == 37455ffd83dbSDimitry Andric cast<VectorType>(GEP.getPointerOperandType())->getElementCount(), 37460b57cec5SDimitry Andric "Vector GEP result width doesn't match operand's", &GEP); 37470b57cec5SDimitry Andric for (Value *Idx : Idxs) { 37480b57cec5SDimitry Andric Type *IndexTy = Idx->getType(); 37495ffd83dbSDimitry Andric if (auto *IndexVTy = dyn_cast<VectorType>(IndexTy)) { 37505ffd83dbSDimitry Andric ElementCount IndexWidth = IndexVTy->getElementCount(); 3751*81ad6265SDimitry Andric Check(IndexWidth == GEPWidth, "Invalid GEP index vector width", &GEP); 37520b57cec5SDimitry Andric } 3753*81ad6265SDimitry Andric Check(IndexTy->isIntOrIntVectorTy(), 37540b57cec5SDimitry Andric "All GEP indices should be of integer type"); 37550b57cec5SDimitry Andric } 37560b57cec5SDimitry Andric } 37570b57cec5SDimitry Andric 37580b57cec5SDimitry Andric if (auto *PTy = dyn_cast<PointerType>(GEP.getType())) { 3759*81ad6265SDimitry Andric Check(GEP.getAddressSpace() == PTy->getAddressSpace(), 37600b57cec5SDimitry Andric "GEP address space doesn't match type", &GEP); 37610b57cec5SDimitry Andric } 37620b57cec5SDimitry Andric 37630b57cec5SDimitry Andric visitInstruction(GEP); 37640b57cec5SDimitry Andric } 37650b57cec5SDimitry Andric 37660b57cec5SDimitry Andric static bool isContiguous(const ConstantRange &A, const ConstantRange &B) { 37670b57cec5SDimitry Andric return A.getUpper() == B.getLower() || A.getLower() == B.getUpper(); 37680b57cec5SDimitry Andric } 37690b57cec5SDimitry Andric 37700b57cec5SDimitry Andric void Verifier::visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty) { 37710b57cec5SDimitry Andric assert(Range && Range == I.getMetadata(LLVMContext::MD_range) && 37720b57cec5SDimitry Andric "precondition violation"); 37730b57cec5SDimitry Andric 37740b57cec5SDimitry Andric unsigned NumOperands = Range->getNumOperands(); 3775*81ad6265SDimitry Andric Check(NumOperands % 2 == 0, "Unfinished range!", Range); 37760b57cec5SDimitry Andric unsigned NumRanges = NumOperands / 2; 3777*81ad6265SDimitry Andric Check(NumRanges >= 1, "It should have at least one range!", Range); 37780b57cec5SDimitry Andric 37790b57cec5SDimitry Andric ConstantRange LastRange(1, true); // Dummy initial value 37800b57cec5SDimitry Andric for (unsigned i = 0; i < NumRanges; ++i) { 37810b57cec5SDimitry Andric ConstantInt *Low = 37820b57cec5SDimitry Andric mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i)); 3783*81ad6265SDimitry Andric Check(Low, "The lower limit must be an integer!", Low); 37840b57cec5SDimitry Andric ConstantInt *High = 37850b57cec5SDimitry Andric mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i + 1)); 3786*81ad6265SDimitry Andric Check(High, "The upper limit must be an integer!", High); 3787*81ad6265SDimitry Andric Check(High->getType() == Low->getType() && High->getType() == Ty, 37880b57cec5SDimitry Andric "Range types must match instruction type!", &I); 37890b57cec5SDimitry Andric 37900b57cec5SDimitry Andric APInt HighV = High->getValue(); 37910b57cec5SDimitry Andric APInt LowV = Low->getValue(); 37920b57cec5SDimitry Andric ConstantRange CurRange(LowV, HighV); 3793*81ad6265SDimitry Andric Check(!CurRange.isEmptySet() && !CurRange.isFullSet(), 37940b57cec5SDimitry Andric "Range must not be empty!", Range); 37950b57cec5SDimitry Andric if (i != 0) { 3796*81ad6265SDimitry Andric Check(CurRange.intersectWith(LastRange).isEmptySet(), 37970b57cec5SDimitry Andric "Intervals are overlapping", Range); 3798*81ad6265SDimitry Andric Check(LowV.sgt(LastRange.getLower()), "Intervals are not in order", 37990b57cec5SDimitry Andric Range); 3800*81ad6265SDimitry Andric Check(!isContiguous(CurRange, LastRange), "Intervals are contiguous", 38010b57cec5SDimitry Andric Range); 38020b57cec5SDimitry Andric } 38030b57cec5SDimitry Andric LastRange = ConstantRange(LowV, HighV); 38040b57cec5SDimitry Andric } 38050b57cec5SDimitry Andric if (NumRanges > 2) { 38060b57cec5SDimitry Andric APInt FirstLow = 38070b57cec5SDimitry Andric mdconst::dyn_extract<ConstantInt>(Range->getOperand(0))->getValue(); 38080b57cec5SDimitry Andric APInt FirstHigh = 38090b57cec5SDimitry Andric mdconst::dyn_extract<ConstantInt>(Range->getOperand(1))->getValue(); 38100b57cec5SDimitry Andric ConstantRange FirstRange(FirstLow, FirstHigh); 3811*81ad6265SDimitry Andric Check(FirstRange.intersectWith(LastRange).isEmptySet(), 38120b57cec5SDimitry Andric "Intervals are overlapping", Range); 3813*81ad6265SDimitry Andric Check(!isContiguous(FirstRange, LastRange), "Intervals are contiguous", 38140b57cec5SDimitry Andric Range); 38150b57cec5SDimitry Andric } 38160b57cec5SDimitry Andric } 38170b57cec5SDimitry Andric 38180b57cec5SDimitry Andric void Verifier::checkAtomicMemAccessSize(Type *Ty, const Instruction *I) { 38190b57cec5SDimitry Andric unsigned Size = DL.getTypeSizeInBits(Ty); 3820*81ad6265SDimitry Andric Check(Size >= 8, "atomic memory access' size must be byte-sized", Ty, I); 3821*81ad6265SDimitry Andric Check(!(Size & (Size - 1)), 38220b57cec5SDimitry Andric "atomic memory access' operand must have a power-of-two size", Ty, I); 38230b57cec5SDimitry Andric } 38240b57cec5SDimitry Andric 38250b57cec5SDimitry Andric void Verifier::visitLoadInst(LoadInst &LI) { 38260b57cec5SDimitry Andric PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType()); 3827*81ad6265SDimitry Andric Check(PTy, "Load operand must be a pointer.", &LI); 38280b57cec5SDimitry Andric Type *ElTy = LI.getType(); 38290eae32dcSDimitry Andric if (MaybeAlign A = LI.getAlign()) { 3830*81ad6265SDimitry Andric Check(A->value() <= Value::MaximumAlignment, 38310b57cec5SDimitry Andric "huge alignment values are unsupported", &LI); 38320eae32dcSDimitry Andric } 3833*81ad6265SDimitry Andric Check(ElTy->isSized(), "loading unsized types is not allowed", &LI); 38340b57cec5SDimitry Andric if (LI.isAtomic()) { 3835*81ad6265SDimitry Andric Check(LI.getOrdering() != AtomicOrdering::Release && 38360b57cec5SDimitry Andric LI.getOrdering() != AtomicOrdering::AcquireRelease, 38370b57cec5SDimitry Andric "Load cannot have Release ordering", &LI); 3838*81ad6265SDimitry Andric Check(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(), 38390b57cec5SDimitry Andric "atomic load operand must have integer, pointer, or floating point " 38400b57cec5SDimitry Andric "type!", 38410b57cec5SDimitry Andric ElTy, &LI); 38420b57cec5SDimitry Andric checkAtomicMemAccessSize(ElTy, &LI); 38430b57cec5SDimitry Andric } else { 3844*81ad6265SDimitry Andric Check(LI.getSyncScopeID() == SyncScope::System, 38450b57cec5SDimitry Andric "Non-atomic load cannot have SynchronizationScope specified", &LI); 38460b57cec5SDimitry Andric } 38470b57cec5SDimitry Andric 38480b57cec5SDimitry Andric visitInstruction(LI); 38490b57cec5SDimitry Andric } 38500b57cec5SDimitry Andric 38510b57cec5SDimitry Andric void Verifier::visitStoreInst(StoreInst &SI) { 38520b57cec5SDimitry Andric PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType()); 3853*81ad6265SDimitry Andric Check(PTy, "Store operand must be a pointer.", &SI); 3854fe6060f1SDimitry Andric Type *ElTy = SI.getOperand(0)->getType(); 3855*81ad6265SDimitry Andric Check(PTy->isOpaqueOrPointeeTypeMatches(ElTy), 38560b57cec5SDimitry Andric "Stored value type does not match pointer operand type!", &SI, ElTy); 38570eae32dcSDimitry Andric if (MaybeAlign A = SI.getAlign()) { 3858*81ad6265SDimitry Andric Check(A->value() <= Value::MaximumAlignment, 38590b57cec5SDimitry Andric "huge alignment values are unsupported", &SI); 38600eae32dcSDimitry Andric } 3861*81ad6265SDimitry Andric Check(ElTy->isSized(), "storing unsized types is not allowed", &SI); 38620b57cec5SDimitry Andric if (SI.isAtomic()) { 3863*81ad6265SDimitry Andric Check(SI.getOrdering() != AtomicOrdering::Acquire && 38640b57cec5SDimitry Andric SI.getOrdering() != AtomicOrdering::AcquireRelease, 38650b57cec5SDimitry Andric "Store cannot have Acquire ordering", &SI); 3866*81ad6265SDimitry Andric Check(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(), 38670b57cec5SDimitry Andric "atomic store operand must have integer, pointer, or floating point " 38680b57cec5SDimitry Andric "type!", 38690b57cec5SDimitry Andric ElTy, &SI); 38700b57cec5SDimitry Andric checkAtomicMemAccessSize(ElTy, &SI); 38710b57cec5SDimitry Andric } else { 3872*81ad6265SDimitry Andric Check(SI.getSyncScopeID() == SyncScope::System, 38730b57cec5SDimitry Andric "Non-atomic store cannot have SynchronizationScope specified", &SI); 38740b57cec5SDimitry Andric } 38750b57cec5SDimitry Andric visitInstruction(SI); 38760b57cec5SDimitry Andric } 38770b57cec5SDimitry Andric 38780b57cec5SDimitry Andric /// Check that SwiftErrorVal is used as a swifterror argument in CS. 38790b57cec5SDimitry Andric void Verifier::verifySwiftErrorCall(CallBase &Call, 38800b57cec5SDimitry Andric const Value *SwiftErrorVal) { 3881fe6060f1SDimitry Andric for (const auto &I : llvm::enumerate(Call.args())) { 3882fe6060f1SDimitry Andric if (I.value() == SwiftErrorVal) { 3883*81ad6265SDimitry Andric Check(Call.paramHasAttr(I.index(), Attribute::SwiftError), 38840b57cec5SDimitry Andric "swifterror value when used in a callsite should be marked " 38850b57cec5SDimitry Andric "with swifterror attribute", 38860b57cec5SDimitry Andric SwiftErrorVal, Call); 38870b57cec5SDimitry Andric } 38880b57cec5SDimitry Andric } 38890b57cec5SDimitry Andric } 38900b57cec5SDimitry Andric 38910b57cec5SDimitry Andric void Verifier::verifySwiftErrorValue(const Value *SwiftErrorVal) { 38920b57cec5SDimitry Andric // Check that swifterror value is only used by loads, stores, or as 38930b57cec5SDimitry Andric // a swifterror argument. 38940b57cec5SDimitry Andric for (const User *U : SwiftErrorVal->users()) { 3895*81ad6265SDimitry Andric Check(isa<LoadInst>(U) || isa<StoreInst>(U) || isa<CallInst>(U) || 38960b57cec5SDimitry Andric isa<InvokeInst>(U), 38970b57cec5SDimitry Andric "swifterror value can only be loaded and stored from, or " 38980b57cec5SDimitry Andric "as a swifterror argument!", 38990b57cec5SDimitry Andric SwiftErrorVal, U); 39000b57cec5SDimitry Andric // If it is used by a store, check it is the second operand. 39010b57cec5SDimitry Andric if (auto StoreI = dyn_cast<StoreInst>(U)) 3902*81ad6265SDimitry Andric Check(StoreI->getOperand(1) == SwiftErrorVal, 39030b57cec5SDimitry Andric "swifterror value should be the second operand when used " 3904*81ad6265SDimitry Andric "by stores", 3905*81ad6265SDimitry Andric SwiftErrorVal, U); 39060b57cec5SDimitry Andric if (auto *Call = dyn_cast<CallBase>(U)) 39070b57cec5SDimitry Andric verifySwiftErrorCall(*const_cast<CallBase *>(Call), SwiftErrorVal); 39080b57cec5SDimitry Andric } 39090b57cec5SDimitry Andric } 39100b57cec5SDimitry Andric 39110b57cec5SDimitry Andric void Verifier::visitAllocaInst(AllocaInst &AI) { 39120b57cec5SDimitry Andric SmallPtrSet<Type*, 4> Visited; 3913*81ad6265SDimitry Andric Check(AI.getAllocatedType()->isSized(&Visited), 39140b57cec5SDimitry Andric "Cannot allocate unsized type", &AI); 3915*81ad6265SDimitry Andric Check(AI.getArraySize()->getType()->isIntegerTy(), 39160b57cec5SDimitry Andric "Alloca array size must have integer type", &AI); 39170eae32dcSDimitry Andric if (MaybeAlign A = AI.getAlign()) { 3918*81ad6265SDimitry Andric Check(A->value() <= Value::MaximumAlignment, 39190b57cec5SDimitry Andric "huge alignment values are unsupported", &AI); 39200eae32dcSDimitry Andric } 39210b57cec5SDimitry Andric 39220b57cec5SDimitry Andric if (AI.isSwiftError()) { 3923*81ad6265SDimitry Andric Check(AI.getAllocatedType()->isPointerTy(), 3924*81ad6265SDimitry Andric "swifterror alloca must have pointer type", &AI); 3925*81ad6265SDimitry Andric Check(!AI.isArrayAllocation(), 3926*81ad6265SDimitry Andric "swifterror alloca must not be array allocation", &AI); 39270b57cec5SDimitry Andric verifySwiftErrorValue(&AI); 39280b57cec5SDimitry Andric } 39290b57cec5SDimitry Andric 39300b57cec5SDimitry Andric visitInstruction(AI); 39310b57cec5SDimitry Andric } 39320b57cec5SDimitry Andric 39330b57cec5SDimitry Andric void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI) { 3934fe6060f1SDimitry Andric Type *ElTy = CXI.getOperand(1)->getType(); 3935*81ad6265SDimitry Andric Check(ElTy->isIntOrPtrTy(), 39360b57cec5SDimitry Andric "cmpxchg operand must have integer or pointer type", ElTy, &CXI); 39370b57cec5SDimitry Andric checkAtomicMemAccessSize(ElTy, &CXI); 39380b57cec5SDimitry Andric visitInstruction(CXI); 39390b57cec5SDimitry Andric } 39400b57cec5SDimitry Andric 39410b57cec5SDimitry Andric void Verifier::visitAtomicRMWInst(AtomicRMWInst &RMWI) { 3942*81ad6265SDimitry Andric Check(RMWI.getOrdering() != AtomicOrdering::Unordered, 39430b57cec5SDimitry Andric "atomicrmw instructions cannot be unordered.", &RMWI); 39440b57cec5SDimitry Andric auto Op = RMWI.getOperation(); 3945fe6060f1SDimitry Andric Type *ElTy = RMWI.getOperand(1)->getType(); 39460b57cec5SDimitry Andric if (Op == AtomicRMWInst::Xchg) { 3947*81ad6265SDimitry Andric Check(ElTy->isIntegerTy() || ElTy->isFloatingPointTy() || 3948*81ad6265SDimitry Andric ElTy->isPointerTy(), 3949*81ad6265SDimitry Andric "atomicrmw " + AtomicRMWInst::getOperationName(Op) + 39500b57cec5SDimitry Andric " operand must have integer or floating point type!", 39510b57cec5SDimitry Andric &RMWI, ElTy); 39520b57cec5SDimitry Andric } else if (AtomicRMWInst::isFPOperation(Op)) { 3953*81ad6265SDimitry Andric Check(ElTy->isFloatingPointTy(), 3954*81ad6265SDimitry Andric "atomicrmw " + AtomicRMWInst::getOperationName(Op) + 39550b57cec5SDimitry Andric " operand must have floating point type!", 39560b57cec5SDimitry Andric &RMWI, ElTy); 39570b57cec5SDimitry Andric } else { 3958*81ad6265SDimitry Andric Check(ElTy->isIntegerTy(), 3959*81ad6265SDimitry Andric "atomicrmw " + AtomicRMWInst::getOperationName(Op) + 39600b57cec5SDimitry Andric " operand must have integer type!", 39610b57cec5SDimitry Andric &RMWI, ElTy); 39620b57cec5SDimitry Andric } 39630b57cec5SDimitry Andric checkAtomicMemAccessSize(ElTy, &RMWI); 3964*81ad6265SDimitry Andric Check(AtomicRMWInst::FIRST_BINOP <= Op && Op <= AtomicRMWInst::LAST_BINOP, 39650b57cec5SDimitry Andric "Invalid binary operation!", &RMWI); 39660b57cec5SDimitry Andric visitInstruction(RMWI); 39670b57cec5SDimitry Andric } 39680b57cec5SDimitry Andric 39690b57cec5SDimitry Andric void Verifier::visitFenceInst(FenceInst &FI) { 39700b57cec5SDimitry Andric const AtomicOrdering Ordering = FI.getOrdering(); 3971*81ad6265SDimitry Andric Check(Ordering == AtomicOrdering::Acquire || 39720b57cec5SDimitry Andric Ordering == AtomicOrdering::Release || 39730b57cec5SDimitry Andric Ordering == AtomicOrdering::AcquireRelease || 39740b57cec5SDimitry Andric Ordering == AtomicOrdering::SequentiallyConsistent, 39750b57cec5SDimitry Andric "fence instructions may only have acquire, release, acq_rel, or " 39760b57cec5SDimitry Andric "seq_cst ordering.", 39770b57cec5SDimitry Andric &FI); 39780b57cec5SDimitry Andric visitInstruction(FI); 39790b57cec5SDimitry Andric } 39800b57cec5SDimitry Andric 39810b57cec5SDimitry Andric void Verifier::visitExtractValueInst(ExtractValueInst &EVI) { 3982*81ad6265SDimitry Andric Check(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(), 39830b57cec5SDimitry Andric EVI.getIndices()) == EVI.getType(), 39840b57cec5SDimitry Andric "Invalid ExtractValueInst operands!", &EVI); 39850b57cec5SDimitry Andric 39860b57cec5SDimitry Andric visitInstruction(EVI); 39870b57cec5SDimitry Andric } 39880b57cec5SDimitry Andric 39890b57cec5SDimitry Andric void Verifier::visitInsertValueInst(InsertValueInst &IVI) { 3990*81ad6265SDimitry Andric Check(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(), 39910b57cec5SDimitry Andric IVI.getIndices()) == 39920b57cec5SDimitry Andric IVI.getOperand(1)->getType(), 39930b57cec5SDimitry Andric "Invalid InsertValueInst operands!", &IVI); 39940b57cec5SDimitry Andric 39950b57cec5SDimitry Andric visitInstruction(IVI); 39960b57cec5SDimitry Andric } 39970b57cec5SDimitry Andric 39980b57cec5SDimitry Andric static Value *getParentPad(Value *EHPad) { 39990b57cec5SDimitry Andric if (auto *FPI = dyn_cast<FuncletPadInst>(EHPad)) 40000b57cec5SDimitry Andric return FPI->getParentPad(); 40010b57cec5SDimitry Andric 40020b57cec5SDimitry Andric return cast<CatchSwitchInst>(EHPad)->getParentPad(); 40030b57cec5SDimitry Andric } 40040b57cec5SDimitry Andric 40050b57cec5SDimitry Andric void Verifier::visitEHPadPredecessors(Instruction &I) { 40060b57cec5SDimitry Andric assert(I.isEHPad()); 40070b57cec5SDimitry Andric 40080b57cec5SDimitry Andric BasicBlock *BB = I.getParent(); 40090b57cec5SDimitry Andric Function *F = BB->getParent(); 40100b57cec5SDimitry Andric 4011*81ad6265SDimitry Andric Check(BB != &F->getEntryBlock(), "EH pad cannot be in entry block.", &I); 40120b57cec5SDimitry Andric 40130b57cec5SDimitry Andric if (auto *LPI = dyn_cast<LandingPadInst>(&I)) { 40140b57cec5SDimitry Andric // The landingpad instruction defines its parent as a landing pad block. The 40150b57cec5SDimitry Andric // landing pad block may be branched to only by the unwind edge of an 40160b57cec5SDimitry Andric // invoke. 40170b57cec5SDimitry Andric for (BasicBlock *PredBB : predecessors(BB)) { 40180b57cec5SDimitry Andric const auto *II = dyn_cast<InvokeInst>(PredBB->getTerminator()); 4019*81ad6265SDimitry Andric Check(II && II->getUnwindDest() == BB && II->getNormalDest() != BB, 40200b57cec5SDimitry Andric "Block containing LandingPadInst must be jumped to " 40210b57cec5SDimitry Andric "only by the unwind edge of an invoke.", 40220b57cec5SDimitry Andric LPI); 40230b57cec5SDimitry Andric } 40240b57cec5SDimitry Andric return; 40250b57cec5SDimitry Andric } 40260b57cec5SDimitry Andric if (auto *CPI = dyn_cast<CatchPadInst>(&I)) { 40270b57cec5SDimitry Andric if (!pred_empty(BB)) 4028*81ad6265SDimitry Andric Check(BB->getUniquePredecessor() == CPI->getCatchSwitch()->getParent(), 40290b57cec5SDimitry Andric "Block containg CatchPadInst must be jumped to " 40300b57cec5SDimitry Andric "only by its catchswitch.", 40310b57cec5SDimitry Andric CPI); 4032*81ad6265SDimitry Andric Check(BB != CPI->getCatchSwitch()->getUnwindDest(), 40330b57cec5SDimitry Andric "Catchswitch cannot unwind to one of its catchpads", 40340b57cec5SDimitry Andric CPI->getCatchSwitch(), CPI); 40350b57cec5SDimitry Andric return; 40360b57cec5SDimitry Andric } 40370b57cec5SDimitry Andric 40380b57cec5SDimitry Andric // Verify that each pred has a legal terminator with a legal to/from EH 40390b57cec5SDimitry Andric // pad relationship. 40400b57cec5SDimitry Andric Instruction *ToPad = &I; 40410b57cec5SDimitry Andric Value *ToPadParent = getParentPad(ToPad); 40420b57cec5SDimitry Andric for (BasicBlock *PredBB : predecessors(BB)) { 40430b57cec5SDimitry Andric Instruction *TI = PredBB->getTerminator(); 40440b57cec5SDimitry Andric Value *FromPad; 40450b57cec5SDimitry Andric if (auto *II = dyn_cast<InvokeInst>(TI)) { 4046*81ad6265SDimitry Andric Check(II->getUnwindDest() == BB && II->getNormalDest() != BB, 40470b57cec5SDimitry Andric "EH pad must be jumped to via an unwind edge", ToPad, II); 40480b57cec5SDimitry Andric if (auto Bundle = II->getOperandBundle(LLVMContext::OB_funclet)) 40490b57cec5SDimitry Andric FromPad = Bundle->Inputs[0]; 40500b57cec5SDimitry Andric else 40510b57cec5SDimitry Andric FromPad = ConstantTokenNone::get(II->getContext()); 40520b57cec5SDimitry Andric } else if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) { 40530b57cec5SDimitry Andric FromPad = CRI->getOperand(0); 4054*81ad6265SDimitry Andric Check(FromPad != ToPadParent, "A cleanupret must exit its cleanup", CRI); 40550b57cec5SDimitry Andric } else if (auto *CSI = dyn_cast<CatchSwitchInst>(TI)) { 40560b57cec5SDimitry Andric FromPad = CSI; 40570b57cec5SDimitry Andric } else { 4058*81ad6265SDimitry Andric Check(false, "EH pad must be jumped to via an unwind edge", ToPad, TI); 40590b57cec5SDimitry Andric } 40600b57cec5SDimitry Andric 40610b57cec5SDimitry Andric // The edge may exit from zero or more nested pads. 40620b57cec5SDimitry Andric SmallSet<Value *, 8> Seen; 40630b57cec5SDimitry Andric for (;; FromPad = getParentPad(FromPad)) { 4064*81ad6265SDimitry Andric Check(FromPad != ToPad, 40650b57cec5SDimitry Andric "EH pad cannot handle exceptions raised within it", FromPad, TI); 40660b57cec5SDimitry Andric if (FromPad == ToPadParent) { 40670b57cec5SDimitry Andric // This is a legal unwind edge. 40680b57cec5SDimitry Andric break; 40690b57cec5SDimitry Andric } 4070*81ad6265SDimitry Andric Check(!isa<ConstantTokenNone>(FromPad), 40710b57cec5SDimitry Andric "A single unwind edge may only enter one EH pad", TI); 4072*81ad6265SDimitry Andric Check(Seen.insert(FromPad).second, "EH pad jumps through a cycle of pads", 4073*81ad6265SDimitry Andric FromPad); 407404eeddc0SDimitry Andric 407504eeddc0SDimitry Andric // This will be diagnosed on the corresponding instruction already. We 407604eeddc0SDimitry Andric // need the extra check here to make sure getParentPad() works. 4077*81ad6265SDimitry Andric Check(isa<FuncletPadInst>(FromPad) || isa<CatchSwitchInst>(FromPad), 407804eeddc0SDimitry Andric "Parent pad must be catchpad/cleanuppad/catchswitch", TI); 40790b57cec5SDimitry Andric } 40800b57cec5SDimitry Andric } 40810b57cec5SDimitry Andric } 40820b57cec5SDimitry Andric 40830b57cec5SDimitry Andric void Verifier::visitLandingPadInst(LandingPadInst &LPI) { 40840b57cec5SDimitry Andric // The landingpad instruction is ill-formed if it doesn't have any clauses and 40850b57cec5SDimitry Andric // isn't a cleanup. 4086*81ad6265SDimitry Andric Check(LPI.getNumClauses() > 0 || LPI.isCleanup(), 40870b57cec5SDimitry Andric "LandingPadInst needs at least one clause or to be a cleanup.", &LPI); 40880b57cec5SDimitry Andric 40890b57cec5SDimitry Andric visitEHPadPredecessors(LPI); 40900b57cec5SDimitry Andric 40910b57cec5SDimitry Andric if (!LandingPadResultTy) 40920b57cec5SDimitry Andric LandingPadResultTy = LPI.getType(); 40930b57cec5SDimitry Andric else 4094*81ad6265SDimitry Andric Check(LandingPadResultTy == LPI.getType(), 40950b57cec5SDimitry Andric "The landingpad instruction should have a consistent result type " 40960b57cec5SDimitry Andric "inside a function.", 40970b57cec5SDimitry Andric &LPI); 40980b57cec5SDimitry Andric 40990b57cec5SDimitry Andric Function *F = LPI.getParent()->getParent(); 4100*81ad6265SDimitry Andric Check(F->hasPersonalityFn(), 41010b57cec5SDimitry Andric "LandingPadInst needs to be in a function with a personality.", &LPI); 41020b57cec5SDimitry Andric 41030b57cec5SDimitry Andric // The landingpad instruction must be the first non-PHI instruction in the 41040b57cec5SDimitry Andric // block. 4105*81ad6265SDimitry Andric Check(LPI.getParent()->getLandingPadInst() == &LPI, 4106*81ad6265SDimitry Andric "LandingPadInst not the first non-PHI instruction in the block.", &LPI); 41070b57cec5SDimitry Andric 41080b57cec5SDimitry Andric for (unsigned i = 0, e = LPI.getNumClauses(); i < e; ++i) { 41090b57cec5SDimitry Andric Constant *Clause = LPI.getClause(i); 41100b57cec5SDimitry Andric if (LPI.isCatch(i)) { 4111*81ad6265SDimitry Andric Check(isa<PointerType>(Clause->getType()), 41120b57cec5SDimitry Andric "Catch operand does not have pointer type!", &LPI); 41130b57cec5SDimitry Andric } else { 4114*81ad6265SDimitry Andric Check(LPI.isFilter(i), "Clause is neither catch nor filter!", &LPI); 4115*81ad6265SDimitry Andric Check(isa<ConstantArray>(Clause) || isa<ConstantAggregateZero>(Clause), 41160b57cec5SDimitry Andric "Filter operand is not an array of constants!", &LPI); 41170b57cec5SDimitry Andric } 41180b57cec5SDimitry Andric } 41190b57cec5SDimitry Andric 41200b57cec5SDimitry Andric visitInstruction(LPI); 41210b57cec5SDimitry Andric } 41220b57cec5SDimitry Andric 41230b57cec5SDimitry Andric void Verifier::visitResumeInst(ResumeInst &RI) { 4124*81ad6265SDimitry Andric Check(RI.getFunction()->hasPersonalityFn(), 41250b57cec5SDimitry Andric "ResumeInst needs to be in a function with a personality.", &RI); 41260b57cec5SDimitry Andric 41270b57cec5SDimitry Andric if (!LandingPadResultTy) 41280b57cec5SDimitry Andric LandingPadResultTy = RI.getValue()->getType(); 41290b57cec5SDimitry Andric else 4130*81ad6265SDimitry Andric Check(LandingPadResultTy == RI.getValue()->getType(), 41310b57cec5SDimitry Andric "The resume instruction should have a consistent result type " 41320b57cec5SDimitry Andric "inside a function.", 41330b57cec5SDimitry Andric &RI); 41340b57cec5SDimitry Andric 41350b57cec5SDimitry Andric visitTerminator(RI); 41360b57cec5SDimitry Andric } 41370b57cec5SDimitry Andric 41380b57cec5SDimitry Andric void Verifier::visitCatchPadInst(CatchPadInst &CPI) { 41390b57cec5SDimitry Andric BasicBlock *BB = CPI.getParent(); 41400b57cec5SDimitry Andric 41410b57cec5SDimitry Andric Function *F = BB->getParent(); 4142*81ad6265SDimitry Andric Check(F->hasPersonalityFn(), 41430b57cec5SDimitry Andric "CatchPadInst needs to be in a function with a personality.", &CPI); 41440b57cec5SDimitry Andric 4145*81ad6265SDimitry Andric Check(isa<CatchSwitchInst>(CPI.getParentPad()), 41460b57cec5SDimitry Andric "CatchPadInst needs to be directly nested in a CatchSwitchInst.", 41470b57cec5SDimitry Andric CPI.getParentPad()); 41480b57cec5SDimitry Andric 41490b57cec5SDimitry Andric // The catchpad instruction must be the first non-PHI instruction in the 41500b57cec5SDimitry Andric // block. 4151*81ad6265SDimitry Andric Check(BB->getFirstNonPHI() == &CPI, 41520b57cec5SDimitry Andric "CatchPadInst not the first non-PHI instruction in the block.", &CPI); 41530b57cec5SDimitry Andric 41540b57cec5SDimitry Andric visitEHPadPredecessors(CPI); 41550b57cec5SDimitry Andric visitFuncletPadInst(CPI); 41560b57cec5SDimitry Andric } 41570b57cec5SDimitry Andric 41580b57cec5SDimitry Andric void Verifier::visitCatchReturnInst(CatchReturnInst &CatchReturn) { 4159*81ad6265SDimitry Andric Check(isa<CatchPadInst>(CatchReturn.getOperand(0)), 41600b57cec5SDimitry Andric "CatchReturnInst needs to be provided a CatchPad", &CatchReturn, 41610b57cec5SDimitry Andric CatchReturn.getOperand(0)); 41620b57cec5SDimitry Andric 41630b57cec5SDimitry Andric visitTerminator(CatchReturn); 41640b57cec5SDimitry Andric } 41650b57cec5SDimitry Andric 41660b57cec5SDimitry Andric void Verifier::visitCleanupPadInst(CleanupPadInst &CPI) { 41670b57cec5SDimitry Andric BasicBlock *BB = CPI.getParent(); 41680b57cec5SDimitry Andric 41690b57cec5SDimitry Andric Function *F = BB->getParent(); 4170*81ad6265SDimitry Andric Check(F->hasPersonalityFn(), 41710b57cec5SDimitry Andric "CleanupPadInst needs to be in a function with a personality.", &CPI); 41720b57cec5SDimitry Andric 41730b57cec5SDimitry Andric // The cleanuppad instruction must be the first non-PHI instruction in the 41740b57cec5SDimitry Andric // block. 4175*81ad6265SDimitry Andric Check(BB->getFirstNonPHI() == &CPI, 4176*81ad6265SDimitry Andric "CleanupPadInst not the first non-PHI instruction in the block.", &CPI); 41770b57cec5SDimitry Andric 41780b57cec5SDimitry Andric auto *ParentPad = CPI.getParentPad(); 4179*81ad6265SDimitry Andric Check(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad), 41800b57cec5SDimitry Andric "CleanupPadInst has an invalid parent.", &CPI); 41810b57cec5SDimitry Andric 41820b57cec5SDimitry Andric visitEHPadPredecessors(CPI); 41830b57cec5SDimitry Andric visitFuncletPadInst(CPI); 41840b57cec5SDimitry Andric } 41850b57cec5SDimitry Andric 41860b57cec5SDimitry Andric void Verifier::visitFuncletPadInst(FuncletPadInst &FPI) { 41870b57cec5SDimitry Andric User *FirstUser = nullptr; 41880b57cec5SDimitry Andric Value *FirstUnwindPad = nullptr; 41890b57cec5SDimitry Andric SmallVector<FuncletPadInst *, 8> Worklist({&FPI}); 41900b57cec5SDimitry Andric SmallSet<FuncletPadInst *, 8> Seen; 41910b57cec5SDimitry Andric 41920b57cec5SDimitry Andric while (!Worklist.empty()) { 41930b57cec5SDimitry Andric FuncletPadInst *CurrentPad = Worklist.pop_back_val(); 4194*81ad6265SDimitry Andric Check(Seen.insert(CurrentPad).second, 41950b57cec5SDimitry Andric "FuncletPadInst must not be nested within itself", CurrentPad); 41960b57cec5SDimitry Andric Value *UnresolvedAncestorPad = nullptr; 41970b57cec5SDimitry Andric for (User *U : CurrentPad->users()) { 41980b57cec5SDimitry Andric BasicBlock *UnwindDest; 41990b57cec5SDimitry Andric if (auto *CRI = dyn_cast<CleanupReturnInst>(U)) { 42000b57cec5SDimitry Andric UnwindDest = CRI->getUnwindDest(); 42010b57cec5SDimitry Andric } else if (auto *CSI = dyn_cast<CatchSwitchInst>(U)) { 42020b57cec5SDimitry Andric // We allow catchswitch unwind to caller to nest 42030b57cec5SDimitry Andric // within an outer pad that unwinds somewhere else, 42040b57cec5SDimitry Andric // because catchswitch doesn't have a nounwind variant. 42050b57cec5SDimitry Andric // See e.g. SimplifyCFGOpt::SimplifyUnreachable. 42060b57cec5SDimitry Andric if (CSI->unwindsToCaller()) 42070b57cec5SDimitry Andric continue; 42080b57cec5SDimitry Andric UnwindDest = CSI->getUnwindDest(); 42090b57cec5SDimitry Andric } else if (auto *II = dyn_cast<InvokeInst>(U)) { 42100b57cec5SDimitry Andric UnwindDest = II->getUnwindDest(); 42110b57cec5SDimitry Andric } else if (isa<CallInst>(U)) { 42120b57cec5SDimitry Andric // Calls which don't unwind may be found inside funclet 42130b57cec5SDimitry Andric // pads that unwind somewhere else. We don't *require* 42140b57cec5SDimitry Andric // such calls to be annotated nounwind. 42150b57cec5SDimitry Andric continue; 42160b57cec5SDimitry Andric } else if (auto *CPI = dyn_cast<CleanupPadInst>(U)) { 42170b57cec5SDimitry Andric // The unwind dest for a cleanup can only be found by 42180b57cec5SDimitry Andric // recursive search. Add it to the worklist, and we'll 42190b57cec5SDimitry Andric // search for its first use that determines where it unwinds. 42200b57cec5SDimitry Andric Worklist.push_back(CPI); 42210b57cec5SDimitry Andric continue; 42220b57cec5SDimitry Andric } else { 4223*81ad6265SDimitry Andric Check(isa<CatchReturnInst>(U), "Bogus funclet pad use", U); 42240b57cec5SDimitry Andric continue; 42250b57cec5SDimitry Andric } 42260b57cec5SDimitry Andric 42270b57cec5SDimitry Andric Value *UnwindPad; 42280b57cec5SDimitry Andric bool ExitsFPI; 42290b57cec5SDimitry Andric if (UnwindDest) { 42300b57cec5SDimitry Andric UnwindPad = UnwindDest->getFirstNonPHI(); 42310b57cec5SDimitry Andric if (!cast<Instruction>(UnwindPad)->isEHPad()) 42320b57cec5SDimitry Andric continue; 42330b57cec5SDimitry Andric Value *UnwindParent = getParentPad(UnwindPad); 42340b57cec5SDimitry Andric // Ignore unwind edges that don't exit CurrentPad. 42350b57cec5SDimitry Andric if (UnwindParent == CurrentPad) 42360b57cec5SDimitry Andric continue; 42370b57cec5SDimitry Andric // Determine whether the original funclet pad is exited, 42380b57cec5SDimitry Andric // and if we are scanning nested pads determine how many 42390b57cec5SDimitry Andric // of them are exited so we can stop searching their 42400b57cec5SDimitry Andric // children. 42410b57cec5SDimitry Andric Value *ExitedPad = CurrentPad; 42420b57cec5SDimitry Andric ExitsFPI = false; 42430b57cec5SDimitry Andric do { 42440b57cec5SDimitry Andric if (ExitedPad == &FPI) { 42450b57cec5SDimitry Andric ExitsFPI = true; 42460b57cec5SDimitry Andric // Now we can resolve any ancestors of CurrentPad up to 42470b57cec5SDimitry Andric // FPI, but not including FPI since we need to make sure 42480b57cec5SDimitry Andric // to check all direct users of FPI for consistency. 42490b57cec5SDimitry Andric UnresolvedAncestorPad = &FPI; 42500b57cec5SDimitry Andric break; 42510b57cec5SDimitry Andric } 42520b57cec5SDimitry Andric Value *ExitedParent = getParentPad(ExitedPad); 42530b57cec5SDimitry Andric if (ExitedParent == UnwindParent) { 42540b57cec5SDimitry Andric // ExitedPad is the ancestor-most pad which this unwind 42550b57cec5SDimitry Andric // edge exits, so we can resolve up to it, meaning that 42560b57cec5SDimitry Andric // ExitedParent is the first ancestor still unresolved. 42570b57cec5SDimitry Andric UnresolvedAncestorPad = ExitedParent; 42580b57cec5SDimitry Andric break; 42590b57cec5SDimitry Andric } 42600b57cec5SDimitry Andric ExitedPad = ExitedParent; 42610b57cec5SDimitry Andric } while (!isa<ConstantTokenNone>(ExitedPad)); 42620b57cec5SDimitry Andric } else { 42630b57cec5SDimitry Andric // Unwinding to caller exits all pads. 42640b57cec5SDimitry Andric UnwindPad = ConstantTokenNone::get(FPI.getContext()); 42650b57cec5SDimitry Andric ExitsFPI = true; 42660b57cec5SDimitry Andric UnresolvedAncestorPad = &FPI; 42670b57cec5SDimitry Andric } 42680b57cec5SDimitry Andric 42690b57cec5SDimitry Andric if (ExitsFPI) { 42700b57cec5SDimitry Andric // This unwind edge exits FPI. Make sure it agrees with other 42710b57cec5SDimitry Andric // such edges. 42720b57cec5SDimitry Andric if (FirstUser) { 4273*81ad6265SDimitry Andric Check(UnwindPad == FirstUnwindPad, 4274*81ad6265SDimitry Andric "Unwind edges out of a funclet " 42750b57cec5SDimitry Andric "pad must have the same unwind " 42760b57cec5SDimitry Andric "dest", 42770b57cec5SDimitry Andric &FPI, U, FirstUser); 42780b57cec5SDimitry Andric } else { 42790b57cec5SDimitry Andric FirstUser = U; 42800b57cec5SDimitry Andric FirstUnwindPad = UnwindPad; 42810b57cec5SDimitry Andric // Record cleanup sibling unwinds for verifySiblingFuncletUnwinds 42820b57cec5SDimitry Andric if (isa<CleanupPadInst>(&FPI) && !isa<ConstantTokenNone>(UnwindPad) && 42830b57cec5SDimitry Andric getParentPad(UnwindPad) == getParentPad(&FPI)) 42840b57cec5SDimitry Andric SiblingFuncletInfo[&FPI] = cast<Instruction>(U); 42850b57cec5SDimitry Andric } 42860b57cec5SDimitry Andric } 42870b57cec5SDimitry Andric // Make sure we visit all uses of FPI, but for nested pads stop as 42880b57cec5SDimitry Andric // soon as we know where they unwind to. 42890b57cec5SDimitry Andric if (CurrentPad != &FPI) 42900b57cec5SDimitry Andric break; 42910b57cec5SDimitry Andric } 42920b57cec5SDimitry Andric if (UnresolvedAncestorPad) { 42930b57cec5SDimitry Andric if (CurrentPad == UnresolvedAncestorPad) { 42940b57cec5SDimitry Andric // When CurrentPad is FPI itself, we don't mark it as resolved even if 42950b57cec5SDimitry Andric // we've found an unwind edge that exits it, because we need to verify 42960b57cec5SDimitry Andric // all direct uses of FPI. 42970b57cec5SDimitry Andric assert(CurrentPad == &FPI); 42980b57cec5SDimitry Andric continue; 42990b57cec5SDimitry Andric } 43000b57cec5SDimitry Andric // Pop off the worklist any nested pads that we've found an unwind 43010b57cec5SDimitry Andric // destination for. The pads on the worklist are the uncles, 43020b57cec5SDimitry Andric // great-uncles, etc. of CurrentPad. We've found an unwind destination 43030b57cec5SDimitry Andric // for all ancestors of CurrentPad up to but not including 43040b57cec5SDimitry Andric // UnresolvedAncestorPad. 43050b57cec5SDimitry Andric Value *ResolvedPad = CurrentPad; 43060b57cec5SDimitry Andric while (!Worklist.empty()) { 43070b57cec5SDimitry Andric Value *UnclePad = Worklist.back(); 43080b57cec5SDimitry Andric Value *AncestorPad = getParentPad(UnclePad); 43090b57cec5SDimitry Andric // Walk ResolvedPad up the ancestor list until we either find the 43100b57cec5SDimitry Andric // uncle's parent or the last resolved ancestor. 43110b57cec5SDimitry Andric while (ResolvedPad != AncestorPad) { 43120b57cec5SDimitry Andric Value *ResolvedParent = getParentPad(ResolvedPad); 43130b57cec5SDimitry Andric if (ResolvedParent == UnresolvedAncestorPad) { 43140b57cec5SDimitry Andric break; 43150b57cec5SDimitry Andric } 43160b57cec5SDimitry Andric ResolvedPad = ResolvedParent; 43170b57cec5SDimitry Andric } 43180b57cec5SDimitry Andric // If the resolved ancestor search didn't find the uncle's parent, 43190b57cec5SDimitry Andric // then the uncle is not yet resolved. 43200b57cec5SDimitry Andric if (ResolvedPad != AncestorPad) 43210b57cec5SDimitry Andric break; 43220b57cec5SDimitry Andric // This uncle is resolved, so pop it from the worklist. 43230b57cec5SDimitry Andric Worklist.pop_back(); 43240b57cec5SDimitry Andric } 43250b57cec5SDimitry Andric } 43260b57cec5SDimitry Andric } 43270b57cec5SDimitry Andric 43280b57cec5SDimitry Andric if (FirstUnwindPad) { 43290b57cec5SDimitry Andric if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FPI.getParentPad())) { 43300b57cec5SDimitry Andric BasicBlock *SwitchUnwindDest = CatchSwitch->getUnwindDest(); 43310b57cec5SDimitry Andric Value *SwitchUnwindPad; 43320b57cec5SDimitry Andric if (SwitchUnwindDest) 43330b57cec5SDimitry Andric SwitchUnwindPad = SwitchUnwindDest->getFirstNonPHI(); 43340b57cec5SDimitry Andric else 43350b57cec5SDimitry Andric SwitchUnwindPad = ConstantTokenNone::get(FPI.getContext()); 4336*81ad6265SDimitry Andric Check(SwitchUnwindPad == FirstUnwindPad, 43370b57cec5SDimitry Andric "Unwind edges out of a catch must have the same unwind dest as " 43380b57cec5SDimitry Andric "the parent catchswitch", 43390b57cec5SDimitry Andric &FPI, FirstUser, CatchSwitch); 43400b57cec5SDimitry Andric } 43410b57cec5SDimitry Andric } 43420b57cec5SDimitry Andric 43430b57cec5SDimitry Andric visitInstruction(FPI); 43440b57cec5SDimitry Andric } 43450b57cec5SDimitry Andric 43460b57cec5SDimitry Andric void Verifier::visitCatchSwitchInst(CatchSwitchInst &CatchSwitch) { 43470b57cec5SDimitry Andric BasicBlock *BB = CatchSwitch.getParent(); 43480b57cec5SDimitry Andric 43490b57cec5SDimitry Andric Function *F = BB->getParent(); 4350*81ad6265SDimitry Andric Check(F->hasPersonalityFn(), 43510b57cec5SDimitry Andric "CatchSwitchInst needs to be in a function with a personality.", 43520b57cec5SDimitry Andric &CatchSwitch); 43530b57cec5SDimitry Andric 43540b57cec5SDimitry Andric // The catchswitch instruction must be the first non-PHI instruction in the 43550b57cec5SDimitry Andric // block. 4356*81ad6265SDimitry Andric Check(BB->getFirstNonPHI() == &CatchSwitch, 43570b57cec5SDimitry Andric "CatchSwitchInst not the first non-PHI instruction in the block.", 43580b57cec5SDimitry Andric &CatchSwitch); 43590b57cec5SDimitry Andric 43600b57cec5SDimitry Andric auto *ParentPad = CatchSwitch.getParentPad(); 4361*81ad6265SDimitry Andric Check(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad), 43620b57cec5SDimitry Andric "CatchSwitchInst has an invalid parent.", ParentPad); 43630b57cec5SDimitry Andric 43640b57cec5SDimitry Andric if (BasicBlock *UnwindDest = CatchSwitch.getUnwindDest()) { 43650b57cec5SDimitry Andric Instruction *I = UnwindDest->getFirstNonPHI(); 4366*81ad6265SDimitry Andric Check(I->isEHPad() && !isa<LandingPadInst>(I), 43670b57cec5SDimitry Andric "CatchSwitchInst must unwind to an EH block which is not a " 43680b57cec5SDimitry Andric "landingpad.", 43690b57cec5SDimitry Andric &CatchSwitch); 43700b57cec5SDimitry Andric 43710b57cec5SDimitry Andric // Record catchswitch sibling unwinds for verifySiblingFuncletUnwinds 43720b57cec5SDimitry Andric if (getParentPad(I) == ParentPad) 43730b57cec5SDimitry Andric SiblingFuncletInfo[&CatchSwitch] = &CatchSwitch; 43740b57cec5SDimitry Andric } 43750b57cec5SDimitry Andric 4376*81ad6265SDimitry Andric Check(CatchSwitch.getNumHandlers() != 0, 43770b57cec5SDimitry Andric "CatchSwitchInst cannot have empty handler list", &CatchSwitch); 43780b57cec5SDimitry Andric 43790b57cec5SDimitry Andric for (BasicBlock *Handler : CatchSwitch.handlers()) { 4380*81ad6265SDimitry Andric Check(isa<CatchPadInst>(Handler->getFirstNonPHI()), 43810b57cec5SDimitry Andric "CatchSwitchInst handlers must be catchpads", &CatchSwitch, Handler); 43820b57cec5SDimitry Andric } 43830b57cec5SDimitry Andric 43840b57cec5SDimitry Andric visitEHPadPredecessors(CatchSwitch); 43850b57cec5SDimitry Andric visitTerminator(CatchSwitch); 43860b57cec5SDimitry Andric } 43870b57cec5SDimitry Andric 43880b57cec5SDimitry Andric void Verifier::visitCleanupReturnInst(CleanupReturnInst &CRI) { 4389*81ad6265SDimitry Andric Check(isa<CleanupPadInst>(CRI.getOperand(0)), 43900b57cec5SDimitry Andric "CleanupReturnInst needs to be provided a CleanupPad", &CRI, 43910b57cec5SDimitry Andric CRI.getOperand(0)); 43920b57cec5SDimitry Andric 43930b57cec5SDimitry Andric if (BasicBlock *UnwindDest = CRI.getUnwindDest()) { 43940b57cec5SDimitry Andric Instruction *I = UnwindDest->getFirstNonPHI(); 4395*81ad6265SDimitry Andric Check(I->isEHPad() && !isa<LandingPadInst>(I), 43960b57cec5SDimitry Andric "CleanupReturnInst must unwind to an EH block which is not a " 43970b57cec5SDimitry Andric "landingpad.", 43980b57cec5SDimitry Andric &CRI); 43990b57cec5SDimitry Andric } 44000b57cec5SDimitry Andric 44010b57cec5SDimitry Andric visitTerminator(CRI); 44020b57cec5SDimitry Andric } 44030b57cec5SDimitry Andric 44040b57cec5SDimitry Andric void Verifier::verifyDominatesUse(Instruction &I, unsigned i) { 44050b57cec5SDimitry Andric Instruction *Op = cast<Instruction>(I.getOperand(i)); 44060b57cec5SDimitry Andric // If the we have an invalid invoke, don't try to compute the dominance. 44070b57cec5SDimitry Andric // We already reject it in the invoke specific checks and the dominance 44080b57cec5SDimitry Andric // computation doesn't handle multiple edges. 44090b57cec5SDimitry Andric if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) { 44100b57cec5SDimitry Andric if (II->getNormalDest() == II->getUnwindDest()) 44110b57cec5SDimitry Andric return; 44120b57cec5SDimitry Andric } 44130b57cec5SDimitry Andric 44140b57cec5SDimitry Andric // Quick check whether the def has already been encountered in the same block. 44150b57cec5SDimitry Andric // PHI nodes are not checked to prevent accepting preceding PHIs, because PHI 44160b57cec5SDimitry Andric // uses are defined to happen on the incoming edge, not at the instruction. 44170b57cec5SDimitry Andric // 44180b57cec5SDimitry Andric // FIXME: If this operand is a MetadataAsValue (wrapping a LocalAsMetadata) 44190b57cec5SDimitry Andric // wrapping an SSA value, assert that we've already encountered it. See 44200b57cec5SDimitry Andric // related FIXME in Mapper::mapLocalAsMetadata in ValueMapper.cpp. 44210b57cec5SDimitry Andric if (!isa<PHINode>(I) && InstsInThisBlock.count(Op)) 44220b57cec5SDimitry Andric return; 44230b57cec5SDimitry Andric 44240b57cec5SDimitry Andric const Use &U = I.getOperandUse(i); 4425*81ad6265SDimitry Andric Check(DT.dominates(Op, U), "Instruction does not dominate all uses!", Op, &I); 44260b57cec5SDimitry Andric } 44270b57cec5SDimitry Andric 44280b57cec5SDimitry Andric void Verifier::visitDereferenceableMetadata(Instruction& I, MDNode* MD) { 4429*81ad6265SDimitry Andric Check(I.getType()->isPointerTy(), 4430*81ad6265SDimitry Andric "dereferenceable, dereferenceable_or_null " 4431*81ad6265SDimitry Andric "apply only to pointer types", 4432*81ad6265SDimitry Andric &I); 4433*81ad6265SDimitry Andric Check((isa<LoadInst>(I) || isa<IntToPtrInst>(I)), 44340b57cec5SDimitry Andric "dereferenceable, dereferenceable_or_null apply only to load" 4435*81ad6265SDimitry Andric " and inttoptr instructions, use attributes for calls or invokes", 4436*81ad6265SDimitry Andric &I); 4437*81ad6265SDimitry Andric Check(MD->getNumOperands() == 1, 4438*81ad6265SDimitry Andric "dereferenceable, dereferenceable_or_null " 4439*81ad6265SDimitry Andric "take one operand!", 4440*81ad6265SDimitry Andric &I); 44410b57cec5SDimitry Andric ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(MD->getOperand(0)); 4442*81ad6265SDimitry Andric Check(CI && CI->getType()->isIntegerTy(64), 4443*81ad6265SDimitry Andric "dereferenceable, " 4444*81ad6265SDimitry Andric "dereferenceable_or_null metadata value must be an i64!", 4445*81ad6265SDimitry Andric &I); 44460b57cec5SDimitry Andric } 44470b57cec5SDimitry Andric 44488bcb0991SDimitry Andric void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) { 4449*81ad6265SDimitry Andric Check(MD->getNumOperands() >= 2, 44508bcb0991SDimitry Andric "!prof annotations should have no less than 2 operands", MD); 44518bcb0991SDimitry Andric 44528bcb0991SDimitry Andric // Check first operand. 4453*81ad6265SDimitry Andric Check(MD->getOperand(0) != nullptr, "first operand should not be null", MD); 4454*81ad6265SDimitry Andric Check(isa<MDString>(MD->getOperand(0)), 44558bcb0991SDimitry Andric "expected string with name of the !prof annotation", MD); 44568bcb0991SDimitry Andric MDString *MDS = cast<MDString>(MD->getOperand(0)); 44578bcb0991SDimitry Andric StringRef ProfName = MDS->getString(); 44588bcb0991SDimitry Andric 44598bcb0991SDimitry Andric // Check consistency of !prof branch_weights metadata. 44608bcb0991SDimitry Andric if (ProfName.equals("branch_weights")) { 44615ffd83dbSDimitry Andric if (isa<InvokeInst>(&I)) { 4462*81ad6265SDimitry Andric Check(MD->getNumOperands() == 2 || MD->getNumOperands() == 3, 44635ffd83dbSDimitry Andric "Wrong number of InvokeInst branch_weights operands", MD); 44645ffd83dbSDimitry Andric } else { 44658bcb0991SDimitry Andric unsigned ExpectedNumOperands = 0; 44668bcb0991SDimitry Andric if (BranchInst *BI = dyn_cast<BranchInst>(&I)) 44678bcb0991SDimitry Andric ExpectedNumOperands = BI->getNumSuccessors(); 44688bcb0991SDimitry Andric else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I)) 44698bcb0991SDimitry Andric ExpectedNumOperands = SI->getNumSuccessors(); 44705ffd83dbSDimitry Andric else if (isa<CallInst>(&I)) 44718bcb0991SDimitry Andric ExpectedNumOperands = 1; 44728bcb0991SDimitry Andric else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I)) 44738bcb0991SDimitry Andric ExpectedNumOperands = IBI->getNumDestinations(); 44748bcb0991SDimitry Andric else if (isa<SelectInst>(&I)) 44758bcb0991SDimitry Andric ExpectedNumOperands = 2; 44768bcb0991SDimitry Andric else 44778bcb0991SDimitry Andric CheckFailed("!prof branch_weights are not allowed for this instruction", 44788bcb0991SDimitry Andric MD); 44798bcb0991SDimitry Andric 4480*81ad6265SDimitry Andric Check(MD->getNumOperands() == 1 + ExpectedNumOperands, 44818bcb0991SDimitry Andric "Wrong number of operands", MD); 44825ffd83dbSDimitry Andric } 44838bcb0991SDimitry Andric for (unsigned i = 1; i < MD->getNumOperands(); ++i) { 44848bcb0991SDimitry Andric auto &MDO = MD->getOperand(i); 4485*81ad6265SDimitry Andric Check(MDO, "second operand should not be null", MD); 4486*81ad6265SDimitry Andric Check(mdconst::dyn_extract<ConstantInt>(MDO), 44878bcb0991SDimitry Andric "!prof brunch_weights operand is not a const int"); 44888bcb0991SDimitry Andric } 44898bcb0991SDimitry Andric } 44908bcb0991SDimitry Andric } 44918bcb0991SDimitry Andric 4492e8d8bef9SDimitry Andric void Verifier::visitAnnotationMetadata(MDNode *Annotation) { 4493*81ad6265SDimitry Andric Check(isa<MDTuple>(Annotation), "annotation must be a tuple"); 4494*81ad6265SDimitry Andric Check(Annotation->getNumOperands() >= 1, 4495e8d8bef9SDimitry Andric "annotation must have at least one operand"); 4496e8d8bef9SDimitry Andric for (const MDOperand &Op : Annotation->operands()) 4497*81ad6265SDimitry Andric Check(isa<MDString>(Op.get()), "operands must be strings"); 4498e8d8bef9SDimitry Andric } 4499e8d8bef9SDimitry Andric 4500349cc55cSDimitry Andric void Verifier::visitAliasScopeMetadata(const MDNode *MD) { 4501349cc55cSDimitry Andric unsigned NumOps = MD->getNumOperands(); 4502*81ad6265SDimitry Andric Check(NumOps >= 2 && NumOps <= 3, "scope must have two or three operands", 4503349cc55cSDimitry Andric MD); 4504*81ad6265SDimitry Andric Check(MD->getOperand(0).get() == MD || isa<MDString>(MD->getOperand(0)), 4505349cc55cSDimitry Andric "first scope operand must be self-referential or string", MD); 4506349cc55cSDimitry Andric if (NumOps == 3) 4507*81ad6265SDimitry Andric Check(isa<MDString>(MD->getOperand(2)), 4508349cc55cSDimitry Andric "third scope operand must be string (if used)", MD); 4509349cc55cSDimitry Andric 4510349cc55cSDimitry Andric MDNode *Domain = dyn_cast<MDNode>(MD->getOperand(1)); 4511*81ad6265SDimitry Andric Check(Domain != nullptr, "second scope operand must be MDNode", MD); 4512349cc55cSDimitry Andric 4513349cc55cSDimitry Andric unsigned NumDomainOps = Domain->getNumOperands(); 4514*81ad6265SDimitry Andric Check(NumDomainOps >= 1 && NumDomainOps <= 2, 4515349cc55cSDimitry Andric "domain must have one or two operands", Domain); 4516*81ad6265SDimitry Andric Check(Domain->getOperand(0).get() == Domain || 4517349cc55cSDimitry Andric isa<MDString>(Domain->getOperand(0)), 4518349cc55cSDimitry Andric "first domain operand must be self-referential or string", Domain); 4519349cc55cSDimitry Andric if (NumDomainOps == 2) 4520*81ad6265SDimitry Andric Check(isa<MDString>(Domain->getOperand(1)), 4521349cc55cSDimitry Andric "second domain operand must be string (if used)", Domain); 4522349cc55cSDimitry Andric } 4523349cc55cSDimitry Andric 4524349cc55cSDimitry Andric void Verifier::visitAliasScopeListMetadata(const MDNode *MD) { 4525349cc55cSDimitry Andric for (const MDOperand &Op : MD->operands()) { 4526349cc55cSDimitry Andric const MDNode *OpMD = dyn_cast<MDNode>(Op); 4527*81ad6265SDimitry Andric Check(OpMD != nullptr, "scope list must consist of MDNodes", MD); 4528349cc55cSDimitry Andric visitAliasScopeMetadata(OpMD); 4529349cc55cSDimitry Andric } 4530349cc55cSDimitry Andric } 4531349cc55cSDimitry Andric 4532*81ad6265SDimitry Andric void Verifier::visitAccessGroupMetadata(const MDNode *MD) { 4533*81ad6265SDimitry Andric auto IsValidAccessScope = [](const MDNode *MD) { 4534*81ad6265SDimitry Andric return MD->getNumOperands() == 0 && MD->isDistinct(); 4535*81ad6265SDimitry Andric }; 4536*81ad6265SDimitry Andric 4537*81ad6265SDimitry Andric // It must be either an access scope itself... 4538*81ad6265SDimitry Andric if (IsValidAccessScope(MD)) 4539*81ad6265SDimitry Andric return; 4540*81ad6265SDimitry Andric 4541*81ad6265SDimitry Andric // ...or a list of access scopes. 4542*81ad6265SDimitry Andric for (const MDOperand &Op : MD->operands()) { 4543*81ad6265SDimitry Andric const MDNode *OpMD = dyn_cast<MDNode>(Op); 4544*81ad6265SDimitry Andric Check(OpMD != nullptr, "Access scope list must consist of MDNodes", MD); 4545*81ad6265SDimitry Andric Check(IsValidAccessScope(OpMD), 4546*81ad6265SDimitry Andric "Access scope list contains invalid access scope", MD); 4547*81ad6265SDimitry Andric } 4548*81ad6265SDimitry Andric } 4549*81ad6265SDimitry Andric 45500b57cec5SDimitry Andric /// verifyInstruction - Verify that an instruction is well formed. 45510b57cec5SDimitry Andric /// 45520b57cec5SDimitry Andric void Verifier::visitInstruction(Instruction &I) { 45530b57cec5SDimitry Andric BasicBlock *BB = I.getParent(); 4554*81ad6265SDimitry Andric Check(BB, "Instruction not embedded in basic block!", &I); 45550b57cec5SDimitry Andric 45560b57cec5SDimitry Andric if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential 45570b57cec5SDimitry Andric for (User *U : I.users()) { 4558*81ad6265SDimitry Andric Check(U != (User *)&I || !DT.isReachableFromEntry(BB), 45590b57cec5SDimitry Andric "Only PHI nodes may reference their own value!", &I); 45600b57cec5SDimitry Andric } 45610b57cec5SDimitry Andric } 45620b57cec5SDimitry Andric 45630b57cec5SDimitry Andric // Check that void typed values don't have names 4564*81ad6265SDimitry Andric Check(!I.getType()->isVoidTy() || !I.hasName(), 45650b57cec5SDimitry Andric "Instruction has a name, but provides a void value!", &I); 45660b57cec5SDimitry Andric 45670b57cec5SDimitry Andric // Check that the return value of the instruction is either void or a legal 45680b57cec5SDimitry Andric // value type. 4569*81ad6265SDimitry Andric Check(I.getType()->isVoidTy() || I.getType()->isFirstClassType(), 45700b57cec5SDimitry Andric "Instruction returns a non-scalar type!", &I); 45710b57cec5SDimitry Andric 45720b57cec5SDimitry Andric // Check that the instruction doesn't produce metadata. Calls are already 45730b57cec5SDimitry Andric // checked against the callee type. 4574*81ad6265SDimitry Andric Check(!I.getType()->isMetadataTy() || isa<CallInst>(I) || isa<InvokeInst>(I), 45750b57cec5SDimitry Andric "Invalid use of metadata!", &I); 45760b57cec5SDimitry Andric 45770b57cec5SDimitry Andric // Check that all uses of the instruction, if they are instructions 45780b57cec5SDimitry Andric // themselves, actually have parent basic blocks. If the use is not an 45790b57cec5SDimitry Andric // instruction, it is an error! 45800b57cec5SDimitry Andric for (Use &U : I.uses()) { 45810b57cec5SDimitry Andric if (Instruction *Used = dyn_cast<Instruction>(U.getUser())) 4582*81ad6265SDimitry Andric Check(Used->getParent() != nullptr, 45830b57cec5SDimitry Andric "Instruction referencing" 45840b57cec5SDimitry Andric " instruction not embedded in a basic block!", 45850b57cec5SDimitry Andric &I, Used); 45860b57cec5SDimitry Andric else { 45870b57cec5SDimitry Andric CheckFailed("Use of instruction is not an instruction!", U); 45880b57cec5SDimitry Andric return; 45890b57cec5SDimitry Andric } 45900b57cec5SDimitry Andric } 45910b57cec5SDimitry Andric 45920b57cec5SDimitry Andric // Get a pointer to the call base of the instruction if it is some form of 45930b57cec5SDimitry Andric // call. 45940b57cec5SDimitry Andric const CallBase *CBI = dyn_cast<CallBase>(&I); 45950b57cec5SDimitry Andric 45960b57cec5SDimitry Andric for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { 4597*81ad6265SDimitry Andric Check(I.getOperand(i) != nullptr, "Instruction has null operand!", &I); 45980b57cec5SDimitry Andric 45990b57cec5SDimitry Andric // Check to make sure that only first-class-values are operands to 46000b57cec5SDimitry Andric // instructions. 46010b57cec5SDimitry Andric if (!I.getOperand(i)->getType()->isFirstClassType()) { 4602*81ad6265SDimitry Andric Check(false, "Instruction operands must be first-class values!", &I); 46030b57cec5SDimitry Andric } 46040b57cec5SDimitry Andric 46050b57cec5SDimitry Andric if (Function *F = dyn_cast<Function>(I.getOperand(i))) { 4606349cc55cSDimitry Andric // This code checks whether the function is used as the operand of a 4607349cc55cSDimitry Andric // clang_arc_attachedcall operand bundle. 4608349cc55cSDimitry Andric auto IsAttachedCallOperand = [](Function *F, const CallBase *CBI, 4609349cc55cSDimitry Andric int Idx) { 4610349cc55cSDimitry Andric return CBI && CBI->isOperandBundleOfType( 4611349cc55cSDimitry Andric LLVMContext::OB_clang_arc_attachedcall, Idx); 4612349cc55cSDimitry Andric }; 4613349cc55cSDimitry Andric 46140b57cec5SDimitry Andric // Check to make sure that the "address of" an intrinsic function is never 4615349cc55cSDimitry Andric // taken. Ignore cases where the address of the intrinsic function is used 4616349cc55cSDimitry Andric // as the argument of operand bundle "clang.arc.attachedcall" as those 4617349cc55cSDimitry Andric // cases are handled in verifyAttachedCallBundle. 4618*81ad6265SDimitry Andric Check((!F->isIntrinsic() || 4619349cc55cSDimitry Andric (CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i)) || 4620349cc55cSDimitry Andric IsAttachedCallOperand(F, CBI, i)), 46210b57cec5SDimitry Andric "Cannot take the address of an intrinsic!", &I); 4622*81ad6265SDimitry Andric Check(!F->isIntrinsic() || isa<CallInst>(I) || 46230b57cec5SDimitry Andric F->getIntrinsicID() == Intrinsic::donothing || 4624fe6060f1SDimitry Andric F->getIntrinsicID() == Intrinsic::seh_try_begin || 4625fe6060f1SDimitry Andric F->getIntrinsicID() == Intrinsic::seh_try_end || 4626fe6060f1SDimitry Andric F->getIntrinsicID() == Intrinsic::seh_scope_begin || 4627fe6060f1SDimitry Andric F->getIntrinsicID() == Intrinsic::seh_scope_end || 46280b57cec5SDimitry Andric F->getIntrinsicID() == Intrinsic::coro_resume || 46290b57cec5SDimitry Andric F->getIntrinsicID() == Intrinsic::coro_destroy || 4630*81ad6265SDimitry Andric F->getIntrinsicID() == 4631*81ad6265SDimitry Andric Intrinsic::experimental_patchpoint_void || 46320b57cec5SDimitry Andric F->getIntrinsicID() == Intrinsic::experimental_patchpoint_i64 || 46330b57cec5SDimitry Andric F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint || 4634349cc55cSDimitry Andric F->getIntrinsicID() == Intrinsic::wasm_rethrow || 4635349cc55cSDimitry Andric IsAttachedCallOperand(F, CBI, i), 46360b57cec5SDimitry Andric "Cannot invoke an intrinsic other than donothing, patchpoint, " 4637349cc55cSDimitry Andric "statepoint, coro_resume, coro_destroy or clang.arc.attachedcall", 46380b57cec5SDimitry Andric &I); 4639*81ad6265SDimitry Andric Check(F->getParent() == &M, "Referencing function in another module!", &I, 4640*81ad6265SDimitry Andric &M, F, F->getParent()); 46410b57cec5SDimitry Andric } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) { 4642*81ad6265SDimitry Andric Check(OpBB->getParent() == BB->getParent(), 46430b57cec5SDimitry Andric "Referring to a basic block in another function!", &I); 46440b57cec5SDimitry Andric } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) { 4645*81ad6265SDimitry Andric Check(OpArg->getParent() == BB->getParent(), 46460b57cec5SDimitry Andric "Referring to an argument in another function!", &I); 46470b57cec5SDimitry Andric } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) { 4648*81ad6265SDimitry Andric Check(GV->getParent() == &M, "Referencing global in another module!", &I, 46490b57cec5SDimitry Andric &M, GV, GV->getParent()); 46500b57cec5SDimitry Andric } else if (isa<Instruction>(I.getOperand(i))) { 46510b57cec5SDimitry Andric verifyDominatesUse(I, i); 46520b57cec5SDimitry Andric } else if (isa<InlineAsm>(I.getOperand(i))) { 4653*81ad6265SDimitry Andric Check(CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i), 46540b57cec5SDimitry Andric "Cannot take the address of an inline asm!", &I); 46550b57cec5SDimitry Andric } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(i))) { 4656fe6060f1SDimitry Andric if (CE->getType()->isPtrOrPtrVectorTy()) { 46570b57cec5SDimitry Andric // If we have a ConstantExpr pointer, we need to see if it came from an 4658fe6060f1SDimitry Andric // illegal bitcast. 46590b57cec5SDimitry Andric visitConstantExprsRecursively(CE); 46600b57cec5SDimitry Andric } 46610b57cec5SDimitry Andric } 46620b57cec5SDimitry Andric } 46630b57cec5SDimitry Andric 46640b57cec5SDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpmath)) { 4665*81ad6265SDimitry Andric Check(I.getType()->isFPOrFPVectorTy(), 46660b57cec5SDimitry Andric "fpmath requires a floating point result!", &I); 4667*81ad6265SDimitry Andric Check(MD->getNumOperands() == 1, "fpmath takes one operand!", &I); 46680b57cec5SDimitry Andric if (ConstantFP *CFP0 = 46690b57cec5SDimitry Andric mdconst::dyn_extract_or_null<ConstantFP>(MD->getOperand(0))) { 46700b57cec5SDimitry Andric const APFloat &Accuracy = CFP0->getValueAPF(); 4671*81ad6265SDimitry Andric Check(&Accuracy.getSemantics() == &APFloat::IEEEsingle(), 46720b57cec5SDimitry Andric "fpmath accuracy must have float type", &I); 4673*81ad6265SDimitry Andric Check(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(), 46740b57cec5SDimitry Andric "fpmath accuracy not a positive number!", &I); 46750b57cec5SDimitry Andric } else { 4676*81ad6265SDimitry Andric Check(false, "invalid fpmath accuracy!", &I); 46770b57cec5SDimitry Andric } 46780b57cec5SDimitry Andric } 46790b57cec5SDimitry Andric 46800b57cec5SDimitry Andric if (MDNode *Range = I.getMetadata(LLVMContext::MD_range)) { 4681*81ad6265SDimitry Andric Check(isa<LoadInst>(I) || isa<CallInst>(I) || isa<InvokeInst>(I), 46820b57cec5SDimitry Andric "Ranges are only for loads, calls and invokes!", &I); 46830b57cec5SDimitry Andric visitRangeMetadata(I, Range, I.getType()); 46840b57cec5SDimitry Andric } 46850b57cec5SDimitry Andric 4686349cc55cSDimitry Andric if (I.hasMetadata(LLVMContext::MD_invariant_group)) { 4687*81ad6265SDimitry Andric Check(isa<LoadInst>(I) || isa<StoreInst>(I), 4688349cc55cSDimitry Andric "invariant.group metadata is only for loads and stores", &I); 4689349cc55cSDimitry Andric } 4690349cc55cSDimitry Andric 46910b57cec5SDimitry Andric if (I.getMetadata(LLVMContext::MD_nonnull)) { 4692*81ad6265SDimitry Andric Check(I.getType()->isPointerTy(), "nonnull applies only to pointer types", 46930b57cec5SDimitry Andric &I); 4694*81ad6265SDimitry Andric Check(isa<LoadInst>(I), 46950b57cec5SDimitry Andric "nonnull applies only to load instructions, use attributes" 46960b57cec5SDimitry Andric " for calls or invokes", 46970b57cec5SDimitry Andric &I); 46980b57cec5SDimitry Andric } 46990b57cec5SDimitry Andric 47000b57cec5SDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable)) 47010b57cec5SDimitry Andric visitDereferenceableMetadata(I, MD); 47020b57cec5SDimitry Andric 47030b57cec5SDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable_or_null)) 47040b57cec5SDimitry Andric visitDereferenceableMetadata(I, MD); 47050b57cec5SDimitry Andric 47060b57cec5SDimitry Andric if (MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa)) 47070b57cec5SDimitry Andric TBAAVerifyHelper.visitTBAAMetadata(I, TBAA); 47080b57cec5SDimitry Andric 4709349cc55cSDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_noalias)) 4710349cc55cSDimitry Andric visitAliasScopeListMetadata(MD); 4711349cc55cSDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_alias_scope)) 4712349cc55cSDimitry Andric visitAliasScopeListMetadata(MD); 4713349cc55cSDimitry Andric 4714*81ad6265SDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_access_group)) 4715*81ad6265SDimitry Andric visitAccessGroupMetadata(MD); 4716*81ad6265SDimitry Andric 47170b57cec5SDimitry Andric if (MDNode *AlignMD = I.getMetadata(LLVMContext::MD_align)) { 4718*81ad6265SDimitry Andric Check(I.getType()->isPointerTy(), "align applies only to pointer types", 47190b57cec5SDimitry Andric &I); 4720*81ad6265SDimitry Andric Check(isa<LoadInst>(I), 4721*81ad6265SDimitry Andric "align applies only to load instructions, " 4722*81ad6265SDimitry Andric "use attributes for calls or invokes", 4723*81ad6265SDimitry Andric &I); 4724*81ad6265SDimitry Andric Check(AlignMD->getNumOperands() == 1, "align takes one operand!", &I); 47250b57cec5SDimitry Andric ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(AlignMD->getOperand(0)); 4726*81ad6265SDimitry Andric Check(CI && CI->getType()->isIntegerTy(64), 47270b57cec5SDimitry Andric "align metadata value must be an i64!", &I); 47280b57cec5SDimitry Andric uint64_t Align = CI->getZExtValue(); 4729*81ad6265SDimitry Andric Check(isPowerOf2_64(Align), "align metadata value must be a power of 2!", 4730*81ad6265SDimitry Andric &I); 4731*81ad6265SDimitry Andric Check(Align <= Value::MaximumAlignment, 47320b57cec5SDimitry Andric "alignment is larger that implementation defined limit", &I); 47330b57cec5SDimitry Andric } 47340b57cec5SDimitry Andric 47358bcb0991SDimitry Andric if (MDNode *MD = I.getMetadata(LLVMContext::MD_prof)) 47368bcb0991SDimitry Andric visitProfMetadata(I, MD); 47378bcb0991SDimitry Andric 4738e8d8bef9SDimitry Andric if (MDNode *Annotation = I.getMetadata(LLVMContext::MD_annotation)) 4739e8d8bef9SDimitry Andric visitAnnotationMetadata(Annotation); 4740e8d8bef9SDimitry Andric 47410b57cec5SDimitry Andric if (MDNode *N = I.getDebugLoc().getAsMDNode()) { 4742*81ad6265SDimitry Andric CheckDI(isa<DILocation>(N), "invalid !dbg metadata attachment", &I, N); 47435ffd83dbSDimitry Andric visitMDNode(*N, AreDebugLocsAllowed::Yes); 47440b57cec5SDimitry Andric } 47450b57cec5SDimitry Andric 47468bcb0991SDimitry Andric if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) { 47470b57cec5SDimitry Andric verifyFragmentExpression(*DII); 47488bcb0991SDimitry Andric verifyNotEntryValue(*DII); 47498bcb0991SDimitry Andric } 47500b57cec5SDimitry Andric 47515ffd83dbSDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 47525ffd83dbSDimitry Andric I.getAllMetadata(MDs); 47535ffd83dbSDimitry Andric for (auto Attachment : MDs) { 47545ffd83dbSDimitry Andric unsigned Kind = Attachment.first; 47555ffd83dbSDimitry Andric auto AllowLocs = 47565ffd83dbSDimitry Andric (Kind == LLVMContext::MD_dbg || Kind == LLVMContext::MD_loop) 47575ffd83dbSDimitry Andric ? AreDebugLocsAllowed::Yes 47585ffd83dbSDimitry Andric : AreDebugLocsAllowed::No; 47595ffd83dbSDimitry Andric visitMDNode(*Attachment.second, AllowLocs); 47605ffd83dbSDimitry Andric } 47615ffd83dbSDimitry Andric 47620b57cec5SDimitry Andric InstsInThisBlock.insert(&I); 47630b57cec5SDimitry Andric } 47640b57cec5SDimitry Andric 47650b57cec5SDimitry Andric /// Allow intrinsics to be verified in different ways. 47660b57cec5SDimitry Andric void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) { 47670b57cec5SDimitry Andric Function *IF = Call.getCalledFunction(); 4768*81ad6265SDimitry Andric Check(IF->isDeclaration(), "Intrinsic functions should never be defined!", 47690b57cec5SDimitry Andric IF); 47700b57cec5SDimitry Andric 47710b57cec5SDimitry Andric // Verify that the intrinsic prototype lines up with what the .td files 47720b57cec5SDimitry Andric // describe. 47730b57cec5SDimitry Andric FunctionType *IFTy = IF->getFunctionType(); 47740b57cec5SDimitry Andric bool IsVarArg = IFTy->isVarArg(); 47750b57cec5SDimitry Andric 47760b57cec5SDimitry Andric SmallVector<Intrinsic::IITDescriptor, 8> Table; 47770b57cec5SDimitry Andric getIntrinsicInfoTableEntries(ID, Table); 47780b57cec5SDimitry Andric ArrayRef<Intrinsic::IITDescriptor> TableRef = Table; 47790b57cec5SDimitry Andric 47800b57cec5SDimitry Andric // Walk the descriptors to extract overloaded types. 47810b57cec5SDimitry Andric SmallVector<Type *, 4> ArgTys; 47820b57cec5SDimitry Andric Intrinsic::MatchIntrinsicTypesResult Res = 47830b57cec5SDimitry Andric Intrinsic::matchIntrinsicSignature(IFTy, TableRef, ArgTys); 4784*81ad6265SDimitry Andric Check(Res != Intrinsic::MatchIntrinsicTypes_NoMatchRet, 47850b57cec5SDimitry Andric "Intrinsic has incorrect return type!", IF); 4786*81ad6265SDimitry Andric Check(Res != Intrinsic::MatchIntrinsicTypes_NoMatchArg, 47870b57cec5SDimitry Andric "Intrinsic has incorrect argument type!", IF); 47880b57cec5SDimitry Andric 47890b57cec5SDimitry Andric // Verify if the intrinsic call matches the vararg property. 47900b57cec5SDimitry Andric if (IsVarArg) 4791*81ad6265SDimitry Andric Check(!Intrinsic::matchIntrinsicVarArg(IsVarArg, TableRef), 47920b57cec5SDimitry Andric "Intrinsic was not defined with variable arguments!", IF); 47930b57cec5SDimitry Andric else 4794*81ad6265SDimitry Andric Check(!Intrinsic::matchIntrinsicVarArg(IsVarArg, TableRef), 47950b57cec5SDimitry Andric "Callsite was not defined with variable arguments!", IF); 47960b57cec5SDimitry Andric 47970b57cec5SDimitry Andric // All descriptors should be absorbed by now. 4798*81ad6265SDimitry Andric Check(TableRef.empty(), "Intrinsic has too few arguments!", IF); 47990b57cec5SDimitry Andric 48000b57cec5SDimitry Andric // Now that we have the intrinsic ID and the actual argument types (and we 48010b57cec5SDimitry Andric // know they are legal for the intrinsic!) get the intrinsic name through the 48020b57cec5SDimitry Andric // usual means. This allows us to verify the mangling of argument types into 48030b57cec5SDimitry Andric // the name. 4804fe6060f1SDimitry Andric const std::string ExpectedName = 4805fe6060f1SDimitry Andric Intrinsic::getName(ID, ArgTys, IF->getParent(), IFTy); 4806*81ad6265SDimitry Andric Check(ExpectedName == IF->getName(), 48070b57cec5SDimitry Andric "Intrinsic name not mangled correctly for type arguments! " 48080b57cec5SDimitry Andric "Should be: " + 48090b57cec5SDimitry Andric ExpectedName, 48100b57cec5SDimitry Andric IF); 48110b57cec5SDimitry Andric 48120b57cec5SDimitry Andric // If the intrinsic takes MDNode arguments, verify that they are either global 48130b57cec5SDimitry Andric // or are local to *this* function. 4814fe6060f1SDimitry Andric for (Value *V : Call.args()) { 48150b57cec5SDimitry Andric if (auto *MD = dyn_cast<MetadataAsValue>(V)) 48160b57cec5SDimitry Andric visitMetadataAsValue(*MD, Call.getCaller()); 4817fe6060f1SDimitry Andric if (auto *Const = dyn_cast<Constant>(V)) 4818*81ad6265SDimitry Andric Check(!Const->getType()->isX86_AMXTy(), 4819fe6060f1SDimitry Andric "const x86_amx is not allowed in argument!"); 4820fe6060f1SDimitry Andric } 48210b57cec5SDimitry Andric 48220b57cec5SDimitry Andric switch (ID) { 48230b57cec5SDimitry Andric default: 48240b57cec5SDimitry Andric break; 48255ffd83dbSDimitry Andric case Intrinsic::assume: { 48265ffd83dbSDimitry Andric for (auto &Elem : Call.bundle_op_infos()) { 4827*81ad6265SDimitry Andric Check(Elem.Tag->getKey() == "ignore" || 48285ffd83dbSDimitry Andric Attribute::isExistingAttribute(Elem.Tag->getKey()), 4829349cc55cSDimitry Andric "tags must be valid attribute names", Call); 48305ffd83dbSDimitry Andric Attribute::AttrKind Kind = 48315ffd83dbSDimitry Andric Attribute::getAttrKindFromName(Elem.Tag->getKey()); 4832e8d8bef9SDimitry Andric unsigned ArgCount = Elem.End - Elem.Begin; 4833e8d8bef9SDimitry Andric if (Kind == Attribute::Alignment) { 4834*81ad6265SDimitry Andric Check(ArgCount <= 3 && ArgCount >= 2, 4835349cc55cSDimitry Andric "alignment assumptions should have 2 or 3 arguments", Call); 4836*81ad6265SDimitry Andric Check(Call.getOperand(Elem.Begin)->getType()->isPointerTy(), 4837349cc55cSDimitry Andric "first argument should be a pointer", Call); 4838*81ad6265SDimitry Andric Check(Call.getOperand(Elem.Begin + 1)->getType()->isIntegerTy(), 4839349cc55cSDimitry Andric "second argument should be an integer", Call); 4840e8d8bef9SDimitry Andric if (ArgCount == 3) 4841*81ad6265SDimitry Andric Check(Call.getOperand(Elem.Begin + 2)->getType()->isIntegerTy(), 4842349cc55cSDimitry Andric "third argument should be an integer if present", Call); 4843e8d8bef9SDimitry Andric return; 4844e8d8bef9SDimitry Andric } 4845*81ad6265SDimitry Andric Check(ArgCount <= 2, "too many arguments", Call); 48465ffd83dbSDimitry Andric if (Kind == Attribute::None) 48475ffd83dbSDimitry Andric break; 4848fe6060f1SDimitry Andric if (Attribute::isIntAttrKind(Kind)) { 4849*81ad6265SDimitry Andric Check(ArgCount == 2, "this attribute should have 2 arguments", Call); 4850*81ad6265SDimitry Andric Check(isa<ConstantInt>(Call.getOperand(Elem.Begin + 1)), 4851349cc55cSDimitry Andric "the second argument should be a constant integral value", Call); 4852fe6060f1SDimitry Andric } else if (Attribute::canUseAsParamAttr(Kind)) { 4853*81ad6265SDimitry Andric Check((ArgCount) == 1, "this attribute should have one argument", Call); 4854fe6060f1SDimitry Andric } else if (Attribute::canUseAsFnAttr(Kind)) { 4855*81ad6265SDimitry Andric Check((ArgCount) == 0, "this attribute has no argument", Call); 48565ffd83dbSDimitry Andric } 48575ffd83dbSDimitry Andric } 48585ffd83dbSDimitry Andric break; 48595ffd83dbSDimitry Andric } 48600b57cec5SDimitry Andric case Intrinsic::coro_id: { 48610b57cec5SDimitry Andric auto *InfoArg = Call.getArgOperand(3)->stripPointerCasts(); 48620b57cec5SDimitry Andric if (isa<ConstantPointerNull>(InfoArg)) 48630b57cec5SDimitry Andric break; 48640b57cec5SDimitry Andric auto *GV = dyn_cast<GlobalVariable>(InfoArg); 4865*81ad6265SDimitry Andric Check(GV && GV->isConstant() && GV->hasDefinitiveInitializer(), 4866fe6060f1SDimitry Andric "info argument of llvm.coro.id must refer to an initialized " 48670b57cec5SDimitry Andric "constant"); 48680b57cec5SDimitry Andric Constant *Init = GV->getInitializer(); 4869*81ad6265SDimitry Andric Check(isa<ConstantStruct>(Init) || isa<ConstantArray>(Init), 4870fe6060f1SDimitry Andric "info argument of llvm.coro.id must refer to either a struct or " 48710b57cec5SDimitry Andric "an array"); 48720b57cec5SDimitry Andric break; 48730b57cec5SDimitry Andric } 4874*81ad6265SDimitry Andric case Intrinsic::fptrunc_round: { 4875*81ad6265SDimitry Andric // Check the rounding mode 4876*81ad6265SDimitry Andric Metadata *MD = nullptr; 4877*81ad6265SDimitry Andric auto *MAV = dyn_cast<MetadataAsValue>(Call.getOperand(1)); 4878*81ad6265SDimitry Andric if (MAV) 4879*81ad6265SDimitry Andric MD = MAV->getMetadata(); 4880*81ad6265SDimitry Andric 4881*81ad6265SDimitry Andric Check(MD != nullptr, "missing rounding mode argument", Call); 4882*81ad6265SDimitry Andric 4883*81ad6265SDimitry Andric Check(isa<MDString>(MD), 4884*81ad6265SDimitry Andric ("invalid value for llvm.fptrunc.round metadata operand" 4885*81ad6265SDimitry Andric " (the operand should be a string)"), 4886*81ad6265SDimitry Andric MD); 4887*81ad6265SDimitry Andric 4888*81ad6265SDimitry Andric Optional<RoundingMode> RoundMode = 4889*81ad6265SDimitry Andric convertStrToRoundingMode(cast<MDString>(MD)->getString()); 4890*81ad6265SDimitry Andric Check(RoundMode && *RoundMode != RoundingMode::Dynamic, 4891*81ad6265SDimitry Andric "unsupported rounding mode argument", Call); 4892*81ad6265SDimitry Andric break; 4893*81ad6265SDimitry Andric } 4894*81ad6265SDimitry Andric #define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID: 4895*81ad6265SDimitry Andric #include "llvm/IR/VPIntrinsics.def" 4896*81ad6265SDimitry Andric visitVPIntrinsic(cast<VPIntrinsic>(Call)); 4897*81ad6265SDimitry Andric break; 48985ffd83dbSDimitry Andric #define INSTRUCTION(NAME, NARGS, ROUND_MODE, INTRINSIC) \ 4899480093f4SDimitry Andric case Intrinsic::INTRINSIC: 4900480093f4SDimitry Andric #include "llvm/IR/ConstrainedOps.def" 49010b57cec5SDimitry Andric visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(Call)); 49020b57cec5SDimitry Andric break; 49030b57cec5SDimitry Andric case Intrinsic::dbg_declare: // llvm.dbg.declare 4904*81ad6265SDimitry Andric Check(isa<MetadataAsValue>(Call.getArgOperand(0)), 49050b57cec5SDimitry Andric "invalid llvm.dbg.declare intrinsic call 1", Call); 49060b57cec5SDimitry Andric visitDbgIntrinsic("declare", cast<DbgVariableIntrinsic>(Call)); 49070b57cec5SDimitry Andric break; 49080b57cec5SDimitry Andric case Intrinsic::dbg_addr: // llvm.dbg.addr 49090b57cec5SDimitry Andric visitDbgIntrinsic("addr", cast<DbgVariableIntrinsic>(Call)); 49100b57cec5SDimitry Andric break; 49110b57cec5SDimitry Andric case Intrinsic::dbg_value: // llvm.dbg.value 49120b57cec5SDimitry Andric visitDbgIntrinsic("value", cast<DbgVariableIntrinsic>(Call)); 49130b57cec5SDimitry Andric break; 49140b57cec5SDimitry Andric case Intrinsic::dbg_label: // llvm.dbg.label 49150b57cec5SDimitry Andric visitDbgLabelIntrinsic("label", cast<DbgLabelInst>(Call)); 49160b57cec5SDimitry Andric break; 49170b57cec5SDimitry Andric case Intrinsic::memcpy: 49185ffd83dbSDimitry Andric case Intrinsic::memcpy_inline: 49190b57cec5SDimitry Andric case Intrinsic::memmove: 4920*81ad6265SDimitry Andric case Intrinsic::memset: 4921*81ad6265SDimitry Andric case Intrinsic::memset_inline: { 49220b57cec5SDimitry Andric const auto *MI = cast<MemIntrinsic>(&Call); 49230b57cec5SDimitry Andric auto IsValidAlignment = [&](unsigned Alignment) -> bool { 49240b57cec5SDimitry Andric return Alignment == 0 || isPowerOf2_32(Alignment); 49250b57cec5SDimitry Andric }; 4926*81ad6265SDimitry Andric Check(IsValidAlignment(MI->getDestAlignment()), 49270b57cec5SDimitry Andric "alignment of arg 0 of memory intrinsic must be 0 or a power of 2", 49280b57cec5SDimitry Andric Call); 49290b57cec5SDimitry Andric if (const auto *MTI = dyn_cast<MemTransferInst>(MI)) { 4930*81ad6265SDimitry Andric Check(IsValidAlignment(MTI->getSourceAlignment()), 49310b57cec5SDimitry Andric "alignment of arg 1 of memory intrinsic must be 0 or a power of 2", 49320b57cec5SDimitry Andric Call); 49330b57cec5SDimitry Andric } 49340b57cec5SDimitry Andric 49350b57cec5SDimitry Andric break; 49360b57cec5SDimitry Andric } 49370b57cec5SDimitry Andric case Intrinsic::memcpy_element_unordered_atomic: 49380b57cec5SDimitry Andric case Intrinsic::memmove_element_unordered_atomic: 49390b57cec5SDimitry Andric case Intrinsic::memset_element_unordered_atomic: { 49400b57cec5SDimitry Andric const auto *AMI = cast<AtomicMemIntrinsic>(&Call); 49410b57cec5SDimitry Andric 49420b57cec5SDimitry Andric ConstantInt *ElementSizeCI = 49430b57cec5SDimitry Andric cast<ConstantInt>(AMI->getRawElementSizeInBytes()); 49440b57cec5SDimitry Andric const APInt &ElementSizeVal = ElementSizeCI->getValue(); 4945*81ad6265SDimitry Andric Check(ElementSizeVal.isPowerOf2(), 49460b57cec5SDimitry Andric "element size of the element-wise atomic memory intrinsic " 49470b57cec5SDimitry Andric "must be a power of 2", 49480b57cec5SDimitry Andric Call); 49490b57cec5SDimitry Andric 49500b57cec5SDimitry Andric auto IsValidAlignment = [&](uint64_t Alignment) { 49510b57cec5SDimitry Andric return isPowerOf2_64(Alignment) && ElementSizeVal.ule(Alignment); 49520b57cec5SDimitry Andric }; 49530b57cec5SDimitry Andric uint64_t DstAlignment = AMI->getDestAlignment(); 4954*81ad6265SDimitry Andric Check(IsValidAlignment(DstAlignment), 49550b57cec5SDimitry Andric "incorrect alignment of the destination argument", Call); 49560b57cec5SDimitry Andric if (const auto *AMT = dyn_cast<AtomicMemTransferInst>(AMI)) { 49570b57cec5SDimitry Andric uint64_t SrcAlignment = AMT->getSourceAlignment(); 4958*81ad6265SDimitry Andric Check(IsValidAlignment(SrcAlignment), 49590b57cec5SDimitry Andric "incorrect alignment of the source argument", Call); 49600b57cec5SDimitry Andric } 49610b57cec5SDimitry Andric break; 49620b57cec5SDimitry Andric } 49635ffd83dbSDimitry Andric case Intrinsic::call_preallocated_setup: { 49645ffd83dbSDimitry Andric auto *NumArgs = dyn_cast<ConstantInt>(Call.getArgOperand(0)); 4965*81ad6265SDimitry Andric Check(NumArgs != nullptr, 49665ffd83dbSDimitry Andric "llvm.call.preallocated.setup argument must be a constant"); 49675ffd83dbSDimitry Andric bool FoundCall = false; 49685ffd83dbSDimitry Andric for (User *U : Call.users()) { 49695ffd83dbSDimitry Andric auto *UseCall = dyn_cast<CallBase>(U); 4970*81ad6265SDimitry Andric Check(UseCall != nullptr, 49715ffd83dbSDimitry Andric "Uses of llvm.call.preallocated.setup must be calls"); 49725ffd83dbSDimitry Andric const Function *Fn = UseCall->getCalledFunction(); 49735ffd83dbSDimitry Andric if (Fn && Fn->getIntrinsicID() == Intrinsic::call_preallocated_arg) { 49745ffd83dbSDimitry Andric auto *AllocArgIndex = dyn_cast<ConstantInt>(UseCall->getArgOperand(1)); 4975*81ad6265SDimitry Andric Check(AllocArgIndex != nullptr, 49765ffd83dbSDimitry Andric "llvm.call.preallocated.alloc arg index must be a constant"); 49775ffd83dbSDimitry Andric auto AllocArgIndexInt = AllocArgIndex->getValue(); 4978*81ad6265SDimitry Andric Check(AllocArgIndexInt.sge(0) && 49795ffd83dbSDimitry Andric AllocArgIndexInt.slt(NumArgs->getValue()), 49805ffd83dbSDimitry Andric "llvm.call.preallocated.alloc arg index must be between 0 and " 49815ffd83dbSDimitry Andric "corresponding " 49825ffd83dbSDimitry Andric "llvm.call.preallocated.setup's argument count"); 49835ffd83dbSDimitry Andric } else if (Fn && Fn->getIntrinsicID() == 49845ffd83dbSDimitry Andric Intrinsic::call_preallocated_teardown) { 49855ffd83dbSDimitry Andric // nothing to do 49865ffd83dbSDimitry Andric } else { 4987*81ad6265SDimitry Andric Check(!FoundCall, "Can have at most one call corresponding to a " 49885ffd83dbSDimitry Andric "llvm.call.preallocated.setup"); 49895ffd83dbSDimitry Andric FoundCall = true; 49905ffd83dbSDimitry Andric size_t NumPreallocatedArgs = 0; 4991349cc55cSDimitry Andric for (unsigned i = 0; i < UseCall->arg_size(); i++) { 49925ffd83dbSDimitry Andric if (UseCall->paramHasAttr(i, Attribute::Preallocated)) { 49935ffd83dbSDimitry Andric ++NumPreallocatedArgs; 49945ffd83dbSDimitry Andric } 49955ffd83dbSDimitry Andric } 4996*81ad6265SDimitry Andric Check(NumPreallocatedArgs != 0, 49975ffd83dbSDimitry Andric "cannot use preallocated intrinsics on a call without " 49985ffd83dbSDimitry Andric "preallocated arguments"); 4999*81ad6265SDimitry Andric Check(NumArgs->equalsInt(NumPreallocatedArgs), 50005ffd83dbSDimitry Andric "llvm.call.preallocated.setup arg size must be equal to number " 50015ffd83dbSDimitry Andric "of preallocated arguments " 50025ffd83dbSDimitry Andric "at call site", 50035ffd83dbSDimitry Andric Call, *UseCall); 50045ffd83dbSDimitry Andric // getOperandBundle() cannot be called if more than one of the operand 50055ffd83dbSDimitry Andric // bundle exists. There is already a check elsewhere for this, so skip 50065ffd83dbSDimitry Andric // here if we see more than one. 50075ffd83dbSDimitry Andric if (UseCall->countOperandBundlesOfType(LLVMContext::OB_preallocated) > 50085ffd83dbSDimitry Andric 1) { 50095ffd83dbSDimitry Andric return; 50105ffd83dbSDimitry Andric } 50115ffd83dbSDimitry Andric auto PreallocatedBundle = 50125ffd83dbSDimitry Andric UseCall->getOperandBundle(LLVMContext::OB_preallocated); 5013*81ad6265SDimitry Andric Check(PreallocatedBundle, 50145ffd83dbSDimitry Andric "Use of llvm.call.preallocated.setup outside intrinsics " 50155ffd83dbSDimitry Andric "must be in \"preallocated\" operand bundle"); 5016*81ad6265SDimitry Andric Check(PreallocatedBundle->Inputs.front().get() == &Call, 50175ffd83dbSDimitry Andric "preallocated bundle must have token from corresponding " 50185ffd83dbSDimitry Andric "llvm.call.preallocated.setup"); 50195ffd83dbSDimitry Andric } 50205ffd83dbSDimitry Andric } 50215ffd83dbSDimitry Andric break; 50225ffd83dbSDimitry Andric } 50235ffd83dbSDimitry Andric case Intrinsic::call_preallocated_arg: { 50245ffd83dbSDimitry Andric auto *Token = dyn_cast<CallBase>(Call.getArgOperand(0)); 5025*81ad6265SDimitry Andric Check(Token && Token->getCalledFunction()->getIntrinsicID() == 50265ffd83dbSDimitry Andric Intrinsic::call_preallocated_setup, 50275ffd83dbSDimitry Andric "llvm.call.preallocated.arg token argument must be a " 50285ffd83dbSDimitry Andric "llvm.call.preallocated.setup"); 5029*81ad6265SDimitry Andric Check(Call.hasFnAttr(Attribute::Preallocated), 50305ffd83dbSDimitry Andric "llvm.call.preallocated.arg must be called with a \"preallocated\" " 50315ffd83dbSDimitry Andric "call site attribute"); 50325ffd83dbSDimitry Andric break; 50335ffd83dbSDimitry Andric } 50345ffd83dbSDimitry Andric case Intrinsic::call_preallocated_teardown: { 50355ffd83dbSDimitry Andric auto *Token = dyn_cast<CallBase>(Call.getArgOperand(0)); 5036*81ad6265SDimitry Andric Check(Token && Token->getCalledFunction()->getIntrinsicID() == 50375ffd83dbSDimitry Andric Intrinsic::call_preallocated_setup, 50385ffd83dbSDimitry Andric "llvm.call.preallocated.teardown token argument must be a " 50395ffd83dbSDimitry Andric "llvm.call.preallocated.setup"); 50405ffd83dbSDimitry Andric break; 50415ffd83dbSDimitry Andric } 50420b57cec5SDimitry Andric case Intrinsic::gcroot: 50430b57cec5SDimitry Andric case Intrinsic::gcwrite: 50440b57cec5SDimitry Andric case Intrinsic::gcread: 50450b57cec5SDimitry Andric if (ID == Intrinsic::gcroot) { 50460b57cec5SDimitry Andric AllocaInst *AI = 50470b57cec5SDimitry Andric dyn_cast<AllocaInst>(Call.getArgOperand(0)->stripPointerCasts()); 5048*81ad6265SDimitry Andric Check(AI, "llvm.gcroot parameter #1 must be an alloca.", Call); 5049*81ad6265SDimitry Andric Check(isa<Constant>(Call.getArgOperand(1)), 50500b57cec5SDimitry Andric "llvm.gcroot parameter #2 must be a constant.", Call); 50510b57cec5SDimitry Andric if (!AI->getAllocatedType()->isPointerTy()) { 5052*81ad6265SDimitry Andric Check(!isa<ConstantPointerNull>(Call.getArgOperand(1)), 50530b57cec5SDimitry Andric "llvm.gcroot parameter #1 must either be a pointer alloca, " 50540b57cec5SDimitry Andric "or argument #2 must be a non-null constant.", 50550b57cec5SDimitry Andric Call); 50560b57cec5SDimitry Andric } 50570b57cec5SDimitry Andric } 50580b57cec5SDimitry Andric 5059*81ad6265SDimitry Andric Check(Call.getParent()->getParent()->hasGC(), 50600b57cec5SDimitry Andric "Enclosing function does not use GC.", Call); 50610b57cec5SDimitry Andric break; 50620b57cec5SDimitry Andric case Intrinsic::init_trampoline: 5063*81ad6265SDimitry Andric Check(isa<Function>(Call.getArgOperand(1)->stripPointerCasts()), 50640b57cec5SDimitry Andric "llvm.init_trampoline parameter #2 must resolve to a function.", 50650b57cec5SDimitry Andric Call); 50660b57cec5SDimitry Andric break; 50670b57cec5SDimitry Andric case Intrinsic::prefetch: 5068*81ad6265SDimitry Andric Check(cast<ConstantInt>(Call.getArgOperand(1))->getZExtValue() < 2 && 50690b57cec5SDimitry Andric cast<ConstantInt>(Call.getArgOperand(2))->getZExtValue() < 4, 50700b57cec5SDimitry Andric "invalid arguments to llvm.prefetch", Call); 50710b57cec5SDimitry Andric break; 50720b57cec5SDimitry Andric case Intrinsic::stackprotector: 5073*81ad6265SDimitry Andric Check(isa<AllocaInst>(Call.getArgOperand(1)->stripPointerCasts()), 50740b57cec5SDimitry Andric "llvm.stackprotector parameter #2 must resolve to an alloca.", Call); 50750b57cec5SDimitry Andric break; 50760b57cec5SDimitry Andric case Intrinsic::localescape: { 50770b57cec5SDimitry Andric BasicBlock *BB = Call.getParent(); 5078*81ad6265SDimitry Andric Check(BB == &BB->getParent()->front(), 50790b57cec5SDimitry Andric "llvm.localescape used outside of entry block", Call); 5080*81ad6265SDimitry Andric Check(!SawFrameEscape, "multiple calls to llvm.localescape in one function", 5081*81ad6265SDimitry Andric Call); 50820b57cec5SDimitry Andric for (Value *Arg : Call.args()) { 50830b57cec5SDimitry Andric if (isa<ConstantPointerNull>(Arg)) 50840b57cec5SDimitry Andric continue; // Null values are allowed as placeholders. 50850b57cec5SDimitry Andric auto *AI = dyn_cast<AllocaInst>(Arg->stripPointerCasts()); 5086*81ad6265SDimitry Andric Check(AI && AI->isStaticAlloca(), 50870b57cec5SDimitry Andric "llvm.localescape only accepts static allocas", Call); 50880b57cec5SDimitry Andric } 5089349cc55cSDimitry Andric FrameEscapeInfo[BB->getParent()].first = Call.arg_size(); 50900b57cec5SDimitry Andric SawFrameEscape = true; 50910b57cec5SDimitry Andric break; 50920b57cec5SDimitry Andric } 50930b57cec5SDimitry Andric case Intrinsic::localrecover: { 50940b57cec5SDimitry Andric Value *FnArg = Call.getArgOperand(0)->stripPointerCasts(); 50950b57cec5SDimitry Andric Function *Fn = dyn_cast<Function>(FnArg); 5096*81ad6265SDimitry Andric Check(Fn && !Fn->isDeclaration(), 50970b57cec5SDimitry Andric "llvm.localrecover first " 50980b57cec5SDimitry Andric "argument must be function defined in this module", 50990b57cec5SDimitry Andric Call); 51000b57cec5SDimitry Andric auto *IdxArg = cast<ConstantInt>(Call.getArgOperand(2)); 51010b57cec5SDimitry Andric auto &Entry = FrameEscapeInfo[Fn]; 51020b57cec5SDimitry Andric Entry.second = unsigned( 51030b57cec5SDimitry Andric std::max(uint64_t(Entry.second), IdxArg->getLimitedValue(~0U) + 1)); 51040b57cec5SDimitry Andric break; 51050b57cec5SDimitry Andric } 51060b57cec5SDimitry Andric 51070b57cec5SDimitry Andric case Intrinsic::experimental_gc_statepoint: 51080b57cec5SDimitry Andric if (auto *CI = dyn_cast<CallInst>(&Call)) 5109*81ad6265SDimitry Andric Check(!CI->isInlineAsm(), 51100b57cec5SDimitry Andric "gc.statepoint support for inline assembly unimplemented", CI); 5111*81ad6265SDimitry Andric Check(Call.getParent()->getParent()->hasGC(), 51120b57cec5SDimitry Andric "Enclosing function does not use GC.", Call); 51130b57cec5SDimitry Andric 51140b57cec5SDimitry Andric verifyStatepoint(Call); 51150b57cec5SDimitry Andric break; 51160b57cec5SDimitry Andric case Intrinsic::experimental_gc_result: { 5117*81ad6265SDimitry Andric Check(Call.getParent()->getParent()->hasGC(), 51180b57cec5SDimitry Andric "Enclosing function does not use GC.", Call); 51190b57cec5SDimitry Andric // Are we tied to a statepoint properly? 51200b57cec5SDimitry Andric const auto *StatepointCall = dyn_cast<CallBase>(Call.getArgOperand(0)); 51210b57cec5SDimitry Andric const Function *StatepointFn = 51220b57cec5SDimitry Andric StatepointCall ? StatepointCall->getCalledFunction() : nullptr; 5123*81ad6265SDimitry Andric Check(StatepointFn && StatepointFn->isDeclaration() && 51240b57cec5SDimitry Andric StatepointFn->getIntrinsicID() == 51250b57cec5SDimitry Andric Intrinsic::experimental_gc_statepoint, 51260b57cec5SDimitry Andric "gc.result operand #1 must be from a statepoint", Call, 51270b57cec5SDimitry Andric Call.getArgOperand(0)); 51280b57cec5SDimitry Andric 5129*81ad6265SDimitry Andric // Check that result type matches wrapped callee. 5130*81ad6265SDimitry Andric auto *TargetFuncType = 5131*81ad6265SDimitry Andric cast<FunctionType>(StatepointCall->getParamElementType(2)); 5132*81ad6265SDimitry Andric Check(Call.getType() == TargetFuncType->getReturnType(), 51330b57cec5SDimitry Andric "gc.result result type does not match wrapped callee", Call); 51340b57cec5SDimitry Andric break; 51350b57cec5SDimitry Andric } 51360b57cec5SDimitry Andric case Intrinsic::experimental_gc_relocate: { 5137*81ad6265SDimitry Andric Check(Call.arg_size() == 3, "wrong number of arguments", Call); 51380b57cec5SDimitry Andric 5139*81ad6265SDimitry Andric Check(isa<PointerType>(Call.getType()->getScalarType()), 51400b57cec5SDimitry Andric "gc.relocate must return a pointer or a vector of pointers", Call); 51410b57cec5SDimitry Andric 51420b57cec5SDimitry Andric // Check that this relocate is correctly tied to the statepoint 51430b57cec5SDimitry Andric 51440b57cec5SDimitry Andric // This is case for relocate on the unwinding path of an invoke statepoint 51450b57cec5SDimitry Andric if (LandingPadInst *LandingPad = 51460b57cec5SDimitry Andric dyn_cast<LandingPadInst>(Call.getArgOperand(0))) { 51470b57cec5SDimitry Andric 51480b57cec5SDimitry Andric const BasicBlock *InvokeBB = 51490b57cec5SDimitry Andric LandingPad->getParent()->getUniquePredecessor(); 51500b57cec5SDimitry Andric 51510b57cec5SDimitry Andric // Landingpad relocates should have only one predecessor with invoke 51520b57cec5SDimitry Andric // statepoint terminator 5153*81ad6265SDimitry Andric Check(InvokeBB, "safepoints should have unique landingpads", 51540b57cec5SDimitry Andric LandingPad->getParent()); 5155*81ad6265SDimitry Andric Check(InvokeBB->getTerminator(), "safepoint block should be well formed", 51560b57cec5SDimitry Andric InvokeBB); 5157*81ad6265SDimitry Andric Check(isa<GCStatepointInst>(InvokeBB->getTerminator()), 51580b57cec5SDimitry Andric "gc relocate should be linked to a statepoint", InvokeBB); 51590b57cec5SDimitry Andric } else { 51600b57cec5SDimitry Andric // In all other cases relocate should be tied to the statepoint directly. 51610b57cec5SDimitry Andric // This covers relocates on a normal return path of invoke statepoint and 51620b57cec5SDimitry Andric // relocates of a call statepoint. 51630b57cec5SDimitry Andric auto Token = Call.getArgOperand(0); 5164*81ad6265SDimitry Andric Check(isa<GCStatepointInst>(Token), 51650b57cec5SDimitry Andric "gc relocate is incorrectly tied to the statepoint", Call, Token); 51660b57cec5SDimitry Andric } 51670b57cec5SDimitry Andric 51680b57cec5SDimitry Andric // Verify rest of the relocate arguments. 51690b57cec5SDimitry Andric const CallBase &StatepointCall = 51705ffd83dbSDimitry Andric *cast<GCRelocateInst>(Call).getStatepoint(); 51710b57cec5SDimitry Andric 51720b57cec5SDimitry Andric // Both the base and derived must be piped through the safepoint. 51730b57cec5SDimitry Andric Value *Base = Call.getArgOperand(1); 5174*81ad6265SDimitry Andric Check(isa<ConstantInt>(Base), 51750b57cec5SDimitry Andric "gc.relocate operand #2 must be integer offset", Call); 51760b57cec5SDimitry Andric 51770b57cec5SDimitry Andric Value *Derived = Call.getArgOperand(2); 5178*81ad6265SDimitry Andric Check(isa<ConstantInt>(Derived), 51790b57cec5SDimitry Andric "gc.relocate operand #3 must be integer offset", Call); 51800b57cec5SDimitry Andric 51815ffd83dbSDimitry Andric const uint64_t BaseIndex = cast<ConstantInt>(Base)->getZExtValue(); 51825ffd83dbSDimitry Andric const uint64_t DerivedIndex = cast<ConstantInt>(Derived)->getZExtValue(); 51835ffd83dbSDimitry Andric 51840b57cec5SDimitry Andric // Check the bounds 51855ffd83dbSDimitry Andric if (auto Opt = StatepointCall.getOperandBundle(LLVMContext::OB_gc_live)) { 5186*81ad6265SDimitry Andric Check(BaseIndex < Opt->Inputs.size(), 51870b57cec5SDimitry Andric "gc.relocate: statepoint base index out of bounds", Call); 5188*81ad6265SDimitry Andric Check(DerivedIndex < Opt->Inputs.size(), 51895ffd83dbSDimitry Andric "gc.relocate: statepoint derived index out of bounds", Call); 51905ffd83dbSDimitry Andric } 51910b57cec5SDimitry Andric 51920b57cec5SDimitry Andric // Relocated value must be either a pointer type or vector-of-pointer type, 51930b57cec5SDimitry Andric // but gc_relocate does not need to return the same pointer type as the 51940b57cec5SDimitry Andric // relocated pointer. It can be casted to the correct type later if it's 51950b57cec5SDimitry Andric // desired. However, they must have the same address space and 'vectorness' 51960b57cec5SDimitry Andric GCRelocateInst &Relocate = cast<GCRelocateInst>(Call); 5197*81ad6265SDimitry Andric Check(Relocate.getDerivedPtr()->getType()->isPtrOrPtrVectorTy(), 51980b57cec5SDimitry Andric "gc.relocate: relocated value must be a gc pointer", Call); 51990b57cec5SDimitry Andric 52000b57cec5SDimitry Andric auto ResultType = Call.getType(); 52010b57cec5SDimitry Andric auto DerivedType = Relocate.getDerivedPtr()->getType(); 5202*81ad6265SDimitry Andric Check(ResultType->isVectorTy() == DerivedType->isVectorTy(), 52030b57cec5SDimitry Andric "gc.relocate: vector relocates to vector and pointer to pointer", 52040b57cec5SDimitry Andric Call); 5205*81ad6265SDimitry Andric Check( 52060b57cec5SDimitry Andric ResultType->getPointerAddressSpace() == 52070b57cec5SDimitry Andric DerivedType->getPointerAddressSpace(), 52080b57cec5SDimitry Andric "gc.relocate: relocating a pointer shouldn't change its address space", 52090b57cec5SDimitry Andric Call); 52100b57cec5SDimitry Andric break; 52110b57cec5SDimitry Andric } 52120b57cec5SDimitry Andric case Intrinsic::eh_exceptioncode: 52130b57cec5SDimitry Andric case Intrinsic::eh_exceptionpointer: { 5214*81ad6265SDimitry Andric Check(isa<CatchPadInst>(Call.getArgOperand(0)), 52150b57cec5SDimitry Andric "eh.exceptionpointer argument must be a catchpad", Call); 52160b57cec5SDimitry Andric break; 52170b57cec5SDimitry Andric } 52185ffd83dbSDimitry Andric case Intrinsic::get_active_lane_mask: { 5219*81ad6265SDimitry Andric Check(Call.getType()->isVectorTy(), 5220*81ad6265SDimitry Andric "get_active_lane_mask: must return a " 5221*81ad6265SDimitry Andric "vector", 5222*81ad6265SDimitry Andric Call); 52235ffd83dbSDimitry Andric auto *ElemTy = Call.getType()->getScalarType(); 5224*81ad6265SDimitry Andric Check(ElemTy->isIntegerTy(1), 5225*81ad6265SDimitry Andric "get_active_lane_mask: element type is not " 5226*81ad6265SDimitry Andric "i1", 5227*81ad6265SDimitry Andric Call); 52285ffd83dbSDimitry Andric break; 52295ffd83dbSDimitry Andric } 52300b57cec5SDimitry Andric case Intrinsic::masked_load: { 5231*81ad6265SDimitry Andric Check(Call.getType()->isVectorTy(), "masked_load: must return a vector", 52320b57cec5SDimitry Andric Call); 52330b57cec5SDimitry Andric 52340b57cec5SDimitry Andric Value *Ptr = Call.getArgOperand(0); 52350b57cec5SDimitry Andric ConstantInt *Alignment = cast<ConstantInt>(Call.getArgOperand(1)); 52360b57cec5SDimitry Andric Value *Mask = Call.getArgOperand(2); 52370b57cec5SDimitry Andric Value *PassThru = Call.getArgOperand(3); 5238*81ad6265SDimitry Andric Check(Mask->getType()->isVectorTy(), "masked_load: mask must be vector", 52390b57cec5SDimitry Andric Call); 5240*81ad6265SDimitry Andric Check(Alignment->getValue().isPowerOf2(), 52410b57cec5SDimitry Andric "masked_load: alignment must be a power of 2", Call); 52420b57cec5SDimitry Andric 5243fe6060f1SDimitry Andric PointerType *PtrTy = cast<PointerType>(Ptr->getType()); 5244*81ad6265SDimitry Andric Check(PtrTy->isOpaqueOrPointeeTypeMatches(Call.getType()), 52450b57cec5SDimitry Andric "masked_load: return must match pointer type", Call); 5246*81ad6265SDimitry Andric Check(PassThru->getType() == Call.getType(), 5247fe6060f1SDimitry Andric "masked_load: pass through and return type must match", Call); 5248*81ad6265SDimitry Andric Check(cast<VectorType>(Mask->getType())->getElementCount() == 5249fe6060f1SDimitry Andric cast<VectorType>(Call.getType())->getElementCount(), 5250fe6060f1SDimitry Andric "masked_load: vector mask must be same length as return", Call); 52510b57cec5SDimitry Andric break; 52520b57cec5SDimitry Andric } 52530b57cec5SDimitry Andric case Intrinsic::masked_store: { 52540b57cec5SDimitry Andric Value *Val = Call.getArgOperand(0); 52550b57cec5SDimitry Andric Value *Ptr = Call.getArgOperand(1); 52560b57cec5SDimitry Andric ConstantInt *Alignment = cast<ConstantInt>(Call.getArgOperand(2)); 52570b57cec5SDimitry Andric Value *Mask = Call.getArgOperand(3); 5258*81ad6265SDimitry Andric Check(Mask->getType()->isVectorTy(), "masked_store: mask must be vector", 52590b57cec5SDimitry Andric Call); 5260*81ad6265SDimitry Andric Check(Alignment->getValue().isPowerOf2(), 52610b57cec5SDimitry Andric "masked_store: alignment must be a power of 2", Call); 52620b57cec5SDimitry Andric 5263fe6060f1SDimitry Andric PointerType *PtrTy = cast<PointerType>(Ptr->getType()); 5264*81ad6265SDimitry Andric Check(PtrTy->isOpaqueOrPointeeTypeMatches(Val->getType()), 52650b57cec5SDimitry Andric "masked_store: storee must match pointer type", Call); 5266*81ad6265SDimitry Andric Check(cast<VectorType>(Mask->getType())->getElementCount() == 5267fe6060f1SDimitry Andric cast<VectorType>(Val->getType())->getElementCount(), 5268fe6060f1SDimitry Andric "masked_store: vector mask must be same length as value", Call); 52690b57cec5SDimitry Andric break; 52700b57cec5SDimitry Andric } 52710b57cec5SDimitry Andric 52725ffd83dbSDimitry Andric case Intrinsic::masked_gather: { 52735ffd83dbSDimitry Andric const APInt &Alignment = 52745ffd83dbSDimitry Andric cast<ConstantInt>(Call.getArgOperand(1))->getValue(); 5275*81ad6265SDimitry Andric Check(Alignment.isZero() || Alignment.isPowerOf2(), 52765ffd83dbSDimitry Andric "masked_gather: alignment must be 0 or a power of 2", Call); 52775ffd83dbSDimitry Andric break; 52785ffd83dbSDimitry Andric } 52795ffd83dbSDimitry Andric case Intrinsic::masked_scatter: { 52805ffd83dbSDimitry Andric const APInt &Alignment = 52815ffd83dbSDimitry Andric cast<ConstantInt>(Call.getArgOperand(2))->getValue(); 5282*81ad6265SDimitry Andric Check(Alignment.isZero() || Alignment.isPowerOf2(), 52835ffd83dbSDimitry Andric "masked_scatter: alignment must be 0 or a power of 2", Call); 52845ffd83dbSDimitry Andric break; 52855ffd83dbSDimitry Andric } 52865ffd83dbSDimitry Andric 52870b57cec5SDimitry Andric case Intrinsic::experimental_guard: { 5288*81ad6265SDimitry Andric Check(isa<CallInst>(Call), "experimental_guard cannot be invoked", Call); 5289*81ad6265SDimitry Andric Check(Call.countOperandBundlesOfType(LLVMContext::OB_deopt) == 1, 52900b57cec5SDimitry Andric "experimental_guard must have exactly one " 52910b57cec5SDimitry Andric "\"deopt\" operand bundle"); 52920b57cec5SDimitry Andric break; 52930b57cec5SDimitry Andric } 52940b57cec5SDimitry Andric 52950b57cec5SDimitry Andric case Intrinsic::experimental_deoptimize: { 5296*81ad6265SDimitry Andric Check(isa<CallInst>(Call), "experimental_deoptimize cannot be invoked", 52970b57cec5SDimitry Andric Call); 5298*81ad6265SDimitry Andric Check(Call.countOperandBundlesOfType(LLVMContext::OB_deopt) == 1, 52990b57cec5SDimitry Andric "experimental_deoptimize must have exactly one " 53000b57cec5SDimitry Andric "\"deopt\" operand bundle"); 5301*81ad6265SDimitry Andric Check(Call.getType() == Call.getFunction()->getReturnType(), 53020b57cec5SDimitry Andric "experimental_deoptimize return type must match caller return type"); 53030b57cec5SDimitry Andric 53040b57cec5SDimitry Andric if (isa<CallInst>(Call)) { 53050b57cec5SDimitry Andric auto *RI = dyn_cast<ReturnInst>(Call.getNextNode()); 5306*81ad6265SDimitry Andric Check(RI, 53070b57cec5SDimitry Andric "calls to experimental_deoptimize must be followed by a return"); 53080b57cec5SDimitry Andric 53090b57cec5SDimitry Andric if (!Call.getType()->isVoidTy() && RI) 5310*81ad6265SDimitry Andric Check(RI->getReturnValue() == &Call, 53110b57cec5SDimitry Andric "calls to experimental_deoptimize must be followed by a return " 53120b57cec5SDimitry Andric "of the value computed by experimental_deoptimize"); 53130b57cec5SDimitry Andric } 53140b57cec5SDimitry Andric 53150b57cec5SDimitry Andric break; 53160b57cec5SDimitry Andric } 5317fe6060f1SDimitry Andric case Intrinsic::vector_reduce_and: 5318fe6060f1SDimitry Andric case Intrinsic::vector_reduce_or: 5319fe6060f1SDimitry Andric case Intrinsic::vector_reduce_xor: 5320fe6060f1SDimitry Andric case Intrinsic::vector_reduce_add: 5321fe6060f1SDimitry Andric case Intrinsic::vector_reduce_mul: 5322fe6060f1SDimitry Andric case Intrinsic::vector_reduce_smax: 5323fe6060f1SDimitry Andric case Intrinsic::vector_reduce_smin: 5324fe6060f1SDimitry Andric case Intrinsic::vector_reduce_umax: 5325fe6060f1SDimitry Andric case Intrinsic::vector_reduce_umin: { 5326fe6060f1SDimitry Andric Type *ArgTy = Call.getArgOperand(0)->getType(); 5327*81ad6265SDimitry Andric Check(ArgTy->isIntOrIntVectorTy() && ArgTy->isVectorTy(), 5328fe6060f1SDimitry Andric "Intrinsic has incorrect argument type!"); 5329fe6060f1SDimitry Andric break; 5330fe6060f1SDimitry Andric } 5331fe6060f1SDimitry Andric case Intrinsic::vector_reduce_fmax: 5332fe6060f1SDimitry Andric case Intrinsic::vector_reduce_fmin: { 5333fe6060f1SDimitry Andric Type *ArgTy = Call.getArgOperand(0)->getType(); 5334*81ad6265SDimitry Andric Check(ArgTy->isFPOrFPVectorTy() && ArgTy->isVectorTy(), 5335fe6060f1SDimitry Andric "Intrinsic has incorrect argument type!"); 5336fe6060f1SDimitry Andric break; 5337fe6060f1SDimitry Andric } 5338fe6060f1SDimitry Andric case Intrinsic::vector_reduce_fadd: 5339fe6060f1SDimitry Andric case Intrinsic::vector_reduce_fmul: { 5340fe6060f1SDimitry Andric // Unlike the other reductions, the first argument is a start value. The 5341fe6060f1SDimitry Andric // second argument is the vector to be reduced. 5342fe6060f1SDimitry Andric Type *ArgTy = Call.getArgOperand(1)->getType(); 5343*81ad6265SDimitry Andric Check(ArgTy->isFPOrFPVectorTy() && ArgTy->isVectorTy(), 5344fe6060f1SDimitry Andric "Intrinsic has incorrect argument type!"); 53450b57cec5SDimitry Andric break; 53460b57cec5SDimitry Andric } 53470b57cec5SDimitry Andric case Intrinsic::smul_fix: 53480b57cec5SDimitry Andric case Intrinsic::smul_fix_sat: 53498bcb0991SDimitry Andric case Intrinsic::umul_fix: 5350480093f4SDimitry Andric case Intrinsic::umul_fix_sat: 5351480093f4SDimitry Andric case Intrinsic::sdiv_fix: 53525ffd83dbSDimitry Andric case Intrinsic::sdiv_fix_sat: 53535ffd83dbSDimitry Andric case Intrinsic::udiv_fix: 53545ffd83dbSDimitry Andric case Intrinsic::udiv_fix_sat: { 53550b57cec5SDimitry Andric Value *Op1 = Call.getArgOperand(0); 53560b57cec5SDimitry Andric Value *Op2 = Call.getArgOperand(1); 5357*81ad6265SDimitry Andric Check(Op1->getType()->isIntOrIntVectorTy(), 5358480093f4SDimitry Andric "first operand of [us][mul|div]_fix[_sat] must be an int type or " 5359480093f4SDimitry Andric "vector of ints"); 5360*81ad6265SDimitry Andric Check(Op2->getType()->isIntOrIntVectorTy(), 5361480093f4SDimitry Andric "second operand of [us][mul|div]_fix[_sat] must be an int type or " 5362480093f4SDimitry Andric "vector of ints"); 53630b57cec5SDimitry Andric 53640b57cec5SDimitry Andric auto *Op3 = cast<ConstantInt>(Call.getArgOperand(2)); 5365*81ad6265SDimitry Andric Check(Op3->getType()->getBitWidth() <= 32, 5366480093f4SDimitry Andric "third argument of [us][mul|div]_fix[_sat] must fit within 32 bits"); 53670b57cec5SDimitry Andric 5368480093f4SDimitry Andric if (ID == Intrinsic::smul_fix || ID == Intrinsic::smul_fix_sat || 53695ffd83dbSDimitry Andric ID == Intrinsic::sdiv_fix || ID == Intrinsic::sdiv_fix_sat) { 5370*81ad6265SDimitry Andric Check(Op3->getZExtValue() < Op1->getType()->getScalarSizeInBits(), 5371480093f4SDimitry Andric "the scale of s[mul|div]_fix[_sat] must be less than the width of " 5372480093f4SDimitry Andric "the operands"); 53730b57cec5SDimitry Andric } else { 5374*81ad6265SDimitry Andric Check(Op3->getZExtValue() <= Op1->getType()->getScalarSizeInBits(), 5375480093f4SDimitry Andric "the scale of u[mul|div]_fix[_sat] must be less than or equal " 5376480093f4SDimitry Andric "to the width of the operands"); 53770b57cec5SDimitry Andric } 53780b57cec5SDimitry Andric break; 53790b57cec5SDimitry Andric } 53800b57cec5SDimitry Andric case Intrinsic::lround: 53810b57cec5SDimitry Andric case Intrinsic::llround: 53820b57cec5SDimitry Andric case Intrinsic::lrint: 53830b57cec5SDimitry Andric case Intrinsic::llrint: { 53840b57cec5SDimitry Andric Type *ValTy = Call.getArgOperand(0)->getType(); 53850b57cec5SDimitry Andric Type *ResultTy = Call.getType(); 5386*81ad6265SDimitry Andric Check(!ValTy->isVectorTy() && !ResultTy->isVectorTy(), 53870b57cec5SDimitry Andric "Intrinsic does not support vectors", &Call); 53880b57cec5SDimitry Andric break; 53890b57cec5SDimitry Andric } 53905ffd83dbSDimitry Andric case Intrinsic::bswap: { 53915ffd83dbSDimitry Andric Type *Ty = Call.getType(); 53925ffd83dbSDimitry Andric unsigned Size = Ty->getScalarSizeInBits(); 5393*81ad6265SDimitry Andric Check(Size % 16 == 0, "bswap must be an even number of bytes", &Call); 53945ffd83dbSDimitry Andric break; 53955ffd83dbSDimitry Andric } 5396e8d8bef9SDimitry Andric case Intrinsic::invariant_start: { 5397e8d8bef9SDimitry Andric ConstantInt *InvariantSize = dyn_cast<ConstantInt>(Call.getArgOperand(0)); 5398*81ad6265SDimitry Andric Check(InvariantSize && 5399e8d8bef9SDimitry Andric (!InvariantSize->isNegative() || InvariantSize->isMinusOne()), 5400e8d8bef9SDimitry Andric "invariant_start parameter must be -1, 0 or a positive number", 5401e8d8bef9SDimitry Andric &Call); 5402e8d8bef9SDimitry Andric break; 5403e8d8bef9SDimitry Andric } 54045ffd83dbSDimitry Andric case Intrinsic::matrix_multiply: 54055ffd83dbSDimitry Andric case Intrinsic::matrix_transpose: 54065ffd83dbSDimitry Andric case Intrinsic::matrix_column_major_load: 54075ffd83dbSDimitry Andric case Intrinsic::matrix_column_major_store: { 54085ffd83dbSDimitry Andric Function *IF = Call.getCalledFunction(); 54095ffd83dbSDimitry Andric ConstantInt *Stride = nullptr; 54105ffd83dbSDimitry Andric ConstantInt *NumRows; 54115ffd83dbSDimitry Andric ConstantInt *NumColumns; 54125ffd83dbSDimitry Andric VectorType *ResultTy; 54135ffd83dbSDimitry Andric Type *Op0ElemTy = nullptr; 54145ffd83dbSDimitry Andric Type *Op1ElemTy = nullptr; 54155ffd83dbSDimitry Andric switch (ID) { 54165ffd83dbSDimitry Andric case Intrinsic::matrix_multiply: 54175ffd83dbSDimitry Andric NumRows = cast<ConstantInt>(Call.getArgOperand(2)); 54185ffd83dbSDimitry Andric NumColumns = cast<ConstantInt>(Call.getArgOperand(4)); 54195ffd83dbSDimitry Andric ResultTy = cast<VectorType>(Call.getType()); 54205ffd83dbSDimitry Andric Op0ElemTy = 54215ffd83dbSDimitry Andric cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType(); 54225ffd83dbSDimitry Andric Op1ElemTy = 54235ffd83dbSDimitry Andric cast<VectorType>(Call.getArgOperand(1)->getType())->getElementType(); 54245ffd83dbSDimitry Andric break; 54255ffd83dbSDimitry Andric case Intrinsic::matrix_transpose: 54265ffd83dbSDimitry Andric NumRows = cast<ConstantInt>(Call.getArgOperand(1)); 54275ffd83dbSDimitry Andric NumColumns = cast<ConstantInt>(Call.getArgOperand(2)); 54285ffd83dbSDimitry Andric ResultTy = cast<VectorType>(Call.getType()); 54295ffd83dbSDimitry Andric Op0ElemTy = 54305ffd83dbSDimitry Andric cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType(); 54315ffd83dbSDimitry Andric break; 54324824e7fdSDimitry Andric case Intrinsic::matrix_column_major_load: { 54335ffd83dbSDimitry Andric Stride = dyn_cast<ConstantInt>(Call.getArgOperand(1)); 54345ffd83dbSDimitry Andric NumRows = cast<ConstantInt>(Call.getArgOperand(3)); 54355ffd83dbSDimitry Andric NumColumns = cast<ConstantInt>(Call.getArgOperand(4)); 54365ffd83dbSDimitry Andric ResultTy = cast<VectorType>(Call.getType()); 54374824e7fdSDimitry Andric 54384824e7fdSDimitry Andric PointerType *Op0PtrTy = 54394824e7fdSDimitry Andric cast<PointerType>(Call.getArgOperand(0)->getType()); 54404824e7fdSDimitry Andric if (!Op0PtrTy->isOpaque()) 544104eeddc0SDimitry Andric Op0ElemTy = Op0PtrTy->getNonOpaquePointerElementType(); 54425ffd83dbSDimitry Andric break; 54434824e7fdSDimitry Andric } 54444824e7fdSDimitry Andric case Intrinsic::matrix_column_major_store: { 54455ffd83dbSDimitry Andric Stride = dyn_cast<ConstantInt>(Call.getArgOperand(2)); 54465ffd83dbSDimitry Andric NumRows = cast<ConstantInt>(Call.getArgOperand(4)); 54475ffd83dbSDimitry Andric NumColumns = cast<ConstantInt>(Call.getArgOperand(5)); 54485ffd83dbSDimitry Andric ResultTy = cast<VectorType>(Call.getArgOperand(0)->getType()); 54495ffd83dbSDimitry Andric Op0ElemTy = 54505ffd83dbSDimitry Andric cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType(); 54514824e7fdSDimitry Andric 54524824e7fdSDimitry Andric PointerType *Op1PtrTy = 54534824e7fdSDimitry Andric cast<PointerType>(Call.getArgOperand(1)->getType()); 54544824e7fdSDimitry Andric if (!Op1PtrTy->isOpaque()) 545504eeddc0SDimitry Andric Op1ElemTy = Op1PtrTy->getNonOpaquePointerElementType(); 54565ffd83dbSDimitry Andric break; 54574824e7fdSDimitry Andric } 54585ffd83dbSDimitry Andric default: 54595ffd83dbSDimitry Andric llvm_unreachable("unexpected intrinsic"); 54605ffd83dbSDimitry Andric } 54615ffd83dbSDimitry Andric 5462*81ad6265SDimitry Andric Check(ResultTy->getElementType()->isIntegerTy() || 54635ffd83dbSDimitry Andric ResultTy->getElementType()->isFloatingPointTy(), 54645ffd83dbSDimitry Andric "Result type must be an integer or floating-point type!", IF); 54655ffd83dbSDimitry Andric 54664824e7fdSDimitry Andric if (Op0ElemTy) 5467*81ad6265SDimitry Andric Check(ResultTy->getElementType() == Op0ElemTy, 54685ffd83dbSDimitry Andric "Vector element type mismatch of the result and first operand " 5469*81ad6265SDimitry Andric "vector!", 5470*81ad6265SDimitry Andric IF); 54715ffd83dbSDimitry Andric 54725ffd83dbSDimitry Andric if (Op1ElemTy) 5473*81ad6265SDimitry Andric Check(ResultTy->getElementType() == Op1ElemTy, 54745ffd83dbSDimitry Andric "Vector element type mismatch of the result and second operand " 5475*81ad6265SDimitry Andric "vector!", 5476*81ad6265SDimitry Andric IF); 54775ffd83dbSDimitry Andric 5478*81ad6265SDimitry Andric Check(cast<FixedVectorType>(ResultTy)->getNumElements() == 54795ffd83dbSDimitry Andric NumRows->getZExtValue() * NumColumns->getZExtValue(), 54805ffd83dbSDimitry Andric "Result of a matrix operation does not fit in the returned vector!"); 54815ffd83dbSDimitry Andric 54825ffd83dbSDimitry Andric if (Stride) 5483*81ad6265SDimitry Andric Check(Stride->getZExtValue() >= NumRows->getZExtValue(), 54845ffd83dbSDimitry Andric "Stride must be greater or equal than the number of rows!", IF); 54855ffd83dbSDimitry Andric 54865ffd83dbSDimitry Andric break; 54875ffd83dbSDimitry Andric } 548804eeddc0SDimitry Andric case Intrinsic::experimental_vector_splice: { 548904eeddc0SDimitry Andric VectorType *VecTy = cast<VectorType>(Call.getType()); 549004eeddc0SDimitry Andric int64_t Idx = cast<ConstantInt>(Call.getArgOperand(2))->getSExtValue(); 549104eeddc0SDimitry Andric int64_t KnownMinNumElements = VecTy->getElementCount().getKnownMinValue(); 549204eeddc0SDimitry Andric if (Call.getParent() && Call.getParent()->getParent()) { 549304eeddc0SDimitry Andric AttributeList Attrs = Call.getParent()->getParent()->getAttributes(); 549404eeddc0SDimitry Andric if (Attrs.hasFnAttr(Attribute::VScaleRange)) 549504eeddc0SDimitry Andric KnownMinNumElements *= Attrs.getFnAttrs().getVScaleRangeMin(); 549604eeddc0SDimitry Andric } 5497*81ad6265SDimitry Andric Check((Idx < 0 && std::abs(Idx) <= KnownMinNumElements) || 549804eeddc0SDimitry Andric (Idx >= 0 && Idx < KnownMinNumElements), 549904eeddc0SDimitry Andric "The splice index exceeds the range [-VL, VL-1] where VL is the " 550004eeddc0SDimitry Andric "known minimum number of elements in the vector. For scalable " 550104eeddc0SDimitry Andric "vectors the minimum number of elements is determined from " 550204eeddc0SDimitry Andric "vscale_range.", 550304eeddc0SDimitry Andric &Call); 550404eeddc0SDimitry Andric break; 550504eeddc0SDimitry Andric } 5506fe6060f1SDimitry Andric case Intrinsic::experimental_stepvector: { 5507fe6060f1SDimitry Andric VectorType *VecTy = dyn_cast<VectorType>(Call.getType()); 5508*81ad6265SDimitry Andric Check(VecTy && VecTy->getScalarType()->isIntegerTy() && 5509fe6060f1SDimitry Andric VecTy->getScalarSizeInBits() >= 8, 5510fe6060f1SDimitry Andric "experimental_stepvector only supported for vectors of integers " 5511fe6060f1SDimitry Andric "with a bitwidth of at least 8.", 5512fe6060f1SDimitry Andric &Call); 5513fe6060f1SDimitry Andric break; 5514fe6060f1SDimitry Andric } 5515*81ad6265SDimitry Andric case Intrinsic::vector_insert: { 5516fe6060f1SDimitry Andric Value *Vec = Call.getArgOperand(0); 5517fe6060f1SDimitry Andric Value *SubVec = Call.getArgOperand(1); 5518fe6060f1SDimitry Andric Value *Idx = Call.getArgOperand(2); 5519fe6060f1SDimitry Andric unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue(); 5520e8d8bef9SDimitry Andric 5521fe6060f1SDimitry Andric VectorType *VecTy = cast<VectorType>(Vec->getType()); 5522fe6060f1SDimitry Andric VectorType *SubVecTy = cast<VectorType>(SubVec->getType()); 5523fe6060f1SDimitry Andric 5524fe6060f1SDimitry Andric ElementCount VecEC = VecTy->getElementCount(); 5525fe6060f1SDimitry Andric ElementCount SubVecEC = SubVecTy->getElementCount(); 5526*81ad6265SDimitry Andric Check(VecTy->getElementType() == SubVecTy->getElementType(), 5527*81ad6265SDimitry Andric "vector_insert parameters must have the same element " 5528e8d8bef9SDimitry Andric "type.", 5529e8d8bef9SDimitry Andric &Call); 5530*81ad6265SDimitry Andric Check(IdxN % SubVecEC.getKnownMinValue() == 0, 5531*81ad6265SDimitry Andric "vector_insert index must be a constant multiple of " 5532fe6060f1SDimitry Andric "the subvector's known minimum vector length."); 5533fe6060f1SDimitry Andric 5534fe6060f1SDimitry Andric // If this insertion is not the 'mixed' case where a fixed vector is 5535fe6060f1SDimitry Andric // inserted into a scalable vector, ensure that the insertion of the 5536fe6060f1SDimitry Andric // subvector does not overrun the parent vector. 5537fe6060f1SDimitry Andric if (VecEC.isScalable() == SubVecEC.isScalable()) { 5538*81ad6265SDimitry Andric Check(IdxN < VecEC.getKnownMinValue() && 5539fe6060f1SDimitry Andric IdxN + SubVecEC.getKnownMinValue() <= VecEC.getKnownMinValue(), 5540*81ad6265SDimitry Andric "subvector operand of vector_insert would overrun the " 5541fe6060f1SDimitry Andric "vector being inserted into."); 5542fe6060f1SDimitry Andric } 5543e8d8bef9SDimitry Andric break; 5544e8d8bef9SDimitry Andric } 5545*81ad6265SDimitry Andric case Intrinsic::vector_extract: { 5546fe6060f1SDimitry Andric Value *Vec = Call.getArgOperand(0); 5547fe6060f1SDimitry Andric Value *Idx = Call.getArgOperand(1); 5548fe6060f1SDimitry Andric unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue(); 5549fe6060f1SDimitry Andric 5550e8d8bef9SDimitry Andric VectorType *ResultTy = cast<VectorType>(Call.getType()); 5551fe6060f1SDimitry Andric VectorType *VecTy = cast<VectorType>(Vec->getType()); 5552fe6060f1SDimitry Andric 5553fe6060f1SDimitry Andric ElementCount VecEC = VecTy->getElementCount(); 5554fe6060f1SDimitry Andric ElementCount ResultEC = ResultTy->getElementCount(); 5555e8d8bef9SDimitry Andric 5556*81ad6265SDimitry Andric Check(ResultTy->getElementType() == VecTy->getElementType(), 5557*81ad6265SDimitry Andric "vector_extract result must have the same element " 5558e8d8bef9SDimitry Andric "type as the input vector.", 5559e8d8bef9SDimitry Andric &Call); 5560*81ad6265SDimitry Andric Check(IdxN % ResultEC.getKnownMinValue() == 0, 5561*81ad6265SDimitry Andric "vector_extract index must be a constant multiple of " 5562fe6060f1SDimitry Andric "the result type's known minimum vector length."); 5563fe6060f1SDimitry Andric 5564fe6060f1SDimitry Andric // If this extraction is not the 'mixed' case where a fixed vector is is 5565fe6060f1SDimitry Andric // extracted from a scalable vector, ensure that the extraction does not 5566fe6060f1SDimitry Andric // overrun the parent vector. 5567fe6060f1SDimitry Andric if (VecEC.isScalable() == ResultEC.isScalable()) { 5568*81ad6265SDimitry Andric Check(IdxN < VecEC.getKnownMinValue() && 5569fe6060f1SDimitry Andric IdxN + ResultEC.getKnownMinValue() <= VecEC.getKnownMinValue(), 5570*81ad6265SDimitry Andric "vector_extract would overrun."); 5571fe6060f1SDimitry Andric } 5572e8d8bef9SDimitry Andric break; 5573e8d8bef9SDimitry Andric } 5574e8d8bef9SDimitry Andric case Intrinsic::experimental_noalias_scope_decl: { 5575e8d8bef9SDimitry Andric NoAliasScopeDecls.push_back(cast<IntrinsicInst>(&Call)); 5576e8d8bef9SDimitry Andric break; 5577e8d8bef9SDimitry Andric } 5578fe6060f1SDimitry Andric case Intrinsic::preserve_array_access_index: 5579*81ad6265SDimitry Andric case Intrinsic::preserve_struct_access_index: 5580*81ad6265SDimitry Andric case Intrinsic::aarch64_ldaxr: 5581*81ad6265SDimitry Andric case Intrinsic::aarch64_ldxr: 5582*81ad6265SDimitry Andric case Intrinsic::arm_ldaex: 5583*81ad6265SDimitry Andric case Intrinsic::arm_ldrex: { 5584*81ad6265SDimitry Andric Type *ElemTy = Call.getParamElementType(0); 5585*81ad6265SDimitry Andric Check(ElemTy, "Intrinsic requires elementtype attribute on first argument.", 5586*81ad6265SDimitry Andric &Call); 5587*81ad6265SDimitry Andric break; 5588*81ad6265SDimitry Andric } 5589*81ad6265SDimitry Andric case Intrinsic::aarch64_stlxr: 5590*81ad6265SDimitry Andric case Intrinsic::aarch64_stxr: 5591*81ad6265SDimitry Andric case Intrinsic::arm_stlex: 5592*81ad6265SDimitry Andric case Intrinsic::arm_strex: { 5593*81ad6265SDimitry Andric Type *ElemTy = Call.getAttributes().getParamElementType(1); 5594*81ad6265SDimitry Andric Check(ElemTy, 5595*81ad6265SDimitry Andric "Intrinsic requires elementtype attribute on second argument.", 5596fe6060f1SDimitry Andric &Call); 5597fe6060f1SDimitry Andric break; 5598fe6060f1SDimitry Andric } 55990b57cec5SDimitry Andric }; 56000b57cec5SDimitry Andric } 56010b57cec5SDimitry Andric 56020b57cec5SDimitry Andric /// Carefully grab the subprogram from a local scope. 56030b57cec5SDimitry Andric /// 56040b57cec5SDimitry Andric /// This carefully grabs the subprogram from a local scope, avoiding the 56050b57cec5SDimitry Andric /// built-in assertions that would typically fire. 56060b57cec5SDimitry Andric static DISubprogram *getSubprogram(Metadata *LocalScope) { 56070b57cec5SDimitry Andric if (!LocalScope) 56080b57cec5SDimitry Andric return nullptr; 56090b57cec5SDimitry Andric 56100b57cec5SDimitry Andric if (auto *SP = dyn_cast<DISubprogram>(LocalScope)) 56110b57cec5SDimitry Andric return SP; 56120b57cec5SDimitry Andric 56130b57cec5SDimitry Andric if (auto *LB = dyn_cast<DILexicalBlockBase>(LocalScope)) 56140b57cec5SDimitry Andric return getSubprogram(LB->getRawScope()); 56150b57cec5SDimitry Andric 56160b57cec5SDimitry Andric // Just return null; broken scope chains are checked elsewhere. 56170b57cec5SDimitry Andric assert(!isa<DILocalScope>(LocalScope) && "Unknown type of local scope"); 56180b57cec5SDimitry Andric return nullptr; 56190b57cec5SDimitry Andric } 56200b57cec5SDimitry Andric 5621*81ad6265SDimitry Andric void Verifier::visitVPIntrinsic(VPIntrinsic &VPI) { 5622*81ad6265SDimitry Andric if (auto *VPCast = dyn_cast<VPCastIntrinsic>(&VPI)) { 5623*81ad6265SDimitry Andric auto *RetTy = cast<VectorType>(VPCast->getType()); 5624*81ad6265SDimitry Andric auto *ValTy = cast<VectorType>(VPCast->getOperand(0)->getType()); 5625*81ad6265SDimitry Andric Check(RetTy->getElementCount() == ValTy->getElementCount(), 5626*81ad6265SDimitry Andric "VP cast intrinsic first argument and result vector lengths must be " 5627*81ad6265SDimitry Andric "equal", 5628*81ad6265SDimitry Andric *VPCast); 5629*81ad6265SDimitry Andric 5630*81ad6265SDimitry Andric switch (VPCast->getIntrinsicID()) { 5631*81ad6265SDimitry Andric default: 5632*81ad6265SDimitry Andric llvm_unreachable("Unknown VP cast intrinsic"); 5633*81ad6265SDimitry Andric case Intrinsic::vp_trunc: 5634*81ad6265SDimitry Andric Check(RetTy->isIntOrIntVectorTy() && ValTy->isIntOrIntVectorTy(), 5635*81ad6265SDimitry Andric "llvm.vp.trunc intrinsic first argument and result element type " 5636*81ad6265SDimitry Andric "must be integer", 5637*81ad6265SDimitry Andric *VPCast); 5638*81ad6265SDimitry Andric Check(RetTy->getScalarSizeInBits() < ValTy->getScalarSizeInBits(), 5639*81ad6265SDimitry Andric "llvm.vp.trunc intrinsic the bit size of first argument must be " 5640*81ad6265SDimitry Andric "larger than the bit size of the return type", 5641*81ad6265SDimitry Andric *VPCast); 5642*81ad6265SDimitry Andric break; 5643*81ad6265SDimitry Andric case Intrinsic::vp_zext: 5644*81ad6265SDimitry Andric case Intrinsic::vp_sext: 5645*81ad6265SDimitry Andric Check(RetTy->isIntOrIntVectorTy() && ValTy->isIntOrIntVectorTy(), 5646*81ad6265SDimitry Andric "llvm.vp.zext or llvm.vp.sext intrinsic first argument and result " 5647*81ad6265SDimitry Andric "element type must be integer", 5648*81ad6265SDimitry Andric *VPCast); 5649*81ad6265SDimitry Andric Check(RetTy->getScalarSizeInBits() > ValTy->getScalarSizeInBits(), 5650*81ad6265SDimitry Andric "llvm.vp.zext or llvm.vp.sext intrinsic the bit size of first " 5651*81ad6265SDimitry Andric "argument must be smaller than the bit size of the return type", 5652*81ad6265SDimitry Andric *VPCast); 5653*81ad6265SDimitry Andric break; 5654*81ad6265SDimitry Andric case Intrinsic::vp_fptoui: 5655*81ad6265SDimitry Andric case Intrinsic::vp_fptosi: 5656*81ad6265SDimitry Andric Check( 5657*81ad6265SDimitry Andric RetTy->isIntOrIntVectorTy() && ValTy->isFPOrFPVectorTy(), 5658*81ad6265SDimitry Andric "llvm.vp.fptoui or llvm.vp.fptosi intrinsic first argument element " 5659*81ad6265SDimitry Andric "type must be floating-point and result element type must be integer", 5660*81ad6265SDimitry Andric *VPCast); 5661*81ad6265SDimitry Andric break; 5662*81ad6265SDimitry Andric case Intrinsic::vp_uitofp: 5663*81ad6265SDimitry Andric case Intrinsic::vp_sitofp: 5664*81ad6265SDimitry Andric Check( 5665*81ad6265SDimitry Andric RetTy->isFPOrFPVectorTy() && ValTy->isIntOrIntVectorTy(), 5666*81ad6265SDimitry Andric "llvm.vp.uitofp or llvm.vp.sitofp intrinsic first argument element " 5667*81ad6265SDimitry Andric "type must be integer and result element type must be floating-point", 5668*81ad6265SDimitry Andric *VPCast); 5669*81ad6265SDimitry Andric break; 5670*81ad6265SDimitry Andric case Intrinsic::vp_fptrunc: 5671*81ad6265SDimitry Andric Check(RetTy->isFPOrFPVectorTy() && ValTy->isFPOrFPVectorTy(), 5672*81ad6265SDimitry Andric "llvm.vp.fptrunc intrinsic first argument and result element type " 5673*81ad6265SDimitry Andric "must be floating-point", 5674*81ad6265SDimitry Andric *VPCast); 5675*81ad6265SDimitry Andric Check(RetTy->getScalarSizeInBits() < ValTy->getScalarSizeInBits(), 5676*81ad6265SDimitry Andric "llvm.vp.fptrunc intrinsic the bit size of first argument must be " 5677*81ad6265SDimitry Andric "larger than the bit size of the return type", 5678*81ad6265SDimitry Andric *VPCast); 5679*81ad6265SDimitry Andric break; 5680*81ad6265SDimitry Andric case Intrinsic::vp_fpext: 5681*81ad6265SDimitry Andric Check(RetTy->isFPOrFPVectorTy() && ValTy->isFPOrFPVectorTy(), 5682*81ad6265SDimitry Andric "llvm.vp.fpext intrinsic first argument and result element type " 5683*81ad6265SDimitry Andric "must be floating-point", 5684*81ad6265SDimitry Andric *VPCast); 5685*81ad6265SDimitry Andric Check(RetTy->getScalarSizeInBits() > ValTy->getScalarSizeInBits(), 5686*81ad6265SDimitry Andric "llvm.vp.fpext intrinsic the bit size of first argument must be " 5687*81ad6265SDimitry Andric "smaller than the bit size of the return type", 5688*81ad6265SDimitry Andric *VPCast); 5689*81ad6265SDimitry Andric break; 5690*81ad6265SDimitry Andric case Intrinsic::vp_ptrtoint: 5691*81ad6265SDimitry Andric Check(RetTy->isIntOrIntVectorTy() && ValTy->isPtrOrPtrVectorTy(), 5692*81ad6265SDimitry Andric "llvm.vp.ptrtoint intrinsic first argument element type must be " 5693*81ad6265SDimitry Andric "pointer and result element type must be integer", 5694*81ad6265SDimitry Andric *VPCast); 5695*81ad6265SDimitry Andric break; 5696*81ad6265SDimitry Andric case Intrinsic::vp_inttoptr: 5697*81ad6265SDimitry Andric Check(RetTy->isPtrOrPtrVectorTy() && ValTy->isIntOrIntVectorTy(), 5698*81ad6265SDimitry Andric "llvm.vp.inttoptr intrinsic first argument element type must be " 5699*81ad6265SDimitry Andric "integer and result element type must be pointer", 5700*81ad6265SDimitry Andric *VPCast); 5701*81ad6265SDimitry Andric break; 5702*81ad6265SDimitry Andric } 5703*81ad6265SDimitry Andric } 5704*81ad6265SDimitry Andric if (VPI.getIntrinsicID() == Intrinsic::vp_fcmp) { 5705*81ad6265SDimitry Andric auto Pred = cast<VPCmpIntrinsic>(&VPI)->getPredicate(); 5706*81ad6265SDimitry Andric Check(CmpInst::isFPPredicate(Pred), 5707*81ad6265SDimitry Andric "invalid predicate for VP FP comparison intrinsic", &VPI); 5708*81ad6265SDimitry Andric } 5709*81ad6265SDimitry Andric if (VPI.getIntrinsicID() == Intrinsic::vp_icmp) { 5710*81ad6265SDimitry Andric auto Pred = cast<VPCmpIntrinsic>(&VPI)->getPredicate(); 5711*81ad6265SDimitry Andric Check(CmpInst::isIntPredicate(Pred), 5712*81ad6265SDimitry Andric "invalid predicate for VP integer comparison intrinsic", &VPI); 5713*81ad6265SDimitry Andric } 5714*81ad6265SDimitry Andric } 5715*81ad6265SDimitry Andric 57160b57cec5SDimitry Andric void Verifier::visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI) { 5717480093f4SDimitry Andric unsigned NumOperands; 5718480093f4SDimitry Andric bool HasRoundingMD; 57190b57cec5SDimitry Andric switch (FPI.getIntrinsicID()) { 57205ffd83dbSDimitry Andric #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \ 5721480093f4SDimitry Andric case Intrinsic::INTRINSIC: \ 5722480093f4SDimitry Andric NumOperands = NARG; \ 5723480093f4SDimitry Andric HasRoundingMD = ROUND_MODE; \ 57240b57cec5SDimitry Andric break; 5725480093f4SDimitry Andric #include "llvm/IR/ConstrainedOps.def" 5726480093f4SDimitry Andric default: 5727480093f4SDimitry Andric llvm_unreachable("Invalid constrained FP intrinsic!"); 5728480093f4SDimitry Andric } 5729480093f4SDimitry Andric NumOperands += (1 + HasRoundingMD); 5730480093f4SDimitry Andric // Compare intrinsics carry an extra predicate metadata operand. 5731480093f4SDimitry Andric if (isa<ConstrainedFPCmpIntrinsic>(FPI)) 5732480093f4SDimitry Andric NumOperands += 1; 5733*81ad6265SDimitry Andric Check((FPI.arg_size() == NumOperands), 5734480093f4SDimitry Andric "invalid arguments for constrained FP intrinsic", &FPI); 57350b57cec5SDimitry Andric 5736480093f4SDimitry Andric switch (FPI.getIntrinsicID()) { 57378bcb0991SDimitry Andric case Intrinsic::experimental_constrained_lrint: 57388bcb0991SDimitry Andric case Intrinsic::experimental_constrained_llrint: { 57398bcb0991SDimitry Andric Type *ValTy = FPI.getArgOperand(0)->getType(); 57408bcb0991SDimitry Andric Type *ResultTy = FPI.getType(); 5741*81ad6265SDimitry Andric Check(!ValTy->isVectorTy() && !ResultTy->isVectorTy(), 57428bcb0991SDimitry Andric "Intrinsic does not support vectors", &FPI); 57438bcb0991SDimitry Andric } 57448bcb0991SDimitry Andric break; 57458bcb0991SDimitry Andric 57468bcb0991SDimitry Andric case Intrinsic::experimental_constrained_lround: 57478bcb0991SDimitry Andric case Intrinsic::experimental_constrained_llround: { 57488bcb0991SDimitry Andric Type *ValTy = FPI.getArgOperand(0)->getType(); 57498bcb0991SDimitry Andric Type *ResultTy = FPI.getType(); 5750*81ad6265SDimitry Andric Check(!ValTy->isVectorTy() && !ResultTy->isVectorTy(), 57518bcb0991SDimitry Andric "Intrinsic does not support vectors", &FPI); 57528bcb0991SDimitry Andric break; 57538bcb0991SDimitry Andric } 57548bcb0991SDimitry Andric 5755480093f4SDimitry Andric case Intrinsic::experimental_constrained_fcmp: 5756480093f4SDimitry Andric case Intrinsic::experimental_constrained_fcmps: { 5757480093f4SDimitry Andric auto Pred = cast<ConstrainedFPCmpIntrinsic>(&FPI)->getPredicate(); 5758*81ad6265SDimitry Andric Check(CmpInst::isFPPredicate(Pred), 5759480093f4SDimitry Andric "invalid predicate for constrained FP comparison intrinsic", &FPI); 57600b57cec5SDimitry Andric break; 5761480093f4SDimitry Andric } 57620b57cec5SDimitry Andric 57638bcb0991SDimitry Andric case Intrinsic::experimental_constrained_fptosi: 57648bcb0991SDimitry Andric case Intrinsic::experimental_constrained_fptoui: { 57658bcb0991SDimitry Andric Value *Operand = FPI.getArgOperand(0); 57668bcb0991SDimitry Andric uint64_t NumSrcElem = 0; 5767*81ad6265SDimitry Andric Check(Operand->getType()->isFPOrFPVectorTy(), 57688bcb0991SDimitry Andric "Intrinsic first argument must be floating point", &FPI); 57698bcb0991SDimitry Andric if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) { 5770e8d8bef9SDimitry Andric NumSrcElem = cast<FixedVectorType>(OperandT)->getNumElements(); 57718bcb0991SDimitry Andric } 57728bcb0991SDimitry Andric 57738bcb0991SDimitry Andric Operand = &FPI; 5774*81ad6265SDimitry Andric Check((NumSrcElem > 0) == Operand->getType()->isVectorTy(), 57758bcb0991SDimitry Andric "Intrinsic first argument and result disagree on vector use", &FPI); 5776*81ad6265SDimitry Andric Check(Operand->getType()->isIntOrIntVectorTy(), 57778bcb0991SDimitry Andric "Intrinsic result must be an integer", &FPI); 57788bcb0991SDimitry Andric if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) { 5779*81ad6265SDimitry Andric Check(NumSrcElem == cast<FixedVectorType>(OperandT)->getNumElements(), 57808bcb0991SDimitry Andric "Intrinsic first argument and result vector lengths must be equal", 57818bcb0991SDimitry Andric &FPI); 57828bcb0991SDimitry Andric } 57838bcb0991SDimitry Andric } 57848bcb0991SDimitry Andric break; 57858bcb0991SDimitry Andric 5786480093f4SDimitry Andric case Intrinsic::experimental_constrained_sitofp: 5787480093f4SDimitry Andric case Intrinsic::experimental_constrained_uitofp: { 5788480093f4SDimitry Andric Value *Operand = FPI.getArgOperand(0); 5789480093f4SDimitry Andric uint64_t NumSrcElem = 0; 5790*81ad6265SDimitry Andric Check(Operand->getType()->isIntOrIntVectorTy(), 5791480093f4SDimitry Andric "Intrinsic first argument must be integer", &FPI); 5792480093f4SDimitry Andric if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) { 5793e8d8bef9SDimitry Andric NumSrcElem = cast<FixedVectorType>(OperandT)->getNumElements(); 5794480093f4SDimitry Andric } 5795480093f4SDimitry Andric 5796480093f4SDimitry Andric Operand = &FPI; 5797*81ad6265SDimitry Andric Check((NumSrcElem > 0) == Operand->getType()->isVectorTy(), 5798480093f4SDimitry Andric "Intrinsic first argument and result disagree on vector use", &FPI); 5799*81ad6265SDimitry Andric Check(Operand->getType()->isFPOrFPVectorTy(), 5800480093f4SDimitry Andric "Intrinsic result must be a floating point", &FPI); 5801480093f4SDimitry Andric if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) { 5802*81ad6265SDimitry Andric Check(NumSrcElem == cast<FixedVectorType>(OperandT)->getNumElements(), 5803480093f4SDimitry Andric "Intrinsic first argument and result vector lengths must be equal", 5804480093f4SDimitry Andric &FPI); 5805480093f4SDimitry Andric } 5806480093f4SDimitry Andric } break; 5807480093f4SDimitry Andric 58080b57cec5SDimitry Andric case Intrinsic::experimental_constrained_fptrunc: 58090b57cec5SDimitry Andric case Intrinsic::experimental_constrained_fpext: { 58100b57cec5SDimitry Andric Value *Operand = FPI.getArgOperand(0); 58110b57cec5SDimitry Andric Type *OperandTy = Operand->getType(); 58120b57cec5SDimitry Andric Value *Result = &FPI; 58130b57cec5SDimitry Andric Type *ResultTy = Result->getType(); 5814*81ad6265SDimitry Andric Check(OperandTy->isFPOrFPVectorTy(), 58150b57cec5SDimitry Andric "Intrinsic first argument must be FP or FP vector", &FPI); 5816*81ad6265SDimitry Andric Check(ResultTy->isFPOrFPVectorTy(), 58170b57cec5SDimitry Andric "Intrinsic result must be FP or FP vector", &FPI); 5818*81ad6265SDimitry Andric Check(OperandTy->isVectorTy() == ResultTy->isVectorTy(), 58190b57cec5SDimitry Andric "Intrinsic first argument and result disagree on vector use", &FPI); 58200b57cec5SDimitry Andric if (OperandTy->isVectorTy()) { 5821*81ad6265SDimitry Andric Check(cast<FixedVectorType>(OperandTy)->getNumElements() == 5822e8d8bef9SDimitry Andric cast<FixedVectorType>(ResultTy)->getNumElements(), 58230b57cec5SDimitry Andric "Intrinsic first argument and result vector lengths must be equal", 58240b57cec5SDimitry Andric &FPI); 58250b57cec5SDimitry Andric } 58260b57cec5SDimitry Andric if (FPI.getIntrinsicID() == Intrinsic::experimental_constrained_fptrunc) { 5827*81ad6265SDimitry Andric Check(OperandTy->getScalarSizeInBits() > ResultTy->getScalarSizeInBits(), 58280b57cec5SDimitry Andric "Intrinsic first argument's type must be larger than result type", 58290b57cec5SDimitry Andric &FPI); 58300b57cec5SDimitry Andric } else { 5831*81ad6265SDimitry Andric Check(OperandTy->getScalarSizeInBits() < ResultTy->getScalarSizeInBits(), 58320b57cec5SDimitry Andric "Intrinsic first argument's type must be smaller than result type", 58330b57cec5SDimitry Andric &FPI); 58340b57cec5SDimitry Andric } 58350b57cec5SDimitry Andric } 58360b57cec5SDimitry Andric break; 58370b57cec5SDimitry Andric 58380b57cec5SDimitry Andric default: 5839480093f4SDimitry Andric break; 58400b57cec5SDimitry Andric } 58410b57cec5SDimitry Andric 58420b57cec5SDimitry Andric // If a non-metadata argument is passed in a metadata slot then the 58430b57cec5SDimitry Andric // error will be caught earlier when the incorrect argument doesn't 58440b57cec5SDimitry Andric // match the specification in the intrinsic call table. Thus, no 58450b57cec5SDimitry Andric // argument type check is needed here. 58460b57cec5SDimitry Andric 5847*81ad6265SDimitry Andric Check(FPI.getExceptionBehavior().has_value(), 58480b57cec5SDimitry Andric "invalid exception behavior argument", &FPI); 58490b57cec5SDimitry Andric if (HasRoundingMD) { 5850*81ad6265SDimitry Andric Check(FPI.getRoundingMode().has_value(), "invalid rounding mode argument", 5851*81ad6265SDimitry Andric &FPI); 58520b57cec5SDimitry Andric } 58530b57cec5SDimitry Andric } 58540b57cec5SDimitry Andric 58550b57cec5SDimitry Andric void Verifier::visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII) { 5856fe6060f1SDimitry Andric auto *MD = DII.getRawLocation(); 5857*81ad6265SDimitry Andric CheckDI(isa<ValueAsMetadata>(MD) || isa<DIArgList>(MD) || 58580b57cec5SDimitry Andric (isa<MDNode>(MD) && !cast<MDNode>(MD)->getNumOperands()), 58590b57cec5SDimitry Andric "invalid llvm.dbg." + Kind + " intrinsic address/value", &DII, MD); 5860*81ad6265SDimitry Andric CheckDI(isa<DILocalVariable>(DII.getRawVariable()), 58610b57cec5SDimitry Andric "invalid llvm.dbg." + Kind + " intrinsic variable", &DII, 58620b57cec5SDimitry Andric DII.getRawVariable()); 5863*81ad6265SDimitry Andric CheckDI(isa<DIExpression>(DII.getRawExpression()), 58640b57cec5SDimitry Andric "invalid llvm.dbg." + Kind + " intrinsic expression", &DII, 58650b57cec5SDimitry Andric DII.getRawExpression()); 58660b57cec5SDimitry Andric 58670b57cec5SDimitry Andric // Ignore broken !dbg attachments; they're checked elsewhere. 58680b57cec5SDimitry Andric if (MDNode *N = DII.getDebugLoc().getAsMDNode()) 58690b57cec5SDimitry Andric if (!isa<DILocation>(N)) 58700b57cec5SDimitry Andric return; 58710b57cec5SDimitry Andric 58720b57cec5SDimitry Andric BasicBlock *BB = DII.getParent(); 58730b57cec5SDimitry Andric Function *F = BB ? BB->getParent() : nullptr; 58740b57cec5SDimitry Andric 58750b57cec5SDimitry Andric // The scopes for variables and !dbg attachments must agree. 58760b57cec5SDimitry Andric DILocalVariable *Var = DII.getVariable(); 58770b57cec5SDimitry Andric DILocation *Loc = DII.getDebugLoc(); 5878*81ad6265SDimitry Andric CheckDI(Loc, "llvm.dbg." + Kind + " intrinsic requires a !dbg attachment", 58790b57cec5SDimitry Andric &DII, BB, F); 58800b57cec5SDimitry Andric 58810b57cec5SDimitry Andric DISubprogram *VarSP = getSubprogram(Var->getRawScope()); 58820b57cec5SDimitry Andric DISubprogram *LocSP = getSubprogram(Loc->getRawScope()); 58830b57cec5SDimitry Andric if (!VarSP || !LocSP) 58840b57cec5SDimitry Andric return; // Broken scope chains are checked elsewhere. 58850b57cec5SDimitry Andric 5886*81ad6265SDimitry Andric CheckDI(VarSP == LocSP, 5887*81ad6265SDimitry Andric "mismatched subprogram between llvm.dbg." + Kind + 58880b57cec5SDimitry Andric " variable and !dbg attachment", 58890b57cec5SDimitry Andric &DII, BB, F, Var, Var->getScope()->getSubprogram(), Loc, 58900b57cec5SDimitry Andric Loc->getScope()->getSubprogram()); 58910b57cec5SDimitry Andric 58920b57cec5SDimitry Andric // This check is redundant with one in visitLocalVariable(). 5893*81ad6265SDimitry Andric CheckDI(isType(Var->getRawType()), "invalid type ref", Var, 58940b57cec5SDimitry Andric Var->getRawType()); 58950b57cec5SDimitry Andric verifyFnArgs(DII); 58960b57cec5SDimitry Andric } 58970b57cec5SDimitry Andric 58980b57cec5SDimitry Andric void Verifier::visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI) { 5899*81ad6265SDimitry Andric CheckDI(isa<DILabel>(DLI.getRawLabel()), 59000b57cec5SDimitry Andric "invalid llvm.dbg." + Kind + " intrinsic variable", &DLI, 59010b57cec5SDimitry Andric DLI.getRawLabel()); 59020b57cec5SDimitry Andric 59030b57cec5SDimitry Andric // Ignore broken !dbg attachments; they're checked elsewhere. 59040b57cec5SDimitry Andric if (MDNode *N = DLI.getDebugLoc().getAsMDNode()) 59050b57cec5SDimitry Andric if (!isa<DILocation>(N)) 59060b57cec5SDimitry Andric return; 59070b57cec5SDimitry Andric 59080b57cec5SDimitry Andric BasicBlock *BB = DLI.getParent(); 59090b57cec5SDimitry Andric Function *F = BB ? BB->getParent() : nullptr; 59100b57cec5SDimitry Andric 59110b57cec5SDimitry Andric // The scopes for variables and !dbg attachments must agree. 59120b57cec5SDimitry Andric DILabel *Label = DLI.getLabel(); 59130b57cec5SDimitry Andric DILocation *Loc = DLI.getDebugLoc(); 5914*81ad6265SDimitry Andric Check(Loc, "llvm.dbg." + Kind + " intrinsic requires a !dbg attachment", &DLI, 5915*81ad6265SDimitry Andric BB, F); 59160b57cec5SDimitry Andric 59170b57cec5SDimitry Andric DISubprogram *LabelSP = getSubprogram(Label->getRawScope()); 59180b57cec5SDimitry Andric DISubprogram *LocSP = getSubprogram(Loc->getRawScope()); 59190b57cec5SDimitry Andric if (!LabelSP || !LocSP) 59200b57cec5SDimitry Andric return; 59210b57cec5SDimitry Andric 5922*81ad6265SDimitry Andric CheckDI(LabelSP == LocSP, 5923*81ad6265SDimitry Andric "mismatched subprogram between llvm.dbg." + Kind + 59240b57cec5SDimitry Andric " label and !dbg attachment", 59250b57cec5SDimitry Andric &DLI, BB, F, Label, Label->getScope()->getSubprogram(), Loc, 59260b57cec5SDimitry Andric Loc->getScope()->getSubprogram()); 59270b57cec5SDimitry Andric } 59280b57cec5SDimitry Andric 59290b57cec5SDimitry Andric void Verifier::verifyFragmentExpression(const DbgVariableIntrinsic &I) { 59300b57cec5SDimitry Andric DILocalVariable *V = dyn_cast_or_null<DILocalVariable>(I.getRawVariable()); 59310b57cec5SDimitry Andric DIExpression *E = dyn_cast_or_null<DIExpression>(I.getRawExpression()); 59320b57cec5SDimitry Andric 59330b57cec5SDimitry Andric // We don't know whether this intrinsic verified correctly. 59340b57cec5SDimitry Andric if (!V || !E || !E->isValid()) 59350b57cec5SDimitry Andric return; 59360b57cec5SDimitry Andric 59370b57cec5SDimitry Andric // Nothing to do if this isn't a DW_OP_LLVM_fragment expression. 59380b57cec5SDimitry Andric auto Fragment = E->getFragmentInfo(); 59390b57cec5SDimitry Andric if (!Fragment) 59400b57cec5SDimitry Andric return; 59410b57cec5SDimitry Andric 59420b57cec5SDimitry Andric // The frontend helps out GDB by emitting the members of local anonymous 59430b57cec5SDimitry Andric // unions as artificial local variables with shared storage. When SROA splits 59440b57cec5SDimitry Andric // the storage for artificial local variables that are smaller than the entire 59450b57cec5SDimitry Andric // union, the overhang piece will be outside of the allotted space for the 59460b57cec5SDimitry Andric // variable and this check fails. 59470b57cec5SDimitry Andric // FIXME: Remove this check as soon as clang stops doing this; it hides bugs. 59480b57cec5SDimitry Andric if (V->isArtificial()) 59490b57cec5SDimitry Andric return; 59500b57cec5SDimitry Andric 59510b57cec5SDimitry Andric verifyFragmentExpression(*V, *Fragment, &I); 59520b57cec5SDimitry Andric } 59530b57cec5SDimitry Andric 59540b57cec5SDimitry Andric template <typename ValueOrMetadata> 59550b57cec5SDimitry Andric void Verifier::verifyFragmentExpression(const DIVariable &V, 59560b57cec5SDimitry Andric DIExpression::FragmentInfo Fragment, 59570b57cec5SDimitry Andric ValueOrMetadata *Desc) { 59580b57cec5SDimitry Andric // If there's no size, the type is broken, but that should be checked 59590b57cec5SDimitry Andric // elsewhere. 59600b57cec5SDimitry Andric auto VarSize = V.getSizeInBits(); 59610b57cec5SDimitry Andric if (!VarSize) 59620b57cec5SDimitry Andric return; 59630b57cec5SDimitry Andric 59640b57cec5SDimitry Andric unsigned FragSize = Fragment.SizeInBits; 59650b57cec5SDimitry Andric unsigned FragOffset = Fragment.OffsetInBits; 5966*81ad6265SDimitry Andric CheckDI(FragSize + FragOffset <= *VarSize, 59670b57cec5SDimitry Andric "fragment is larger than or outside of variable", Desc, &V); 5968*81ad6265SDimitry Andric CheckDI(FragSize != *VarSize, "fragment covers entire variable", Desc, &V); 59690b57cec5SDimitry Andric } 59700b57cec5SDimitry Andric 59710b57cec5SDimitry Andric void Verifier::verifyFnArgs(const DbgVariableIntrinsic &I) { 59720b57cec5SDimitry Andric // This function does not take the scope of noninlined function arguments into 59730b57cec5SDimitry Andric // account. Don't run it if current function is nodebug, because it may 59740b57cec5SDimitry Andric // contain inlined debug intrinsics. 59750b57cec5SDimitry Andric if (!HasDebugInfo) 59760b57cec5SDimitry Andric return; 59770b57cec5SDimitry Andric 59780b57cec5SDimitry Andric // For performance reasons only check non-inlined ones. 59790b57cec5SDimitry Andric if (I.getDebugLoc()->getInlinedAt()) 59800b57cec5SDimitry Andric return; 59810b57cec5SDimitry Andric 59820b57cec5SDimitry Andric DILocalVariable *Var = I.getVariable(); 5983*81ad6265SDimitry Andric CheckDI(Var, "dbg intrinsic without variable"); 59840b57cec5SDimitry Andric 59850b57cec5SDimitry Andric unsigned ArgNo = Var->getArg(); 59860b57cec5SDimitry Andric if (!ArgNo) 59870b57cec5SDimitry Andric return; 59880b57cec5SDimitry Andric 59890b57cec5SDimitry Andric // Verify there are no duplicate function argument debug info entries. 59900b57cec5SDimitry Andric // These will cause hard-to-debug assertions in the DWARF backend. 59910b57cec5SDimitry Andric if (DebugFnArgs.size() < ArgNo) 59920b57cec5SDimitry Andric DebugFnArgs.resize(ArgNo, nullptr); 59930b57cec5SDimitry Andric 59940b57cec5SDimitry Andric auto *Prev = DebugFnArgs[ArgNo - 1]; 59950b57cec5SDimitry Andric DebugFnArgs[ArgNo - 1] = Var; 5996*81ad6265SDimitry Andric CheckDI(!Prev || (Prev == Var), "conflicting debug info for argument", &I, 59970b57cec5SDimitry Andric Prev, Var); 59980b57cec5SDimitry Andric } 59990b57cec5SDimitry Andric 60008bcb0991SDimitry Andric void Verifier::verifyNotEntryValue(const DbgVariableIntrinsic &I) { 60018bcb0991SDimitry Andric DIExpression *E = dyn_cast_or_null<DIExpression>(I.getRawExpression()); 60028bcb0991SDimitry Andric 60038bcb0991SDimitry Andric // We don't know whether this intrinsic verified correctly. 60048bcb0991SDimitry Andric if (!E || !E->isValid()) 60058bcb0991SDimitry Andric return; 60068bcb0991SDimitry Andric 6007*81ad6265SDimitry Andric CheckDI(!E->isEntryValue(), "Entry values are only allowed in MIR", &I); 60088bcb0991SDimitry Andric } 60098bcb0991SDimitry Andric 60100b57cec5SDimitry Andric void Verifier::verifyCompileUnits() { 60110b57cec5SDimitry Andric // When more than one Module is imported into the same context, such as during 60120b57cec5SDimitry Andric // an LTO build before linking the modules, ODR type uniquing may cause types 60130b57cec5SDimitry Andric // to point to a different CU. This check does not make sense in this case. 60140b57cec5SDimitry Andric if (M.getContext().isODRUniquingDebugTypes()) 60150b57cec5SDimitry Andric return; 60160b57cec5SDimitry Andric auto *CUs = M.getNamedMetadata("llvm.dbg.cu"); 60170b57cec5SDimitry Andric SmallPtrSet<const Metadata *, 2> Listed; 60180b57cec5SDimitry Andric if (CUs) 60190b57cec5SDimitry Andric Listed.insert(CUs->op_begin(), CUs->op_end()); 60200b57cec5SDimitry Andric for (auto *CU : CUVisited) 6021*81ad6265SDimitry Andric CheckDI(Listed.count(CU), "DICompileUnit not listed in llvm.dbg.cu", CU); 60220b57cec5SDimitry Andric CUVisited.clear(); 60230b57cec5SDimitry Andric } 60240b57cec5SDimitry Andric 60250b57cec5SDimitry Andric void Verifier::verifyDeoptimizeCallingConvs() { 60260b57cec5SDimitry Andric if (DeoptimizeDeclarations.empty()) 60270b57cec5SDimitry Andric return; 60280b57cec5SDimitry Andric 60290b57cec5SDimitry Andric const Function *First = DeoptimizeDeclarations[0]; 60300b57cec5SDimitry Andric for (auto *F : makeArrayRef(DeoptimizeDeclarations).slice(1)) { 6031*81ad6265SDimitry Andric Check(First->getCallingConv() == F->getCallingConv(), 60320b57cec5SDimitry Andric "All llvm.experimental.deoptimize declarations must have the same " 60330b57cec5SDimitry Andric "calling convention", 60340b57cec5SDimitry Andric First, F); 60350b57cec5SDimitry Andric } 60360b57cec5SDimitry Andric } 60370b57cec5SDimitry Andric 6038349cc55cSDimitry Andric void Verifier::verifyAttachedCallBundle(const CallBase &Call, 6039349cc55cSDimitry Andric const OperandBundleUse &BU) { 6040349cc55cSDimitry Andric FunctionType *FTy = Call.getFunctionType(); 6041349cc55cSDimitry Andric 6042*81ad6265SDimitry Andric Check((FTy->getReturnType()->isPointerTy() || 6043349cc55cSDimitry Andric (Call.doesNotReturn() && FTy->getReturnType()->isVoidTy())), 6044349cc55cSDimitry Andric "a call with operand bundle \"clang.arc.attachedcall\" must call a " 6045349cc55cSDimitry Andric "function returning a pointer or a non-returning function that has a " 6046349cc55cSDimitry Andric "void return type", 6047349cc55cSDimitry Andric Call); 6048349cc55cSDimitry Andric 6049*81ad6265SDimitry Andric Check(BU.Inputs.size() == 1 && isa<Function>(BU.Inputs.front()), 60501fd87a68SDimitry Andric "operand bundle \"clang.arc.attachedcall\" requires one function as " 60511fd87a68SDimitry Andric "an argument", 6052349cc55cSDimitry Andric Call); 6053349cc55cSDimitry Andric 6054349cc55cSDimitry Andric auto *Fn = cast<Function>(BU.Inputs.front()); 6055349cc55cSDimitry Andric Intrinsic::ID IID = Fn->getIntrinsicID(); 6056349cc55cSDimitry Andric 6057349cc55cSDimitry Andric if (IID) { 6058*81ad6265SDimitry Andric Check((IID == Intrinsic::objc_retainAutoreleasedReturnValue || 6059349cc55cSDimitry Andric IID == Intrinsic::objc_unsafeClaimAutoreleasedReturnValue), 6060349cc55cSDimitry Andric "invalid function argument", Call); 6061349cc55cSDimitry Andric } else { 6062349cc55cSDimitry Andric StringRef FnName = Fn->getName(); 6063*81ad6265SDimitry Andric Check((FnName == "objc_retainAutoreleasedReturnValue" || 6064349cc55cSDimitry Andric FnName == "objc_unsafeClaimAutoreleasedReturnValue"), 6065349cc55cSDimitry Andric "invalid function argument", Call); 6066349cc55cSDimitry Andric } 6067349cc55cSDimitry Andric } 6068349cc55cSDimitry Andric 60690b57cec5SDimitry Andric void Verifier::verifySourceDebugInfo(const DICompileUnit &U, const DIFile &F) { 6070*81ad6265SDimitry Andric bool HasSource = F.getSource().has_value(); 60710b57cec5SDimitry Andric if (!HasSourceDebugInfo.count(&U)) 60720b57cec5SDimitry Andric HasSourceDebugInfo[&U] = HasSource; 6073*81ad6265SDimitry Andric CheckDI(HasSource == HasSourceDebugInfo[&U], 60740b57cec5SDimitry Andric "inconsistent use of embedded source"); 60750b57cec5SDimitry Andric } 60760b57cec5SDimitry Andric 6077e8d8bef9SDimitry Andric void Verifier::verifyNoAliasScopeDecl() { 6078e8d8bef9SDimitry Andric if (NoAliasScopeDecls.empty()) 6079e8d8bef9SDimitry Andric return; 6080e8d8bef9SDimitry Andric 6081e8d8bef9SDimitry Andric // only a single scope must be declared at a time. 6082e8d8bef9SDimitry Andric for (auto *II : NoAliasScopeDecls) { 6083e8d8bef9SDimitry Andric assert(II->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl && 6084e8d8bef9SDimitry Andric "Not a llvm.experimental.noalias.scope.decl ?"); 6085e8d8bef9SDimitry Andric const auto *ScopeListMV = dyn_cast<MetadataAsValue>( 6086e8d8bef9SDimitry Andric II->getOperand(Intrinsic::NoAliasScopeDeclScopeArg)); 6087*81ad6265SDimitry Andric Check(ScopeListMV != nullptr, 6088e8d8bef9SDimitry Andric "llvm.experimental.noalias.scope.decl must have a MetadataAsValue " 6089e8d8bef9SDimitry Andric "argument", 6090e8d8bef9SDimitry Andric II); 6091e8d8bef9SDimitry Andric 6092e8d8bef9SDimitry Andric const auto *ScopeListMD = dyn_cast<MDNode>(ScopeListMV->getMetadata()); 6093*81ad6265SDimitry Andric Check(ScopeListMD != nullptr, "!id.scope.list must point to an MDNode", II); 6094*81ad6265SDimitry Andric Check(ScopeListMD->getNumOperands() == 1, 6095e8d8bef9SDimitry Andric "!id.scope.list must point to a list with a single scope", II); 6096349cc55cSDimitry Andric visitAliasScopeListMetadata(ScopeListMD); 6097e8d8bef9SDimitry Andric } 6098e8d8bef9SDimitry Andric 6099e8d8bef9SDimitry Andric // Only check the domination rule when requested. Once all passes have been 6100e8d8bef9SDimitry Andric // adapted this option can go away. 6101e8d8bef9SDimitry Andric if (!VerifyNoAliasScopeDomination) 6102e8d8bef9SDimitry Andric return; 6103e8d8bef9SDimitry Andric 6104e8d8bef9SDimitry Andric // Now sort the intrinsics based on the scope MDNode so that declarations of 6105e8d8bef9SDimitry Andric // the same scopes are next to each other. 6106e8d8bef9SDimitry Andric auto GetScope = [](IntrinsicInst *II) { 6107e8d8bef9SDimitry Andric const auto *ScopeListMV = cast<MetadataAsValue>( 6108e8d8bef9SDimitry Andric II->getOperand(Intrinsic::NoAliasScopeDeclScopeArg)); 6109e8d8bef9SDimitry Andric return &cast<MDNode>(ScopeListMV->getMetadata())->getOperand(0); 6110e8d8bef9SDimitry Andric }; 6111e8d8bef9SDimitry Andric 6112e8d8bef9SDimitry Andric // We are sorting on MDNode pointers here. For valid input IR this is ok. 6113e8d8bef9SDimitry Andric // TODO: Sort on Metadata ID to avoid non-deterministic error messages. 6114e8d8bef9SDimitry Andric auto Compare = [GetScope](IntrinsicInst *Lhs, IntrinsicInst *Rhs) { 6115e8d8bef9SDimitry Andric return GetScope(Lhs) < GetScope(Rhs); 6116e8d8bef9SDimitry Andric }; 6117e8d8bef9SDimitry Andric 6118e8d8bef9SDimitry Andric llvm::sort(NoAliasScopeDecls, Compare); 6119e8d8bef9SDimitry Andric 6120e8d8bef9SDimitry Andric // Go over the intrinsics and check that for the same scope, they are not 6121e8d8bef9SDimitry Andric // dominating each other. 6122e8d8bef9SDimitry Andric auto ItCurrent = NoAliasScopeDecls.begin(); 6123e8d8bef9SDimitry Andric while (ItCurrent != NoAliasScopeDecls.end()) { 6124e8d8bef9SDimitry Andric auto CurScope = GetScope(*ItCurrent); 6125e8d8bef9SDimitry Andric auto ItNext = ItCurrent; 6126e8d8bef9SDimitry Andric do { 6127e8d8bef9SDimitry Andric ++ItNext; 6128e8d8bef9SDimitry Andric } while (ItNext != NoAliasScopeDecls.end() && 6129e8d8bef9SDimitry Andric GetScope(*ItNext) == CurScope); 6130e8d8bef9SDimitry Andric 6131e8d8bef9SDimitry Andric // [ItCurrent, ItNext) represents the declarations for the same scope. 6132e8d8bef9SDimitry Andric // Ensure they are not dominating each other.. but only if it is not too 6133e8d8bef9SDimitry Andric // expensive. 6134e8d8bef9SDimitry Andric if (ItNext - ItCurrent < 32) 6135e8d8bef9SDimitry Andric for (auto *I : llvm::make_range(ItCurrent, ItNext)) 6136e8d8bef9SDimitry Andric for (auto *J : llvm::make_range(ItCurrent, ItNext)) 6137e8d8bef9SDimitry Andric if (I != J) 6138*81ad6265SDimitry Andric Check(!DT.dominates(I, J), 6139e8d8bef9SDimitry Andric "llvm.experimental.noalias.scope.decl dominates another one " 6140e8d8bef9SDimitry Andric "with the same scope", 6141e8d8bef9SDimitry Andric I); 6142e8d8bef9SDimitry Andric ItCurrent = ItNext; 6143e8d8bef9SDimitry Andric } 6144e8d8bef9SDimitry Andric } 6145e8d8bef9SDimitry Andric 61460b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 61470b57cec5SDimitry Andric // Implement the public interfaces to this file... 61480b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 61490b57cec5SDimitry Andric 61500b57cec5SDimitry Andric bool llvm::verifyFunction(const Function &f, raw_ostream *OS) { 61510b57cec5SDimitry Andric Function &F = const_cast<Function &>(f); 61520b57cec5SDimitry Andric 61530b57cec5SDimitry Andric // Don't use a raw_null_ostream. Printing IR is expensive. 61540b57cec5SDimitry Andric Verifier V(OS, /*ShouldTreatBrokenDebugInfoAsError=*/true, *f.getParent()); 61550b57cec5SDimitry Andric 61560b57cec5SDimitry Andric // Note that this function's return value is inverted from what you would 61570b57cec5SDimitry Andric // expect of a function called "verify". 61580b57cec5SDimitry Andric return !V.verify(F); 61590b57cec5SDimitry Andric } 61600b57cec5SDimitry Andric 61610b57cec5SDimitry Andric bool llvm::verifyModule(const Module &M, raw_ostream *OS, 61620b57cec5SDimitry Andric bool *BrokenDebugInfo) { 61630b57cec5SDimitry Andric // Don't use a raw_null_ostream. Printing IR is expensive. 61640b57cec5SDimitry Andric Verifier V(OS, /*ShouldTreatBrokenDebugInfoAsError=*/!BrokenDebugInfo, M); 61650b57cec5SDimitry Andric 61660b57cec5SDimitry Andric bool Broken = false; 61670b57cec5SDimitry Andric for (const Function &F : M) 61680b57cec5SDimitry Andric Broken |= !V.verify(F); 61690b57cec5SDimitry Andric 61700b57cec5SDimitry Andric Broken |= !V.verify(); 61710b57cec5SDimitry Andric if (BrokenDebugInfo) 61720b57cec5SDimitry Andric *BrokenDebugInfo = V.hasBrokenDebugInfo(); 61730b57cec5SDimitry Andric // Note that this function's return value is inverted from what you would 61740b57cec5SDimitry Andric // expect of a function called "verify". 61750b57cec5SDimitry Andric return Broken; 61760b57cec5SDimitry Andric } 61770b57cec5SDimitry Andric 61780b57cec5SDimitry Andric namespace { 61790b57cec5SDimitry Andric 61800b57cec5SDimitry Andric struct VerifierLegacyPass : public FunctionPass { 61810b57cec5SDimitry Andric static char ID; 61820b57cec5SDimitry Andric 61830b57cec5SDimitry Andric std::unique_ptr<Verifier> V; 61840b57cec5SDimitry Andric bool FatalErrors = true; 61850b57cec5SDimitry Andric 61860b57cec5SDimitry Andric VerifierLegacyPass() : FunctionPass(ID) { 61870b57cec5SDimitry Andric initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry()); 61880b57cec5SDimitry Andric } 61890b57cec5SDimitry Andric explicit VerifierLegacyPass(bool FatalErrors) 61900b57cec5SDimitry Andric : FunctionPass(ID), 61910b57cec5SDimitry Andric FatalErrors(FatalErrors) { 61920b57cec5SDimitry Andric initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry()); 61930b57cec5SDimitry Andric } 61940b57cec5SDimitry Andric 61950b57cec5SDimitry Andric bool doInitialization(Module &M) override { 61968bcb0991SDimitry Andric V = std::make_unique<Verifier>( 61970b57cec5SDimitry Andric &dbgs(), /*ShouldTreatBrokenDebugInfoAsError=*/false, M); 61980b57cec5SDimitry Andric return false; 61990b57cec5SDimitry Andric } 62000b57cec5SDimitry Andric 62010b57cec5SDimitry Andric bool runOnFunction(Function &F) override { 62020b57cec5SDimitry Andric if (!V->verify(F) && FatalErrors) { 62030b57cec5SDimitry Andric errs() << "in function " << F.getName() << '\n'; 62040b57cec5SDimitry Andric report_fatal_error("Broken function found, compilation aborted!"); 62050b57cec5SDimitry Andric } 62060b57cec5SDimitry Andric return false; 62070b57cec5SDimitry Andric } 62080b57cec5SDimitry Andric 62090b57cec5SDimitry Andric bool doFinalization(Module &M) override { 62100b57cec5SDimitry Andric bool HasErrors = false; 62110b57cec5SDimitry Andric for (Function &F : M) 62120b57cec5SDimitry Andric if (F.isDeclaration()) 62130b57cec5SDimitry Andric HasErrors |= !V->verify(F); 62140b57cec5SDimitry Andric 62150b57cec5SDimitry Andric HasErrors |= !V->verify(); 62160b57cec5SDimitry Andric if (FatalErrors && (HasErrors || V->hasBrokenDebugInfo())) 62170b57cec5SDimitry Andric report_fatal_error("Broken module found, compilation aborted!"); 62180b57cec5SDimitry Andric return false; 62190b57cec5SDimitry Andric } 62200b57cec5SDimitry Andric 62210b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 62220b57cec5SDimitry Andric AU.setPreservesAll(); 62230b57cec5SDimitry Andric } 62240b57cec5SDimitry Andric }; 62250b57cec5SDimitry Andric 62260b57cec5SDimitry Andric } // end anonymous namespace 62270b57cec5SDimitry Andric 62280b57cec5SDimitry Andric /// Helper to issue failure from the TBAA verification 62290b57cec5SDimitry Andric template <typename... Tys> void TBAAVerifier::CheckFailed(Tys &&... Args) { 62300b57cec5SDimitry Andric if (Diagnostic) 62310b57cec5SDimitry Andric return Diagnostic->CheckFailed(Args...); 62320b57cec5SDimitry Andric } 62330b57cec5SDimitry Andric 6234*81ad6265SDimitry Andric #define CheckTBAA(C, ...) \ 62350b57cec5SDimitry Andric do { \ 62360b57cec5SDimitry Andric if (!(C)) { \ 62370b57cec5SDimitry Andric CheckFailed(__VA_ARGS__); \ 62380b57cec5SDimitry Andric return false; \ 62390b57cec5SDimitry Andric } \ 62400b57cec5SDimitry Andric } while (false) 62410b57cec5SDimitry Andric 62420b57cec5SDimitry Andric /// Verify that \p BaseNode can be used as the "base type" in the struct-path 62430b57cec5SDimitry Andric /// TBAA scheme. This means \p BaseNode is either a scalar node, or a 62440b57cec5SDimitry Andric /// struct-type node describing an aggregate data structure (like a struct). 62450b57cec5SDimitry Andric TBAAVerifier::TBAABaseNodeSummary 62460b57cec5SDimitry Andric TBAAVerifier::verifyTBAABaseNode(Instruction &I, const MDNode *BaseNode, 62470b57cec5SDimitry Andric bool IsNewFormat) { 62480b57cec5SDimitry Andric if (BaseNode->getNumOperands() < 2) { 62490b57cec5SDimitry Andric CheckFailed("Base nodes must have at least two operands", &I, BaseNode); 62500b57cec5SDimitry Andric return {true, ~0u}; 62510b57cec5SDimitry Andric } 62520b57cec5SDimitry Andric 62530b57cec5SDimitry Andric auto Itr = TBAABaseNodes.find(BaseNode); 62540b57cec5SDimitry Andric if (Itr != TBAABaseNodes.end()) 62550b57cec5SDimitry Andric return Itr->second; 62560b57cec5SDimitry Andric 62570b57cec5SDimitry Andric auto Result = verifyTBAABaseNodeImpl(I, BaseNode, IsNewFormat); 62580b57cec5SDimitry Andric auto InsertResult = TBAABaseNodes.insert({BaseNode, Result}); 62590b57cec5SDimitry Andric (void)InsertResult; 62600b57cec5SDimitry Andric assert(InsertResult.second && "We just checked!"); 62610b57cec5SDimitry Andric return Result; 62620b57cec5SDimitry Andric } 62630b57cec5SDimitry Andric 62640b57cec5SDimitry Andric TBAAVerifier::TBAABaseNodeSummary 62650b57cec5SDimitry Andric TBAAVerifier::verifyTBAABaseNodeImpl(Instruction &I, const MDNode *BaseNode, 62660b57cec5SDimitry Andric bool IsNewFormat) { 62670b57cec5SDimitry Andric const TBAAVerifier::TBAABaseNodeSummary InvalidNode = {true, ~0u}; 62680b57cec5SDimitry Andric 62690b57cec5SDimitry Andric if (BaseNode->getNumOperands() == 2) { 62700b57cec5SDimitry Andric // Scalar nodes can only be accessed at offset 0. 62710b57cec5SDimitry Andric return isValidScalarTBAANode(BaseNode) 62720b57cec5SDimitry Andric ? TBAAVerifier::TBAABaseNodeSummary({false, 0}) 62730b57cec5SDimitry Andric : InvalidNode; 62740b57cec5SDimitry Andric } 62750b57cec5SDimitry Andric 62760b57cec5SDimitry Andric if (IsNewFormat) { 62770b57cec5SDimitry Andric if (BaseNode->getNumOperands() % 3 != 0) { 62780b57cec5SDimitry Andric CheckFailed("Access tag nodes must have the number of operands that is a " 62790b57cec5SDimitry Andric "multiple of 3!", BaseNode); 62800b57cec5SDimitry Andric return InvalidNode; 62810b57cec5SDimitry Andric } 62820b57cec5SDimitry Andric } else { 62830b57cec5SDimitry Andric if (BaseNode->getNumOperands() % 2 != 1) { 62840b57cec5SDimitry Andric CheckFailed("Struct tag nodes must have an odd number of operands!", 62850b57cec5SDimitry Andric BaseNode); 62860b57cec5SDimitry Andric return InvalidNode; 62870b57cec5SDimitry Andric } 62880b57cec5SDimitry Andric } 62890b57cec5SDimitry Andric 62900b57cec5SDimitry Andric // Check the type size field. 62910b57cec5SDimitry Andric if (IsNewFormat) { 62920b57cec5SDimitry Andric auto *TypeSizeNode = mdconst::dyn_extract_or_null<ConstantInt>( 62930b57cec5SDimitry Andric BaseNode->getOperand(1)); 62940b57cec5SDimitry Andric if (!TypeSizeNode) { 62950b57cec5SDimitry Andric CheckFailed("Type size nodes must be constants!", &I, BaseNode); 62960b57cec5SDimitry Andric return InvalidNode; 62970b57cec5SDimitry Andric } 62980b57cec5SDimitry Andric } 62990b57cec5SDimitry Andric 63000b57cec5SDimitry Andric // Check the type name field. In the new format it can be anything. 63010b57cec5SDimitry Andric if (!IsNewFormat && !isa<MDString>(BaseNode->getOperand(0))) { 63020b57cec5SDimitry Andric CheckFailed("Struct tag nodes have a string as their first operand", 63030b57cec5SDimitry Andric BaseNode); 63040b57cec5SDimitry Andric return InvalidNode; 63050b57cec5SDimitry Andric } 63060b57cec5SDimitry Andric 63070b57cec5SDimitry Andric bool Failed = false; 63080b57cec5SDimitry Andric 63090b57cec5SDimitry Andric Optional<APInt> PrevOffset; 63100b57cec5SDimitry Andric unsigned BitWidth = ~0u; 63110b57cec5SDimitry Andric 63120b57cec5SDimitry Andric // We've already checked that BaseNode is not a degenerate root node with one 63130b57cec5SDimitry Andric // operand in \c verifyTBAABaseNode, so this loop should run at least once. 63140b57cec5SDimitry Andric unsigned FirstFieldOpNo = IsNewFormat ? 3 : 1; 63150b57cec5SDimitry Andric unsigned NumOpsPerField = IsNewFormat ? 3 : 2; 63160b57cec5SDimitry Andric for (unsigned Idx = FirstFieldOpNo; Idx < BaseNode->getNumOperands(); 63170b57cec5SDimitry Andric Idx += NumOpsPerField) { 63180b57cec5SDimitry Andric const MDOperand &FieldTy = BaseNode->getOperand(Idx); 63190b57cec5SDimitry Andric const MDOperand &FieldOffset = BaseNode->getOperand(Idx + 1); 63200b57cec5SDimitry Andric if (!isa<MDNode>(FieldTy)) { 63210b57cec5SDimitry Andric CheckFailed("Incorrect field entry in struct type node!", &I, BaseNode); 63220b57cec5SDimitry Andric Failed = true; 63230b57cec5SDimitry Andric continue; 63240b57cec5SDimitry Andric } 63250b57cec5SDimitry Andric 63260b57cec5SDimitry Andric auto *OffsetEntryCI = 63270b57cec5SDimitry Andric mdconst::dyn_extract_or_null<ConstantInt>(FieldOffset); 63280b57cec5SDimitry Andric if (!OffsetEntryCI) { 63290b57cec5SDimitry Andric CheckFailed("Offset entries must be constants!", &I, BaseNode); 63300b57cec5SDimitry Andric Failed = true; 63310b57cec5SDimitry Andric continue; 63320b57cec5SDimitry Andric } 63330b57cec5SDimitry Andric 63340b57cec5SDimitry Andric if (BitWidth == ~0u) 63350b57cec5SDimitry Andric BitWidth = OffsetEntryCI->getBitWidth(); 63360b57cec5SDimitry Andric 63370b57cec5SDimitry Andric if (OffsetEntryCI->getBitWidth() != BitWidth) { 63380b57cec5SDimitry Andric CheckFailed( 63390b57cec5SDimitry Andric "Bitwidth between the offsets and struct type entries must match", &I, 63400b57cec5SDimitry Andric BaseNode); 63410b57cec5SDimitry Andric Failed = true; 63420b57cec5SDimitry Andric continue; 63430b57cec5SDimitry Andric } 63440b57cec5SDimitry Andric 63450b57cec5SDimitry Andric // NB! As far as I can tell, we generate a non-strictly increasing offset 63460b57cec5SDimitry Andric // sequence only from structs that have zero size bit fields. When 63470b57cec5SDimitry Andric // recursing into a contained struct in \c getFieldNodeFromTBAABaseNode we 63480b57cec5SDimitry Andric // pick the field lexically the latest in struct type metadata node. This 63490b57cec5SDimitry Andric // mirrors the actual behavior of the alias analysis implementation. 63500b57cec5SDimitry Andric bool IsAscending = 63510b57cec5SDimitry Andric !PrevOffset || PrevOffset->ule(OffsetEntryCI->getValue()); 63520b57cec5SDimitry Andric 63530b57cec5SDimitry Andric if (!IsAscending) { 63540b57cec5SDimitry Andric CheckFailed("Offsets must be increasing!", &I, BaseNode); 63550b57cec5SDimitry Andric Failed = true; 63560b57cec5SDimitry Andric } 63570b57cec5SDimitry Andric 63580b57cec5SDimitry Andric PrevOffset = OffsetEntryCI->getValue(); 63590b57cec5SDimitry Andric 63600b57cec5SDimitry Andric if (IsNewFormat) { 63610b57cec5SDimitry Andric auto *MemberSizeNode = mdconst::dyn_extract_or_null<ConstantInt>( 63620b57cec5SDimitry Andric BaseNode->getOperand(Idx + 2)); 63630b57cec5SDimitry Andric if (!MemberSizeNode) { 63640b57cec5SDimitry Andric CheckFailed("Member size entries must be constants!", &I, BaseNode); 63650b57cec5SDimitry Andric Failed = true; 63660b57cec5SDimitry Andric continue; 63670b57cec5SDimitry Andric } 63680b57cec5SDimitry Andric } 63690b57cec5SDimitry Andric } 63700b57cec5SDimitry Andric 63710b57cec5SDimitry Andric return Failed ? InvalidNode 63720b57cec5SDimitry Andric : TBAAVerifier::TBAABaseNodeSummary(false, BitWidth); 63730b57cec5SDimitry Andric } 63740b57cec5SDimitry Andric 63750b57cec5SDimitry Andric static bool IsRootTBAANode(const MDNode *MD) { 63760b57cec5SDimitry Andric return MD->getNumOperands() < 2; 63770b57cec5SDimitry Andric } 63780b57cec5SDimitry Andric 63790b57cec5SDimitry Andric static bool IsScalarTBAANodeImpl(const MDNode *MD, 63800b57cec5SDimitry Andric SmallPtrSetImpl<const MDNode *> &Visited) { 63810b57cec5SDimitry Andric if (MD->getNumOperands() != 2 && MD->getNumOperands() != 3) 63820b57cec5SDimitry Andric return false; 63830b57cec5SDimitry Andric 63840b57cec5SDimitry Andric if (!isa<MDString>(MD->getOperand(0))) 63850b57cec5SDimitry Andric return false; 63860b57cec5SDimitry Andric 63870b57cec5SDimitry Andric if (MD->getNumOperands() == 3) { 63880b57cec5SDimitry Andric auto *Offset = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2)); 63890b57cec5SDimitry Andric if (!(Offset && Offset->isZero() && isa<MDString>(MD->getOperand(0)))) 63900b57cec5SDimitry Andric return false; 63910b57cec5SDimitry Andric } 63920b57cec5SDimitry Andric 63930b57cec5SDimitry Andric auto *Parent = dyn_cast_or_null<MDNode>(MD->getOperand(1)); 63940b57cec5SDimitry Andric return Parent && Visited.insert(Parent).second && 63950b57cec5SDimitry Andric (IsRootTBAANode(Parent) || IsScalarTBAANodeImpl(Parent, Visited)); 63960b57cec5SDimitry Andric } 63970b57cec5SDimitry Andric 63980b57cec5SDimitry Andric bool TBAAVerifier::isValidScalarTBAANode(const MDNode *MD) { 63990b57cec5SDimitry Andric auto ResultIt = TBAAScalarNodes.find(MD); 64000b57cec5SDimitry Andric if (ResultIt != TBAAScalarNodes.end()) 64010b57cec5SDimitry Andric return ResultIt->second; 64020b57cec5SDimitry Andric 64030b57cec5SDimitry Andric SmallPtrSet<const MDNode *, 4> Visited; 64040b57cec5SDimitry Andric bool Result = IsScalarTBAANodeImpl(MD, Visited); 64050b57cec5SDimitry Andric auto InsertResult = TBAAScalarNodes.insert({MD, Result}); 64060b57cec5SDimitry Andric (void)InsertResult; 64070b57cec5SDimitry Andric assert(InsertResult.second && "Just checked!"); 64080b57cec5SDimitry Andric 64090b57cec5SDimitry Andric return Result; 64100b57cec5SDimitry Andric } 64110b57cec5SDimitry Andric 64120b57cec5SDimitry Andric /// Returns the field node at the offset \p Offset in \p BaseNode. Update \p 64130b57cec5SDimitry Andric /// Offset in place to be the offset within the field node returned. 64140b57cec5SDimitry Andric /// 64150b57cec5SDimitry Andric /// We assume we've okayed \p BaseNode via \c verifyTBAABaseNode. 64160b57cec5SDimitry Andric MDNode *TBAAVerifier::getFieldNodeFromTBAABaseNode(Instruction &I, 64170b57cec5SDimitry Andric const MDNode *BaseNode, 64180b57cec5SDimitry Andric APInt &Offset, 64190b57cec5SDimitry Andric bool IsNewFormat) { 64200b57cec5SDimitry Andric assert(BaseNode->getNumOperands() >= 2 && "Invalid base node!"); 64210b57cec5SDimitry Andric 64220b57cec5SDimitry Andric // Scalar nodes have only one possible "field" -- their parent in the access 64230b57cec5SDimitry Andric // hierarchy. Offset must be zero at this point, but our caller is supposed 6424*81ad6265SDimitry Andric // to check that. 64250b57cec5SDimitry Andric if (BaseNode->getNumOperands() == 2) 64260b57cec5SDimitry Andric return cast<MDNode>(BaseNode->getOperand(1)); 64270b57cec5SDimitry Andric 64280b57cec5SDimitry Andric unsigned FirstFieldOpNo = IsNewFormat ? 3 : 1; 64290b57cec5SDimitry Andric unsigned NumOpsPerField = IsNewFormat ? 3 : 2; 64300b57cec5SDimitry Andric for (unsigned Idx = FirstFieldOpNo; Idx < BaseNode->getNumOperands(); 64310b57cec5SDimitry Andric Idx += NumOpsPerField) { 64320b57cec5SDimitry Andric auto *OffsetEntryCI = 64330b57cec5SDimitry Andric mdconst::extract<ConstantInt>(BaseNode->getOperand(Idx + 1)); 64340b57cec5SDimitry Andric if (OffsetEntryCI->getValue().ugt(Offset)) { 64350b57cec5SDimitry Andric if (Idx == FirstFieldOpNo) { 64360b57cec5SDimitry Andric CheckFailed("Could not find TBAA parent in struct type node", &I, 64370b57cec5SDimitry Andric BaseNode, &Offset); 64380b57cec5SDimitry Andric return nullptr; 64390b57cec5SDimitry Andric } 64400b57cec5SDimitry Andric 64410b57cec5SDimitry Andric unsigned PrevIdx = Idx - NumOpsPerField; 64420b57cec5SDimitry Andric auto *PrevOffsetEntryCI = 64430b57cec5SDimitry Andric mdconst::extract<ConstantInt>(BaseNode->getOperand(PrevIdx + 1)); 64440b57cec5SDimitry Andric Offset -= PrevOffsetEntryCI->getValue(); 64450b57cec5SDimitry Andric return cast<MDNode>(BaseNode->getOperand(PrevIdx)); 64460b57cec5SDimitry Andric } 64470b57cec5SDimitry Andric } 64480b57cec5SDimitry Andric 64490b57cec5SDimitry Andric unsigned LastIdx = BaseNode->getNumOperands() - NumOpsPerField; 64500b57cec5SDimitry Andric auto *LastOffsetEntryCI = mdconst::extract<ConstantInt>( 64510b57cec5SDimitry Andric BaseNode->getOperand(LastIdx + 1)); 64520b57cec5SDimitry Andric Offset -= LastOffsetEntryCI->getValue(); 64530b57cec5SDimitry Andric return cast<MDNode>(BaseNode->getOperand(LastIdx)); 64540b57cec5SDimitry Andric } 64550b57cec5SDimitry Andric 64560b57cec5SDimitry Andric static bool isNewFormatTBAATypeNode(llvm::MDNode *Type) { 64570b57cec5SDimitry Andric if (!Type || Type->getNumOperands() < 3) 64580b57cec5SDimitry Andric return false; 64590b57cec5SDimitry Andric 64600b57cec5SDimitry Andric // In the new format type nodes shall have a reference to the parent type as 64610b57cec5SDimitry Andric // its first operand. 6462349cc55cSDimitry Andric return isa_and_nonnull<MDNode>(Type->getOperand(0)); 64630b57cec5SDimitry Andric } 64640b57cec5SDimitry Andric 64650b57cec5SDimitry Andric bool TBAAVerifier::visitTBAAMetadata(Instruction &I, const MDNode *MD) { 6466*81ad6265SDimitry Andric CheckTBAA(isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallInst>(I) || 64670b57cec5SDimitry Andric isa<VAArgInst>(I) || isa<AtomicRMWInst>(I) || 64680b57cec5SDimitry Andric isa<AtomicCmpXchgInst>(I), 64690b57cec5SDimitry Andric "This instruction shall not have a TBAA access tag!", &I); 64700b57cec5SDimitry Andric 64710b57cec5SDimitry Andric bool IsStructPathTBAA = 64720b57cec5SDimitry Andric isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3; 64730b57cec5SDimitry Andric 6474*81ad6265SDimitry Andric CheckTBAA(IsStructPathTBAA, 6475*81ad6265SDimitry Andric "Old-style TBAA is no longer allowed, use struct-path TBAA instead", 6476*81ad6265SDimitry Andric &I); 64770b57cec5SDimitry Andric 64780b57cec5SDimitry Andric MDNode *BaseNode = dyn_cast_or_null<MDNode>(MD->getOperand(0)); 64790b57cec5SDimitry Andric MDNode *AccessType = dyn_cast_or_null<MDNode>(MD->getOperand(1)); 64800b57cec5SDimitry Andric 64810b57cec5SDimitry Andric bool IsNewFormat = isNewFormatTBAATypeNode(AccessType); 64820b57cec5SDimitry Andric 64830b57cec5SDimitry Andric if (IsNewFormat) { 6484*81ad6265SDimitry Andric CheckTBAA(MD->getNumOperands() == 4 || MD->getNumOperands() == 5, 64850b57cec5SDimitry Andric "Access tag metadata must have either 4 or 5 operands", &I, MD); 64860b57cec5SDimitry Andric } else { 6487*81ad6265SDimitry Andric CheckTBAA(MD->getNumOperands() < 5, 64880b57cec5SDimitry Andric "Struct tag metadata must have either 3 or 4 operands", &I, MD); 64890b57cec5SDimitry Andric } 64900b57cec5SDimitry Andric 64910b57cec5SDimitry Andric // Check the access size field. 64920b57cec5SDimitry Andric if (IsNewFormat) { 64930b57cec5SDimitry Andric auto *AccessSizeNode = mdconst::dyn_extract_or_null<ConstantInt>( 64940b57cec5SDimitry Andric MD->getOperand(3)); 6495*81ad6265SDimitry Andric CheckTBAA(AccessSizeNode, "Access size field must be a constant", &I, MD); 64960b57cec5SDimitry Andric } 64970b57cec5SDimitry Andric 64980b57cec5SDimitry Andric // Check the immutability flag. 64990b57cec5SDimitry Andric unsigned ImmutabilityFlagOpNo = IsNewFormat ? 4 : 3; 65000b57cec5SDimitry Andric if (MD->getNumOperands() == ImmutabilityFlagOpNo + 1) { 65010b57cec5SDimitry Andric auto *IsImmutableCI = mdconst::dyn_extract_or_null<ConstantInt>( 65020b57cec5SDimitry Andric MD->getOperand(ImmutabilityFlagOpNo)); 6503*81ad6265SDimitry Andric CheckTBAA(IsImmutableCI, 6504*81ad6265SDimitry Andric "Immutability tag on struct tag metadata must be a constant", &I, 6505*81ad6265SDimitry Andric MD); 6506*81ad6265SDimitry Andric CheckTBAA( 65070b57cec5SDimitry Andric IsImmutableCI->isZero() || IsImmutableCI->isOne(), 65080b57cec5SDimitry Andric "Immutability part of the struct tag metadata must be either 0 or 1", 65090b57cec5SDimitry Andric &I, MD); 65100b57cec5SDimitry Andric } 65110b57cec5SDimitry Andric 6512*81ad6265SDimitry Andric CheckTBAA(BaseNode && AccessType, 65130b57cec5SDimitry Andric "Malformed struct tag metadata: base and access-type " 65140b57cec5SDimitry Andric "should be non-null and point to Metadata nodes", 65150b57cec5SDimitry Andric &I, MD, BaseNode, AccessType); 65160b57cec5SDimitry Andric 65170b57cec5SDimitry Andric if (!IsNewFormat) { 6518*81ad6265SDimitry Andric CheckTBAA(isValidScalarTBAANode(AccessType), 65190b57cec5SDimitry Andric "Access type node must be a valid scalar type", &I, MD, 65200b57cec5SDimitry Andric AccessType); 65210b57cec5SDimitry Andric } 65220b57cec5SDimitry Andric 65230b57cec5SDimitry Andric auto *OffsetCI = mdconst::dyn_extract_or_null<ConstantInt>(MD->getOperand(2)); 6524*81ad6265SDimitry Andric CheckTBAA(OffsetCI, "Offset must be constant integer", &I, MD); 65250b57cec5SDimitry Andric 65260b57cec5SDimitry Andric APInt Offset = OffsetCI->getValue(); 65270b57cec5SDimitry Andric bool SeenAccessTypeInPath = false; 65280b57cec5SDimitry Andric 65290b57cec5SDimitry Andric SmallPtrSet<MDNode *, 4> StructPath; 65300b57cec5SDimitry Andric 65310b57cec5SDimitry Andric for (/* empty */; BaseNode && !IsRootTBAANode(BaseNode); 65320b57cec5SDimitry Andric BaseNode = getFieldNodeFromTBAABaseNode(I, BaseNode, Offset, 65330b57cec5SDimitry Andric IsNewFormat)) { 65340b57cec5SDimitry Andric if (!StructPath.insert(BaseNode).second) { 65350b57cec5SDimitry Andric CheckFailed("Cycle detected in struct path", &I, MD); 65360b57cec5SDimitry Andric return false; 65370b57cec5SDimitry Andric } 65380b57cec5SDimitry Andric 65390b57cec5SDimitry Andric bool Invalid; 65400b57cec5SDimitry Andric unsigned BaseNodeBitWidth; 65410b57cec5SDimitry Andric std::tie(Invalid, BaseNodeBitWidth) = verifyTBAABaseNode(I, BaseNode, 65420b57cec5SDimitry Andric IsNewFormat); 65430b57cec5SDimitry Andric 65440b57cec5SDimitry Andric // If the base node is invalid in itself, then we've already printed all the 65450b57cec5SDimitry Andric // errors we wanted to print. 65460b57cec5SDimitry Andric if (Invalid) 65470b57cec5SDimitry Andric return false; 65480b57cec5SDimitry Andric 65490b57cec5SDimitry Andric SeenAccessTypeInPath |= BaseNode == AccessType; 65500b57cec5SDimitry Andric 65510b57cec5SDimitry Andric if (isValidScalarTBAANode(BaseNode) || BaseNode == AccessType) 6552*81ad6265SDimitry Andric CheckTBAA(Offset == 0, "Offset not zero at the point of scalar access", 65530b57cec5SDimitry Andric &I, MD, &Offset); 65540b57cec5SDimitry Andric 6555*81ad6265SDimitry Andric CheckTBAA(BaseNodeBitWidth == Offset.getBitWidth() || 65560b57cec5SDimitry Andric (BaseNodeBitWidth == 0 && Offset == 0) || 65570b57cec5SDimitry Andric (IsNewFormat && BaseNodeBitWidth == ~0u), 65580b57cec5SDimitry Andric "Access bit-width not the same as description bit-width", &I, MD, 65590b57cec5SDimitry Andric BaseNodeBitWidth, Offset.getBitWidth()); 65600b57cec5SDimitry Andric 65610b57cec5SDimitry Andric if (IsNewFormat && SeenAccessTypeInPath) 65620b57cec5SDimitry Andric break; 65630b57cec5SDimitry Andric } 65640b57cec5SDimitry Andric 6565*81ad6265SDimitry Andric CheckTBAA(SeenAccessTypeInPath, "Did not see access type in access path!", &I, 6566*81ad6265SDimitry Andric MD); 65670b57cec5SDimitry Andric return true; 65680b57cec5SDimitry Andric } 65690b57cec5SDimitry Andric 65700b57cec5SDimitry Andric char VerifierLegacyPass::ID = 0; 65710b57cec5SDimitry Andric INITIALIZE_PASS(VerifierLegacyPass, "verify", "Module Verifier", false, false) 65720b57cec5SDimitry Andric 65730b57cec5SDimitry Andric FunctionPass *llvm::createVerifierPass(bool FatalErrors) { 65740b57cec5SDimitry Andric return new VerifierLegacyPass(FatalErrors); 65750b57cec5SDimitry Andric } 65760b57cec5SDimitry Andric 65770b57cec5SDimitry Andric AnalysisKey VerifierAnalysis::Key; 65780b57cec5SDimitry Andric VerifierAnalysis::Result VerifierAnalysis::run(Module &M, 65790b57cec5SDimitry Andric ModuleAnalysisManager &) { 65800b57cec5SDimitry Andric Result Res; 65810b57cec5SDimitry Andric Res.IRBroken = llvm::verifyModule(M, &dbgs(), &Res.DebugInfoBroken); 65820b57cec5SDimitry Andric return Res; 65830b57cec5SDimitry Andric } 65840b57cec5SDimitry Andric 65850b57cec5SDimitry Andric VerifierAnalysis::Result VerifierAnalysis::run(Function &F, 65860b57cec5SDimitry Andric FunctionAnalysisManager &) { 65870b57cec5SDimitry Andric return { llvm::verifyFunction(F, &dbgs()), false }; 65880b57cec5SDimitry Andric } 65890b57cec5SDimitry Andric 65900b57cec5SDimitry Andric PreservedAnalyses VerifierPass::run(Module &M, ModuleAnalysisManager &AM) { 65910b57cec5SDimitry Andric auto Res = AM.getResult<VerifierAnalysis>(M); 65920b57cec5SDimitry Andric if (FatalErrors && (Res.IRBroken || Res.DebugInfoBroken)) 65930b57cec5SDimitry Andric report_fatal_error("Broken module found, compilation aborted!"); 65940b57cec5SDimitry Andric 65950b57cec5SDimitry Andric return PreservedAnalyses::all(); 65960b57cec5SDimitry Andric } 65970b57cec5SDimitry Andric 65980b57cec5SDimitry Andric PreservedAnalyses VerifierPass::run(Function &F, FunctionAnalysisManager &AM) { 65990b57cec5SDimitry Andric auto res = AM.getResult<VerifierAnalysis>(F); 66000b57cec5SDimitry Andric if (res.IRBroken && FatalErrors) 66010b57cec5SDimitry Andric report_fatal_error("Broken function found, compilation aborted!"); 66020b57cec5SDimitry Andric 66030b57cec5SDimitry Andric return PreservedAnalyses::all(); 66040b57cec5SDimitry Andric } 6605