[LLVM][Alignment] Introduce Alignment In Attributes
[llvm-core.git] / lib / IR / Verifier.cpp
blob1793ba42009ea72781a721caa141654a72ed6935
1 //===-- Verifier.cpp - Implement the Module Verifier -----------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the function verifier interface, that can be used for some
10 // sanity checking of input to the system.
12 // Note that this does not provide full `Java style' security and verifications,
13 // instead it just tries to ensure that code is well-formed.
15 // * Both of a binary operator's parameters are of the same type
16 // * Verify that the indices of mem access instructions match other operands
17 // * Verify that arithmetic and other things are only performed on first-class
18 // types. Verify that shifts & logicals only happen on integrals f.e.
19 // * All of the constants in a switch statement are of the correct type
20 // * The code is in valid SSA form
21 // * It should be illegal to put a label into any other type (like a structure)
22 // or to return one. [except constant arrays!]
23 // * Only phi nodes can be self referential: 'add i32 %0, %0 ; <int>:0' is bad
24 // * PHI nodes must have an entry for each predecessor, with no extras.
25 // * PHI nodes must be the first thing in a basic block, all grouped together
26 // * PHI nodes must have at least one entry
27 // * All basic blocks should only end with terminator insts, not contain them
28 // * The entry node to a function must not have predecessors
29 // * All Instructions must be embedded into a basic block
30 // * Functions cannot take a void-typed parameter
31 // * Verify that a function's argument list agrees with it's declared type.
32 // * It is illegal to specify a name for a void value.
33 // * It is illegal to have a internal global value with no initializer
34 // * It is illegal to have a ret instruction that returns a value that does not
35 // agree with the function return value type.
36 // * Function call argument types match the function prototype
37 // * A landing pad is defined by a landingpad instruction, and can be jumped to
38 // only by the unwind edge of an invoke instruction.
39 // * A landingpad instruction must be the first non-PHI instruction in the
40 // block.
41 // * Landingpad instructions must be in a function with a personality function.
42 // * All other things that are tested by asserts spread about the code...
44 //===----------------------------------------------------------------------===//
46 #include "llvm/IR/Verifier.h"
47 #include "llvm/ADT/APFloat.h"
48 #include "llvm/ADT/APInt.h"
49 #include "llvm/ADT/ArrayRef.h"
50 #include "llvm/ADT/DenseMap.h"
51 #include "llvm/ADT/MapVector.h"
52 #include "llvm/ADT/Optional.h"
53 #include "llvm/ADT/STLExtras.h"
54 #include "llvm/ADT/SmallPtrSet.h"
55 #include "llvm/ADT/SmallSet.h"
56 #include "llvm/ADT/SmallVector.h"
57 #include "llvm/ADT/StringExtras.h"
58 #include "llvm/ADT/StringMap.h"
59 #include "llvm/ADT/StringRef.h"
60 #include "llvm/ADT/Twine.h"
61 #include "llvm/ADT/ilist.h"
62 #include "llvm/BinaryFormat/Dwarf.h"
63 #include "llvm/IR/Argument.h"
64 #include "llvm/IR/Attributes.h"
65 #include "llvm/IR/BasicBlock.h"
66 #include "llvm/IR/CFG.h"
67 #include "llvm/IR/CallingConv.h"
68 #include "llvm/IR/Comdat.h"
69 #include "llvm/IR/Constant.h"
70 #include "llvm/IR/ConstantRange.h"
71 #include "llvm/IR/Constants.h"
72 #include "llvm/IR/DataLayout.h"
73 #include "llvm/IR/DebugInfo.h"
74 #include "llvm/IR/DebugInfoMetadata.h"
75 #include "llvm/IR/DebugLoc.h"
76 #include "llvm/IR/DerivedTypes.h"
77 #include "llvm/IR/Dominators.h"
78 #include "llvm/IR/Function.h"
79 #include "llvm/IR/GlobalAlias.h"
80 #include "llvm/IR/GlobalValue.h"
81 #include "llvm/IR/GlobalVariable.h"
82 #include "llvm/IR/InlineAsm.h"
83 #include "llvm/IR/InstVisitor.h"
84 #include "llvm/IR/InstrTypes.h"
85 #include "llvm/IR/Instruction.h"
86 #include "llvm/IR/Instructions.h"
87 #include "llvm/IR/IntrinsicInst.h"
88 #include "llvm/IR/Intrinsics.h"
89 #include "llvm/IR/LLVMContext.h"
90 #include "llvm/IR/Metadata.h"
91 #include "llvm/IR/Module.h"
92 #include "llvm/IR/ModuleSlotTracker.h"
93 #include "llvm/IR/PassManager.h"
94 #include "llvm/IR/Statepoint.h"
95 #include "llvm/IR/Type.h"
96 #include "llvm/IR/Use.h"
97 #include "llvm/IR/User.h"
98 #include "llvm/IR/Value.h"
99 #include "llvm/Pass.h"
100 #include "llvm/Support/AtomicOrdering.h"
101 #include "llvm/Support/Casting.h"
102 #include "llvm/Support/CommandLine.h"
103 #include "llvm/Support/Debug.h"
104 #include "llvm/Support/ErrorHandling.h"
105 #include "llvm/Support/MathExtras.h"
106 #include "llvm/Support/raw_ostream.h"
107 #include <algorithm>
108 #include <cassert>
109 #include <cstdint>
110 #include <memory>
111 #include <string>
112 #include <utility>
114 using namespace llvm;
116 namespace llvm {
118 struct VerifierSupport {
119 raw_ostream *OS;
120 const Module &M;
121 ModuleSlotTracker MST;
122 const DataLayout &DL;
123 LLVMContext &Context;
125 /// Track the brokenness of the module while recursively visiting.
126 bool Broken = false;
127 /// Broken debug info can be "recovered" from by stripping the debug info.
128 bool BrokenDebugInfo = false;
129 /// Whether to treat broken debug info as an error.
130 bool TreatBrokenDebugInfoAsError = true;
132 explicit VerifierSupport(raw_ostream *OS, const Module &M)
133 : OS(OS), M(M), MST(&M), DL(M.getDataLayout()), Context(M.getContext()) {}
135 private:
136 void Write(const Module *M) {
137 *OS << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
140 void Write(const Value *V) {
141 if (V)
142 Write(*V);
145 void Write(const Value &V) {
146 if (isa<Instruction>(V)) {
147 V.print(*OS, MST);
148 *OS << '\n';
149 } else {
150 V.printAsOperand(*OS, true, MST);
151 *OS << '\n';
155 void Write(const Metadata *MD) {
156 if (!MD)
157 return;
158 MD->print(*OS, MST, &M);
159 *OS << '\n';
162 template <class T> void Write(const MDTupleTypedArrayWrapper<T> &MD) {
163 Write(MD.get());
166 void Write(const NamedMDNode *NMD) {
167 if (!NMD)
168 return;
169 NMD->print(*OS, MST);
170 *OS << '\n';
173 void Write(Type *T) {
174 if (!T)
175 return;
176 *OS << ' ' << *T;
179 void Write(const Comdat *C) {
180 if (!C)
181 return;
182 *OS << *C;
185 void Write(const APInt *AI) {
186 if (!AI)
187 return;
188 *OS << *AI << '\n';
191 void Write(const unsigned i) { *OS << i << '\n'; }
193 template <typename T> void Write(ArrayRef<T> Vs) {
194 for (const T &V : Vs)
195 Write(V);
198 template <typename T1, typename... Ts>
199 void WriteTs(const T1 &V1, const Ts &... Vs) {
200 Write(V1);
201 WriteTs(Vs...);
204 template <typename... Ts> void WriteTs() {}
206 public:
207 /// A check failed, so printout out the condition and the message.
209 /// This provides a nice place to put a breakpoint if you want to see why
210 /// something is not correct.
211 void CheckFailed(const Twine &Message) {
212 if (OS)
213 *OS << Message << '\n';
214 Broken = true;
217 /// A check failed (with values to print).
219 /// This calls the Message-only version so that the above is easier to set a
220 /// breakpoint on.
221 template <typename T1, typename... Ts>
222 void CheckFailed(const Twine &Message, const T1 &V1, const Ts &... Vs) {
223 CheckFailed(Message);
224 if (OS)
225 WriteTs(V1, Vs...);
228 /// A debug info check failed.
229 void DebugInfoCheckFailed(const Twine &Message) {
230 if (OS)
231 *OS << Message << '\n';
232 Broken |= TreatBrokenDebugInfoAsError;
233 BrokenDebugInfo = true;
236 /// A debug info check failed (with values to print).
237 template <typename T1, typename... Ts>
238 void DebugInfoCheckFailed(const Twine &Message, const T1 &V1,
239 const Ts &... Vs) {
240 DebugInfoCheckFailed(Message);
241 if (OS)
242 WriteTs(V1, Vs...);
246 } // namespace llvm
248 namespace {
250 class Verifier : public InstVisitor<Verifier>, VerifierSupport {
251 friend class InstVisitor<Verifier>;
253 DominatorTree DT;
255 /// When verifying a basic block, keep track of all of the
256 /// instructions we have seen so far.
258 /// This allows us to do efficient dominance checks for the case when an
259 /// instruction has an operand that is an instruction in the same block.
260 SmallPtrSet<Instruction *, 16> InstsInThisBlock;
262 /// Keep track of the metadata nodes that have been checked already.
263 SmallPtrSet<const Metadata *, 32> MDNodes;
265 /// Keep track which DISubprogram is attached to which function.
266 DenseMap<const DISubprogram *, const Function *> DISubprogramAttachments;
268 /// Track all DICompileUnits visited.
269 SmallPtrSet<const Metadata *, 2> CUVisited;
271 /// The result type for a landingpad.
272 Type *LandingPadResultTy;
274 /// Whether we've seen a call to @llvm.localescape in this function
275 /// already.
276 bool SawFrameEscape;
278 /// Whether the current function has a DISubprogram attached to it.
279 bool HasDebugInfo = false;
281 /// Whether source was present on the first DIFile encountered in each CU.
282 DenseMap<const DICompileUnit *, bool> HasSourceDebugInfo;
284 /// Stores the count of how many objects were passed to llvm.localescape for a
285 /// given function and the largest index passed to llvm.localrecover.
286 DenseMap<Function *, std::pair<unsigned, unsigned>> FrameEscapeInfo;
288 // Maps catchswitches and cleanuppads that unwind to siblings to the
289 // terminators that indicate the unwind, used to detect cycles therein.
290 MapVector<Instruction *, Instruction *> SiblingFuncletInfo;
292 /// Cache of constants visited in search of ConstantExprs.
293 SmallPtrSet<const Constant *, 32> ConstantExprVisited;
295 /// Cache of declarations of the llvm.experimental.deoptimize.<ty> intrinsic.
296 SmallVector<const Function *, 4> DeoptimizeDeclarations;
298 // Verify that this GlobalValue is only used in this module.
299 // This map is used to avoid visiting uses twice. We can arrive at a user
300 // twice, if they have multiple operands. In particular for very large
301 // constant expressions, we can arrive at a particular user many times.
302 SmallPtrSet<const Value *, 32> GlobalValueVisited;
304 // Keeps track of duplicate function argument debug info.
305 SmallVector<const DILocalVariable *, 16> DebugFnArgs;
307 TBAAVerifier TBAAVerifyHelper;
309 void checkAtomicMemAccessSize(Type *Ty, const Instruction *I);
311 public:
312 explicit Verifier(raw_ostream *OS, bool ShouldTreatBrokenDebugInfoAsError,
313 const Module &M)
314 : VerifierSupport(OS, M), LandingPadResultTy(nullptr),
315 SawFrameEscape(false), TBAAVerifyHelper(this) {
316 TreatBrokenDebugInfoAsError = ShouldTreatBrokenDebugInfoAsError;
319 bool hasBrokenDebugInfo() const { return BrokenDebugInfo; }
321 bool verify(const Function &F) {
322 assert(F.getParent() == &M &&
323 "An instance of this class only works with a specific module!");
325 // First ensure the function is well-enough formed to compute dominance
326 // information, and directly compute a dominance tree. We don't rely on the
327 // pass manager to provide this as it isolates us from a potentially
328 // out-of-date dominator tree and makes it significantly more complex to run
329 // this code outside of a pass manager.
330 // FIXME: It's really gross that we have to cast away constness here.
331 if (!F.empty())
332 DT.recalculate(const_cast<Function &>(F));
334 for (const BasicBlock &BB : F) {
335 if (!BB.empty() && BB.back().isTerminator())
336 continue;
338 if (OS) {
339 *OS << "Basic Block in function '" << F.getName()
340 << "' does not have terminator!\n";
341 BB.printAsOperand(*OS, true, MST);
342 *OS << "\n";
344 return false;
347 Broken = false;
348 // FIXME: We strip const here because the inst visitor strips const.
349 visit(const_cast<Function &>(F));
350 verifySiblingFuncletUnwinds();
351 InstsInThisBlock.clear();
352 DebugFnArgs.clear();
353 LandingPadResultTy = nullptr;
354 SawFrameEscape = false;
355 SiblingFuncletInfo.clear();
357 return !Broken;
360 /// Verify the module that this instance of \c Verifier was initialized with.
361 bool verify() {
362 Broken = false;
364 // Collect all declarations of the llvm.experimental.deoptimize intrinsic.
365 for (const Function &F : M)
366 if (F.getIntrinsicID() == Intrinsic::experimental_deoptimize)
367 DeoptimizeDeclarations.push_back(&F);
369 // Now that we've visited every function, verify that we never asked to
370 // recover a frame index that wasn't escaped.
371 verifyFrameRecoverIndices();
372 for (const GlobalVariable &GV : M.globals())
373 visitGlobalVariable(GV);
375 for (const GlobalAlias &GA : M.aliases())
376 visitGlobalAlias(GA);
378 for (const NamedMDNode &NMD : M.named_metadata())
379 visitNamedMDNode(NMD);
381 for (const StringMapEntry<Comdat> &SMEC : M.getComdatSymbolTable())
382 visitComdat(SMEC.getValue());
384 visitModuleFlags(M);
385 visitModuleIdents(M);
386 visitModuleCommandLines(M);
388 verifyCompileUnits();
390 verifyDeoptimizeCallingConvs();
391 DISubprogramAttachments.clear();
392 return !Broken;
395 private:
396 // Verification methods...
397 void visitGlobalValue(const GlobalValue &GV);
398 void visitGlobalVariable(const GlobalVariable &GV);
399 void visitGlobalAlias(const GlobalAlias &GA);
400 void visitAliaseeSubExpr(const GlobalAlias &A, const Constant &C);
401 void visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias *> &Visited,
402 const GlobalAlias &A, const Constant &C);
403 void visitNamedMDNode(const NamedMDNode &NMD);
404 void visitMDNode(const MDNode &MD);
405 void visitMetadataAsValue(const MetadataAsValue &MD, Function *F);
406 void visitValueAsMetadata(const ValueAsMetadata &MD, Function *F);
407 void visitComdat(const Comdat &C);
408 void visitModuleIdents(const Module &M);
409 void visitModuleCommandLines(const Module &M);
410 void visitModuleFlags(const Module &M);
411 void visitModuleFlag(const MDNode *Op,
412 DenseMap<const MDString *, const MDNode *> &SeenIDs,
413 SmallVectorImpl<const MDNode *> &Requirements);
414 void visitModuleFlagCGProfileEntry(const MDOperand &MDO);
415 void visitFunction(const Function &F);
416 void visitBasicBlock(BasicBlock &BB);
417 void visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty);
418 void visitDereferenceableMetadata(Instruction &I, MDNode *MD);
420 template <class Ty> bool isValidMetadataArray(const MDTuple &N);
421 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N);
422 #include "llvm/IR/Metadata.def"
423 void visitDIScope(const DIScope &N);
424 void visitDIVariable(const DIVariable &N);
425 void visitDILexicalBlockBase(const DILexicalBlockBase &N);
426 void visitDITemplateParameter(const DITemplateParameter &N);
428 void visitTemplateParams(const MDNode &N, const Metadata &RawParams);
430 // InstVisitor overrides...
431 using InstVisitor<Verifier>::visit;
432 void visit(Instruction &I);
434 void visitTruncInst(TruncInst &I);
435 void visitZExtInst(ZExtInst &I);
436 void visitSExtInst(SExtInst &I);
437 void visitFPTruncInst(FPTruncInst &I);
438 void visitFPExtInst(FPExtInst &I);
439 void visitFPToUIInst(FPToUIInst &I);
440 void visitFPToSIInst(FPToSIInst &I);
441 void visitUIToFPInst(UIToFPInst &I);
442 void visitSIToFPInst(SIToFPInst &I);
443 void visitIntToPtrInst(IntToPtrInst &I);
444 void visitPtrToIntInst(PtrToIntInst &I);
445 void visitBitCastInst(BitCastInst &I);
446 void visitAddrSpaceCastInst(AddrSpaceCastInst &I);
447 void visitPHINode(PHINode &PN);
448 void visitCallBase(CallBase &Call);
449 void visitUnaryOperator(UnaryOperator &U);
450 void visitBinaryOperator(BinaryOperator &B);
451 void visitICmpInst(ICmpInst &IC);
452 void visitFCmpInst(FCmpInst &FC);
453 void visitExtractElementInst(ExtractElementInst &EI);
454 void visitInsertElementInst(InsertElementInst &EI);
455 void visitShuffleVectorInst(ShuffleVectorInst &EI);
456 void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
457 void visitCallInst(CallInst &CI);
458 void visitInvokeInst(InvokeInst &II);
459 void visitGetElementPtrInst(GetElementPtrInst &GEP);
460 void visitLoadInst(LoadInst &LI);
461 void visitStoreInst(StoreInst &SI);
462 void verifyDominatesUse(Instruction &I, unsigned i);
463 void visitInstruction(Instruction &I);
464 void visitTerminator(Instruction &I);
465 void visitBranchInst(BranchInst &BI);
466 void visitReturnInst(ReturnInst &RI);
467 void visitSwitchInst(SwitchInst &SI);
468 void visitIndirectBrInst(IndirectBrInst &BI);
469 void visitCallBrInst(CallBrInst &CBI);
470 void visitSelectInst(SelectInst &SI);
471 void visitUserOp1(Instruction &I);
472 void visitUserOp2(Instruction &I) { visitUserOp1(I); }
473 void visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call);
474 void visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI);
475 void visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII);
476 void visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI);
477 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI);
478 void visitAtomicRMWInst(AtomicRMWInst &RMWI);
479 void visitFenceInst(FenceInst &FI);
480 void visitAllocaInst(AllocaInst &AI);
481 void visitExtractValueInst(ExtractValueInst &EVI);
482 void visitInsertValueInst(InsertValueInst &IVI);
483 void visitEHPadPredecessors(Instruction &I);
484 void visitLandingPadInst(LandingPadInst &LPI);
485 void visitResumeInst(ResumeInst &RI);
486 void visitCatchPadInst(CatchPadInst &CPI);
487 void visitCatchReturnInst(CatchReturnInst &CatchReturn);
488 void visitCleanupPadInst(CleanupPadInst &CPI);
489 void visitFuncletPadInst(FuncletPadInst &FPI);
490 void visitCatchSwitchInst(CatchSwitchInst &CatchSwitch);
491 void visitCleanupReturnInst(CleanupReturnInst &CRI);
493 void verifySwiftErrorCall(CallBase &Call, const Value *SwiftErrorVal);
494 void verifySwiftErrorValue(const Value *SwiftErrorVal);
495 void verifyMustTailCall(CallInst &CI);
496 bool performTypeCheck(Intrinsic::ID ID, Function *F, Type *Ty, int VT,
497 unsigned ArgNo, std::string &Suffix);
498 bool verifyAttributeCount(AttributeList Attrs, unsigned Params);
499 void verifyAttributeTypes(AttributeSet Attrs, bool IsFunction,
500 const Value *V);
501 void verifyParameterAttrs(AttributeSet Attrs, Type *Ty, const Value *V);
502 void verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
503 const Value *V, bool IsIntrinsic);
504 void verifyFunctionMetadata(ArrayRef<std::pair<unsigned, MDNode *>> MDs);
506 void visitConstantExprsRecursively(const Constant *EntryC);
507 void visitConstantExpr(const ConstantExpr *CE);
508 void verifyStatepoint(const CallBase &Call);
509 void verifyFrameRecoverIndices();
510 void verifySiblingFuncletUnwinds();
512 void verifyFragmentExpression(const DbgVariableIntrinsic &I);
513 template <typename ValueOrMetadata>
514 void verifyFragmentExpression(const DIVariable &V,
515 DIExpression::FragmentInfo Fragment,
516 ValueOrMetadata *Desc);
517 void verifyFnArgs(const DbgVariableIntrinsic &I);
519 /// Module-level debug info verification...
520 void verifyCompileUnits();
522 /// Module-level verification that all @llvm.experimental.deoptimize
523 /// declarations share the same calling convention.
524 void verifyDeoptimizeCallingConvs();
526 /// Verify all-or-nothing property of DIFile source attribute within a CU.
527 void verifySourceDebugInfo(const DICompileUnit &U, const DIFile &F);
530 } // end anonymous namespace
532 /// We know that cond should be true, if not print an error message.
533 #define Assert(C, ...) \
534 do { if (!(C)) { CheckFailed(__VA_ARGS__); return; } } while (false)
536 /// We know that a debug info condition should be true, if not print
537 /// an error message.
538 #define AssertDI(C, ...) \
539 do { if (!(C)) { DebugInfoCheckFailed(__VA_ARGS__); return; } } while (false)
541 void Verifier::visit(Instruction &I) {
542 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
543 Assert(I.getOperand(i) != nullptr, "Operand is null", &I);
544 InstVisitor<Verifier>::visit(I);
547 // Helper to recursively iterate over indirect users. By
548 // returning false, the callback can ask to stop recursing
549 // further.
550 static void forEachUser(const Value *User,
551 SmallPtrSet<const Value *, 32> &Visited,
552 llvm::function_ref<bool(const Value *)> Callback) {
553 if (!Visited.insert(User).second)
554 return;
555 for (const Value *TheNextUser : User->materialized_users())
556 if (Callback(TheNextUser))
557 forEachUser(TheNextUser, Visited, Callback);
560 void Verifier::visitGlobalValue(const GlobalValue &GV) {
561 Assert(!GV.isDeclaration() || GV.hasValidDeclarationLinkage(),
562 "Global is external, but doesn't have external or weak linkage!", &GV);
564 Assert(GV.getAlignment() <= Value::MaximumAlignment,
565 "huge alignment values are unsupported", &GV);
566 Assert(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
567 "Only global variables can have appending linkage!", &GV);
569 if (GV.hasAppendingLinkage()) {
570 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV);
571 Assert(GVar && GVar->getValueType()->isArrayTy(),
572 "Only global arrays can have appending linkage!", GVar);
575 if (GV.isDeclarationForLinker())
576 Assert(!GV.hasComdat(), "Declaration may not be in a Comdat!", &GV);
578 if (GV.hasDLLImportStorageClass()) {
579 Assert(!GV.isDSOLocal(),
580 "GlobalValue with DLLImport Storage is dso_local!", &GV);
582 Assert((GV.isDeclaration() && GV.hasExternalLinkage()) ||
583 GV.hasAvailableExternallyLinkage(),
584 "Global is marked as dllimport, but not external", &GV);
587 if (GV.hasLocalLinkage())
588 Assert(GV.isDSOLocal(),
589 "GlobalValue with private or internal linkage must be dso_local!",
590 &GV);
592 if (!GV.hasDefaultVisibility() && !GV.hasExternalWeakLinkage())
593 Assert(GV.isDSOLocal(),
594 "GlobalValue with non default visibility must be dso_local!", &GV);
596 forEachUser(&GV, GlobalValueVisited, [&](const Value *V) -> bool {
597 if (const Instruction *I = dyn_cast<Instruction>(V)) {
598 if (!I->getParent() || !I->getParent()->getParent())
599 CheckFailed("Global is referenced by parentless instruction!", &GV, &M,
601 else if (I->getParent()->getParent()->getParent() != &M)
602 CheckFailed("Global is referenced in a different module!", &GV, &M, I,
603 I->getParent()->getParent(),
604 I->getParent()->getParent()->getParent());
605 return false;
606 } else if (const Function *F = dyn_cast<Function>(V)) {
607 if (F->getParent() != &M)
608 CheckFailed("Global is used by function in a different module", &GV, &M,
609 F, F->getParent());
610 return false;
612 return true;
616 void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
617 if (GV.hasInitializer()) {
618 Assert(GV.getInitializer()->getType() == GV.getValueType(),
619 "Global variable initializer type does not match global "
620 "variable type!",
621 &GV);
622 // If the global has common linkage, it must have a zero initializer and
623 // cannot be constant.
624 if (GV.hasCommonLinkage()) {
625 Assert(GV.getInitializer()->isNullValue(),
626 "'common' global must have a zero initializer!", &GV);
627 Assert(!GV.isConstant(), "'common' global may not be marked constant!",
628 &GV);
629 Assert(!GV.hasComdat(), "'common' global may not be in a Comdat!", &GV);
633 if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
634 GV.getName() == "llvm.global_dtors")) {
635 Assert(!GV.hasInitializer() || GV.hasAppendingLinkage(),
636 "invalid linkage for intrinsic global variable", &GV);
637 // Don't worry about emitting an error for it not being an array,
638 // visitGlobalValue will complain on appending non-array.
639 if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getValueType())) {
640 StructType *STy = dyn_cast<StructType>(ATy->getElementType());
641 PointerType *FuncPtrTy =
642 FunctionType::get(Type::getVoidTy(Context), false)->
643 getPointerTo(DL.getProgramAddressSpace());
644 Assert(STy &&
645 (STy->getNumElements() == 2 || STy->getNumElements() == 3) &&
646 STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
647 STy->getTypeAtIndex(1) == FuncPtrTy,
648 "wrong type for intrinsic global variable", &GV);
649 Assert(STy->getNumElements() == 3,
650 "the third field of the element type is mandatory, "
651 "specify i8* null to migrate from the obsoleted 2-field form");
652 Type *ETy = STy->getTypeAtIndex(2);
653 Assert(ETy->isPointerTy() &&
654 cast<PointerType>(ETy)->getElementType()->isIntegerTy(8),
655 "wrong type for intrinsic global variable", &GV);
659 if (GV.hasName() && (GV.getName() == "llvm.used" ||
660 GV.getName() == "llvm.compiler.used")) {
661 Assert(!GV.hasInitializer() || GV.hasAppendingLinkage(),
662 "invalid linkage for intrinsic global variable", &GV);
663 Type *GVType = GV.getValueType();
664 if (ArrayType *ATy = dyn_cast<ArrayType>(GVType)) {
665 PointerType *PTy = dyn_cast<PointerType>(ATy->getElementType());
666 Assert(PTy, "wrong type for intrinsic global variable", &GV);
667 if (GV.hasInitializer()) {
668 const Constant *Init = GV.getInitializer();
669 const ConstantArray *InitArray = dyn_cast<ConstantArray>(Init);
670 Assert(InitArray, "wrong initalizer for intrinsic global variable",
671 Init);
672 for (Value *Op : InitArray->operands()) {
673 Value *V = Op->stripPointerCastsNoFollowAliases();
674 Assert(isa<GlobalVariable>(V) || isa<Function>(V) ||
675 isa<GlobalAlias>(V),
676 "invalid llvm.used member", V);
677 Assert(V->hasName(), "members of llvm.used must be named", V);
683 // Visit any debug info attachments.
684 SmallVector<MDNode *, 1> MDs;
685 GV.getMetadata(LLVMContext::MD_dbg, MDs);
686 for (auto *MD : MDs) {
687 if (auto *GVE = dyn_cast<DIGlobalVariableExpression>(MD))
688 visitDIGlobalVariableExpression(*GVE);
689 else
690 AssertDI(false, "!dbg attachment of global variable must be a "
691 "DIGlobalVariableExpression");
694 // Scalable vectors cannot be global variables, since we don't know
695 // the runtime size. If the global is a struct or an array containing
696 // scalable vectors, that will be caught by the isValidElementType methods
697 // in StructType or ArrayType instead.
698 if (auto *VTy = dyn_cast<VectorType>(GV.getValueType()))
699 Assert(!VTy->isScalable(), "Globals cannot contain scalable vectors", &GV);
701 if (!GV.hasInitializer()) {
702 visitGlobalValue(GV);
703 return;
706 // Walk any aggregate initializers looking for bitcasts between address spaces
707 visitConstantExprsRecursively(GV.getInitializer());
709 visitGlobalValue(GV);
712 void Verifier::visitAliaseeSubExpr(const GlobalAlias &GA, const Constant &C) {
713 SmallPtrSet<const GlobalAlias*, 4> Visited;
714 Visited.insert(&GA);
715 visitAliaseeSubExpr(Visited, GA, C);
718 void Verifier::visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias*> &Visited,
719 const GlobalAlias &GA, const Constant &C) {
720 if (const auto *GV = dyn_cast<GlobalValue>(&C)) {
721 Assert(!GV->isDeclarationForLinker(), "Alias must point to a definition",
722 &GA);
724 if (const auto *GA2 = dyn_cast<GlobalAlias>(GV)) {
725 Assert(Visited.insert(GA2).second, "Aliases cannot form a cycle", &GA);
727 Assert(!GA2->isInterposable(), "Alias cannot point to an interposable alias",
728 &GA);
729 } else {
730 // Only continue verifying subexpressions of GlobalAliases.
731 // Do not recurse into global initializers.
732 return;
736 if (const auto *CE = dyn_cast<ConstantExpr>(&C))
737 visitConstantExprsRecursively(CE);
739 for (const Use &U : C.operands()) {
740 Value *V = &*U;
741 if (const auto *GA2 = dyn_cast<GlobalAlias>(V))
742 visitAliaseeSubExpr(Visited, GA, *GA2->getAliasee());
743 else if (const auto *C2 = dyn_cast<Constant>(V))
744 visitAliaseeSubExpr(Visited, GA, *C2);
748 void Verifier::visitGlobalAlias(const GlobalAlias &GA) {
749 Assert(GlobalAlias::isValidLinkage(GA.getLinkage()),
750 "Alias should have private, internal, linkonce, weak, linkonce_odr, "
751 "weak_odr, or external linkage!",
752 &GA);
753 const Constant *Aliasee = GA.getAliasee();
754 Assert(Aliasee, "Aliasee cannot be NULL!", &GA);
755 Assert(GA.getType() == Aliasee->getType(),
756 "Alias and aliasee types should match!", &GA);
758 Assert(isa<GlobalValue>(Aliasee) || isa<ConstantExpr>(Aliasee),
759 "Aliasee should be either GlobalValue or ConstantExpr", &GA);
761 visitAliaseeSubExpr(GA, *Aliasee);
763 visitGlobalValue(GA);
766 void Verifier::visitNamedMDNode(const NamedMDNode &NMD) {
767 // There used to be various other llvm.dbg.* nodes, but we don't support
768 // upgrading them and we want to reserve the namespace for future uses.
769 if (NMD.getName().startswith("llvm.dbg."))
770 AssertDI(NMD.getName() == "llvm.dbg.cu",
771 "unrecognized named metadata node in the llvm.dbg namespace",
772 &NMD);
773 for (const MDNode *MD : NMD.operands()) {
774 if (NMD.getName() == "llvm.dbg.cu")
775 AssertDI(MD && isa<DICompileUnit>(MD), "invalid compile unit", &NMD, MD);
777 if (!MD)
778 continue;
780 visitMDNode(*MD);
784 void Verifier::visitMDNode(const MDNode &MD) {
785 // Only visit each node once. Metadata can be mutually recursive, so this
786 // avoids infinite recursion here, as well as being an optimization.
787 if (!MDNodes.insert(&MD).second)
788 return;
790 switch (MD.getMetadataID()) {
791 default:
792 llvm_unreachable("Invalid MDNode subclass");
793 case Metadata::MDTupleKind:
794 break;
795 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
796 case Metadata::CLASS##Kind: \
797 visit##CLASS(cast<CLASS>(MD)); \
798 break;
799 #include "llvm/IR/Metadata.def"
802 for (const Metadata *Op : MD.operands()) {
803 if (!Op)
804 continue;
805 Assert(!isa<LocalAsMetadata>(Op), "Invalid operand for global metadata!",
806 &MD, Op);
807 if (auto *N = dyn_cast<MDNode>(Op)) {
808 visitMDNode(*N);
809 continue;
811 if (auto *V = dyn_cast<ValueAsMetadata>(Op)) {
812 visitValueAsMetadata(*V, nullptr);
813 continue;
817 // Check these last, so we diagnose problems in operands first.
818 Assert(!MD.isTemporary(), "Expected no forward declarations!", &MD);
819 Assert(MD.isResolved(), "All nodes should be resolved!", &MD);
822 void Verifier::visitValueAsMetadata(const ValueAsMetadata &MD, Function *F) {
823 Assert(MD.getValue(), "Expected valid value", &MD);
824 Assert(!MD.getValue()->getType()->isMetadataTy(),
825 "Unexpected metadata round-trip through values", &MD, MD.getValue());
827 auto *L = dyn_cast<LocalAsMetadata>(&MD);
828 if (!L)
829 return;
831 Assert(F, "function-local metadata used outside a function", L);
833 // If this was an instruction, bb, or argument, verify that it is in the
834 // function that we expect.
835 Function *ActualF = nullptr;
836 if (Instruction *I = dyn_cast<Instruction>(L->getValue())) {
837 Assert(I->getParent(), "function-local metadata not in basic block", L, I);
838 ActualF = I->getParent()->getParent();
839 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(L->getValue()))
840 ActualF = BB->getParent();
841 else if (Argument *A = dyn_cast<Argument>(L->getValue()))
842 ActualF = A->getParent();
843 assert(ActualF && "Unimplemented function local metadata case!");
845 Assert(ActualF == F, "function-local metadata used in wrong function", L);
848 void Verifier::visitMetadataAsValue(const MetadataAsValue &MDV, Function *F) {
849 Metadata *MD = MDV.getMetadata();
850 if (auto *N = dyn_cast<MDNode>(MD)) {
851 visitMDNode(*N);
852 return;
855 // Only visit each node once. Metadata can be mutually recursive, so this
856 // avoids infinite recursion here, as well as being an optimization.
857 if (!MDNodes.insert(MD).second)
858 return;
860 if (auto *V = dyn_cast<ValueAsMetadata>(MD))
861 visitValueAsMetadata(*V, F);
864 static bool isType(const Metadata *MD) { return !MD || isa<DIType>(MD); }
865 static bool isScope(const Metadata *MD) { return !MD || isa<DIScope>(MD); }
866 static bool isDINode(const Metadata *MD) { return !MD || isa<DINode>(MD); }
868 void Verifier::visitDILocation(const DILocation &N) {
869 AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
870 "location requires a valid scope", &N, N.getRawScope());
871 if (auto *IA = N.getRawInlinedAt())
872 AssertDI(isa<DILocation>(IA), "inlined-at should be a location", &N, IA);
873 if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
874 AssertDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
877 void Verifier::visitGenericDINode(const GenericDINode &N) {
878 AssertDI(N.getTag(), "invalid tag", &N);
881 void Verifier::visitDIScope(const DIScope &N) {
882 if (auto *F = N.getRawFile())
883 AssertDI(isa<DIFile>(F), "invalid file", &N, F);
886 void Verifier::visitDISubrange(const DISubrange &N) {
887 AssertDI(N.getTag() == dwarf::DW_TAG_subrange_type, "invalid tag", &N);
888 auto Count = N.getCount();
889 AssertDI(Count, "Count must either be a signed constant or a DIVariable",
890 &N);
891 AssertDI(!Count.is<ConstantInt*>() ||
892 Count.get<ConstantInt*>()->getSExtValue() >= -1,
893 "invalid subrange count", &N);
896 void Verifier::visitDIEnumerator(const DIEnumerator &N) {
897 AssertDI(N.getTag() == dwarf::DW_TAG_enumerator, "invalid tag", &N);
900 void Verifier::visitDIBasicType(const DIBasicType &N) {
901 AssertDI(N.getTag() == dwarf::DW_TAG_base_type ||
902 N.getTag() == dwarf::DW_TAG_unspecified_type,
903 "invalid tag", &N);
904 AssertDI(!(N.isBigEndian() && N.isLittleEndian()) ,
905 "has conflicting flags", &N);
908 void Verifier::visitDIDerivedType(const DIDerivedType &N) {
909 // Common scope checks.
910 visitDIScope(N);
912 AssertDI(N.getTag() == dwarf::DW_TAG_typedef ||
913 N.getTag() == dwarf::DW_TAG_pointer_type ||
914 N.getTag() == dwarf::DW_TAG_ptr_to_member_type ||
915 N.getTag() == dwarf::DW_TAG_reference_type ||
916 N.getTag() == dwarf::DW_TAG_rvalue_reference_type ||
917 N.getTag() == dwarf::DW_TAG_const_type ||
918 N.getTag() == dwarf::DW_TAG_volatile_type ||
919 N.getTag() == dwarf::DW_TAG_restrict_type ||
920 N.getTag() == dwarf::DW_TAG_atomic_type ||
921 N.getTag() == dwarf::DW_TAG_member ||
922 N.getTag() == dwarf::DW_TAG_inheritance ||
923 N.getTag() == dwarf::DW_TAG_friend,
924 "invalid tag", &N);
925 if (N.getTag() == dwarf::DW_TAG_ptr_to_member_type) {
926 AssertDI(isType(N.getRawExtraData()), "invalid pointer to member type", &N,
927 N.getRawExtraData());
930 AssertDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
931 AssertDI(isType(N.getRawBaseType()), "invalid base type", &N,
932 N.getRawBaseType());
934 if (N.getDWARFAddressSpace()) {
935 AssertDI(N.getTag() == dwarf::DW_TAG_pointer_type ||
936 N.getTag() == dwarf::DW_TAG_reference_type ||
937 N.getTag() == dwarf::DW_TAG_rvalue_reference_type,
938 "DWARF address space only applies to pointer or reference types",
939 &N);
943 /// Detect mutually exclusive flags.
944 static bool hasConflictingReferenceFlags(unsigned Flags) {
945 return ((Flags & DINode::FlagLValueReference) &&
946 (Flags & DINode::FlagRValueReference)) ||
947 ((Flags & DINode::FlagTypePassByValue) &&
948 (Flags & DINode::FlagTypePassByReference));
951 void Verifier::visitTemplateParams(const MDNode &N, const Metadata &RawParams) {
952 auto *Params = dyn_cast<MDTuple>(&RawParams);
953 AssertDI(Params, "invalid template params", &N, &RawParams);
954 for (Metadata *Op : Params->operands()) {
955 AssertDI(Op && isa<DITemplateParameter>(Op), "invalid template parameter",
956 &N, Params, Op);
960 void Verifier::visitDICompositeType(const DICompositeType &N) {
961 // Common scope checks.
962 visitDIScope(N);
964 AssertDI(N.getTag() == dwarf::DW_TAG_array_type ||
965 N.getTag() == dwarf::DW_TAG_structure_type ||
966 N.getTag() == dwarf::DW_TAG_union_type ||
967 N.getTag() == dwarf::DW_TAG_enumeration_type ||
968 N.getTag() == dwarf::DW_TAG_class_type ||
969 N.getTag() == dwarf::DW_TAG_variant_part,
970 "invalid tag", &N);
972 AssertDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
973 AssertDI(isType(N.getRawBaseType()), "invalid base type", &N,
974 N.getRawBaseType());
976 AssertDI(!N.getRawElements() || isa<MDTuple>(N.getRawElements()),
977 "invalid composite elements", &N, N.getRawElements());
978 AssertDI(isType(N.getRawVTableHolder()), "invalid vtable holder", &N,
979 N.getRawVTableHolder());
980 AssertDI(!hasConflictingReferenceFlags(N.getFlags()),
981 "invalid reference flags", &N);
983 if (N.isVector()) {
984 const DINodeArray Elements = N.getElements();
985 AssertDI(Elements.size() == 1 &&
986 Elements[0]->getTag() == dwarf::DW_TAG_subrange_type,
987 "invalid vector, expected one element of type subrange", &N);
990 if (auto *Params = N.getRawTemplateParams())
991 visitTemplateParams(N, *Params);
993 if (N.getTag() == dwarf::DW_TAG_class_type ||
994 N.getTag() == dwarf::DW_TAG_union_type) {
995 AssertDI(N.getFile() && !N.getFile()->getFilename().empty(),
996 "class/union requires a filename", &N, N.getFile());
999 if (auto *D = N.getRawDiscriminator()) {
1000 AssertDI(isa<DIDerivedType>(D) && N.getTag() == dwarf::DW_TAG_variant_part,
1001 "discriminator can only appear on variant part");
1005 void Verifier::visitDISubroutineType(const DISubroutineType &N) {
1006 AssertDI(N.getTag() == dwarf::DW_TAG_subroutine_type, "invalid tag", &N);
1007 if (auto *Types = N.getRawTypeArray()) {
1008 AssertDI(isa<MDTuple>(Types), "invalid composite elements", &N, Types);
1009 for (Metadata *Ty : N.getTypeArray()->operands()) {
1010 AssertDI(isType(Ty), "invalid subroutine type ref", &N, Types, Ty);
1013 AssertDI(!hasConflictingReferenceFlags(N.getFlags()),
1014 "invalid reference flags", &N);
1017 void Verifier::visitDIFile(const DIFile &N) {
1018 AssertDI(N.getTag() == dwarf::DW_TAG_file_type, "invalid tag", &N);
1019 Optional<DIFile::ChecksumInfo<StringRef>> Checksum = N.getChecksum();
1020 if (Checksum) {
1021 AssertDI(Checksum->Kind <= DIFile::ChecksumKind::CSK_Last,
1022 "invalid checksum kind", &N);
1023 size_t Size;
1024 switch (Checksum->Kind) {
1025 case DIFile::CSK_MD5:
1026 Size = 32;
1027 break;
1028 case DIFile::CSK_SHA1:
1029 Size = 40;
1030 break;
1032 AssertDI(Checksum->Value.size() == Size, "invalid checksum length", &N);
1033 AssertDI(Checksum->Value.find_if_not(llvm::isHexDigit) == StringRef::npos,
1034 "invalid checksum", &N);
1038 void Verifier::visitDICompileUnit(const DICompileUnit &N) {
1039 AssertDI(N.isDistinct(), "compile units must be distinct", &N);
1040 AssertDI(N.getTag() == dwarf::DW_TAG_compile_unit, "invalid tag", &N);
1042 // Don't bother verifying the compilation directory or producer string
1043 // as those could be empty.
1044 AssertDI(N.getRawFile() && isa<DIFile>(N.getRawFile()), "invalid file", &N,
1045 N.getRawFile());
1046 AssertDI(!N.getFile()->getFilename().empty(), "invalid filename", &N,
1047 N.getFile());
1049 verifySourceDebugInfo(N, *N.getFile());
1051 AssertDI((N.getEmissionKind() <= DICompileUnit::LastEmissionKind),
1052 "invalid emission kind", &N);
1054 if (auto *Array = N.getRawEnumTypes()) {
1055 AssertDI(isa<MDTuple>(Array), "invalid enum list", &N, Array);
1056 for (Metadata *Op : N.getEnumTypes()->operands()) {
1057 auto *Enum = dyn_cast_or_null<DICompositeType>(Op);
1058 AssertDI(Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type,
1059 "invalid enum type", &N, N.getEnumTypes(), Op);
1062 if (auto *Array = N.getRawRetainedTypes()) {
1063 AssertDI(isa<MDTuple>(Array), "invalid retained type list", &N, Array);
1064 for (Metadata *Op : N.getRetainedTypes()->operands()) {
1065 AssertDI(Op && (isa<DIType>(Op) ||
1066 (isa<DISubprogram>(Op) &&
1067 !cast<DISubprogram>(Op)->isDefinition())),
1068 "invalid retained type", &N, Op);
1071 if (auto *Array = N.getRawGlobalVariables()) {
1072 AssertDI(isa<MDTuple>(Array), "invalid global variable list", &N, Array);
1073 for (Metadata *Op : N.getGlobalVariables()->operands()) {
1074 AssertDI(Op && (isa<DIGlobalVariableExpression>(Op)),
1075 "invalid global variable ref", &N, Op);
1078 if (auto *Array = N.getRawImportedEntities()) {
1079 AssertDI(isa<MDTuple>(Array), "invalid imported entity list", &N, Array);
1080 for (Metadata *Op : N.getImportedEntities()->operands()) {
1081 AssertDI(Op && isa<DIImportedEntity>(Op), "invalid imported entity ref",
1082 &N, Op);
1085 if (auto *Array = N.getRawMacros()) {
1086 AssertDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
1087 for (Metadata *Op : N.getMacros()->operands()) {
1088 AssertDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
1091 CUVisited.insert(&N);
1094 void Verifier::visitDISubprogram(const DISubprogram &N) {
1095 AssertDI(N.getTag() == dwarf::DW_TAG_subprogram, "invalid tag", &N);
1096 AssertDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
1097 if (auto *F = N.getRawFile())
1098 AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1099 else
1100 AssertDI(N.getLine() == 0, "line specified with no file", &N, N.getLine());
1101 if (auto *T = N.getRawType())
1102 AssertDI(isa<DISubroutineType>(T), "invalid subroutine type", &N, T);
1103 AssertDI(isType(N.getRawContainingType()), "invalid containing type", &N,
1104 N.getRawContainingType());
1105 if (auto *Params = N.getRawTemplateParams())
1106 visitTemplateParams(N, *Params);
1107 if (auto *S = N.getRawDeclaration())
1108 AssertDI(isa<DISubprogram>(S) && !cast<DISubprogram>(S)->isDefinition(),
1109 "invalid subprogram declaration", &N, S);
1110 if (auto *RawNode = N.getRawRetainedNodes()) {
1111 auto *Node = dyn_cast<MDTuple>(RawNode);
1112 AssertDI(Node, "invalid retained nodes list", &N, RawNode);
1113 for (Metadata *Op : Node->operands()) {
1114 AssertDI(Op && (isa<DILocalVariable>(Op) || isa<DILabel>(Op)),
1115 "invalid retained nodes, expected DILocalVariable or DILabel",
1116 &N, Node, Op);
1119 AssertDI(!hasConflictingReferenceFlags(N.getFlags()),
1120 "invalid reference flags", &N);
1122 auto *Unit = N.getRawUnit();
1123 if (N.isDefinition()) {
1124 // Subprogram definitions (not part of the type hierarchy).
1125 AssertDI(N.isDistinct(), "subprogram definitions must be distinct", &N);
1126 AssertDI(Unit, "subprogram definitions must have a compile unit", &N);
1127 AssertDI(isa<DICompileUnit>(Unit), "invalid unit type", &N, Unit);
1128 if (N.getFile())
1129 verifySourceDebugInfo(*N.getUnit(), *N.getFile());
1130 } else {
1131 // Subprogram declarations (part of the type hierarchy).
1132 AssertDI(!Unit, "subprogram declarations must not have a compile unit", &N);
1135 if (auto *RawThrownTypes = N.getRawThrownTypes()) {
1136 auto *ThrownTypes = dyn_cast<MDTuple>(RawThrownTypes);
1137 AssertDI(ThrownTypes, "invalid thrown types list", &N, RawThrownTypes);
1138 for (Metadata *Op : ThrownTypes->operands())
1139 AssertDI(Op && isa<DIType>(Op), "invalid thrown type", &N, ThrownTypes,
1140 Op);
1143 if (N.areAllCallsDescribed())
1144 AssertDI(N.isDefinition(),
1145 "DIFlagAllCallsDescribed must be attached to a definition");
1148 void Verifier::visitDILexicalBlockBase(const DILexicalBlockBase &N) {
1149 AssertDI(N.getTag() == dwarf::DW_TAG_lexical_block, "invalid tag", &N);
1150 AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1151 "invalid local scope", &N, N.getRawScope());
1152 if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
1153 AssertDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
1156 void Verifier::visitDILexicalBlock(const DILexicalBlock &N) {
1157 visitDILexicalBlockBase(N);
1159 AssertDI(N.getLine() || !N.getColumn(),
1160 "cannot have column info without line info", &N);
1163 void Verifier::visitDILexicalBlockFile(const DILexicalBlockFile &N) {
1164 visitDILexicalBlockBase(N);
1167 void Verifier::visitDICommonBlock(const DICommonBlock &N) {
1168 AssertDI(N.getTag() == dwarf::DW_TAG_common_block, "invalid tag", &N);
1169 if (auto *S = N.getRawScope())
1170 AssertDI(isa<DIScope>(S), "invalid scope ref", &N, S);
1171 if (auto *S = N.getRawDecl())
1172 AssertDI(isa<DIGlobalVariable>(S), "invalid declaration", &N, S);
1175 void Verifier::visitDINamespace(const DINamespace &N) {
1176 AssertDI(N.getTag() == dwarf::DW_TAG_namespace, "invalid tag", &N);
1177 if (auto *S = N.getRawScope())
1178 AssertDI(isa<DIScope>(S), "invalid scope ref", &N, S);
1181 void Verifier::visitDIMacro(const DIMacro &N) {
1182 AssertDI(N.getMacinfoType() == dwarf::DW_MACINFO_define ||
1183 N.getMacinfoType() == dwarf::DW_MACINFO_undef,
1184 "invalid macinfo type", &N);
1185 AssertDI(!N.getName().empty(), "anonymous macro", &N);
1186 if (!N.getValue().empty()) {
1187 assert(N.getValue().data()[0] != ' ' && "Macro value has a space prefix");
1191 void Verifier::visitDIMacroFile(const DIMacroFile &N) {
1192 AssertDI(N.getMacinfoType() == dwarf::DW_MACINFO_start_file,
1193 "invalid macinfo type", &N);
1194 if (auto *F = N.getRawFile())
1195 AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1197 if (auto *Array = N.getRawElements()) {
1198 AssertDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
1199 for (Metadata *Op : N.getElements()->operands()) {
1200 AssertDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
1205 void Verifier::visitDIModule(const DIModule &N) {
1206 AssertDI(N.getTag() == dwarf::DW_TAG_module, "invalid tag", &N);
1207 AssertDI(!N.getName().empty(), "anonymous module", &N);
1210 void Verifier::visitDITemplateParameter(const DITemplateParameter &N) {
1211 AssertDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1214 void Verifier::visitDITemplateTypeParameter(const DITemplateTypeParameter &N) {
1215 visitDITemplateParameter(N);
1217 AssertDI(N.getTag() == dwarf::DW_TAG_template_type_parameter, "invalid tag",
1218 &N);
1221 void Verifier::visitDITemplateValueParameter(
1222 const DITemplateValueParameter &N) {
1223 visitDITemplateParameter(N);
1225 AssertDI(N.getTag() == dwarf::DW_TAG_template_value_parameter ||
1226 N.getTag() == dwarf::DW_TAG_GNU_template_template_param ||
1227 N.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack,
1228 "invalid tag", &N);
1231 void Verifier::visitDIVariable(const DIVariable &N) {
1232 if (auto *S = N.getRawScope())
1233 AssertDI(isa<DIScope>(S), "invalid scope", &N, S);
1234 if (auto *F = N.getRawFile())
1235 AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1238 void Verifier::visitDIGlobalVariable(const DIGlobalVariable &N) {
1239 // Checks common to all variables.
1240 visitDIVariable(N);
1242 AssertDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
1243 AssertDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1244 AssertDI(N.getType(), "missing global variable type", &N);
1245 if (auto *Member = N.getRawStaticDataMemberDeclaration()) {
1246 AssertDI(isa<DIDerivedType>(Member),
1247 "invalid static data member declaration", &N, Member);
1251 void Verifier::visitDILocalVariable(const DILocalVariable &N) {
1252 // Checks common to all variables.
1253 visitDIVariable(N);
1255 AssertDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1256 AssertDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
1257 AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1258 "local variable requires a valid scope", &N, N.getRawScope());
1259 if (auto Ty = N.getType())
1260 AssertDI(!isa<DISubroutineType>(Ty), "invalid type", &N, N.getType());
1263 void Verifier::visitDILabel(const DILabel &N) {
1264 if (auto *S = N.getRawScope())
1265 AssertDI(isa<DIScope>(S), "invalid scope", &N, S);
1266 if (auto *F = N.getRawFile())
1267 AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1269 AssertDI(N.getTag() == dwarf::DW_TAG_label, "invalid tag", &N);
1270 AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1271 "label requires a valid scope", &N, N.getRawScope());
1274 void Verifier::visitDIExpression(const DIExpression &N) {
1275 AssertDI(N.isValid(), "invalid expression", &N);
1278 void Verifier::visitDIGlobalVariableExpression(
1279 const DIGlobalVariableExpression &GVE) {
1280 AssertDI(GVE.getVariable(), "missing variable");
1281 if (auto *Var = GVE.getVariable())
1282 visitDIGlobalVariable(*Var);
1283 if (auto *Expr = GVE.getExpression()) {
1284 visitDIExpression(*Expr);
1285 if (auto Fragment = Expr->getFragmentInfo())
1286 verifyFragmentExpression(*GVE.getVariable(), *Fragment, &GVE);
1290 void Verifier::visitDIObjCProperty(const DIObjCProperty &N) {
1291 AssertDI(N.getTag() == dwarf::DW_TAG_APPLE_property, "invalid tag", &N);
1292 if (auto *T = N.getRawType())
1293 AssertDI(isType(T), "invalid type ref", &N, T);
1294 if (auto *F = N.getRawFile())
1295 AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1298 void Verifier::visitDIImportedEntity(const DIImportedEntity &N) {
1299 AssertDI(N.getTag() == dwarf::DW_TAG_imported_module ||
1300 N.getTag() == dwarf::DW_TAG_imported_declaration,
1301 "invalid tag", &N);
1302 if (auto *S = N.getRawScope())
1303 AssertDI(isa<DIScope>(S), "invalid scope for imported entity", &N, S);
1304 AssertDI(isDINode(N.getRawEntity()), "invalid imported entity", &N,
1305 N.getRawEntity());
1308 void Verifier::visitComdat(const Comdat &C) {
1309 // The Module is invalid if the GlobalValue has private linkage. Entities
1310 // with private linkage don't have entries in the symbol table.
1311 if (const GlobalValue *GV = M.getNamedValue(C.getName()))
1312 Assert(!GV->hasPrivateLinkage(), "comdat global value has private linkage",
1313 GV);
1316 void Verifier::visitModuleIdents(const Module &M) {
1317 const NamedMDNode *Idents = M.getNamedMetadata("llvm.ident");
1318 if (!Idents)
1319 return;
1321 // llvm.ident takes a list of metadata entry. Each entry has only one string.
1322 // Scan each llvm.ident entry and make sure that this requirement is met.
1323 for (const MDNode *N : Idents->operands()) {
1324 Assert(N->getNumOperands() == 1,
1325 "incorrect number of operands in llvm.ident metadata", N);
1326 Assert(dyn_cast_or_null<MDString>(N->getOperand(0)),
1327 ("invalid value for llvm.ident metadata entry operand"
1328 "(the operand should be a string)"),
1329 N->getOperand(0));
1333 void Verifier::visitModuleCommandLines(const Module &M) {
1334 const NamedMDNode *CommandLines = M.getNamedMetadata("llvm.commandline");
1335 if (!CommandLines)
1336 return;
1338 // llvm.commandline takes a list of metadata entry. Each entry has only one
1339 // string. Scan each llvm.commandline entry and make sure that this
1340 // requirement is met.
1341 for (const MDNode *N : CommandLines->operands()) {
1342 Assert(N->getNumOperands() == 1,
1343 "incorrect number of operands in llvm.commandline metadata", N);
1344 Assert(dyn_cast_or_null<MDString>(N->getOperand(0)),
1345 ("invalid value for llvm.commandline metadata entry operand"
1346 "(the operand should be a string)"),
1347 N->getOperand(0));
1351 void Verifier::visitModuleFlags(const Module &M) {
1352 const NamedMDNode *Flags = M.getModuleFlagsMetadata();
1353 if (!Flags) return;
1355 // Scan each flag, and track the flags and requirements.
1356 DenseMap<const MDString*, const MDNode*> SeenIDs;
1357 SmallVector<const MDNode*, 16> Requirements;
1358 for (const MDNode *MDN : Flags->operands())
1359 visitModuleFlag(MDN, SeenIDs, Requirements);
1361 // Validate that the requirements in the module are valid.
1362 for (const MDNode *Requirement : Requirements) {
1363 const MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1364 const Metadata *ReqValue = Requirement->getOperand(1);
1366 const MDNode *Op = SeenIDs.lookup(Flag);
1367 if (!Op) {
1368 CheckFailed("invalid requirement on flag, flag is not present in module",
1369 Flag);
1370 continue;
1373 if (Op->getOperand(2) != ReqValue) {
1374 CheckFailed(("invalid requirement on flag, "
1375 "flag does not have the required value"),
1376 Flag);
1377 continue;
1382 void
1383 Verifier::visitModuleFlag(const MDNode *Op,
1384 DenseMap<const MDString *, const MDNode *> &SeenIDs,
1385 SmallVectorImpl<const MDNode *> &Requirements) {
1386 // Each module flag should have three arguments, the merge behavior (a
1387 // constant int), the flag ID (an MDString), and the value.
1388 Assert(Op->getNumOperands() == 3,
1389 "incorrect number of operands in module flag", Op);
1390 Module::ModFlagBehavior MFB;
1391 if (!Module::isValidModFlagBehavior(Op->getOperand(0), MFB)) {
1392 Assert(
1393 mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0)),
1394 "invalid behavior operand in module flag (expected constant integer)",
1395 Op->getOperand(0));
1396 Assert(false,
1397 "invalid behavior operand in module flag (unexpected constant)",
1398 Op->getOperand(0));
1400 MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
1401 Assert(ID, "invalid ID operand in module flag (expected metadata string)",
1402 Op->getOperand(1));
1404 // Sanity check the values for behaviors with additional requirements.
1405 switch (MFB) {
1406 case Module::Error:
1407 case Module::Warning:
1408 case Module::Override:
1409 // These behavior types accept any value.
1410 break;
1412 case Module::Max: {
1413 Assert(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2)),
1414 "invalid value for 'max' module flag (expected constant integer)",
1415 Op->getOperand(2));
1416 break;
1419 case Module::Require: {
1420 // The value should itself be an MDNode with two operands, a flag ID (an
1421 // MDString), and a value.
1422 MDNode *Value = dyn_cast<MDNode>(Op->getOperand(2));
1423 Assert(Value && Value->getNumOperands() == 2,
1424 "invalid value for 'require' module flag (expected metadata pair)",
1425 Op->getOperand(2));
1426 Assert(isa<MDString>(Value->getOperand(0)),
1427 ("invalid value for 'require' module flag "
1428 "(first value operand should be a string)"),
1429 Value->getOperand(0));
1431 // Append it to the list of requirements, to check once all module flags are
1432 // scanned.
1433 Requirements.push_back(Value);
1434 break;
1437 case Module::Append:
1438 case Module::AppendUnique: {
1439 // These behavior types require the operand be an MDNode.
1440 Assert(isa<MDNode>(Op->getOperand(2)),
1441 "invalid value for 'append'-type module flag "
1442 "(expected a metadata node)",
1443 Op->getOperand(2));
1444 break;
1448 // Unless this is a "requires" flag, check the ID is unique.
1449 if (MFB != Module::Require) {
1450 bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
1451 Assert(Inserted,
1452 "module flag identifiers must be unique (or of 'require' type)", ID);
1455 if (ID->getString() == "wchar_size") {
1456 ConstantInt *Value
1457 = mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1458 Assert(Value, "wchar_size metadata requires constant integer argument");
1461 if (ID->getString() == "Linker Options") {
1462 // If the llvm.linker.options named metadata exists, we assume that the
1463 // bitcode reader has upgraded the module flag. Otherwise the flag might
1464 // have been created by a client directly.
1465 Assert(M.getNamedMetadata("llvm.linker.options"),
1466 "'Linker Options' named metadata no longer supported");
1469 if (ID->getString() == "CG Profile") {
1470 for (const MDOperand &MDO : cast<MDNode>(Op->getOperand(2))->operands())
1471 visitModuleFlagCGProfileEntry(MDO);
1475 void Verifier::visitModuleFlagCGProfileEntry(const MDOperand &MDO) {
1476 auto CheckFunction = [&](const MDOperand &FuncMDO) {
1477 if (!FuncMDO)
1478 return;
1479 auto F = dyn_cast<ValueAsMetadata>(FuncMDO);
1480 Assert(F && isa<Function>(F->getValue()), "expected a Function or null",
1481 FuncMDO);
1483 auto Node = dyn_cast_or_null<MDNode>(MDO);
1484 Assert(Node && Node->getNumOperands() == 3, "expected a MDNode triple", MDO);
1485 CheckFunction(Node->getOperand(0));
1486 CheckFunction(Node->getOperand(1));
1487 auto Count = dyn_cast_or_null<ConstantAsMetadata>(Node->getOperand(2));
1488 Assert(Count && Count->getType()->isIntegerTy(),
1489 "expected an integer constant", Node->getOperand(2));
1492 /// Return true if this attribute kind only applies to functions.
1493 static bool isFuncOnlyAttr(Attribute::AttrKind Kind) {
1494 switch (Kind) {
1495 case Attribute::NoReturn:
1496 case Attribute::NoSync:
1497 case Attribute::WillReturn:
1498 case Attribute::NoCfCheck:
1499 case Attribute::NoUnwind:
1500 case Attribute::NoInline:
1501 case Attribute::NoFree:
1502 case Attribute::AlwaysInline:
1503 case Attribute::OptimizeForSize:
1504 case Attribute::StackProtect:
1505 case Attribute::StackProtectReq:
1506 case Attribute::StackProtectStrong:
1507 case Attribute::SafeStack:
1508 case Attribute::ShadowCallStack:
1509 case Attribute::NoRedZone:
1510 case Attribute::NoImplicitFloat:
1511 case Attribute::Naked:
1512 case Attribute::InlineHint:
1513 case Attribute::StackAlignment:
1514 case Attribute::UWTable:
1515 case Attribute::NonLazyBind:
1516 case Attribute::ReturnsTwice:
1517 case Attribute::SanitizeAddress:
1518 case Attribute::SanitizeHWAddress:
1519 case Attribute::SanitizeMemTag:
1520 case Attribute::SanitizeThread:
1521 case Attribute::SanitizeMemory:
1522 case Attribute::MinSize:
1523 case Attribute::NoDuplicate:
1524 case Attribute::Builtin:
1525 case Attribute::NoBuiltin:
1526 case Attribute::Cold:
1527 case Attribute::OptForFuzzing:
1528 case Attribute::OptimizeNone:
1529 case Attribute::JumpTable:
1530 case Attribute::Convergent:
1531 case Attribute::ArgMemOnly:
1532 case Attribute::NoRecurse:
1533 case Attribute::InaccessibleMemOnly:
1534 case Attribute::InaccessibleMemOrArgMemOnly:
1535 case Attribute::AllocSize:
1536 case Attribute::SpeculativeLoadHardening:
1537 case Attribute::Speculatable:
1538 case Attribute::StrictFP:
1539 return true;
1540 default:
1541 break;
1543 return false;
1546 /// Return true if this is a function attribute that can also appear on
1547 /// arguments.
1548 static bool isFuncOrArgAttr(Attribute::AttrKind Kind) {
1549 return Kind == Attribute::ReadOnly || Kind == Attribute::WriteOnly ||
1550 Kind == Attribute::ReadNone;
1553 void Verifier::verifyAttributeTypes(AttributeSet Attrs, bool IsFunction,
1554 const Value *V) {
1555 for (Attribute A : Attrs) {
1556 if (A.isStringAttribute())
1557 continue;
1559 if (isFuncOnlyAttr(A.getKindAsEnum())) {
1560 if (!IsFunction) {
1561 CheckFailed("Attribute '" + A.getAsString() +
1562 "' only applies to functions!",
1564 return;
1566 } else if (IsFunction && !isFuncOrArgAttr(A.getKindAsEnum())) {
1567 CheckFailed("Attribute '" + A.getAsString() +
1568 "' does not apply to functions!",
1570 return;
1575 // VerifyParameterAttrs - Check the given attributes for an argument or return
1576 // value of the specified type. The value V is printed in error messages.
1577 void Verifier::verifyParameterAttrs(AttributeSet Attrs, Type *Ty,
1578 const Value *V) {
1579 if (!Attrs.hasAttributes())
1580 return;
1582 verifyAttributeTypes(Attrs, /*IsFunction=*/false, V);
1584 if (Attrs.hasAttribute(Attribute::ImmArg)) {
1585 Assert(Attrs.getNumAttributes() == 1,
1586 "Attribute 'immarg' is incompatible with other attributes", V);
1589 // Check for mutually incompatible attributes. Only inreg is compatible with
1590 // sret.
1591 unsigned AttrCount = 0;
1592 AttrCount += Attrs.hasAttribute(Attribute::ByVal);
1593 AttrCount += Attrs.hasAttribute(Attribute::InAlloca);
1594 AttrCount += Attrs.hasAttribute(Attribute::StructRet) ||
1595 Attrs.hasAttribute(Attribute::InReg);
1596 AttrCount += Attrs.hasAttribute(Attribute::Nest);
1597 Assert(AttrCount <= 1, "Attributes 'byval', 'inalloca', 'inreg', 'nest', "
1598 "and 'sret' are incompatible!",
1601 Assert(!(Attrs.hasAttribute(Attribute::InAlloca) &&
1602 Attrs.hasAttribute(Attribute::ReadOnly)),
1603 "Attributes "
1604 "'inalloca and readonly' are incompatible!",
1607 Assert(!(Attrs.hasAttribute(Attribute::StructRet) &&
1608 Attrs.hasAttribute(Attribute::Returned)),
1609 "Attributes "
1610 "'sret and returned' are incompatible!",
1613 Assert(!(Attrs.hasAttribute(Attribute::ZExt) &&
1614 Attrs.hasAttribute(Attribute::SExt)),
1615 "Attributes "
1616 "'zeroext and signext' are incompatible!",
1619 Assert(!(Attrs.hasAttribute(Attribute::ReadNone) &&
1620 Attrs.hasAttribute(Attribute::ReadOnly)),
1621 "Attributes "
1622 "'readnone and readonly' are incompatible!",
1625 Assert(!(Attrs.hasAttribute(Attribute::ReadNone) &&
1626 Attrs.hasAttribute(Attribute::WriteOnly)),
1627 "Attributes "
1628 "'readnone and writeonly' are incompatible!",
1631 Assert(!(Attrs.hasAttribute(Attribute::ReadOnly) &&
1632 Attrs.hasAttribute(Attribute::WriteOnly)),
1633 "Attributes "
1634 "'readonly and writeonly' are incompatible!",
1637 Assert(!(Attrs.hasAttribute(Attribute::NoInline) &&
1638 Attrs.hasAttribute(Attribute::AlwaysInline)),
1639 "Attributes "
1640 "'noinline and alwaysinline' are incompatible!",
1643 if (Attrs.hasAttribute(Attribute::ByVal) && Attrs.getByValType()) {
1644 Assert(Attrs.getByValType() == cast<PointerType>(Ty)->getElementType(),
1645 "Attribute 'byval' type does not match parameter!", V);
1648 AttrBuilder IncompatibleAttrs = AttributeFuncs::typeIncompatible(Ty);
1649 Assert(!AttrBuilder(Attrs).overlaps(IncompatibleAttrs),
1650 "Wrong types for attribute: " +
1651 AttributeSet::get(Context, IncompatibleAttrs).getAsString(),
1654 if (PointerType *PTy = dyn_cast<PointerType>(Ty)) {
1655 SmallPtrSet<Type*, 4> Visited;
1656 if (!PTy->getElementType()->isSized(&Visited)) {
1657 Assert(!Attrs.hasAttribute(Attribute::ByVal) &&
1658 !Attrs.hasAttribute(Attribute::InAlloca),
1659 "Attributes 'byval' and 'inalloca' do not support unsized types!",
1662 if (!isa<PointerType>(PTy->getElementType()))
1663 Assert(!Attrs.hasAttribute(Attribute::SwiftError),
1664 "Attribute 'swifterror' only applies to parameters "
1665 "with pointer to pointer type!",
1667 } else {
1668 Assert(!Attrs.hasAttribute(Attribute::ByVal),
1669 "Attribute 'byval' only applies to parameters with pointer type!",
1671 Assert(!Attrs.hasAttribute(Attribute::SwiftError),
1672 "Attribute 'swifterror' only applies to parameters "
1673 "with pointer type!",
1678 // Check parameter attributes against a function type.
1679 // The value V is printed in error messages.
1680 void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
1681 const Value *V, bool IsIntrinsic) {
1682 if (Attrs.isEmpty())
1683 return;
1685 bool SawNest = false;
1686 bool SawReturned = false;
1687 bool SawSRet = false;
1688 bool SawSwiftSelf = false;
1689 bool SawSwiftError = false;
1691 // Verify return value attributes.
1692 AttributeSet RetAttrs = Attrs.getRetAttributes();
1693 Assert((!RetAttrs.hasAttribute(Attribute::ByVal) &&
1694 !RetAttrs.hasAttribute(Attribute::Nest) &&
1695 !RetAttrs.hasAttribute(Attribute::StructRet) &&
1696 !RetAttrs.hasAttribute(Attribute::NoCapture) &&
1697 !RetAttrs.hasAttribute(Attribute::Returned) &&
1698 !RetAttrs.hasAttribute(Attribute::InAlloca) &&
1699 !RetAttrs.hasAttribute(Attribute::SwiftSelf) &&
1700 !RetAttrs.hasAttribute(Attribute::SwiftError)),
1701 "Attributes 'byval', 'inalloca', 'nest', 'sret', 'nocapture', "
1702 "'returned', 'swiftself', and 'swifterror' do not apply to return "
1703 "values!",
1705 Assert((!RetAttrs.hasAttribute(Attribute::ReadOnly) &&
1706 !RetAttrs.hasAttribute(Attribute::WriteOnly) &&
1707 !RetAttrs.hasAttribute(Attribute::ReadNone)),
1708 "Attribute '" + RetAttrs.getAsString() +
1709 "' does not apply to function returns",
1711 verifyParameterAttrs(RetAttrs, FT->getReturnType(), V);
1713 // Verify parameter attributes.
1714 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1715 Type *Ty = FT->getParamType(i);
1716 AttributeSet ArgAttrs = Attrs.getParamAttributes(i);
1718 if (!IsIntrinsic) {
1719 Assert(!ArgAttrs.hasAttribute(Attribute::ImmArg),
1720 "immarg attribute only applies to intrinsics",V);
1723 verifyParameterAttrs(ArgAttrs, Ty, V);
1725 if (ArgAttrs.hasAttribute(Attribute::Nest)) {
1726 Assert(!SawNest, "More than one parameter has attribute nest!", V);
1727 SawNest = true;
1730 if (ArgAttrs.hasAttribute(Attribute::Returned)) {
1731 Assert(!SawReturned, "More than one parameter has attribute returned!",
1733 Assert(Ty->canLosslesslyBitCastTo(FT->getReturnType()),
1734 "Incompatible argument and return types for 'returned' attribute",
1736 SawReturned = true;
1739 if (ArgAttrs.hasAttribute(Attribute::StructRet)) {
1740 Assert(!SawSRet, "Cannot have multiple 'sret' parameters!", V);
1741 Assert(i == 0 || i == 1,
1742 "Attribute 'sret' is not on first or second parameter!", V);
1743 SawSRet = true;
1746 if (ArgAttrs.hasAttribute(Attribute::SwiftSelf)) {
1747 Assert(!SawSwiftSelf, "Cannot have multiple 'swiftself' parameters!", V);
1748 SawSwiftSelf = true;
1751 if (ArgAttrs.hasAttribute(Attribute::SwiftError)) {
1752 Assert(!SawSwiftError, "Cannot have multiple 'swifterror' parameters!",
1754 SawSwiftError = true;
1757 if (ArgAttrs.hasAttribute(Attribute::InAlloca)) {
1758 Assert(i == FT->getNumParams() - 1,
1759 "inalloca isn't on the last parameter!", V);
1763 if (!Attrs.hasAttributes(AttributeList::FunctionIndex))
1764 return;
1766 verifyAttributeTypes(Attrs.getFnAttributes(), /*IsFunction=*/true, V);
1768 Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
1769 Attrs.hasFnAttribute(Attribute::ReadOnly)),
1770 "Attributes 'readnone and readonly' are incompatible!", V);
1772 Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
1773 Attrs.hasFnAttribute(Attribute::WriteOnly)),
1774 "Attributes 'readnone and writeonly' are incompatible!", V);
1776 Assert(!(Attrs.hasFnAttribute(Attribute::ReadOnly) &&
1777 Attrs.hasFnAttribute(Attribute::WriteOnly)),
1778 "Attributes 'readonly and writeonly' are incompatible!", V);
1780 Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
1781 Attrs.hasFnAttribute(Attribute::InaccessibleMemOrArgMemOnly)),
1782 "Attributes 'readnone and inaccessiblemem_or_argmemonly' are "
1783 "incompatible!",
1786 Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
1787 Attrs.hasFnAttribute(Attribute::InaccessibleMemOnly)),
1788 "Attributes 'readnone and inaccessiblememonly' are incompatible!", V);
1790 Assert(!(Attrs.hasFnAttribute(Attribute::NoInline) &&
1791 Attrs.hasFnAttribute(Attribute::AlwaysInline)),
1792 "Attributes 'noinline and alwaysinline' are incompatible!", V);
1794 if (Attrs.hasFnAttribute(Attribute::OptimizeNone)) {
1795 Assert(Attrs.hasFnAttribute(Attribute::NoInline),
1796 "Attribute 'optnone' requires 'noinline'!", V);
1798 Assert(!Attrs.hasFnAttribute(Attribute::OptimizeForSize),
1799 "Attributes 'optsize and optnone' are incompatible!", V);
1801 Assert(!Attrs.hasFnAttribute(Attribute::MinSize),
1802 "Attributes 'minsize and optnone' are incompatible!", V);
1805 if (Attrs.hasFnAttribute(Attribute::JumpTable)) {
1806 const GlobalValue *GV = cast<GlobalValue>(V);
1807 Assert(GV->hasGlobalUnnamedAddr(),
1808 "Attribute 'jumptable' requires 'unnamed_addr'", V);
1811 if (Attrs.hasFnAttribute(Attribute::AllocSize)) {
1812 std::pair<unsigned, Optional<unsigned>> Args =
1813 Attrs.getAllocSizeArgs(AttributeList::FunctionIndex);
1815 auto CheckParam = [&](StringRef Name, unsigned ParamNo) {
1816 if (ParamNo >= FT->getNumParams()) {
1817 CheckFailed("'allocsize' " + Name + " argument is out of bounds", V);
1818 return false;
1821 if (!FT->getParamType(ParamNo)->isIntegerTy()) {
1822 CheckFailed("'allocsize' " + Name +
1823 " argument must refer to an integer parameter",
1825 return false;
1828 return true;
1831 if (!CheckParam("element size", Args.first))
1832 return;
1834 if (Args.second && !CheckParam("number of elements", *Args.second))
1835 return;
1839 void Verifier::verifyFunctionMetadata(
1840 ArrayRef<std::pair<unsigned, MDNode *>> MDs) {
1841 for (const auto &Pair : MDs) {
1842 if (Pair.first == LLVMContext::MD_prof) {
1843 MDNode *MD = Pair.second;
1844 Assert(MD->getNumOperands() >= 2,
1845 "!prof annotations should have no less than 2 operands", MD);
1847 // Check first operand.
1848 Assert(MD->getOperand(0) != nullptr, "first operand should not be null",
1849 MD);
1850 Assert(isa<MDString>(MD->getOperand(0)),
1851 "expected string with name of the !prof annotation", MD);
1852 MDString *MDS = cast<MDString>(MD->getOperand(0));
1853 StringRef ProfName = MDS->getString();
1854 Assert(ProfName.equals("function_entry_count") ||
1855 ProfName.equals("synthetic_function_entry_count"),
1856 "first operand should be 'function_entry_count'"
1857 " or 'synthetic_function_entry_count'",
1858 MD);
1860 // Check second operand.
1861 Assert(MD->getOperand(1) != nullptr, "second operand should not be null",
1862 MD);
1863 Assert(isa<ConstantAsMetadata>(MD->getOperand(1)),
1864 "expected integer argument to function_entry_count", MD);
1869 void Verifier::visitConstantExprsRecursively(const Constant *EntryC) {
1870 if (!ConstantExprVisited.insert(EntryC).second)
1871 return;
1873 SmallVector<const Constant *, 16> Stack;
1874 Stack.push_back(EntryC);
1876 while (!Stack.empty()) {
1877 const Constant *C = Stack.pop_back_val();
1879 // Check this constant expression.
1880 if (const auto *CE = dyn_cast<ConstantExpr>(C))
1881 visitConstantExpr(CE);
1883 if (const auto *GV = dyn_cast<GlobalValue>(C)) {
1884 // Global Values get visited separately, but we do need to make sure
1885 // that the global value is in the correct module
1886 Assert(GV->getParent() == &M, "Referencing global in another module!",
1887 EntryC, &M, GV, GV->getParent());
1888 continue;
1891 // Visit all sub-expressions.
1892 for (const Use &U : C->operands()) {
1893 const auto *OpC = dyn_cast<Constant>(U);
1894 if (!OpC)
1895 continue;
1896 if (!ConstantExprVisited.insert(OpC).second)
1897 continue;
1898 Stack.push_back(OpC);
1903 void Verifier::visitConstantExpr(const ConstantExpr *CE) {
1904 if (CE->getOpcode() == Instruction::BitCast)
1905 Assert(CastInst::castIsValid(Instruction::BitCast, CE->getOperand(0),
1906 CE->getType()),
1907 "Invalid bitcast", CE);
1909 if (CE->getOpcode() == Instruction::IntToPtr ||
1910 CE->getOpcode() == Instruction::PtrToInt) {
1911 auto *PtrTy = CE->getOpcode() == Instruction::IntToPtr
1912 ? CE->getType()
1913 : CE->getOperand(0)->getType();
1914 StringRef Msg = CE->getOpcode() == Instruction::IntToPtr
1915 ? "inttoptr not supported for non-integral pointers"
1916 : "ptrtoint not supported for non-integral pointers";
1917 Assert(
1918 !DL.isNonIntegralPointerType(cast<PointerType>(PtrTy->getScalarType())),
1919 Msg);
1923 bool Verifier::verifyAttributeCount(AttributeList Attrs, unsigned Params) {
1924 // There shouldn't be more attribute sets than there are parameters plus the
1925 // function and return value.
1926 return Attrs.getNumAttrSets() <= Params + 2;
1929 /// Verify that statepoint intrinsic is well formed.
1930 void Verifier::verifyStatepoint(const CallBase &Call) {
1931 assert(Call.getCalledFunction() &&
1932 Call.getCalledFunction()->getIntrinsicID() ==
1933 Intrinsic::experimental_gc_statepoint);
1935 Assert(!Call.doesNotAccessMemory() && !Call.onlyReadsMemory() &&
1936 !Call.onlyAccessesArgMemory(),
1937 "gc.statepoint must read and write all memory to preserve "
1938 "reordering restrictions required by safepoint semantics",
1939 Call);
1941 const int64_t NumPatchBytes =
1942 cast<ConstantInt>(Call.getArgOperand(1))->getSExtValue();
1943 assert(isInt<32>(NumPatchBytes) && "NumPatchBytesV is an i32!");
1944 Assert(NumPatchBytes >= 0,
1945 "gc.statepoint number of patchable bytes must be "
1946 "positive",
1947 Call);
1949 const Value *Target = Call.getArgOperand(2);
1950 auto *PT = dyn_cast<PointerType>(Target->getType());
1951 Assert(PT && PT->getElementType()->isFunctionTy(),
1952 "gc.statepoint callee must be of function pointer type", Call, Target);
1953 FunctionType *TargetFuncType = cast<FunctionType>(PT->getElementType());
1955 const int NumCallArgs = cast<ConstantInt>(Call.getArgOperand(3))->getZExtValue();
1956 Assert(NumCallArgs >= 0,
1957 "gc.statepoint number of arguments to underlying call "
1958 "must be positive",
1959 Call);
1960 const int NumParams = (int)TargetFuncType->getNumParams();
1961 if (TargetFuncType->isVarArg()) {
1962 Assert(NumCallArgs >= NumParams,
1963 "gc.statepoint mismatch in number of vararg call args", Call);
1965 // TODO: Remove this limitation
1966 Assert(TargetFuncType->getReturnType()->isVoidTy(),
1967 "gc.statepoint doesn't support wrapping non-void "
1968 "vararg functions yet",
1969 Call);
1970 } else
1971 Assert(NumCallArgs == NumParams,
1972 "gc.statepoint mismatch in number of call args", Call);
1974 const uint64_t Flags
1975 = cast<ConstantInt>(Call.getArgOperand(4))->getZExtValue();
1976 Assert((Flags & ~(uint64_t)StatepointFlags::MaskAll) == 0,
1977 "unknown flag used in gc.statepoint flags argument", Call);
1979 // Verify that the types of the call parameter arguments match
1980 // the type of the wrapped callee.
1981 AttributeList Attrs = Call.getAttributes();
1982 for (int i = 0; i < NumParams; i++) {
1983 Type *ParamType = TargetFuncType->getParamType(i);
1984 Type *ArgType = Call.getArgOperand(5 + i)->getType();
1985 Assert(ArgType == ParamType,
1986 "gc.statepoint call argument does not match wrapped "
1987 "function type",
1988 Call);
1990 if (TargetFuncType->isVarArg()) {
1991 AttributeSet ArgAttrs = Attrs.getParamAttributes(5 + i);
1992 Assert(!ArgAttrs.hasAttribute(Attribute::StructRet),
1993 "Attribute 'sret' cannot be used for vararg call arguments!",
1994 Call);
1998 const int EndCallArgsInx = 4 + NumCallArgs;
2000 const Value *NumTransitionArgsV = Call.getArgOperand(EndCallArgsInx + 1);
2001 Assert(isa<ConstantInt>(NumTransitionArgsV),
2002 "gc.statepoint number of transition arguments "
2003 "must be constant integer",
2004 Call);
2005 const int NumTransitionArgs =
2006 cast<ConstantInt>(NumTransitionArgsV)->getZExtValue();
2007 Assert(NumTransitionArgs >= 0,
2008 "gc.statepoint number of transition arguments must be positive", Call);
2009 const int EndTransitionArgsInx = EndCallArgsInx + 1 + NumTransitionArgs;
2011 const Value *NumDeoptArgsV = Call.getArgOperand(EndTransitionArgsInx + 1);
2012 Assert(isa<ConstantInt>(NumDeoptArgsV),
2013 "gc.statepoint number of deoptimization arguments "
2014 "must be constant integer",
2015 Call);
2016 const int NumDeoptArgs = cast<ConstantInt>(NumDeoptArgsV)->getZExtValue();
2017 Assert(NumDeoptArgs >= 0,
2018 "gc.statepoint number of deoptimization arguments "
2019 "must be positive",
2020 Call);
2022 const int ExpectedNumArgs =
2023 7 + NumCallArgs + NumTransitionArgs + NumDeoptArgs;
2024 Assert(ExpectedNumArgs <= (int)Call.arg_size(),
2025 "gc.statepoint too few arguments according to length fields", Call);
2027 // Check that the only uses of this gc.statepoint are gc.result or
2028 // gc.relocate calls which are tied to this statepoint and thus part
2029 // of the same statepoint sequence
2030 for (const User *U : Call.users()) {
2031 const CallInst *UserCall = dyn_cast<const CallInst>(U);
2032 Assert(UserCall, "illegal use of statepoint token", Call, U);
2033 if (!UserCall)
2034 continue;
2035 Assert(isa<GCRelocateInst>(UserCall) || isa<GCResultInst>(UserCall),
2036 "gc.result or gc.relocate are the only value uses "
2037 "of a gc.statepoint",
2038 Call, U);
2039 if (isa<GCResultInst>(UserCall)) {
2040 Assert(UserCall->getArgOperand(0) == &Call,
2041 "gc.result connected to wrong gc.statepoint", Call, UserCall);
2042 } else if (isa<GCRelocateInst>(Call)) {
2043 Assert(UserCall->getArgOperand(0) == &Call,
2044 "gc.relocate connected to wrong gc.statepoint", Call, UserCall);
2048 // Note: It is legal for a single derived pointer to be listed multiple
2049 // times. It's non-optimal, but it is legal. It can also happen after
2050 // insertion if we strip a bitcast away.
2051 // Note: It is really tempting to check that each base is relocated and
2052 // that a derived pointer is never reused as a base pointer. This turns
2053 // out to be problematic since optimizations run after safepoint insertion
2054 // can recognize equality properties that the insertion logic doesn't know
2055 // about. See example statepoint.ll in the verifier subdirectory
2058 void Verifier::verifyFrameRecoverIndices() {
2059 for (auto &Counts : FrameEscapeInfo) {
2060 Function *F = Counts.first;
2061 unsigned EscapedObjectCount = Counts.second.first;
2062 unsigned MaxRecoveredIndex = Counts.second.second;
2063 Assert(MaxRecoveredIndex <= EscapedObjectCount,
2064 "all indices passed to llvm.localrecover must be less than the "
2065 "number of arguments passed to llvm.localescape in the parent "
2066 "function",
2071 static Instruction *getSuccPad(Instruction *Terminator) {
2072 BasicBlock *UnwindDest;
2073 if (auto *II = dyn_cast<InvokeInst>(Terminator))
2074 UnwindDest = II->getUnwindDest();
2075 else if (auto *CSI = dyn_cast<CatchSwitchInst>(Terminator))
2076 UnwindDest = CSI->getUnwindDest();
2077 else
2078 UnwindDest = cast<CleanupReturnInst>(Terminator)->getUnwindDest();
2079 return UnwindDest->getFirstNonPHI();
2082 void Verifier::verifySiblingFuncletUnwinds() {
2083 SmallPtrSet<Instruction *, 8> Visited;
2084 SmallPtrSet<Instruction *, 8> Active;
2085 for (const auto &Pair : SiblingFuncletInfo) {
2086 Instruction *PredPad = Pair.first;
2087 if (Visited.count(PredPad))
2088 continue;
2089 Active.insert(PredPad);
2090 Instruction *Terminator = Pair.second;
2091 do {
2092 Instruction *SuccPad = getSuccPad(Terminator);
2093 if (Active.count(SuccPad)) {
2094 // Found a cycle; report error
2095 Instruction *CyclePad = SuccPad;
2096 SmallVector<Instruction *, 8> CycleNodes;
2097 do {
2098 CycleNodes.push_back(CyclePad);
2099 Instruction *CycleTerminator = SiblingFuncletInfo[CyclePad];
2100 if (CycleTerminator != CyclePad)
2101 CycleNodes.push_back(CycleTerminator);
2102 CyclePad = getSuccPad(CycleTerminator);
2103 } while (CyclePad != SuccPad);
2104 Assert(false, "EH pads can't handle each other's exceptions",
2105 ArrayRef<Instruction *>(CycleNodes));
2107 // Don't re-walk a node we've already checked
2108 if (!Visited.insert(SuccPad).second)
2109 break;
2110 // Walk to this successor if it has a map entry.
2111 PredPad = SuccPad;
2112 auto TermI = SiblingFuncletInfo.find(PredPad);
2113 if (TermI == SiblingFuncletInfo.end())
2114 break;
2115 Terminator = TermI->second;
2116 Active.insert(PredPad);
2117 } while (true);
2118 // Each node only has one successor, so we've walked all the active
2119 // nodes' successors.
2120 Active.clear();
2124 // visitFunction - Verify that a function is ok.
2126 void Verifier::visitFunction(const Function &F) {
2127 visitGlobalValue(F);
2129 // Check function arguments.
2130 FunctionType *FT = F.getFunctionType();
2131 unsigned NumArgs = F.arg_size();
2133 Assert(&Context == &F.getContext(),
2134 "Function context does not match Module context!", &F);
2136 Assert(!F.hasCommonLinkage(), "Functions may not have common linkage", &F);
2137 Assert(FT->getNumParams() == NumArgs,
2138 "# formal arguments must match # of arguments for function type!", &F,
2139 FT);
2140 Assert(F.getReturnType()->isFirstClassType() ||
2141 F.getReturnType()->isVoidTy() || F.getReturnType()->isStructTy(),
2142 "Functions cannot return aggregate values!", &F);
2144 Assert(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
2145 "Invalid struct return type!", &F);
2147 AttributeList Attrs = F.getAttributes();
2149 Assert(verifyAttributeCount(Attrs, FT->getNumParams()),
2150 "Attribute after last parameter!", &F);
2152 bool isLLVMdotName = F.getName().size() >= 5 &&
2153 F.getName().substr(0, 5) == "llvm.";
2155 // Check function attributes.
2156 verifyFunctionAttrs(FT, Attrs, &F, isLLVMdotName);
2158 // On function declarations/definitions, we do not support the builtin
2159 // attribute. We do not check this in VerifyFunctionAttrs since that is
2160 // checking for Attributes that can/can not ever be on functions.
2161 Assert(!Attrs.hasFnAttribute(Attribute::Builtin),
2162 "Attribute 'builtin' can only be applied to a callsite.", &F);
2164 // Check that this function meets the restrictions on this calling convention.
2165 // Sometimes varargs is used for perfectly forwarding thunks, so some of these
2166 // restrictions can be lifted.
2167 switch (F.getCallingConv()) {
2168 default:
2169 case CallingConv::C:
2170 break;
2171 case CallingConv::AMDGPU_KERNEL:
2172 case CallingConv::SPIR_KERNEL:
2173 Assert(F.getReturnType()->isVoidTy(),
2174 "Calling convention requires void return type", &F);
2175 LLVM_FALLTHROUGH;
2176 case CallingConv::AMDGPU_VS:
2177 case CallingConv::AMDGPU_HS:
2178 case CallingConv::AMDGPU_GS:
2179 case CallingConv::AMDGPU_PS:
2180 case CallingConv::AMDGPU_CS:
2181 Assert(!F.hasStructRetAttr(),
2182 "Calling convention does not allow sret", &F);
2183 LLVM_FALLTHROUGH;
2184 case CallingConv::Fast:
2185 case CallingConv::Cold:
2186 case CallingConv::Intel_OCL_BI:
2187 case CallingConv::PTX_Kernel:
2188 case CallingConv::PTX_Device:
2189 Assert(!F.isVarArg(), "Calling convention does not support varargs or "
2190 "perfect forwarding!",
2191 &F);
2192 break;
2195 // Check that the argument values match the function type for this function...
2196 unsigned i = 0;
2197 for (const Argument &Arg : F.args()) {
2198 Assert(Arg.getType() == FT->getParamType(i),
2199 "Argument value does not match function argument type!", &Arg,
2200 FT->getParamType(i));
2201 Assert(Arg.getType()->isFirstClassType(),
2202 "Function arguments must have first-class types!", &Arg);
2203 if (!isLLVMdotName) {
2204 Assert(!Arg.getType()->isMetadataTy(),
2205 "Function takes metadata but isn't an intrinsic", &Arg, &F);
2206 Assert(!Arg.getType()->isTokenTy(),
2207 "Function takes token but isn't an intrinsic", &Arg, &F);
2210 // Check that swifterror argument is only used by loads and stores.
2211 if (Attrs.hasParamAttribute(i, Attribute::SwiftError)) {
2212 verifySwiftErrorValue(&Arg);
2214 ++i;
2217 if (!isLLVMdotName)
2218 Assert(!F.getReturnType()->isTokenTy(),
2219 "Functions returns a token but isn't an intrinsic", &F);
2221 // Get the function metadata attachments.
2222 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2223 F.getAllMetadata(MDs);
2224 assert(F.hasMetadata() != MDs.empty() && "Bit out-of-sync");
2225 verifyFunctionMetadata(MDs);
2227 // Check validity of the personality function
2228 if (F.hasPersonalityFn()) {
2229 auto *Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
2230 if (Per)
2231 Assert(Per->getParent() == F.getParent(),
2232 "Referencing personality function in another module!",
2233 &F, F.getParent(), Per, Per->getParent());
2236 if (F.isMaterializable()) {
2237 // Function has a body somewhere we can't see.
2238 Assert(MDs.empty(), "unmaterialized function cannot have metadata", &F,
2239 MDs.empty() ? nullptr : MDs.front().second);
2240 } else if (F.isDeclaration()) {
2241 for (const auto &I : MDs) {
2242 // This is used for call site debug information.
2243 AssertDI(I.first != LLVMContext::MD_dbg ||
2244 !cast<DISubprogram>(I.second)->isDistinct(),
2245 "function declaration may only have a unique !dbg attachment",
2246 &F);
2247 Assert(I.first != LLVMContext::MD_prof,
2248 "function declaration may not have a !prof attachment", &F);
2250 // Verify the metadata itself.
2251 visitMDNode(*I.second);
2253 Assert(!F.hasPersonalityFn(),
2254 "Function declaration shouldn't have a personality routine", &F);
2255 } else {
2256 // Verify that this function (which has a body) is not named "llvm.*". It
2257 // is not legal to define intrinsics.
2258 Assert(!isLLVMdotName, "llvm intrinsics cannot be defined!", &F);
2260 // Check the entry node
2261 const BasicBlock *Entry = &F.getEntryBlock();
2262 Assert(pred_empty(Entry),
2263 "Entry block to function must not have predecessors!", Entry);
2265 // The address of the entry block cannot be taken, unless it is dead.
2266 if (Entry->hasAddressTaken()) {
2267 Assert(!BlockAddress::lookup(Entry)->isConstantUsed(),
2268 "blockaddress may not be used with the entry block!", Entry);
2271 unsigned NumDebugAttachments = 0, NumProfAttachments = 0;
2272 // Visit metadata attachments.
2273 for (const auto &I : MDs) {
2274 // Verify that the attachment is legal.
2275 switch (I.first) {
2276 default:
2277 break;
2278 case LLVMContext::MD_dbg: {
2279 ++NumDebugAttachments;
2280 AssertDI(NumDebugAttachments == 1,
2281 "function must have a single !dbg attachment", &F, I.second);
2282 AssertDI(isa<DISubprogram>(I.second),
2283 "function !dbg attachment must be a subprogram", &F, I.second);
2284 auto *SP = cast<DISubprogram>(I.second);
2285 const Function *&AttachedTo = DISubprogramAttachments[SP];
2286 AssertDI(!AttachedTo || AttachedTo == &F,
2287 "DISubprogram attached to more than one function", SP, &F);
2288 AttachedTo = &F;
2289 break;
2291 case LLVMContext::MD_prof:
2292 ++NumProfAttachments;
2293 Assert(NumProfAttachments == 1,
2294 "function must have a single !prof attachment", &F, I.second);
2295 break;
2298 // Verify the metadata itself.
2299 visitMDNode(*I.second);
2303 // If this function is actually an intrinsic, verify that it is only used in
2304 // direct call/invokes, never having its "address taken".
2305 // Only do this if the module is materialized, otherwise we don't have all the
2306 // uses.
2307 if (F.getIntrinsicID() && F.getParent()->isMaterialized()) {
2308 const User *U;
2309 if (F.hasAddressTaken(&U))
2310 Assert(false, "Invalid user of intrinsic instruction!", U);
2313 auto *N = F.getSubprogram();
2314 HasDebugInfo = (N != nullptr);
2315 if (!HasDebugInfo)
2316 return;
2318 // Check that all !dbg attachments lead to back to N (or, at least, another
2319 // subprogram that describes the same function).
2321 // FIXME: Check this incrementally while visiting !dbg attachments.
2322 // FIXME: Only check when N is the canonical subprogram for F.
2323 SmallPtrSet<const MDNode *, 32> Seen;
2324 auto VisitDebugLoc = [&](const Instruction &I, const MDNode *Node) {
2325 // Be careful about using DILocation here since we might be dealing with
2326 // broken code (this is the Verifier after all).
2327 const DILocation *DL = dyn_cast_or_null<DILocation>(Node);
2328 if (!DL)
2329 return;
2330 if (!Seen.insert(DL).second)
2331 return;
2333 Metadata *Parent = DL->getRawScope();
2334 AssertDI(Parent && isa<DILocalScope>(Parent),
2335 "DILocation's scope must be a DILocalScope", N, &F, &I, DL,
2336 Parent);
2337 DILocalScope *Scope = DL->getInlinedAtScope();
2338 if (Scope && !Seen.insert(Scope).second)
2339 return;
2341 DISubprogram *SP = Scope ? Scope->getSubprogram() : nullptr;
2343 // Scope and SP could be the same MDNode and we don't want to skip
2344 // validation in that case
2345 if (SP && ((Scope != SP) && !Seen.insert(SP).second))
2346 return;
2348 // FIXME: Once N is canonical, check "SP == &N".
2349 AssertDI(SP->describes(&F),
2350 "!dbg attachment points at wrong subprogram for function", N, &F,
2351 &I, DL, Scope, SP);
2353 for (auto &BB : F)
2354 for (auto &I : BB) {
2355 VisitDebugLoc(I, I.getDebugLoc().getAsMDNode());
2356 // The llvm.loop annotations also contain two DILocations.
2357 if (auto MD = I.getMetadata(LLVMContext::MD_loop))
2358 for (unsigned i = 1; i < MD->getNumOperands(); ++i)
2359 VisitDebugLoc(I, dyn_cast_or_null<MDNode>(MD->getOperand(i)));
2360 if (BrokenDebugInfo)
2361 return;
2365 // verifyBasicBlock - Verify that a basic block is well formed...
2367 void Verifier::visitBasicBlock(BasicBlock &BB) {
2368 InstsInThisBlock.clear();
2370 // Ensure that basic blocks have terminators!
2371 Assert(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
2373 // Check constraints that this basic block imposes on all of the PHI nodes in
2374 // it.
2375 if (isa<PHINode>(BB.front())) {
2376 SmallVector<BasicBlock*, 8> Preds(pred_begin(&BB), pred_end(&BB));
2377 SmallVector<std::pair<BasicBlock*, Value*>, 8> Values;
2378 llvm::sort(Preds);
2379 for (const PHINode &PN : BB.phis()) {
2380 // Ensure that PHI nodes have at least one entry!
2381 Assert(PN.getNumIncomingValues() != 0,
2382 "PHI nodes must have at least one entry. If the block is dead, "
2383 "the PHI should be removed!",
2384 &PN);
2385 Assert(PN.getNumIncomingValues() == Preds.size(),
2386 "PHINode should have one entry for each predecessor of its "
2387 "parent basic block!",
2388 &PN);
2390 // Get and sort all incoming values in the PHI node...
2391 Values.clear();
2392 Values.reserve(PN.getNumIncomingValues());
2393 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
2394 Values.push_back(
2395 std::make_pair(PN.getIncomingBlock(i), PN.getIncomingValue(i)));
2396 llvm::sort(Values);
2398 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
2399 // Check to make sure that if there is more than one entry for a
2400 // particular basic block in this PHI node, that the incoming values are
2401 // all identical.
2403 Assert(i == 0 || Values[i].first != Values[i - 1].first ||
2404 Values[i].second == Values[i - 1].second,
2405 "PHI node has multiple entries for the same basic block with "
2406 "different incoming values!",
2407 &PN, Values[i].first, Values[i].second, Values[i - 1].second);
2409 // Check to make sure that the predecessors and PHI node entries are
2410 // matched up.
2411 Assert(Values[i].first == Preds[i],
2412 "PHI node entries do not match predecessors!", &PN,
2413 Values[i].first, Preds[i]);
2418 // Check that all instructions have their parent pointers set up correctly.
2419 for (auto &I : BB)
2421 Assert(I.getParent() == &BB, "Instruction has bogus parent pointer!");
2425 void Verifier::visitTerminator(Instruction &I) {
2426 // Ensure that terminators only exist at the end of the basic block.
2427 Assert(&I == I.getParent()->getTerminator(),
2428 "Terminator found in the middle of a basic block!", I.getParent());
2429 visitInstruction(I);
2432 void Verifier::visitBranchInst(BranchInst &BI) {
2433 if (BI.isConditional()) {
2434 Assert(BI.getCondition()->getType()->isIntegerTy(1),
2435 "Branch condition is not 'i1' type!", &BI, BI.getCondition());
2437 visitTerminator(BI);
2440 void Verifier::visitReturnInst(ReturnInst &RI) {
2441 Function *F = RI.getParent()->getParent();
2442 unsigned N = RI.getNumOperands();
2443 if (F->getReturnType()->isVoidTy())
2444 Assert(N == 0,
2445 "Found return instr that returns non-void in Function of void "
2446 "return type!",
2447 &RI, F->getReturnType());
2448 else
2449 Assert(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(),
2450 "Function return type does not match operand "
2451 "type of return inst!",
2452 &RI, F->getReturnType());
2454 // Check to make sure that the return value has necessary properties for
2455 // terminators...
2456 visitTerminator(RI);
2459 void Verifier::visitSwitchInst(SwitchInst &SI) {
2460 // Check to make sure that all of the constants in the switch instruction
2461 // have the same type as the switched-on value.
2462 Type *SwitchTy = SI.getCondition()->getType();
2463 SmallPtrSet<ConstantInt*, 32> Constants;
2464 for (auto &Case : SI.cases()) {
2465 Assert(Case.getCaseValue()->getType() == SwitchTy,
2466 "Switch constants must all be same type as switch value!", &SI);
2467 Assert(Constants.insert(Case.getCaseValue()).second,
2468 "Duplicate integer as switch case", &SI, Case.getCaseValue());
2471 visitTerminator(SI);
2474 void Verifier::visitIndirectBrInst(IndirectBrInst &BI) {
2475 Assert(BI.getAddress()->getType()->isPointerTy(),
2476 "Indirectbr operand must have pointer type!", &BI);
2477 for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i)
2478 Assert(BI.getDestination(i)->getType()->isLabelTy(),
2479 "Indirectbr destinations must all have pointer type!", &BI);
2481 visitTerminator(BI);
2484 void Verifier::visitCallBrInst(CallBrInst &CBI) {
2485 Assert(CBI.isInlineAsm(), "Callbr is currently only used for asm-goto!",
2486 &CBI);
2487 Assert(CBI.getType()->isVoidTy(), "Callbr return value is not supported!",
2488 &CBI);
2489 for (unsigned i = 0, e = CBI.getNumSuccessors(); i != e; ++i)
2490 Assert(CBI.getSuccessor(i)->getType()->isLabelTy(),
2491 "Callbr successors must all have pointer type!", &CBI);
2492 for (unsigned i = 0, e = CBI.getNumOperands(); i != e; ++i) {
2493 Assert(i >= CBI.getNumArgOperands() || !isa<BasicBlock>(CBI.getOperand(i)),
2494 "Using an unescaped label as a callbr argument!", &CBI);
2495 if (isa<BasicBlock>(CBI.getOperand(i)))
2496 for (unsigned j = i + 1; j != e; ++j)
2497 Assert(CBI.getOperand(i) != CBI.getOperand(j),
2498 "Duplicate callbr destination!", &CBI);
2501 visitTerminator(CBI);
2504 void Verifier::visitSelectInst(SelectInst &SI) {
2505 Assert(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1),
2506 SI.getOperand(2)),
2507 "Invalid operands for select instruction!", &SI);
2509 Assert(SI.getTrueValue()->getType() == SI.getType(),
2510 "Select values must have same type as select instruction!", &SI);
2511 visitInstruction(SI);
2514 /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
2515 /// a pass, if any exist, it's an error.
2517 void Verifier::visitUserOp1(Instruction &I) {
2518 Assert(false, "User-defined operators should not live outside of a pass!", &I);
2521 void Verifier::visitTruncInst(TruncInst &I) {
2522 // Get the source and destination types
2523 Type *SrcTy = I.getOperand(0)->getType();
2524 Type *DestTy = I.getType();
2526 // Get the size of the types in bits, we'll need this later
2527 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2528 unsigned DestBitSize = DestTy->getScalarSizeInBits();
2530 Assert(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I);
2531 Assert(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I);
2532 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2533 "trunc source and destination must both be a vector or neither", &I);
2534 Assert(SrcBitSize > DestBitSize, "DestTy too big for Trunc", &I);
2536 visitInstruction(I);
2539 void Verifier::visitZExtInst(ZExtInst &I) {
2540 // Get the source and destination types
2541 Type *SrcTy = I.getOperand(0)->getType();
2542 Type *DestTy = I.getType();
2544 // Get the size of the types in bits, we'll need this later
2545 Assert(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I);
2546 Assert(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I);
2547 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2548 "zext source and destination must both be a vector or neither", &I);
2549 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2550 unsigned DestBitSize = DestTy->getScalarSizeInBits();
2552 Assert(SrcBitSize < DestBitSize, "Type too small for ZExt", &I);
2554 visitInstruction(I);
2557 void Verifier::visitSExtInst(SExtInst &I) {
2558 // Get the source and destination types
2559 Type *SrcTy = I.getOperand(0)->getType();
2560 Type *DestTy = I.getType();
2562 // Get the size of the types in bits, we'll need this later
2563 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2564 unsigned DestBitSize = DestTy->getScalarSizeInBits();
2566 Assert(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I);
2567 Assert(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I);
2568 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2569 "sext source and destination must both be a vector or neither", &I);
2570 Assert(SrcBitSize < DestBitSize, "Type too small for SExt", &I);
2572 visitInstruction(I);
2575 void Verifier::visitFPTruncInst(FPTruncInst &I) {
2576 // Get the source and destination types
2577 Type *SrcTy = I.getOperand(0)->getType();
2578 Type *DestTy = I.getType();
2579 // Get the size of the types in bits, we'll need this later
2580 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2581 unsigned DestBitSize = DestTy->getScalarSizeInBits();
2583 Assert(SrcTy->isFPOrFPVectorTy(), "FPTrunc only operates on FP", &I);
2584 Assert(DestTy->isFPOrFPVectorTy(), "FPTrunc only produces an FP", &I);
2585 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2586 "fptrunc source and destination must both be a vector or neither", &I);
2587 Assert(SrcBitSize > DestBitSize, "DestTy too big for FPTrunc", &I);
2589 visitInstruction(I);
2592 void Verifier::visitFPExtInst(FPExtInst &I) {
2593 // Get the source and destination types
2594 Type *SrcTy = I.getOperand(0)->getType();
2595 Type *DestTy = I.getType();
2597 // Get the size of the types in bits, we'll need this later
2598 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2599 unsigned DestBitSize = DestTy->getScalarSizeInBits();
2601 Assert(SrcTy->isFPOrFPVectorTy(), "FPExt only operates on FP", &I);
2602 Assert(DestTy->isFPOrFPVectorTy(), "FPExt only produces an FP", &I);
2603 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2604 "fpext source and destination must both be a vector or neither", &I);
2605 Assert(SrcBitSize < DestBitSize, "DestTy too small for FPExt", &I);
2607 visitInstruction(I);
2610 void Verifier::visitUIToFPInst(UIToFPInst &I) {
2611 // Get the source and destination types
2612 Type *SrcTy = I.getOperand(0)->getType();
2613 Type *DestTy = I.getType();
2615 bool SrcVec = SrcTy->isVectorTy();
2616 bool DstVec = DestTy->isVectorTy();
2618 Assert(SrcVec == DstVec,
2619 "UIToFP source and dest must both be vector or scalar", &I);
2620 Assert(SrcTy->isIntOrIntVectorTy(),
2621 "UIToFP source must be integer or integer vector", &I);
2622 Assert(DestTy->isFPOrFPVectorTy(), "UIToFP result must be FP or FP vector",
2623 &I);
2625 if (SrcVec && DstVec)
2626 Assert(cast<VectorType>(SrcTy)->getNumElements() ==
2627 cast<VectorType>(DestTy)->getNumElements(),
2628 "UIToFP source and dest vector length mismatch", &I);
2630 visitInstruction(I);
2633 void Verifier::visitSIToFPInst(SIToFPInst &I) {
2634 // Get the source and destination types
2635 Type *SrcTy = I.getOperand(0)->getType();
2636 Type *DestTy = I.getType();
2638 bool SrcVec = SrcTy->isVectorTy();
2639 bool DstVec = DestTy->isVectorTy();
2641 Assert(SrcVec == DstVec,
2642 "SIToFP source and dest must both be vector or scalar", &I);
2643 Assert(SrcTy->isIntOrIntVectorTy(),
2644 "SIToFP source must be integer or integer vector", &I);
2645 Assert(DestTy->isFPOrFPVectorTy(), "SIToFP result must be FP or FP vector",
2646 &I);
2648 if (SrcVec && DstVec)
2649 Assert(cast<VectorType>(SrcTy)->getNumElements() ==
2650 cast<VectorType>(DestTy)->getNumElements(),
2651 "SIToFP source and dest vector length mismatch", &I);
2653 visitInstruction(I);
2656 void Verifier::visitFPToUIInst(FPToUIInst &I) {
2657 // Get the source and destination types
2658 Type *SrcTy = I.getOperand(0)->getType();
2659 Type *DestTy = I.getType();
2661 bool SrcVec = SrcTy->isVectorTy();
2662 bool DstVec = DestTy->isVectorTy();
2664 Assert(SrcVec == DstVec,
2665 "FPToUI source and dest must both be vector or scalar", &I);
2666 Assert(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector",
2667 &I);
2668 Assert(DestTy->isIntOrIntVectorTy(),
2669 "FPToUI result must be integer or integer vector", &I);
2671 if (SrcVec && DstVec)
2672 Assert(cast<VectorType>(SrcTy)->getNumElements() ==
2673 cast<VectorType>(DestTy)->getNumElements(),
2674 "FPToUI source and dest vector length mismatch", &I);
2676 visitInstruction(I);
2679 void Verifier::visitFPToSIInst(FPToSIInst &I) {
2680 // Get the source and destination types
2681 Type *SrcTy = I.getOperand(0)->getType();
2682 Type *DestTy = I.getType();
2684 bool SrcVec = SrcTy->isVectorTy();
2685 bool DstVec = DestTy->isVectorTy();
2687 Assert(SrcVec == DstVec,
2688 "FPToSI source and dest must both be vector or scalar", &I);
2689 Assert(SrcTy->isFPOrFPVectorTy(), "FPToSI source must be FP or FP vector",
2690 &I);
2691 Assert(DestTy->isIntOrIntVectorTy(),
2692 "FPToSI result must be integer or integer vector", &I);
2694 if (SrcVec && DstVec)
2695 Assert(cast<VectorType>(SrcTy)->getNumElements() ==
2696 cast<VectorType>(DestTy)->getNumElements(),
2697 "FPToSI source and dest vector length mismatch", &I);
2699 visitInstruction(I);
2702 void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
2703 // Get the source and destination types
2704 Type *SrcTy = I.getOperand(0)->getType();
2705 Type *DestTy = I.getType();
2707 Assert(SrcTy->isPtrOrPtrVectorTy(), "PtrToInt source must be pointer", &I);
2709 if (auto *PTy = dyn_cast<PointerType>(SrcTy->getScalarType()))
2710 Assert(!DL.isNonIntegralPointerType(PTy),
2711 "ptrtoint not supported for non-integral pointers");
2713 Assert(DestTy->isIntOrIntVectorTy(), "PtrToInt result must be integral", &I);
2714 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(), "PtrToInt type mismatch",
2715 &I);
2717 if (SrcTy->isVectorTy()) {
2718 VectorType *VSrc = dyn_cast<VectorType>(SrcTy);
2719 VectorType *VDest = dyn_cast<VectorType>(DestTy);
2720 Assert(VSrc->getNumElements() == VDest->getNumElements(),
2721 "PtrToInt Vector width mismatch", &I);
2724 visitInstruction(I);
2727 void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
2728 // Get the source and destination types
2729 Type *SrcTy = I.getOperand(0)->getType();
2730 Type *DestTy = I.getType();
2732 Assert(SrcTy->isIntOrIntVectorTy(),
2733 "IntToPtr source must be an integral", &I);
2734 Assert(DestTy->isPtrOrPtrVectorTy(), "IntToPtr result must be a pointer", &I);
2736 if (auto *PTy = dyn_cast<PointerType>(DestTy->getScalarType()))
2737 Assert(!DL.isNonIntegralPointerType(PTy),
2738 "inttoptr not supported for non-integral pointers");
2740 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(), "IntToPtr type mismatch",
2741 &I);
2742 if (SrcTy->isVectorTy()) {
2743 VectorType *VSrc = dyn_cast<VectorType>(SrcTy);
2744 VectorType *VDest = dyn_cast<VectorType>(DestTy);
2745 Assert(VSrc->getNumElements() == VDest->getNumElements(),
2746 "IntToPtr Vector width mismatch", &I);
2748 visitInstruction(I);
2751 void Verifier::visitBitCastInst(BitCastInst &I) {
2752 Assert(
2753 CastInst::castIsValid(Instruction::BitCast, I.getOperand(0), I.getType()),
2754 "Invalid bitcast", &I);
2755 visitInstruction(I);
2758 void Verifier::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
2759 Type *SrcTy = I.getOperand(0)->getType();
2760 Type *DestTy = I.getType();
2762 Assert(SrcTy->isPtrOrPtrVectorTy(), "AddrSpaceCast source must be a pointer",
2763 &I);
2764 Assert(DestTy->isPtrOrPtrVectorTy(), "AddrSpaceCast result must be a pointer",
2765 &I);
2766 Assert(SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace(),
2767 "AddrSpaceCast must be between different address spaces", &I);
2768 if (SrcTy->isVectorTy())
2769 Assert(SrcTy->getVectorNumElements() == DestTy->getVectorNumElements(),
2770 "AddrSpaceCast vector pointer number of elements mismatch", &I);
2771 visitInstruction(I);
2774 /// visitPHINode - Ensure that a PHI node is well formed.
2776 void Verifier::visitPHINode(PHINode &PN) {
2777 // Ensure that the PHI nodes are all grouped together at the top of the block.
2778 // This can be tested by checking whether the instruction before this is
2779 // either nonexistent (because this is begin()) or is a PHI node. If not,
2780 // then there is some other instruction before a PHI.
2781 Assert(&PN == &PN.getParent()->front() ||
2782 isa<PHINode>(--BasicBlock::iterator(&PN)),
2783 "PHI nodes not grouped at top of basic block!", &PN, PN.getParent());
2785 // Check that a PHI doesn't yield a Token.
2786 Assert(!PN.getType()->isTokenTy(), "PHI nodes cannot have token type!");
2788 // Check that all of the values of the PHI node have the same type as the
2789 // result, and that the incoming blocks are really basic blocks.
2790 for (Value *IncValue : PN.incoming_values()) {
2791 Assert(PN.getType() == IncValue->getType(),
2792 "PHI node operands are not the same type as the result!", &PN);
2795 // All other PHI node constraints are checked in the visitBasicBlock method.
2797 visitInstruction(PN);
2800 void Verifier::visitCallBase(CallBase &Call) {
2801 Assert(Call.getCalledValue()->getType()->isPointerTy(),
2802 "Called function must be a pointer!", Call);
2803 PointerType *FPTy = cast<PointerType>(Call.getCalledValue()->getType());
2805 Assert(FPTy->getElementType()->isFunctionTy(),
2806 "Called function is not pointer to function type!", Call);
2808 Assert(FPTy->getElementType() == Call.getFunctionType(),
2809 "Called function is not the same type as the call!", Call);
2811 FunctionType *FTy = Call.getFunctionType();
2813 // Verify that the correct number of arguments are being passed
2814 if (FTy->isVarArg())
2815 Assert(Call.arg_size() >= FTy->getNumParams(),
2816 "Called function requires more parameters than were provided!",
2817 Call);
2818 else
2819 Assert(Call.arg_size() == FTy->getNumParams(),
2820 "Incorrect number of arguments passed to called function!", Call);
2822 // Verify that all arguments to the call match the function type.
2823 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
2824 Assert(Call.getArgOperand(i)->getType() == FTy->getParamType(i),
2825 "Call parameter type does not match function signature!",
2826 Call.getArgOperand(i), FTy->getParamType(i), Call);
2828 AttributeList Attrs = Call.getAttributes();
2830 Assert(verifyAttributeCount(Attrs, Call.arg_size()),
2831 "Attribute after last parameter!", Call);
2833 bool IsIntrinsic = Call.getCalledFunction() &&
2834 Call.getCalledFunction()->getName().startswith("llvm.");
2836 Function *Callee
2837 = dyn_cast<Function>(Call.getCalledValue()->stripPointerCasts());
2839 if (Attrs.hasAttribute(AttributeList::FunctionIndex, Attribute::Speculatable)) {
2840 // Don't allow speculatable on call sites, unless the underlying function
2841 // declaration is also speculatable.
2842 Assert(Callee && Callee->isSpeculatable(),
2843 "speculatable attribute may not apply to call sites", Call);
2846 // Verify call attributes.
2847 verifyFunctionAttrs(FTy, Attrs, &Call, IsIntrinsic);
2849 // Conservatively check the inalloca argument.
2850 // We have a bug if we can find that there is an underlying alloca without
2851 // inalloca.
2852 if (Call.hasInAllocaArgument()) {
2853 Value *InAllocaArg = Call.getArgOperand(FTy->getNumParams() - 1);
2854 if (auto AI = dyn_cast<AllocaInst>(InAllocaArg->stripInBoundsOffsets()))
2855 Assert(AI->isUsedWithInAlloca(),
2856 "inalloca argument for call has mismatched alloca", AI, Call);
2859 // For each argument of the callsite, if it has the swifterror argument,
2860 // make sure the underlying alloca/parameter it comes from has a swifterror as
2861 // well.
2862 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2863 if (Call.paramHasAttr(i, Attribute::SwiftError)) {
2864 Value *SwiftErrorArg = Call.getArgOperand(i);
2865 if (auto AI = dyn_cast<AllocaInst>(SwiftErrorArg->stripInBoundsOffsets())) {
2866 Assert(AI->isSwiftError(),
2867 "swifterror argument for call has mismatched alloca", AI, Call);
2868 continue;
2870 auto ArgI = dyn_cast<Argument>(SwiftErrorArg);
2871 Assert(ArgI,
2872 "swifterror argument should come from an alloca or parameter",
2873 SwiftErrorArg, Call);
2874 Assert(ArgI->hasSwiftErrorAttr(),
2875 "swifterror argument for call has mismatched parameter", ArgI,
2876 Call);
2879 if (Attrs.hasParamAttribute(i, Attribute::ImmArg)) {
2880 // Don't allow immarg on call sites, unless the underlying declaration
2881 // also has the matching immarg.
2882 Assert(Callee && Callee->hasParamAttribute(i, Attribute::ImmArg),
2883 "immarg may not apply only to call sites",
2884 Call.getArgOperand(i), Call);
2887 if (Call.paramHasAttr(i, Attribute::ImmArg)) {
2888 Value *ArgVal = Call.getArgOperand(i);
2889 Assert(isa<ConstantInt>(ArgVal) || isa<ConstantFP>(ArgVal),
2890 "immarg operand has non-immediate parameter", ArgVal, Call);
2894 if (FTy->isVarArg()) {
2895 // FIXME? is 'nest' even legal here?
2896 bool SawNest = false;
2897 bool SawReturned = false;
2899 for (unsigned Idx = 0; Idx < FTy->getNumParams(); ++Idx) {
2900 if (Attrs.hasParamAttribute(Idx, Attribute::Nest))
2901 SawNest = true;
2902 if (Attrs.hasParamAttribute(Idx, Attribute::Returned))
2903 SawReturned = true;
2906 // Check attributes on the varargs part.
2907 for (unsigned Idx = FTy->getNumParams(); Idx < Call.arg_size(); ++Idx) {
2908 Type *Ty = Call.getArgOperand(Idx)->getType();
2909 AttributeSet ArgAttrs = Attrs.getParamAttributes(Idx);
2910 verifyParameterAttrs(ArgAttrs, Ty, &Call);
2912 if (ArgAttrs.hasAttribute(Attribute::Nest)) {
2913 Assert(!SawNest, "More than one parameter has attribute nest!", Call);
2914 SawNest = true;
2917 if (ArgAttrs.hasAttribute(Attribute::Returned)) {
2918 Assert(!SawReturned, "More than one parameter has attribute returned!",
2919 Call);
2920 Assert(Ty->canLosslesslyBitCastTo(FTy->getReturnType()),
2921 "Incompatible argument and return types for 'returned' "
2922 "attribute",
2923 Call);
2924 SawReturned = true;
2927 // Statepoint intrinsic is vararg but the wrapped function may be not.
2928 // Allow sret here and check the wrapped function in verifyStatepoint.
2929 if (!Call.getCalledFunction() ||
2930 Call.getCalledFunction()->getIntrinsicID() !=
2931 Intrinsic::experimental_gc_statepoint)
2932 Assert(!ArgAttrs.hasAttribute(Attribute::StructRet),
2933 "Attribute 'sret' cannot be used for vararg call arguments!",
2934 Call);
2936 if (ArgAttrs.hasAttribute(Attribute::InAlloca))
2937 Assert(Idx == Call.arg_size() - 1,
2938 "inalloca isn't on the last argument!", Call);
2942 // Verify that there's no metadata unless it's a direct call to an intrinsic.
2943 if (!IsIntrinsic) {
2944 for (Type *ParamTy : FTy->params()) {
2945 Assert(!ParamTy->isMetadataTy(),
2946 "Function has metadata parameter but isn't an intrinsic", Call);
2947 Assert(!ParamTy->isTokenTy(),
2948 "Function has token parameter but isn't an intrinsic", Call);
2952 // Verify that indirect calls don't return tokens.
2953 if (!Call.getCalledFunction())
2954 Assert(!FTy->getReturnType()->isTokenTy(),
2955 "Return type cannot be token for indirect call!");
2957 if (Function *F = Call.getCalledFunction())
2958 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
2959 visitIntrinsicCall(ID, Call);
2961 // Verify that a callsite has at most one "deopt", at most one "funclet" and
2962 // at most one "gc-transition" operand bundle.
2963 bool FoundDeoptBundle = false, FoundFuncletBundle = false,
2964 FoundGCTransitionBundle = false;
2965 for (unsigned i = 0, e = Call.getNumOperandBundles(); i < e; ++i) {
2966 OperandBundleUse BU = Call.getOperandBundleAt(i);
2967 uint32_t Tag = BU.getTagID();
2968 if (Tag == LLVMContext::OB_deopt) {
2969 Assert(!FoundDeoptBundle, "Multiple deopt operand bundles", Call);
2970 FoundDeoptBundle = true;
2971 } else if (Tag == LLVMContext::OB_gc_transition) {
2972 Assert(!FoundGCTransitionBundle, "Multiple gc-transition operand bundles",
2973 Call);
2974 FoundGCTransitionBundle = true;
2975 } else if (Tag == LLVMContext::OB_funclet) {
2976 Assert(!FoundFuncletBundle, "Multiple funclet operand bundles", Call);
2977 FoundFuncletBundle = true;
2978 Assert(BU.Inputs.size() == 1,
2979 "Expected exactly one funclet bundle operand", Call);
2980 Assert(isa<FuncletPadInst>(BU.Inputs.front()),
2981 "Funclet bundle operands should correspond to a FuncletPadInst",
2982 Call);
2986 // Verify that each inlinable callsite of a debug-info-bearing function in a
2987 // debug-info-bearing function has a debug location attached to it. Failure to
2988 // do so causes assertion failures when the inliner sets up inline scope info.
2989 if (Call.getFunction()->getSubprogram() && Call.getCalledFunction() &&
2990 Call.getCalledFunction()->getSubprogram())
2991 AssertDI(Call.getDebugLoc(),
2992 "inlinable function call in a function with "
2993 "debug info must have a !dbg location",
2994 Call);
2996 visitInstruction(Call);
2999 /// Two types are "congruent" if they are identical, or if they are both pointer
3000 /// types with different pointee types and the same address space.
3001 static bool isTypeCongruent(Type *L, Type *R) {
3002 if (L == R)
3003 return true;
3004 PointerType *PL = dyn_cast<PointerType>(L);
3005 PointerType *PR = dyn_cast<PointerType>(R);
3006 if (!PL || !PR)
3007 return false;
3008 return PL->getAddressSpace() == PR->getAddressSpace();
3011 static AttrBuilder getParameterABIAttributes(int I, AttributeList Attrs) {
3012 static const Attribute::AttrKind ABIAttrs[] = {
3013 Attribute::StructRet, Attribute::ByVal, Attribute::InAlloca,
3014 Attribute::InReg, Attribute::Returned, Attribute::SwiftSelf,
3015 Attribute::SwiftError};
3016 AttrBuilder Copy;
3017 for (auto AK : ABIAttrs) {
3018 if (Attrs.hasParamAttribute(I, AK))
3019 Copy.addAttribute(AK);
3021 if (Attrs.hasParamAttribute(I, Attribute::Alignment))
3022 Copy.addAlignmentAttr(Attrs.getParamAlignment(I));
3023 return Copy;
3026 void Verifier::verifyMustTailCall(CallInst &CI) {
3027 Assert(!CI.isInlineAsm(), "cannot use musttail call with inline asm", &CI);
3029 // - The caller and callee prototypes must match. Pointer types of
3030 // parameters or return types may differ in pointee type, but not
3031 // address space.
3032 Function *F = CI.getParent()->getParent();
3033 FunctionType *CallerTy = F->getFunctionType();
3034 FunctionType *CalleeTy = CI.getFunctionType();
3035 if (!CI.getCalledFunction() || !CI.getCalledFunction()->isIntrinsic()) {
3036 Assert(CallerTy->getNumParams() == CalleeTy->getNumParams(),
3037 "cannot guarantee tail call due to mismatched parameter counts",
3038 &CI);
3039 for (int I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3040 Assert(
3041 isTypeCongruent(CallerTy->getParamType(I), CalleeTy->getParamType(I)),
3042 "cannot guarantee tail call due to mismatched parameter types", &CI);
3045 Assert(CallerTy->isVarArg() == CalleeTy->isVarArg(),
3046 "cannot guarantee tail call due to mismatched varargs", &CI);
3047 Assert(isTypeCongruent(CallerTy->getReturnType(), CalleeTy->getReturnType()),
3048 "cannot guarantee tail call due to mismatched return types", &CI);
3050 // - The calling conventions of the caller and callee must match.
3051 Assert(F->getCallingConv() == CI.getCallingConv(),
3052 "cannot guarantee tail call due to mismatched calling conv", &CI);
3054 // - All ABI-impacting function attributes, such as sret, byval, inreg,
3055 // returned, and inalloca, must match.
3056 AttributeList CallerAttrs = F->getAttributes();
3057 AttributeList CalleeAttrs = CI.getAttributes();
3058 for (int I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3059 AttrBuilder CallerABIAttrs = getParameterABIAttributes(I, CallerAttrs);
3060 AttrBuilder CalleeABIAttrs = getParameterABIAttributes(I, CalleeAttrs);
3061 Assert(CallerABIAttrs == CalleeABIAttrs,
3062 "cannot guarantee tail call due to mismatched ABI impacting "
3063 "function attributes",
3064 &CI, CI.getOperand(I));
3067 // - The call must immediately precede a :ref:`ret <i_ret>` instruction,
3068 // or a pointer bitcast followed by a ret instruction.
3069 // - The ret instruction must return the (possibly bitcasted) value
3070 // produced by the call or void.
3071 Value *RetVal = &CI;
3072 Instruction *Next = CI.getNextNode();
3074 // Handle the optional bitcast.
3075 if (BitCastInst *BI = dyn_cast_or_null<BitCastInst>(Next)) {
3076 Assert(BI->getOperand(0) == RetVal,
3077 "bitcast following musttail call must use the call", BI);
3078 RetVal = BI;
3079 Next = BI->getNextNode();
3082 // Check the return.
3083 ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next);
3084 Assert(Ret, "musttail call must precede a ret with an optional bitcast",
3085 &CI);
3086 Assert(!Ret->getReturnValue() || Ret->getReturnValue() == RetVal,
3087 "musttail call result must be returned", Ret);
3090 void Verifier::visitCallInst(CallInst &CI) {
3091 visitCallBase(CI);
3093 if (CI.isMustTailCall())
3094 verifyMustTailCall(CI);
3097 void Verifier::visitInvokeInst(InvokeInst &II) {
3098 visitCallBase(II);
3100 // Verify that the first non-PHI instruction of the unwind destination is an
3101 // exception handling instruction.
3102 Assert(
3103 II.getUnwindDest()->isEHPad(),
3104 "The unwind destination does not have an exception handling instruction!",
3105 &II);
3107 visitTerminator(II);
3110 /// visitUnaryOperator - Check the argument to the unary operator.
3112 void Verifier::visitUnaryOperator(UnaryOperator &U) {
3113 Assert(U.getType() == U.getOperand(0)->getType(),
3114 "Unary operators must have same type for"
3115 "operands and result!",
3116 &U);
3118 switch (U.getOpcode()) {
3119 // Check that floating-point arithmetic operators are only used with
3120 // floating-point operands.
3121 case Instruction::FNeg:
3122 Assert(U.getType()->isFPOrFPVectorTy(),
3123 "FNeg operator only works with float types!", &U);
3124 break;
3125 default:
3126 llvm_unreachable("Unknown UnaryOperator opcode!");
3129 visitInstruction(U);
3132 /// visitBinaryOperator - Check that both arguments to the binary operator are
3133 /// of the same type!
3135 void Verifier::visitBinaryOperator(BinaryOperator &B) {
3136 Assert(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
3137 "Both operands to a binary operator are not of the same type!", &B);
3139 switch (B.getOpcode()) {
3140 // Check that integer arithmetic operators are only used with
3141 // integral operands.
3142 case Instruction::Add:
3143 case Instruction::Sub:
3144 case Instruction::Mul:
3145 case Instruction::SDiv:
3146 case Instruction::UDiv:
3147 case Instruction::SRem:
3148 case Instruction::URem:
3149 Assert(B.getType()->isIntOrIntVectorTy(),
3150 "Integer arithmetic operators only work with integral types!", &B);
3151 Assert(B.getType() == B.getOperand(0)->getType(),
3152 "Integer arithmetic operators must have same type "
3153 "for operands and result!",
3154 &B);
3155 break;
3156 // Check that floating-point arithmetic operators are only used with
3157 // floating-point operands.
3158 case Instruction::FAdd:
3159 case Instruction::FSub:
3160 case Instruction::FMul:
3161 case Instruction::FDiv:
3162 case Instruction::FRem:
3163 Assert(B.getType()->isFPOrFPVectorTy(),
3164 "Floating-point arithmetic operators only work with "
3165 "floating-point types!",
3166 &B);
3167 Assert(B.getType() == B.getOperand(0)->getType(),
3168 "Floating-point arithmetic operators must have same type "
3169 "for operands and result!",
3170 &B);
3171 break;
3172 // Check that logical operators are only used with integral operands.
3173 case Instruction::And:
3174 case Instruction::Or:
3175 case Instruction::Xor:
3176 Assert(B.getType()->isIntOrIntVectorTy(),
3177 "Logical operators only work with integral types!", &B);
3178 Assert(B.getType() == B.getOperand(0)->getType(),
3179 "Logical operators must have same type for operands and result!",
3180 &B);
3181 break;
3182 case Instruction::Shl:
3183 case Instruction::LShr:
3184 case Instruction::AShr:
3185 Assert(B.getType()->isIntOrIntVectorTy(),
3186 "Shifts only work with integral types!", &B);
3187 Assert(B.getType() == B.getOperand(0)->getType(),
3188 "Shift return type must be same as operands!", &B);
3189 break;
3190 default:
3191 llvm_unreachable("Unknown BinaryOperator opcode!");
3194 visitInstruction(B);
3197 void Verifier::visitICmpInst(ICmpInst &IC) {
3198 // Check that the operands are the same type
3199 Type *Op0Ty = IC.getOperand(0)->getType();
3200 Type *Op1Ty = IC.getOperand(1)->getType();
3201 Assert(Op0Ty == Op1Ty,
3202 "Both operands to ICmp instruction are not of the same type!", &IC);
3203 // Check that the operands are the right type
3204 Assert(Op0Ty->isIntOrIntVectorTy() || Op0Ty->isPtrOrPtrVectorTy(),
3205 "Invalid operand types for ICmp instruction", &IC);
3206 // Check that the predicate is valid.
3207 Assert(IC.isIntPredicate(),
3208 "Invalid predicate in ICmp instruction!", &IC);
3210 visitInstruction(IC);
3213 void Verifier::visitFCmpInst(FCmpInst &FC) {
3214 // Check that the operands are the same type
3215 Type *Op0Ty = FC.getOperand(0)->getType();
3216 Type *Op1Ty = FC.getOperand(1)->getType();
3217 Assert(Op0Ty == Op1Ty,
3218 "Both operands to FCmp instruction are not of the same type!", &FC);
3219 // Check that the operands are the right type
3220 Assert(Op0Ty->isFPOrFPVectorTy(),
3221 "Invalid operand types for FCmp instruction", &FC);
3222 // Check that the predicate is valid.
3223 Assert(FC.isFPPredicate(),
3224 "Invalid predicate in FCmp instruction!", &FC);
3226 visitInstruction(FC);
3229 void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
3230 Assert(
3231 ExtractElementInst::isValidOperands(EI.getOperand(0), EI.getOperand(1)),
3232 "Invalid extractelement operands!", &EI);
3233 visitInstruction(EI);
3236 void Verifier::visitInsertElementInst(InsertElementInst &IE) {
3237 Assert(InsertElementInst::isValidOperands(IE.getOperand(0), IE.getOperand(1),
3238 IE.getOperand(2)),
3239 "Invalid insertelement operands!", &IE);
3240 visitInstruction(IE);
3243 void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
3244 Assert(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1),
3245 SV.getOperand(2)),
3246 "Invalid shufflevector operands!", &SV);
3247 visitInstruction(SV);
3250 void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
3251 Type *TargetTy = GEP.getPointerOperandType()->getScalarType();
3253 Assert(isa<PointerType>(TargetTy),
3254 "GEP base pointer is not a vector or a vector of pointers", &GEP);
3255 Assert(GEP.getSourceElementType()->isSized(), "GEP into unsized type!", &GEP);
3257 SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end());
3258 Assert(all_of(
3259 Idxs, [](Value* V) { return V->getType()->isIntOrIntVectorTy(); }),
3260 "GEP indexes must be integers", &GEP);
3261 Type *ElTy =
3262 GetElementPtrInst::getIndexedType(GEP.getSourceElementType(), Idxs);
3263 Assert(ElTy, "Invalid indices for GEP pointer type!", &GEP);
3265 Assert(GEP.getType()->isPtrOrPtrVectorTy() &&
3266 GEP.getResultElementType() == ElTy,
3267 "GEP is not of right type for indices!", &GEP, ElTy);
3269 if (GEP.getType()->isVectorTy()) {
3270 // Additional checks for vector GEPs.
3271 unsigned GEPWidth = GEP.getType()->getVectorNumElements();
3272 if (GEP.getPointerOperandType()->isVectorTy())
3273 Assert(GEPWidth == GEP.getPointerOperandType()->getVectorNumElements(),
3274 "Vector GEP result width doesn't match operand's", &GEP);
3275 for (Value *Idx : Idxs) {
3276 Type *IndexTy = Idx->getType();
3277 if (IndexTy->isVectorTy()) {
3278 unsigned IndexWidth = IndexTy->getVectorNumElements();
3279 Assert(IndexWidth == GEPWidth, "Invalid GEP index vector width", &GEP);
3281 Assert(IndexTy->isIntOrIntVectorTy(),
3282 "All GEP indices should be of integer type");
3286 if (auto *PTy = dyn_cast<PointerType>(GEP.getType())) {
3287 Assert(GEP.getAddressSpace() == PTy->getAddressSpace(),
3288 "GEP address space doesn't match type", &GEP);
3291 visitInstruction(GEP);
3294 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
3295 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
3298 void Verifier::visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty) {
3299 assert(Range && Range == I.getMetadata(LLVMContext::MD_range) &&
3300 "precondition violation");
3302 unsigned NumOperands = Range->getNumOperands();
3303 Assert(NumOperands % 2 == 0, "Unfinished range!", Range);
3304 unsigned NumRanges = NumOperands / 2;
3305 Assert(NumRanges >= 1, "It should have at least one range!", Range);
3307 ConstantRange LastRange(1, true); // Dummy initial value
3308 for (unsigned i = 0; i < NumRanges; ++i) {
3309 ConstantInt *Low =
3310 mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i));
3311 Assert(Low, "The lower limit must be an integer!", Low);
3312 ConstantInt *High =
3313 mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i + 1));
3314 Assert(High, "The upper limit must be an integer!", High);
3315 Assert(High->getType() == Low->getType() && High->getType() == Ty,
3316 "Range types must match instruction type!", &I);
3318 APInt HighV = High->getValue();
3319 APInt LowV = Low->getValue();
3320 ConstantRange CurRange(LowV, HighV);
3321 Assert(!CurRange.isEmptySet() && !CurRange.isFullSet(),
3322 "Range must not be empty!", Range);
3323 if (i != 0) {
3324 Assert(CurRange.intersectWith(LastRange).isEmptySet(),
3325 "Intervals are overlapping", Range);
3326 Assert(LowV.sgt(LastRange.getLower()), "Intervals are not in order",
3327 Range);
3328 Assert(!isContiguous(CurRange, LastRange), "Intervals are contiguous",
3329 Range);
3331 LastRange = ConstantRange(LowV, HighV);
3333 if (NumRanges > 2) {
3334 APInt FirstLow =
3335 mdconst::dyn_extract<ConstantInt>(Range->getOperand(0))->getValue();
3336 APInt FirstHigh =
3337 mdconst::dyn_extract<ConstantInt>(Range->getOperand(1))->getValue();
3338 ConstantRange FirstRange(FirstLow, FirstHigh);
3339 Assert(FirstRange.intersectWith(LastRange).isEmptySet(),
3340 "Intervals are overlapping", Range);
3341 Assert(!isContiguous(FirstRange, LastRange), "Intervals are contiguous",
3342 Range);
3346 void Verifier::checkAtomicMemAccessSize(Type *Ty, const Instruction *I) {
3347 unsigned Size = DL.getTypeSizeInBits(Ty);
3348 Assert(Size >= 8, "atomic memory access' size must be byte-sized", Ty, I);
3349 Assert(!(Size & (Size - 1)),
3350 "atomic memory access' operand must have a power-of-two size", Ty, I);
3353 void Verifier::visitLoadInst(LoadInst &LI) {
3354 PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType());
3355 Assert(PTy, "Load operand must be a pointer.", &LI);
3356 Type *ElTy = LI.getType();
3357 Assert(LI.getAlignment() <= Value::MaximumAlignment,
3358 "huge alignment values are unsupported", &LI);
3359 Assert(ElTy->isSized(), "loading unsized types is not allowed", &LI);
3360 if (LI.isAtomic()) {
3361 Assert(LI.getOrdering() != AtomicOrdering::Release &&
3362 LI.getOrdering() != AtomicOrdering::AcquireRelease,
3363 "Load cannot have Release ordering", &LI);
3364 Assert(LI.getAlignment() != 0,
3365 "Atomic load must specify explicit alignment", &LI);
3366 Assert(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
3367 "atomic load operand must have integer, pointer, or floating point "
3368 "type!",
3369 ElTy, &LI);
3370 checkAtomicMemAccessSize(ElTy, &LI);
3371 } else {
3372 Assert(LI.getSyncScopeID() == SyncScope::System,
3373 "Non-atomic load cannot have SynchronizationScope specified", &LI);
3376 visitInstruction(LI);
3379 void Verifier::visitStoreInst(StoreInst &SI) {
3380 PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType());
3381 Assert(PTy, "Store operand must be a pointer.", &SI);
3382 Type *ElTy = PTy->getElementType();
3383 Assert(ElTy == SI.getOperand(0)->getType(),
3384 "Stored value type does not match pointer operand type!", &SI, ElTy);
3385 Assert(SI.getAlignment() <= Value::MaximumAlignment,
3386 "huge alignment values are unsupported", &SI);
3387 Assert(ElTy->isSized(), "storing unsized types is not allowed", &SI);
3388 if (SI.isAtomic()) {
3389 Assert(SI.getOrdering() != AtomicOrdering::Acquire &&
3390 SI.getOrdering() != AtomicOrdering::AcquireRelease,
3391 "Store cannot have Acquire ordering", &SI);
3392 Assert(SI.getAlignment() != 0,
3393 "Atomic store must specify explicit alignment", &SI);
3394 Assert(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
3395 "atomic store operand must have integer, pointer, or floating point "
3396 "type!",
3397 ElTy, &SI);
3398 checkAtomicMemAccessSize(ElTy, &SI);
3399 } else {
3400 Assert(SI.getSyncScopeID() == SyncScope::System,
3401 "Non-atomic store cannot have SynchronizationScope specified", &SI);
3403 visitInstruction(SI);
3406 /// Check that SwiftErrorVal is used as a swifterror argument in CS.
3407 void Verifier::verifySwiftErrorCall(CallBase &Call,
3408 const Value *SwiftErrorVal) {
3409 unsigned Idx = 0;
3410 for (auto I = Call.arg_begin(), E = Call.arg_end(); I != E; ++I, ++Idx) {
3411 if (*I == SwiftErrorVal) {
3412 Assert(Call.paramHasAttr(Idx, Attribute::SwiftError),
3413 "swifterror value when used in a callsite should be marked "
3414 "with swifterror attribute",
3415 SwiftErrorVal, Call);
3420 void Verifier::verifySwiftErrorValue(const Value *SwiftErrorVal) {
3421 // Check that swifterror value is only used by loads, stores, or as
3422 // a swifterror argument.
3423 for (const User *U : SwiftErrorVal->users()) {
3424 Assert(isa<LoadInst>(U) || isa<StoreInst>(U) || isa<CallInst>(U) ||
3425 isa<InvokeInst>(U),
3426 "swifterror value can only be loaded and stored from, or "
3427 "as a swifterror argument!",
3428 SwiftErrorVal, U);
3429 // If it is used by a store, check it is the second operand.
3430 if (auto StoreI = dyn_cast<StoreInst>(U))
3431 Assert(StoreI->getOperand(1) == SwiftErrorVal,
3432 "swifterror value should be the second operand when used "
3433 "by stores", SwiftErrorVal, U);
3434 if (auto *Call = dyn_cast<CallBase>(U))
3435 verifySwiftErrorCall(*const_cast<CallBase *>(Call), SwiftErrorVal);
3439 void Verifier::visitAllocaInst(AllocaInst &AI) {
3440 SmallPtrSet<Type*, 4> Visited;
3441 PointerType *PTy = AI.getType();
3442 // TODO: Relax this restriction?
3443 Assert(PTy->getAddressSpace() == DL.getAllocaAddrSpace(),
3444 "Allocation instruction pointer not in the stack address space!",
3445 &AI);
3446 Assert(AI.getAllocatedType()->isSized(&Visited),
3447 "Cannot allocate unsized type", &AI);
3448 Assert(AI.getArraySize()->getType()->isIntegerTy(),
3449 "Alloca array size must have integer type", &AI);
3450 Assert(AI.getAlignment() <= Value::MaximumAlignment,
3451 "huge alignment values are unsupported", &AI);
3453 if (AI.isSwiftError()) {
3454 verifySwiftErrorValue(&AI);
3457 visitInstruction(AI);
3460 void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI) {
3462 // FIXME: more conditions???
3463 Assert(CXI.getSuccessOrdering() != AtomicOrdering::NotAtomic,
3464 "cmpxchg instructions must be atomic.", &CXI);
3465 Assert(CXI.getFailureOrdering() != AtomicOrdering::NotAtomic,
3466 "cmpxchg instructions must be atomic.", &CXI);
3467 Assert(CXI.getSuccessOrdering() != AtomicOrdering::Unordered,
3468 "cmpxchg instructions cannot be unordered.", &CXI);
3469 Assert(CXI.getFailureOrdering() != AtomicOrdering::Unordered,
3470 "cmpxchg instructions cannot be unordered.", &CXI);
3471 Assert(!isStrongerThan(CXI.getFailureOrdering(), CXI.getSuccessOrdering()),
3472 "cmpxchg instructions failure argument shall be no stronger than the "
3473 "success argument",
3474 &CXI);
3475 Assert(CXI.getFailureOrdering() != AtomicOrdering::Release &&
3476 CXI.getFailureOrdering() != AtomicOrdering::AcquireRelease,
3477 "cmpxchg failure ordering cannot include release semantics", &CXI);
3479 PointerType *PTy = dyn_cast<PointerType>(CXI.getOperand(0)->getType());
3480 Assert(PTy, "First cmpxchg operand must be a pointer.", &CXI);
3481 Type *ElTy = PTy->getElementType();
3482 Assert(ElTy->isIntOrPtrTy(),
3483 "cmpxchg operand must have integer or pointer type", ElTy, &CXI);
3484 checkAtomicMemAccessSize(ElTy, &CXI);
3485 Assert(ElTy == CXI.getOperand(1)->getType(),
3486 "Expected value type does not match pointer operand type!", &CXI,
3487 ElTy);
3488 Assert(ElTy == CXI.getOperand(2)->getType(),
3489 "Stored value type does not match pointer operand type!", &CXI, ElTy);
3490 visitInstruction(CXI);
3493 void Verifier::visitAtomicRMWInst(AtomicRMWInst &RMWI) {
3494 Assert(RMWI.getOrdering() != AtomicOrdering::NotAtomic,
3495 "atomicrmw instructions must be atomic.", &RMWI);
3496 Assert(RMWI.getOrdering() != AtomicOrdering::Unordered,
3497 "atomicrmw instructions cannot be unordered.", &RMWI);
3498 auto Op = RMWI.getOperation();
3499 PointerType *PTy = dyn_cast<PointerType>(RMWI.getOperand(0)->getType());
3500 Assert(PTy, "First atomicrmw operand must be a pointer.", &RMWI);
3501 Type *ElTy = PTy->getElementType();
3502 if (Op == AtomicRMWInst::Xchg) {
3503 Assert(ElTy->isIntegerTy() || ElTy->isFloatingPointTy(), "atomicrmw " +
3504 AtomicRMWInst::getOperationName(Op) +
3505 " operand must have integer or floating point type!",
3506 &RMWI, ElTy);
3507 } else if (AtomicRMWInst::isFPOperation(Op)) {
3508 Assert(ElTy->isFloatingPointTy(), "atomicrmw " +
3509 AtomicRMWInst::getOperationName(Op) +
3510 " operand must have floating point type!",
3511 &RMWI, ElTy);
3512 } else {
3513 Assert(ElTy->isIntegerTy(), "atomicrmw " +
3514 AtomicRMWInst::getOperationName(Op) +
3515 " operand must have integer type!",
3516 &RMWI, ElTy);
3518 checkAtomicMemAccessSize(ElTy, &RMWI);
3519 Assert(ElTy == RMWI.getOperand(1)->getType(),
3520 "Argument value type does not match pointer operand type!", &RMWI,
3521 ElTy);
3522 Assert(AtomicRMWInst::FIRST_BINOP <= Op && Op <= AtomicRMWInst::LAST_BINOP,
3523 "Invalid binary operation!", &RMWI);
3524 visitInstruction(RMWI);
3527 void Verifier::visitFenceInst(FenceInst &FI) {
3528 const AtomicOrdering Ordering = FI.getOrdering();
3529 Assert(Ordering == AtomicOrdering::Acquire ||
3530 Ordering == AtomicOrdering::Release ||
3531 Ordering == AtomicOrdering::AcquireRelease ||
3532 Ordering == AtomicOrdering::SequentiallyConsistent,
3533 "fence instructions may only have acquire, release, acq_rel, or "
3534 "seq_cst ordering.",
3535 &FI);
3536 visitInstruction(FI);
3539 void Verifier::visitExtractValueInst(ExtractValueInst &EVI) {
3540 Assert(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(),
3541 EVI.getIndices()) == EVI.getType(),
3542 "Invalid ExtractValueInst operands!", &EVI);
3544 visitInstruction(EVI);
3547 void Verifier::visitInsertValueInst(InsertValueInst &IVI) {
3548 Assert(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(),
3549 IVI.getIndices()) ==
3550 IVI.getOperand(1)->getType(),
3551 "Invalid InsertValueInst operands!", &IVI);
3553 visitInstruction(IVI);
3556 static Value *getParentPad(Value *EHPad) {
3557 if (auto *FPI = dyn_cast<FuncletPadInst>(EHPad))
3558 return FPI->getParentPad();
3560 return cast<CatchSwitchInst>(EHPad)->getParentPad();
3563 void Verifier::visitEHPadPredecessors(Instruction &I) {
3564 assert(I.isEHPad());
3566 BasicBlock *BB = I.getParent();
3567 Function *F = BB->getParent();
3569 Assert(BB != &F->getEntryBlock(), "EH pad cannot be in entry block.", &I);
3571 if (auto *LPI = dyn_cast<LandingPadInst>(&I)) {
3572 // The landingpad instruction defines its parent as a landing pad block. The
3573 // landing pad block may be branched to only by the unwind edge of an
3574 // invoke.
3575 for (BasicBlock *PredBB : predecessors(BB)) {
3576 const auto *II = dyn_cast<InvokeInst>(PredBB->getTerminator());
3577 Assert(II && II->getUnwindDest() == BB && II->getNormalDest() != BB,
3578 "Block containing LandingPadInst must be jumped to "
3579 "only by the unwind edge of an invoke.",
3580 LPI);
3582 return;
3584 if (auto *CPI = dyn_cast<CatchPadInst>(&I)) {
3585 if (!pred_empty(BB))
3586 Assert(BB->getUniquePredecessor() == CPI->getCatchSwitch()->getParent(),
3587 "Block containg CatchPadInst must be jumped to "
3588 "only by its catchswitch.",
3589 CPI);
3590 Assert(BB != CPI->getCatchSwitch()->getUnwindDest(),
3591 "Catchswitch cannot unwind to one of its catchpads",
3592 CPI->getCatchSwitch(), CPI);
3593 return;
3596 // Verify that each pred has a legal terminator with a legal to/from EH
3597 // pad relationship.
3598 Instruction *ToPad = &I;
3599 Value *ToPadParent = getParentPad(ToPad);
3600 for (BasicBlock *PredBB : predecessors(BB)) {
3601 Instruction *TI = PredBB->getTerminator();
3602 Value *FromPad;
3603 if (auto *II = dyn_cast<InvokeInst>(TI)) {
3604 Assert(II->getUnwindDest() == BB && II->getNormalDest() != BB,
3605 "EH pad must be jumped to via an unwind edge", ToPad, II);
3606 if (auto Bundle = II->getOperandBundle(LLVMContext::OB_funclet))
3607 FromPad = Bundle->Inputs[0];
3608 else
3609 FromPad = ConstantTokenNone::get(II->getContext());
3610 } else if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) {
3611 FromPad = CRI->getOperand(0);
3612 Assert(FromPad != ToPadParent, "A cleanupret must exit its cleanup", CRI);
3613 } else if (auto *CSI = dyn_cast<CatchSwitchInst>(TI)) {
3614 FromPad = CSI;
3615 } else {
3616 Assert(false, "EH pad must be jumped to via an unwind edge", ToPad, TI);
3619 // The edge may exit from zero or more nested pads.
3620 SmallSet<Value *, 8> Seen;
3621 for (;; FromPad = getParentPad(FromPad)) {
3622 Assert(FromPad != ToPad,
3623 "EH pad cannot handle exceptions raised within it", FromPad, TI);
3624 if (FromPad == ToPadParent) {
3625 // This is a legal unwind edge.
3626 break;
3628 Assert(!isa<ConstantTokenNone>(FromPad),
3629 "A single unwind edge may only enter one EH pad", TI);
3630 Assert(Seen.insert(FromPad).second,
3631 "EH pad jumps through a cycle of pads", FromPad);
3636 void Verifier::visitLandingPadInst(LandingPadInst &LPI) {
3637 // The landingpad instruction is ill-formed if it doesn't have any clauses and
3638 // isn't a cleanup.
3639 Assert(LPI.getNumClauses() > 0 || LPI.isCleanup(),
3640 "LandingPadInst needs at least one clause or to be a cleanup.", &LPI);
3642 visitEHPadPredecessors(LPI);
3644 if (!LandingPadResultTy)
3645 LandingPadResultTy = LPI.getType();
3646 else
3647 Assert(LandingPadResultTy == LPI.getType(),
3648 "The landingpad instruction should have a consistent result type "
3649 "inside a function.",
3650 &LPI);
3652 Function *F = LPI.getParent()->getParent();
3653 Assert(F->hasPersonalityFn(),
3654 "LandingPadInst needs to be in a function with a personality.", &LPI);
3656 // The landingpad instruction must be the first non-PHI instruction in the
3657 // block.
3658 Assert(LPI.getParent()->getLandingPadInst() == &LPI,
3659 "LandingPadInst not the first non-PHI instruction in the block.",
3660 &LPI);
3662 for (unsigned i = 0, e = LPI.getNumClauses(); i < e; ++i) {
3663 Constant *Clause = LPI.getClause(i);
3664 if (LPI.isCatch(i)) {
3665 Assert(isa<PointerType>(Clause->getType()),
3666 "Catch operand does not have pointer type!", &LPI);
3667 } else {
3668 Assert(LPI.isFilter(i), "Clause is neither catch nor filter!", &LPI);
3669 Assert(isa<ConstantArray>(Clause) || isa<ConstantAggregateZero>(Clause),
3670 "Filter operand is not an array of constants!", &LPI);
3674 visitInstruction(LPI);
3677 void Verifier::visitResumeInst(ResumeInst &RI) {
3678 Assert(RI.getFunction()->hasPersonalityFn(),
3679 "ResumeInst needs to be in a function with a personality.", &RI);
3681 if (!LandingPadResultTy)
3682 LandingPadResultTy = RI.getValue()->getType();
3683 else
3684 Assert(LandingPadResultTy == RI.getValue()->getType(),
3685 "The resume instruction should have a consistent result type "
3686 "inside a function.",
3687 &RI);
3689 visitTerminator(RI);
3692 void Verifier::visitCatchPadInst(CatchPadInst &CPI) {
3693 BasicBlock *BB = CPI.getParent();
3695 Function *F = BB->getParent();
3696 Assert(F->hasPersonalityFn(),
3697 "CatchPadInst needs to be in a function with a personality.", &CPI);
3699 Assert(isa<CatchSwitchInst>(CPI.getParentPad()),
3700 "CatchPadInst needs to be directly nested in a CatchSwitchInst.",
3701 CPI.getParentPad());
3703 // The catchpad instruction must be the first non-PHI instruction in the
3704 // block.
3705 Assert(BB->getFirstNonPHI() == &CPI,
3706 "CatchPadInst not the first non-PHI instruction in the block.", &CPI);
3708 visitEHPadPredecessors(CPI);
3709 visitFuncletPadInst(CPI);
3712 void Verifier::visitCatchReturnInst(CatchReturnInst &CatchReturn) {
3713 Assert(isa<CatchPadInst>(CatchReturn.getOperand(0)),
3714 "CatchReturnInst needs to be provided a CatchPad", &CatchReturn,
3715 CatchReturn.getOperand(0));
3717 visitTerminator(CatchReturn);
3720 void Verifier::visitCleanupPadInst(CleanupPadInst &CPI) {
3721 BasicBlock *BB = CPI.getParent();
3723 Function *F = BB->getParent();
3724 Assert(F->hasPersonalityFn(),
3725 "CleanupPadInst needs to be in a function with a personality.", &CPI);
3727 // The cleanuppad instruction must be the first non-PHI instruction in the
3728 // block.
3729 Assert(BB->getFirstNonPHI() == &CPI,
3730 "CleanupPadInst not the first non-PHI instruction in the block.",
3731 &CPI);
3733 auto *ParentPad = CPI.getParentPad();
3734 Assert(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
3735 "CleanupPadInst has an invalid parent.", &CPI);
3737 visitEHPadPredecessors(CPI);
3738 visitFuncletPadInst(CPI);
3741 void Verifier::visitFuncletPadInst(FuncletPadInst &FPI) {
3742 User *FirstUser = nullptr;
3743 Value *FirstUnwindPad = nullptr;
3744 SmallVector<FuncletPadInst *, 8> Worklist({&FPI});
3745 SmallSet<FuncletPadInst *, 8> Seen;
3747 while (!Worklist.empty()) {
3748 FuncletPadInst *CurrentPad = Worklist.pop_back_val();
3749 Assert(Seen.insert(CurrentPad).second,
3750 "FuncletPadInst must not be nested within itself", CurrentPad);
3751 Value *UnresolvedAncestorPad = nullptr;
3752 for (User *U : CurrentPad->users()) {
3753 BasicBlock *UnwindDest;
3754 if (auto *CRI = dyn_cast<CleanupReturnInst>(U)) {
3755 UnwindDest = CRI->getUnwindDest();
3756 } else if (auto *CSI = dyn_cast<CatchSwitchInst>(U)) {
3757 // We allow catchswitch unwind to caller to nest
3758 // within an outer pad that unwinds somewhere else,
3759 // because catchswitch doesn't have a nounwind variant.
3760 // See e.g. SimplifyCFGOpt::SimplifyUnreachable.
3761 if (CSI->unwindsToCaller())
3762 continue;
3763 UnwindDest = CSI->getUnwindDest();
3764 } else if (auto *II = dyn_cast<InvokeInst>(U)) {
3765 UnwindDest = II->getUnwindDest();
3766 } else if (isa<CallInst>(U)) {
3767 // Calls which don't unwind may be found inside funclet
3768 // pads that unwind somewhere else. We don't *require*
3769 // such calls to be annotated nounwind.
3770 continue;
3771 } else if (auto *CPI = dyn_cast<CleanupPadInst>(U)) {
3772 // The unwind dest for a cleanup can only be found by
3773 // recursive search. Add it to the worklist, and we'll
3774 // search for its first use that determines where it unwinds.
3775 Worklist.push_back(CPI);
3776 continue;
3777 } else {
3778 Assert(isa<CatchReturnInst>(U), "Bogus funclet pad use", U);
3779 continue;
3782 Value *UnwindPad;
3783 bool ExitsFPI;
3784 if (UnwindDest) {
3785 UnwindPad = UnwindDest->getFirstNonPHI();
3786 if (!cast<Instruction>(UnwindPad)->isEHPad())
3787 continue;
3788 Value *UnwindParent = getParentPad(UnwindPad);
3789 // Ignore unwind edges that don't exit CurrentPad.
3790 if (UnwindParent == CurrentPad)
3791 continue;
3792 // Determine whether the original funclet pad is exited,
3793 // and if we are scanning nested pads determine how many
3794 // of them are exited so we can stop searching their
3795 // children.
3796 Value *ExitedPad = CurrentPad;
3797 ExitsFPI = false;
3798 do {
3799 if (ExitedPad == &FPI) {
3800 ExitsFPI = true;
3801 // Now we can resolve any ancestors of CurrentPad up to
3802 // FPI, but not including FPI since we need to make sure
3803 // to check all direct users of FPI for consistency.
3804 UnresolvedAncestorPad = &FPI;
3805 break;
3807 Value *ExitedParent = getParentPad(ExitedPad);
3808 if (ExitedParent == UnwindParent) {
3809 // ExitedPad is the ancestor-most pad which this unwind
3810 // edge exits, so we can resolve up to it, meaning that
3811 // ExitedParent is the first ancestor still unresolved.
3812 UnresolvedAncestorPad = ExitedParent;
3813 break;
3815 ExitedPad = ExitedParent;
3816 } while (!isa<ConstantTokenNone>(ExitedPad));
3817 } else {
3818 // Unwinding to caller exits all pads.
3819 UnwindPad = ConstantTokenNone::get(FPI.getContext());
3820 ExitsFPI = true;
3821 UnresolvedAncestorPad = &FPI;
3824 if (ExitsFPI) {
3825 // This unwind edge exits FPI. Make sure it agrees with other
3826 // such edges.
3827 if (FirstUser) {
3828 Assert(UnwindPad == FirstUnwindPad, "Unwind edges out of a funclet "
3829 "pad must have the same unwind "
3830 "dest",
3831 &FPI, U, FirstUser);
3832 } else {
3833 FirstUser = U;
3834 FirstUnwindPad = UnwindPad;
3835 // Record cleanup sibling unwinds for verifySiblingFuncletUnwinds
3836 if (isa<CleanupPadInst>(&FPI) && !isa<ConstantTokenNone>(UnwindPad) &&
3837 getParentPad(UnwindPad) == getParentPad(&FPI))
3838 SiblingFuncletInfo[&FPI] = cast<Instruction>(U);
3841 // Make sure we visit all uses of FPI, but for nested pads stop as
3842 // soon as we know where they unwind to.
3843 if (CurrentPad != &FPI)
3844 break;
3846 if (UnresolvedAncestorPad) {
3847 if (CurrentPad == UnresolvedAncestorPad) {
3848 // When CurrentPad is FPI itself, we don't mark it as resolved even if
3849 // we've found an unwind edge that exits it, because we need to verify
3850 // all direct uses of FPI.
3851 assert(CurrentPad == &FPI);
3852 continue;
3854 // Pop off the worklist any nested pads that we've found an unwind
3855 // destination for. The pads on the worklist are the uncles,
3856 // great-uncles, etc. of CurrentPad. We've found an unwind destination
3857 // for all ancestors of CurrentPad up to but not including
3858 // UnresolvedAncestorPad.
3859 Value *ResolvedPad = CurrentPad;
3860 while (!Worklist.empty()) {
3861 Value *UnclePad = Worklist.back();
3862 Value *AncestorPad = getParentPad(UnclePad);
3863 // Walk ResolvedPad up the ancestor list until we either find the
3864 // uncle's parent or the last resolved ancestor.
3865 while (ResolvedPad != AncestorPad) {
3866 Value *ResolvedParent = getParentPad(ResolvedPad);
3867 if (ResolvedParent == UnresolvedAncestorPad) {
3868 break;
3870 ResolvedPad = ResolvedParent;
3872 // If the resolved ancestor search didn't find the uncle's parent,
3873 // then the uncle is not yet resolved.
3874 if (ResolvedPad != AncestorPad)
3875 break;
3876 // This uncle is resolved, so pop it from the worklist.
3877 Worklist.pop_back();
3882 if (FirstUnwindPad) {
3883 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FPI.getParentPad())) {
3884 BasicBlock *SwitchUnwindDest = CatchSwitch->getUnwindDest();
3885 Value *SwitchUnwindPad;
3886 if (SwitchUnwindDest)
3887 SwitchUnwindPad = SwitchUnwindDest->getFirstNonPHI();
3888 else
3889 SwitchUnwindPad = ConstantTokenNone::get(FPI.getContext());
3890 Assert(SwitchUnwindPad == FirstUnwindPad,
3891 "Unwind edges out of a catch must have the same unwind dest as "
3892 "the parent catchswitch",
3893 &FPI, FirstUser, CatchSwitch);
3897 visitInstruction(FPI);
3900 void Verifier::visitCatchSwitchInst(CatchSwitchInst &CatchSwitch) {
3901 BasicBlock *BB = CatchSwitch.getParent();
3903 Function *F = BB->getParent();
3904 Assert(F->hasPersonalityFn(),
3905 "CatchSwitchInst needs to be in a function with a personality.",
3906 &CatchSwitch);
3908 // The catchswitch instruction must be the first non-PHI instruction in the
3909 // block.
3910 Assert(BB->getFirstNonPHI() == &CatchSwitch,
3911 "CatchSwitchInst not the first non-PHI instruction in the block.",
3912 &CatchSwitch);
3914 auto *ParentPad = CatchSwitch.getParentPad();
3915 Assert(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
3916 "CatchSwitchInst has an invalid parent.", ParentPad);
3918 if (BasicBlock *UnwindDest = CatchSwitch.getUnwindDest()) {
3919 Instruction *I = UnwindDest->getFirstNonPHI();
3920 Assert(I->isEHPad() && !isa<LandingPadInst>(I),
3921 "CatchSwitchInst must unwind to an EH block which is not a "
3922 "landingpad.",
3923 &CatchSwitch);
3925 // Record catchswitch sibling unwinds for verifySiblingFuncletUnwinds
3926 if (getParentPad(I) == ParentPad)
3927 SiblingFuncletInfo[&CatchSwitch] = &CatchSwitch;
3930 Assert(CatchSwitch.getNumHandlers() != 0,
3931 "CatchSwitchInst cannot have empty handler list", &CatchSwitch);
3933 for (BasicBlock *Handler : CatchSwitch.handlers()) {
3934 Assert(isa<CatchPadInst>(Handler->getFirstNonPHI()),
3935 "CatchSwitchInst handlers must be catchpads", &CatchSwitch, Handler);
3938 visitEHPadPredecessors(CatchSwitch);
3939 visitTerminator(CatchSwitch);
3942 void Verifier::visitCleanupReturnInst(CleanupReturnInst &CRI) {
3943 Assert(isa<CleanupPadInst>(CRI.getOperand(0)),
3944 "CleanupReturnInst needs to be provided a CleanupPad", &CRI,
3945 CRI.getOperand(0));
3947 if (BasicBlock *UnwindDest = CRI.getUnwindDest()) {
3948 Instruction *I = UnwindDest->getFirstNonPHI();
3949 Assert(I->isEHPad() && !isa<LandingPadInst>(I),
3950 "CleanupReturnInst must unwind to an EH block which is not a "
3951 "landingpad.",
3952 &CRI);
3955 visitTerminator(CRI);
3958 void Verifier::verifyDominatesUse(Instruction &I, unsigned i) {
3959 Instruction *Op = cast<Instruction>(I.getOperand(i));
3960 // If the we have an invalid invoke, don't try to compute the dominance.
3961 // We already reject it in the invoke specific checks and the dominance
3962 // computation doesn't handle multiple edges.
3963 if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
3964 if (II->getNormalDest() == II->getUnwindDest())
3965 return;
3968 // Quick check whether the def has already been encountered in the same block.
3969 // PHI nodes are not checked to prevent accepting preceding PHIs, because PHI
3970 // uses are defined to happen on the incoming edge, not at the instruction.
3972 // FIXME: If this operand is a MetadataAsValue (wrapping a LocalAsMetadata)
3973 // wrapping an SSA value, assert that we've already encountered it. See
3974 // related FIXME in Mapper::mapLocalAsMetadata in ValueMapper.cpp.
3975 if (!isa<PHINode>(I) && InstsInThisBlock.count(Op))
3976 return;
3978 const Use &U = I.getOperandUse(i);
3979 Assert(DT.dominates(Op, U),
3980 "Instruction does not dominate all uses!", Op, &I);
3983 void Verifier::visitDereferenceableMetadata(Instruction& I, MDNode* MD) {
3984 Assert(I.getType()->isPointerTy(), "dereferenceable, dereferenceable_or_null "
3985 "apply only to pointer types", &I);
3986 Assert((isa<LoadInst>(I) || isa<IntToPtrInst>(I)),
3987 "dereferenceable, dereferenceable_or_null apply only to load"
3988 " and inttoptr instructions, use attributes for calls or invokes", &I);
3989 Assert(MD->getNumOperands() == 1, "dereferenceable, dereferenceable_or_null "
3990 "take one operand!", &I);
3991 ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(MD->getOperand(0));
3992 Assert(CI && CI->getType()->isIntegerTy(64), "dereferenceable, "
3993 "dereferenceable_or_null metadata value must be an i64!", &I);
3996 /// verifyInstruction - Verify that an instruction is well formed.
3998 void Verifier::visitInstruction(Instruction &I) {
3999 BasicBlock *BB = I.getParent();
4000 Assert(BB, "Instruction not embedded in basic block!", &I);
4002 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
4003 for (User *U : I.users()) {
4004 Assert(U != (User *)&I || !DT.isReachableFromEntry(BB),
4005 "Only PHI nodes may reference their own value!", &I);
4009 // Check that void typed values don't have names
4010 Assert(!I.getType()->isVoidTy() || !I.hasName(),
4011 "Instruction has a name, but provides a void value!", &I);
4013 // Check that the return value of the instruction is either void or a legal
4014 // value type.
4015 Assert(I.getType()->isVoidTy() || I.getType()->isFirstClassType(),
4016 "Instruction returns a non-scalar type!", &I);
4018 // Check that the instruction doesn't produce metadata. Calls are already
4019 // checked against the callee type.
4020 Assert(!I.getType()->isMetadataTy() || isa<CallInst>(I) || isa<InvokeInst>(I),
4021 "Invalid use of metadata!", &I);
4023 // Check that all uses of the instruction, if they are instructions
4024 // themselves, actually have parent basic blocks. If the use is not an
4025 // instruction, it is an error!
4026 for (Use &U : I.uses()) {
4027 if (Instruction *Used = dyn_cast<Instruction>(U.getUser()))
4028 Assert(Used->getParent() != nullptr,
4029 "Instruction referencing"
4030 " instruction not embedded in a basic block!",
4031 &I, Used);
4032 else {
4033 CheckFailed("Use of instruction is not an instruction!", U);
4034 return;
4038 // Get a pointer to the call base of the instruction if it is some form of
4039 // call.
4040 const CallBase *CBI = dyn_cast<CallBase>(&I);
4042 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
4043 Assert(I.getOperand(i) != nullptr, "Instruction has null operand!", &I);
4045 // Check to make sure that only first-class-values are operands to
4046 // instructions.
4047 if (!I.getOperand(i)->getType()->isFirstClassType()) {
4048 Assert(false, "Instruction operands must be first-class values!", &I);
4051 if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
4052 // Check to make sure that the "address of" an intrinsic function is never
4053 // taken.
4054 Assert(!F->isIntrinsic() ||
4055 (CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i)),
4056 "Cannot take the address of an intrinsic!", &I);
4057 Assert(
4058 !F->isIntrinsic() || isa<CallInst>(I) ||
4059 F->getIntrinsicID() == Intrinsic::donothing ||
4060 F->getIntrinsicID() == Intrinsic::coro_resume ||
4061 F->getIntrinsicID() == Intrinsic::coro_destroy ||
4062 F->getIntrinsicID() == Intrinsic::experimental_patchpoint_void ||
4063 F->getIntrinsicID() == Intrinsic::experimental_patchpoint_i64 ||
4064 F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint ||
4065 F->getIntrinsicID() == Intrinsic::wasm_rethrow_in_catch,
4066 "Cannot invoke an intrinsic other than donothing, patchpoint, "
4067 "statepoint, coro_resume or coro_destroy",
4068 &I);
4069 Assert(F->getParent() == &M, "Referencing function in another module!",
4070 &I, &M, F, F->getParent());
4071 } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
4072 Assert(OpBB->getParent() == BB->getParent(),
4073 "Referring to a basic block in another function!", &I);
4074 } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
4075 Assert(OpArg->getParent() == BB->getParent(),
4076 "Referring to an argument in another function!", &I);
4077 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) {
4078 Assert(GV->getParent() == &M, "Referencing global in another module!", &I,
4079 &M, GV, GV->getParent());
4080 } else if (isa<Instruction>(I.getOperand(i))) {
4081 verifyDominatesUse(I, i);
4082 } else if (isa<InlineAsm>(I.getOperand(i))) {
4083 Assert(CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i),
4084 "Cannot take the address of an inline asm!", &I);
4085 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(i))) {
4086 if (CE->getType()->isPtrOrPtrVectorTy() ||
4087 !DL.getNonIntegralAddressSpaces().empty()) {
4088 // If we have a ConstantExpr pointer, we need to see if it came from an
4089 // illegal bitcast. If the datalayout string specifies non-integral
4090 // address spaces then we also need to check for illegal ptrtoint and
4091 // inttoptr expressions.
4092 visitConstantExprsRecursively(CE);
4097 if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpmath)) {
4098 Assert(I.getType()->isFPOrFPVectorTy(),
4099 "fpmath requires a floating point result!", &I);
4100 Assert(MD->getNumOperands() == 1, "fpmath takes one operand!", &I);
4101 if (ConstantFP *CFP0 =
4102 mdconst::dyn_extract_or_null<ConstantFP>(MD->getOperand(0))) {
4103 const APFloat &Accuracy = CFP0->getValueAPF();
4104 Assert(&Accuracy.getSemantics() == &APFloat::IEEEsingle(),
4105 "fpmath accuracy must have float type", &I);
4106 Assert(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(),
4107 "fpmath accuracy not a positive number!", &I);
4108 } else {
4109 Assert(false, "invalid fpmath accuracy!", &I);
4113 if (MDNode *Range = I.getMetadata(LLVMContext::MD_range)) {
4114 Assert(isa<LoadInst>(I) || isa<CallInst>(I) || isa<InvokeInst>(I),
4115 "Ranges are only for loads, calls and invokes!", &I);
4116 visitRangeMetadata(I, Range, I.getType());
4119 if (I.getMetadata(LLVMContext::MD_nonnull)) {
4120 Assert(I.getType()->isPointerTy(), "nonnull applies only to pointer types",
4121 &I);
4122 Assert(isa<LoadInst>(I),
4123 "nonnull applies only to load instructions, use attributes"
4124 " for calls or invokes",
4125 &I);
4128 if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable))
4129 visitDereferenceableMetadata(I, MD);
4131 if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable_or_null))
4132 visitDereferenceableMetadata(I, MD);
4134 if (MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa))
4135 TBAAVerifyHelper.visitTBAAMetadata(I, TBAA);
4137 if (MDNode *AlignMD = I.getMetadata(LLVMContext::MD_align)) {
4138 Assert(I.getType()->isPointerTy(), "align applies only to pointer types",
4139 &I);
4140 Assert(isa<LoadInst>(I), "align applies only to load instructions, "
4141 "use attributes for calls or invokes", &I);
4142 Assert(AlignMD->getNumOperands() == 1, "align takes one operand!", &I);
4143 ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(AlignMD->getOperand(0));
4144 Assert(CI && CI->getType()->isIntegerTy(64),
4145 "align metadata value must be an i64!", &I);
4146 uint64_t Align = CI->getZExtValue();
4147 Assert(isPowerOf2_64(Align),
4148 "align metadata value must be a power of 2!", &I);
4149 Assert(Align <= Value::MaximumAlignment,
4150 "alignment is larger that implementation defined limit", &I);
4153 if (MDNode *N = I.getDebugLoc().getAsMDNode()) {
4154 AssertDI(isa<DILocation>(N), "invalid !dbg metadata attachment", &I, N);
4155 visitMDNode(*N);
4158 if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I))
4159 verifyFragmentExpression(*DII);
4161 InstsInThisBlock.insert(&I);
4164 /// Allow intrinsics to be verified in different ways.
4165 void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
4166 Function *IF = Call.getCalledFunction();
4167 Assert(IF->isDeclaration(), "Intrinsic functions should never be defined!",
4168 IF);
4170 // Verify that the intrinsic prototype lines up with what the .td files
4171 // describe.
4172 FunctionType *IFTy = IF->getFunctionType();
4173 bool IsVarArg = IFTy->isVarArg();
4175 SmallVector<Intrinsic::IITDescriptor, 8> Table;
4176 getIntrinsicInfoTableEntries(ID, Table);
4177 ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
4179 // Walk the descriptors to extract overloaded types.
4180 SmallVector<Type *, 4> ArgTys;
4181 Intrinsic::MatchIntrinsicTypesResult Res =
4182 Intrinsic::matchIntrinsicSignature(IFTy, TableRef, ArgTys);
4183 Assert(Res != Intrinsic::MatchIntrinsicTypes_NoMatchRet,
4184 "Intrinsic has incorrect return type!", IF);
4185 Assert(Res != Intrinsic::MatchIntrinsicTypes_NoMatchArg,
4186 "Intrinsic has incorrect argument type!", IF);
4188 // Verify if the intrinsic call matches the vararg property.
4189 if (IsVarArg)
4190 Assert(!Intrinsic::matchIntrinsicVarArg(IsVarArg, TableRef),
4191 "Intrinsic was not defined with variable arguments!", IF);
4192 else
4193 Assert(!Intrinsic::matchIntrinsicVarArg(IsVarArg, TableRef),
4194 "Callsite was not defined with variable arguments!", IF);
4196 // All descriptors should be absorbed by now.
4197 Assert(TableRef.empty(), "Intrinsic has too few arguments!", IF);
4199 // Now that we have the intrinsic ID and the actual argument types (and we
4200 // know they are legal for the intrinsic!) get the intrinsic name through the
4201 // usual means. This allows us to verify the mangling of argument types into
4202 // the name.
4203 const std::string ExpectedName = Intrinsic::getName(ID, ArgTys);
4204 Assert(ExpectedName == IF->getName(),
4205 "Intrinsic name not mangled correctly for type arguments! "
4206 "Should be: " +
4207 ExpectedName,
4208 IF);
4210 // If the intrinsic takes MDNode arguments, verify that they are either global
4211 // or are local to *this* function.
4212 for (Value *V : Call.args())
4213 if (auto *MD = dyn_cast<MetadataAsValue>(V))
4214 visitMetadataAsValue(*MD, Call.getCaller());
4216 switch (ID) {
4217 default:
4218 break;
4219 case Intrinsic::coro_id: {
4220 auto *InfoArg = Call.getArgOperand(3)->stripPointerCasts();
4221 if (isa<ConstantPointerNull>(InfoArg))
4222 break;
4223 auto *GV = dyn_cast<GlobalVariable>(InfoArg);
4224 Assert(GV && GV->isConstant() && GV->hasDefinitiveInitializer(),
4225 "info argument of llvm.coro.begin must refer to an initialized "
4226 "constant");
4227 Constant *Init = GV->getInitializer();
4228 Assert(isa<ConstantStruct>(Init) || isa<ConstantArray>(Init),
4229 "info argument of llvm.coro.begin must refer to either a struct or "
4230 "an array");
4231 break;
4233 case Intrinsic::experimental_constrained_fadd:
4234 case Intrinsic::experimental_constrained_fsub:
4235 case Intrinsic::experimental_constrained_fmul:
4236 case Intrinsic::experimental_constrained_fdiv:
4237 case Intrinsic::experimental_constrained_frem:
4238 case Intrinsic::experimental_constrained_fma:
4239 case Intrinsic::experimental_constrained_fptrunc:
4240 case Intrinsic::experimental_constrained_fpext:
4241 case Intrinsic::experimental_constrained_sqrt:
4242 case Intrinsic::experimental_constrained_pow:
4243 case Intrinsic::experimental_constrained_powi:
4244 case Intrinsic::experimental_constrained_sin:
4245 case Intrinsic::experimental_constrained_cos:
4246 case Intrinsic::experimental_constrained_exp:
4247 case Intrinsic::experimental_constrained_exp2:
4248 case Intrinsic::experimental_constrained_log:
4249 case Intrinsic::experimental_constrained_log10:
4250 case Intrinsic::experimental_constrained_log2:
4251 case Intrinsic::experimental_constrained_rint:
4252 case Intrinsic::experimental_constrained_nearbyint:
4253 case Intrinsic::experimental_constrained_maxnum:
4254 case Intrinsic::experimental_constrained_minnum:
4255 case Intrinsic::experimental_constrained_ceil:
4256 case Intrinsic::experimental_constrained_floor:
4257 case Intrinsic::experimental_constrained_round:
4258 case Intrinsic::experimental_constrained_trunc:
4259 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(Call));
4260 break;
4261 case Intrinsic::dbg_declare: // llvm.dbg.declare
4262 Assert(isa<MetadataAsValue>(Call.getArgOperand(0)),
4263 "invalid llvm.dbg.declare intrinsic call 1", Call);
4264 visitDbgIntrinsic("declare", cast<DbgVariableIntrinsic>(Call));
4265 break;
4266 case Intrinsic::dbg_addr: // llvm.dbg.addr
4267 visitDbgIntrinsic("addr", cast<DbgVariableIntrinsic>(Call));
4268 break;
4269 case Intrinsic::dbg_value: // llvm.dbg.value
4270 visitDbgIntrinsic("value", cast<DbgVariableIntrinsic>(Call));
4271 break;
4272 case Intrinsic::dbg_label: // llvm.dbg.label
4273 visitDbgLabelIntrinsic("label", cast<DbgLabelInst>(Call));
4274 break;
4275 case Intrinsic::memcpy:
4276 case Intrinsic::memmove:
4277 case Intrinsic::memset: {
4278 const auto *MI = cast<MemIntrinsic>(&Call);
4279 auto IsValidAlignment = [&](unsigned Alignment) -> bool {
4280 return Alignment == 0 || isPowerOf2_32(Alignment);
4282 Assert(IsValidAlignment(MI->getDestAlignment()),
4283 "alignment of arg 0 of memory intrinsic must be 0 or a power of 2",
4284 Call);
4285 if (const auto *MTI = dyn_cast<MemTransferInst>(MI)) {
4286 Assert(IsValidAlignment(MTI->getSourceAlignment()),
4287 "alignment of arg 1 of memory intrinsic must be 0 or a power of 2",
4288 Call);
4291 break;
4293 case Intrinsic::memcpy_element_unordered_atomic:
4294 case Intrinsic::memmove_element_unordered_atomic:
4295 case Intrinsic::memset_element_unordered_atomic: {
4296 const auto *AMI = cast<AtomicMemIntrinsic>(&Call);
4298 ConstantInt *ElementSizeCI =
4299 cast<ConstantInt>(AMI->getRawElementSizeInBytes());
4300 const APInt &ElementSizeVal = ElementSizeCI->getValue();
4301 Assert(ElementSizeVal.isPowerOf2(),
4302 "element size of the element-wise atomic memory intrinsic "
4303 "must be a power of 2",
4304 Call);
4306 if (auto *LengthCI = dyn_cast<ConstantInt>(AMI->getLength())) {
4307 uint64_t Length = LengthCI->getZExtValue();
4308 uint64_t ElementSize = AMI->getElementSizeInBytes();
4309 Assert((Length % ElementSize) == 0,
4310 "constant length must be a multiple of the element size in the "
4311 "element-wise atomic memory intrinsic",
4312 Call);
4315 auto IsValidAlignment = [&](uint64_t Alignment) {
4316 return isPowerOf2_64(Alignment) && ElementSizeVal.ule(Alignment);
4318 uint64_t DstAlignment = AMI->getDestAlignment();
4319 Assert(IsValidAlignment(DstAlignment),
4320 "incorrect alignment of the destination argument", Call);
4321 if (const auto *AMT = dyn_cast<AtomicMemTransferInst>(AMI)) {
4322 uint64_t SrcAlignment = AMT->getSourceAlignment();
4323 Assert(IsValidAlignment(SrcAlignment),
4324 "incorrect alignment of the source argument", Call);
4326 break;
4328 case Intrinsic::gcroot:
4329 case Intrinsic::gcwrite:
4330 case Intrinsic::gcread:
4331 if (ID == Intrinsic::gcroot) {
4332 AllocaInst *AI =
4333 dyn_cast<AllocaInst>(Call.getArgOperand(0)->stripPointerCasts());
4334 Assert(AI, "llvm.gcroot parameter #1 must be an alloca.", Call);
4335 Assert(isa<Constant>(Call.getArgOperand(1)),
4336 "llvm.gcroot parameter #2 must be a constant.", Call);
4337 if (!AI->getAllocatedType()->isPointerTy()) {
4338 Assert(!isa<ConstantPointerNull>(Call.getArgOperand(1)),
4339 "llvm.gcroot parameter #1 must either be a pointer alloca, "
4340 "or argument #2 must be a non-null constant.",
4341 Call);
4345 Assert(Call.getParent()->getParent()->hasGC(),
4346 "Enclosing function does not use GC.", Call);
4347 break;
4348 case Intrinsic::init_trampoline:
4349 Assert(isa<Function>(Call.getArgOperand(1)->stripPointerCasts()),
4350 "llvm.init_trampoline parameter #2 must resolve to a function.",
4351 Call);
4352 break;
4353 case Intrinsic::prefetch:
4354 Assert(cast<ConstantInt>(Call.getArgOperand(1))->getZExtValue() < 2 &&
4355 cast<ConstantInt>(Call.getArgOperand(2))->getZExtValue() < 4,
4356 "invalid arguments to llvm.prefetch", Call);
4357 break;
4358 case Intrinsic::stackprotector:
4359 Assert(isa<AllocaInst>(Call.getArgOperand(1)->stripPointerCasts()),
4360 "llvm.stackprotector parameter #2 must resolve to an alloca.", Call);
4361 break;
4362 case Intrinsic::localescape: {
4363 BasicBlock *BB = Call.getParent();
4364 Assert(BB == &BB->getParent()->front(),
4365 "llvm.localescape used outside of entry block", Call);
4366 Assert(!SawFrameEscape,
4367 "multiple calls to llvm.localescape in one function", Call);
4368 for (Value *Arg : Call.args()) {
4369 if (isa<ConstantPointerNull>(Arg))
4370 continue; // Null values are allowed as placeholders.
4371 auto *AI = dyn_cast<AllocaInst>(Arg->stripPointerCasts());
4372 Assert(AI && AI->isStaticAlloca(),
4373 "llvm.localescape only accepts static allocas", Call);
4375 FrameEscapeInfo[BB->getParent()].first = Call.getNumArgOperands();
4376 SawFrameEscape = true;
4377 break;
4379 case Intrinsic::localrecover: {
4380 Value *FnArg = Call.getArgOperand(0)->stripPointerCasts();
4381 Function *Fn = dyn_cast<Function>(FnArg);
4382 Assert(Fn && !Fn->isDeclaration(),
4383 "llvm.localrecover first "
4384 "argument must be function defined in this module",
4385 Call);
4386 auto *IdxArg = cast<ConstantInt>(Call.getArgOperand(2));
4387 auto &Entry = FrameEscapeInfo[Fn];
4388 Entry.second = unsigned(
4389 std::max(uint64_t(Entry.second), IdxArg->getLimitedValue(~0U) + 1));
4390 break;
4393 case Intrinsic::experimental_gc_statepoint:
4394 if (auto *CI = dyn_cast<CallInst>(&Call))
4395 Assert(!CI->isInlineAsm(),
4396 "gc.statepoint support for inline assembly unimplemented", CI);
4397 Assert(Call.getParent()->getParent()->hasGC(),
4398 "Enclosing function does not use GC.", Call);
4400 verifyStatepoint(Call);
4401 break;
4402 case Intrinsic::experimental_gc_result: {
4403 Assert(Call.getParent()->getParent()->hasGC(),
4404 "Enclosing function does not use GC.", Call);
4405 // Are we tied to a statepoint properly?
4406 const auto *StatepointCall = dyn_cast<CallBase>(Call.getArgOperand(0));
4407 const Function *StatepointFn =
4408 StatepointCall ? StatepointCall->getCalledFunction() : nullptr;
4409 Assert(StatepointFn && StatepointFn->isDeclaration() &&
4410 StatepointFn->getIntrinsicID() ==
4411 Intrinsic::experimental_gc_statepoint,
4412 "gc.result operand #1 must be from a statepoint", Call,
4413 Call.getArgOperand(0));
4415 // Assert that result type matches wrapped callee.
4416 const Value *Target = StatepointCall->getArgOperand(2);
4417 auto *PT = cast<PointerType>(Target->getType());
4418 auto *TargetFuncType = cast<FunctionType>(PT->getElementType());
4419 Assert(Call.getType() == TargetFuncType->getReturnType(),
4420 "gc.result result type does not match wrapped callee", Call);
4421 break;
4423 case Intrinsic::experimental_gc_relocate: {
4424 Assert(Call.getNumArgOperands() == 3, "wrong number of arguments", Call);
4426 Assert(isa<PointerType>(Call.getType()->getScalarType()),
4427 "gc.relocate must return a pointer or a vector of pointers", Call);
4429 // Check that this relocate is correctly tied to the statepoint
4431 // This is case for relocate on the unwinding path of an invoke statepoint
4432 if (LandingPadInst *LandingPad =
4433 dyn_cast<LandingPadInst>(Call.getArgOperand(0))) {
4435 const BasicBlock *InvokeBB =
4436 LandingPad->getParent()->getUniquePredecessor();
4438 // Landingpad relocates should have only one predecessor with invoke
4439 // statepoint terminator
4440 Assert(InvokeBB, "safepoints should have unique landingpads",
4441 LandingPad->getParent());
4442 Assert(InvokeBB->getTerminator(), "safepoint block should be well formed",
4443 InvokeBB);
4444 Assert(isStatepoint(InvokeBB->getTerminator()),
4445 "gc relocate should be linked to a statepoint", InvokeBB);
4446 } else {
4447 // In all other cases relocate should be tied to the statepoint directly.
4448 // This covers relocates on a normal return path of invoke statepoint and
4449 // relocates of a call statepoint.
4450 auto Token = Call.getArgOperand(0);
4451 Assert(isa<Instruction>(Token) && isStatepoint(cast<Instruction>(Token)),
4452 "gc relocate is incorrectly tied to the statepoint", Call, Token);
4455 // Verify rest of the relocate arguments.
4456 const CallBase &StatepointCall =
4457 *cast<CallBase>(cast<GCRelocateInst>(Call).getStatepoint());
4459 // Both the base and derived must be piped through the safepoint.
4460 Value *Base = Call.getArgOperand(1);
4461 Assert(isa<ConstantInt>(Base),
4462 "gc.relocate operand #2 must be integer offset", Call);
4464 Value *Derived = Call.getArgOperand(2);
4465 Assert(isa<ConstantInt>(Derived),
4466 "gc.relocate operand #3 must be integer offset", Call);
4468 const int BaseIndex = cast<ConstantInt>(Base)->getZExtValue();
4469 const int DerivedIndex = cast<ConstantInt>(Derived)->getZExtValue();
4470 // Check the bounds
4471 Assert(0 <= BaseIndex && BaseIndex < (int)StatepointCall.arg_size(),
4472 "gc.relocate: statepoint base index out of bounds", Call);
4473 Assert(0 <= DerivedIndex && DerivedIndex < (int)StatepointCall.arg_size(),
4474 "gc.relocate: statepoint derived index out of bounds", Call);
4476 // Check that BaseIndex and DerivedIndex fall within the 'gc parameters'
4477 // section of the statepoint's argument.
4478 Assert(StatepointCall.arg_size() > 0,
4479 "gc.statepoint: insufficient arguments");
4480 Assert(isa<ConstantInt>(StatepointCall.getArgOperand(3)),
4481 "gc.statement: number of call arguments must be constant integer");
4482 const unsigned NumCallArgs =
4483 cast<ConstantInt>(StatepointCall.getArgOperand(3))->getZExtValue();
4484 Assert(StatepointCall.arg_size() > NumCallArgs + 5,
4485 "gc.statepoint: mismatch in number of call arguments");
4486 Assert(isa<ConstantInt>(StatepointCall.getArgOperand(NumCallArgs + 5)),
4487 "gc.statepoint: number of transition arguments must be "
4488 "a constant integer");
4489 const int NumTransitionArgs =
4490 cast<ConstantInt>(StatepointCall.getArgOperand(NumCallArgs + 5))
4491 ->getZExtValue();
4492 const int DeoptArgsStart = 4 + NumCallArgs + 1 + NumTransitionArgs + 1;
4493 Assert(isa<ConstantInt>(StatepointCall.getArgOperand(DeoptArgsStart)),
4494 "gc.statepoint: number of deoptimization arguments must be "
4495 "a constant integer");
4496 const int NumDeoptArgs =
4497 cast<ConstantInt>(StatepointCall.getArgOperand(DeoptArgsStart))
4498 ->getZExtValue();
4499 const int GCParamArgsStart = DeoptArgsStart + 1 + NumDeoptArgs;
4500 const int GCParamArgsEnd = StatepointCall.arg_size();
4501 Assert(GCParamArgsStart <= BaseIndex && BaseIndex < GCParamArgsEnd,
4502 "gc.relocate: statepoint base index doesn't fall within the "
4503 "'gc parameters' section of the statepoint call",
4504 Call);
4505 Assert(GCParamArgsStart <= DerivedIndex && DerivedIndex < GCParamArgsEnd,
4506 "gc.relocate: statepoint derived index doesn't fall within the "
4507 "'gc parameters' section of the statepoint call",
4508 Call);
4510 // Relocated value must be either a pointer type or vector-of-pointer type,
4511 // but gc_relocate does not need to return the same pointer type as the
4512 // relocated pointer. It can be casted to the correct type later if it's
4513 // desired. However, they must have the same address space and 'vectorness'
4514 GCRelocateInst &Relocate = cast<GCRelocateInst>(Call);
4515 Assert(Relocate.getDerivedPtr()->getType()->isPtrOrPtrVectorTy(),
4516 "gc.relocate: relocated value must be a gc pointer", Call);
4518 auto ResultType = Call.getType();
4519 auto DerivedType = Relocate.getDerivedPtr()->getType();
4520 Assert(ResultType->isVectorTy() == DerivedType->isVectorTy(),
4521 "gc.relocate: vector relocates to vector and pointer to pointer",
4522 Call);
4523 Assert(
4524 ResultType->getPointerAddressSpace() ==
4525 DerivedType->getPointerAddressSpace(),
4526 "gc.relocate: relocating a pointer shouldn't change its address space",
4527 Call);
4528 break;
4530 case Intrinsic::eh_exceptioncode:
4531 case Intrinsic::eh_exceptionpointer: {
4532 Assert(isa<CatchPadInst>(Call.getArgOperand(0)),
4533 "eh.exceptionpointer argument must be a catchpad", Call);
4534 break;
4536 case Intrinsic::masked_load: {
4537 Assert(Call.getType()->isVectorTy(), "masked_load: must return a vector",
4538 Call);
4540 Value *Ptr = Call.getArgOperand(0);
4541 ConstantInt *Alignment = cast<ConstantInt>(Call.getArgOperand(1));
4542 Value *Mask = Call.getArgOperand(2);
4543 Value *PassThru = Call.getArgOperand(3);
4544 Assert(Mask->getType()->isVectorTy(), "masked_load: mask must be vector",
4545 Call);
4546 Assert(Alignment->getValue().isPowerOf2(),
4547 "masked_load: alignment must be a power of 2", Call);
4549 // DataTy is the overloaded type
4550 Type *DataTy = cast<PointerType>(Ptr->getType())->getElementType();
4551 Assert(DataTy == Call.getType(),
4552 "masked_load: return must match pointer type", Call);
4553 Assert(PassThru->getType() == DataTy,
4554 "masked_load: pass through and data type must match", Call);
4555 Assert(Mask->getType()->getVectorNumElements() ==
4556 DataTy->getVectorNumElements(),
4557 "masked_load: vector mask must be same length as data", Call);
4558 break;
4560 case Intrinsic::masked_store: {
4561 Value *Val = Call.getArgOperand(0);
4562 Value *Ptr = Call.getArgOperand(1);
4563 ConstantInt *Alignment = cast<ConstantInt>(Call.getArgOperand(2));
4564 Value *Mask = Call.getArgOperand(3);
4565 Assert(Mask->getType()->isVectorTy(), "masked_store: mask must be vector",
4566 Call);
4567 Assert(Alignment->getValue().isPowerOf2(),
4568 "masked_store: alignment must be a power of 2", Call);
4570 // DataTy is the overloaded type
4571 Type *DataTy = cast<PointerType>(Ptr->getType())->getElementType();
4572 Assert(DataTy == Val->getType(),
4573 "masked_store: storee must match pointer type", Call);
4574 Assert(Mask->getType()->getVectorNumElements() ==
4575 DataTy->getVectorNumElements(),
4576 "masked_store: vector mask must be same length as data", Call);
4577 break;
4580 case Intrinsic::experimental_guard: {
4581 Assert(isa<CallInst>(Call), "experimental_guard cannot be invoked", Call);
4582 Assert(Call.countOperandBundlesOfType(LLVMContext::OB_deopt) == 1,
4583 "experimental_guard must have exactly one "
4584 "\"deopt\" operand bundle");
4585 break;
4588 case Intrinsic::experimental_deoptimize: {
4589 Assert(isa<CallInst>(Call), "experimental_deoptimize cannot be invoked",
4590 Call);
4591 Assert(Call.countOperandBundlesOfType(LLVMContext::OB_deopt) == 1,
4592 "experimental_deoptimize must have exactly one "
4593 "\"deopt\" operand bundle");
4594 Assert(Call.getType() == Call.getFunction()->getReturnType(),
4595 "experimental_deoptimize return type must match caller return type");
4597 if (isa<CallInst>(Call)) {
4598 auto *RI = dyn_cast<ReturnInst>(Call.getNextNode());
4599 Assert(RI,
4600 "calls to experimental_deoptimize must be followed by a return");
4602 if (!Call.getType()->isVoidTy() && RI)
4603 Assert(RI->getReturnValue() == &Call,
4604 "calls to experimental_deoptimize must be followed by a return "
4605 "of the value computed by experimental_deoptimize");
4608 break;
4610 case Intrinsic::sadd_sat:
4611 case Intrinsic::uadd_sat:
4612 case Intrinsic::ssub_sat:
4613 case Intrinsic::usub_sat: {
4614 Value *Op1 = Call.getArgOperand(0);
4615 Value *Op2 = Call.getArgOperand(1);
4616 Assert(Op1->getType()->isIntOrIntVectorTy(),
4617 "first operand of [us][add|sub]_sat must be an int type or vector "
4618 "of ints");
4619 Assert(Op2->getType()->isIntOrIntVectorTy(),
4620 "second operand of [us][add|sub]_sat must be an int type or vector "
4621 "of ints");
4622 break;
4624 case Intrinsic::smul_fix:
4625 case Intrinsic::smul_fix_sat:
4626 case Intrinsic::umul_fix: {
4627 Value *Op1 = Call.getArgOperand(0);
4628 Value *Op2 = Call.getArgOperand(1);
4629 Assert(Op1->getType()->isIntOrIntVectorTy(),
4630 "first operand of [us]mul_fix[_sat] must be an int type or vector "
4631 "of ints");
4632 Assert(Op2->getType()->isIntOrIntVectorTy(),
4633 "second operand of [us]mul_fix_[sat] must be an int type or vector "
4634 "of ints");
4636 auto *Op3 = cast<ConstantInt>(Call.getArgOperand(2));
4637 Assert(Op3->getType()->getBitWidth() <= 32,
4638 "third argument of [us]mul_fix[_sat] must fit within 32 bits");
4640 if (ID == Intrinsic::smul_fix || ID == Intrinsic::smul_fix_sat) {
4641 Assert(
4642 Op3->getZExtValue() < Op1->getType()->getScalarSizeInBits(),
4643 "the scale of smul_fix[_sat] must be less than the width of the operands");
4644 } else {
4645 Assert(Op3->getZExtValue() <= Op1->getType()->getScalarSizeInBits(),
4646 "the scale of umul_fix[_sat] must be less than or equal to the width of "
4647 "the operands");
4649 break;
4651 case Intrinsic::lround:
4652 case Intrinsic::llround:
4653 case Intrinsic::lrint:
4654 case Intrinsic::llrint: {
4655 Type *ValTy = Call.getArgOperand(0)->getType();
4656 Type *ResultTy = Call.getType();
4657 Assert(!ValTy->isVectorTy() && !ResultTy->isVectorTy(),
4658 "Intrinsic does not support vectors", &Call);
4659 break;
4664 /// Carefully grab the subprogram from a local scope.
4666 /// This carefully grabs the subprogram from a local scope, avoiding the
4667 /// built-in assertions that would typically fire.
4668 static DISubprogram *getSubprogram(Metadata *LocalScope) {
4669 if (!LocalScope)
4670 return nullptr;
4672 if (auto *SP = dyn_cast<DISubprogram>(LocalScope))
4673 return SP;
4675 if (auto *LB = dyn_cast<DILexicalBlockBase>(LocalScope))
4676 return getSubprogram(LB->getRawScope());
4678 // Just return null; broken scope chains are checked elsewhere.
4679 assert(!isa<DILocalScope>(LocalScope) && "Unknown type of local scope");
4680 return nullptr;
4683 void Verifier::visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI) {
4684 unsigned NumOperands = FPI.getNumArgOperands();
4685 bool HasExceptionMD = false;
4686 bool HasRoundingMD = false;
4687 switch (FPI.getIntrinsicID()) {
4688 case Intrinsic::experimental_constrained_sqrt:
4689 case Intrinsic::experimental_constrained_sin:
4690 case Intrinsic::experimental_constrained_cos:
4691 case Intrinsic::experimental_constrained_exp:
4692 case Intrinsic::experimental_constrained_exp2:
4693 case Intrinsic::experimental_constrained_log:
4694 case Intrinsic::experimental_constrained_log10:
4695 case Intrinsic::experimental_constrained_log2:
4696 case Intrinsic::experimental_constrained_rint:
4697 case Intrinsic::experimental_constrained_nearbyint:
4698 case Intrinsic::experimental_constrained_ceil:
4699 case Intrinsic::experimental_constrained_floor:
4700 case Intrinsic::experimental_constrained_round:
4701 case Intrinsic::experimental_constrained_trunc:
4702 Assert((NumOperands == 3), "invalid arguments for constrained FP intrinsic",
4703 &FPI);
4704 HasExceptionMD = true;
4705 HasRoundingMD = true;
4706 break;
4708 case Intrinsic::experimental_constrained_fma:
4709 Assert((NumOperands == 5), "invalid arguments for constrained FP intrinsic",
4710 &FPI);
4711 HasExceptionMD = true;
4712 HasRoundingMD = true;
4713 break;
4715 case Intrinsic::experimental_constrained_fadd:
4716 case Intrinsic::experimental_constrained_fsub:
4717 case Intrinsic::experimental_constrained_fmul:
4718 case Intrinsic::experimental_constrained_fdiv:
4719 case Intrinsic::experimental_constrained_frem:
4720 case Intrinsic::experimental_constrained_pow:
4721 case Intrinsic::experimental_constrained_powi:
4722 case Intrinsic::experimental_constrained_maxnum:
4723 case Intrinsic::experimental_constrained_minnum:
4724 Assert((NumOperands == 4), "invalid arguments for constrained FP intrinsic",
4725 &FPI);
4726 HasExceptionMD = true;
4727 HasRoundingMD = true;
4728 break;
4730 case Intrinsic::experimental_constrained_fptrunc:
4731 case Intrinsic::experimental_constrained_fpext: {
4732 if (FPI.getIntrinsicID() == Intrinsic::experimental_constrained_fptrunc) {
4733 Assert((NumOperands == 3),
4734 "invalid arguments for constrained FP intrinsic", &FPI);
4735 HasRoundingMD = true;
4736 } else {
4737 Assert((NumOperands == 2),
4738 "invalid arguments for constrained FP intrinsic", &FPI);
4740 HasExceptionMD = true;
4742 Value *Operand = FPI.getArgOperand(0);
4743 Type *OperandTy = Operand->getType();
4744 Value *Result = &FPI;
4745 Type *ResultTy = Result->getType();
4746 Assert(OperandTy->isFPOrFPVectorTy(),
4747 "Intrinsic first argument must be FP or FP vector", &FPI);
4748 Assert(ResultTy->isFPOrFPVectorTy(),
4749 "Intrinsic result must be FP or FP vector", &FPI);
4750 Assert(OperandTy->isVectorTy() == ResultTy->isVectorTy(),
4751 "Intrinsic first argument and result disagree on vector use", &FPI);
4752 if (OperandTy->isVectorTy()) {
4753 auto *OperandVecTy = cast<VectorType>(OperandTy);
4754 auto *ResultVecTy = cast<VectorType>(ResultTy);
4755 Assert(OperandVecTy->getNumElements() == ResultVecTy->getNumElements(),
4756 "Intrinsic first argument and result vector lengths must be equal",
4757 &FPI);
4759 if (FPI.getIntrinsicID() == Intrinsic::experimental_constrained_fptrunc) {
4760 Assert(OperandTy->getScalarSizeInBits() > ResultTy->getScalarSizeInBits(),
4761 "Intrinsic first argument's type must be larger than result type",
4762 &FPI);
4763 } else {
4764 Assert(OperandTy->getScalarSizeInBits() < ResultTy->getScalarSizeInBits(),
4765 "Intrinsic first argument's type must be smaller than result type",
4766 &FPI);
4769 break;
4771 default:
4772 llvm_unreachable("Invalid constrained FP intrinsic!");
4775 // If a non-metadata argument is passed in a metadata slot then the
4776 // error will be caught earlier when the incorrect argument doesn't
4777 // match the specification in the intrinsic call table. Thus, no
4778 // argument type check is needed here.
4780 if (HasExceptionMD) {
4781 Assert(FPI.getExceptionBehavior().hasValue(),
4782 "invalid exception behavior argument", &FPI);
4784 if (HasRoundingMD) {
4785 Assert(FPI.getRoundingMode().hasValue(),
4786 "invalid rounding mode argument", &FPI);
4790 void Verifier::visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII) {
4791 auto *MD = cast<MetadataAsValue>(DII.getArgOperand(0))->getMetadata();
4792 AssertDI(isa<ValueAsMetadata>(MD) ||
4793 (isa<MDNode>(MD) && !cast<MDNode>(MD)->getNumOperands()),
4794 "invalid llvm.dbg." + Kind + " intrinsic address/value", &DII, MD);
4795 AssertDI(isa<DILocalVariable>(DII.getRawVariable()),
4796 "invalid llvm.dbg." + Kind + " intrinsic variable", &DII,
4797 DII.getRawVariable());
4798 AssertDI(isa<DIExpression>(DII.getRawExpression()),
4799 "invalid llvm.dbg." + Kind + " intrinsic expression", &DII,
4800 DII.getRawExpression());
4802 // Ignore broken !dbg attachments; they're checked elsewhere.
4803 if (MDNode *N = DII.getDebugLoc().getAsMDNode())
4804 if (!isa<DILocation>(N))
4805 return;
4807 BasicBlock *BB = DII.getParent();
4808 Function *F = BB ? BB->getParent() : nullptr;
4810 // The scopes for variables and !dbg attachments must agree.
4811 DILocalVariable *Var = DII.getVariable();
4812 DILocation *Loc = DII.getDebugLoc();
4813 AssertDI(Loc, "llvm.dbg." + Kind + " intrinsic requires a !dbg attachment",
4814 &DII, BB, F);
4816 DISubprogram *VarSP = getSubprogram(Var->getRawScope());
4817 DISubprogram *LocSP = getSubprogram(Loc->getRawScope());
4818 if (!VarSP || !LocSP)
4819 return; // Broken scope chains are checked elsewhere.
4821 AssertDI(VarSP == LocSP, "mismatched subprogram between llvm.dbg." + Kind +
4822 " variable and !dbg attachment",
4823 &DII, BB, F, Var, Var->getScope()->getSubprogram(), Loc,
4824 Loc->getScope()->getSubprogram());
4826 // This check is redundant with one in visitLocalVariable().
4827 AssertDI(isType(Var->getRawType()), "invalid type ref", Var,
4828 Var->getRawType());
4829 if (auto *Type = dyn_cast_or_null<DIType>(Var->getRawType()))
4830 if (Type->isBlockByrefStruct())
4831 AssertDI(DII.getExpression() && DII.getExpression()->getNumElements(),
4832 "BlockByRef variable without complex expression", Var, &DII);
4834 verifyFnArgs(DII);
4837 void Verifier::visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI) {
4838 AssertDI(isa<DILabel>(DLI.getRawLabel()),
4839 "invalid llvm.dbg." + Kind + " intrinsic variable", &DLI,
4840 DLI.getRawLabel());
4842 // Ignore broken !dbg attachments; they're checked elsewhere.
4843 if (MDNode *N = DLI.getDebugLoc().getAsMDNode())
4844 if (!isa<DILocation>(N))
4845 return;
4847 BasicBlock *BB = DLI.getParent();
4848 Function *F = BB ? BB->getParent() : nullptr;
4850 // The scopes for variables and !dbg attachments must agree.
4851 DILabel *Label = DLI.getLabel();
4852 DILocation *Loc = DLI.getDebugLoc();
4853 Assert(Loc, "llvm.dbg." + Kind + " intrinsic requires a !dbg attachment",
4854 &DLI, BB, F);
4856 DISubprogram *LabelSP = getSubprogram(Label->getRawScope());
4857 DISubprogram *LocSP = getSubprogram(Loc->getRawScope());
4858 if (!LabelSP || !LocSP)
4859 return;
4861 AssertDI(LabelSP == LocSP, "mismatched subprogram between llvm.dbg." + Kind +
4862 " label and !dbg attachment",
4863 &DLI, BB, F, Label, Label->getScope()->getSubprogram(), Loc,
4864 Loc->getScope()->getSubprogram());
4867 void Verifier::verifyFragmentExpression(const DbgVariableIntrinsic &I) {
4868 DILocalVariable *V = dyn_cast_or_null<DILocalVariable>(I.getRawVariable());
4869 DIExpression *E = dyn_cast_or_null<DIExpression>(I.getRawExpression());
4871 // We don't know whether this intrinsic verified correctly.
4872 if (!V || !E || !E->isValid())
4873 return;
4875 // Nothing to do if this isn't a DW_OP_LLVM_fragment expression.
4876 auto Fragment = E->getFragmentInfo();
4877 if (!Fragment)
4878 return;
4880 // The frontend helps out GDB by emitting the members of local anonymous
4881 // unions as artificial local variables with shared storage. When SROA splits
4882 // the storage for artificial local variables that are smaller than the entire
4883 // union, the overhang piece will be outside of the allotted space for the
4884 // variable and this check fails.
4885 // FIXME: Remove this check as soon as clang stops doing this; it hides bugs.
4886 if (V->isArtificial())
4887 return;
4889 verifyFragmentExpression(*V, *Fragment, &I);
4892 template <typename ValueOrMetadata>
4893 void Verifier::verifyFragmentExpression(const DIVariable &V,
4894 DIExpression::FragmentInfo Fragment,
4895 ValueOrMetadata *Desc) {
4896 // If there's no size, the type is broken, but that should be checked
4897 // elsewhere.
4898 auto VarSize = V.getSizeInBits();
4899 if (!VarSize)
4900 return;
4902 unsigned FragSize = Fragment.SizeInBits;
4903 unsigned FragOffset = Fragment.OffsetInBits;
4904 AssertDI(FragSize + FragOffset <= *VarSize,
4905 "fragment is larger than or outside of variable", Desc, &V);
4906 AssertDI(FragSize != *VarSize, "fragment covers entire variable", Desc, &V);
4909 void Verifier::verifyFnArgs(const DbgVariableIntrinsic &I) {
4910 // This function does not take the scope of noninlined function arguments into
4911 // account. Don't run it if current function is nodebug, because it may
4912 // contain inlined debug intrinsics.
4913 if (!HasDebugInfo)
4914 return;
4916 // For performance reasons only check non-inlined ones.
4917 if (I.getDebugLoc()->getInlinedAt())
4918 return;
4920 DILocalVariable *Var = I.getVariable();
4921 AssertDI(Var, "dbg intrinsic without variable");
4923 unsigned ArgNo = Var->getArg();
4924 if (!ArgNo)
4925 return;
4927 // Verify there are no duplicate function argument debug info entries.
4928 // These will cause hard-to-debug assertions in the DWARF backend.
4929 if (DebugFnArgs.size() < ArgNo)
4930 DebugFnArgs.resize(ArgNo, nullptr);
4932 auto *Prev = DebugFnArgs[ArgNo - 1];
4933 DebugFnArgs[ArgNo - 1] = Var;
4934 AssertDI(!Prev || (Prev == Var), "conflicting debug info for argument", &I,
4935 Prev, Var);
4938 void Verifier::verifyCompileUnits() {
4939 // When more than one Module is imported into the same context, such as during
4940 // an LTO build before linking the modules, ODR type uniquing may cause types
4941 // to point to a different CU. This check does not make sense in this case.
4942 if (M.getContext().isODRUniquingDebugTypes())
4943 return;
4944 auto *CUs = M.getNamedMetadata("llvm.dbg.cu");
4945 SmallPtrSet<const Metadata *, 2> Listed;
4946 if (CUs)
4947 Listed.insert(CUs->op_begin(), CUs->op_end());
4948 for (auto *CU : CUVisited)
4949 AssertDI(Listed.count(CU), "DICompileUnit not listed in llvm.dbg.cu", CU);
4950 CUVisited.clear();
4953 void Verifier::verifyDeoptimizeCallingConvs() {
4954 if (DeoptimizeDeclarations.empty())
4955 return;
4957 const Function *First = DeoptimizeDeclarations[0];
4958 for (auto *F : makeArrayRef(DeoptimizeDeclarations).slice(1)) {
4959 Assert(First->getCallingConv() == F->getCallingConv(),
4960 "All llvm.experimental.deoptimize declarations must have the same "
4961 "calling convention",
4962 First, F);
4966 void Verifier::verifySourceDebugInfo(const DICompileUnit &U, const DIFile &F) {
4967 bool HasSource = F.getSource().hasValue();
4968 if (!HasSourceDebugInfo.count(&U))
4969 HasSourceDebugInfo[&U] = HasSource;
4970 AssertDI(HasSource == HasSourceDebugInfo[&U],
4971 "inconsistent use of embedded source");
4974 //===----------------------------------------------------------------------===//
4975 // Implement the public interfaces to this file...
4976 //===----------------------------------------------------------------------===//
4978 bool llvm::verifyFunction(const Function &f, raw_ostream *OS) {
4979 Function &F = const_cast<Function &>(f);
4981 // Don't use a raw_null_ostream. Printing IR is expensive.
4982 Verifier V(OS, /*ShouldTreatBrokenDebugInfoAsError=*/true, *f.getParent());
4984 // Note that this function's return value is inverted from what you would
4985 // expect of a function called "verify".
4986 return !V.verify(F);
4989 bool llvm::verifyModule(const Module &M, raw_ostream *OS,
4990 bool *BrokenDebugInfo) {
4991 // Don't use a raw_null_ostream. Printing IR is expensive.
4992 Verifier V(OS, /*ShouldTreatBrokenDebugInfoAsError=*/!BrokenDebugInfo, M);
4994 bool Broken = false;
4995 for (const Function &F : M)
4996 Broken |= !V.verify(F);
4998 Broken |= !V.verify();
4999 if (BrokenDebugInfo)
5000 *BrokenDebugInfo = V.hasBrokenDebugInfo();
5001 // Note that this function's return value is inverted from what you would
5002 // expect of a function called "verify".
5003 return Broken;
5006 namespace {
5008 struct VerifierLegacyPass : public FunctionPass {
5009 static char ID;
5011 std::unique_ptr<Verifier> V;
5012 bool FatalErrors = true;
5014 VerifierLegacyPass() : FunctionPass(ID) {
5015 initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
5017 explicit VerifierLegacyPass(bool FatalErrors)
5018 : FunctionPass(ID),
5019 FatalErrors(FatalErrors) {
5020 initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
5023 bool doInitialization(Module &M) override {
5024 V = llvm::make_unique<Verifier>(
5025 &dbgs(), /*ShouldTreatBrokenDebugInfoAsError=*/false, M);
5026 return false;
5029 bool runOnFunction(Function &F) override {
5030 if (!V->verify(F) && FatalErrors) {
5031 errs() << "in function " << F.getName() << '\n';
5032 report_fatal_error("Broken function found, compilation aborted!");
5034 return false;
5037 bool doFinalization(Module &M) override {
5038 bool HasErrors = false;
5039 for (Function &F : M)
5040 if (F.isDeclaration())
5041 HasErrors |= !V->verify(F);
5043 HasErrors |= !V->verify();
5044 if (FatalErrors && (HasErrors || V->hasBrokenDebugInfo()))
5045 report_fatal_error("Broken module found, compilation aborted!");
5046 return false;
5049 void getAnalysisUsage(AnalysisUsage &AU) const override {
5050 AU.setPreservesAll();
5054 } // end anonymous namespace
5056 /// Helper to issue failure from the TBAA verification
5057 template <typename... Tys> void TBAAVerifier::CheckFailed(Tys &&... Args) {
5058 if (Diagnostic)
5059 return Diagnostic->CheckFailed(Args...);
5062 #define AssertTBAA(C, ...) \
5063 do { \
5064 if (!(C)) { \
5065 CheckFailed(__VA_ARGS__); \
5066 return false; \
5068 } while (false)
5070 /// Verify that \p BaseNode can be used as the "base type" in the struct-path
5071 /// TBAA scheme. This means \p BaseNode is either a scalar node, or a
5072 /// struct-type node describing an aggregate data structure (like a struct).
5073 TBAAVerifier::TBAABaseNodeSummary
5074 TBAAVerifier::verifyTBAABaseNode(Instruction &I, const MDNode *BaseNode,
5075 bool IsNewFormat) {
5076 if (BaseNode->getNumOperands() < 2) {
5077 CheckFailed("Base nodes must have at least two operands", &I, BaseNode);
5078 return {true, ~0u};
5081 auto Itr = TBAABaseNodes.find(BaseNode);
5082 if (Itr != TBAABaseNodes.end())
5083 return Itr->second;
5085 auto Result = verifyTBAABaseNodeImpl(I, BaseNode, IsNewFormat);
5086 auto InsertResult = TBAABaseNodes.insert({BaseNode, Result});
5087 (void)InsertResult;
5088 assert(InsertResult.second && "We just checked!");
5089 return Result;
5092 TBAAVerifier::TBAABaseNodeSummary
5093 TBAAVerifier::verifyTBAABaseNodeImpl(Instruction &I, const MDNode *BaseNode,
5094 bool IsNewFormat) {
5095 const TBAAVerifier::TBAABaseNodeSummary InvalidNode = {true, ~0u};
5097 if (BaseNode->getNumOperands() == 2) {
5098 // Scalar nodes can only be accessed at offset 0.
5099 return isValidScalarTBAANode(BaseNode)
5100 ? TBAAVerifier::TBAABaseNodeSummary({false, 0})
5101 : InvalidNode;
5104 if (IsNewFormat) {
5105 if (BaseNode->getNumOperands() % 3 != 0) {
5106 CheckFailed("Access tag nodes must have the number of operands that is a "
5107 "multiple of 3!", BaseNode);
5108 return InvalidNode;
5110 } else {
5111 if (BaseNode->getNumOperands() % 2 != 1) {
5112 CheckFailed("Struct tag nodes must have an odd number of operands!",
5113 BaseNode);
5114 return InvalidNode;
5118 // Check the type size field.
5119 if (IsNewFormat) {
5120 auto *TypeSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
5121 BaseNode->getOperand(1));
5122 if (!TypeSizeNode) {
5123 CheckFailed("Type size nodes must be constants!", &I, BaseNode);
5124 return InvalidNode;
5128 // Check the type name field. In the new format it can be anything.
5129 if (!IsNewFormat && !isa<MDString>(BaseNode->getOperand(0))) {
5130 CheckFailed("Struct tag nodes have a string as their first operand",
5131 BaseNode);
5132 return InvalidNode;
5135 bool Failed = false;
5137 Optional<APInt> PrevOffset;
5138 unsigned BitWidth = ~0u;
5140 // We've already checked that BaseNode is not a degenerate root node with one
5141 // operand in \c verifyTBAABaseNode, so this loop should run at least once.
5142 unsigned FirstFieldOpNo = IsNewFormat ? 3 : 1;
5143 unsigned NumOpsPerField = IsNewFormat ? 3 : 2;
5144 for (unsigned Idx = FirstFieldOpNo; Idx < BaseNode->getNumOperands();
5145 Idx += NumOpsPerField) {
5146 const MDOperand &FieldTy = BaseNode->getOperand(Idx);
5147 const MDOperand &FieldOffset = BaseNode->getOperand(Idx + 1);
5148 if (!isa<MDNode>(FieldTy)) {
5149 CheckFailed("Incorrect field entry in struct type node!", &I, BaseNode);
5150 Failed = true;
5151 continue;
5154 auto *OffsetEntryCI =
5155 mdconst::dyn_extract_or_null<ConstantInt>(FieldOffset);
5156 if (!OffsetEntryCI) {
5157 CheckFailed("Offset entries must be constants!", &I, BaseNode);
5158 Failed = true;
5159 continue;
5162 if (BitWidth == ~0u)
5163 BitWidth = OffsetEntryCI->getBitWidth();
5165 if (OffsetEntryCI->getBitWidth() != BitWidth) {
5166 CheckFailed(
5167 "Bitwidth between the offsets and struct type entries must match", &I,
5168 BaseNode);
5169 Failed = true;
5170 continue;
5173 // NB! As far as I can tell, we generate a non-strictly increasing offset
5174 // sequence only from structs that have zero size bit fields. When
5175 // recursing into a contained struct in \c getFieldNodeFromTBAABaseNode we
5176 // pick the field lexically the latest in struct type metadata node. This
5177 // mirrors the actual behavior of the alias analysis implementation.
5178 bool IsAscending =
5179 !PrevOffset || PrevOffset->ule(OffsetEntryCI->getValue());
5181 if (!IsAscending) {
5182 CheckFailed("Offsets must be increasing!", &I, BaseNode);
5183 Failed = true;
5186 PrevOffset = OffsetEntryCI->getValue();
5188 if (IsNewFormat) {
5189 auto *MemberSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
5190 BaseNode->getOperand(Idx + 2));
5191 if (!MemberSizeNode) {
5192 CheckFailed("Member size entries must be constants!", &I, BaseNode);
5193 Failed = true;
5194 continue;
5199 return Failed ? InvalidNode
5200 : TBAAVerifier::TBAABaseNodeSummary(false, BitWidth);
5203 static bool IsRootTBAANode(const MDNode *MD) {
5204 return MD->getNumOperands() < 2;
5207 static bool IsScalarTBAANodeImpl(const MDNode *MD,
5208 SmallPtrSetImpl<const MDNode *> &Visited) {
5209 if (MD->getNumOperands() != 2 && MD->getNumOperands() != 3)
5210 return false;
5212 if (!isa<MDString>(MD->getOperand(0)))
5213 return false;
5215 if (MD->getNumOperands() == 3) {
5216 auto *Offset = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
5217 if (!(Offset && Offset->isZero() && isa<MDString>(MD->getOperand(0))))
5218 return false;
5221 auto *Parent = dyn_cast_or_null<MDNode>(MD->getOperand(1));
5222 return Parent && Visited.insert(Parent).second &&
5223 (IsRootTBAANode(Parent) || IsScalarTBAANodeImpl(Parent, Visited));
5226 bool TBAAVerifier::isValidScalarTBAANode(const MDNode *MD) {
5227 auto ResultIt = TBAAScalarNodes.find(MD);
5228 if (ResultIt != TBAAScalarNodes.end())
5229 return ResultIt->second;
5231 SmallPtrSet<const MDNode *, 4> Visited;
5232 bool Result = IsScalarTBAANodeImpl(MD, Visited);
5233 auto InsertResult = TBAAScalarNodes.insert({MD, Result});
5234 (void)InsertResult;
5235 assert(InsertResult.second && "Just checked!");
5237 return Result;
5240 /// Returns the field node at the offset \p Offset in \p BaseNode. Update \p
5241 /// Offset in place to be the offset within the field node returned.
5243 /// We assume we've okayed \p BaseNode via \c verifyTBAABaseNode.
5244 MDNode *TBAAVerifier::getFieldNodeFromTBAABaseNode(Instruction &I,
5245 const MDNode *BaseNode,
5246 APInt &Offset,
5247 bool IsNewFormat) {
5248 assert(BaseNode->getNumOperands() >= 2 && "Invalid base node!");
5250 // Scalar nodes have only one possible "field" -- their parent in the access
5251 // hierarchy. Offset must be zero at this point, but our caller is supposed
5252 // to Assert that.
5253 if (BaseNode->getNumOperands() == 2)
5254 return cast<MDNode>(BaseNode->getOperand(1));
5256 unsigned FirstFieldOpNo = IsNewFormat ? 3 : 1;
5257 unsigned NumOpsPerField = IsNewFormat ? 3 : 2;
5258 for (unsigned Idx = FirstFieldOpNo; Idx < BaseNode->getNumOperands();
5259 Idx += NumOpsPerField) {
5260 auto *OffsetEntryCI =
5261 mdconst::extract<ConstantInt>(BaseNode->getOperand(Idx + 1));
5262 if (OffsetEntryCI->getValue().ugt(Offset)) {
5263 if (Idx == FirstFieldOpNo) {
5264 CheckFailed("Could not find TBAA parent in struct type node", &I,
5265 BaseNode, &Offset);
5266 return nullptr;
5269 unsigned PrevIdx = Idx - NumOpsPerField;
5270 auto *PrevOffsetEntryCI =
5271 mdconst::extract<ConstantInt>(BaseNode->getOperand(PrevIdx + 1));
5272 Offset -= PrevOffsetEntryCI->getValue();
5273 return cast<MDNode>(BaseNode->getOperand(PrevIdx));
5277 unsigned LastIdx = BaseNode->getNumOperands() - NumOpsPerField;
5278 auto *LastOffsetEntryCI = mdconst::extract<ConstantInt>(
5279 BaseNode->getOperand(LastIdx + 1));
5280 Offset -= LastOffsetEntryCI->getValue();
5281 return cast<MDNode>(BaseNode->getOperand(LastIdx));
5284 static bool isNewFormatTBAATypeNode(llvm::MDNode *Type) {
5285 if (!Type || Type->getNumOperands() < 3)
5286 return false;
5288 // In the new format type nodes shall have a reference to the parent type as
5289 // its first operand.
5290 MDNode *Parent = dyn_cast_or_null<MDNode>(Type->getOperand(0));
5291 if (!Parent)
5292 return false;
5294 return true;
5297 bool TBAAVerifier::visitTBAAMetadata(Instruction &I, const MDNode *MD) {
5298 AssertTBAA(isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallInst>(I) ||
5299 isa<VAArgInst>(I) || isa<AtomicRMWInst>(I) ||
5300 isa<AtomicCmpXchgInst>(I),
5301 "This instruction shall not have a TBAA access tag!", &I);
5303 bool IsStructPathTBAA =
5304 isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3;
5306 AssertTBAA(
5307 IsStructPathTBAA,
5308 "Old-style TBAA is no longer allowed, use struct-path TBAA instead", &I);
5310 MDNode *BaseNode = dyn_cast_or_null<MDNode>(MD->getOperand(0));
5311 MDNode *AccessType = dyn_cast_or_null<MDNode>(MD->getOperand(1));
5313 bool IsNewFormat = isNewFormatTBAATypeNode(AccessType);
5315 if (IsNewFormat) {
5316 AssertTBAA(MD->getNumOperands() == 4 || MD->getNumOperands() == 5,
5317 "Access tag metadata must have either 4 or 5 operands", &I, MD);
5318 } else {
5319 AssertTBAA(MD->getNumOperands() < 5,
5320 "Struct tag metadata must have either 3 or 4 operands", &I, MD);
5323 // Check the access size field.
5324 if (IsNewFormat) {
5325 auto *AccessSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
5326 MD->getOperand(3));
5327 AssertTBAA(AccessSizeNode, "Access size field must be a constant", &I, MD);
5330 // Check the immutability flag.
5331 unsigned ImmutabilityFlagOpNo = IsNewFormat ? 4 : 3;
5332 if (MD->getNumOperands() == ImmutabilityFlagOpNo + 1) {
5333 auto *IsImmutableCI = mdconst::dyn_extract_or_null<ConstantInt>(
5334 MD->getOperand(ImmutabilityFlagOpNo));
5335 AssertTBAA(IsImmutableCI,
5336 "Immutability tag on struct tag metadata must be a constant",
5337 &I, MD);
5338 AssertTBAA(
5339 IsImmutableCI->isZero() || IsImmutableCI->isOne(),
5340 "Immutability part of the struct tag metadata must be either 0 or 1",
5341 &I, MD);
5344 AssertTBAA(BaseNode && AccessType,
5345 "Malformed struct tag metadata: base and access-type "
5346 "should be non-null and point to Metadata nodes",
5347 &I, MD, BaseNode, AccessType);
5349 if (!IsNewFormat) {
5350 AssertTBAA(isValidScalarTBAANode(AccessType),
5351 "Access type node must be a valid scalar type", &I, MD,
5352 AccessType);
5355 auto *OffsetCI = mdconst::dyn_extract_or_null<ConstantInt>(MD->getOperand(2));
5356 AssertTBAA(OffsetCI, "Offset must be constant integer", &I, MD);
5358 APInt Offset = OffsetCI->getValue();
5359 bool SeenAccessTypeInPath = false;
5361 SmallPtrSet<MDNode *, 4> StructPath;
5363 for (/* empty */; BaseNode && !IsRootTBAANode(BaseNode);
5364 BaseNode = getFieldNodeFromTBAABaseNode(I, BaseNode, Offset,
5365 IsNewFormat)) {
5366 if (!StructPath.insert(BaseNode).second) {
5367 CheckFailed("Cycle detected in struct path", &I, MD);
5368 return false;
5371 bool Invalid;
5372 unsigned BaseNodeBitWidth;
5373 std::tie(Invalid, BaseNodeBitWidth) = verifyTBAABaseNode(I, BaseNode,
5374 IsNewFormat);
5376 // If the base node is invalid in itself, then we've already printed all the
5377 // errors we wanted to print.
5378 if (Invalid)
5379 return false;
5381 SeenAccessTypeInPath |= BaseNode == AccessType;
5383 if (isValidScalarTBAANode(BaseNode) || BaseNode == AccessType)
5384 AssertTBAA(Offset == 0, "Offset not zero at the point of scalar access",
5385 &I, MD, &Offset);
5387 AssertTBAA(BaseNodeBitWidth == Offset.getBitWidth() ||
5388 (BaseNodeBitWidth == 0 && Offset == 0) ||
5389 (IsNewFormat && BaseNodeBitWidth == ~0u),
5390 "Access bit-width not the same as description bit-width", &I, MD,
5391 BaseNodeBitWidth, Offset.getBitWidth());
5393 if (IsNewFormat && SeenAccessTypeInPath)
5394 break;
5397 AssertTBAA(SeenAccessTypeInPath, "Did not see access type in access path!",
5398 &I, MD);
5399 return true;
5402 char VerifierLegacyPass::ID = 0;
5403 INITIALIZE_PASS(VerifierLegacyPass, "verify", "Module Verifier", false, false)
5405 FunctionPass *llvm::createVerifierPass(bool FatalErrors) {
5406 return new VerifierLegacyPass(FatalErrors);
5409 AnalysisKey VerifierAnalysis::Key;
5410 VerifierAnalysis::Result VerifierAnalysis::run(Module &M,
5411 ModuleAnalysisManager &) {
5412 Result Res;
5413 Res.IRBroken = llvm::verifyModule(M, &dbgs(), &Res.DebugInfoBroken);
5414 return Res;
5417 VerifierAnalysis::Result VerifierAnalysis::run(Function &F,
5418 FunctionAnalysisManager &) {
5419 return { llvm::verifyFunction(F, &dbgs()), false };
5422 PreservedAnalyses VerifierPass::run(Module &M, ModuleAnalysisManager &AM) {
5423 auto Res = AM.getResult<VerifierAnalysis>(M);
5424 if (FatalErrors && (Res.IRBroken || Res.DebugInfoBroken))
5425 report_fatal_error("Broken module found, compilation aborted!");
5427 return PreservedAnalyses::all();
5430 PreservedAnalyses VerifierPass::run(Function &F, FunctionAnalysisManager &AM) {
5431 auto res = AM.getResult<VerifierAnalysis>(F);
5432 if (res.IRBroken && FatalErrors)
5433 report_fatal_error("Broken function found, compilation aborted!");
5435 return PreservedAnalyses::all();